[Cryptech-Commits] [core/platform/novena] 01/01: Use correct addresses for 256-bit keys. Report whether tests passed instead of trusting human beings to read hex. Other trivial cleanups.

git at cryptech.is git at cryptech.is
Sun May 17 16:57:28 UTC 2015


This is an automated email from the git hooks/post-receive script.

sra at hactrn.net pushed a commit to branch master
in repository core/platform/novena.

commit d6ff18eac146c5a933eb7aadb034624d1ef54dc5
Author: Rob Austein <sra at hactrn.net>
Date:   Sun May 17 12:54:51 2015 -0400

    Use correct addresses for 256-bit keys.  Report whether tests passed
    instead of trusting human beings to read hex.  Other trivial cleanups.
---
 sw/aes_tester.c | 169 +++++++++++++++++++++++++++++---------------------------
 1 file changed, 88 insertions(+), 81 deletions(-)

diff --git a/sw/aes_tester.c b/sw/aes_tester.c
index 80acc80..3509950 100644
--- a/sw/aes_tester.c
+++ b/sw/aes_tester.c
@@ -58,26 +58,18 @@
 //------------------------------------------------------------------
 // Robs macros. Scary scary.
 //------------------------------------------------------------------
-#define check(_expr_)\
-  do {\
-  if ((_expr_) != 0)\
-    printf("%s failed\n", #_expr_), 1;\
-  } while (0)
-
-#define set_and_check(_field_)\
-  do {\
-  printf("Setting %s\n", #_field_);\
-  check(tc_write(_field_,  one, sizeof(one)));\
-  check(tc_read( _field_,  res, sizeof(res)));\
-  if (memcmp(one, res, sizeof(one)))\
-    printf("MISMATCH\n");\
-  printf("\n");\
+#define check(_expr_)				\
+  do {						\
+    if ((_expr_) != 0) {			\
+      printf("%s failed\n", #_expr_);		\
+      exit(1);					\
+    }						\
   } while (0)
 
 
 //------------------------------------------------------------------
 //------------------------------------------------------------------
-void check_aes_access()
+static void check_aes_access(void)
 {
   uint8_t name0[4], name1[4], version[4];
 
@@ -92,23 +84,23 @@ void check_aes_access()
 
 //------------------------------------------------------------------
 //------------------------------------------------------------------
-void tc_w32(addr, data)
+static void tc_w32(const off_t addr, const uint32_t data)
 {
   uint8_t w[4];
   w[0] = data >> 24 & 0xff;
   w[1] = data >> 16 & 0xff;
   w[2] = data >> 8  & 0xff;
   w[3] = data       & 0xff;
-  tc_write(addr, w, 4);
+  check(tc_write(addr, w, 4));
 }
 
 
 //------------------------------------------------------------------
 //------------------------------------------------------------------
-uint32_t tc_r32(addr)
+static uint32_t tc_r32(const off_t addr)
 {
   uint8_t w[4];
-  tc_read(addr, w, 4);
+  check(tc_read(addr, w, 4));
   return (uint32_t)((w[0] << 24) + (w[1] << 16) + (w[2] << 8) + w[3]);
 }
 
@@ -116,8 +108,8 @@ uint32_t tc_r32(addr)
 //------------------------------------------------------------------
 // dual_block_test
 //------------------------------------------------------------------
-void dual_block_test(uint8_t keylength, uint32_t *key, uint32_t *block0,
-                     uint32_t *block1, uint32_t *expected)
+static void dual_block_test(const uint8_t keylength, const uint32_t *key, const uint32_t *block0,
+			    const uint32_t *block1, const uint32_t *expected)
 {
 
 }
@@ -128,22 +120,20 @@ void dual_block_test(uint8_t keylength, uint32_t *key, uint32_t *block0,
 //
 // Perform single block tests.
 //------------------------------------------------------------------
-void single_block_test(uint32_t keylength, uint32_t *key, uint32_t *block,
-                       uint32_t *expected)
+static void single_block_test(const uint32_t keylength, const uint32_t *key,
+			      const uint32_t *block, const uint32_t *expected)
 {
   uint32_t enc_result[4];
   uint32_t dec_result[4];
 
   if (VERBOSE) {
-    if (keylength == 256) {
+    if (keylength == 256)
       printf("Writing key 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
 	     key[0], key[1], key[2], key[3],
 	     key[4], key[5], key[6], key[7]);
-    }
-    else {
+    else
       printf("Writing key 0x%08x 0x%08x 0x%08x 0x%08x\n",
 	     key[0], key[1], key[2], key[3]);
-    }
   }
 
   tc_w32(AES_ADDR_KEY0, key[0]);
@@ -152,20 +142,28 @@ void single_block_test(uint32_t keylength, uint32_t *key, uint32_t *block,
   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_KEY4, key[4]);
+    tc_w32(AES_ADDR_KEY5, key[5]);
+    tc_w32(AES_ADDR_KEY6, key[6]);
+    tc_w32(AES_ADDR_KEY7, key[7]);
   }
 
-  if (CHECK_WRITE)
-    {
-      printf("Reading back key: 0x%08x 0x%08x 0x%08x 0x%08x  0x%08x 0x%08x 0x%08x 0x%08x\n",
-	     tc_r32(AES_ADDR_KEY0), tc_r32(AES_ADDR_KEY1),
-	     tc_r32(AES_ADDR_KEY2), tc_r32(AES_ADDR_KEY3),
-	     tc_r32(AES_ADDR_KEY4), tc_r32(AES_ADDR_KEY5),
-	     tc_r32(AES_ADDR_KEY6), tc_r32(AES_ADDR_KEY7));
-    }
+  if (CHECK_WRITE) {
+    const uint32_t
+      k0 = tc_r32(AES_ADDR_KEY0), k1 = tc_r32(AES_ADDR_KEY1),
+      k2 = tc_r32(AES_ADDR_KEY2), k3 = tc_r32(AES_ADDR_KEY3),
+      k4 = tc_r32(AES_ADDR_KEY4), k5 = tc_r32(AES_ADDR_KEY5),
+      k6 = tc_r32(AES_ADDR_KEY6), k7 = tc_r32(AES_ADDR_KEY7);
+    const int
+      ok1 = k0 == key[0] && k1 == key[1] && k2 == key[2] && k3 == key[3],
+      ok2 = k4 == key[4] && k5 == key[5] && k6 == key[6] && k7 == key[7];
+    if (keylength == 256)
+      printf("Reading back key: 0x%08x 0x%08x 0x%08x 0x%08x  0x%08x 0x%08x 0x%08x 0x%08x %s\n",
+	     k0, k1, k2, k3, k4, k5, k6, k7, (ok1 && ok2) ? "OK" : "BAD");
+    else
+      printf("Reading back key: 0x%08x 0x%08x 0x%08x 0x%08x %s\n",
+	     k0, k1, k2, k3, ok1 ? "OK" : "BAD");
+  }
 
   // Performing init i.e. key expansion,
   printf("Doing key init\n");
@@ -178,23 +176,27 @@ void single_block_test(uint32_t keylength, uint32_t *key, uint32_t *block,
 
 
   if (VERBOSE)
-      printf("Writing block 0x%08x 0x%08x 0x%08x 0x%08x\n",
-	     block[0], block[1], block[2], block[3]);
+    printf("Writing block 0x%08x 0x%08x 0x%08x 0x%08x\n",
+	   block[0], block[1], block[2], block[3]);
 
   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]);
 
-  if (CHECK_WRITE)
-    {
-      printf("Reading back block: 0x%08x  0x%08x 0x%08x 0x%08x\n",
-	     tc_r32(AES_ADDR_BLOCK0), tc_r32(AES_ADDR_BLOCK1),
-	     tc_r32(AES_ADDR_BLOCK2), tc_r32(AES_ADDR_BLOCK3));
-    }
+  if (CHECK_WRITE) {
+    const uint32_t
+      b0 = tc_r32(AES_ADDR_BLOCK0), b1 = tc_r32(AES_ADDR_BLOCK1),
+      b2 = tc_r32(AES_ADDR_BLOCK2), b3 = tc_r32(AES_ADDR_BLOCK3);
+    const int
+      ok = b0 == block[0] && b1 == block[1] && b2 == block[2] && b3 == block[3];
+    printf("Reading back block: 0x%08x  0x%08x 0x%08x 0x%08x %s\n",
+	   b0, b1, b2, b3, ok ? "OK" : "BAD");
+  }
 
   if (VERBOSE)
     printf("Starting single block encipher operation\n");
+
   if (keylength == 256)
     tc_w32(AES_ADDR_CONFIG, 0x00000003);
   else
@@ -205,12 +207,13 @@ void single_block_test(uint32_t keylength, uint32_t *key, uint32_t *block,
   if (VERBOSE)
       printf("Checking ready: 0x%08x\n",
 	     tc_r32(AES_ADDR_STATUS));
-  tc_wait_ready(AES_ADDR_STATUS);
+
+  check(tc_wait_ready(AES_ADDR_STATUS));
 
   if (VERBOSE)
-      printf("Ready seen. Result: 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));
+    printf("Ready seen. Result: 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] = tc_r32(AES_ADDR_RESULT0);
   enc_result[1] = tc_r32(AES_ADDR_RESULT1);
@@ -229,56 +232,60 @@ void single_block_test(uint32_t keylength, uint32_t *key, uint32_t *block,
     tc_w32(AES_ADDR_CONFIG, 0x00000000);
   tc_w32(AES_ADDR_CTRL,   0x00000002);
 
-  tc_wait_ready(AES_ADDR_STATUS);
+  check(tc_wait_ready(AES_ADDR_STATUS));
 
   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);
 
-
-  printf("Generated cipher block:\n");
-  printf("0x%08x 0x%08x 0x%08x 0x%08x\n",
+  printf("Generated cipher block: 0x%08x 0x%08x 0x%08x 0x%08x\n",
          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",
+  printf("Expected cipher block:  0x%08x 0x%08x 0x%08x 0x%08x\n",
          expected[0], expected[1], expected[2], expected[3]);
+  if (enc_result[0] == expected[0] && enc_result[1] == expected[1] &&
+      enc_result[2] == expected[2] && enc_result[3] == expected[3])
+    printf("OK\n");
+  else
+    printf("BAD\n");
   printf("\n");
 
-  printf("Generated decipher block:\n");
-  printf("0x%08x 0x%08x 0x%08x 0x%08x\n",
+  printf("Generated decipher block: 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",
+  printf("Expected decipher block:  0x%08x 0x%08x 0x%08x 0x%08x\n",
          block[0], block[1], block[2], block[3]);
+  if (dec_result[0] == block[0] && dec_result[1] == block[1] &&
+      dec_result[2] == block[2] && dec_result[3] == block[3])
+    printf("OK\n");
+  else
+    printf("BAD\n");
   printf("\n");
-
 }
 
 
 //------------------------------------------------------------------
 // run_nist_tests()
 //------------------------------------------------------------------
-void run_nist_tests()
+static 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};
+  static const uint32_t nist_aes128_key[4] = {0x2b7e1516, 0x28aed2a6, 0xabf71588, 0x09cf4f3c};
+  static const uint32_t nist_aes256_key[8] = {0x603deb10, 0x15ca71be, 0x2b73aef0, 0x857d7781,
+					      0x1f352c07, 0x3b6108d7, 0x2d9810a3, 0x0914dff4};
+
+  static const uint32_t nist_plaintext0[4] = {0x6bc1bee2, 0x2e409f96, 0xe93d7e11, 0x7393172a};
+  static const uint32_t nist_plaintext1[4] = {0xae2d8a57, 0x1e03ac9c, 0x9eb76fac, 0x45af8e51};
+  static const uint32_t nist_plaintext2[4] = {0x30c81c46, 0xa35ce411, 0xe5fbc119, 0x1a0a52ef};
+  static const uint32_t nist_plaintext3[4] = {0xf69f2445, 0xdf4f9b17, 0xad2b417b, 0xe66c3710};
+
+  static const uint32_t nist_ecb_128_enc_expected0[4] = {0x3ad77bb4, 0x0d7a3660, 0xa89ecaf3, 0x2466ef97};
+  static const uint32_t nist_ecb_128_enc_expected1[4] = {0xf5d3d585, 0x03b9699d, 0xe785895a, 0x96fdbaaf};
+  static const uint32_t nist_ecb_128_enc_expected2[4] = {0x43b1cd7f, 0x598ece23, 0x881b00e3, 0xed030688};
+  static const uint32_t nist_ecb_128_enc_expected3[4] = {0x7b0c785e, 0x27e8ad3f, 0x82232071, 0x04725dd4};
+
+  static const uint32_t nist_ecb_256_enc_expected0[4] = {0xf3eed1bd, 0xb5d2a03c, 0x064b5a7e, 0x3db181f8};
+  static const uint32_t nist_ecb_256_enc_expected1[4] = {0x591ccb10, 0xd410ed26, 0xdc5ba74a, 0x31362870};
+  static const uint32_t nist_ecb_256_enc_expected2[4] = {0xb6ed21b9, 0x9ca6f4f9, 0xf153e7b1, 0xbeafed1d};
+  static const uint32_t nist_ecb_256_enc_expected3[4] = {0x23304b7a, 0x39f9f3ff, 0x067d8d8f, 0x9e24ecc7};
 
   printf("Running NIST single block test.\n");
 



More information about the Commits mailing list