diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-11-05 19:44:44 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-11-05 19:44:44 +0100 |
commit | 4aef9e49be9e075a0918ef96b79a120ef47d870c (patch) | |
tree | 95725ed7053f1f383d971f0d2751b5b4bc4e5437 /src/test | |
parent | add check rule to the makefile (diff) | |
download | libkeccak-4aef9e49be9e075a0918ef96b79a120ef47d870c.tar.gz libkeccak-4aef9e49be9e075a0918ef96b79a120ef47d870c.tar.bz2 libkeccak-4aef9e49be9e075a0918ef96b79a120ef47d870c.tar.xz |
add test for libkeccak_update
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/test.c | 116 |
1 files changed, 110 insertions, 6 deletions
diff --git a/src/test/test.c b/src/test/test.c index 0b47706..aa51682 100644 --- a/src/test/test.c +++ b/src/test/test.c @@ -177,27 +177,27 @@ static int test_digest_case(const libkeccak_spec_t* restrict spec, const char* r static int test_digest(void) { #define sha3(output, message) \ - (printf("Testing SHA3-"#output"(%s): ", #message), \ + (printf(" Testing SHA3-"#output"(%s): ", #message), \ libkeccak_spec_sha3(&spec, output), \ test_digest_case(&spec, LIBKECCAK_SHA3_SUFFIX, message, 0, answer)) #define keccak(output, message) \ - (printf("Testing Keccak-"#output"(%s): ", #message), \ + (printf(" Testing Keccak-"#output"(%s): ", #message), \ libkeccak_spec_sha3(&spec, output) /* sic! */, \ test_digest_case(&spec, "", message, 0, answer)) #define keccak_bits(output, message, bits) \ - (printf("Testing Keccak-"#output"(%s-%i): ", #message, bits), \ + (printf(" Testing Keccak-"#output"(%s-%i): ", #message, bits), \ libkeccak_spec_sha3(&spec, output) /* sic! */, \ test_digest_case(&spec, "", message, bits, answer)) #define rawshake(semicapacity, output, message) \ - (printf("Testing RawSHAKE-"#semicapacity"(%s, %i): ", #message, output), \ + (printf(" Testing RawSHAKE-"#semicapacity"(%s, %i): ", #message, output), \ libkeccak_spec_rawshake(&spec, semicapacity, output), \ test_digest_case(&spec, LIBKECCAK_RAWSHAKE_SUFFIX, message, 0, answer)) #define rawshake_bits(semicapacity, output, message, bits) \ - (printf("Testing RawSHAKE-"#semicapacity"(%s-%i, %i): ", #message, bits, output), \ + (printf(" Testing RawSHAKE-"#semicapacity"(%s-%i, %i): ", #message, bits, output), \ libkeccak_spec_rawshake(&spec, semicapacity, output), \ test_digest_case(&spec, LIBKECCAK_RAWSHAKE_SUFFIX, message, bits, answer)) #define shake(semicapacity, output, message) \ - (printf("Testing SHAKE-"#semicapacity"(%s, %i): ", #message, output), \ + (printf(" Testing SHAKE-"#semicapacity"(%s, %i): ", #message, output), \ libkeccak_spec_shake(&spec, semicapacity, output), \ test_digest_case(&spec, LIBKECCAK_SHAKE_SUFFIX, message, 0, answer)) @@ -205,6 +205,8 @@ static int test_digest(void) libkeccak_spec_t spec; const char* answer; + printf("Testing libkeccak_digest:\n"); + answer = "6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7"; if (sha3(224, "")) return -1; @@ -297,6 +299,107 @@ static int test_digest(void) } +/** + * Run a test case for `libkeccak_update` + * + * @param spec The specification for the hashing + * @param suffix The message suffix (padding prefix) + * @param message The message to digest + * @param expected_answer The expected answer, must be lowercase + * @return Zero on success, -1 on error + */ +static int test_update_case(const libkeccak_spec_t* restrict spec, const char* restrict suffix, + const char* restrict message, const char* restrict expected_answer) +{ + libkeccak_state_t state; + char* restrict hashsum; + char* restrict hexsum; + char* restrict msg; + int ok; + + if (libkeccak_state_initialise(&state, spec)) + return perror("libkeccak_state_initialise"), -1; + if (hashsum = malloc((spec->output + 7) / 8), hashsum == NULL) + return perror("malloc"), -1; + if (hexsum = malloc((spec->output + 7) / 8 * 2 + 1), hexsum == NULL) + return perror("malloc"), -1; + if (msg = strdup(message), msg == NULL) + return perror("strdup"), -1; + + libkeccak_update(&state, msg, strlen(msg)); + libkeccak_digest(&state, NULL, 0, 0, suffix, hashsum); + libkeccak_state_fast_destroy(&state); + free(msg); + + libkeccak_behex_lower(hexsum, hashsum, (spec->output + 7) / 8); + ok = !strcmp(hexsum, expected_answer); + printf("%s%s\n", ok ? "OK" : "Fail: ", ok ? "" : hexsum); + if (!ok) + printf(" r, c, n = %li, %li, %li\n", spec->bitrate, spec->capacity, spec->output); + + free(hashsum); + free(hexsum); + + return ok - 1; +} + + +/** + * Run test cases for `libkeccak_update` + * + * @return Zero on success, -1 on error + */ +static int test_update(void) +{ +#define sha3(output, message) \ + (printf(" Testing SHA3-"#output"(%s): ", #message), \ + libkeccak_spec_sha3(&spec, output), \ + test_update_case(&spec, LIBKECCAK_SHA3_SUFFIX, message, answer)) + + libkeccak_spec_t spec; + const char* answer; + + printf("Testing libkeccak_update:\n"); + + + answer = "22c8017ac8bcf65f59d1b7e92c9d4c6739d25e34ce5cb608b24ff096"; + if (sha3(224, "withdrew hypothesis snakebird qmc2")) return -1; + + answer = "43808dde2662143dc4eed5dac5e98c74b06711829f02a3b121bd74f3"; + if (sha3(224, "intensifierat sturdiness perl-image-exiftool vingla")) return -1; + + answer = "d32b4ac86065774dee5eb5cdd2f67b4e86501086d7373884e8b20a36"; + if (sha3(224, "timjan avogadro uppdriven lib32-llvm-amdgpu-snapshot")) return -1; + + answer = "efbd76d45bfa952485148f8ad46143897f17c27ffdc8eb7287f9353b"; + if (sha3(224, "grilo-plugins auditorium tull dissimilarity's")) return -1; + + answer = "6705aa36ecf58f333e0e6364ac1d0b7931d402e13282127cfd6f876c"; + if (sha3(224, "royalty tt yellowstone deficiencies")) return -1; + + answer = "803a0ff09dda0df306e483a9f91b20a3dbbf9c2ebb8d0a3b28f3b9e0"; + if (sha3(224, "kdegames-kdiamond tunisisk occurrence's outtalad")) return -1; + + answer = "a64779aca943a6aef1d2e7c9a0f4e997f4dabd1f77112a22121d3ed5"; + if (sha3(224, "chevalier slat's spindel representations")) return -1; + + answer = "f0a3e0587af7723f0aa4719059d3f5107115a5b3667cd5209cc4d867"; + if (sha3(224, "archery lexicographical equine veered")) return -1; + + answer = "312e7e3c6403ab1a086155fb9a52b22a3d0d257876afd2b93fb7272e"; + if (sha3(224, "splay washbasin opposing there")) return -1; + + answer = "270ba05b764221ff5b5d94adfb4fdb1f36f07fe7c438904a5f3df071"; + if (sha3(224, "faktum desist thundered klen")) return -1; + + + printf("\n"); + return 0; + +#undef sha3 +} + + int main(void) { libkeccak_generalised_spec_t gspec; @@ -322,6 +425,7 @@ int main(void) if (test_hex()) return 1; if (test_state(&spec)) return 1; if (test_digest()) return 1; + if (test_update()) return 1; return 0; } |