| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "hexdecoct.h" |
| 4 |
#include "hmac.h" |
| 5 |
#include "tests.h" |
| 6 |
|
| 7 |
static void hmac_sha256_by_string(const char *key, const char *value, uint8_t res[static SHA256_DIGEST_SIZE]) { |
| 8 |
hmac_sha256(key, strlen(key), value, strlen(value), res); |
| 9 |
} |
| 10 |
|
| 11 |
TEST(hmac) { |
| 12 |
uint8_t result[SHA256_DIGEST_SIZE]; |
| 13 |
char *hex_result = NULL; |
| 14 |
|
| 15 |
/* Results compared with output of 'echo -n "<input>" | openssl dgst -sha256 -hmac "<key>"' */ |
| 16 |
|
| 17 |
hmac_sha256_by_string("waldo", |
| 18 |
"", |
| 19 |
result); |
| 20 |
hex_result = hexmem(result, sizeof(result)); |
| 21 |
ASSERT_STREQ(hex_result, "cadd5e42114351181f3abff477641d88efb57d2b5641a1e5c6d623363a6d3bad"); |
| 22 |
hex_result = mfree(hex_result); |
| 23 |
|
| 24 |
hmac_sha256_by_string("waldo", |
| 25 |
"baldohaldo", |
| 26 |
result); |
| 27 |
hex_result = hexmem(result, sizeof(result)); |
| 28 |
ASSERT_STREQ(hex_result, "c47ad5031ba21605e52c6ca68090d66a2dd5ccf84efa4bace15361a8cba63cda"); |
| 29 |
hex_result = mfree(hex_result); |
| 30 |
|
| 31 |
hmac_sha256_by_string("waldo", |
| 32 |
"baldo haldo", |
| 33 |
result); |
| 34 |
hex_result = hexmem(result, sizeof(result)); |
| 35 |
ASSERT_STREQ(hex_result, "4e8974ad6c08b98cc2519cd1e27aa7195769fcf86db1dd7ceaab4d44c490ad69"); |
| 36 |
hex_result = mfree(hex_result); |
| 37 |
|
| 38 |
hmac_sha256_by_string("waldo", |
| 39 |
"baldo 4e8974ad6c08b98cc2519cd1e27aa7195769fcf86db1dd7ceaab4d44c490ad69 haldo", |
| 40 |
result); |
| 41 |
hex_result = hexmem(result, sizeof(result)); |
| 42 |
ASSERT_STREQ(hex_result, "039f3df430b19753ffb493e5b90708f75c5210b63c6bcbef3374eb3f0a3f97f7"); |
| 43 |
hex_result = mfree(hex_result); |
| 44 |
|
| 45 |
hmac_sha256_by_string("4e8974ad6c08b98cc2519cd1e27aa7195769fcf86db1dd7ceaab4d44c490ad69", |
| 46 |
"baldo haldo", |
| 47 |
result); |
| 48 |
hex_result = hexmem(result, sizeof(result)); |
| 49 |
ASSERT_STREQ(hex_result, "c4cfaf48077cbb0bbd177a09e59ec4c248f4ca771503410f5b54b98d88d2f47b"); |
| 50 |
hex_result = mfree(hex_result); |
| 51 |
|
| 52 |
hmac_sha256_by_string("4e8974ad6c08b98cc2519cd1e27aa7195769fcf86db1dd7ceaab4d44c490ad69", |
| 53 |
"supercalifragilisticexpialidocious", |
| 54 |
result); |
| 55 |
hex_result = hexmem(result, sizeof(result)); |
| 56 |
ASSERT_STREQ(hex_result, "2c059e7a63c4c3b23f47966a65fd2f8a2f5d7161e2e90d78ff68866b5c375cb7"); |
| 57 |
hex_result = mfree(hex_result); |
| 58 |
|
| 59 |
hmac_sha256_by_string("4e8974ad6c08b98cc2519cd1e27aa7195769fcf86db1dd7ceaab4d44c490ad69c47ad5031ba21605e52c6ca68090d66a2dd5ccf84efa4bace15361a8cba63cda", |
| 60 |
"supercalifragilisticexpialidocious", |
| 61 |
result); |
| 62 |
hex_result = hexmem(result, sizeof(result)); |
| 63 |
ASSERT_STREQ(hex_result, "1dd1d1d45b9d9f9673dc9983c968c46ff3168e03cfeb4156a219eba1af4cff5f"); |
| 64 |
hex_result = mfree(hex_result); |
| 65 |
} |
| 66 |
|
| 67 |
DEFINE_TEST_MAIN(LOG_INFO); |