Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For Windows CNG, fix TLS 1.3 additionalData length check #52

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions cng/aes.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,14 @@ func (x *cbcCipher) SetIV(iv []byte) {
const (
gcmTagSize = 16
gcmStandardNonceSize = 12
gcmTlsAddSize = 13
// TLS 1.2 additional data is constructed as:
//
// additional_data = seq_num(8) + TLSCompressed.type(1) + TLSCompressed.version(2) + TLSCompressed.length(2);
gcmTls12AddSize = 13
// TLS 1.3 additional data is constructed as:
//
// additional_data = TLSCiphertext.opaque_type(1) || TLSCiphertext.legacy_record_version(2) || TLSCiphertext.length(2)
gcmTls13AddSize = 5
gcmTlsFixedNonceSize = 4
)

Expand Down Expand Up @@ -261,8 +268,10 @@ func (g *aesGCM) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
panic("cipher: message too large for buffer")
}
if g.tls != cipherGCMTLSNone {
if len(additionalData) != gcmTlsAddSize {
panic("cipher: incorrect additional data length given to GCM TLS")
if g.tls == cipherGCMTLS12 && len(additionalData) != gcmTls12AddSize {
panic("cipher: incorrect additional data length given to GCM TLS 1.2")
} else if g.tls == cipherGCMTLS13 && len(additionalData) != gcmTls13AddSize {
panic("cipher: incorrect additional data length given to GCM TLS 1.3")
}
counter := bigUint64(nonce[gcmTlsFixedNonceSize:])
if g.tls == cipherGCMTLS13 {
Expand Down
28 changes: 20 additions & 8 deletions cng/aes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ func TestSealAndOpen(t *testing.T) {
func TestSealAndOpenTLS(t *testing.T) {
tests := []struct {
name string
new func(c cipher.Block) (cipher.AEAD, error)
tls string
mask func(n *[12]byte)
}{
{"1.2", NewGCMTLS, nil},
{"1.3", NewGCMTLS13, nil},
{"1.3_masked", NewGCMTLS13, func(n *[12]byte) {
{"1.2", "1.2", nil},
{"1.3", "1.3", nil},
{"1.3_masked", "1.3", func(n *[12]byte) {
// Arbitrary mask in the high bits.
n[9] ^= 0x42
// Mask the very first bit. This makes sure that if Seal doesn't
Expand All @@ -93,7 +93,13 @@ func TestSealAndOpenTLS(t *testing.T) {
if err != nil {
t.Fatal(err)
}
gcm, err := tt.new(ci)
var gcm cipher.AEAD
switch tt.tls {
case "1.2":
gcm, err = NewGCMTLS(ci)
case "1.3":
gcm, err = NewGCMTLS13(ci)
}
if err != nil {
t.Fatal(err)
}
Expand All @@ -108,9 +114,15 @@ func TestSealAndOpenTLS(t *testing.T) {
}
}
plainText := []byte{0x01, 0x02, 0x03}
additionalData := make([]byte, 13)
additionalData[11] = byte(len(plainText) >> 8)
additionalData[12] = byte(len(plainText))
var additionalData []byte
switch tt.tls {
case "1.2":
additionalData = make([]byte, 13)
case "1.3":
additionalData = []byte{23, 3, 3, 0, 0}
}
additionalData[len(additionalData)-2] = byte(len(plainText) >> 8)
additionalData[len(additionalData)-1] = byte(len(plainText))
sealed := gcm.Seal(nil, nonce[:], plainText, additionalData)
assertPanic(t, func() {
gcm.Seal(nil, nonce[:], plainText, additionalData)
Expand Down
Loading