xref: /linux/crypto/testmgr.c (revision 714ca27e9bf4608fcb1f627cd5599441f448771e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Algorithm testing framework and tests.
4  *
5  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
6  * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
7  * Copyright (c) 2007 Nokia Siemens Networks
8  * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
9  * Copyright (c) 2019 Google LLC
10  *
11  * Updated RFC4106 AES-GCM testing.
12  *    Authors: Aidan O'Mahony (aidan.o.mahony@intel.com)
13  *             Adrian Hoban <adrian.hoban@intel.com>
14  *             Gabriele Paoloni <gabriele.paoloni@intel.com>
15  *             Tadeusz Struk (tadeusz.struk@intel.com)
16  *    Copyright (c) 2010, Intel Corporation.
17  */
18 
19 #include <crypto/aead.h>
20 #include <crypto/hash.h>
21 #include <crypto/skcipher.h>
22 #include <linux/err.h>
23 #include <linux/fips.h>
24 #include <linux/module.h>
25 #include <linux/once.h>
26 #include <linux/prandom.h>
27 #include <linux/scatterlist.h>
28 #include <linux/slab.h>
29 #include <linux/string.h>
30 #include <linux/uio.h>
31 #include <crypto/rng.h>
32 #include <crypto/drbg.h>
33 #include <crypto/akcipher.h>
34 #include <crypto/kpp.h>
35 #include <crypto/acompress.h>
36 #include <crypto/sig.h>
37 #include <crypto/internal/cipher.h>
38 #include <crypto/internal/simd.h>
39 
40 #include "internal.h"
41 
42 MODULE_IMPORT_NS("CRYPTO_INTERNAL");
43 
44 static bool notests;
45 module_param(notests, bool, 0644);
46 MODULE_PARM_DESC(notests, "disable all crypto self-tests");
47 
48 static bool noslowtests;
49 module_param(noslowtests, bool, 0644);
50 MODULE_PARM_DESC(noslowtests, "disable slow crypto self-tests");
51 
52 static unsigned int fuzz_iterations = 100;
53 module_param(fuzz_iterations, uint, 0644);
54 MODULE_PARM_DESC(fuzz_iterations, "number of fuzz test iterations");
55 
56 #ifndef CONFIG_CRYPTO_SELFTESTS
57 
58 /* a perfect nop */
59 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
60 {
61 	return 0;
62 }
63 
64 #else
65 
66 #include "testmgr.h"
67 
68 /*
69  * Need slab memory for testing (size in number of pages).
70  */
71 #define XBUFSIZE	8
72 
73 /*
74 * Used by test_cipher()
75 */
76 #define ENCRYPT 1
77 #define DECRYPT 0
78 
79 struct aead_test_suite {
80 	const struct aead_testvec *vecs;
81 	unsigned int count;
82 
83 	/*
84 	 * Set if trying to decrypt an inauthentic ciphertext with this
85 	 * algorithm might result in EINVAL rather than EBADMSG, due to other
86 	 * validation the algorithm does on the inputs such as length checks.
87 	 */
88 	unsigned int einval_allowed : 1;
89 
90 	/*
91 	 * Set if this algorithm requires that the IV be located at the end of
92 	 * the AAD buffer, in addition to being given in the normal way.  The
93 	 * behavior when the two IV copies differ is implementation-defined.
94 	 */
95 	unsigned int aad_iv : 1;
96 };
97 
98 struct cipher_test_suite {
99 	const struct cipher_testvec *vecs;
100 	unsigned int count;
101 };
102 
103 struct comp_test_suite {
104 	struct {
105 		const struct comp_testvec *vecs;
106 		unsigned int count;
107 	} comp, decomp;
108 };
109 
110 struct hash_test_suite {
111 	const struct hash_testvec *vecs;
112 	unsigned int count;
113 };
114 
115 struct cprng_test_suite {
116 	const struct cprng_testvec *vecs;
117 	unsigned int count;
118 };
119 
120 struct drbg_test_suite {
121 	const struct drbg_testvec *vecs;
122 	unsigned int count;
123 };
124 
125 struct akcipher_test_suite {
126 	const struct akcipher_testvec *vecs;
127 	unsigned int count;
128 };
129 
130 struct sig_test_suite {
131 	const struct sig_testvec *vecs;
132 	unsigned int count;
133 };
134 
135 struct kpp_test_suite {
136 	const struct kpp_testvec *vecs;
137 	unsigned int count;
138 };
139 
140 struct alg_test_desc {
141 	const char *alg;
142 	const char *generic_driver;
143 	int (*test)(const struct alg_test_desc *desc, const char *driver,
144 		    u32 type, u32 mask);
145 	int fips_allowed;	/* set if alg is allowed in fips mode */
146 
147 	union {
148 		struct aead_test_suite aead;
149 		struct cipher_test_suite cipher;
150 		struct comp_test_suite comp;
151 		struct hash_test_suite hash;
152 		struct cprng_test_suite cprng;
153 		struct drbg_test_suite drbg;
154 		struct akcipher_test_suite akcipher;
155 		struct sig_test_suite sig;
156 		struct kpp_test_suite kpp;
157 	} suite;
158 };
159 
160 static void hexdump(unsigned char *buf, unsigned int len)
161 {
162 	print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
163 			16, 1,
164 			buf, len, false);
165 }
166 
167 static int __testmgr_alloc_buf(char *buf[XBUFSIZE], int order)
168 {
169 	int i;
170 
171 	for (i = 0; i < XBUFSIZE; i++) {
172 		buf[i] = (char *)__get_free_pages(GFP_KERNEL, order);
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_pages((unsigned long)buf[i], order);
182 
183 	return -ENOMEM;
184 }
185 
186 static int testmgr_alloc_buf(char *buf[XBUFSIZE])
187 {
188 	return __testmgr_alloc_buf(buf, 0);
189 }
190 
191 static void __testmgr_free_buf(char *buf[XBUFSIZE], int order)
192 {
193 	int i;
194 
195 	for (i = 0; i < XBUFSIZE; i++)
196 		free_pages((unsigned long)buf[i], order);
197 }
198 
199 static void testmgr_free_buf(char *buf[XBUFSIZE])
200 {
201 	__testmgr_free_buf(buf, 0);
202 }
203 
204 #define TESTMGR_POISON_BYTE	0xfe
205 #define TESTMGR_POISON_LEN	16
206 
207 static inline void testmgr_poison(void *addr, size_t len)
208 {
209 	memset(addr, TESTMGR_POISON_BYTE, len);
210 }
211 
212 /* Is the memory region still fully poisoned? */
213 static inline bool testmgr_is_poison(const void *addr, size_t len)
214 {
215 	return memchr_inv(addr, TESTMGR_POISON_BYTE, len) == NULL;
216 }
217 
218 /* flush type for hash algorithms */
219 enum flush_type {
220 	/* merge with update of previous buffer(s) */
221 	FLUSH_TYPE_NONE = 0,
222 
223 	/* update with previous buffer(s) before doing this one */
224 	FLUSH_TYPE_FLUSH,
225 
226 	/* likewise, but also export and re-import the intermediate state */
227 	FLUSH_TYPE_REIMPORT,
228 };
229 
230 /* finalization function for hash algorithms */
231 enum finalization_type {
232 	FINALIZATION_TYPE_FINAL,	/* use final() */
233 	FINALIZATION_TYPE_FINUP,	/* use finup() */
234 	FINALIZATION_TYPE_DIGEST,	/* use digest() */
235 };
236 
237 /*
238  * Whether the crypto operation will occur in-place, and if so whether the
239  * source and destination scatterlist pointers will coincide (req->src ==
240  * req->dst), or whether they'll merely point to two separate scatterlists
241  * (req->src != req->dst) that reference the same underlying memory.
242  *
243  * This is only relevant for algorithm types that support in-place operation.
244  */
245 enum inplace_mode {
246 	OUT_OF_PLACE,
247 	INPLACE_ONE_SGLIST,
248 	INPLACE_TWO_SGLISTS,
249 };
250 
251 #define TEST_SG_TOTAL	10000
252 
253 /**
254  * struct test_sg_division - description of a scatterlist entry
255  *
256  * This struct describes one entry of a scatterlist being constructed to check a
257  * crypto test vector.
258  *
259  * @proportion_of_total: length of this chunk relative to the total length,
260  *			 given as a proportion out of TEST_SG_TOTAL so that it
261  *			 scales to fit any test vector
262  * @offset: byte offset into a 2-page buffer at which this chunk will start
263  * @offset_relative_to_alignmask: if true, add the algorithm's alignmask to the
264  *				  @offset
265  * @flush_type: for hashes, whether an update() should be done now vs.
266  *		continuing to accumulate data
267  * @nosimd: if doing the pending update(), do it with SIMD disabled?
268  */
269 struct test_sg_division {
270 	unsigned int proportion_of_total;
271 	unsigned int offset;
272 	bool offset_relative_to_alignmask;
273 	enum flush_type flush_type;
274 	bool nosimd;
275 };
276 
277 /**
278  * struct testvec_config - configuration for testing a crypto test vector
279  *
280  * This struct describes the data layout and other parameters with which each
281  * crypto test vector can be tested.
282  *
283  * @name: name of this config, logged for debugging purposes if a test fails
284  * @inplace_mode: whether and how to operate on the data in-place, if applicable
285  * @req_flags: extra request_flags, e.g. CRYPTO_TFM_REQ_MAY_SLEEP
286  * @src_divs: description of how to arrange the source scatterlist
287  * @dst_divs: description of how to arrange the dst scatterlist, if applicable
288  *	      for the algorithm type.  Defaults to @src_divs if unset.
289  * @iv_offset: misalignment of the IV in the range [0..MAX_ALGAPI_ALIGNMASK+1],
290  *	       where 0 is aligned to a 2*(MAX_ALGAPI_ALIGNMASK+1) byte boundary
291  * @iv_offset_relative_to_alignmask: if true, add the algorithm's alignmask to
292  *				     the @iv_offset
293  * @key_offset: misalignment of the key, where 0 is default alignment
294  * @key_offset_relative_to_alignmask: if true, add the algorithm's alignmask to
295  *				      the @key_offset
296  * @finalization_type: what finalization function to use for hashes
297  * @nosimd: execute with SIMD disabled?  Requires !CRYPTO_TFM_REQ_MAY_SLEEP.
298  *	    This applies to the parts of the operation that aren't controlled
299  *	    individually by @nosimd_setkey or @src_divs[].nosimd.
300  * @nosimd_setkey: set the key (if applicable) with SIMD disabled?  Requires
301  *		   !CRYPTO_TFM_REQ_MAY_SLEEP.
302  */
303 struct testvec_config {
304 	const char *name;
305 	enum inplace_mode inplace_mode;
306 	u32 req_flags;
307 	struct test_sg_division src_divs[XBUFSIZE];
308 	struct test_sg_division dst_divs[XBUFSIZE];
309 	unsigned int iv_offset;
310 	unsigned int key_offset;
311 	bool iv_offset_relative_to_alignmask;
312 	bool key_offset_relative_to_alignmask;
313 	enum finalization_type finalization_type;
314 	bool nosimd;
315 	bool nosimd_setkey;
316 };
317 
318 #define TESTVEC_CONFIG_NAMELEN	192
319 
320 /*
321  * The following are the lists of testvec_configs to test for each algorithm
322  * type when the fast crypto self-tests are enabled.  They aim to provide good
323  * test coverage, while keeping the test time much shorter than the full tests
324  * so that the fast tests can be used to fulfill FIPS 140 testing requirements.
325  */
326 
327 /* Configs for skciphers and aeads */
328 static const struct testvec_config default_cipher_testvec_configs[] = {
329 	{
330 		.name = "in-place (one sglist)",
331 		.inplace_mode = INPLACE_ONE_SGLIST,
332 		.src_divs = { { .proportion_of_total = 10000 } },
333 	}, {
334 		.name = "in-place (two sglists)",
335 		.inplace_mode = INPLACE_TWO_SGLISTS,
336 		.src_divs = { { .proportion_of_total = 10000 } },
337 	}, {
338 		.name = "out-of-place",
339 		.inplace_mode = OUT_OF_PLACE,
340 		.src_divs = { { .proportion_of_total = 10000 } },
341 	}, {
342 		.name = "unaligned buffer, offset=1",
343 		.src_divs = { { .proportion_of_total = 10000, .offset = 1 } },
344 		.iv_offset = 1,
345 		.key_offset = 1,
346 	}, {
347 		.name = "buffer aligned only to alignmask",
348 		.src_divs = {
349 			{
350 				.proportion_of_total = 10000,
351 				.offset = 1,
352 				.offset_relative_to_alignmask = true,
353 			},
354 		},
355 		.iv_offset = 1,
356 		.iv_offset_relative_to_alignmask = true,
357 		.key_offset = 1,
358 		.key_offset_relative_to_alignmask = true,
359 	}, {
360 		.name = "two even aligned splits",
361 		.src_divs = {
362 			{ .proportion_of_total = 5000 },
363 			{ .proportion_of_total = 5000 },
364 		},
365 	}, {
366 		.name = "one src, two even splits dst",
367 		.inplace_mode = OUT_OF_PLACE,
368 		.src_divs = { { .proportion_of_total = 10000 } },
369 		.dst_divs = {
370 			{ .proportion_of_total = 5000 },
371 			{ .proportion_of_total = 5000 },
372 		 },
373 	}, {
374 		.name = "uneven misaligned splits, may sleep",
375 		.req_flags = CRYPTO_TFM_REQ_MAY_SLEEP,
376 		.src_divs = {
377 			{ .proportion_of_total = 1900, .offset = 33 },
378 			{ .proportion_of_total = 3300, .offset = 7  },
379 			{ .proportion_of_total = 4800, .offset = 18 },
380 		},
381 		.iv_offset = 3,
382 		.key_offset = 3,
383 	}, {
384 		.name = "misaligned splits crossing pages, inplace",
385 		.inplace_mode = INPLACE_ONE_SGLIST,
386 		.src_divs = {
387 			{
388 				.proportion_of_total = 7500,
389 				.offset = PAGE_SIZE - 32
390 			}, {
391 				.proportion_of_total = 2500,
392 				.offset = PAGE_SIZE - 7
393 			},
394 		},
395 	}
396 };
397 
398 static const struct testvec_config default_hash_testvec_configs[] = {
399 	{
400 		.name = "init+update+final aligned buffer",
401 		.src_divs = { { .proportion_of_total = 10000 } },
402 		.finalization_type = FINALIZATION_TYPE_FINAL,
403 	}, {
404 		.name = "init+finup aligned buffer",
405 		.src_divs = { { .proportion_of_total = 10000 } },
406 		.finalization_type = FINALIZATION_TYPE_FINUP,
407 	}, {
408 		.name = "digest aligned buffer",
409 		.src_divs = { { .proportion_of_total = 10000 } },
410 		.finalization_type = FINALIZATION_TYPE_DIGEST,
411 	}, {
412 		.name = "init+update+final misaligned buffer",
413 		.src_divs = { { .proportion_of_total = 10000, .offset = 1 } },
414 		.finalization_type = FINALIZATION_TYPE_FINAL,
415 		.key_offset = 1,
416 	}, {
417 		.name = "digest misaligned buffer",
418 		.src_divs = {
419 			{
420 				.proportion_of_total = 10000,
421 				.offset = 1,
422 			},
423 		},
424 		.finalization_type = FINALIZATION_TYPE_DIGEST,
425 		.key_offset = 1,
426 	}, {
427 		.name = "init+update+update+final two even splits",
428 		.src_divs = {
429 			{ .proportion_of_total = 5000 },
430 			{
431 				.proportion_of_total = 5000,
432 				.flush_type = FLUSH_TYPE_FLUSH,
433 			},
434 		},
435 		.finalization_type = FINALIZATION_TYPE_FINAL,
436 	}, {
437 		.name = "digest uneven misaligned splits, may sleep",
438 		.req_flags = CRYPTO_TFM_REQ_MAY_SLEEP,
439 		.src_divs = {
440 			{ .proportion_of_total = 1900, .offset = 33 },
441 			{ .proportion_of_total = 3300, .offset = 7  },
442 			{ .proportion_of_total = 4800, .offset = 18 },
443 		},
444 		.finalization_type = FINALIZATION_TYPE_DIGEST,
445 	}, {
446 		.name = "digest misaligned splits crossing pages",
447 		.src_divs = {
448 			{
449 				.proportion_of_total = 7500,
450 				.offset = PAGE_SIZE - 32,
451 			}, {
452 				.proportion_of_total = 2500,
453 				.offset = PAGE_SIZE - 7,
454 			},
455 		},
456 		.finalization_type = FINALIZATION_TYPE_DIGEST,
457 	}, {
458 		.name = "import/export",
459 		.src_divs = {
460 			{
461 				.proportion_of_total = 6500,
462 				.flush_type = FLUSH_TYPE_REIMPORT,
463 			}, {
464 				.proportion_of_total = 3500,
465 				.flush_type = FLUSH_TYPE_REIMPORT,
466 			},
467 		},
468 		.finalization_type = FINALIZATION_TYPE_FINAL,
469 	}
470 };
471 
472 static unsigned int count_test_sg_divisions(const struct test_sg_division *divs)
473 {
474 	unsigned int remaining = TEST_SG_TOTAL;
475 	unsigned int ndivs = 0;
476 
477 	do {
478 		remaining -= divs[ndivs++].proportion_of_total;
479 	} while (remaining);
480 
481 	return ndivs;
482 }
483 
484 #define SGDIVS_HAVE_FLUSHES	BIT(0)
485 #define SGDIVS_HAVE_NOSIMD	BIT(1)
486 
487 static bool valid_sg_divisions(const struct test_sg_division *divs,
488 			       unsigned int count, int *flags_ret)
489 {
490 	unsigned int total = 0;
491 	unsigned int i;
492 
493 	for (i = 0; i < count && total != TEST_SG_TOTAL; i++) {
494 		if (divs[i].proportion_of_total <= 0 ||
495 		    divs[i].proportion_of_total > TEST_SG_TOTAL - total)
496 			return false;
497 		total += divs[i].proportion_of_total;
498 		if (divs[i].flush_type != FLUSH_TYPE_NONE)
499 			*flags_ret |= SGDIVS_HAVE_FLUSHES;
500 		if (divs[i].nosimd)
501 			*flags_ret |= SGDIVS_HAVE_NOSIMD;
502 	}
503 	return total == TEST_SG_TOTAL &&
504 		memchr_inv(&divs[i], 0, (count - i) * sizeof(divs[0])) == NULL;
505 }
506 
507 /*
508  * Check whether the given testvec_config is valid.  This isn't strictly needed
509  * since every testvec_config should be valid, but check anyway so that people
510  * don't unknowingly add broken configs that don't do what they wanted.
511  */
512 static bool valid_testvec_config(const struct testvec_config *cfg)
513 {
514 	int flags = 0;
515 
516 	if (cfg->name == NULL)
517 		return false;
518 
519 	if (!valid_sg_divisions(cfg->src_divs, ARRAY_SIZE(cfg->src_divs),
520 				&flags))
521 		return false;
522 
523 	if (cfg->dst_divs[0].proportion_of_total) {
524 		if (!valid_sg_divisions(cfg->dst_divs,
525 					ARRAY_SIZE(cfg->dst_divs), &flags))
526 			return false;
527 	} else {
528 		if (memchr_inv(cfg->dst_divs, 0, sizeof(cfg->dst_divs)))
529 			return false;
530 		/* defaults to dst_divs=src_divs */
531 	}
532 
533 	if (cfg->iv_offset +
534 	    (cfg->iv_offset_relative_to_alignmask ? MAX_ALGAPI_ALIGNMASK : 0) >
535 	    MAX_ALGAPI_ALIGNMASK + 1)
536 		return false;
537 
538 	if ((flags & (SGDIVS_HAVE_FLUSHES | SGDIVS_HAVE_NOSIMD)) &&
539 	    cfg->finalization_type == FINALIZATION_TYPE_DIGEST)
540 		return false;
541 
542 	if ((cfg->nosimd || cfg->nosimd_setkey ||
543 	     (flags & SGDIVS_HAVE_NOSIMD)) &&
544 	    (cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP))
545 		return false;
546 
547 	return true;
548 }
549 
550 struct test_sglist {
551 	char *bufs[XBUFSIZE];
552 	struct scatterlist sgl[XBUFSIZE];
553 	struct scatterlist sgl_saved[XBUFSIZE];
554 	struct scatterlist *sgl_ptr;
555 	unsigned int nents;
556 };
557 
558 static int init_test_sglist(struct test_sglist *tsgl)
559 {
560 	return __testmgr_alloc_buf(tsgl->bufs, 1 /* two pages per buffer */);
561 }
562 
563 static void destroy_test_sglist(struct test_sglist *tsgl)
564 {
565 	return __testmgr_free_buf(tsgl->bufs, 1 /* two pages per buffer */);
566 }
567 
568 /**
569  * build_test_sglist() - build a scatterlist for a crypto test
570  *
571  * @tsgl: the scatterlist to build.  @tsgl->bufs[] contains an array of 2-page
572  *	  buffers which the scatterlist @tsgl->sgl[] will be made to point into.
573  * @divs: the layout specification on which the scatterlist will be based
574  * @alignmask: the algorithm's alignmask
575  * @total_len: the total length of the scatterlist to build in bytes
576  * @data: if non-NULL, the buffers will be filled with this data until it ends.
577  *	  Otherwise the buffers will be poisoned.  In both cases, some bytes
578  *	  past the end of each buffer will be poisoned to help detect overruns.
579  * @out_divs: if non-NULL, the test_sg_division to which each scatterlist entry
580  *	      corresponds will be returned here.  This will match @divs except
581  *	      that divisions resolving to a length of 0 are omitted as they are
582  *	      not included in the scatterlist.
583  *
584  * Return: 0 or a -errno value
585  */
586 static int build_test_sglist(struct test_sglist *tsgl,
587 			     const struct test_sg_division *divs,
588 			     const unsigned int alignmask,
589 			     const unsigned int total_len,
590 			     struct iov_iter *data,
591 			     const struct test_sg_division *out_divs[XBUFSIZE])
592 {
593 	struct {
594 		const struct test_sg_division *div;
595 		size_t length;
596 	} partitions[XBUFSIZE];
597 	const unsigned int ndivs = count_test_sg_divisions(divs);
598 	unsigned int len_remaining = total_len;
599 	unsigned int i;
600 
601 	BUILD_BUG_ON(ARRAY_SIZE(partitions) != ARRAY_SIZE(tsgl->sgl));
602 	if (WARN_ON(ndivs > ARRAY_SIZE(partitions)))
603 		return -EINVAL;
604 
605 	/* Calculate the (div, length) pairs */
606 	tsgl->nents = 0;
607 	for (i = 0; i < ndivs; i++) {
608 		unsigned int len_this_sg =
609 			min(len_remaining,
610 			    (total_len * divs[i].proportion_of_total +
611 			     TEST_SG_TOTAL / 2) / TEST_SG_TOTAL);
612 
613 		if (len_this_sg != 0) {
614 			partitions[tsgl->nents].div = &divs[i];
615 			partitions[tsgl->nents].length = len_this_sg;
616 			tsgl->nents++;
617 			len_remaining -= len_this_sg;
618 		}
619 	}
620 	if (tsgl->nents == 0) {
621 		partitions[tsgl->nents].div = &divs[0];
622 		partitions[tsgl->nents].length = 0;
623 		tsgl->nents++;
624 	}
625 	partitions[tsgl->nents - 1].length += len_remaining;
626 
627 	/* Set up the sgl entries and fill the data or poison */
628 	sg_init_table(tsgl->sgl, tsgl->nents);
629 	for (i = 0; i < tsgl->nents; i++) {
630 		unsigned int offset = partitions[i].div->offset;
631 		void *addr;
632 
633 		if (partitions[i].div->offset_relative_to_alignmask)
634 			offset += alignmask;
635 
636 		while (offset + partitions[i].length + TESTMGR_POISON_LEN >
637 		       2 * PAGE_SIZE) {
638 			if (WARN_ON(offset <= 0))
639 				return -EINVAL;
640 			offset /= 2;
641 		}
642 
643 		addr = &tsgl->bufs[i][offset];
644 		sg_set_buf(&tsgl->sgl[i], addr, partitions[i].length);
645 
646 		if (out_divs)
647 			out_divs[i] = partitions[i].div;
648 
649 		if (data) {
650 			size_t copy_len, copied;
651 
652 			copy_len = min(partitions[i].length, data->count);
653 			copied = copy_from_iter(addr, copy_len, data);
654 			if (WARN_ON(copied != copy_len))
655 				return -EINVAL;
656 			testmgr_poison(addr + copy_len, partitions[i].length +
657 				       TESTMGR_POISON_LEN - copy_len);
658 		} else {
659 			testmgr_poison(addr, partitions[i].length +
660 				       TESTMGR_POISON_LEN);
661 		}
662 	}
663 
664 	sg_mark_end(&tsgl->sgl[tsgl->nents - 1]);
665 	tsgl->sgl_ptr = tsgl->sgl;
666 	memcpy(tsgl->sgl_saved, tsgl->sgl, tsgl->nents * sizeof(tsgl->sgl[0]));
667 	return 0;
668 }
669 
670 /*
671  * Verify that a scatterlist crypto operation produced the correct output.
672  *
673  * @tsgl: scatterlist containing the actual output
674  * @expected_output: buffer containing the expected output
675  * @len_to_check: length of @expected_output in bytes
676  * @unchecked_prefix_len: number of ignored bytes in @tsgl prior to real result
677  * @check_poison: verify that the poison bytes after each chunk are intact?
678  *
679  * Return: 0 if correct, -EINVAL if incorrect, -EOVERFLOW if buffer overrun.
680  */
681 static int verify_correct_output(const struct test_sglist *tsgl,
682 				 const char *expected_output,
683 				 unsigned int len_to_check,
684 				 unsigned int unchecked_prefix_len,
685 				 bool check_poison)
686 {
687 	unsigned int i;
688 
689 	for (i = 0; i < tsgl->nents; i++) {
690 		struct scatterlist *sg = &tsgl->sgl_ptr[i];
691 		unsigned int len = sg->length;
692 		unsigned int offset = sg->offset;
693 		const char *actual_output;
694 
695 		if (unchecked_prefix_len) {
696 			if (unchecked_prefix_len >= len) {
697 				unchecked_prefix_len -= len;
698 				continue;
699 			}
700 			offset += unchecked_prefix_len;
701 			len -= unchecked_prefix_len;
702 			unchecked_prefix_len = 0;
703 		}
704 		len = min(len, len_to_check);
705 		actual_output = page_address(sg_page(sg)) + offset;
706 		if (memcmp(expected_output, actual_output, len) != 0)
707 			return -EINVAL;
708 		if (check_poison &&
709 		    !testmgr_is_poison(actual_output + len, TESTMGR_POISON_LEN))
710 			return -EOVERFLOW;
711 		len_to_check -= len;
712 		expected_output += len;
713 	}
714 	if (WARN_ON(len_to_check != 0))
715 		return -EINVAL;
716 	return 0;
717 }
718 
719 static bool is_test_sglist_corrupted(const struct test_sglist *tsgl)
720 {
721 	unsigned int i;
722 
723 	for (i = 0; i < tsgl->nents; i++) {
724 		if (tsgl->sgl[i].page_link != tsgl->sgl_saved[i].page_link)
725 			return true;
726 		if (tsgl->sgl[i].offset != tsgl->sgl_saved[i].offset)
727 			return true;
728 		if (tsgl->sgl[i].length != tsgl->sgl_saved[i].length)
729 			return true;
730 	}
731 	return false;
732 }
733 
734 struct cipher_test_sglists {
735 	struct test_sglist src;
736 	struct test_sglist dst;
737 };
738 
739 static struct cipher_test_sglists *alloc_cipher_test_sglists(void)
740 {
741 	struct cipher_test_sglists *tsgls;
742 
743 	tsgls = kmalloc(sizeof(*tsgls), GFP_KERNEL);
744 	if (!tsgls)
745 		return NULL;
746 
747 	if (init_test_sglist(&tsgls->src) != 0)
748 		goto fail_kfree;
749 	if (init_test_sglist(&tsgls->dst) != 0)
750 		goto fail_destroy_src;
751 
752 	return tsgls;
753 
754 fail_destroy_src:
755 	destroy_test_sglist(&tsgls->src);
756 fail_kfree:
757 	kfree(tsgls);
758 	return NULL;
759 }
760 
761 static void free_cipher_test_sglists(struct cipher_test_sglists *tsgls)
762 {
763 	if (tsgls) {
764 		destroy_test_sglist(&tsgls->src);
765 		destroy_test_sglist(&tsgls->dst);
766 		kfree(tsgls);
767 	}
768 }
769 
770 /* Build the src and dst scatterlists for an skcipher or AEAD test */
771 static int build_cipher_test_sglists(struct cipher_test_sglists *tsgls,
772 				     const struct testvec_config *cfg,
773 				     unsigned int alignmask,
774 				     unsigned int src_total_len,
775 				     unsigned int dst_total_len,
776 				     const struct kvec *inputs,
777 				     unsigned int nr_inputs)
778 {
779 	struct iov_iter input;
780 	int err;
781 
782 	iov_iter_kvec(&input, ITER_SOURCE, inputs, nr_inputs, src_total_len);
783 	err = build_test_sglist(&tsgls->src, cfg->src_divs, alignmask,
784 				cfg->inplace_mode != OUT_OF_PLACE ?
785 					max(dst_total_len, src_total_len) :
786 					src_total_len,
787 				&input, NULL);
788 	if (err)
789 		return err;
790 
791 	/*
792 	 * In-place crypto operations can use the same scatterlist for both the
793 	 * source and destination (req->src == req->dst), or can use separate
794 	 * scatterlists (req->src != req->dst) which point to the same
795 	 * underlying memory.  Make sure to test both cases.
796 	 */
797 	if (cfg->inplace_mode == INPLACE_ONE_SGLIST) {
798 		tsgls->dst.sgl_ptr = tsgls->src.sgl;
799 		tsgls->dst.nents = tsgls->src.nents;
800 		return 0;
801 	}
802 	if (cfg->inplace_mode == INPLACE_TWO_SGLISTS) {
803 		/*
804 		 * For now we keep it simple and only test the case where the
805 		 * two scatterlists have identical entries, rather than
806 		 * different entries that split up the same memory differently.
807 		 */
808 		memcpy(tsgls->dst.sgl, tsgls->src.sgl,
809 		       tsgls->src.nents * sizeof(tsgls->src.sgl[0]));
810 		memcpy(tsgls->dst.sgl_saved, tsgls->src.sgl,
811 		       tsgls->src.nents * sizeof(tsgls->src.sgl[0]));
812 		tsgls->dst.sgl_ptr = tsgls->dst.sgl;
813 		tsgls->dst.nents = tsgls->src.nents;
814 		return 0;
815 	}
816 	/* Out of place */
817 	return build_test_sglist(&tsgls->dst,
818 				 cfg->dst_divs[0].proportion_of_total ?
819 					cfg->dst_divs : cfg->src_divs,
820 				 alignmask, dst_total_len, NULL, NULL);
821 }
822 
823 /*
824  * Support for testing passing a misaligned key to setkey():
825  *
826  * If cfg->key_offset is set, copy the key into a new buffer at that offset,
827  * optionally adding alignmask.  Else, just use the key directly.
828  */
829 static int prepare_keybuf(const u8 *key, unsigned int ksize,
830 			  const struct testvec_config *cfg,
831 			  unsigned int alignmask,
832 			  const u8 **keybuf_ret, const u8 **keyptr_ret)
833 {
834 	unsigned int key_offset = cfg->key_offset;
835 	u8 *keybuf = NULL, *keyptr = (u8 *)key;
836 
837 	if (key_offset != 0) {
838 		if (cfg->key_offset_relative_to_alignmask)
839 			key_offset += alignmask;
840 		keybuf = kmalloc(key_offset + ksize, GFP_KERNEL);
841 		if (!keybuf)
842 			return -ENOMEM;
843 		keyptr = keybuf + key_offset;
844 		memcpy(keyptr, key, ksize);
845 	}
846 	*keybuf_ret = keybuf;
847 	*keyptr_ret = keyptr;
848 	return 0;
849 }
850 
851 /*
852  * Like setkey_f(tfm, key, ksize), but sometimes misalign the key.
853  * In addition, run the setkey function in no-SIMD context if requested.
854  */
855 #define do_setkey(setkey_f, tfm, key, ksize, cfg, alignmask)		\
856 ({									\
857 	const u8 *keybuf, *keyptr;					\
858 	int err;							\
859 									\
860 	err = prepare_keybuf((key), (ksize), (cfg), (alignmask),	\
861 			     &keybuf, &keyptr);				\
862 	if (err == 0) {							\
863 		if ((cfg)->nosimd_setkey)				\
864 			crypto_disable_simd_for_test();			\
865 		err = setkey_f((tfm), keyptr, (ksize));			\
866 		if ((cfg)->nosimd_setkey)				\
867 			crypto_reenable_simd_for_test();		\
868 		kfree(keybuf);						\
869 	}								\
870 	err;								\
871 })
872 
873 /*
874  * The fuzz tests use prandom instead of the normal Linux RNG since they don't
875  * need cryptographically secure random numbers.  This greatly improves the
876  * performance of these tests, especially if they are run before the Linux RNG
877  * has been initialized or if they are run on a lockdep-enabled kernel.
878  */
879 
880 static inline void init_rnd_state(struct rnd_state *rng)
881 {
882 	prandom_seed_state(rng, get_random_u64());
883 }
884 
885 static inline u8 prandom_u8(struct rnd_state *rng)
886 {
887 	return prandom_u32_state(rng);
888 }
889 
890 static inline u32 prandom_u32_below(struct rnd_state *rng, u32 ceil)
891 {
892 	/*
893 	 * This is slightly biased for non-power-of-2 values of 'ceil', but this
894 	 * isn't important here.
895 	 */
896 	return prandom_u32_state(rng) % ceil;
897 }
898 
899 static inline bool prandom_bool(struct rnd_state *rng)
900 {
901 	return prandom_u32_below(rng, 2);
902 }
903 
904 static inline u32 prandom_u32_inclusive(struct rnd_state *rng,
905 					u32 floor, u32 ceil)
906 {
907 	return floor + prandom_u32_below(rng, ceil - floor + 1);
908 }
909 
910 /* Generate a random length in range [0, max_len], but prefer smaller values */
911 static unsigned int generate_random_length(struct rnd_state *rng,
912 					   unsigned int max_len)
913 {
914 	unsigned int len = prandom_u32_below(rng, max_len + 1);
915 
916 	switch (prandom_u32_below(rng, 4)) {
917 	case 0:
918 		len %= 64;
919 		break;
920 	case 1:
921 		len %= 256;
922 		break;
923 	case 2:
924 		len %= 1024;
925 		break;
926 	default:
927 		break;
928 	}
929 	if (len && prandom_u32_below(rng, 4) == 0)
930 		len = rounddown_pow_of_two(len);
931 	return len;
932 }
933 
934 /* Flip a random bit in the given nonempty data buffer */
935 static void flip_random_bit(struct rnd_state *rng, u8 *buf, size_t size)
936 {
937 	size_t bitpos;
938 
939 	bitpos = prandom_u32_below(rng, size * 8);
940 	buf[bitpos / 8] ^= 1 << (bitpos % 8);
941 }
942 
943 /* Flip a random byte in the given nonempty data buffer */
944 static void flip_random_byte(struct rnd_state *rng, u8 *buf, size_t size)
945 {
946 	buf[prandom_u32_below(rng, size)] ^= 0xff;
947 }
948 
949 /* Sometimes make some random changes to the given nonempty data buffer */
950 static void mutate_buffer(struct rnd_state *rng, u8 *buf, size_t size)
951 {
952 	size_t num_flips;
953 	size_t i;
954 
955 	/* Sometimes flip some bits */
956 	if (prandom_u32_below(rng, 4) == 0) {
957 		num_flips = min_t(size_t, 1 << prandom_u32_below(rng, 8),
958 				  size * 8);
959 		for (i = 0; i < num_flips; i++)
960 			flip_random_bit(rng, buf, size);
961 	}
962 
963 	/* Sometimes flip some bytes */
964 	if (prandom_u32_below(rng, 4) == 0) {
965 		num_flips = min_t(size_t, 1 << prandom_u32_below(rng, 8), size);
966 		for (i = 0; i < num_flips; i++)
967 			flip_random_byte(rng, buf, size);
968 	}
969 }
970 
971 /* Randomly generate 'count' bytes, but sometimes make them "interesting" */
972 static void generate_random_bytes(struct rnd_state *rng, u8 *buf, size_t count)
973 {
974 	u8 b;
975 	u8 increment;
976 	size_t i;
977 
978 	if (count == 0)
979 		return;
980 
981 	switch (prandom_u32_below(rng, 8)) { /* Choose a generation strategy */
982 	case 0:
983 	case 1:
984 		/* All the same byte, plus optional mutations */
985 		switch (prandom_u32_below(rng, 4)) {
986 		case 0:
987 			b = 0x00;
988 			break;
989 		case 1:
990 			b = 0xff;
991 			break;
992 		default:
993 			b = prandom_u8(rng);
994 			break;
995 		}
996 		memset(buf, b, count);
997 		mutate_buffer(rng, buf, count);
998 		break;
999 	case 2:
1000 		/* Ascending or descending bytes, plus optional mutations */
1001 		increment = prandom_u8(rng);
1002 		b = prandom_u8(rng);
1003 		for (i = 0; i < count; i++, b += increment)
1004 			buf[i] = b;
1005 		mutate_buffer(rng, buf, count);
1006 		break;
1007 	default:
1008 		/* Fully random bytes */
1009 		prandom_bytes_state(rng, buf, count);
1010 	}
1011 }
1012 
1013 static char *generate_random_sgl_divisions(struct rnd_state *rng,
1014 					   struct test_sg_division *divs,
1015 					   size_t max_divs, char *p, char *end,
1016 					   bool gen_flushes, u32 req_flags)
1017 {
1018 	struct test_sg_division *div = divs;
1019 	unsigned int remaining = TEST_SG_TOTAL;
1020 
1021 	do {
1022 		unsigned int this_len;
1023 		const char *flushtype_str;
1024 
1025 		if (div == &divs[max_divs - 1] || prandom_bool(rng))
1026 			this_len = remaining;
1027 		else if (prandom_u32_below(rng, 4) == 0)
1028 			this_len = (remaining + 1) / 2;
1029 		else
1030 			this_len = prandom_u32_inclusive(rng, 1, remaining);
1031 		div->proportion_of_total = this_len;
1032 
1033 		if (prandom_u32_below(rng, 4) == 0)
1034 			div->offset = prandom_u32_inclusive(rng,
1035 							    PAGE_SIZE - 128,
1036 							    PAGE_SIZE - 1);
1037 		else if (prandom_bool(rng))
1038 			div->offset = prandom_u32_below(rng, 32);
1039 		else
1040 			div->offset = prandom_u32_below(rng, PAGE_SIZE);
1041 		if (prandom_u32_below(rng, 8) == 0)
1042 			div->offset_relative_to_alignmask = true;
1043 
1044 		div->flush_type = FLUSH_TYPE_NONE;
1045 		if (gen_flushes) {
1046 			switch (prandom_u32_below(rng, 4)) {
1047 			case 0:
1048 				div->flush_type = FLUSH_TYPE_REIMPORT;
1049 				break;
1050 			case 1:
1051 				div->flush_type = FLUSH_TYPE_FLUSH;
1052 				break;
1053 			}
1054 		}
1055 
1056 		if (div->flush_type != FLUSH_TYPE_NONE &&
1057 		    !(req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) &&
1058 		    prandom_bool(rng))
1059 			div->nosimd = true;
1060 
1061 		switch (div->flush_type) {
1062 		case FLUSH_TYPE_FLUSH:
1063 			if (div->nosimd)
1064 				flushtype_str = "<flush,nosimd>";
1065 			else
1066 				flushtype_str = "<flush>";
1067 			break;
1068 		case FLUSH_TYPE_REIMPORT:
1069 			if (div->nosimd)
1070 				flushtype_str = "<reimport,nosimd>";
1071 			else
1072 				flushtype_str = "<reimport>";
1073 			break;
1074 		default:
1075 			flushtype_str = "";
1076 			break;
1077 		}
1078 
1079 		BUILD_BUG_ON(TEST_SG_TOTAL != 10000); /* for "%u.%u%%" */
1080 		p += scnprintf(p, end - p, "%s%u.%u%%@%s+%u%s", flushtype_str,
1081 			       this_len / 100, this_len % 100,
1082 			       div->offset_relative_to_alignmask ?
1083 					"alignmask" : "",
1084 			       div->offset, this_len == remaining ? "" : ", ");
1085 		remaining -= this_len;
1086 		div++;
1087 	} while (remaining);
1088 
1089 	return p;
1090 }
1091 
1092 /* Generate a random testvec_config for fuzz testing */
1093 static void generate_random_testvec_config(struct rnd_state *rng,
1094 					   struct testvec_config *cfg,
1095 					   char *name, size_t max_namelen)
1096 {
1097 	char *p = name;
1098 	char * const end = name + max_namelen;
1099 
1100 	memset(cfg, 0, sizeof(*cfg));
1101 
1102 	cfg->name = name;
1103 
1104 	p += scnprintf(p, end - p, "random:");
1105 
1106 	switch (prandom_u32_below(rng, 4)) {
1107 	case 0:
1108 	case 1:
1109 		cfg->inplace_mode = OUT_OF_PLACE;
1110 		break;
1111 	case 2:
1112 		cfg->inplace_mode = INPLACE_ONE_SGLIST;
1113 		p += scnprintf(p, end - p, " inplace_one_sglist");
1114 		break;
1115 	default:
1116 		cfg->inplace_mode = INPLACE_TWO_SGLISTS;
1117 		p += scnprintf(p, end - p, " inplace_two_sglists");
1118 		break;
1119 	}
1120 
1121 	if (prandom_bool(rng)) {
1122 		cfg->req_flags |= CRYPTO_TFM_REQ_MAY_SLEEP;
1123 		p += scnprintf(p, end - p, " may_sleep");
1124 	}
1125 
1126 	switch (prandom_u32_below(rng, 4)) {
1127 	case 0:
1128 		cfg->finalization_type = FINALIZATION_TYPE_FINAL;
1129 		p += scnprintf(p, end - p, " use_final");
1130 		break;
1131 	case 1:
1132 		cfg->finalization_type = FINALIZATION_TYPE_FINUP;
1133 		p += scnprintf(p, end - p, " use_finup");
1134 		break;
1135 	default:
1136 		cfg->finalization_type = FINALIZATION_TYPE_DIGEST;
1137 		p += scnprintf(p, end - p, " use_digest");
1138 		break;
1139 	}
1140 
1141 	if (!(cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP)) {
1142 		if (prandom_bool(rng)) {
1143 			cfg->nosimd = true;
1144 			p += scnprintf(p, end - p, " nosimd");
1145 		}
1146 		if (prandom_bool(rng)) {
1147 			cfg->nosimd_setkey = true;
1148 			p += scnprintf(p, end - p, " nosimd_setkey");
1149 		}
1150 	}
1151 
1152 	p += scnprintf(p, end - p, " src_divs=[");
1153 	p = generate_random_sgl_divisions(rng, cfg->src_divs,
1154 					  ARRAY_SIZE(cfg->src_divs), p, end,
1155 					  (cfg->finalization_type !=
1156 					   FINALIZATION_TYPE_DIGEST),
1157 					  cfg->req_flags);
1158 	p += scnprintf(p, end - p, "]");
1159 
1160 	if (cfg->inplace_mode == OUT_OF_PLACE && prandom_bool(rng)) {
1161 		p += scnprintf(p, end - p, " dst_divs=[");
1162 		p = generate_random_sgl_divisions(rng, cfg->dst_divs,
1163 						  ARRAY_SIZE(cfg->dst_divs),
1164 						  p, end, false,
1165 						  cfg->req_flags);
1166 		p += scnprintf(p, end - p, "]");
1167 	}
1168 
1169 	if (prandom_bool(rng)) {
1170 		cfg->iv_offset = prandom_u32_inclusive(rng, 1,
1171 						       MAX_ALGAPI_ALIGNMASK);
1172 		p += scnprintf(p, end - p, " iv_offset=%u", cfg->iv_offset);
1173 	}
1174 
1175 	if (prandom_bool(rng)) {
1176 		cfg->key_offset = prandom_u32_inclusive(rng, 1,
1177 							MAX_ALGAPI_ALIGNMASK);
1178 		p += scnprintf(p, end - p, " key_offset=%u", cfg->key_offset);
1179 	}
1180 
1181 	WARN_ON_ONCE(!valid_testvec_config(cfg));
1182 }
1183 
1184 static void crypto_disable_simd_for_test(void)
1185 {
1186 	migrate_disable();
1187 	__this_cpu_write(crypto_simd_disabled_for_test, true);
1188 }
1189 
1190 static void crypto_reenable_simd_for_test(void)
1191 {
1192 	__this_cpu_write(crypto_simd_disabled_for_test, false);
1193 	migrate_enable();
1194 }
1195 
1196 /*
1197  * Given an algorithm name, build the name of the generic implementation of that
1198  * algorithm, assuming the usual naming convention.  Specifically, this appends
1199  * "-generic" to every part of the name that is not a template name.  Examples:
1200  *
1201  *	aes => aes-generic
1202  *	cbc(aes) => cbc(aes-generic)
1203  *	cts(cbc(aes)) => cts(cbc(aes-generic))
1204  *	rfc7539(chacha20,poly1305) => rfc7539(chacha20-generic,poly1305-generic)
1205  *
1206  * Return: 0 on success, or -ENAMETOOLONG if the generic name would be too long
1207  */
1208 static int build_generic_driver_name(const char *algname,
1209 				     char driver_name[CRYPTO_MAX_ALG_NAME])
1210 {
1211 	const char *in = algname;
1212 	char *out = driver_name;
1213 	size_t len = strlen(algname);
1214 
1215 	if (len >= CRYPTO_MAX_ALG_NAME)
1216 		goto too_long;
1217 	do {
1218 		const char *in_saved = in;
1219 
1220 		while (*in && *in != '(' && *in != ')' && *in != ',')
1221 			*out++ = *in++;
1222 		if (*in != '(' && in > in_saved) {
1223 			len += 8;
1224 			if (len >= CRYPTO_MAX_ALG_NAME)
1225 				goto too_long;
1226 			memcpy(out, "-generic", 8);
1227 			out += 8;
1228 		}
1229 	} while ((*out++ = *in++) != '\0');
1230 	return 0;
1231 
1232 too_long:
1233 	pr_err("alg: generic driver name for \"%s\" would be too long\n",
1234 	       algname);
1235 	return -ENAMETOOLONG;
1236 }
1237 
1238 static int build_hash_sglist(struct test_sglist *tsgl,
1239 			     const struct hash_testvec *vec,
1240 			     const struct testvec_config *cfg,
1241 			     unsigned int alignmask,
1242 			     const struct test_sg_division *divs[XBUFSIZE])
1243 {
1244 	struct kvec kv;
1245 	struct iov_iter input;
1246 
1247 	kv.iov_base = (void *)vec->plaintext;
1248 	kv.iov_len = vec->psize;
1249 	iov_iter_kvec(&input, ITER_SOURCE, &kv, 1, vec->psize);
1250 	return build_test_sglist(tsgl, cfg->src_divs, alignmask, vec->psize,
1251 				 &input, divs);
1252 }
1253 
1254 static int check_hash_result(const char *type,
1255 			     const u8 *result, unsigned int digestsize,
1256 			     const struct hash_testvec *vec,
1257 			     const char *vec_name,
1258 			     const char *driver,
1259 			     const struct testvec_config *cfg)
1260 {
1261 	if (memcmp(result, vec->digest, digestsize) != 0) {
1262 		pr_err("alg: %s: %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
1263 		       type, driver, vec_name, cfg->name);
1264 		return -EINVAL;
1265 	}
1266 	if (!testmgr_is_poison(&result[digestsize], TESTMGR_POISON_LEN)) {
1267 		pr_err("alg: %s: %s overran result buffer on test vector %s, cfg=\"%s\"\n",
1268 		       type, driver, vec_name, cfg->name);
1269 		return -EOVERFLOW;
1270 	}
1271 	return 0;
1272 }
1273 
1274 static inline int check_shash_op(const char *op, int err,
1275 				 const char *driver, const char *vec_name,
1276 				 const struct testvec_config *cfg)
1277 {
1278 	if (err)
1279 		pr_err("alg: shash: %s %s() failed with err %d on test vector %s, cfg=\"%s\"\n",
1280 		       driver, op, err, vec_name, cfg->name);
1281 	return err;
1282 }
1283 
1284 /* Test one hash test vector in one configuration, using the shash API */
1285 static int test_shash_vec_cfg(const struct hash_testvec *vec,
1286 			      const char *vec_name,
1287 			      const struct testvec_config *cfg,
1288 			      struct shash_desc *desc,
1289 			      struct test_sglist *tsgl,
1290 			      u8 *hashstate)
1291 {
1292 	struct crypto_shash *tfm = desc->tfm;
1293 	const unsigned int digestsize = crypto_shash_digestsize(tfm);
1294 	const unsigned int statesize = crypto_shash_statesize(tfm);
1295 	const char *driver = crypto_shash_driver_name(tfm);
1296 	const struct test_sg_division *divs[XBUFSIZE];
1297 	unsigned int i;
1298 	u8 result[HASH_MAX_DIGESTSIZE + TESTMGR_POISON_LEN];
1299 	int err;
1300 
1301 	/* Set the key, if specified */
1302 	if (vec->ksize) {
1303 		err = do_setkey(crypto_shash_setkey, tfm, vec->key, vec->ksize,
1304 				cfg, 0);
1305 		if (err) {
1306 			if (err == vec->setkey_error)
1307 				return 0;
1308 			pr_err("alg: shash: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1309 			       driver, vec_name, vec->setkey_error, err,
1310 			       crypto_shash_get_flags(tfm));
1311 			return err;
1312 		}
1313 		if (vec->setkey_error) {
1314 			pr_err("alg: shash: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1315 			       driver, vec_name, vec->setkey_error);
1316 			return -EINVAL;
1317 		}
1318 	}
1319 
1320 	/* Build the scatterlist for the source data */
1321 	err = build_hash_sglist(tsgl, vec, cfg, 0, divs);
1322 	if (err) {
1323 		pr_err("alg: shash: %s: error preparing scatterlist for test vector %s, cfg=\"%s\"\n",
1324 		       driver, vec_name, cfg->name);
1325 		return err;
1326 	}
1327 
1328 	/* Do the actual hashing */
1329 
1330 	testmgr_poison(desc->__ctx, crypto_shash_descsize(tfm));
1331 	testmgr_poison(result, digestsize + TESTMGR_POISON_LEN);
1332 
1333 	if (cfg->finalization_type == FINALIZATION_TYPE_DIGEST ||
1334 	    vec->digest_error) {
1335 		/* Just using digest() */
1336 		if (tsgl->nents != 1)
1337 			return 0;
1338 		if (cfg->nosimd)
1339 			crypto_disable_simd_for_test();
1340 		err = crypto_shash_digest(desc, sg_virt(&tsgl->sgl[0]),
1341 					  tsgl->sgl[0].length, result);
1342 		if (cfg->nosimd)
1343 			crypto_reenable_simd_for_test();
1344 		if (err) {
1345 			if (err == vec->digest_error)
1346 				return 0;
1347 			pr_err("alg: shash: %s digest() failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
1348 			       driver, vec_name, vec->digest_error, err,
1349 			       cfg->name);
1350 			return err;
1351 		}
1352 		if (vec->digest_error) {
1353 			pr_err("alg: shash: %s digest() unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
1354 			       driver, vec_name, vec->digest_error, cfg->name);
1355 			return -EINVAL;
1356 		}
1357 		goto result_ready;
1358 	}
1359 
1360 	/* Using init(), zero or more update(), then final() or finup() */
1361 
1362 	if (cfg->nosimd)
1363 		crypto_disable_simd_for_test();
1364 	err = crypto_shash_init(desc);
1365 	if (cfg->nosimd)
1366 		crypto_reenable_simd_for_test();
1367 	err = check_shash_op("init", err, driver, vec_name, cfg);
1368 	if (err)
1369 		return err;
1370 
1371 	for (i = 0; i < tsgl->nents; i++) {
1372 		if (i + 1 == tsgl->nents &&
1373 		    cfg->finalization_type == FINALIZATION_TYPE_FINUP) {
1374 			if (divs[i]->nosimd)
1375 				crypto_disable_simd_for_test();
1376 			err = crypto_shash_finup(desc, sg_virt(&tsgl->sgl[i]),
1377 						 tsgl->sgl[i].length, result);
1378 			if (divs[i]->nosimd)
1379 				crypto_reenable_simd_for_test();
1380 			err = check_shash_op("finup", err, driver, vec_name,
1381 					     cfg);
1382 			if (err)
1383 				return err;
1384 			goto result_ready;
1385 		}
1386 		if (divs[i]->nosimd)
1387 			crypto_disable_simd_for_test();
1388 		err = crypto_shash_update(desc, sg_virt(&tsgl->sgl[i]),
1389 					  tsgl->sgl[i].length);
1390 		if (divs[i]->nosimd)
1391 			crypto_reenable_simd_for_test();
1392 		err = check_shash_op("update", err, driver, vec_name, cfg);
1393 		if (err)
1394 			return err;
1395 		if (divs[i]->flush_type == FLUSH_TYPE_REIMPORT) {
1396 			/* Test ->export() and ->import() */
1397 			testmgr_poison(hashstate + statesize,
1398 				       TESTMGR_POISON_LEN);
1399 			err = crypto_shash_export(desc, hashstate);
1400 			err = check_shash_op("export", err, driver, vec_name,
1401 					     cfg);
1402 			if (err)
1403 				return err;
1404 			if (!testmgr_is_poison(hashstate + statesize,
1405 					       TESTMGR_POISON_LEN)) {
1406 				pr_err("alg: shash: %s export() overran state buffer on test vector %s, cfg=\"%s\"\n",
1407 				       driver, vec_name, cfg->name);
1408 				return -EOVERFLOW;
1409 			}
1410 			testmgr_poison(desc->__ctx, crypto_shash_descsize(tfm));
1411 			err = crypto_shash_import(desc, hashstate);
1412 			err = check_shash_op("import", err, driver, vec_name,
1413 					     cfg);
1414 			if (err)
1415 				return err;
1416 		}
1417 	}
1418 
1419 	if (cfg->nosimd)
1420 		crypto_disable_simd_for_test();
1421 	err = crypto_shash_final(desc, result);
1422 	if (cfg->nosimd)
1423 		crypto_reenable_simd_for_test();
1424 	err = check_shash_op("final", err, driver, vec_name, cfg);
1425 	if (err)
1426 		return err;
1427 result_ready:
1428 	return check_hash_result("shash", result, digestsize, vec, vec_name,
1429 				 driver, cfg);
1430 }
1431 
1432 static int do_ahash_op(int (*op)(struct ahash_request *req),
1433 		       struct ahash_request *req,
1434 		       struct crypto_wait *wait, bool nosimd)
1435 {
1436 	int err;
1437 
1438 	if (nosimd)
1439 		crypto_disable_simd_for_test();
1440 
1441 	err = op(req);
1442 
1443 	if (nosimd)
1444 		crypto_reenable_simd_for_test();
1445 
1446 	return crypto_wait_req(err, wait);
1447 }
1448 
1449 static int check_nonfinal_ahash_op(const char *op, int err,
1450 				   u8 *result, unsigned int digestsize,
1451 				   const char *driver, const char *vec_name,
1452 				   const struct testvec_config *cfg)
1453 {
1454 	if (err) {
1455 		pr_err("alg: ahash: %s %s() failed with err %d on test vector %s, cfg=\"%s\"\n",
1456 		       driver, op, err, vec_name, cfg->name);
1457 		return err;
1458 	}
1459 	if (!testmgr_is_poison(result, digestsize)) {
1460 		pr_err("alg: ahash: %s %s() used result buffer on test vector %s, cfg=\"%s\"\n",
1461 		       driver, op, vec_name, cfg->name);
1462 		return -EINVAL;
1463 	}
1464 	return 0;
1465 }
1466 
1467 /* Test one hash test vector in one configuration, using the ahash API */
1468 static int test_ahash_vec_cfg(const struct hash_testvec *vec,
1469 			      const char *vec_name,
1470 			      const struct testvec_config *cfg,
1471 			      struct ahash_request *req,
1472 			      struct test_sglist *tsgl,
1473 			      u8 *hashstate)
1474 {
1475 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1476 	const unsigned int digestsize = crypto_ahash_digestsize(tfm);
1477 	const unsigned int statesize = crypto_ahash_statesize(tfm);
1478 	const char *driver = crypto_ahash_driver_name(tfm);
1479 	const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1480 	const struct test_sg_division *divs[XBUFSIZE];
1481 	DECLARE_CRYPTO_WAIT(wait);
1482 	unsigned int i;
1483 	struct scatterlist *pending_sgl;
1484 	unsigned int pending_len;
1485 	u8 result[HASH_MAX_DIGESTSIZE + TESTMGR_POISON_LEN];
1486 	int err;
1487 
1488 	/* Set the key, if specified */
1489 	if (vec->ksize) {
1490 		err = do_setkey(crypto_ahash_setkey, tfm, vec->key, vec->ksize,
1491 				cfg, 0);
1492 		if (err) {
1493 			if (err == vec->setkey_error)
1494 				return 0;
1495 			pr_err("alg: ahash: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1496 			       driver, vec_name, vec->setkey_error, err,
1497 			       crypto_ahash_get_flags(tfm));
1498 			return err;
1499 		}
1500 		if (vec->setkey_error) {
1501 			pr_err("alg: ahash: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1502 			       driver, vec_name, vec->setkey_error);
1503 			return -EINVAL;
1504 		}
1505 	}
1506 
1507 	/* Build the scatterlist for the source data */
1508 	err = build_hash_sglist(tsgl, vec, cfg, 0, divs);
1509 	if (err) {
1510 		pr_err("alg: ahash: %s: error preparing scatterlist for test vector %s, cfg=\"%s\"\n",
1511 		       driver, vec_name, cfg->name);
1512 		return err;
1513 	}
1514 
1515 	/* Do the actual hashing */
1516 
1517 	testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
1518 	testmgr_poison(result, digestsize + TESTMGR_POISON_LEN);
1519 
1520 	if (cfg->finalization_type == FINALIZATION_TYPE_DIGEST ||
1521 	    vec->digest_error) {
1522 		/* Just using digest() */
1523 		ahash_request_set_callback(req, req_flags, crypto_req_done,
1524 					   &wait);
1525 		ahash_request_set_crypt(req, tsgl->sgl, result, vec->psize);
1526 		err = do_ahash_op(crypto_ahash_digest, req, &wait, cfg->nosimd);
1527 		if (err) {
1528 			if (err == vec->digest_error)
1529 				return 0;
1530 			pr_err("alg: ahash: %s digest() failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
1531 			       driver, vec_name, vec->digest_error, err,
1532 			       cfg->name);
1533 			return err;
1534 		}
1535 		if (vec->digest_error) {
1536 			pr_err("alg: ahash: %s digest() unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
1537 			       driver, vec_name, vec->digest_error, cfg->name);
1538 			return -EINVAL;
1539 		}
1540 		goto result_ready;
1541 	}
1542 
1543 	/* Using init(), zero or more update(), then final() or finup() */
1544 
1545 	ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
1546 	ahash_request_set_crypt(req, NULL, result, 0);
1547 	err = do_ahash_op(crypto_ahash_init, req, &wait, cfg->nosimd);
1548 	err = check_nonfinal_ahash_op("init", err, result, digestsize,
1549 				      driver, vec_name, cfg);
1550 	if (err)
1551 		return err;
1552 
1553 	pending_sgl = NULL;
1554 	pending_len = 0;
1555 	for (i = 0; i < tsgl->nents; i++) {
1556 		if (divs[i]->flush_type != FLUSH_TYPE_NONE &&
1557 		    pending_sgl != NULL) {
1558 			/* update() with the pending data */
1559 			ahash_request_set_callback(req, req_flags,
1560 						   crypto_req_done, &wait);
1561 			ahash_request_set_crypt(req, pending_sgl, result,
1562 						pending_len);
1563 			err = do_ahash_op(crypto_ahash_update, req, &wait,
1564 					  divs[i]->nosimd);
1565 			err = check_nonfinal_ahash_op("update", err,
1566 						      result, digestsize,
1567 						      driver, vec_name, cfg);
1568 			if (err)
1569 				return err;
1570 			pending_sgl = NULL;
1571 			pending_len = 0;
1572 		}
1573 		if (divs[i]->flush_type == FLUSH_TYPE_REIMPORT) {
1574 			/* Test ->export() and ->import() */
1575 			testmgr_poison(hashstate + statesize,
1576 				       TESTMGR_POISON_LEN);
1577 			err = crypto_ahash_export(req, hashstate);
1578 			err = check_nonfinal_ahash_op("export", err,
1579 						      result, digestsize,
1580 						      driver, vec_name, cfg);
1581 			if (err)
1582 				return err;
1583 			if (!testmgr_is_poison(hashstate + statesize,
1584 					       TESTMGR_POISON_LEN)) {
1585 				pr_err("alg: ahash: %s export() overran state buffer on test vector %s, cfg=\"%s\"\n",
1586 				       driver, vec_name, cfg->name);
1587 				return -EOVERFLOW;
1588 			}
1589 
1590 			testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
1591 			err = crypto_ahash_import(req, hashstate);
1592 			err = check_nonfinal_ahash_op("import", err,
1593 						      result, digestsize,
1594 						      driver, vec_name, cfg);
1595 			if (err)
1596 				return err;
1597 		}
1598 		if (pending_sgl == NULL)
1599 			pending_sgl = &tsgl->sgl[i];
1600 		pending_len += tsgl->sgl[i].length;
1601 	}
1602 
1603 	ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
1604 	ahash_request_set_crypt(req, pending_sgl, result, pending_len);
1605 	if (cfg->finalization_type == FINALIZATION_TYPE_FINAL) {
1606 		/* finish with update() and final() */
1607 		err = do_ahash_op(crypto_ahash_update, req, &wait, cfg->nosimd);
1608 		err = check_nonfinal_ahash_op("update", err, result, digestsize,
1609 					      driver, vec_name, cfg);
1610 		if (err)
1611 			return err;
1612 		err = do_ahash_op(crypto_ahash_final, req, &wait, cfg->nosimd);
1613 		if (err) {
1614 			pr_err("alg: ahash: %s final() failed with err %d on test vector %s, cfg=\"%s\"\n",
1615 			       driver, err, vec_name, cfg->name);
1616 			return err;
1617 		}
1618 	} else {
1619 		/* finish with finup() */
1620 		err = do_ahash_op(crypto_ahash_finup, req, &wait, cfg->nosimd);
1621 		if (err) {
1622 			pr_err("alg: ahash: %s finup() failed with err %d on test vector %s, cfg=\"%s\"\n",
1623 			       driver, err, vec_name, cfg->name);
1624 			return err;
1625 		}
1626 	}
1627 
1628 result_ready:
1629 	return check_hash_result("ahash", result, digestsize, vec, vec_name,
1630 				 driver, cfg);
1631 }
1632 
1633 static int test_hash_vec_cfg(const struct hash_testvec *vec,
1634 			     const char *vec_name,
1635 			     const struct testvec_config *cfg,
1636 			     struct ahash_request *req,
1637 			     struct shash_desc *desc,
1638 			     struct test_sglist *tsgl,
1639 			     u8 *hashstate)
1640 {
1641 	int err;
1642 
1643 	/*
1644 	 * For algorithms implemented as "shash", most bugs will be detected by
1645 	 * both the shash and ahash tests.  Test the shash API first so that the
1646 	 * failures involve less indirection, so are easier to debug.
1647 	 */
1648 
1649 	if (desc) {
1650 		err = test_shash_vec_cfg(vec, vec_name, cfg, desc, tsgl,
1651 					 hashstate);
1652 		if (err)
1653 			return err;
1654 	}
1655 
1656 	return test_ahash_vec_cfg(vec, vec_name, cfg, req, tsgl, hashstate);
1657 }
1658 
1659 static int test_hash_vec(const struct hash_testvec *vec, unsigned int vec_num,
1660 			 struct ahash_request *req, struct shash_desc *desc,
1661 			 struct test_sglist *tsgl, u8 *hashstate)
1662 {
1663 	char vec_name[16];
1664 	unsigned int i;
1665 	int err;
1666 
1667 	sprintf(vec_name, "%u", vec_num);
1668 
1669 	for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++) {
1670 		err = test_hash_vec_cfg(vec, vec_name,
1671 					&default_hash_testvec_configs[i],
1672 					req, desc, tsgl, hashstate);
1673 		if (err)
1674 			return err;
1675 	}
1676 
1677 	if (!noslowtests) {
1678 		struct rnd_state rng;
1679 		struct testvec_config cfg;
1680 		char cfgname[TESTVEC_CONFIG_NAMELEN];
1681 
1682 		init_rnd_state(&rng);
1683 
1684 		for (i = 0; i < fuzz_iterations; i++) {
1685 			generate_random_testvec_config(&rng, &cfg, cfgname,
1686 						       sizeof(cfgname));
1687 			err = test_hash_vec_cfg(vec, vec_name, &cfg,
1688 						req, desc, tsgl, hashstate);
1689 			if (err)
1690 				return err;
1691 			cond_resched();
1692 		}
1693 	}
1694 	return 0;
1695 }
1696 
1697 /*
1698  * Generate a hash test vector from the given implementation.
1699  * Assumes the buffers in 'vec' were already allocated.
1700  */
1701 static void generate_random_hash_testvec(struct rnd_state *rng,
1702 					 struct shash_desc *desc,
1703 					 struct hash_testvec *vec,
1704 					 unsigned int maxkeysize,
1705 					 unsigned int maxdatasize,
1706 					 char *name, size_t max_namelen)
1707 {
1708 	/* Data */
1709 	vec->psize = generate_random_length(rng, maxdatasize);
1710 	generate_random_bytes(rng, (u8 *)vec->plaintext, vec->psize);
1711 
1712 	/*
1713 	 * Key: length in range [1, maxkeysize], but usually choose maxkeysize.
1714 	 * If algorithm is unkeyed, then maxkeysize == 0 and set ksize = 0.
1715 	 */
1716 	vec->setkey_error = 0;
1717 	vec->ksize = 0;
1718 	if (maxkeysize) {
1719 		vec->ksize = maxkeysize;
1720 		if (prandom_u32_below(rng, 4) == 0)
1721 			vec->ksize = prandom_u32_inclusive(rng, 1, maxkeysize);
1722 		generate_random_bytes(rng, (u8 *)vec->key, vec->ksize);
1723 
1724 		vec->setkey_error = crypto_shash_setkey(desc->tfm, vec->key,
1725 							vec->ksize);
1726 		/* If the key couldn't be set, no need to continue to digest. */
1727 		if (vec->setkey_error)
1728 			goto done;
1729 	}
1730 
1731 	/* Digest */
1732 	vec->digest_error = crypto_shash_digest(desc, vec->plaintext,
1733 						vec->psize, (u8 *)vec->digest);
1734 done:
1735 	snprintf(name, max_namelen, "\"random: psize=%u ksize=%u\"",
1736 		 vec->psize, vec->ksize);
1737 }
1738 
1739 /*
1740  * Test the hash algorithm represented by @req against the corresponding generic
1741  * implementation, if one is available.
1742  */
1743 static int test_hash_vs_generic_impl(const char *generic_driver,
1744 				     unsigned int maxkeysize,
1745 				     struct ahash_request *req,
1746 				     struct shash_desc *desc,
1747 				     struct test_sglist *tsgl,
1748 				     u8 *hashstate)
1749 {
1750 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1751 	const unsigned int digestsize = crypto_ahash_digestsize(tfm);
1752 	const unsigned int blocksize = crypto_ahash_blocksize(tfm);
1753 	const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
1754 	const char *algname = crypto_hash_alg_common(tfm)->base.cra_name;
1755 	const char *driver = crypto_ahash_driver_name(tfm);
1756 	struct rnd_state rng;
1757 	char _generic_driver[CRYPTO_MAX_ALG_NAME];
1758 	struct crypto_shash *generic_tfm = NULL;
1759 	struct shash_desc *generic_desc = NULL;
1760 	unsigned int i;
1761 	struct hash_testvec vec = { 0 };
1762 	char vec_name[64];
1763 	struct testvec_config *cfg;
1764 	char cfgname[TESTVEC_CONFIG_NAMELEN];
1765 	int err;
1766 
1767 	if (noslowtests)
1768 		return 0;
1769 
1770 	init_rnd_state(&rng);
1771 
1772 	if (!generic_driver) { /* Use default naming convention? */
1773 		err = build_generic_driver_name(algname, _generic_driver);
1774 		if (err)
1775 			return err;
1776 		generic_driver = _generic_driver;
1777 	}
1778 
1779 	if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
1780 		return 0;
1781 
1782 	generic_tfm = crypto_alloc_shash(generic_driver, 0, 0);
1783 	if (IS_ERR(generic_tfm)) {
1784 		err = PTR_ERR(generic_tfm);
1785 		if (err == -ENOENT) {
1786 			pr_warn("alg: hash: skipping comparison tests for %s because %s is unavailable\n",
1787 				driver, generic_driver);
1788 			return 0;
1789 		}
1790 		pr_err("alg: hash: error allocating %s (generic impl of %s): %d\n",
1791 		       generic_driver, algname, err);
1792 		return err;
1793 	}
1794 
1795 	cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1796 	if (!cfg) {
1797 		err = -ENOMEM;
1798 		goto out;
1799 	}
1800 
1801 	generic_desc = kzalloc(sizeof(*desc) +
1802 			       crypto_shash_descsize(generic_tfm), GFP_KERNEL);
1803 	if (!generic_desc) {
1804 		err = -ENOMEM;
1805 		goto out;
1806 	}
1807 	generic_desc->tfm = generic_tfm;
1808 
1809 	/* Check the algorithm properties for consistency. */
1810 
1811 	if (digestsize != crypto_shash_digestsize(generic_tfm)) {
1812 		pr_err("alg: hash: digestsize for %s (%u) doesn't match generic impl (%u)\n",
1813 		       driver, digestsize,
1814 		       crypto_shash_digestsize(generic_tfm));
1815 		err = -EINVAL;
1816 		goto out;
1817 	}
1818 
1819 	if (blocksize != crypto_shash_blocksize(generic_tfm)) {
1820 		pr_err("alg: hash: blocksize for %s (%u) doesn't match generic impl (%u)\n",
1821 		       driver, blocksize, crypto_shash_blocksize(generic_tfm));
1822 		err = -EINVAL;
1823 		goto out;
1824 	}
1825 
1826 	/*
1827 	 * Now generate test vectors using the generic implementation, and test
1828 	 * the other implementation against them.
1829 	 */
1830 
1831 	vec.key = kmalloc(maxkeysize, GFP_KERNEL);
1832 	vec.plaintext = kmalloc(maxdatasize, GFP_KERNEL);
1833 	vec.digest = kmalloc(digestsize, GFP_KERNEL);
1834 	if (!vec.key || !vec.plaintext || !vec.digest) {
1835 		err = -ENOMEM;
1836 		goto out;
1837 	}
1838 
1839 	for (i = 0; i < fuzz_iterations * 8; i++) {
1840 		generate_random_hash_testvec(&rng, generic_desc, &vec,
1841 					     maxkeysize, maxdatasize,
1842 					     vec_name, sizeof(vec_name));
1843 		generate_random_testvec_config(&rng, cfg, cfgname,
1844 					       sizeof(cfgname));
1845 
1846 		err = test_hash_vec_cfg(&vec, vec_name, cfg,
1847 					req, desc, tsgl, hashstate);
1848 		if (err)
1849 			goto out;
1850 		cond_resched();
1851 	}
1852 	err = 0;
1853 out:
1854 	kfree(cfg);
1855 	kfree(vec.key);
1856 	kfree(vec.plaintext);
1857 	kfree(vec.digest);
1858 	crypto_free_shash(generic_tfm);
1859 	kfree_sensitive(generic_desc);
1860 	return err;
1861 }
1862 
1863 static int alloc_shash(const char *driver, u32 type, u32 mask,
1864 		       struct crypto_shash **tfm_ret,
1865 		       struct shash_desc **desc_ret)
1866 {
1867 	struct crypto_shash *tfm;
1868 	struct shash_desc *desc;
1869 
1870 	tfm = crypto_alloc_shash(driver, type, mask);
1871 	if (IS_ERR(tfm)) {
1872 		if (PTR_ERR(tfm) == -ENOENT) {
1873 			/*
1874 			 * This algorithm is only available through the ahash
1875 			 * API, not the shash API, so skip the shash tests.
1876 			 */
1877 			return 0;
1878 		}
1879 		pr_err("alg: hash: failed to allocate shash transform for %s: %ld\n",
1880 		       driver, PTR_ERR(tfm));
1881 		return PTR_ERR(tfm);
1882 	}
1883 
1884 	desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL);
1885 	if (!desc) {
1886 		crypto_free_shash(tfm);
1887 		return -ENOMEM;
1888 	}
1889 	desc->tfm = tfm;
1890 
1891 	*tfm_ret = tfm;
1892 	*desc_ret = desc;
1893 	return 0;
1894 }
1895 
1896 static int __alg_test_hash(const struct hash_testvec *vecs,
1897 			   unsigned int num_vecs, const char *driver,
1898 			   u32 type, u32 mask,
1899 			   const char *generic_driver, unsigned int maxkeysize)
1900 {
1901 	struct crypto_ahash *atfm = NULL;
1902 	struct ahash_request *req = NULL;
1903 	struct crypto_shash *stfm = NULL;
1904 	struct shash_desc *desc = NULL;
1905 	struct test_sglist *tsgl = NULL;
1906 	u8 *hashstate = NULL;
1907 	unsigned int statesize;
1908 	unsigned int i;
1909 	int err;
1910 
1911 	/*
1912 	 * Always test the ahash API.  This works regardless of whether the
1913 	 * algorithm is implemented as ahash or shash.
1914 	 */
1915 
1916 	atfm = crypto_alloc_ahash(driver, type, mask);
1917 	if (IS_ERR(atfm)) {
1918 		if (PTR_ERR(atfm) == -ENOENT)
1919 			return 0;
1920 		pr_err("alg: hash: failed to allocate transform for %s: %ld\n",
1921 		       driver, PTR_ERR(atfm));
1922 		return PTR_ERR(atfm);
1923 	}
1924 	driver = crypto_ahash_driver_name(atfm);
1925 
1926 	req = ahash_request_alloc(atfm, GFP_KERNEL);
1927 	if (!req) {
1928 		pr_err("alg: hash: failed to allocate request for %s\n",
1929 		       driver);
1930 		err = -ENOMEM;
1931 		goto out;
1932 	}
1933 
1934 	/*
1935 	 * If available also test the shash API, to cover corner cases that may
1936 	 * be missed by testing the ahash API only.
1937 	 */
1938 	err = alloc_shash(driver, type, mask, &stfm, &desc);
1939 	if (err)
1940 		goto out;
1941 
1942 	tsgl = kmalloc(sizeof(*tsgl), GFP_KERNEL);
1943 	if (!tsgl || init_test_sglist(tsgl) != 0) {
1944 		pr_err("alg: hash: failed to allocate test buffers for %s\n",
1945 		       driver);
1946 		kfree(tsgl);
1947 		tsgl = NULL;
1948 		err = -ENOMEM;
1949 		goto out;
1950 	}
1951 
1952 	statesize = crypto_ahash_statesize(atfm);
1953 	if (stfm)
1954 		statesize = max(statesize, crypto_shash_statesize(stfm));
1955 	hashstate = kmalloc(statesize + TESTMGR_POISON_LEN, GFP_KERNEL);
1956 	if (!hashstate) {
1957 		pr_err("alg: hash: failed to allocate hash state buffer for %s\n",
1958 		       driver);
1959 		err = -ENOMEM;
1960 		goto out;
1961 	}
1962 
1963 	for (i = 0; i < num_vecs; i++) {
1964 		if (fips_enabled && vecs[i].fips_skip)
1965 			continue;
1966 
1967 		err = test_hash_vec(&vecs[i], i, req, desc, tsgl, hashstate);
1968 		if (err)
1969 			goto out;
1970 		cond_resched();
1971 	}
1972 	err = test_hash_vs_generic_impl(generic_driver, maxkeysize, req,
1973 					desc, tsgl, hashstate);
1974 out:
1975 	kfree(hashstate);
1976 	if (tsgl) {
1977 		destroy_test_sglist(tsgl);
1978 		kfree(tsgl);
1979 	}
1980 	kfree(desc);
1981 	crypto_free_shash(stfm);
1982 	ahash_request_free(req);
1983 	crypto_free_ahash(atfm);
1984 	return err;
1985 }
1986 
1987 static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1988 			 u32 type, u32 mask)
1989 {
1990 	const struct hash_testvec *template = desc->suite.hash.vecs;
1991 	unsigned int tcount = desc->suite.hash.count;
1992 	unsigned int nr_unkeyed, nr_keyed;
1993 	unsigned int maxkeysize = 0;
1994 	int err;
1995 
1996 	/*
1997 	 * For OPTIONAL_KEY algorithms, we have to do all the unkeyed tests
1998 	 * first, before setting a key on the tfm.  To make this easier, we
1999 	 * require that the unkeyed test vectors (if any) are listed first.
2000 	 */
2001 
2002 	for (nr_unkeyed = 0; nr_unkeyed < tcount; nr_unkeyed++) {
2003 		if (template[nr_unkeyed].ksize)
2004 			break;
2005 	}
2006 	for (nr_keyed = 0; nr_unkeyed + nr_keyed < tcount; nr_keyed++) {
2007 		if (!template[nr_unkeyed + nr_keyed].ksize) {
2008 			pr_err("alg: hash: test vectors for %s out of order, "
2009 			       "unkeyed ones must come first\n", desc->alg);
2010 			return -EINVAL;
2011 		}
2012 		maxkeysize = max_t(unsigned int, maxkeysize,
2013 				   template[nr_unkeyed + nr_keyed].ksize);
2014 	}
2015 
2016 	err = 0;
2017 	if (nr_unkeyed) {
2018 		err = __alg_test_hash(template, nr_unkeyed, driver, type, mask,
2019 				      desc->generic_driver, maxkeysize);
2020 		template += nr_unkeyed;
2021 	}
2022 
2023 	if (!err && nr_keyed)
2024 		err = __alg_test_hash(template, nr_keyed, driver, type, mask,
2025 				      desc->generic_driver, maxkeysize);
2026 
2027 	return err;
2028 }
2029 
2030 static int test_aead_vec_cfg(int enc, const struct aead_testvec *vec,
2031 			     const char *vec_name,
2032 			     const struct testvec_config *cfg,
2033 			     struct aead_request *req,
2034 			     struct cipher_test_sglists *tsgls)
2035 {
2036 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2037 	const unsigned int alignmask = crypto_aead_alignmask(tfm);
2038 	const unsigned int ivsize = crypto_aead_ivsize(tfm);
2039 	const unsigned int authsize = vec->clen - vec->plen;
2040 	const char *driver = crypto_aead_driver_name(tfm);
2041 	const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
2042 	const char *op = enc ? "encryption" : "decryption";
2043 	DECLARE_CRYPTO_WAIT(wait);
2044 	u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
2045 	u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
2046 		 cfg->iv_offset +
2047 		 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
2048 	struct kvec input[2];
2049 	int err;
2050 
2051 	/* Set the key */
2052 	if (vec->wk)
2053 		crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
2054 	else
2055 		crypto_aead_clear_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
2056 
2057 	err = do_setkey(crypto_aead_setkey, tfm, vec->key, vec->klen,
2058 			cfg, alignmask);
2059 	if (err && err != vec->setkey_error) {
2060 		pr_err("alg: aead: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
2061 		       driver, vec_name, vec->setkey_error, err,
2062 		       crypto_aead_get_flags(tfm));
2063 		return err;
2064 	}
2065 	if (!err && vec->setkey_error) {
2066 		pr_err("alg: aead: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
2067 		       driver, vec_name, vec->setkey_error);
2068 		return -EINVAL;
2069 	}
2070 
2071 	/* Set the authentication tag size */
2072 	err = crypto_aead_setauthsize(tfm, authsize);
2073 	if (err && err != vec->setauthsize_error) {
2074 		pr_err("alg: aead: %s setauthsize failed on test vector %s; expected_error=%d, actual_error=%d\n",
2075 		       driver, vec_name, vec->setauthsize_error, err);
2076 		return err;
2077 	}
2078 	if (!err && vec->setauthsize_error) {
2079 		pr_err("alg: aead: %s setauthsize unexpectedly succeeded on test vector %s; expected_error=%d\n",
2080 		       driver, vec_name, vec->setauthsize_error);
2081 		return -EINVAL;
2082 	}
2083 
2084 	if (vec->setkey_error || vec->setauthsize_error)
2085 		return 0;
2086 
2087 	/* The IV must be copied to a buffer, as the algorithm may modify it */
2088 	if (WARN_ON(ivsize > MAX_IVLEN))
2089 		return -EINVAL;
2090 	if (vec->iv)
2091 		memcpy(iv, vec->iv, ivsize);
2092 	else
2093 		memset(iv, 0, ivsize);
2094 
2095 	/* Build the src/dst scatterlists */
2096 	input[0].iov_base = (void *)vec->assoc;
2097 	input[0].iov_len = vec->alen;
2098 	input[1].iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
2099 	input[1].iov_len = enc ? vec->plen : vec->clen;
2100 	err = build_cipher_test_sglists(tsgls, cfg, alignmask,
2101 					vec->alen + (enc ? vec->plen :
2102 						     vec->clen),
2103 					vec->alen + (enc ? vec->clen :
2104 						     vec->plen),
2105 					input, 2);
2106 	if (err) {
2107 		pr_err("alg: aead: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
2108 		       driver, op, vec_name, cfg->name);
2109 		return err;
2110 	}
2111 
2112 	/* Do the actual encryption or decryption */
2113 	testmgr_poison(req->__ctx, crypto_aead_reqsize(tfm));
2114 	aead_request_set_callback(req, req_flags, crypto_req_done, &wait);
2115 	aead_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
2116 			       enc ? vec->plen : vec->clen, iv);
2117 	aead_request_set_ad(req, vec->alen);
2118 	if (cfg->nosimd)
2119 		crypto_disable_simd_for_test();
2120 	err = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
2121 	if (cfg->nosimd)
2122 		crypto_reenable_simd_for_test();
2123 	err = crypto_wait_req(err, &wait);
2124 
2125 	/* Check that the algorithm didn't overwrite things it shouldn't have */
2126 	if (req->cryptlen != (enc ? vec->plen : vec->clen) ||
2127 	    req->assoclen != vec->alen ||
2128 	    req->iv != iv ||
2129 	    req->src != tsgls->src.sgl_ptr ||
2130 	    req->dst != tsgls->dst.sgl_ptr ||
2131 	    crypto_aead_reqtfm(req) != tfm ||
2132 	    req->base.complete != crypto_req_done ||
2133 	    req->base.flags != req_flags ||
2134 	    req->base.data != &wait) {
2135 		pr_err("alg: aead: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
2136 		       driver, op, vec_name, cfg->name);
2137 		if (req->cryptlen != (enc ? vec->plen : vec->clen))
2138 			pr_err("alg: aead: changed 'req->cryptlen'\n");
2139 		if (req->assoclen != vec->alen)
2140 			pr_err("alg: aead: changed 'req->assoclen'\n");
2141 		if (req->iv != iv)
2142 			pr_err("alg: aead: changed 'req->iv'\n");
2143 		if (req->src != tsgls->src.sgl_ptr)
2144 			pr_err("alg: aead: changed 'req->src'\n");
2145 		if (req->dst != tsgls->dst.sgl_ptr)
2146 			pr_err("alg: aead: changed 'req->dst'\n");
2147 		if (crypto_aead_reqtfm(req) != tfm)
2148 			pr_err("alg: aead: changed 'req->base.tfm'\n");
2149 		if (req->base.complete != crypto_req_done)
2150 			pr_err("alg: aead: changed 'req->base.complete'\n");
2151 		if (req->base.flags != req_flags)
2152 			pr_err("alg: aead: changed 'req->base.flags'\n");
2153 		if (req->base.data != &wait)
2154 			pr_err("alg: aead: changed 'req->base.data'\n");
2155 		return -EINVAL;
2156 	}
2157 	if (is_test_sglist_corrupted(&tsgls->src)) {
2158 		pr_err("alg: aead: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
2159 		       driver, op, vec_name, cfg->name);
2160 		return -EINVAL;
2161 	}
2162 	if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
2163 	    is_test_sglist_corrupted(&tsgls->dst)) {
2164 		pr_err("alg: aead: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
2165 		       driver, op, vec_name, cfg->name);
2166 		return -EINVAL;
2167 	}
2168 
2169 	/* Check for unexpected success or failure, or wrong error code */
2170 	if ((err == 0 && vec->novrfy) ||
2171 	    (err != vec->crypt_error && !(err == -EBADMSG && vec->novrfy))) {
2172 		char expected_error[32];
2173 
2174 		if (vec->novrfy &&
2175 		    vec->crypt_error != 0 && vec->crypt_error != -EBADMSG)
2176 			sprintf(expected_error, "-EBADMSG or %d",
2177 				vec->crypt_error);
2178 		else if (vec->novrfy)
2179 			sprintf(expected_error, "-EBADMSG");
2180 		else
2181 			sprintf(expected_error, "%d", vec->crypt_error);
2182 		if (err) {
2183 			pr_err("alg: aead: %s %s failed on test vector %s; expected_error=%s, actual_error=%d, cfg=\"%s\"\n",
2184 			       driver, op, vec_name, expected_error, err,
2185 			       cfg->name);
2186 			return err;
2187 		}
2188 		pr_err("alg: aead: %s %s unexpectedly succeeded on test vector %s; expected_error=%s, cfg=\"%s\"\n",
2189 		       driver, op, vec_name, expected_error, cfg->name);
2190 		return -EINVAL;
2191 	}
2192 	if (err) /* Expectedly failed. */
2193 		return 0;
2194 
2195 	/* Check for the correct output (ciphertext or plaintext) */
2196 	err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
2197 				    enc ? vec->clen : vec->plen,
2198 				    vec->alen,
2199 				    enc || cfg->inplace_mode == OUT_OF_PLACE);
2200 	if (err == -EOVERFLOW) {
2201 		pr_err("alg: aead: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
2202 		       driver, op, vec_name, cfg->name);
2203 		return err;
2204 	}
2205 	if (err) {
2206 		pr_err("alg: aead: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
2207 		       driver, op, vec_name, cfg->name);
2208 		return err;
2209 	}
2210 
2211 	return 0;
2212 }
2213 
2214 static int test_aead_vec(int enc, const struct aead_testvec *vec,
2215 			 unsigned int vec_num, struct aead_request *req,
2216 			 struct cipher_test_sglists *tsgls)
2217 {
2218 	char vec_name[16];
2219 	unsigned int i;
2220 	int err;
2221 
2222 	if (enc && vec->novrfy)
2223 		return 0;
2224 
2225 	sprintf(vec_name, "%u", vec_num);
2226 
2227 	for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
2228 		err = test_aead_vec_cfg(enc, vec, vec_name,
2229 					&default_cipher_testvec_configs[i],
2230 					req, tsgls);
2231 		if (err)
2232 			return err;
2233 	}
2234 
2235 	if (!noslowtests) {
2236 		struct rnd_state rng;
2237 		struct testvec_config cfg;
2238 		char cfgname[TESTVEC_CONFIG_NAMELEN];
2239 
2240 		init_rnd_state(&rng);
2241 
2242 		for (i = 0; i < fuzz_iterations; i++) {
2243 			generate_random_testvec_config(&rng, &cfg, cfgname,
2244 						       sizeof(cfgname));
2245 			err = test_aead_vec_cfg(enc, vec, vec_name,
2246 						&cfg, req, tsgls);
2247 			if (err)
2248 				return err;
2249 			cond_resched();
2250 		}
2251 	}
2252 	return 0;
2253 }
2254 
2255 struct aead_slow_tests_ctx {
2256 	struct rnd_state rng;
2257 	struct aead_request *req;
2258 	struct crypto_aead *tfm;
2259 	const struct alg_test_desc *test_desc;
2260 	struct cipher_test_sglists *tsgls;
2261 	unsigned int maxdatasize;
2262 	unsigned int maxkeysize;
2263 
2264 	struct aead_testvec vec;
2265 	char vec_name[64];
2266 	char cfgname[TESTVEC_CONFIG_NAMELEN];
2267 	struct testvec_config cfg;
2268 };
2269 
2270 /*
2271  * Make at least one random change to a (ciphertext, AAD) pair.  "Ciphertext"
2272  * here means the full ciphertext including the authentication tag.  The
2273  * authentication tag (and hence also the ciphertext) is assumed to be nonempty.
2274  */
2275 static void mutate_aead_message(struct rnd_state *rng,
2276 				struct aead_testvec *vec, bool aad_iv,
2277 				unsigned int ivsize)
2278 {
2279 	const unsigned int aad_tail_size = aad_iv ? ivsize : 0;
2280 	const unsigned int authsize = vec->clen - vec->plen;
2281 
2282 	if (prandom_bool(rng) && vec->alen > aad_tail_size) {
2283 		 /* Mutate the AAD */
2284 		flip_random_bit(rng, (u8 *)vec->assoc,
2285 				vec->alen - aad_tail_size);
2286 		if (prandom_bool(rng))
2287 			return;
2288 	}
2289 	if (prandom_bool(rng)) {
2290 		/* Mutate auth tag (assuming it's at the end of ciphertext) */
2291 		flip_random_bit(rng, (u8 *)vec->ctext + vec->plen, authsize);
2292 	} else {
2293 		/* Mutate any part of the ciphertext */
2294 		flip_random_bit(rng, (u8 *)vec->ctext, vec->clen);
2295 	}
2296 }
2297 
2298 /*
2299  * Minimum authentication tag size in bytes at which we assume that we can
2300  * reliably generate inauthentic messages, i.e. not generate an authentic
2301  * message by chance.
2302  */
2303 #define MIN_COLLISION_FREE_AUTHSIZE 8
2304 
2305 static void generate_aead_message(struct rnd_state *rng,
2306 				  struct aead_request *req,
2307 				  const struct aead_test_suite *suite,
2308 				  struct aead_testvec *vec,
2309 				  bool prefer_inauthentic)
2310 {
2311 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2312 	const unsigned int ivsize = crypto_aead_ivsize(tfm);
2313 	const unsigned int authsize = vec->clen - vec->plen;
2314 	const bool inauthentic = (authsize >= MIN_COLLISION_FREE_AUTHSIZE) &&
2315 				 (prefer_inauthentic ||
2316 				  prandom_u32_below(rng, 4) == 0);
2317 
2318 	/* Generate the AAD. */
2319 	generate_random_bytes(rng, (u8 *)vec->assoc, vec->alen);
2320 	if (suite->aad_iv && vec->alen >= ivsize)
2321 		/* Avoid implementation-defined behavior. */
2322 		memcpy((u8 *)vec->assoc + vec->alen - ivsize, vec->iv, ivsize);
2323 
2324 	if (inauthentic && prandom_bool(rng)) {
2325 		/* Generate a random ciphertext. */
2326 		generate_random_bytes(rng, (u8 *)vec->ctext, vec->clen);
2327 	} else {
2328 		int i = 0;
2329 		struct scatterlist src[2], dst;
2330 		u8 iv[MAX_IVLEN];
2331 		DECLARE_CRYPTO_WAIT(wait);
2332 
2333 		/* Generate a random plaintext and encrypt it. */
2334 		sg_init_table(src, 2);
2335 		if (vec->alen)
2336 			sg_set_buf(&src[i++], vec->assoc, vec->alen);
2337 		if (vec->plen) {
2338 			generate_random_bytes(rng, (u8 *)vec->ptext, vec->plen);
2339 			sg_set_buf(&src[i++], vec->ptext, vec->plen);
2340 		}
2341 		sg_init_one(&dst, vec->ctext, vec->alen + vec->clen);
2342 		memcpy(iv, vec->iv, ivsize);
2343 		aead_request_set_callback(req, 0, crypto_req_done, &wait);
2344 		aead_request_set_crypt(req, src, &dst, vec->plen, iv);
2345 		aead_request_set_ad(req, vec->alen);
2346 		vec->crypt_error = crypto_wait_req(crypto_aead_encrypt(req),
2347 						   &wait);
2348 		/* If encryption failed, we're done. */
2349 		if (vec->crypt_error != 0)
2350 			return;
2351 		memmove((u8 *)vec->ctext, vec->ctext + vec->alen, vec->clen);
2352 		if (!inauthentic)
2353 			return;
2354 		/*
2355 		 * Mutate the authentic (ciphertext, AAD) pair to get an
2356 		 * inauthentic one.
2357 		 */
2358 		mutate_aead_message(rng, vec, suite->aad_iv, ivsize);
2359 	}
2360 	vec->novrfy = 1;
2361 	if (suite->einval_allowed)
2362 		vec->crypt_error = -EINVAL;
2363 }
2364 
2365 /*
2366  * Generate an AEAD test vector 'vec' using the implementation specified by
2367  * 'req'.  The buffers in 'vec' must already be allocated.
2368  *
2369  * If 'prefer_inauthentic' is true, then this function will generate inauthentic
2370  * test vectors (i.e. vectors with 'vec->novrfy=1') more often.
2371  */
2372 static void generate_random_aead_testvec(struct rnd_state *rng,
2373 					 struct aead_request *req,
2374 					 struct aead_testvec *vec,
2375 					 const struct aead_test_suite *suite,
2376 					 unsigned int maxkeysize,
2377 					 unsigned int maxdatasize,
2378 					 char *name, size_t max_namelen,
2379 					 bool prefer_inauthentic)
2380 {
2381 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2382 	const unsigned int ivsize = crypto_aead_ivsize(tfm);
2383 	const unsigned int maxauthsize = crypto_aead_maxauthsize(tfm);
2384 	unsigned int authsize;
2385 	unsigned int total_len;
2386 
2387 	/* Key: length in [0, maxkeysize], but usually choose maxkeysize */
2388 	vec->klen = maxkeysize;
2389 	if (prandom_u32_below(rng, 4) == 0)
2390 		vec->klen = prandom_u32_below(rng, maxkeysize + 1);
2391 	generate_random_bytes(rng, (u8 *)vec->key, vec->klen);
2392 	vec->setkey_error = crypto_aead_setkey(tfm, vec->key, vec->klen);
2393 
2394 	/* IV */
2395 	generate_random_bytes(rng, (u8 *)vec->iv, ivsize);
2396 
2397 	/* Tag length: in [0, maxauthsize], but usually choose maxauthsize */
2398 	authsize = maxauthsize;
2399 	if (prandom_u32_below(rng, 4) == 0)
2400 		authsize = prandom_u32_below(rng, maxauthsize + 1);
2401 	if (prefer_inauthentic && authsize < MIN_COLLISION_FREE_AUTHSIZE)
2402 		authsize = MIN_COLLISION_FREE_AUTHSIZE;
2403 	if (WARN_ON(authsize > maxdatasize))
2404 		authsize = maxdatasize;
2405 	maxdatasize -= authsize;
2406 	vec->setauthsize_error = crypto_aead_setauthsize(tfm, authsize);
2407 
2408 	/* AAD, plaintext, and ciphertext lengths */
2409 	total_len = generate_random_length(rng, maxdatasize);
2410 	if (prandom_u32_below(rng, 4) == 0)
2411 		vec->alen = 0;
2412 	else
2413 		vec->alen = generate_random_length(rng, total_len);
2414 	vec->plen = total_len - vec->alen;
2415 	vec->clen = vec->plen + authsize;
2416 
2417 	/*
2418 	 * Generate the AAD, plaintext, and ciphertext.  Not applicable if the
2419 	 * key or the authentication tag size couldn't be set.
2420 	 */
2421 	vec->novrfy = 0;
2422 	vec->crypt_error = 0;
2423 	if (vec->setkey_error == 0 && vec->setauthsize_error == 0)
2424 		generate_aead_message(rng, req, suite, vec, prefer_inauthentic);
2425 	snprintf(name, max_namelen,
2426 		 "\"random: alen=%u plen=%u authsize=%u klen=%u novrfy=%d\"",
2427 		 vec->alen, vec->plen, authsize, vec->klen, vec->novrfy);
2428 }
2429 
2430 static void try_to_generate_inauthentic_testvec(struct aead_slow_tests_ctx *ctx)
2431 {
2432 	int i;
2433 
2434 	for (i = 0; i < 10; i++) {
2435 		generate_random_aead_testvec(&ctx->rng, ctx->req, &ctx->vec,
2436 					     &ctx->test_desc->suite.aead,
2437 					     ctx->maxkeysize, ctx->maxdatasize,
2438 					     ctx->vec_name,
2439 					     sizeof(ctx->vec_name), true);
2440 		if (ctx->vec.novrfy)
2441 			return;
2442 	}
2443 }
2444 
2445 /*
2446  * Generate inauthentic test vectors (i.e. ciphertext, AAD pairs that aren't the
2447  * result of an encryption with the key) and verify that decryption fails.
2448  */
2449 static int test_aead_inauthentic_inputs(struct aead_slow_tests_ctx *ctx)
2450 {
2451 	unsigned int i;
2452 	int err;
2453 
2454 	for (i = 0; i < fuzz_iterations * 8; i++) {
2455 		/*
2456 		 * Since this part of the tests isn't comparing the
2457 		 * implementation to another, there's no point in testing any
2458 		 * test vectors other than inauthentic ones (vec.novrfy=1) here.
2459 		 *
2460 		 * If we're having trouble generating such a test vector, e.g.
2461 		 * if the algorithm keeps rejecting the generated keys, don't
2462 		 * retry forever; just continue on.
2463 		 */
2464 		try_to_generate_inauthentic_testvec(ctx);
2465 		if (ctx->vec.novrfy) {
2466 			generate_random_testvec_config(&ctx->rng, &ctx->cfg,
2467 						       ctx->cfgname,
2468 						       sizeof(ctx->cfgname));
2469 			err = test_aead_vec_cfg(DECRYPT, &ctx->vec,
2470 						ctx->vec_name, &ctx->cfg,
2471 						ctx->req, ctx->tsgls);
2472 			if (err)
2473 				return err;
2474 		}
2475 		cond_resched();
2476 	}
2477 	return 0;
2478 }
2479 
2480 /*
2481  * Test the AEAD algorithm against the corresponding generic implementation, if
2482  * one is available.
2483  */
2484 static int test_aead_vs_generic_impl(struct aead_slow_tests_ctx *ctx)
2485 {
2486 	struct crypto_aead *tfm = ctx->tfm;
2487 	const char *algname = crypto_aead_alg(tfm)->base.cra_name;
2488 	const char *driver = crypto_aead_driver_name(tfm);
2489 	const char *generic_driver = ctx->test_desc->generic_driver;
2490 	char _generic_driver[CRYPTO_MAX_ALG_NAME];
2491 	struct crypto_aead *generic_tfm = NULL;
2492 	struct aead_request *generic_req = NULL;
2493 	unsigned int i;
2494 	int err;
2495 
2496 	if (!generic_driver) { /* Use default naming convention? */
2497 		err = build_generic_driver_name(algname, _generic_driver);
2498 		if (err)
2499 			return err;
2500 		generic_driver = _generic_driver;
2501 	}
2502 
2503 	if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
2504 		return 0;
2505 
2506 	generic_tfm = crypto_alloc_aead(generic_driver, 0, 0);
2507 	if (IS_ERR(generic_tfm)) {
2508 		err = PTR_ERR(generic_tfm);
2509 		if (err == -ENOENT) {
2510 			pr_warn("alg: aead: skipping comparison tests for %s because %s is unavailable\n",
2511 				driver, generic_driver);
2512 			return 0;
2513 		}
2514 		pr_err("alg: aead: error allocating %s (generic impl of %s): %d\n",
2515 		       generic_driver, algname, err);
2516 		return err;
2517 	}
2518 
2519 	generic_req = aead_request_alloc(generic_tfm, GFP_KERNEL);
2520 	if (!generic_req) {
2521 		err = -ENOMEM;
2522 		goto out;
2523 	}
2524 
2525 	/* Check the algorithm properties for consistency. */
2526 
2527 	if (crypto_aead_maxauthsize(tfm) !=
2528 	    crypto_aead_maxauthsize(generic_tfm)) {
2529 		pr_err("alg: aead: maxauthsize for %s (%u) doesn't match generic impl (%u)\n",
2530 		       driver, crypto_aead_maxauthsize(tfm),
2531 		       crypto_aead_maxauthsize(generic_tfm));
2532 		err = -EINVAL;
2533 		goto out;
2534 	}
2535 
2536 	if (crypto_aead_ivsize(tfm) != crypto_aead_ivsize(generic_tfm)) {
2537 		pr_err("alg: aead: ivsize for %s (%u) doesn't match generic impl (%u)\n",
2538 		       driver, crypto_aead_ivsize(tfm),
2539 		       crypto_aead_ivsize(generic_tfm));
2540 		err = -EINVAL;
2541 		goto out;
2542 	}
2543 
2544 	if (crypto_aead_blocksize(tfm) != crypto_aead_blocksize(generic_tfm)) {
2545 		pr_err("alg: aead: blocksize for %s (%u) doesn't match generic impl (%u)\n",
2546 		       driver, crypto_aead_blocksize(tfm),
2547 		       crypto_aead_blocksize(generic_tfm));
2548 		err = -EINVAL;
2549 		goto out;
2550 	}
2551 
2552 	/*
2553 	 * Now generate test vectors using the generic implementation, and test
2554 	 * the other implementation against them.
2555 	 */
2556 	for (i = 0; i < fuzz_iterations * 8; i++) {
2557 		generate_random_aead_testvec(&ctx->rng, generic_req, &ctx->vec,
2558 					     &ctx->test_desc->suite.aead,
2559 					     ctx->maxkeysize, ctx->maxdatasize,
2560 					     ctx->vec_name,
2561 					     sizeof(ctx->vec_name), false);
2562 		generate_random_testvec_config(&ctx->rng, &ctx->cfg,
2563 					       ctx->cfgname,
2564 					       sizeof(ctx->cfgname));
2565 		if (!ctx->vec.novrfy) {
2566 			err = test_aead_vec_cfg(ENCRYPT, &ctx->vec,
2567 						ctx->vec_name, &ctx->cfg,
2568 						ctx->req, ctx->tsgls);
2569 			if (err)
2570 				goto out;
2571 		}
2572 		if (ctx->vec.crypt_error == 0 || ctx->vec.novrfy) {
2573 			err = test_aead_vec_cfg(DECRYPT, &ctx->vec,
2574 						ctx->vec_name, &ctx->cfg,
2575 						ctx->req, ctx->tsgls);
2576 			if (err)
2577 				goto out;
2578 		}
2579 		cond_resched();
2580 	}
2581 	err = 0;
2582 out:
2583 	crypto_free_aead(generic_tfm);
2584 	aead_request_free(generic_req);
2585 	return err;
2586 }
2587 
2588 static int test_aead_slow(const struct alg_test_desc *test_desc,
2589 			  struct aead_request *req,
2590 			  struct cipher_test_sglists *tsgls)
2591 {
2592 	struct aead_slow_tests_ctx *ctx;
2593 	unsigned int i;
2594 	int err;
2595 
2596 	if (noslowtests)
2597 		return 0;
2598 
2599 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2600 	if (!ctx)
2601 		return -ENOMEM;
2602 	init_rnd_state(&ctx->rng);
2603 	ctx->req = req;
2604 	ctx->tfm = crypto_aead_reqtfm(req);
2605 	ctx->test_desc = test_desc;
2606 	ctx->tsgls = tsgls;
2607 	ctx->maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
2608 	ctx->maxkeysize = 0;
2609 	for (i = 0; i < test_desc->suite.aead.count; i++)
2610 		ctx->maxkeysize = max_t(unsigned int, ctx->maxkeysize,
2611 					test_desc->suite.aead.vecs[i].klen);
2612 
2613 	ctx->vec.key = kmalloc(ctx->maxkeysize, GFP_KERNEL);
2614 	ctx->vec.iv = kmalloc(crypto_aead_ivsize(ctx->tfm), GFP_KERNEL);
2615 	ctx->vec.assoc = kmalloc(ctx->maxdatasize, GFP_KERNEL);
2616 	ctx->vec.ptext = kmalloc(ctx->maxdatasize, GFP_KERNEL);
2617 	ctx->vec.ctext = kmalloc(ctx->maxdatasize, GFP_KERNEL);
2618 	if (!ctx->vec.key || !ctx->vec.iv || !ctx->vec.assoc ||
2619 	    !ctx->vec.ptext || !ctx->vec.ctext) {
2620 		err = -ENOMEM;
2621 		goto out;
2622 	}
2623 
2624 	err = test_aead_vs_generic_impl(ctx);
2625 	if (err)
2626 		goto out;
2627 
2628 	err = test_aead_inauthentic_inputs(ctx);
2629 out:
2630 	kfree(ctx->vec.key);
2631 	kfree(ctx->vec.iv);
2632 	kfree(ctx->vec.assoc);
2633 	kfree(ctx->vec.ptext);
2634 	kfree(ctx->vec.ctext);
2635 	kfree(ctx);
2636 	return err;
2637 }
2638 
2639 static int test_aead(int enc, const struct aead_test_suite *suite,
2640 		     struct aead_request *req,
2641 		     struct cipher_test_sglists *tsgls)
2642 {
2643 	unsigned int i;
2644 	int err;
2645 
2646 	for (i = 0; i < suite->count; i++) {
2647 		err = test_aead_vec(enc, &suite->vecs[i], i, req, tsgls);
2648 		if (err)
2649 			return err;
2650 		cond_resched();
2651 	}
2652 	return 0;
2653 }
2654 
2655 static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
2656 			 u32 type, u32 mask)
2657 {
2658 	const struct aead_test_suite *suite = &desc->suite.aead;
2659 	struct crypto_aead *tfm;
2660 	struct aead_request *req = NULL;
2661 	struct cipher_test_sglists *tsgls = NULL;
2662 	int err;
2663 
2664 	if (suite->count <= 0) {
2665 		pr_err("alg: aead: empty test suite for %s\n", driver);
2666 		return -EINVAL;
2667 	}
2668 
2669 	tfm = crypto_alloc_aead(driver, type, mask);
2670 	if (IS_ERR(tfm)) {
2671 		if (PTR_ERR(tfm) == -ENOENT)
2672 			return 0;
2673 		pr_err("alg: aead: failed to allocate transform for %s: %ld\n",
2674 		       driver, PTR_ERR(tfm));
2675 		return PTR_ERR(tfm);
2676 	}
2677 	driver = crypto_aead_driver_name(tfm);
2678 
2679 	req = aead_request_alloc(tfm, GFP_KERNEL);
2680 	if (!req) {
2681 		pr_err("alg: aead: failed to allocate request for %s\n",
2682 		       driver);
2683 		err = -ENOMEM;
2684 		goto out;
2685 	}
2686 
2687 	tsgls = alloc_cipher_test_sglists();
2688 	if (!tsgls) {
2689 		pr_err("alg: aead: failed to allocate test buffers for %s\n",
2690 		       driver);
2691 		err = -ENOMEM;
2692 		goto out;
2693 	}
2694 
2695 	err = test_aead(ENCRYPT, suite, req, tsgls);
2696 	if (err)
2697 		goto out;
2698 
2699 	err = test_aead(DECRYPT, suite, req, tsgls);
2700 	if (err)
2701 		goto out;
2702 
2703 	err = test_aead_slow(desc, req, tsgls);
2704 out:
2705 	free_cipher_test_sglists(tsgls);
2706 	aead_request_free(req);
2707 	crypto_free_aead(tfm);
2708 	return err;
2709 }
2710 
2711 static int test_cipher(struct crypto_cipher *tfm, int enc,
2712 		       const struct cipher_testvec *template,
2713 		       unsigned int tcount)
2714 {
2715 	const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
2716 	unsigned int i, j, k;
2717 	char *q;
2718 	const char *e;
2719 	const char *input, *result;
2720 	void *data;
2721 	char *xbuf[XBUFSIZE];
2722 	int ret = -ENOMEM;
2723 
2724 	if (testmgr_alloc_buf(xbuf))
2725 		goto out_nobuf;
2726 
2727 	if (enc == ENCRYPT)
2728 	        e = "encryption";
2729 	else
2730 		e = "decryption";
2731 
2732 	j = 0;
2733 	for (i = 0; i < tcount; i++) {
2734 
2735 		if (fips_enabled && template[i].fips_skip)
2736 			continue;
2737 
2738 		input  = enc ? template[i].ptext : template[i].ctext;
2739 		result = enc ? template[i].ctext : template[i].ptext;
2740 		j++;
2741 
2742 		ret = -EINVAL;
2743 		if (WARN_ON(template[i].len > PAGE_SIZE))
2744 			goto out;
2745 
2746 		data = xbuf[0];
2747 		memcpy(data, input, template[i].len);
2748 
2749 		crypto_cipher_clear_flags(tfm, ~0);
2750 		if (template[i].wk)
2751 			crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
2752 
2753 		ret = crypto_cipher_setkey(tfm, template[i].key,
2754 					   template[i].klen);
2755 		if (ret) {
2756 			if (ret == template[i].setkey_error)
2757 				continue;
2758 			pr_err("alg: cipher: %s setkey failed on test vector %u; expected_error=%d, actual_error=%d, flags=%#x\n",
2759 			       algo, j, template[i].setkey_error, ret,
2760 			       crypto_cipher_get_flags(tfm));
2761 			goto out;
2762 		}
2763 		if (template[i].setkey_error) {
2764 			pr_err("alg: cipher: %s setkey unexpectedly succeeded on test vector %u; expected_error=%d\n",
2765 			       algo, j, template[i].setkey_error);
2766 			ret = -EINVAL;
2767 			goto out;
2768 		}
2769 
2770 		for (k = 0; k < template[i].len;
2771 		     k += crypto_cipher_blocksize(tfm)) {
2772 			if (enc)
2773 				crypto_cipher_encrypt_one(tfm, data + k,
2774 							  data + k);
2775 			else
2776 				crypto_cipher_decrypt_one(tfm, data + k,
2777 							  data + k);
2778 		}
2779 
2780 		q = data;
2781 		if (memcmp(q, result, template[i].len)) {
2782 			printk(KERN_ERR "alg: cipher: Test %d failed "
2783 			       "on %s for %s\n", j, e, algo);
2784 			hexdump(q, template[i].len);
2785 			ret = -EINVAL;
2786 			goto out;
2787 		}
2788 	}
2789 
2790 	ret = 0;
2791 
2792 out:
2793 	testmgr_free_buf(xbuf);
2794 out_nobuf:
2795 	return ret;
2796 }
2797 
2798 static int test_skcipher_vec_cfg(int enc, const struct cipher_testvec *vec,
2799 				 const char *vec_name,
2800 				 const struct testvec_config *cfg,
2801 				 struct skcipher_request *req,
2802 				 struct cipher_test_sglists *tsgls)
2803 {
2804 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2805 	const unsigned int alignmask = crypto_skcipher_alignmask(tfm);
2806 	const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2807 	const char *driver = crypto_skcipher_driver_name(tfm);
2808 	const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
2809 	const char *op = enc ? "encryption" : "decryption";
2810 	DECLARE_CRYPTO_WAIT(wait);
2811 	u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
2812 	u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
2813 		 cfg->iv_offset +
2814 		 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
2815 	struct kvec input;
2816 	int err;
2817 
2818 	/* Set the key */
2819 	if (vec->wk)
2820 		crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
2821 	else
2822 		crypto_skcipher_clear_flags(tfm,
2823 					    CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
2824 	err = do_setkey(crypto_skcipher_setkey, tfm, vec->key, vec->klen,
2825 			cfg, alignmask);
2826 	if (err) {
2827 		if (err == vec->setkey_error)
2828 			return 0;
2829 		pr_err("alg: skcipher: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
2830 		       driver, vec_name, vec->setkey_error, err,
2831 		       crypto_skcipher_get_flags(tfm));
2832 		return err;
2833 	}
2834 	if (vec->setkey_error) {
2835 		pr_err("alg: skcipher: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
2836 		       driver, vec_name, vec->setkey_error);
2837 		return -EINVAL;
2838 	}
2839 
2840 	/* The IV must be copied to a buffer, as the algorithm may modify it */
2841 	if (ivsize) {
2842 		if (WARN_ON(ivsize > MAX_IVLEN))
2843 			return -EINVAL;
2844 		if (vec->iv)
2845 			memcpy(iv, vec->iv, ivsize);
2846 		else
2847 			memset(iv, 0, ivsize);
2848 	} else {
2849 		iv = NULL;
2850 	}
2851 
2852 	/* Build the src/dst scatterlists */
2853 	input.iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
2854 	input.iov_len = vec->len;
2855 	err = build_cipher_test_sglists(tsgls, cfg, alignmask,
2856 					vec->len, vec->len, &input, 1);
2857 	if (err) {
2858 		pr_err("alg: skcipher: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
2859 		       driver, op, vec_name, cfg->name);
2860 		return err;
2861 	}
2862 
2863 	/* Do the actual encryption or decryption */
2864 	testmgr_poison(req->__ctx, crypto_skcipher_reqsize(tfm));
2865 	skcipher_request_set_callback(req, req_flags, crypto_req_done, &wait);
2866 	skcipher_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
2867 				   vec->len, iv);
2868 	if (cfg->nosimd)
2869 		crypto_disable_simd_for_test();
2870 	err = enc ? crypto_skcipher_encrypt(req) : crypto_skcipher_decrypt(req);
2871 	if (cfg->nosimd)
2872 		crypto_reenable_simd_for_test();
2873 	err = crypto_wait_req(err, &wait);
2874 
2875 	/* Check that the algorithm didn't overwrite things it shouldn't have */
2876 	if (req->cryptlen != vec->len ||
2877 	    req->iv != iv ||
2878 	    req->src != tsgls->src.sgl_ptr ||
2879 	    req->dst != tsgls->dst.sgl_ptr ||
2880 	    crypto_skcipher_reqtfm(req) != tfm ||
2881 	    req->base.complete != crypto_req_done ||
2882 	    req->base.flags != req_flags ||
2883 	    req->base.data != &wait) {
2884 		pr_err("alg: skcipher: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
2885 		       driver, op, vec_name, cfg->name);
2886 		if (req->cryptlen != vec->len)
2887 			pr_err("alg: skcipher: changed 'req->cryptlen'\n");
2888 		if (req->iv != iv)
2889 			pr_err("alg: skcipher: changed 'req->iv'\n");
2890 		if (req->src != tsgls->src.sgl_ptr)
2891 			pr_err("alg: skcipher: changed 'req->src'\n");
2892 		if (req->dst != tsgls->dst.sgl_ptr)
2893 			pr_err("alg: skcipher: changed 'req->dst'\n");
2894 		if (crypto_skcipher_reqtfm(req) != tfm)
2895 			pr_err("alg: skcipher: changed 'req->base.tfm'\n");
2896 		if (req->base.complete != crypto_req_done)
2897 			pr_err("alg: skcipher: changed 'req->base.complete'\n");
2898 		if (req->base.flags != req_flags)
2899 			pr_err("alg: skcipher: changed 'req->base.flags'\n");
2900 		if (req->base.data != &wait)
2901 			pr_err("alg: skcipher: changed 'req->base.data'\n");
2902 		return -EINVAL;
2903 	}
2904 	if (is_test_sglist_corrupted(&tsgls->src)) {
2905 		pr_err("alg: skcipher: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
2906 		       driver, op, vec_name, cfg->name);
2907 		return -EINVAL;
2908 	}
2909 	if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
2910 	    is_test_sglist_corrupted(&tsgls->dst)) {
2911 		pr_err("alg: skcipher: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
2912 		       driver, op, vec_name, cfg->name);
2913 		return -EINVAL;
2914 	}
2915 
2916 	/* Check for success or failure */
2917 	if (err) {
2918 		if (err == vec->crypt_error)
2919 			return 0;
2920 		pr_err("alg: skcipher: %s %s failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
2921 		       driver, op, vec_name, vec->crypt_error, err, cfg->name);
2922 		return err;
2923 	}
2924 	if (vec->crypt_error) {
2925 		pr_err("alg: skcipher: %s %s unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
2926 		       driver, op, vec_name, vec->crypt_error, cfg->name);
2927 		return -EINVAL;
2928 	}
2929 
2930 	/* Check for the correct output (ciphertext or plaintext) */
2931 	err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
2932 				    vec->len, 0, true);
2933 	if (err == -EOVERFLOW) {
2934 		pr_err("alg: skcipher: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
2935 		       driver, op, vec_name, cfg->name);
2936 		return err;
2937 	}
2938 	if (err) {
2939 		pr_err("alg: skcipher: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
2940 		       driver, op, vec_name, cfg->name);
2941 		return err;
2942 	}
2943 
2944 	/* If applicable, check that the algorithm generated the correct IV */
2945 	if (vec->iv_out && memcmp(iv, vec->iv_out, ivsize) != 0) {
2946 		pr_err("alg: skcipher: %s %s test failed (wrong output IV) on test vector %s, cfg=\"%s\"\n",
2947 		       driver, op, vec_name, cfg->name);
2948 		hexdump(iv, ivsize);
2949 		return -EINVAL;
2950 	}
2951 
2952 	return 0;
2953 }
2954 
2955 static int test_skcipher_vec(int enc, const struct cipher_testvec *vec,
2956 			     unsigned int vec_num,
2957 			     struct skcipher_request *req,
2958 			     struct cipher_test_sglists *tsgls)
2959 {
2960 	char vec_name[16];
2961 	unsigned int i;
2962 	int err;
2963 
2964 	if (fips_enabled && vec->fips_skip)
2965 		return 0;
2966 
2967 	sprintf(vec_name, "%u", vec_num);
2968 
2969 	for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
2970 		err = test_skcipher_vec_cfg(enc, vec, vec_name,
2971 					    &default_cipher_testvec_configs[i],
2972 					    req, tsgls);
2973 		if (err)
2974 			return err;
2975 	}
2976 
2977 	if (!noslowtests) {
2978 		struct rnd_state rng;
2979 		struct testvec_config cfg;
2980 		char cfgname[TESTVEC_CONFIG_NAMELEN];
2981 
2982 		init_rnd_state(&rng);
2983 
2984 		for (i = 0; i < fuzz_iterations; i++) {
2985 			generate_random_testvec_config(&rng, &cfg, cfgname,
2986 						       sizeof(cfgname));
2987 			err = test_skcipher_vec_cfg(enc, vec, vec_name,
2988 						    &cfg, req, tsgls);
2989 			if (err)
2990 				return err;
2991 			cond_resched();
2992 		}
2993 	}
2994 	return 0;
2995 }
2996 
2997 /*
2998  * Generate a symmetric cipher test vector from the given implementation.
2999  * Assumes the buffers in 'vec' were already allocated.
3000  */
3001 static void generate_random_cipher_testvec(struct rnd_state *rng,
3002 					   struct skcipher_request *req,
3003 					   struct cipher_testvec *vec,
3004 					   unsigned int maxdatasize,
3005 					   char *name, size_t max_namelen)
3006 {
3007 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
3008 	const unsigned int maxkeysize = crypto_skcipher_max_keysize(tfm);
3009 	const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
3010 	struct scatterlist src, dst;
3011 	u8 iv[MAX_IVLEN];
3012 	DECLARE_CRYPTO_WAIT(wait);
3013 
3014 	/* Key: length in [0, maxkeysize], but usually choose maxkeysize */
3015 	vec->klen = maxkeysize;
3016 	if (prandom_u32_below(rng, 4) == 0)
3017 		vec->klen = prandom_u32_below(rng, maxkeysize + 1);
3018 	generate_random_bytes(rng, (u8 *)vec->key, vec->klen);
3019 	vec->setkey_error = crypto_skcipher_setkey(tfm, vec->key, vec->klen);
3020 
3021 	/* IV */
3022 	generate_random_bytes(rng, (u8 *)vec->iv, ivsize);
3023 
3024 	/* Plaintext */
3025 	vec->len = generate_random_length(rng, maxdatasize);
3026 	generate_random_bytes(rng, (u8 *)vec->ptext, vec->len);
3027 
3028 	/* If the key couldn't be set, no need to continue to encrypt. */
3029 	if (vec->setkey_error)
3030 		goto done;
3031 
3032 	/* Ciphertext */
3033 	sg_init_one(&src, vec->ptext, vec->len);
3034 	sg_init_one(&dst, vec->ctext, vec->len);
3035 	memcpy(iv, vec->iv, ivsize);
3036 	skcipher_request_set_callback(req, 0, crypto_req_done, &wait);
3037 	skcipher_request_set_crypt(req, &src, &dst, vec->len, iv);
3038 	vec->crypt_error = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
3039 	if (vec->crypt_error != 0) {
3040 		/*
3041 		 * The only acceptable error here is for an invalid length, so
3042 		 * skcipher decryption should fail with the same error too.
3043 		 * We'll test for this.  But to keep the API usage well-defined,
3044 		 * explicitly initialize the ciphertext buffer too.
3045 		 */
3046 		memset((u8 *)vec->ctext, 0, vec->len);
3047 	}
3048 done:
3049 	snprintf(name, max_namelen, "\"random: len=%u klen=%u\"",
3050 		 vec->len, vec->klen);
3051 }
3052 
3053 /*
3054  * Test the skcipher algorithm represented by @req against the corresponding
3055  * generic implementation, if one is available.
3056  */
3057 static int test_skcipher_vs_generic_impl(const char *generic_driver,
3058 					 struct skcipher_request *req,
3059 					 struct cipher_test_sglists *tsgls)
3060 {
3061 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
3062 	const unsigned int maxkeysize = crypto_skcipher_max_keysize(tfm);
3063 	const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
3064 	const unsigned int blocksize = crypto_skcipher_blocksize(tfm);
3065 	const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
3066 	const char *algname = crypto_skcipher_alg(tfm)->base.cra_name;
3067 	const char *driver = crypto_skcipher_driver_name(tfm);
3068 	struct rnd_state rng;
3069 	char _generic_driver[CRYPTO_MAX_ALG_NAME];
3070 	struct crypto_skcipher *generic_tfm = NULL;
3071 	struct skcipher_request *generic_req = NULL;
3072 	unsigned int i;
3073 	struct cipher_testvec vec = { 0 };
3074 	char vec_name[64];
3075 	struct testvec_config *cfg;
3076 	char cfgname[TESTVEC_CONFIG_NAMELEN];
3077 	int err;
3078 
3079 	if (noslowtests)
3080 		return 0;
3081 
3082 	init_rnd_state(&rng);
3083 
3084 	if (!generic_driver) { /* Use default naming convention? */
3085 		err = build_generic_driver_name(algname, _generic_driver);
3086 		if (err)
3087 			return err;
3088 		generic_driver = _generic_driver;
3089 	}
3090 
3091 	if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
3092 		return 0;
3093 
3094 	generic_tfm = crypto_alloc_skcipher(generic_driver, 0, 0);
3095 	if (IS_ERR(generic_tfm)) {
3096 		err = PTR_ERR(generic_tfm);
3097 		if (err == -ENOENT) {
3098 			pr_warn("alg: skcipher: skipping comparison tests for %s because %s is unavailable\n",
3099 				driver, generic_driver);
3100 			return 0;
3101 		}
3102 		pr_err("alg: skcipher: error allocating %s (generic impl of %s): %d\n",
3103 		       generic_driver, algname, err);
3104 		return err;
3105 	}
3106 
3107 	cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
3108 	if (!cfg) {
3109 		err = -ENOMEM;
3110 		goto out;
3111 	}
3112 
3113 	generic_req = skcipher_request_alloc(generic_tfm, GFP_KERNEL);
3114 	if (!generic_req) {
3115 		err = -ENOMEM;
3116 		goto out;
3117 	}
3118 
3119 	/* Check the algorithm properties for consistency. */
3120 
3121 	if (crypto_skcipher_min_keysize(tfm) !=
3122 	    crypto_skcipher_min_keysize(generic_tfm)) {
3123 		pr_err("alg: skcipher: min keysize for %s (%u) doesn't match generic impl (%u)\n",
3124 		       driver, crypto_skcipher_min_keysize(tfm),
3125 		       crypto_skcipher_min_keysize(generic_tfm));
3126 		err = -EINVAL;
3127 		goto out;
3128 	}
3129 
3130 	if (maxkeysize != crypto_skcipher_max_keysize(generic_tfm)) {
3131 		pr_err("alg: skcipher: max keysize for %s (%u) doesn't match generic impl (%u)\n",
3132 		       driver, maxkeysize,
3133 		       crypto_skcipher_max_keysize(generic_tfm));
3134 		err = -EINVAL;
3135 		goto out;
3136 	}
3137 
3138 	if (ivsize != crypto_skcipher_ivsize(generic_tfm)) {
3139 		pr_err("alg: skcipher: ivsize for %s (%u) doesn't match generic impl (%u)\n",
3140 		       driver, ivsize, crypto_skcipher_ivsize(generic_tfm));
3141 		err = -EINVAL;
3142 		goto out;
3143 	}
3144 
3145 	if (blocksize != crypto_skcipher_blocksize(generic_tfm)) {
3146 		pr_err("alg: skcipher: blocksize for %s (%u) doesn't match generic impl (%u)\n",
3147 		       driver, blocksize,
3148 		       crypto_skcipher_blocksize(generic_tfm));
3149 		err = -EINVAL;
3150 		goto out;
3151 	}
3152 
3153 	/*
3154 	 * Now generate test vectors using the generic implementation, and test
3155 	 * the other implementation against them.
3156 	 */
3157 
3158 	vec.key = kmalloc(maxkeysize, GFP_KERNEL);
3159 	vec.iv = kmalloc(ivsize, GFP_KERNEL);
3160 	vec.ptext = kmalloc(maxdatasize, GFP_KERNEL);
3161 	vec.ctext = kmalloc(maxdatasize, GFP_KERNEL);
3162 	if (!vec.key || !vec.iv || !vec.ptext || !vec.ctext) {
3163 		err = -ENOMEM;
3164 		goto out;
3165 	}
3166 
3167 	for (i = 0; i < fuzz_iterations * 8; i++) {
3168 		generate_random_cipher_testvec(&rng, generic_req, &vec,
3169 					       maxdatasize,
3170 					       vec_name, sizeof(vec_name));
3171 		generate_random_testvec_config(&rng, cfg, cfgname,
3172 					       sizeof(cfgname));
3173 
3174 		err = test_skcipher_vec_cfg(ENCRYPT, &vec, vec_name,
3175 					    cfg, req, tsgls);
3176 		if (err)
3177 			goto out;
3178 		err = test_skcipher_vec_cfg(DECRYPT, &vec, vec_name,
3179 					    cfg, req, tsgls);
3180 		if (err)
3181 			goto out;
3182 		cond_resched();
3183 	}
3184 	err = 0;
3185 out:
3186 	kfree(cfg);
3187 	kfree(vec.key);
3188 	kfree(vec.iv);
3189 	kfree(vec.ptext);
3190 	kfree(vec.ctext);
3191 	crypto_free_skcipher(generic_tfm);
3192 	skcipher_request_free(generic_req);
3193 	return err;
3194 }
3195 
3196 static int test_skcipher(int enc, const struct cipher_test_suite *suite,
3197 			 struct skcipher_request *req,
3198 			 struct cipher_test_sglists *tsgls)
3199 {
3200 	unsigned int i;
3201 	int err;
3202 
3203 	for (i = 0; i < suite->count; i++) {
3204 		err = test_skcipher_vec(enc, &suite->vecs[i], i, req, tsgls);
3205 		if (err)
3206 			return err;
3207 		cond_resched();
3208 	}
3209 	return 0;
3210 }
3211 
3212 static int alg_test_skcipher(const struct alg_test_desc *desc,
3213 			     const char *driver, u32 type, u32 mask)
3214 {
3215 	const struct cipher_test_suite *suite = &desc->suite.cipher;
3216 	struct crypto_skcipher *tfm;
3217 	struct skcipher_request *req = NULL;
3218 	struct cipher_test_sglists *tsgls = NULL;
3219 	int err;
3220 
3221 	if (suite->count <= 0) {
3222 		pr_err("alg: skcipher: empty test suite for %s\n", driver);
3223 		return -EINVAL;
3224 	}
3225 
3226 	tfm = crypto_alloc_skcipher(driver, type, mask);
3227 	if (IS_ERR(tfm)) {
3228 		if (PTR_ERR(tfm) == -ENOENT)
3229 			return 0;
3230 		pr_err("alg: skcipher: failed to allocate transform for %s: %ld\n",
3231 		       driver, PTR_ERR(tfm));
3232 		return PTR_ERR(tfm);
3233 	}
3234 	driver = crypto_skcipher_driver_name(tfm);
3235 
3236 	req = skcipher_request_alloc(tfm, GFP_KERNEL);
3237 	if (!req) {
3238 		pr_err("alg: skcipher: failed to allocate request for %s\n",
3239 		       driver);
3240 		err = -ENOMEM;
3241 		goto out;
3242 	}
3243 
3244 	tsgls = alloc_cipher_test_sglists();
3245 	if (!tsgls) {
3246 		pr_err("alg: skcipher: failed to allocate test buffers for %s\n",
3247 		       driver);
3248 		err = -ENOMEM;
3249 		goto out;
3250 	}
3251 
3252 	err = test_skcipher(ENCRYPT, suite, req, tsgls);
3253 	if (err)
3254 		goto out;
3255 
3256 	err = test_skcipher(DECRYPT, suite, req, tsgls);
3257 	if (err)
3258 		goto out;
3259 
3260 	err = test_skcipher_vs_generic_impl(desc->generic_driver, req, tsgls);
3261 out:
3262 	free_cipher_test_sglists(tsgls);
3263 	skcipher_request_free(req);
3264 	crypto_free_skcipher(tfm);
3265 	return err;
3266 }
3267 
3268 static int test_acomp(struct crypto_acomp *tfm,
3269 		      const struct comp_testvec *ctemplate,
3270 		      const struct comp_testvec *dtemplate,
3271 		      int ctcount, int dtcount)
3272 {
3273 	const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
3274 	unsigned int i;
3275 	char *output, *decomp_out;
3276 	int ret;
3277 	struct scatterlist src, dst;
3278 	struct acomp_req *req;
3279 	struct crypto_wait wait;
3280 
3281 	output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3282 	if (!output)
3283 		return -ENOMEM;
3284 
3285 	decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3286 	if (!decomp_out) {
3287 		kfree(output);
3288 		return -ENOMEM;
3289 	}
3290 
3291 	for (i = 0; i < ctcount; i++) {
3292 		unsigned int dlen = COMP_BUF_SIZE;
3293 		int ilen = ctemplate[i].inlen;
3294 		void *input_vec;
3295 
3296 		input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL);
3297 		if (!input_vec) {
3298 			ret = -ENOMEM;
3299 			goto out;
3300 		}
3301 
3302 		memset(output, 0, dlen);
3303 		crypto_init_wait(&wait);
3304 		sg_init_one(&src, input_vec, ilen);
3305 		sg_init_one(&dst, output, dlen);
3306 
3307 		req = acomp_request_alloc(tfm);
3308 		if (!req) {
3309 			pr_err("alg: acomp: request alloc failed for %s\n",
3310 			       algo);
3311 			kfree(input_vec);
3312 			ret = -ENOMEM;
3313 			goto out;
3314 		}
3315 
3316 		acomp_request_set_params(req, &src, &dst, ilen, dlen);
3317 		acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3318 					   crypto_req_done, &wait);
3319 
3320 		ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
3321 		if (ret) {
3322 			pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
3323 			       i + 1, algo, -ret);
3324 			kfree(input_vec);
3325 			acomp_request_free(req);
3326 			goto out;
3327 		}
3328 
3329 		ilen = req->dlen;
3330 		dlen = COMP_BUF_SIZE;
3331 		sg_init_one(&src, output, ilen);
3332 		sg_init_one(&dst, decomp_out, dlen);
3333 		crypto_init_wait(&wait);
3334 		acomp_request_set_params(req, &src, &dst, ilen, dlen);
3335 
3336 		ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
3337 		if (ret) {
3338 			pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
3339 			       i + 1, algo, -ret);
3340 			kfree(input_vec);
3341 			acomp_request_free(req);
3342 			goto out;
3343 		}
3344 
3345 		if (req->dlen != ctemplate[i].inlen) {
3346 			pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
3347 			       i + 1, algo, req->dlen);
3348 			ret = -EINVAL;
3349 			kfree(input_vec);
3350 			acomp_request_free(req);
3351 			goto out;
3352 		}
3353 
3354 		if (memcmp(input_vec, decomp_out, req->dlen)) {
3355 			pr_err("alg: acomp: Compression test %d failed for %s\n",
3356 			       i + 1, algo);
3357 			hexdump(output, req->dlen);
3358 			ret = -EINVAL;
3359 			kfree(input_vec);
3360 			acomp_request_free(req);
3361 			goto out;
3362 		}
3363 
3364 		kfree(input_vec);
3365 		acomp_request_free(req);
3366 	}
3367 
3368 	for (i = 0; i < dtcount; i++) {
3369 		unsigned int dlen = COMP_BUF_SIZE;
3370 		int ilen = dtemplate[i].inlen;
3371 		void *input_vec;
3372 
3373 		input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL);
3374 		if (!input_vec) {
3375 			ret = -ENOMEM;
3376 			goto out;
3377 		}
3378 
3379 		memset(output, 0, dlen);
3380 		crypto_init_wait(&wait);
3381 		sg_init_one(&src, input_vec, ilen);
3382 		sg_init_one(&dst, output, dlen);
3383 
3384 		req = acomp_request_alloc(tfm);
3385 		if (!req) {
3386 			pr_err("alg: acomp: request alloc failed for %s\n",
3387 			       algo);
3388 			kfree(input_vec);
3389 			ret = -ENOMEM;
3390 			goto out;
3391 		}
3392 
3393 		acomp_request_set_params(req, &src, &dst, ilen, dlen);
3394 		acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3395 					   crypto_req_done, &wait);
3396 
3397 		ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
3398 		if (ret) {
3399 			pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
3400 			       i + 1, algo, -ret);
3401 			kfree(input_vec);
3402 			acomp_request_free(req);
3403 			goto out;
3404 		}
3405 
3406 		if (req->dlen != dtemplate[i].outlen) {
3407 			pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
3408 			       i + 1, algo, req->dlen);
3409 			ret = -EINVAL;
3410 			kfree(input_vec);
3411 			acomp_request_free(req);
3412 			goto out;
3413 		}
3414 
3415 		if (memcmp(output, dtemplate[i].output, req->dlen)) {
3416 			pr_err("alg: acomp: Decompression test %d failed for %s\n",
3417 			       i + 1, algo);
3418 			hexdump(output, req->dlen);
3419 			ret = -EINVAL;
3420 			kfree(input_vec);
3421 			acomp_request_free(req);
3422 			goto out;
3423 		}
3424 
3425 		kfree(input_vec);
3426 		acomp_request_free(req);
3427 	}
3428 
3429 	ret = 0;
3430 
3431 out:
3432 	kfree(decomp_out);
3433 	kfree(output);
3434 	return ret;
3435 }
3436 
3437 static int test_cprng(struct crypto_rng *tfm,
3438 		      const struct cprng_testvec *template,
3439 		      unsigned int tcount)
3440 {
3441 	const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
3442 	int err = 0, i, j, seedsize;
3443 	u8 *seed;
3444 	char result[32];
3445 
3446 	seedsize = crypto_rng_seedsize(tfm);
3447 
3448 	seed = kmalloc(seedsize, GFP_KERNEL);
3449 	if (!seed) {
3450 		printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
3451 		       "for %s\n", algo);
3452 		return -ENOMEM;
3453 	}
3454 
3455 	for (i = 0; i < tcount; i++) {
3456 		memset(result, 0, 32);
3457 
3458 		memcpy(seed, template[i].v, template[i].vlen);
3459 		memcpy(seed + template[i].vlen, template[i].key,
3460 		       template[i].klen);
3461 		memcpy(seed + template[i].vlen + template[i].klen,
3462 		       template[i].dt, template[i].dtlen);
3463 
3464 		err = crypto_rng_reset(tfm, seed, seedsize);
3465 		if (err) {
3466 			printk(KERN_ERR "alg: cprng: Failed to reset rng "
3467 			       "for %s\n", algo);
3468 			goto out;
3469 		}
3470 
3471 		for (j = 0; j < template[i].loops; j++) {
3472 			err = crypto_rng_get_bytes(tfm, result,
3473 						   template[i].rlen);
3474 			if (err < 0) {
3475 				printk(KERN_ERR "alg: cprng: Failed to obtain "
3476 				       "the correct amount of random data for "
3477 				       "%s (requested %d)\n", algo,
3478 				       template[i].rlen);
3479 				goto out;
3480 			}
3481 		}
3482 
3483 		err = memcmp(result, template[i].result,
3484 			     template[i].rlen);
3485 		if (err) {
3486 			printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
3487 			       i, algo);
3488 			hexdump(result, template[i].rlen);
3489 			err = -EINVAL;
3490 			goto out;
3491 		}
3492 	}
3493 
3494 out:
3495 	kfree(seed);
3496 	return err;
3497 }
3498 
3499 static int alg_test_cipher(const struct alg_test_desc *desc,
3500 			   const char *driver, u32 type, u32 mask)
3501 {
3502 	const struct cipher_test_suite *suite = &desc->suite.cipher;
3503 	struct crypto_cipher *tfm;
3504 	int err;
3505 
3506 	tfm = crypto_alloc_cipher(driver, type, mask);
3507 	if (IS_ERR(tfm)) {
3508 		if (PTR_ERR(tfm) == -ENOENT)
3509 			return 0;
3510 		printk(KERN_ERR "alg: cipher: Failed to load transform for "
3511 		       "%s: %ld\n", driver, PTR_ERR(tfm));
3512 		return PTR_ERR(tfm);
3513 	}
3514 
3515 	err = test_cipher(tfm, ENCRYPT, suite->vecs, suite->count);
3516 	if (!err)
3517 		err = test_cipher(tfm, DECRYPT, suite->vecs, suite->count);
3518 
3519 	crypto_free_cipher(tfm);
3520 	return err;
3521 }
3522 
3523 static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
3524 			 u32 type, u32 mask)
3525 {
3526 	struct crypto_acomp *acomp;
3527 	int err;
3528 
3529 	acomp = crypto_alloc_acomp(driver, type, mask);
3530 	if (IS_ERR(acomp)) {
3531 		if (PTR_ERR(acomp) == -ENOENT)
3532 			return 0;
3533 		pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
3534 		       driver, PTR_ERR(acomp));
3535 		return PTR_ERR(acomp);
3536 	}
3537 	err = test_acomp(acomp, desc->suite.comp.comp.vecs,
3538 			 desc->suite.comp.decomp.vecs,
3539 			 desc->suite.comp.comp.count,
3540 			 desc->suite.comp.decomp.count);
3541 	crypto_free_acomp(acomp);
3542 	return err;
3543 }
3544 
3545 static int alg_test_crc32c(const struct alg_test_desc *desc,
3546 			   const char *driver, u32 type, u32 mask)
3547 {
3548 	struct crypto_shash *tfm;
3549 	__le32 val;
3550 	int err;
3551 
3552 	err = alg_test_hash(desc, driver, type, mask);
3553 	if (err)
3554 		return err;
3555 
3556 	tfm = crypto_alloc_shash(driver, type, mask);
3557 	if (IS_ERR(tfm)) {
3558 		if (PTR_ERR(tfm) == -ENOENT) {
3559 			/*
3560 			 * This crc32c implementation is only available through
3561 			 * ahash API, not the shash API, so the remaining part
3562 			 * of the test is not applicable to it.
3563 			 */
3564 			return 0;
3565 		}
3566 		printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
3567 		       "%ld\n", driver, PTR_ERR(tfm));
3568 		return PTR_ERR(tfm);
3569 	}
3570 	driver = crypto_shash_driver_name(tfm);
3571 
3572 	do {
3573 		SHASH_DESC_ON_STACK(shash, tfm);
3574 		u32 *ctx = (u32 *)shash_desc_ctx(shash);
3575 
3576 		shash->tfm = tfm;
3577 
3578 		*ctx = 420553207;
3579 		err = crypto_shash_final(shash, (u8 *)&val);
3580 		if (err) {
3581 			printk(KERN_ERR "alg: crc32c: Operation failed for "
3582 			       "%s: %d\n", driver, err);
3583 			break;
3584 		}
3585 
3586 		if (val != cpu_to_le32(~420553207)) {
3587 			pr_err("alg: crc32c: Test failed for %s: %u\n",
3588 			       driver, le32_to_cpu(val));
3589 			err = -EINVAL;
3590 		}
3591 	} while (0);
3592 
3593 	crypto_free_shash(tfm);
3594 
3595 	return err;
3596 }
3597 
3598 static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
3599 			  u32 type, u32 mask)
3600 {
3601 	struct crypto_rng *rng;
3602 	int err;
3603 
3604 	rng = crypto_alloc_rng(driver, type, mask);
3605 	if (IS_ERR(rng)) {
3606 		if (PTR_ERR(rng) == -ENOENT)
3607 			return 0;
3608 		printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
3609 		       "%ld\n", driver, PTR_ERR(rng));
3610 		return PTR_ERR(rng);
3611 	}
3612 
3613 	err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
3614 
3615 	crypto_free_rng(rng);
3616 
3617 	return err;
3618 }
3619 
3620 
3621 static int drbg_cavs_test(const struct drbg_testvec *test, int pr,
3622 			  const char *driver, u32 type, u32 mask)
3623 {
3624 	int ret = -EAGAIN;
3625 	struct crypto_rng *drng;
3626 	struct drbg_test_data test_data;
3627 	struct drbg_string addtl, pers, testentropy;
3628 	unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
3629 
3630 	if (!buf)
3631 		return -ENOMEM;
3632 
3633 	drng = crypto_alloc_rng(driver, type, mask);
3634 	if (IS_ERR(drng)) {
3635 		kfree_sensitive(buf);
3636 		if (PTR_ERR(drng) == -ENOENT)
3637 			return 0;
3638 		printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
3639 		       "%s\n", driver);
3640 		return PTR_ERR(drng);
3641 	}
3642 
3643 	test_data.testentropy = &testentropy;
3644 	drbg_string_fill(&testentropy, test->entropy, test->entropylen);
3645 	drbg_string_fill(&pers, test->pers, test->perslen);
3646 	ret = crypto_drbg_reset_test(drng, &pers, &test_data);
3647 	if (ret) {
3648 		printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
3649 		goto outbuf;
3650 	}
3651 
3652 	drbg_string_fill(&addtl, test->addtla, test->addtllen);
3653 	if (pr) {
3654 		drbg_string_fill(&testentropy, test->entpra, test->entprlen);
3655 		ret = crypto_drbg_get_bytes_addtl_test(drng,
3656 			buf, test->expectedlen, &addtl,	&test_data);
3657 	} else {
3658 		ret = crypto_drbg_get_bytes_addtl(drng,
3659 			buf, test->expectedlen, &addtl);
3660 	}
3661 	if (ret < 0) {
3662 		printk(KERN_ERR "alg: drbg: could not obtain random data for "
3663 		       "driver %s\n", driver);
3664 		goto outbuf;
3665 	}
3666 
3667 	drbg_string_fill(&addtl, test->addtlb, test->addtllen);
3668 	if (pr) {
3669 		drbg_string_fill(&testentropy, test->entprb, test->entprlen);
3670 		ret = crypto_drbg_get_bytes_addtl_test(drng,
3671 			buf, test->expectedlen, &addtl, &test_data);
3672 	} else {
3673 		ret = crypto_drbg_get_bytes_addtl(drng,
3674 			buf, test->expectedlen, &addtl);
3675 	}
3676 	if (ret < 0) {
3677 		printk(KERN_ERR "alg: drbg: could not obtain random data for "
3678 		       "driver %s\n", driver);
3679 		goto outbuf;
3680 	}
3681 
3682 	ret = memcmp(test->expected, buf, test->expectedlen);
3683 
3684 outbuf:
3685 	crypto_free_rng(drng);
3686 	kfree_sensitive(buf);
3687 	return ret;
3688 }
3689 
3690 
3691 static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
3692 			 u32 type, u32 mask)
3693 {
3694 	int err = 0;
3695 	int pr = 0;
3696 	int i = 0;
3697 	const struct drbg_testvec *template = desc->suite.drbg.vecs;
3698 	unsigned int tcount = desc->suite.drbg.count;
3699 
3700 	if (0 == memcmp(driver, "drbg_pr_", 8))
3701 		pr = 1;
3702 
3703 	for (i = 0; i < tcount; i++) {
3704 		err = drbg_cavs_test(&template[i], pr, driver, type, mask);
3705 		if (err) {
3706 			printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
3707 			       i, driver);
3708 			err = -EINVAL;
3709 			break;
3710 		}
3711 	}
3712 	return err;
3713 
3714 }
3715 
3716 static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
3717 		       const char *alg)
3718 {
3719 	struct kpp_request *req;
3720 	void *input_buf = NULL;
3721 	void *output_buf = NULL;
3722 	void *a_public = NULL;
3723 	void *a_ss = NULL;
3724 	void *shared_secret = NULL;
3725 	struct crypto_wait wait;
3726 	unsigned int out_len_max;
3727 	int err = -ENOMEM;
3728 	struct scatterlist src, dst;
3729 
3730 	req = kpp_request_alloc(tfm, GFP_KERNEL);
3731 	if (!req)
3732 		return err;
3733 
3734 	crypto_init_wait(&wait);
3735 
3736 	err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
3737 	if (err < 0)
3738 		goto free_req;
3739 
3740 	out_len_max = crypto_kpp_maxsize(tfm);
3741 	output_buf = kzalloc(out_len_max, GFP_KERNEL);
3742 	if (!output_buf) {
3743 		err = -ENOMEM;
3744 		goto free_req;
3745 	}
3746 
3747 	/* Use appropriate parameter as base */
3748 	kpp_request_set_input(req, NULL, 0);
3749 	sg_init_one(&dst, output_buf, out_len_max);
3750 	kpp_request_set_output(req, &dst, out_len_max);
3751 	kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3752 				 crypto_req_done, &wait);
3753 
3754 	/* Compute party A's public key */
3755 	err = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
3756 	if (err) {
3757 		pr_err("alg: %s: Party A: generate public key test failed. err %d\n",
3758 		       alg, err);
3759 		goto free_output;
3760 	}
3761 
3762 	if (vec->genkey) {
3763 		/* Save party A's public key */
3764 		a_public = kmemdup(sg_virt(req->dst), out_len_max, GFP_KERNEL);
3765 		if (!a_public) {
3766 			err = -ENOMEM;
3767 			goto free_output;
3768 		}
3769 	} else {
3770 		/* Verify calculated public key */
3771 		if (memcmp(vec->expected_a_public, sg_virt(req->dst),
3772 			   vec->expected_a_public_size)) {
3773 			pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n",
3774 			       alg);
3775 			err = -EINVAL;
3776 			goto free_output;
3777 		}
3778 	}
3779 
3780 	/* Calculate shared secret key by using counter part (b) public key. */
3781 	input_buf = kmemdup(vec->b_public, vec->b_public_size, GFP_KERNEL);
3782 	if (!input_buf) {
3783 		err = -ENOMEM;
3784 		goto free_output;
3785 	}
3786 
3787 	sg_init_one(&src, input_buf, vec->b_public_size);
3788 	sg_init_one(&dst, output_buf, out_len_max);
3789 	kpp_request_set_input(req, &src, vec->b_public_size);
3790 	kpp_request_set_output(req, &dst, out_len_max);
3791 	kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3792 				 crypto_req_done, &wait);
3793 	err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait);
3794 	if (err) {
3795 		pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n",
3796 		       alg, err);
3797 		goto free_all;
3798 	}
3799 
3800 	if (vec->genkey) {
3801 		/* Save the shared secret obtained by party A */
3802 		a_ss = kmemdup(sg_virt(req->dst), vec->expected_ss_size, GFP_KERNEL);
3803 		if (!a_ss) {
3804 			err = -ENOMEM;
3805 			goto free_all;
3806 		}
3807 
3808 		/*
3809 		 * Calculate party B's shared secret by using party A's
3810 		 * public key.
3811 		 */
3812 		err = crypto_kpp_set_secret(tfm, vec->b_secret,
3813 					    vec->b_secret_size);
3814 		if (err < 0)
3815 			goto free_all;
3816 
3817 		sg_init_one(&src, a_public, vec->expected_a_public_size);
3818 		sg_init_one(&dst, output_buf, out_len_max);
3819 		kpp_request_set_input(req, &src, vec->expected_a_public_size);
3820 		kpp_request_set_output(req, &dst, out_len_max);
3821 		kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3822 					 crypto_req_done, &wait);
3823 		err = crypto_wait_req(crypto_kpp_compute_shared_secret(req),
3824 				      &wait);
3825 		if (err) {
3826 			pr_err("alg: %s: Party B: compute shared secret failed. err %d\n",
3827 			       alg, err);
3828 			goto free_all;
3829 		}
3830 
3831 		shared_secret = a_ss;
3832 	} else {
3833 		shared_secret = (void *)vec->expected_ss;
3834 	}
3835 
3836 	/*
3837 	 * verify shared secret from which the user will derive
3838 	 * secret key by executing whatever hash it has chosen
3839 	 */
3840 	if (memcmp(shared_secret, sg_virt(req->dst),
3841 		   vec->expected_ss_size)) {
3842 		pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
3843 		       alg);
3844 		err = -EINVAL;
3845 	}
3846 
3847 free_all:
3848 	kfree(a_ss);
3849 	kfree(input_buf);
3850 free_output:
3851 	kfree(a_public);
3852 	kfree(output_buf);
3853 free_req:
3854 	kpp_request_free(req);
3855 	return err;
3856 }
3857 
3858 static int test_kpp(struct crypto_kpp *tfm, const char *alg,
3859 		    const struct kpp_testvec *vecs, unsigned int tcount)
3860 {
3861 	int ret, i;
3862 
3863 	for (i = 0; i < tcount; i++) {
3864 		ret = do_test_kpp(tfm, vecs++, alg);
3865 		if (ret) {
3866 			pr_err("alg: %s: test failed on vector %d, err=%d\n",
3867 			       alg, i + 1, ret);
3868 			return ret;
3869 		}
3870 	}
3871 	return 0;
3872 }
3873 
3874 static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
3875 			u32 type, u32 mask)
3876 {
3877 	struct crypto_kpp *tfm;
3878 	int err = 0;
3879 
3880 	tfm = crypto_alloc_kpp(driver, type, mask);
3881 	if (IS_ERR(tfm)) {
3882 		if (PTR_ERR(tfm) == -ENOENT)
3883 			return 0;
3884 		pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
3885 		       driver, PTR_ERR(tfm));
3886 		return PTR_ERR(tfm);
3887 	}
3888 	if (desc->suite.kpp.vecs)
3889 		err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
3890 			       desc->suite.kpp.count);
3891 
3892 	crypto_free_kpp(tfm);
3893 	return err;
3894 }
3895 
3896 static u8 *test_pack_u32(u8 *dst, u32 val)
3897 {
3898 	memcpy(dst, &val, sizeof(val));
3899 	return dst + sizeof(val);
3900 }
3901 
3902 static int test_akcipher_one(struct crypto_akcipher *tfm,
3903 			     const struct akcipher_testvec *vecs)
3904 {
3905 	char *xbuf[XBUFSIZE];
3906 	struct akcipher_request *req;
3907 	void *outbuf_enc = NULL;
3908 	void *outbuf_dec = NULL;
3909 	struct crypto_wait wait;
3910 	unsigned int out_len_max, out_len = 0;
3911 	int err = -ENOMEM;
3912 	struct scatterlist src, dst, src_tab[2];
3913 	const char *c;
3914 	unsigned int c_size;
3915 
3916 	if (testmgr_alloc_buf(xbuf))
3917 		return err;
3918 
3919 	req = akcipher_request_alloc(tfm, GFP_KERNEL);
3920 	if (!req)
3921 		goto free_xbuf;
3922 
3923 	crypto_init_wait(&wait);
3924 
3925 	if (vecs->public_key_vec)
3926 		err = crypto_akcipher_set_pub_key(tfm, vecs->key,
3927 						  vecs->key_len);
3928 	else
3929 		err = crypto_akcipher_set_priv_key(tfm, vecs->key,
3930 						   vecs->key_len);
3931 	if (err)
3932 		goto free_req;
3933 
3934 	/* First run encrypt test which does not require a private key */
3935 	err = -ENOMEM;
3936 	out_len_max = crypto_akcipher_maxsize(tfm);
3937 	outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
3938 	if (!outbuf_enc)
3939 		goto free_req;
3940 
3941 	c = vecs->c;
3942 	c_size = vecs->c_size;
3943 
3944 	err = -E2BIG;
3945 	if (WARN_ON(vecs->m_size > PAGE_SIZE))
3946 		goto free_all;
3947 	memcpy(xbuf[0], vecs->m, vecs->m_size);
3948 
3949 	sg_init_table(src_tab, 2);
3950 	sg_set_buf(&src_tab[0], xbuf[0], 8);
3951 	sg_set_buf(&src_tab[1], xbuf[0] + 8, vecs->m_size - 8);
3952 	sg_init_one(&dst, outbuf_enc, out_len_max);
3953 	akcipher_request_set_crypt(req, src_tab, &dst, vecs->m_size,
3954 				   out_len_max);
3955 	akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3956 				      crypto_req_done, &wait);
3957 
3958 	err = crypto_wait_req(crypto_akcipher_encrypt(req), &wait);
3959 	if (err) {
3960 		pr_err("alg: akcipher: encrypt test failed. err %d\n", err);
3961 		goto free_all;
3962 	}
3963 	if (c) {
3964 		if (req->dst_len != c_size) {
3965 			pr_err("alg: akcipher: encrypt test failed. Invalid output len\n");
3966 			err = -EINVAL;
3967 			goto free_all;
3968 		}
3969 		/* verify that encrypted message is equal to expected */
3970 		if (memcmp(c, outbuf_enc, c_size) != 0) {
3971 			pr_err("alg: akcipher: encrypt test failed. Invalid output\n");
3972 			hexdump(outbuf_enc, c_size);
3973 			err = -EINVAL;
3974 			goto free_all;
3975 		}
3976 	}
3977 
3978 	/*
3979 	 * Don't invoke decrypt test which requires a private key
3980 	 * for vectors with only a public key.
3981 	 */
3982 	if (vecs->public_key_vec) {
3983 		err = 0;
3984 		goto free_all;
3985 	}
3986 	outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
3987 	if (!outbuf_dec) {
3988 		err = -ENOMEM;
3989 		goto free_all;
3990 	}
3991 
3992 	if (!c) {
3993 		c = outbuf_enc;
3994 		c_size = req->dst_len;
3995 	}
3996 
3997 	err = -E2BIG;
3998 	if (WARN_ON(c_size > PAGE_SIZE))
3999 		goto free_all;
4000 	memcpy(xbuf[0], c, c_size);
4001 
4002 	sg_init_one(&src, xbuf[0], c_size);
4003 	sg_init_one(&dst, outbuf_dec, out_len_max);
4004 	crypto_init_wait(&wait);
4005 	akcipher_request_set_crypt(req, &src, &dst, c_size, out_len_max);
4006 
4007 	err = crypto_wait_req(crypto_akcipher_decrypt(req), &wait);
4008 	if (err) {
4009 		pr_err("alg: akcipher: decrypt test failed. err %d\n", err);
4010 		goto free_all;
4011 	}
4012 	out_len = req->dst_len;
4013 	if (out_len < vecs->m_size) {
4014 		pr_err("alg: akcipher: decrypt test failed. Invalid output len %u\n",
4015 		       out_len);
4016 		err = -EINVAL;
4017 		goto free_all;
4018 	}
4019 	/* verify that decrypted message is equal to the original msg */
4020 	if (memchr_inv(outbuf_dec, 0, out_len - vecs->m_size) ||
4021 	    memcmp(vecs->m, outbuf_dec + out_len - vecs->m_size,
4022 		   vecs->m_size)) {
4023 		pr_err("alg: akcipher: decrypt test failed. Invalid output\n");
4024 		hexdump(outbuf_dec, out_len);
4025 		err = -EINVAL;
4026 	}
4027 free_all:
4028 	kfree(outbuf_dec);
4029 	kfree(outbuf_enc);
4030 free_req:
4031 	akcipher_request_free(req);
4032 free_xbuf:
4033 	testmgr_free_buf(xbuf);
4034 	return err;
4035 }
4036 
4037 static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
4038 			 const struct akcipher_testvec *vecs,
4039 			 unsigned int tcount)
4040 {
4041 	const char *algo =
4042 		crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
4043 	int ret, i;
4044 
4045 	for (i = 0; i < tcount; i++) {
4046 		ret = test_akcipher_one(tfm, vecs++);
4047 		if (!ret)
4048 			continue;
4049 
4050 		pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
4051 		       i + 1, algo, ret);
4052 		return ret;
4053 	}
4054 	return 0;
4055 }
4056 
4057 static int alg_test_akcipher(const struct alg_test_desc *desc,
4058 			     const char *driver, u32 type, u32 mask)
4059 {
4060 	struct crypto_akcipher *tfm;
4061 	int err = 0;
4062 
4063 	tfm = crypto_alloc_akcipher(driver, type, mask);
4064 	if (IS_ERR(tfm)) {
4065 		if (PTR_ERR(tfm) == -ENOENT)
4066 			return 0;
4067 		pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
4068 		       driver, PTR_ERR(tfm));
4069 		return PTR_ERR(tfm);
4070 	}
4071 	if (desc->suite.akcipher.vecs)
4072 		err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
4073 				    desc->suite.akcipher.count);
4074 
4075 	crypto_free_akcipher(tfm);
4076 	return err;
4077 }
4078 
4079 static int test_sig_one(struct crypto_sig *tfm, const struct sig_testvec *vecs)
4080 {
4081 	u8 *ptr, *key __free(kfree);
4082 	int err, sig_size;
4083 
4084 	key = kmalloc(vecs->key_len + 2 * sizeof(u32) + vecs->param_len,
4085 		      GFP_KERNEL);
4086 	if (!key)
4087 		return -ENOMEM;
4088 
4089 	/* ecrdsa expects additional parameters appended to the key */
4090 	memcpy(key, vecs->key, vecs->key_len);
4091 	ptr = key + vecs->key_len;
4092 	ptr = test_pack_u32(ptr, vecs->algo);
4093 	ptr = test_pack_u32(ptr, vecs->param_len);
4094 	memcpy(ptr, vecs->params, vecs->param_len);
4095 
4096 	if (vecs->public_key_vec)
4097 		err = crypto_sig_set_pubkey(tfm, key, vecs->key_len);
4098 	else
4099 		err = crypto_sig_set_privkey(tfm, key, vecs->key_len);
4100 	if (err)
4101 		return err;
4102 
4103 	/*
4104 	 * Run asymmetric signature verification first
4105 	 * (which does not require a private key)
4106 	 */
4107 	err = crypto_sig_verify(tfm, vecs->c, vecs->c_size,
4108 				vecs->m, vecs->m_size);
4109 	if (err) {
4110 		pr_err("alg: sig: verify test failed: err %d\n", err);
4111 		return err;
4112 	}
4113 
4114 	/*
4115 	 * Don't invoke sign test (which requires a private key)
4116 	 * for vectors with only a public key.
4117 	 */
4118 	if (vecs->public_key_vec)
4119 		return 0;
4120 
4121 	sig_size = crypto_sig_maxsize(tfm);
4122 	if (sig_size < vecs->c_size) {
4123 		pr_err("alg: sig: invalid maxsize %u\n", sig_size);
4124 		return -EINVAL;
4125 	}
4126 
4127 	u8 *sig __free(kfree) = kzalloc(sig_size, GFP_KERNEL);
4128 	if (!sig)
4129 		return -ENOMEM;
4130 
4131 	/* Run asymmetric signature generation */
4132 	err = crypto_sig_sign(tfm, vecs->m, vecs->m_size, sig, sig_size);
4133 	if (err < 0) {
4134 		pr_err("alg: sig: sign test failed: err %d\n", err);
4135 		return err;
4136 	}
4137 
4138 	/* Verify that generated signature equals cooked signature */
4139 	if (err != vecs->c_size ||
4140 	    memcmp(sig, vecs->c, vecs->c_size) ||
4141 	    memchr_inv(sig + vecs->c_size, 0, sig_size - vecs->c_size)) {
4142 		pr_err("alg: sig: sign test failed: invalid output\n");
4143 		hexdump(sig, sig_size);
4144 		return -EINVAL;
4145 	}
4146 
4147 	return 0;
4148 }
4149 
4150 static int test_sig(struct crypto_sig *tfm, const char *alg,
4151 		    const struct sig_testvec *vecs, unsigned int tcount)
4152 {
4153 	const char *algo = crypto_tfm_alg_driver_name(crypto_sig_tfm(tfm));
4154 	int ret, i;
4155 
4156 	for (i = 0; i < tcount; i++) {
4157 		ret = test_sig_one(tfm, vecs++);
4158 		if (ret) {
4159 			pr_err("alg: sig: test %d failed for %s: err %d\n",
4160 			       i + 1, algo, ret);
4161 			return ret;
4162 		}
4163 	}
4164 	return 0;
4165 }
4166 
4167 static int alg_test_sig(const struct alg_test_desc *desc, const char *driver,
4168 			u32 type, u32 mask)
4169 {
4170 	struct crypto_sig *tfm;
4171 	int err = 0;
4172 
4173 	tfm = crypto_alloc_sig(driver, type, mask);
4174 	if (IS_ERR(tfm)) {
4175 		pr_err("alg: sig: Failed to load tfm for %s: %ld\n",
4176 		       driver, PTR_ERR(tfm));
4177 		return PTR_ERR(tfm);
4178 	}
4179 	if (desc->suite.sig.vecs)
4180 		err = test_sig(tfm, desc->alg, desc->suite.sig.vecs,
4181 			       desc->suite.sig.count);
4182 
4183 	crypto_free_sig(tfm);
4184 	return err;
4185 }
4186 
4187 static int alg_test_null(const struct alg_test_desc *desc,
4188 			     const char *driver, u32 type, u32 mask)
4189 {
4190 	return 0;
4191 }
4192 
4193 #define ____VECS(tv)	.vecs = tv, .count = ARRAY_SIZE(tv)
4194 #define __VECS(tv)	{ ____VECS(tv) }
4195 
4196 /* Please keep this list sorted by algorithm name. */
4197 static const struct alg_test_desc alg_test_descs[] = {
4198 	{
4199 		.alg = "adiantum(xchacha12,aes)",
4200 		.generic_driver = "adiantum(xchacha12-generic,aes-generic,nhpoly1305-generic)",
4201 		.test = alg_test_skcipher,
4202 		.suite = {
4203 			.cipher = __VECS(adiantum_xchacha12_aes_tv_template)
4204 		},
4205 	}, {
4206 		.alg = "adiantum(xchacha20,aes)",
4207 		.generic_driver = "adiantum(xchacha20-generic,aes-generic,nhpoly1305-generic)",
4208 		.test = alg_test_skcipher,
4209 		.suite = {
4210 			.cipher = __VECS(adiantum_xchacha20_aes_tv_template)
4211 		},
4212 	}, {
4213 		.alg = "aegis128",
4214 		.test = alg_test_aead,
4215 		.suite = {
4216 			.aead = __VECS(aegis128_tv_template)
4217 		}
4218 	}, {
4219 		.alg = "ansi_cprng",
4220 		.test = alg_test_cprng,
4221 		.suite = {
4222 			.cprng = __VECS(ansi_cprng_aes_tv_template)
4223 		}
4224 	}, {
4225 		.alg = "authenc(hmac(md5),ecb(cipher_null))",
4226 		.test = alg_test_aead,
4227 		.suite = {
4228 			.aead = __VECS(hmac_md5_ecb_cipher_null_tv_template)
4229 		}
4230 	}, {
4231 		.alg = "authenc(hmac(sha1),cbc(aes))",
4232 		.test = alg_test_aead,
4233 		.fips_allowed = 1,
4234 		.suite = {
4235 			.aead = __VECS(hmac_sha1_aes_cbc_tv_temp)
4236 		}
4237 	}, {
4238 		.alg = "authenc(hmac(sha1),cbc(des))",
4239 		.test = alg_test_aead,
4240 		.suite = {
4241 			.aead = __VECS(hmac_sha1_des_cbc_tv_temp)
4242 		}
4243 	}, {
4244 		.alg = "authenc(hmac(sha1),cbc(des3_ede))",
4245 		.test = alg_test_aead,
4246 		.suite = {
4247 			.aead = __VECS(hmac_sha1_des3_ede_cbc_tv_temp)
4248 		}
4249 	}, {
4250 		.alg = "authenc(hmac(sha1),ctr(aes))",
4251 		.test = alg_test_null,
4252 		.fips_allowed = 1,
4253 	}, {
4254 		.alg = "authenc(hmac(sha1),ecb(cipher_null))",
4255 		.test = alg_test_aead,
4256 		.suite = {
4257 			.aead = __VECS(hmac_sha1_ecb_cipher_null_tv_temp)
4258 		}
4259 	}, {
4260 		.alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
4261 		.test = alg_test_null,
4262 		.fips_allowed = 1,
4263 	}, {
4264 		.alg = "authenc(hmac(sha224),cbc(des))",
4265 		.test = alg_test_aead,
4266 		.suite = {
4267 			.aead = __VECS(hmac_sha224_des_cbc_tv_temp)
4268 		}
4269 	}, {
4270 		.alg = "authenc(hmac(sha224),cbc(des3_ede))",
4271 		.test = alg_test_aead,
4272 		.suite = {
4273 			.aead = __VECS(hmac_sha224_des3_ede_cbc_tv_temp)
4274 		}
4275 	}, {
4276 		.alg = "authenc(hmac(sha256),cbc(aes))",
4277 		.test = alg_test_aead,
4278 		.fips_allowed = 1,
4279 		.suite = {
4280 			.aead = __VECS(hmac_sha256_aes_cbc_tv_temp)
4281 		}
4282 	}, {
4283 		.alg = "authenc(hmac(sha256),cbc(des))",
4284 		.test = alg_test_aead,
4285 		.suite = {
4286 			.aead = __VECS(hmac_sha256_des_cbc_tv_temp)
4287 		}
4288 	}, {
4289 		.alg = "authenc(hmac(sha256),cbc(des3_ede))",
4290 		.test = alg_test_aead,
4291 		.suite = {
4292 			.aead = __VECS(hmac_sha256_des3_ede_cbc_tv_temp)
4293 		}
4294 	}, {
4295 		.alg = "authenc(hmac(sha256),ctr(aes))",
4296 		.test = alg_test_null,
4297 		.fips_allowed = 1,
4298 	}, {
4299 		.alg = "authenc(hmac(sha256),cts(cbc(aes)))",
4300 		.test = alg_test_aead,
4301 		.suite = {
4302 			.aead = __VECS(krb5_test_aes128_cts_hmac_sha256_128)
4303 		}
4304 	}, {
4305 		.alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
4306 		.test = alg_test_null,
4307 		.fips_allowed = 1,
4308 	}, {
4309 		.alg = "authenc(hmac(sha384),cbc(des))",
4310 		.test = alg_test_aead,
4311 		.suite = {
4312 			.aead = __VECS(hmac_sha384_des_cbc_tv_temp)
4313 		}
4314 	}, {
4315 		.alg = "authenc(hmac(sha384),cbc(des3_ede))",
4316 		.test = alg_test_aead,
4317 		.suite = {
4318 			.aead = __VECS(hmac_sha384_des3_ede_cbc_tv_temp)
4319 		}
4320 	}, {
4321 		.alg = "authenc(hmac(sha384),ctr(aes))",
4322 		.test = alg_test_null,
4323 		.fips_allowed = 1,
4324 	}, {
4325 		.alg = "authenc(hmac(sha384),cts(cbc(aes)))",
4326 		.test = alg_test_aead,
4327 		.suite = {
4328 			.aead = __VECS(krb5_test_aes256_cts_hmac_sha384_192)
4329 		}
4330 	}, {
4331 		.alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
4332 		.test = alg_test_null,
4333 		.fips_allowed = 1,
4334 	}, {
4335 		.alg = "authenc(hmac(sha512),cbc(aes))",
4336 		.fips_allowed = 1,
4337 		.test = alg_test_aead,
4338 		.suite = {
4339 			.aead = __VECS(hmac_sha512_aes_cbc_tv_temp)
4340 		}
4341 	}, {
4342 		.alg = "authenc(hmac(sha512),cbc(des))",
4343 		.test = alg_test_aead,
4344 		.suite = {
4345 			.aead = __VECS(hmac_sha512_des_cbc_tv_temp)
4346 		}
4347 	}, {
4348 		.alg = "authenc(hmac(sha512),cbc(des3_ede))",
4349 		.test = alg_test_aead,
4350 		.suite = {
4351 			.aead = __VECS(hmac_sha512_des3_ede_cbc_tv_temp)
4352 		}
4353 	}, {
4354 		.alg = "authenc(hmac(sha512),ctr(aes))",
4355 		.test = alg_test_null,
4356 		.fips_allowed = 1,
4357 	}, {
4358 		.alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
4359 		.test = alg_test_null,
4360 		.fips_allowed = 1,
4361 	}, {
4362 		.alg = "blake2b-160",
4363 		.test = alg_test_hash,
4364 		.fips_allowed = 0,
4365 		.suite = {
4366 			.hash = __VECS(blake2b_160_tv_template)
4367 		}
4368 	}, {
4369 		.alg = "blake2b-256",
4370 		.test = alg_test_hash,
4371 		.fips_allowed = 0,
4372 		.suite = {
4373 			.hash = __VECS(blake2b_256_tv_template)
4374 		}
4375 	}, {
4376 		.alg = "blake2b-384",
4377 		.test = alg_test_hash,
4378 		.fips_allowed = 0,
4379 		.suite = {
4380 			.hash = __VECS(blake2b_384_tv_template)
4381 		}
4382 	}, {
4383 		.alg = "blake2b-512",
4384 		.test = alg_test_hash,
4385 		.fips_allowed = 0,
4386 		.suite = {
4387 			.hash = __VECS(blake2b_512_tv_template)
4388 		}
4389 	}, {
4390 		.alg = "cbc(aes)",
4391 		.test = alg_test_skcipher,
4392 		.fips_allowed = 1,
4393 		.suite = {
4394 			.cipher = __VECS(aes_cbc_tv_template)
4395 		},
4396 	}, {
4397 		.alg = "cbc(anubis)",
4398 		.test = alg_test_skcipher,
4399 		.suite = {
4400 			.cipher = __VECS(anubis_cbc_tv_template)
4401 		},
4402 	}, {
4403 		.alg = "cbc(aria)",
4404 		.test = alg_test_skcipher,
4405 		.suite = {
4406 			.cipher = __VECS(aria_cbc_tv_template)
4407 		},
4408 	}, {
4409 		.alg = "cbc(blowfish)",
4410 		.test = alg_test_skcipher,
4411 		.suite = {
4412 			.cipher = __VECS(bf_cbc_tv_template)
4413 		},
4414 	}, {
4415 		.alg = "cbc(camellia)",
4416 		.test = alg_test_skcipher,
4417 		.suite = {
4418 			.cipher = __VECS(camellia_cbc_tv_template)
4419 		},
4420 	}, {
4421 		.alg = "cbc(cast5)",
4422 		.test = alg_test_skcipher,
4423 		.suite = {
4424 			.cipher = __VECS(cast5_cbc_tv_template)
4425 		},
4426 	}, {
4427 		.alg = "cbc(cast6)",
4428 		.test = alg_test_skcipher,
4429 		.suite = {
4430 			.cipher = __VECS(cast6_cbc_tv_template)
4431 		},
4432 	}, {
4433 		.alg = "cbc(des)",
4434 		.test = alg_test_skcipher,
4435 		.suite = {
4436 			.cipher = __VECS(des_cbc_tv_template)
4437 		},
4438 	}, {
4439 		.alg = "cbc(des3_ede)",
4440 		.test = alg_test_skcipher,
4441 		.suite = {
4442 			.cipher = __VECS(des3_ede_cbc_tv_template)
4443 		},
4444 	}, {
4445 		/* Same as cbc(aes) except the key is stored in
4446 		 * hardware secure memory which we reference by index
4447 		 */
4448 		.alg = "cbc(paes)",
4449 		.test = alg_test_null,
4450 		.fips_allowed = 1,
4451 	}, {
4452 		/* Same as cbc(sm4) except the key is stored in
4453 		 * hardware secure memory which we reference by index
4454 		 */
4455 		.alg = "cbc(psm4)",
4456 		.test = alg_test_null,
4457 	}, {
4458 		.alg = "cbc(serpent)",
4459 		.test = alg_test_skcipher,
4460 		.suite = {
4461 			.cipher = __VECS(serpent_cbc_tv_template)
4462 		},
4463 	}, {
4464 		.alg = "cbc(sm4)",
4465 		.test = alg_test_skcipher,
4466 		.suite = {
4467 			.cipher = __VECS(sm4_cbc_tv_template)
4468 		}
4469 	}, {
4470 		.alg = "cbc(twofish)",
4471 		.test = alg_test_skcipher,
4472 		.suite = {
4473 			.cipher = __VECS(tf_cbc_tv_template)
4474 		},
4475 	}, {
4476 #if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
4477 		.alg = "cbc-paes-s390",
4478 		.fips_allowed = 1,
4479 		.test = alg_test_skcipher,
4480 		.suite = {
4481 			.cipher = __VECS(aes_cbc_tv_template)
4482 		}
4483 	}, {
4484 #endif
4485 		.alg = "cbcmac(aes)",
4486 		.test = alg_test_hash,
4487 		.suite = {
4488 			.hash = __VECS(aes_cbcmac_tv_template)
4489 		}
4490 	}, {
4491 		.alg = "cbcmac(sm4)",
4492 		.test = alg_test_hash,
4493 		.suite = {
4494 			.hash = __VECS(sm4_cbcmac_tv_template)
4495 		}
4496 	}, {
4497 		.alg = "ccm(aes)",
4498 		.generic_driver = "ccm_base(ctr(aes-generic),cbcmac(aes-generic))",
4499 		.test = alg_test_aead,
4500 		.fips_allowed = 1,
4501 		.suite = {
4502 			.aead = {
4503 				____VECS(aes_ccm_tv_template),
4504 				.einval_allowed = 1,
4505 			}
4506 		}
4507 	}, {
4508 		.alg = "ccm(sm4)",
4509 		.generic_driver = "ccm_base(ctr(sm4-generic),cbcmac(sm4-generic))",
4510 		.test = alg_test_aead,
4511 		.suite = {
4512 			.aead = {
4513 				____VECS(sm4_ccm_tv_template),
4514 				.einval_allowed = 1,
4515 			}
4516 		}
4517 	}, {
4518 		.alg = "chacha20",
4519 		.test = alg_test_skcipher,
4520 		.suite = {
4521 			.cipher = __VECS(chacha20_tv_template)
4522 		},
4523 	}, {
4524 		.alg = "cmac(aes)",
4525 		.fips_allowed = 1,
4526 		.test = alg_test_hash,
4527 		.suite = {
4528 			.hash = __VECS(aes_cmac128_tv_template)
4529 		}
4530 	}, {
4531 		.alg = "cmac(camellia)",
4532 		.test = alg_test_hash,
4533 		.suite = {
4534 			.hash = __VECS(camellia_cmac128_tv_template)
4535 		}
4536 	}, {
4537 		.alg = "cmac(des3_ede)",
4538 		.test = alg_test_hash,
4539 		.suite = {
4540 			.hash = __VECS(des3_ede_cmac64_tv_template)
4541 		}
4542 	}, {
4543 		.alg = "cmac(sm4)",
4544 		.test = alg_test_hash,
4545 		.suite = {
4546 			.hash = __VECS(sm4_cmac128_tv_template)
4547 		}
4548 	}, {
4549 		.alg = "crc32",
4550 		.test = alg_test_hash,
4551 		.fips_allowed = 1,
4552 		.suite = {
4553 			.hash = __VECS(crc32_tv_template)
4554 		}
4555 	}, {
4556 		.alg = "crc32c",
4557 		.test = alg_test_crc32c,
4558 		.fips_allowed = 1,
4559 		.suite = {
4560 			.hash = __VECS(crc32c_tv_template)
4561 		}
4562 	}, {
4563 		.alg = "ctr(aes)",
4564 		.test = alg_test_skcipher,
4565 		.fips_allowed = 1,
4566 		.suite = {
4567 			.cipher = __VECS(aes_ctr_tv_template)
4568 		}
4569 	}, {
4570 		.alg = "ctr(aria)",
4571 		.test = alg_test_skcipher,
4572 		.suite = {
4573 			.cipher = __VECS(aria_ctr_tv_template)
4574 		}
4575 	}, {
4576 		.alg = "ctr(blowfish)",
4577 		.test = alg_test_skcipher,
4578 		.suite = {
4579 			.cipher = __VECS(bf_ctr_tv_template)
4580 		}
4581 	}, {
4582 		.alg = "ctr(camellia)",
4583 		.test = alg_test_skcipher,
4584 		.suite = {
4585 			.cipher = __VECS(camellia_ctr_tv_template)
4586 		}
4587 	}, {
4588 		.alg = "ctr(cast5)",
4589 		.test = alg_test_skcipher,
4590 		.suite = {
4591 			.cipher = __VECS(cast5_ctr_tv_template)
4592 		}
4593 	}, {
4594 		.alg = "ctr(cast6)",
4595 		.test = alg_test_skcipher,
4596 		.suite = {
4597 			.cipher = __VECS(cast6_ctr_tv_template)
4598 		}
4599 	}, {
4600 		.alg = "ctr(des)",
4601 		.test = alg_test_skcipher,
4602 		.suite = {
4603 			.cipher = __VECS(des_ctr_tv_template)
4604 		}
4605 	}, {
4606 		.alg = "ctr(des3_ede)",
4607 		.test = alg_test_skcipher,
4608 		.suite = {
4609 			.cipher = __VECS(des3_ede_ctr_tv_template)
4610 		}
4611 	}, {
4612 		/* Same as ctr(aes) except the key is stored in
4613 		 * hardware secure memory which we reference by index
4614 		 */
4615 		.alg = "ctr(paes)",
4616 		.test = alg_test_null,
4617 		.fips_allowed = 1,
4618 	}, {
4619 
4620 		/* Same as ctr(sm4) except the key is stored in
4621 		 * hardware secure memory which we reference by index
4622 		 */
4623 		.alg = "ctr(psm4)",
4624 		.test = alg_test_null,
4625 	}, {
4626 		.alg = "ctr(serpent)",
4627 		.test = alg_test_skcipher,
4628 		.suite = {
4629 			.cipher = __VECS(serpent_ctr_tv_template)
4630 		}
4631 	}, {
4632 		.alg = "ctr(sm4)",
4633 		.test = alg_test_skcipher,
4634 		.suite = {
4635 			.cipher = __VECS(sm4_ctr_tv_template)
4636 		}
4637 	}, {
4638 		.alg = "ctr(twofish)",
4639 		.test = alg_test_skcipher,
4640 		.suite = {
4641 			.cipher = __VECS(tf_ctr_tv_template)
4642 		}
4643 	}, {
4644 #if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
4645 		.alg = "ctr-paes-s390",
4646 		.fips_allowed = 1,
4647 		.test = alg_test_skcipher,
4648 		.suite = {
4649 			.cipher = __VECS(aes_ctr_tv_template)
4650 		}
4651 	}, {
4652 #endif
4653 		.alg = "cts(cbc(aes))",
4654 		.test = alg_test_skcipher,
4655 		.fips_allowed = 1,
4656 		.suite = {
4657 			.cipher = __VECS(cts_mode_tv_template)
4658 		}
4659 	}, {
4660 		/* Same as cts(cbc((aes)) except the key is stored in
4661 		 * hardware secure memory which we reference by index
4662 		 */
4663 		.alg = "cts(cbc(paes))",
4664 		.test = alg_test_null,
4665 		.fips_allowed = 1,
4666 	}, {
4667 		.alg = "cts(cbc(sm4))",
4668 		.test = alg_test_skcipher,
4669 		.suite = {
4670 			.cipher = __VECS(sm4_cts_tv_template)
4671 		}
4672 	}, {
4673 		.alg = "curve25519",
4674 		.test = alg_test_kpp,
4675 		.suite = {
4676 			.kpp = __VECS(curve25519_tv_template)
4677 		}
4678 	}, {
4679 		.alg = "deflate",
4680 		.test = alg_test_comp,
4681 		.fips_allowed = 1,
4682 		.suite = {
4683 			.comp = {
4684 				.comp = __VECS(deflate_comp_tv_template),
4685 				.decomp = __VECS(deflate_decomp_tv_template)
4686 			}
4687 		}
4688 	}, {
4689 		.alg = "deflate-iaa",
4690 		.test = alg_test_comp,
4691 		.fips_allowed = 1,
4692 		.suite = {
4693 			.comp = {
4694 				.comp = __VECS(deflate_comp_tv_template),
4695 				.decomp = __VECS(deflate_decomp_tv_template)
4696 			}
4697 		}
4698 	}, {
4699 		.alg = "dh",
4700 		.test = alg_test_kpp,
4701 		.suite = {
4702 			.kpp = __VECS(dh_tv_template)
4703 		}
4704 	}, {
4705 		.alg = "digest_null",
4706 		.test = alg_test_null,
4707 	}, {
4708 		.alg = "drbg_nopr_ctr_aes128",
4709 		.test = alg_test_drbg,
4710 		.fips_allowed = 1,
4711 		.suite = {
4712 			.drbg = __VECS(drbg_nopr_ctr_aes128_tv_template)
4713 		}
4714 	}, {
4715 		.alg = "drbg_nopr_ctr_aes192",
4716 		.test = alg_test_drbg,
4717 		.fips_allowed = 1,
4718 		.suite = {
4719 			.drbg = __VECS(drbg_nopr_ctr_aes192_tv_template)
4720 		}
4721 	}, {
4722 		.alg = "drbg_nopr_ctr_aes256",
4723 		.test = alg_test_drbg,
4724 		.fips_allowed = 1,
4725 		.suite = {
4726 			.drbg = __VECS(drbg_nopr_ctr_aes256_tv_template)
4727 		}
4728 	}, {
4729 		.alg = "drbg_nopr_hmac_sha256",
4730 		.test = alg_test_drbg,
4731 		.fips_allowed = 1,
4732 		.suite = {
4733 			.drbg = __VECS(drbg_nopr_hmac_sha256_tv_template)
4734 		}
4735 	}, {
4736 		/*
4737 		 * There is no need to specifically test the DRBG with every
4738 		 * backend cipher -- covered by drbg_nopr_hmac_sha512 test
4739 		 */
4740 		.alg = "drbg_nopr_hmac_sha384",
4741 		.test = alg_test_null,
4742 	}, {
4743 		.alg = "drbg_nopr_hmac_sha512",
4744 		.test = alg_test_drbg,
4745 		.fips_allowed = 1,
4746 		.suite = {
4747 			.drbg = __VECS(drbg_nopr_hmac_sha512_tv_template)
4748 		}
4749 	}, {
4750 		.alg = "drbg_nopr_sha256",
4751 		.test = alg_test_drbg,
4752 		.fips_allowed = 1,
4753 		.suite = {
4754 			.drbg = __VECS(drbg_nopr_sha256_tv_template)
4755 		}
4756 	}, {
4757 		/* covered by drbg_nopr_sha256 test */
4758 		.alg = "drbg_nopr_sha384",
4759 		.test = alg_test_null,
4760 	}, {
4761 		.alg = "drbg_nopr_sha512",
4762 		.fips_allowed = 1,
4763 		.test = alg_test_null,
4764 	}, {
4765 		.alg = "drbg_pr_ctr_aes128",
4766 		.test = alg_test_drbg,
4767 		.fips_allowed = 1,
4768 		.suite = {
4769 			.drbg = __VECS(drbg_pr_ctr_aes128_tv_template)
4770 		}
4771 	}, {
4772 		/* covered by drbg_pr_ctr_aes128 test */
4773 		.alg = "drbg_pr_ctr_aes192",
4774 		.fips_allowed = 1,
4775 		.test = alg_test_null,
4776 	}, {
4777 		.alg = "drbg_pr_ctr_aes256",
4778 		.fips_allowed = 1,
4779 		.test = alg_test_null,
4780 	}, {
4781 		.alg = "drbg_pr_hmac_sha256",
4782 		.test = alg_test_drbg,
4783 		.fips_allowed = 1,
4784 		.suite = {
4785 			.drbg = __VECS(drbg_pr_hmac_sha256_tv_template)
4786 		}
4787 	}, {
4788 		/* covered by drbg_pr_hmac_sha256 test */
4789 		.alg = "drbg_pr_hmac_sha384",
4790 		.test = alg_test_null,
4791 	}, {
4792 		.alg = "drbg_pr_hmac_sha512",
4793 		.test = alg_test_null,
4794 		.fips_allowed = 1,
4795 	}, {
4796 		.alg = "drbg_pr_sha256",
4797 		.test = alg_test_drbg,
4798 		.fips_allowed = 1,
4799 		.suite = {
4800 			.drbg = __VECS(drbg_pr_sha256_tv_template)
4801 		}
4802 	}, {
4803 		/* covered by drbg_pr_sha256 test */
4804 		.alg = "drbg_pr_sha384",
4805 		.test = alg_test_null,
4806 	}, {
4807 		.alg = "drbg_pr_sha512",
4808 		.fips_allowed = 1,
4809 		.test = alg_test_null,
4810 	}, {
4811 		.alg = "ecb(aes)",
4812 		.test = alg_test_skcipher,
4813 		.fips_allowed = 1,
4814 		.suite = {
4815 			.cipher = __VECS(aes_tv_template)
4816 		}
4817 	}, {
4818 		.alg = "ecb(anubis)",
4819 		.test = alg_test_skcipher,
4820 		.suite = {
4821 			.cipher = __VECS(anubis_tv_template)
4822 		}
4823 	}, {
4824 		.alg = "ecb(arc4)",
4825 		.generic_driver = "arc4-generic",
4826 		.test = alg_test_skcipher,
4827 		.suite = {
4828 			.cipher = __VECS(arc4_tv_template)
4829 		}
4830 	}, {
4831 		.alg = "ecb(aria)",
4832 		.test = alg_test_skcipher,
4833 		.suite = {
4834 			.cipher = __VECS(aria_tv_template)
4835 		}
4836 	}, {
4837 		.alg = "ecb(blowfish)",
4838 		.test = alg_test_skcipher,
4839 		.suite = {
4840 			.cipher = __VECS(bf_tv_template)
4841 		}
4842 	}, {
4843 		.alg = "ecb(camellia)",
4844 		.test = alg_test_skcipher,
4845 		.suite = {
4846 			.cipher = __VECS(camellia_tv_template)
4847 		}
4848 	}, {
4849 		.alg = "ecb(cast5)",
4850 		.test = alg_test_skcipher,
4851 		.suite = {
4852 			.cipher = __VECS(cast5_tv_template)
4853 		}
4854 	}, {
4855 		.alg = "ecb(cast6)",
4856 		.test = alg_test_skcipher,
4857 		.suite = {
4858 			.cipher = __VECS(cast6_tv_template)
4859 		}
4860 	}, {
4861 		.alg = "ecb(cipher_null)",
4862 		.test = alg_test_null,
4863 		.fips_allowed = 1,
4864 	}, {
4865 		.alg = "ecb(des)",
4866 		.test = alg_test_skcipher,
4867 		.suite = {
4868 			.cipher = __VECS(des_tv_template)
4869 		}
4870 	}, {
4871 		.alg = "ecb(des3_ede)",
4872 		.test = alg_test_skcipher,
4873 		.suite = {
4874 			.cipher = __VECS(des3_ede_tv_template)
4875 		}
4876 	}, {
4877 		.alg = "ecb(fcrypt)",
4878 		.test = alg_test_skcipher,
4879 		.suite = {
4880 			.cipher = {
4881 				.vecs = fcrypt_pcbc_tv_template,
4882 				.count = 1
4883 			}
4884 		}
4885 	}, {
4886 		.alg = "ecb(khazad)",
4887 		.test = alg_test_skcipher,
4888 		.suite = {
4889 			.cipher = __VECS(khazad_tv_template)
4890 		}
4891 	}, {
4892 		/* Same as ecb(aes) except the key is stored in
4893 		 * hardware secure memory which we reference by index
4894 		 */
4895 		.alg = "ecb(paes)",
4896 		.test = alg_test_null,
4897 		.fips_allowed = 1,
4898 	}, {
4899 		.alg = "ecb(seed)",
4900 		.test = alg_test_skcipher,
4901 		.suite = {
4902 			.cipher = __VECS(seed_tv_template)
4903 		}
4904 	}, {
4905 		.alg = "ecb(serpent)",
4906 		.test = alg_test_skcipher,
4907 		.suite = {
4908 			.cipher = __VECS(serpent_tv_template)
4909 		}
4910 	}, {
4911 		.alg = "ecb(sm4)",
4912 		.test = alg_test_skcipher,
4913 		.suite = {
4914 			.cipher = __VECS(sm4_tv_template)
4915 		}
4916 	}, {
4917 		.alg = "ecb(tea)",
4918 		.test = alg_test_skcipher,
4919 		.suite = {
4920 			.cipher = __VECS(tea_tv_template)
4921 		}
4922 	}, {
4923 		.alg = "ecb(twofish)",
4924 		.test = alg_test_skcipher,
4925 		.suite = {
4926 			.cipher = __VECS(tf_tv_template)
4927 		}
4928 	}, {
4929 		.alg = "ecb(xeta)",
4930 		.test = alg_test_skcipher,
4931 		.suite = {
4932 			.cipher = __VECS(xeta_tv_template)
4933 		}
4934 	}, {
4935 		.alg = "ecb(xtea)",
4936 		.test = alg_test_skcipher,
4937 		.suite = {
4938 			.cipher = __VECS(xtea_tv_template)
4939 		}
4940 	}, {
4941 #if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
4942 		.alg = "ecb-paes-s390",
4943 		.fips_allowed = 1,
4944 		.test = alg_test_skcipher,
4945 		.suite = {
4946 			.cipher = __VECS(aes_tv_template)
4947 		}
4948 	}, {
4949 #endif
4950 		.alg = "ecdh-nist-p192",
4951 		.test = alg_test_kpp,
4952 		.suite = {
4953 			.kpp = __VECS(ecdh_p192_tv_template)
4954 		}
4955 	}, {
4956 		.alg = "ecdh-nist-p256",
4957 		.test = alg_test_kpp,
4958 		.fips_allowed = 1,
4959 		.suite = {
4960 			.kpp = __VECS(ecdh_p256_tv_template)
4961 		}
4962 	}, {
4963 		.alg = "ecdh-nist-p384",
4964 		.test = alg_test_kpp,
4965 		.fips_allowed = 1,
4966 		.suite = {
4967 			.kpp = __VECS(ecdh_p384_tv_template)
4968 		}
4969 	}, {
4970 		.alg = "ecdsa-nist-p192",
4971 		.test = alg_test_sig,
4972 		.suite = {
4973 			.sig = __VECS(ecdsa_nist_p192_tv_template)
4974 		}
4975 	}, {
4976 		.alg = "ecdsa-nist-p256",
4977 		.test = alg_test_sig,
4978 		.fips_allowed = 1,
4979 		.suite = {
4980 			.sig = __VECS(ecdsa_nist_p256_tv_template)
4981 		}
4982 	}, {
4983 		.alg = "ecdsa-nist-p384",
4984 		.test = alg_test_sig,
4985 		.fips_allowed = 1,
4986 		.suite = {
4987 			.sig = __VECS(ecdsa_nist_p384_tv_template)
4988 		}
4989 	}, {
4990 		.alg = "ecdsa-nist-p521",
4991 		.test = alg_test_sig,
4992 		.fips_allowed = 1,
4993 		.suite = {
4994 			.sig = __VECS(ecdsa_nist_p521_tv_template)
4995 		}
4996 	}, {
4997 		.alg = "ecrdsa",
4998 		.test = alg_test_sig,
4999 		.suite = {
5000 			.sig = __VECS(ecrdsa_tv_template)
5001 		}
5002 	}, {
5003 		.alg = "essiv(authenc(hmac(sha256),cbc(aes)),sha256)",
5004 		.test = alg_test_aead,
5005 		.fips_allowed = 1,
5006 		.suite = {
5007 			.aead = __VECS(essiv_hmac_sha256_aes_cbc_tv_temp)
5008 		}
5009 	}, {
5010 		.alg = "essiv(cbc(aes),sha256)",
5011 		.test = alg_test_skcipher,
5012 		.fips_allowed = 1,
5013 		.suite = {
5014 			.cipher = __VECS(essiv_aes_cbc_tv_template)
5015 		}
5016 	}, {
5017 #if IS_ENABLED(CONFIG_CRYPTO_DH_RFC7919_GROUPS)
5018 		.alg = "ffdhe2048(dh)",
5019 		.test = alg_test_kpp,
5020 		.fips_allowed = 1,
5021 		.suite = {
5022 			.kpp = __VECS(ffdhe2048_dh_tv_template)
5023 		}
5024 	}, {
5025 		.alg = "ffdhe3072(dh)",
5026 		.test = alg_test_kpp,
5027 		.fips_allowed = 1,
5028 		.suite = {
5029 			.kpp = __VECS(ffdhe3072_dh_tv_template)
5030 		}
5031 	}, {
5032 		.alg = "ffdhe4096(dh)",
5033 		.test = alg_test_kpp,
5034 		.fips_allowed = 1,
5035 		.suite = {
5036 			.kpp = __VECS(ffdhe4096_dh_tv_template)
5037 		}
5038 	}, {
5039 		.alg = "ffdhe6144(dh)",
5040 		.test = alg_test_kpp,
5041 		.fips_allowed = 1,
5042 		.suite = {
5043 			.kpp = __VECS(ffdhe6144_dh_tv_template)
5044 		}
5045 	}, {
5046 		.alg = "ffdhe8192(dh)",
5047 		.test = alg_test_kpp,
5048 		.fips_allowed = 1,
5049 		.suite = {
5050 			.kpp = __VECS(ffdhe8192_dh_tv_template)
5051 		}
5052 	}, {
5053 #endif /* CONFIG_CRYPTO_DH_RFC7919_GROUPS */
5054 		.alg = "gcm(aes)",
5055 		.generic_driver = "gcm_base(ctr(aes-generic),ghash-generic)",
5056 		.test = alg_test_aead,
5057 		.fips_allowed = 1,
5058 		.suite = {
5059 			.aead = __VECS(aes_gcm_tv_template)
5060 		}
5061 	}, {
5062 		.alg = "gcm(aria)",
5063 		.generic_driver = "gcm_base(ctr(aria-generic),ghash-generic)",
5064 		.test = alg_test_aead,
5065 		.suite = {
5066 			.aead = __VECS(aria_gcm_tv_template)
5067 		}
5068 	}, {
5069 		.alg = "gcm(sm4)",
5070 		.generic_driver = "gcm_base(ctr(sm4-generic),ghash-generic)",
5071 		.test = alg_test_aead,
5072 		.suite = {
5073 			.aead = __VECS(sm4_gcm_tv_template)
5074 		}
5075 	}, {
5076 		.alg = "ghash",
5077 		.test = alg_test_hash,
5078 		.suite = {
5079 			.hash = __VECS(ghash_tv_template)
5080 		}
5081 	}, {
5082 		.alg = "hctr2(aes)",
5083 		.generic_driver =
5084 		    "hctr2_base(xctr(aes-generic),polyval-generic)",
5085 		.test = alg_test_skcipher,
5086 		.suite = {
5087 			.cipher = __VECS(aes_hctr2_tv_template)
5088 		}
5089 	}, {
5090 		.alg = "hmac(md5)",
5091 		.test = alg_test_hash,
5092 		.suite = {
5093 			.hash = __VECS(hmac_md5_tv_template)
5094 		}
5095 	}, {
5096 		.alg = "hmac(rmd160)",
5097 		.test = alg_test_hash,
5098 		.suite = {
5099 			.hash = __VECS(hmac_rmd160_tv_template)
5100 		}
5101 	}, {
5102 		.alg = "hmac(sha1)",
5103 		.test = alg_test_hash,
5104 		.fips_allowed = 1,
5105 		.suite = {
5106 			.hash = __VECS(hmac_sha1_tv_template)
5107 		}
5108 	}, {
5109 		.alg = "hmac(sha224)",
5110 		.test = alg_test_hash,
5111 		.fips_allowed = 1,
5112 		.suite = {
5113 			.hash = __VECS(hmac_sha224_tv_template)
5114 		}
5115 	}, {
5116 		.alg = "hmac(sha256)",
5117 		.test = alg_test_hash,
5118 		.fips_allowed = 1,
5119 		.suite = {
5120 			.hash = __VECS(hmac_sha256_tv_template)
5121 		}
5122 	}, {
5123 		.alg = "hmac(sha3-224)",
5124 		.test = alg_test_hash,
5125 		.fips_allowed = 1,
5126 		.suite = {
5127 			.hash = __VECS(hmac_sha3_224_tv_template)
5128 		}
5129 	}, {
5130 		.alg = "hmac(sha3-256)",
5131 		.test = alg_test_hash,
5132 		.fips_allowed = 1,
5133 		.suite = {
5134 			.hash = __VECS(hmac_sha3_256_tv_template)
5135 		}
5136 	}, {
5137 		.alg = "hmac(sha3-384)",
5138 		.test = alg_test_hash,
5139 		.fips_allowed = 1,
5140 		.suite = {
5141 			.hash = __VECS(hmac_sha3_384_tv_template)
5142 		}
5143 	}, {
5144 		.alg = "hmac(sha3-512)",
5145 		.test = alg_test_hash,
5146 		.fips_allowed = 1,
5147 		.suite = {
5148 			.hash = __VECS(hmac_sha3_512_tv_template)
5149 		}
5150 	}, {
5151 		.alg = "hmac(sha384)",
5152 		.test = alg_test_hash,
5153 		.fips_allowed = 1,
5154 		.suite = {
5155 			.hash = __VECS(hmac_sha384_tv_template)
5156 		}
5157 	}, {
5158 		.alg = "hmac(sha512)",
5159 		.test = alg_test_hash,
5160 		.fips_allowed = 1,
5161 		.suite = {
5162 			.hash = __VECS(hmac_sha512_tv_template)
5163 		}
5164 	}, {
5165 		.alg = "hmac(sm3)",
5166 		.test = alg_test_hash,
5167 		.suite = {
5168 			.hash = __VECS(hmac_sm3_tv_template)
5169 		}
5170 	}, {
5171 		.alg = "hmac(streebog256)",
5172 		.test = alg_test_hash,
5173 		.suite = {
5174 			.hash = __VECS(hmac_streebog256_tv_template)
5175 		}
5176 	}, {
5177 		.alg = "hmac(streebog512)",
5178 		.test = alg_test_hash,
5179 		.suite = {
5180 			.hash = __VECS(hmac_streebog512_tv_template)
5181 		}
5182 	}, {
5183 		.alg = "jitterentropy_rng",
5184 		.fips_allowed = 1,
5185 		.test = alg_test_null,
5186 	}, {
5187 		.alg = "krb5enc(cmac(camellia),cts(cbc(camellia)))",
5188 		.test = alg_test_aead,
5189 		.suite.aead = __VECS(krb5_test_camellia_cts_cmac)
5190 	}, {
5191 		.alg = "lrw(aes)",
5192 		.generic_driver = "lrw(ecb(aes-generic))",
5193 		.test = alg_test_skcipher,
5194 		.suite = {
5195 			.cipher = __VECS(aes_lrw_tv_template)
5196 		}
5197 	}, {
5198 		.alg = "lrw(camellia)",
5199 		.generic_driver = "lrw(ecb(camellia-generic))",
5200 		.test = alg_test_skcipher,
5201 		.suite = {
5202 			.cipher = __VECS(camellia_lrw_tv_template)
5203 		}
5204 	}, {
5205 		.alg = "lrw(cast6)",
5206 		.generic_driver = "lrw(ecb(cast6-generic))",
5207 		.test = alg_test_skcipher,
5208 		.suite = {
5209 			.cipher = __VECS(cast6_lrw_tv_template)
5210 		}
5211 	}, {
5212 		.alg = "lrw(serpent)",
5213 		.generic_driver = "lrw(ecb(serpent-generic))",
5214 		.test = alg_test_skcipher,
5215 		.suite = {
5216 			.cipher = __VECS(serpent_lrw_tv_template)
5217 		}
5218 	}, {
5219 		.alg = "lrw(twofish)",
5220 		.generic_driver = "lrw(ecb(twofish-generic))",
5221 		.test = alg_test_skcipher,
5222 		.suite = {
5223 			.cipher = __VECS(tf_lrw_tv_template)
5224 		}
5225 	}, {
5226 		.alg = "lz4",
5227 		.test = alg_test_comp,
5228 		.fips_allowed = 1,
5229 		.suite = {
5230 			.comp = {
5231 				.comp = __VECS(lz4_comp_tv_template),
5232 				.decomp = __VECS(lz4_decomp_tv_template)
5233 			}
5234 		}
5235 	}, {
5236 		.alg = "lz4hc",
5237 		.test = alg_test_comp,
5238 		.fips_allowed = 1,
5239 		.suite = {
5240 			.comp = {
5241 				.comp = __VECS(lz4hc_comp_tv_template),
5242 				.decomp = __VECS(lz4hc_decomp_tv_template)
5243 			}
5244 		}
5245 	}, {
5246 		.alg = "lzo",
5247 		.test = alg_test_comp,
5248 		.fips_allowed = 1,
5249 		.suite = {
5250 			.comp = {
5251 				.comp = __VECS(lzo_comp_tv_template),
5252 				.decomp = __VECS(lzo_decomp_tv_template)
5253 			}
5254 		}
5255 	}, {
5256 		.alg = "lzo-rle",
5257 		.test = alg_test_comp,
5258 		.fips_allowed = 1,
5259 		.suite = {
5260 			.comp = {
5261 				.comp = __VECS(lzorle_comp_tv_template),
5262 				.decomp = __VECS(lzorle_decomp_tv_template)
5263 			}
5264 		}
5265 	}, {
5266 		.alg = "md4",
5267 		.test = alg_test_hash,
5268 		.suite = {
5269 			.hash = __VECS(md4_tv_template)
5270 		}
5271 	}, {
5272 		.alg = "md5",
5273 		.test = alg_test_hash,
5274 		.suite = {
5275 			.hash = __VECS(md5_tv_template)
5276 		}
5277 	}, {
5278 		.alg = "michael_mic",
5279 		.test = alg_test_hash,
5280 		.suite = {
5281 			.hash = __VECS(michael_mic_tv_template)
5282 		}
5283 	}, {
5284 		.alg = "nhpoly1305",
5285 		.test = alg_test_hash,
5286 		.suite = {
5287 			.hash = __VECS(nhpoly1305_tv_template)
5288 		}
5289 	}, {
5290 		.alg = "p1363(ecdsa-nist-p192)",
5291 		.test = alg_test_null,
5292 	}, {
5293 		.alg = "p1363(ecdsa-nist-p256)",
5294 		.test = alg_test_sig,
5295 		.fips_allowed = 1,
5296 		.suite = {
5297 			.sig = __VECS(p1363_ecdsa_nist_p256_tv_template)
5298 		}
5299 	}, {
5300 		.alg = "p1363(ecdsa-nist-p384)",
5301 		.test = alg_test_null,
5302 		.fips_allowed = 1,
5303 	}, {
5304 		.alg = "p1363(ecdsa-nist-p521)",
5305 		.test = alg_test_null,
5306 		.fips_allowed = 1,
5307 	}, {
5308 		.alg = "pcbc(fcrypt)",
5309 		.test = alg_test_skcipher,
5310 		.suite = {
5311 			.cipher = __VECS(fcrypt_pcbc_tv_template)
5312 		}
5313 	}, {
5314 		.alg = "pkcs1(rsa,none)",
5315 		.test = alg_test_sig,
5316 		.suite = {
5317 			.sig = __VECS(pkcs1_rsa_none_tv_template)
5318 		}
5319 	}, {
5320 		.alg = "pkcs1(rsa,sha224)",
5321 		.test = alg_test_null,
5322 		.fips_allowed = 1,
5323 	}, {
5324 		.alg = "pkcs1(rsa,sha256)",
5325 		.test = alg_test_sig,
5326 		.fips_allowed = 1,
5327 		.suite = {
5328 			.sig = __VECS(pkcs1_rsa_tv_template)
5329 		}
5330 	}, {
5331 		.alg = "pkcs1(rsa,sha3-256)",
5332 		.test = alg_test_null,
5333 		.fips_allowed = 1,
5334 	}, {
5335 		.alg = "pkcs1(rsa,sha3-384)",
5336 		.test = alg_test_null,
5337 		.fips_allowed = 1,
5338 	}, {
5339 		.alg = "pkcs1(rsa,sha3-512)",
5340 		.test = alg_test_null,
5341 		.fips_allowed = 1,
5342 	}, {
5343 		.alg = "pkcs1(rsa,sha384)",
5344 		.test = alg_test_null,
5345 		.fips_allowed = 1,
5346 	}, {
5347 		.alg = "pkcs1(rsa,sha512)",
5348 		.test = alg_test_null,
5349 		.fips_allowed = 1,
5350 	}, {
5351 		.alg = "pkcs1pad(rsa)",
5352 		.test = alg_test_null,
5353 		.fips_allowed = 1,
5354 	}, {
5355 		.alg = "polyval",
5356 		.test = alg_test_hash,
5357 		.suite = {
5358 			.hash = __VECS(polyval_tv_template)
5359 		}
5360 	}, {
5361 		.alg = "rfc3686(ctr(aes))",
5362 		.test = alg_test_skcipher,
5363 		.fips_allowed = 1,
5364 		.suite = {
5365 			.cipher = __VECS(aes_ctr_rfc3686_tv_template)
5366 		}
5367 	}, {
5368 		.alg = "rfc3686(ctr(sm4))",
5369 		.test = alg_test_skcipher,
5370 		.suite = {
5371 			.cipher = __VECS(sm4_ctr_rfc3686_tv_template)
5372 		}
5373 	}, {
5374 		.alg = "rfc4106(gcm(aes))",
5375 		.generic_driver = "rfc4106(gcm_base(ctr(aes-generic),ghash-generic))",
5376 		.test = alg_test_aead,
5377 		.fips_allowed = 1,
5378 		.suite = {
5379 			.aead = {
5380 				____VECS(aes_gcm_rfc4106_tv_template),
5381 				.einval_allowed = 1,
5382 				.aad_iv = 1,
5383 			}
5384 		}
5385 	}, {
5386 		.alg = "rfc4309(ccm(aes))",
5387 		.generic_driver = "rfc4309(ccm_base(ctr(aes-generic),cbcmac(aes-generic)))",
5388 		.test = alg_test_aead,
5389 		.fips_allowed = 1,
5390 		.suite = {
5391 			.aead = {
5392 				____VECS(aes_ccm_rfc4309_tv_template),
5393 				.einval_allowed = 1,
5394 				.aad_iv = 1,
5395 			}
5396 		}
5397 	}, {
5398 		.alg = "rfc4543(gcm(aes))",
5399 		.generic_driver = "rfc4543(gcm_base(ctr(aes-generic),ghash-generic))",
5400 		.test = alg_test_aead,
5401 		.suite = {
5402 			.aead = {
5403 				____VECS(aes_gcm_rfc4543_tv_template),
5404 				.einval_allowed = 1,
5405 				.aad_iv = 1,
5406 			}
5407 		}
5408 	}, {
5409 		.alg = "rfc7539(chacha20,poly1305)",
5410 		.test = alg_test_aead,
5411 		.suite = {
5412 			.aead = __VECS(rfc7539_tv_template)
5413 		}
5414 	}, {
5415 		.alg = "rfc7539esp(chacha20,poly1305)",
5416 		.test = alg_test_aead,
5417 		.suite = {
5418 			.aead = {
5419 				____VECS(rfc7539esp_tv_template),
5420 				.einval_allowed = 1,
5421 				.aad_iv = 1,
5422 			}
5423 		}
5424 	}, {
5425 		.alg = "rmd160",
5426 		.test = alg_test_hash,
5427 		.suite = {
5428 			.hash = __VECS(rmd160_tv_template)
5429 		}
5430 	}, {
5431 		.alg = "rsa",
5432 		.test = alg_test_akcipher,
5433 		.fips_allowed = 1,
5434 		.suite = {
5435 			.akcipher = __VECS(rsa_tv_template)
5436 		}
5437 	}, {
5438 		.alg = "sha1",
5439 		.test = alg_test_hash,
5440 		.fips_allowed = 1,
5441 		.suite = {
5442 			.hash = __VECS(sha1_tv_template)
5443 		}
5444 	}, {
5445 		.alg = "sha224",
5446 		.test = alg_test_hash,
5447 		.fips_allowed = 1,
5448 		.suite = {
5449 			.hash = __VECS(sha224_tv_template)
5450 		}
5451 	}, {
5452 		.alg = "sha256",
5453 		.test = alg_test_hash,
5454 		.fips_allowed = 1,
5455 		.suite = {
5456 			.hash = __VECS(sha256_tv_template)
5457 		}
5458 	}, {
5459 		.alg = "sha3-224",
5460 		.test = alg_test_hash,
5461 		.fips_allowed = 1,
5462 		.suite = {
5463 			.hash = __VECS(sha3_224_tv_template)
5464 		}
5465 	}, {
5466 		.alg = "sha3-256",
5467 		.test = alg_test_hash,
5468 		.fips_allowed = 1,
5469 		.suite = {
5470 			.hash = __VECS(sha3_256_tv_template)
5471 		}
5472 	}, {
5473 		.alg = "sha3-384",
5474 		.test = alg_test_hash,
5475 		.fips_allowed = 1,
5476 		.suite = {
5477 			.hash = __VECS(sha3_384_tv_template)
5478 		}
5479 	}, {
5480 		.alg = "sha3-512",
5481 		.test = alg_test_hash,
5482 		.fips_allowed = 1,
5483 		.suite = {
5484 			.hash = __VECS(sha3_512_tv_template)
5485 		}
5486 	}, {
5487 		.alg = "sha384",
5488 		.test = alg_test_hash,
5489 		.fips_allowed = 1,
5490 		.suite = {
5491 			.hash = __VECS(sha384_tv_template)
5492 		}
5493 	}, {
5494 		.alg = "sha512",
5495 		.test = alg_test_hash,
5496 		.fips_allowed = 1,
5497 		.suite = {
5498 			.hash = __VECS(sha512_tv_template)
5499 		}
5500 	}, {
5501 		.alg = "sm3",
5502 		.test = alg_test_hash,
5503 		.suite = {
5504 			.hash = __VECS(sm3_tv_template)
5505 		}
5506 	}, {
5507 		.alg = "streebog256",
5508 		.test = alg_test_hash,
5509 		.suite = {
5510 			.hash = __VECS(streebog256_tv_template)
5511 		}
5512 	}, {
5513 		.alg = "streebog512",
5514 		.test = alg_test_hash,
5515 		.suite = {
5516 			.hash = __VECS(streebog512_tv_template)
5517 		}
5518 	}, {
5519 		.alg = "wp256",
5520 		.test = alg_test_hash,
5521 		.suite = {
5522 			.hash = __VECS(wp256_tv_template)
5523 		}
5524 	}, {
5525 		.alg = "wp384",
5526 		.test = alg_test_hash,
5527 		.suite = {
5528 			.hash = __VECS(wp384_tv_template)
5529 		}
5530 	}, {
5531 		.alg = "wp512",
5532 		.test = alg_test_hash,
5533 		.suite = {
5534 			.hash = __VECS(wp512_tv_template)
5535 		}
5536 	}, {
5537 		.alg = "x962(ecdsa-nist-p192)",
5538 		.test = alg_test_sig,
5539 		.suite = {
5540 			.sig = __VECS(x962_ecdsa_nist_p192_tv_template)
5541 		}
5542 	}, {
5543 		.alg = "x962(ecdsa-nist-p256)",
5544 		.test = alg_test_sig,
5545 		.fips_allowed = 1,
5546 		.suite = {
5547 			.sig = __VECS(x962_ecdsa_nist_p256_tv_template)
5548 		}
5549 	}, {
5550 		.alg = "x962(ecdsa-nist-p384)",
5551 		.test = alg_test_sig,
5552 		.fips_allowed = 1,
5553 		.suite = {
5554 			.sig = __VECS(x962_ecdsa_nist_p384_tv_template)
5555 		}
5556 	}, {
5557 		.alg = "x962(ecdsa-nist-p521)",
5558 		.test = alg_test_sig,
5559 		.fips_allowed = 1,
5560 		.suite = {
5561 			.sig = __VECS(x962_ecdsa_nist_p521_tv_template)
5562 		}
5563 	}, {
5564 		.alg = "xcbc(aes)",
5565 		.test = alg_test_hash,
5566 		.suite = {
5567 			.hash = __VECS(aes_xcbc128_tv_template)
5568 		}
5569 	}, {
5570 		.alg = "xcbc(sm4)",
5571 		.test = alg_test_hash,
5572 		.suite = {
5573 			.hash = __VECS(sm4_xcbc128_tv_template)
5574 		}
5575 	}, {
5576 		.alg = "xchacha12",
5577 		.test = alg_test_skcipher,
5578 		.suite = {
5579 			.cipher = __VECS(xchacha12_tv_template)
5580 		},
5581 	}, {
5582 		.alg = "xchacha20",
5583 		.test = alg_test_skcipher,
5584 		.suite = {
5585 			.cipher = __VECS(xchacha20_tv_template)
5586 		},
5587 	}, {
5588 		.alg = "xctr(aes)",
5589 		.test = alg_test_skcipher,
5590 		.suite = {
5591 			.cipher = __VECS(aes_xctr_tv_template)
5592 		}
5593 	}, {
5594 		.alg = "xts(aes)",
5595 		.generic_driver = "xts(ecb(aes-generic))",
5596 		.test = alg_test_skcipher,
5597 		.fips_allowed = 1,
5598 		.suite = {
5599 			.cipher = __VECS(aes_xts_tv_template)
5600 		}
5601 	}, {
5602 		.alg = "xts(camellia)",
5603 		.generic_driver = "xts(ecb(camellia-generic))",
5604 		.test = alg_test_skcipher,
5605 		.suite = {
5606 			.cipher = __VECS(camellia_xts_tv_template)
5607 		}
5608 	}, {
5609 		.alg = "xts(cast6)",
5610 		.generic_driver = "xts(ecb(cast6-generic))",
5611 		.test = alg_test_skcipher,
5612 		.suite = {
5613 			.cipher = __VECS(cast6_xts_tv_template)
5614 		}
5615 	}, {
5616 		/* Same as xts(aes) except the key is stored in
5617 		 * hardware secure memory which we reference by index
5618 		 */
5619 		.alg = "xts(paes)",
5620 		.test = alg_test_null,
5621 		.fips_allowed = 1,
5622 	}, {
5623 		.alg = "xts(serpent)",
5624 		.generic_driver = "xts(ecb(serpent-generic))",
5625 		.test = alg_test_skcipher,
5626 		.suite = {
5627 			.cipher = __VECS(serpent_xts_tv_template)
5628 		}
5629 	}, {
5630 		.alg = "xts(sm4)",
5631 		.generic_driver = "xts(ecb(sm4-generic))",
5632 		.test = alg_test_skcipher,
5633 		.suite = {
5634 			.cipher = __VECS(sm4_xts_tv_template)
5635 		}
5636 	}, {
5637 		.alg = "xts(twofish)",
5638 		.generic_driver = "xts(ecb(twofish-generic))",
5639 		.test = alg_test_skcipher,
5640 		.suite = {
5641 			.cipher = __VECS(tf_xts_tv_template)
5642 		}
5643 	}, {
5644 #if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
5645 		.alg = "xts-paes-s390",
5646 		.fips_allowed = 1,
5647 		.test = alg_test_skcipher,
5648 		.suite = {
5649 			.cipher = __VECS(aes_xts_tv_template)
5650 		}
5651 	}, {
5652 #endif
5653 		.alg = "xxhash64",
5654 		.test = alg_test_hash,
5655 		.fips_allowed = 1,
5656 		.suite = {
5657 			.hash = __VECS(xxhash64_tv_template)
5658 		}
5659 	}, {
5660 		.alg = "zstd",
5661 		.test = alg_test_comp,
5662 		.fips_allowed = 1,
5663 		.suite = {
5664 			.comp = {
5665 				.comp = __VECS(zstd_comp_tv_template),
5666 				.decomp = __VECS(zstd_decomp_tv_template)
5667 			}
5668 		}
5669 	}
5670 };
5671 
5672 static void alg_check_test_descs_order(void)
5673 {
5674 	int i;
5675 
5676 	for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
5677 		int diff = strcmp(alg_test_descs[i - 1].alg,
5678 				  alg_test_descs[i].alg);
5679 
5680 		if (WARN_ON(diff > 0)) {
5681 			pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
5682 				alg_test_descs[i - 1].alg,
5683 				alg_test_descs[i].alg);
5684 		}
5685 
5686 		if (WARN_ON(diff == 0)) {
5687 			pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
5688 				alg_test_descs[i].alg);
5689 		}
5690 	}
5691 }
5692 
5693 static void alg_check_testvec_configs(void)
5694 {
5695 	int i;
5696 
5697 	for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++)
5698 		WARN_ON(!valid_testvec_config(
5699 				&default_cipher_testvec_configs[i]));
5700 
5701 	for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++)
5702 		WARN_ON(!valid_testvec_config(
5703 				&default_hash_testvec_configs[i]));
5704 }
5705 
5706 static void testmgr_onetime_init(void)
5707 {
5708 	alg_check_test_descs_order();
5709 	alg_check_testvec_configs();
5710 
5711 	if (!noslowtests)
5712 		pr_warn("alg: full crypto tests enabled.  This is intended for developer use only.\n");
5713 }
5714 
5715 static int alg_find_test(const char *alg)
5716 {
5717 	int start = 0;
5718 	int end = ARRAY_SIZE(alg_test_descs);
5719 
5720 	while (start < end) {
5721 		int i = (start + end) / 2;
5722 		int diff = strcmp(alg_test_descs[i].alg, alg);
5723 
5724 		if (diff > 0) {
5725 			end = i;
5726 			continue;
5727 		}
5728 
5729 		if (diff < 0) {
5730 			start = i + 1;
5731 			continue;
5732 		}
5733 
5734 		return i;
5735 	}
5736 
5737 	return -1;
5738 }
5739 
5740 static int alg_fips_disabled(const char *driver, const char *alg)
5741 {
5742 	pr_info("alg: %s (%s) is disabled due to FIPS\n", alg, driver);
5743 
5744 	return -ECANCELED;
5745 }
5746 
5747 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
5748 {
5749 	int i;
5750 	int j;
5751 	int rc;
5752 
5753 	if (!fips_enabled && notests) {
5754 		printk_once(KERN_INFO "alg: self-tests disabled\n");
5755 		return 0;
5756 	}
5757 
5758 	DO_ONCE(testmgr_onetime_init);
5759 
5760 	if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
5761 		char nalg[CRYPTO_MAX_ALG_NAME];
5762 
5763 		if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
5764 		    sizeof(nalg))
5765 			return -ENAMETOOLONG;
5766 
5767 		i = alg_find_test(nalg);
5768 		if (i < 0)
5769 			goto notest;
5770 
5771 		if (fips_enabled && !alg_test_descs[i].fips_allowed)
5772 			goto non_fips_alg;
5773 
5774 		rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
5775 		goto test_done;
5776 	}
5777 
5778 	i = alg_find_test(alg);
5779 	j = alg_find_test(driver);
5780 	if (i < 0 && j < 0)
5781 		goto notest;
5782 
5783 	if (fips_enabled) {
5784 		if (j >= 0 && !alg_test_descs[j].fips_allowed)
5785 			return -EINVAL;
5786 
5787 		if (i >= 0 && !alg_test_descs[i].fips_allowed)
5788 			goto non_fips_alg;
5789 	}
5790 
5791 	rc = 0;
5792 	if (i >= 0)
5793 		rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
5794 					     type, mask);
5795 	if (j >= 0 && j != i)
5796 		rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
5797 					     type, mask);
5798 
5799 test_done:
5800 	if (rc) {
5801 		if (fips_enabled) {
5802 			fips_fail_notify();
5803 			panic("alg: self-tests for %s (%s) failed in fips mode!\n",
5804 			      driver, alg);
5805 		}
5806 		pr_warn("alg: self-tests for %s using %s failed (rc=%d)",
5807 			alg, driver, rc);
5808 		WARN(rc != -ENOENT,
5809 		     "alg: self-tests for %s using %s failed (rc=%d)",
5810 		     alg, driver, rc);
5811 	} else {
5812 		if (fips_enabled)
5813 			pr_info("alg: self-tests for %s (%s) passed\n",
5814 				driver, alg);
5815 	}
5816 
5817 	return rc;
5818 
5819 notest:
5820 	if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_LSKCIPHER) {
5821 		char nalg[CRYPTO_MAX_ALG_NAME];
5822 
5823 		if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
5824 		    sizeof(nalg))
5825 			goto notest2;
5826 
5827 		i = alg_find_test(nalg);
5828 		if (i < 0)
5829 			goto notest2;
5830 
5831 		if (fips_enabled && !alg_test_descs[i].fips_allowed)
5832 			goto non_fips_alg;
5833 
5834 		rc = alg_test_skcipher(alg_test_descs + i, driver, type, mask);
5835 		goto test_done;
5836 	}
5837 
5838 notest2:
5839 	printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
5840 
5841 	if (type & CRYPTO_ALG_FIPS_INTERNAL)
5842 		return alg_fips_disabled(driver, alg);
5843 
5844 	return 0;
5845 non_fips_alg:
5846 	return alg_fips_disabled(driver, alg);
5847 }
5848 
5849 #endif /* CONFIG_CRYPTO_SELFTESTS */
5850 
5851 EXPORT_SYMBOL_GPL(alg_test);
5852