@@ -1256,5 +1256,105 @@ func main() {
12561256 block, _ := aes.NewCipher([]byte("12345678123456781234567812345678"))
12571257 _ = cipher.NewCTR(block, iv)
12581258}
1259+ ` }, 1 , gosec .NewConfig ()},
1260+
1261+ // Decryption tests - should NOT be flagged as decryption uses the same nonce as encryption
1262+ {[]string {`package main
1263+
1264+ import (
1265+ "crypto/aes"
1266+ "crypto/cipher"
1267+ )
1268+
1269+ func Decrypt(data []byte, key [32]byte) ([]byte, error) {
1270+ block, _ := aes.NewCipher(key[:32])
1271+ gcm, _ := cipher.NewGCM(block)
1272+ // Using a hardcoded nonce for DECRYPTION is safe - must match encryption nonce
1273+ nonce := []byte("ILoveMyNonce")
1274+ return gcm.Open(nil, nonce, data[gcm.NonceSize():], nil)
1275+ }
1276+ ` }, 0 , gosec .NewConfig ()},
1277+
1278+ {[]string {`package main
1279+
1280+ import (
1281+ "crypto/aes"
1282+ "crypto/cipher"
1283+ )
1284+
1285+ func main() {
1286+ block, _ := aes.NewCipher([]byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})
1287+ aesGCM, _ := cipher.NewGCM(block)
1288+
1289+ // Encrypt with hardcoded nonce - SHOULD be flagged
1290+ cipherText := aesGCM.Seal(nil, []byte("ILoveMyNonce"), []byte("My secret message"), nil)
1291+
1292+ // Decrypt with same nonce - should NOT be flagged (same nonce as encryption)
1293+ cipherText, _ = aesGCM.Open(nil, []byte("ILoveMyNonce"), cipherText, nil)
1294+ }
1295+ ` }, 1 , gosec .NewConfig ()},
1296+
1297+ {[]string {`package main
1298+
1299+ import (
1300+ "crypto/aes"
1301+ "crypto/cipher"
1302+ )
1303+
1304+ func main() {
1305+ block, _ := aes.NewCipher([]byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})
1306+ // NewCBCDecrypter should not be flagged - decryption must use same nonce as encryption
1307+ aesCBC := cipher.NewCBCDecrypter(block, []byte("ILoveMyNonceAlot"))
1308+ var output = make([]byte, 16)
1309+ aesCBC.CryptBlocks(output, []byte("encrypted_block!"))
1310+ }
1311+ ` }, 0 , gosec .NewConfig ()},
1312+
1313+ {[]string {`package main
1314+
1315+ import (
1316+ "crypto/aes"
1317+ "crypto/cipher"
1318+ )
1319+
1320+ func main() {
1321+ block, _ := aes.NewCipher([]byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})
1322+ // NewCFBDecrypter should not be flagged - decryption must use same nonce as encryption
1323+ aesCFB := cipher.NewCFBDecrypter(block, []byte("ILoveMyNonceAlot"))
1324+ var output = make([]byte, 16)
1325+ aesCFB.XORKeyStream(output, []byte("Very Cool thing!"))
1326+ }
1327+ ` }, 0 , gosec .NewConfig ()},
1328+
1329+ {[]string {`package main
1330+
1331+ import (
1332+ "crypto/aes"
1333+ "crypto/cipher"
1334+ )
1335+
1336+ func main() {
1337+ block, _ := aes.NewCipher([]byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})
1338+ // NewCBCEncrypter SHOULD be flagged - encryption should use random nonce
1339+ aesCBC := cipher.NewCBCEncrypter(block, []byte("ILoveMyNonceAlot"))
1340+ var output = make([]byte, 16)
1341+ aesCBC.CryptBlocks(output, []byte("Very Cool thing!"))
1342+ }
1343+ ` }, 1 , gosec .NewConfig ()},
1344+
1345+ {[]string {`package main
1346+
1347+ import (
1348+ "crypto/aes"
1349+ "crypto/cipher"
1350+ )
1351+
1352+ func main() {
1353+ block, _ := aes.NewCipher([]byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})
1354+ // NewCFBEncrypter SHOULD be flagged - encryption should use random nonce
1355+ aesCFB := cipher.NewCFBEncrypter(block, []byte("ILoveMyNonceAlot"))
1356+ var output = make([]byte, 16)
1357+ aesCFB.XORKeyStream(output, []byte("Very Cool thing!"))
1358+ }
12591359` }, 1 , gosec .NewConfig ()},
12601360}
0 commit comments