2013-01-20 16:25:22 -07:00
|
|
|
|
2013-01-20 16:55:10 -07:00
|
|
|
#define TEST_NAME "auth"
|
|
|
|
#include "cmptest.h"
|
|
|
|
|
2013-01-20 16:25:22 -07:00
|
|
|
/* "Test Case 2" from RFC 4231 */
|
2015-11-23 08:07:13 -07:00
|
|
|
static unsigned char key[32] = "Jefe";
|
2017-02-23 03:15:29 -07:00
|
|
|
static unsigned char c[] = "what do ya want for nothing?";
|
2013-01-20 16:25:22 -07:00
|
|
|
|
2014-09-16 14:59:19 -07:00
|
|
|
/* Hacker manifesto */
|
2017-02-23 03:15:29 -07:00
|
|
|
static unsigned char key2[] =
|
|
|
|
"Another one got caught today, it's all over the papers. \"Teenager "
|
|
|
|
"Arrested in Computer Crime Scandal\", \"Hacker Arrested after Bank "
|
|
|
|
"Tampering\"... Damn kids. They're all alike.";
|
2014-09-16 14:59:19 -07:00
|
|
|
|
2015-11-23 08:07:13 -07:00
|
|
|
static unsigned char a[crypto_auth_BYTES];
|
|
|
|
static unsigned char a2[crypto_auth_hmacsha512_BYTES];
|
2019-02-02 14:47:31 -07:00
|
|
|
static unsigned char a3[crypto_auth_hmacsha512_BYTES];
|
2013-01-20 16:25:22 -07:00
|
|
|
|
2017-02-23 03:15:29 -07:00
|
|
|
int
|
|
|
|
main(void)
|
2013-01-20 16:25:22 -07:00
|
|
|
{
|
2014-09-16 14:59:19 -07:00
|
|
|
crypto_auth_hmacsha512_state st;
|
2017-07-24 14:04:23 -07:00
|
|
|
crypto_auth_hmacsha256_state st256;
|
2019-02-02 14:47:31 -07:00
|
|
|
crypto_auth_hmacsha512256_state st512_256;
|
2017-02-23 03:15:29 -07:00
|
|
|
size_t i;
|
2014-09-13 14:11:12 -07:00
|
|
|
|
2015-01-23 15:08:49 -07:00
|
|
|
assert(crypto_auth_hmacsha512_statebytes() ==
|
|
|
|
sizeof(crypto_auth_hmacsha512_state));
|
2014-09-14 11:32:55 -07:00
|
|
|
crypto_auth(a, c, sizeof c - 1U, key);
|
2014-09-16 14:59:19 -07:00
|
|
|
for (i = 0; i < sizeof a; ++i) {
|
2017-02-23 03:15:29 -07:00
|
|
|
printf(",0x%02x", (unsigned int) a[i]);
|
2014-09-14 11:32:55 -07:00
|
|
|
if (i % 8 == 7)
|
|
|
|
printf("\n");
|
2014-09-14 10:34:16 -07:00
|
|
|
}
|
2014-09-16 14:59:19 -07:00
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
crypto_auth_hmacsha512_init(&st, key, sizeof key);
|
|
|
|
crypto_auth_hmacsha512_update(&st, c, 1U);
|
|
|
|
crypto_auth_hmacsha512_update(&st, c, sizeof c - 2U);
|
|
|
|
crypto_auth_hmacsha512_final(&st, a2);
|
|
|
|
for (i = 0; i < sizeof a2; ++i) {
|
2017-02-23 03:15:29 -07:00
|
|
|
printf(",0x%02x", (unsigned int) a2[i]);
|
2014-09-16 14:59:19 -07:00
|
|
|
if (i % 8 == 7)
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
crypto_auth_hmacsha512_init(&st, key2, sizeof key2);
|
|
|
|
crypto_auth_hmacsha512_update(&st, c, 1U);
|
|
|
|
crypto_auth_hmacsha512_update(&st, c, sizeof c - 2U);
|
|
|
|
crypto_auth_hmacsha512_final(&st, a2);
|
|
|
|
for (i = 0; i < sizeof a2; ++i) {
|
2017-02-23 03:15:29 -07:00
|
|
|
printf(",0x%02x", (unsigned int) a2[i]);
|
2014-09-16 14:59:19 -07:00
|
|
|
if (i % 8 == 7)
|
|
|
|
printf("\n");
|
|
|
|
}
|
2014-09-13 14:11:12 -07:00
|
|
|
|
2017-07-24 13:08:07 -07:00
|
|
|
memset(a2, 0, sizeof a2);
|
2017-07-24 14:04:23 -07:00
|
|
|
crypto_auth_hmacsha256_init(&st256, key2, sizeof key2);
|
2018-12-24 07:02:59 -07:00
|
|
|
crypto_auth_hmacsha256_update(&st256, guard_page, 0U);
|
2017-07-24 14:04:23 -07:00
|
|
|
crypto_auth_hmacsha256_update(&st256, c, 1U);
|
|
|
|
crypto_auth_hmacsha256_update(&st256, c, sizeof c - 2U);
|
|
|
|
crypto_auth_hmacsha256_final(&st256, a2);
|
2017-07-24 13:08:07 -07:00
|
|
|
for (i = 0; i < sizeof a2; ++i) {
|
|
|
|
printf(",0x%02x", (unsigned int) a2[i]);
|
|
|
|
if (i % 8 == 7)
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2019-02-09 12:47:24 -07:00
|
|
|
/* Empty message tests: HMAC-SHA512 */
|
2019-02-02 14:47:31 -07:00
|
|
|
memset(a2, 0, sizeof a2);
|
|
|
|
crypto_auth_hmacsha512_init(&st, key, sizeof key);
|
|
|
|
crypto_auth_hmacsha512_final(&st, a2);
|
|
|
|
|
|
|
|
memset(a3, 0, sizeof a3);
|
|
|
|
crypto_auth_hmacsha512_init(&st, key, sizeof key);
|
|
|
|
crypto_auth_hmacsha512_update(&st, a2, 0U);
|
|
|
|
crypto_auth_hmacsha512_final(&st, a3);
|
|
|
|
assert(sodium_memcmp(a2, a3, sizeof a2) == 0);
|
|
|
|
|
|
|
|
memset(a3, 0, sizeof a3);
|
|
|
|
crypto_auth_hmacsha512_init(&st, key, sizeof key);
|
2019-02-09 12:47:24 -07:00
|
|
|
crypto_auth_hmacsha512_update(&st, guard_page, 0U);
|
2019-02-02 14:47:31 -07:00
|
|
|
crypto_auth_hmacsha512_final(&st, a3);
|
|
|
|
assert(sodium_memcmp(a2, a3, sizeof a2) == 0);
|
|
|
|
|
2019-02-09 12:47:24 -07:00
|
|
|
/* Empty message tests: HMAC-SHA512-256 */
|
2019-02-02 14:47:31 -07:00
|
|
|
memset(a2, 0, sizeof a2);
|
|
|
|
crypto_auth_hmacsha512256_init(&st512_256, key, sizeof key);
|
|
|
|
crypto_auth_hmacsha512256_final(&st512_256, a2);
|
|
|
|
|
|
|
|
memset(a3, 0, sizeof a3);
|
|
|
|
crypto_auth_hmacsha512256_init(&st512_256, key, sizeof key);
|
|
|
|
crypto_auth_hmacsha512256_update(&st512_256, a2, 0U);
|
|
|
|
crypto_auth_hmacsha512256_final(&st512_256, a3);
|
|
|
|
assert(sodium_memcmp(a2, a3, sizeof a2) == 0);
|
|
|
|
|
|
|
|
memset(a3, 0, sizeof a3);
|
|
|
|
crypto_auth_hmacsha512256_init(&st512_256, key, sizeof key);
|
2019-02-09 12:47:24 -07:00
|
|
|
crypto_auth_hmacsha512256_update(&st512_256, guard_page, 0U);
|
2019-02-02 14:47:31 -07:00
|
|
|
crypto_auth_hmacsha512256_final(&st512_256, a3);
|
|
|
|
assert(sodium_memcmp(a2, a3, sizeof a2) == 0);
|
|
|
|
|
2019-02-09 12:47:24 -07:00
|
|
|
/* Empty message tests: HMAC-SHA256 */
|
2019-02-02 14:47:31 -07:00
|
|
|
|
|
|
|
memset(a2, 0, sizeof a2);
|
|
|
|
crypto_auth_hmacsha256_init(&st256, key, sizeof key);
|
|
|
|
crypto_auth_hmacsha256_final(&st256, a2);
|
|
|
|
|
|
|
|
memset(a3, 0, sizeof a3);
|
|
|
|
crypto_auth_hmacsha256_init(&st256, key, sizeof key);
|
|
|
|
crypto_auth_hmacsha256_update(&st256, a2, 0U);
|
|
|
|
crypto_auth_hmacsha256_final(&st256, a3);
|
|
|
|
assert(sodium_memcmp(a2, a3, sizeof a2) == 0);
|
|
|
|
|
|
|
|
memset(a3, 0, sizeof a3);
|
|
|
|
crypto_auth_hmacsha256_init(&st256, key, sizeof key);
|
2019-02-09 12:47:24 -07:00
|
|
|
crypto_auth_hmacsha256_update(&st256, guard_page, 0U);
|
2019-02-02 14:47:31 -07:00
|
|
|
crypto_auth_hmacsha256_final(&st256, a3);
|
|
|
|
assert(sodium_memcmp(a2, a3, sizeof a2) == 0);
|
|
|
|
|
2019-02-09 12:47:24 -07:00
|
|
|
/* --- */
|
|
|
|
|
2014-09-14 10:34:16 -07:00
|
|
|
assert(crypto_auth_bytes() > 0U);
|
|
|
|
assert(crypto_auth_keybytes() > 0U);
|
|
|
|
assert(strcmp(crypto_auth_primitive(), "hmacsha512256") == 0);
|
|
|
|
assert(crypto_auth_hmacsha256_bytes() > 0U);
|
|
|
|
assert(crypto_auth_hmacsha256_keybytes() > 0U);
|
|
|
|
assert(crypto_auth_hmacsha512_bytes() > 0U);
|
|
|
|
assert(crypto_auth_hmacsha512_keybytes() > 0U);
|
|
|
|
assert(crypto_auth_hmacsha512256_bytes() == crypto_auth_bytes());
|
|
|
|
assert(crypto_auth_hmacsha512256_keybytes() == crypto_auth_keybytes());
|
2015-05-09 06:54:18 -07:00
|
|
|
assert(crypto_auth_hmacsha512256_statebytes() >=
|
|
|
|
crypto_auth_hmacsha512256_keybytes());
|
2015-01-23 15:08:49 -07:00
|
|
|
assert(crypto_auth_hmacsha256_statebytes() ==
|
|
|
|
sizeof(crypto_auth_hmacsha256_state));
|
|
|
|
assert(crypto_auth_hmacsha512_statebytes() ==
|
|
|
|
sizeof(crypto_auth_hmacsha512_state));
|
2014-09-14 10:34:16 -07:00
|
|
|
|
|
|
|
return 0;
|
2013-01-20 16:25:22 -07:00
|
|
|
}
|