Skip to content

Commit 4f3ec9c

Browse files
authored
SNOW-1569290 Use 12 bytes for IV in gcm (#1239)
1 parent 27e76c9 commit 4f3ec9c

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

encrypt_util.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"strconv"
1616
)
1717

18+
const gcmIvLengthInBytes = 12
19+
1820
var (
1921
defaultKeyAad = make([]byte, 0)
2022
defaultDataAad = make([]byte, 0)
@@ -305,7 +307,7 @@ func initGcm(encryptionKey []byte) (cipher.AEAD, error) {
305307
if err != nil {
306308
return nil, err
307309
}
308-
return cipher.NewGCMWithNonceSize(block, 16)
310+
return cipher.NewGCM(block)
309311
}
310312

311313
func encryptFileGCM(
@@ -334,13 +336,13 @@ func encryptFileGCM(
334336
}
335337
keySize := len(kek)
336338
fileKey := getSecureRandom(keySize)
337-
keyIv := getSecureRandom(keySize)
339+
keyIv := getSecureRandom(gcmIvLengthInBytes)
338340
encryptedFileKey, err := encryptGCM(keyIv, fileKey, kek, defaultKeyAad)
339341
if err != nil {
340342
return nil, "", err
341343
}
342344

343-
dataIv := getSecureRandom(keySize)
345+
dataIv := getSecureRandom(gcmIvLengthInBytes)
344346
encryptedData, err := encryptGCM(dataIv, plaintext, fileKey, defaultDataAad)
345347
if err != nil {
346348
return nil, "", err

encrypt_util_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,11 @@ func generateKLinesOfNFiles(k int, n int, compress bool, tmpDir string) (string,
240240

241241
func TestEncryptDecryptGCM(t *testing.T) {
242242
input := []byte("abc")
243-
iv := []byte("abcdef1234567890") // pragma: allowlist secret
243+
iv := []byte("ab1234567890") // pragma: allowlist secret
244244
key := []byte("1234567890abcdef") // pragma: allowlist secret
245245
encrypted, err := encryptGCM(iv, input, key, nil)
246246
assertNilF(t, err)
247-
assertEqualE(t, base64.StdEncoding.EncodeToString(encrypted), "pgs/wjNH2TYekmN7mbhFjeHH0A==")
247+
assertEqualE(t, base64.StdEncoding.EncodeToString(encrypted), "iG+lT4o27hkzj3kblYRzQikLVQ==")
248248

249249
decrypted, err := decryptGCM(iv, encrypted, key, nil)
250250
assertNilF(t, err)

0 commit comments

Comments
 (0)