Path: blob/main/sys/contrib/libsodium/test/default/auth.c
48378 views
1#define TEST_NAME "auth"2#include "cmptest.h"34/* "Test Case 2" from RFC 4231 */5static unsigned char key[32] = "Jefe";6static unsigned char c[] = "what do ya want for nothing?";78/* Hacker manifesto */9static unsigned char key2[] =10"Another one got caught today, it's all over the papers. \"Teenager "11"Arrested in Computer Crime Scandal\", \"Hacker Arrested after Bank "12"Tampering\"... Damn kids. They're all alike.";1314static unsigned char a[crypto_auth_BYTES];15static unsigned char a2[crypto_auth_hmacsha512_BYTES];1617int18main(void)19{20crypto_auth_hmacsha512_state st;21crypto_auth_hmacsha256_state st256;22size_t i;2324assert(crypto_auth_hmacsha512_statebytes() ==25sizeof(crypto_auth_hmacsha512_state));26crypto_auth(a, c, sizeof c - 1U, key);27for (i = 0; i < sizeof a; ++i) {28printf(",0x%02x", (unsigned int) a[i]);29if (i % 8 == 7)30printf("\n");31}32printf("\n");3334crypto_auth_hmacsha512_init(&st, key, sizeof key);35crypto_auth_hmacsha512_update(&st, c, 1U);36crypto_auth_hmacsha512_update(&st, c, sizeof c - 2U);37crypto_auth_hmacsha512_final(&st, a2);38for (i = 0; i < sizeof a2; ++i) {39printf(",0x%02x", (unsigned int) a2[i]);40if (i % 8 == 7)41printf("\n");42}43printf("\n");4445crypto_auth_hmacsha512_init(&st, key2, sizeof key2);46crypto_auth_hmacsha512_update(&st, c, 1U);47crypto_auth_hmacsha512_update(&st, c, sizeof c - 2U);48crypto_auth_hmacsha512_final(&st, a2);49for (i = 0; i < sizeof a2; ++i) {50printf(",0x%02x", (unsigned int) a2[i]);51if (i % 8 == 7)52printf("\n");53}5455memset(a2, 0, sizeof a2);56crypto_auth_hmacsha256_init(&st256, key2, sizeof key2);57crypto_auth_hmacsha256_update(&st256, NULL, 0U);58crypto_auth_hmacsha256_update(&st256, c, 1U);59crypto_auth_hmacsha256_update(&st256, c, sizeof c - 2U);60crypto_auth_hmacsha256_final(&st256, a2);61for (i = 0; i < sizeof a2; ++i) {62printf(",0x%02x", (unsigned int) a2[i]);63if (i % 8 == 7)64printf("\n");65}6667assert(crypto_auth_bytes() > 0U);68assert(crypto_auth_keybytes() > 0U);69assert(strcmp(crypto_auth_primitive(), "hmacsha512256") == 0);70assert(crypto_auth_hmacsha256_bytes() > 0U);71assert(crypto_auth_hmacsha256_keybytes() > 0U);72assert(crypto_auth_hmacsha512_bytes() > 0U);73assert(crypto_auth_hmacsha512_keybytes() > 0U);74assert(crypto_auth_hmacsha512256_bytes() == crypto_auth_bytes());75assert(crypto_auth_hmacsha512256_keybytes() == crypto_auth_keybytes());76assert(crypto_auth_hmacsha512256_statebytes() >=77crypto_auth_hmacsha512256_keybytes());78assert(crypto_auth_hmacsha256_statebytes() ==79sizeof(crypto_auth_hmacsha256_state));80assert(crypto_auth_hmacsha512_statebytes() ==81sizeof(crypto_auth_hmacsha512_state));8283return 0;84}858687