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