[Cryptech-Commits] [core/novena_eim] 01/01: add repeat and quiet options to hash_tester
git at cryptech.is
git at cryptech.is
Wed Jan 7 12:09:11 UTC 2015
This is an automated email from the git hooks/post-receive script.
paul at psgd.org pushed a commit to branch gothenburg_test
in repository core/novena_eim.
commit 9a80bf0b2e4acf549a6f917cab9a79d0fcd52661
Author: Paul Selkirk <paul at psgd.org>
Date: Wed Jan 7 05:59:36 2015 -0500
add repeat and quiet options to hash_tester
---
src/sw/hash_tester.c | 136 +++++++++++++++++++++++++++++++++++++--------------
1 file changed, 98 insertions(+), 38 deletions(-)
diff --git a/src/sw/hash_tester.c b/src/sw/hash_tester.c
index 9d86fa8..191138c 100644
--- a/src/sw/hash_tester.c
+++ b/src/sw/hash_tester.c
@@ -50,11 +50,15 @@
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
+#include <sys/time.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <ctype.h>
+#include <signal.h>
int debug = 0;
+int quiet = 0;
+int repeat = 0;
#define EIM_ADDR_BASE 0x08040000
@@ -510,15 +514,20 @@ void eim_copy(void *dest, void *src, int len)
/* ---------------- test-case low-level code ---------------- */
-int tc_write(off_t offset, const uint8_t *buf, int len)
+void dump(char *label, const uint8_t *buf, int len)
{
if (debug) {
- int i;
- printf("write [");
- for (i = 0; i < len; ++i)
- printf(" %02x", buf[i]);
- printf(" ]\n");
+ int i;
+ printf("%s [", label);
+ for (i = 0; i < len; ++i)
+ printf(" %02x", buf[i]);
+ printf(" ]\n");
}
+}
+
+int tc_write(off_t offset, const uint8_t *buf, int len)
+{
+ dump("write ", buf, len);
eim_copy((void *)(mem_8 + offset), (void *)buf, len);
@@ -529,13 +538,7 @@ int tc_read(off_t offset, uint8_t *buf, int len)
{
eim_copy((void *)buf, (void *)(mem_8 + offset), len);
- if (debug) {
- int i;
- printf("read [");
- for (i = 0; i < len; ++i)
- printf(" %02x", buf[i]);
- printf(" ]\n");
- }
+ dump("read ", buf, len);
return 0;
}
@@ -550,13 +553,7 @@ int tc_expected(off_t offset, const uint8_t *expected, int len)
perror("malloc");
return 1;
}
- if (debug) {
- int i;
- printf("expect [");
- for (i = 0; i < len; ++i)
- printf(" %02x", expected[i]);
- printf(" ]\n");
- }
+ dump("expect", expected, len);
if (tc_read(offset, buf, len) != 0)
goto errout;
@@ -620,7 +617,8 @@ int TC1(void)
uint8_t name1[4] = { 0x20, 0x20, 0x20, 0x20 }; /* " " */
uint8_t version[4] = { 0x30, 0x2e, 0x35, 0x30 }; /* "0.50" */
- printf("TC1: Reading name, type and version words from SHA-1 core.\n");
+ if (!quiet)
+ printf("TC1: Reading name, type and version words from SHA-1 core.\n");
return
tc_expected(SHA1_ADDR_NAME0, name0, 4) ||
@@ -634,7 +632,8 @@ int TC2(void)
const uint8_t *block = NIST_512_SINGLE;
const uint8_t *expected = SHA1_SINGLE_DIGEST;
- printf("TC2: Single block message test for SHA-1.\n");
+ if (!quiet)
+ printf("TC2: Single block message test for SHA-1.\n");
return
/* Write block to SHA-1. */
@@ -656,7 +655,8 @@ int TC3(void)
0x4A, 0x56, 0x65, 0x72 };
const uint8_t *expected = SHA1_DOUBLE_DIGEST;
- printf("TC3: Double block message test for SHA-1.\n");
+ if (!quiet)
+ printf("TC3: Double block message test for SHA-1.\n");
return
/* Write first block to SHA-1. */
@@ -684,7 +684,8 @@ int TC4(void)
uint8_t name1[4] = { 0x2d, 0x32, 0x35, 0x36 }; /* "-256" */
uint8_t version[4] = { 0x30, 0x2e, 0x38, 0x30 }; /* "0.80" */
- printf("TC4: Reading name, type and version words from SHA-256 core.\n");
+ if (!quiet)
+ printf("TC4: Reading name, type and version words from SHA-256 core.\n");
return
tc_expected(SHA256_ADDR_NAME0, name0, 4) ||
@@ -698,7 +699,8 @@ int TC5()
const uint8_t *block = NIST_512_SINGLE;
const uint8_t *expected = SHA256_SINGLE_DIGEST;
- printf("TC5: Single block message test for SHA-256.\n");
+ if (!quiet)
+ printf("TC5: Single block message test for SHA-256.\n");
return
/* Write block to SHA-256. */
@@ -721,7 +723,8 @@ int TC6()
0xCC, 0x4B, 0x32, 0xC1, 0xF2, 0x0E, 0x53, 0x3A };
const uint8_t *expected = SHA256_DOUBLE_DIGEST;
- printf("TC6: Double block message test for SHA-256.\n");
+ if (!quiet)
+ printf("TC6: Double block message test for SHA-256.\n");
return
/* Write first block to SHA-256. */
@@ -762,7 +765,8 @@ int TC7()
int i, n = 1000;
- printf("TC7: Message with %d blocks test for SHA-256.\n", n);
+ 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))
@@ -797,7 +801,8 @@ int TC8()
uint8_t name1[4] = { 0x2d, 0x35, 0x31, 0x32 }; /* "-512" */
uint8_t version[4] = { 0x30, 0x2e, 0x38, 0x30 }; /* "0.80" */
- printf("TC8: Reading name, type and version words from SHA-512 core.\n");
+ if (!quiet)
+ printf("TC8: Reading name, type and version words from SHA-512 core.\n");
return
tc_expected(SHA512_ADDR_NAME0, name0, 4) ||
@@ -824,19 +829,23 @@ int tc9(int mode, const uint8_t *expected, int digest_len)
int TC9()
{
- printf("TC9-1: Single block message test for SHA-512/224.\n");
+ 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)
return 1;
- printf("TC9-2: Single block message test for SHA-512/256.\n");
+ if (!quiet)
+ printf("TC9-2: Single block message test for SHA-512/256.\n");
if (tc9(MODE_SHA_512_256, SHA512_256_SINGLE_DIGEST, SHA512_256_DIGEST_LEN) != 0)
return 1;
- printf("TC9-3: Single block message test for SHA-384.\n");
+ if (!quiet)
+ printf("TC9-3: Single block message test for SHA-384.\n");
if (tc9(MODE_SHA_384, SHA384_SINGLE_DIGEST, SHA384_DIGEST_LEN) != 0)
return 1;
- printf("TC9-4: Single block message test for SHA-512.\n");
+ if (!quiet)
+ printf("TC9-4: Single block message test for SHA-512.\n");
if (tc9(MODE_SHA_512, SHA512_SINGLE_DIGEST, SHA512_DIGEST_LEN) != 0)
return 1;
@@ -868,19 +877,23 @@ int tc10(int mode, const uint8_t *expected, int digest_len)
int TC10()
{
- printf("TC10-1: Double block message test for SHA-512/224.\n");
+ 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)
return 1;
- printf("TC10-2: Double block message test for SHA-512/256.\n");
+ if (!quiet)
+ printf("TC10-2: Double block message test for SHA-512/256.\n");
if (tc10(MODE_SHA_512_256, SHA512_256_DOUBLE_DIGEST, SHA512_256_DIGEST_LEN) != 0)
return 1;
- printf("TC10-3: Double block message test for SHA-384.\n");
+ if (!quiet)
+ printf("TC10-3: Double block message test for SHA-384.\n");
if (tc10(MODE_SHA_384, SHA384_DOUBLE_DIGEST, SHA384_DIGEST_LEN) != 0)
return 1;
- printf("TC10-4: Double block message test for SHA-512.\n");
+ if (!quiet)
+ printf("TC10-4: Double block message test for SHA-512.\n");
if (tc10(MODE_SHA_512, SHA512_DOUBLE_DIGEST, SHA512_DIGEST_LEN) != 0)
return 1;
@@ -889,6 +902,20 @@ int TC10()
/* ---------------- main ---------------- */
+unsigned long iter = 0;
+struct timeval tv_start, tv_end;
+void sighandler(int unused)
+{
+ double tv_diff;
+
+ gettimeofday(&tv_end, NULL);
+ tv_diff = (double)(tv_end.tv_sec - tv_start.tv_sec) +
+ (double)(tv_end.tv_usec - tv_start.tv_usec)/1000000;
+ printf("\n%lu iterations in %.3f seconds (%.3f iterations/sec)\n",
+ iter, tv_diff, (double)iter/tv_diff);
+ exit(0);
+}
+
int main(int argc, char *argv[])
{
typedef int (*tcfp)(void);
@@ -897,10 +924,10 @@ int main(int argc, char *argv[])
tcfp sha512_tests[] = { TC8, TC9, TC10 };
tcfp all_tests[] = { TC1, TC2, TC3, TC4, TC5, TC6, TC7, TC8, TC9, TC10 };
- char *usage = "Usage: %s [-h] [-d] tc...\n";
+ char *usage = "Usage: %s [-h] [-d] [-q] [-r] tc...\n";
int i, j, opt;
- while ((opt = getopt(argc, argv, "h?d")) != -1) {
+ while ((opt = getopt(argc, argv, "h?dqr")) != -1) {
switch (opt) {
case 'h':
case '?':
@@ -909,6 +936,12 @@ int main(int argc, char *argv[])
case 'd':
debug = 1;
break;
+ case 'q':
+ quiet = 1;
+ break;
+ case 'r':
+ repeat = 1;
+ break;
default:
fprintf(stderr, usage, argv[0]);
return 1;
@@ -917,6 +950,33 @@ int main(int argc, char *argv[])
setup_fpga();
+ if (repeat) {
+ tcfp tc;
+ if (optind != argc - 1) {
+ fprintf(stderr, "only one test case can be repeated\n");
+ return 1;
+ }
+ j = atoi(argv[optind]);
+ if (j <= 0 || j > sizeof(all_tests)/sizeof(all_tests[0])) {
+ fprintf(stderr, "invalid test number %s\n", argv[optind]);
+ return 1;
+ }
+ tc = (all_tests[j - 1]);
+ srand(time(NULL));
+ signal(SIGINT, sighandler);
+ gettimeofday(&tv_start, NULL);
+ while (1) {
+ ++iter;
+ if ((iter & 0xffff) == 0) {
+ printf(".");
+ fflush(stdout);
+ }
+ if (tc() != 0)
+ sighandler(0);
+ }
+ return 0; /*NOTREACHED*/
+ }
+
/* no args == run all tests */
if (optind >= argc) {
for (j = 0; j < sizeof(all_tests)/sizeof(all_tests[0]); ++j)
More information about the Commits
mailing list