aboutsummaryrefslogtreecommitdiff
path: root/sw/hash_tester.c
diff options
context:
space:
mode:
Diffstat (limited to 'sw/hash_tester.c')
-rw-r--r--sw/hash_tester.c206
1 files changed, 150 insertions, 56 deletions
diff --git a/sw/hash_tester.c b/sw/hash_tester.c
index 7e4f2c8..d3a2964 100644
--- a/sw/hash_tester.c
+++ b/sw/hash_tester.c
@@ -225,6 +225,27 @@ const uint8_t SHA512_DOUBLE_DIGEST[] =
0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26, 0x54,
0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9, 0x09 };
+
+/* ---------------- startup code ---------------- */
+
+static off_t board_addr_base = 0;
+static off_t sha1_addr_base, sha256_addr_base, sha512_addr_base;
+
+static int init(void)
+{
+ static int inited = 0;
+
+ if (inited)
+ return 0;
+
+ sha1_addr_base = tc_core_base("sha1");
+ sha256_addr_base = tc_core_base("sha2-256");
+ sha512_addr_base = tc_core_base("sha2-512");
+
+ inited = 1;
+ return 0;
+}
+
/* ---------------- sanity test case ---------------- */
int TC0()
@@ -234,6 +255,9 @@ int TC0()
uint8_t board_version[4] = NOVENA_BOARD_VERSION;
uint8_t t[4];
+ if (init() != 0)
+ return -1;
+
if (!quiet)
printf("TC0: Reading board type, version, and dummy reg from global registers.\n");
@@ -241,14 +265,14 @@ int TC0()
* to make sure that we can actually write something into EIM
*/
(void)time((time_t *)t);
- if (tc_write(BOARD_ADDR_DUMMY, t, 4) != 0)
+ if (tc_write(board_addr_base + BOARD_ADDR_DUMMY, t, 4) != 0)
return 1;
return
- tc_expected(BOARD_ADDR_NAME0, board_name0, 4) ||
- tc_expected(BOARD_ADDR_NAME1, board_name1, 4) ||
- tc_expected(BOARD_ADDR_VERSION, board_version, 4) ||
- tc_expected(BOARD_ADDR_DUMMY, t, 4);
+ tc_expected(board_addr_base + BOARD_ADDR_NAME0, board_name0, 4) ||
+ tc_expected(board_addr_base + BOARD_ADDR_NAME1, board_name1, 4) ||
+ tc_expected(board_addr_base + BOARD_ADDR_VERSION, board_version, 4) ||
+ tc_expected(board_addr_base + BOARD_ADDR_DUMMY, t, 4);
}
/* ---------------- SHA-1 test cases ---------------- */
@@ -260,13 +284,20 @@ int TC1(void)
uint8_t name1[4] = SHA1_NAME1;
uint8_t version[4] = SHA1_VERSION;
+ if (init() != 0)
+ return -1;
+ if ((sha1_addr_base == 0) && !quiet) {
+ printf("TC1: SHA-1 not present\n");
+ return 0;
+ }
+
if (!quiet)
printf("TC1: Reading name and version words from SHA-1 core.\n");
return
- tc_expected(SHA1_ADDR_NAME0, name0, 4) ||
- tc_expected(SHA1_ADDR_NAME1, name1, 4) ||
- tc_expected(SHA1_ADDR_VERSION, version, 4);
+ tc_expected(sha1_addr_base + SHA1_ADDR_NAME0, name0, 4) ||
+ tc_expected(sha1_addr_base + SHA1_ADDR_NAME1, name1, 4) ||
+ tc_expected(sha1_addr_base + SHA1_ADDR_VERSION, version, 4);
}
/* TC2: SHA-1 Single block message test as specified by NIST. */
@@ -276,16 +307,23 @@ int TC2(void)
const uint8_t *expected = SHA1_SINGLE_DIGEST;
int ret;
+ if (init() != 0)
+ return -1;
+ if ((sha1_addr_base == 0) && !quiet) {
+ printf("TC2: SHA-1 not present\n");
+ return 0;
+ }
+
if (!quiet)
printf("TC2: Single block message test for SHA-1.\n");
/* Write block to SHA-1. */
- tc_write(SHA1_ADDR_BLOCK, block, SHA1_BLOCK_LEN);
+ tc_write(sha1_addr_base + SHA1_ADDR_BLOCK, block, SHA1_BLOCK_LEN);
/* Start initial block hashing, wait and check status. */
- tc_init(SHA1_ADDR_CTRL);
- tc_wait_valid(SHA1_ADDR_STATUS);
+ tc_init(sha1_addr_base + SHA1_ADDR_CTRL);
+ tc_wait_valid(sha1_addr_base + SHA1_ADDR_STATUS);
/* Extract the digest. */
- ret = tc_expected(SHA1_ADDR_DIGEST, expected, SHA1_DIGEST_LEN);
+ ret = tc_expected(sha1_addr_base + SHA1_ADDR_DIGEST, expected, SHA1_DIGEST_LEN);
return ret;
}
@@ -300,23 +338,30 @@ int TC3(void)
const uint8_t *expected = SHA1_DOUBLE_DIGEST;
int ret;
+ if (init() != 0)
+ return -1;
+ if ((sha1_addr_base == 0) && !quiet) {
+ printf("TC3: SHA-1 not present\n");
+ return 0;
+ }
+
if (!quiet)
printf("TC3: Double block message test for SHA-1.\n");
/* Write first block to SHA-1. */
- tc_write(SHA1_ADDR_BLOCK, block[0], SHA1_BLOCK_LEN);
+ tc_write(sha1_addr_base + SHA1_ADDR_BLOCK, block[0], SHA1_BLOCK_LEN);
/* Start initial block hashing, wait and check status. */
- tc_init(SHA1_ADDR_CTRL);
- tc_wait_valid(SHA1_ADDR_STATUS);
+ tc_init(sha1_addr_base + SHA1_ADDR_CTRL);
+ tc_wait_valid(sha1_addr_base + SHA1_ADDR_STATUS);
/* Extract the first digest. */
- tc_expected(SHA1_ADDR_DIGEST, block0_expected, SHA1_DIGEST_LEN);
+ tc_expected(sha1_addr_base + SHA1_ADDR_DIGEST, block0_expected, SHA1_DIGEST_LEN);
/* Write second block to SHA-1. */
- tc_write(SHA1_ADDR_BLOCK, block[1], SHA1_BLOCK_LEN);
+ tc_write(sha1_addr_base + SHA1_ADDR_BLOCK, block[1], SHA1_BLOCK_LEN);
/* Start next block hashing, wait and check status. */
- tc_next(SHA1_ADDR_CTRL);
- tc_wait_valid(SHA1_ADDR_STATUS);
+ tc_next(sha1_addr_base + SHA1_ADDR_CTRL);
+ tc_wait_valid(sha1_addr_base + SHA1_ADDR_STATUS);
/* Extract the second digest. */
- ret = tc_expected(SHA1_ADDR_DIGEST, expected, SHA1_DIGEST_LEN);
+ ret = tc_expected(sha1_addr_base + SHA1_ADDR_DIGEST, expected, SHA1_DIGEST_LEN);
return ret;
}
@@ -329,13 +374,20 @@ int TC4(void)
uint8_t name1[4] = SHA256_NAME1;
uint8_t version[4] = SHA256_VERSION;
+ if (init() != 0)
+ return -1;
+ if ((sha256_addr_base == 0) && !quiet) {
+ printf("TC4: SHA-256 not present\n");
+ return 0;
+ }
+
if (!quiet)
printf("TC4: Reading name and version words from SHA-256 core.\n");
return
- tc_expected(SHA256_ADDR_NAME0, name0, 4) ||
- tc_expected(SHA256_ADDR_NAME1, name1, 4) ||
- tc_expected(SHA256_ADDR_VERSION, version, 4);
+ tc_expected(sha256_addr_base + SHA256_ADDR_NAME0, name0, 4) ||
+ tc_expected(sha256_addr_base + SHA256_ADDR_NAME1, name1, 4) ||
+ tc_expected(sha256_addr_base + SHA256_ADDR_VERSION, version, 4);
}
/* TC5: SHA-256 Single block message test as specified by NIST. */
@@ -344,17 +396,24 @@ int TC5()
const uint8_t *block = NIST_512_SINGLE;
const uint8_t *expected = SHA256_SINGLE_DIGEST;
+ if (init() != 0)
+ return -1;
+ if ((sha256_addr_base == 0) && !quiet) {
+ printf("TC5: SHA-256 not present\n");
+ return 0;
+ }
+
if (!quiet)
printf("TC5: Single block message test for SHA-256.\n");
return
/* Write block to SHA-256. */
- tc_write(SHA256_ADDR_BLOCK, block, SHA256_BLOCK_LEN) ||
+ tc_write(sha256_addr_base + SHA256_ADDR_BLOCK, block, SHA256_BLOCK_LEN) ||
/* Start initial block hashing, wait and check status. */
- tc_init(SHA256_ADDR_CTRL) ||
- tc_wait_valid(SHA256_ADDR_STATUS) ||
+ tc_init(sha256_addr_base + SHA256_ADDR_CTRL) ||
+ tc_wait_valid(sha256_addr_base + SHA256_ADDR_STATUS) ||
/* Extract the digest. */
- tc_expected(SHA256_ADDR_DIGEST, expected, SHA256_DIGEST_LEN);
+ tc_expected(sha256_addr_base + SHA256_ADDR_DIGEST, expected, SHA256_DIGEST_LEN);
}
/* TC6: SHA-256 Double block message test as specified by NIST. */
@@ -368,24 +427,31 @@ int TC6()
0xCC, 0x4B, 0x32, 0xC1, 0xF2, 0x0E, 0x53, 0x3A };
const uint8_t *expected = SHA256_DOUBLE_DIGEST;
+ if (init() != 0)
+ return -1;
+ if ((sha256_addr_base == 0) && !quiet) {
+ printf("TC6: SHA-256 not present\n");
+ return 0;
+ }
+
if (!quiet)
printf("TC6: Double block message test for SHA-256.\n");
return
/* Write first block to SHA-256. */
- tc_write(SHA256_ADDR_BLOCK, block[0], SHA256_BLOCK_LEN) ||
+ tc_write(sha256_addr_base + SHA256_ADDR_BLOCK, block[0], SHA256_BLOCK_LEN) ||
/* Start initial block hashing, wait and check status. */
- tc_init(SHA256_ADDR_CTRL) ||
- tc_wait_valid(SHA256_ADDR_STATUS) ||
+ tc_init(sha256_addr_base + SHA256_ADDR_CTRL) ||
+ tc_wait_valid(sha256_addr_base + SHA256_ADDR_STATUS) ||
/* Extract the first digest. */
- tc_expected(SHA256_ADDR_DIGEST, block0_expected, SHA256_DIGEST_LEN) ||
+ tc_expected(sha256_addr_base + SHA256_ADDR_DIGEST, block0_expected, SHA256_DIGEST_LEN) ||
/* Write second block to SHA-256. */
- tc_write(SHA256_ADDR_BLOCK, block[1], SHA256_BLOCK_LEN) ||
+ tc_write(sha256_addr_base + SHA256_ADDR_BLOCK, block[1], SHA256_BLOCK_LEN) ||
/* Start next block hashing, wait and check status. */
- tc_next(SHA256_ADDR_CTRL) ||
- tc_wait_valid(SHA256_ADDR_STATUS) ||
+ tc_next(sha256_addr_base + SHA256_ADDR_CTRL) ||
+ tc_wait_valid(sha256_addr_base + SHA256_ADDR_STATUS) ||
/* Extract the second digest. */
- tc_expected(SHA256_ADDR_DIGEST, expected, SHA256_DIGEST_LEN);
+ tc_expected(sha256_addr_base + SHA256_ADDR_DIGEST, expected, SHA256_DIGEST_LEN);
}
/* TC7: SHA-256 Huge message test. */
@@ -410,31 +476,38 @@ int TC7()
int i, n = 1000;
+ if (init() != 0)
+ return -1;
+ if ((sha256_addr_base == 0) && !quiet) {
+ printf("TC7: SHA-256 not present\n");
+ return 0;
+ }
+
if (!quiet)
printf("TC7: Message with %d blocks test for SHA-256.\n", n);
/* Write block data to SHA-256. */
- if (tc_write(SHA256_ADDR_BLOCK, block, SHA256_BLOCK_LEN))
+ if (tc_write(sha256_addr_base + SHA256_ADDR_BLOCK, block, SHA256_BLOCK_LEN))
return 1;
/* Start initial block hashing, wait and check status. */
- if (tc_init(SHA256_ADDR_CTRL) ||
- tc_wait_ready(SHA256_ADDR_STATUS))
+ if (tc_init(sha256_addr_base + SHA256_ADDR_CTRL) ||
+ tc_wait_ready(sha256_addr_base + SHA256_ADDR_STATUS))
return 1;
/* First block done. Do the rest. */
for (i = 1; i < n; ++i) {
/* Start next block hashing, wait and check status. */
- if (tc_next(SHA256_ADDR_CTRL) ||
- tc_wait_ready(SHA256_ADDR_STATUS))
+ if (tc_next(sha256_addr_base + SHA256_ADDR_CTRL) ||
+ tc_wait_ready(sha256_addr_base + SHA256_ADDR_STATUS))
return 1;
}
/* XXX valid is probably set at the same time as ready */
- if (tc_wait_valid(SHA256_ADDR_STATUS))
+ if (tc_wait_valid(sha256_addr_base + SHA256_ADDR_STATUS))
return 1;
/* Extract the final digest. */
- return tc_expected(SHA256_ADDR_DIGEST, expected, SHA256_DIGEST_LEN);
+ return tc_expected(sha256_addr_base + SHA256_ADDR_DIGEST, expected, SHA256_DIGEST_LEN);
}
/* ---------------- SHA-512 test cases ---------------- */
@@ -446,13 +519,20 @@ int TC8()
uint8_t name1[4] = SHA512_NAME1;
uint8_t version[4] = SHA512_VERSION;
+ if (init() != 0)
+ return -1;
+ if ((sha512_addr_base == 0) && !quiet) {
+ printf("TC8: SHA-512 not present\n");
+ return 0;
+ }
+
if (!quiet)
printf("TC8: Reading name and version words from SHA-512 core.\n");
return
- tc_expected(SHA512_ADDR_NAME0, name0, 4) ||
- tc_expected(SHA512_ADDR_NAME1, name1, 4) ||
- tc_expected(SHA512_ADDR_VERSION, version, 4);
+ tc_expected(sha512_addr_base + SHA512_ADDR_NAME0, name0, 4) ||
+ tc_expected(sha512_addr_base + SHA512_ADDR_NAME1, name1, 4) ||
+ tc_expected(sha512_addr_base + SHA512_ADDR_VERSION, version, 4);
}
/* TC9: SHA-512 Single block message test as specified by NIST.
@@ -464,16 +544,23 @@ int tc9(int mode, const uint8_t *expected, int digest_len)
return
/* Write block to SHA-512. */
- tc_write(SHA512_ADDR_BLOCK, block, SHA512_BLOCK_LEN) ||
+ tc_write(sha512_addr_base + SHA512_ADDR_BLOCK, block, SHA512_BLOCK_LEN) ||
/* Start initial block hashing, wait and check status. */
- tc_write(SHA512_ADDR_CTRL, init, 4) ||
- tc_wait_valid(SHA512_ADDR_STATUS) ||
+ tc_write(sha512_addr_base + SHA512_ADDR_CTRL, init, 4) ||
+ tc_wait_valid(sha512_addr_base + SHA512_ADDR_STATUS) ||
/* Extract the digest. */
- tc_expected(SHA512_ADDR_DIGEST, expected, digest_len);
+ tc_expected(sha512_addr_base + SHA512_ADDR_DIGEST, expected, digest_len);
}
int TC9()
{
+ if (init() != 0)
+ return -1;
+ if ((sha512_addr_base == 0) && !quiet) {
+ printf("TC9: SHA-512 not present\n");
+ return 0;
+ }
+
if (!quiet)
printf("TC9-1: Single block message test for SHA-512/224.\n");
if (tc9(MODE_SHA_512_224, SHA512_224_SINGLE_DIGEST, SHA512_224_DIGEST_LEN) != 0)
@@ -507,21 +594,28 @@ int tc10(int mode, const uint8_t *expected, int digest_len)
return
/* Write first block to SHA-512. */
- tc_write(SHA512_ADDR_BLOCK, block[0], SHA512_BLOCK_LEN) ||
+ tc_write(sha512_addr_base + SHA512_ADDR_BLOCK, block[0], SHA512_BLOCK_LEN) ||
/* Start initial block hashing, wait and check status. */
- tc_write(SHA512_ADDR_CTRL, init, 4) ||
- tc_wait_ready(SHA512_ADDR_STATUS) ||
+ tc_write(sha512_addr_base + SHA512_ADDR_CTRL, init, 4) ||
+ tc_wait_ready(sha512_addr_base + SHA512_ADDR_STATUS) ||
/* Write second block to SHA-512. */
- tc_write(SHA512_ADDR_BLOCK, block[1], SHA512_BLOCK_LEN) ||
+ tc_write(sha512_addr_base + SHA512_ADDR_BLOCK, block[1], SHA512_BLOCK_LEN) ||
/* Start next block hashing, wait and check status. */
- tc_write(SHA512_ADDR_CTRL, next, 4) ||
- tc_wait_valid(SHA512_ADDR_STATUS) ||
+ tc_write(sha512_addr_base + SHA512_ADDR_CTRL, next, 4) ||
+ tc_wait_valid(sha512_addr_base + SHA512_ADDR_STATUS) ||
/* Extract the digest. */
- tc_expected(SHA512_ADDR_DIGEST, expected, digest_len);
+ tc_expected(sha512_addr_base + SHA512_ADDR_DIGEST, expected, digest_len);
}
int TC10()
{
+ if (init() != 0)
+ return -1;
+ if ((sha512_addr_base == 0) && !quiet) {
+ printf("TC10: SHA-512 not present\n");
+ return 0;
+ }
+
if (!quiet)
printf("TC10-1: Double block message test for SHA-512/224.\n");
if (tc10(MODE_SHA_512_224, SHA512_224_DOUBLE_DIGEST, SHA512_224_DIGEST_LEN) != 0)