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