diff options
author | Joachim StroĢmbergson <joachim@secworks.se> | 2015-05-13 18:04:21 +0200 |
---|---|---|
committer | Joachim StroĢmbergson <joachim@secworks.se> | 2015-05-13 18:04:21 +0200 |
commit | 644e959a05afa08dfbcfb112ed3609a64a6b84db (patch) | |
tree | aaef71fa855b61155cacb39bf9d7d94e57cfeaec | |
parent | fc6b7ce367eaeda3772575be2bc265a8521ffa84 (diff) |
Adding code for running single block tests doing encipher and decipher operations. Adding test runner for NIST tests.
-rw-r--r-- | sw/aes_tester.c | 155 |
1 files changed, 101 insertions, 54 deletions
diff --git a/sw/aes_tester.c b/sw/aes_tester.c index b8c5f52..d2f0656 100644 --- a/sw/aes_tester.c +++ b/sw/aes_tester.c @@ -47,30 +47,6 @@ #include "cryptech.h" - -//------------------------------------------------------------------ -// NIST test vectors. -//------------------------------------------------------------------ -uint32_t nist_aes128_key[4] = {0x2b7e1516, 0x28aed2a6, 0xabf71588, 0x09cf4f3c}; -uint32_t nist_aes256_key[8] = {0x603deb10, 0x15ca71be, 0x2b73aef0, 0x857d7781, - 0x1f352c07, 0x3b6108d7, 0x2d9810a3, 0x0914dff4}; - -uint32_t nist_plaintext0[4] = {0x6bc1bee2, 0x2e409f96, 0xe93d7e11, 0x7393172a}; -uint32_t nist_plaintext1[4] = {0xae2d8a57, 0x1e03ac9c, 0x9eb76fac, 0x45af8e51}; -uint32_t nist_plaintext2[4] = {0x30c81c46, 0xa35ce411, 0xe5fbc119, 0x1a0a52ef}; -uint32_t nist_plaintext3[4] = {0xf69f2445, 0xdf4f9b17, 0xad2b417b, 0xe66c3710}; - -uint32_t nist_ecb_128_enc_expected0[4] = {0x3ad77bb4, 0x0d7a3660, 0xa89ecaf3, 0x2466ef97}; -uint32_t nist_ecb_128_enc_expected1[4] = {0xf5d3d585, 0x03b9699d, 0xe785895a, 0x96fdbaaf}; -uint32_t nist_ecb_128_enc_expected2[4] = {0x43b1cd7f, 0x598ece23, 0x881b00e3, 0xed030688}; -uint32_t nist_ecb_128_enc_expected3[4] = {0x7b0c785e, 0x27e8ad3f, 0x82232071, 0x04725dd4}; - -uint32_t nist_ecb_256_enc_expected0[4] = {0xf3eed1bd, 0xb5d2a03c, 0x064b5a7e, 0x3db181f8}; -uint32_t nist_ecb_256_enc_expected1[4] = {0x591ccb10, 0xd410ed26, 0xdc5ba74a, 0x31362870}; -uint32_t nist_ecb_256_enc_expected2[4] = {0xb6ed21b9, 0x9ca6f4f9, 0xf153e7b1, 0xbeafed1d}; -uint32_t nist_ecb_256_enc_expected3[4] = {0x23304b7a, 0x39f9f3ff, 0x067d8d8f, 0x9e24ecc7}; - - //------------------------------------------------------------------ // Robs macros. Scary scary. //------------------------------------------------------------------ @@ -145,47 +121,118 @@ void dual_block_test(uint8_t keylength, uint32_t *key, uint32_t *block0, // // Perform single block tests. //------------------------------------------------------------------ -void single_block_test(uint8_t keylength, uint32_t *key, uint32_t *block, +void single_block_test(uint32_t keylength, uint32_t *key, uint32_t *block, uint32_t *expected) { + uint32_t enc_result[4]; + uint32_t dec_result[4]; + + tc_w32(AES_ADDR_KEY0, key[0]); + tc_w32(AES_ADDR_KEY1, key[1]); + tc_w32(AES_ADDR_KEY2, key[2]); + tc_w32(AES_ADDR_KEY3, key[3]); + + if (keylength == 256) { + tc_w32(AES_ADDR_KEY0, key[4]); + tc_w32(AES_ADDR_KEY1, key[5]); + tc_w32(AES_ADDR_KEY2, key[6]); + tc_w32(AES_ADDR_KEY3, key[7]); + } + + tc_w32(AES_ADDR_BLOCK0, block[0]); + tc_w32(AES_ADDR_BLOCK1, block[1]); + tc_w32(AES_ADDR_BLOCK2, block[2]); + tc_w32(AES_ADDR_BLOCK3, block[3]); + + // Single block encipher operation. + if (keylength == 256) + tc_w32(AES_ADDR_CONFIG, 0x00000003); + else + tc_w32(AES_ADDR_CONFIG, 0x00000001); + + tc_w32(AES_ADDR_CTRL, 0x00000001); + tc_wait_ready(AES_ADDR_STATUS); -} + enc_result[0] = tc_r32(AES_ADDR_RESULT0); + enc_result[1] = tc_r32(AES_ADDR_RESULT1); + enc_result[2] = tc_r32(AES_ADDR_RESULT2); + enc_result[3] = tc_r32(AES_ADDR_RESULT3); -//------------------------------------------------------------------ -// nist_single_block_ecb_128() -// -// The first NIST aes ecb mode single block test with -// 128 bit key. -//------------------------------------------------------------------ -void nist_single_block_ecb_128() -{ - printf("Doing NIST ECB mode single block test with 128 bit key.\n"); + tc_w32(AES_ADDR_BLOCK0, enc_result[0]); + tc_w32(AES_ADDR_BLOCK1, enc_result[1]); + tc_w32(AES_ADDR_BLOCK2, enc_result[2]); + tc_w32(AES_ADDR_BLOCK3, enc_result[3]); - printf("Writing 128 bit key.\n"); - tc_w32(AES_ADDR_KEY0, 0x2b7e1516); - tc_w32(AES_ADDR_KEY1, 0x28aed2a6); - tc_w32(AES_ADDR_KEY2, 0xabf71588); - tc_w32(AES_ADDR_KEY3, 0x09cf4f3c); - printf("\n"); + // Single block decipher operation. + if (keylength == 256) + tc_w32(AES_ADDR_CONFIG, 0x00000002); + else + tc_w32(AES_ADDR_CONFIG, 0x00000000); + tc_w32(AES_ADDR_CTRL, 0x00000001); - printf("Writing data.\n"); - tc_w32(AES_ADDR_BLOCK0, 0x6bc1bee2); - tc_w32(AES_ADDR_BLOCK1, 0x2e409f96); - tc_w32(AES_ADDR_BLOCK2, 0xe93d7e11); - tc_w32(AES_ADDR_BLOCK3, 0x7393172a); + tc_wait_ready(AES_ADDR_STATUS); - printf("Setting 128-bit key mode and encipher operation.\n"); - printf("Starting the core processing.\n"); - tc_w32(AES_ADDR_CONFIG, 0x00000001); - tc_w32(AES_ADDR_CTRL, 0x00000001); + dec_result[0] = tc_r32(AES_ADDR_RESULT0); + dec_result[1] = tc_r32(AES_ADDR_RESULT1); + dec_result[2] = tc_r32(AES_ADDR_RESULT2); + dec_result[3] = tc_r32(AES_ADDR_RESULT3); - tc_wait_ready(AES_ADDR_STATUS); printf("Generated cipher block:\n"); printf("0x%08x 0x%08x 0x%08x 0x%08x\n", - tc_r32(AES_ADDR_RESULT0), tc_r32(AES_ADDR_RESULT1), - tc_r32(AES_ADDR_RESULT2), tc_r32(AES_ADDR_RESULT3)); + enc_result[0], enc_result[1], enc_result[2], enc_result[3]); + printf("Expected cipher block:\n"); + printf("0x%08x 0x%08x 0x%08x 0x%08x\n", + expected[0], expected[1], expected[2], expected[3]); + printf("\n"); + + printf("Generated decipher block:\n"); + printf("0x%08x 0x%08x 0x%08x 0x%08x\n", + dec_result[0], dec_result[1], dec_result[2], dec_result[3]); + printf("Expected decipher block:\n"); + printf("0x%08x 0x%08x 0x%08x 0x%08x\n", + block[0], block[1], block[2], block[3]); + printf("\n"); + +} + + +//------------------------------------------------------------------ +// run_nist_tests() +//------------------------------------------------------------------ +void run_nist_tests() +{ + uint32_t nist_aes128_key[4] = {0x2b7e1516, 0x28aed2a6, 0xabf71588, 0x09cf4f3c}; + uint32_t nist_aes256_key[8] = {0x603deb10, 0x15ca71be, 0x2b73aef0, 0x857d7781, + 0x1f352c07, 0x3b6108d7, 0x2d9810a3, 0x0914dff4}; + + uint32_t nist_plaintext0[4] = {0x6bc1bee2, 0x2e409f96, 0xe93d7e11, 0x7393172a}; + uint32_t nist_plaintext1[4] = {0xae2d8a57, 0x1e03ac9c, 0x9eb76fac, 0x45af8e51}; + uint32_t nist_plaintext2[4] = {0x30c81c46, 0xa35ce411, 0xe5fbc119, 0x1a0a52ef}; + uint32_t nist_plaintext3[4] = {0xf69f2445, 0xdf4f9b17, 0xad2b417b, 0xe66c3710}; + + uint32_t nist_ecb_128_enc_expected0[4] = {0x3ad77bb4, 0x0d7a3660, 0xa89ecaf3, 0x2466ef97}; + uint32_t nist_ecb_128_enc_expected1[4] = {0xf5d3d585, 0x03b9699d, 0xe785895a, 0x96fdbaaf}; + uint32_t nist_ecb_128_enc_expected2[4] = {0x43b1cd7f, 0x598ece23, 0x881b00e3, 0xed030688}; + uint32_t nist_ecb_128_enc_expected3[4] = {0x7b0c785e, 0x27e8ad3f, 0x82232071, 0x04725dd4}; + + uint32_t nist_ecb_256_enc_expected0[4] = {0xf3eed1bd, 0xb5d2a03c, 0x064b5a7e, 0x3db181f8}; + uint32_t nist_ecb_256_enc_expected1[4] = {0x591ccb10, 0xd410ed26, 0xdc5ba74a, 0x31362870}; + uint32_t nist_ecb_256_enc_expected2[4] = {0xb6ed21b9, 0x9ca6f4f9, 0xf153e7b1, 0xbeafed1d}; + uint32_t nist_ecb_256_enc_expected3[4] = {0x23304b7a, 0x39f9f3ff, 0x067d8d8f, 0x9e24ecc7}; + + printf("Running NIST single block test.\n"); + + single_block_test(128, &nist_aes128_key[0], &nist_plaintext0[0], &nist_ecb_128_enc_expected0[0]); + single_block_test(128, &nist_aes128_key[0], &nist_plaintext1[0], &nist_ecb_128_enc_expected1[0]); + single_block_test(128, &nist_aes128_key[0], &nist_plaintext2[0], &nist_ecb_128_enc_expected2[0]); + single_block_test(128, &nist_aes128_key[0], &nist_plaintext3[0], &nist_ecb_128_enc_expected3[0]); + + single_block_test(256, &nist_aes256_key[0], &nist_plaintext0[0], &nist_ecb_256_enc_expected0[0]); + single_block_test(256, &nist_aes256_key[0], &nist_plaintext1[0], &nist_ecb_256_enc_expected1[0]); + single_block_test(256, &nist_aes256_key[0], &nist_plaintext2[0], &nist_ecb_256_enc_expected2[0]); + single_block_test(256, &nist_aes256_key[0], &nist_plaintext3[0], &nist_ecb_256_enc_expected3[0]); } @@ -195,7 +242,7 @@ int main(int argc, char *argv[]) { check_aes_access(); tc_set_debug(1); - nist_single_block_ecb_128(); + run_nist_tests(); return 0; } |