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