xref: /freebsd/sys/contrib/libsodium/test/default/generichash2.c (revision 3611ec604864a7d4dcc9a3ea898c80eb35eef8a0)
1*0ac341f1SConrad Meyer 
2*0ac341f1SConrad Meyer #define TEST_NAME "generichash2"
3*0ac341f1SConrad Meyer #include "cmptest.h"
4*0ac341f1SConrad Meyer 
5*0ac341f1SConrad Meyer int
main(void)6*0ac341f1SConrad Meyer main(void)
7*0ac341f1SConrad Meyer {
8*0ac341f1SConrad Meyer #define MAXLEN 64
9*0ac341f1SConrad Meyer     crypto_generichash_state *st;
10*0ac341f1SConrad Meyer     unsigned char            in[MAXLEN];
11*0ac341f1SConrad Meyer     unsigned char            out[crypto_generichash_BYTES_MAX];
12*0ac341f1SConrad Meyer     unsigned char            k[crypto_generichash_KEYBYTES_MAX];
13*0ac341f1SConrad Meyer     size_t                   h, i, j;
14*0ac341f1SConrad Meyer 
15*0ac341f1SConrad Meyer     assert(crypto_generichash_statebytes() >= sizeof *st);
16*0ac341f1SConrad Meyer     st = (crypto_generichash_state *)
17*0ac341f1SConrad Meyer         sodium_malloc(crypto_generichash_statebytes());
18*0ac341f1SConrad Meyer     for (h = 0; h < crypto_generichash_KEYBYTES_MAX; ++h) {
19*0ac341f1SConrad Meyer         k[h] = (unsigned char) h;
20*0ac341f1SConrad Meyer     }
21*0ac341f1SConrad Meyer     for (i = 0; i < MAXLEN; ++i) {
22*0ac341f1SConrad Meyer         in[i] = (unsigned char) i;
23*0ac341f1SConrad Meyer         if (crypto_generichash_init(st, k,
24*0ac341f1SConrad Meyer                                     1 + i % crypto_generichash_KEYBYTES_MAX,
25*0ac341f1SConrad Meyer                                     1 + i % crypto_generichash_BYTES_MAX) != 0) {
26*0ac341f1SConrad Meyer             printf("crypto_generichash_init()\n");
27*0ac341f1SConrad Meyer             return 1;
28*0ac341f1SConrad Meyer         }
29*0ac341f1SConrad Meyer         crypto_generichash_update(st, in, i);
30*0ac341f1SConrad Meyer         crypto_generichash_update(st, in, i);
31*0ac341f1SConrad Meyer         crypto_generichash_update(st, in, i);
32*0ac341f1SConrad Meyer         if (crypto_generichash_final(st, out,
33*0ac341f1SConrad Meyer                                      1 + i % crypto_generichash_BYTES_MAX) != 0) {
34*0ac341f1SConrad Meyer             printf("crypto_generichash_final() should have returned 0\n");
35*0ac341f1SConrad Meyer         }
36*0ac341f1SConrad Meyer         for (j = 0; j < 1 + i % crypto_generichash_BYTES_MAX; ++j) {
37*0ac341f1SConrad Meyer             printf("%02x", (unsigned int) out[j]);
38*0ac341f1SConrad Meyer         }
39*0ac341f1SConrad Meyer         printf("\n");
40*0ac341f1SConrad Meyer         if (crypto_generichash_final(st, out,
41*0ac341f1SConrad Meyer                                      1 + i % crypto_generichash_BYTES_MAX) != -1) {
42*0ac341f1SConrad Meyer             printf("crypto_generichash_final() should have returned -1\n");
43*0ac341f1SConrad Meyer         }
44*0ac341f1SConrad Meyer     }
45*0ac341f1SConrad Meyer 
46*0ac341f1SConrad Meyer     assert(crypto_generichash_init(st, k, sizeof k, 0U) == -1);
47*0ac341f1SConrad Meyer     assert(crypto_generichash_init(st, k, sizeof k,
48*0ac341f1SConrad Meyer                                    crypto_generichash_BYTES_MAX + 1U) == -1);
49*0ac341f1SConrad Meyer     assert(crypto_generichash_init(st, k, crypto_generichash_KEYBYTES_MAX + 1U,
50*0ac341f1SConrad Meyer                                    sizeof out) == -1);
51*0ac341f1SConrad Meyer     assert(crypto_generichash_init(st, k, 0U, sizeof out) == 0);
52*0ac341f1SConrad Meyer     assert(crypto_generichash_init(st, k, 1U, sizeof out) == 0);
53*0ac341f1SConrad Meyer     assert(crypto_generichash_init(st, NULL, 1U, 0U) == -1);
54*0ac341f1SConrad Meyer     assert(crypto_generichash_init(st, NULL, crypto_generichash_KEYBYTES,
55*0ac341f1SConrad Meyer                                    1U) == 0);
56*0ac341f1SConrad Meyer     assert(crypto_generichash_init(st, NULL, crypto_generichash_KEYBYTES,
57*0ac341f1SConrad Meyer                                    0U) == -1);
58*0ac341f1SConrad Meyer 
59*0ac341f1SConrad Meyer     sodium_free(st);
60*0ac341f1SConrad Meyer 
61*0ac341f1SConrad Meyer     return 0;
62*0ac341f1SConrad Meyer }
63