diff options
author | Mattias Andrée <maandree@kth.se> | 2024-09-04 21:41:59 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2024-09-04 21:41:59 +0200 |
commit | 1a9ebda1a5294e91db3015d4536ce36f62ec7058 (patch) | |
tree | bc7118da36d7aadb25aab4feee8c460abf4148e8 /t/blake2s.c | |
parent | Fix and test BLAKE2 support (diff) | |
download | libhashsum-1a9ebda1a5294e91db3015d4536ce36f62ec7058.tar.gz libhashsum-1a9ebda1a5294e91db3015d4536ce36f62ec7058.tar.bz2 libhashsum-1a9ebda1a5294e91db3015d4536ce36f62ec7058.tar.xz |
Add more BLAKE2 tests and fix keyed BLAKE2 support in libhashsum_init_hasher_from_string
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r-- | t/blake2s.c | 102 |
1 files changed, 101 insertions, 1 deletions
diff --git a/t/blake2s.c b/t/blake2s.c index 58408df..5e37552 100644 --- a/t/blake2s.c +++ b/t/blake2s.c @@ -15,8 +15,108 @@ static struct testcase testcases[] = { #endif +static int +kat_test(void) +{ + TEST_MAIN("BLAKE2s", BLAKE2S, 0); +} + + int main(void) { - TEST_MAIN("BLAKE2s", BLAKE2S, 0); +#ifdef SUPPORT_BLAKE2S + struct libhashsum_hasher hasher1, hasher2; + char salt1[8], salt2[8], salt3[8], key[32], buf[1024]; + size_t i; +#endif + + int r = kat_test(); + if (r) + return r; + +#ifdef SUPPORT_BLAKE2S + memset(salt1, 0, sizeof(salt1)); + memset(salt2, 0xAB, sizeof(salt2)); + memset(salt3, 0xCD, sizeof(salt3)); + + ASSERT(!libhashsum_init_blake2s_hasher(&hasher1, 0, NULL, NULL, NULL, 0)); + ASSERT(!libhashsum_init_blake2s_hasher(&hasher2, 0, salt1, NULL, NULL, 0)); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE2s")); + ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE2s")); + 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_blake2s_hasher(&hasher1, 0, NULL, NULL, NULL, 0)); + ASSERT(!libhashsum_init_blake2s_hasher(&hasher2, 0, salt1, NULL, NULL, 0)); + 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_blake2s_hasher(&hasher1, 0, salt1, NULL, NULL, 0)); + ASSERT(!libhashsum_init_blake2s_hasher(&hasher2, 0, salt2, NULL, NULL, 0)); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE2s")); + ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE2s[salt=abababababababab]")); + 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_blake2s_hasher(&hasher1, 0, salt1, NULL, NULL, 0)); + ASSERT(!libhashsum_init_blake2s_hasher(&hasher2, 0, salt2, NULL, NULL, 0)); + 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, "BLAKE2s[n=64,salt=0000000000000000]")); + ASSERT(!libhashsum_init_hasher_from_string(&hasher2, "BLAKE2s[n=64,salt=abababababababab]")); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE2s[n=64]")); + ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE2s[n=64,salt=abababababababab]")); + 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(hasher1.hash_size == 64U / 8U); + + ASSERT(!libhashsum_init_blake2s_hasher(&hasher1, 0, NULL, salt1, NULL, 0)); + ASSERT(!libhashsum_init_blake2s_hasher(&hasher2, 0, NULL, salt2, NULL, 0)); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE2s")); + ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE2s[pepper=abababababababab]")); + 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_blake2s_hasher(&hasher1, 0, NULL, NULL, NULL, 0)); + ASSERT(!libhashsum_init_blake2s_hasher(&hasher2, 0, salt2, salt3, NULL, 0)); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE2s")); + ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE2s[salt=abababababababab,pepper=cdcdcdcdcdcdcdcd]")); + 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_hasher_from_string(&hasher1, "BLAKE2s[salt=0101010101010101,pepper=0202020202020202]")); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE2s[salt=0101010101010101,pepper=0202020202020202]")); + + ASSERT(!libhashsum_init_blake2s_hasher(&hasher1, 0, salt2, NULL, NULL, 0)); + ASSERT(!libhashsum_init_blake2s_hasher(&hasher2, 0, NULL, salt2, NULL, 0)); + 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)); + + for (i = 0; i < sizeof(key); i++) + key[i] = (char)i; + ASSERT(!libhashsum_init_blake2s_hasher(&hasher1, 0, NULL, NULL, key, 16 * 8U)); + ASSERT(!libhashsum_init_blake2s_hasher(&hasher2, 0, NULL, NULL, key, 32 * 8U)); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE2s[key=000102030405060708090a0b0c0d0e0f]")); + ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE2s[key=000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f]")); + 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)); + hex(buf, hasher2.hash_output, hasher2.hash_size); + ASSERT(!strcmp(buf, "48a8997da407876b3d79c0d92325ad3b89cbb754d86ab71aee047ad345fd2c49")); + + ASSERT(!libhashsum_init_hasher_from_string(&hasher1, "BLAKE2s[pepper=0202020202020202,key=0123456789]")); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE2s[pepper=0202020202020202,key=0123456789]")); +#endif + + return 0; } |