Imported Upstream version 2.6.1
[platform/upstream/cryptsetup.git] / tests / crypto-vectors.c
1 /*
2  * cryptsetup crypto backend test vectors
3  *
4  * Copyright (C) 2018-2023 Milan Broz
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27
28 #include "crypto_backend/crypto_backend.h"
29
30 #ifndef ARRAY_SIZE
31 # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
32 #endif
33
34 static void printhex(const char *s, const char *buf, size_t len)
35 {
36         size_t i;
37
38         printf("%s: ", s);
39         for (i = 0; i < len; i++)
40                 printf(" %02x", (unsigned char)buf[i]);
41         printf("\n");
42         fflush(stdout);
43 }
44
45 static bool fips_mode(void)
46 {
47         int fd;
48         char buf = 0;
49
50         fd = open("/proc/sys/crypto/fips_enabled", O_RDONLY);
51
52         if (fd < 0)
53                 return false;
54
55         if (read(fd, &buf, 1) != 1)
56                 buf = '0';
57
58         close(fd);
59
60         return (buf == '1');
61 }
62
63 /*
64  * KDF tests
65  */
66 struct kdf_test_vector {
67         const char *type;
68         const char *hash;
69         unsigned int hash_block_length;
70         unsigned int iterations;
71         unsigned int memory;
72         unsigned int parallelism;
73         const char *password;
74         unsigned int password_length;
75         const char *salt;
76         unsigned int salt_length;
77 //      const char *key;
78 //      unsigned int key_length;
79 //      const char *ad;
80 //      unsigned int ad_length;
81         const char *output;
82         unsigned int output_length;
83 };
84
85 static struct kdf_test_vector kdf_test_vectors[] = {
86         /* Argon2 RFC (without key and ad values) */
87         {
88                 "argon2i", NULL, 0, 3, 32, 4,
89                 "\x01\x01\x01\x01\x01\x01\x01\x01"
90                 "\x01\x01\x01\x01\x01\x01\x01\x01"
91                 "\x01\x01\x01\x01\x01\x01\x01\x01"
92                 "\x01\x01\x01\x01\x01\x01\x01\x01", 32,
93                 "\x02\x02\x02\x02\x02\x02\x02\x02"
94                 "\x02\x02\x02\x02\x02\x02\x02\x02", 16,
95 //              "\x03\x03\x03\x03\x03\x03\x03\x03", 8,
96 //              "\x04\x04\x04\x04\x04\x04\x04\x04"
97 //              "\x04\x04\x04\x04", 12,
98                 "\xa9\xa7\x51\x0e\x6d\xb4\xd5\x88"
99                 "\xba\x34\x14\xcd\x0e\x09\x4d\x48"
100                 "\x0d\x68\x3f\x97\xb9\xcc\xb6\x12"
101                 "\xa5\x44\xfe\x8e\xf6\x5b\xa8\xe0", 32
102 //              "\xc8\x14\xd9\xd1\xdc\x7f\x37\xaa"
103 //              "\x13\xf0\xd7\x7f\x24\x94\xbd\xa1"
104 //              "\xc8\xde\x6b\x01\x6d\xd3\x88\xd2"
105 //              "\x99\x52\xa4\xc4\x67\x2b\x6c\xe8", 32
106         },
107         {
108                 "argon2id", NULL, 0, 3, 32, 4,
109                 "\x01\x01\x01\x01\x01\x01\x01\x01"
110                 "\x01\x01\x01\x01\x01\x01\x01\x01"
111                 "\x01\x01\x01\x01\x01\x01\x01\x01"
112                 "\x01\x01\x01\x01\x01\x01\x01\x01", 32,
113                 "\x02\x02\x02\x02\x02\x02\x02\x02"
114                 "\x02\x02\x02\x02\x02\x02\x02\x02", 16,
115 //              "\x03\x03\x03\x03\x03\x03\x03\x03", 8,
116 //              "\x04\x04\x04\x04\x04\x04\x04\x04"
117 //              "\x04\x04\x04\x04", 12,
118                 "\x03\xaa\xb9\x65\xc1\x20\x01\xc9"
119                 "\xd7\xd0\xd2\xde\x33\x19\x2c\x04"
120                 "\x94\xb6\x84\xbb\x14\x81\x96\xd7"
121                 "\x3c\x1d\xf1\xac\xaf\x6d\x0c\x2e", 32
122 //              "\x0d\x64\x0d\xf5\x8d\x78\x76\x6c"
123 //              "\x08\xc0\x37\xa3\x4a\x8b\x53\xc9"
124 //              "\xd0\x1e\xf0\x45\x2d\x75\xb6\x5e"
125 //              "\xb5\x25\x20\xe9\x6b\x01\xe6\x59", 32
126         },
127         /* empty password */
128         {
129                 "argon2i", NULL, 0, 3, 128, 1,
130                 "", 0,
131                 "\x00\x01\x02\x03\x04\x05\x06\x07"
132                 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f", 16,
133                 "\xbb\x1f\xf2\xb9\x9f\xd4\x4a\xd9"
134                 "\xdf\x7f\xb9\x54\x55\x9e\xb8\xeb"
135                 "\xb5\x9d\xab\xce\x2e\x62\x9f\x9b"
136                 "\x89\x09\xfe\xde\x57\xcc\x63\x86", 32
137         },
138         {
139                 "argon2id", NULL, 0, 3, 128, 1,
140                 "", 0,
141                 "\x00\x01\x02\x03\x04\x05\x06\x07"
142                 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f", 16,
143                 "\x09\x2f\x38\x35\xac\xb2\x43\x92"
144                 "\x93\xeb\xcd\xe8\x04\x16\x6a\x31"
145                 "\xce\x14\xd4\x55\xdb\xd8\xf7\xe6"
146                 "\xb4\xf5\x9d\x64\x8e\xd0\x3a\xdb", 32
147         },
148         /* RFC 3962 */
149         {
150                 "pbkdf2", "sha1", 64, 1, 0, 0,
151                 "password", 8,
152                 "ATHENA.MIT.EDUraeburn", 21,
153                 "\xcd\xed\xb5\x28\x1b\xb2\xf8\x01"
154                 "\x56\x5a\x11\x22\xb2\x56\x35\x15"
155                 "\x0a\xd1\xf7\xa0\x4b\xb9\xf3\xa3"
156                 "\x33\xec\xc0\xe2\xe1\xf7\x08\x37", 32
157         }, {
158                 "pbkdf2", "sha1", 64, 2, 0, 0,
159                 "password", 8,
160                 "ATHENA.MIT.EDUraeburn", 21,
161                 "\x01\xdb\xee\x7f\x4a\x9e\x24\x3e"
162                 "\x98\x8b\x62\xc7\x3c\xda\x93\x5d"
163                 "\xa0\x53\x78\xb9\x32\x44\xec\x8f"
164                 "\x48\xa9\x9e\x61\xad\x79\x9d\x86", 32
165         }, {
166                 "pbkdf2", "sha1", 64, 1200, 0, 0,
167                 "password", 8,
168                 "ATHENA.MIT.EDUraeburn", 21,
169                 "\x5c\x08\xeb\x61\xfd\xf7\x1e\x4e"
170                 "\x4e\xc3\xcf\x6b\xa1\xf5\x51\x2b"
171                 "\xa7\xe5\x2d\xdb\xc5\xe5\x14\x2f"
172                 "\x70\x8a\x31\xe2\xe6\x2b\x1e\x13", 32
173         }, {
174                 "pbkdf2", "sha1", 64, 5, 0, 0,
175                 "password", 8,
176                 "\0224VxxV4\022", 8, // "\x1234567878563412
177                 "\xd1\xda\xa7\x86\x15\xf2\x87\xe6"
178                 "\xa1\xc8\xb1\x20\xd7\x06\x2a\x49"
179                 "\x3f\x98\xd2\x03\xe6\xbe\x49\xa6"
180                 "\xad\xf4\xfa\x57\x4b\x6e\x64\xee", 32
181         }, {
182                 "pbkdf2", "sha1", 64, 1200, 0, 0,
183                 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
184                 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 64,
185                 "pass phrase equals block size", 29,
186                 "\x13\x9c\x30\xc0\x96\x6b\xc3\x2b"
187                 "\xa5\x5f\xdb\xf2\x12\x53\x0a\xc9"
188                 "\xc5\xec\x59\xf1\xa4\x52\xf5\xcc"
189                 "\x9a\xd9\x40\xfe\xa0\x59\x8e\xd1", 32
190         }, {
191                 "pbkdf2", "sha1", 64, 1200, 0, 0,
192                 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
193                 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 65,
194                 "pass phrase exceeds block size", 30,
195                 "\x9c\xca\xd6\xd4\x68\x77\x0c\xd5"
196                 "\x1b\x10\xe6\xa6\x87\x21\xbe\x61"
197                 "\x1a\x8b\x4d\x28\x26\x01\xdb\x3b"
198                 "\x36\xbe\x92\x46\x91\x5e\xc8\x2a", 32
199         }, {
200                 "pbkdf2", "sha1", 64, 50, 0, 0,
201                 "\360\235\204\236", 4, // g-clef ("\xf09d849e)
202                 "EXAMPLE.COMpianist", 18,
203                 "\x6b\x9c\xf2\x6d\x45\x45\x5a\x43"
204                 "\xa5\xb8\xbb\x27\x6a\x40\x3b\x39"
205                 "\xe7\xfe\x37\xa0\xc4\x1e\x02\xc2"
206                 "\x81\xff\x30\x69\xe1\xe9\x4f\x52", 32
207         }, {
208         /* RFC-6070 */
209                 "pbkdf2", "sha1", 64, 1, 0, 0,
210                 "password", 8,
211                 "salt", 4,
212                 "\x0c\x60\xc8\x0f\x96\x1f\x0e\x71\xf3\xa9"
213                 "\xb5\x24\xaf\x60\x12\x06\x2f\xe0\x37\xa6", 20
214         }, {
215                 "pbkdf2", "sha1", 64, 2, 0, 0,
216                 "password", 8,
217                 "salt", 4,
218                 "\xea\x6c\x01\x4d\xc7\x2d\x6f\x8c\xcd\x1e"
219                 "\xd9\x2a\xce\x1d\x41\xf0\xd8\xde\x89\x57", 20
220         }, {
221                 "pbkdf2", "sha1", 64, 4096, 0, 0,
222                 "password", 8,
223                 "salt", 4,
224                 "\x4b\x00\x79\x01\xb7\x65\x48\x9a\xbe\xad"
225                 "\x49\xd9\x26\xf7\x21\xd0\x65\xa4\x29\xc1", 20
226         }, {
227                 "pbkdf2", "sha1", 64, 16777216, 0, 0,
228                 "password", 8,
229                 "salt", 4,
230                 "\xee\xfe\x3d\x61\xcd\x4d\xa4\xe4\xe9\x94"
231                 "\x5b\x3d\x6b\xa2\x15\x8c\x26\x34\xe9\x84", 20
232         }, {
233                 "pbkdf2", "sha1", 64, 4096, 0, 0,
234                 "passwordPASSWORDpassword", 24,
235                 "saltSALTsaltSALTsaltSALTsaltSALTsalt", 36,
236                 "\x3d\x2e\xec\x4f\xe4\x1c\x84\x9b\x80\xc8"
237                 "\xd8\x36\x62\xc0\xe4\x4a\x8b\x29\x1a\x96"
238                 "\x4c\xf2\xf0\x70\x38", 25
239         }, {
240                 "pbkdf2", "sha1", 64, 4096, 0, 0,
241                 "pass\0word", 9,
242                 "sa\0lt", 5,
243                 "\x56\xfa\x6a\xa7\x55\x48\x09\x9d\xcc\x37"
244                 "\xd7\xf0\x34\x25\xe0\xc3", 16
245         }, {
246         /* empty password test */
247                 "pbkdf2", "sha1", 64, 2, 0, 0,
248                 "", 0,
249                 "salt", 4,
250                 "\x13\x3a\x4c\xe8\x37\xb4\xd2\x52\x1e\xe2"
251                 "\xbf\x03\xe1\x1c\x71\xca\x79\x4e\x07\x97", 20
252         }, {
253         /* Password exceeds block size test */
254                 "pbkdf2", "sha256", 64, 1200, 0, 0,
255                 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
256                 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 65,
257                 "pass phrase exceeds block size", 30,
258                 "\x22\x34\x4b\xc4\xb6\xe3\x26\x75"
259                 "\xa8\x09\x0f\x3e\xa8\x0b\xe0\x1d"
260                 "\x5f\x95\x12\x6a\x2c\xdd\xc3\xfa"
261                 "\xcc\x4a\x5e\x6d\xca\x04\xec\x58", 32
262         }, {
263                 "pbkdf2", "sha512", 128, 1200, 0, 0,
264                 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
265                 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
266                 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
267                 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 129,
268                 "pass phrase exceeds block size", 30,
269                 "\x0f\xb2\xed\x2c\x0e\x6e\xfb\x7d"
270                 "\x7d\x8e\xdd\x58\x01\xb4\x59\x72"
271                 "\x99\x92\x16\x30\x5e\xa4\x36\x8d"
272                 "\x76\x14\x80\xf3\xe3\x7a\x22\xb9", 32
273         }, {
274                 "pbkdf2", "whirlpool", 64, 1200, 0, 0,
275                 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
276                 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 65,
277                 "pass phrase exceeds block size", 30,
278                 "\x9c\x1c\x74\xf5\x88\x26\xe7\x6a"
279                 "\x53\x58\xf4\x0c\x39\xe7\x80\x89"
280                 "\x07\xc0\x31\x19\x9a\x50\xa2\x48"
281                 "\xf1\xd9\xfe\x78\x64\xe5\x84\x50", 32
282         }
283 };
284
285 /*
286  * Hash tests
287  */
288 struct hash_test_vector {
289         const char *data;
290         unsigned int data_length;
291         struct {
292                 const char *name;
293                 unsigned int length;
294                 const char *out;
295         } out[8];
296 };
297
298 static struct hash_test_vector hash_test_vectors[] = {
299 {
300         "", 0, {
301         { "crc32",      4, "\x00\x00\x00\x00" },
302         { "sha1",      20, "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18\x90\xaf\xd8\x07\x09" },
303         { "sha256",    32, "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9\x24"
304                            "\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52\xb8\x55" },
305         { "sha512",    64, "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80\x07"
306                            "\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c\xe9\xce"
307                            "\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87\x7e\xec\x2f"
308                            "\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a\xf9\x27\xda\x3e" },
309         { "ripemd160", 20, "\x9c\x11\x85\xa5\xc5\xe9\xfc\x54\x61\x28\x08\x97\x7e\xe8\xf5\x48\xb2\x25\x8d\x31" },
310         { "whirlpool", 64, "\x19\xfa\x61\xd7\x55\x22\xa4\x66\x9b\x44\xe3\x9c\x1d\x2e\x17\x26"
311                            "\xc5\x30\x23\x21\x30\xd4\x07\xf8\x9a\xfe\xe0\x96\x49\x97\xf7\xa7"
312                            "\x3e\x83\xbe\x69\x8b\x28\x8f\xeb\xcf\x88\xe3\xe0\x3c\x4f\x07\x57"
313                            "\xea\x89\x64\xe5\x9b\x63\xd9\x37\x08\xb1\x38\xcc\x42\xa6\x6e\xb3" },
314         { "blake2b-512",64,"\x78\x6a\x02\xf7\x42\x01\x59\x03\xc6\xc6\xfd\x85\x25\x52\xd2\x72"
315                            "\x91\x2f\x47\x40\xe1\x58\x47\x61\x8a\x86\xe2\x17\xf7\x1f\x54\x19"
316                            "\xd2\x5e\x10\x31\xaf\xee\x58\x53\x13\x89\x64\x44\x93\x4e\xb0\x4b"
317                            "\x90\x3a\x68\x5b\x14\x48\xb7\x55\xd5\x6f\x70\x1a\xfe\x9b\xe2\xce" },
318         { "blake2s-256",32,"\x69\x21\x7a\x30\x79\x90\x80\x94\xe1\x11\x21\xd0\x42\x35\x4a\x7c"
319                            "\x1f\x55\xb6\x48\x2c\xa1\xa5\x1e\x1b\x25\x0d\xfd\x1e\xd0\xee\xf9" },
320 }},{
321         "a", 1, {
322         { "crc32",      4, "\xe8\xb7\xbe\x43" },
323         { "sha1",      20, "\x86\xf7\xe4\x37\xfa\xa5\xa7\xfc\xe1\x5d\x1d\xdc\xb9\xea\xea\xea\x37\x76\x67\xb8" },
324         { "sha256",    32, "\xca\x97\x81\x12\xca\x1b\xbd\xca\xfa\xc2\x31\xb3\x9a\x23\xdc\x4d"
325                            "\xa7\x86\xef\xf8\x14\x7c\x4e\x72\xb9\x80\x77\x85\xaf\xee\x48\xbb" },
326         { "sha512",    64, "\x1f\x40\xfc\x92\xda\x24\x16\x94\x75\x09\x79\xee\x6c\xf5\x82\xf2"
327                            "\xd5\xd7\xd2\x8e\x18\x33\x5d\xe0\x5a\xbc\x54\xd0\x56\x0e\x0f\x53"
328                            "\x02\x86\x0c\x65\x2b\xf0\x8d\x56\x02\x52\xaa\x5e\x74\x21\x05\x46"
329                            "\xf3\x69\xfb\xbb\xce\x8c\x12\xcf\xc7\x95\x7b\x26\x52\xfe\x9a\x75" },
330         { "ripemd160", 20, "\x0b\xdc\x9d\x2d\x25\x6b\x3e\xe9\xda\xae\x34\x7b\xe6\xf4\xdc\x83\x5a\x46\x7f\xfe" },
331         { "whirlpool", 64, "\x8a\xca\x26\x02\x79\x2a\xec\x6f\x11\xa6\x72\x06\x53\x1f\xb7\xd7"
332                            "\xf0\xdf\xf5\x94\x13\x14\x5e\x69\x73\xc4\x50\x01\xd0\x08\x7b\x42"
333                            "\xd1\x1b\xc6\x45\x41\x3a\xef\xf6\x3a\x42\x39\x1a\x39\x14\x5a\x59"
334                            "\x1a\x92\x20\x0d\x56\x01\x95\xe5\x3b\x47\x85\x84\xfd\xae\x23\x1a" },
335         { "blake2b-512",64,"\x33\x3f\xcb\x4e\xe1\xaa\x7c\x11\x53\x55\xec\x66\xce\xac\x91\x7c"
336                            "\x8b\xfd\x81\x5b\xf7\x58\x7d\x32\x5a\xec\x18\x64\xed\xd2\x4e\x34"
337                            "\xd5\xab\xe2\xc6\xb1\xb5\xee\x3f\xac\xe6\x2f\xed\x78\xdb\xef\x80"
338                            "\x2f\x2a\x85\xcb\x91\xd4\x55\xa8\xf5\x24\x9d\x33\x08\x53\xcb\x3c" },
339         { "blake2s-256",32,"\x4a\x0d\x12\x98\x73\x40\x30\x37\xc2\xcd\x9b\x90\x48\x20\x36\x87"
340                            "\xf6\x23\x3f\xb6\x73\x89\x56\xe0\x34\x9b\xd4\x32\x0f\xec\x3e\x90" },
341 }},{
342         "abc", 3, {
343         { "crc32",      4, "\x35\x24\x41\xc2" },
344         { "sha1",      20, "\xa9\x99\x3e\x36\x47\x06\x81\x6a\xba\x3e\x25\x71\x78\x50\xc2\x6c\x9c\xd0\xd8\x9d" },
345         { "sha256",    32, "\xba\x78\x16\xbf\x8f\x01\xcf\xea\x41\x41\x40\xde\x5d\xae\x22\x23"
346                            "\xb0\x03\x61\xa3\x96\x17\x7a\x9c\xb4\x10\xff\x61\xf2\x00\x15\xad" },
347         { "sha512",    64, "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41\x31"
348                            "\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55\xd3\x9a"
349                            "\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3\xfe\xeb\xbd"
350                            "\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f\xa5\x4c\xa4\x9f" },
351         { "ripemd160", 20, "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6\xb0\x87\xf1\x5a\x0b\xfc" },
352         { "whirlpool", 64, "\x4e\x24\x48\xa4\xc6\xf4\x86\xbb\x16\xb6\x56\x2c\x73\xb4\x02\x0b"
353                            "\xf3\x04\x3e\x3a\x73\x1b\xce\x72\x1a\xe1\xb3\x03\xd9\x7e\x6d\x4c"
354                            "\x71\x81\xee\xbd\xb6\xc5\x7e\x27\x7d\x0e\x34\x95\x71\x14\xcb\xd6"
355                            "\xc7\x97\xfc\x9d\x95\xd8\xb5\x82\xd2\x25\x29\x20\x76\xd4\xee\xf5" },
356         { "blake2b-512",64,"\xba\x80\xa5\x3f\x98\x1c\x4d\x0d\x6a\x27\x97\xb6\x9f\x12\xf6\xe9"
357                            "\x4c\x21\x2f\x14\x68\x5a\xc4\xb7\x4b\x12\xbb\x6f\xdb\xff\xa2\xd1"
358                            "\x7d\x87\xc5\x39\x2a\xab\x79\x2d\xc2\x52\xd5\xde\x45\x33\xcc\x95"
359                            "\x18\xd3\x8a\xa8\xdb\xf1\x92\x5a\xb9\x23\x86\xed\xd4\x00\x99\x23" },
360         { "blake2s-256",32,"\x50\x8c\x5e\x8c\x32\x7c\x14\xe2\xe1\xa7\x2b\xa3\x4e\xeb\x45\x2f"
361                            "\x37\x45\x8b\x20\x9e\xd6\x3a\x29\x4d\x99\x9b\x4c\x86\x67\x59\x82" },
362 }},{
363         "abcdefghijklmnopqrstuvwxyz", 26, {
364         { "crc32",      4, "\x4c\x27\x50\xbd" },
365         { "sha1",      20, "\x32\xd1\x0c\x7b\x8c\xf9\x65\x70\xca\x04\xce\x37\xf2\xa1\x9d\x84\x24\x0d\x3a\x89" },
366         { "sha256",    32, "\x71\xc4\x80\xdf\x93\xd6\xae\x2f\x1e\xfa\xd1\x44\x7c\x66\xc9\x52"
367                            "\x5e\x31\x62\x18\xcf\x51\xfc\x8d\x9e\xd8\x32\xf2\xda\xf1\x8b\x73" },
368         { "sha512",    64, "\x4d\xbf\xf8\x6c\xc2\xca\x1b\xae\x1e\x16\x46\x8a\x05\xcb\x98\x81"
369                            "\xc9\x7f\x17\x53\xbc\xe3\x61\x90\x34\x89\x8f\xaa\x1a\xab\xe4\x29"
370                            "\x95\x5a\x1b\xf8\xec\x48\x3d\x74\x21\xfe\x3c\x16\x46\x61\x3a\x59"
371                            "\xed\x54\x41\xfb\x0f\x32\x13\x89\xf7\x7f\x48\xa8\x79\xc7\xb1\xf1" },
372         { "ripemd160", 20, "\xf7\x1c\x27\x10\x9c\x69\x2c\x1b\x56\xbb\xdc\xeb\x5b\x9d\x28\x65\xb3\x70\x8d\xbc" },
373         { "whirlpool", 64, "\xf1\xd7\x54\x66\x26\x36\xff\xe9\x2c\x82\xeb\xb9\x21\x2a\x48\x4a"
374                            "\x8d\x38\x63\x1e\xad\x42\x38\xf5\x44\x2e\xe1\x3b\x80\x54\xe4\x1b"
375                            "\x08\xbf\x2a\x92\x51\xc3\x0b\x6a\x0b\x8a\xae\x86\x17\x7a\xb4\xa6"
376                            "\xf6\x8f\x67\x3e\x72\x07\x86\x5d\x5d\x98\x19\xa3\xdb\xa4\xeb\x3b" },
377         { "blake2b-512",64,"\xc6\x8e\xde\x14\x3e\x41\x6e\xb7\xb4\xaa\xae\x0d\x8e\x48\xe5\x5d"
378                            "\xd5\x29\xea\xfe\xd1\x0b\x1d\xf1\xa6\x14\x16\x95\x3a\x2b\x0a\x56"
379                            "\x66\xc7\x61\xe7\xd4\x12\xe6\x70\x9e\x31\xff\xe2\x21\xb7\xa7\xa7"
380                            "\x39\x08\xcb\x95\xa4\xd1\x20\xb8\xb0\x90\xa8\x7d\x1f\xbe\xdb\x4c" },
381         { "blake2s-256",32,"\xbd\xf8\x8e\xb1\xf8\x6a\x0c\xdf\x0e\x84\x0b\xa8\x8f\xa1\x18\x50"
382                            "\x83\x69\xdf\x18\x6c\x73\x55\xb4\xb1\x6c\xf7\x9f\xa2\x71\x0a\x12" },
383 }},{
384         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 62, {
385         { "crc32",      4, "\x1f\xc2\xe6\xd2" },
386         { "sha1",      20, "\x76\x1c\x45\x7b\xf7\x3b\x14\xd2\x7e\x9e\x92\x65\xc4\x6f\x4b\x4d\xda\x11\xf9\x40" },
387         { "sha256",    32, "\xdb\x4b\xfc\xbd\x4d\xa0\xcd\x85\xa6\x0c\x3c\x37\xd3\xfb\xd8\x80"
388                            "\x5c\x77\xf1\x5f\xc6\xb1\xfd\xfe\x61\x4e\xe0\xa7\xc8\xfd\xb4\xc0" },
389         { "sha512",    64, "\x1e\x07\xbe\x23\xc2\x6a\x86\xea\x37\xea\x81\x0c\x8e\xc7\x80\x93"
390                            "\x52\x51\x5a\x97\x0e\x92\x53\xc2\x6f\x53\x6c\xfc\x7a\x99\x96\xc4"
391                            "\x5c\x83\x70\x58\x3e\x0a\x78\xfa\x4a\x90\x04\x1d\x71\xa4\xce\xab"
392                            "\x74\x23\xf1\x9c\x71\xb9\xd5\xa3\xe0\x12\x49\xf0\xbe\xbd\x58\x94" },
393         { "ripemd160", 20, "\xb0\xe2\x0b\x6e\x31\x16\x64\x02\x86\xed\x3a\x87\xa5\x71\x30\x79\xb2\x1f\x51\x89" },
394         { "whirlpool", 64, "\xdc\x37\xe0\x08\xcf\x9e\xe6\x9b\xf1\x1f\x00\xed\x9a\xba\x26\x90"
395                            "\x1d\xd7\xc2\x8c\xde\xc0\x66\xcc\x6a\xf4\x2e\x40\xf8\x2f\x3a\x1e"
396                            "\x08\xeb\xa2\x66\x29\x12\x9d\x8f\xb7\xcb\x57\x21\x1b\x92\x81\xa6"
397                            "\x55\x17\xcc\x87\x9d\x7b\x96\x21\x42\xc6\x5f\x5a\x7a\xf0\x14\x67" },
398         { "blake2b-512",64,"\x99\x96\x48\x02\xe5\xc2\x5e\x70\x37\x22\x90\x5d\x3f\xb8\x00\x46"
399                            "\xb6\xbc\xa6\x98\xca\x9e\x2c\xc7\xe4\x9b\x4f\xe1\xfa\x08\x7c\x2e"
400                            "\xdf\x03\x12\xdf\xbb\x27\x5c\xf2\x50\xa1\xe5\x42\xfd\x5d\xc2\xed"
401                            "\xd3\x13\xf9\xc4\x91\x12\x7c\x2e\x8c\x0c\x9b\x24\x16\x8e\x2d\x50" },
402         { "blake2s-256",32,"\xc7\x54\x39\xea\x17\xe1\xde\x6f\xa4\x51\x0c\x33\x5d\xc3\xd3\xf3"
403                            "\x43\xe6\xf9\xe1\xce\x27\x73\xe2\x5b\x41\x74\xf1\xdf\x8b\x11\x9b" },
404 }},{
405         "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56, {
406         { "crc32",      4, "\x17\x1a\x3f\x5f" },
407         { "sha1",      20, "\x84\x98\x3e\x44\x1c\x3b\xd2\x6e\xba\xae\x4a\xa1\xf9\x51\x29\xe5\xe5\x46\x70\xf1" },
408         { "sha256",    32, "\x24\x8d\x6a\x61\xd2\x06\x38\xb8\xe5\xc0\x26\x93\x0c\x3e\x60\x39"
409                            "\xa3\x3c\xe4\x59\x64\xff\x21\x67\xf6\xec\xed\xd4\x19\xdb\x06\xc1" },
410         { "sha512",    64, "\x20\x4a\x8f\xc6\xdd\xa8\x2f\x0a\x0c\xed\x7b\xeb\x8e\x08\xa4\x16"
411                            "\x57\xc1\x6e\xf4\x68\xb2\x28\xa8\x27\x9b\xe3\x31\xa7\x03\xc3\x35"
412                            "\x96\xfd\x15\xc1\x3b\x1b\x07\xf9\xaa\x1d\x3b\xea\x57\x78\x9c\xa0"
413                            "\x31\xad\x85\xc7\xa7\x1d\xd7\x03\x54\xec\x63\x12\x38\xca\x34\x45" },
414         { "ripemd160", 20, "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc\xf4\x9a\xda\x62\xeb\x2b" },
415         { "whirlpool", 64, "\x52\x6b\x23\x94\xd8\x56\x83\xe2\x4b\x29\xac\xd0\xfd\x37\xf7\xd5"
416                            "\x02\x7f\x61\x36\x6a\x14\x07\x26\x2d\xc2\xa6\xa3\x45\xd9\xe2\x40"
417                            "\xc0\x17\xc1\x83\x3d\xb1\xe6\xdb\x6a\x46\xbd\x44\x4b\x0c\x69\x52"
418                            "\x0c\x85\x6e\x7c\x6e\x9c\x36\x6d\x15\x0a\x7d\xa3\xae\xb1\x60\xd1" },
419         { "blake2b-512",64,"\x72\x85\xff\x3e\x8b\xd7\x68\xd6\x9b\xe6\x2b\x3b\xf1\x87\x65\xa3"
420                            "\x25\x91\x7f\xa9\x74\x4a\xc2\xf5\x82\xa2\x08\x50\xbc\x2b\x11\x41"
421                            "\xed\x1b\x3e\x45\x28\x59\x5a\xcc\x90\x77\x2b\xdf\x2d\x37\xdc\x8a"
422                            "\x47\x13\x0b\x44\xf3\x3a\x02\xe8\x73\x0e\x5a\xd8\xe1\x66\xe8\x88" },
423         { "blake2s-256",32,"\x6f\x4d\xf5\x11\x6a\x6f\x33\x2e\xda\xb1\xd9\xe1\x0e\xe8\x7d\xf6"
424                            "\x55\x7b\xea\xb6\x25\x9d\x76\x63\xf3\xbc\xd5\x72\x2c\x13\xf1\x89" },
425 }},{
426         "message digest", 14, {
427         { "crc32",      4, "\x20\x15\x9d\x7f" },
428         { "sha1",      20, "\xc1\x22\x52\xce\xda\x8b\xe8\x99\x4d\x5f\xa0\x29\x0a\x47\x23\x1c\x1d\x16\xaa\xe3" },
429         { "sha256",    32, "\xf7\x84\x6f\x55\xcf\x23\xe1\x4e\xeb\xea\xb5\xb4\xe1\x55\x0c\xad"
430                            "\x5b\x50\x9e\x33\x48\xfb\xc4\xef\xa3\xa1\x41\x3d\x39\x3c\xb6\x50" },
431         { "sha512",    64, "\x10\x7d\xbf\x38\x9d\x9e\x9f\x71\xa3\xa9\x5f\x6c\x05\x5b\x92\x51"
432                            "\xbc\x52\x68\xc2\xbe\x16\xd6\xc1\x34\x92\xea\x45\xb0\x19\x9f\x33"
433                            "\x09\xe1\x64\x55\xab\x1e\x96\x11\x8e\x8a\x90\x5d\x55\x97\xb7\x20"
434                            "\x38\xdd\xb3\x72\xa8\x98\x26\x04\x6d\xe6\x66\x87\xbb\x42\x0e\x7c" },
435         { "ripemd160", 20, "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8\x5f\xfa\x21\x59\x5f\x36" },
436         { "whirlpool", 64, "\x37\x8c\x84\xa4\x12\x6e\x2d\xc6\xe5\x6d\xcc\x74\x58\x37\x7a\xac"
437                            "\x83\x8d\x00\x03\x22\x30\xf5\x3c\xe1\xf5\x70\x0c\x0f\xfb\x4d\x3b"
438                            "\x84\x21\x55\x76\x59\xef\x55\xc1\x06\xb4\xb5\x2a\xc5\xa4\xaa\xa6"
439                            "\x92\xed\x92\x00\x52\x83\x8f\x33\x62\xe8\x6d\xbd\x37\xa8\x90\x3e" },
440         { "blake2b-512",64,"\x3c\x26\xce\x48\x7b\x1c\x0f\x06\x23\x63\xaf\xa3\xc6\x75\xeb\xdb"
441                            "\xf5\xf4\xef\x9b\xdc\x02\x2c\xfb\xef\x91\xe3\x11\x1c\xdc\x28\x38"
442                            "\x40\xd8\x33\x1f\xc3\x0a\x8a\x09\x06\xcf\xf4\xbc\xdb\xcd\x23\x0c"
443                            "\x61\xaa\xec\x60\xfd\xfa\xd4\x57\xed\x96\xb7\x09\xa3\x82\x35\x9a" },
444         { "blake2s-256",32,"\xfa\x10\xab\x77\x5a\xcf\x89\xb7\xd3\xc8\xa6\xe8\x23\xd5\x86\xf6"
445                            "\xb6\x7b\xdb\xac\x4c\xe2\x07\xfe\x14\x5b\x7d\x3a\xc2\x5c\xd2\x8c" },
446 }}};
447
448 /*
449  * HMAC tests
450  */
451 // RFC 4231 - HMAC test vectors for SHA-256, SHA-512
452 // RFC 2202 - HMAC test vectors for SHA-1
453
454 struct hmac_test_vector {
455         const char *key;
456         unsigned int key_length;
457         const char *data;
458         unsigned int data_length;
459         struct {
460                 const char *name;
461                 unsigned int length;
462                 const char *out;
463         } out[3];
464 };
465
466 static struct hmac_test_vector hmac_test_vectors[] = {
467 {
468         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", 20,
469         "\x48\x69\x20\x54\x68\x65\x72\x65", 8, /* "Hi There" */ {
470         { "sha1",   20, "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00" },
471         { "sha256", 32, "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1\x2b"
472                         "\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32\xcf\xf7" },
473         { "sha512", 64, "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c\xb0"
474                         "\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1\x7c\xde"
475                         "\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae\xa3\xf4\xe4"
476                         "\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20\x3a\x12\x68\x54" },
477 }},{
478         "\x4a\x65\x66\x65", 4, /* "Jefe" */
479         "\x77\x68\x61\x74\x20\x64\x6f\x20\x79\x61\x20\x77\x61\x6e\x74\x20"
480         "\x66\x6f\x72\x20\x6e\x6f\x74\x68\x69\x6e\x67\x3f", 28, /* "what do ya want for nothing?" */ {
481         { "sha1",   20, "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79" },
482         { "sha256", 32, "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75\xc7"
483                         "\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec\x38\x43" },
484         { "sha512", 64, "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0\xa3"
485                         "\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25\x05\x54"
486                         "\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8\xf0\xe6\xfd"
487                         "\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a\x38\xbc\xe7\x37" },
488 }},{
489         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa", 20,
490         "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
491         "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
492         "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd", 50, {
493         { "sha1",   20, "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3" },
494         { "sha256", 32, "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81\xa7"
495                         "\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5\x65\xfe" },
496         { "sha512", 64, "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b\xe9"
497                         "\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27\x9d\x39"
498                         "\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e\x67\xc8\x07"
499                         "\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59\xe1\x32\x92\xfb" },
500 }},{
501         "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19", 25,
502         "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
503         "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
504         "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd", 50, {
505         { "sha1",   20, "\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda" },
506         { "sha256", 32, "\x82\x55\x8a\x38\x9a\x44\x3c\x0e\xa4\xcc\x81\x98\x99\xf2\x08\x3a"
507                         "\x85\xf0\xfa\xa3\xe5\x78\xf8\x07\x7a\x2e\x3f\xf4\x67\x29\x66\x5b" },
508         { "sha512", 64, "\xb0\xba\x46\x56\x37\x45\x8c\x69\x90\xe5\xa8\xc5\xf6\x1d\x4a\xf7"
509                         "\xe5\x76\xd9\x7f\xf9\x4b\x87\x2d\xe7\x6f\x80\x50\x36\x1e\xe3\xdb"
510                         "\xa9\x1c\xa5\xc1\x1a\xa2\x5e\xb4\xd6\x79\x27\x5c\xc5\x78\x80\x63"
511                         "\xa5\xf1\x97\x41\x12\x0c\x4f\x2d\xe2\xad\xeb\xeb\x10\xa2\x98\xdd" },
512 }},{
513         // Long key
514         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
515         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
516         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
517         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
518         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
519         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
520         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
521         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa", 131,
522         "\x54\x65\x73\x74\x20\x55\x73\x69\x6e\x67\x20\x4c\x61\x72\x67\x65"
523         "\x72\x20\x54\x68\x61\x6e\x20\x42\x6c\x6f\x63\x6b\x2d\x53\x69\x7a"
524         "\x65\x20\x4b\x65\x79\x20\x2d\x20\x48\x61\x73\x68\x20\x4b\x65\x79"
525         "\x20\x46\x69\x72\x73\x74", 54, /* "Test Using Larger Than Block-Size Key - Hash Key First" */ {
526         { "sha1",   20, "\x90\xd0\xda\xce\x1c\x1b\xdc\x95\x73\x39\x30\x78\x03\x16\x03\x35\xbd\xe6\xdf\x2b" },
527         { "sha256", 32, "\x60\xe4\x31\x59\x1e\xe0\xb6\x7f\x0d\x8a\x26\xaa\xcb\xf5\xb7\x7f"
528                         "\x8e\x0b\xc6\x21\x37\x28\xc5\x14\x05\x46\x04\x0f\x0e\xe3\x7f\x54" },
529         { "sha512", 64, "\x80\xb2\x42\x63\xc7\xc1\xa3\xeb\xb7\x14\x93\xc1\xdd\x7b\xe8\xb4"
530                         "\x9b\x46\xd1\xf4\x1b\x4a\xee\xc1\x12\x1b\x01\x37\x83\xf8\xf3\x52"
531                         "\x6b\x56\xd0\x37\xe0\x5f\x25\x98\xbd\x0f\xd2\x21\x5d\x6a\x1e\x52"
532                         "\x95\xe6\x4f\x73\xf6\x3f\x0a\xec\x8b\x91\x5a\x98\x5d\x78\x65\x98" },
533 }},{
534         // Long key and long data
535         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
536         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
537         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
538         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
539         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
540         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
541         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
542         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa", 131,
543         "\x54\x68\x69\x73\x20\x69\x73\x20\x61\x20\x74\x65\x73\x74\x20\x75"
544         "\x73\x69\x6e\x67\x20\x61\x20\x6c\x61\x72\x67\x65\x72\x20\x74\x68"
545         "\x61\x6e\x20\x62\x6c\x6f\x63\x6b\x2d\x73\x69\x7a\x65\x20\x6b\x65"
546         "\x79\x20\x61\x6e\x64\x20\x61\x20\x6c\x61\x72\x67\x65\x72\x20\x74"
547         "\x68\x61\x6e\x20\x62\x6c\x6f\x63\x6b\x2d\x73\x69\x7a\x65\x20\x64"
548         "\x61\x74\x61\x2e\x20\x54\x68\x65\x20\x6b\x65\x79\x20\x6e\x65\x65"
549         "\x64\x73\x20\x74\x6f\x20\x62\x65\x20\x68\x61\x73\x68\x65\x64\x20"
550         "\x62\x65\x66\x6f\x72\x65\x20\x62\x65\x69\x6e\x67\x20\x75\x73\x65"
551         "\x64\x20\x62\x79\x20\x74\x68\x65\x20\x48\x4d\x41\x43\x20\x61\x6c\x67\x6f\x72\x69\x74\x68\x6d\x2e", 152, {
552         { "sha1",   20, "\x21\x7e\x44\xbb\x08\xb6\xe0\x6a\x2d\x6c\x30\xf3\xcb\x9f\x53\x7f\x97\xc6\x33\x56" },
553         { "sha256", 32, "\x9b\x09\xff\xa7\x1b\x94\x2f\xcb\x27\x63\x5f\xbc\xd5\xb0\xe9\x44"
554                         "\xbf\xdc\x63\x64\x4f\x07\x13\x93\x8a\x7f\x51\x53\x5c\x3a\x35\xe2" },
555         { "sha512", 64, "\xe3\x7b\x6a\x77\x5d\xc8\x7d\xba\xa4\xdf\xa9\xf9\x6e\x5e\x3f\xfd"
556                         "\xde\xbd\x71\xf8\x86\x72\x89\x86\x5d\xf5\xa3\x2d\x20\xcd\xc9\x44"
557                         "\xb6\x02\x2c\xac\x3c\x49\x82\xb1\x0d\x5e\xeb\x55\xc3\xe4\xde\x15"
558                         "\x13\x46\x76\xfb\x6d\xe0\x44\x60\x65\xc9\x74\x40\xfa\x8c\x6a\x58" },
559 }}};
560
561 /*
562  * Block cipher tests
563  */
564 struct cipher_test_vector {
565         const char *key;
566         unsigned int key_length;
567         const char *iv;
568         unsigned int iv_length;
569         const char *plaintext;
570         unsigned int data_length;
571         struct {
572                 const char *name;
573                 const char *mode;
574                 const char *ciphertext;
575         } out[2];
576 };
577
578 static struct cipher_test_vector cipher_test_vectors[] = {
579 { // NIST SP 800-38A
580         "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c", 16,
581         NULL, 0,
582         "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a"
583         "\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51"
584         "\x30\xc8\x1c\x46\xa3\x5c\xe4\x11\xe5\xfb\xc1\x19\x1a\x0a\x52\xef"
585         "\xf6\x9f\x24\x45\xdf\x4f\x9b\x17\xad\x2b\x41\x7b\xe6\x6c\x37\x10", 64, {
586         {
587                 "aes", "ecb",
588                 "\x3a\xd7\x7b\xb4\x0d\x7a\x36\x60\xa8\x9e\xca\xf3\x24\x66\xef\x97"
589                 "\xf5\xd3\xd5\x85\x03\xb9\x69\x9d\xe7\x85\x89\x5a\x96\xfd\xba\xaf"
590                 "\x43\xb1\xcd\x7f\x59\x8e\xce\x23\x88\x1b\x00\xe3\xed\x03\x06\x88"
591                 "\x7b\x0c\x78\x5e\x27\xe8\xad\x3f\x82\x23\x20\x71\x04\x72\x5d\xd4"
592         },{
593                 "serpent", "ecb",
594                 "\xf7\xa7\x21\xe6\xc7\x56\xb6\x55\xcb\xdf\x53\x3f\xc3\xb3\x1a\xc4"
595                 "\x4b\xc6\x04\x29\x3a\x81\xa6\xa6\xe4\xcb\xa7\x8d\x1a\x32\xa2\x9e"
596                 "\xcf\xc2\x8e\x50\x97\xdd\x6b\x49\xa9\x38\xb1\x51\x5e\xbc\x5a\xac"
597                 "\xfe\xd2\xc4\x95\x92\xf9\x1c\x0c\x9f\x17\xcd\x86\x38\x65\x29\xeb"
598         },
599 }},{ // NIST SP 800-38A
600         "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c", 16,
601         "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f", 16,
602         "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a"
603         "\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51"
604         "\x30\xc8\x1c\x46\xa3\x5c\xe4\x11\xe5\xfb\xc1\x19\x1a\x0a\x52\xef"
605         "\xf6\x9f\x24\x45\xdf\x4f\x9b\x17\xad\x2b\x41\x7b\xe6\x6c\x37\x10", 64, {
606         {
607                 "aes", "cbc",
608                 "\x76\x49\xab\xac\x81\x19\xb2\x46\xce\xe9\x8e\x9b\x12\xe9\x19\x7d"
609                 "\x50\x86\xcb\x9b\x50\x72\x19\xee\x95\xdb\x11\x3a\x91\x76\x78\xb2"
610                 "\x73\xbe\xd6\xb8\xe3\xc1\x74\x3b\x71\x16\xe6\x9e\x22\x22\x95\x16"
611                 "\x3f\xf1\xca\xa1\x68\x1f\xac\x09\x12\x0e\xca\x30\x75\x86\xe1\xa7"
612         },{
613                 "serpent", "cbc",
614                 "\xdd\x73\x69\x1a\xb5\x66\xb6\x38\xe3\xb9\x62\x36\xc8\xc8\xa1\xdd"
615                 "\xa9\xb5\xd9\xdb\x20\xfb\x8b\x82\x51\x40\xbf\xe6\x4d\xf2\x1c\xa8"
616                 "\x5f\x48\xbc\x29\xff\x62\x27\xda\x09\x7c\xaa\x22\x75\x6f\x43\xff"
617                 "\x31\xd8\x3e\x83\x4d\x92\x48\xeb\x49\x1c\xf8\x26\x80\x4e\xb9\x02"
618         },
619 }},{ // NIST SP 800-38A
620         "\x60\x3d\xeb\x10\x15\xca\x71\xbe\x2b\x73\xae\xf0\x85\x7d\x77\x81"
621         "\x1f\x35\x2c\x07\x3b\x61\x08\xd7\x2d\x98\x10\xa3\x09\x14\xdf\xf4", 32,
622         NULL, 0,
623         "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a"
624         "\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51"
625         "\x30\xc8\x1c\x46\xa3\x5c\xe4\x11\xe5\xfb\xc1\x19\x1a\x0a\x52\xef"
626         "\xf6\x9f\x24\x45\xdf\x4f\x9b\x17\xad\x2b\x41\x7b\xe6\x6c\x37\x10", 64, {
627         {
628                 "aes", "ecb",
629                 "\xf3\xee\xd1\xbd\xb5\xd2\xa0\x3c\x06\x4b\x5a\x7e\x3d\xb1\x81\xf8"
630                 "\x59\x1c\xcb\x10\xd4\x10\xed\x26\xdc\x5b\xa7\x4a\x31\x36\x28\x70"
631                 "\xb6\xed\x21\xb9\x9c\xa6\xf4\xf9\xf1\x53\xe7\xb1\xbe\xaf\xed\x1d"
632                 "\x23\x30\x4b\x7a\x39\xf9\xf3\xff\x06\x7d\x8d\x8f\x9e\x24\xec\xc7"
633         },{
634                 "serpent", "ecb",
635                 "\x78\xe5\x84\x8e\xd9\xd5\xde\x2d\x4d\xb0\x2f\x53\x61\x6a\xfd\xf2"
636                 "\x50\x5d\xf1\x68\x92\x40\x8e\xf6\x9c\x3b\x9e\xa6\x67\xd9\xdd\xb8"
637                 "\xb9\x5f\xc8\x20\x76\x52\x1d\xce\x60\xe4\xfc\xac\xe3\xd3\x91\x51"
638                 "\x09\x22\x62\xde\x62\x6d\xc5\x7b\x4c\x87\x0c\x65\xe7\x1f\xc7\x13"
639         },
640 }},{ // NIST SP 800-38A
641         "\x60\x3d\xeb\x10\x15\xca\x71\xbe\x2b\x73\xae\xf0\x85\x7d\x77\x81"
642         "\x1f\x35\x2c\x07\x3b\x61\x08\xd7\x2d\x98\x10\xa3\x09\x14\xdf\xf4", 32,
643         "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f", 16,
644         "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a"
645         "\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51"
646         "\x30\xc8\x1c\x46\xa3\x5c\xe4\x11\xe5\xfb\xc1\x19\x1a\x0a\x52\xef"
647         "\xf6\x9f\x24\x45\xdf\x4f\x9b\x17\xad\x2b\x41\x7b\xe6\x6c\x37\x10", 64, {
648         {
649                 "aes", "cbc",
650                 "\xf5\x8c\x4c\x04\xd6\xe5\xf1\xba\x77\x9e\xab\xfb\x5f\x7b\xfb\xd6"
651                 "\x9c\xfc\x4e\x96\x7e\xdb\x80\x8d\x67\x9f\x77\x7b\xc6\x70\x2c\x7d"
652                 "\x39\xf2\x33\x69\xa9\xd9\xba\xcf\xa5\x30\xe2\x63\x04\x23\x14\x61"
653                 "\xb2\xeb\x05\xe2\xc3\x9b\xe9\xfc\xda\x6c\x19\x07\x8c\x6a\x9d\x1b"
654         },{
655                 "serpent", "cbc",
656                 "\xb8\x93\xc8\xde\xc5\xc8\x5f\x03\x01\xac\x32\x74\xdf\xc6\x71\x9d"
657                 "\x37\x61\xc5\xf8\x34\x4d\xe9\x10\x91\xd3\x87\x80\x42\xcc\x70\x95"
658                 "\x40\x95\xa3\x2c\xdb\x38\xe2\x6f\x03\x91\xf5\xd3\x51\x7e\x52\xb0"
659                 "\x8a\x1c\x2d\x7f\x04\x59\x13\x93\x31\xa9\x82\xc9\x4e\xd9\x11\x0c"
660         },
661 }},{ // CAVS XTSGenAES128,101
662         "\xb7\xb9\x3f\x51\x6a\xef\x29\x5e\xff\x3a\x29\xd8\x37\xcf\x1f\x13"
663         "\x53\x47\xe8\xa2\x1d\xae\x61\x6f\xf5\x06\x2b\x2e\x8d\x78\xce\x5e", 32,
664         "\x87\x3e\xde\xa6\x53\xb6\x43\xbd\x8b\xcf\x51\x40\x31\x97\xed\x14", 16,
665         "\x23\x6f\x8a\x5b\x58\xdd\x55\xf6\x19\x4e\xd7\x0c\x4a\xc1\xa1\x7f"
666         "\x1f\xe6\x0e\xc9\xa6\xc4\x54\xd0\x87\xcc\xb7\x7d\x6b\x63\x8c\x47", 32, {
667         {
668                 "aes", "xts",
669                 "\x22\xe6\xa3\xc6\x37\x9d\xcf\x75\x99\xb0\x52\xb5\xa7\x49\xc7\xf7"
670                 "\x8a\xd8\xa1\x1b\x9f\x1a\xa9\x43\x0c\xf3\xae\xf4\x45\x68\x2e\x19"
671         },{
672                 "serpent", "xts",
673                 "\x6d\xa2\xa4\x2b\x18\x71\x57\xdc\x03\xaf\x8b\x82\x28\x66\x3d\xf1"
674                 "\x70\x8b\x75\x98\xd2\xdd\xbf\x72\x9e\xb3\xb4\xc2\x3f\x18\xdf\xa1"
675         },
676 }},{ // CAVS XTSGenAES256,101
677         "\x26\x6c\x33\x6b\x3b\x01\x48\x9f\x32\x67\xf5\x28\x35\xfd\x92\xf6"
678         "\x74\x37\x4b\x88\xb4\xe1\xeb\xd2\xd3\x6a\x5f\x45\x75\x81\xd9\xd0"
679         "\x42\xc3\xee\xf7\xb0\xb7\xe5\x13\x7b\x08\x64\x96\xb4\xd9\xe6\xac"
680         "\x65\x8d\x71\x96\xa2\x3f\x23\xf0\x36\x17\x2f\xdb\x8f\xae\xe5\x27", 64,
681         "\x06\xb2\x09\xa7\xa2\x2f\x48\x6e\xcb\xfa\xdb\x0f\x31\x37\xba\x42", 16,
682         "\xca\x7d\x65\xef\x8d\x3d\xfa\xd3\x45\xb6\x1c\xcd\xdc\xa1\xad\x81"
683         "\xde\x83\x0b\x9e\x86\xc7\xb4\x26\xd7\x6c\xb7\xdb\x76\x68\x52\xd9"
684         "\x81\xc6\xb2\x14\x09\x39\x9d\x78\xf4\x2c\xc0\xb3\x3a\x7b\xbb\x06", 48, {
685         {
686                 "aes", "xts",
687                 "\xc7\x32\x56\x87\x0c\xc2\xf4\xdd\x57\xac\xc7\x4b\x54\x56\xdb\xd7"
688                 "\x76\x91\x2a\x12\x8b\xc1\xf7\x7d\x72\xcd\xeb\xbf\x27\x00\x44\xb7"
689                 "\xa4\x3c\xee\xd2\x90\x25\xe1\xe8\xbe\x21\x1f\xa3\xc3\xed\x00\x2d"
690         },{
691                 "serpent", "xts",
692                 "\x37\xe4\xc0\xa9\xf1\x49\xe5\x3e\x73\xb9\x1f\xec\xdc\xe0\xbd\xc5"
693                 "\x31\xd7\xef\x08\x65\x20\xe3\xad\xd9\x84\x60\xdc\x61\x6f\x26\x86"
694                 "\xb8\xd5\x29\x4b\x04\x41\x52\x59\x05\x00\xb0\xc2\x9b\x30\xda\x48"
695         },
696 }},{
697         "\xa5\x28\x24\x34\x1a\x3c\xd8\xf7\x05\x91\x8f\xee\x85\x1f\x35\x7f"
698         "\x80\x3d\xfc\x9b\x94\xf6\xfc\x9e\x19\x09\x00\xa9\x04\x31\x4f\x11", 32,
699         "\xa1\xba\x49\x95\xff\x34\x6d\xb8\xcd\x87\x5d\x5e\xfd\xea\x85\xdb"
700         "\x8a\x7b\x5e\xb2\x5d\x57\xdd\x62\xac\xa9\x8c\x41\x42\x94\x75\xb7", 32,
701         "\x69\xb4\xe8\x8c\x37\xe8\x67\x82\xf1\xec\x5d\x04\xe5\x14\x91\x13"
702         "\xdf\xf2\x87\x1b\x69\x81\x1d\x71\x70\x9e\x9c\x3b\xde\x49\x70\x11"
703         "\xa0\xa3\xdb\x0d\x54\x4f\x66\x69\xd7\xdb\x80\xa7\x70\x92\x68\xce"
704         "\x81\x04\x2c\xc6\xab\xae\xe5\x60\x15\xe9\x6f\xef\xaa\x8f\xa7\xa7"
705         "\x63\x8f\xf2\xf0\x77\xf1\xa8\xea\xe1\xb7\x1f\x9e\xab\x9e\x4b\x3f"
706         "\x07\x87\x5b\x6f\xcd\xa8\xaf\xb9\xfa\x70\x0b\x52\xb8\xa8\xa7\x9e"
707         "\x07\x5f\xa6\x0e\xb3\x9b\x79\x13\x79\xc3\x3e\x8d\x1c\x2c\x68\xc8"
708         "\x51\x1d\x3c\x7b\x7d\x79\x77\x2a\x56\x65\xc5\x54\x23\x28\xb0\x03", 128, {
709         {
710                 "xchacha12,aes", "adiantum",
711                 "\x9e\x16\xab\xed\x4b\xa7\x42\x5a\xc6\xfb\x4e\x76\xff\xbe\x03\xa0"
712                 "\x0f\xe3\xad\xba\xe4\x98\x2b\x0e\x21\x48\xa0\xb8\x65\x48\x27\x48"
713                 "\x84\x54\x54\xb2\x9a\x94\x7b\xe6\x4b\x29\xe9\xcf\x05\x91\x80\x1a"
714                 "\x3a\xf3\x41\x96\x85\x1d\x9f\x74\x51\x56\x63\xfa\x7c\x28\x85\x49"
715                 "\xf7\x2f\xf9\xf2\x18\x46\xf5\x33\x80\xa3\x3c\xce\xb2\x57\x93\xf5"
716                 "\xae\xbd\xa9\xf5\x7b\x30\xc4\x93\x66\xe0\x30\x77\x16\xe4\xa0\x31"
717                 "\xba\x70\xbc\x68\x13\xf5\xb0\x9a\xc1\xfc\x7e\xfe\x55\x80\x5c\x48"
718                 "\x74\xa6\xaa\xa3\xac\xdc\xc2\xf5\x8d\xde\x34\x86\x78\x60\x75\x8d",
719         },{
720                 "xchacha20,aes", "adiantum",
721                 "\xb1\x8b\xa0\x05\x77\xa8\x4d\x59\x1b\x8e\x21\xfc\x3a\x49\xfa\xd4"
722                 "\xeb\x36\xf3\xc4\xdf\xdc\xae\x67\x07\x3f\x70\x0e\xe9\x66\xf5\x0c"
723                 "\x30\x4d\x66\xc9\xa4\x2f\x73\x9c\x13\xc8\x49\x44\xcc\x0a\x90\x9d"
724                 "\x7c\xdd\x19\x3f\xea\x72\x8d\x58\xab\xe7\x09\x2c\xec\xb5\x44\xd2"
725                 "\xca\xa6\x2d\x7a\x5c\x9c\x2b\x15\xec\x2a\xa6\x69\x91\xf9\xf3\x13"
726                 "\xf7\x72\xc1\xc1\x40\xd5\xe1\x94\xf4\x29\xa1\x3e\x25\x02\xa8\x3e"
727                 "\x94\xc1\x91\x14\xa1\x14\xcb\xbe\x67\x4c\xb9\x38\xfe\xa7\xaa\x32"
728                 "\x29\x62\x0d\xb2\xf6\x3c\x58\x57\xc1\xd5\x5a\xbb\xd6\xa6\x2a\xe5"
729         },
730 }}};
731
732 /*
733  * Cipher IV tests
734  */
735 struct cipher_iv_test_vector {
736         const char *cipher_name;
737         const char *cipher_mode;
738         const char *key;
739         unsigned int key_length;
740         const char *iv_name;
741         uint64_t iv_offset;
742         unsigned int data_length;
743         const char in_sha256[32];
744         struct {
745                 size_t sector_size;
746                 bool large_iv;
747                 const char out_sha256[32];
748         } out[7];
749 };
750
751 static struct cipher_iv_test_vector cipher_iv_test_vectors[] = {
752 {
753         "aes", "cbc",
754         "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c", 16,
755         "null", UINT32_MAX-7, 8192,
756         "\x9f\x1d\xcb\xc3\x5c\x35\x0d\x60\x27\xf9\x8b\xe0\xf5\xc8\xb4\x3b"
757         "\x42\xca\x52\xb7\x60\x44\x59\xc0\xc4\x2b\xe3\xaa\x88\x91\x3d\x47", {
758         {       512, false,
759                 "\xfd\x05\xd0\x4d\x51\xb9\xd4\x87\xa4\x57\x9a\x62\x07\x39\xc9\x4a"
760                 "\x00\x90\x3e\xaf\xe8\xb2\xac\x12\xca\xeb\x58\xf9\x48\xf6\xef\x08"
761         },{     1024, false,
762                 "\x55\x87\x5c\xde\x86\x6a\x8b\xab\x08\xbe\x5b\x38\x17\x53\xdf\xe5"
763                 "\x7e\xb9\x5f\x59\xaf\x07\xa4\xca\x6a\x24\xd1\x12\xa9\x15\x25\xf4"
764         },{     1024, true,
765                 "\x55\x87\x5c\xde\x86\x6a\x8b\xab\x08\xbe\x5b\x38\x17\x53\xdf\xe5"
766                 "\x7e\xb9\x5f\x59\xaf\x07\xa4\xca\x6a\x24\xd1\x12\xa9\x15\x25\xf4"
767         },{     2048, false,
768                 "\x55\x5b\x8e\x74\x90\x9d\x0d\x4b\x74\x8c\x16\x7e\x29\xcf\xa9\xa3"
769                 "\xf3\x42\x8b\x62\xda\x2d\x8c\xda\xc9\x32\xc8\x78\xe2\x7e\xd2\x70"
770         },{     2048, true,
771                 "\x55\x5b\x8e\x74\x90\x9d\x0d\x4b\x74\x8c\x16\x7e\x29\xcf\xa9\xa3"
772                 "\xf3\x42\x8b\x62\xda\x2d\x8c\xda\xc9\x32\xc8\x78\xe2\x7e\xd2\x70"
773         },{     4096, false,
774                 "\xc6\x45\xba\xe0\x40\x3a\x96\x09\x5e\x46\x0d\x19\x9d\x58\x4b\x93"
775                 "\x78\xc5\x3f\xa4\x2e\x9e\xb0\x19\x04\x4b\x73\x26\xf4\xa6\xb5\xc3"
776         },{     4096, true,
777                 "\xc6\x45\xba\xe0\x40\x3a\x96\x09\x5e\x46\x0d\x19\x9d\x58\x4b\x93"
778                 "\x78\xc5\x3f\xa4\x2e\x9e\xb0\x19\x04\x4b\x73\x26\xf4\xa6\xb5\xc3"
779         },
780 }},
781 {
782         "aes", "cbc",
783         "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c", 16,
784         "plain", UINT32_MAX-7, 8192,
785         "\x9f\x1d\xcb\xc3\x5c\x35\x0d\x60\x27\xf9\x8b\xe0\xf5\xc8\xb4\x3b"
786         "\x42\xca\x52\xb7\x60\x44\x59\xc0\xc4\x2b\xe3\xaa\x88\x91\x3d\x47", {
787         {       512, false,
788                 "\x43\xfd\x6e\x25\x80\xb2\x13\xf5\xca\x71\x79\x18\xe4\x12\x91\xe0"
789                 "\x6e\x37\x24\x32\xfd\x40\x4b\x42\xcb\xc1\x72\x1a\xc7\x5a\x19\xc8"
790         },{     1024, false,
791                 "\x18\x79\x8d\xad\xf2\x7b\x38\x03\x27\xa5\x76\x19\x07\xcd\x12\x62"
792                 "\x03\x36\x57\x85\x88\x50\xd0\x6c\xf6\xdf\xf1\xcf\xb8\xcf\x01\x77"
793         },{     1024, true,
794                 "\xd0\x21\xcf\xb2\x7a\x01\xa8\x94\xb2\x87\x49\xc4\x9f\x9c\xb2\x3a"
795                 "\x7c\xc4\x0d\x50\x08\xea\x4d\xfb\x87\xe4\x49\x8c\x1a\xd6\xec\x16"
796         },{     2048, false,
797                 "\xa4\x89\x72\xb9\xcf\x78\x0c\x2a\xc8\x20\x4f\xd5\x13\xcb\x75\x30"
798                 "\x90\xd2\x4a\xfd\xd3\xb2\xe8\xf0\xd2\xb7\x9d\x07\xbd\xa9\x70\x97"
799         },{     2048, true,
800                 "\x2a\xcf\x07\x57\xc8\xea\x64\xc7\xd0\xd5\x28\xe6\xd1\x9a\xb5\x7d"
801                 "\xe4\xb9\x63\xa2\x66\x5a\x3d\x14\xbd\x27\xc7\x09\xc0\x3c\xd9\x00"
802         },{     4096, false,
803                 "\x12\x1b\x00\x54\x6e\x2d\x08\xc1\x15\x8b\x15\x57\xc5\x11\x30\x8b"
804                 "\x63\x33\x64\xa0\xd1\x45\xd6\xcb\xdd\x49\x91\x04\x29\xe6\x93\x08"
805         },{     4096, true,
806                 "\x44\xaa\xf1\x23\x0c\x34\x32\x2a\xfa\xe3\xf7\x95\x7a\x7c\xa8\x8b"
807                 "\x34\x78\xbd\x12\x5c\xae\x4a\x65\x23\x8a\x6f\x3a\x96\x05\xfa\xae"
808         },
809 }},
810 {
811         "aes", "cbc",
812         "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c", 16,
813         "plain64", UINT32_MAX-7, 8192,
814         "\x9f\x1d\xcb\xc3\x5c\x35\x0d\x60\x27\xf9\x8b\xe0\xf5\xc8\xb4\x3b"
815         "\x42\xca\x52\xb7\x60\x44\x59\xc0\xc4\x2b\xe3\xaa\x88\x91\x3d\x47", {
816         {       512, false,
817                 "\xb3\x65\x7e\x6c\xba\xe0\x39\xcd\x1e\x1d\xaf\x65\xae\xb7\xda\x20"
818                 "\x25\x17\x6a\x38\x75\x79\x68\x4c\x9a\x75\xc7\xfb\x2b\xa2\x17\xd2"
819         },{     1024, false,
820                 "\x0a\xa3\x23\x72\x80\xd3\x76\x33\x8b\x2b\xae\x01\x03\x99\xa5\xca"
821                 "\xcd\x95\x27\x40\x27\xec\x14\x90\xfd\x58\xb0\x08\x9b\x99\x27\xe2"
822         },{     1024, true,
823                 "\xd0\x21\xcf\xb2\x7a\x01\xa8\x94\xb2\x87\x49\xc4\x9f\x9c\xb2\x3a"
824                 "\x7c\xc4\x0d\x50\x08\xea\x4d\xfb\x87\xe4\x49\x8c\x1a\xd6\xec\x16"
825         },{     2048, false,
826                 "\x67\x87\xeb\xed\xe1\x16\x85\x0a\x3f\xb2\x5c\xbc\x27\x61\x99\x52"
827                 "\xfe\x64\xb9\xab\x24\xdd\x2c\x1a\x2c\xff\xcd\x7e\x2e\x74\xb5\xd4"
828         },{     2048, true,
829                 "\x2a\xcf\x07\x57\xc8\xea\x64\xc7\xd0\xd5\x28\xe6\xd1\x9a\xb5\x7d"
830                 "\xe4\xb9\x63\xa2\x66\x5a\x3d\x14\xbd\x27\xc7\x09\xc0\x3c\xd9\x00"
831         },{     4096, false,
832                 "\xb2\xf1\x0e\x66\xd4\x58\x4e\x93\xe7\x98\xae\x9c\x3e\xa7\xad\xf2"
833                 "\x93\x1a\xaa\x3c\xc4\x90\x12\x05\x00\x58\x25\x8f\x1f\x5d\xc6\x67"
834         },{     4096, true,
835                 "\x44\xaa\xf1\x23\x0c\x34\x32\x2a\xfa\xe3\xf7\x95\x7a\x7c\xa8\x8b"
836                 "\x34\x78\xbd\x12\x5c\xae\x4a\x65\x23\x8a\x6f\x3a\x96\x05\xfa\xae"
837         },
838 }},
839 {
840         "aes", "cbc",
841         "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c", 16,
842         "plain64be", UINT32_MAX-7, 8192,
843         "\x9f\x1d\xcb\xc3\x5c\x35\x0d\x60\x27\xf9\x8b\xe0\xf5\xc8\xb4\x3b"
844         "\x42\xca\x52\xb7\x60\x44\x59\xc0\xc4\x2b\xe3\xaa\x88\x91\x3d\x47", {
845         {       512, false,
846                 "\x28\xbf\x09\xe1\x68\xcc\x05\x1b\x20\xaf\x8d\x01\x36\x21\x8a\x8d"
847                 "\x7a\x94\x98\xa8\x99\xe9\xf4\x66\xd8\xb7\x99\xca\x04\x58\x83\x90"
848         },{     1024, false,
849                 "\x9b\x74\xf7\xd5\x5a\x6b\xb2\x3a\xd2\x09\xdd\x80\x59\x28\x70\x8f"
850                 "\x3a\x61\xf2\x14\xc3\x0d\xa8\xd7\xd9\xcb\x57\x26\x73\x88\x93\xd2"
851         },{     1024, true,
852                 "\x36\xb5\x68\x08\x29\x55\xb9\xe9\x01\xc1\xa8\xcf\x3e\x5b\x00\x28"
853                 "\xb6\xd1\x35\xc5\xf7\x0c\xf6\x59\xb5\x8f\xb9\xa2\x00\x43\x29\x48"
854         },{     2048, false,
855                 "\x94\x4f\xc8\xb4\xfe\xad\xdc\x56\xf0\x62\x00\x8d\x52\x0b\x2d\x58"
856                 "\xc0\x05\xd6\x1d\x47\x35\xc6\x6a\x42\xec\x98\xee\x21\x74\x7b\xe5"
857         },{     2048, true,
858                 "\x14\x6b\xaa\x2f\xf4\xa8\x24\x3f\x4e\x92\x97\x1a\xca\x1c\xbb\x46"
859                 "\xa7\x08\xbb\xc5\x95\xac\x73\x81\x25\x34\x33\x41\x95\x71\xd9\xe7"
860         },{     4096, false,
861                 "\xa8\x17\x5d\x84\xc8\x16\x06\x7f\xa2\x68\xdd\x1e\x7d\x63\x34\x93"
862                 "\x7b\x45\x2d\xf4\x10\x0b\x90\xfa\x14\x8b\x73\x86\xbc\x09\x4a\xe3"
863         },{     4096, true,
864                 "\xe2\xc3\x30\xd8\xa1\xb3\xa8\xeb\xde\xdc\xfe\x9b\xe0\x0b\x62\x4e"
865                 "\x38\x2f\xa1\x45\x0e\x8f\x6c\xf0\x4e\x88\x58\x17\x13\xb5\x10\x98"
866         },
867 }},
868 {
869         "aes", "cbc",
870         "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c", 16,
871         "essiv:sha256", 0, 8192,
872         "\x9f\x1d\xcb\xc3\x5c\x35\x0d\x60\x27\xf9\x8b\xe0\xf5\xc8\xb4\x3b"
873         "\x42\xca\x52\xb7\x60\x44\x59\xc0\xc4\x2b\xe3\xaa\x88\x91\x3d\x47", {
874         {       512, false,
875                 "\xa5\x3e\x74\xc4\x1a\x5c\xf3\x6b\x63\x49\xd5\xd9\xbb\x7a\x89\x5a"
876                 "\xd5\x3e\x76\x6f\x4c\x2d\x0b\xd3\x8b\x5e\x0e\x91\xa3\x8c\x2a\xde"
877         },{     1024, false,
878                 "\x41\x6b\xc6\x75\x2e\x99\x76\xa1\x83\xea\xd5\x97\x64\x0e\x24\x8c"
879                 "\x91\x17\x03\x38\xe7\xd8\x66\x64\xaa\xd7\x27\x50\x2a\xd3\x0b\xe6"
880         },{     1024, true,
881                 "\x02\x3c\xbe\xe6\x1e\x9a\xf3\x14\xab\x16\xff\x6f\xb6\xa2\x3e\x03"
882                 "\xa1\xbd\xe9\xe4\xfa\x44\x5b\x22\xc6\x53\xe8\x60\x58\x15\x99\xea"
883         },{     2048, false,
884                 "\x84\xdc\x45\xd3\x61\x03\xa8\x51\x85\x5b\xef\xf8\x92\x6b\x12\x06"
885                 "\x2c\xfe\x75\x3e\xcf\x28\xd1\x8b\x4d\xcb\x88\x9e\x31\xb0\x0b\x92"
886         },{     2048, true,
887                 "\x4b\x9d\xe4\x3c\xe2\x4e\x7a\x13\x72\x02\x48\xf8\x7a\x7e\x15\xe8"
888                 "\x3a\xc3\x92\x0b\xe8\x30\xac\xb7\x9a\xe0\xcf\xf9\xb1\xf5\x61\x5b"
889         },{     4096, false,
890                 "\xbb\x1b\xa3\xa9\x41\xbf\x17\xd8\x76\x19\x08\x8e\x3f\x50\xed\xfd"
891                 "\x57\x1d\xd2\xc2\x8a\x32\x01\xb9\xd9\x8a\xcc\x0d\xa0\x65\x8b\x6d"
892         },{     4096, true,
893                 "\xa6\xdc\x7d\xc8\xc4\x9b\x78\x81\x72\xe9\xdd\x35\x6c\x07\xeb\x7b"
894                 "\xd6\x56\x9e\xe4\xdf\xf5\xdd\x2e\x2c\x19\x8f\x63\x58\xdb\xa7\xd0"
895         },
896 }},
897 {
898         "aes", "cbc",
899         "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c", 16,
900         "benbi", 0, 8192,
901         "\x9f\x1d\xcb\xc3\x5c\x35\x0d\x60\x27\xf9\x8b\xe0\xf5\xc8\xb4\x3b"
902         "\x42\xca\x52\xb7\x60\x44\x59\xc0\xc4\x2b\xe3\xaa\x88\x91\x3d\x47", {
903         {       512, false,
904                 "\x3c\xe3\x94\xe3\x6d\x68\x5b\xdb\x5a\x8d\x71\xbf\xd3\xa6\x68\xb9"
905                 "\x1f\x33\x0f\x97\xe2\xd6\xe8\xe2\xe1\xfc\x7e\x80\x28\xf1\x73\xbd"
906         },{     1024, false,
907                 "\x0f\x27\xa7\xae\x31\x9e\x71\x02\x12\x16\x44\x5f\xbb\xc6\xcb\x78"
908                 "\xd4\x84\x49\xe0\x88\x85\x04\xbf\x6d\xea\x60\x76\x98\x34\x0a\x7e"
909         },{     1024, true,
910                 "\x3e\xf3\x08\x8d\x3b\x20\x4b\x51\x54\xde\x7f\x77\x5b\xcf\x02\x8b"
911                 "\x0e\xb0\x74\x2e\x8e\x29\xfa\x5e\x86\xb4\xab\x65\x18\x59\x48\xb1"
912         },{     2048, false,
913                 "\xb0\x9a\xe5\x31\x5f\x2e\x9d\x13\x04\x08\x2a\x02\x71\x3d\xdb\x5d"
914                 "\xb2\xc9\x68\x5b\xdc\xd1\x38\xc2\x96\xb3\x3b\x72\xda\x9d\xcb\xe6"
915         },{     2048, true,
916                 "\x6f\x34\xf0\xc1\xea\x72\xe4\xdc\x91\x91\x78\xb3\x7c\xb0\x9d\x41"
917                 "\x94\xf6\xb8\xad\x05\xc4\x0e\x49\x05\x31\x90\xf0\x56\xfe\x21\x3f"
918         },{     4096, false,
919                 "\xaa\x74\x7d\xd6\x73\xa7\x77\xe1\x7f\xb9\x76\xf7\x5c\xcf\xc0\xb7"
920                 "\xfa\x7b\xed\x15\xc2\x32\x7c\x27\xbb\x35\xfc\xfe\x12\xee\x14\x2d"
921         },{     4096, true,
922                 "\x71\x1b\x3d\x26\xf4\x44\x82\x72\x1b\x7a\x65\x0b\x37\x8c\x94\x5b"
923                 "\x1c\xd3\x30\x2f\xf6\xce\xa4\x24\x25\xeb\x9b\xb9\x83\xe5\x71\xbb"
924         },
925 }},
926 {
927         "aes", "cbc",
928         "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c", 16,
929         "eboiv", 0, 8192,
930         "\x9f\x1d\xcb\xc3\x5c\x35\x0d\x60\x27\xf9\x8b\xe0\xf5\xc8\xb4\x3b"
931         "\x42\xca\x52\xb7\x60\x44\x59\xc0\xc4\x2b\xe3\xaa\x88\x91\x3d\x47", {
932         {       512, false,
933                 "\x04\x4e\x92\x9f\x79\x66\xfe\x93\x1b\xa5\xb8\x02\xfe\x7e\xf9\x26"
934                 "\x7b\x64\x39\xe7\xb3\xca\xc4\x6e\xca\x27\xa0\x2f\xe2\xea\x91\x16"
935         },{     1024, false,
936                 "\xb0\x4a\xa4\xb5\xd6\x45\x7a\x86\xe9\x43\x3d\xd6\x01\xf7\x68\x8e"
937                 "\xe6\x81\x8d\x50\x55\x18\x8e\x4b\xb6\xa7\x89\xdf\xe2\x4b\x94\xe2"
938         },{     1024, true,
939                 "\x95\x08\x4d\x4e\x89\xab\x91\x4e\xae\x56\x5d\xec\xf2\x78\x13\xb1"
940                 "\x82\xf7\xc8\xb5\x03\xd6\xfa\xb0\xe3\xf9\xc1\x01\xc0\x0c\x35\xa4"
941         },{     2048, false,
942                 "\xd4\x00\x1f\x26\x18\xd1\x6d\xd5\xc4\xbf\x4a\x13\x30\xae\xd7\x4b"
943                 "\x33\x1e\xd5\xe8\x43\x2d\x95\x84\x67\x39\x04\x51\x5f\x1f\x49\xe4"
944         },{     2048, true,
945                 "\x89\x8d\xa2\xec\x45\x7f\xf0\xac\xfc\x70\xb6\x36\xf0\x89\xca\x86"
946                 "\x6b\xbf\x09\xd2\x54\xa0\x7c\xbc\x17\xd3\x4e\xb8\x10\x8a\x3f\x5d"
947         },{     4096, false,
948                 "\xd1\xd7\x4f\x70\x9a\xa0\x22\x27\x60\xdb\x40\x5a\x84\xce\x89\x2c"
949                 "\x4f\x98\x55\xd2\x2d\xd1\xea\x9e\x47\xae\x8a\x83\xb5\x90\xbb\x49"
950         },{     4096, true,
951                 "\xdb\xe7\xd2\x25\xb0\x4f\x5d\x36\x20\xc4\xc2\xb4\xe8\x7e\xae\xe9"
952                 "\x95\x10\x45\x5d\xdd\xc4\xcd\x33\xad\xbd\x39\x49\xf2\x85\x82\x4c"
953         },
954 }}};
955
956 /* Base64 test vectors */
957 struct base64_test_vector {
958         size_t decoded_len;
959         const char *decoded;
960         const char *encoded;
961 };
962
963 static struct base64_test_vector base64_test_vectors[] = {
964         {  0, "", "" },
965         {  1, "\x00", "AA==" },
966         {  1, "f", "Zg==" },
967         {  2, "fo", "Zm8=" },
968         {  3, "foo", "Zm9v" },
969         {  4, "foob", "Zm9vYg==" },
970         {  5, "fooba", "Zm9vYmE=" },
971         {  6, "foobar", "Zm9vYmFy" },
972         { 11, "Hello world", "SGVsbG8gd29ybGQ=" },
973         { 22, "\x36\x03\x84\xdc\x4e\x03\x46\xa0\xb5\x2d\x03"
974               "\x6e\xd0\x56\xed\xa0\x37\x02\xac\xc6\x65\xd1",
975               "NgOE3E4DRqC1LQNu0FbtoDcCrMZl0Q==" },
976         {  3, "***", "Kioq" },
977         {  4, "\x01\x02\x03\x04", "AQIDBA==" },
978         {  5, "\xAD\xAD\xAD\xAD\xAD", "ra2tra0=" },
979         {  5, "\xFF\xFF\xFF\xFF\xFF", "//////8=" },
980         { 32, "\x40\xC1\x3F\xBD\x05\x4C\x72\x2A\xA3\xC2\xF2"
981               "\x11\x73\xC0\x69\xEA\x49\x7D\x35\x29\x6B\xCC"
982               "\x24\x65\xF6\xF9\xD0\x41\x08\x7B\xD7\xA9",
983               "QME/vQVMciqjwvIRc8Bp6kl9NSlrzCRl9vnQQQh716k=" },
984         {  7, "\x54\x0f\xdc\xf0\x0f\xaf\x4a", "VA/c8A+vSg==" },
985         {179, "blah blah blah blah blah blah blah blah blah "
986               "blah blah blah blah blah blah blah blah blah "
987               "blah blah blah blah blah blah blah blah blah "
988               "blah blah blah blah blah blah blah blah blah",
989               "YmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxh"
990               "aCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBi"
991               "bGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFo"
992               "IGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJs"
993               "YWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWgg"
994               "YmxhaCBibGFoIGJsYWg=" },
995 };
996
997 /* UTF8 to UTF16LE test vectors */
998 struct utf8_16_test_vector {
999         size_t len8;
1000         size_t len16;
1001         const char *utf8;
1002         const char *utf16;
1003 };
1004
1005 static struct utf8_16_test_vector utf8_16_test_vectors[] = {
1006         {  1,  2, "a", "\x61\x00" },
1007         { 16, 32, "0123456789abcdef",
1008         "\x30\x00\x31\x00\x32\x00\x33\x00\x34\x00\x35\x00\x36\x00\x37\x00"
1009         "\x38\x00\x39\x00\x61\x00\x62\x00\x63\x00\x64\x00\x65\x00\x66\x00" },
1010         { 77, 78,
1011         "\xf2\xa4\xa5\x94\x49\xf2\xa1\x98\x98\xd8\x8a\xe1\xb4\x88\xea\xa7"
1012         "\xaa\xde\x95\xe2\x85\xb1\xe7\xb1\x9a\xf2\xb5\xa1\xae\x37\x2d\xd0"
1013         "\xa9\xe1\x9a\x9c\xe8\xb0\xb7\xc8\x95\x0a\xf3\xaa\x92\xba\xf2\x83"
1014         "\xb0\x99\xf0\x9b\xbe\x8f\x4f\xc8\x86\x30\xe7\xab\xa0\xda\xb9\xd8"
1015         "\x89\xd8\xbc\xd7\x8a\xd9\xbc\xc3\x8f\x33\x62\xda\xb7",
1016         "\x52\xda\x54\xdd\x49\x00\x45\xda\x18\xde\x0a\x06\x08\x1d\xea\xa9"
1017         "\x95\x07\x71\x21\x5a\x7c\x96\xda\x6e\xdc\x37\x00\x2d\x00\x29\x04"
1018         "\x9c\x16\x37\x8c\x15\x02\x0a\x00\x69\xdb\xba\xdc\xcf\xd9\x19\xdc"
1019         "\x2f\xd8\x8f\xdf\x4f\x00\x06\x02\x30\x00\xe0\x7a\xb9\x06\x09\x06"
1020         "\x3c\x06\xca\x05\x7c\x06\xcf\x00\x33\x00\x62\x00\xb7\x06" },
1021 };
1022
1023 static int pbkdf_test_vectors(void)
1024 {
1025         char result[256];
1026         unsigned int i;
1027         const struct kdf_test_vector *vec;
1028
1029         for (i = 0; i < ARRAY_SIZE(kdf_test_vectors); i++) {
1030                 crypt_backend_memzero(result, sizeof(result));
1031                 vec = &kdf_test_vectors[i];
1032                 printf("PBKDF vector %02d %s ", i, vec->type);
1033                 if (vec->hash && crypt_hmac_size(vec->hash) < 0) {
1034                         printf("[%s N/A]\n", vec->hash);
1035                         continue;
1036                 }
1037                 if (crypt_pbkdf(vec->type, vec->hash,
1038                     vec->password, vec->password_length,
1039                     vec->salt, vec->salt_length,
1040                     result, vec->output_length,
1041                     vec->iterations, vec->memory, vec->parallelism) < 0) {
1042                         printf("[%s-%s N/A]\n", vec->type, vec->hash);
1043                         continue;
1044                 }
1045                 if (memcmp(result, vec->output, vec->output_length)) {
1046                         printf("[FAILED]\n");
1047                         printhex(" got", result, vec->output_length);
1048                         printhex("want", vec->output, vec->output_length);
1049                         return EXIT_FAILURE;
1050                 }
1051                 printf("[OK]\n");
1052         }
1053         return EXIT_SUCCESS;
1054 }
1055
1056 static int crc32_test(const struct hash_test_vector *vector, unsigned int i)
1057 {
1058         uint32_t crc32;
1059
1060         if (vector->out[i].length != sizeof(uint32_t))
1061                 return EXIT_FAILURE;
1062
1063         crc32 = crypt_crc32(~0, (const unsigned char*)vector->data, vector->data_length) ^ ~0;
1064
1065         if ((unsigned char)vector->out[i].out[0] != ((crc32 >> 24) & 0xFF) ||
1066             (unsigned char)vector->out[i].out[1] != ((crc32 >> 16) & 0xFF) ||
1067             (unsigned char)vector->out[i].out[2] != ((crc32 >>  8) & 0xFF) ||
1068             (unsigned char)vector->out[i].out[3] != ((crc32 >>  0) & 0xFF)) {
1069                 printf("[FAILED]\n");
1070                 printhex(" got", (const char *)&crc32, sizeof(crc32));
1071                 printhex("want", vector->out[i].out, vector->out[i].length);
1072                 return EXIT_FAILURE;
1073         }
1074
1075         return EXIT_SUCCESS;
1076 }
1077
1078 static int hash_test(void)
1079 {
1080         const struct hash_test_vector *vector;
1081         unsigned int i, j;
1082         int r;
1083         struct crypt_hash *h;
1084         char result[64];
1085
1086         for (i = 0; i < ARRAY_SIZE(hash_test_vectors); i++) {
1087                 vector = &hash_test_vectors[i];
1088                 printf("Hash vector %02d: ", i);
1089
1090                 for (j = 0; j < ARRAY_SIZE(vector->out); j++) {
1091
1092                         // CRC32 vector test is special
1093                         if (!strcmp("crc32", vector->out[j].name)) {
1094                                 if (crc32_test(vector, j) < 0)
1095                                         return EXIT_FAILURE;
1096                                 printf("[%s]", vector->out[j].name);
1097                                 continue;
1098                         }
1099
1100                         if (crypt_hash_size(vector->out[j].name) < 0) {
1101                                 printf("[%s N/A]", vector->out[j].name);
1102                                 continue;
1103                         }
1104
1105                         if (crypt_hash_size(vector->out[j].name) != (int)vector->out[j].length)
1106                                 return EXIT_FAILURE;
1107
1108                         if (sizeof(result) < vector->out[j].length)
1109                                 return EXIT_FAILURE;
1110
1111                         crypt_backend_memzero(result, sizeof(result));
1112                         printf("[%s]", vector->out[j].name);
1113
1114                         if (crypt_hash_init(&h, vector->out[j].name)) {
1115                                 printf("[%s N/A (init)]", vector->out[j].name);
1116                                 continue;
1117                         }
1118
1119                         r = crypt_hash_write(h, vector->data, vector->data_length);
1120                         if (!r)
1121                                 r = crypt_hash_final(h, result, vector->out[j].length);
1122
1123
1124                         if (r) {
1125                                 crypt_hash_destroy(h);
1126                                 return EXIT_FAILURE;
1127                         }
1128
1129                         if (memcmp(result, vector->out[j].out, vector->out[j].length)) {
1130                                 printf("[FAILED]\n");
1131                                 printhex(" got", result, vector->out[j].length);
1132                                 printhex("want", vector->out[j].out, vector->out[j].length);
1133                                 crypt_hash_destroy(h);
1134                                 return EXIT_FAILURE;
1135                         }
1136
1137                         /*
1138                          * After crypt_hash_final() the context must be reset, repeat
1139                          */
1140                         crypt_backend_memzero(result, sizeof(result));
1141                         r = crypt_hash_write(h, vector->data, vector->data_length);
1142                         if (!r)
1143                                 r = crypt_hash_final(h, result, vector->out[j].length);
1144
1145                         if (r || memcmp(result, vector->out[j].out, vector->out[j].length)) {
1146                                 printf("[FAILED (RESET CONTEXT)]\n");
1147                                 printhex(" got", result, vector->out[j].length);
1148                                 printhex("want", vector->out[j].out, vector->out[j].length);
1149                                 crypt_hash_destroy(h);
1150                                 return EXIT_FAILURE;
1151                         }
1152
1153                         crypt_hash_destroy(h);
1154                 }
1155                 printf("\n");
1156         }
1157
1158         return EXIT_SUCCESS;
1159 }
1160
1161 static int hmac_test(void)
1162 {
1163         const struct hmac_test_vector *vector;
1164         struct crypt_hmac *hmac;
1165         unsigned int i, j;
1166         int r;
1167         char result[64];
1168
1169         for (i = 0; i < ARRAY_SIZE(hmac_test_vectors); i++) {
1170                 vector = &hmac_test_vectors[i];
1171                 printf("HMAC vector %02d: ", i);
1172
1173                 for(j = 0; j < ARRAY_SIZE(vector->out); j++) {
1174
1175                         if (crypt_hmac_size(vector->out[j].name) < 0) {
1176                                 printf("[%s N/A]", vector->out[j].name);
1177                                 continue;
1178                         }
1179
1180                         if (crypt_hmac_size(vector->out[j].name) != (int)vector->out[j].length)
1181                                 return EXIT_FAILURE;
1182
1183                         if (sizeof(result) < vector->out[j].length)
1184                                 return EXIT_FAILURE;
1185
1186                         crypt_backend_memzero(result, sizeof(result));
1187                         printf("[%s]", vector->out[j].name);
1188
1189                         if (crypt_hmac_init(&hmac, vector->out[j].name, vector->key, vector->key_length))
1190                                 return EXIT_FAILURE;
1191
1192                         r = crypt_hmac_write(hmac, vector->data, vector->data_length);
1193                         if (!r)
1194                                 r = crypt_hmac_final(hmac, result, vector->out[j].length);
1195
1196                         if (r) {
1197                                 crypt_hmac_destroy(hmac);
1198                                 return EXIT_FAILURE;
1199                         }
1200
1201                         if (memcmp(result, vector->out[j].out, vector->out[j].length)) {
1202                                 printf("[FAILED]\n");
1203                                 printhex(" got", result, vector->out[j].length);
1204                                 printhex("want", vector->out[j].out, vector->out[j].length);
1205                                 crypt_hmac_destroy(hmac);
1206                                 return EXIT_FAILURE;
1207                         }
1208
1209                         /*
1210                          * After crypt_hmac_final() the context must be reset, repeat
1211                          */
1212                         crypt_backend_memzero(result, sizeof(result));
1213                         r = crypt_hmac_write(hmac, vector->data, vector->data_length);
1214                         if (!r)
1215                                 r = crypt_hmac_final(hmac, result, vector->out[j].length);
1216
1217                         if (r || memcmp(result, vector->out[j].out, vector->out[j].length)) {
1218                                 printf("[FAILED (RESET CONTEXT)]\n");
1219                                 printhex(" got", result, vector->out[j].length);
1220                                 printhex("want", vector->out[j].out, vector->out[j].length);
1221                                 crypt_hmac_destroy(hmac);
1222                                 return EXIT_FAILURE;
1223                         }
1224
1225                         crypt_hmac_destroy(hmac);
1226                 }
1227                 printf("\n");
1228         }
1229
1230         return EXIT_SUCCESS;
1231 }
1232
1233 static int cipher_test(void)
1234 {
1235         const struct cipher_test_vector *vector;
1236         struct crypt_cipher *cipher;
1237         unsigned int i, j;
1238         char result[256];
1239         int r;
1240
1241         for (i = 0; i < ARRAY_SIZE(cipher_test_vectors); i++) {
1242                 vector = &cipher_test_vectors[i];
1243                 printf("CIPHER vector %02d: ", i);
1244
1245                 for (j = 0; j < ARRAY_SIZE(vector->out); j++) {
1246                         if (vector->iv_length &&
1247                             crypt_cipher_ivsize(vector->out[j].name, vector->out[j].mode) != (int)vector->iv_length)
1248                                 return EXIT_FAILURE;
1249                         if (vector->data_length > sizeof(result))
1250                                 return EXIT_FAILURE;
1251
1252                         r = crypt_cipher_init(&cipher, vector->out[j].name, vector->out[j].mode,
1253                                               vector->key, vector->key_length);
1254                         if (r == -ENOENT || r == -ENOTSUP) {
1255                                 printf("[%s-%s N/A]", vector->out[j].name, vector->out[j].mode);
1256                                 continue;
1257                         } else {
1258                                 printf("[%s-%s,%dbits]", vector->out[j].name, vector->out[j].mode, vector->key_length * 8);
1259                                 if (r)
1260                                         return EXIT_FAILURE;
1261                         }
1262
1263                         crypt_backend_memzero(result, sizeof(result));
1264                         if (crypt_cipher_encrypt(cipher, vector->plaintext, result, vector->data_length,
1265                                                  vector->iv, vector->iv_length)) {
1266                                 crypt_cipher_destroy(cipher);
1267                                 return EXIT_FAILURE;
1268                         }
1269
1270                         if (memcmp(vector->out[j].ciphertext, result, vector->data_length)) {
1271                                 printf("[ENCRYPTION FAILED]\n");
1272                                 printhex(" got", result, vector->data_length);
1273                                 printhex("want", vector->out[j].ciphertext, vector->data_length);
1274                                 crypt_cipher_destroy(cipher);
1275                                 return EXIT_FAILURE;
1276                         }
1277
1278                         crypt_backend_memzero(result, sizeof(result));
1279                         if (crypt_cipher_decrypt(cipher, vector->out[j].ciphertext, result, vector->data_length,
1280                                                  vector->iv, vector->iv_length)) {
1281                                 crypt_cipher_destroy(cipher);
1282                                 return EXIT_FAILURE;
1283                         }
1284
1285                         if (memcmp(vector->plaintext, result, vector->data_length)) {
1286                                 printf("[DECRYPTION FAILED]\n");
1287                                 printhex(" got", result, vector->data_length);
1288                                 printhex("want", vector->plaintext, vector->data_length);
1289                                 crypt_cipher_destroy(cipher);
1290                                 return EXIT_FAILURE;
1291                         }
1292
1293                         crypt_cipher_destroy(cipher);
1294                 }
1295                 printf("\n");
1296         }
1297
1298         return EXIT_SUCCESS;
1299 }
1300
1301 static void get_sha256(const char *in, size_t length, char out[32])
1302 {
1303         struct crypt_hash *h;
1304
1305         crypt_backend_memzero(out, 32);
1306         if (crypt_hash_init(&h, "sha256"))
1307                 return;
1308
1309         if (!crypt_hash_write(h, in, length))
1310                 crypt_hash_final(h, out, 32);
1311
1312         crypt_hash_destroy(h);
1313 }
1314
1315 static int cipher_iv_test(void)
1316 {
1317         const struct cipher_iv_test_vector *vector;
1318         struct crypt_storage *storage;
1319         unsigned int i, j;
1320         char mode_iv[256];
1321         char result[8192], hash[32];
1322         int r;
1323
1324         for (i = 0; i < ARRAY_SIZE(cipher_iv_test_vectors); i++) {
1325                 vector = &cipher_iv_test_vectors[i];
1326                 printf("IV vector %02d: [%s-%s-%s]", i, vector->cipher_name, vector->cipher_mode, vector->iv_name);
1327
1328                 for (j = 0; j < ARRAY_SIZE(vector->out); j++) {
1329                         if (vector->data_length > sizeof(result))
1330                                 return EXIT_FAILURE;
1331
1332                         if (snprintf(mode_iv, sizeof(mode_iv)-2, "%s-%s", vector->cipher_mode, vector->iv_name) < 0)
1333                                 return EXIT_FAILURE;
1334
1335                         r = crypt_storage_init(&storage, vector->out[j].sector_size, vector->cipher_name, mode_iv,
1336                                                vector->key, vector->key_length, vector->out[j].large_iv);
1337                         if (r == -ENOENT || r == -ENOTSUP) {
1338                                 printf("[N/A]");
1339                                 continue;
1340                         } else {
1341                                 printf("[%i%s]", (int)vector->out[j].sector_size, vector->out[j].large_iv ? "L" : "");
1342                                 if (r)
1343                                         return EXIT_FAILURE;
1344                         }
1345
1346                         crypt_backend_memzero(result, sizeof(result));
1347                         if (crypt_storage_encrypt(storage, vector->iv_offset, vector->data_length, result)) {
1348                                 crypt_storage_destroy(storage);
1349                                 return EXIT_FAILURE;
1350                         }
1351
1352                         get_sha256(result, vector->data_length, hash);
1353                         if (memcmp(vector->out[j].out_sha256, hash, sizeof(hash))) {
1354                                 printf("[ENCRYPTION FAILED]\n");
1355                                 printhex(" got", hash, sizeof(hash));
1356                                 printhex("want", vector->out[j].out_sha256, sizeof(vector->out[j].out_sha256));
1357                                 crypt_storage_destroy(storage);
1358                                 return EXIT_FAILURE;
1359                         }
1360
1361                         if (crypt_storage_decrypt(storage, vector->iv_offset, vector->data_length, result)) {
1362                                 crypt_storage_destroy(storage);
1363                                 return EXIT_FAILURE;
1364                         }
1365
1366                         get_sha256(result, vector->data_length, hash);
1367                         if (memcmp(vector->in_sha256, hash, sizeof(hash))) {
1368                                 printf("[DECRYPTION FAILED]\n");
1369                                 printhex(" got", hash, sizeof(hash));
1370                                 printhex("want", vector->in_sha256, sizeof(vector->in_sha256));
1371                                 crypt_storage_destroy(storage);
1372                                 return EXIT_FAILURE;
1373                         }
1374
1375                         crypt_storage_destroy(storage);
1376                 }
1377                 printf("\n");
1378         }
1379
1380         return EXIT_SUCCESS;
1381 }
1382
1383 static int check_hash(const char *hash)
1384 {
1385         struct crypt_hash *h;
1386
1387         if (crypt_hash_size(hash) < 0)
1388                 return EXIT_FAILURE;
1389
1390         if (crypt_hash_init(&h, hash))
1391                 return EXIT_FAILURE;
1392
1393         crypt_hash_destroy(h);
1394         return EXIT_SUCCESS;
1395 }
1396
1397 static int base64_test(void)
1398 {
1399         unsigned int i;
1400         char *s;
1401         size_t s_len;
1402
1403         for (i = 0; i < ARRAY_SIZE(base64_test_vectors); i++) {
1404                 printf("BASE64 %02d ", i);
1405                 s = NULL;
1406                 s_len = 0;
1407                 if (crypt_base64_encode(&s, &s_len,
1408                         base64_test_vectors[i].decoded,
1409                         base64_test_vectors[i].decoded_len) < 0) {
1410                         printf("[ENCODE FAILED]\n");
1411                         return EXIT_FAILURE;
1412                 } else if (strcmp(s, base64_test_vectors[i].encoded)) {
1413                         printf("[ENCODE FAILED]\n");
1414                         free(s);
1415                         return EXIT_FAILURE;
1416                 }
1417                 printf("[encode]");
1418                 free(s);
1419
1420                 s = NULL;
1421                 s_len = 0;
1422                 if (crypt_base64_decode(&s, &s_len,
1423                         base64_test_vectors[i].encoded,
1424                         strlen(base64_test_vectors[i].encoded)) < 0) {
1425                         printf("[DECODE FAILED]\n");
1426                         return EXIT_FAILURE;
1427                 } else if (s_len != base64_test_vectors[i].decoded_len ||
1428                            memcmp(s, base64_test_vectors[i].decoded, s_len)) {
1429                         printf("[DECODE FAILED]\n");
1430                         return EXIT_FAILURE;
1431                 }
1432                 printf("[decode]\n");
1433                 free(s);
1434         }
1435
1436         return EXIT_SUCCESS;
1437 }
1438
1439 static int utf8_16_test(void)
1440 {
1441         unsigned int i;
1442         char s8[128], *s;
1443         char16_t c16[256], s16[256], *su;
1444
1445         for (i = 0; i < ARRAY_SIZE(utf8_16_test_vectors); i++) {
1446                 printf("UTF8/16 %02d ", i);
1447                 crypt_backend_memzero(s16, sizeof(s16));
1448                 su = &s16[0];
1449                 if (crypt_utf8_to_utf16(&su, utf8_16_test_vectors[i].utf8,
1450                                         utf8_16_test_vectors[i].len8) < 0 ||
1451                         memcmp(utf8_16_test_vectors[i].utf16, s16,
1452                                utf8_16_test_vectors[i].len16)) {
1453                         printf("[UTF8_TO_UTF16 FAILED]\n");
1454                         return EXIT_FAILURE;
1455                 }
1456                 printf("[UTF8_TO_UTF16]");
1457
1458                 crypt_backend_memzero(s8, sizeof(s8));
1459                 s = &s8[0];
1460                 memcpy(c16, utf8_16_test_vectors[i].utf16, utf8_16_test_vectors[i].len16);
1461                 if (crypt_utf16_to_utf8(&s, c16, utf8_16_test_vectors[i].len16) < 0 ||
1462                         utf8_16_test_vectors[i].len8 != strlen(s8) ||
1463                         memcmp(utf8_16_test_vectors[i].utf8, s8,
1464                                utf8_16_test_vectors[i].len8)) {
1465                         printf("[UTF16_TO_UTF8 FAILED]\n");
1466                         return EXIT_FAILURE;
1467                 }
1468                 printf("[UTF16_TO_UTF8]\n");
1469         }
1470
1471         return EXIT_SUCCESS;
1472 }
1473
1474 static int default_alg_test(void)
1475 {
1476         printf("Defaults: [LUKS1 hash %s] ", DEFAULT_LUKS1_HASH);
1477         if (check_hash(DEFAULT_LUKS1_HASH))
1478                 return EXIT_FAILURE;
1479
1480         printf("[PLAIN hash %s] ", DEFAULT_PLAIN_HASH);
1481         if (check_hash(DEFAULT_PLAIN_HASH))
1482                 return EXIT_FAILURE;
1483
1484         printf("[VERITY hash %s] ", DEFAULT_VERITY_HASH);
1485         if (check_hash(DEFAULT_VERITY_HASH))
1486                 return EXIT_FAILURE;
1487
1488         printf("[OK]\n");
1489
1490         return EXIT_SUCCESS;
1491 }
1492
1493 static int memcmp_test(void)
1494 {
1495         printf("MEMEQ ");
1496         if (!crypt_backend_memeq("aaaaaaaa", "bbbbbbbb", 8))
1497                 return EXIT_FAILURE;
1498         if (crypt_backend_memeq("aaaaaaaa", "aaaaaaaa", 8))
1499                 return EXIT_FAILURE;
1500         printf("[OK]\n");
1501
1502         return EXIT_SUCCESS;
1503 }
1504
1505 static void __attribute__((noreturn)) exit_test(const char *msg, int r)
1506 {
1507         if (msg)
1508                 printf("%s\n", msg);
1509         crypt_backend_destroy();
1510         exit(r);
1511 }
1512
1513 int main(__attribute__ ((unused)) int argc, __attribute__ ((unused))char *argv[])
1514 {
1515         setvbuf(stdout, NULL, _IONBF, 0);
1516
1517         if (getenv("CRYPTSETUP_PATH")) {
1518                 printf("Cannot run this test with CRYPTSETUP_PATH set.\n");
1519                 exit(77);
1520         }
1521
1522         if (crypt_backend_init(fips_mode()))
1523                 exit_test("Crypto backend init error.", EXIT_FAILURE);
1524
1525         printf("Test vectors using %s crypto backend.\n", crypt_backend_version());
1526
1527         if (pbkdf_test_vectors())
1528                 exit_test("PBKDF test failed.", EXIT_FAILURE);
1529
1530         if (hash_test())
1531                 exit_test("HASH test failed.", EXIT_FAILURE);
1532
1533         if (hmac_test())
1534                 exit_test("HMAC test failed.", EXIT_FAILURE);
1535
1536         if (cipher_test())
1537                 exit_test("CIPHER test failed.", EXIT_FAILURE);
1538
1539         if (cipher_iv_test())
1540                 exit_test("IV test failed.", EXIT_FAILURE);
1541
1542         if (base64_test())
1543                 exit_test("BASE64 test failed.", EXIT_FAILURE);
1544
1545         if (memcmp_test())
1546                 exit_test("Memcmp test failed.", EXIT_FAILURE);
1547
1548         if (utf8_16_test())
1549                 exit_test("UTF8/16 test failed.", EXIT_FAILURE);
1550
1551         if (default_alg_test()) {
1552                 if (fips_mode())
1553                         printf("\nDefault compiled-in algorithms test ignored (FIPS mode on).\n");
1554                 else
1555                         exit_test("\nDefault compiled-in algorithms test failed.", EXIT_FAILURE);
1556         }
1557
1558         exit_test(NULL, EXIT_SUCCESS);
1559 }