diff options
-rw-r--r-- | libhashsum_init_hasher_from_string.c | 5 | ||||
-rw-r--r-- | t/blake2b.c | 107 | ||||
-rw-r--r-- | t/blake2s.c | 102 | ||||
-rw-r--r-- | t/misc-tests.c | 14 |
4 files changed, 218 insertions, 10 deletions
diff --git a/libhashsum_init_hasher_from_string.c b/libhashsum_init_hasher_from_string.c index ffd23eb..e7a26d0 100644 --- a/libhashsum_init_hasher_from_string.c +++ b/libhashsum_init_hasher_from_string.c @@ -256,7 +256,10 @@ next: if (*p++ != ']' || *p) goto einval; - r = initfunc(this, n, salti ? salt : NULL, pepperi ? pepper : NULL, key, keyi); + if (keyi > SIZE_MAX / 8U) + goto einval; + + r = initfunc(this, n, salti ? salt : NULL, pepperi ? pepper : NULL, key, keyi * 8U); free(key); return r; diff --git a/t/blake2b.c b/t/blake2b.c index 8f0c877..a497c34 100644 --- a/t/blake2b.c +++ b/t/blake2b.c @@ -23,8 +23,113 @@ static struct testcase testcases[] = { #endif +static int +kat_test(void) +{ + TEST_MAIN("BLAKE2b", BLAKE2B, 0); +} + + int main(void) { - TEST_MAIN("BLAKE2b", BLAKE2B, 0); +#ifdef SUPPORT_BLAKE2B + struct libhashsum_hasher hasher1, hasher2; + char salt1[16], salt2[16], salt3[16], key[64], buf[1024]; + size_t i; +#endif + + int r = kat_test(); + if (r) + return r; + +#ifdef SUPPORT_BLAKE2B + memset(salt1, 0, sizeof(salt1)); + memset(salt2, 0xAB, sizeof(salt2)); + memset(salt3, 0xCD, sizeof(salt3)); + + ASSERT(!libhashsum_init_blake2b_hasher(&hasher1, 0, NULL, NULL, NULL, 0)); + ASSERT(!libhashsum_init_blake2b_hasher(&hasher2, 0, salt1, NULL, NULL, 0)); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE2b")); + ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE2b")); + 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_blake2b_hasher(&hasher1, 0, NULL, NULL, NULL, 0)); + ASSERT(!libhashsum_init_blake2b_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_blake2b_hasher(&hasher1, 0, salt1, NULL, NULL, 0)); + ASSERT(!libhashsum_init_blake2b_hasher(&hasher2, 0, salt2, NULL, NULL, 0)); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE2b")); + ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE2b[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_blake2b_hasher(&hasher1, 0, salt1, NULL, NULL, 0)); + ASSERT(!libhashsum_init_blake2b_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, "BLAKE2b[n=64,salt=00000000000000000000000000000000]")); + ASSERT(!libhashsum_init_hasher_from_string(&hasher2, "BLAKE2b[n=64,salt=abababababababababababababababab]")); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE2b[n=64]")); + ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE2b[n=64,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(hasher1.hash_size == 64U / 8U); + + ASSERT(!libhashsum_init_blake2b_hasher(&hasher1, 0, NULL, salt1, NULL, 0)); + ASSERT(!libhashsum_init_blake2b_hasher(&hasher2, 0, NULL, salt2, NULL, 0)); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE2b")); + ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE2b[pepper=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_blake2b_hasher(&hasher1, 0, NULL, NULL, NULL, 0)); + ASSERT(!libhashsum_init_blake2b_hasher(&hasher2, 0, salt2, salt3, NULL, 0)); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE2b")); + ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE2b[salt=abababababababababababababababab," + "pepper=cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd]")); + 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, "BLAKE2b[salt=01010101010101010101010101010101," + "pepper=02020202020202020202020202020202]")); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE2b[salt=01010101010101010101010101010101," + "pepper=02020202020202020202020202020202]")); + + ASSERT(!libhashsum_init_blake2b_hasher(&hasher1, 0, salt2, NULL, NULL, 0)); + ASSERT(!libhashsum_init_blake2b_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_blake2b_hasher(&hasher1, 0, NULL, NULL, key, 32 * 8U)); + ASSERT(!libhashsum_init_blake2b_hasher(&hasher2, 0, NULL, NULL, key, 64 * 8U)); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE2b[key=000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f]")); + ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE2b[key=000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f" + "202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f]")); + 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, "10ebb67700b1868efb4417987acf4690ae9d972fb7a590c2f02871799aaa4786" + "b5e996e8f0f4eb981fc214b005f42d2ff4233499391653df7aefcbc13fc51568")); + + ASSERT(!libhashsum_init_hasher_from_string(&hasher1, "BLAKE2b[pepper=02020202020202020202020202020202,key=0123456789]")); + ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE2b[pepper=02020202020202020202020202020202,key=0123456789]")); +#endif + + return 0; } 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; } diff --git a/t/misc-tests.c b/t/misc-tests.c index 3942bfb..3d1bb2d 100644 --- a/t/misc-tests.c +++ b/t/misc-tests.c @@ -685,10 +685,10 @@ main(void) CHECK("BLAKE2s[key=]", libhashsum_init_blake2s_hasher(&hasher, 0, NULL, NULL, NULL, 0)); CHECK("BLAKE2s[salt=0123456789abcDEF]", libhashsum_init_blake2s_hasher(&hasher, 0, SALT, NULL, NULL, 0)); CHECK("BLAKE2s[pepper=0123456789abcDEF]", libhashsum_init_blake2s_hasher(&hasher, 0, NULL, SALT, NULL, 0)); - CHECK("BLAKE2s[key=0123456789abcDEF]", libhashsum_init_blake2s_hasher(&hasher, 0, NULL, NULL, SALT, 8)); - CHECK("BLAKE2s[KEY=0123456789abcdef0123456789ABCDEF]", libhashsum_init_blake2s_hasher(&hasher, 0, NULL, NULL, SALT, 16)); + CHECK("BLAKE2s[key=0123456789abcDEF]", libhashsum_init_blake2s_hasher(&hasher, 0, NULL, NULL, SALT, 64)); + CHECK("BLAKE2s[KEY=0123456789abcdef0123456789ABCDEF]", libhashsum_init_blake2s_hasher(&hasher, 0, NULL, NULL, SALT, 128)); CHECK("BLAKE2s[key=0123456789abcdef0123456789ABCDEF0123456789abcdef0123456789ABCDEF]", - libhashsum_init_blake2s_hasher(&hasher, 0, NULL, NULL, SALT SALT, 32)); + libhashsum_init_blake2s_hasher(&hasher, 0, NULL, NULL, SALT SALT, 256)); CHECK("BLAKE2b", libhashsum_init_blake2b_hasher(&hasher, 0, NULL, NULL, NULL, 0)); CHECK("BLAKE2b[]", libhashsum_init_blake2b_hasher(&hasher, 0, NULL, NULL, NULL, 0)); CHECK("BLAKE2b[n=64]", libhashsum_init_blake2b_hasher(&hasher, 64, NULL, NULL, NULL, 0)); @@ -697,18 +697,18 @@ main(void) libhashsum_init_blake2b_hasher(&hasher, 0, SALT, NULL, NULL, 0)); CHECK("BLAKE2b[pepper=0123456789abcDEF0123456789abcDEF]", libhashsum_init_blake2b_hasher(&hasher, 0, NULL, SALT, NULL, 0)); - CHECK("BLAKE2b[key=0123456789abcDEF]", libhashsum_init_blake2b_hasher(&hasher, 0, NULL, NULL, SALT, 8)); + CHECK("BLAKE2b[key=0123456789abcDEF]", libhashsum_init_blake2b_hasher(&hasher, 0, NULL, NULL, SALT, 64)); CHECK("BLAKE2b[salt=00000000000000000000000000000000]", libhashsum_init_blake2b_hasher(&hasher, 0, NULL, NULL, NULL, 0)); CHECK("BLAKE2b[pepper=00000000000000000000000000000000]", libhashsum_init_blake2b_hasher(&hasher, 0, NULL, NULL, NULL, 0)); CHECK("BLAKE2b[key=]", libhashsum_init_blake2b_hasher(&hasher, 0, NULL, NULL, NULL, 0)); - CHECK("BLAKE2b[key=0123456789abcdef0123456789ABCDEF]", libhashsum_init_blake2b_hasher(&hasher, 0, NULL, NULL, SALT, 16)); + CHECK("BLAKE2b[key=0123456789abcdef0123456789ABCDEF]", libhashsum_init_blake2b_hasher(&hasher, 0, NULL, NULL, SALT, 128)); CHECK("BLAKE2b[key=0123456789abcdef0123456789ABCDEF0123456789abcdef0123456789ABCDEF]", - libhashsum_init_blake2b_hasher(&hasher, 0, NULL, NULL, SALT SALT, 32)); + libhashsum_init_blake2b_hasher(&hasher, 0, NULL, NULL, SALT SALT, 256)); CHECK("BLAKE2b[key=0123456789abcdef0123456789ABCDEF0123456789abcdef0123456789ABCDEF" "0123456789abcdef0123456789ABCDEF0123456789abcdef0123456789ABCDEF]", - libhashsum_init_blake2b_hasher(&hasher, 0, NULL, NULL, SALT SALT, 64)); + libhashsum_init_blake2b_hasher(&hasher, 0, NULL, NULL, SALT SALT SALT SALT, 512)); #undef CHECK #undef SALT |