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