static int calc_and_compare_hash_256(const uint8_t *msg, size_t msg_len, const uint8_t *expected) { static unsigned char hash[SHA3_256_DIGEST_LENGTH]; sha3_256(hash, msg, msg_len); return (memcmp(expected, hash, sizeof(hash)) == 0); }
static void sha3_256_t (ut_test_t *const t) { const char *const msg = "abc"; const char *const expected_msg_digest_as_string = "3a985da74fe225b2" "045c172d6bd390bd" "855f086e3e9d525b" "46bfe24511431532"; char msg_digest[sha3_256_digest_lenght]; char msg_digest_as_string[sha3_224_digest_lenght * 2 + 1]; // Two hex digit per byte plus string termination. size_t msg_len = strlen(msg); sha3_256(msg_digest, msg, msg_len); sha3_msg_digest_to_string(msg_digest_as_string, msg_digest, sha3_256_digest_lenght); ut_assert(t, strcmp(expected_msg_digest_as_string, msg_digest_as_string) == 0); }
int sha3_test(const unsigned char *input, unsigned long input_bits, int print) { unsigned char *hash; hash = sha3_224(input, input_bits); if (hash) { if (print) { sha3_print_hash(hash, 224UL, "sha3_224(input, %lu) =", input_bits); } free(hash); } else { return EXIT_FAILURE; } hash = sha3_256(input, input_bits); if (hash) { if (print) { sha3_print_hash(hash, 256UL, "sha3_256(input, %lu) =", input_bits); } free(hash); } else { return EXIT_FAILURE; } hash = sha3_384(input, input_bits); if (hash) { if (print) { sha3_print_hash(hash, 384UL, "sha3_384(input, %lu) =", input_bits); } free(hash); } else { return EXIT_FAILURE; } hash = sha3_512(input, input_bits); if (hash) { if (print) { sha3_print_hash(hash, 512UL, "sha3_512(input, %lu) =", input_bits); } free(hash); } else { return EXIT_FAILURE; } return EXIT_SUCCESS; }