[Cryptech-Commits] [sw/libhal] 10/58: First pass on hash test code.
git at cryptech.is
git at cryptech.is
Tue Jul 7 18:24:54 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 sw/libhal.
commit e4a7f76368954edb2622d9b9f333f6b6ef6001e1
Author: Rob Austein <sra at hactrn.net>
Date: Sun May 24 16:31:12 2015 -0400
First pass on hash test code.
---
tests/Makefile.in | 2 +-
tests/test-hash.c | 196 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 197 insertions(+), 1 deletion(-)
diff --git a/tests/Makefile.in b/tests/Makefile.in
index f63e4fc..ba45a57 100644
--- a/tests/Makefile.in
+++ b/tests/Makefile.in
@@ -2,7 +2,7 @@
INC = ../cryptech.h
LIB = ../libcryptech.a
-BIN = test-aes-key-wrap
+BIN = test-aes-key-wrap test-hash
CC = @CC@
CFLAGS = @CFLAGS@ -I..
diff --git a/tests/test-hash.c b/tests/test-hash.c
new file mode 100644
index 0000000..c13e49c
--- /dev/null
+++ b/tests/test-hash.c
@@ -0,0 +1,196 @@
+/*
+ * Test code for hash cores.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <assert.h>
+
+#include <cryptech.h>
+
+/* Usual NIST sample messages. */
+
+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 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 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,
+ 0x67, 0x68, 0x69, 0x6a, 0x68, 0x69, 0x6a, 0x6b, 0x69, 0x6a, 0x6b, 0x6c,
+ 0x6a, 0x6b, 0x6c, 0x6d, 0x6b, 0x6c, 0x6d, 0x6e, 0x6c, 0x6d, 0x6e, 0x6f,
+ 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 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 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 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 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,
+ 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x65, 0x66, 0x67, 0x68,
+ 0x69, 0x6a, 0x6b, 0x6c, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d,
+ 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x68, 0x69, 0x6a, 0x6b,
+ 0x6c, 0x6d, 0x6e, 0x6f, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,
+ 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x6b, 0x6c, 0x6d, 0x6e,
+ 0x6f, 0x70, 0x71, 0x72, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73,
+ 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x6e, 0x6f, 0x70, 0x71,
+ 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 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 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)
+{
+ uint8_t state[512], digest[512];
+ hal_error_t err;
+
+ assert(hash != NULL && data != NULL && result != NULL);
+
+ assert(result_len <= sizeof(digest) && hal_hash_state_size() <= sizeof(state));
+
+ if (comment != NULL)
+ printf("%s\n", comment);
+
+ 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");
+ if (memcmp(result, digest, result_len)) {
+ printf("MISMATCH\n");
+ return 0;
+ }
+
+ printf("OK\n");
+ return 1;
+}
+
+int main (int argc, char *argv[])
+{
+ int ok = 1;
+
+ /*
+ * Missing some tests here because I started from the Cryptlib test
+ * 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");
+
+ return !ok;
+}
More information about the Commits
mailing list