Skip to content

Commit 61762de

Browse files
authored
Use sequence number for nonce in GCM ciphers (#796)
1 parent 46ee7c3 commit 61762de

1 file changed

Lines changed: 3 additions & 6 deletions

File tree

pkg/crypto/ciphersuite/ciphersuite.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package ciphersuite
66

77
import (
88
"crypto/cipher"
9-
"crypto/rand"
109
"encoding/binary"
1110
"errors"
1211
"fmt"
@@ -83,12 +82,10 @@ func (a *aead) encrypt(pkt *recordlayer.RecordLayer, raw []byte) ([]byte, error)
8382
nonce := *noncePtr
8483

8584
copy(nonce, a.localWriteIV[:4])
86-
if _, err := rand.Read(nonce[4:]); err != nil {
87-
// Return nonce buffer to pool
88-
a.nonceBufferPool.Put(noncePtr)
8985

90-
return nil, err
91-
}
86+
// https://www.rfc-editor.org/rfc/rfc9325#name-nonce-reuse-in-tls-12
87+
seq64 := (uint64(pkt.Header.Epoch) << 48) | (pkt.Header.SequenceNumber & 0x0000ffffffffffff)
88+
binary.BigEndian.PutUint64(nonce[4:], seq64)
9289

9390
var additionalData []byte
9491
if pkt.Header.ContentType == protocol.ContentTypeConnectionID {

0 commit comments

Comments
 (0)