Skip to content

Commit f77901b

Browse files
committed
adjust API
1 parent 17e4f90 commit f77901b

24 files changed

Lines changed: 213 additions & 215 deletions

conn.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -726,14 +726,13 @@ func (c *Conn) processHandshakePacket(pkt *packet, dtlsHandshake *handshake.Hand
726726
SequenceNumber: pkt.record.Header.SequenceNumber,
727727
}
728728

729-
hs := recordlayer.FixedHeaderSize + len(cidHeader.ConnectionID)
730-
rawPacket = make([]byte, hs+len(rawInner))
731-
err = cidHeader.MarshalInto(rawPacket)
729+
rawPacket = make([]byte, cidHeader.MarshalSize()+len(rawInner))
730+
_, err = cidHeader.MarshalTo(rawPacket)
732731
if err != nil {
733732
return nil, err
734733
}
735734
pkt.record.Header = *cidHeader
736-
copy(rawPacket[hs:], rawInner)
735+
copy(rawPacket[cidHeader.MarshalSize():], rawInner)
737736
} else {
738737
recordlayerHeader := &recordlayer.Header{
739738
Version: pkt.record.Header.Version,
@@ -743,15 +742,14 @@ func (c *Conn) processHandshakePacket(pkt *packet, dtlsHandshake *handshake.Hand
743742
SequenceNumber: seq,
744743
}
745744

746-
hs := recordlayer.FixedHeaderSize + len(recordlayerHeader.ConnectionID)
747-
rawPacket = make([]byte, hs+len(handshakeFragment))
748-
err = recordlayerHeader.MarshalInto(rawPacket)
745+
rawPacket = make([]byte, recordlayerHeader.MarshalSize()+len(handshakeFragment))
746+
_, err = recordlayerHeader.MarshalTo(rawPacket)
749747
if err != nil {
750748
return nil, err
751749
}
752750

753751
pkt.record.Header = *recordlayerHeader
754-
copy(rawPacket[hs:], handshakeFragment)
752+
copy(rawPacket[recordlayerHeader.MarshalSize():], handshakeFragment)
755753
}
756754

757755
if pkt.shouldEncrypt {
@@ -797,7 +795,7 @@ func (c *Conn) fragmentHandshake(dtlsHandshake *handshake.Handshake) ([][]byte,
797795
offset += contentFragmentLen
798796

799797
fragmentedHandshake := make([]byte, handshake.HeaderLength+len(contentFragment))
800-
err := headerFragment.MarshalInto(fragmentedHandshake)
798+
_, err := headerFragment.MarshalTo(fragmentedHandshake)
801799
if err != nil {
802800
return nil, err
803801
}
@@ -1011,7 +1009,7 @@ func (c *Conn) handleIncomingPacket(
10111009
if header.ContentType == protocol.ContentTypeConnectionID {
10121010
originalCID = true
10131011
ip := &recordlayer.InnerPlaintext{}
1014-
if err := ip.Unmarshal(buf[header.Size():]); err != nil { //nolint:govet
1012+
if err := ip.Unmarshal(buf[header.MarshalSize():]); err != nil { //nolint:govet
10151013
c.log.Debugf("unpacking inner plaintext failed: %s", err)
10161014

10171015
return false, false, nil, nil

pkg/crypto/ciphersuite/cbc.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ func NewCBC(
6868

6969
// Encrypt encrypt a DTLS RecordLayer message.
7070
func (c *CBC) Encrypt(pkt *recordlayer.RecordLayer, raw []byte) ([]byte, error) {
71-
payload := raw[pkt.Header.Size():]
72-
raw = raw[:pkt.Header.Size()]
71+
payload := raw[pkt.Header.MarshalSize():]
72+
raw = raw[:pkt.Header.MarshalSize()]
7373
blockSize := c.writeCBC.BlockSize()
7474

7575
// Generate + Append MAC
@@ -110,7 +110,7 @@ func (c *CBC) Encrypt(pkt *recordlayer.RecordLayer, raw []byte) ([]byte, error)
110110
raw = append(raw, payload...)
111111

112112
// Update recordLayer size to include IV+MAC+Padding
113-
binary.BigEndian.PutUint16(raw[pkt.Header.Size()-2:], uint16(len(raw)-pkt.Header.Size())) //nolint:gosec //G115
113+
binary.BigEndian.PutUint16(raw[pkt.Header.MarshalSize()-2:], uint16(len(raw)-pkt.Header.MarshalSize())) //nolint:gosec //G115
114114

115115
return raw, nil
116116
}
@@ -123,7 +123,7 @@ func (c *CBC) Decrypt(header recordlayer.Header, in []byte) ([]byte, error) {
123123
if err := header.Unmarshal(in); err != nil {
124124
return nil, err
125125
}
126-
body := in[header.Size():]
126+
body := in[header.MarshalSize():]
127127

128128
switch {
129129
case header.ContentType == protocol.ContentTypeChangeCipherSpec:
@@ -171,7 +171,7 @@ func (c *CBC) Decrypt(header recordlayer.Header, in []byte) ([]byte, error) {
171171
return nil, errInvalidMAC
172172
}
173173

174-
return append(in[:header.Size()], body[:dataEnd]...), nil
174+
return append(in[:header.MarshalSize()], body[:dataEnd]...), nil
175175
}
176176

177177
func (c *CBC) hmac(

pkg/crypto/ciphersuite/chacha20poly1305.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ func NewChaCha20Poly1305(localKey, localWriteIV, remoteKey, remoteWriteIV []byte
5151

5252
// Encrypt encrypts a DTLS RecordLayer message.
5353
func (c *ChaCha20Poly1305) Encrypt(pkt *recordlayer.RecordLayer, raw []byte) ([]byte, error) {
54-
payload := raw[pkt.Header.Size():]
55-
raw = raw[:pkt.Header.Size()]
54+
payload := raw[pkt.Header.MarshalSize():]
55+
raw = raw[:pkt.Header.MarshalSize()]
5656

5757
var nonce [chachaNonceLength]byte
5858
copy(nonce[:], c.localWriteIV)
@@ -80,7 +80,7 @@ func (c *ChaCha20Poly1305) Encrypt(pkt *recordlayer.RecordLayer, raw []byte) ([]
8080
copy(result, raw)
8181
copy(result[len(raw):], encrypted)
8282

83-
binary.BigEndian.PutUint16(result[pkt.Header.Size()-2:], uint16(len(encrypted))) //nolint:gosec
83+
binary.BigEndian.PutUint16(result[pkt.Header.MarshalSize()-2:], uint16(len(encrypted))) //nolint:gosec
8484

8585
return result, nil
8686
}
@@ -108,7 +108,7 @@ func (c *ChaCha20Poly1305) Decrypt(header recordlayer.Header, in []byte) ([]byte
108108
}
109109

110110
// NOTE: ChaCha20-Poly1305 has NO explicit nonce in the record
111-
ciphertext := in[header.Size():]
111+
ciphertext := in[header.MarshalSize():]
112112

113113
var additionalData []byte
114114
if header.ContentType == protocol.ContentTypeConnectionID {
@@ -122,5 +122,5 @@ func (c *ChaCha20Poly1305) Decrypt(header recordlayer.Header, in []byte) ([]byte
122122
return nil, fmt.Errorf("%w: %v", errDecryptPacket, err) //nolint:errorlint
123123
}
124124

125-
return append(in[:header.Size()], plaintext...), nil
125+
return append(in[:header.MarshalSize()], plaintext...), nil
126126
}

pkg/crypto/ciphersuite/ciphersuite.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ func newAEAD(
7373

7474
// encrypt encrypts a DTLS RecordLayer message.
7575
func (a *aead) encrypt(pkt *recordlayer.RecordLayer, raw []byte) ([]byte, error) {
76-
payload := raw[pkt.Header.Size():]
77-
raw = raw[:pkt.Header.Size()]
76+
payload := raw[pkt.Header.MarshalSize():]
77+
raw = raw[:pkt.Header.MarshalSize()]
7878

7979
// Get nonce buffer from pool
8080
noncePtr := a.nonceBufferPool.Get().(*[]byte) // nolint:forcetypeassert
@@ -100,7 +100,7 @@ func (a *aead) encrypt(pkt *recordlayer.RecordLayer, raw []byte) ([]byte, error)
100100
a.localAEAD.Seal(r[len(raw)+8:len(raw)+8], nonce, payload, additionalData)
101101

102102
// Update recordLayer size to include explicit nonce
103-
binary.BigEndian.PutUint16(r[pkt.Header.Size()-2:], uint16(len(r)-pkt.Header.Size())) //nolint:gosec //G115
103+
binary.BigEndian.PutUint16(r[pkt.Header.MarshalSize()-2:], uint16(len(r)-pkt.Header.MarshalSize())) //nolint:gosec //G115
104104

105105
// Return nonce buffer to pool
106106
a.nonceBufferPool.Put(noncePtr)
@@ -117,7 +117,7 @@ func (a *aead) decrypt(header recordlayer.Header, in []byte) ([]byte, error) {
117117
case header.ContentType == protocol.ContentTypeChangeCipherSpec:
118118
// Nothing to encrypt with ChangeCipherSpec
119119
return in, nil
120-
case len(in) <= (8 + header.Size()):
120+
case len(in) <= (8 + header.MarshalSize()):
121121
return nil, errNotEnoughRoomForNonce
122122
}
123123

@@ -126,8 +126,8 @@ func (a *aead) decrypt(header recordlayer.Header, in []byte) ([]byte, error) {
126126
nonce := *noncePtr
127127

128128
copy(nonce[:4], a.remoteWriteIV[:4])
129-
copy(nonce[4:], in[header.Size():header.Size()+8])
130-
out := in[header.Size()+8:]
129+
copy(nonce[4:], in[header.MarshalSize():header.MarshalSize()+8])
130+
out := in[header.MarshalSize()+8:]
131131

132132
var additionalData []byte
133133
if header.ContentType == protocol.ContentTypeConnectionID {
@@ -146,7 +146,7 @@ func (a *aead) decrypt(header recordlayer.Header, in []byte) ([]byte, error) {
146146
// Return nonce buffer to pool
147147
a.nonceBufferPool.Put(noncePtr)
148148

149-
return append(in[:header.Size()], out...), nil
149+
return append(in[:header.MarshalSize()], out...), nil
150150
}
151151

152152
func generateAEADAdditionalData(h *recordlayer.Header, payloadLen int) []byte {

pkg/protocol/alert/alert.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,28 +145,28 @@ func (a Alert) ContentType() protocol.ContentType {
145145
return protocol.ContentTypeAlert
146146
}
147147

148-
// Size returns the minimal buffer size required for MarshalInto.
149-
func (a Alert) Size() int {
148+
// MarshalSize returns the minimal buffer size required for MarshalTo.
149+
func (a Alert) MarshalSize() int {
150150
return 2
151151
}
152152

153153
// Marshal returns the encoded alert.
154154
func (a *Alert) Marshal() ([]byte, error) {
155-
out := make([]byte, a.Size())
156-
err := a.MarshalInto(out)
155+
out := make([]byte, a.MarshalSize())
156+
_, err := a.MarshalTo(out)
157157

158158
return out, err
159159
}
160160

161-
// MarshalInto returns the encoded alert.
162-
func (a *Alert) MarshalInto(out []byte) error {
163-
if len(out) < a.Size() {
164-
return errBufferTooSmall
161+
// MarshalTo returns the encoded alert.
162+
func (a *Alert) MarshalTo(out []byte) (int, error) {
163+
if len(out) < a.MarshalSize() {
164+
return 0, errBufferTooSmall
165165
}
166166
out[0] = byte(a.Level)
167167
out[1] = byte(a.Description)
168168

169-
return nil
169+
return 2, nil
170170
}
171171

172172
// Unmarshal populates the alert from binary data.

pkg/protocol/application_data.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@ func (a ApplicationData) ContentType() ContentType {
2020
// Marshal encodes the ApplicationData to binary.
2121
func (a *ApplicationData) Marshal() ([]byte, error) {
2222
out := make([]byte, len(a.Data))
23-
err := a.MarshalInto(out)
23+
_, err := a.MarshalTo(out)
2424

2525
return out, err
2626
}
2727

28-
// MarshalInto encodes the ApplicationData to binary into a pre-allocated buffer.
29-
func (a *ApplicationData) MarshalInto(out []byte) error {
28+
// MarshalTo encodes the ApplicationData to binary into a pre-allocated buffer.
29+
func (a *ApplicationData) MarshalTo(out []byte) (int, error) {
3030
copy(out, a.Data)
3131

32-
return nil
32+
return len(a.Data), nil
3333
}
3434

35-
// Size returns the size required for MarshalInto.
36-
func (a ApplicationData) Size() int {
35+
// MarshalSize returns the size required for MarshalTo.
36+
func (a ApplicationData) MarshalSize() int {
3737
return len(a.Data)
3838
}
3939

pkg/protocol/change_cipher_spec.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,27 @@ func (c ChangeCipherSpec) ContentType() ContentType {
1515
return ContentTypeChangeCipherSpec
1616
}
1717

18-
// Size returns the minimal buffer size required for MarshalInto.
19-
func (c ChangeCipherSpec) Size() int {
18+
// MarshalSize returns the minimal buffer size required for MarshalTo.
19+
func (c ChangeCipherSpec) MarshalSize() int {
2020
return 1
2121
}
2222

2323
// Marshal encodes the ChangeCipherSpec to binary.
2424
func (c *ChangeCipherSpec) Marshal() ([]byte, error) {
2525
out := make([]byte, 1)
26-
err := c.MarshalInto(out)
26+
_, err := c.MarshalTo(out)
2727

2828
return out, err
2929
}
3030

31-
// MarshalInto encodes the ChangeCipherSpec to binary into a pre-allocated buffer.
32-
func (c *ChangeCipherSpec) MarshalInto(out []byte) error {
33-
if len(out) < c.Size() {
34-
return errBufferTooSmall
31+
// MarshalTo encodes the ChangeCipherSpec to binary into a pre-allocated buffer.
32+
func (c *ChangeCipherSpec) MarshalTo(out []byte) (int, error) {
33+
if len(out) < c.MarshalSize() {
34+
return 0, errBufferTooSmall
3535
}
3636
out[0] = 0x01
3737

38-
return nil
38+
return 1, nil
3939
}
4040

4141
// Unmarshal populates the ChangeCipherSpec from binary.

pkg/protocol/content.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const (
2121
type Content interface {
2222
ContentType() ContentType
2323
Marshal() ([]byte, error)
24-
MarshalInto([]byte) error
24+
MarshalTo([]byte) (int, error)
2525
Unmarshal(data []byte) error
26-
Size() int
26+
MarshalSize() int
2727
}

pkg/protocol/handshake/handshake.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ func (t Type) String() string { //nolint:cyclop
6262
// Message is the body of a Handshake datagram.
6363
type Message interface {
6464
Marshal() ([]byte, error)
65-
MarshalInto([]byte) error
66-
Size() int
65+
MarshalTo([]byte) (int, error)
66+
MarshalSize() int
6767
Unmarshal(data []byte) error
6868
Type() Type
6969
}
@@ -86,9 +86,9 @@ func (h Handshake) ContentType() protocol.ContentType {
8686
return protocol.ContentTypeHandshake
8787
}
8888

89-
// Size returns the minimal buffer size required for MarshalInto.
90-
func (h *Handshake) Size() int {
91-
return HeaderLength + h.Message.Size()
89+
// MarshalSize returns the minimal buffer size required for MarshalTo.
90+
func (h *Handshake) MarshalSize() int {
91+
return HeaderLength + h.Message.MarshalSize()
9292
}
9393

9494
// Marshal encodes a handshake into a binary message.
@@ -98,38 +98,38 @@ func (h *Handshake) Marshal() ([]byte, error) {
9898
} else if h.Header.FragmentOffset != 0 {
9999
return nil, errUnableToMarshalFragmented
100100
}
101-
out := make([]byte, h.Size())
102-
err := h.MarshalInto(out)
101+
out := make([]byte, h.MarshalSize())
102+
_, err := h.MarshalTo(out)
103103

104104
return out, err
105105
}
106106

107-
// MarshalInto encodes a handshake into a binary message into a pre-allocated buffer.
108-
func (h *Handshake) MarshalInto(out []byte) error {
107+
// MarshalTo encodes a handshake into a binary message into a pre-allocated buffer.
108+
func (h *Handshake) MarshalTo(out []byte) (int, error) {
109109
if h.Message == nil {
110-
return errHandshakeMessageUnset
110+
return 0, errHandshakeMessageUnset
111111
} else if h.Header.FragmentOffset != 0 {
112-
return errUnableToMarshalFragmented
112+
return 0, errUnableToMarshalFragmented
113113
}
114114

115-
if len(out) < h.Size() {
116-
return errBufferTooSmall
115+
if len(out) < h.MarshalSize() {
116+
return 0, errBufferTooSmall
117117
}
118118

119-
h.Header.Length = uint32(h.Message.Size()) //nolint:gosec // G115
119+
h.Header.Length = uint32(h.Message.MarshalSize()) //nolint:gosec // G115
120120
h.Header.FragmentLength = h.Header.Length
121121
h.Header.Type = h.Message.Type()
122-
err := h.Header.MarshalInto(out)
122+
_, err := h.Header.MarshalTo(out)
123123
if err != nil {
124-
return err
124+
return 0, err
125125
}
126126

127-
err = h.Message.MarshalInto(out[HeaderLength:])
127+
_, err = h.Message.MarshalTo(out[HeaderLength:])
128128
if err != nil {
129-
return err
129+
return 0, err
130130
}
131131

132-
return nil
132+
return h.MarshalSize(), nil
133133
}
134134

135135
// Unmarshal decodes a handshake from a binary message.

pkg/protocol/handshake/header.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ type Header struct {
2929
// Marshal encodes the Header.
3030
func (h *Header) Marshal() ([]byte, error) {
3131
out := make([]byte, HeaderLength)
32-
err := h.MarshalInto(out)
32+
_, err := h.MarshalTo(out)
3333

3434
return out, err
3535
}
3636

37-
// MarshalInto encodes the Header using a pre-allocated buffer.
38-
func (h *Header) MarshalInto(out []byte) error {
37+
// MarshalTo encodes the Header using a pre-allocated buffer.
38+
func (h *Header) MarshalTo(out []byte) (int, error) {
3939
if len(out) < HeaderLength {
40-
return errBufferTooSmall
40+
return 0, errBufferTooSmall
4141
}
4242

4343
out[0] = byte(h.Type)
@@ -46,7 +46,7 @@ func (h *Header) MarshalInto(out []byte) error {
4646
util.PutBigEndianUint24(out[6:], h.FragmentOffset)
4747
util.PutBigEndianUint24(out[9:], h.FragmentLength)
4848

49-
return nil
49+
return HeaderLength, nil
5050
}
5151

5252
// Unmarshal populates the header from encoded data.

0 commit comments

Comments
 (0)