Skip to content

Commit f63e55b

Browse files
katiehockmandmitshur
authored andcommitted
[release-branch.go1.14] crypto/cipher: require non-zero nonce size for AES-GCM
Also fix typo in crypto/cipher/gcm_test.go. Updates #37118 Fixes #37416 Change-Id: I8544d1eeeb1f0336cebb977b8c5bfa5e4c5ad8c7 Reviewed-on: https://go-review.googlesource.com/c/go/+/218500 Run-TryBot: Katie Hockman <katie@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Filippo Valsorda <filippo@golang.org> (cherry picked from commit 4e8badb) Reviewed-on: https://go-review.googlesource.com/c/go/+/220651 Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Alexander Rakoczy <alex@golang.org>
1 parent 17acbdb commit f63e55b

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/crypto/cipher/gcm.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ func NewGCM(cipher Block) (AEAD, error) {
8686
}
8787

8888
// NewGCMWithNonceSize returns the given 128-bit, block cipher wrapped in Galois
89-
// Counter Mode, which accepts nonces of the given length.
89+
// Counter Mode, which accepts nonces of the given length. The length must not
90+
// be zero.
9091
//
9192
// Only use this function if you require compatibility with an existing
9293
// cryptosystem that uses non-standard nonce lengths. All other users should use
@@ -112,6 +113,10 @@ func newGCMWithNonceAndTagSize(cipher Block, nonceSize, tagSize int) (AEAD, erro
112113
return nil, errors.New("cipher: incorrect tag size given to GCM")
113114
}
114115

116+
if nonceSize <= 0 {
117+
return nil, errors.New("cipher: the nonce can't have zero length, or the security of the key will be immediately compromised")
118+
}
119+
115120
if cipher, ok := cipher.(gcmAble); ok {
116121
return cipher.NewGCM(nonceSize, tagSize)
117122
}

src/crypto/cipher/gcm_test.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,13 @@ var aesGCMTests = []struct {
217217
"2b9680b886b3efb7c6354b38c63b5373",
218218
"e2b7e5ed5ff27fc8664148f5a628a46dcbf2015184fffb82f2651c36",
219219
},
220+
{
221+
"11754cd72aec309bf52f7687212e8957",
222+
"",
223+
"",
224+
"",
225+
"250327c674aaf477aef2675748cf6971",
226+
},
220227
}
221228

222229
func TestAESGCM(t *testing.T) {
@@ -234,14 +241,22 @@ func TestAESGCM(t *testing.T) {
234241

235242
var aesgcm cipher.AEAD
236243
switch {
237-
// Handle non-standard nonce sizes
244+
// Handle non-standard tag sizes
238245
case tagSize != 16:
239246
aesgcm, err = cipher.NewGCMWithTagSize(aes, tagSize)
240247
if err != nil {
241248
t.Fatal(err)
242249
}
243250

244-
// Handle non-standard tag sizes
251+
// Handle 0 nonce size (expect error and continue)
252+
case len(nonce) == 0:
253+
aesgcm, err = cipher.NewGCMWithNonceSize(aes, 0)
254+
if err == nil {
255+
t.Fatal("expected error for zero nonce size")
256+
}
257+
continue
258+
259+
// Handle non-standard nonce sizes
245260
case len(nonce) != 12:
246261
aesgcm, err = cipher.NewGCMWithNonceSize(aes, len(nonce))
247262
if err != nil {

0 commit comments

Comments
 (0)