diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/crypto/aes_gcm_128_test.c | 358 |
1 files changed, 137 insertions, 221 deletions
diff --git a/lib/crypto/aes_gcm_128_test.c b/lib/crypto/aes_gcm_128_test.c index 4b534870fd3..6d2cbf2aed8 100644 --- a/lib/crypto/aes_gcm_128_test.c +++ b/lib/crypto/aes_gcm_128_test.c @@ -19,100 +19,141 @@ #include "replace.h" #include "../lib/util/samba_util.h" #include "../lib/crypto/crypto.h" +#include "../lib/crypto/aes_test.h" +#ifndef AES_GCM_128_ONLY_TESTVECTORS struct torture_context; -bool torture_local_crypto_aes_gcm_128(struct torture_context *torture); +bool torture_local_crypto_aes_gcm_128(struct torture_context *tctx); /* This uses the test values from ... */ -bool torture_local_crypto_aes_gcm_128(struct torture_context *torture) +bool torture_local_crypto_aes_gcm_128(struct torture_context *tctx) { bool ret = true; uint32_t i; - struct { - DATA_BLOB K; - DATA_BLOB IV; - DATA_BLOB A; - DATA_BLOB P; - DATA_BLOB C; - DATA_BLOB T; - } testarray[5]; - - TALLOC_CTX *tctx = talloc_new(torture); - if (!tctx) { return false; }; - - ZERO_STRUCT(testarray); - - testarray[0].K = strhex_to_data_blob(tctx, - "00000000000000000000000000000000"); - testarray[0].IV = strhex_to_data_blob(tctx, - "000000000000000000000000"); - testarray[0].A = data_blob_null; - testarray[0].P = data_blob_null; - testarray[0].C = data_blob_null; - testarray[0].T = strhex_to_data_blob(tctx, - "58e2fccefa7e3061367f1d57a4e7455a"); - - testarray[1].K = strhex_to_data_blob(tctx, - "00000000000000000000000000000000"); - testarray[1].IV = strhex_to_data_blob(tctx, - "000000000000000000000000"); - testarray[1].A = data_blob_null; - testarray[1].P = strhex_to_data_blob(tctx, - "00000000000000000000000000000000"); - testarray[1].C = strhex_to_data_blob(tctx, - "0388dace60b6a392f328c2b971b2fe78"); - testarray[1].T = strhex_to_data_blob(tctx, - "ab6e47d42cec13bdf53a67b21257bddf"); - - testarray[2].K = strhex_to_data_blob(tctx, - "feffe9928665731c6d6a8f9467308308"); - testarray[2].IV = strhex_to_data_blob(tctx, - "cafebabefacedbaddecaf888"); - testarray[2].A = data_blob_null; - testarray[2].P = strhex_to_data_blob(tctx, - "d9313225f88406e5a55909c5aff5269a" - "86a7a9531534f7da2e4c303d8a318a72" - "1c3c0c95956809532fcf0e2449a6b525" - "b16aedf5aa0de657ba637b391aafd255"); - testarray[2].C = strhex_to_data_blob(tctx, - "42831ec2217774244b7221b784d0d49c" - "e3aa212f2c02a4e035c17e2329aca12e" - "21d514b25466931c7d8f6a5aac84aa05" - "1ba30b396a0aac973d58e091473f5985"); - testarray[2].T = strhex_to_data_blob(tctx, - "4d5c2af327cd64a62cf35abd2ba6fab4"); - - testarray[3].K = strhex_to_data_blob(tctx, - "feffe9928665731c6d6a8f9467308308"); - testarray[3].IV = strhex_to_data_blob(tctx, - "cafebabefacedbaddecaf888"); - testarray[3].A = strhex_to_data_blob(tctx, - "feedfacedeadbeeffeedfacedeadbeef" - "abaddad2"); - testarray[3].P = strhex_to_data_blob(tctx, - "d9313225f88406e5a55909c5aff5269a" - "86a7a9531534f7da2e4c303d8a318a72" - "1c3c0c95956809532fcf0e2449a6b525" - "b16aedf5aa0de657ba637b39"); - testarray[3].C = strhex_to_data_blob(tctx, - "42831ec2217774244b7221b784d0d49c" - "e3aa212f2c02a4e035c17e2329aca12e" - "21d514b25466931c7d8f6a5aac84aa05" - "1ba30b396a0aac973d58e091"); - testarray[3].T = strhex_to_data_blob(tctx, - "5bc94fbc3221a5db94fae95ae7121a47"); - - for (i=0; testarray[i].T.length != 0; i++) { + struct aes_mode_testvector testarray[] = { +#endif /* AES_GCM_128_ONLY_TESTVECTORS */ +#define AES_GCM_128_TESTVECTOR(_k, _n, _a, _p, _c, _t) \ + AES_MODE_TESTVECTOR(aes_gcm_128, _k, _n, _a, _p, _c, _t) + + AES_GCM_128_TESTVECTOR( + /* K */ + "8BF9FBC2B8149484FF11AB1F3A544FF6", + /* N */ + "010000000000000077F7A8FF", + /* A */ + "010000000000000077F7A80000000000" + "A8000000000001004100002C00980000", + /* P */ + "FE534D4240000100000000000B00811F" + "00000000000000000600000000000000" + "00000000010000004100002C00980000" + "00000000000000000000000000000000" + "3900000094010600FFFFFFFFFFFFFFFF" + "FFFFFFFFFFFFFFFF7800000030000000" + "000000007800000000000000FFFF0000" + "0100000000000000" + "03005C003100370032002E0033003100" + "2E0039002E003100380033005C006E00" + "650074006C006F0067006F006E000000", + /* C */ + "863C07C1FBFA82D741A080C97DF52CFF" + "432A63A37E5ACFA3865AE4E6E422D502" + "FA7C6FBB9A7418F28C43F00A3869F687" + "257CA665E25E62A0F458C42AA9E95DC4" + "6CB351A0A497FABB7DCE58FEE5B20B08" + "522E0E701B112FB93B36E7A0FB084D35" + "62C0F3FDF0421079DD96BBCCA40949B3" + "A7FC1AA635A72384" + "2037DE3CA6385465D1884B29D7140790" + "88AD3E770E2528D527B302536B7E5B1B" + "430E048230AFE785DB89F4D87FC1F816", + /* T */ + "BC9B5871EBFA89ADE21439ACDCD65D22" + ), + AES_GCM_128_TESTVECTOR( + /* K */ + "00000000000000000000000000000000", + /* N */ + "000000000000000000000000", + /* A */ + "", + /* P */ + "", + /* C */ + "", + /* T */ + "58e2fccefa7e3061367f1d57a4e7455a" + ), + AES_GCM_128_TESTVECTOR( + /* K */ + "00000000000000000000000000000000", + /* N */ + "000000000000000000000000", + /* A */ + "", + /* P */ + "00000000000000000000000000000000", + /* C */ + "0388dace60b6a392f328c2b971b2fe78", + /* T */ + "ab6e47d42cec13bdf53a67b21257bddf" + ), + AES_GCM_128_TESTVECTOR( + /* K */ + "feffe9928665731c6d6a8f9467308308", + /* N */ + "cafebabefacedbaddecaf888", + /* A */ + "", + /* P */ + "d9313225f88406e5a55909c5aff5269a" + "86a7a9531534f7da2e4c303d8a318a72" + "1c3c0c95956809532fcf0e2449a6b525" + "b16aedf5aa0de657ba637b391aafd255", + /* C */ + "42831ec2217774244b7221b784d0d49c" + "e3aa212f2c02a4e035c17e2329aca12e" + "21d514b25466931c7d8f6a5aac84aa05" + "1ba30b396a0aac973d58e091473f5985", + /* T */ + "4d5c2af327cd64a62cf35abd2ba6fab4" + ), + AES_GCM_128_TESTVECTOR( + /* K */ + "feffe9928665731c6d6a8f9467308308", + /* N */ + "cafebabefacedbaddecaf888", + /* A */ + "feedfacedeadbeeffeedfacedeadbeef" + "abaddad2", + /* P */ + "d9313225f88406e5a55909c5aff5269a" + "86a7a9531534f7da2e4c303d8a318a72" + "1c3c0c95956809532fcf0e2449a6b525" + "b16aedf5aa0de657ba637b39", + /* C */ + "42831ec2217774244b7221b784d0d49c" + "e3aa212f2c02a4e035c17e2329aca12e" + "21d514b25466931c7d8f6a5aac84aa05" + "1ba30b396a0aac973d58e091", + /* T */ + "5bc94fbc3221a5db94fae95ae7121a47" + ), +#ifndef AES_GCM_128_ONLY_TESTVECTORS + }; + + for (i=0; i < ARRAY_SIZE(testarray); i++) { struct aes_gcm_128_context ctx; uint8_t T[AES_BLOCK_SIZE]; + DATA_BLOB _T = data_blob_const(T, sizeof(T)); DATA_BLOB C; int e; C = data_blob_dup_talloc(tctx, testarray[i].P); - aes_gcm_128_init(&ctx, testarray[i].K.data, testarray[i].IV.data); + aes_gcm_128_init(&ctx, testarray[i].K.data, testarray[i].N.data); aes_gcm_128_updateA(&ctx, testarray[i].A.data, testarray[i].A.length); @@ -122,61 +163,30 @@ bool torture_local_crypto_aes_gcm_128(struct torture_context *torture) e = memcmp(testarray[i].T.data, T, sizeof(T)); if (e != 0) { - printf("%s: aes_gcm_128 test[%u]: failed\n", __location__, i); - printf("K\n"); - dump_data(0, testarray[i].K.data, testarray[i].K.length); - printf("IV\n"); - dump_data(0, testarray[i].IV.data, testarray[i].IV.length); - printf("A\n"); - dump_data(0, testarray[i].A.data, testarray[i].A.length); - printf("P\n"); - dump_data(0, testarray[i].P.data, testarray[i].P.length); - printf("C1\n"); - dump_data(0, testarray[i].C.data, testarray[i].C.length); - printf("C2\n"); - dump_data(0, C.data, C.length); - printf("T1\n"); - dump_data(0, testarray[i].T.data, testarray[i].T.length); - printf("T2\n"); - dump_data(0, T, sizeof(T)); + aes_mode_testvector_debug(&testarray[i], NULL, &C, &_T); ret = false; goto fail; } e = memcmp(testarray[i].C.data, C.data, C.length); if (e != 0) { - printf("%s: aes_gcm_128 test[%u]: failed\n", __location__, i); - printf("K\n"); - dump_data(0, testarray[i].K.data, testarray[i].K.length); - printf("IV\n"); - dump_data(0, testarray[i].IV.data, testarray[i].IV.length); - printf("A\n"); - dump_data(0, testarray[i].A.data, testarray[i].A.length); - printf("P\n"); - dump_data(0, testarray[i].P.data, testarray[i].P.length); - printf("C1\n"); - dump_data(0, testarray[i].C.data, testarray[i].C.length); - printf("C2\n"); - dump_data(0, C.data, C.length); - printf("T1\n"); - dump_data(0, testarray[i].T.data, testarray[i].T.length); - printf("T2\n"); - dump_data(0, T, sizeof(T)); + aes_mode_testvector_debug(&testarray[i], NULL, &C, &_T); ret = false; goto fail; } } - for (i=0; testarray[i].T.length != 0; i++) { + for (i=0; i < ARRAY_SIZE(testarray); i++) { struct aes_gcm_128_context ctx; uint8_t T[AES_BLOCK_SIZE]; + DATA_BLOB _T = data_blob_const(T, sizeof(T)); DATA_BLOB C; int e; size_t j; C = data_blob_dup_talloc(tctx, testarray[i].P); - aes_gcm_128_init(&ctx, testarray[i].K.data, testarray[i].IV.data); + aes_gcm_128_init(&ctx, testarray[i].K.data, testarray[i].N.data); for (j=0; j < testarray[i].A.length; j++) { aes_gcm_128_updateA(&ctx, NULL, 0); aes_gcm_128_updateA(&ctx, &testarray[i].A.data[j], 1); @@ -194,61 +204,30 @@ bool torture_local_crypto_aes_gcm_128(struct torture_context *torture) e = memcmp(testarray[i].T.data, T, sizeof(T)); if (e != 0) { - printf("%s: aes_gcm_128 test[%u]: failed\n", __location__, i); - printf("K\n"); - dump_data(0, testarray[i].K.data, testarray[i].K.length); - printf("IV\n"); - dump_data(0, testarray[i].IV.data, testarray[i].IV.length); - printf("A\n"); - dump_data(0, testarray[i].A.data, testarray[i].A.length); - printf("P\n"); - dump_data(0, testarray[i].P.data, testarray[i].P.length); - printf("C1\n"); - dump_data(0, testarray[i].C.data, testarray[i].C.length); - printf("C2\n"); - dump_data(0, C.data, C.length); - printf("T1\n"); - dump_data(0, testarray[i].T.data, testarray[i].T.length); - printf("T2\n"); - dump_data(0, T, sizeof(T)); + aes_mode_testvector_debug(&testarray[i], NULL, &C, &_T); ret = false; goto fail; } e = memcmp(testarray[i].C.data, C.data, C.length); if (e != 0) { - printf("%s: aes_gcm_128 test[%u]: failed\n", __location__, i); - printf("K\n"); - dump_data(0, testarray[i].K.data, testarray[i].K.length); - printf("IV\n"); - dump_data(0, testarray[i].IV.data, testarray[i].IV.length); - printf("A\n"); - dump_data(0, testarray[i].A.data, testarray[i].A.length); - printf("P\n"); - dump_data(0, testarray[i].P.data, testarray[i].P.length); - printf("C1\n"); - dump_data(0, testarray[i].C.data, testarray[i].C.length); - printf("C2\n"); - dump_data(0, C.data, C.length); - printf("T1\n"); - dump_data(0, testarray[i].T.data, testarray[i].T.length); - printf("T2\n"); - dump_data(0, T, sizeof(T)); + aes_mode_testvector_debug(&testarray[i], NULL, &C, &_T); ret = false; goto fail; } } - for (i=0; testarray[i].T.length != 0; i++) { + for (i=0; i < ARRAY_SIZE(testarray); i++) { struct aes_gcm_128_context ctx; uint8_t T[AES_BLOCK_SIZE]; + DATA_BLOB _T = data_blob_const(T, sizeof(T)); DATA_BLOB P; int e; size_t j; P = data_blob_dup_talloc(tctx, testarray[i].C); - aes_gcm_128_init(&ctx, testarray[i].K.data, testarray[i].IV.data); + aes_gcm_128_init(&ctx, testarray[i].K.data, testarray[i].N.data); for (j=0; j < testarray[i].A.length; j++) { aes_gcm_128_updateA(&ctx, NULL, 0); aes_gcm_128_updateA(&ctx, &testarray[i].A.data[j], 1); @@ -266,60 +245,29 @@ bool torture_local_crypto_aes_gcm_128(struct torture_context *torture) e = memcmp(testarray[i].T.data, T, sizeof(T)); if (e != 0) { - printf("%s: aes_gcm_128 test[%u]: failed\n", __location__, i); - printf("K\n"); - dump_data(0, testarray[i].K.data, testarray[i].K.length); - printf("IV\n"); - dump_data(0, testarray[i].IV.data, testarray[i].IV.length); - printf("A\n"); - dump_data(0, testarray[i].A.data, testarray[i].A.length); - printf("P1\n"); - dump_data(0, testarray[i].P.data, testarray[i].P.length); - printf("P2\n"); - dump_data(0, P.data, P.length); - printf("C\n"); - dump_data(0, testarray[i].C.data, testarray[i].C.length); - printf("T1\n"); - dump_data(0, testarray[i].T.data, testarray[i].T.length); - printf("T2\n"); - dump_data(0, T, sizeof(T)); + aes_mode_testvector_debug(&testarray[i], &P, NULL, &_T); ret = false; goto fail; } e = memcmp(testarray[i].P.data, P.data, P.length); if (e != 0) { - printf("%s: aes_gcm_128 test[%u]: failed\n", __location__, i); - printf("K\n"); - dump_data(0, testarray[i].K.data, testarray[i].K.length); - printf("IV\n"); - dump_data(0, testarray[i].IV.data, testarray[i].IV.length); - printf("A\n"); - dump_data(0, testarray[i].A.data, testarray[i].A.length); - printf("P1\n"); - dump_data(0, testarray[i].P.data, testarray[i].P.length); - printf("P2\n"); - dump_data(0, P.data, P.length); - printf("C\n"); - dump_data(0, testarray[i].C.data, testarray[i].C.length); - printf("T1\n"); - dump_data(0, testarray[i].T.data, testarray[i].T.length); - printf("T2\n"); - dump_data(0, T, sizeof(T)); + aes_mode_testvector_debug(&testarray[i], &P, NULL, &_T); ret = false; goto fail; } } - for (i=0; testarray[i].T.length != 0; i++) { + for (i=0; i < ARRAY_SIZE(testarray); i++) { struct aes_gcm_128_context ctx; uint8_t T[AES_BLOCK_SIZE]; + DATA_BLOB _T = data_blob_const(T, sizeof(T)); DATA_BLOB P; int e; P = data_blob_dup_talloc(tctx, testarray[i].C); - aes_gcm_128_init(&ctx, testarray[i].K.data, testarray[i].IV.data); + aes_gcm_128_init(&ctx, testarray[i].K.data, testarray[i].N.data); aes_gcm_128_updateA(&ctx, testarray[i].A.data, testarray[i].A.length); aes_gcm_128_updateC(&ctx, P.data, P.length); aes_gcm_128_crypt(&ctx, P.data, P.length); @@ -327,52 +275,20 @@ bool torture_local_crypto_aes_gcm_128(struct torture_context *torture) e = memcmp(testarray[i].T.data, T, sizeof(T)); if (e != 0) { - printf("%s: aes_gcm_128 test[%u]: failed\n", __location__, i); - printf("K\n"); - dump_data(0, testarray[i].K.data, testarray[i].K.length); - printf("IV\n"); - dump_data(0, testarray[i].IV.data, testarray[i].IV.length); - printf("A\n"); - dump_data(0, testarray[i].A.data, testarray[i].A.length); - printf("P1\n"); - dump_data(0, testarray[i].P.data, testarray[i].P.length); - printf("P2\n"); - dump_data(0, P.data, P.length); - printf("C\n"); - dump_data(0, testarray[i].C.data, testarray[i].C.length); - printf("T1\n"); - dump_data(0, testarray[i].T.data, testarray[i].T.length); - printf("T2\n"); - dump_data(0, T, sizeof(T)); + aes_mode_testvector_debug(&testarray[i], &P, NULL, &_T); ret = false; goto fail; } e = memcmp(testarray[i].P.data, P.data, P.length); if (e != 0) { - printf("%s: aes_gcm_128 test[%u]: failed\n", __location__, i); - printf("K\n"); - dump_data(0, testarray[i].K.data, testarray[i].K.length); - printf("IV\n"); - dump_data(0, testarray[i].IV.data, testarray[i].IV.length); - printf("A\n"); - dump_data(0, testarray[i].A.data, testarray[i].A.length); - printf("P1\n"); - dump_data(0, testarray[i].P.data, testarray[i].P.length); - printf("P2\n"); - dump_data(0, P.data, P.length); - printf("C\n"); - dump_data(0, testarray[i].C.data, testarray[i].C.length); - printf("T1\n"); - dump_data(0, testarray[i].T.data, testarray[i].T.length); - printf("T2\n"); - dump_data(0, T, sizeof(T)); + aes_mode_testvector_debug(&testarray[i], &P, NULL, &_T); ret = false; goto fail; } } fail: - talloc_free(tctx); return ret; } +#endif /* AES_GCM_128_ONLY_TESTVECTORS */ |