a8f37978a51f58258219f0ac7f4add82effb2e99
[platform/upstream/cryptsetup.git] / tests / crypto-vectors.c
1 /*
2  * cryptsetup crypto backend test vectors
3  *
4  * Copyright (C) 2018-2020 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
26 #include "crypto_backend.h"
27
28 #ifndef ARRAY_SIZE
29 # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
30 #endif
31
32 static void printhex(const char *s, const char *buf, size_t len)
33 {
34         size_t i;
35
36         printf("%s: ", s);
37         for (i = 0; i < len; i++)
38                 printf(" %02x", (unsigned char)buf[i]);
39         printf("\n");
40         fflush(stdout);
41 }
42
43 /*
44  * KDF tests
45  */
46 struct kdf_test_vector {
47         const char *type;
48         const char *hash;
49         unsigned int hash_block_length;
50         unsigned int iterations;
51         unsigned int memory;
52         unsigned int parallelism;
53         const char *password;
54         unsigned int password_length;
55         const char *salt;
56         unsigned int salt_length;
57 //      const char *key;
58 //      unsigned int key_length;
59 //      const char *ad;
60 //      unsigned int ad_length;
61         const char *output;
62         unsigned int output_length;
63 };
64
65 static struct kdf_test_vector kdf_test_vectors[] = {
66         /* Argon2 RFC (without key and ad values) */
67         {
68                 "argon2i", NULL, 0, 3, 32, 4,
69                 "\x01\x01\x01\x01\x01\x01\x01\x01"
70                 "\x01\x01\x01\x01\x01\x01\x01\x01"
71                 "\x01\x01\x01\x01\x01\x01\x01\x01"
72                 "\x01\x01\x01\x01\x01\x01\x01\x01", 32,
73                 "\x02\x02\x02\x02\x02\x02\x02\x02"
74                 "\x02\x02\x02\x02\x02\x02\x02\x02", 16,
75 //              "\x03\x03\x03\x03\x03\x03\x03\x03", 8,
76 //              "\x04\x04\x04\x04\x04\x04\x04\x04"
77 //              "\x04\x04\x04\x04", 12,
78                 "\xa9\xa7\x51\x0e\x6d\xb4\xd5\x88"
79                 "\xba\x34\x14\xcd\x0e\x09\x4d\x48"
80                 "\x0d\x68\x3f\x97\xb9\xcc\xb6\x12"
81                 "\xa5\x44\xfe\x8e\xf6\x5b\xa8\xe0", 32
82 //              "\xc8\x14\xd9\xd1\xdc\x7f\x37\xaa"
83 //              "\x13\xf0\xd7\x7f\x24\x94\xbd\xa1"
84 //              "\xc8\xde\x6b\x01\x6d\xd3\x88\xd2"
85 //              "\x99\x52\xa4\xc4\x67\x2b\x6c\xe8", 32
86         },
87         {
88                 "argon2id", 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                 "\x03\xaa\xb9\x65\xc1\x20\x01\xc9"
99                 "\xd7\xd0\xd2\xde\x33\x19\x2c\x04"
100                 "\x94\xb6\x84\xbb\x14\x81\x96\xd7"
101                 "\x3c\x1d\xf1\xac\xaf\x6d\x0c\x2e", 32
102 //              "\x0d\x64\x0d\xf5\x8d\x78\x76\x6c"
103 //              "\x08\xc0\x37\xa3\x4a\x8b\x53\xc9"
104 //              "\xd0\x1e\xf0\x45\x2d\x75\xb6\x5e"
105 //              "\xb5\x25\x20\xe9\x6b\x01\xe6\x59", 32
106         },
107         /* RFC 3962 */
108         {
109                 "pbkdf2", "sha1", 64, 1, 0, 0,
110                 "password", 8,
111                 "ATHENA.MIT.EDUraeburn", 21,
112                 "\xcd\xed\xb5\x28\x1b\xb2\xf8\x01"
113                 "\x56\x5a\x11\x22\xb2\x56\x35\x15"
114                 "\x0a\xd1\xf7\xa0\x4b\xb9\xf3\xa3"
115                 "\x33\xec\xc0\xe2\xe1\xf7\x08\x37", 32
116         }, {
117                 "pbkdf2", "sha1", 64, 2, 0, 0,
118                 "password", 8,
119                 "ATHENA.MIT.EDUraeburn", 21,
120                 "\x01\xdb\xee\x7f\x4a\x9e\x24\x3e"
121                 "\x98\x8b\x62\xc7\x3c\xda\x93\x5d"
122                 "\xa0\x53\x78\xb9\x32\x44\xec\x8f"
123                 "\x48\xa9\x9e\x61\xad\x79\x9d\x86", 32
124         }, {
125                 "pbkdf2", "sha1", 64, 1200, 0, 0,
126                 "password", 8,
127                 "ATHENA.MIT.EDUraeburn", 21,
128                 "\x5c\x08\xeb\x61\xfd\xf7\x1e\x4e"
129                 "\x4e\xc3\xcf\x6b\xa1\xf5\x51\x2b"
130                 "\xa7\xe5\x2d\xdb\xc5\xe5\x14\x2f"
131                 "\x70\x8a\x31\xe2\xe6\x2b\x1e\x13", 32
132         }, {
133                 "pbkdf2", "sha1", 64, 5, 0, 0,
134                 "password", 8,
135                 "\0224VxxV4\022", 8, // "\x1234567878563412
136                 "\xd1\xda\xa7\x86\x15\xf2\x87\xe6"
137                 "\xa1\xc8\xb1\x20\xd7\x06\x2a\x49"
138                 "\x3f\x98\xd2\x03\xe6\xbe\x49\xa6"
139                 "\xad\xf4\xfa\x57\x4b\x6e\x64\xee", 32
140         }, {
141                 "pbkdf2", "sha1", 64, 1200, 0, 0,
142                 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
143                 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 64,
144                 "pass phrase equals block size", 29,
145                 "\x13\x9c\x30\xc0\x96\x6b\xc3\x2b"
146                 "\xa5\x5f\xdb\xf2\x12\x53\x0a\xc9"
147                 "\xc5\xec\x59\xf1\xa4\x52\xf5\xcc"
148                 "\x9a\xd9\x40\xfe\xa0\x59\x8e\xd1", 32
149         }, {
150                 "pbkdf2", "sha1", 64, 1200, 0, 0,
151                 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
152                 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 65,
153                 "pass phrase exceeds block size", 30,
154                 "\x9c\xca\xd6\xd4\x68\x77\x0c\xd5"
155                 "\x1b\x10\xe6\xa6\x87\x21\xbe\x61"
156                 "\x1a\x8b\x4d\x28\x26\x01\xdb\x3b"
157                 "\x36\xbe\x92\x46\x91\x5e\xc8\x2a", 32
158         }, {
159                 "pbkdf2", "sha1", 64, 50, 0, 0,
160                 "\360\235\204\236", 4, // g-clef ("\xf09d849e)
161                 "EXAMPLE.COMpianist", 18,
162                 "\x6b\x9c\xf2\x6d\x45\x45\x5a\x43"
163                 "\xa5\xb8\xbb\x27\x6a\x40\x3b\x39"
164                 "\xe7\xfe\x37\xa0\xc4\x1e\x02\xc2"
165                 "\x81\xff\x30\x69\xe1\xe9\x4f\x52", 32
166         }, {
167         /* RFC-6070 */
168                 "pbkdf2", "sha1", 64, 1, 0, 0,
169                 "password", 8,
170                 "salt", 4,
171                 "\x0c\x60\xc8\x0f\x96\x1f\x0e\x71\xf3\xa9"
172                 "\xb5\x24\xaf\x60\x12\x06\x2f\xe0\x37\xa6", 20
173         }, {
174                 "pbkdf2", "sha1", 64, 2, 0, 0,
175                 "password", 8,
176                 "salt", 4,
177                 "\xea\x6c\x01\x4d\xc7\x2d\x6f\x8c\xcd\x1e"
178                 "\xd9\x2a\xce\x1d\x41\xf0\xd8\xde\x89\x57", 20
179         }, {
180                 "pbkdf2", "sha1", 64, 4096, 0, 0,
181                 "password", 8,
182                 "salt", 4,
183                 "\x4b\x00\x79\x01\xb7\x65\x48\x9a\xbe\xad"
184                 "\x49\xd9\x26\xf7\x21\xd0\x65\xa4\x29\xc1", 20
185         }, {
186                 "pbkdf2", "sha1", 64, 16777216, 0, 0,
187                 "password", 8,
188                 "salt", 4,
189                 "\xee\xfe\x3d\x61\xcd\x4d\xa4\xe4\xe9\x94"
190                 "\x5b\x3d\x6b\xa2\x15\x8c\x26\x34\xe9\x84", 20
191         }, {
192                 "pbkdf2", "sha1", 64, 4096, 0, 0,
193                 "passwordPASSWORDpassword", 24,
194                 "saltSALTsaltSALTsaltSALTsaltSALTsalt", 36,
195                 "\x3d\x2e\xec\x4f\xe4\x1c\x84\x9b\x80\xc8"
196                 "\xd8\x36\x62\xc0\xe4\x4a\x8b\x29\x1a\x96"
197                 "\x4c\xf2\xf0\x70\x38", 25
198         }, {
199                 "pbkdf2", "sha1", 64, 4096, 0, 0,
200                 "pass\0word", 9,
201                 "sa\0lt", 5,
202                 "\x56\xfa\x6a\xa7\x55\x48\x09\x9d\xcc\x37"
203                 "\xd7\xf0\x34\x25\xe0\xc3", 16
204         }, {
205         /* empty password test */
206                 "pbkdf2", "sha1", 64, 2, 0, 0,
207                 "", 0,
208                 "salt", 4,
209                 "\x13\x3a\x4c\xe8\x37\xb4\xd2\x52\x1e\xe2"
210                 "\xbf\x03\xe1\x1c\x71\xca\x79\x4e\x07\x97", 20
211         }, {
212         /* Password exceeds block size test */
213                 "pbkdf2", "sha256", 64, 1200, 0, 0,
214                 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
215                 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 65,
216                 "pass phrase exceeds block size", 30,
217                 "\x22\x34\x4b\xc4\xb6\xe3\x26\x75"
218                 "\xa8\x09\x0f\x3e\xa8\x0b\xe0\x1d"
219                 "\x5f\x95\x12\x6a\x2c\xdd\xc3\xfa"
220                 "\xcc\x4a\x5e\x6d\xca\x04\xec\x58", 32
221         }, {
222                 "pbkdf2", "sha512", 128, 1200, 0, 0,
223                 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
224                 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
225                 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
226                 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 129,
227                 "pass phrase exceeds block size", 30,
228                 "\x0f\xb2\xed\x2c\x0e\x6e\xfb\x7d"
229                 "\x7d\x8e\xdd\x58\x01\xb4\x59\x72"
230                 "\x99\x92\x16\x30\x5e\xa4\x36\x8d"
231                 "\x76\x14\x80\xf3\xe3\x7a\x22\xb9", 32
232         }, {
233                 "pbkdf2", "whirlpool", 64, 1200, 0, 0,
234                 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
235                 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 65,
236                 "pass phrase exceeds block size", 30,
237                 "\x9c\x1c\x74\xf5\x88\x26\xe7\x6a"
238                 "\x53\x58\xf4\x0c\x39\xe7\x80\x89"
239                 "\x07\xc0\x31\x19\x9a\x50\xa2\x48"
240                 "\xf1\xd9\xfe\x78\x64\xe5\x84\x50", 32
241         }
242 };
243
244 /*
245  * Hash tests
246  */
247 struct hash_test_vector {
248         const char *data;
249         unsigned int data_length;
250         struct {
251                 const char *name;
252                 unsigned int length;
253                 const char *out;
254         } out[6];
255 };
256
257 static struct hash_test_vector hash_test_vectors[] = {
258 {
259         "", 0, {
260         { "crc32",      4, "\x00\x00\x00\x00" },
261         { "sha1",      20, "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18\x90\xaf\xd8\x07\x09" },
262         { "sha256",    32, "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9\x24"
263                            "\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52\xb8\x55" },
264         { "sha512",    64, "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80\x07"
265                            "\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c\xe9\xce"
266                            "\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87\x7e\xec\x2f"
267                            "\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a\xf9\x27\xda\x3e" },
268         { "ripemd160", 20, "\x9c\x11\x85\xa5\xc5\xe9\xfc\x54\x61\x28\x08\x97\x7e\xe8\xf5\x48\xb2\x25\x8d\x31" },
269         { "whirlpool", 64, "\x19\xfa\x61\xd7\x55\x22\xa4\x66\x9b\x44\xe3\x9c\x1d\x2e\x17\x26"
270                            "\xc5\x30\x23\x21\x30\xd4\x07\xf8\x9a\xfe\xe0\x96\x49\x97\xf7\xa7"
271                            "\x3e\x83\xbe\x69\x8b\x28\x8f\xeb\xcf\x88\xe3\xe0\x3c\x4f\x07\x57"
272                            "\xea\x89\x64\xe5\x9b\x63\xd9\x37\x08\xb1\x38\xcc\x42\xa6\x6e\xb3" },
273 }},{
274         "a", 1, {
275         { "crc32",      4, "\xe8\xb7\xbe\x43" },
276         { "sha1",      20, "\x86\xf7\xe4\x37\xfa\xa5\xa7\xfc\xe1\x5d\x1d\xdc\xb9\xea\xea\xea\x37\x76\x67\xb8" },
277         { "sha256",    32, "\xca\x97\x81\x12\xca\x1b\xbd\xca\xfa\xc2\x31\xb3\x9a\x23\xdc\x4d"
278                            "\xa7\x86\xef\xf8\x14\x7c\x4e\x72\xb9\x80\x77\x85\xaf\xee\x48\xbb" },
279         { "sha512",    64, "\x1f\x40\xfc\x92\xda\x24\x16\x94\x75\x09\x79\xee\x6c\xf5\x82\xf2"
280                            "\xd5\xd7\xd2\x8e\x18\x33\x5d\xe0\x5a\xbc\x54\xd0\x56\x0e\x0f\x53"
281                            "\x02\x86\x0c\x65\x2b\xf0\x8d\x56\x02\x52\xaa\x5e\x74\x21\x05\x46"
282                            "\xf3\x69\xfb\xbb\xce\x8c\x12\xcf\xc7\x95\x7b\x26\x52\xfe\x9a\x75" },
283         { "ripemd160", 20, "\x0b\xdc\x9d\x2d\x25\x6b\x3e\xe9\xda\xae\x34\x7b\xe6\xf4\xdc\x83\x5a\x46\x7f\xfe" },
284         { "whirlpool", 64, "\x8a\xca\x26\x02\x79\x2a\xec\x6f\x11\xa6\x72\x06\x53\x1f\xb7\xd7"
285                            "\xf0\xdf\xf5\x94\x13\x14\x5e\x69\x73\xc4\x50\x01\xd0\x08\x7b\x42"
286                            "\xd1\x1b\xc6\x45\x41\x3a\xef\xf6\x3a\x42\x39\x1a\x39\x14\x5a\x59"
287                            "\x1a\x92\x20\x0d\x56\x01\x95\xe5\x3b\x47\x85\x84\xfd\xae\x23\x1a" },
288 }},{
289         "abc", 3, {
290         { "crc32",      4, "\x35\x24\x41\xc2" },
291         { "sha1",      20, "\xa9\x99\x3e\x36\x47\x06\x81\x6a\xba\x3e\x25\x71\x78\x50\xc2\x6c\x9c\xd0\xd8\x9d" },
292         { "sha256",    32, "\xba\x78\x16\xbf\x8f\x01\xcf\xea\x41\x41\x40\xde\x5d\xae\x22\x23"
293                            "\xb0\x03\x61\xa3\x96\x17\x7a\x9c\xb4\x10\xff\x61\xf2\x00\x15\xad" },
294         { "sha512",    64, "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41\x31"
295                            "\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55\xd3\x9a"
296                            "\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3\xfe\xeb\xbd"
297                            "\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f\xa5\x4c\xa4\x9f" },
298         { "ripemd160", 20, "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6\xb0\x87\xf1\x5a\x0b\xfc" },
299         { "whirlpool", 64, "\x4e\x24\x48\xa4\xc6\xf4\x86\xbb\x16\xb6\x56\x2c\x73\xb4\x02\x0b"
300                            "\xf3\x04\x3e\x3a\x73\x1b\xce\x72\x1a\xe1\xb3\x03\xd9\x7e\x6d\x4c"
301                            "\x71\x81\xee\xbd\xb6\xc5\x7e\x27\x7d\x0e\x34\x95\x71\x14\xcb\xd6"
302                            "\xc7\x97\xfc\x9d\x95\xd8\xb5\x82\xd2\x25\x29\x20\x76\xd4\xee\xf5" },
303 }},{
304         "abcdefghijklmnopqrstuvwxyz", 26, {
305         { "crc32",      4, "\x4c\x27\x50\xbd" },
306         { "sha1",      20, "\x32\xd1\x0c\x7b\x8c\xf9\x65\x70\xca\x04\xce\x37\xf2\xa1\x9d\x84\x24\x0d\x3a\x89" },
307         { "sha256",    32, "\x71\xc4\x80\xdf\x93\xd6\xae\x2f\x1e\xfa\xd1\x44\x7c\x66\xc9\x52"
308                            "\x5e\x31\x62\x18\xcf\x51\xfc\x8d\x9e\xd8\x32\xf2\xda\xf1\x8b\x73" },
309         { "sha512",    64, "\x4d\xbf\xf8\x6c\xc2\xca\x1b\xae\x1e\x16\x46\x8a\x05\xcb\x98\x81"
310                            "\xc9\x7f\x17\x53\xbc\xe3\x61\x90\x34\x89\x8f\xaa\x1a\xab\xe4\x29"
311                            "\x95\x5a\x1b\xf8\xec\x48\x3d\x74\x21\xfe\x3c\x16\x46\x61\x3a\x59"
312                            "\xed\x54\x41\xfb\x0f\x32\x13\x89\xf7\x7f\x48\xa8\x79\xc7\xb1\xf1" },
313         { "ripemd160", 20, "\xf7\x1c\x27\x10\x9c\x69\x2c\x1b\x56\xbb\xdc\xeb\x5b\x9d\x28\x65\xb3\x70\x8d\xbc" },
314         { "whirlpool", 64, "\xf1\xd7\x54\x66\x26\x36\xff\xe9\x2c\x82\xeb\xb9\x21\x2a\x48\x4a"
315                            "\x8d\x38\x63\x1e\xad\x42\x38\xf5\x44\x2e\xe1\x3b\x80\x54\xe4\x1b"
316                            "\x08\xbf\x2a\x92\x51\xc3\x0b\x6a\x0b\x8a\xae\x86\x17\x7a\xb4\xa6"
317                            "\xf6\x8f\x67\x3e\x72\x07\x86\x5d\x5d\x98\x19\xa3\xdb\xa4\xeb\x3b" },
318 }},{
319         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 62, {
320         { "crc32",      4, "\x1f\xc2\xe6\xd2" },
321         { "sha1",      20, "\x76\x1c\x45\x7b\xf7\x3b\x14\xd2\x7e\x9e\x92\x65\xc4\x6f\x4b\x4d\xda\x11\xf9\x40" },
322         { "sha256",    32, "\xdb\x4b\xfc\xbd\x4d\xa0\xcd\x85\xa6\x0c\x3c\x37\xd3\xfb\xd8\x80"
323                            "\x5c\x77\xf1\x5f\xc6\xb1\xfd\xfe\x61\x4e\xe0\xa7\xc8\xfd\xb4\xc0" },
324         { "sha512",    64, "\x1e\x07\xbe\x23\xc2\x6a\x86\xea\x37\xea\x81\x0c\x8e\xc7\x80\x93"
325                            "\x52\x51\x5a\x97\x0e\x92\x53\xc2\x6f\x53\x6c\xfc\x7a\x99\x96\xc4"
326                            "\x5c\x83\x70\x58\x3e\x0a\x78\xfa\x4a\x90\x04\x1d\x71\xa4\xce\xab"
327                            "\x74\x23\xf1\x9c\x71\xb9\xd5\xa3\xe0\x12\x49\xf0\xbe\xbd\x58\x94" },
328         { "ripemd160", 20, "\xb0\xe2\x0b\x6e\x31\x16\x64\x02\x86\xed\x3a\x87\xa5\x71\x30\x79\xb2\x1f\x51\x89" },
329         { "whirlpool", 64, "\xdc\x37\xe0\x08\xcf\x9e\xe6\x9b\xf1\x1f\x00\xed\x9a\xba\x26\x90"
330                            "\x1d\xd7\xc2\x8c\xde\xc0\x66\xcc\x6a\xf4\x2e\x40\xf8\x2f\x3a\x1e"
331                            "\x08\xeb\xa2\x66\x29\x12\x9d\x8f\xb7\xcb\x57\x21\x1b\x92\x81\xa6"
332                            "\x55\x17\xcc\x87\x9d\x7b\x96\x21\x42\xc6\x5f\x5a\x7a\xf0\x14\x67" },
333 }},{
334         "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56, {
335         { "crc32",      4, "\x17\x1a\x3f\x5f" },
336         { "sha1",      20, "\x84\x98\x3e\x44\x1c\x3b\xd2\x6e\xba\xae\x4a\xa1\xf9\x51\x29\xe5\xe5\x46\x70\xf1" },
337         { "sha256",    32, "\x24\x8d\x6a\x61\xd2\x06\x38\xb8\xe5\xc0\x26\x93\x0c\x3e\x60\x39"
338                            "\xa3\x3c\xe4\x59\x64\xff\x21\x67\xf6\xec\xed\xd4\x19\xdb\x06\xc1" },
339         { "sha512",    64, "\x20\x4a\x8f\xc6\xdd\xa8\x2f\x0a\x0c\xed\x7b\xeb\x8e\x08\xa4\x16"
340                            "\x57\xc1\x6e\xf4\x68\xb2\x28\xa8\x27\x9b\xe3\x31\xa7\x03\xc3\x35"
341                            "\x96\xfd\x15\xc1\x3b\x1b\x07\xf9\xaa\x1d\x3b\xea\x57\x78\x9c\xa0"
342                            "\x31\xad\x85\xc7\xa7\x1d\xd7\x03\x54\xec\x63\x12\x38\xca\x34\x45" },
343         { "ripemd160", 20, "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc\xf4\x9a\xda\x62\xeb\x2b" },
344         { "whirlpool", 64, "\x52\x6b\x23\x94\xd8\x56\x83\xe2\x4b\x29\xac\xd0\xfd\x37\xf7\xd5"
345                            "\x02\x7f\x61\x36\x6a\x14\x07\x26\x2d\xc2\xa6\xa3\x45\xd9\xe2\x40"
346                            "\xc0\x17\xc1\x83\x3d\xb1\xe6\xdb\x6a\x46\xbd\x44\x4b\x0c\x69\x52"
347                            "\x0c\x85\x6e\x7c\x6e\x9c\x36\x6d\x15\x0a\x7d\xa3\xae\xb1\x60\xd1" },
348 }},{
349         "message digest", 14, {
350         { "crc32",      4, "\x20\x15\x9d\x7f" },
351         { "sha1",      20, "\xc1\x22\x52\xce\xda\x8b\xe8\x99\x4d\x5f\xa0\x29\x0a\x47\x23\x1c\x1d\x16\xaa\xe3" },
352         { "sha256",    32, "\xf7\x84\x6f\x55\xcf\x23\xe1\x4e\xeb\xea\xb5\xb4\xe1\x55\x0c\xad"
353                            "\x5b\x50\x9e\x33\x48\xfb\xc4\xef\xa3\xa1\x41\x3d\x39\x3c\xb6\x50" },
354         { "sha512",    64, "\x10\x7d\xbf\x38\x9d\x9e\x9f\x71\xa3\xa9\x5f\x6c\x05\x5b\x92\x51"
355                            "\xbc\x52\x68\xc2\xbe\x16\xd6\xc1\x34\x92\xea\x45\xb0\x19\x9f\x33"
356                            "\x09\xe1\x64\x55\xab\x1e\x96\x11\x8e\x8a\x90\x5d\x55\x97\xb7\x20"
357                            "\x38\xdd\xb3\x72\xa8\x98\x26\x04\x6d\xe6\x66\x87\xbb\x42\x0e\x7c" },
358         { "ripemd160", 20, "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8\x5f\xfa\x21\x59\x5f\x36" },
359         { "whirlpool", 64, "\x37\x8c\x84\xa4\x12\x6e\x2d\xc6\xe5\x6d\xcc\x74\x58\x37\x7a\xac"
360                            "\x83\x8d\x00\x03\x22\x30\xf5\x3c\xe1\xf5\x70\x0c\x0f\xfb\x4d\x3b"
361                            "\x84\x21\x55\x76\x59\xef\x55\xc1\x06\xb4\xb5\x2a\xc5\xa4\xaa\xa6"
362                            "\x92\xed\x92\x00\x52\x83\x8f\x33\x62\xe8\x6d\xbd\x37\xa8\x90\x3e" },
363 }}};
364
365 /*
366  * HMAC tests
367  */
368 // RFC 4231 - HMAC test vectors for SHA-256, SHA-512
369 // RFC 2202 - HMAC test vectors for SHA-1
370
371 struct hmac_test_vector {
372         const char *key;
373         unsigned int key_length;
374         const char *data;
375         unsigned int data_length;
376         struct {
377                 const char *name;
378                 unsigned int length;
379                 const char *out;
380         } out[3];
381 };
382
383 static struct hmac_test_vector hmac_test_vectors[] = {
384 {
385         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", 20,
386         "\x48\x69\x20\x54\x68\x65\x72\x65", 8, /* "Hi There" */ {
387         { "sha1",   20, "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00" },
388         { "sha256", 32, "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1\x2b"
389                         "\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32\xcf\xf7" },
390         { "sha512", 64, "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c\xb0"
391                         "\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1\x7c\xde"
392                         "\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae\xa3\xf4\xe4"
393                         "\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20\x3a\x12\x68\x54" },
394 }},{
395         "\x4a\x65\x66\x65", 4, /* "Jefe" */
396         "\x77\x68\x61\x74\x20\x64\x6f\x20\x79\x61\x20\x77\x61\x6e\x74\x20"
397         "\x66\x6f\x72\x20\x6e\x6f\x74\x68\x69\x6e\x67\x3f", 28, /* "what do ya want for nothing?" */ {
398         { "sha1",   20, "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79" },
399         { "sha256", 32, "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75\xc7"
400                         "\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec\x38\x43" },
401         { "sha512", 64, "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0\xa3"
402                         "\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25\x05\x54"
403                         "\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8\xf0\xe6\xfd"
404                         "\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a\x38\xbc\xe7\x37" },
405 }},{
406         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa", 20,
407         "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
408         "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
409         "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd", 50, {
410         { "sha1",   20, "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3" },
411         { "sha256", 32, "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81\xa7"
412                         "\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5\x65\xfe" },
413         { "sha512", 64, "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b\xe9"
414                         "\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27\x9d\x39"
415                         "\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e\x67\xc8\x07"
416                         "\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59\xe1\x32\x92\xfb" },
417 }},{
418         "\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,
419         "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
420         "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
421         "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd", 50, {
422         { "sha1",   20, "\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda" },
423         { "sha256", 32, "\x82\x55\x8a\x38\x9a\x44\x3c\x0e\xa4\xcc\x81\x98\x99\xf2\x08\x3a"
424                         "\x85\xf0\xfa\xa3\xe5\x78\xf8\x07\x7a\x2e\x3f\xf4\x67\x29\x66\x5b" },
425         { "sha512", 64, "\xb0\xba\x46\x56\x37\x45\x8c\x69\x90\xe5\xa8\xc5\xf6\x1d\x4a\xf7"
426                         "\xe5\x76\xd9\x7f\xf9\x4b\x87\x2d\xe7\x6f\x80\x50\x36\x1e\xe3\xdb"
427                         "\xa9\x1c\xa5\xc1\x1a\xa2\x5e\xb4\xd6\x79\x27\x5c\xc5\x78\x80\x63"
428                         "\xa5\xf1\x97\x41\x12\x0c\x4f\x2d\xe2\xad\xeb\xeb\x10\xa2\x98\xdd" },
429 }},{
430         // Long key
431         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
432         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
433         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
434         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
435         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
436         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
437         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
438         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa", 131,
439         "\x54\x65\x73\x74\x20\x55\x73\x69\x6e\x67\x20\x4c\x61\x72\x67\x65"
440         "\x72\x20\x54\x68\x61\x6e\x20\x42\x6c\x6f\x63\x6b\x2d\x53\x69\x7a"
441         "\x65\x20\x4b\x65\x79\x20\x2d\x20\x48\x61\x73\x68\x20\x4b\x65\x79"
442         "\x20\x46\x69\x72\x73\x74", 54, /* "Test Using Larger Than Block-Size Key - Hash Key First" */ {
443         { "sha1",   20, "\x90\xd0\xda\xce\x1c\x1b\xdc\x95\x73\x39\x30\x78\x03\x16\x03\x35\xbd\xe6\xdf\x2b" },
444         { "sha256", 32, "\x60\xe4\x31\x59\x1e\xe0\xb6\x7f\x0d\x8a\x26\xaa\xcb\xf5\xb7\x7f"
445                         "\x8e\x0b\xc6\x21\x37\x28\xc5\x14\x05\x46\x04\x0f\x0e\xe3\x7f\x54" },
446         { "sha512", 64, "\x80\xb2\x42\x63\xc7\xc1\xa3\xeb\xb7\x14\x93\xc1\xdd\x7b\xe8\xb4"
447                         "\x9b\x46\xd1\xf4\x1b\x4a\xee\xc1\x12\x1b\x01\x37\x83\xf8\xf3\x52"
448                         "\x6b\x56\xd0\x37\xe0\x5f\x25\x98\xbd\x0f\xd2\x21\x5d\x6a\x1e\x52"
449                         "\x95\xe6\x4f\x73\xf6\x3f\x0a\xec\x8b\x91\x5a\x98\x5d\x78\x65\x98" },
450 }},{
451         // Long key and long data
452         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
453         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
454         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
455         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
456         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
457         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
458         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
459         "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa", 131,
460         "\x54\x68\x69\x73\x20\x69\x73\x20\x61\x20\x74\x65\x73\x74\x20\x75"
461         "\x73\x69\x6e\x67\x20\x61\x20\x6c\x61\x72\x67\x65\x72\x20\x74\x68"
462         "\x61\x6e\x20\x62\x6c\x6f\x63\x6b\x2d\x73\x69\x7a\x65\x20\x6b\x65"
463         "\x79\x20\x61\x6e\x64\x20\x61\x20\x6c\x61\x72\x67\x65\x72\x20\x74"
464         "\x68\x61\x6e\x20\x62\x6c\x6f\x63\x6b\x2d\x73\x69\x7a\x65\x20\x64"
465         "\x61\x74\x61\x2e\x20\x54\x68\x65\x20\x6b\x65\x79\x20\x6e\x65\x65"
466         "\x64\x73\x20\x74\x6f\x20\x62\x65\x20\x68\x61\x73\x68\x65\x64\x20"
467         "\x62\x65\x66\x6f\x72\x65\x20\x62\x65\x69\x6e\x67\x20\x75\x73\x65"
468         "\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, {
469         { "sha1",   20, "\x21\x7e\x44\xbb\x08\xb6\xe0\x6a\x2d\x6c\x30\xf3\xcb\x9f\x53\x7f\x97\xc6\x33\x56" },
470         { "sha256", 32, "\x9b\x09\xff\xa7\x1b\x94\x2f\xcb\x27\x63\x5f\xbc\xd5\xb0\xe9\x44"
471                         "\xbf\xdc\x63\x64\x4f\x07\x13\x93\x8a\x7f\x51\x53\x5c\x3a\x35\xe2" },
472         { "sha512", 64, "\xe3\x7b\x6a\x77\x5d\xc8\x7d\xba\xa4\xdf\xa9\xf9\x6e\x5e\x3f\xfd"
473                         "\xde\xbd\x71\xf8\x86\x72\x89\x86\x5d\xf5\xa3\x2d\x20\xcd\xc9\x44"
474                         "\xb6\x02\x2c\xac\x3c\x49\x82\xb1\x0d\x5e\xeb\x55\xc3\xe4\xde\x15"
475                         "\x13\x46\x76\xfb\x6d\xe0\x44\x60\x65\xc9\x74\x40\xfa\x8c\x6a\x58" },
476 }}};
477
478 /*
479  * Block cipher tests
480  */
481 struct cipher_test_vector {
482         const char *key;
483         unsigned int key_length;
484         const char *iv;
485         unsigned int iv_length;
486         const char *plaintext;
487         unsigned int data_length;
488         struct {
489                 const char *name;
490                 const char *mode;
491                 const char *ciphertext;
492         } out[2];
493 };
494
495 static struct cipher_test_vector cipher_test_vectors[] = {
496 { // NIST SP 800-38A
497         "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c", 16,
498         NULL, 0,
499         "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a"
500         "\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51"
501         "\x30\xc8\x1c\x46\xa3\x5c\xe4\x11\xe5\xfb\xc1\x19\x1a\x0a\x52\xef"
502         "\xf6\x9f\x24\x45\xdf\x4f\x9b\x17\xad\x2b\x41\x7b\xe6\x6c\x37\x10", 64, {
503         {
504                 "aes", "ecb",
505                 "\x3a\xd7\x7b\xb4\x0d\x7a\x36\x60\xa8\x9e\xca\xf3\x24\x66\xef\x97"
506                 "\xf5\xd3\xd5\x85\x03\xb9\x69\x9d\xe7\x85\x89\x5a\x96\xfd\xba\xaf"
507                 "\x43\xb1\xcd\x7f\x59\x8e\xce\x23\x88\x1b\x00\xe3\xed\x03\x06\x88"
508                 "\x7b\x0c\x78\x5e\x27\xe8\xad\x3f\x82\x23\x20\x71\x04\x72\x5d\xd4"
509         },{
510                 "serpent", "ecb",
511                 "\xf7\xa7\x21\xe6\xc7\x56\xb6\x55\xcb\xdf\x53\x3f\xc3\xb3\x1a\xc4"
512                 "\x4b\xc6\x04\x29\x3a\x81\xa6\xa6\xe4\xcb\xa7\x8d\x1a\x32\xa2\x9e"
513                 "\xcf\xc2\x8e\x50\x97\xdd\x6b\x49\xa9\x38\xb1\x51\x5e\xbc\x5a\xac"
514                 "\xfe\xd2\xc4\x95\x92\xf9\x1c\x0c\x9f\x17\xcd\x86\x38\x65\x29\xeb"
515         },
516 }},{ // NIST SP 800-38A
517         "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c", 16,
518         "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f", 16,
519         "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a"
520         "\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51"
521         "\x30\xc8\x1c\x46\xa3\x5c\xe4\x11\xe5\xfb\xc1\x19\x1a\x0a\x52\xef"
522         "\xf6\x9f\x24\x45\xdf\x4f\x9b\x17\xad\x2b\x41\x7b\xe6\x6c\x37\x10", 64, {
523         {
524                 "aes", "cbc",
525                 "\x76\x49\xab\xac\x81\x19\xb2\x46\xce\xe9\x8e\x9b\x12\xe9\x19\x7d"
526                 "\x50\x86\xcb\x9b\x50\x72\x19\xee\x95\xdb\x11\x3a\x91\x76\x78\xb2"
527                 "\x73\xbe\xd6\xb8\xe3\xc1\x74\x3b\x71\x16\xe6\x9e\x22\x22\x95\x16"
528                 "\x3f\xf1\xca\xa1\x68\x1f\xac\x09\x12\x0e\xca\x30\x75\x86\xe1\xa7"
529         },{
530                 "serpent", "cbc",
531                 "\xdd\x73\x69\x1a\xb5\x66\xb6\x38\xe3\xb9\x62\x36\xc8\xc8\xa1\xdd"
532                 "\xa9\xb5\xd9\xdb\x20\xfb\x8b\x82\x51\x40\xbf\xe6\x4d\xf2\x1c\xa8"
533                 "\x5f\x48\xbc\x29\xff\x62\x27\xda\x09\x7c\xaa\x22\x75\x6f\x43\xff"
534                 "\x31\xd8\x3e\x83\x4d\x92\x48\xeb\x49\x1c\xf8\x26\x80\x4e\xb9\x02"
535         },
536 }},{ // NIST SP 800-38A
537         "\x60\x3d\xeb\x10\x15\xca\x71\xbe\x2b\x73\xae\xf0\x85\x7d\x77\x81"
538         "\x1f\x35\x2c\x07\x3b\x61\x08\xd7\x2d\x98\x10\xa3\x09\x14\xdf\xf4", 32,
539         NULL, 0,
540         "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a"
541         "\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51"
542         "\x30\xc8\x1c\x46\xa3\x5c\xe4\x11\xe5\xfb\xc1\x19\x1a\x0a\x52\xef"
543         "\xf6\x9f\x24\x45\xdf\x4f\x9b\x17\xad\x2b\x41\x7b\xe6\x6c\x37\x10", 64, {
544         {
545                 "aes", "ecb",
546                 "\xf3\xee\xd1\xbd\xb5\xd2\xa0\x3c\x06\x4b\x5a\x7e\x3d\xb1\x81\xf8"
547                 "\x59\x1c\xcb\x10\xd4\x10\xed\x26\xdc\x5b\xa7\x4a\x31\x36\x28\x70"
548                 "\xb6\xed\x21\xb9\x9c\xa6\xf4\xf9\xf1\x53\xe7\xb1\xbe\xaf\xed\x1d"
549                 "\x23\x30\x4b\x7a\x39\xf9\xf3\xff\x06\x7d\x8d\x8f\x9e\x24\xec\xc7"
550         },{
551                 "serpent", "ecb",
552                 "\x78\xe5\x84\x8e\xd9\xd5\xde\x2d\x4d\xb0\x2f\x53\x61\x6a\xfd\xf2"
553                 "\x50\x5d\xf1\x68\x92\x40\x8e\xf6\x9c\x3b\x9e\xa6\x67\xd9\xdd\xb8"
554                 "\xb9\x5f\xc8\x20\x76\x52\x1d\xce\x60\xe4\xfc\xac\xe3\xd3\x91\x51"
555                 "\x09\x22\x62\xde\x62\x6d\xc5\x7b\x4c\x87\x0c\x65\xe7\x1f\xc7\x13"
556         },
557 }},{ // NIST SP 800-38A
558         "\x60\x3d\xeb\x10\x15\xca\x71\xbe\x2b\x73\xae\xf0\x85\x7d\x77\x81"
559         "\x1f\x35\x2c\x07\x3b\x61\x08\xd7\x2d\x98\x10\xa3\x09\x14\xdf\xf4", 32,
560         "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f", 16,
561         "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a"
562         "\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51"
563         "\x30\xc8\x1c\x46\xa3\x5c\xe4\x11\xe5\xfb\xc1\x19\x1a\x0a\x52\xef"
564         "\xf6\x9f\x24\x45\xdf\x4f\x9b\x17\xad\x2b\x41\x7b\xe6\x6c\x37\x10", 64, {
565         {
566                 "aes", "cbc",
567                 "\xf5\x8c\x4c\x04\xd6\xe5\xf1\xba\x77\x9e\xab\xfb\x5f\x7b\xfb\xd6"
568                 "\x9c\xfc\x4e\x96\x7e\xdb\x80\x8d\x67\x9f\x77\x7b\xc6\x70\x2c\x7d"
569                 "\x39\xf2\x33\x69\xa9\xd9\xba\xcf\xa5\x30\xe2\x63\x04\x23\x14\x61"
570                 "\xb2\xeb\x05\xe2\xc3\x9b\xe9\xfc\xda\x6c\x19\x07\x8c\x6a\x9d\x1b"
571         },{
572                 "serpent", "cbc",
573                 "\xb8\x93\xc8\xde\xc5\xc8\x5f\x03\x01\xac\x32\x74\xdf\xc6\x71\x9d"
574                 "\x37\x61\xc5\xf8\x34\x4d\xe9\x10\x91\xd3\x87\x80\x42\xcc\x70\x95"
575                 "\x40\x95\xa3\x2c\xdb\x38\xe2\x6f\x03\x91\xf5\xd3\x51\x7e\x52\xb0"
576                 "\x8a\x1c\x2d\x7f\x04\x59\x13\x93\x31\xa9\x82\xc9\x4e\xd9\x11\x0c"
577         },
578 }},{ // CAVS XTSGenAES128,101
579         "\xb7\xb9\x3f\x51\x6a\xef\x29\x5e\xff\x3a\x29\xd8\x37\xcf\x1f\x13"
580         "\x53\x47\xe8\xa2\x1d\xae\x61\x6f\xf5\x06\x2b\x2e\x8d\x78\xce\x5e", 32,
581         "\x87\x3e\xde\xa6\x53\xb6\x43\xbd\x8b\xcf\x51\x40\x31\x97\xed\x14", 16,
582         "\x23\x6f\x8a\x5b\x58\xdd\x55\xf6\x19\x4e\xd7\x0c\x4a\xc1\xa1\x7f"
583         "\x1f\xe6\x0e\xc9\xa6\xc4\x54\xd0\x87\xcc\xb7\x7d\x6b\x63\x8c\x47", 32, {
584         {
585                 "aes", "xts",
586                 "\x22\xe6\xa3\xc6\x37\x9d\xcf\x75\x99\xb0\x52\xb5\xa7\x49\xc7\xf7"
587                 "\x8a\xd8\xa1\x1b\x9f\x1a\xa9\x43\x0c\xf3\xae\xf4\x45\x68\x2e\x19"
588         },{
589                 "serpent", "xts",
590                 "\x6d\xa2\xa4\x2b\x18\x71\x57\xdc\x03\xaf\x8b\x82\x28\x66\x3d\xf1"
591                 "\x70\x8b\x75\x98\xd2\xdd\xbf\x72\x9e\xb3\xb4\xc2\x3f\x18\xdf\xa1"
592         },
593 }},{ // CAVS XTSGenAES256,101
594         "\x26\x6c\x33\x6b\x3b\x01\x48\x9f\x32\x67\xf5\x28\x35\xfd\x92\xf6"
595         "\x74\x37\x4b\x88\xb4\xe1\xeb\xd2\xd3\x6a\x5f\x45\x75\x81\xd9\xd0"
596         "\x42\xc3\xee\xf7\xb0\xb7\xe5\x13\x7b\x08\x64\x96\xb4\xd9\xe6\xac"
597         "\x65\x8d\x71\x96\xa2\x3f\x23\xf0\x36\x17\x2f\xdb\x8f\xae\xe5\x27", 64,
598         "\x06\xb2\x09\xa7\xa2\x2f\x48\x6e\xcb\xfa\xdb\x0f\x31\x37\xba\x42", 16,
599         "\xca\x7d\x65\xef\x8d\x3d\xfa\xd3\x45\xb6\x1c\xcd\xdc\xa1\xad\x81"
600         "\xde\x83\x0b\x9e\x86\xc7\xb4\x26\xd7\x6c\xb7\xdb\x76\x68\x52\xd9"
601         "\x81\xc6\xb2\x14\x09\x39\x9d\x78\xf4\x2c\xc0\xb3\x3a\x7b\xbb\x06", 48, {
602         {
603                 "aes", "xts",
604                 "\xc7\x32\x56\x87\x0c\xc2\xf4\xdd\x57\xac\xc7\x4b\x54\x56\xdb\xd7"
605                 "\x76\x91\x2a\x12\x8b\xc1\xf7\x7d\x72\xcd\xeb\xbf\x27\x00\x44\xb7"
606                 "\xa4\x3c\xee\xd2\x90\x25\xe1\xe8\xbe\x21\x1f\xa3\xc3\xed\x00\x2d"
607         },{
608                 "serpent", "xts",
609                 "\x37\xe4\xc0\xa9\xf1\x49\xe5\x3e\x73\xb9\x1f\xec\xdc\xe0\xbd\xc5"
610                 "\x31\xd7\xef\x08\x65\x20\xe3\xad\xd9\x84\x60\xdc\x61\x6f\x26\x86"
611                 "\xb8\xd5\x29\x4b\x04\x41\x52\x59\x05\x00\xb0\xc2\x9b\x30\xda\x48"
612         },
613 }},{
614         "\xa5\x28\x24\x34\x1a\x3c\xd8\xf7\x05\x91\x8f\xee\x85\x1f\x35\x7f"
615         "\x80\x3d\xfc\x9b\x94\xf6\xfc\x9e\x19\x09\x00\xa9\x04\x31\x4f\x11", 32,
616         "\xa1\xba\x49\x95\xff\x34\x6d\xb8\xcd\x87\x5d\x5e\xfd\xea\x85\xdb"
617         "\x8a\x7b\x5e\xb2\x5d\x57\xdd\x62\xac\xa9\x8c\x41\x42\x94\x75\xb7", 32,
618         "\x69\xb4\xe8\x8c\x37\xe8\x67\x82\xf1\xec\x5d\x04\xe5\x14\x91\x13"
619         "\xdf\xf2\x87\x1b\x69\x81\x1d\x71\x70\x9e\x9c\x3b\xde\x49\x70\x11"
620         "\xa0\xa3\xdb\x0d\x54\x4f\x66\x69\xd7\xdb\x80\xa7\x70\x92\x68\xce"
621         "\x81\x04\x2c\xc6\xab\xae\xe5\x60\x15\xe9\x6f\xef\xaa\x8f\xa7\xa7"
622         "\x63\x8f\xf2\xf0\x77\xf1\xa8\xea\xe1\xb7\x1f\x9e\xab\x9e\x4b\x3f"
623         "\x07\x87\x5b\x6f\xcd\xa8\xaf\xb9\xfa\x70\x0b\x52\xb8\xa8\xa7\x9e"
624         "\x07\x5f\xa6\x0e\xb3\x9b\x79\x13\x79\xc3\x3e\x8d\x1c\x2c\x68\xc8"
625         "\x51\x1d\x3c\x7b\x7d\x79\x77\x2a\x56\x65\xc5\x54\x23\x28\xb0\x03", 128, {
626         {
627                 "xchacha12,aes", "adiantum",
628                 "\x9e\x16\xab\xed\x4b\xa7\x42\x5a\xc6\xfb\x4e\x76\xff\xbe\x03\xa0"
629                 "\x0f\xe3\xad\xba\xe4\x98\x2b\x0e\x21\x48\xa0\xb8\x65\x48\x27\x48"
630                 "\x84\x54\x54\xb2\x9a\x94\x7b\xe6\x4b\x29\xe9\xcf\x05\x91\x80\x1a"
631                 "\x3a\xf3\x41\x96\x85\x1d\x9f\x74\x51\x56\x63\xfa\x7c\x28\x85\x49"
632                 "\xf7\x2f\xf9\xf2\x18\x46\xf5\x33\x80\xa3\x3c\xce\xb2\x57\x93\xf5"
633                 "\xae\xbd\xa9\xf5\x7b\x30\xc4\x93\x66\xe0\x30\x77\x16\xe4\xa0\x31"
634                 "\xba\x70\xbc\x68\x13\xf5\xb0\x9a\xc1\xfc\x7e\xfe\x55\x80\x5c\x48"
635                 "\x74\xa6\xaa\xa3\xac\xdc\xc2\xf5\x8d\xde\x34\x86\x78\x60\x75\x8d",
636         },{
637                 "xchacha20,aes", "adiantum",
638                 "\xb1\x8b\xa0\x05\x77\xa8\x4d\x59\x1b\x8e\x21\xfc\x3a\x49\xfa\xd4"
639                 "\xeb\x36\xf3\xc4\xdf\xdc\xae\x67\x07\x3f\x70\x0e\xe9\x66\xf5\x0c"
640                 "\x30\x4d\x66\xc9\xa4\x2f\x73\x9c\x13\xc8\x49\x44\xcc\x0a\x90\x9d"
641                 "\x7c\xdd\x19\x3f\xea\x72\x8d\x58\xab\xe7\x09\x2c\xec\xb5\x44\xd2"
642                 "\xca\xa6\x2d\x7a\x5c\x9c\x2b\x15\xec\x2a\xa6\x69\x91\xf9\xf3\x13"
643                 "\xf7\x72\xc1\xc1\x40\xd5\xe1\x94\xf4\x29\xa1\x3e\x25\x02\xa8\x3e"
644                 "\x94\xc1\x91\x14\xa1\x14\xcb\xbe\x67\x4c\xb9\x38\xfe\xa7\xaa\x32"
645                 "\x29\x62\x0d\xb2\xf6\x3c\x58\x57\xc1\xd5\x5a\xbb\xd6\xa6\x2a\xe5"
646         },
647 }}};
648
649 /*
650  * Cipher IV tests
651  */
652 struct cipher_iv_test_vector {
653         const char *cipher_name;
654         const char *cipher_mode;
655         const char *key;
656         unsigned int key_length;
657         const char *iv_name;
658         uint64_t iv_offset;
659         unsigned int data_length;
660         const char in_sha256[32];
661         struct {
662                 size_t sector_size;
663                 const char out_sha256[32];
664         } out[4];
665 };
666
667 static struct cipher_iv_test_vector cipher_iv_test_vectors[] = {
668 {
669         "aes", "cbc",
670         "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c", 16,
671         "null", 0, 8192,
672         "\x9f\x1d\xcb\xc3\x5c\x35\x0d\x60\x27\xf9\x8b\xe0\xf5\xc8\xb4\x3b"
673         "\x42\xca\x52\xb7\x60\x44\x59\xc0\xc4\x2b\xe3\xaa\x88\x91\x3d\x47", {
674         {
675                 512,
676                 "\xfd\x05\xd0\x4d\x51\xb9\xd4\x87\xa4\x57\x9a\x62\x07\x39\xc9\x4a"
677                 "\x00\x90\x3e\xaf\xe8\xb2\xac\x12\xca\xeb\x58\xf9\x48\xf6\xef\x08"
678         },{
679                 1024,
680                 "\x55\x87\x5c\xde\x86\x6a\x8b\xab\x08\xbe\x5b\x38\x17\x53\xdf\xe5"
681                 "\x7e\xb9\x5f\x59\xaf\x07\xa4\xca\x6a\x24\xd1\x12\xa9\x15\x25\xf4"
682         },{
683                 2048,
684                 "\x55\x5b\x8e\x74\x90\x9d\x0d\x4b\x74\x8c\x16\x7e\x29\xcf\xa9\xa3"
685                 "\xf3\x42\x8b\x62\xda\x2d\x8c\xda\xc9\x32\xc8\x78\xe2\x7e\xd2\x70"
686         },{
687                 4096,
688                 "\xc6\x45\xba\xe0\x40\x3a\x96\x09\x5e\x46\x0d\x19\x9d\x58\x4b\x93"
689                 "\x78\xc5\x3f\xa4\x2e\x9e\xb0\x19\x04\x4b\x73\x26\xf4\xa6\xb5\xc3"
690         },
691 }},
692 {
693         "aes", "cbc",
694         "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c", 16,
695         "plain", UINT32_MAX-1, 8192,
696         "\x9f\x1d\xcb\xc3\x5c\x35\x0d\x60\x27\xf9\x8b\xe0\xf5\xc8\xb4\x3b"
697         "\x42\xca\x52\xb7\x60\x44\x59\xc0\xc4\x2b\xe3\xaa\x88\x91\x3d\x47", {
698         {
699                 512,
700                 "\x4e\xa0\x9e\x5b\xf2\x27\x88\xdb\xe9\x05\xfb\x34\xa1\x88\x3b\xa3"
701                 "\xda\x3e\x98\x50\x5d\x52\x68\x72\xc8\xac\x21\x88\x77\x35\x67\xad"
702         },{
703                 1024,
704                 "\x0e\x4d\xba\x93\xef\x44\x06\x3b\xac\x92\x29\x97\xde\x75\xe7\x18"
705                 "\x26\x06\x5c\x8f\x23\xf2\xf8\xe5\xee\xfe\xf7\x9a\xdf\xc7\xd4\x2d"
706         },{
707                 2048,
708                 "\x6f\xd7\x56\x23\x51\x65\x20\x8f\xd2\x11\x35\xe1\xd2\x05\x40\xc3"
709                 "\xd3\x18\xc1\xed\xf0\x1c\xbe\x0e\xdd\xd5\xca\x39\x21\xe0\xe4\x68"
710         },{
711                 4096,
712                 "\x80\xaa\x75\x69\x39\x29\x8f\x93\xbd\x09\x51\x96\x9b\x7d\x0f\xd0"
713                 "\xf5\xb5\xdf\xf4\x48\x8c\x21\x26\x2e\xa7\x5c\x52\x75\xaa\xfc\xe3"
714         },
715 }},
716 {
717         "aes", "cbc",
718         "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c", 16,
719         "plain64", UINT32_MAX-1, 8192,
720         "\x9f\x1d\xcb\xc3\x5c\x35\x0d\x60\x27\xf9\x8b\xe0\xf5\xc8\xb4\x3b"
721         "\x42\xca\x52\xb7\x60\x44\x59\xc0\xc4\x2b\xe3\xaa\x88\x91\x3d\x47", {
722         {
723                 512,
724                 "\x60\xe5\xc9\xf8\xcd\x48\x06\x3c\x96\x11\xc8\xbf\x1e\x67\x60\x21"
725                 "\x0c\x1f\x1a\x8b\x03\x00\x0d\xc1\x39\xc9\x27\xb8\xa8\x73\x17\x69"
726         },{
727                 1024,
728                 "\x25\xc1\x6b\x78\x8a\x22\x72\xb5\x5c\xfb\x3f\xe9\x16\x8b\x89\x96"
729                 "\xfa\x80\xed\xf4\x83\xab\x1c\x79\xd2\xc7\x44\x27\x89\x99\xbb\x83"
730         },{
731                 2048,
732                 "\xb3\x42\x15\xd7\x86\xf6\xdf\x45\x49\x78\x18\x73\xa8\x7f\x3e\xb3"
733                 "\x0b\xb8\x64\x91\x7c\xf1\x5a\x5b\x6d\x20\xbc\x0b\xe2\xab\x9b\xe6"
734         },{
735                 4096,
736                 "\x6b\x45\x5c\x24\x97\xb4\x87\x49\x99\x16\x69\x59\x72\x6b\xd7\xc9"
737                 "\xc9\x90\xec\x7f\x3b\xfb\xe9\xea\x9d\xb4\x39\x62\x4d\x22\xe5\x43"
738         },
739 }},
740 {
741         "aes", "cbc",
742         "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c", 16,
743         "plain64be", UINT32_MAX-1, 8192,
744         "\x9f\x1d\xcb\xc3\x5c\x35\x0d\x60\x27\xf9\x8b\xe0\xf5\xc8\xb4\x3b"
745         "\x42\xca\x52\xb7\x60\x44\x59\xc0\xc4\x2b\xe3\xaa\x88\x91\x3d\x47", {
746         {
747                 512,
748                 "\x7f\xf9\xdb\xe1\xf6\x8c\x4d\xb4\x33\x9d\x61\x7b\x67\x5c\xef\x69"
749                 "\xea\x94\x32\x3d\xa7\x70\x01\xe0\x06\x4c\xf8\x56\x64\xd0\xb7\xdf"
750         },{
751                 1024,
752                 "\x8e\x33\x0b\xa2\x45\x78\x5a\x3d\x5e\xf7\x74\xf9\x75\xb5\xbd\x06"
753                 "\x38\x78\x74\x4f\xd8\xec\x11\x96\xf7\x92\x2b\xb1\x9a\xc2\xc3\xef"
754         },{
755                 2048,
756                 "\xad\x94\xcb\x8d\x96\x47\x10\x5c\x54\xce\x74\xca\xc8\xa3\xbd\x3e"
757                 "\xdf\xa7\xf5\x14\x2a\x77\x4c\x50\xb8\x01\x46\xc3\x89\x50\xa7\x46"
758         },{
759                 4096,
760                 "\x3b\xdb\xbe\x01\x09\xd9\xda\xf7\x77\x85\xe2\x30\xaf\x21\xe7\x70"
761                 "\x51\x2c\x6b\xcc\x75\x40\x7e\x8d\xdc\x90\xab\xaf\x6d\x2e\x0b\x49"
762         },
763 }},
764 {
765         "aes", "cbc",
766         "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c", 16,
767         "essiv:sha256", 0, 8192,
768         "\x9f\x1d\xcb\xc3\x5c\x35\x0d\x60\x27\xf9\x8b\xe0\xf5\xc8\xb4\x3b"
769         "\x42\xca\x52\xb7\x60\x44\x59\xc0\xc4\x2b\xe3\xaa\x88\x91\x3d\x47", {
770         {
771                 512,
772                 "\xa5\x3e\x74\xc4\x1a\x5c\xf3\x6b\x63\x49\xd5\xd9\xbb\x7a\x89\x5a"
773                 "\xd5\x3e\x76\x6f\x4c\x2d\x0b\xd3\x8b\x5e\x0e\x91\xa3\x8c\x2a\xde"
774         },{
775                 1024,
776                 "\x41\x6b\xc6\x75\x2e\x99\x76\xa1\x83\xea\xd5\x97\x64\x0e\x24\x8c"
777                 "\x91\x17\x03\x38\xe7\xd8\x66\x64\xaa\xd7\x27\x50\x2a\xd3\x0b\xe6"
778         },{
779                 2048,
780                 "\x84\xdc\x45\xd3\x61\x03\xa8\x51\x85\x5b\xef\xf8\x92\x6b\x12\x06"
781                 "\x2c\xfe\x75\x3e\xcf\x28\xd1\x8b\x4d\xcb\x88\x9e\x31\xb0\x0b\x92"
782         },{
783                 4096,
784                 "\xbb\x1b\xa3\xa9\x41\xbf\x17\xd8\x76\x19\x08\x8e\x3f\x50\xed\xfd"
785                 "\x57\x1d\xd2\xc2\x8a\x32\x01\xb9\xd9\x8a\xcc\x0d\xa0\x65\x8b\x6d"
786         },
787 }},
788 {
789         "aes", "cbc",
790         "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c", 16,
791         "benbi", 0, 8192,
792         "\x9f\x1d\xcb\xc3\x5c\x35\x0d\x60\x27\xf9\x8b\xe0\xf5\xc8\xb4\x3b"
793         "\x42\xca\x52\xb7\x60\x44\x59\xc0\xc4\x2b\xe3\xaa\x88\x91\x3d\x47", {
794         {
795                 512,
796                 "\x3c\xe3\x94\xe3\x6d\x68\x5b\xdb\x5a\x8d\x71\xbf\xd3\xa6\x68\xb9"
797                 "\x1f\x33\x0f\x97\xe2\xd6\xe8\xe2\xe1\xfc\x7e\x80\x28\xf1\x73\xbd"
798         },{
799                 1024,
800                 "\x0f\x27\xa7\xae\x31\x9e\x71\x02\x12\x16\x44\x5f\xbb\xc6\xcb\x78"
801                 "\xd4\x84\x49\xe0\x88\x85\x04\xbf\x6d\xea\x60\x76\x98\x34\x0a\x7e"
802         },{
803                 2048,
804                 "\xb0\x9a\xe5\x31\x5f\x2e\x9d\x13\x04\x08\x2a\x02\x71\x3d\xdb\x5d"
805                 "\xb2\xc9\x68\x5b\xdc\xd1\x38\xc2\x96\xb3\x3b\x72\xda\x9d\xcb\xe6"
806         },{
807                 4096,
808                 "\xaa\x74\x7d\xd6\x73\xa7\x77\xe1\x7f\xb9\x76\xf7\x5c\xcf\xc0\xb7"
809                 "\xfa\x7b\xed\x15\xc2\x32\x7c\x27\xbb\x35\xfc\xfe\x12\xee\x14\x2d"
810         },
811 }},
812 {
813         "aes", "cbc",
814         "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c", 16,
815         "eboiv", 0, 8192,
816         "\x9f\x1d\xcb\xc3\x5c\x35\x0d\x60\x27\xf9\x8b\xe0\xf5\xc8\xb4\x3b"
817         "\x42\xca\x52\xb7\x60\x44\x59\xc0\xc4\x2b\xe3\xaa\x88\x91\x3d\x47", {
818         {
819                 512,
820                 "\x04\x4e\x92\x9f\x79\x66\xfe\x93\x1b\xa5\xb8\x02\xfe\x7e\xf9\x26"
821                 "\x7b\x64\x39\xe7\xb3\xca\xc4\x6e\xca\x27\xa0\x2f\xe2\xea\x91\x16"
822         },{
823                 1024,
824                 "\xb0\x4a\xa4\xb5\xd6\x45\x7a\x86\xe9\x43\x3d\xd6\x01\xf7\x68\x8e"
825                 "\xe6\x81\x8d\x50\x55\x18\x8e\x4b\xb6\xa7\x89\xdf\xe2\x4b\x94\xe2"
826         },{
827                 2048,
828                 "\xd4\x00\x1f\x26\x18\xd1\x6d\xd5\xc4\xbf\x4a\x13\x30\xae\xd7\x4b"
829                 "\x33\x1e\xd5\xe8\x43\x2d\x95\x84\x67\x39\x04\x51\x5f\x1f\x49\xe4"
830         },{
831                 4096,
832                 "\xd1\xd7\x4f\x70\x9a\xa0\x22\x27\x60\xdb\x40\x5a\x84\xce\x89\x2c"
833                 "\x4f\x98\x55\xd2\x2d\xd1\xea\x9e\x47\xae\x8a\x83\xb5\x90\xbb\x49"
834         },
835 }}};
836
837 static int pbkdf_test_vectors(void)
838 {
839         char result[256];
840         unsigned int i;
841         const struct kdf_test_vector *vec;
842
843         for (i = 0; i < (sizeof(kdf_test_vectors) / sizeof(*kdf_test_vectors)); i++) {
844                 crypt_backend_memzero(result, sizeof(result));
845                 vec = &kdf_test_vectors[i];
846                 printf("PBKDF vector %02d %s ", i, vec->type);
847                 if (vec->hash && crypt_hmac_size(vec->hash) < 0) {
848                         printf("[%s N/A]\n", vec->hash);
849                         continue;
850                 }
851                 if (crypt_pbkdf(vec->type, vec->hash,
852                     vec->password, vec->password_length,
853                     vec->salt, vec->salt_length,
854                     result, vec->output_length,
855                     vec->iterations, vec->memory, vec->parallelism) < 0) {
856                         printf("[%s-%s N/A]\n", vec->type, vec->hash);
857                         continue;
858                 }
859                 if (memcmp(result, vec->output, vec->output_length)) {
860                         printf("[FAILED]\n");
861                         printhex(" got", result, vec->output_length);
862                         printhex("want", vec->output, vec->output_length);
863                         return EXIT_FAILURE;
864                 }
865                 printf("[OK]\n");
866         }
867         return EXIT_SUCCESS;
868 }
869
870 static int crc32_test(const struct hash_test_vector *vector, unsigned int i)
871 {
872         uint32_t crc32;
873
874         if (vector->out[i].length != sizeof(uint32_t))
875                 return EXIT_FAILURE;
876
877         crc32 = crypt_crc32(~0, (const unsigned char*)vector->data, vector->data_length) ^ ~0;
878
879         if ((unsigned char)vector->out[i].out[0] != ((crc32 >> 24) & 0xFF) ||
880             (unsigned char)vector->out[i].out[1] != ((crc32 >> 16) & 0xFF) ||
881             (unsigned char)vector->out[i].out[2] != ((crc32 >>  8) & 0xFF) ||
882             (unsigned char)vector->out[i].out[3] != ((crc32 >>  0) & 0xFF)) {
883                 printf("[FAILED]\n");
884                 printhex(" got", (const char *)&crc32, sizeof(crc32));
885                 printhex("want", vector->out[i].out, vector->out[i].length);
886                 return EXIT_FAILURE;
887         }
888
889         return EXIT_SUCCESS;
890 }
891
892 static int hash_test(void)
893 {
894         const struct hash_test_vector *vector;
895         unsigned int i, j;
896         int r;
897         struct crypt_hash *h;
898         char result[64];
899
900         for (i = 0; i < ARRAY_SIZE(hash_test_vectors); i++) {
901                 vector = &hash_test_vectors[i];
902                 printf("Hash vector %02d: ", i);
903
904                 for (j = 0; j < ARRAY_SIZE(vector->out); j++) {
905
906                         // CRC32 vector test is special
907                         if (!strcmp("crc32", vector->out[j].name)) {
908                                 if (crc32_test(vector, j) < 0)
909                                         return EXIT_FAILURE;
910                                 printf("[%s]", vector->out[j].name);
911                                 continue;
912                         }
913
914                         if (crypt_hash_size(vector->out[j].name) < 0) {
915                                 printf("[%s N/A]", vector->out[j].name);
916                                 continue;
917                         }
918
919                         if (crypt_hash_size(vector->out[j].name) != (int)vector->out[j].length)
920                                 return EXIT_FAILURE;
921
922                         if (sizeof(result) < vector->out[j].length)
923                                 return EXIT_FAILURE;
924
925                         crypt_backend_memzero(result, sizeof(result));
926                         printf("[%s]", vector->out[j].name);
927
928                         if (crypt_hash_init(&h, vector->out[j].name)) {
929                                 printf("[%s N/A (init)]", vector->out[j].name);
930                                 continue;
931                         }
932
933                         r = crypt_hash_write(h, vector->data, vector->data_length);
934                         if (!r)
935                                 r = crypt_hash_final(h, result, vector->out[j].length);
936
937                         crypt_hash_destroy(h);
938
939                         if (r)
940                                 return EXIT_FAILURE;
941
942                         if (memcmp(result, vector->out[j].out, vector->out[j].length)) {
943                                 printf("[FAILED]\n");
944                                 printhex(" got", result, vector->out[j].length);
945                                 printhex("want", vector->out[j].out, vector->out[j].length);
946                                 return EXIT_FAILURE;
947                         }
948                 }
949                 printf("\n");
950         }
951
952         return EXIT_SUCCESS;
953 }
954
955 static int hmac_test(void)
956 {
957         const struct hmac_test_vector *vector;
958         struct crypt_hmac *hmac;
959         unsigned int i, j;
960         int r;
961         char result[64];
962
963         for (i = 0; i < ARRAY_SIZE(hmac_test_vectors); i++) {
964                 vector = &hmac_test_vectors[i];
965                 printf("HMAC vector %02d: ", i);
966
967                 for(j = 0; j < ARRAY_SIZE(vector->out); j++) {
968
969                         if (crypt_hmac_size(vector->out[j].name) < 0) {
970                                 printf("[%s N/A]", vector->out[j].name);
971                                 continue;
972                         }
973
974                         if (crypt_hmac_size(vector->out[j].name) != (int)vector->out[j].length)
975                                 return EXIT_FAILURE;
976
977                         if (sizeof(result) < vector->out[j].length)
978                                 return EXIT_FAILURE;
979
980                         crypt_backend_memzero(result, sizeof(result));
981                         printf("[%s]", vector->out[j].name);
982
983                         if (crypt_hmac_init(&hmac, vector->out[j].name, vector->key, vector->key_length))
984                                 return EXIT_FAILURE;
985
986                         r = crypt_hmac_write(hmac, vector->data, vector->data_length);
987                         if (!r)
988                                 r = crypt_hmac_final(hmac, result, vector->out[j].length);
989
990                         crypt_hmac_destroy(hmac);
991
992                         if (r)
993                                 return EXIT_FAILURE;
994
995                         if (memcmp(result, vector->out[j].out, vector->out[j].length)) {
996                                 printf("[FAILED]\n");
997                                 printhex(" got", result, vector->out[j].length);
998                                 printhex("want", vector->out[j].out, vector->out[j].length);
999                                 return EXIT_FAILURE;
1000                         }
1001                 }
1002                 printf("\n");
1003         }
1004
1005         return EXIT_SUCCESS;
1006 }
1007
1008 static int cipher_test(void)
1009 {
1010         const struct cipher_test_vector *vector;
1011         struct crypt_cipher *cipher;
1012         unsigned int i, j;
1013         char result[256];
1014         int r;
1015
1016         for (i = 0; i < ARRAY_SIZE(cipher_test_vectors); i++) {
1017                 vector = &cipher_test_vectors[i];
1018                 printf("CIPHER vector %02d: ", i);
1019
1020                 for (j = 0; j < ARRAY_SIZE(vector->out); j++) {
1021                         if (vector->iv_length &&
1022                             crypt_cipher_ivsize(vector->out[j].name, vector->out[j].mode) != (int)vector->iv_length)
1023                                 return EXIT_FAILURE;
1024                         if (vector->data_length > sizeof(result))
1025                                 return EXIT_FAILURE;
1026
1027                         r = crypt_cipher_init(&cipher, vector->out[j].name, vector->out[j].mode,
1028                                               vector->key, vector->key_length);
1029                         if (r == -ENOENT || r == -ENOTSUP) {
1030                                 printf("[%s-%s N/A]", vector->out[j].name, vector->out[j].mode);
1031                                 continue;
1032                         } else {
1033                                 printf("[%s-%s,%dbits]", vector->out[j].name, vector->out[j].mode, vector->key_length * 8);
1034                                 if (r)
1035                                         return EXIT_FAILURE;
1036                         }
1037
1038                         crypt_backend_memzero(result, sizeof(result));
1039                         if (crypt_cipher_encrypt(cipher, vector->plaintext, result, vector->data_length,
1040                                                  vector->iv, vector->iv_length)) {
1041                                 crypt_cipher_destroy(cipher);
1042                                 return EXIT_FAILURE;
1043                         }
1044
1045                         if (memcmp(vector->out[j].ciphertext, result, vector->data_length)) {
1046                                 printf("[ENCRYPTION FAILED]\n");
1047                                 printhex(" got", result, vector->data_length);
1048                                 printhex("want", vector->out[j].ciphertext, vector->data_length);
1049                                 crypt_cipher_destroy(cipher);
1050                                 return EXIT_FAILURE;
1051                         }
1052
1053                         crypt_backend_memzero(result, sizeof(result));
1054                         if (crypt_cipher_decrypt(cipher, vector->out[j].ciphertext, result, vector->data_length,
1055                                                  vector->iv, vector->iv_length)) {
1056                                 crypt_cipher_destroy(cipher);
1057                                 return EXIT_FAILURE;
1058                         }
1059
1060                         if (memcmp(vector->plaintext, result, vector->data_length)) {
1061                                 printf("[DECRYPTION FAILED]\n");
1062                                 printhex(" got", result, vector->data_length);
1063                                 printhex("want", vector->plaintext, vector->data_length);
1064                                 crypt_cipher_destroy(cipher);
1065                                 return EXIT_FAILURE;
1066                         }
1067
1068                         crypt_cipher_destroy(cipher);
1069                 }
1070                 printf("\n");
1071         }
1072
1073         return EXIT_SUCCESS;
1074 }
1075
1076 static void get_sha256(const char *in, size_t length, char out[32])
1077 {
1078         struct crypt_hash *h;
1079
1080         crypt_backend_memzero(out, 32);
1081         if (crypt_hash_init(&h, "sha256"))
1082                 return;
1083
1084         if (!crypt_hash_write(h, in, length))
1085                 crypt_hash_final(h, out, 32);
1086
1087         crypt_hash_destroy(h);
1088 }
1089
1090 static int cipher_iv_test(void)
1091 {
1092         const struct cipher_iv_test_vector *vector;
1093         struct crypt_storage *storage;
1094         unsigned int i, j;
1095         char mode_iv[256];
1096         char result[8192], hash[32];
1097         int r;
1098
1099         for (i = 0; i < ARRAY_SIZE(cipher_iv_test_vectors); i++) {
1100                 vector = &cipher_iv_test_vectors[i];
1101                 printf("IV vector %02d: [%s-%s-%s]", i, vector->cipher_name, vector->cipher_mode, vector->iv_name);
1102
1103                 for (j = 0; j < ARRAY_SIZE(vector->out); j++) {
1104                         if (vector->data_length > sizeof(result))
1105                                 return EXIT_FAILURE;
1106
1107                         snprintf(mode_iv, sizeof(mode_iv)-2, "%s-%s", vector->cipher_mode, vector->iv_name);
1108                         r = crypt_storage_init(&storage, vector->out[j].sector_size, vector->cipher_name, mode_iv,
1109                                                vector->key, vector->key_length);
1110                         if (r == -ENOENT || r == -ENOTSUP) {
1111                                 printf("[N/A]");
1112                                 continue;
1113                         } else {
1114                                 printf("[%i]", (int)vector->out[j].sector_size);
1115                                 if (r)
1116                                         return EXIT_FAILURE;
1117                         }
1118
1119                         crypt_backend_memzero(result, sizeof(result));
1120                         if (crypt_storage_encrypt(storage, vector->iv_offset, vector->data_length, result)) {
1121                                 crypt_storage_destroy(storage);
1122                                 return EXIT_FAILURE;
1123                         }
1124
1125                         get_sha256(result, vector->data_length, hash);
1126                         if (memcmp(vector->out[j].out_sha256, hash, sizeof(hash))) {
1127                                 printf("[ENCRYPTION FAILED]\n");
1128                                 printhex(" got", hash, sizeof(hash));
1129                                 printhex("want", vector->out[j].out_sha256, sizeof(vector->out[j].out_sha256));
1130                                 crypt_storage_destroy(storage);
1131                                 return EXIT_FAILURE;
1132                         }
1133
1134                         if (crypt_storage_decrypt(storage, vector->iv_offset, vector->data_length, result)) {
1135                                 crypt_storage_destroy(storage);
1136                                 return EXIT_FAILURE;
1137                         }
1138
1139                         get_sha256(result, vector->data_length, hash);
1140                         if (memcmp(vector->in_sha256, hash, sizeof(hash))) {
1141                                 printf("[DECRYPTION FAILED]\n");
1142                                 printhex(" got", hash, sizeof(hash));
1143                                 printhex("want", vector->in_sha256, sizeof(vector->in_sha256));
1144                                 crypt_storage_destroy(storage);
1145                                 return EXIT_FAILURE;
1146                         }
1147
1148                         crypt_storage_destroy(storage);
1149                 }
1150                 printf("\n");
1151         }
1152
1153         return EXIT_SUCCESS;
1154 }
1155
1156 static void __attribute__((noreturn)) exit_test(const char *msg, int r)
1157 {
1158         if (msg)
1159                 printf("%s\n", msg);
1160         crypt_backend_destroy();
1161         exit(r);
1162 }
1163
1164 int main(__attribute__ ((unused)) int argc, __attribute__ ((unused))char *argv[])
1165 {
1166         setvbuf(stdout, NULL, _IONBF, 0);
1167
1168         if (getenv("CRYPTSETUP_PATH")) {
1169                 printf("Cannot run this test with CRYPTSETUP_PATH set.\n");
1170                 exit(77);
1171         }
1172
1173         if (crypt_backend_init())
1174                 exit_test("Crypto backend init error.", EXIT_FAILURE);
1175
1176         printf("Test vectors using %s crypto backend.\n", crypt_backend_version());
1177
1178         if (pbkdf_test_vectors())
1179                 exit_test("PBKDF test failed.", EXIT_FAILURE);
1180
1181         if (hash_test())
1182                 exit_test("HASH test failed.", EXIT_FAILURE);
1183
1184         if (hmac_test())
1185                 exit_test("HMAC test failed.", EXIT_FAILURE);
1186
1187         if (cipher_test())
1188                 exit_test("CIPHER test failed.", EXIT_FAILURE);
1189
1190         if (cipher_iv_test())
1191                 exit_test("IV test failed.", EXIT_FAILURE);
1192
1193         exit_test(NULL, EXIT_SUCCESS);
1194 }