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