From 3a4c26ddeaaffcbd7d808ef6322cfd61f2b84420 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 31 Aug 2024 21:11:07 +0200 Subject: More testing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- blake224.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- blake256.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- blake384.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- blake512.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- common.h | 53 ++++++++++++++++++++++++++++++++++++++++++++++------ md2.c | 1 + 6 files changed, 288 insertions(+), 10 deletions(-) diff --git a/blake224.c b/blake224.c index 04d78f7..a0b6a9e 100644 --- a/blake224.c +++ b/blake224.c @@ -69,8 +69,65 @@ static struct testcase testcases[] = { #endif +static int +kat_test(void) +{ + TEST_MAIN("BLAKE224", BLAKE224); +} + + int main(void) { - TEST_MAIN("BLAKE224", BLAKE224); +#ifdef SUPPORT_BLAKE224 + struct libhashsum_hasher hasher1, hasher2; + char salt1[16], salt2[16]; +#endif + + int r = kat_test(); + if (r) + return r; + +#ifdef SUPPORT_BLAKE224 + memset(salt1, 0, sizeof(salt1)); + memset(salt2, 0xAB, sizeof(salt2)); + + ASSERT(!libhashsum_init_blake224_hasher(&hasher1, NULL)); + ASSERT(!libhashsum_init_blake224_hasher(&hasher2, salt1)); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE224")); + ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE224")); + ASSERT(!hasher1.finalise_const(&hasher1, NULL, 0, 0)); + ASSERT(!hasher2.finalise_const(&hasher2, NULL, 0, 0)); + ASSERT(!memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size)); + + ASSERT(!libhashsum_init_blake224_hasher(&hasher1, NULL)); + ASSERT(!libhashsum_init_blake224_hasher(&hasher2, salt1)); + ASSERT(!hasher1.finalise_const(&hasher1, "testdata", 8U, 0)); + ASSERT(!hasher2.finalise_const(&hasher2, "testdata", 8U, 0)); + ASSERT(!memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size)); + + ASSERT(!libhashsum_init_blake224_hasher(&hasher1, salt1)); + ASSERT(!libhashsum_init_blake224_hasher(&hasher2, salt2)); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE224")); + ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE224[salt=abababababababababababababababab]")); + ASSERT(!hasher1.finalise_const(&hasher1, NULL, 0, 0)); + ASSERT(!hasher2.finalise_const(&hasher2, NULL, 0, 0)); + ASSERT(memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size)); + + ASSERT(!libhashsum_init_blake224_hasher(&hasher1, salt1)); + ASSERT(!libhashsum_init_blake224_hasher(&hasher2, salt2)); + ASSERT(!hasher1.finalise_const(&hasher1, "testdata", 8U, 0)); + ASSERT(!hasher2.finalise_const(&hasher2, "testdata", 8U, 0)); + ASSERT(memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size)); + + ASSERT(!libhashsum_init_hasher_from_string(&hasher1, "BLAKE224[salt=00000000000000000000000000000000]")); + ASSERT(!libhashsum_init_hasher_from_string(&hasher2, "BLAKE224[salt=abababababababababababababababab]")); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE224")); + ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE224[salt=abababababababababababababababab]")); + ASSERT(!hasher1.finalise_const(&hasher1, NULL, 0, 0)); + ASSERT(!hasher2.finalise_const(&hasher2, NULL, 0, 0)); + ASSERT(memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size)); +#endif + + return 0; } diff --git a/blake256.c b/blake256.c index 4e3951b..412b28f 100644 --- a/blake256.c +++ b/blake256.c @@ -69,8 +69,65 @@ static struct testcase testcases[] = { #endif +static int +kat_test(void) +{ + TEST_MAIN("BLAKE256", BLAKE256); +} + + int main(void) { - TEST_MAIN("BLAKE256", BLAKE256); +#ifdef SUPPORT_BLAKE256 + struct libhashsum_hasher hasher1, hasher2; + char salt1[16], salt2[16]; +#endif + + int r = kat_test(); + if (r) + return r; + +#ifdef SUPPORT_BLAKE256 + memset(salt1, 0, sizeof(salt1)); + memset(salt2, 0xAB, sizeof(salt2)); + + ASSERT(!libhashsum_init_blake256_hasher(&hasher1, NULL)); + ASSERT(!libhashsum_init_blake256_hasher(&hasher2, salt1)); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE256")); + ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE256")); + ASSERT(!hasher1.finalise_const(&hasher1, NULL, 0, 0)); + ASSERT(!hasher2.finalise_const(&hasher2, NULL, 0, 0)); + ASSERT(!memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size)); + + ASSERT(!libhashsum_init_blake256_hasher(&hasher1, NULL)); + ASSERT(!libhashsum_init_blake256_hasher(&hasher2, salt1)); + ASSERT(!hasher1.finalise_const(&hasher1, "testdata", 8U, 0)); + ASSERT(!hasher2.finalise_const(&hasher2, "testdata", 8U, 0)); + ASSERT(!memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size)); + + ASSERT(!libhashsum_init_blake256_hasher(&hasher1, salt1)); + ASSERT(!libhashsum_init_blake256_hasher(&hasher2, salt2)); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE256")); + ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE256[salt=abababababababababababababababab]")); + ASSERT(!hasher1.finalise_const(&hasher1, NULL, 0, 0)); + ASSERT(!hasher2.finalise_const(&hasher2, NULL, 0, 0)); + ASSERT(memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size)); + + ASSERT(!libhashsum_init_blake256_hasher(&hasher1, salt1)); + ASSERT(!libhashsum_init_blake256_hasher(&hasher2, salt2)); + ASSERT(!hasher1.finalise_const(&hasher1, "testdata", 8U, 0)); + ASSERT(!hasher2.finalise_const(&hasher2, "testdata", 8U, 0)); + ASSERT(memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size)); + + ASSERT(!libhashsum_init_hasher_from_string(&hasher1, "BLAKE256[salt=00000000000000000000000000000000]")); + ASSERT(!libhashsum_init_hasher_from_string(&hasher2, "BLAKE256[salt=abababababababababababababababab]")); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE256")); + ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE256[salt=abababababababababababababababab]")); + ASSERT(!hasher1.finalise_const(&hasher1, NULL, 0, 0)); + ASSERT(!hasher2.finalise_const(&hasher2, NULL, 0, 0)); + ASSERT(memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size)); +#endif + + return 0; } diff --git a/blake384.c b/blake384.c index a333e09..d4031e5 100644 --- a/blake384.c +++ b/blake384.c @@ -119,8 +119,69 @@ static struct testcase testcases[] = { #endif +static int +kat_test(void) +{ + TEST_MAIN("BLAKE384", BLAKE384); +} + + int main(void) { - TEST_MAIN("BLAKE384", BLAKE384); +#ifdef SUPPORT_BLAKE384 + struct libhashsum_hasher hasher1, hasher2; + char salt1[32], salt2[32]; +#endif + + int r = kat_test(); + if (r) + return r; + +#ifdef SUPPORT_BLAKE384 + memset(salt1, 0, sizeof(salt1)); + memset(salt2, 0xAB, sizeof(salt2)); + + ASSERT(!libhashsum_init_blake384_hasher(&hasher1, NULL)); + ASSERT(!libhashsum_init_blake384_hasher(&hasher2, salt1)); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE384")); + ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE384")); + ASSERT(!hasher1.finalise_const(&hasher1, NULL, 0, 0)); + ASSERT(!hasher2.finalise_const(&hasher2, NULL, 0, 0)); + ASSERT(!memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size)); + + ASSERT(!libhashsum_init_blake384_hasher(&hasher1, NULL)); + ASSERT(!libhashsum_init_blake384_hasher(&hasher2, salt1)); + ASSERT(!hasher1.finalise_const(&hasher1, "testdata", 8U, 0)); + ASSERT(!hasher2.finalise_const(&hasher2, "testdata", 8U, 0)); + ASSERT(!memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size)); + + ASSERT(!libhashsum_init_blake384_hasher(&hasher1, salt1)); + ASSERT(!libhashsum_init_blake384_hasher(&hasher2, salt2)); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE384")); + ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE384[salt=abababababababababababababababab" + "abababababababababababababababab]")); + ASSERT(!hasher1.finalise_const(&hasher1, NULL, 0, 0)); + ASSERT(!hasher2.finalise_const(&hasher2, NULL, 0, 0)); + ASSERT(memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size)); + + ASSERT(!libhashsum_init_blake384_hasher(&hasher1, salt1)); + ASSERT(!libhashsum_init_blake384_hasher(&hasher2, salt2)); + ASSERT(!hasher1.finalise_const(&hasher1, "testdata", 8U, 0)); + ASSERT(!hasher2.finalise_const(&hasher2, "testdata", 8U, 0)); + ASSERT(memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size)); + + ASSERT(!libhashsum_init_hasher_from_string(&hasher1, "BLAKE384[salt=00000000000000000000000000000000" + "00000000000000000000000000000000]")); + ASSERT(!libhashsum_init_hasher_from_string(&hasher2, "BLAKE384[salt=abababababababababababababababab" + "abababababababababababababababab]")); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE384")); + ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE384[salt=abababababababababababababababab" + "abababababababababababababababab]")); + ASSERT(!hasher1.finalise_const(&hasher1, NULL, 0, 0)); + ASSERT(!hasher2.finalise_const(&hasher2, NULL, 0, 0)); + ASSERT(memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size)); +#endif + + return 0; } diff --git a/blake512.c b/blake512.c index 85f13fd..88b20fc 100644 --- a/blake512.c +++ b/blake512.c @@ -188,8 +188,69 @@ static struct testcase testcases[] = { #endif +static int +kat_test(void) +{ + TEST_MAIN("BLAKE512", BLAKE512); +} + + int main(void) { - TEST_MAIN("BLAKE512", BLAKE512); +#ifdef SUPPORT_BLAKE512 + struct libhashsum_hasher hasher1, hasher2; + char salt1[32], salt2[32]; +#endif + + int r = kat_test(); + if (r) + return r; + +#ifdef SUPPORT_BLAKE512 + memset(salt1, 0, sizeof(salt1)); + memset(salt2, 0xAB, sizeof(salt2)); + + ASSERT(!libhashsum_init_blake512_hasher(&hasher1, NULL)); + ASSERT(!libhashsum_init_blake512_hasher(&hasher2, salt1)); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE512")); + ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE512")); + ASSERT(!hasher1.finalise_const(&hasher1, NULL, 0, 0)); + ASSERT(!hasher2.finalise_const(&hasher2, NULL, 0, 0)); + ASSERT(!memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size)); + + ASSERT(!libhashsum_init_blake512_hasher(&hasher1, NULL)); + ASSERT(!libhashsum_init_blake512_hasher(&hasher2, salt1)); + ASSERT(!hasher1.finalise_const(&hasher1, "testdata", 8U, 0)); + ASSERT(!hasher2.finalise_const(&hasher2, "testdata", 8U, 0)); + ASSERT(!memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size)); + + ASSERT(!libhashsum_init_blake512_hasher(&hasher1, salt1)); + ASSERT(!libhashsum_init_blake512_hasher(&hasher2, salt2)); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE512")); + ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE512[salt=abababababababababababababababab" + "abababababababababababababababab]")); + ASSERT(!hasher1.finalise_const(&hasher1, NULL, 0, 0)); + ASSERT(!hasher2.finalise_const(&hasher2, NULL, 0, 0)); + ASSERT(memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size)); + + ASSERT(!libhashsum_init_blake512_hasher(&hasher1, salt1)); + ASSERT(!libhashsum_init_blake512_hasher(&hasher2, salt2)); + ASSERT(!hasher1.finalise_const(&hasher1, "testdata", 8U, 0)); + ASSERT(!hasher2.finalise_const(&hasher2, "testdata", 8U, 0)); + ASSERT(memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size)); + + ASSERT(!libhashsum_init_hasher_from_string(&hasher1, "BLAKE512[salt=00000000000000000000000000000000" + "00000000000000000000000000000000]")); + ASSERT(!libhashsum_init_hasher_from_string(&hasher2, "BLAKE512[salt=abababababababababababababababab" + "abababababababababababababababab]")); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE512")); + ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE512[salt=abababababababababababababababab" + "abababababababababababababababab]")); + ASSERT(!hasher1.finalise_const(&hasher1, NULL, 0, 0)); + ASSERT(!hasher2.finalise_const(&hasher2, NULL, 0, 0)); + ASSERT(memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size)); +#endif + + return 0; } diff --git a/common.h b/common.h index e55ca46..f3dc8de 100644 --- a/common.h +++ b/common.h @@ -108,13 +108,16 @@ static int run_tests(const char *name, enum libhashsum_algorithm algorithm, size_t hash_size, struct testcase *testcases, size_t ntestcases, char hexsum[]) { - struct libhashsum_hasher hasher; + struct libhashsum_hasher hasher, hasher2; + unsigned char custom_hashbuffer[2048]; + unsigned char custom_hashbuffer2[sizeof(custom_hashbuffer)]; + int use_custom_hashbuffer = 0; char *input, *p, bitstr[sizeof(" + b\"1234567\"")]; unsigned char extra_bit; int ok = 1, caseok; size_t i, j, input_string_len, input_total_len, input_size; size_t bits; - for (i = 0; i < ntestcases; i++) { + for (i = 0; i < ntestcases; i++, use_custom_hashbuffer ^= 1) { if (libhashsum_init_hasher(&hasher, algorithm)) { perror("libhashsum_init_hasher"); return 2; @@ -177,17 +180,47 @@ run_tests(const char *name, enum libhashsum_algorithm algorithm, size_t hash_siz } if (bits) *p = testcases[i].input[input_string_len]; - if (hasher.finalise(&hasher, input, input_total_len, (unsigned)bits, input_size)) { - perror("hasher.finalise"); + hasher.hash_output = use_custom_hashbuffer ? custom_hashbuffer : NULL; + if (hasher.finalise_const(&hasher, input, input_total_len, (unsigned)bits)) { + perror("hasher.finalise_const"); return 2; } if (!hasher.hash_output) { - fprintf(stderr, "hasher.finalise did not set hasher.hash_output\n"); + fprintf(stderr, "hasher.finalise_const did not set hasher.hash_output\n"); return 2; } - free(input); +#ifndef NO_CUSTOM_HASH_BUFFER_SUPPORT + if (use_custom_hashbuffer && hasher.hash_output != custom_hashbuffer) { + fprintf(stderr, "hasher.finalise_const did keep custom hash buffer\n"); + return 2; + } +#endif hex(hexsum, hasher.hash_output, hasher.hash_size); ok &= caseok = !testcases[i].output || !strcmp(hexsum, testcases[i].output); + if (libhashsum_init_hasher(&hasher2, algorithm)) { + perror("libhashsum_init_hasher"); + return 2; + } + hasher2.hash_output = use_custom_hashbuffer ? custom_hashbuffer2 : NULL; + if (hasher2.finalise(&hasher2, input, input_total_len, (unsigned)bits, input_size)) { + perror("hasher.finalise"); + return 2; + } + if (!hasher2.hash_output) { + fprintf(stderr, "hasher.finalise did not set hasher.hash_output\n"); + return 2; + } +#ifndef NO_CUSTOM_HASH_BUFFER_SUPPORT + if (use_custom_hashbuffer && hasher2.hash_output != custom_hashbuffer2) { + fprintf(stderr, "hasher.finalise did keep custom hash buffer\n"); + return 2; + } +#endif + if (memcmp(hasher2.hash_output, hasher.hash_output, hasher.hash_size)) { + fprintf(stderr, "hasher.finalise and hasher.finalise_const produced different hashes\n"); + return 2; + } + free(input); input = escape(testcases[i].input, input_string_len); bitstr[0] = '\0'; if (bits) { @@ -229,6 +262,14 @@ run_tests(const char *name, enum libhashsum_algorithm algorithm, size_t hash_siz return run_tests(NAME, LIBHASHSUM_##ID, LIBHASHSUM_##ID##_HASH_SIZE,\ testcases, sizeof(testcases) / sizeof(*testcases), hexsum) +# define ASSERT(ASSERTION)\ + do {\ + if ((ASSERTION))\ + break;\ + fprintf(stderr, "assertion `%s` at line %i failed\n", #ASSERTION, __LINE__);\ + exit(2);\ + } while (0) + #endif diff --git a/md2.c b/md2.c index 9c15cea..7e2712b 100644 --- a/md2.c +++ b/md2.c @@ -1,6 +1,7 @@ /* See LICENSE file for copyright and license details. */ #ifdef SUPPORT_MD2 # define TEST +# define NO_CUSTOM_HASH_BUFFER_SUPPORT # include "common.h" -- cgit v1.2.3-70-g09d2