xref: /freebsd/sys/contrib/libsodium/test/default/generichash3.c (revision 031beb4e239bfce798af17f5fe8dba8bcaf13d99)
1 
2 #define TEST_NAME "generichash3"
3 #include "cmptest.h"
4 
5 int
6 main(void)
7 {
8 #define MAXLEN 64
9     crypto_generichash_blake2b_state st;
10     unsigned char salt[crypto_generichash_blake2b_SALTBYTES]
11         = { '5', 'b', '6', 'b', '4', '1', 'e', 'd',
12             '9', 'b', '3', '4', '3', 'f', 'e', '0' };
13     unsigned char personal[crypto_generichash_blake2b_PERSONALBYTES]
14         = { '5', '1', '2', '6', 'f', 'b', '2', 'a',
15             '3', '7', '4', '0', '0', 'd', '2', 'a' };
16     unsigned char in[MAXLEN];
17     unsigned char out[crypto_generichash_blake2b_BYTES_MAX];
18     unsigned char k[crypto_generichash_blake2b_KEYBYTES_MAX];
19     size_t        h;
20     size_t        i;
21     size_t        j;
22 
23     assert(crypto_generichash_blake2b_statebytes() >= sizeof st);
24     for (h = 0; h < crypto_generichash_blake2b_KEYBYTES_MAX; ++h) {
25         k[h] = (unsigned char) h;
26     }
27 
28     for (i = 0; i < MAXLEN; ++i) {
29         in[i] = (unsigned char) i;
30         crypto_generichash_blake2b_init_salt_personal(
31             &st, k, 1 + i % crypto_generichash_blake2b_KEYBYTES_MAX,
32             1 + i % crypto_generichash_blake2b_BYTES_MAX, salt, personal);
33         crypto_generichash_blake2b_update(&st, in, (unsigned long long) i);
34         crypto_generichash_blake2b_final(
35             &st, out, 1 + i % crypto_generichash_blake2b_BYTES_MAX);
36         for (j = 0; j < 1 + i % crypto_generichash_blake2b_BYTES_MAX; ++j) {
37             printf("%02x", (unsigned int) out[j]);
38         }
39         printf("\n");
40     }
41 
42     memset(out, 0, sizeof out);
43     crypto_generichash_blake2b_init_salt_personal(
44         &st, k, 0U, crypto_generichash_blake2b_BYTES_MAX, salt, personal);
45     crypto_generichash_blake2b_update(&st, in, MAXLEN);
46     crypto_generichash_blake2b_final(&st, out,
47                                      crypto_generichash_blake2b_BYTES_MAX);
48     for (j = 0; j < crypto_generichash_blake2b_BYTES_MAX; ++j) {
49         printf("%02x", (unsigned int) out[j]);
50     }
51     printf("\n");
52 
53     memset(out, 0, sizeof out);
54     crypto_generichash_blake2b_init_salt_personal(
55         &st, NULL, 1U, crypto_generichash_blake2b_BYTES_MAX, salt, personal);
56     crypto_generichash_blake2b_update(&st, in, MAXLEN);
57     crypto_generichash_blake2b_final(&st, out,
58                                      crypto_generichash_blake2b_BYTES_MAX);
59     for (j = 0; j < crypto_generichash_blake2b_BYTES_MAX; ++j) {
60         printf("%02x", (unsigned int) out[j]);
61     }
62     printf("\n");
63 
64     memset(out, 0, sizeof out);
65     crypto_generichash_blake2b_init_salt_personal(
66         &st, k, crypto_generichash_blake2b_KEYBYTES_MAX,
67     crypto_generichash_blake2b_BYTES_MAX, NULL, personal);
68     crypto_generichash_blake2b_update(&st, in, MAXLEN);
69     crypto_generichash_blake2b_final(&st, out,
70                                      crypto_generichash_blake2b_BYTES_MAX);
71     for (j = 0; j < crypto_generichash_blake2b_BYTES_MAX; ++j) {
72         printf("%02x", (unsigned int) out[j]);
73     }
74     printf("\n");
75 
76     memset(out, 0, sizeof out);
77     crypto_generichash_blake2b_init_salt_personal(
78         &st, k, crypto_generichash_blake2b_KEYBYTES_MAX,
79         crypto_generichash_blake2b_BYTES_MAX, salt, NULL);
80     crypto_generichash_blake2b_update(&st, in, MAXLEN);
81     crypto_generichash_blake2b_final(
82         &st, out, crypto_generichash_blake2b_BYTES_MAX);
83     for (j = 0; j < crypto_generichash_blake2b_BYTES_MAX; ++j) {
84         printf("%02x", (unsigned int) out[j]);
85     }
86     printf("\n");
87 
88     memset(out, 0, sizeof out);
89     crypto_generichash_blake2b_salt_personal(
90         out, crypto_generichash_blake2b_BYTES_MAX, in, MAXLEN,
91         k, 0U, salt, personal);
92     for (j = 0; j < crypto_generichash_blake2b_BYTES_MAX; ++j) {
93         printf("%02x", (unsigned int) out[j]);
94     }
95     printf("\n");
96 
97     memset(out, 0, sizeof out);
98     crypto_generichash_blake2b_salt_personal(
99         out, crypto_generichash_blake2b_BYTES_MAX, in, MAXLEN,
100         NULL, 0U, salt, personal);
101     for (j = 0; j < crypto_generichash_blake2b_BYTES_MAX; ++j) {
102         printf("%02x", (unsigned int) out[j]);
103     }
104     printf("\n");
105 
106     memset(out, 0, sizeof out);
107     crypto_generichash_blake2b_salt_personal(
108         out, crypto_generichash_blake2b_BYTES_MAX, in, MAXLEN,
109         k, crypto_generichash_blake2b_KEYBYTES_MAX, salt, personal);
110     for (j = 0; j < crypto_generichash_blake2b_BYTES_MAX; ++j) {
111         printf("%02x", (unsigned int) out[j]);
112     }
113     printf("\n");
114 
115     memset(out, 0, sizeof out);
116     crypto_generichash_blake2b_salt_personal(
117         out, crypto_generichash_blake2b_BYTES_MAX, in, MAXLEN,
118         k, crypto_generichash_blake2b_KEYBYTES_MAX, NULL, personal);
119     for (j = 0; j < crypto_generichash_blake2b_BYTES_MAX; ++j) {
120         printf("%02x", (unsigned int) out[j]);
121     }
122     printf("\n");
123 
124     memset(out, 0, sizeof out);
125     crypto_generichash_blake2b_salt_personal(
126         out, crypto_generichash_blake2b_BYTES_MAX, in, MAXLEN,
127         k, crypto_generichash_blake2b_KEYBYTES_MAX, salt, NULL);
128     for (j = 0; j < crypto_generichash_blake2b_BYTES_MAX; ++j) {
129         printf("%02x", (unsigned int) out[j]);
130     }
131     printf("\n");
132 
133     assert(crypto_generichash_blake2b_salt_personal
134            (NULL, 0,
135             in, (unsigned long long) sizeof in,
136             k, sizeof k, NULL, NULL) == -1);
137     assert(crypto_generichash_blake2b_salt_personal
138            (NULL, crypto_generichash_BYTES_MAX + 1,
139             in, (unsigned long long) sizeof in,
140             k, sizeof k, NULL, NULL) == -1);
141     assert(crypto_generichash_blake2b_salt_personal
142            (NULL, (unsigned long long) sizeof in,
143             in, (unsigned long long) sizeof in,
144             k, crypto_generichash_KEYBYTES_MAX + 1, NULL, NULL) == -1);
145 
146     crypto_generichash_blake2b_init_salt_personal(&st, NULL, 0U, crypto_generichash_BYTES,
147                                                   NULL, personal);
148     crypto_generichash_blake2b_update(&st, in, MAXLEN);
149     crypto_generichash_blake2b_final(&st, out, crypto_generichash_blake2b_BYTES_MAX);
150     for (j = 0; j < crypto_generichash_blake2b_BYTES_MAX; ++j) {
151         printf("%02x", (unsigned int) out[j]);
152     }
153     printf("\n");
154 
155     crypto_generichash_blake2b_init_salt_personal(&st, NULL, 0U, crypto_generichash_BYTES,
156                                                   salt, NULL);
157     crypto_generichash_blake2b_update(&st, in, MAXLEN);
158     crypto_generichash_blake2b_final(&st, out, crypto_generichash_blake2b_BYTES_MAX);
159     for (j = 0; j < crypto_generichash_blake2b_BYTES_MAX; ++j) {
160         printf("%02x", (unsigned int) out[j]);
161     }
162     printf("\n");
163 
164     assert(crypto_generichash_blake2b_init_salt_personal
165            (&st, k, sizeof k, 0, NULL, NULL) == -1);
166     assert(crypto_generichash_blake2b_init_salt_personal
167            (&st, k, sizeof k, crypto_generichash_blake2b_BYTES_MAX + 1, NULL, NULL) == -1);
168     assert(crypto_generichash_blake2b_init_salt_personal
169            (&st, k, crypto_generichash_blake2b_KEYBYTES_MAX + 1, sizeof out, NULL, NULL) == -1);
170 
171     assert(crypto_generichash_blake2b_init_salt_personal(&st, k, sizeof k, crypto_generichash_BYTES,
172                                                          NULL, personal) == 0);
173     assert(crypto_generichash_blake2b_init_salt_personal(&st, k, sizeof k, crypto_generichash_BYTES,
174                                                          salt, NULL) == 0);
175     return 0;
176 }
177