diff --git a/hash/hash_blake2b.c b/hash/hash_blake2b.c index 69b254ed..3c7b7578 100644 --- a/hash/hash_blake2b.c +++ b/hash/hash_blake2b.c @@ -28,53 +28,45 @@ #endif /** - * @define bb * @brief the size of a data block in bytes */ #define bb 128 /** - * @define KK_MAX * @brief max key length for BLAKE2b */ #define KK_MAX 64 /** - * @define NN_MAX * @brief max length of BLAKE2b digest in bytes */ #define NN_MAX 64 /** - * @define CEIL * @brief ceiling division macro without floats * * @param a dividend - * @param divisor + * @param b divisor */ #define CEIL(a, b) (((a) / (b)) + ((a) % (b) != 0)) /** - * @define MIN * @brief returns minimum value */ #define MIN(a, b) ((a) < (b) ? (a) : (b)) /** - * @define MAX * @brief returns maximum value */ #define MAX(a, b) ((a) > (b) ? (a) : (b)) /** - * @define ROTR64 * @brief macro to rotate 64-bit ints to the right * Ripped from RFC 7693 */ #define ROTR64(n, offset) (((n) >> (offset)) ^ ((n) << (64 - (offset)))) /** - * @define U128_ZERO * @brief zero-value initializer for u128 type */ #define U128_ZERO \ @@ -344,7 +336,8 @@ static int BLAKE2B(uint8_t *dest, block_t *d, size_t dd, u128 ll, uint8_t kk, return 0; } -/* @brief blake2b hash function +/** + * @brief blake2b hash function * * This is the front-end function that sets up the argument for BLAKE2B(). * @@ -398,7 +391,7 @@ uint8_t *blake2b(const uint8_t *message, size_t len, const uint8_t *key, /* If there is a secret key it occupies the first block */ for (i = 0; i < kk; i++) { - long_hold = message[i]; + long_hold = key[i]; long_hold <<= 8 * (i % 8); word_in_block = (i % bb) / 8; @@ -449,15 +442,14 @@ static void assert_bytes(const uint8_t *expected, const uint8_t *actual, { assert(expected[i] == actual[i]); } - - printf("All tests have successfully passed!\n"); } /** - * @brief Main function - * @returns 0 on exit + * @brief testing function + * + * @returns void */ -int main() +static void test() { uint8_t *digest = NULL; @@ -476,5 +468,84 @@ int main() free(digest); + uint8_t key[64] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, + 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, + 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, + 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, + 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, + 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f}; + uint8_t key_answer[64] = { + 0x10, 0xeb, 0xb6, 0x77, 0x00, 0xb1, 0x86, 0x8e, 0xfb, 0x44, 0x17, + 0x98, 0x7a, 0xcf, 0x46, 0x90, 0xae, 0x9d, 0x97, 0x2f, 0xb7, 0xa5, + 0x90, 0xc2, 0xf0, 0x28, 0x71, 0x79, 0x9a, 0xaa, 0x47, 0x86, 0xb5, + 0xe9, 0x96, 0xe8, 0xf0, 0xf4, 0xeb, 0x98, 0x1f, 0xc2, 0x14, 0xb0, + 0x05, 0xf4, 0x2d, 0x2f, 0xf4, 0x23, 0x34, 0x99, 0x39, 0x16, 0x53, + 0xdf, 0x7a, 0xef, 0xcb, 0xc1, 0x3f, 0xc5, 0x15, 0x68}; + + digest = blake2b(NULL, 0, key, 64, 64); + assert_bytes(key_answer, digest, 64); + + free(digest); + + uint8_t zero[1] = {0}; + uint8_t zero_key[64] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, + 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, + 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, + 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, + 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, + 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f}; + uint8_t zero_answer[64] = { + 0x96, 0x1f, 0x6d, 0xd1, 0xe4, 0xdd, 0x30, 0xf6, 0x39, 0x01, 0x69, + 0x0c, 0x51, 0x2e, 0x78, 0xe4, 0xb4, 0x5e, 0x47, 0x42, 0xed, 0x19, + 0x7c, 0x3c, 0x5e, 0x45, 0xc5, 0x49, 0xfd, 0x25, 0xf2, 0xe4, 0x18, + 0x7b, 0x0b, 0xc9, 0xfe, 0x30, 0x49, 0x2b, 0x16, 0xb0, 0xd0, 0xbc, + 0x4e, 0xf9, 0xb0, 0xf3, 0x4c, 0x70, 0x03, 0xfa, 0xc0, 0x9a, 0x5e, + 0xf1, 0x53, 0x2e, 0x69, 0x43, 0x02, 0x34, 0xce, 0xbd}; + + digest = blake2b(zero, 1, zero_key, 64, 64); + assert_bytes(zero_answer, digest, 64); + + free(digest); + + uint8_t filled[64] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, + 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, + 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, + 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, + 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, + 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f}; + uint8_t filled_key[64] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, + 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, + 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, + 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, + 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, + 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f}; + uint8_t filled_answer[64] = { + 0x65, 0x67, 0x6d, 0x80, 0x06, 0x17, 0x97, 0x2f, 0xbd, 0x87, 0xe4, + 0xb9, 0x51, 0x4e, 0x1c, 0x67, 0x40, 0x2b, 0x7a, 0x33, 0x10, 0x96, + 0xd3, 0xbf, 0xac, 0x22, 0xf1, 0xab, 0xb9, 0x53, 0x74, 0xab, 0xc9, + 0x42, 0xf1, 0x6e, 0x9a, 0xb0, 0xea, 0xd3, 0x3b, 0x87, 0xc9, 0x19, + 0x68, 0xa6, 0xe5, 0x09, 0xe1, 0x19, 0xff, 0x07, 0x78, 0x7b, 0x3e, + 0xf4, 0x83, 0xe1, 0xdc, 0xdc, 0xcf, 0x6e, 0x30, 0x22}; + + digest = blake2b(filled, 64, filled_key, 64, 64); + assert_bytes(filled_answer, digest, 64); + + free(digest); + + printf("All tests have successfully passed!\n"); +} + +/** + * @brief main function + * + * @returns 0 on successful program exit + */ +int main() +{ + test(); return 0; }