Browse Source

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.
Yawning Angel 1 year ago
parent
commit
c0f9655ec4
1 changed files with 54 additions and 61 deletions
  1. 54 61
      tests/core/crypto/test_core_crypto_aes.odin

+ 54 - 61
tests/core/crypto/test_core_crypto_aes.odin

@@ -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
-	// for non-96-bit IVs which our implementation does not support.
+	// is only available from an archive.
 	//
 	//
 	// 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,24 +270,22 @@ test_aes_gcm :: proc(t: ^testing.T, impl: aes.Implementation) {
 			"42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091",
 			"42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091",
 			"5bc94fbc3221a5db94fae95ae7121a47",
 			"5bc94fbc3221a5db94fae95ae7121a47",
 		},
 		},
-		/*
-			{
-				"feffe9928665731c6d6a8f9467308308",
-				"cafebabefacedbad",
-				"feedfacedeadbeeffeedfacedeadbeefabaddad2",
-				"d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
-				"61353b4c2806934a777ff51fa22a4755699b2a714fcdc6f83766e5f97b6c742373806900e49f24b22b097544d4896b424989b5e1ebac0f07c23f4598",
-				"3612d2e79e3b0785561be14aaca2fccb",
-			},
-			{
-				"feffe9928665731c6d6a8f9467308308",
-				"9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b",
-				"feedfacedeadbeeffeedfacedeadbeefabaddad2",
-				"d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
-				"8ce24998625615b603a033aca13fb894be9112a5c3a211a8ba262a3cca7e2ca701e4a9a4fba43c90ccdcb281d48c7c6fd62875d2aca417034c34aee5",
-				"619cc5aefffe0bfa462af43c1699d050",
-			},
-		*/
+		{
+			"feffe9928665731c6d6a8f9467308308",
+			"cafebabefacedbad",
+			"feedfacedeadbeeffeedfacedeadbeefabaddad2",
+			"d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
+			"61353b4c2806934a777ff51fa22a4755699b2a714fcdc6f83766e5f97b6c742373806900e49f24b22b097544d4896b424989b5e1ebac0f07c23f4598",
+			"3612d2e79e3b0785561be14aaca2fccb",
+		},
+		{
+			"feffe9928665731c6d6a8f9467308308",
+			"9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b",
+			"feedfacedeadbeeffeedfacedeadbeefabaddad2",
+			"d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
+			"8ce24998625615b603a033aca13fb894be9112a5c3a211a8ba262a3cca7e2ca701e4a9a4fba43c90ccdcb281d48c7c6fd62875d2aca417034c34aee5",
+			"619cc5aefffe0bfa462af43c1699d050",
+		},
 		{
 		{
 			"000000000000000000000000000000000000000000000000",
 			"000000000000000000000000000000000000000000000000",
 			"000000000000000000000000",
 			"000000000000000000000000",
@@ -321,24 +318,22 @@ test_aes_gcm :: proc(t: ^testing.T, impl: aes.Implementation) {
 			"3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c7d773d00c144c525ac619d18c84a3f4718e2448b2fe324d9ccda2710",
 			"3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c7d773d00c144c525ac619d18c84a3f4718e2448b2fe324d9ccda2710",
 			"2519498e80f1478f37ba55bd6d27618c",
 			"2519498e80f1478f37ba55bd6d27618c",
 		},
 		},
-		/*
-			{
-				"feffe9928665731c6d6a8f9467308308feffe9928665731c",
-				"cafebabefacedbad",
-				"feedfacedeadbeeffeedfacedeadbeefabaddad2",
-				"d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
-				"0f10f599ae14a154ed24b36e25324db8c566632ef2bbb34f8347280fc4507057fddc29df9a471f75c66541d4d4dad1c9e93a19a58e8b473fa0f062f7",
-				"65dcc57fcf623a24094fcca40d3533f8",
-			},
-			{
-				"feffe9928665731c6d6a8f9467308308feffe9928665731c",
-				"9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b",
-				"feedfacedeadbeeffeedfacedeadbeefabaddad2",
-				"d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
-				"d27e88681ce3243c4830165a8fdcf9ff1de9a1d8e6b447ef6ef7b79828666e4581e79012af34ddd9e2f037589b292db3e67c036745fa22e7e9b7373b",
-				"dcf566ff291c25bbb8568fc3d376a6d9",
-			},
-		*/
+		{
+			"feffe9928665731c6d6a8f9467308308feffe9928665731c",
+			"cafebabefacedbad",
+			"feedfacedeadbeeffeedfacedeadbeefabaddad2",
+			"d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
+			"0f10f599ae14a154ed24b36e25324db8c566632ef2bbb34f8347280fc4507057fddc29df9a471f75c66541d4d4dad1c9e93a19a58e8b473fa0f062f7",
+			"65dcc57fcf623a24094fcca40d3533f8",
+		},
+		{
+			"feffe9928665731c6d6a8f9467308308feffe9928665731c",
+			"9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b",
+			"feedfacedeadbeeffeedfacedeadbeefabaddad2",
+			"d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
+			"d27e88681ce3243c4830165a8fdcf9ff1de9a1d8e6b447ef6ef7b79828666e4581e79012af34ddd9e2f037589b292db3e67c036745fa22e7e9b7373b",
+			"dcf566ff291c25bbb8568fc3d376a6d9",
+		},
 		{
 		{
 			"0000000000000000000000000000000000000000000000000000000000000000",
 			"0000000000000000000000000000000000000000000000000000000000000000",
 			"000000000000000000000000",
 			"000000000000000000000000",
@@ -371,24 +366,22 @@ test_aes_gcm :: proc(t: ^testing.T, impl: aes.Implementation) {
 			"522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662",
 			"522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662",
 			"76fc6ece0f4e1768cddf8853bb2d551b",
 			"76fc6ece0f4e1768cddf8853bb2d551b",
 		},
 		},
-		/*
-			{
-				"feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
-				"cafebabefacedbad",
-				"feedfacedeadbeeffeedfacedeadbeefabaddad2",
-				"d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
-				"c3762df1ca787d32ae47c13bf19844cbaf1ae14d0b976afac52ff7d79bba9de0feb582d33934a4f0954cc2363bc73f7862ac430e64abe499f47c9b1f",
-				"3a337dbf46a792c45e454913fe2ea8f2",
-			},
-			{
-				"feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
-				"9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b",
-				"feedfacedeadbeeffeedfacedeadbeefabaddad2",
-				"d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
-				"5a8def2f0c9e53f1f75d7853659e2a20eeb2b22aafde6419a058ab4f6f746bf40fc0c3b780f244452da3ebf1c5d82cdea2418997200ef82e44ae7e3f",
-				"a44a8266ee1c8eb0c8b5d4cf5ae9f19a",
-			},
-		*/
+		{
+			"feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
+			"cafebabefacedbad",
+			"feedfacedeadbeeffeedfacedeadbeefabaddad2",
+			"d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
+			"c3762df1ca787d32ae47c13bf19844cbaf1ae14d0b976afac52ff7d79bba9de0feb582d33934a4f0954cc2363bc73f7862ac430e64abe499f47c9b1f",
+			"3a337dbf46a792c45e454913fe2ea8f2",
+		},
+		{
+			"feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
+			"9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b",
+			"feedfacedeadbeeffeedfacedeadbeefabaddad2",
+			"d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
+			"5a8def2f0c9e53f1f75d7853659e2a20eeb2b22aafde6419a058ab4f6f746bf40fc0c3b780f244452da3ebf1c5d82cdea2418997200ef82e44ae7e3f",
+			"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))
-		tag_str := string(hex.encode(tag_[:], context.temp_allocator))
+		dst_str := string(hex.encode(dst, 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,