Skip to content

Commit 33b5efb

Browse files
committed
Implement AddUint48 utility
Adds the AddUint48 utility used to handle sequence numbers in DTLS records. This should be removed when golang/crypto#265 is implemented. Signed-off-by: Daniel Mangum <[email protected]>
1 parent f1dda16 commit 33b5efb

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

internal/util/util.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package util
66

77
import (
88
"encoding/binary"
9+
10+
"golang.org/x/crypto/cryptobyte"
911
)
1012

1113
// BigEndianUint24 returns the value of a big endian uint24
@@ -40,3 +42,10 @@ func Max(a, b int) int {
4042
}
4143
return b
4244
}
45+
46+
// AddUint48 appends a big-endian, 48-bit value to the byte string.
47+
// Remove if / when https://github.com/golang/crypto/pull/265 is merged
48+
// upstream.
49+
func AddUint48(b *cryptobyte.Builder, v uint64) {
50+
b.AddBytes([]byte{byte(v >> 40), byte(v >> 32), byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(v)})
51+
}

internal/util/util_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
2+
// SPDX-License-Identifier: MIT
3+
4+
package util
5+
6+
import (
7+
"bytes"
8+
"testing"
9+
10+
"golang.org/x/crypto/cryptobyte"
11+
)
12+
13+
func TestAddUint48(t *testing.T) {
14+
cases := map[string]struct {
15+
reason string
16+
builder *cryptobyte.Builder
17+
postAdd func(*cryptobyte.Builder)
18+
in uint64
19+
want []byte
20+
}{
21+
"OnlyUint48": {
22+
reason: "Adding only a 48-bit unsigned integer should yield expected result.",
23+
builder: &cryptobyte.Builder{},
24+
in: 0xfefcff3cfdfc,
25+
want: []byte{254, 252, 255, 60, 253, 252},
26+
},
27+
"ExistingAddUint48": {
28+
reason: "Adding a 48-bit unsigned integer to a builder with existing bytes should yield expected result.",
29+
builder: func() *cryptobyte.Builder {
30+
var b cryptobyte.Builder
31+
b.AddUint64(0xffffffffffffffff)
32+
return &b
33+
}(),
34+
in: 0xfefcff3cfdfc,
35+
want: []byte{255, 255, 255, 255, 255, 255, 255, 255, 254, 252, 255, 60, 253, 252},
36+
},
37+
"ExistingAddUint48AndMore": {
38+
reason: "Adding a 48-bit unsigned integer to a builder with existing bytes, then adding more bytes, should yield expected result.",
39+
builder: func() *cryptobyte.Builder {
40+
var b cryptobyte.Builder
41+
b.AddUint64(0xffffffffffffffff)
42+
return &b
43+
}(),
44+
postAdd: func(b *cryptobyte.Builder) {
45+
b.AddUint32(0xffffffff)
46+
},
47+
in: 0xfefcff3cfdfc,
48+
want: []byte{255, 255, 255, 255, 255, 255, 255, 255, 254, 252, 255, 60, 253, 252, 255, 255, 255, 255},
49+
},
50+
}
51+
for name, tc := range cases {
52+
t.Run(name, func(t *testing.T) {
53+
AddUint48(tc.builder, tc.in)
54+
if tc.postAdd != nil {
55+
tc.postAdd(tc.builder)
56+
}
57+
got := tc.builder.BytesOrPanic()
58+
if !bytes.Equal(got, tc.want) {
59+
t.Errorf("Bytes() = %v, want %v", got, tc.want)
60+
}
61+
})
62+
}
63+
}

0 commit comments

Comments
 (0)