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