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