xref: /openbmc/linux/crypto/testmgr.c (revision f3a8b664)
1 /*
2  * Algorithm testing framework and tests.
3  *
4  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5  * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
6  * Copyright (c) 2007 Nokia Siemens Networks
7  * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
8  *
9  * Updated RFC4106 AES-GCM testing.
10  *    Authors: Aidan O'Mahony (aidan.o.mahony@intel.com)
11  *             Adrian Hoban <adrian.hoban@intel.com>
12  *             Gabriele Paoloni <gabriele.paoloni@intel.com>
13  *             Tadeusz Struk (tadeusz.struk@intel.com)
14  *    Copyright (c) 2010, Intel Corporation.
15  *
16  * This program is free software; you can redistribute it and/or modify it
17  * under the terms of the GNU General Public License as published by the Free
18  * Software Foundation; either version 2 of the License, or (at your option)
19  * any later version.
20  *
21  */
22 
23 #include <crypto/aead.h>
24 #include <crypto/hash.h>
25 #include <crypto/skcipher.h>
26 #include <linux/err.h>
27 #include <linux/fips.h>
28 #include <linux/module.h>
29 #include <linux/scatterlist.h>
30 #include <linux/slab.h>
31 #include <linux/string.h>
32 #include <crypto/rng.h>
33 #include <crypto/drbg.h>
34 #include <crypto/akcipher.h>
35 #include <crypto/kpp.h>
36 
37 #include "internal.h"
38 
39 static bool notests;
40 module_param(notests, bool, 0644);
41 MODULE_PARM_DESC(notests, "disable crypto self-tests");
42 
43 #ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
44 
45 /* a perfect nop */
46 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
47 {
48 	return 0;
49 }
50 
51 #else
52 
53 #include "testmgr.h"
54 
55 /*
56  * Need slab memory for testing (size in number of pages).
57  */
58 #define XBUFSIZE	8
59 
60 /*
61  * Indexes into the xbuf to simulate cross-page access.
62  */
63 #define IDX1		32
64 #define IDX2		32400
65 #define IDX3		1
66 #define IDX4		8193
67 #define IDX5		22222
68 #define IDX6		17101
69 #define IDX7		27333
70 #define IDX8		3000
71 
72 /*
73 * Used by test_cipher()
74 */
75 #define ENCRYPT 1
76 #define DECRYPT 0
77 
78 struct tcrypt_result {
79 	struct completion completion;
80 	int err;
81 };
82 
83 struct aead_test_suite {
84 	struct {
85 		struct aead_testvec *vecs;
86 		unsigned int count;
87 	} enc, dec;
88 };
89 
90 struct cipher_test_suite {
91 	struct {
92 		struct cipher_testvec *vecs;
93 		unsigned int count;
94 	} enc, dec;
95 };
96 
97 struct comp_test_suite {
98 	struct {
99 		struct comp_testvec *vecs;
100 		unsigned int count;
101 	} comp, decomp;
102 };
103 
104 struct hash_test_suite {
105 	struct hash_testvec *vecs;
106 	unsigned int count;
107 };
108 
109 struct cprng_test_suite {
110 	struct cprng_testvec *vecs;
111 	unsigned int count;
112 };
113 
114 struct drbg_test_suite {
115 	struct drbg_testvec *vecs;
116 	unsigned int count;
117 };
118 
119 struct akcipher_test_suite {
120 	struct akcipher_testvec *vecs;
121 	unsigned int count;
122 };
123 
124 struct kpp_test_suite {
125 	struct kpp_testvec *vecs;
126 	unsigned int count;
127 };
128 
129 struct alg_test_desc {
130 	const char *alg;
131 	int (*test)(const struct alg_test_desc *desc, const char *driver,
132 		    u32 type, u32 mask);
133 	int fips_allowed;	/* set if alg is allowed in fips mode */
134 
135 	union {
136 		struct aead_test_suite aead;
137 		struct cipher_test_suite cipher;
138 		struct comp_test_suite comp;
139 		struct hash_test_suite hash;
140 		struct cprng_test_suite cprng;
141 		struct drbg_test_suite drbg;
142 		struct akcipher_test_suite akcipher;
143 		struct kpp_test_suite kpp;
144 	} suite;
145 };
146 
147 static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
148 
149 static void hexdump(unsigned char *buf, unsigned int len)
150 {
151 	print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
152 			16, 1,
153 			buf, len, false);
154 }
155 
156 static void tcrypt_complete(struct crypto_async_request *req, int err)
157 {
158 	struct tcrypt_result *res = req->data;
159 
160 	if (err == -EINPROGRESS)
161 		return;
162 
163 	res->err = err;
164 	complete(&res->completion);
165 }
166 
167 static int testmgr_alloc_buf(char *buf[XBUFSIZE])
168 {
169 	int i;
170 
171 	for (i = 0; i < XBUFSIZE; i++) {
172 		buf[i] = (void *)__get_free_page(GFP_KERNEL);
173 		if (!buf[i])
174 			goto err_free_buf;
175 	}
176 
177 	return 0;
178 
179 err_free_buf:
180 	while (i-- > 0)
181 		free_page((unsigned long)buf[i]);
182 
183 	return -ENOMEM;
184 }
185 
186 static void testmgr_free_buf(char *buf[XBUFSIZE])
187 {
188 	int i;
189 
190 	for (i = 0; i < XBUFSIZE; i++)
191 		free_page((unsigned long)buf[i]);
192 }
193 
194 static int wait_async_op(struct tcrypt_result *tr, int ret)
195 {
196 	if (ret == -EINPROGRESS || ret == -EBUSY) {
197 		wait_for_completion(&tr->completion);
198 		reinit_completion(&tr->completion);
199 		ret = tr->err;
200 	}
201 	return ret;
202 }
203 
204 static int ahash_partial_update(struct ahash_request **preq,
205 	struct crypto_ahash *tfm, struct hash_testvec *template,
206 	void *hash_buff, int k, int temp, struct scatterlist *sg,
207 	const char *algo, char *result, struct tcrypt_result *tresult)
208 {
209 	char *state;
210 	struct ahash_request *req;
211 	int statesize, ret = -EINVAL;
212 	const char guard[] = { 0x00, 0xba, 0xad, 0x00 };
213 
214 	req = *preq;
215 	statesize = crypto_ahash_statesize(
216 			crypto_ahash_reqtfm(req));
217 	state = kmalloc(statesize + sizeof(guard), GFP_KERNEL);
218 	if (!state) {
219 		pr_err("alt: hash: Failed to alloc state for %s\n", algo);
220 		goto out_nostate;
221 	}
222 	memcpy(state + statesize, guard, sizeof(guard));
223 	ret = crypto_ahash_export(req, state);
224 	WARN_ON(memcmp(state + statesize, guard, sizeof(guard)));
225 	if (ret) {
226 		pr_err("alt: hash: Failed to export() for %s\n", algo);
227 		goto out;
228 	}
229 	ahash_request_free(req);
230 	req = ahash_request_alloc(tfm, GFP_KERNEL);
231 	if (!req) {
232 		pr_err("alg: hash: Failed to alloc request for %s\n", algo);
233 		goto out_noreq;
234 	}
235 	ahash_request_set_callback(req,
236 		CRYPTO_TFM_REQ_MAY_BACKLOG,
237 		tcrypt_complete, tresult);
238 
239 	memcpy(hash_buff, template->plaintext + temp,
240 		template->tap[k]);
241 	sg_init_one(&sg[0], hash_buff, template->tap[k]);
242 	ahash_request_set_crypt(req, sg, result, template->tap[k]);
243 	ret = crypto_ahash_import(req, state);
244 	if (ret) {
245 		pr_err("alg: hash: Failed to import() for %s\n", algo);
246 		goto out;
247 	}
248 	ret = wait_async_op(tresult, crypto_ahash_update(req));
249 	if (ret)
250 		goto out;
251 	*preq = req;
252 	ret = 0;
253 	goto out_noreq;
254 out:
255 	ahash_request_free(req);
256 out_noreq:
257 	kfree(state);
258 out_nostate:
259 	return ret;
260 }
261 
262 static int __test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
263 		       unsigned int tcount, bool use_digest,
264 		       const int align_offset)
265 {
266 	const char *algo = crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
267 	unsigned int i, j, k, temp;
268 	struct scatterlist sg[8];
269 	char *result;
270 	char *key;
271 	struct ahash_request *req;
272 	struct tcrypt_result tresult;
273 	void *hash_buff;
274 	char *xbuf[XBUFSIZE];
275 	int ret = -ENOMEM;
276 
277 	result = kmalloc(MAX_DIGEST_SIZE, GFP_KERNEL);
278 	if (!result)
279 		return ret;
280 	key = kmalloc(MAX_KEYLEN, GFP_KERNEL);
281 	if (!key)
282 		goto out_nobuf;
283 	if (testmgr_alloc_buf(xbuf))
284 		goto out_nobuf;
285 
286 	init_completion(&tresult.completion);
287 
288 	req = ahash_request_alloc(tfm, GFP_KERNEL);
289 	if (!req) {
290 		printk(KERN_ERR "alg: hash: Failed to allocate request for "
291 		       "%s\n", algo);
292 		goto out_noreq;
293 	}
294 	ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
295 				   tcrypt_complete, &tresult);
296 
297 	j = 0;
298 	for (i = 0; i < tcount; i++) {
299 		if (template[i].np)
300 			continue;
301 
302 		ret = -EINVAL;
303 		if (WARN_ON(align_offset + template[i].psize > PAGE_SIZE))
304 			goto out;
305 
306 		j++;
307 		memset(result, 0, MAX_DIGEST_SIZE);
308 
309 		hash_buff = xbuf[0];
310 		hash_buff += align_offset;
311 
312 		memcpy(hash_buff, template[i].plaintext, template[i].psize);
313 		sg_init_one(&sg[0], hash_buff, template[i].psize);
314 
315 		if (template[i].ksize) {
316 			crypto_ahash_clear_flags(tfm, ~0);
317 			if (template[i].ksize > MAX_KEYLEN) {
318 				pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
319 				       j, algo, template[i].ksize, MAX_KEYLEN);
320 				ret = -EINVAL;
321 				goto out;
322 			}
323 			memcpy(key, template[i].key, template[i].ksize);
324 			ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
325 			if (ret) {
326 				printk(KERN_ERR "alg: hash: setkey failed on "
327 				       "test %d for %s: ret=%d\n", j, algo,
328 				       -ret);
329 				goto out;
330 			}
331 		}
332 
333 		ahash_request_set_crypt(req, sg, result, template[i].psize);
334 		if (use_digest) {
335 			ret = wait_async_op(&tresult, crypto_ahash_digest(req));
336 			if (ret) {
337 				pr_err("alg: hash: digest failed on test %d "
338 				       "for %s: ret=%d\n", j, algo, -ret);
339 				goto out;
340 			}
341 		} else {
342 			ret = wait_async_op(&tresult, crypto_ahash_init(req));
343 			if (ret) {
344 				pr_err("alt: hash: init failed on test %d "
345 				       "for %s: ret=%d\n", j, algo, -ret);
346 				goto out;
347 			}
348 			ret = wait_async_op(&tresult, crypto_ahash_update(req));
349 			if (ret) {
350 				pr_err("alt: hash: update failed on test %d "
351 				       "for %s: ret=%d\n", j, algo, -ret);
352 				goto out;
353 			}
354 			ret = wait_async_op(&tresult, crypto_ahash_final(req));
355 			if (ret) {
356 				pr_err("alt: hash: final failed on test %d "
357 				       "for %s: ret=%d\n", j, algo, -ret);
358 				goto out;
359 			}
360 		}
361 
362 		if (memcmp(result, template[i].digest,
363 			   crypto_ahash_digestsize(tfm))) {
364 			printk(KERN_ERR "alg: hash: Test %d failed for %s\n",
365 			       j, algo);
366 			hexdump(result, crypto_ahash_digestsize(tfm));
367 			ret = -EINVAL;
368 			goto out;
369 		}
370 	}
371 
372 	j = 0;
373 	for (i = 0; i < tcount; i++) {
374 		/* alignment tests are only done with continuous buffers */
375 		if (align_offset != 0)
376 			break;
377 
378 		if (!template[i].np)
379 			continue;
380 
381 		j++;
382 		memset(result, 0, MAX_DIGEST_SIZE);
383 
384 		temp = 0;
385 		sg_init_table(sg, template[i].np);
386 		ret = -EINVAL;
387 		for (k = 0; k < template[i].np; k++) {
388 			if (WARN_ON(offset_in_page(IDX[k]) +
389 				    template[i].tap[k] > PAGE_SIZE))
390 				goto out;
391 			sg_set_buf(&sg[k],
392 				   memcpy(xbuf[IDX[k] >> PAGE_SHIFT] +
393 					  offset_in_page(IDX[k]),
394 					  template[i].plaintext + temp,
395 					  template[i].tap[k]),
396 				   template[i].tap[k]);
397 			temp += template[i].tap[k];
398 		}
399 
400 		if (template[i].ksize) {
401 			if (template[i].ksize > MAX_KEYLEN) {
402 				pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
403 				       j, algo, template[i].ksize, MAX_KEYLEN);
404 				ret = -EINVAL;
405 				goto out;
406 			}
407 			crypto_ahash_clear_flags(tfm, ~0);
408 			memcpy(key, template[i].key, template[i].ksize);
409 			ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
410 
411 			if (ret) {
412 				printk(KERN_ERR "alg: hash: setkey "
413 				       "failed on chunking test %d "
414 				       "for %s: ret=%d\n", j, algo, -ret);
415 				goto out;
416 			}
417 		}
418 
419 		ahash_request_set_crypt(req, sg, result, template[i].psize);
420 		ret = crypto_ahash_digest(req);
421 		switch (ret) {
422 		case 0:
423 			break;
424 		case -EINPROGRESS:
425 		case -EBUSY:
426 			wait_for_completion(&tresult.completion);
427 			reinit_completion(&tresult.completion);
428 			ret = tresult.err;
429 			if (!ret)
430 				break;
431 			/* fall through */
432 		default:
433 			printk(KERN_ERR "alg: hash: digest failed "
434 			       "on chunking test %d for %s: "
435 			       "ret=%d\n", j, algo, -ret);
436 			goto out;
437 		}
438 
439 		if (memcmp(result, template[i].digest,
440 			   crypto_ahash_digestsize(tfm))) {
441 			printk(KERN_ERR "alg: hash: Chunking test %d "
442 			       "failed for %s\n", j, algo);
443 			hexdump(result, crypto_ahash_digestsize(tfm));
444 			ret = -EINVAL;
445 			goto out;
446 		}
447 	}
448 
449 	/* partial update exercise */
450 	j = 0;
451 	for (i = 0; i < tcount; i++) {
452 		/* alignment tests are only done with continuous buffers */
453 		if (align_offset != 0)
454 			break;
455 
456 		if (template[i].np < 2)
457 			continue;
458 
459 		j++;
460 		memset(result, 0, MAX_DIGEST_SIZE);
461 
462 		ret = -EINVAL;
463 		hash_buff = xbuf[0];
464 		memcpy(hash_buff, template[i].plaintext,
465 			template[i].tap[0]);
466 		sg_init_one(&sg[0], hash_buff, template[i].tap[0]);
467 
468 		if (template[i].ksize) {
469 			crypto_ahash_clear_flags(tfm, ~0);
470 			if (template[i].ksize > MAX_KEYLEN) {
471 				pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
472 					j, algo, template[i].ksize, MAX_KEYLEN);
473 				ret = -EINVAL;
474 				goto out;
475 			}
476 			memcpy(key, template[i].key, template[i].ksize);
477 			ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
478 			if (ret) {
479 				pr_err("alg: hash: setkey failed on test %d for %s: ret=%d\n",
480 					j, algo, -ret);
481 				goto out;
482 			}
483 		}
484 
485 		ahash_request_set_crypt(req, sg, result, template[i].tap[0]);
486 		ret = wait_async_op(&tresult, crypto_ahash_init(req));
487 		if (ret) {
488 			pr_err("alt: hash: init failed on test %d for %s: ret=%d\n",
489 				j, algo, -ret);
490 			goto out;
491 		}
492 		ret = wait_async_op(&tresult, crypto_ahash_update(req));
493 		if (ret) {
494 			pr_err("alt: hash: update failed on test %d for %s: ret=%d\n",
495 				j, algo, -ret);
496 			goto out;
497 		}
498 
499 		temp = template[i].tap[0];
500 		for (k = 1; k < template[i].np; k++) {
501 			ret = ahash_partial_update(&req, tfm, &template[i],
502 				hash_buff, k, temp, &sg[0], algo, result,
503 				&tresult);
504 			if (ret) {
505 				pr_err("hash: partial update failed on test %d for %s: ret=%d\n",
506 					j, algo, -ret);
507 				goto out_noreq;
508 			}
509 			temp += template[i].tap[k];
510 		}
511 		ret = wait_async_op(&tresult, crypto_ahash_final(req));
512 		if (ret) {
513 			pr_err("alt: hash: final failed on test %d for %s: ret=%d\n",
514 				j, algo, -ret);
515 			goto out;
516 		}
517 		if (memcmp(result, template[i].digest,
518 			   crypto_ahash_digestsize(tfm))) {
519 			pr_err("alg: hash: Partial Test %d failed for %s\n",
520 			       j, algo);
521 			hexdump(result, crypto_ahash_digestsize(tfm));
522 			ret = -EINVAL;
523 			goto out;
524 		}
525 	}
526 
527 	ret = 0;
528 
529 out:
530 	ahash_request_free(req);
531 out_noreq:
532 	testmgr_free_buf(xbuf);
533 out_nobuf:
534 	kfree(key);
535 	kfree(result);
536 	return ret;
537 }
538 
539 static int test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
540 		     unsigned int tcount, bool use_digest)
541 {
542 	unsigned int alignmask;
543 	int ret;
544 
545 	ret = __test_hash(tfm, template, tcount, use_digest, 0);
546 	if (ret)
547 		return ret;
548 
549 	/* test unaligned buffers, check with one byte offset */
550 	ret = __test_hash(tfm, template, tcount, use_digest, 1);
551 	if (ret)
552 		return ret;
553 
554 	alignmask = crypto_tfm_alg_alignmask(&tfm->base);
555 	if (alignmask) {
556 		/* Check if alignment mask for tfm is correctly set. */
557 		ret = __test_hash(tfm, template, tcount, use_digest,
558 				  alignmask + 1);
559 		if (ret)
560 			return ret;
561 	}
562 
563 	return 0;
564 }
565 
566 static int __test_aead(struct crypto_aead *tfm, int enc,
567 		       struct aead_testvec *template, unsigned int tcount,
568 		       const bool diff_dst, const int align_offset)
569 {
570 	const char *algo = crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm));
571 	unsigned int i, j, k, n, temp;
572 	int ret = -ENOMEM;
573 	char *q;
574 	char *key;
575 	struct aead_request *req;
576 	struct scatterlist *sg;
577 	struct scatterlist *sgout;
578 	const char *e, *d;
579 	struct tcrypt_result result;
580 	unsigned int authsize, iv_len;
581 	void *input;
582 	void *output;
583 	void *assoc;
584 	char *iv;
585 	char *xbuf[XBUFSIZE];
586 	char *xoutbuf[XBUFSIZE];
587 	char *axbuf[XBUFSIZE];
588 
589 	iv = kzalloc(MAX_IVLEN, GFP_KERNEL);
590 	if (!iv)
591 		return ret;
592 	key = kmalloc(MAX_KEYLEN, GFP_KERNEL);
593 	if (!key)
594 		goto out_noxbuf;
595 	if (testmgr_alloc_buf(xbuf))
596 		goto out_noxbuf;
597 	if (testmgr_alloc_buf(axbuf))
598 		goto out_noaxbuf;
599 	if (diff_dst && testmgr_alloc_buf(xoutbuf))
600 		goto out_nooutbuf;
601 
602 	/* avoid "the frame size is larger than 1024 bytes" compiler warning */
603 	sg = kmalloc(sizeof(*sg) * 8 * (diff_dst ? 4 : 2), GFP_KERNEL);
604 	if (!sg)
605 		goto out_nosg;
606 	sgout = &sg[16];
607 
608 	if (diff_dst)
609 		d = "-ddst";
610 	else
611 		d = "";
612 
613 	if (enc == ENCRYPT)
614 		e = "encryption";
615 	else
616 		e = "decryption";
617 
618 	init_completion(&result.completion);
619 
620 	req = aead_request_alloc(tfm, GFP_KERNEL);
621 	if (!req) {
622 		pr_err("alg: aead%s: Failed to allocate request for %s\n",
623 		       d, algo);
624 		goto out;
625 	}
626 
627 	aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
628 				  tcrypt_complete, &result);
629 
630 	iv_len = crypto_aead_ivsize(tfm);
631 
632 	for (i = 0, j = 0; i < tcount; i++) {
633 		if (template[i].np)
634 			continue;
635 
636 		j++;
637 
638 		/* some templates have no input data but they will
639 		 * touch input
640 		 */
641 		input = xbuf[0];
642 		input += align_offset;
643 		assoc = axbuf[0];
644 
645 		ret = -EINVAL;
646 		if (WARN_ON(align_offset + template[i].ilen >
647 			    PAGE_SIZE || template[i].alen > PAGE_SIZE))
648 			goto out;
649 
650 		memcpy(input, template[i].input, template[i].ilen);
651 		memcpy(assoc, template[i].assoc, template[i].alen);
652 		if (template[i].iv)
653 			memcpy(iv, template[i].iv, iv_len);
654 		else
655 			memset(iv, 0, iv_len);
656 
657 		crypto_aead_clear_flags(tfm, ~0);
658 		if (template[i].wk)
659 			crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
660 
661 		if (template[i].klen > MAX_KEYLEN) {
662 			pr_err("alg: aead%s: setkey failed on test %d for %s: key size %d > %d\n",
663 			       d, j, algo, template[i].klen,
664 			       MAX_KEYLEN);
665 			ret = -EINVAL;
666 			goto out;
667 		}
668 		memcpy(key, template[i].key, template[i].klen);
669 
670 		ret = crypto_aead_setkey(tfm, key, template[i].klen);
671 		if (template[i].fail == !ret) {
672 			pr_err("alg: aead%s: setkey failed on test %d for %s: flags=%x\n",
673 			       d, j, algo, crypto_aead_get_flags(tfm));
674 			goto out;
675 		} else if (ret)
676 			continue;
677 
678 		authsize = abs(template[i].rlen - template[i].ilen);
679 		ret = crypto_aead_setauthsize(tfm, authsize);
680 		if (ret) {
681 			pr_err("alg: aead%s: Failed to set authsize to %u on test %d for %s\n",
682 			       d, authsize, j, algo);
683 			goto out;
684 		}
685 
686 		k = !!template[i].alen;
687 		sg_init_table(sg, k + 1);
688 		sg_set_buf(&sg[0], assoc, template[i].alen);
689 		sg_set_buf(&sg[k], input,
690 			   template[i].ilen + (enc ? authsize : 0));
691 		output = input;
692 
693 		if (diff_dst) {
694 			sg_init_table(sgout, k + 1);
695 			sg_set_buf(&sgout[0], assoc, template[i].alen);
696 
697 			output = xoutbuf[0];
698 			output += align_offset;
699 			sg_set_buf(&sgout[k], output,
700 				   template[i].rlen + (enc ? 0 : authsize));
701 		}
702 
703 		aead_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
704 				       template[i].ilen, iv);
705 
706 		aead_request_set_ad(req, template[i].alen);
707 
708 		ret = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
709 
710 		switch (ret) {
711 		case 0:
712 			if (template[i].novrfy) {
713 				/* verification was supposed to fail */
714 				pr_err("alg: aead%s: %s failed on test %d for %s: ret was 0, expected -EBADMSG\n",
715 				       d, e, j, algo);
716 				/* so really, we got a bad message */
717 				ret = -EBADMSG;
718 				goto out;
719 			}
720 			break;
721 		case -EINPROGRESS:
722 		case -EBUSY:
723 			wait_for_completion(&result.completion);
724 			reinit_completion(&result.completion);
725 			ret = result.err;
726 			if (!ret)
727 				break;
728 		case -EBADMSG:
729 			if (template[i].novrfy)
730 				/* verification failure was expected */
731 				continue;
732 			/* fall through */
733 		default:
734 			pr_err("alg: aead%s: %s failed on test %d for %s: ret=%d\n",
735 			       d, e, j, algo, -ret);
736 			goto out;
737 		}
738 
739 		q = output;
740 		if (memcmp(q, template[i].result, template[i].rlen)) {
741 			pr_err("alg: aead%s: Test %d failed on %s for %s\n",
742 			       d, j, e, algo);
743 			hexdump(q, template[i].rlen);
744 			ret = -EINVAL;
745 			goto out;
746 		}
747 	}
748 
749 	for (i = 0, j = 0; i < tcount; i++) {
750 		/* alignment tests are only done with continuous buffers */
751 		if (align_offset != 0)
752 			break;
753 
754 		if (!template[i].np)
755 			continue;
756 
757 		j++;
758 
759 		if (template[i].iv)
760 			memcpy(iv, template[i].iv, iv_len);
761 		else
762 			memset(iv, 0, MAX_IVLEN);
763 
764 		crypto_aead_clear_flags(tfm, ~0);
765 		if (template[i].wk)
766 			crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
767 		if (template[i].klen > MAX_KEYLEN) {
768 			pr_err("alg: aead%s: setkey failed on test %d for %s: key size %d > %d\n",
769 			       d, j, algo, template[i].klen, MAX_KEYLEN);
770 			ret = -EINVAL;
771 			goto out;
772 		}
773 		memcpy(key, template[i].key, template[i].klen);
774 
775 		ret = crypto_aead_setkey(tfm, key, template[i].klen);
776 		if (template[i].fail == !ret) {
777 			pr_err("alg: aead%s: setkey failed on chunk test %d for %s: flags=%x\n",
778 			       d, j, algo, crypto_aead_get_flags(tfm));
779 			goto out;
780 		} else if (ret)
781 			continue;
782 
783 		authsize = abs(template[i].rlen - template[i].ilen);
784 
785 		ret = -EINVAL;
786 		sg_init_table(sg, template[i].anp + template[i].np);
787 		if (diff_dst)
788 			sg_init_table(sgout, template[i].anp + template[i].np);
789 
790 		ret = -EINVAL;
791 		for (k = 0, temp = 0; k < template[i].anp; k++) {
792 			if (WARN_ON(offset_in_page(IDX[k]) +
793 				    template[i].atap[k] > PAGE_SIZE))
794 				goto out;
795 			sg_set_buf(&sg[k],
796 				   memcpy(axbuf[IDX[k] >> PAGE_SHIFT] +
797 					  offset_in_page(IDX[k]),
798 					  template[i].assoc + temp,
799 					  template[i].atap[k]),
800 				   template[i].atap[k]);
801 			if (diff_dst)
802 				sg_set_buf(&sgout[k],
803 					   axbuf[IDX[k] >> PAGE_SHIFT] +
804 					   offset_in_page(IDX[k]),
805 					   template[i].atap[k]);
806 			temp += template[i].atap[k];
807 		}
808 
809 		for (k = 0, temp = 0; k < template[i].np; k++) {
810 			if (WARN_ON(offset_in_page(IDX[k]) +
811 				    template[i].tap[k] > PAGE_SIZE))
812 				goto out;
813 
814 			q = xbuf[IDX[k] >> PAGE_SHIFT] + offset_in_page(IDX[k]);
815 			memcpy(q, template[i].input + temp, template[i].tap[k]);
816 			sg_set_buf(&sg[template[i].anp + k],
817 				   q, template[i].tap[k]);
818 
819 			if (diff_dst) {
820 				q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
821 				    offset_in_page(IDX[k]);
822 
823 				memset(q, 0, template[i].tap[k]);
824 
825 				sg_set_buf(&sgout[template[i].anp + k],
826 					   q, template[i].tap[k]);
827 			}
828 
829 			n = template[i].tap[k];
830 			if (k == template[i].np - 1 && enc)
831 				n += authsize;
832 			if (offset_in_page(q) + n < PAGE_SIZE)
833 				q[n] = 0;
834 
835 			temp += template[i].tap[k];
836 		}
837 
838 		ret = crypto_aead_setauthsize(tfm, authsize);
839 		if (ret) {
840 			pr_err("alg: aead%s: Failed to set authsize to %u on chunk test %d for %s\n",
841 			       d, authsize, j, algo);
842 			goto out;
843 		}
844 
845 		if (enc) {
846 			if (WARN_ON(sg[template[i].anp + k - 1].offset +
847 				    sg[template[i].anp + k - 1].length +
848 				    authsize > PAGE_SIZE)) {
849 				ret = -EINVAL;
850 				goto out;
851 			}
852 
853 			if (diff_dst)
854 				sgout[template[i].anp + k - 1].length +=
855 					authsize;
856 			sg[template[i].anp + k - 1].length += authsize;
857 		}
858 
859 		aead_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
860 				       template[i].ilen,
861 				       iv);
862 
863 		aead_request_set_ad(req, template[i].alen);
864 
865 		ret = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
866 
867 		switch (ret) {
868 		case 0:
869 			if (template[i].novrfy) {
870 				/* verification was supposed to fail */
871 				pr_err("alg: aead%s: %s failed on chunk test %d for %s: ret was 0, expected -EBADMSG\n",
872 				       d, e, j, algo);
873 				/* so really, we got a bad message */
874 				ret = -EBADMSG;
875 				goto out;
876 			}
877 			break;
878 		case -EINPROGRESS:
879 		case -EBUSY:
880 			wait_for_completion(&result.completion);
881 			reinit_completion(&result.completion);
882 			ret = result.err;
883 			if (!ret)
884 				break;
885 		case -EBADMSG:
886 			if (template[i].novrfy)
887 				/* verification failure was expected */
888 				continue;
889 			/* fall through */
890 		default:
891 			pr_err("alg: aead%s: %s failed on chunk test %d for %s: ret=%d\n",
892 			       d, e, j, algo, -ret);
893 			goto out;
894 		}
895 
896 		ret = -EINVAL;
897 		for (k = 0, temp = 0; k < template[i].np; k++) {
898 			if (diff_dst)
899 				q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
900 				    offset_in_page(IDX[k]);
901 			else
902 				q = xbuf[IDX[k] >> PAGE_SHIFT] +
903 				    offset_in_page(IDX[k]);
904 
905 			n = template[i].tap[k];
906 			if (k == template[i].np - 1)
907 				n += enc ? authsize : -authsize;
908 
909 			if (memcmp(q, template[i].result + temp, n)) {
910 				pr_err("alg: aead%s: Chunk test %d failed on %s at page %u for %s\n",
911 				       d, j, e, k, algo);
912 				hexdump(q, n);
913 				goto out;
914 			}
915 
916 			q += n;
917 			if (k == template[i].np - 1 && !enc) {
918 				if (!diff_dst &&
919 					memcmp(q, template[i].input +
920 					      temp + n, authsize))
921 					n = authsize;
922 				else
923 					n = 0;
924 			} else {
925 				for (n = 0; offset_in_page(q + n) && q[n]; n++)
926 					;
927 			}
928 			if (n) {
929 				pr_err("alg: aead%s: Result buffer corruption in chunk test %d on %s at page %u for %s: %u bytes:\n",
930 				       d, j, e, k, algo, n);
931 				hexdump(q, n);
932 				goto out;
933 			}
934 
935 			temp += template[i].tap[k];
936 		}
937 	}
938 
939 	ret = 0;
940 
941 out:
942 	aead_request_free(req);
943 	kfree(sg);
944 out_nosg:
945 	if (diff_dst)
946 		testmgr_free_buf(xoutbuf);
947 out_nooutbuf:
948 	testmgr_free_buf(axbuf);
949 out_noaxbuf:
950 	testmgr_free_buf(xbuf);
951 out_noxbuf:
952 	kfree(key);
953 	kfree(iv);
954 	return ret;
955 }
956 
957 static int test_aead(struct crypto_aead *tfm, int enc,
958 		     struct aead_testvec *template, unsigned int tcount)
959 {
960 	unsigned int alignmask;
961 	int ret;
962 
963 	/* test 'dst == src' case */
964 	ret = __test_aead(tfm, enc, template, tcount, false, 0);
965 	if (ret)
966 		return ret;
967 
968 	/* test 'dst != src' case */
969 	ret = __test_aead(tfm, enc, template, tcount, true, 0);
970 	if (ret)
971 		return ret;
972 
973 	/* test unaligned buffers, check with one byte offset */
974 	ret = __test_aead(tfm, enc, template, tcount, true, 1);
975 	if (ret)
976 		return ret;
977 
978 	alignmask = crypto_tfm_alg_alignmask(&tfm->base);
979 	if (alignmask) {
980 		/* Check if alignment mask for tfm is correctly set. */
981 		ret = __test_aead(tfm, enc, template, tcount, true,
982 				  alignmask + 1);
983 		if (ret)
984 			return ret;
985 	}
986 
987 	return 0;
988 }
989 
990 static int test_cipher(struct crypto_cipher *tfm, int enc,
991 		       struct cipher_testvec *template, unsigned int tcount)
992 {
993 	const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
994 	unsigned int i, j, k;
995 	char *q;
996 	const char *e;
997 	void *data;
998 	char *xbuf[XBUFSIZE];
999 	int ret = -ENOMEM;
1000 
1001 	if (testmgr_alloc_buf(xbuf))
1002 		goto out_nobuf;
1003 
1004 	if (enc == ENCRYPT)
1005 	        e = "encryption";
1006 	else
1007 		e = "decryption";
1008 
1009 	j = 0;
1010 	for (i = 0; i < tcount; i++) {
1011 		if (template[i].np)
1012 			continue;
1013 
1014 		if (fips_enabled && template[i].fips_skip)
1015 			continue;
1016 
1017 		j++;
1018 
1019 		ret = -EINVAL;
1020 		if (WARN_ON(template[i].ilen > PAGE_SIZE))
1021 			goto out;
1022 
1023 		data = xbuf[0];
1024 		memcpy(data, template[i].input, template[i].ilen);
1025 
1026 		crypto_cipher_clear_flags(tfm, ~0);
1027 		if (template[i].wk)
1028 			crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
1029 
1030 		ret = crypto_cipher_setkey(tfm, template[i].key,
1031 					   template[i].klen);
1032 		if (template[i].fail == !ret) {
1033 			printk(KERN_ERR "alg: cipher: setkey failed "
1034 			       "on test %d for %s: flags=%x\n", j,
1035 			       algo, crypto_cipher_get_flags(tfm));
1036 			goto out;
1037 		} else if (ret)
1038 			continue;
1039 
1040 		for (k = 0; k < template[i].ilen;
1041 		     k += crypto_cipher_blocksize(tfm)) {
1042 			if (enc)
1043 				crypto_cipher_encrypt_one(tfm, data + k,
1044 							  data + k);
1045 			else
1046 				crypto_cipher_decrypt_one(tfm, data + k,
1047 							  data + k);
1048 		}
1049 
1050 		q = data;
1051 		if (memcmp(q, template[i].result, template[i].rlen)) {
1052 			printk(KERN_ERR "alg: cipher: Test %d failed "
1053 			       "on %s for %s\n", j, e, algo);
1054 			hexdump(q, template[i].rlen);
1055 			ret = -EINVAL;
1056 			goto out;
1057 		}
1058 	}
1059 
1060 	ret = 0;
1061 
1062 out:
1063 	testmgr_free_buf(xbuf);
1064 out_nobuf:
1065 	return ret;
1066 }
1067 
1068 static int __test_skcipher(struct crypto_skcipher *tfm, int enc,
1069 			   struct cipher_testvec *template, unsigned int tcount,
1070 			   const bool diff_dst, const int align_offset)
1071 {
1072 	const char *algo =
1073 		crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm));
1074 	unsigned int i, j, k, n, temp;
1075 	char *q;
1076 	struct skcipher_request *req;
1077 	struct scatterlist sg[8];
1078 	struct scatterlist sgout[8];
1079 	const char *e, *d;
1080 	struct tcrypt_result result;
1081 	void *data;
1082 	char iv[MAX_IVLEN];
1083 	char *xbuf[XBUFSIZE];
1084 	char *xoutbuf[XBUFSIZE];
1085 	int ret = -ENOMEM;
1086 	unsigned int ivsize = crypto_skcipher_ivsize(tfm);
1087 
1088 	if (testmgr_alloc_buf(xbuf))
1089 		goto out_nobuf;
1090 
1091 	if (diff_dst && testmgr_alloc_buf(xoutbuf))
1092 		goto out_nooutbuf;
1093 
1094 	if (diff_dst)
1095 		d = "-ddst";
1096 	else
1097 		d = "";
1098 
1099 	if (enc == ENCRYPT)
1100 	        e = "encryption";
1101 	else
1102 		e = "decryption";
1103 
1104 	init_completion(&result.completion);
1105 
1106 	req = skcipher_request_alloc(tfm, GFP_KERNEL);
1107 	if (!req) {
1108 		pr_err("alg: skcipher%s: Failed to allocate request for %s\n",
1109 		       d, algo);
1110 		goto out;
1111 	}
1112 
1113 	skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1114 				      tcrypt_complete, &result);
1115 
1116 	j = 0;
1117 	for (i = 0; i < tcount; i++) {
1118 		if (template[i].np && !template[i].also_non_np)
1119 			continue;
1120 
1121 		if (fips_enabled && template[i].fips_skip)
1122 			continue;
1123 
1124 		if (template[i].iv)
1125 			memcpy(iv, template[i].iv, ivsize);
1126 		else
1127 			memset(iv, 0, MAX_IVLEN);
1128 
1129 		j++;
1130 		ret = -EINVAL;
1131 		if (WARN_ON(align_offset + template[i].ilen > PAGE_SIZE))
1132 			goto out;
1133 
1134 		data = xbuf[0];
1135 		data += align_offset;
1136 		memcpy(data, template[i].input, template[i].ilen);
1137 
1138 		crypto_skcipher_clear_flags(tfm, ~0);
1139 		if (template[i].wk)
1140 			crypto_skcipher_set_flags(tfm,
1141 						  CRYPTO_TFM_REQ_WEAK_KEY);
1142 
1143 		ret = crypto_skcipher_setkey(tfm, template[i].key,
1144 					     template[i].klen);
1145 		if (template[i].fail == !ret) {
1146 			pr_err("alg: skcipher%s: setkey failed on test %d for %s: flags=%x\n",
1147 			       d, j, algo, crypto_skcipher_get_flags(tfm));
1148 			goto out;
1149 		} else if (ret)
1150 			continue;
1151 
1152 		sg_init_one(&sg[0], data, template[i].ilen);
1153 		if (diff_dst) {
1154 			data = xoutbuf[0];
1155 			data += align_offset;
1156 			sg_init_one(&sgout[0], data, template[i].ilen);
1157 		}
1158 
1159 		skcipher_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
1160 					   template[i].ilen, iv);
1161 		ret = enc ? crypto_skcipher_encrypt(req) :
1162 			    crypto_skcipher_decrypt(req);
1163 
1164 		switch (ret) {
1165 		case 0:
1166 			break;
1167 		case -EINPROGRESS:
1168 		case -EBUSY:
1169 			wait_for_completion(&result.completion);
1170 			reinit_completion(&result.completion);
1171 			ret = result.err;
1172 			if (!ret)
1173 				break;
1174 			/* fall through */
1175 		default:
1176 			pr_err("alg: skcipher%s: %s failed on test %d for %s: ret=%d\n",
1177 			       d, e, j, algo, -ret);
1178 			goto out;
1179 		}
1180 
1181 		q = data;
1182 		if (memcmp(q, template[i].result, template[i].rlen)) {
1183 			pr_err("alg: skcipher%s: Test %d failed (invalid result) on %s for %s\n",
1184 			       d, j, e, algo);
1185 			hexdump(q, template[i].rlen);
1186 			ret = -EINVAL;
1187 			goto out;
1188 		}
1189 
1190 		if (template[i].iv_out &&
1191 		    memcmp(iv, template[i].iv_out,
1192 			   crypto_skcipher_ivsize(tfm))) {
1193 			pr_err("alg: skcipher%s: Test %d failed (invalid output IV) on %s for %s\n",
1194 			       d, j, e, algo);
1195 			hexdump(iv, crypto_skcipher_ivsize(tfm));
1196 			ret = -EINVAL;
1197 			goto out;
1198 		}
1199 	}
1200 
1201 	j = 0;
1202 	for (i = 0; i < tcount; i++) {
1203 		/* alignment tests are only done with continuous buffers */
1204 		if (align_offset != 0)
1205 			break;
1206 
1207 		if (!template[i].np)
1208 			continue;
1209 
1210 		if (fips_enabled && template[i].fips_skip)
1211 			continue;
1212 
1213 		if (template[i].iv)
1214 			memcpy(iv, template[i].iv, ivsize);
1215 		else
1216 			memset(iv, 0, MAX_IVLEN);
1217 
1218 		j++;
1219 		crypto_skcipher_clear_flags(tfm, ~0);
1220 		if (template[i].wk)
1221 			crypto_skcipher_set_flags(tfm,
1222 						  CRYPTO_TFM_REQ_WEAK_KEY);
1223 
1224 		ret = crypto_skcipher_setkey(tfm, template[i].key,
1225 					     template[i].klen);
1226 		if (template[i].fail == !ret) {
1227 			pr_err("alg: skcipher%s: setkey failed on chunk test %d for %s: flags=%x\n",
1228 			       d, j, algo, crypto_skcipher_get_flags(tfm));
1229 			goto out;
1230 		} else if (ret)
1231 			continue;
1232 
1233 		temp = 0;
1234 		ret = -EINVAL;
1235 		sg_init_table(sg, template[i].np);
1236 		if (diff_dst)
1237 			sg_init_table(sgout, template[i].np);
1238 		for (k = 0; k < template[i].np; k++) {
1239 			if (WARN_ON(offset_in_page(IDX[k]) +
1240 				    template[i].tap[k] > PAGE_SIZE))
1241 				goto out;
1242 
1243 			q = xbuf[IDX[k] >> PAGE_SHIFT] + offset_in_page(IDX[k]);
1244 
1245 			memcpy(q, template[i].input + temp, template[i].tap[k]);
1246 
1247 			if (offset_in_page(q) + template[i].tap[k] < PAGE_SIZE)
1248 				q[template[i].tap[k]] = 0;
1249 
1250 			sg_set_buf(&sg[k], q, template[i].tap[k]);
1251 			if (diff_dst) {
1252 				q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
1253 				    offset_in_page(IDX[k]);
1254 
1255 				sg_set_buf(&sgout[k], q, template[i].tap[k]);
1256 
1257 				memset(q, 0, template[i].tap[k]);
1258 				if (offset_in_page(q) +
1259 				    template[i].tap[k] < PAGE_SIZE)
1260 					q[template[i].tap[k]] = 0;
1261 			}
1262 
1263 			temp += template[i].tap[k];
1264 		}
1265 
1266 		skcipher_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
1267 					   template[i].ilen, iv);
1268 
1269 		ret = enc ? crypto_skcipher_encrypt(req) :
1270 			    crypto_skcipher_decrypt(req);
1271 
1272 		switch (ret) {
1273 		case 0:
1274 			break;
1275 		case -EINPROGRESS:
1276 		case -EBUSY:
1277 			wait_for_completion(&result.completion);
1278 			reinit_completion(&result.completion);
1279 			ret = result.err;
1280 			if (!ret)
1281 				break;
1282 			/* fall through */
1283 		default:
1284 			pr_err("alg: skcipher%s: %s failed on chunk test %d for %s: ret=%d\n",
1285 			       d, e, j, algo, -ret);
1286 			goto out;
1287 		}
1288 
1289 		temp = 0;
1290 		ret = -EINVAL;
1291 		for (k = 0; k < template[i].np; k++) {
1292 			if (diff_dst)
1293 				q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
1294 				    offset_in_page(IDX[k]);
1295 			else
1296 				q = xbuf[IDX[k] >> PAGE_SHIFT] +
1297 				    offset_in_page(IDX[k]);
1298 
1299 			if (memcmp(q, template[i].result + temp,
1300 				   template[i].tap[k])) {
1301 				pr_err("alg: skcipher%s: Chunk test %d failed on %s at page %u for %s\n",
1302 				       d, j, e, k, algo);
1303 				hexdump(q, template[i].tap[k]);
1304 				goto out;
1305 			}
1306 
1307 			q += template[i].tap[k];
1308 			for (n = 0; offset_in_page(q + n) && q[n]; n++)
1309 				;
1310 			if (n) {
1311 				pr_err("alg: skcipher%s: Result buffer corruption in chunk test %d on %s at page %u for %s: %u bytes:\n",
1312 				       d, j, e, k, algo, n);
1313 				hexdump(q, n);
1314 				goto out;
1315 			}
1316 			temp += template[i].tap[k];
1317 		}
1318 	}
1319 
1320 	ret = 0;
1321 
1322 out:
1323 	skcipher_request_free(req);
1324 	if (diff_dst)
1325 		testmgr_free_buf(xoutbuf);
1326 out_nooutbuf:
1327 	testmgr_free_buf(xbuf);
1328 out_nobuf:
1329 	return ret;
1330 }
1331 
1332 static int test_skcipher(struct crypto_skcipher *tfm, int enc,
1333 			 struct cipher_testvec *template, unsigned int tcount)
1334 {
1335 	unsigned int alignmask;
1336 	int ret;
1337 
1338 	/* test 'dst == src' case */
1339 	ret = __test_skcipher(tfm, enc, template, tcount, false, 0);
1340 	if (ret)
1341 		return ret;
1342 
1343 	/* test 'dst != src' case */
1344 	ret = __test_skcipher(tfm, enc, template, tcount, true, 0);
1345 	if (ret)
1346 		return ret;
1347 
1348 	/* test unaligned buffers, check with one byte offset */
1349 	ret = __test_skcipher(tfm, enc, template, tcount, true, 1);
1350 	if (ret)
1351 		return ret;
1352 
1353 	alignmask = crypto_tfm_alg_alignmask(&tfm->base);
1354 	if (alignmask) {
1355 		/* Check if alignment mask for tfm is correctly set. */
1356 		ret = __test_skcipher(tfm, enc, template, tcount, true,
1357 				      alignmask + 1);
1358 		if (ret)
1359 			return ret;
1360 	}
1361 
1362 	return 0;
1363 }
1364 
1365 static int test_comp(struct crypto_comp *tfm, struct comp_testvec *ctemplate,
1366 		     struct comp_testvec *dtemplate, int ctcount, int dtcount)
1367 {
1368 	const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
1369 	unsigned int i;
1370 	char result[COMP_BUF_SIZE];
1371 	int ret;
1372 
1373 	for (i = 0; i < ctcount; i++) {
1374 		int ilen;
1375 		unsigned int dlen = COMP_BUF_SIZE;
1376 
1377 		memset(result, 0, sizeof (result));
1378 
1379 		ilen = ctemplate[i].inlen;
1380 		ret = crypto_comp_compress(tfm, ctemplate[i].input,
1381 		                           ilen, result, &dlen);
1382 		if (ret) {
1383 			printk(KERN_ERR "alg: comp: compression failed "
1384 			       "on test %d for %s: ret=%d\n", i + 1, algo,
1385 			       -ret);
1386 			goto out;
1387 		}
1388 
1389 		if (dlen != ctemplate[i].outlen) {
1390 			printk(KERN_ERR "alg: comp: Compression test %d "
1391 			       "failed for %s: output len = %d\n", i + 1, algo,
1392 			       dlen);
1393 			ret = -EINVAL;
1394 			goto out;
1395 		}
1396 
1397 		if (memcmp(result, ctemplate[i].output, dlen)) {
1398 			printk(KERN_ERR "alg: comp: Compression test %d "
1399 			       "failed for %s\n", i + 1, algo);
1400 			hexdump(result, dlen);
1401 			ret = -EINVAL;
1402 			goto out;
1403 		}
1404 	}
1405 
1406 	for (i = 0; i < dtcount; i++) {
1407 		int ilen;
1408 		unsigned int dlen = COMP_BUF_SIZE;
1409 
1410 		memset(result, 0, sizeof (result));
1411 
1412 		ilen = dtemplate[i].inlen;
1413 		ret = crypto_comp_decompress(tfm, dtemplate[i].input,
1414 		                             ilen, result, &dlen);
1415 		if (ret) {
1416 			printk(KERN_ERR "alg: comp: decompression failed "
1417 			       "on test %d for %s: ret=%d\n", i + 1, algo,
1418 			       -ret);
1419 			goto out;
1420 		}
1421 
1422 		if (dlen != dtemplate[i].outlen) {
1423 			printk(KERN_ERR "alg: comp: Decompression test %d "
1424 			       "failed for %s: output len = %d\n", i + 1, algo,
1425 			       dlen);
1426 			ret = -EINVAL;
1427 			goto out;
1428 		}
1429 
1430 		if (memcmp(result, dtemplate[i].output, dlen)) {
1431 			printk(KERN_ERR "alg: comp: Decompression test %d "
1432 			       "failed for %s\n", i + 1, algo);
1433 			hexdump(result, dlen);
1434 			ret = -EINVAL;
1435 			goto out;
1436 		}
1437 	}
1438 
1439 	ret = 0;
1440 
1441 out:
1442 	return ret;
1443 }
1444 
1445 static int test_cprng(struct crypto_rng *tfm, struct cprng_testvec *template,
1446 		      unsigned int tcount)
1447 {
1448 	const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
1449 	int err = 0, i, j, seedsize;
1450 	u8 *seed;
1451 	char result[32];
1452 
1453 	seedsize = crypto_rng_seedsize(tfm);
1454 
1455 	seed = kmalloc(seedsize, GFP_KERNEL);
1456 	if (!seed) {
1457 		printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
1458 		       "for %s\n", algo);
1459 		return -ENOMEM;
1460 	}
1461 
1462 	for (i = 0; i < tcount; i++) {
1463 		memset(result, 0, 32);
1464 
1465 		memcpy(seed, template[i].v, template[i].vlen);
1466 		memcpy(seed + template[i].vlen, template[i].key,
1467 		       template[i].klen);
1468 		memcpy(seed + template[i].vlen + template[i].klen,
1469 		       template[i].dt, template[i].dtlen);
1470 
1471 		err = crypto_rng_reset(tfm, seed, seedsize);
1472 		if (err) {
1473 			printk(KERN_ERR "alg: cprng: Failed to reset rng "
1474 			       "for %s\n", algo);
1475 			goto out;
1476 		}
1477 
1478 		for (j = 0; j < template[i].loops; j++) {
1479 			err = crypto_rng_get_bytes(tfm, result,
1480 						   template[i].rlen);
1481 			if (err < 0) {
1482 				printk(KERN_ERR "alg: cprng: Failed to obtain "
1483 				       "the correct amount of random data for "
1484 				       "%s (requested %d)\n", algo,
1485 				       template[i].rlen);
1486 				goto out;
1487 			}
1488 		}
1489 
1490 		err = memcmp(result, template[i].result,
1491 			     template[i].rlen);
1492 		if (err) {
1493 			printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
1494 			       i, algo);
1495 			hexdump(result, template[i].rlen);
1496 			err = -EINVAL;
1497 			goto out;
1498 		}
1499 	}
1500 
1501 out:
1502 	kfree(seed);
1503 	return err;
1504 }
1505 
1506 static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
1507 			 u32 type, u32 mask)
1508 {
1509 	struct crypto_aead *tfm;
1510 	int err = 0;
1511 
1512 	tfm = crypto_alloc_aead(driver, type | CRYPTO_ALG_INTERNAL, mask);
1513 	if (IS_ERR(tfm)) {
1514 		printk(KERN_ERR "alg: aead: Failed to load transform for %s: "
1515 		       "%ld\n", driver, PTR_ERR(tfm));
1516 		return PTR_ERR(tfm);
1517 	}
1518 
1519 	if (desc->suite.aead.enc.vecs) {
1520 		err = test_aead(tfm, ENCRYPT, desc->suite.aead.enc.vecs,
1521 				desc->suite.aead.enc.count);
1522 		if (err)
1523 			goto out;
1524 	}
1525 
1526 	if (!err && desc->suite.aead.dec.vecs)
1527 		err = test_aead(tfm, DECRYPT, desc->suite.aead.dec.vecs,
1528 				desc->suite.aead.dec.count);
1529 
1530 out:
1531 	crypto_free_aead(tfm);
1532 	return err;
1533 }
1534 
1535 static int alg_test_cipher(const struct alg_test_desc *desc,
1536 			   const char *driver, u32 type, u32 mask)
1537 {
1538 	struct crypto_cipher *tfm;
1539 	int err = 0;
1540 
1541 	tfm = crypto_alloc_cipher(driver, type | CRYPTO_ALG_INTERNAL, mask);
1542 	if (IS_ERR(tfm)) {
1543 		printk(KERN_ERR "alg: cipher: Failed to load transform for "
1544 		       "%s: %ld\n", driver, PTR_ERR(tfm));
1545 		return PTR_ERR(tfm);
1546 	}
1547 
1548 	if (desc->suite.cipher.enc.vecs) {
1549 		err = test_cipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
1550 				  desc->suite.cipher.enc.count);
1551 		if (err)
1552 			goto out;
1553 	}
1554 
1555 	if (desc->suite.cipher.dec.vecs)
1556 		err = test_cipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
1557 				  desc->suite.cipher.dec.count);
1558 
1559 out:
1560 	crypto_free_cipher(tfm);
1561 	return err;
1562 }
1563 
1564 static int alg_test_skcipher(const struct alg_test_desc *desc,
1565 			     const char *driver, u32 type, u32 mask)
1566 {
1567 	struct crypto_skcipher *tfm;
1568 	int err = 0;
1569 
1570 	tfm = crypto_alloc_skcipher(driver, type | CRYPTO_ALG_INTERNAL, mask);
1571 	if (IS_ERR(tfm)) {
1572 		printk(KERN_ERR "alg: skcipher: Failed to load transform for "
1573 		       "%s: %ld\n", driver, PTR_ERR(tfm));
1574 		return PTR_ERR(tfm);
1575 	}
1576 
1577 	if (desc->suite.cipher.enc.vecs) {
1578 		err = test_skcipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
1579 				    desc->suite.cipher.enc.count);
1580 		if (err)
1581 			goto out;
1582 	}
1583 
1584 	if (desc->suite.cipher.dec.vecs)
1585 		err = test_skcipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
1586 				    desc->suite.cipher.dec.count);
1587 
1588 out:
1589 	crypto_free_skcipher(tfm);
1590 	return err;
1591 }
1592 
1593 static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
1594 			 u32 type, u32 mask)
1595 {
1596 	struct crypto_comp *tfm;
1597 	int err;
1598 
1599 	tfm = crypto_alloc_comp(driver, type, mask);
1600 	if (IS_ERR(tfm)) {
1601 		printk(KERN_ERR "alg: comp: Failed to load transform for %s: "
1602 		       "%ld\n", driver, PTR_ERR(tfm));
1603 		return PTR_ERR(tfm);
1604 	}
1605 
1606 	err = test_comp(tfm, desc->suite.comp.comp.vecs,
1607 			desc->suite.comp.decomp.vecs,
1608 			desc->suite.comp.comp.count,
1609 			desc->suite.comp.decomp.count);
1610 
1611 	crypto_free_comp(tfm);
1612 	return err;
1613 }
1614 
1615 static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1616 			 u32 type, u32 mask)
1617 {
1618 	struct crypto_ahash *tfm;
1619 	int err;
1620 
1621 	tfm = crypto_alloc_ahash(driver, type | CRYPTO_ALG_INTERNAL, mask);
1622 	if (IS_ERR(tfm)) {
1623 		printk(KERN_ERR "alg: hash: Failed to load transform for %s: "
1624 		       "%ld\n", driver, PTR_ERR(tfm));
1625 		return PTR_ERR(tfm);
1626 	}
1627 
1628 	err = test_hash(tfm, desc->suite.hash.vecs,
1629 			desc->suite.hash.count, true);
1630 	if (!err)
1631 		err = test_hash(tfm, desc->suite.hash.vecs,
1632 				desc->suite.hash.count, false);
1633 
1634 	crypto_free_ahash(tfm);
1635 	return err;
1636 }
1637 
1638 static int alg_test_crc32c(const struct alg_test_desc *desc,
1639 			   const char *driver, u32 type, u32 mask)
1640 {
1641 	struct crypto_shash *tfm;
1642 	u32 val;
1643 	int err;
1644 
1645 	err = alg_test_hash(desc, driver, type, mask);
1646 	if (err)
1647 		goto out;
1648 
1649 	tfm = crypto_alloc_shash(driver, type | CRYPTO_ALG_INTERNAL, mask);
1650 	if (IS_ERR(tfm)) {
1651 		printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
1652 		       "%ld\n", driver, PTR_ERR(tfm));
1653 		err = PTR_ERR(tfm);
1654 		goto out;
1655 	}
1656 
1657 	do {
1658 		SHASH_DESC_ON_STACK(shash, tfm);
1659 		u32 *ctx = (u32 *)shash_desc_ctx(shash);
1660 
1661 		shash->tfm = tfm;
1662 		shash->flags = 0;
1663 
1664 		*ctx = le32_to_cpu(420553207);
1665 		err = crypto_shash_final(shash, (u8 *)&val);
1666 		if (err) {
1667 			printk(KERN_ERR "alg: crc32c: Operation failed for "
1668 			       "%s: %d\n", driver, err);
1669 			break;
1670 		}
1671 
1672 		if (val != ~420553207) {
1673 			printk(KERN_ERR "alg: crc32c: Test failed for %s: "
1674 			       "%d\n", driver, val);
1675 			err = -EINVAL;
1676 		}
1677 	} while (0);
1678 
1679 	crypto_free_shash(tfm);
1680 
1681 out:
1682 	return err;
1683 }
1684 
1685 static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
1686 			  u32 type, u32 mask)
1687 {
1688 	struct crypto_rng *rng;
1689 	int err;
1690 
1691 	rng = crypto_alloc_rng(driver, type | CRYPTO_ALG_INTERNAL, mask);
1692 	if (IS_ERR(rng)) {
1693 		printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
1694 		       "%ld\n", driver, PTR_ERR(rng));
1695 		return PTR_ERR(rng);
1696 	}
1697 
1698 	err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
1699 
1700 	crypto_free_rng(rng);
1701 
1702 	return err;
1703 }
1704 
1705 
1706 static int drbg_cavs_test(struct drbg_testvec *test, int pr,
1707 			  const char *driver, u32 type, u32 mask)
1708 {
1709 	int ret = -EAGAIN;
1710 	struct crypto_rng *drng;
1711 	struct drbg_test_data test_data;
1712 	struct drbg_string addtl, pers, testentropy;
1713 	unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
1714 
1715 	if (!buf)
1716 		return -ENOMEM;
1717 
1718 	drng = crypto_alloc_rng(driver, type | CRYPTO_ALG_INTERNAL, mask);
1719 	if (IS_ERR(drng)) {
1720 		printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
1721 		       "%s\n", driver);
1722 		kzfree(buf);
1723 		return -ENOMEM;
1724 	}
1725 
1726 	test_data.testentropy = &testentropy;
1727 	drbg_string_fill(&testentropy, test->entropy, test->entropylen);
1728 	drbg_string_fill(&pers, test->pers, test->perslen);
1729 	ret = crypto_drbg_reset_test(drng, &pers, &test_data);
1730 	if (ret) {
1731 		printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
1732 		goto outbuf;
1733 	}
1734 
1735 	drbg_string_fill(&addtl, test->addtla, test->addtllen);
1736 	if (pr) {
1737 		drbg_string_fill(&testentropy, test->entpra, test->entprlen);
1738 		ret = crypto_drbg_get_bytes_addtl_test(drng,
1739 			buf, test->expectedlen, &addtl,	&test_data);
1740 	} else {
1741 		ret = crypto_drbg_get_bytes_addtl(drng,
1742 			buf, test->expectedlen, &addtl);
1743 	}
1744 	if (ret < 0) {
1745 		printk(KERN_ERR "alg: drbg: could not obtain random data for "
1746 		       "driver %s\n", driver);
1747 		goto outbuf;
1748 	}
1749 
1750 	drbg_string_fill(&addtl, test->addtlb, test->addtllen);
1751 	if (pr) {
1752 		drbg_string_fill(&testentropy, test->entprb, test->entprlen);
1753 		ret = crypto_drbg_get_bytes_addtl_test(drng,
1754 			buf, test->expectedlen, &addtl, &test_data);
1755 	} else {
1756 		ret = crypto_drbg_get_bytes_addtl(drng,
1757 			buf, test->expectedlen, &addtl);
1758 	}
1759 	if (ret < 0) {
1760 		printk(KERN_ERR "alg: drbg: could not obtain random data for "
1761 		       "driver %s\n", driver);
1762 		goto outbuf;
1763 	}
1764 
1765 	ret = memcmp(test->expected, buf, test->expectedlen);
1766 
1767 outbuf:
1768 	crypto_free_rng(drng);
1769 	kzfree(buf);
1770 	return ret;
1771 }
1772 
1773 
1774 static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
1775 			 u32 type, u32 mask)
1776 {
1777 	int err = 0;
1778 	int pr = 0;
1779 	int i = 0;
1780 	struct drbg_testvec *template = desc->suite.drbg.vecs;
1781 	unsigned int tcount = desc->suite.drbg.count;
1782 
1783 	if (0 == memcmp(driver, "drbg_pr_", 8))
1784 		pr = 1;
1785 
1786 	for (i = 0; i < tcount; i++) {
1787 		err = drbg_cavs_test(&template[i], pr, driver, type, mask);
1788 		if (err) {
1789 			printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
1790 			       i, driver);
1791 			err = -EINVAL;
1792 			break;
1793 		}
1794 	}
1795 	return err;
1796 
1797 }
1798 
1799 static int do_test_kpp(struct crypto_kpp *tfm, struct kpp_testvec *vec,
1800 		       const char *alg)
1801 {
1802 	struct kpp_request *req;
1803 	void *input_buf = NULL;
1804 	void *output_buf = NULL;
1805 	struct tcrypt_result result;
1806 	unsigned int out_len_max;
1807 	int err = -ENOMEM;
1808 	struct scatterlist src, dst;
1809 
1810 	req = kpp_request_alloc(tfm, GFP_KERNEL);
1811 	if (!req)
1812 		return err;
1813 
1814 	init_completion(&result.completion);
1815 
1816 	err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
1817 	if (err < 0)
1818 		goto free_req;
1819 
1820 	out_len_max = crypto_kpp_maxsize(tfm);
1821 	output_buf = kzalloc(out_len_max, GFP_KERNEL);
1822 	if (!output_buf) {
1823 		err = -ENOMEM;
1824 		goto free_req;
1825 	}
1826 
1827 	/* Use appropriate parameter as base */
1828 	kpp_request_set_input(req, NULL, 0);
1829 	sg_init_one(&dst, output_buf, out_len_max);
1830 	kpp_request_set_output(req, &dst, out_len_max);
1831 	kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1832 				 tcrypt_complete, &result);
1833 
1834 	/* Compute public key */
1835 	err = wait_async_op(&result, crypto_kpp_generate_public_key(req));
1836 	if (err) {
1837 		pr_err("alg: %s: generate public key test failed. err %d\n",
1838 		       alg, err);
1839 		goto free_output;
1840 	}
1841 	/* Verify calculated public key */
1842 	if (memcmp(vec->expected_a_public, sg_virt(req->dst),
1843 		   vec->expected_a_public_size)) {
1844 		pr_err("alg: %s: generate public key test failed. Invalid output\n",
1845 		       alg);
1846 		err = -EINVAL;
1847 		goto free_output;
1848 	}
1849 
1850 	/* Calculate shared secret key by using counter part (b) public key. */
1851 	input_buf = kzalloc(vec->b_public_size, GFP_KERNEL);
1852 	if (!input_buf) {
1853 		err = -ENOMEM;
1854 		goto free_output;
1855 	}
1856 
1857 	memcpy(input_buf, vec->b_public, vec->b_public_size);
1858 	sg_init_one(&src, input_buf, vec->b_public_size);
1859 	sg_init_one(&dst, output_buf, out_len_max);
1860 	kpp_request_set_input(req, &src, vec->b_public_size);
1861 	kpp_request_set_output(req, &dst, out_len_max);
1862 	kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1863 				 tcrypt_complete, &result);
1864 	err = wait_async_op(&result, crypto_kpp_compute_shared_secret(req));
1865 	if (err) {
1866 		pr_err("alg: %s: compute shard secret test failed. err %d\n",
1867 		       alg, err);
1868 		goto free_all;
1869 	}
1870 	/*
1871 	 * verify shared secret from which the user will derive
1872 	 * secret key by executing whatever hash it has chosen
1873 	 */
1874 	if (memcmp(vec->expected_ss, sg_virt(req->dst),
1875 		   vec->expected_ss_size)) {
1876 		pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
1877 		       alg);
1878 		err = -EINVAL;
1879 	}
1880 
1881 free_all:
1882 	kfree(input_buf);
1883 free_output:
1884 	kfree(output_buf);
1885 free_req:
1886 	kpp_request_free(req);
1887 	return err;
1888 }
1889 
1890 static int test_kpp(struct crypto_kpp *tfm, const char *alg,
1891 		    struct kpp_testvec *vecs, unsigned int tcount)
1892 {
1893 	int ret, i;
1894 
1895 	for (i = 0; i < tcount; i++) {
1896 		ret = do_test_kpp(tfm, vecs++, alg);
1897 		if (ret) {
1898 			pr_err("alg: %s: test failed on vector %d, err=%d\n",
1899 			       alg, i + 1, ret);
1900 			return ret;
1901 		}
1902 	}
1903 	return 0;
1904 }
1905 
1906 static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
1907 			u32 type, u32 mask)
1908 {
1909 	struct crypto_kpp *tfm;
1910 	int err = 0;
1911 
1912 	tfm = crypto_alloc_kpp(driver, type | CRYPTO_ALG_INTERNAL, mask);
1913 	if (IS_ERR(tfm)) {
1914 		pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
1915 		       driver, PTR_ERR(tfm));
1916 		return PTR_ERR(tfm);
1917 	}
1918 	if (desc->suite.kpp.vecs)
1919 		err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
1920 			       desc->suite.kpp.count);
1921 
1922 	crypto_free_kpp(tfm);
1923 	return err;
1924 }
1925 
1926 static int test_akcipher_one(struct crypto_akcipher *tfm,
1927 			     struct akcipher_testvec *vecs)
1928 {
1929 	char *xbuf[XBUFSIZE];
1930 	struct akcipher_request *req;
1931 	void *outbuf_enc = NULL;
1932 	void *outbuf_dec = NULL;
1933 	struct tcrypt_result result;
1934 	unsigned int out_len_max, out_len = 0;
1935 	int err = -ENOMEM;
1936 	struct scatterlist src, dst, src_tab[2];
1937 
1938 	if (testmgr_alloc_buf(xbuf))
1939 		return err;
1940 
1941 	req = akcipher_request_alloc(tfm, GFP_KERNEL);
1942 	if (!req)
1943 		goto free_xbuf;
1944 
1945 	init_completion(&result.completion);
1946 
1947 	if (vecs->public_key_vec)
1948 		err = crypto_akcipher_set_pub_key(tfm, vecs->key,
1949 						  vecs->key_len);
1950 	else
1951 		err = crypto_akcipher_set_priv_key(tfm, vecs->key,
1952 						   vecs->key_len);
1953 	if (err)
1954 		goto free_req;
1955 
1956 	err = -ENOMEM;
1957 	out_len_max = crypto_akcipher_maxsize(tfm);
1958 	outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
1959 	if (!outbuf_enc)
1960 		goto free_req;
1961 
1962 	if (WARN_ON(vecs->m_size > PAGE_SIZE))
1963 		goto free_all;
1964 
1965 	memcpy(xbuf[0], vecs->m, vecs->m_size);
1966 
1967 	sg_init_table(src_tab, 2);
1968 	sg_set_buf(&src_tab[0], xbuf[0], 8);
1969 	sg_set_buf(&src_tab[1], xbuf[0] + 8, vecs->m_size - 8);
1970 	sg_init_one(&dst, outbuf_enc, out_len_max);
1971 	akcipher_request_set_crypt(req, src_tab, &dst, vecs->m_size,
1972 				   out_len_max);
1973 	akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1974 				      tcrypt_complete, &result);
1975 
1976 	/* Run RSA encrypt - c = m^e mod n;*/
1977 	err = wait_async_op(&result, crypto_akcipher_encrypt(req));
1978 	if (err) {
1979 		pr_err("alg: akcipher: encrypt test failed. err %d\n", err);
1980 		goto free_all;
1981 	}
1982 	if (req->dst_len != vecs->c_size) {
1983 		pr_err("alg: akcipher: encrypt test failed. Invalid output len\n");
1984 		err = -EINVAL;
1985 		goto free_all;
1986 	}
1987 	/* verify that encrypted message is equal to expected */
1988 	if (memcmp(vecs->c, outbuf_enc, vecs->c_size)) {
1989 		pr_err("alg: akcipher: encrypt test failed. Invalid output\n");
1990 		hexdump(outbuf_enc, vecs->c_size);
1991 		err = -EINVAL;
1992 		goto free_all;
1993 	}
1994 	/* Don't invoke decrypt for vectors with public key */
1995 	if (vecs->public_key_vec) {
1996 		err = 0;
1997 		goto free_all;
1998 	}
1999 	outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
2000 	if (!outbuf_dec) {
2001 		err = -ENOMEM;
2002 		goto free_all;
2003 	}
2004 
2005 	if (WARN_ON(vecs->c_size > PAGE_SIZE))
2006 		goto free_all;
2007 
2008 	memcpy(xbuf[0], vecs->c, vecs->c_size);
2009 
2010 	sg_init_one(&src, xbuf[0], vecs->c_size);
2011 	sg_init_one(&dst, outbuf_dec, out_len_max);
2012 	init_completion(&result.completion);
2013 	akcipher_request_set_crypt(req, &src, &dst, vecs->c_size, out_len_max);
2014 
2015 	/* Run RSA decrypt - m = c^d mod n;*/
2016 	err = wait_async_op(&result, crypto_akcipher_decrypt(req));
2017 	if (err) {
2018 		pr_err("alg: akcipher: decrypt test failed. err %d\n", err);
2019 		goto free_all;
2020 	}
2021 	out_len = req->dst_len;
2022 	if (out_len < vecs->m_size) {
2023 		pr_err("alg: akcipher: decrypt test failed. "
2024 		       "Invalid output len %u\n", out_len);
2025 		err = -EINVAL;
2026 		goto free_all;
2027 	}
2028 	/* verify that decrypted message is equal to the original msg */
2029 	if (memchr_inv(outbuf_dec, 0, out_len - vecs->m_size) ||
2030 	    memcmp(vecs->m, outbuf_dec + out_len - vecs->m_size,
2031 		   vecs->m_size)) {
2032 		pr_err("alg: akcipher: decrypt test failed. Invalid output\n");
2033 		hexdump(outbuf_dec, out_len);
2034 		err = -EINVAL;
2035 	}
2036 free_all:
2037 	kfree(outbuf_dec);
2038 	kfree(outbuf_enc);
2039 free_req:
2040 	akcipher_request_free(req);
2041 free_xbuf:
2042 	testmgr_free_buf(xbuf);
2043 	return err;
2044 }
2045 
2046 static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
2047 			 struct akcipher_testvec *vecs, unsigned int tcount)
2048 {
2049 	const char *algo =
2050 		crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
2051 	int ret, i;
2052 
2053 	for (i = 0; i < tcount; i++) {
2054 		ret = test_akcipher_one(tfm, vecs++);
2055 		if (!ret)
2056 			continue;
2057 
2058 		pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
2059 		       i + 1, algo, ret);
2060 		return ret;
2061 	}
2062 	return 0;
2063 }
2064 
2065 static int alg_test_akcipher(const struct alg_test_desc *desc,
2066 			     const char *driver, u32 type, u32 mask)
2067 {
2068 	struct crypto_akcipher *tfm;
2069 	int err = 0;
2070 
2071 	tfm = crypto_alloc_akcipher(driver, type | CRYPTO_ALG_INTERNAL, mask);
2072 	if (IS_ERR(tfm)) {
2073 		pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
2074 		       driver, PTR_ERR(tfm));
2075 		return PTR_ERR(tfm);
2076 	}
2077 	if (desc->suite.akcipher.vecs)
2078 		err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
2079 				    desc->suite.akcipher.count);
2080 
2081 	crypto_free_akcipher(tfm);
2082 	return err;
2083 }
2084 
2085 static int alg_test_null(const struct alg_test_desc *desc,
2086 			     const char *driver, u32 type, u32 mask)
2087 {
2088 	return 0;
2089 }
2090 
2091 /* Please keep this list sorted by algorithm name. */
2092 static const struct alg_test_desc alg_test_descs[] = {
2093 	{
2094 		.alg = "__cbc-cast5-avx",
2095 		.test = alg_test_null,
2096 	}, {
2097 		.alg = "__cbc-cast6-avx",
2098 		.test = alg_test_null,
2099 	}, {
2100 		.alg = "__cbc-serpent-avx",
2101 		.test = alg_test_null,
2102 	}, {
2103 		.alg = "__cbc-serpent-avx2",
2104 		.test = alg_test_null,
2105 	}, {
2106 		.alg = "__cbc-serpent-sse2",
2107 		.test = alg_test_null,
2108 	}, {
2109 		.alg = "__cbc-twofish-avx",
2110 		.test = alg_test_null,
2111 	}, {
2112 		.alg = "__driver-cbc-aes-aesni",
2113 		.test = alg_test_null,
2114 		.fips_allowed = 1,
2115 	}, {
2116 		.alg = "__driver-cbc-camellia-aesni",
2117 		.test = alg_test_null,
2118 	}, {
2119 		.alg = "__driver-cbc-camellia-aesni-avx2",
2120 		.test = alg_test_null,
2121 	}, {
2122 		.alg = "__driver-cbc-cast5-avx",
2123 		.test = alg_test_null,
2124 	}, {
2125 		.alg = "__driver-cbc-cast6-avx",
2126 		.test = alg_test_null,
2127 	}, {
2128 		.alg = "__driver-cbc-serpent-avx",
2129 		.test = alg_test_null,
2130 	}, {
2131 		.alg = "__driver-cbc-serpent-avx2",
2132 		.test = alg_test_null,
2133 	}, {
2134 		.alg = "__driver-cbc-serpent-sse2",
2135 		.test = alg_test_null,
2136 	}, {
2137 		.alg = "__driver-cbc-twofish-avx",
2138 		.test = alg_test_null,
2139 	}, {
2140 		.alg = "__driver-ecb-aes-aesni",
2141 		.test = alg_test_null,
2142 		.fips_allowed = 1,
2143 	}, {
2144 		.alg = "__driver-ecb-camellia-aesni",
2145 		.test = alg_test_null,
2146 	}, {
2147 		.alg = "__driver-ecb-camellia-aesni-avx2",
2148 		.test = alg_test_null,
2149 	}, {
2150 		.alg = "__driver-ecb-cast5-avx",
2151 		.test = alg_test_null,
2152 	}, {
2153 		.alg = "__driver-ecb-cast6-avx",
2154 		.test = alg_test_null,
2155 	}, {
2156 		.alg = "__driver-ecb-serpent-avx",
2157 		.test = alg_test_null,
2158 	}, {
2159 		.alg = "__driver-ecb-serpent-avx2",
2160 		.test = alg_test_null,
2161 	}, {
2162 		.alg = "__driver-ecb-serpent-sse2",
2163 		.test = alg_test_null,
2164 	}, {
2165 		.alg = "__driver-ecb-twofish-avx",
2166 		.test = alg_test_null,
2167 	}, {
2168 		.alg = "__driver-gcm-aes-aesni",
2169 		.test = alg_test_null,
2170 		.fips_allowed = 1,
2171 	}, {
2172 		.alg = "__ghash-pclmulqdqni",
2173 		.test = alg_test_null,
2174 		.fips_allowed = 1,
2175 	}, {
2176 		.alg = "ansi_cprng",
2177 		.test = alg_test_cprng,
2178 		.suite = {
2179 			.cprng = {
2180 				.vecs = ansi_cprng_aes_tv_template,
2181 				.count = ANSI_CPRNG_AES_TEST_VECTORS
2182 			}
2183 		}
2184 	}, {
2185 		.alg = "authenc(hmac(md5),ecb(cipher_null))",
2186 		.test = alg_test_aead,
2187 		.suite = {
2188 			.aead = {
2189 				.enc = {
2190 					.vecs = hmac_md5_ecb_cipher_null_enc_tv_template,
2191 					.count = HMAC_MD5_ECB_CIPHER_NULL_ENC_TEST_VECTORS
2192 				},
2193 				.dec = {
2194 					.vecs = hmac_md5_ecb_cipher_null_dec_tv_template,
2195 					.count = HMAC_MD5_ECB_CIPHER_NULL_DEC_TEST_VECTORS
2196 				}
2197 			}
2198 		}
2199 	}, {
2200 		.alg = "authenc(hmac(sha1),cbc(aes))",
2201 		.test = alg_test_aead,
2202 		.suite = {
2203 			.aead = {
2204 				.enc = {
2205 					.vecs =
2206 					hmac_sha1_aes_cbc_enc_tv_temp,
2207 					.count =
2208 					HMAC_SHA1_AES_CBC_ENC_TEST_VEC
2209 				}
2210 			}
2211 		}
2212 	}, {
2213 		.alg = "authenc(hmac(sha1),cbc(des))",
2214 		.test = alg_test_aead,
2215 		.suite = {
2216 			.aead = {
2217 				.enc = {
2218 					.vecs =
2219 					hmac_sha1_des_cbc_enc_tv_temp,
2220 					.count =
2221 					HMAC_SHA1_DES_CBC_ENC_TEST_VEC
2222 				}
2223 			}
2224 		}
2225 	}, {
2226 		.alg = "authenc(hmac(sha1),cbc(des3_ede))",
2227 		.test = alg_test_aead,
2228 		.fips_allowed = 1,
2229 		.suite = {
2230 			.aead = {
2231 				.enc = {
2232 					.vecs =
2233 					hmac_sha1_des3_ede_cbc_enc_tv_temp,
2234 					.count =
2235 					HMAC_SHA1_DES3_EDE_CBC_ENC_TEST_VEC
2236 				}
2237 			}
2238 		}
2239 	}, {
2240 		.alg = "authenc(hmac(sha1),ctr(aes))",
2241 		.test = alg_test_null,
2242 		.fips_allowed = 1,
2243 	}, {
2244 		.alg = "authenc(hmac(sha1),ecb(cipher_null))",
2245 		.test = alg_test_aead,
2246 		.suite = {
2247 			.aead = {
2248 				.enc = {
2249 					.vecs =
2250 					hmac_sha1_ecb_cipher_null_enc_tv_temp,
2251 					.count =
2252 					HMAC_SHA1_ECB_CIPHER_NULL_ENC_TEST_VEC
2253 				},
2254 				.dec = {
2255 					.vecs =
2256 					hmac_sha1_ecb_cipher_null_dec_tv_temp,
2257 					.count =
2258 					HMAC_SHA1_ECB_CIPHER_NULL_DEC_TEST_VEC
2259 				}
2260 			}
2261 		}
2262 	}, {
2263 		.alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
2264 		.test = alg_test_null,
2265 		.fips_allowed = 1,
2266 	}, {
2267 		.alg = "authenc(hmac(sha224),cbc(des))",
2268 		.test = alg_test_aead,
2269 		.suite = {
2270 			.aead = {
2271 				.enc = {
2272 					.vecs =
2273 					hmac_sha224_des_cbc_enc_tv_temp,
2274 					.count =
2275 					HMAC_SHA224_DES_CBC_ENC_TEST_VEC
2276 				}
2277 			}
2278 		}
2279 	}, {
2280 		.alg = "authenc(hmac(sha224),cbc(des3_ede))",
2281 		.test = alg_test_aead,
2282 		.fips_allowed = 1,
2283 		.suite = {
2284 			.aead = {
2285 				.enc = {
2286 					.vecs =
2287 					hmac_sha224_des3_ede_cbc_enc_tv_temp,
2288 					.count =
2289 					HMAC_SHA224_DES3_EDE_CBC_ENC_TEST_VEC
2290 				}
2291 			}
2292 		}
2293 	}, {
2294 		.alg = "authenc(hmac(sha256),cbc(aes))",
2295 		.test = alg_test_aead,
2296 		.fips_allowed = 1,
2297 		.suite = {
2298 			.aead = {
2299 				.enc = {
2300 					.vecs =
2301 					hmac_sha256_aes_cbc_enc_tv_temp,
2302 					.count =
2303 					HMAC_SHA256_AES_CBC_ENC_TEST_VEC
2304 				}
2305 			}
2306 		}
2307 	}, {
2308 		.alg = "authenc(hmac(sha256),cbc(des))",
2309 		.test = alg_test_aead,
2310 		.suite = {
2311 			.aead = {
2312 				.enc = {
2313 					.vecs =
2314 					hmac_sha256_des_cbc_enc_tv_temp,
2315 					.count =
2316 					HMAC_SHA256_DES_CBC_ENC_TEST_VEC
2317 				}
2318 			}
2319 		}
2320 	}, {
2321 		.alg = "authenc(hmac(sha256),cbc(des3_ede))",
2322 		.test = alg_test_aead,
2323 		.fips_allowed = 1,
2324 		.suite = {
2325 			.aead = {
2326 				.enc = {
2327 					.vecs =
2328 					hmac_sha256_des3_ede_cbc_enc_tv_temp,
2329 					.count =
2330 					HMAC_SHA256_DES3_EDE_CBC_ENC_TEST_VEC
2331 				}
2332 			}
2333 		}
2334 	}, {
2335 		.alg = "authenc(hmac(sha256),ctr(aes))",
2336 		.test = alg_test_null,
2337 		.fips_allowed = 1,
2338 	}, {
2339 		.alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
2340 		.test = alg_test_null,
2341 		.fips_allowed = 1,
2342 	}, {
2343 		.alg = "authenc(hmac(sha384),cbc(des))",
2344 		.test = alg_test_aead,
2345 		.suite = {
2346 			.aead = {
2347 				.enc = {
2348 					.vecs =
2349 					hmac_sha384_des_cbc_enc_tv_temp,
2350 					.count =
2351 					HMAC_SHA384_DES_CBC_ENC_TEST_VEC
2352 				}
2353 			}
2354 		}
2355 	}, {
2356 		.alg = "authenc(hmac(sha384),cbc(des3_ede))",
2357 		.test = alg_test_aead,
2358 		.fips_allowed = 1,
2359 		.suite = {
2360 			.aead = {
2361 				.enc = {
2362 					.vecs =
2363 					hmac_sha384_des3_ede_cbc_enc_tv_temp,
2364 					.count =
2365 					HMAC_SHA384_DES3_EDE_CBC_ENC_TEST_VEC
2366 				}
2367 			}
2368 		}
2369 	}, {
2370 		.alg = "authenc(hmac(sha384),ctr(aes))",
2371 		.test = alg_test_null,
2372 		.fips_allowed = 1,
2373 	}, {
2374 		.alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
2375 		.test = alg_test_null,
2376 		.fips_allowed = 1,
2377 	}, {
2378 		.alg = "authenc(hmac(sha512),cbc(aes))",
2379 		.fips_allowed = 1,
2380 		.test = alg_test_aead,
2381 		.suite = {
2382 			.aead = {
2383 				.enc = {
2384 					.vecs =
2385 					hmac_sha512_aes_cbc_enc_tv_temp,
2386 					.count =
2387 					HMAC_SHA512_AES_CBC_ENC_TEST_VEC
2388 				}
2389 			}
2390 		}
2391 	}, {
2392 		.alg = "authenc(hmac(sha512),cbc(des))",
2393 		.test = alg_test_aead,
2394 		.suite = {
2395 			.aead = {
2396 				.enc = {
2397 					.vecs =
2398 					hmac_sha512_des_cbc_enc_tv_temp,
2399 					.count =
2400 					HMAC_SHA512_DES_CBC_ENC_TEST_VEC
2401 				}
2402 			}
2403 		}
2404 	}, {
2405 		.alg = "authenc(hmac(sha512),cbc(des3_ede))",
2406 		.test = alg_test_aead,
2407 		.fips_allowed = 1,
2408 		.suite = {
2409 			.aead = {
2410 				.enc = {
2411 					.vecs =
2412 					hmac_sha512_des3_ede_cbc_enc_tv_temp,
2413 					.count =
2414 					HMAC_SHA512_DES3_EDE_CBC_ENC_TEST_VEC
2415 				}
2416 			}
2417 		}
2418 	}, {
2419 		.alg = "authenc(hmac(sha512),ctr(aes))",
2420 		.test = alg_test_null,
2421 		.fips_allowed = 1,
2422 	}, {
2423 		.alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
2424 		.test = alg_test_null,
2425 		.fips_allowed = 1,
2426 	}, {
2427 		.alg = "cbc(aes)",
2428 		.test = alg_test_skcipher,
2429 		.fips_allowed = 1,
2430 		.suite = {
2431 			.cipher = {
2432 				.enc = {
2433 					.vecs = aes_cbc_enc_tv_template,
2434 					.count = AES_CBC_ENC_TEST_VECTORS
2435 				},
2436 				.dec = {
2437 					.vecs = aes_cbc_dec_tv_template,
2438 					.count = AES_CBC_DEC_TEST_VECTORS
2439 				}
2440 			}
2441 		}
2442 	}, {
2443 		.alg = "cbc(anubis)",
2444 		.test = alg_test_skcipher,
2445 		.suite = {
2446 			.cipher = {
2447 				.enc = {
2448 					.vecs = anubis_cbc_enc_tv_template,
2449 					.count = ANUBIS_CBC_ENC_TEST_VECTORS
2450 				},
2451 				.dec = {
2452 					.vecs = anubis_cbc_dec_tv_template,
2453 					.count = ANUBIS_CBC_DEC_TEST_VECTORS
2454 				}
2455 			}
2456 		}
2457 	}, {
2458 		.alg = "cbc(blowfish)",
2459 		.test = alg_test_skcipher,
2460 		.suite = {
2461 			.cipher = {
2462 				.enc = {
2463 					.vecs = bf_cbc_enc_tv_template,
2464 					.count = BF_CBC_ENC_TEST_VECTORS
2465 				},
2466 				.dec = {
2467 					.vecs = bf_cbc_dec_tv_template,
2468 					.count = BF_CBC_DEC_TEST_VECTORS
2469 				}
2470 			}
2471 		}
2472 	}, {
2473 		.alg = "cbc(camellia)",
2474 		.test = alg_test_skcipher,
2475 		.suite = {
2476 			.cipher = {
2477 				.enc = {
2478 					.vecs = camellia_cbc_enc_tv_template,
2479 					.count = CAMELLIA_CBC_ENC_TEST_VECTORS
2480 				},
2481 				.dec = {
2482 					.vecs = camellia_cbc_dec_tv_template,
2483 					.count = CAMELLIA_CBC_DEC_TEST_VECTORS
2484 				}
2485 			}
2486 		}
2487 	}, {
2488 		.alg = "cbc(cast5)",
2489 		.test = alg_test_skcipher,
2490 		.suite = {
2491 			.cipher = {
2492 				.enc = {
2493 					.vecs = cast5_cbc_enc_tv_template,
2494 					.count = CAST5_CBC_ENC_TEST_VECTORS
2495 				},
2496 				.dec = {
2497 					.vecs = cast5_cbc_dec_tv_template,
2498 					.count = CAST5_CBC_DEC_TEST_VECTORS
2499 				}
2500 			}
2501 		}
2502 	}, {
2503 		.alg = "cbc(cast6)",
2504 		.test = alg_test_skcipher,
2505 		.suite = {
2506 			.cipher = {
2507 				.enc = {
2508 					.vecs = cast6_cbc_enc_tv_template,
2509 					.count = CAST6_CBC_ENC_TEST_VECTORS
2510 				},
2511 				.dec = {
2512 					.vecs = cast6_cbc_dec_tv_template,
2513 					.count = CAST6_CBC_DEC_TEST_VECTORS
2514 				}
2515 			}
2516 		}
2517 	}, {
2518 		.alg = "cbc(des)",
2519 		.test = alg_test_skcipher,
2520 		.suite = {
2521 			.cipher = {
2522 				.enc = {
2523 					.vecs = des_cbc_enc_tv_template,
2524 					.count = DES_CBC_ENC_TEST_VECTORS
2525 				},
2526 				.dec = {
2527 					.vecs = des_cbc_dec_tv_template,
2528 					.count = DES_CBC_DEC_TEST_VECTORS
2529 				}
2530 			}
2531 		}
2532 	}, {
2533 		.alg = "cbc(des3_ede)",
2534 		.test = alg_test_skcipher,
2535 		.fips_allowed = 1,
2536 		.suite = {
2537 			.cipher = {
2538 				.enc = {
2539 					.vecs = des3_ede_cbc_enc_tv_template,
2540 					.count = DES3_EDE_CBC_ENC_TEST_VECTORS
2541 				},
2542 				.dec = {
2543 					.vecs = des3_ede_cbc_dec_tv_template,
2544 					.count = DES3_EDE_CBC_DEC_TEST_VECTORS
2545 				}
2546 			}
2547 		}
2548 	}, {
2549 		.alg = "cbc(serpent)",
2550 		.test = alg_test_skcipher,
2551 		.suite = {
2552 			.cipher = {
2553 				.enc = {
2554 					.vecs = serpent_cbc_enc_tv_template,
2555 					.count = SERPENT_CBC_ENC_TEST_VECTORS
2556 				},
2557 				.dec = {
2558 					.vecs = serpent_cbc_dec_tv_template,
2559 					.count = SERPENT_CBC_DEC_TEST_VECTORS
2560 				}
2561 			}
2562 		}
2563 	}, {
2564 		.alg = "cbc(twofish)",
2565 		.test = alg_test_skcipher,
2566 		.suite = {
2567 			.cipher = {
2568 				.enc = {
2569 					.vecs = tf_cbc_enc_tv_template,
2570 					.count = TF_CBC_ENC_TEST_VECTORS
2571 				},
2572 				.dec = {
2573 					.vecs = tf_cbc_dec_tv_template,
2574 					.count = TF_CBC_DEC_TEST_VECTORS
2575 				}
2576 			}
2577 		}
2578 	}, {
2579 		.alg = "ccm(aes)",
2580 		.test = alg_test_aead,
2581 		.fips_allowed = 1,
2582 		.suite = {
2583 			.aead = {
2584 				.enc = {
2585 					.vecs = aes_ccm_enc_tv_template,
2586 					.count = AES_CCM_ENC_TEST_VECTORS
2587 				},
2588 				.dec = {
2589 					.vecs = aes_ccm_dec_tv_template,
2590 					.count = AES_CCM_DEC_TEST_VECTORS
2591 				}
2592 			}
2593 		}
2594 	}, {
2595 		.alg = "chacha20",
2596 		.test = alg_test_skcipher,
2597 		.suite = {
2598 			.cipher = {
2599 				.enc = {
2600 					.vecs = chacha20_enc_tv_template,
2601 					.count = CHACHA20_ENC_TEST_VECTORS
2602 				},
2603 				.dec = {
2604 					.vecs = chacha20_enc_tv_template,
2605 					.count = CHACHA20_ENC_TEST_VECTORS
2606 				},
2607 			}
2608 		}
2609 	}, {
2610 		.alg = "cmac(aes)",
2611 		.fips_allowed = 1,
2612 		.test = alg_test_hash,
2613 		.suite = {
2614 			.hash = {
2615 				.vecs = aes_cmac128_tv_template,
2616 				.count = CMAC_AES_TEST_VECTORS
2617 			}
2618 		}
2619 	}, {
2620 		.alg = "cmac(des3_ede)",
2621 		.fips_allowed = 1,
2622 		.test = alg_test_hash,
2623 		.suite = {
2624 			.hash = {
2625 				.vecs = des3_ede_cmac64_tv_template,
2626 				.count = CMAC_DES3_EDE_TEST_VECTORS
2627 			}
2628 		}
2629 	}, {
2630 		.alg = "compress_null",
2631 		.test = alg_test_null,
2632 	}, {
2633 		.alg = "crc32",
2634 		.test = alg_test_hash,
2635 		.suite = {
2636 			.hash = {
2637 				.vecs = crc32_tv_template,
2638 				.count = CRC32_TEST_VECTORS
2639 			}
2640 		}
2641 	}, {
2642 		.alg = "crc32c",
2643 		.test = alg_test_crc32c,
2644 		.fips_allowed = 1,
2645 		.suite = {
2646 			.hash = {
2647 				.vecs = crc32c_tv_template,
2648 				.count = CRC32C_TEST_VECTORS
2649 			}
2650 		}
2651 	}, {
2652 		.alg = "crct10dif",
2653 		.test = alg_test_hash,
2654 		.fips_allowed = 1,
2655 		.suite = {
2656 			.hash = {
2657 				.vecs = crct10dif_tv_template,
2658 				.count = CRCT10DIF_TEST_VECTORS
2659 			}
2660 		}
2661 	}, {
2662 		.alg = "cryptd(__driver-cbc-aes-aesni)",
2663 		.test = alg_test_null,
2664 		.fips_allowed = 1,
2665 	}, {
2666 		.alg = "cryptd(__driver-cbc-camellia-aesni)",
2667 		.test = alg_test_null,
2668 	}, {
2669 		.alg = "cryptd(__driver-cbc-camellia-aesni-avx2)",
2670 		.test = alg_test_null,
2671 	}, {
2672 		.alg = "cryptd(__driver-cbc-serpent-avx2)",
2673 		.test = alg_test_null,
2674 	}, {
2675 		.alg = "cryptd(__driver-ecb-aes-aesni)",
2676 		.test = alg_test_null,
2677 		.fips_allowed = 1,
2678 	}, {
2679 		.alg = "cryptd(__driver-ecb-camellia-aesni)",
2680 		.test = alg_test_null,
2681 	}, {
2682 		.alg = "cryptd(__driver-ecb-camellia-aesni-avx2)",
2683 		.test = alg_test_null,
2684 	}, {
2685 		.alg = "cryptd(__driver-ecb-cast5-avx)",
2686 		.test = alg_test_null,
2687 	}, {
2688 		.alg = "cryptd(__driver-ecb-cast6-avx)",
2689 		.test = alg_test_null,
2690 	}, {
2691 		.alg = "cryptd(__driver-ecb-serpent-avx)",
2692 		.test = alg_test_null,
2693 	}, {
2694 		.alg = "cryptd(__driver-ecb-serpent-avx2)",
2695 		.test = alg_test_null,
2696 	}, {
2697 		.alg = "cryptd(__driver-ecb-serpent-sse2)",
2698 		.test = alg_test_null,
2699 	}, {
2700 		.alg = "cryptd(__driver-ecb-twofish-avx)",
2701 		.test = alg_test_null,
2702 	}, {
2703 		.alg = "cryptd(__driver-gcm-aes-aesni)",
2704 		.test = alg_test_null,
2705 		.fips_allowed = 1,
2706 	}, {
2707 		.alg = "cryptd(__ghash-pclmulqdqni)",
2708 		.test = alg_test_null,
2709 		.fips_allowed = 1,
2710 	}, {
2711 		.alg = "ctr(aes)",
2712 		.test = alg_test_skcipher,
2713 		.fips_allowed = 1,
2714 		.suite = {
2715 			.cipher = {
2716 				.enc = {
2717 					.vecs = aes_ctr_enc_tv_template,
2718 					.count = AES_CTR_ENC_TEST_VECTORS
2719 				},
2720 				.dec = {
2721 					.vecs = aes_ctr_dec_tv_template,
2722 					.count = AES_CTR_DEC_TEST_VECTORS
2723 				}
2724 			}
2725 		}
2726 	}, {
2727 		.alg = "ctr(blowfish)",
2728 		.test = alg_test_skcipher,
2729 		.suite = {
2730 			.cipher = {
2731 				.enc = {
2732 					.vecs = bf_ctr_enc_tv_template,
2733 					.count = BF_CTR_ENC_TEST_VECTORS
2734 				},
2735 				.dec = {
2736 					.vecs = bf_ctr_dec_tv_template,
2737 					.count = BF_CTR_DEC_TEST_VECTORS
2738 				}
2739 			}
2740 		}
2741 	}, {
2742 		.alg = "ctr(camellia)",
2743 		.test = alg_test_skcipher,
2744 		.suite = {
2745 			.cipher = {
2746 				.enc = {
2747 					.vecs = camellia_ctr_enc_tv_template,
2748 					.count = CAMELLIA_CTR_ENC_TEST_VECTORS
2749 				},
2750 				.dec = {
2751 					.vecs = camellia_ctr_dec_tv_template,
2752 					.count = CAMELLIA_CTR_DEC_TEST_VECTORS
2753 				}
2754 			}
2755 		}
2756 	}, {
2757 		.alg = "ctr(cast5)",
2758 		.test = alg_test_skcipher,
2759 		.suite = {
2760 			.cipher = {
2761 				.enc = {
2762 					.vecs = cast5_ctr_enc_tv_template,
2763 					.count = CAST5_CTR_ENC_TEST_VECTORS
2764 				},
2765 				.dec = {
2766 					.vecs = cast5_ctr_dec_tv_template,
2767 					.count = CAST5_CTR_DEC_TEST_VECTORS
2768 				}
2769 			}
2770 		}
2771 	}, {
2772 		.alg = "ctr(cast6)",
2773 		.test = alg_test_skcipher,
2774 		.suite = {
2775 			.cipher = {
2776 				.enc = {
2777 					.vecs = cast6_ctr_enc_tv_template,
2778 					.count = CAST6_CTR_ENC_TEST_VECTORS
2779 				},
2780 				.dec = {
2781 					.vecs = cast6_ctr_dec_tv_template,
2782 					.count = CAST6_CTR_DEC_TEST_VECTORS
2783 				}
2784 			}
2785 		}
2786 	}, {
2787 		.alg = "ctr(des)",
2788 		.test = alg_test_skcipher,
2789 		.suite = {
2790 			.cipher = {
2791 				.enc = {
2792 					.vecs = des_ctr_enc_tv_template,
2793 					.count = DES_CTR_ENC_TEST_VECTORS
2794 				},
2795 				.dec = {
2796 					.vecs = des_ctr_dec_tv_template,
2797 					.count = DES_CTR_DEC_TEST_VECTORS
2798 				}
2799 			}
2800 		}
2801 	}, {
2802 		.alg = "ctr(des3_ede)",
2803 		.test = alg_test_skcipher,
2804 		.suite = {
2805 			.cipher = {
2806 				.enc = {
2807 					.vecs = des3_ede_ctr_enc_tv_template,
2808 					.count = DES3_EDE_CTR_ENC_TEST_VECTORS
2809 				},
2810 				.dec = {
2811 					.vecs = des3_ede_ctr_dec_tv_template,
2812 					.count = DES3_EDE_CTR_DEC_TEST_VECTORS
2813 				}
2814 			}
2815 		}
2816 	}, {
2817 		.alg = "ctr(serpent)",
2818 		.test = alg_test_skcipher,
2819 		.suite = {
2820 			.cipher = {
2821 				.enc = {
2822 					.vecs = serpent_ctr_enc_tv_template,
2823 					.count = SERPENT_CTR_ENC_TEST_VECTORS
2824 				},
2825 				.dec = {
2826 					.vecs = serpent_ctr_dec_tv_template,
2827 					.count = SERPENT_CTR_DEC_TEST_VECTORS
2828 				}
2829 			}
2830 		}
2831 	}, {
2832 		.alg = "ctr(twofish)",
2833 		.test = alg_test_skcipher,
2834 		.suite = {
2835 			.cipher = {
2836 				.enc = {
2837 					.vecs = tf_ctr_enc_tv_template,
2838 					.count = TF_CTR_ENC_TEST_VECTORS
2839 				},
2840 				.dec = {
2841 					.vecs = tf_ctr_dec_tv_template,
2842 					.count = TF_CTR_DEC_TEST_VECTORS
2843 				}
2844 			}
2845 		}
2846 	}, {
2847 		.alg = "cts(cbc(aes))",
2848 		.test = alg_test_skcipher,
2849 		.suite = {
2850 			.cipher = {
2851 				.enc = {
2852 					.vecs = cts_mode_enc_tv_template,
2853 					.count = CTS_MODE_ENC_TEST_VECTORS
2854 				},
2855 				.dec = {
2856 					.vecs = cts_mode_dec_tv_template,
2857 					.count = CTS_MODE_DEC_TEST_VECTORS
2858 				}
2859 			}
2860 		}
2861 	}, {
2862 		.alg = "deflate",
2863 		.test = alg_test_comp,
2864 		.fips_allowed = 1,
2865 		.suite = {
2866 			.comp = {
2867 				.comp = {
2868 					.vecs = deflate_comp_tv_template,
2869 					.count = DEFLATE_COMP_TEST_VECTORS
2870 				},
2871 				.decomp = {
2872 					.vecs = deflate_decomp_tv_template,
2873 					.count = DEFLATE_DECOMP_TEST_VECTORS
2874 				}
2875 			}
2876 		}
2877 	}, {
2878 		.alg = "dh",
2879 		.test = alg_test_kpp,
2880 		.fips_allowed = 1,
2881 		.suite = {
2882 			.kpp = {
2883 				.vecs = dh_tv_template,
2884 				.count = DH_TEST_VECTORS
2885 			}
2886 		}
2887 	}, {
2888 		.alg = "digest_null",
2889 		.test = alg_test_null,
2890 	}, {
2891 		.alg = "drbg_nopr_ctr_aes128",
2892 		.test = alg_test_drbg,
2893 		.fips_allowed = 1,
2894 		.suite = {
2895 			.drbg = {
2896 				.vecs = drbg_nopr_ctr_aes128_tv_template,
2897 				.count = ARRAY_SIZE(drbg_nopr_ctr_aes128_tv_template)
2898 			}
2899 		}
2900 	}, {
2901 		.alg = "drbg_nopr_ctr_aes192",
2902 		.test = alg_test_drbg,
2903 		.fips_allowed = 1,
2904 		.suite = {
2905 			.drbg = {
2906 				.vecs = drbg_nopr_ctr_aes192_tv_template,
2907 				.count = ARRAY_SIZE(drbg_nopr_ctr_aes192_tv_template)
2908 			}
2909 		}
2910 	}, {
2911 		.alg = "drbg_nopr_ctr_aes256",
2912 		.test = alg_test_drbg,
2913 		.fips_allowed = 1,
2914 		.suite = {
2915 			.drbg = {
2916 				.vecs = drbg_nopr_ctr_aes256_tv_template,
2917 				.count = ARRAY_SIZE(drbg_nopr_ctr_aes256_tv_template)
2918 			}
2919 		}
2920 	}, {
2921 		/*
2922 		 * There is no need to specifically test the DRBG with every
2923 		 * backend cipher -- covered by drbg_nopr_hmac_sha256 test
2924 		 */
2925 		.alg = "drbg_nopr_hmac_sha1",
2926 		.fips_allowed = 1,
2927 		.test = alg_test_null,
2928 	}, {
2929 		.alg = "drbg_nopr_hmac_sha256",
2930 		.test = alg_test_drbg,
2931 		.fips_allowed = 1,
2932 		.suite = {
2933 			.drbg = {
2934 				.vecs = drbg_nopr_hmac_sha256_tv_template,
2935 				.count =
2936 				ARRAY_SIZE(drbg_nopr_hmac_sha256_tv_template)
2937 			}
2938 		}
2939 	}, {
2940 		/* covered by drbg_nopr_hmac_sha256 test */
2941 		.alg = "drbg_nopr_hmac_sha384",
2942 		.fips_allowed = 1,
2943 		.test = alg_test_null,
2944 	}, {
2945 		.alg = "drbg_nopr_hmac_sha512",
2946 		.test = alg_test_null,
2947 		.fips_allowed = 1,
2948 	}, {
2949 		.alg = "drbg_nopr_sha1",
2950 		.fips_allowed = 1,
2951 		.test = alg_test_null,
2952 	}, {
2953 		.alg = "drbg_nopr_sha256",
2954 		.test = alg_test_drbg,
2955 		.fips_allowed = 1,
2956 		.suite = {
2957 			.drbg = {
2958 				.vecs = drbg_nopr_sha256_tv_template,
2959 				.count = ARRAY_SIZE(drbg_nopr_sha256_tv_template)
2960 			}
2961 		}
2962 	}, {
2963 		/* covered by drbg_nopr_sha256 test */
2964 		.alg = "drbg_nopr_sha384",
2965 		.fips_allowed = 1,
2966 		.test = alg_test_null,
2967 	}, {
2968 		.alg = "drbg_nopr_sha512",
2969 		.fips_allowed = 1,
2970 		.test = alg_test_null,
2971 	}, {
2972 		.alg = "drbg_pr_ctr_aes128",
2973 		.test = alg_test_drbg,
2974 		.fips_allowed = 1,
2975 		.suite = {
2976 			.drbg = {
2977 				.vecs = drbg_pr_ctr_aes128_tv_template,
2978 				.count = ARRAY_SIZE(drbg_pr_ctr_aes128_tv_template)
2979 			}
2980 		}
2981 	}, {
2982 		/* covered by drbg_pr_ctr_aes128 test */
2983 		.alg = "drbg_pr_ctr_aes192",
2984 		.fips_allowed = 1,
2985 		.test = alg_test_null,
2986 	}, {
2987 		.alg = "drbg_pr_ctr_aes256",
2988 		.fips_allowed = 1,
2989 		.test = alg_test_null,
2990 	}, {
2991 		.alg = "drbg_pr_hmac_sha1",
2992 		.fips_allowed = 1,
2993 		.test = alg_test_null,
2994 	}, {
2995 		.alg = "drbg_pr_hmac_sha256",
2996 		.test = alg_test_drbg,
2997 		.fips_allowed = 1,
2998 		.suite = {
2999 			.drbg = {
3000 				.vecs = drbg_pr_hmac_sha256_tv_template,
3001 				.count = ARRAY_SIZE(drbg_pr_hmac_sha256_tv_template)
3002 			}
3003 		}
3004 	}, {
3005 		/* covered by drbg_pr_hmac_sha256 test */
3006 		.alg = "drbg_pr_hmac_sha384",
3007 		.fips_allowed = 1,
3008 		.test = alg_test_null,
3009 	}, {
3010 		.alg = "drbg_pr_hmac_sha512",
3011 		.test = alg_test_null,
3012 		.fips_allowed = 1,
3013 	}, {
3014 		.alg = "drbg_pr_sha1",
3015 		.fips_allowed = 1,
3016 		.test = alg_test_null,
3017 	}, {
3018 		.alg = "drbg_pr_sha256",
3019 		.test = alg_test_drbg,
3020 		.fips_allowed = 1,
3021 		.suite = {
3022 			.drbg = {
3023 				.vecs = drbg_pr_sha256_tv_template,
3024 				.count = ARRAY_SIZE(drbg_pr_sha256_tv_template)
3025 			}
3026 		}
3027 	}, {
3028 		/* covered by drbg_pr_sha256 test */
3029 		.alg = "drbg_pr_sha384",
3030 		.fips_allowed = 1,
3031 		.test = alg_test_null,
3032 	}, {
3033 		.alg = "drbg_pr_sha512",
3034 		.fips_allowed = 1,
3035 		.test = alg_test_null,
3036 	}, {
3037 		.alg = "ecb(__aes-aesni)",
3038 		.test = alg_test_null,
3039 		.fips_allowed = 1,
3040 	}, {
3041 		.alg = "ecb(aes)",
3042 		.test = alg_test_skcipher,
3043 		.fips_allowed = 1,
3044 		.suite = {
3045 			.cipher = {
3046 				.enc = {
3047 					.vecs = aes_enc_tv_template,
3048 					.count = AES_ENC_TEST_VECTORS
3049 				},
3050 				.dec = {
3051 					.vecs = aes_dec_tv_template,
3052 					.count = AES_DEC_TEST_VECTORS
3053 				}
3054 			}
3055 		}
3056 	}, {
3057 		.alg = "ecb(anubis)",
3058 		.test = alg_test_skcipher,
3059 		.suite = {
3060 			.cipher = {
3061 				.enc = {
3062 					.vecs = anubis_enc_tv_template,
3063 					.count = ANUBIS_ENC_TEST_VECTORS
3064 				},
3065 				.dec = {
3066 					.vecs = anubis_dec_tv_template,
3067 					.count = ANUBIS_DEC_TEST_VECTORS
3068 				}
3069 			}
3070 		}
3071 	}, {
3072 		.alg = "ecb(arc4)",
3073 		.test = alg_test_skcipher,
3074 		.suite = {
3075 			.cipher = {
3076 				.enc = {
3077 					.vecs = arc4_enc_tv_template,
3078 					.count = ARC4_ENC_TEST_VECTORS
3079 				},
3080 				.dec = {
3081 					.vecs = arc4_dec_tv_template,
3082 					.count = ARC4_DEC_TEST_VECTORS
3083 				}
3084 			}
3085 		}
3086 	}, {
3087 		.alg = "ecb(blowfish)",
3088 		.test = alg_test_skcipher,
3089 		.suite = {
3090 			.cipher = {
3091 				.enc = {
3092 					.vecs = bf_enc_tv_template,
3093 					.count = BF_ENC_TEST_VECTORS
3094 				},
3095 				.dec = {
3096 					.vecs = bf_dec_tv_template,
3097 					.count = BF_DEC_TEST_VECTORS
3098 				}
3099 			}
3100 		}
3101 	}, {
3102 		.alg = "ecb(camellia)",
3103 		.test = alg_test_skcipher,
3104 		.suite = {
3105 			.cipher = {
3106 				.enc = {
3107 					.vecs = camellia_enc_tv_template,
3108 					.count = CAMELLIA_ENC_TEST_VECTORS
3109 				},
3110 				.dec = {
3111 					.vecs = camellia_dec_tv_template,
3112 					.count = CAMELLIA_DEC_TEST_VECTORS
3113 				}
3114 			}
3115 		}
3116 	}, {
3117 		.alg = "ecb(cast5)",
3118 		.test = alg_test_skcipher,
3119 		.suite = {
3120 			.cipher = {
3121 				.enc = {
3122 					.vecs = cast5_enc_tv_template,
3123 					.count = CAST5_ENC_TEST_VECTORS
3124 				},
3125 				.dec = {
3126 					.vecs = cast5_dec_tv_template,
3127 					.count = CAST5_DEC_TEST_VECTORS
3128 				}
3129 			}
3130 		}
3131 	}, {
3132 		.alg = "ecb(cast6)",
3133 		.test = alg_test_skcipher,
3134 		.suite = {
3135 			.cipher = {
3136 				.enc = {
3137 					.vecs = cast6_enc_tv_template,
3138 					.count = CAST6_ENC_TEST_VECTORS
3139 				},
3140 				.dec = {
3141 					.vecs = cast6_dec_tv_template,
3142 					.count = CAST6_DEC_TEST_VECTORS
3143 				}
3144 			}
3145 		}
3146 	}, {
3147 		.alg = "ecb(cipher_null)",
3148 		.test = alg_test_null,
3149 	}, {
3150 		.alg = "ecb(des)",
3151 		.test = alg_test_skcipher,
3152 		.suite = {
3153 			.cipher = {
3154 				.enc = {
3155 					.vecs = des_enc_tv_template,
3156 					.count = DES_ENC_TEST_VECTORS
3157 				},
3158 				.dec = {
3159 					.vecs = des_dec_tv_template,
3160 					.count = DES_DEC_TEST_VECTORS
3161 				}
3162 			}
3163 		}
3164 	}, {
3165 		.alg = "ecb(des3_ede)",
3166 		.test = alg_test_skcipher,
3167 		.fips_allowed = 1,
3168 		.suite = {
3169 			.cipher = {
3170 				.enc = {
3171 					.vecs = des3_ede_enc_tv_template,
3172 					.count = DES3_EDE_ENC_TEST_VECTORS
3173 				},
3174 				.dec = {
3175 					.vecs = des3_ede_dec_tv_template,
3176 					.count = DES3_EDE_DEC_TEST_VECTORS
3177 				}
3178 			}
3179 		}
3180 	}, {
3181 		.alg = "ecb(fcrypt)",
3182 		.test = alg_test_skcipher,
3183 		.suite = {
3184 			.cipher = {
3185 				.enc = {
3186 					.vecs = fcrypt_pcbc_enc_tv_template,
3187 					.count = 1
3188 				},
3189 				.dec = {
3190 					.vecs = fcrypt_pcbc_dec_tv_template,
3191 					.count = 1
3192 				}
3193 			}
3194 		}
3195 	}, {
3196 		.alg = "ecb(khazad)",
3197 		.test = alg_test_skcipher,
3198 		.suite = {
3199 			.cipher = {
3200 				.enc = {
3201 					.vecs = khazad_enc_tv_template,
3202 					.count = KHAZAD_ENC_TEST_VECTORS
3203 				},
3204 				.dec = {
3205 					.vecs = khazad_dec_tv_template,
3206 					.count = KHAZAD_DEC_TEST_VECTORS
3207 				}
3208 			}
3209 		}
3210 	}, {
3211 		.alg = "ecb(seed)",
3212 		.test = alg_test_skcipher,
3213 		.suite = {
3214 			.cipher = {
3215 				.enc = {
3216 					.vecs = seed_enc_tv_template,
3217 					.count = SEED_ENC_TEST_VECTORS
3218 				},
3219 				.dec = {
3220 					.vecs = seed_dec_tv_template,
3221 					.count = SEED_DEC_TEST_VECTORS
3222 				}
3223 			}
3224 		}
3225 	}, {
3226 		.alg = "ecb(serpent)",
3227 		.test = alg_test_skcipher,
3228 		.suite = {
3229 			.cipher = {
3230 				.enc = {
3231 					.vecs = serpent_enc_tv_template,
3232 					.count = SERPENT_ENC_TEST_VECTORS
3233 				},
3234 				.dec = {
3235 					.vecs = serpent_dec_tv_template,
3236 					.count = SERPENT_DEC_TEST_VECTORS
3237 				}
3238 			}
3239 		}
3240 	}, {
3241 		.alg = "ecb(tea)",
3242 		.test = alg_test_skcipher,
3243 		.suite = {
3244 			.cipher = {
3245 				.enc = {
3246 					.vecs = tea_enc_tv_template,
3247 					.count = TEA_ENC_TEST_VECTORS
3248 				},
3249 				.dec = {
3250 					.vecs = tea_dec_tv_template,
3251 					.count = TEA_DEC_TEST_VECTORS
3252 				}
3253 			}
3254 		}
3255 	}, {
3256 		.alg = "ecb(tnepres)",
3257 		.test = alg_test_skcipher,
3258 		.suite = {
3259 			.cipher = {
3260 				.enc = {
3261 					.vecs = tnepres_enc_tv_template,
3262 					.count = TNEPRES_ENC_TEST_VECTORS
3263 				},
3264 				.dec = {
3265 					.vecs = tnepres_dec_tv_template,
3266 					.count = TNEPRES_DEC_TEST_VECTORS
3267 				}
3268 			}
3269 		}
3270 	}, {
3271 		.alg = "ecb(twofish)",
3272 		.test = alg_test_skcipher,
3273 		.suite = {
3274 			.cipher = {
3275 				.enc = {
3276 					.vecs = tf_enc_tv_template,
3277 					.count = TF_ENC_TEST_VECTORS
3278 				},
3279 				.dec = {
3280 					.vecs = tf_dec_tv_template,
3281 					.count = TF_DEC_TEST_VECTORS
3282 				}
3283 			}
3284 		}
3285 	}, {
3286 		.alg = "ecb(xeta)",
3287 		.test = alg_test_skcipher,
3288 		.suite = {
3289 			.cipher = {
3290 				.enc = {
3291 					.vecs = xeta_enc_tv_template,
3292 					.count = XETA_ENC_TEST_VECTORS
3293 				},
3294 				.dec = {
3295 					.vecs = xeta_dec_tv_template,
3296 					.count = XETA_DEC_TEST_VECTORS
3297 				}
3298 			}
3299 		}
3300 	}, {
3301 		.alg = "ecb(xtea)",
3302 		.test = alg_test_skcipher,
3303 		.suite = {
3304 			.cipher = {
3305 				.enc = {
3306 					.vecs = xtea_enc_tv_template,
3307 					.count = XTEA_ENC_TEST_VECTORS
3308 				},
3309 				.dec = {
3310 					.vecs = xtea_dec_tv_template,
3311 					.count = XTEA_DEC_TEST_VECTORS
3312 				}
3313 			}
3314 		}
3315 	}, {
3316 		.alg = "ecdh",
3317 		.test = alg_test_kpp,
3318 		.fips_allowed = 1,
3319 		.suite = {
3320 			.kpp = {
3321 				.vecs = ecdh_tv_template,
3322 				.count = ECDH_TEST_VECTORS
3323 			}
3324 		}
3325 	}, {
3326 		.alg = "gcm(aes)",
3327 		.test = alg_test_aead,
3328 		.fips_allowed = 1,
3329 		.suite = {
3330 			.aead = {
3331 				.enc = {
3332 					.vecs = aes_gcm_enc_tv_template,
3333 					.count = AES_GCM_ENC_TEST_VECTORS
3334 				},
3335 				.dec = {
3336 					.vecs = aes_gcm_dec_tv_template,
3337 					.count = AES_GCM_DEC_TEST_VECTORS
3338 				}
3339 			}
3340 		}
3341 	}, {
3342 		.alg = "ghash",
3343 		.test = alg_test_hash,
3344 		.fips_allowed = 1,
3345 		.suite = {
3346 			.hash = {
3347 				.vecs = ghash_tv_template,
3348 				.count = GHASH_TEST_VECTORS
3349 			}
3350 		}
3351 	}, {
3352 		.alg = "hmac(crc32)",
3353 		.test = alg_test_hash,
3354 		.suite = {
3355 			.hash = {
3356 				.vecs = bfin_crc_tv_template,
3357 				.count = BFIN_CRC_TEST_VECTORS
3358 			}
3359 		}
3360 	}, {
3361 		.alg = "hmac(md5)",
3362 		.test = alg_test_hash,
3363 		.suite = {
3364 			.hash = {
3365 				.vecs = hmac_md5_tv_template,
3366 				.count = HMAC_MD5_TEST_VECTORS
3367 			}
3368 		}
3369 	}, {
3370 		.alg = "hmac(rmd128)",
3371 		.test = alg_test_hash,
3372 		.suite = {
3373 			.hash = {
3374 				.vecs = hmac_rmd128_tv_template,
3375 				.count = HMAC_RMD128_TEST_VECTORS
3376 			}
3377 		}
3378 	}, {
3379 		.alg = "hmac(rmd160)",
3380 		.test = alg_test_hash,
3381 		.suite = {
3382 			.hash = {
3383 				.vecs = hmac_rmd160_tv_template,
3384 				.count = HMAC_RMD160_TEST_VECTORS
3385 			}
3386 		}
3387 	}, {
3388 		.alg = "hmac(sha1)",
3389 		.test = alg_test_hash,
3390 		.fips_allowed = 1,
3391 		.suite = {
3392 			.hash = {
3393 				.vecs = hmac_sha1_tv_template,
3394 				.count = HMAC_SHA1_TEST_VECTORS
3395 			}
3396 		}
3397 	}, {
3398 		.alg = "hmac(sha224)",
3399 		.test = alg_test_hash,
3400 		.fips_allowed = 1,
3401 		.suite = {
3402 			.hash = {
3403 				.vecs = hmac_sha224_tv_template,
3404 				.count = HMAC_SHA224_TEST_VECTORS
3405 			}
3406 		}
3407 	}, {
3408 		.alg = "hmac(sha256)",
3409 		.test = alg_test_hash,
3410 		.fips_allowed = 1,
3411 		.suite = {
3412 			.hash = {
3413 				.vecs = hmac_sha256_tv_template,
3414 				.count = HMAC_SHA256_TEST_VECTORS
3415 			}
3416 		}
3417 	}, {
3418 		.alg = "hmac(sha3-224)",
3419 		.test = alg_test_hash,
3420 		.fips_allowed = 1,
3421 		.suite = {
3422 			.hash = {
3423 				.vecs = hmac_sha3_224_tv_template,
3424 				.count = HMAC_SHA3_224_TEST_VECTORS
3425 			}
3426 		}
3427 	}, {
3428 		.alg = "hmac(sha3-256)",
3429 		.test = alg_test_hash,
3430 		.fips_allowed = 1,
3431 		.suite = {
3432 			.hash = {
3433 				.vecs = hmac_sha3_256_tv_template,
3434 				.count = HMAC_SHA3_256_TEST_VECTORS
3435 			}
3436 		}
3437 	}, {
3438 		.alg = "hmac(sha3-384)",
3439 		.test = alg_test_hash,
3440 		.fips_allowed = 1,
3441 		.suite = {
3442 			.hash = {
3443 				.vecs = hmac_sha3_384_tv_template,
3444 				.count = HMAC_SHA3_384_TEST_VECTORS
3445 			}
3446 		}
3447 	}, {
3448 		.alg = "hmac(sha3-512)",
3449 		.test = alg_test_hash,
3450 		.fips_allowed = 1,
3451 		.suite = {
3452 			.hash = {
3453 				.vecs = hmac_sha3_512_tv_template,
3454 				.count = HMAC_SHA3_512_TEST_VECTORS
3455 			}
3456 		}
3457 	}, {
3458 		.alg = "hmac(sha384)",
3459 		.test = alg_test_hash,
3460 		.fips_allowed = 1,
3461 		.suite = {
3462 			.hash = {
3463 				.vecs = hmac_sha384_tv_template,
3464 				.count = HMAC_SHA384_TEST_VECTORS
3465 			}
3466 		}
3467 	}, {
3468 		.alg = "hmac(sha512)",
3469 		.test = alg_test_hash,
3470 		.fips_allowed = 1,
3471 		.suite = {
3472 			.hash = {
3473 				.vecs = hmac_sha512_tv_template,
3474 				.count = HMAC_SHA512_TEST_VECTORS
3475 			}
3476 		}
3477 	}, {
3478 		.alg = "jitterentropy_rng",
3479 		.fips_allowed = 1,
3480 		.test = alg_test_null,
3481 	}, {
3482 		.alg = "kw(aes)",
3483 		.test = alg_test_skcipher,
3484 		.fips_allowed = 1,
3485 		.suite = {
3486 			.cipher = {
3487 				.enc = {
3488 					.vecs = aes_kw_enc_tv_template,
3489 					.count = ARRAY_SIZE(aes_kw_enc_tv_template)
3490 				},
3491 				.dec = {
3492 					.vecs = aes_kw_dec_tv_template,
3493 					.count = ARRAY_SIZE(aes_kw_dec_tv_template)
3494 				}
3495 			}
3496 		}
3497 	}, {
3498 		.alg = "lrw(aes)",
3499 		.test = alg_test_skcipher,
3500 		.suite = {
3501 			.cipher = {
3502 				.enc = {
3503 					.vecs = aes_lrw_enc_tv_template,
3504 					.count = AES_LRW_ENC_TEST_VECTORS
3505 				},
3506 				.dec = {
3507 					.vecs = aes_lrw_dec_tv_template,
3508 					.count = AES_LRW_DEC_TEST_VECTORS
3509 				}
3510 			}
3511 		}
3512 	}, {
3513 		.alg = "lrw(camellia)",
3514 		.test = alg_test_skcipher,
3515 		.suite = {
3516 			.cipher = {
3517 				.enc = {
3518 					.vecs = camellia_lrw_enc_tv_template,
3519 					.count = CAMELLIA_LRW_ENC_TEST_VECTORS
3520 				},
3521 				.dec = {
3522 					.vecs = camellia_lrw_dec_tv_template,
3523 					.count = CAMELLIA_LRW_DEC_TEST_VECTORS
3524 				}
3525 			}
3526 		}
3527 	}, {
3528 		.alg = "lrw(cast6)",
3529 		.test = alg_test_skcipher,
3530 		.suite = {
3531 			.cipher = {
3532 				.enc = {
3533 					.vecs = cast6_lrw_enc_tv_template,
3534 					.count = CAST6_LRW_ENC_TEST_VECTORS
3535 				},
3536 				.dec = {
3537 					.vecs = cast6_lrw_dec_tv_template,
3538 					.count = CAST6_LRW_DEC_TEST_VECTORS
3539 				}
3540 			}
3541 		}
3542 	}, {
3543 		.alg = "lrw(serpent)",
3544 		.test = alg_test_skcipher,
3545 		.suite = {
3546 			.cipher = {
3547 				.enc = {
3548 					.vecs = serpent_lrw_enc_tv_template,
3549 					.count = SERPENT_LRW_ENC_TEST_VECTORS
3550 				},
3551 				.dec = {
3552 					.vecs = serpent_lrw_dec_tv_template,
3553 					.count = SERPENT_LRW_DEC_TEST_VECTORS
3554 				}
3555 			}
3556 		}
3557 	}, {
3558 		.alg = "lrw(twofish)",
3559 		.test = alg_test_skcipher,
3560 		.suite = {
3561 			.cipher = {
3562 				.enc = {
3563 					.vecs = tf_lrw_enc_tv_template,
3564 					.count = TF_LRW_ENC_TEST_VECTORS
3565 				},
3566 				.dec = {
3567 					.vecs = tf_lrw_dec_tv_template,
3568 					.count = TF_LRW_DEC_TEST_VECTORS
3569 				}
3570 			}
3571 		}
3572 	}, {
3573 		.alg = "lz4",
3574 		.test = alg_test_comp,
3575 		.fips_allowed = 1,
3576 		.suite = {
3577 			.comp = {
3578 				.comp = {
3579 					.vecs = lz4_comp_tv_template,
3580 					.count = LZ4_COMP_TEST_VECTORS
3581 				},
3582 				.decomp = {
3583 					.vecs = lz4_decomp_tv_template,
3584 					.count = LZ4_DECOMP_TEST_VECTORS
3585 				}
3586 			}
3587 		}
3588 	}, {
3589 		.alg = "lz4hc",
3590 		.test = alg_test_comp,
3591 		.fips_allowed = 1,
3592 		.suite = {
3593 			.comp = {
3594 				.comp = {
3595 					.vecs = lz4hc_comp_tv_template,
3596 					.count = LZ4HC_COMP_TEST_VECTORS
3597 				},
3598 				.decomp = {
3599 					.vecs = lz4hc_decomp_tv_template,
3600 					.count = LZ4HC_DECOMP_TEST_VECTORS
3601 				}
3602 			}
3603 		}
3604 	}, {
3605 		.alg = "lzo",
3606 		.test = alg_test_comp,
3607 		.fips_allowed = 1,
3608 		.suite = {
3609 			.comp = {
3610 				.comp = {
3611 					.vecs = lzo_comp_tv_template,
3612 					.count = LZO_COMP_TEST_VECTORS
3613 				},
3614 				.decomp = {
3615 					.vecs = lzo_decomp_tv_template,
3616 					.count = LZO_DECOMP_TEST_VECTORS
3617 				}
3618 			}
3619 		}
3620 	}, {
3621 		.alg = "md4",
3622 		.test = alg_test_hash,
3623 		.suite = {
3624 			.hash = {
3625 				.vecs = md4_tv_template,
3626 				.count = MD4_TEST_VECTORS
3627 			}
3628 		}
3629 	}, {
3630 		.alg = "md5",
3631 		.test = alg_test_hash,
3632 		.suite = {
3633 			.hash = {
3634 				.vecs = md5_tv_template,
3635 				.count = MD5_TEST_VECTORS
3636 			}
3637 		}
3638 	}, {
3639 		.alg = "michael_mic",
3640 		.test = alg_test_hash,
3641 		.suite = {
3642 			.hash = {
3643 				.vecs = michael_mic_tv_template,
3644 				.count = MICHAEL_MIC_TEST_VECTORS
3645 			}
3646 		}
3647 	}, {
3648 		.alg = "ofb(aes)",
3649 		.test = alg_test_skcipher,
3650 		.fips_allowed = 1,
3651 		.suite = {
3652 			.cipher = {
3653 				.enc = {
3654 					.vecs = aes_ofb_enc_tv_template,
3655 					.count = AES_OFB_ENC_TEST_VECTORS
3656 				},
3657 				.dec = {
3658 					.vecs = aes_ofb_dec_tv_template,
3659 					.count = AES_OFB_DEC_TEST_VECTORS
3660 				}
3661 			}
3662 		}
3663 	}, {
3664 		.alg = "pcbc(fcrypt)",
3665 		.test = alg_test_skcipher,
3666 		.suite = {
3667 			.cipher = {
3668 				.enc = {
3669 					.vecs = fcrypt_pcbc_enc_tv_template,
3670 					.count = FCRYPT_ENC_TEST_VECTORS
3671 				},
3672 				.dec = {
3673 					.vecs = fcrypt_pcbc_dec_tv_template,
3674 					.count = FCRYPT_DEC_TEST_VECTORS
3675 				}
3676 			}
3677 		}
3678 	}, {
3679 		.alg = "poly1305",
3680 		.test = alg_test_hash,
3681 		.suite = {
3682 			.hash = {
3683 				.vecs = poly1305_tv_template,
3684 				.count = POLY1305_TEST_VECTORS
3685 			}
3686 		}
3687 	}, {
3688 		.alg = "rfc3686(ctr(aes))",
3689 		.test = alg_test_skcipher,
3690 		.fips_allowed = 1,
3691 		.suite = {
3692 			.cipher = {
3693 				.enc = {
3694 					.vecs = aes_ctr_rfc3686_enc_tv_template,
3695 					.count = AES_CTR_3686_ENC_TEST_VECTORS
3696 				},
3697 				.dec = {
3698 					.vecs = aes_ctr_rfc3686_dec_tv_template,
3699 					.count = AES_CTR_3686_DEC_TEST_VECTORS
3700 				}
3701 			}
3702 		}
3703 	}, {
3704 		.alg = "rfc4106(gcm(aes))",
3705 		.test = alg_test_aead,
3706 		.fips_allowed = 1,
3707 		.suite = {
3708 			.aead = {
3709 				.enc = {
3710 					.vecs = aes_gcm_rfc4106_enc_tv_template,
3711 					.count = AES_GCM_4106_ENC_TEST_VECTORS
3712 				},
3713 				.dec = {
3714 					.vecs = aes_gcm_rfc4106_dec_tv_template,
3715 					.count = AES_GCM_4106_DEC_TEST_VECTORS
3716 				}
3717 			}
3718 		}
3719 	}, {
3720 		.alg = "rfc4309(ccm(aes))",
3721 		.test = alg_test_aead,
3722 		.fips_allowed = 1,
3723 		.suite = {
3724 			.aead = {
3725 				.enc = {
3726 					.vecs = aes_ccm_rfc4309_enc_tv_template,
3727 					.count = AES_CCM_4309_ENC_TEST_VECTORS
3728 				},
3729 				.dec = {
3730 					.vecs = aes_ccm_rfc4309_dec_tv_template,
3731 					.count = AES_CCM_4309_DEC_TEST_VECTORS
3732 				}
3733 			}
3734 		}
3735 	}, {
3736 		.alg = "rfc4543(gcm(aes))",
3737 		.test = alg_test_aead,
3738 		.suite = {
3739 			.aead = {
3740 				.enc = {
3741 					.vecs = aes_gcm_rfc4543_enc_tv_template,
3742 					.count = AES_GCM_4543_ENC_TEST_VECTORS
3743 				},
3744 				.dec = {
3745 					.vecs = aes_gcm_rfc4543_dec_tv_template,
3746 					.count = AES_GCM_4543_DEC_TEST_VECTORS
3747 				},
3748 			}
3749 		}
3750 	}, {
3751 		.alg = "rfc7539(chacha20,poly1305)",
3752 		.test = alg_test_aead,
3753 		.suite = {
3754 			.aead = {
3755 				.enc = {
3756 					.vecs = rfc7539_enc_tv_template,
3757 					.count = RFC7539_ENC_TEST_VECTORS
3758 				},
3759 				.dec = {
3760 					.vecs = rfc7539_dec_tv_template,
3761 					.count = RFC7539_DEC_TEST_VECTORS
3762 				},
3763 			}
3764 		}
3765 	}, {
3766 		.alg = "rfc7539esp(chacha20,poly1305)",
3767 		.test = alg_test_aead,
3768 		.suite = {
3769 			.aead = {
3770 				.enc = {
3771 					.vecs = rfc7539esp_enc_tv_template,
3772 					.count = RFC7539ESP_ENC_TEST_VECTORS
3773 				},
3774 				.dec = {
3775 					.vecs = rfc7539esp_dec_tv_template,
3776 					.count = RFC7539ESP_DEC_TEST_VECTORS
3777 				},
3778 			}
3779 		}
3780 	}, {
3781 		.alg = "rmd128",
3782 		.test = alg_test_hash,
3783 		.suite = {
3784 			.hash = {
3785 				.vecs = rmd128_tv_template,
3786 				.count = RMD128_TEST_VECTORS
3787 			}
3788 		}
3789 	}, {
3790 		.alg = "rmd160",
3791 		.test = alg_test_hash,
3792 		.suite = {
3793 			.hash = {
3794 				.vecs = rmd160_tv_template,
3795 				.count = RMD160_TEST_VECTORS
3796 			}
3797 		}
3798 	}, {
3799 		.alg = "rmd256",
3800 		.test = alg_test_hash,
3801 		.suite = {
3802 			.hash = {
3803 				.vecs = rmd256_tv_template,
3804 				.count = RMD256_TEST_VECTORS
3805 			}
3806 		}
3807 	}, {
3808 		.alg = "rmd320",
3809 		.test = alg_test_hash,
3810 		.suite = {
3811 			.hash = {
3812 				.vecs = rmd320_tv_template,
3813 				.count = RMD320_TEST_VECTORS
3814 			}
3815 		}
3816 	}, {
3817 		.alg = "rsa",
3818 		.test = alg_test_akcipher,
3819 		.fips_allowed = 1,
3820 		.suite = {
3821 			.akcipher = {
3822 				.vecs = rsa_tv_template,
3823 				.count = RSA_TEST_VECTORS
3824 			}
3825 		}
3826 	}, {
3827 		.alg = "salsa20",
3828 		.test = alg_test_skcipher,
3829 		.suite = {
3830 			.cipher = {
3831 				.enc = {
3832 					.vecs = salsa20_stream_enc_tv_template,
3833 					.count = SALSA20_STREAM_ENC_TEST_VECTORS
3834 				}
3835 			}
3836 		}
3837 	}, {
3838 		.alg = "sha1",
3839 		.test = alg_test_hash,
3840 		.fips_allowed = 1,
3841 		.suite = {
3842 			.hash = {
3843 				.vecs = sha1_tv_template,
3844 				.count = SHA1_TEST_VECTORS
3845 			}
3846 		}
3847 	}, {
3848 		.alg = "sha224",
3849 		.test = alg_test_hash,
3850 		.fips_allowed = 1,
3851 		.suite = {
3852 			.hash = {
3853 				.vecs = sha224_tv_template,
3854 				.count = SHA224_TEST_VECTORS
3855 			}
3856 		}
3857 	}, {
3858 		.alg = "sha256",
3859 		.test = alg_test_hash,
3860 		.fips_allowed = 1,
3861 		.suite = {
3862 			.hash = {
3863 				.vecs = sha256_tv_template,
3864 				.count = SHA256_TEST_VECTORS
3865 			}
3866 		}
3867 	}, {
3868 		.alg = "sha3-224",
3869 		.test = alg_test_hash,
3870 		.fips_allowed = 1,
3871 		.suite = {
3872 			.hash = {
3873 				.vecs = sha3_224_tv_template,
3874 				.count = SHA3_224_TEST_VECTORS
3875 			}
3876 		}
3877 	}, {
3878 		.alg = "sha3-256",
3879 		.test = alg_test_hash,
3880 		.fips_allowed = 1,
3881 		.suite = {
3882 			.hash = {
3883 				.vecs = sha3_256_tv_template,
3884 				.count = SHA3_256_TEST_VECTORS
3885 			}
3886 		}
3887 	}, {
3888 		.alg = "sha3-384",
3889 		.test = alg_test_hash,
3890 		.fips_allowed = 1,
3891 		.suite = {
3892 			.hash = {
3893 				.vecs = sha3_384_tv_template,
3894 				.count = SHA3_384_TEST_VECTORS
3895 			}
3896 		}
3897 	}, {
3898 		.alg = "sha3-512",
3899 		.test = alg_test_hash,
3900 		.fips_allowed = 1,
3901 		.suite = {
3902 			.hash = {
3903 				.vecs = sha3_512_tv_template,
3904 				.count = SHA3_512_TEST_VECTORS
3905 			}
3906 		}
3907 	}, {
3908 		.alg = "sha384",
3909 		.test = alg_test_hash,
3910 		.fips_allowed = 1,
3911 		.suite = {
3912 			.hash = {
3913 				.vecs = sha384_tv_template,
3914 				.count = SHA384_TEST_VECTORS
3915 			}
3916 		}
3917 	}, {
3918 		.alg = "sha512",
3919 		.test = alg_test_hash,
3920 		.fips_allowed = 1,
3921 		.suite = {
3922 			.hash = {
3923 				.vecs = sha512_tv_template,
3924 				.count = SHA512_TEST_VECTORS
3925 			}
3926 		}
3927 	}, {
3928 		.alg = "tgr128",
3929 		.test = alg_test_hash,
3930 		.suite = {
3931 			.hash = {
3932 				.vecs = tgr128_tv_template,
3933 				.count = TGR128_TEST_VECTORS
3934 			}
3935 		}
3936 	}, {
3937 		.alg = "tgr160",
3938 		.test = alg_test_hash,
3939 		.suite = {
3940 			.hash = {
3941 				.vecs = tgr160_tv_template,
3942 				.count = TGR160_TEST_VECTORS
3943 			}
3944 		}
3945 	}, {
3946 		.alg = "tgr192",
3947 		.test = alg_test_hash,
3948 		.suite = {
3949 			.hash = {
3950 				.vecs = tgr192_tv_template,
3951 				.count = TGR192_TEST_VECTORS
3952 			}
3953 		}
3954 	}, {
3955 		.alg = "vmac(aes)",
3956 		.test = alg_test_hash,
3957 		.suite = {
3958 			.hash = {
3959 				.vecs = aes_vmac128_tv_template,
3960 				.count = VMAC_AES_TEST_VECTORS
3961 			}
3962 		}
3963 	}, {
3964 		.alg = "wp256",
3965 		.test = alg_test_hash,
3966 		.suite = {
3967 			.hash = {
3968 				.vecs = wp256_tv_template,
3969 				.count = WP256_TEST_VECTORS
3970 			}
3971 		}
3972 	}, {
3973 		.alg = "wp384",
3974 		.test = alg_test_hash,
3975 		.suite = {
3976 			.hash = {
3977 				.vecs = wp384_tv_template,
3978 				.count = WP384_TEST_VECTORS
3979 			}
3980 		}
3981 	}, {
3982 		.alg = "wp512",
3983 		.test = alg_test_hash,
3984 		.suite = {
3985 			.hash = {
3986 				.vecs = wp512_tv_template,
3987 				.count = WP512_TEST_VECTORS
3988 			}
3989 		}
3990 	}, {
3991 		.alg = "xcbc(aes)",
3992 		.test = alg_test_hash,
3993 		.suite = {
3994 			.hash = {
3995 				.vecs = aes_xcbc128_tv_template,
3996 				.count = XCBC_AES_TEST_VECTORS
3997 			}
3998 		}
3999 	}, {
4000 		.alg = "xts(aes)",
4001 		.test = alg_test_skcipher,
4002 		.fips_allowed = 1,
4003 		.suite = {
4004 			.cipher = {
4005 				.enc = {
4006 					.vecs = aes_xts_enc_tv_template,
4007 					.count = AES_XTS_ENC_TEST_VECTORS
4008 				},
4009 				.dec = {
4010 					.vecs = aes_xts_dec_tv_template,
4011 					.count = AES_XTS_DEC_TEST_VECTORS
4012 				}
4013 			}
4014 		}
4015 	}, {
4016 		.alg = "xts(camellia)",
4017 		.test = alg_test_skcipher,
4018 		.suite = {
4019 			.cipher = {
4020 				.enc = {
4021 					.vecs = camellia_xts_enc_tv_template,
4022 					.count = CAMELLIA_XTS_ENC_TEST_VECTORS
4023 				},
4024 				.dec = {
4025 					.vecs = camellia_xts_dec_tv_template,
4026 					.count = CAMELLIA_XTS_DEC_TEST_VECTORS
4027 				}
4028 			}
4029 		}
4030 	}, {
4031 		.alg = "xts(cast6)",
4032 		.test = alg_test_skcipher,
4033 		.suite = {
4034 			.cipher = {
4035 				.enc = {
4036 					.vecs = cast6_xts_enc_tv_template,
4037 					.count = CAST6_XTS_ENC_TEST_VECTORS
4038 				},
4039 				.dec = {
4040 					.vecs = cast6_xts_dec_tv_template,
4041 					.count = CAST6_XTS_DEC_TEST_VECTORS
4042 				}
4043 			}
4044 		}
4045 	}, {
4046 		.alg = "xts(serpent)",
4047 		.test = alg_test_skcipher,
4048 		.suite = {
4049 			.cipher = {
4050 				.enc = {
4051 					.vecs = serpent_xts_enc_tv_template,
4052 					.count = SERPENT_XTS_ENC_TEST_VECTORS
4053 				},
4054 				.dec = {
4055 					.vecs = serpent_xts_dec_tv_template,
4056 					.count = SERPENT_XTS_DEC_TEST_VECTORS
4057 				}
4058 			}
4059 		}
4060 	}, {
4061 		.alg = "xts(twofish)",
4062 		.test = alg_test_skcipher,
4063 		.suite = {
4064 			.cipher = {
4065 				.enc = {
4066 					.vecs = tf_xts_enc_tv_template,
4067 					.count = TF_XTS_ENC_TEST_VECTORS
4068 				},
4069 				.dec = {
4070 					.vecs = tf_xts_dec_tv_template,
4071 					.count = TF_XTS_DEC_TEST_VECTORS
4072 				}
4073 			}
4074 		}
4075 	}
4076 };
4077 
4078 static bool alg_test_descs_checked;
4079 
4080 static void alg_test_descs_check_order(void)
4081 {
4082 	int i;
4083 
4084 	/* only check once */
4085 	if (alg_test_descs_checked)
4086 		return;
4087 
4088 	alg_test_descs_checked = true;
4089 
4090 	for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
4091 		int diff = strcmp(alg_test_descs[i - 1].alg,
4092 				  alg_test_descs[i].alg);
4093 
4094 		if (WARN_ON(diff > 0)) {
4095 			pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
4096 				alg_test_descs[i - 1].alg,
4097 				alg_test_descs[i].alg);
4098 		}
4099 
4100 		if (WARN_ON(diff == 0)) {
4101 			pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
4102 				alg_test_descs[i].alg);
4103 		}
4104 	}
4105 }
4106 
4107 static int alg_find_test(const char *alg)
4108 {
4109 	int start = 0;
4110 	int end = ARRAY_SIZE(alg_test_descs);
4111 
4112 	while (start < end) {
4113 		int i = (start + end) / 2;
4114 		int diff = strcmp(alg_test_descs[i].alg, alg);
4115 
4116 		if (diff > 0) {
4117 			end = i;
4118 			continue;
4119 		}
4120 
4121 		if (diff < 0) {
4122 			start = i + 1;
4123 			continue;
4124 		}
4125 
4126 		return i;
4127 	}
4128 
4129 	return -1;
4130 }
4131 
4132 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
4133 {
4134 	int i;
4135 	int j;
4136 	int rc;
4137 
4138 	if (!fips_enabled && notests) {
4139 		printk_once(KERN_INFO "alg: self-tests disabled\n");
4140 		return 0;
4141 	}
4142 
4143 	alg_test_descs_check_order();
4144 
4145 	if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
4146 		char nalg[CRYPTO_MAX_ALG_NAME];
4147 
4148 		if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
4149 		    sizeof(nalg))
4150 			return -ENAMETOOLONG;
4151 
4152 		i = alg_find_test(nalg);
4153 		if (i < 0)
4154 			goto notest;
4155 
4156 		if (fips_enabled && !alg_test_descs[i].fips_allowed)
4157 			goto non_fips_alg;
4158 
4159 		rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
4160 		goto test_done;
4161 	}
4162 
4163 	i = alg_find_test(alg);
4164 	j = alg_find_test(driver);
4165 	if (i < 0 && j < 0)
4166 		goto notest;
4167 
4168 	if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
4169 			     (j >= 0 && !alg_test_descs[j].fips_allowed)))
4170 		goto non_fips_alg;
4171 
4172 	rc = 0;
4173 	if (i >= 0)
4174 		rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
4175 					     type, mask);
4176 	if (j >= 0 && j != i)
4177 		rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
4178 					     type, mask);
4179 
4180 test_done:
4181 	if (fips_enabled && rc)
4182 		panic("%s: %s alg self test failed in fips mode!\n", driver, alg);
4183 
4184 	if (fips_enabled && !rc)
4185 		pr_info("alg: self-tests for %s (%s) passed\n", driver, alg);
4186 
4187 	return rc;
4188 
4189 notest:
4190 	printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
4191 	return 0;
4192 non_fips_alg:
4193 	return -EINVAL;
4194 }
4195 
4196 #endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
4197 
4198 EXPORT_SYMBOL_GPL(alg_test);
4199