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