[Cryptech-Commits] [user/sra/libhal] 11/15: Debug hash-testing code.

git at cryptech.is git at cryptech.is
Mon May 25 21:04:26 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 user/sra/libhal.

commit a05b7a97e05c53b70e2edc2cc14a6a4348b38739
Author: Rob Austein <sra at hactrn.net>
Date:   Sun May 24 18:31:21 2015 -0400

    Debug hash-testing code.
---
 hal_io_eim.c      |   2 +-
 hash.c            |   4 +-
 tests/test-hash.c | 184 ++++++++++++++++++++++++++----------------------------
 3 files changed, 90 insertions(+), 100 deletions(-)

diff --git a/hal_io_eim.c b/hal_io_eim.c
index 5936d0c..ee5a5de 100644
--- a/hal_io_eim.c
+++ b/hal_io_eim.c
@@ -151,7 +151,7 @@ hal_error_t hal_io_expected(off_t offset, const uint8_t *expected, size_t len)
   dump("expect", expected, len);
 
   for (i = 0; i < len; i++) {
-    if ((i & 3) == 0 && (err = hal_io_read(offset, buf, sizeof(buf))) != HAL_OK)
+    if ((i & 3) == 0 && (err = hal_io_read(offset + i/4, buf, sizeof(buf))) != HAL_OK)
       return err;
     if (buf[i & 3] != expected[i])
       return HAL_ERROR_IO_UNEXPECTED;
diff --git a/hash.c b/hash.c
index 4a15491..ae49c06 100644
--- a/hash.c
+++ b/hash.c
@@ -147,8 +147,8 @@ static hal_error_t hash_write_block(const off_t block_addr,
  * Read hash result from core.
  */
 
-static hal_error_t hash_read_digest(const uint8_t digest_addr,
-                                    const uint8_t status_addr,
+static hal_error_t hash_read_digest(const off_t digest_addr,
+                                    const off_t status_addr,
                                     uint8_t *digest,
                                     const size_t digest_length)
 {
diff --git a/tests/test-hash.c b/tests/test-hash.c
index c13e49c..4230c69 100644
--- a/tests/test-hash.c
+++ b/tests/test-hash.c
@@ -12,26 +12,23 @@
 
 /* Usual NIST sample messages. */
 
+/* "abc" */
 static const uint8_t nist_512_single[] = { /* 3 bytes */
   0x61, 0x62, 0x63
 };
 
-static const uint8_t sha1_single_digest[] = { /* 40 bytes */
-  0x61, 0x39, 0x39, 0x39, 0x33, 0x65, 0x33, 0x36, 0x34, 0x37, 0x30, 0x36,
-  0x38, 0x31, 0x36, 0x61, 0x62, 0x61, 0x33, 0x65, 0x32, 0x35, 0x37, 0x31,
-  0x37, 0x38, 0x35, 0x30, 0x63, 0x32, 0x36, 0x63, 0x39, 0x63, 0x64, 0x30,
-  0x64, 0x38, 0x39, 0x64
+static const uint8_t sha1_single_digest[] = { /* 20 bytes */
+  0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e, 0x25, 0x71,
+  0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d
 };
 
-static const uint8_t sha256_single_digest[] = { /* 64 bytes */
-  0x62, 0x61, 0x37, 0x38, 0x31, 0x36, 0x62, 0x66, 0x38, 0x66, 0x30, 0x31,
-  0x63, 0x66, 0x65, 0x61, 0x34, 0x31, 0x34, 0x31, 0x34, 0x30, 0x64, 0x65,
-  0x35, 0x64, 0x61, 0x65, 0x32, 0x32, 0x32, 0x33, 0x62, 0x30, 0x30, 0x33,
-  0x36, 0x31, 0x61, 0x33, 0x39, 0x36, 0x31, 0x37, 0x37, 0x61, 0x39, 0x63,
-  0x62, 0x34, 0x31, 0x30, 0x66, 0x66, 0x36, 0x31, 0x66, 0x32, 0x30, 0x30,
-  0x31, 0x35, 0x61, 0x64
+static const uint8_t sha256_single_digest[] = { /* 32 bytes */
+  0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde,
+  0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
+  0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
 };
 
+/* "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" */
 static const uint8_t nist_512_double[] = { /* 56 bytes */
   0x61, 0x62, 0x63, 0x64, 0x62, 0x63, 0x64, 0x65, 0x63, 0x64, 0x65, 0x66,
   0x64, 0x65, 0x66, 0x67, 0x65, 0x66, 0x67, 0x68, 0x66, 0x67, 0x68, 0x69,
@@ -40,51 +37,40 @@ static const uint8_t nist_512_double[] = { /* 56 bytes */
   0x6d, 0x6e, 0x6f, 0x70, 0x6e, 0x6f, 0x70, 0x71
 };
 
-static const uint8_t sha1_double_digest[] = { /* 40 bytes */
-  0x38, 0x34, 0x39, 0x38, 0x33, 0x65, 0x34, 0x34, 0x31, 0x63, 0x33, 0x62,
-  0x64, 0x32, 0x36, 0x65, 0x62, 0x61, 0x61, 0x65, 0x34, 0x61, 0x61, 0x31,
-  0x66, 0x39, 0x35, 0x31, 0x32, 0x39, 0x65, 0x35, 0x65, 0x35, 0x34, 0x36,
-  0x37, 0x30, 0x66, 0x31
+static const uint8_t sha1_double_digest[] = { /* 20 bytes */
+  0x84, 0x98, 0x3e, 0x44, 0x1c, 0x3b, 0xd2, 0x6e, 0xba, 0xae, 0x4a, 0xa1,
+  0xf9, 0x51, 0x29, 0xe5, 0xe5, 0x46, 0x70, 0xf1
 };
 
-static const uint8_t sha256_double_digest[] = { /* 64 bytes */
-  0x32, 0x34, 0x38, 0x64, 0x36, 0x61, 0x36, 0x31, 0x64, 0x32, 0x30, 0x36,
-  0x33, 0x38, 0x62, 0x38, 0x65, 0x35, 0x63, 0x30, 0x32, 0x36, 0x39, 0x33,
-  0x30, 0x63, 0x33, 0x65, 0x36, 0x30, 0x33, 0x39, 0x61, 0x33, 0x33, 0x63,
-  0x65, 0x34, 0x35, 0x39, 0x36, 0x34, 0x66, 0x66, 0x32, 0x31, 0x36, 0x37,
-  0x66, 0x36, 0x65, 0x63, 0x65, 0x64, 0x64, 0x34, 0x31, 0x39, 0x64, 0x62,
-  0x30, 0x36, 0x63, 0x31
+static const uint8_t sha256_double_digest[] = { /* 32 bytes */
+  0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, 0xc0, 0x26, 0x93,
+  0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
+  0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1
 };
 
+/* "abc" */
 static const uint8_t nist_1024_single[] = { /* 3 bytes */
   0x61, 0x62, 0x63
 };
 
-static const uint8_t sha384_single_digest[] = { /* 96 bytes */
-  0x63, 0x62, 0x30, 0x30, 0x37, 0x35, 0x33, 0x66, 0x34, 0x35, 0x61, 0x33,
-  0x35, 0x65, 0x38, 0x62, 0x62, 0x35, 0x61, 0x30, 0x33, 0x64, 0x36, 0x39,
-  0x39, 0x61, 0x63, 0x36, 0x35, 0x30, 0x30, 0x37, 0x32, 0x37, 0x32, 0x63,
-  0x33, 0x32, 0x61, 0x62, 0x30, 0x65, 0x64, 0x65, 0x64, 0x31, 0x36, 0x33,
-  0x31, 0x61, 0x38, 0x62, 0x36, 0x30, 0x35, 0x61, 0x34, 0x33, 0x66, 0x66,
-  0x35, 0x62, 0x65, 0x64, 0x38, 0x30, 0x38, 0x36, 0x30, 0x37, 0x32, 0x62,
-  0x61, 0x31, 0x65, 0x37, 0x63, 0x63, 0x32, 0x33, 0x35, 0x38, 0x62, 0x61,
-  0x65, 0x63, 0x61, 0x31, 0x33, 0x34, 0x63, 0x38, 0x32, 0x35, 0x61, 0x37
+static const uint8_t sha384_single_digest[] = { /* 48 bytes */
+  0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b, 0xb5, 0xa0, 0x3d, 0x69,
+  0x9a, 0xc6, 0x50, 0x07, 0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63,
+  0x1a, 0x8b, 0x60, 0x5a, 0x43, 0xff, 0x5b, 0xed, 0x80, 0x86, 0x07, 0x2b,
+  0xa1, 0xe7, 0xcc, 0x23, 0x58, 0xba, 0xec, 0xa1, 0x34, 0xc8, 0x25, 0xa7
 };
 
-static const uint8_t sha512_single_digest[] = { /* 128 bytes */
-  0x64, 0x64, 0x61, 0x66, 0x33, 0x35, 0x61, 0x31, 0x39, 0x33, 0x36, 0x31,
-  0x37, 0x61, 0x62, 0x61, 0x63, 0x63, 0x34, 0x31, 0x37, 0x33, 0x34, 0x39,
-  0x61, 0x65, 0x32, 0x30, 0x34, 0x31, 0x33, 0x31, 0x31, 0x32, 0x65, 0x36,
-  0x66, 0x61, 0x34, 0x65, 0x38, 0x39, 0x61, 0x39, 0x37, 0x65, 0x61, 0x32,
-  0x30, 0x61, 0x39, 0x65, 0x65, 0x65, 0x65, 0x36, 0x34, 0x62, 0x35, 0x35,
-  0x64, 0x33, 0x39, 0x61, 0x32, 0x31, 0x39, 0x32, 0x39, 0x39, 0x32, 0x61,
-  0x32, 0x37, 0x34, 0x66, 0x63, 0x31, 0x61, 0x38, 0x33, 0x36, 0x62, 0x61,
-  0x33, 0x63, 0x32, 0x33, 0x61, 0x33, 0x66, 0x65, 0x65, 0x62, 0x62, 0x64,
-  0x34, 0x35, 0x34, 0x64, 0x34, 0x34, 0x32, 0x33, 0x36, 0x34, 0x33, 0x63,
-  0x65, 0x38, 0x30, 0x65, 0x32, 0x61, 0x39, 0x61, 0x63, 0x39, 0x34, 0x66,
-  0x61, 0x35, 0x34, 0x63, 0x61, 0x34, 0x39, 0x66
+static const uint8_t sha512_single_digest[] = { /* 64 bytes */
+  0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba, 0xcc, 0x41, 0x73, 0x49,
+  0xae, 0x20, 0x41, 0x31, 0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2,
+  0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a, 0x21, 0x92, 0x99, 0x2a,
+  0x27, 0x4f, 0xc1, 0xa8, 0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd,
+  0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e, 0x2a, 0x9a, 0xc9, 0x4f,
+  0xa5, 0x4c, 0xa4, 0x9f
 };
 
+/* "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
+   "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" */
 static const uint8_t nist_1024_double[] = { /* 112 bytes */
   0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x62, 0x63, 0x64, 0x65,
   0x66, 0x67, 0x68, 0x69, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a,
@@ -98,64 +84,58 @@ static const uint8_t nist_1024_double[] = { /* 112 bytes */
   0x72, 0x73, 0x74, 0x75
 };
 
-static const uint8_t sha384_double_digest[] = { /* 96 bytes */
-  0x30, 0x39, 0x33, 0x33, 0x30, 0x63, 0x33, 0x33, 0x66, 0x37, 0x31, 0x31,
-  0x34, 0x37, 0x65, 0x38, 0x33, 0x64, 0x31, 0x39, 0x32, 0x66, 0x63, 0x37,
-  0x38, 0x32, 0x63, 0x64, 0x31, 0x62, 0x34, 0x37, 0x35, 0x33, 0x31, 0x31,
-  0x31, 0x62, 0x31, 0x37, 0x33, 0x62, 0x33, 0x62, 0x30, 0x35, 0x64, 0x32,
-  0x32, 0x66, 0x61, 0x30, 0x38, 0x30, 0x38, 0x36, 0x65, 0x33, 0x62, 0x30,
-  0x66, 0x37, 0x31, 0x32, 0x66, 0x63, 0x63, 0x37, 0x63, 0x37, 0x31, 0x61,
-  0x35, 0x35, 0x37, 0x65, 0x32, 0x64, 0x62, 0x39, 0x36, 0x36, 0x63, 0x33,
-  0x65, 0x39, 0x66, 0x61, 0x39, 0x31, 0x37, 0x34, 0x36, 0x30, 0x33, 0x39
+static const uint8_t sha384_double_digest[] = { /* 48 bytes */
+  0x09, 0x33, 0x0c, 0x33, 0xf7, 0x11, 0x47, 0xe8, 0x3d, 0x19, 0x2f, 0xc7,
+  0x82, 0xcd, 0x1b, 0x47, 0x53, 0x11, 0x1b, 0x17, 0x3b, 0x3b, 0x05, 0xd2,
+  0x2f, 0xa0, 0x80, 0x86, 0xe3, 0xb0, 0xf7, 0x12, 0xfc, 0xc7, 0xc7, 0x1a,
+  0x55, 0x7e, 0x2d, 0xb9, 0x66, 0xc3, 0xe9, 0xfa, 0x91, 0x74, 0x60, 0x39
 };
 
-static const uint8_t sha512_double_digest[] = { /* 128 bytes */
-  0x38, 0x65, 0x39, 0x35, 0x39, 0x62, 0x37, 0x35, 0x64, 0x61, 0x65, 0x33,
-  0x31, 0x33, 0x64, 0x61, 0x38, 0x63, 0x66, 0x34, 0x66, 0x37, 0x32, 0x38,
-  0x31, 0x34, 0x66, 0x63, 0x31, 0x34, 0x33, 0x66, 0x38, 0x66, 0x37, 0x37,
-  0x37, 0x39, 0x63, 0x36, 0x65, 0x62, 0x39, 0x66, 0x37, 0x66, 0x61, 0x31,
-  0x37, 0x32, 0x39, 0x39, 0x61, 0x65, 0x61, 0x64, 0x62, 0x36, 0x38, 0x38,
-  0x39, 0x30, 0x31, 0x38, 0x35, 0x30, 0x31, 0x64, 0x32, 0x38, 0x39, 0x65,
-  0x34, 0x39, 0x30, 0x30, 0x66, 0x37, 0x65, 0x34, 0x33, 0x33, 0x31, 0x62,
-  0x39, 0x39, 0x64, 0x65, 0x63, 0x34, 0x62, 0x35, 0x34, 0x33, 0x33, 0x61,
-  0x63, 0x37, 0x64, 0x33, 0x32, 0x39, 0x65, 0x65, 0x62, 0x36, 0x64, 0x64,
-  0x32, 0x36, 0x35, 0x34, 0x35, 0x65, 0x39, 0x36, 0x65, 0x35, 0x35, 0x62,
-  0x38, 0x37, 0x34, 0x62, 0x65, 0x39, 0x30, 0x39
+static const uint8_t sha512_double_digest[] = { /* 64 bytes */
+  0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda, 0x8c, 0xf4, 0xf7, 0x28,
+  0x14, 0xfc, 0x14, 0x3f, 0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f, 0x7f, 0xa1,
+  0x72, 0x99, 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18, 0x50, 0x1d, 0x28, 0x9e,
+  0x49, 0x00, 0xf7, 0xe4, 0x33, 0x1b, 0x99, 0xde, 0xc4, 0xb5, 0x43, 0x3a,
+  0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26, 0x54, 0x5e, 0x96, 0xe5, 0x5b,
+  0x87, 0x4b, 0xe9, 0x09
 };
 
-static int test_hash(hal_error_t (*hash)(void *, const uint8_t *, const size_t, uint8_t *, const size_t),
-		     const uint8_t * const data, const size_t data_len,
-		     const uint8_t * const result, const size_t result_len,
-		     const char * const comment)
+static int _test_hash(hal_error_t (*hash)(void *, const uint8_t *, const size_t, uint8_t *, const size_t),
+		      const uint8_t * const data, const size_t data_len,
+		      const uint8_t * const result, const size_t result_len,
+		      const char * const label)
 {
   uint8_t state[512], digest[512];
   hal_error_t err;
 
-  assert(hash != NULL && data != NULL && result != NULL);
+  assert(hash != NULL && data != NULL && result != NULL && label != NULL);
 
   assert(result_len <= sizeof(digest) && hal_hash_state_size() <= sizeof(state));
 
-  if (comment != NULL)
-    printf("%s\n", comment);
+  printf("Starting %s test\n", label);
 
-  printf("Initializing state\n");
   hal_hash_state_initialize(state);
 
-  printf("Hashing data %lu bytes of data\n", (unsigned long) data_len);
   if ((err = hash(state, data, data_len, NULL, 0)) != HAL_OK) {
     printf("Failed: %s\n", hal_error_string(err));
     return 0;
   }
 
-  printf("Finalizing hash and reading %lu bytes of result\n", (unsigned long) result_len);
   if ((err = hash(state, NULL, 0, digest, sizeof(digest))) != HAL_OK) {
     printf("Failed: %s\n", hal_error_string(err));
     return 0;
   }
 
-  printf("Comparing result with known value");
+  printf("Comparing result with known value\n");
   if (memcmp(result, digest, result_len)) {
-    printf("MISMATCH\n");
+    size_t i;
+    printf("MISMATCH\nExpected:");
+    for (i = 0; i < result_len; i++)
+      printf(" %02x", result[i]);
+    printf("\nGot:     ");
+    for (i = 0; i < result_len; i++)
+      printf(" %02x", digest[i]);
+    printf("\n");
     return 0;
   }
 
@@ -163,6 +143,9 @@ static int test_hash(hal_error_t (*hash)(void *, const uint8_t *, const size_t,
     return 1;
 }
 
+#define test_hash(_hash_, _data_, _result_, _label_) \
+  _test_hash(_hash_, _data_, sizeof(_data_), _result_, sizeof(_result_), _label_)
+
 int main (int argc, char *argv[])
 {
   int ok = 1;
@@ -172,25 +155,32 @@ int main (int argc, char *argv[])
    * script, which skips the 224 and 256 options of the SHA-512 core.
    */
 
-  ok &= test_hash(hal_hash_sha1,   nist_512_single,  sizeof(nist_512_single),  sha1_single_digest,   sizeof(sha1_single_digest),
-		  "SHA-1 single block test");
-  ok &= test_hash(hal_hash_sha1,   nist_512_double,  sizeof(nist_512_double),  sha1_double_digest,   sizeof(sha1_double_digest),
-		  "SHA-1 double block test");
-
-  ok &= test_hash(hal_hash_sha256, nist_512_single,  sizeof(nist_512_single),  sha256_single_digest, sizeof(sha256_single_digest),
-		  "SHA-256 single block test");
-  ok &= test_hash(hal_hash_sha256, nist_512_double,  sizeof(nist_512_double),  sha256_double_digest, sizeof(sha256_double_digest),
-		  "SHA-256 double block test");
-
-  ok &= test_hash(hal_hash_sha384, nist_1024_single, sizeof(nist_1024_single), sha384_single_digest, sizeof(sha384_single_digest),
-		  "SHA-384 single block test");
-  ok &= test_hash(hal_hash_sha384, nist_1024_double, sizeof(nist_1024_double), sha384_double_digest, sizeof(sha384_double_digest),
-		  "SHA-384 double block test");
-
-  ok &= test_hash(hal_hash_sha512, nist_1024_single, sizeof(nist_1024_single), sha512_single_digest, sizeof(sha512_single_digest),
-		  "SHA-512 single block test");
-  ok &= test_hash(hal_hash_sha512, nist_1024_double, sizeof(nist_1024_double), sha512_double_digest, sizeof(sha512_double_digest),
-		  "SHA-512 double block test");
+  if (hash_sha1_core_present() == HAL_OK) {
+    ok &= test_hash(hal_hash_sha1,   nist_512_single, sha1_single_digest, "SHA-1 single block");
+    ok &= test_hash(hal_hash_sha1,   nist_512_double, sha1_double_digest, "SHA-1 double block");
+  }
+  else {
+    printf("SHA-1 core not present, skipping tests which depend on it\n");
+  }
+
+  if (hash_sha256_core_present() == HAL_OK) {
+    ok &= test_hash(hal_hash_sha256, nist_512_single, sha256_single_digest, "SHA-256 single block");
+    ok &= test_hash(hal_hash_sha256, nist_512_double, sha256_double_digest, "SHA-256 double block");
+  }
+  else {
+    printf("SHA-256 core not present, skipping tests which depend on it\n");
+  }
+
+  if (hash_sha512_core_present() == HAL_OK) {
+    ok &= test_hash(hal_hash_sha384, nist_1024_single, sha384_single_digest, "SHA-384 single block");
+    ok &= test_hash(hal_hash_sha384, nist_1024_double, sha384_double_digest, "SHA-384 double block");
+
+    ok &= test_hash(hal_hash_sha512, nist_1024_single, sha512_single_digest, "SHA-512 single block");
+    ok &= test_hash(hal_hash_sha512, nist_1024_double, sha512_double_digest, "SHA-512 double block");
+  }
+  else {
+    printf("SHA-512 core not present, skipping tests which depend on it\n");
+  }
 
   return !ok;
 }



More information about the Commits mailing list