xref: /openbsd/regress/lib/libcrypto/bf/bf_test.c (revision 3bef86f7)
1 /*	$OpenBSD: bf_test.c,v 1.2 2022/11/07 23:04:25 joshua Exp $ */
2 /*
3  * Copyright (c) 2022 Joshua Sing <joshua@hypera.dev>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <openssl/evp.h>
19 #include <openssl/blowfish.h>
20 
21 #include <stdint.h>
22 #include <string.h>
23 
24 struct bf_test {
25 	const int mode;
26 	const uint8_t key[64];
27 	const int key_len;
28 	const uint8_t iv[64];
29 	const int iv_len;
30 	const uint8_t in[64];
31 	const int in_len;
32 	const uint8_t out[64];
33 	const int out_len;
34 	const int padding;
35 };
36 
37 static const struct bf_test bf_tests[] = {
38 	/*
39 	 * ECB - Test vectors from
40 	 * https://www.schneier.com/wp-content/uploads/2015/12/vectors-2.txt
41 	 */
42 	{
43 		.mode = NID_bf_ecb,
44 		.key = {
45 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46 		},
47 		.key_len = 8,
48 		.in = {
49 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50 		},
51 		.in_len = 8,
52 		.out = {
53 			0x4E, 0xF9, 0x97, 0x45, 0x61, 0x98, 0xDD, 0x78,
54 		},
55 		.out_len = 8,
56 	},
57 	{
58 		.mode = NID_bf_ecb,
59 		.key = {
60 			0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
61 		},
62 		.key_len = 8,
63 		.in = {
64 			0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
65 		},
66 		.in_len = 8,
67 		.out = {
68 			0x51, 0x86, 0x6F, 0xD5, 0xB8, 0x5E, 0xCB, 0x8A,
69 		},
70 		.out_len = 8,
71 	},
72 	{
73 		.mode = NID_bf_ecb,
74 		.key = {
75 			0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76 		},
77 		.key_len = 8,
78 		.in = {
79 			0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
80 		},
81 		.in_len = 8,
82 		.out = {
83 			0x7D, 0x85, 0x6F, 0x9A, 0x61, 0x30, 0x63, 0xF2,
84 		},
85 		.out_len = 8,
86 	},
87 	{
88 		.mode = NID_bf_ecb,
89 		.key = {
90 			0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
91 		},
92 		.key_len = 8,
93 		.in = {
94 			0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
95 		},
96 		.in_len = 8,
97 		.out = {
98 			0x24, 0x66, 0xDD, 0x87, 0x8B, 0x96, 0x3C, 0x9D,
99 		},
100 		.out_len = 8,
101 	},
102 	{
103 		.mode = NID_bf_ecb,
104 		.key = {
105 			0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
106 		},
107 		.key_len = 8,
108 		.in = {
109 			0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
110 		},
111 		.in_len = 8,
112 		.out = {
113 			0x61, 0xF9, 0xC3, 0x80, 0x22, 0x81, 0xB0, 0x96,
114 		},
115 		.out_len = 8,
116 	},
117 	{
118 		.mode = NID_bf_ecb,
119 		.key = {
120 			0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
121 		},
122 		.key_len = 8,
123 		.in = {
124 			0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
125 		},
126 		.in_len = 8,
127 		.out = {
128 			0x7D, 0x0C, 0xC6, 0x30, 0xAF, 0xDA, 0x1E, 0xC7,
129 		},
130 		.out_len = 8,
131 	},
132 	{
133 		.mode = NID_bf_ecb,
134 		.key = {
135 			0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
136 		},
137 		.key_len = 8,
138 		.in = {
139 			0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
140 		},
141 		.in_len = 8,
142 		.out = {
143 			0x0A, 0xCE, 0xAB, 0x0F, 0xC6, 0xA0, 0xA2, 0x8D,
144 		},
145 		.out_len = 8,
146 	},
147 	{
148 		.mode = NID_bf_ecb,
149 		.key = {
150 			0x7C, 0xA1, 0x10, 0x45, 0x4A, 0x1A, 0x6E, 0x57,
151 		},
152 		.key_len = 8,
153 		.in = {
154 			0x01, 0xA1, 0xD6, 0xD0, 0x39, 0x77, 0x67, 0x42,
155 		},
156 		.in_len = 8,
157 		.out = {
158 			0x59, 0xC6, 0x82, 0x45, 0xEB, 0x05, 0x28, 0x2B,
159 		},
160 		.out_len = 8,
161 	},
162 	{
163 		.mode = NID_bf_ecb,
164 		.key = {
165 			0x01, 0x31, 0xD9, 0x61, 0x9D, 0xC1, 0x37, 0x6E,
166 		},
167 		.key_len = 8,
168 		.in = {
169 			0x5C, 0xD5, 0x4C, 0xA8, 0x3D, 0xEF, 0x57, 0xDA,
170 		},
171 		.in_len = 8,
172 		.out = {
173 			0xB1, 0xB8, 0xCC, 0x0B, 0x25, 0x0F, 0x09, 0xA0,
174 		},
175 		.out_len = 8,
176 	},
177 	{
178 		.mode = NID_bf_ecb,
179 		.key = {
180 			0x07, 0xA1, 0x13, 0x3E, 0x4A, 0x0B, 0x26, 0x86,
181 		},
182 		.key_len = 8,
183 		.in = {
184 			0x02, 0x48, 0xD4, 0x38, 0x06, 0xF6, 0x71, 0x72,
185 		},
186 		.in_len = 8,
187 		.out = {
188 			0x17, 0x30, 0xE5, 0x77, 0x8B, 0xEA, 0x1D, 0xA4,
189 		},
190 		.out_len = 8,
191 	},
192 	{
193 		.mode = NID_bf_ecb,
194 		.key = {
195 			0x38, 0x49, 0x67, 0x4C, 0x26, 0x02, 0x31, 0x9E,
196 		},
197 		.key_len = 8,
198 		.in = {
199 			0x51, 0x45, 0x4B, 0x58, 0x2D, 0xDF, 0x44, 0x0A,
200 		},
201 		.in_len = 8,
202 		.out = {
203 			0xA2, 0x5E, 0x78, 0x56, 0xCF, 0x26, 0x51, 0xEB,
204 		},
205 		.out_len = 8,
206 	},
207 	{
208 		.mode = NID_bf_ecb,
209 		.key = {
210 			0x04, 0xB9, 0x15, 0xBA, 0x43, 0xFE, 0xB5, 0xB6,
211 		},
212 		.key_len = 8,
213 		.in = {
214 			0x42, 0xFD, 0x44, 0x30, 0x59, 0x57, 0x7F, 0xA2,
215 		},
216 		.in_len = 8,
217 		.out = {
218 			0x35, 0x38, 0x82, 0xB1, 0x09, 0xCE, 0x8F, 0x1A,
219 		},
220 		.out_len = 8,
221 	},
222 	{
223 		.mode = NID_bf_ecb,
224 		.key = {
225 			0x01, 0x13, 0xB9, 0x70, 0xFD, 0x34, 0xF2, 0xCE,
226 		},
227 		.key_len = 8,
228 		.in = {
229 			0x05, 0x9B, 0x5E, 0x08, 0x51, 0xCF, 0x14, 0x3A,
230 		},
231 		.in_len = 8,
232 		.out = {
233 			0x48, 0xF4, 0xD0, 0x88, 0x4C, 0x37, 0x99, 0x18,
234 		},
235 		.out_len = 8,
236 	},
237 	{
238 		.mode = NID_bf_ecb,
239 		.key = {
240 			0x01, 0x70, 0xF1, 0x75, 0x46, 0x8F, 0xB5, 0xE6,
241 		},
242 		.key_len = 8,
243 		.in = {
244 			0x07, 0x56, 0xD8, 0xE0, 0x77, 0x47, 0x61, 0xD2,
245 		},
246 		.in_len = 8,
247 		.out = {
248 			0x43, 0x21, 0x93, 0xB7, 0x89, 0x51, 0xFC, 0x98,
249 		},
250 		.out_len = 8,
251 	},
252 	{
253 		.mode = NID_bf_ecb,
254 		.key = {
255 			0x43, 0x29, 0x7F, 0xAD, 0x38, 0xE3, 0x73, 0xFE,
256 		},
257 		.key_len = 8,
258 		.in = {
259 			0x76, 0x25, 0x14, 0xB8, 0x29, 0xBF, 0x48, 0x6A,
260 		},
261 		.in_len = 8,
262 		.out = {
263 			0x13, 0xF0, 0x41, 0x54, 0xD6, 0x9D, 0x1A, 0xE5,
264 		},
265 		.out_len = 8,
266 	},
267 	{
268 		.mode = NID_bf_ecb,
269 		.key = {
270 			0x07, 0xA7, 0x13, 0x70, 0x45, 0xDA, 0x2A, 0x16,
271 		},
272 		.key_len = 8,
273 		.in = {
274 			0x3B, 0xDD, 0x11, 0x90, 0x49, 0x37, 0x28, 0x02,
275 		},
276 		.in_len = 8,
277 		.out = {
278 			0x2E, 0xED, 0xDA, 0x93, 0xFF, 0xD3, 0x9C, 0x79,
279 		},
280 		.out_len = 8,
281 	},
282 	{
283 		.mode = NID_bf_ecb,
284 		.key = {
285 			0x04, 0x68, 0x91, 0x04, 0xC2, 0xFD, 0x3B, 0x2F,
286 		},
287 		.key_len = 8,
288 		.in = {
289 			0x26, 0x95, 0x5F, 0x68, 0x35, 0xAF, 0x60, 0x9A,
290 		},
291 		.in_len = 8,
292 		.out = {
293 			0xD8, 0x87, 0xE0, 0x39, 0x3C, 0x2D, 0xA6, 0xE3,
294 		},
295 		.out_len = 8,
296 	},
297 	{
298 		.mode = NID_bf_ecb,
299 		.key = {
300 			0x37, 0xD0, 0x6B, 0xB5, 0x16, 0xCB, 0x75, 0x46,
301 		},
302 		.key_len = 8,
303 		.in = {
304 			0x16, 0x4D, 0x5E, 0x40, 0x4F, 0x27, 0x52, 0x32,
305 		},
306 		.in_len = 8,
307 		.out = {
308 			0x5F, 0x99, 0xD0, 0x4F, 0x5B, 0x16, 0x39, 0x69,
309 		},
310 		.out_len = 8,
311 	},
312 	{
313 		.mode = NID_bf_ecb,
314 		.key = {
315 			0x1F, 0x08, 0x26, 0x0D, 0x1A, 0xC2, 0x46, 0x5E,
316 		},
317 		.key_len = 8,
318 		.in = {
319 			0x6B, 0x05, 0x6E, 0x18, 0x75, 0x9F, 0x5C, 0xCA,
320 		},
321 		.in_len = 8,
322 		.out = {
323 			0x4A, 0x05, 0x7A, 0x3B, 0x24, 0xD3, 0x97, 0x7B,
324 		},
325 		.out_len = 8,
326 	},
327 	{
328 		.mode = NID_bf_ecb,
329 		.key = {
330 			0x58, 0x40, 0x23, 0x64, 0x1A, 0xBA, 0x61, 0x76,
331 		},
332 		.key_len = 8,
333 		.in = {
334 			0x00, 0x4B, 0xD6, 0xEF, 0x09, 0x17, 0x60, 0x62,
335 		},
336 		.in_len = 8,
337 		.out = {
338 			0x45, 0x20, 0x31, 0xC1, 0xE4, 0xFA, 0xDA, 0x8E,
339 		},
340 		.out_len = 8,
341 	},
342 	{
343 		.mode = NID_bf_ecb,
344 		.key = {
345 			0x02, 0x58, 0x16, 0x16, 0x46, 0x29, 0xB0, 0x07,
346 		},
347 		.key_len = 8,
348 		.in = {
349 			0x48, 0x0D, 0x39, 0x00, 0x6E, 0xE7, 0x62, 0xF2,
350 		},
351 		.in_len = 8,
352 		.out = {
353 			0x75, 0x55, 0xAE, 0x39, 0xF5, 0x9B, 0x87, 0xBD,
354 		},
355 		.out_len = 8,
356 	},
357 	{
358 		.mode = NID_bf_ecb,
359 		.key = {
360 			0x49, 0x79, 0x3E, 0xBC, 0x79, 0xB3, 0x25, 0x8F,
361 		},
362 		.key_len = 8,
363 		.in = {
364 			0x43, 0x75, 0x40, 0xC8, 0x69, 0x8F, 0x3C, 0xFA,
365 		},
366 		.in_len = 8,
367 		.out = {
368 			0x53, 0xC5, 0x5F, 0x9C, 0xB4, 0x9F, 0xC0, 0x19,
369 		},
370 		.out_len = 8,
371 	},
372 	{
373 		.mode = NID_bf_ecb,
374 		.key = {
375 			0x4F, 0xB0, 0x5E, 0x15, 0x15, 0xAB, 0x73, 0xA7,
376 		},
377 		.key_len = 8,
378 		.in = {
379 			0x07, 0x2D, 0x43, 0xA0, 0x77, 0x07, 0x52, 0x92,
380 		},
381 		.in_len = 8,
382 		.out = {
383 			0x7A, 0x8E, 0x7B, 0xFA, 0x93, 0x7E, 0x89, 0xA3,
384 		},
385 		.out_len = 8,
386 	},
387 	{
388 		.mode = NID_bf_ecb,
389 		.key = {
390 			0x49, 0xE9, 0x5D, 0x6D, 0x4C, 0xA2, 0x29, 0xBF,
391 		},
392 		.key_len = 8,
393 		.in = {
394 			0x02, 0xFE, 0x55, 0x77, 0x81, 0x17, 0xF1, 0x2A,
395 		},
396 		.in_len = 8,
397 		.out = {
398 			0xCF, 0x9C, 0x5D, 0x7A, 0x49, 0x86, 0xAD, 0xB5,
399 		},
400 		.out_len = 8,
401 	},
402 	{
403 		.mode = NID_bf_ecb,
404 		.key = {
405 			0x01, 0x83, 0x10, 0xDC, 0x40, 0x9B, 0x26, 0xD6,
406 		},
407 		.key_len = 8,
408 		.in = {
409 			0x1D, 0x9D, 0x5C, 0x50, 0x18, 0xF7, 0x28, 0xC2,
410 		},
411 		.in_len = 8,
412 		.out = {
413 			0xD1, 0xAB, 0xB2, 0x90, 0x65, 0x8B, 0xC7, 0x78,
414 		},
415 		.out_len = 8,
416 	},
417 	{
418 		.mode = NID_bf_ecb,
419 		.key = {
420 			0x1C, 0x58, 0x7F, 0x1C, 0x13, 0x92, 0x4F, 0xEF,
421 		},
422 		.key_len = 8,
423 		.in = {
424 			0x30, 0x55, 0x32, 0x28, 0x6D, 0x6F, 0x29, 0x5A,
425 		},
426 		.in_len = 8,
427 		.out = {
428 			0x55, 0xCB, 0x37, 0x74, 0xD1, 0x3E, 0xF2, 0x01,
429 		},
430 		.out_len = 8,
431 	},
432 	{
433 		.mode = NID_bf_ecb,
434 		.key = {
435 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
436 		},
437 		.key_len = 8,
438 		.in = {
439 			0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
440 		},
441 		.in_len = 8,
442 		.out = {
443 			0xFA, 0x34, 0xEC, 0x48, 0x47, 0xB2, 0x68, 0xB2,
444 		},
445 		.out_len = 8,
446 	},
447 	{
448 		.mode = NID_bf_ecb,
449 		.key = {
450 			0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E,
451 		},
452 		.key_len = 8,
453 		.in = {
454 			0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
455 		},
456 		.in_len = 8,
457 		.out = {
458 			0xA7, 0x90, 0x79, 0x51, 0x08, 0xEA, 0x3C, 0xAE,
459 		},
460 		.out_len = 8,
461 	},
462 	{
463 		.mode = NID_bf_ecb,
464 		.key = {
465 			0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE,
466 		},
467 		.key_len = 8,
468 		.in = {
469 			0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
470 		},
471 		.in_len = 8,
472 		.out = {
473 			0xC3, 0x9E, 0x07, 0x2D, 0x9F, 0xAC, 0x63, 0x1D,
474 		},
475 		.out_len = 8,
476 	},
477 	{
478 		.mode = NID_bf_ecb,
479 		.key = {
480 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
481 		},
482 		.key_len = 8,
483 		.in = {
484 			0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
485 		},
486 		.in_len = 8,
487 		.out = {
488 			0x01, 0x49, 0x33, 0xE0, 0xCD, 0xAF, 0xF6, 0xE4,
489 		},
490 		.out_len = 8,
491 	},
492 	{
493 		.mode = NID_bf_ecb,
494 		.key = {
495 			0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
496 		},
497 		.key_len = 8,
498 		.in = {
499 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
500 		},
501 		.in_len = 8,
502 		.out = {
503 			0xF2, 0x1E, 0x9A, 0x77, 0xB7, 0x1C, 0x49, 0xBC,
504 		},
505 		.out_len = 8,
506 	},
507 	{
508 		.mode = NID_bf_ecb,
509 		.key = {
510 			0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
511 		},
512 		.key_len = 8,
513 		.in = {
514 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
515 		},
516 		.in_len = 8,
517 		.out = {
518 			0x24, 0x59, 0x46, 0x88, 0x57, 0x54, 0x36, 0x9A,
519 		},
520 		.out_len = 8,
521 	},
522 	{
523 		.mode = NID_bf_ecb,
524 		.key = {
525 			0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
526 		},
527 		.key_len = 8,
528 		.in = {
529 			0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
530 		},
531 		.in_len = 8,
532 		.out = {
533 			0x6B, 0x5C, 0x5A, 0x9C, 0x5D, 0x9E, 0x0A, 0x5A,
534 		},
535 		.out_len = 8,
536 	},
537 
538 	/*
539 	 * CBC - Test vector from
540 	 * https://www.schneier.com/wp-content/uploads/2015/12/vectors-2.txt
541 	 */
542 	{
543 		.mode = NID_bf_cbc,
544 		.key = {
545 			0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
546 			0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87,
547 		},
548 		.key_len = 16,
549 		.iv = {
550 			0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
551 		},
552 		.iv_len = 8,
553 		.in = {
554 			0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x20,
555 			0x4E, 0x6F, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
556 			0x68, 0x65, 0x20, 0x74, 0x69, 0x6D, 0x65, 0x20,
557 			0x66, 0x6F, 0x72, 0x20, 0x00, 0x00, 0x00, 0x00,
558 		},
559 		.in_len = 32,
560 		.out = {
561 			0x6B, 0x77, 0xB4, 0xD6, 0x30, 0x06, 0xDE, 0xE6,
562 			0x05, 0xB1, 0x56, 0xE2, 0x74, 0x03, 0x97, 0x93,
563 			0x58, 0xDE, 0xB9, 0xE7, 0x15, 0x46, 0x16, 0xD9,
564 			0x59, 0xF1, 0x65, 0x2B, 0xD5, 0xFF, 0x92, 0xCC,
565 		},
566 		.out_len = 32,
567 		.padding = 0,
568 	},
569 
570 	/* CBC (generated using https://github.com/joshuasing/libressl-test-gen) */
571 	{
572 		.mode = NID_bf_cbc,
573 		.key = {
574 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
575 		},
576 		.key_len = 8,
577 		.iv = {
578 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
579 		},
580 		.iv_len = 8,
581 		.in = {
582 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
583 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
584 		},
585 		.in_len = 16,
586 		.out = {
587 			0x4e, 0xf9, 0x97, 0x45, 0x61, 0x98, 0xdd, 0x78,
588 			0xe1, 0xc0, 0x30, 0xe7, 0x4c, 0x14, 0xd2, 0x61,
589 		},
590 		.out_len = 16,
591 	},
592 	{
593 		.mode = NID_bf_cbc,
594 		.key = {
595 			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
596 			0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
597 		},
598 		.key_len = 16,
599 		.iv = {
600 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
601 		},
602 		.iv_len = 8,
603 		.in = {
604 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
605 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
606 		},
607 		.in_len = 16,
608 		.out = {
609 			0xb9, 0x95, 0xf2, 0x4d, 0xdf, 0xe8, 0x7b, 0xf0,
610 			0x05, 0x3c, 0x33, 0x39, 0x43, 0x35, 0x83, 0x62,
611 		},
612 		.out_len = 16,
613 	},
614 	{
615 		.mode = NID_bf_cbc,
616 		.key = {
617 			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
618 			0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
619 		},
620 		.key_len = 16,
621 		.iv = {
622 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
623 		},
624 		.iv_len = 8,
625 		.in = {
626 			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
627 			0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
628 		},
629 		.in_len = 16,
630 		.out = {
631 			0x86, 0x6f, 0x5e, 0x72, 0xe5, 0x9a, 0x19, 0x51,
632 			0x56, 0xf3, 0x2f, 0x5e, 0x95, 0xfb, 0xd6, 0x52,
633 		},
634 		.out_len = 16,
635 	},
636 	{
637 		.mode = NID_bf_cbc,
638 		.key = {
639 			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
640 			0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
641 		},
642 		.key_len = 16,
643 		.iv = {
644 			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
645 		},
646 		.iv_len = 8,
647 		.in = {
648 			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
649 			0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
650 			0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
651 			0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
652 		},
653 		.in_len = 32,
654 		.out = {
655 			0xb9, 0x95, 0xf2, 0x4d, 0xdf, 0xe8, 0x7b, 0xf0,
656 			0x00, 0xf6, 0x2e, 0xf6, 0x6a, 0x03, 0x2d, 0x40,
657 			0x9c, 0xc9, 0x06, 0x31, 0x67, 0x7f, 0x6e, 0x24,
658 			0xeb, 0x2d, 0x3b, 0x02, 0xa3, 0x53, 0x52, 0xe9,
659 		},
660 		.out_len = 32,
661 	},
662 	{
663 		.mode = NID_bf_cbc,
664 		.key = {
665 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
666 		},
667 		.key_len = 8,
668 		.iv = {
669 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
670 		},
671 		.iv_len = 8,
672 		.in = {
673 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
674 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
675 		},
676 		.in_len = 16,
677 		.out = {
678 			0x4e, 0xf9, 0x97, 0x45, 0x61, 0x98, 0xdd, 0x78,
679 			0xe1, 0xc0, 0x30, 0xe7, 0x4c, 0x14, 0xd2, 0x61,
680 			0x8b, 0xa5, 0x5d, 0x18, 0x27, 0x44, 0x9c, 0xd3,
681 		},
682 		.out_len = 24,
683 		.padding = 1,
684 	},
685 	{
686 		.mode = NID_bf_cbc,
687 		.key = {
688 			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
689 		},
690 		.key_len = 8,
691 		.iv = {
692 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
693 		},
694 		.iv_len = 8,
695 		.in = {
696 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
697 			0x00, 0x00, 0x00, 0x00,
698 		},
699 		.in_len = 12,
700 		.out = {
701 			0xc0, 0x1f, 0xae, 0x76, 0x86, 0x86, 0xe7, 0xb7,
702 			0x3b, 0x0d, 0xd9, 0x72, 0x33, 0x2b, 0x38, 0x5d,
703 		},
704 		.out_len = 16,
705 		.padding = 1,
706 	},
707 
708 	/* CFB64 (generated using https://github.com/joshuasing/libressl-test-gen) */
709 	{
710 		.mode = NID_bf_cfb64,
711 		.key = {
712 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
713 		},
714 		.key_len = 8,
715 		.iv = {
716 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
717 		},
718 		.iv_len = 8,
719 		.in = {
720 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
721 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
722 		},
723 		.in_len = 16,
724 		.out = {
725 			0x4e, 0xf9, 0x97, 0x45, 0x61, 0x98, 0xdd, 0x78,
726 			0xe1, 0xc0, 0x30, 0xe7, 0x4c, 0x14, 0xd2, 0x61,
727 		},
728 		.out_len = 16,
729 	},
730 	{
731 		.mode = NID_bf_cfb64,
732 		.key = {
733 			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
734 			0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
735 		},
736 		.key_len = 16,
737 		.iv = {
738 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
739 		},
740 		.iv_len = 8,
741 		.in = {
742 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
743 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
744 		},
745 		.in_len = 16,
746 		.out = {
747 			0xb9, 0x95, 0xf2, 0x4d, 0xdf, 0xe8, 0x7b, 0xf0,
748 			0x05, 0x3c, 0x33, 0x39, 0x43, 0x35, 0x83, 0x62,
749 		},
750 		.out_len = 16,
751 	},
752 	{
753 		.mode = NID_bf_cfb64,
754 		.key = {
755 			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
756 			0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
757 		},
758 		.key_len = 16,
759 		.iv = {
760 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
761 		},
762 		.iv_len = 8,
763 		.in = {
764 			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
765 			0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
766 		},
767 		.in_len = 16,
768 		.out = {
769 			0xb9, 0x94, 0xf0, 0x4e, 0xdb, 0xed, 0x7d, 0xf7,
770 			0x0a, 0xf8, 0x96, 0xbf, 0x4d, 0x3c, 0x95, 0xdf,
771 		},
772 		.out_len = 16,
773 	},
774 	{
775 		.mode = NID_bf_cfb64,
776 		.key = {
777 			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
778 			0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
779 		},
780 		.key_len = 16,
781 		.iv = {
782 			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
783 		},
784 		.iv_len = 8,
785 		.in = {
786 			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
787 			0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
788 			0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
789 			0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
790 		},
791 		.in_len = 32,
792 		.out = {
793 			0x86, 0x6e, 0x5c, 0x71, 0xe1, 0x9f, 0x1f, 0x56,
794 			0x1f, 0x02, 0xaa, 0x8c, 0x09, 0xe0, 0x61, 0x43,
795 			0x91, 0x8d, 0xd2, 0x43, 0x70, 0x5d, 0xa3, 0xf1,
796 			0xc7, 0x96, 0x56, 0x77, 0xfc, 0x33, 0x74, 0x9e,
797 		},
798 		.out_len = 32,
799 	},
800 	{
801 		.mode = NID_bf_cfb64,
802 		.key = {
803 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
804 		},
805 		.key_len = 8,
806 		.iv = {
807 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
808 		},
809 		.iv_len = 8,
810 		.in = {
811 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
812 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
813 		},
814 		.in_len = 16,
815 		.out = {
816 			0x4e, 0xf9, 0x97, 0x45, 0x61, 0x98, 0xdd, 0x78,
817 			0xe1, 0xc0, 0x30, 0xe7, 0x4c, 0x14, 0xd2, 0x61,
818 		},
819 		.out_len = 16,
820 	},
821 	{
822 		.mode = NID_bf_cfb64,
823 		.key = {
824 			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
825 		},
826 		.key_len = 8,
827 		.iv = {
828 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
829 		},
830 		.iv_len = 8,
831 		.in = {
832 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
833 			0x00, 0x00, 0x00, 0x00,
834 		},
835 		.in_len = 12,
836 		.out = {
837 			0xc0, 0x1f, 0xae, 0x76, 0x86, 0x86, 0xe7, 0xb7,
838 			0x05, 0xbb, 0xd4, 0x5e,
839 		},
840 		.out_len = 12,
841 	},
842 
843 	/* OFB64 (generated using https://github.com/joshuasing/libressl-test-gen) */
844 	{
845 		.mode = NID_bf_ofb64,
846 		.key = {
847 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
848 		},
849 		.key_len = 8,
850 		.iv = {
851 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
852 		},
853 		.iv_len = 8,
854 		.in = {
855 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
856 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
857 		},
858 		.in_len = 16,
859 		.out = {
860 			0x4e, 0xf9, 0x97, 0x45, 0x61, 0x98, 0xdd, 0x78,
861 			0xe1, 0xc0, 0x30, 0xe7, 0x4c, 0x14, 0xd2, 0x61,
862 		},
863 		.out_len = 16,
864 	},
865 	{
866 		.mode = NID_bf_ofb64,
867 		.key = {
868 			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
869 			0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
870 		},
871 		.key_len = 16,
872 		.iv = {
873 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
874 		},
875 		.iv_len = 8,
876 		.in = {
877 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
878 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
879 		},
880 		.in_len = 16,
881 		.out = {
882 			0xb9, 0x95, 0xf2, 0x4d, 0xdf, 0xe8, 0x7b, 0xf0,
883 			0x05, 0x3c, 0x33, 0x39, 0x43, 0x35, 0x83, 0x62,
884 		},
885 		.out_len = 16,
886 	},
887 	{
888 		.mode = NID_bf_ofb64,
889 		.key = {
890 			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
891 			0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
892 		},
893 		.key_len = 16,
894 		.iv = {
895 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
896 		},
897 		.iv_len = 8,
898 		.in = {
899 			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
900 			0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
901 		},
902 		.in_len = 16,
903 		.out = {
904 			0xb9, 0x94, 0xf0, 0x4e, 0xdb, 0xed, 0x7d, 0xf7,
905 			0x0d, 0x35, 0x39, 0x32, 0x4f, 0x38, 0x8d, 0x6d,
906 		},
907 		.out_len = 16,
908 	},
909 	{
910 		.mode = NID_bf_ofb64,
911 		.key = {
912 			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
913 			0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
914 		},
915 		.key_len = 16,
916 		.iv = {
917 			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
918 		},
919 		.iv_len = 8,
920 		.in = {
921 			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
922 			0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
923 			0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
924 			0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
925 		},
926 		.in_len = 32,
927 		.out = {
928 			0x86, 0x6e, 0x5c, 0x71, 0xe1, 0x9f, 0x1f, 0x56,
929 			0xbb, 0xcb, 0xd9, 0x35, 0x81, 0x57, 0xea, 0xb9,
930 			0xd7, 0x85, 0x28, 0x4a, 0xdc, 0xeb, 0x94, 0x99,
931 			0xf0, 0x87, 0x7c, 0x5a, 0x56, 0x60, 0xc7, 0x60,
932 		},
933 		.out_len = 32,
934 	},
935 	{
936 		.mode = NID_bf_ofb64,
937 		.key = {
938 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
939 		},
940 		.key_len = 8,
941 		.iv = {
942 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
943 		},
944 		.iv_len = 8,
945 		.in = {
946 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
947 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
948 		},
949 		.in_len = 16,
950 		.out = {
951 			0x4e, 0xf9, 0x97, 0x45, 0x61, 0x98, 0xdd, 0x78,
952 			0xe1, 0xc0, 0x30, 0xe7, 0x4c, 0x14, 0xd2, 0x61,
953 		},
954 		.out_len = 16,
955 	},
956 	{
957 		.mode = NID_bf_ofb64,
958 		.key = {
959 			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
960 		},
961 		.key_len = 8,
962 		.iv = {
963 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
964 		},
965 		.iv_len = 8,
966 		.in = {
967 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
968 			0x00, 0x00, 0x00, 0x00,
969 		},
970 		.in_len = 12,
971 		.out = {
972 			0xc0, 0x1f, 0xae, 0x76, 0x86, 0x86, 0xe7, 0xb7,
973 			0x05, 0xbb, 0xd4, 0x5e,
974 		},
975 		.out_len = 12,
976 	},
977 };
978 
979 #define N_BF_TESTS (sizeof(bf_tests) / sizeof(bf_tests[0]))
980 
981 static int
982 bf_ecb_test(size_t test_number, const struct bf_test *bt)
983 {
984 	BF_KEY key;
985 	uint8_t out[8];
986 
987 	if (bt->padding) {
988 		/* XXX - Handle padding */
989 		return 1;
990 	}
991 
992 	/* Encryption */
993 	memset(out, 0, sizeof(out));
994 	BF_set_key(&key, bt->key_len, bt->key);
995 	BF_ecb_encrypt(bt->in, out, &key, 1);
996 
997 	if (memcmp(bt->out, out, bt->out_len) != 0) {
998 		fprintf(stderr, "FAIL (%s:%zu): encryption mismatch\n",
999 		    SN_bf_ecb, test_number);
1000 		return 0;
1001 	}
1002 
1003 	/* Decryption */
1004 	memset(out, 0, sizeof(out));
1005 	BF_set_key(&key, bt->key_len, bt->key);
1006 	BF_ecb_encrypt(bt->out, out, &key, 0);
1007 
1008 	if (memcmp(bt->in, out, bt->in_len) != 0) {
1009 		fprintf(stderr, "FAIL (%s:%zu): decryption mismatch\n",
1010 		    SN_bf_ecb, test_number);
1011 		return 0;
1012 	}
1013 
1014 	return 1;
1015 }
1016 
1017 static int
1018 bf_cbc_test(size_t test_number, const struct bf_test *bt)
1019 {
1020 	BF_KEY key;
1021 	uint8_t out[512];
1022 	uint8_t iv[64];
1023 
1024 	if (bt->padding) {
1025 		/* XXX - Handle padding */
1026 		return 1;
1027 	}
1028 
1029 	/* Encryption */
1030 	memset(out, 0, sizeof(out));
1031 	memcpy(iv, bt->iv, bt->iv_len);
1032 	BF_set_key(&key, bt->key_len, bt->key);
1033 	BF_cbc_encrypt(bt->in, out, bt->in_len, &key, iv, 1);
1034 
1035 	if (memcmp(bt->out, out, bt->out_len) != 0) {
1036 		fprintf(stderr, "FAIL (%s:%zu): encryption mismatch\n",
1037 		    SN_bf_cbc, test_number);
1038 		return 0;
1039 	}
1040 
1041 	/* Decryption */
1042 	memset(out, 0, sizeof(out));
1043 	memcpy(iv, bt->iv, bt->iv_len);
1044 	BF_set_key(&key, bt->key_len, bt->key);
1045 	BF_cbc_encrypt(bt->out, out, bt->out_len, &key, iv, 0);
1046 
1047 	if (memcmp(bt->in, out, bt->in_len) != 0) {
1048 		fprintf(stderr, "FAIL (%s:%zu): decryption mismatch\n",
1049 		    SN_bf_cbc, test_number);
1050 		return 0;
1051 	}
1052 
1053 	return 1;
1054 }
1055 
1056 static int
1057 bf_cfb64_test(size_t test_number, const struct bf_test *bt)
1058 {
1059 	BF_KEY key;
1060 	uint8_t out[512];
1061 	uint8_t iv[64];
1062 	int remainder = 0;
1063 
1064 	if (bt->padding) {
1065 		/* XXX - Handle padding */
1066 		return 1;
1067 	}
1068 
1069 	/* Encryption */
1070 	memset(out, 0, sizeof(out));
1071 	memcpy(iv, bt->iv, bt->iv_len);
1072 	BF_set_key(&key, bt->key_len, bt->key);
1073 	BF_cfb64_encrypt(bt->in, out, bt->in_len * 8, &key, iv, &remainder, 1);
1074 
1075 	if (memcmp(bt->out, out, bt->out_len) != 0) {
1076 		fprintf(stderr, "FAIL (%s:%zu): encryption mismatch\n",
1077 		    SN_bf_cfb64, test_number);
1078 		return 0;
1079 	}
1080 
1081 	/* Decryption */
1082 	remainder = 0;
1083 	memset(out, 0, sizeof(out));
1084 	memcpy(iv, bt->iv, bt->iv_len);
1085 	BF_set_key(&key, bt->key_len, bt->key);
1086 	BF_cfb64_encrypt(bt->out, out, bt->out_len, &key, iv, &remainder, 0);
1087 
1088 	if (memcmp(bt->in, out, bt->in_len) != 0) {
1089 		fprintf(stderr, "FAIL (%s:%zu): decryption mismatch\n",
1090 		    SN_bf_cfb64, test_number);
1091 		return 0;
1092 	}
1093 
1094 	return 1;
1095 }
1096 
1097 static int
1098 bf_ofb64_test(size_t test_number, const struct bf_test *bt)
1099 {
1100 	BF_KEY key;
1101 	uint8_t out[512];
1102 	uint8_t iv[64];
1103 	int remainder = 0;
1104 
1105 	if (bt->padding) {
1106 		/* XXX - Handle padding */
1107 		return 1;
1108 	}
1109 
1110 	/* Encryption */
1111 	memset(out, 0, sizeof(out));
1112 	memcpy(iv, bt->iv, bt->iv_len);
1113 	BF_set_key(&key, bt->key_len, bt->key);
1114 	BF_ofb64_encrypt(bt->in, out, bt->in_len, &key, iv, &remainder);
1115 
1116 	if (memcmp(bt->out, out, bt->out_len) != 0) {
1117 		fprintf(stderr, "FAIL (%s:%zu): encryption mismatch\n",
1118 		    SN_bf_ofb64, test_number);
1119 		return 0;
1120 	}
1121 
1122 	/* Decryption */
1123 	remainder = 0;
1124 	memset(out, 0, sizeof(out));
1125 	memcpy(iv, bt->iv, bt->iv_len);
1126 	BF_set_key(&key, bt->key_len, bt->key);
1127 	BF_ofb64_encrypt(bt->out, out, bt->out_len, &key, iv, &remainder);
1128 
1129 	if (memcmp(bt->in, out, bt->in_len) != 0) {
1130 		fprintf(stderr, "FAIL (%s:%zu): decryption mismatch\n",
1131 		    SN_bf_ofb64, test_number);
1132 		return 0;
1133 	}
1134 
1135 	return 1;
1136 }
1137 
1138 static int
1139 bf_evp_test(size_t test_number, const struct bf_test *bt, const char *label,
1140     const EVP_CIPHER *cipher)
1141 {
1142 	EVP_CIPHER_CTX *ctx;
1143 	uint8_t out[512];
1144 	int in_len, out_len, total_len;
1145 	int i;
1146 	int success = 0;
1147 
1148 	if ((ctx = EVP_CIPHER_CTX_new()) == NULL) {
1149 		fprintf(stderr, "FAIL (%s:%zu): EVP_CIPHER_CTX_new failed\n",
1150 		    label, test_number);
1151 		goto failed;
1152 	}
1153 
1154 	/* EVP encryption */
1155 	total_len = 0;
1156 	memset(out, 0, sizeof(out));
1157 	if (!EVP_EncryptInit(ctx, cipher, NULL, NULL)) {
1158 		fprintf(stderr, "FAIL (%s:%zu): EVP_EncryptInit failed\n",
1159 		    label, test_number);
1160 		goto failed;
1161 	}
1162 
1163 	if (!EVP_CIPHER_CTX_set_key_length(ctx, bt->key_len)) {
1164 		fprintf(stderr,
1165 		    "FAIL (%s:%zu): EVP_CIPHER_CTX_set_key_length failed\n",
1166 		    label, test_number);
1167 		goto failed;
1168 	}
1169 
1170 	if (!EVP_CIPHER_CTX_set_padding(ctx, bt->padding)) {
1171 		fprintf(stderr,
1172 		    "FAIL (%s:%zu): EVP_CIPHER_CTX_set_padding failed\n",
1173 		    label, test_number);
1174 		goto failed;
1175 	}
1176 
1177 	if (!EVP_EncryptInit(ctx, NULL, bt->key, bt->iv)) {
1178 		fprintf(stderr, "FAIL (%s:%zu): EVP_EncryptInit failed\n",
1179 		    label, test_number);
1180 		goto failed;
1181 	}
1182 
1183 	for (i = 0; i < bt->in_len;) {
1184 		in_len = arc4random_uniform(bt->in_len / 2);
1185 		if (in_len > bt->in_len - i)
1186 			in_len = bt->in_len - i;
1187 
1188 		if (!EVP_EncryptUpdate(ctx, out + total_len, &out_len,
1189 		    bt->in + i, in_len)) {
1190 			fprintf(stderr,
1191 			    "FAIL (%s:%zu): EVP_EncryptUpdate failed\n",
1192 			    label, test_number);
1193 			goto failed;
1194 		}
1195 
1196 		i += in_len;
1197 		total_len += out_len;
1198 	}
1199 
1200 	if (!EVP_EncryptFinal_ex(ctx, out + total_len, &out_len)) {
1201 		fprintf(stderr, "FAIL (%s:%zu): EVP_EncryptFinal_ex failed\n",
1202 		    label, test_number);
1203 		goto failed;
1204 	}
1205 	total_len += out_len;
1206 
1207 	if (!EVP_CIPHER_CTX_reset(ctx)) {
1208 		fprintf(stderr,
1209 		    "FAIL (%s:%zu): EVP_CIPHER_CTX_reset failed\n",
1210 		    label, test_number);
1211 		goto failed;
1212 	}
1213 
1214 	if (total_len != bt->out_len) {
1215 		fprintf(stderr,
1216 		    "FAIL (%s:%zu): EVP encryption length mismatch "
1217 		    "(%d != %d)\n", label, test_number, total_len, bt->out_len);
1218 		goto failed;
1219 	}
1220 
1221 	if (memcmp(bt->out, out, bt->out_len) != 0) {
1222 		fprintf(stderr, "FAIL (%s:%zu): EVP encryption mismatch\n",
1223 		    label, test_number);
1224 		goto failed;
1225 	}
1226 
1227 	/* EVP decryption */
1228 	total_len = 0;
1229 	memset(out, 0, sizeof(out));
1230 	if (!EVP_DecryptInit(ctx, cipher, NULL, NULL)) {
1231 		fprintf(stderr, "FAIL (%s:%zu): EVP_DecryptInit failed\n",
1232 		    label, test_number);
1233 		goto failed;
1234 	}
1235 
1236 	if (!EVP_CIPHER_CTX_set_key_length(ctx, bt->key_len)) {
1237 		fprintf(stderr,
1238 		    "FAIL (%s:%zu): EVP_CIPHER_CTX_set_key_length failed\n",
1239 		    label, test_number);
1240 		goto failed;
1241 	}
1242 
1243 	if (!EVP_CIPHER_CTX_set_padding(ctx, bt->padding)) {
1244 		fprintf(stderr,
1245 		    "FAIL (%s:%zu): EVP_CIPHER_CTX_set_padding failed\n",
1246 		    label, test_number);
1247 		goto failed;
1248 	}
1249 
1250 	if (!EVP_DecryptInit(ctx, NULL, bt->key, bt->iv)) {
1251 		fprintf(stderr, "FAIL (%s:%zu): EVP_DecryptInit failed\n",
1252 		    label, test_number);
1253 		goto failed;
1254 	}
1255 
1256 	for (i = 0; i < bt->out_len;) {
1257 		in_len = arc4random_uniform(bt->out_len / 2);
1258 		if (in_len > bt->out_len - i)
1259 			in_len = bt->out_len - i;
1260 
1261 		if (!EVP_DecryptUpdate(ctx, out + total_len, &out_len,
1262 		    bt->out + i, in_len)) {
1263 			fprintf(stderr,
1264 			    "FAIL (%s:%zu): EVP_DecryptUpdate failed\n",
1265 			    label, test_number);
1266 			goto failed;
1267 		}
1268 
1269 		i += in_len;
1270 		total_len += out_len;
1271 	}
1272 
1273 	if (!EVP_DecryptFinal_ex(ctx, out + total_len, &out_len)) {
1274 		fprintf(stderr, "FAIL (%s:%zu): EVP_DecryptFinal_ex failed\n",
1275 		    label, test_number);
1276 		goto failed;
1277 	}
1278 	total_len += out_len;
1279 
1280 	if (!EVP_CIPHER_CTX_reset(ctx)) {
1281 		fprintf(stderr,
1282 		    "FAIL (%s:%zu): EVP_CIPHER_CTX_reset failed\n",
1283 		    label, test_number);
1284 		goto failed;
1285 	}
1286 
1287 	if (total_len != bt->in_len) {
1288 		fprintf(stderr,
1289 		    "FAIL (%s:%zu): EVP decryption length mismatch\n",
1290 		    label, test_number);
1291 		goto failed;
1292 	}
1293 
1294 	if (memcmp(bt->in, out, bt->in_len) != 0) {
1295 		fprintf(stderr, "FAIL (%s:%zu): EVP decryption mismatch\n",
1296 		    label, test_number);
1297 		goto failed;
1298 	}
1299 
1300 	success = 1;
1301 
1302  failed:
1303 	EVP_CIPHER_CTX_free(ctx);
1304 	return success;
1305 }
1306 
1307 static int
1308 bf_test(void)
1309 {
1310 	const struct bf_test *bt;
1311 	const char *label;
1312 	const EVP_CIPHER *cipher;
1313 	size_t i;
1314 	int failed = 1;
1315 
1316 	for (i = 0; i < N_BF_TESTS; i++) {
1317 		bt = &bf_tests[i];
1318 		switch (bt->mode) {
1319 		case NID_bf_ecb:
1320 			label = SN_bf_ecb;
1321 			cipher = EVP_bf_ecb();
1322 			if (!bf_ecb_test(i, bt))
1323 				goto failed;
1324 			break;
1325 		case NID_bf_cbc:
1326 			label = SN_bf_cbc;
1327 			cipher = EVP_bf_cbc();
1328 			if (!bf_cbc_test(i, bt))
1329 				goto failed;
1330 			break;
1331 		case NID_bf_cfb64:
1332 			label = SN_bf_cfb64;
1333 			cipher = EVP_bf_cfb64();
1334 			if (!bf_cfb64_test(i, bt))
1335 				goto failed;
1336 			break;
1337 		case NID_bf_ofb64:
1338 			label = SN_bf_ofb64;
1339 			cipher = EVP_bf_ofb();
1340 			if (!bf_ofb64_test(i, bt))
1341 				goto failed;
1342 			break;
1343 		default:
1344 			fprintf(stderr, "FAIL: unknown mode (%d)\n",
1345 			    bt->mode);
1346 			goto failed;
1347 		}
1348 
1349 		if (!bf_evp_test(i, bt, label, cipher))
1350 			goto failed;
1351 	}
1352 
1353 	failed = 0;
1354 
1355  failed:
1356 	return failed;
1357 }
1358 
1359 int
1360 main(int argc, char **argv)
1361 {
1362 	int failed = 0;
1363 
1364 	failed |= bf_test();
1365 
1366 	return failed;
1367 }
1368 
1369