#include #define TEST_NAME "hash" #include #include #include #include #include #include #include #include "sodium.h" #include "quirks.h" static unsigned char x[] = "testing\n"; static unsigned char x2[] = "The Conscience of a Hacker is a small essay written January 8, 1986 by a " "computer security hacker who went by the handle of The Mentor, who " "belonged to the 2nd generation of Legion of Doom."; static unsigned char h[crypto_hash_BYTES]; // Ground Truth static unsigned int h_x_sha512[crypto_hash_BYTES] = {0x24, 0xf9, 0x50, 0xaa, 0xc7, 0xb9, 0xea, 0x9b, 0x3c, 0xb7, 0x28, 0x22, 0x8a, 0x0c, 0x82, 0xb6, 0x7c, 0x39, 0xe9, 0x6b, 0x4b, 0x34, 0x47, 0x98, 0x87, 0x0d, 0x5d, 0xae, 0xe9, 0x3e, 0x3a, 0xe5, 0x93, 0x1b, 0xaa, 0xe8, 0xc7, 0xca, 0xcf, 0xea, 0x4b, 0x62, 0x94, 0x52, 0xc3, 0x80, 0x26, 0xa8, 0x1d, 0x13, 0x8b, 0xc7, 0xaa, 0xd1, 0xaf, 0x3e, 0xf7, 0xbf, 0xd5, 0xec, 0x64, 0x6d, 0x6c, 0x28}; static unsigned int h_x2_sha512[crypto_hash_BYTES] = {0xa7, 0x7a, 0xbe, 0x1c, 0xcf, 0x8f, 0x54, 0x97, 0xe2, 0x28, 0xfb, 0xc0, 0xac, 0xd7, 0x3a, 0x52, 0x1e, 0xde, 0xdb, 0x21, 0xb8, 0x97, 0x26, 0x68, 0x4a, 0x6e, 0xbb, 0xc3, 0xba, 0xa3, 0x23, 0x61, 0xac, 0xa5, 0xa2, 0x44, 0xda, 0xa8, 0x4f, 0x24, 0xbf, 0x19, 0xc6, 0x8b, 0xaf, 0x78, 0xe6, 0x90, 0x76, 0x25, 0xa6, 0x59, 0xb1, 0x54, 0x79, 0xeb, 0x7b, 0xd4, 0x26, 0xfc, 0x62, 0xaa, 0xfa, 0x73}; static unsigned int h_x_sha256[crypto_hash_sha256_BYTES] = {0x12, 0xa6, 0x1f, 0x4e, 0x17, 0x3f, 0xb3, 0xa1, 0x1c, 0x05, 0xd6, 0x47, 0x1f, 0x74, 0x72, 0x8f, 0x76, 0x23, 0x1b, 0x4a, 0x5f, 0xcd, 0x96, 0x67, 0xce, 0xf3, 0xaf, 0x87, 0xa3, 0xae, 0x4d, 0xc2}; static unsigned int h_x2_sha256[crypto_hash_sha256_BYTES] = {0x71, 0xcc, 0x81, 0x23, 0xfe, 0xf8, 0xc2, 0x36, 0xe4, 0x51, 0xd3, 0xc3, 0xdd, 0xf1, 0xad, 0xae, 0x9a, 0xa6, 0xcd, 0x95, 0x21, 0xe7, 0x04, 0x17, 0x69, 0xd7, 0x37, 0x02, 0x49, 0x00, 0xa0, 0x3a}; int libsodium_hash_test(void) { size_t i; // SHA512 crypto_hash(h, x, sizeof x - 1U); printf("\nsha512(\"testing\\n\") = "); for (i = 0; i < crypto_hash_BYTES; ++i) { printf("%02x", (unsigned int) h[i]); assert((unsigned int)h[i] == h_x_sha512[i]); } printf("\n"); crypto_hash(h, x2, sizeof(x2) - 1U); printf("\nsha512(\"%s\") = ", x2); for (i = 0; i < crypto_hash_BYTES; ++i) { printf("%02x", (unsigned int) h[i]); assert((unsigned int)h[i] == h_x2_sha512[i]); } printf("\n"); // SHA256 crypto_hash_sha256(h, x, sizeof(x) - 1U); printf("\nsha256(\"testing\\n\") = "); for (i = 0; i < crypto_hash_sha256_BYTES; ++i) { printf("%02x", (unsigned int) h[i]); assert((unsigned int)h[i] == h_x_sha256[i]); } printf("\n"); crypto_hash_sha256(h, x2, sizeof(x2) - 1U); printf("\nsha256(\"%s\") = ", x2); for (i = 0; i < crypto_hash_sha256_BYTES; ++i) { printf("%02x", (unsigned int) h[i]); assert((unsigned int)h[i] == h_x2_sha256[i]); } printf("\n"); assert(crypto_hash_bytes() > 0U); assert(strcmp(crypto_hash_primitive(), "sha512") == 0); assert(crypto_hash_sha256_bytes() > 0U); assert(crypto_hash_sha512_bytes() >= crypto_hash_sha256_bytes()); assert(crypto_hash_sha512_bytes() == crypto_hash_bytes()); assert(crypto_hash_sha256_statebytes() == sizeof(crypto_hash_sha256_state)); assert(crypto_hash_sha512_statebytes() == sizeof(crypto_hash_sha512_state)); printf("\n====== HASH TEST PASS ======\n"); return 0; } MSH_CMD_EXPORT(libsodium_hash_test, libsodium hash test)