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