test/core/crypto: Minor changes to AES related tests

- Test against the non-standard GCM nonce length vectors
- Fix the CTR mode test to match the comment

Correctness was fine without this change.
This commit is contained in:
Yawning Angel
2024-08-10 18:32:37 +09:00
parent be7a1f671c
commit c0f9655ec4
+6 -13
View File
@@ -203,7 +203,7 @@ test_aes_ctr :: proc(t: ^testing.T, impl: aes.Implementation) {
h_ctx: sha2.Context_512 h_ctx: sha2.Context_512
sha2.init_512_256(&h_ctx) sha2.init_512_256(&h_ctx)
for i := 1; i < 2048; i = i + 1 { for i := 1; i <= 2048; i = i + 1 {
aes.keystream_bytes_ctr(&ctx, tmp[:i]) aes.keystream_bytes_ctr(&ctx, tmp[:i])
sha2.update(&h_ctx, tmp[:i]) sha2.update(&h_ctx, tmp[:i])
} }
@@ -212,7 +212,7 @@ test_aes_ctr :: proc(t: ^testing.T, impl: aes.Implementation) {
sha2.final(&h_ctx, digest[:]) sha2.final(&h_ctx, digest[:])
digest_str := string(hex.encode(digest[:], context.temp_allocator)) digest_str := string(hex.encode(digest[:], context.temp_allocator))
expected_digest_str := "d4445343afeb9d1237f95b10d00358aed4c1d7d57c9fe480cd0afb5e2ffd448c" expected_digest_str := "b5ba4e7d6e3d1ff2bb54387fc1528573a6b351610ce7bcc80b00da089f4b1bf0"
testing.expectf( testing.expectf(
t, t,
expected_digest_str == digest_str, expected_digest_str == digest_str,
@@ -227,8 +227,7 @@ test_aes_gcm :: proc(t: ^testing.T, impl: aes.Implementation) {
log.debugf("Testing AES-GCM/%v", impl) log.debugf("Testing AES-GCM/%v", impl)
// NIST did a reorg of their site, so the source of the test vectors // NIST did a reorg of their site, so the source of the test vectors
// is only available from an archive. The commented out tests are // is only available from an archive.
// for non-96-bit IVs which our implementation does not support.
// //
// https://csrc.nist.rip/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-revised-spec.pdf // https://csrc.nist.rip/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-revised-spec.pdf
test_vectors := []struct { test_vectors := []struct {
@@ -271,7 +270,6 @@ test_aes_gcm :: proc(t: ^testing.T, impl: aes.Implementation) {
"42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091", "42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091",
"5bc94fbc3221a5db94fae95ae7121a47", "5bc94fbc3221a5db94fae95ae7121a47",
}, },
/*
{ {
"feffe9928665731c6d6a8f9467308308", "feffe9928665731c6d6a8f9467308308",
"cafebabefacedbad", "cafebabefacedbad",
@@ -288,7 +286,6 @@ test_aes_gcm :: proc(t: ^testing.T, impl: aes.Implementation) {
"8ce24998625615b603a033aca13fb894be9112a5c3a211a8ba262a3cca7e2ca701e4a9a4fba43c90ccdcb281d48c7c6fd62875d2aca417034c34aee5", "8ce24998625615b603a033aca13fb894be9112a5c3a211a8ba262a3cca7e2ca701e4a9a4fba43c90ccdcb281d48c7c6fd62875d2aca417034c34aee5",
"619cc5aefffe0bfa462af43c1699d050", "619cc5aefffe0bfa462af43c1699d050",
}, },
*/
{ {
"000000000000000000000000000000000000000000000000", "000000000000000000000000000000000000000000000000",
"000000000000000000000000", "000000000000000000000000",
@@ -321,7 +318,6 @@ test_aes_gcm :: proc(t: ^testing.T, impl: aes.Implementation) {
"3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c7d773d00c144c525ac619d18c84a3f4718e2448b2fe324d9ccda2710", "3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c7d773d00c144c525ac619d18c84a3f4718e2448b2fe324d9ccda2710",
"2519498e80f1478f37ba55bd6d27618c", "2519498e80f1478f37ba55bd6d27618c",
}, },
/*
{ {
"feffe9928665731c6d6a8f9467308308feffe9928665731c", "feffe9928665731c6d6a8f9467308308feffe9928665731c",
"cafebabefacedbad", "cafebabefacedbad",
@@ -338,7 +334,6 @@ test_aes_gcm :: proc(t: ^testing.T, impl: aes.Implementation) {
"d27e88681ce3243c4830165a8fdcf9ff1de9a1d8e6b447ef6ef7b79828666e4581e79012af34ddd9e2f037589b292db3e67c036745fa22e7e9b7373b", "d27e88681ce3243c4830165a8fdcf9ff1de9a1d8e6b447ef6ef7b79828666e4581e79012af34ddd9e2f037589b292db3e67c036745fa22e7e9b7373b",
"dcf566ff291c25bbb8568fc3d376a6d9", "dcf566ff291c25bbb8568fc3d376a6d9",
}, },
*/
{ {
"0000000000000000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000000000000000",
"000000000000000000000000", "000000000000000000000000",
@@ -371,7 +366,6 @@ test_aes_gcm :: proc(t: ^testing.T, impl: aes.Implementation) {
"522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662", "522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662",
"76fc6ece0f4e1768cddf8853bb2d551b", "76fc6ece0f4e1768cddf8853bb2d551b",
}, },
/*
{ {
"feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308", "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
"cafebabefacedbad", "cafebabefacedbad",
@@ -388,7 +382,6 @@ test_aes_gcm :: proc(t: ^testing.T, impl: aes.Implementation) {
"5a8def2f0c9e53f1f75d7853659e2a20eeb2b22aafde6419a058ab4f6f746bf40fc0c3b780f244452da3ebf1c5d82cdea2418997200ef82e44ae7e3f", "5a8def2f0c9e53f1f75d7853659e2a20eeb2b22aafde6419a058ab4f6f746bf40fc0c3b780f244452da3ebf1c5d82cdea2418997200ef82e44ae7e3f",
"a44a8266ee1c8eb0c8b5d4cf5ae9f19a", "a44a8266ee1c8eb0c8b5d4cf5ae9f19a",
}, },
*/
} }
for v, _ in test_vectors { for v, _ in test_vectors {
key, _ := hex.decode(transmute([]byte)(v.key), context.temp_allocator) key, _ := hex.decode(transmute([]byte)(v.key), context.temp_allocator)
@@ -405,8 +398,8 @@ test_aes_gcm :: proc(t: ^testing.T, impl: aes.Implementation) {
aes.init_gcm(&ctx, key, impl) aes.init_gcm(&ctx, key, impl)
aes.seal_gcm(&ctx, dst, tag_, iv, aad, plaintext) aes.seal_gcm(&ctx, dst, tag_, iv, aad, plaintext)
dst_str := string(hex.encode(dst[:], context.temp_allocator)) dst_str := string(hex.encode(dst, context.temp_allocator))
tag_str := string(hex.encode(tag_[:], context.temp_allocator)) tag_str := string(hex.encode(tag_, context.temp_allocator))
testing.expectf( testing.expectf(
t, t,
@@ -424,7 +417,7 @@ test_aes_gcm :: proc(t: ^testing.T, impl: aes.Implementation) {
) )
ok := aes.open_gcm(&ctx, dst, iv, aad, ciphertext, tag) ok := aes.open_gcm(&ctx, dst, iv, aad, ciphertext, tag)
dst_str = string(hex.encode(dst[:], context.temp_allocator)) dst_str = string(hex.encode(dst, context.temp_allocator))
testing.expectf( testing.expectf(
t, t,