diff options
author | Paul Selkirk <paul@psgd.org> | 2015-02-11 12:13:23 -0500 |
---|---|---|
committer | Paul Selkirk <paul@psgd.org> | 2015-02-11 12:13:23 -0500 |
commit | ba254d4d83bed050f16026b750d6a7d175098c77 (patch) | |
tree | a2b8669f98f7cb4332bd56e0773ac9d6c9e4f5bf /sw/hash_tester.c | |
parent | 5f769e9b78a61d6b69355a6aae8572128a8f54a3 (diff) |
Reformat C code for readability, remove non-API stuff from novena-eim.h, change eim_setup() success value to 0.
Diffstat (limited to 'sw/hash_tester.c')
-rw-r--r-- | sw/hash_tester.c | 286 |
1 files changed, 143 insertions, 143 deletions
diff --git a/sw/hash_tester.c b/sw/hash_tester.c index 4fbeaa3..a0ab0f1 100644 --- a/sw/hash_tester.c +++ b/sw/hash_tester.c @@ -63,13 +63,13 @@ int quiet = 0; int repeat = 0; /* instead of core number 0 we have a page of global registers */ -#define ADDR_GLOBAL_BOARD_TYPE EIM_BASE_ADDR + (0x00 << 2) -#define ADDR_GLOBAL_BITSTREAM_VER EIM_BASE_ADDR + (0x01 << 2) -#define ADDR_GLOBAL_DUMMY_REG EIM_BASE_ADDR + (0xFF << 2) +#define ADDR_GLOBAL_BOARD_TYPE EIM_BASE_ADDR + (0x00 << 2) +#define ADDR_GLOBAL_BITSTREAM_VER EIM_BASE_ADDR + (0x01 << 2) +#define ADDR_GLOBAL_DUMMY_REG EIM_BASE_ADDR + (0xFF << 2) -#define SEGMENT_OFFSET_HASHES EIM_BASE_ADDR + 0x000000 -#define SEGMENT_OFFSET_RNGS EIM_BASE_ADDR + 0x010000 -#define SEGMENT_OFFSET_CIPHERS EIM_BASE_ADDR + 0x020000 +#define SEGMENT_OFFSET_HASHES EIM_BASE_ADDR + 0x000000 +#define SEGMENT_OFFSET_RNGS EIM_BASE_ADDR + 0x010000 +#define SEGMENT_OFFSET_CIPHERS EIM_BASE_ADDR + 0x020000 /* addresses and codes common to all hash cores */ @@ -85,10 +85,10 @@ int repeat = 0; #define ADDR_BLOCK 0x40 #define ADDR_DIGEST 0x80 -#define HASH_CORE_SIZE 0x400 +#define HASH_CORE_SIZE 0x400 /* addresses and codes for the specific hash cores */ -#define SHA1_ADDR_BASE EIM_BASE_ADDR + (1*HASH_CORE_SIZE) +#define SHA1_ADDR_BASE SEGMENT_OFFSET_HASHES + (1*HASH_CORE_SIZE) #define SHA1_ADDR_NAME0 SHA1_ADDR_BASE + ADDR_NAME0 #define SHA1_ADDR_NAME1 SHA1_ADDR_BASE + ADDR_NAME1 #define SHA1_ADDR_VERSION SHA1_ADDR_BASE + ADDR_VERSION @@ -99,7 +99,7 @@ int repeat = 0; #define SHA1_BLOCK_LEN 512 / 8 #define SHA1_DIGEST_LEN 160 / 8 -#define SHA256_ADDR_BASE EIM_BASE_ADDR + (2*HASH_CORE_SIZE) +#define SHA256_ADDR_BASE SEGMENT_OFFSET_HASHES + (2*HASH_CORE_SIZE) #define SHA256_ADDR_NAME0 SHA256_ADDR_BASE + ADDR_NAME0 #define SHA256_ADDR_NAME1 SHA256_ADDR_BASE + ADDR_NAME1 #define SHA256_ADDR_VERSION SHA256_ADDR_BASE + ADDR_VERSION @@ -110,7 +110,7 @@ int repeat = 0; #define SHA256_BLOCK_LEN 512 / 8 #define SHA256_DIGEST_LEN 256 / 8 -#define SHA512_ADDR_BASE EIM_BASE_ADDR + (3*HASH_CORE_SIZE) +#define SHA512_ADDR_BASE SEGMENT_OFFSET_HASHES + (3*HASH_CORE_SIZE) #define SHA512_ADDR_NAME0 SHA512_ADDR_BASE + ADDR_NAME0 #define SHA512_ADDR_NAME1 SHA512_ADDR_BASE + ADDR_NAME1 #define SHA512_ADDR_VERSION SHA512_ADDR_BASE + ADDR_VERSION @@ -300,11 +300,11 @@ const uint8_t SHA512_DOUBLE_DIGEST[] = void dump(char *label, const uint8_t *buf, int len) { if (debug) { - int i; - printf("%s [", label); - 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"); } } @@ -313,9 +313,9 @@ int tc_write(off_t offset, const uint8_t *buf, int len) dump("write ", buf, len); for (; len > 0; offset += 4, buf += 4, len -= 4) { - uint32_t val; - val = htonl(*(uint32_t *)buf); - eim_write_32(offset, &val); + uint32_t val; + val = htonl(*(uint32_t *)buf); + eim_write_32(offset, &val); } return 0; @@ -327,9 +327,9 @@ int tc_read(off_t offset, uint8_t *buf, int len) int rlen = len; for (; rlen > 0; offset += 4, rbuf += 4, rlen -= 4) { - uint32_t val; - eim_read_32(offset, &val); - *(uint32_t *)rbuf = ntohl(val); + uint32_t val; + eim_read_32(offset, &val); + *(uint32_t *)rbuf = ntohl(val); } dump("read ", buf, len); @@ -396,14 +396,14 @@ int tc_wait(off_t offset, uint8_t status) for (i = 0; i < 10; ++i) { if (tc_read(offset, buf, 4) != 0) return 1; - if (buf[3] & status) - return 0; + if (buf[3] & status) + return 0; } fprintf(stderr, "tc_wait timed out\n"); return 1; #endif } - + int tc_wait_ready(off_t offset) { return tc_wait(offset, STATUS_READY_BIT); @@ -414,6 +414,45 @@ int tc_wait_valid(off_t offset) return tc_wait(offset, STATUS_VALID_BIT); } +/* ---------------- sanity test case ---------------- */ + +int TC0() +{ + uint8_t board_type[4] = { 'P', 'V', 'T', '1'}; /* "PVT1" */ + uint8_t bitstream_ver[4] = { 0x00, 0x01, 0x00, 0x0B }; /* v0.1.0b */ + uint8_t t[4]; + + uint8_t seg_rngs_reg_first[4] = { 0xAA, 0xAA, 0xAA, 0xAA}; + uint8_t seg_rngs_reg_second[4] = { 0xBB, 0xBB, 0xBB, 0xBB}; + uint8_t seg_rngs_reg_third[4] = { 0xCC, 0xCC, 0xCC, 0xCC}; + + uint8_t seg_ciphers_reg_first[4] = { 0xDD, 0xDD, 0xDD, 0xDD}; + uint8_t seg_ciphers_reg_second[4] = { 0xEE, 0xEE, 0xEE, 0xEE}; + uint8_t seg_ciphers_reg_third[4] = { 0xFF, 0xFF, 0xFF, 0xFF}; + + if (!quiet) + printf("TC0: Reading board type, version, and dummy reg from global registers.\n"); + + /* write current time into dummy register, then try to read it back + * to make sure that we can actually write something into EIM + */ + (void)time((time_t *)t); + tc_write(ADDR_GLOBAL_DUMMY_REG, (void *)&t, 4); + + return + tc_expected(ADDR_GLOBAL_BOARD_TYPE, board_type, 4) || + tc_expected(ADDR_GLOBAL_BITSTREAM_VER, bitstream_ver, 4) || + tc_expected(ADDR_GLOBAL_DUMMY_REG, (void *)t, 4) || + + tc_expected(SEGMENT_OFFSET_RNGS + (0 << 2), seg_rngs_reg_first, 4) || + tc_expected(SEGMENT_OFFSET_RNGS + (1 << 2), seg_rngs_reg_second, 4) || + tc_expected(SEGMENT_OFFSET_RNGS + (2 << 2), seg_rngs_reg_third, 4) || + + tc_expected(SEGMENT_OFFSET_CIPHERS + (0 << 2), seg_ciphers_reg_first, 4) || + tc_expected(SEGMENT_OFFSET_CIPHERS + (1 << 2), seg_ciphers_reg_second, 4) || + tc_expected(SEGMENT_OFFSET_CIPHERS + (2 << 2), seg_ciphers_reg_third, 4); +} + /* ---------------- SHA-1 test cases ---------------- */ /* TC1: Read name and version from SHA-1 core. */ @@ -424,7 +463,7 @@ int TC1(void) uint8_t version[4] = { 0x30, 0x2e, 0x35, 0x30 }; /* "0.50" */ if (!quiet) - printf("TC1: Reading name, type and version words from SHA-1 core.\n"); + printf("TC1: Reading name, type and version words from SHA-1 core.\n"); return tc_expected(SHA1_ADDR_NAME0, name0, 4) || @@ -440,7 +479,7 @@ int TC2(void) int ret; if (!quiet) - printf("TC2: Single block message test for SHA-1.\n"); + printf("TC2: Single block message test for SHA-1.\n"); /* Write block to SHA-1. */ tc_write(SHA1_ADDR_BLOCK, block, SHA1_BLOCK_LEN); @@ -464,7 +503,7 @@ int TC3(void) int ret; if (!quiet) - printf("TC3: Double block message test for SHA-1.\n"); + 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); @@ -493,7 +532,7 @@ int TC4(void) uint8_t version[4] = { 0x30, 0x2e, 0x38, 0x30 }; /* "0.80" */ if (!quiet) - printf("TC4: Reading name, type and version words from SHA-256 core.\n"); + printf("TC4: Reading name, type and version words from SHA-256 core.\n"); return tc_expected(SHA256_ADDR_NAME0, name0, 4) || @@ -508,7 +547,7 @@ int TC5() const uint8_t *expected = SHA256_SINGLE_DIGEST; if (!quiet) - printf("TC5: Single block message test for SHA-256.\n"); + printf("TC5: Single block message test for SHA-256.\n"); return /* Write block to SHA-256. */ @@ -532,7 +571,7 @@ int TC6() const uint8_t *expected = SHA256_DOUBLE_DIGEST; if (!quiet) - printf("TC6: Double block message test for SHA-256.\n"); + printf("TC6: Double block message test for SHA-256.\n"); return /* Write first block to SHA-256. */ @@ -574,11 +613,11 @@ int TC7() int i, n = 1000; if (!quiet) - printf("TC7: Message with %d blocks test for SHA-256.\n", n); + 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)) - return 1; + return 1; /* Start initial block hashing, wait and check status. */ if (tc_init(SHA256_ADDR_CTRL) || @@ -605,12 +644,12 @@ int TC7() /* TC8: Read name and version from SHA-512 core. */ int TC8() { - uint8_t name0[4] = { 0x73, 0x68, 0x61, 0x32 }; /* "sha2" */ - uint8_t name1[4] = { 0x2d, 0x35, 0x31, 0x32 }; /* "-512" */ - uint8_t version[4] = { 0x30, 0x2e, 0x38, 0x30 }; /* "0.80" */ + uint8_t name0[4] = { 0x73, 0x68, 0x61, 0x32 }; /* "sha2" */ + uint8_t name1[4] = { 0x2d, 0x35, 0x31, 0x32 }; /* "-512" */ + uint8_t version[4] = { 0x30, 0x2e, 0x38, 0x30 }; /* "0.80" */ if (!quiet) - printf("TC8: Reading name, type and version words from SHA-512 core.\n"); + printf("TC8: Reading name, type and version words from SHA-512 core.\n"); return tc_expected(SHA512_ADDR_NAME0, name0, 4) || @@ -638,22 +677,22 @@ int tc9(int mode, const uint8_t *expected, int digest_len) int TC9() { if (!quiet) - printf("TC9-1: Single block message test for SHA-512/224.\n"); + 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; if (!quiet) - printf("TC9-2: Single block message test for SHA-512/256.\n"); + 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; if (!quiet) - printf("TC9-3: Single block message test for SHA-384.\n"); + 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; if (!quiet) - printf("TC9-4: Single block message test for SHA-512.\n"); + 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; @@ -686,68 +725,31 @@ int tc10(int mode, const uint8_t *expected, int digest_len) int TC10() { if (!quiet) - printf("TC10-1: Double block message test for SHA-512/224.\n"); + 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; if (!quiet) - printf("TC10-2: Double block message test for SHA-512/256.\n"); + 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; if (!quiet) - printf("TC10-3: Double block message test for SHA-384.\n"); + 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; if (!quiet) - printf("TC10-4: Double block message test for SHA-512.\n"); + 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; return 0; } -int TC11() -{ - uint8_t board_type[4] = { 'P', 'V', 'T', '1'}; /* "PVT1" */ - uint8_t bitstream_ver[4] = { 0x00, 0x01, 0x00, 0x0B }; /* v0.1.0b */ - uint8_t t[4]; - - uint8_t seg_rngs_reg_first[4] = { 0xAA, 0xAA, 0xAA, 0xAA}; - uint8_t seg_rngs_reg_second[4] = { 0xBB, 0xBB, 0xBB, 0xBB}; - uint8_t seg_rngs_reg_third[4] = { 0xCC, 0xCC, 0xCC, 0xCC}; - - uint8_t seg_ciphers_reg_first[4] = { 0xDD, 0xDD, 0xDD, 0xDD}; - uint8_t seg_ciphers_reg_second[4] = { 0xEE, 0xEE, 0xEE, 0xEE}; - uint8_t seg_ciphers_reg_third[4] = { 0xFF, 0xFF, 0xFF, 0xFF}; - - // write current time into dummy register, then try to read it back - // to make sure that we can actually write something into eim - (void)time((time_t *)t); - tc_write(ADDR_GLOBAL_DUMMY_REG, (void *)&t, 4); - - if (!quiet) - printf("TC11: Reading board type, bitstream version and dummy register from global registers.\n"); - if (!quiet) - printf("TC11: Reading dummy registers from RNG and CIPHER memory segments.\n"); - - return - tc_expected(ADDR_GLOBAL_BOARD_TYPE, board_type, 4) || - tc_expected(ADDR_GLOBAL_BITSTREAM_VER, bitstream_ver, 4) || - tc_expected(ADDR_GLOBAL_DUMMY_REG, (void *)t, 4) || - - tc_expected(SEGMENT_OFFSET_RNGS + (0 << 2), seg_rngs_reg_first, 4) || - tc_expected(SEGMENT_OFFSET_RNGS + (1 << 2), seg_rngs_reg_second, 4) || - tc_expected(SEGMENT_OFFSET_RNGS + (2 << 2), seg_rngs_reg_third, 4) || - - tc_expected(SEGMENT_OFFSET_CIPHERS + (0 << 2), seg_ciphers_reg_first, 4) || - tc_expected(SEGMENT_OFFSET_CIPHERS + (1 << 2), seg_ciphers_reg_second, 4) || - tc_expected(SEGMENT_OFFSET_CIPHERS + (2 << 2), seg_ciphers_reg_third, 4); -} - /* ---------------- main ---------------- */ +/* signal handler for ctrl-c to end repeat testing */ unsigned long iter = 0; struct timeval tv_start, tv_end; void sighandler(int unused) @@ -756,19 +758,19 @@ void sighandler(int unused) 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; + (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); + iter, tv_diff, (double)iter/tv_diff); + exit(EXIT_SUCCESS); } int main(int argc, char *argv[]) { typedef int (*tcfp)(void); + tcfp all_tests[] = { TC0, TC1, TC2, TC3, TC4, TC5, TC6, TC7, TC8, TC9, TC10 }; tcfp sha1_tests[] = { TC1, TC2, TC3 }; tcfp sha256_tests[] = { TC4, TC5, TC6, TC7 }; tcfp sha512_tests[] = { TC8, TC9, TC10 }; - tcfp all_tests[] = { TC1, TC2, TC3, TC4, TC5, TC6, TC7, TC8, TC9, TC10, TC11 }; char *usage = "Usage: %s [-h] [-d] [-q] [-r] tc...\n"; int i, j, opt; @@ -778,99 +780,97 @@ int main(int argc, char *argv[]) case 'h': case '?': printf(usage, argv[0]); - return 0; + return EXIT_SUCCESS; case 'd': debug = 1; break; - case 'q': - quiet = 1; - break; - case 'r': - repeat = 1; - break; + case 'q': + quiet = 1; + break; + case 'r': + repeat = 1; + break; default: fprintf(stderr, usage, argv[0]); - return 1; + return EXIT_FAILURE; } } - // try to setup eim (return value should be 1) - printf("Configuring EIM .. "); - if (eim_setup() < 1) { - printf("ERROR\n"); - return EXIT_FAILURE; - } - else { - printf("EIM Setup ok.\n"); + /* set up EIM */ + if (eim_setup() != 0) { + fprintf(stderr, "EIM setup failed\n"); + return EXIT_FAILURE; } + /* repeat one test until interrupted */ 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*/ + tcfp tc; + if (optind != argc - 1) { + fprintf(stderr, "only one test case can be repeated\n"); + return EXIT_FAILURE; + } + 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 EXIT_FAILURE; + } + tc = (all_tests[j]); + 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 EXIT_SUCCESS; /*NOTREACHED*/ } /* no args == run all tests */ if (optind >= argc) { for (j = 0; j < sizeof(all_tests)/sizeof(all_tests[0]); ++j) if (all_tests[j]() != 0) - return 1; - return 0; + return EXIT_FAILURE; + return EXIT_SUCCESS; } + /* run one or more tests (by number) or groups of tests (by name) */ for (i = optind; i < argc; ++i) { - if (strcmp(argv[i], "sha1") == 0) { + if (strcmp(argv[i], "all") == 0) { + for (j = 0; j < sizeof(all_tests)/sizeof(all_tests[0]); ++j) + if (all_tests[j]() != 0) + return EXIT_FAILURE; + } + else if (strcmp(argv[i], "sha1") == 0) { for (j = 0; j < sizeof(sha1_tests)/sizeof(sha1_tests[0]); ++j) if (sha1_tests[j]() != 0) - return 1; + return EXIT_FAILURE; } else if (strcmp(argv[i], "sha256") == 0) { for (j = 0; j < sizeof(sha256_tests)/sizeof(sha256_tests[0]); ++j) if (sha256_tests[j]() != 0) - return 1; + return EXIT_FAILURE; } else if (strcmp(argv[i], "sha512") == 0) { for (j = 0; j < sizeof(sha512_tests)/sizeof(sha512_tests[0]); ++j) if (sha512_tests[j]() != 0) - return 1; - } - else if (strcmp(argv[i], "all") == 0) { - for (j = 0; j < sizeof(all_tests)/sizeof(all_tests[0]); ++j) - if (all_tests[j]() != 0) - return 1; + return EXIT_FAILURE; } else if (isdigit(argv[i][0]) && - (((j = atoi(argv[i])) > 0) && - (j <= sizeof(all_tests)/sizeof(all_tests[0])))) { - if (all_tests[j - 1]() != 0) - return 1; + (((j = atoi(argv[i])) >= 0) && + (j < sizeof(all_tests)/sizeof(all_tests[0])))) { + if (all_tests[j]() != 0) + return EXIT_FAILURE; } else { fprintf(stderr, "unknown test case %s\n", argv[i]); - return 1; + return EXIT_FAILURE; } } - return 0; + return EXIT_SUCCESS; } |