From 623ff164def5279ef4505b5dee72b0d30dbf758c Mon Sep 17 00:00:00 2001 From: Dmitry Podgorny Date: Mon, 13 Jan 2020 01:05:50 +0200 Subject: [PATCH] tests/scram: add HMAC test vector Add test for HMAC and make other tests independent from SHA1. --- tests/test_scram.c | 107 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 90 insertions(+), 17 deletions(-) diff --git a/tests/test_scram.c b/tests/test_scram.c index 3826aee..af90e7b 100644 --- a/tests/test_scram.c +++ b/tests/test_scram.c @@ -58,19 +58,19 @@ static const struct { }, }; -static void test_df(void) +static void test_df(const struct hash_alg *alg) { size_t i; const char *s; - uint8_t dk[SHA1_DIGEST_SIZE]; + uint8_t dk[SCRAM_DIGEST_SIZE]; - printf("Derivation function tests (SCRAM_SHA1_Hi).\n"); + printf("Derivation function SCRAM_Hi tests for %s.\n", alg->scram_name); for (i = 0; i < ARRAY_SIZE(df_vectors); ++i) { printf("Test #%d: ", (int)i + 1); - SCRAM_Hi(&scram_sha1, (uint8_t *)df_vectors[i].P, df_vectors[i].P_len, + SCRAM_Hi(alg, (uint8_t *)df_vectors[i].P, df_vectors[i].P_len, (uint8_t *)df_vectors[i].S, df_vectors[i].S_len, df_vectors[i].c, dk); - s = test_bin_to_hex(dk, sizeof(dk)); + s = test_bin_to_hex(dk, alg->digest_size); COMPARE(df_vectors[i].DK, s); printf("ok\n"); } @@ -101,42 +101,115 @@ static const struct { }, }; -static void test_scram(void) +static void test_scram(const struct hash_alg *alg) { - uint8_t key[SHA1_DIGEST_SIZE]; - uint8_t sign[SHA1_DIGEST_SIZE]; + uint8_t key[SCRAM_DIGEST_SIZE]; + uint8_t sign[SCRAM_DIGEST_SIZE]; uint8_t salt[256]; size_t salt_len; char auth[512]; const char *s; size_t i; - int j; + size_t j; - printf("SCRAM_SHA1_ClientKey and SCRAM_SHA1_ClientSignature tests.\n"); + printf("SCRAM_ClientKey and SCRAM_ClientSignature tests for %s.\n", + alg->scram_name); for (i = 0; i < ARRAY_SIZE(scram_vectors); ++i) { printf("Test #%d: ", (int)i + 1); snprintf(auth, sizeof(auth), "%s,%s,%s", scram_vectors[i].initial + 3, scram_vectors[i].challenge, scram_vectors[i].response); test_hex_to_bin(scram_vectors[i].salt, salt, &salt_len); - SCRAM_ClientKey(&scram_sha1, (uint8_t *)scram_vectors[i].password, + SCRAM_ClientKey(alg, (uint8_t *)scram_vectors[i].password, strlen(scram_vectors[i].password), salt, salt_len, scram_vectors[i].i, key); - SCRAM_ClientSignature(&scram_sha1, key, (uint8_t *)auth, strlen(auth), - sign); - for (j = 0; j < SHA1_DIGEST_SIZE; j++) { + SCRAM_ClientSignature(alg, key, (uint8_t *)auth, strlen(auth), sign); + for (j = 0; j < alg->digest_size; j++) { sign[j] ^= key[j]; } - s = test_bin_to_hex(sign, SHA1_DIGEST_SIZE); + s = test_bin_to_hex(sign, alg->digest_size); COMPARE(scram_vectors[i].sign, s); printf("ok\n"); } } +/* + * Test vectors for HMAC (RFC2202, RFC4231). + */ +static const uint8_t hmac_key1[] = {0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; +static const uint8_t hmac_data1[] = {0x48, 0x69, 0x20, 0x54, + 0x68, 0x65, 0x72, 0x65}; +static const struct { + const uint8_t *key; + size_t key_len; + const uint8_t *data; + size_t data_len; + const uint8_t sha1[SHA1_DIGEST_SIZE]; + const uint8_t sha256[SHA256_DIGEST_SIZE]; + const uint8_t sha512[SHA512_DIGEST_SIZE]; +} hmac_vectors[] = { + { + .key = hmac_key1, + .key_len = sizeof(hmac_key1), + .data = hmac_data1, + .data_len = sizeof(hmac_data1), + .sha1 = {0xb6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64, 0xe2, 0x8b, + 0xc0, 0xb6, 0xfb, 0x37, 0x8c, 0x8e, 0xf1, 0x46, 0xbe, 0x00}, + .sha256 = {0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53, + 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b, + 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7, + 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7}, + .sha512 = {0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d, 0x4f, 0xf0, + 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0, 0x23, 0x79, 0xf4, 0xe2, + 0xce, 0x4e, 0xc2, 0x78, 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, + 0x7c, 0xde, 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02, + 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4, 0xbe, 0x9d, + 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70, 0x2e, 0x69, 0x6c, 0x20, + 0x3a, 0x12, 0x68, 0x54}, + }, +}; + +static void test_hmac(const struct hash_alg *alg) +{ + size_t i; + uint8_t digest[SCRAM_DIGEST_SIZE]; + + printf("HMAC tests for %s.\n", alg->scram_name); + for (i = 0; i < ARRAY_SIZE(hmac_vectors); ++i) { + printf("Test #%d: ", (int)i + 1); + memset(digest, 0, sizeof(digest)); + crypto_HMAC(alg, hmac_vectors[i].key, hmac_vectors[i].key_len, + hmac_vectors[i].data, hmac_vectors[i].data_len, digest); + switch (alg->digest_size) { + case SHA1_DIGEST_SIZE: + COMPARE_BUF(hmac_vectors[i].sha1, alg->digest_size, digest, + alg->digest_size); + break; + case SHA256_DIGEST_SIZE: + COMPARE_BUF(hmac_vectors[i].sha256, alg->digest_size, digest, + alg->digest_size); + break; + case SHA512_DIGEST_SIZE: + COMPARE_BUF(hmac_vectors[i].sha512, alg->digest_size, digest, + alg->digest_size); + break; + default: + printf("Unknown digest size: %zu\n", alg->digest_size); + exit(1); + } + printf("ok\n"); + } +} + int main(int argc, char **argv) { - test_df(); - test_scram(); + test_df(&scram_sha1); + test_scram(&scram_sha1); + test_hmac(&scram_sha1); + test_hmac(&scram_sha256); + test_hmac(&scram_sha512); return 0; }