a896d77e96118976d228d238c19f2a8697d618e3
[linux-2.6-microblaze.git] / crypto / testmgr.c
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 */
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
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
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
187 static int testmgr_alloc_buf(char *buf[XBUFSIZE])
188 {
189         return __testmgr_alloc_buf(buf, 0);
190 }
191
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
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
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? */
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
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
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  */
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
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
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  */
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  */
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
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
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
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 */
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  */
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 */
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 */
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 */
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 */
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" */
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
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 */
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
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
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  */
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 */
1116 static void crypto_disable_simd_for_test(void)
1117 {
1118 }
1119
1120 static void crypto_reenable_simd_for_test(void)
1121 {
1122 }
1123 #endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1124
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
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
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
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 */
1177 static int test_shash_vec_cfg(const struct hash_testvec *vec,
1178                               const char *vec_name,
1179                               const struct testvec_config *cfg,
1180                               struct shash_desc *desc,
1181                               struct test_sglist *tsgl,
1182                               u8 *hashstate)
1183 {
1184         struct crypto_shash *tfm = desc->tfm;
1185         const unsigned int alignmask = crypto_shash_alignmask(tfm);
1186         const unsigned int digestsize = crypto_shash_digestsize(tfm);
1187         const unsigned int statesize = crypto_shash_statesize(tfm);
1188         const char *driver = crypto_shash_driver_name(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
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
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 */
1361 static int test_ahash_vec_cfg(const struct hash_testvec *vec,
1362                               const char *vec_name,
1363                               const struct testvec_config *cfg,
1364                               struct ahash_request *req,
1365                               struct test_sglist *tsgl,
1366                               u8 *hashstate)
1367 {
1368         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1369         const unsigned int alignmask = crypto_ahash_alignmask(tfm);
1370         const unsigned int digestsize = crypto_ahash_digestsize(tfm);
1371         const unsigned int statesize = crypto_ahash_statesize(tfm);
1372         const char *driver = crypto_ahash_driver_name(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
1527 static int test_hash_vec_cfg(const struct hash_testvec *vec,
1528                              const char *vec_name,
1529                              const struct testvec_config *cfg,
1530                              struct ahash_request *req,
1531                              struct shash_desc *desc,
1532                              struct test_sglist *tsgl,
1533                              u8 *hashstate)
1534 {
1535         int err;
1536
1537         /*
1538          * For algorithms implemented as "shash", most bugs will be detected by
1539          * both the shash and ahash tests.  Test the shash API first so that the
1540          * failures involve less indirection, so are easier to debug.
1541          */
1542
1543         if (desc) {
1544                 err = test_shash_vec_cfg(vec, vec_name, cfg, desc, tsgl,
1545                                          hashstate);
1546                 if (err)
1547                         return err;
1548         }
1549
1550         return test_ahash_vec_cfg(vec, vec_name, cfg, req, tsgl, hashstate);
1551 }
1552
1553 static int test_hash_vec(const struct hash_testvec *vec, unsigned int vec_num,
1554                          struct ahash_request *req, struct shash_desc *desc,
1555                          struct test_sglist *tsgl, u8 *hashstate)
1556 {
1557         char vec_name[16];
1558         unsigned int i;
1559         int err;
1560
1561         sprintf(vec_name, "%u", vec_num);
1562
1563         for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++) {
1564                 err = test_hash_vec_cfg(vec, vec_name,
1565                                         &default_hash_testvec_configs[i],
1566                                         req, desc, tsgl, hashstate);
1567                 if (err)
1568                         return err;
1569         }
1570
1571 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1572         if (!noextratests) {
1573                 struct testvec_config cfg;
1574                 char cfgname[TESTVEC_CONFIG_NAMELEN];
1575
1576                 for (i = 0; i < fuzz_iterations; i++) {
1577                         generate_random_testvec_config(&cfg, cfgname,
1578                                                        sizeof(cfgname));
1579                         err = test_hash_vec_cfg(vec, vec_name, &cfg,
1580                                                 req, desc, tsgl, hashstate);
1581                         if (err)
1582                                 return err;
1583                         cond_resched();
1584                 }
1585         }
1586 #endif
1587         return 0;
1588 }
1589
1590 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1591 /*
1592  * Generate a hash test vector from the given implementation.
1593  * Assumes the buffers in 'vec' were already allocated.
1594  */
1595 static void generate_random_hash_testvec(struct shash_desc *desc,
1596                                          struct hash_testvec *vec,
1597                                          unsigned int maxkeysize,
1598                                          unsigned int maxdatasize,
1599                                          char *name, size_t max_namelen)
1600 {
1601         /* Data */
1602         vec->psize = generate_random_length(maxdatasize);
1603         generate_random_bytes((u8 *)vec->plaintext, vec->psize);
1604
1605         /*
1606          * Key: length in range [1, maxkeysize], but usually choose maxkeysize.
1607          * If algorithm is unkeyed, then maxkeysize == 0 and set ksize = 0.
1608          */
1609         vec->setkey_error = 0;
1610         vec->ksize = 0;
1611         if (maxkeysize) {
1612                 vec->ksize = maxkeysize;
1613                 if (prandom_u32() % 4 == 0)
1614                         vec->ksize = 1 + (prandom_u32() % maxkeysize);
1615                 generate_random_bytes((u8 *)vec->key, vec->ksize);
1616
1617                 vec->setkey_error = crypto_shash_setkey(desc->tfm, vec->key,
1618                                                         vec->ksize);
1619                 /* If the key couldn't be set, no need to continue to digest. */
1620                 if (vec->setkey_error)
1621                         goto done;
1622         }
1623
1624         /* Digest */
1625         vec->digest_error = crypto_shash_digest(desc, vec->plaintext,
1626                                                 vec->psize, (u8 *)vec->digest);
1627 done:
1628         snprintf(name, max_namelen, "\"random: psize=%u ksize=%u\"",
1629                  vec->psize, vec->ksize);
1630 }
1631
1632 /*
1633  * Test the hash algorithm represented by @req against the corresponding generic
1634  * implementation, if one is available.
1635  */
1636 static int test_hash_vs_generic_impl(const char *generic_driver,
1637                                      unsigned int maxkeysize,
1638                                      struct ahash_request *req,
1639                                      struct shash_desc *desc,
1640                                      struct test_sglist *tsgl,
1641                                      u8 *hashstate)
1642 {
1643         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1644         const unsigned int digestsize = crypto_ahash_digestsize(tfm);
1645         const unsigned int blocksize = crypto_ahash_blocksize(tfm);
1646         const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
1647         const char *algname = crypto_hash_alg_common(tfm)->base.cra_name;
1648         const char *driver = crypto_ahash_driver_name(tfm);
1649         char _generic_driver[CRYPTO_MAX_ALG_NAME];
1650         struct crypto_shash *generic_tfm = NULL;
1651         struct shash_desc *generic_desc = NULL;
1652         unsigned int i;
1653         struct hash_testvec vec = { 0 };
1654         char vec_name[64];
1655         struct testvec_config *cfg;
1656         char cfgname[TESTVEC_CONFIG_NAMELEN];
1657         int err;
1658
1659         if (noextratests)
1660                 return 0;
1661
1662         if (!generic_driver) { /* Use default naming convention? */
1663                 err = build_generic_driver_name(algname, _generic_driver);
1664                 if (err)
1665                         return err;
1666                 generic_driver = _generic_driver;
1667         }
1668
1669         if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
1670                 return 0;
1671
1672         generic_tfm = crypto_alloc_shash(generic_driver, 0, 0);
1673         if (IS_ERR(generic_tfm)) {
1674                 err = PTR_ERR(generic_tfm);
1675                 if (err == -ENOENT) {
1676                         pr_warn("alg: hash: skipping comparison tests for %s because %s is unavailable\n",
1677                                 driver, generic_driver);
1678                         return 0;
1679                 }
1680                 pr_err("alg: hash: error allocating %s (generic impl of %s): %d\n",
1681                        generic_driver, algname, err);
1682                 return err;
1683         }
1684
1685         cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1686         if (!cfg) {
1687                 err = -ENOMEM;
1688                 goto out;
1689         }
1690
1691         generic_desc = kzalloc(sizeof(*desc) +
1692                                crypto_shash_descsize(generic_tfm), GFP_KERNEL);
1693         if (!generic_desc) {
1694                 err = -ENOMEM;
1695                 goto out;
1696         }
1697         generic_desc->tfm = generic_tfm;
1698
1699         /* Check the algorithm properties for consistency. */
1700
1701         if (digestsize != crypto_shash_digestsize(generic_tfm)) {
1702                 pr_err("alg: hash: digestsize for %s (%u) doesn't match generic impl (%u)\n",
1703                        driver, digestsize,
1704                        crypto_shash_digestsize(generic_tfm));
1705                 err = -EINVAL;
1706                 goto out;
1707         }
1708
1709         if (blocksize != crypto_shash_blocksize(generic_tfm)) {
1710                 pr_err("alg: hash: blocksize for %s (%u) doesn't match generic impl (%u)\n",
1711                        driver, blocksize, crypto_shash_blocksize(generic_tfm));
1712                 err = -EINVAL;
1713                 goto out;
1714         }
1715
1716         /*
1717          * Now generate test vectors using the generic implementation, and test
1718          * the other implementation against them.
1719          */
1720
1721         vec.key = kmalloc(maxkeysize, GFP_KERNEL);
1722         vec.plaintext = kmalloc(maxdatasize, GFP_KERNEL);
1723         vec.digest = kmalloc(digestsize, GFP_KERNEL);
1724         if (!vec.key || !vec.plaintext || !vec.digest) {
1725                 err = -ENOMEM;
1726                 goto out;
1727         }
1728
1729         for (i = 0; i < fuzz_iterations * 8; i++) {
1730                 generate_random_hash_testvec(generic_desc, &vec,
1731                                              maxkeysize, maxdatasize,
1732                                              vec_name, sizeof(vec_name));
1733                 generate_random_testvec_config(cfg, cfgname, sizeof(cfgname));
1734
1735                 err = test_hash_vec_cfg(&vec, vec_name, cfg,
1736                                         req, desc, tsgl, hashstate);
1737                 if (err)
1738                         goto out;
1739                 cond_resched();
1740         }
1741         err = 0;
1742 out:
1743         kfree(cfg);
1744         kfree(vec.key);
1745         kfree(vec.plaintext);
1746         kfree(vec.digest);
1747         crypto_free_shash(generic_tfm);
1748         kfree_sensitive(generic_desc);
1749         return err;
1750 }
1751 #else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1752 static int test_hash_vs_generic_impl(const char *generic_driver,
1753                                      unsigned int maxkeysize,
1754                                      struct ahash_request *req,
1755                                      struct shash_desc *desc,
1756                                      struct test_sglist *tsgl,
1757                                      u8 *hashstate)
1758 {
1759         return 0;
1760 }
1761 #endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1762
1763 static int alloc_shash(const char *driver, u32 type, u32 mask,
1764                        struct crypto_shash **tfm_ret,
1765                        struct shash_desc **desc_ret)
1766 {
1767         struct crypto_shash *tfm;
1768         struct shash_desc *desc;
1769
1770         tfm = crypto_alloc_shash(driver, type, mask);
1771         if (IS_ERR(tfm)) {
1772                 if (PTR_ERR(tfm) == -ENOENT) {
1773                         /*
1774                          * This algorithm is only available through the ahash
1775                          * API, not the shash API, so skip the shash tests.
1776                          */
1777                         return 0;
1778                 }
1779                 pr_err("alg: hash: failed to allocate shash transform for %s: %ld\n",
1780                        driver, PTR_ERR(tfm));
1781                 return PTR_ERR(tfm);
1782         }
1783
1784         desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL);
1785         if (!desc) {
1786                 crypto_free_shash(tfm);
1787                 return -ENOMEM;
1788         }
1789         desc->tfm = tfm;
1790
1791         *tfm_ret = tfm;
1792         *desc_ret = desc;
1793         return 0;
1794 }
1795
1796 static int __alg_test_hash(const struct hash_testvec *vecs,
1797                            unsigned int num_vecs, const char *driver,
1798                            u32 type, u32 mask,
1799                            const char *generic_driver, unsigned int maxkeysize)
1800 {
1801         struct crypto_ahash *atfm = NULL;
1802         struct ahash_request *req = NULL;
1803         struct crypto_shash *stfm = NULL;
1804         struct shash_desc *desc = NULL;
1805         struct test_sglist *tsgl = NULL;
1806         u8 *hashstate = NULL;
1807         unsigned int statesize;
1808         unsigned int i;
1809         int err;
1810
1811         /*
1812          * Always test the ahash API.  This works regardless of whether the
1813          * algorithm is implemented as ahash or shash.
1814          */
1815
1816         atfm = crypto_alloc_ahash(driver, type, mask);
1817         if (IS_ERR(atfm)) {
1818                 pr_err("alg: hash: failed to allocate transform for %s: %ld\n",
1819                        driver, PTR_ERR(atfm));
1820                 return PTR_ERR(atfm);
1821         }
1822         driver = crypto_ahash_driver_name(atfm);
1823
1824         req = ahash_request_alloc(atfm, GFP_KERNEL);
1825         if (!req) {
1826                 pr_err("alg: hash: failed to allocate request for %s\n",
1827                        driver);
1828                 err = -ENOMEM;
1829                 goto out;
1830         }
1831
1832         /*
1833          * If available also test the shash API, to cover corner cases that may
1834          * be missed by testing the ahash API only.
1835          */
1836         err = alloc_shash(driver, type, mask, &stfm, &desc);
1837         if (err)
1838                 goto out;
1839
1840         tsgl = kmalloc(sizeof(*tsgl), GFP_KERNEL);
1841         if (!tsgl || init_test_sglist(tsgl) != 0) {
1842                 pr_err("alg: hash: failed to allocate test buffers for %s\n",
1843                        driver);
1844                 kfree(tsgl);
1845                 tsgl = NULL;
1846                 err = -ENOMEM;
1847                 goto out;
1848         }
1849
1850         statesize = crypto_ahash_statesize(atfm);
1851         if (stfm)
1852                 statesize = max(statesize, crypto_shash_statesize(stfm));
1853         hashstate = kmalloc(statesize + TESTMGR_POISON_LEN, GFP_KERNEL);
1854         if (!hashstate) {
1855                 pr_err("alg: hash: failed to allocate hash state buffer for %s\n",
1856                        driver);
1857                 err = -ENOMEM;
1858                 goto out;
1859         }
1860
1861         for (i = 0; i < num_vecs; i++) {
1862                 err = test_hash_vec(&vecs[i], i, req, desc, tsgl, hashstate);
1863                 if (err)
1864                         goto out;
1865                 cond_resched();
1866         }
1867         err = test_hash_vs_generic_impl(generic_driver, maxkeysize, req,
1868                                         desc, tsgl, hashstate);
1869 out:
1870         kfree(hashstate);
1871         if (tsgl) {
1872                 destroy_test_sglist(tsgl);
1873                 kfree(tsgl);
1874         }
1875         kfree(desc);
1876         crypto_free_shash(stfm);
1877         ahash_request_free(req);
1878         crypto_free_ahash(atfm);
1879         return err;
1880 }
1881
1882 static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1883                          u32 type, u32 mask)
1884 {
1885         const struct hash_testvec *template = desc->suite.hash.vecs;
1886         unsigned int tcount = desc->suite.hash.count;
1887         unsigned int nr_unkeyed, nr_keyed;
1888         unsigned int maxkeysize = 0;
1889         int err;
1890
1891         /*
1892          * For OPTIONAL_KEY algorithms, we have to do all the unkeyed tests
1893          * first, before setting a key on the tfm.  To make this easier, we
1894          * require that the unkeyed test vectors (if any) are listed first.
1895          */
1896
1897         for (nr_unkeyed = 0; nr_unkeyed < tcount; nr_unkeyed++) {
1898                 if (template[nr_unkeyed].ksize)
1899                         break;
1900         }
1901         for (nr_keyed = 0; nr_unkeyed + nr_keyed < tcount; nr_keyed++) {
1902                 if (!template[nr_unkeyed + nr_keyed].ksize) {
1903                         pr_err("alg: hash: test vectors for %s out of order, "
1904                                "unkeyed ones must come first\n", desc->alg);
1905                         return -EINVAL;
1906                 }
1907                 maxkeysize = max_t(unsigned int, maxkeysize,
1908                                    template[nr_unkeyed + nr_keyed].ksize);
1909         }
1910
1911         err = 0;
1912         if (nr_unkeyed) {
1913                 err = __alg_test_hash(template, nr_unkeyed, driver, type, mask,
1914                                       desc->generic_driver, maxkeysize);
1915                 template += nr_unkeyed;
1916         }
1917
1918         if (!err && nr_keyed)
1919                 err = __alg_test_hash(template, nr_keyed, driver, type, mask,
1920                                       desc->generic_driver, maxkeysize);
1921
1922         return err;
1923 }
1924
1925 static int test_aead_vec_cfg(int enc, const struct aead_testvec *vec,
1926                              const char *vec_name,
1927                              const struct testvec_config *cfg,
1928                              struct aead_request *req,
1929                              struct cipher_test_sglists *tsgls)
1930 {
1931         struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1932         const unsigned int alignmask = crypto_aead_alignmask(tfm);
1933         const unsigned int ivsize = crypto_aead_ivsize(tfm);
1934         const unsigned int authsize = vec->clen - vec->plen;
1935         const char *driver = crypto_aead_driver_name(tfm);
1936         const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1937         const char *op = enc ? "encryption" : "decryption";
1938         DECLARE_CRYPTO_WAIT(wait);
1939         u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
1940         u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
1941                  cfg->iv_offset +
1942                  (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
1943         struct kvec input[2];
1944         int err;
1945
1946         /* Set the key */
1947         if (vec->wk)
1948                 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1949         else
1950                 crypto_aead_clear_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1951
1952         err = do_setkey(crypto_aead_setkey, tfm, vec->key, vec->klen,
1953                         cfg, alignmask);
1954         if (err && err != vec->setkey_error) {
1955                 pr_err("alg: aead: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1956                        driver, vec_name, vec->setkey_error, err,
1957                        crypto_aead_get_flags(tfm));
1958                 return err;
1959         }
1960         if (!err && vec->setkey_error) {
1961                 pr_err("alg: aead: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1962                        driver, vec_name, vec->setkey_error);
1963                 return -EINVAL;
1964         }
1965
1966         /* Set the authentication tag size */
1967         err = crypto_aead_setauthsize(tfm, authsize);
1968         if (err && err != vec->setauthsize_error) {
1969                 pr_err("alg: aead: %s setauthsize failed on test vector %s; expected_error=%d, actual_error=%d\n",
1970                        driver, vec_name, vec->setauthsize_error, err);
1971                 return err;
1972         }
1973         if (!err && vec->setauthsize_error) {
1974                 pr_err("alg: aead: %s setauthsize unexpectedly succeeded on test vector %s; expected_error=%d\n",
1975                        driver, vec_name, vec->setauthsize_error);
1976                 return -EINVAL;
1977         }
1978
1979         if (vec->setkey_error || vec->setauthsize_error)
1980                 return 0;
1981
1982         /* The IV must be copied to a buffer, as the algorithm may modify it */
1983         if (WARN_ON(ivsize > MAX_IVLEN))
1984                 return -EINVAL;
1985         if (vec->iv)
1986                 memcpy(iv, vec->iv, ivsize);
1987         else
1988                 memset(iv, 0, ivsize);
1989
1990         /* Build the src/dst scatterlists */
1991         input[0].iov_base = (void *)vec->assoc;
1992         input[0].iov_len = vec->alen;
1993         input[1].iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
1994         input[1].iov_len = enc ? vec->plen : vec->clen;
1995         err = build_cipher_test_sglists(tsgls, cfg, alignmask,
1996                                         vec->alen + (enc ? vec->plen :
1997                                                      vec->clen),
1998                                         vec->alen + (enc ? vec->clen :
1999                                                      vec->plen),
2000                                         input, 2);
2001         if (err) {
2002                 pr_err("alg: aead: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
2003                        driver, op, vec_name, cfg->name);
2004                 return err;
2005         }
2006
2007         /* Do the actual encryption or decryption */
2008         testmgr_poison(req->__ctx, crypto_aead_reqsize(tfm));
2009         aead_request_set_callback(req, req_flags, crypto_req_done, &wait);
2010         aead_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
2011                                enc ? vec->plen : vec->clen, iv);
2012         aead_request_set_ad(req, vec->alen);
2013         if (cfg->nosimd)
2014                 crypto_disable_simd_for_test();
2015         err = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
2016         if (cfg->nosimd)
2017                 crypto_reenable_simd_for_test();
2018         err = crypto_wait_req(err, &wait);
2019
2020         /* Check that the algorithm didn't overwrite things it shouldn't have */
2021         if (req->cryptlen != (enc ? vec->plen : vec->clen) ||
2022             req->assoclen != vec->alen ||
2023             req->iv != iv ||
2024             req->src != tsgls->src.sgl_ptr ||
2025             req->dst != tsgls->dst.sgl_ptr ||
2026             crypto_aead_reqtfm(req) != tfm ||
2027             req->base.complete != crypto_req_done ||
2028             req->base.flags != req_flags ||
2029             req->base.data != &wait) {
2030                 pr_err("alg: aead: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
2031                        driver, op, vec_name, cfg->name);
2032                 if (req->cryptlen != (enc ? vec->plen : vec->clen))
2033                         pr_err("alg: aead: changed 'req->cryptlen'\n");
2034                 if (req->assoclen != vec->alen)
2035                         pr_err("alg: aead: changed 'req->assoclen'\n");
2036                 if (req->iv != iv)
2037                         pr_err("alg: aead: changed 'req->iv'\n");
2038                 if (req->src != tsgls->src.sgl_ptr)
2039                         pr_err("alg: aead: changed 'req->src'\n");
2040                 if (req->dst != tsgls->dst.sgl_ptr)
2041                         pr_err("alg: aead: changed 'req->dst'\n");
2042                 if (crypto_aead_reqtfm(req) != tfm)
2043                         pr_err("alg: aead: changed 'req->base.tfm'\n");
2044                 if (req->base.complete != crypto_req_done)
2045                         pr_err("alg: aead: changed 'req->base.complete'\n");
2046                 if (req->base.flags != req_flags)
2047                         pr_err("alg: aead: changed 'req->base.flags'\n");
2048                 if (req->base.data != &wait)
2049                         pr_err("alg: aead: changed 'req->base.data'\n");
2050                 return -EINVAL;
2051         }
2052         if (is_test_sglist_corrupted(&tsgls->src)) {
2053                 pr_err("alg: aead: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
2054                        driver, op, vec_name, cfg->name);
2055                 return -EINVAL;
2056         }
2057         if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
2058             is_test_sglist_corrupted(&tsgls->dst)) {
2059                 pr_err("alg: aead: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
2060                        driver, op, vec_name, cfg->name);
2061                 return -EINVAL;
2062         }
2063
2064         /* Check for unexpected success or failure, or wrong error code */
2065         if ((err == 0 && vec->novrfy) ||
2066             (err != vec->crypt_error && !(err == -EBADMSG && vec->novrfy))) {
2067                 char expected_error[32];
2068
2069                 if (vec->novrfy &&
2070                     vec->crypt_error != 0 && vec->crypt_error != -EBADMSG)
2071                         sprintf(expected_error, "-EBADMSG or %d",
2072                                 vec->crypt_error);
2073                 else if (vec->novrfy)
2074                         sprintf(expected_error, "-EBADMSG");
2075                 else
2076                         sprintf(expected_error, "%d", vec->crypt_error);
2077                 if (err) {
2078                         pr_err("alg: aead: %s %s failed on test vector %s; expected_error=%s, actual_error=%d, cfg=\"%s\"\n",
2079                                driver, op, vec_name, expected_error, err,
2080                                cfg->name);
2081                         return err;
2082                 }
2083                 pr_err("alg: aead: %s %s unexpectedly succeeded on test vector %s; expected_error=%s, cfg=\"%s\"\n",
2084                        driver, op, vec_name, expected_error, cfg->name);
2085                 return -EINVAL;
2086         }
2087         if (err) /* Expectedly failed. */
2088                 return 0;
2089
2090         /* Check for the correct output (ciphertext or plaintext) */
2091         err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
2092                                     enc ? vec->clen : vec->plen,
2093                                     vec->alen, enc || !cfg->inplace);
2094         if (err == -EOVERFLOW) {
2095                 pr_err("alg: aead: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
2096                        driver, op, vec_name, cfg->name);
2097                 return err;
2098         }
2099         if (err) {
2100                 pr_err("alg: aead: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
2101                        driver, op, vec_name, cfg->name);
2102                 return err;
2103         }
2104
2105         return 0;
2106 }
2107
2108 static int test_aead_vec(int enc, const struct aead_testvec *vec,
2109                          unsigned int vec_num, struct aead_request *req,
2110                          struct cipher_test_sglists *tsgls)
2111 {
2112         char vec_name[16];
2113         unsigned int i;
2114         int err;
2115
2116         if (enc && vec->novrfy)
2117                 return 0;
2118
2119         sprintf(vec_name, "%u", vec_num);
2120
2121         for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
2122                 err = test_aead_vec_cfg(enc, vec, vec_name,
2123                                         &default_cipher_testvec_configs[i],
2124                                         req, tsgls);
2125                 if (err)
2126                         return err;
2127         }
2128
2129 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2130         if (!noextratests) {
2131                 struct testvec_config cfg;
2132                 char cfgname[TESTVEC_CONFIG_NAMELEN];
2133
2134                 for (i = 0; i < fuzz_iterations; i++) {
2135                         generate_random_testvec_config(&cfg, cfgname,
2136                                                        sizeof(cfgname));
2137                         err = test_aead_vec_cfg(enc, vec, vec_name,
2138                                                 &cfg, req, tsgls);
2139                         if (err)
2140                                 return err;
2141                         cond_resched();
2142                 }
2143         }
2144 #endif
2145         return 0;
2146 }
2147
2148 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2149
2150 struct aead_extra_tests_ctx {
2151         struct aead_request *req;
2152         struct crypto_aead *tfm;
2153         const struct alg_test_desc *test_desc;
2154         struct cipher_test_sglists *tsgls;
2155         unsigned int maxdatasize;
2156         unsigned int maxkeysize;
2157
2158         struct aead_testvec vec;
2159         char vec_name[64];
2160         char cfgname[TESTVEC_CONFIG_NAMELEN];
2161         struct testvec_config cfg;
2162 };
2163
2164 /*
2165  * Make at least one random change to a (ciphertext, AAD) pair.  "Ciphertext"
2166  * here means the full ciphertext including the authentication tag.  The
2167  * authentication tag (and hence also the ciphertext) is assumed to be nonempty.
2168  */
2169 static void mutate_aead_message(struct aead_testvec *vec, bool aad_iv,
2170                                 unsigned int ivsize)
2171 {
2172         const unsigned int aad_tail_size = aad_iv ? ivsize : 0;
2173         const unsigned int authsize = vec->clen - vec->plen;
2174
2175         if (prandom_u32() % 2 == 0 && vec->alen > aad_tail_size) {
2176                  /* Mutate the AAD */
2177                 flip_random_bit((u8 *)vec->assoc, vec->alen - aad_tail_size);
2178                 if (prandom_u32() % 2 == 0)
2179                         return;
2180         }
2181         if (prandom_u32() % 2 == 0) {
2182                 /* Mutate auth tag (assuming it's at the end of ciphertext) */
2183                 flip_random_bit((u8 *)vec->ctext + vec->plen, authsize);
2184         } else {
2185                 /* Mutate any part of the ciphertext */
2186                 flip_random_bit((u8 *)vec->ctext, vec->clen);
2187         }
2188 }
2189
2190 /*
2191  * Minimum authentication tag size in bytes at which we assume that we can
2192  * reliably generate inauthentic messages, i.e. not generate an authentic
2193  * message by chance.
2194  */
2195 #define MIN_COLLISION_FREE_AUTHSIZE 8
2196
2197 static void generate_aead_message(struct aead_request *req,
2198                                   const struct aead_test_suite *suite,
2199                                   struct aead_testvec *vec,
2200                                   bool prefer_inauthentic)
2201 {
2202         struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2203         const unsigned int ivsize = crypto_aead_ivsize(tfm);
2204         const unsigned int authsize = vec->clen - vec->plen;
2205         const bool inauthentic = (authsize >= MIN_COLLISION_FREE_AUTHSIZE) &&
2206                                  (prefer_inauthentic || prandom_u32() % 4 == 0);
2207
2208         /* Generate the AAD. */
2209         generate_random_bytes((u8 *)vec->assoc, vec->alen);
2210         if (suite->aad_iv && vec->alen >= ivsize)
2211                 /* Avoid implementation-defined behavior. */
2212                 memcpy((u8 *)vec->assoc + vec->alen - ivsize, vec->iv, ivsize);
2213
2214         if (inauthentic && prandom_u32() % 2 == 0) {
2215                 /* Generate a random ciphertext. */
2216                 generate_random_bytes((u8 *)vec->ctext, vec->clen);
2217         } else {
2218                 int i = 0;
2219                 struct scatterlist src[2], dst;
2220                 u8 iv[MAX_IVLEN];
2221                 DECLARE_CRYPTO_WAIT(wait);
2222
2223                 /* Generate a random plaintext and encrypt it. */
2224                 sg_init_table(src, 2);
2225                 if (vec->alen)
2226                         sg_set_buf(&src[i++], vec->assoc, vec->alen);
2227                 if (vec->plen) {
2228                         generate_random_bytes((u8 *)vec->ptext, vec->plen);
2229                         sg_set_buf(&src[i++], vec->ptext, vec->plen);
2230                 }
2231                 sg_init_one(&dst, vec->ctext, vec->alen + vec->clen);
2232                 memcpy(iv, vec->iv, ivsize);
2233                 aead_request_set_callback(req, 0, crypto_req_done, &wait);
2234                 aead_request_set_crypt(req, src, &dst, vec->plen, iv);
2235                 aead_request_set_ad(req, vec->alen);
2236                 vec->crypt_error = crypto_wait_req(crypto_aead_encrypt(req),
2237                                                    &wait);
2238                 /* If encryption failed, we're done. */
2239                 if (vec->crypt_error != 0)
2240                         return;
2241                 memmove((u8 *)vec->ctext, vec->ctext + vec->alen, vec->clen);
2242                 if (!inauthentic)
2243                         return;
2244                 /*
2245                  * Mutate the authentic (ciphertext, AAD) pair to get an
2246                  * inauthentic one.
2247                  */
2248                 mutate_aead_message(vec, suite->aad_iv, ivsize);
2249         }
2250         vec->novrfy = 1;
2251         if (suite->einval_allowed)
2252                 vec->crypt_error = -EINVAL;
2253 }
2254
2255 /*
2256  * Generate an AEAD test vector 'vec' using the implementation specified by
2257  * 'req'.  The buffers in 'vec' must already be allocated.
2258  *
2259  * If 'prefer_inauthentic' is true, then this function will generate inauthentic
2260  * test vectors (i.e. vectors with 'vec->novrfy=1') more often.
2261  */
2262 static void generate_random_aead_testvec(struct aead_request *req,
2263                                          struct aead_testvec *vec,
2264                                          const struct aead_test_suite *suite,
2265                                          unsigned int maxkeysize,
2266                                          unsigned int maxdatasize,
2267                                          char *name, size_t max_namelen,
2268                                          bool prefer_inauthentic)
2269 {
2270         struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2271         const unsigned int ivsize = crypto_aead_ivsize(tfm);
2272         const unsigned int maxauthsize = crypto_aead_maxauthsize(tfm);
2273         unsigned int authsize;
2274         unsigned int total_len;
2275
2276         /* Key: length in [0, maxkeysize], but usually choose maxkeysize */
2277         vec->klen = maxkeysize;
2278         if (prandom_u32() % 4 == 0)
2279                 vec->klen = prandom_u32() % (maxkeysize + 1);
2280         generate_random_bytes((u8 *)vec->key, vec->klen);
2281         vec->setkey_error = crypto_aead_setkey(tfm, vec->key, vec->klen);
2282
2283         /* IV */
2284         generate_random_bytes((u8 *)vec->iv, ivsize);
2285
2286         /* Tag length: in [0, maxauthsize], but usually choose maxauthsize */
2287         authsize = maxauthsize;
2288         if (prandom_u32() % 4 == 0)
2289                 authsize = prandom_u32() % (maxauthsize + 1);
2290         if (prefer_inauthentic && authsize < MIN_COLLISION_FREE_AUTHSIZE)
2291                 authsize = MIN_COLLISION_FREE_AUTHSIZE;
2292         if (WARN_ON(authsize > maxdatasize))
2293                 authsize = maxdatasize;
2294         maxdatasize -= authsize;
2295         vec->setauthsize_error = crypto_aead_setauthsize(tfm, authsize);
2296
2297         /* AAD, plaintext, and ciphertext lengths */
2298         total_len = generate_random_length(maxdatasize);
2299         if (prandom_u32() % 4 == 0)
2300                 vec->alen = 0;
2301         else
2302                 vec->alen = generate_random_length(total_len);
2303         vec->plen = total_len - vec->alen;
2304         vec->clen = vec->plen + authsize;
2305
2306         /*
2307          * Generate the AAD, plaintext, and ciphertext.  Not applicable if the
2308          * key or the authentication tag size couldn't be set.
2309          */
2310         vec->novrfy = 0;
2311         vec->crypt_error = 0;
2312         if (vec->setkey_error == 0 && vec->setauthsize_error == 0)
2313                 generate_aead_message(req, suite, vec, prefer_inauthentic);
2314         snprintf(name, max_namelen,
2315                  "\"random: alen=%u plen=%u authsize=%u klen=%u novrfy=%d\"",
2316                  vec->alen, vec->plen, authsize, vec->klen, vec->novrfy);
2317 }
2318
2319 static void try_to_generate_inauthentic_testvec(
2320                                         struct aead_extra_tests_ctx *ctx)
2321 {
2322         int i;
2323
2324         for (i = 0; i < 10; i++) {
2325                 generate_random_aead_testvec(ctx->req, &ctx->vec,
2326                                              &ctx->test_desc->suite.aead,
2327                                              ctx->maxkeysize, ctx->maxdatasize,
2328                                              ctx->vec_name,
2329                                              sizeof(ctx->vec_name), true);
2330                 if (ctx->vec.novrfy)
2331                         return;
2332         }
2333 }
2334
2335 /*
2336  * Generate inauthentic test vectors (i.e. ciphertext, AAD pairs that aren't the
2337  * result of an encryption with the key) and verify that decryption fails.
2338  */
2339 static int test_aead_inauthentic_inputs(struct aead_extra_tests_ctx *ctx)
2340 {
2341         unsigned int i;
2342         int err;
2343
2344         for (i = 0; i < fuzz_iterations * 8; i++) {
2345                 /*
2346                  * Since this part of the tests isn't comparing the
2347                  * implementation to another, there's no point in testing any
2348                  * test vectors other than inauthentic ones (vec.novrfy=1) here.
2349                  *
2350                  * If we're having trouble generating such a test vector, e.g.
2351                  * if the algorithm keeps rejecting the generated keys, don't
2352                  * retry forever; just continue on.
2353                  */
2354                 try_to_generate_inauthentic_testvec(ctx);
2355                 if (ctx->vec.novrfy) {
2356                         generate_random_testvec_config(&ctx->cfg, ctx->cfgname,
2357                                                        sizeof(ctx->cfgname));
2358                         err = test_aead_vec_cfg(DECRYPT, &ctx->vec,
2359                                                 ctx->vec_name, &ctx->cfg,
2360                                                 ctx->req, ctx->tsgls);
2361                         if (err)
2362                                 return err;
2363                 }
2364                 cond_resched();
2365         }
2366         return 0;
2367 }
2368
2369 /*
2370  * Test the AEAD algorithm against the corresponding generic implementation, if
2371  * one is available.
2372  */
2373 static int test_aead_vs_generic_impl(struct aead_extra_tests_ctx *ctx)
2374 {
2375         struct crypto_aead *tfm = ctx->tfm;
2376         const char *algname = crypto_aead_alg(tfm)->base.cra_name;
2377         const char *driver = crypto_aead_driver_name(tfm);
2378         const char *generic_driver = ctx->test_desc->generic_driver;
2379         char _generic_driver[CRYPTO_MAX_ALG_NAME];
2380         struct crypto_aead *generic_tfm = NULL;
2381         struct aead_request *generic_req = NULL;
2382         unsigned int i;
2383         int err;
2384
2385         if (!generic_driver) { /* Use default naming convention? */
2386                 err = build_generic_driver_name(algname, _generic_driver);
2387                 if (err)
2388                         return err;
2389                 generic_driver = _generic_driver;
2390         }
2391
2392         if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
2393                 return 0;
2394
2395         generic_tfm = crypto_alloc_aead(generic_driver, 0, 0);
2396         if (IS_ERR(generic_tfm)) {
2397                 err = PTR_ERR(generic_tfm);
2398                 if (err == -ENOENT) {
2399                         pr_warn("alg: aead: skipping comparison tests for %s because %s is unavailable\n",
2400                                 driver, generic_driver);
2401                         return 0;
2402                 }
2403                 pr_err("alg: aead: error allocating %s (generic impl of %s): %d\n",
2404                        generic_driver, algname, err);
2405                 return err;
2406         }
2407
2408         generic_req = aead_request_alloc(generic_tfm, GFP_KERNEL);
2409         if (!generic_req) {
2410                 err = -ENOMEM;
2411                 goto out;
2412         }
2413
2414         /* Check the algorithm properties for consistency. */
2415
2416         if (crypto_aead_maxauthsize(tfm) !=
2417             crypto_aead_maxauthsize(generic_tfm)) {
2418                 pr_err("alg: aead: maxauthsize for %s (%u) doesn't match generic impl (%u)\n",
2419                        driver, crypto_aead_maxauthsize(tfm),
2420                        crypto_aead_maxauthsize(generic_tfm));
2421                 err = -EINVAL;
2422                 goto out;
2423         }
2424
2425         if (crypto_aead_ivsize(tfm) != crypto_aead_ivsize(generic_tfm)) {
2426                 pr_err("alg: aead: ivsize for %s (%u) doesn't match generic impl (%u)\n",
2427                        driver, crypto_aead_ivsize(tfm),
2428                        crypto_aead_ivsize(generic_tfm));
2429                 err = -EINVAL;
2430                 goto out;
2431         }
2432
2433         if (crypto_aead_blocksize(tfm) != crypto_aead_blocksize(generic_tfm)) {
2434                 pr_err("alg: aead: blocksize for %s (%u) doesn't match generic impl (%u)\n",
2435                        driver, crypto_aead_blocksize(tfm),
2436                        crypto_aead_blocksize(generic_tfm));
2437                 err = -EINVAL;
2438                 goto out;
2439         }
2440
2441         /*
2442          * Now generate test vectors using the generic implementation, and test
2443          * the other implementation against them.
2444          */
2445         for (i = 0; i < fuzz_iterations * 8; i++) {
2446                 generate_random_aead_testvec(generic_req, &ctx->vec,
2447                                              &ctx->test_desc->suite.aead,
2448                                              ctx->maxkeysize, ctx->maxdatasize,
2449                                              ctx->vec_name,
2450                                              sizeof(ctx->vec_name), false);
2451                 generate_random_testvec_config(&ctx->cfg, ctx->cfgname,
2452                                                sizeof(ctx->cfgname));
2453                 if (!ctx->vec.novrfy) {
2454                         err = test_aead_vec_cfg(ENCRYPT, &ctx->vec,
2455                                                 ctx->vec_name, &ctx->cfg,
2456                                                 ctx->req, ctx->tsgls);
2457                         if (err)
2458                                 goto out;
2459                 }
2460                 if (ctx->vec.crypt_error == 0 || ctx->vec.novrfy) {
2461                         err = test_aead_vec_cfg(DECRYPT, &ctx->vec,
2462                                                 ctx->vec_name, &ctx->cfg,
2463                                                 ctx->req, ctx->tsgls);
2464                         if (err)
2465                                 goto out;
2466                 }
2467                 cond_resched();
2468         }
2469         err = 0;
2470 out:
2471         crypto_free_aead(generic_tfm);
2472         aead_request_free(generic_req);
2473         return err;
2474 }
2475
2476 static int test_aead_extra(const struct alg_test_desc *test_desc,
2477                            struct aead_request *req,
2478                            struct cipher_test_sglists *tsgls)
2479 {
2480         struct aead_extra_tests_ctx *ctx;
2481         unsigned int i;
2482         int err;
2483
2484         if (noextratests)
2485                 return 0;
2486
2487         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2488         if (!ctx)
2489                 return -ENOMEM;
2490         ctx->req = req;
2491         ctx->tfm = crypto_aead_reqtfm(req);
2492         ctx->test_desc = test_desc;
2493         ctx->tsgls = tsgls;
2494         ctx->maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
2495         ctx->maxkeysize = 0;
2496         for (i = 0; i < test_desc->suite.aead.count; i++)
2497                 ctx->maxkeysize = max_t(unsigned int, ctx->maxkeysize,
2498                                         test_desc->suite.aead.vecs[i].klen);
2499
2500         ctx->vec.key = kmalloc(ctx->maxkeysize, GFP_KERNEL);
2501         ctx->vec.iv = kmalloc(crypto_aead_ivsize(ctx->tfm), GFP_KERNEL);
2502         ctx->vec.assoc = kmalloc(ctx->maxdatasize, GFP_KERNEL);
2503         ctx->vec.ptext = kmalloc(ctx->maxdatasize, GFP_KERNEL);
2504         ctx->vec.ctext = kmalloc(ctx->maxdatasize, GFP_KERNEL);
2505         if (!ctx->vec.key || !ctx->vec.iv || !ctx->vec.assoc ||
2506             !ctx->vec.ptext || !ctx->vec.ctext) {
2507                 err = -ENOMEM;
2508                 goto out;
2509         }
2510
2511         err = test_aead_vs_generic_impl(ctx);
2512         if (err)
2513                 goto out;
2514
2515         err = test_aead_inauthentic_inputs(ctx);
2516 out:
2517         kfree(ctx->vec.key);
2518         kfree(ctx->vec.iv);
2519         kfree(ctx->vec.assoc);
2520         kfree(ctx->vec.ptext);
2521         kfree(ctx->vec.ctext);
2522         kfree(ctx);
2523         return err;
2524 }
2525 #else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
2526 static int test_aead_extra(const struct alg_test_desc *test_desc,
2527                            struct aead_request *req,
2528                            struct cipher_test_sglists *tsgls)
2529 {
2530         return 0;
2531 }
2532 #endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
2533
2534 static int test_aead(int enc, const struct aead_test_suite *suite,
2535                      struct aead_request *req,
2536                      struct cipher_test_sglists *tsgls)
2537 {
2538         unsigned int i;
2539         int err;
2540
2541         for (i = 0; i < suite->count; i++) {
2542                 err = test_aead_vec(enc, &suite->vecs[i], i, req, tsgls);
2543                 if (err)
2544                         return err;
2545                 cond_resched();
2546         }
2547         return 0;
2548 }
2549
2550 static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
2551                          u32 type, u32 mask)
2552 {
2553         const struct aead_test_suite *suite = &desc->suite.aead;
2554         struct crypto_aead *tfm;
2555         struct aead_request *req = NULL;
2556         struct cipher_test_sglists *tsgls = NULL;
2557         int err;
2558
2559         if (suite->count <= 0) {
2560                 pr_err("alg: aead: empty test suite for %s\n", driver);
2561                 return -EINVAL;
2562         }
2563
2564         tfm = crypto_alloc_aead(driver, type, mask);
2565         if (IS_ERR(tfm)) {
2566                 pr_err("alg: aead: failed to allocate transform for %s: %ld\n",
2567                        driver, PTR_ERR(tfm));
2568                 return PTR_ERR(tfm);
2569         }
2570         driver = crypto_aead_driver_name(tfm);
2571
2572         req = aead_request_alloc(tfm, GFP_KERNEL);
2573         if (!req) {
2574                 pr_err("alg: aead: failed to allocate request for %s\n",
2575                        driver);
2576                 err = -ENOMEM;
2577                 goto out;
2578         }
2579
2580         tsgls = alloc_cipher_test_sglists();
2581         if (!tsgls) {
2582                 pr_err("alg: aead: failed to allocate test buffers for %s\n",
2583                        driver);
2584                 err = -ENOMEM;
2585                 goto out;
2586         }
2587
2588         err = test_aead(ENCRYPT, suite, req, tsgls);
2589         if (err)
2590                 goto out;
2591
2592         err = test_aead(DECRYPT, suite, req, tsgls);
2593         if (err)
2594                 goto out;
2595
2596         err = test_aead_extra(desc, req, tsgls);
2597 out:
2598         free_cipher_test_sglists(tsgls);
2599         aead_request_free(req);
2600         crypto_free_aead(tfm);
2601         return err;
2602 }
2603
2604 static int test_cipher(struct crypto_cipher *tfm, int enc,
2605                        const struct cipher_testvec *template,
2606                        unsigned int tcount)
2607 {
2608         const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
2609         unsigned int i, j, k;
2610         char *q;
2611         const char *e;
2612         const char *input, *result;
2613         void *data;
2614         char *xbuf[XBUFSIZE];
2615         int ret = -ENOMEM;
2616
2617         if (testmgr_alloc_buf(xbuf))
2618                 goto out_nobuf;
2619
2620         if (enc == ENCRYPT)
2621                 e = "encryption";
2622         else
2623                 e = "decryption";
2624
2625         j = 0;
2626         for (i = 0; i < tcount; i++) {
2627
2628                 if (fips_enabled && template[i].fips_skip)
2629                         continue;
2630
2631                 input  = enc ? template[i].ptext : template[i].ctext;
2632                 result = enc ? template[i].ctext : template[i].ptext;
2633                 j++;
2634
2635                 ret = -EINVAL;
2636                 if (WARN_ON(template[i].len > PAGE_SIZE))
2637                         goto out;
2638
2639                 data = xbuf[0];
2640                 memcpy(data, input, template[i].len);
2641
2642                 crypto_cipher_clear_flags(tfm, ~0);
2643                 if (template[i].wk)
2644                         crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
2645
2646                 ret = crypto_cipher_setkey(tfm, template[i].key,
2647                                            template[i].klen);
2648                 if (ret) {
2649                         if (ret == template[i].setkey_error)
2650                                 continue;
2651                         pr_err("alg: cipher: %s setkey failed on test vector %u; expected_error=%d, actual_error=%d, flags=%#x\n",
2652                                algo, j, template[i].setkey_error, ret,
2653                                crypto_cipher_get_flags(tfm));
2654                         goto out;
2655                 }
2656                 if (template[i].setkey_error) {
2657                         pr_err("alg: cipher: %s setkey unexpectedly succeeded on test vector %u; expected_error=%d\n",
2658                                algo, j, template[i].setkey_error);
2659                         ret = -EINVAL;
2660                         goto out;
2661                 }
2662
2663                 for (k = 0; k < template[i].len;
2664                      k += crypto_cipher_blocksize(tfm)) {
2665                         if (enc)
2666                                 crypto_cipher_encrypt_one(tfm, data + k,
2667                                                           data + k);
2668                         else
2669                                 crypto_cipher_decrypt_one(tfm, data + k,
2670                                                           data + k);
2671                 }
2672
2673                 q = data;
2674                 if (memcmp(q, result, template[i].len)) {
2675                         printk(KERN_ERR "alg: cipher: Test %d failed "
2676                                "on %s for %s\n", j, e, algo);
2677                         hexdump(q, template[i].len);
2678                         ret = -EINVAL;
2679                         goto out;
2680                 }
2681         }
2682
2683         ret = 0;
2684
2685 out:
2686         testmgr_free_buf(xbuf);
2687 out_nobuf:
2688         return ret;
2689 }
2690
2691 static int test_skcipher_vec_cfg(int enc, const struct cipher_testvec *vec,
2692                                  const char *vec_name,
2693                                  const struct testvec_config *cfg,
2694                                  struct skcipher_request *req,
2695                                  struct cipher_test_sglists *tsgls)
2696 {
2697         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2698         const unsigned int alignmask = crypto_skcipher_alignmask(tfm);
2699         const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2700         const char *driver = crypto_skcipher_driver_name(tfm);
2701         const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
2702         const char *op = enc ? "encryption" : "decryption";
2703         DECLARE_CRYPTO_WAIT(wait);
2704         u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
2705         u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
2706                  cfg->iv_offset +
2707                  (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
2708         struct kvec input;
2709         int err;
2710
2711         /* Set the key */
2712         if (vec->wk)
2713                 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
2714         else
2715                 crypto_skcipher_clear_flags(tfm,
2716                                             CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
2717         err = do_setkey(crypto_skcipher_setkey, tfm, vec->key, vec->klen,
2718                         cfg, alignmask);
2719         if (err) {
2720                 if (err == vec->setkey_error)
2721                         return 0;
2722                 pr_err("alg: skcipher: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
2723                        driver, vec_name, vec->setkey_error, err,
2724                        crypto_skcipher_get_flags(tfm));
2725                 return err;
2726         }
2727         if (vec->setkey_error) {
2728                 pr_err("alg: skcipher: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
2729                        driver, vec_name, vec->setkey_error);
2730                 return -EINVAL;
2731         }
2732
2733         /* The IV must be copied to a buffer, as the algorithm may modify it */
2734         if (ivsize) {
2735                 if (WARN_ON(ivsize > MAX_IVLEN))
2736                         return -EINVAL;
2737                 if (vec->generates_iv && !enc)
2738                         memcpy(iv, vec->iv_out, ivsize);
2739                 else if (vec->iv)
2740                         memcpy(iv, vec->iv, ivsize);
2741                 else
2742                         memset(iv, 0, ivsize);
2743         } else {
2744                 if (vec->generates_iv) {
2745                         pr_err("alg: skcipher: %s has ivsize=0 but test vector %s generates IV!\n",
2746                                driver, vec_name);
2747                         return -EINVAL;
2748                 }
2749                 iv = NULL;
2750         }
2751
2752         /* Build the src/dst scatterlists */
2753         input.iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
2754         input.iov_len = vec->len;
2755         err = build_cipher_test_sglists(tsgls, cfg, alignmask,
2756                                         vec->len, vec->len, &input, 1);
2757         if (err) {
2758                 pr_err("alg: skcipher: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
2759                        driver, op, vec_name, cfg->name);
2760                 return err;
2761         }
2762
2763         /* Do the actual encryption or decryption */
2764         testmgr_poison(req->__ctx, crypto_skcipher_reqsize(tfm));
2765         skcipher_request_set_callback(req, req_flags, crypto_req_done, &wait);
2766         skcipher_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
2767                                    vec->len, iv);
2768         if (cfg->nosimd)
2769                 crypto_disable_simd_for_test();
2770         err = enc ? crypto_skcipher_encrypt(req) : crypto_skcipher_decrypt(req);
2771         if (cfg->nosimd)
2772                 crypto_reenable_simd_for_test();
2773         err = crypto_wait_req(err, &wait);
2774
2775         /* Check that the algorithm didn't overwrite things it shouldn't have */
2776         if (req->cryptlen != vec->len ||
2777             req->iv != iv ||
2778             req->src != tsgls->src.sgl_ptr ||
2779             req->dst != tsgls->dst.sgl_ptr ||
2780             crypto_skcipher_reqtfm(req) != tfm ||
2781             req->base.complete != crypto_req_done ||
2782             req->base.flags != req_flags ||
2783             req->base.data != &wait) {
2784                 pr_err("alg: skcipher: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
2785                        driver, op, vec_name, cfg->name);
2786                 if (req->cryptlen != vec->len)
2787                         pr_err("alg: skcipher: changed 'req->cryptlen'\n");
2788                 if (req->iv != iv)
2789                         pr_err("alg: skcipher: changed 'req->iv'\n");
2790                 if (req->src != tsgls->src.sgl_ptr)
2791                         pr_err("alg: skcipher: changed 'req->src'\n");
2792                 if (req->dst != tsgls->dst.sgl_ptr)
2793                         pr_err("alg: skcipher: changed 'req->dst'\n");
2794                 if (crypto_skcipher_reqtfm(req) != tfm)
2795                         pr_err("alg: skcipher: changed 'req->base.tfm'\n");
2796                 if (req->base.complete != crypto_req_done)
2797                         pr_err("alg: skcipher: changed 'req->base.complete'\n");
2798                 if (req->base.flags != req_flags)
2799                         pr_err("alg: skcipher: changed 'req->base.flags'\n");
2800                 if (req->base.data != &wait)
2801                         pr_err("alg: skcipher: changed 'req->base.data'\n");
2802                 return -EINVAL;
2803         }
2804         if (is_test_sglist_corrupted(&tsgls->src)) {
2805                 pr_err("alg: skcipher: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
2806                        driver, op, vec_name, cfg->name);
2807                 return -EINVAL;
2808         }
2809         if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
2810             is_test_sglist_corrupted(&tsgls->dst)) {
2811                 pr_err("alg: skcipher: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
2812                        driver, op, vec_name, cfg->name);
2813                 return -EINVAL;
2814         }
2815
2816         /* Check for success or failure */
2817         if (err) {
2818                 if (err == vec->crypt_error)
2819                         return 0;
2820                 pr_err("alg: skcipher: %s %s failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
2821                        driver, op, vec_name, vec->crypt_error, err, cfg->name);
2822                 return err;
2823         }
2824         if (vec->crypt_error) {
2825                 pr_err("alg: skcipher: %s %s unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
2826                        driver, op, vec_name, vec->crypt_error, cfg->name);
2827                 return -EINVAL;
2828         }
2829
2830         /* Check for the correct output (ciphertext or plaintext) */
2831         err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
2832                                     vec->len, 0, true);
2833         if (err == -EOVERFLOW) {
2834                 pr_err("alg: skcipher: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
2835                        driver, op, vec_name, cfg->name);
2836                 return err;
2837         }
2838         if (err) {
2839                 pr_err("alg: skcipher: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
2840                        driver, op, vec_name, cfg->name);
2841                 return err;
2842         }
2843
2844         /* If applicable, check that the algorithm generated the correct IV */
2845         if (vec->iv_out && memcmp(iv, vec->iv_out, ivsize) != 0) {
2846                 pr_err("alg: skcipher: %s %s test failed (wrong output IV) on test vector %s, cfg=\"%s\"\n",
2847                        driver, op, vec_name, cfg->name);
2848                 hexdump(iv, ivsize);
2849                 return -EINVAL;
2850         }
2851
2852         return 0;
2853 }
2854
2855 static int test_skcipher_vec(int enc, const struct cipher_testvec *vec,
2856                              unsigned int vec_num,
2857                              struct skcipher_request *req,
2858                              struct cipher_test_sglists *tsgls)
2859 {
2860         char vec_name[16];
2861         unsigned int i;
2862         int err;
2863
2864         if (fips_enabled && vec->fips_skip)
2865                 return 0;
2866
2867         sprintf(vec_name, "%u", vec_num);
2868
2869         for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
2870                 err = test_skcipher_vec_cfg(enc, vec, vec_name,
2871                                             &default_cipher_testvec_configs[i],
2872                                             req, tsgls);
2873                 if (err)
2874                         return err;
2875         }
2876
2877 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2878         if (!noextratests) {
2879                 struct testvec_config cfg;
2880                 char cfgname[TESTVEC_CONFIG_NAMELEN];
2881
2882                 for (i = 0; i < fuzz_iterations; i++) {
2883                         generate_random_testvec_config(&cfg, cfgname,
2884                                                        sizeof(cfgname));
2885                         err = test_skcipher_vec_cfg(enc, vec, vec_name,
2886                                                     &cfg, req, tsgls);
2887                         if (err)
2888                                 return err;
2889                         cond_resched();
2890                 }
2891         }
2892 #endif
2893         return 0;
2894 }
2895
2896 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2897 /*
2898  * Generate a symmetric cipher test vector from the given implementation.
2899  * Assumes the buffers in 'vec' were already allocated.
2900  */
2901 static void generate_random_cipher_testvec(struct skcipher_request *req,
2902                                            struct cipher_testvec *vec,
2903                                            unsigned int maxdatasize,
2904                                            char *name, size_t max_namelen)
2905 {
2906         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2907         const unsigned int maxkeysize = crypto_skcipher_max_keysize(tfm);
2908         const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2909         struct scatterlist src, dst;
2910         u8 iv[MAX_IVLEN];
2911         DECLARE_CRYPTO_WAIT(wait);
2912
2913         /* Key: length in [0, maxkeysize], but usually choose maxkeysize */
2914         vec->klen = maxkeysize;
2915         if (prandom_u32() % 4 == 0)
2916                 vec->klen = prandom_u32() % (maxkeysize + 1);
2917         generate_random_bytes((u8 *)vec->key, vec->klen);
2918         vec->setkey_error = crypto_skcipher_setkey(tfm, vec->key, vec->klen);
2919
2920         /* IV */
2921         generate_random_bytes((u8 *)vec->iv, ivsize);
2922
2923         /* Plaintext */
2924         vec->len = generate_random_length(maxdatasize);
2925         generate_random_bytes((u8 *)vec->ptext, vec->len);
2926
2927         /* If the key couldn't be set, no need to continue to encrypt. */
2928         if (vec->setkey_error)
2929                 goto done;
2930
2931         /* Ciphertext */
2932         sg_init_one(&src, vec->ptext, vec->len);
2933         sg_init_one(&dst, vec->ctext, vec->len);
2934         memcpy(iv, vec->iv, ivsize);
2935         skcipher_request_set_callback(req, 0, crypto_req_done, &wait);
2936         skcipher_request_set_crypt(req, &src, &dst, vec->len, iv);
2937         vec->crypt_error = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
2938         if (vec->crypt_error != 0) {
2939                 /*
2940                  * The only acceptable error here is for an invalid length, so
2941                  * skcipher decryption should fail with the same error too.
2942                  * We'll test for this.  But to keep the API usage well-defined,
2943                  * explicitly initialize the ciphertext buffer too.
2944                  */
2945                 memset((u8 *)vec->ctext, 0, vec->len);
2946         }
2947 done:
2948         snprintf(name, max_namelen, "\"random: len=%u klen=%u\"",
2949                  vec->len, vec->klen);
2950 }
2951
2952 /*
2953  * Test the skcipher algorithm represented by @req against the corresponding
2954  * generic implementation, if one is available.
2955  */
2956 static int test_skcipher_vs_generic_impl(const char *generic_driver,
2957                                          struct skcipher_request *req,
2958                                          struct cipher_test_sglists *tsgls)
2959 {
2960         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2961         const unsigned int maxkeysize = crypto_skcipher_max_keysize(tfm);
2962         const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2963         const unsigned int blocksize = crypto_skcipher_blocksize(tfm);
2964         const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
2965         const char *algname = crypto_skcipher_alg(tfm)->base.cra_name;
2966         const char *driver = crypto_skcipher_driver_name(tfm);
2967         char _generic_driver[CRYPTO_MAX_ALG_NAME];
2968         struct crypto_skcipher *generic_tfm = NULL;
2969         struct skcipher_request *generic_req = NULL;
2970         unsigned int i;
2971         struct cipher_testvec vec = { 0 };
2972         char vec_name[64];
2973         struct testvec_config *cfg;
2974         char cfgname[TESTVEC_CONFIG_NAMELEN];
2975         int err;
2976
2977         if (noextratests)
2978                 return 0;
2979
2980         /* Keywrap isn't supported here yet as it handles its IV differently. */
2981         if (strncmp(algname, "kw(", 3) == 0)
2982                 return 0;
2983
2984         if (!generic_driver) { /* Use default naming convention? */
2985                 err = build_generic_driver_name(algname, _generic_driver);
2986                 if (err)
2987                         return err;
2988                 generic_driver = _generic_driver;
2989         }
2990
2991         if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
2992                 return 0;
2993
2994         generic_tfm = crypto_alloc_skcipher(generic_driver, 0, 0);
2995         if (IS_ERR(generic_tfm)) {
2996                 err = PTR_ERR(generic_tfm);
2997                 if (err == -ENOENT) {
2998                         pr_warn("alg: skcipher: skipping comparison tests for %s because %s is unavailable\n",
2999                                 driver, generic_driver);
3000                         return 0;
3001                 }
3002                 pr_err("alg: skcipher: error allocating %s (generic impl of %s): %d\n",
3003                        generic_driver, algname, err);
3004                 return err;
3005         }
3006
3007         cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
3008         if (!cfg) {
3009                 err = -ENOMEM;
3010                 goto out;
3011         }
3012
3013         generic_req = skcipher_request_alloc(generic_tfm, GFP_KERNEL);
3014         if (!generic_req) {
3015                 err = -ENOMEM;
3016                 goto out;
3017         }
3018
3019         /* Check the algorithm properties for consistency. */
3020
3021         if (crypto_skcipher_min_keysize(tfm) !=
3022             crypto_skcipher_min_keysize(generic_tfm)) {
3023                 pr_err("alg: skcipher: min keysize for %s (%u) doesn't match generic impl (%u)\n",
3024                        driver, crypto_skcipher_min_keysize(tfm),
3025                        crypto_skcipher_min_keysize(generic_tfm));
3026                 err = -EINVAL;
3027                 goto out;
3028         }
3029
3030         if (maxkeysize != crypto_skcipher_max_keysize(generic_tfm)) {
3031                 pr_err("alg: skcipher: max keysize for %s (%u) doesn't match generic impl (%u)\n",
3032                        driver, maxkeysize,
3033                        crypto_skcipher_max_keysize(generic_tfm));
3034                 err = -EINVAL;
3035                 goto out;
3036         }
3037
3038         if (ivsize != crypto_skcipher_ivsize(generic_tfm)) {
3039                 pr_err("alg: skcipher: ivsize for %s (%u) doesn't match generic impl (%u)\n",
3040                        driver, ivsize, crypto_skcipher_ivsize(generic_tfm));
3041                 err = -EINVAL;
3042                 goto out;
3043         }
3044
3045         if (blocksize != crypto_skcipher_blocksize(generic_tfm)) {
3046                 pr_err("alg: skcipher: blocksize for %s (%u) doesn't match generic impl (%u)\n",
3047                        driver, blocksize,
3048                        crypto_skcipher_blocksize(generic_tfm));
3049                 err = -EINVAL;
3050                 goto out;
3051         }
3052
3053         /*
3054          * Now generate test vectors using the generic implementation, and test
3055          * the other implementation against them.
3056          */
3057
3058         vec.key = kmalloc(maxkeysize, GFP_KERNEL);
3059         vec.iv = kmalloc(ivsize, GFP_KERNEL);
3060         vec.ptext = kmalloc(maxdatasize, GFP_KERNEL);
3061         vec.ctext = kmalloc(maxdatasize, GFP_KERNEL);
3062         if (!vec.key || !vec.iv || !vec.ptext || !vec.ctext) {
3063                 err = -ENOMEM;
3064                 goto out;
3065         }
3066
3067         for (i = 0; i < fuzz_iterations * 8; i++) {
3068                 generate_random_cipher_testvec(generic_req, &vec, maxdatasize,
3069                                                vec_name, sizeof(vec_name));
3070                 generate_random_testvec_config(cfg, cfgname, sizeof(cfgname));
3071
3072                 err = test_skcipher_vec_cfg(ENCRYPT, &vec, vec_name,
3073                                             cfg, req, tsgls);
3074                 if (err)
3075                         goto out;
3076                 err = test_skcipher_vec_cfg(DECRYPT, &vec, vec_name,
3077                                             cfg, req, tsgls);
3078                 if (err)
3079                         goto out;
3080                 cond_resched();
3081         }
3082         err = 0;
3083 out:
3084         kfree(cfg);
3085         kfree(vec.key);
3086         kfree(vec.iv);
3087         kfree(vec.ptext);
3088         kfree(vec.ctext);
3089         crypto_free_skcipher(generic_tfm);
3090         skcipher_request_free(generic_req);
3091         return err;
3092 }
3093 #else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
3094 static int test_skcipher_vs_generic_impl(const char *generic_driver,
3095                                          struct skcipher_request *req,
3096                                          struct cipher_test_sglists *tsgls)
3097 {
3098         return 0;
3099 }
3100 #endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
3101
3102 static int test_skcipher(int enc, const struct cipher_test_suite *suite,
3103                          struct skcipher_request *req,
3104                          struct cipher_test_sglists *tsgls)
3105 {
3106         unsigned int i;
3107         int err;
3108
3109         for (i = 0; i < suite->count; i++) {
3110                 err = test_skcipher_vec(enc, &suite->vecs[i], i, req, tsgls);
3111                 if (err)
3112                         return err;
3113                 cond_resched();
3114         }
3115         return 0;
3116 }
3117
3118 static int alg_test_skcipher(const struct alg_test_desc *desc,
3119                              const char *driver, u32 type, u32 mask)
3120 {
3121         const struct cipher_test_suite *suite = &desc->suite.cipher;
3122         struct crypto_skcipher *tfm;
3123         struct skcipher_request *req = NULL;
3124         struct cipher_test_sglists *tsgls = NULL;
3125         int err;
3126
3127         if (suite->count <= 0) {
3128                 pr_err("alg: skcipher: empty test suite for %s\n", driver);
3129                 return -EINVAL;
3130         }
3131
3132         tfm = crypto_alloc_skcipher(driver, type, mask);
3133         if (IS_ERR(tfm)) {
3134                 pr_err("alg: skcipher: failed to allocate transform for %s: %ld\n",
3135                        driver, PTR_ERR(tfm));
3136                 return PTR_ERR(tfm);
3137         }
3138         driver = crypto_skcipher_driver_name(tfm);
3139
3140         req = skcipher_request_alloc(tfm, GFP_KERNEL);
3141         if (!req) {
3142                 pr_err("alg: skcipher: failed to allocate request for %s\n",
3143                        driver);
3144                 err = -ENOMEM;
3145                 goto out;
3146         }
3147
3148         tsgls = alloc_cipher_test_sglists();
3149         if (!tsgls) {
3150                 pr_err("alg: skcipher: failed to allocate test buffers for %s\n",
3151                        driver);
3152                 err = -ENOMEM;
3153                 goto out;
3154         }
3155
3156         err = test_skcipher(ENCRYPT, suite, req, tsgls);
3157         if (err)
3158                 goto out;
3159
3160         err = test_skcipher(DECRYPT, suite, req, tsgls);
3161         if (err)
3162                 goto out;
3163
3164         err = test_skcipher_vs_generic_impl(desc->generic_driver, req, tsgls);
3165 out:
3166         free_cipher_test_sglists(tsgls);
3167         skcipher_request_free(req);
3168         crypto_free_skcipher(tfm);
3169         return err;
3170 }
3171
3172 static int test_comp(struct crypto_comp *tfm,
3173                      const struct comp_testvec *ctemplate,
3174                      const struct comp_testvec *dtemplate,
3175                      int ctcount, int dtcount)
3176 {
3177         const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
3178         char *output, *decomp_output;
3179         unsigned int i;
3180         int ret;
3181
3182         output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3183         if (!output)
3184                 return -ENOMEM;
3185
3186         decomp_output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3187         if (!decomp_output) {
3188                 kfree(output);
3189                 return -ENOMEM;
3190         }
3191
3192         for (i = 0; i < ctcount; i++) {
3193                 int ilen;
3194                 unsigned int dlen = COMP_BUF_SIZE;
3195
3196                 memset(output, 0, COMP_BUF_SIZE);
3197                 memset(decomp_output, 0, COMP_BUF_SIZE);
3198
3199                 ilen = ctemplate[i].inlen;
3200                 ret = crypto_comp_compress(tfm, ctemplate[i].input,
3201                                            ilen, output, &dlen);
3202                 if (ret) {
3203                         printk(KERN_ERR "alg: comp: compression failed "
3204                                "on test %d for %s: ret=%d\n", i + 1, algo,
3205                                -ret);
3206                         goto out;
3207                 }
3208
3209                 ilen = dlen;
3210                 dlen = COMP_BUF_SIZE;
3211                 ret = crypto_comp_decompress(tfm, output,
3212                                              ilen, decomp_output, &dlen);
3213                 if (ret) {
3214                         pr_err("alg: comp: compression failed: decompress: on test %d for %s failed: ret=%d\n",
3215                                i + 1, algo, -ret);
3216                         goto out;
3217                 }
3218
3219                 if (dlen != ctemplate[i].inlen) {
3220                         printk(KERN_ERR "alg: comp: Compression test %d "
3221                                "failed for %s: output len = %d\n", i + 1, algo,
3222                                dlen);
3223                         ret = -EINVAL;
3224                         goto out;
3225                 }
3226
3227                 if (memcmp(decomp_output, ctemplate[i].input,
3228                            ctemplate[i].inlen)) {
3229                         pr_err("alg: comp: compression failed: output differs: on test %d for %s\n",
3230                                i + 1, algo);
3231                         hexdump(decomp_output, dlen);
3232                         ret = -EINVAL;
3233                         goto out;
3234                 }
3235         }
3236
3237         for (i = 0; i < dtcount; i++) {
3238                 int ilen;
3239                 unsigned int dlen = COMP_BUF_SIZE;
3240
3241                 memset(decomp_output, 0, COMP_BUF_SIZE);
3242
3243                 ilen = dtemplate[i].inlen;
3244                 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
3245                                              ilen, decomp_output, &dlen);
3246                 if (ret) {
3247                         printk(KERN_ERR "alg: comp: decompression failed "
3248                                "on test %d for %s: ret=%d\n", i + 1, algo,
3249                                -ret);
3250                         goto out;
3251                 }
3252
3253                 if (dlen != dtemplate[i].outlen) {
3254                         printk(KERN_ERR "alg: comp: Decompression test %d "
3255                                "failed for %s: output len = %d\n", i + 1, algo,
3256                                dlen);
3257                         ret = -EINVAL;
3258                         goto out;
3259                 }
3260
3261                 if (memcmp(decomp_output, dtemplate[i].output, dlen)) {
3262                         printk(KERN_ERR "alg: comp: Decompression test %d "
3263                                "failed for %s\n", i + 1, algo);
3264                         hexdump(decomp_output, dlen);
3265                         ret = -EINVAL;
3266                         goto out;
3267                 }
3268         }
3269
3270         ret = 0;
3271
3272 out:
3273         kfree(decomp_output);
3274         kfree(output);
3275         return ret;
3276 }
3277
3278 static int test_acomp(struct crypto_acomp *tfm,
3279                               const struct comp_testvec *ctemplate,
3280                       const struct comp_testvec *dtemplate,
3281                       int ctcount, int dtcount)
3282 {
3283         const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
3284         unsigned int i;
3285         char *output, *decomp_out;
3286         int ret;
3287         struct scatterlist src, dst;
3288         struct acomp_req *req;
3289         struct crypto_wait wait;
3290
3291         output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3292         if (!output)
3293                 return -ENOMEM;
3294
3295         decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3296         if (!decomp_out) {
3297                 kfree(output);
3298                 return -ENOMEM;
3299         }
3300
3301         for (i = 0; i < ctcount; i++) {
3302                 unsigned int dlen = COMP_BUF_SIZE;
3303                 int ilen = ctemplate[i].inlen;
3304                 void *input_vec;
3305
3306                 input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL);
3307                 if (!input_vec) {
3308                         ret = -ENOMEM;
3309                         goto out;
3310                 }
3311
3312                 memset(output, 0, dlen);
3313                 crypto_init_wait(&wait);
3314                 sg_init_one(&src, input_vec, ilen);
3315                 sg_init_one(&dst, output, dlen);
3316
3317                 req = acomp_request_alloc(tfm);
3318                 if (!req) {
3319                         pr_err("alg: acomp: request alloc failed for %s\n",
3320                                algo);
3321                         kfree(input_vec);
3322                         ret = -ENOMEM;
3323                         goto out;
3324                 }
3325
3326                 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3327                 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3328                                            crypto_req_done, &wait);
3329
3330                 ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
3331                 if (ret) {
3332                         pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
3333                                i + 1, algo, -ret);
3334                         kfree(input_vec);
3335                         acomp_request_free(req);
3336                         goto out;
3337                 }
3338
3339                 ilen = req->dlen;
3340                 dlen = COMP_BUF_SIZE;
3341                 sg_init_one(&src, output, ilen);
3342                 sg_init_one(&dst, decomp_out, dlen);
3343                 crypto_init_wait(&wait);
3344                 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3345
3346                 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
3347                 if (ret) {
3348                         pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
3349                                i + 1, algo, -ret);
3350                         kfree(input_vec);
3351                         acomp_request_free(req);
3352                         goto out;
3353                 }
3354
3355                 if (req->dlen != ctemplate[i].inlen) {
3356                         pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
3357                                i + 1, algo, req->dlen);
3358                         ret = -EINVAL;
3359                         kfree(input_vec);
3360                         acomp_request_free(req);
3361                         goto out;
3362                 }
3363
3364                 if (memcmp(input_vec, decomp_out, req->dlen)) {
3365                         pr_err("alg: acomp: Compression test %d failed for %s\n",
3366                                i + 1, algo);
3367                         hexdump(output, req->dlen);
3368                         ret = -EINVAL;
3369                         kfree(input_vec);
3370                         acomp_request_free(req);
3371                         goto out;
3372                 }
3373
3374                 kfree(input_vec);
3375                 acomp_request_free(req);
3376         }
3377
3378         for (i = 0; i < dtcount; i++) {
3379                 unsigned int dlen = COMP_BUF_SIZE;
3380                 int ilen = dtemplate[i].inlen;
3381                 void *input_vec;
3382
3383                 input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL);
3384                 if (!input_vec) {
3385                         ret = -ENOMEM;
3386                         goto out;
3387                 }
3388
3389                 memset(output, 0, dlen);
3390                 crypto_init_wait(&wait);
3391                 sg_init_one(&src, input_vec, ilen);
3392                 sg_init_one(&dst, output, dlen);
3393
3394                 req = acomp_request_alloc(tfm);
3395                 if (!req) {
3396                         pr_err("alg: acomp: request alloc failed for %s\n",
3397                                algo);
3398                         kfree(input_vec);
3399                         ret = -ENOMEM;
3400                         goto out;
3401                 }
3402
3403                 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3404                 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3405                                            crypto_req_done, &wait);
3406
3407                 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
3408                 if (ret) {
3409                         pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
3410                                i + 1, algo, -ret);
3411                         kfree(input_vec);
3412                         acomp_request_free(req);
3413                         goto out;
3414                 }
3415
3416                 if (req->dlen != dtemplate[i].outlen) {
3417                         pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
3418                                i + 1, algo, req->dlen);
3419                         ret = -EINVAL;
3420                         kfree(input_vec);
3421                         acomp_request_free(req);
3422                         goto out;
3423                 }
3424
3425                 if (memcmp(output, dtemplate[i].output, req->dlen)) {
3426                         pr_err("alg: acomp: Decompression test %d failed for %s\n",
3427                                i + 1, algo);
3428                         hexdump(output, req->dlen);
3429                         ret = -EINVAL;
3430                         kfree(input_vec);
3431                         acomp_request_free(req);
3432                         goto out;
3433                 }
3434
3435                 kfree(input_vec);
3436                 acomp_request_free(req);
3437         }
3438
3439         ret = 0;
3440
3441 out:
3442         kfree(decomp_out);
3443         kfree(output);
3444         return ret;
3445 }
3446
3447 static int test_cprng(struct crypto_rng *tfm,
3448                       const struct cprng_testvec *template,
3449                       unsigned int tcount)
3450 {
3451         const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
3452         int err = 0, i, j, seedsize;
3453         u8 *seed;
3454         char result[32];
3455
3456         seedsize = crypto_rng_seedsize(tfm);
3457
3458         seed = kmalloc(seedsize, GFP_KERNEL);
3459         if (!seed) {
3460                 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
3461                        "for %s\n", algo);
3462                 return -ENOMEM;
3463         }
3464
3465         for (i = 0; i < tcount; i++) {
3466                 memset(result, 0, 32);
3467
3468                 memcpy(seed, template[i].v, template[i].vlen);
3469                 memcpy(seed + template[i].vlen, template[i].key,
3470                        template[i].klen);
3471                 memcpy(seed + template[i].vlen + template[i].klen,
3472                        template[i].dt, template[i].dtlen);
3473
3474                 err = crypto_rng_reset(tfm, seed, seedsize);
3475                 if (err) {
3476                         printk(KERN_ERR "alg: cprng: Failed to reset rng "
3477                                "for %s\n", algo);
3478                         goto out;
3479                 }
3480
3481                 for (j = 0; j < template[i].loops; j++) {
3482                         err = crypto_rng_get_bytes(tfm, result,
3483                                                    template[i].rlen);
3484                         if (err < 0) {
3485                                 printk(KERN_ERR "alg: cprng: Failed to obtain "
3486                                        "the correct amount of random data for "
3487                                        "%s (requested %d)\n", algo,
3488                                        template[i].rlen);
3489                                 goto out;
3490                         }
3491                 }
3492
3493                 err = memcmp(result, template[i].result,
3494                              template[i].rlen);
3495                 if (err) {
3496                         printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
3497                                i, algo);
3498                         hexdump(result, template[i].rlen);
3499                         err = -EINVAL;
3500                         goto out;
3501                 }
3502         }
3503
3504 out:
3505         kfree(seed);
3506         return err;
3507 }
3508
3509 static int alg_test_cipher(const struct alg_test_desc *desc,
3510                            const char *driver, u32 type, u32 mask)
3511 {
3512         const struct cipher_test_suite *suite = &desc->suite.cipher;
3513         struct crypto_cipher *tfm;
3514         int err;
3515
3516         tfm = crypto_alloc_cipher(driver, type, mask);
3517         if (IS_ERR(tfm)) {
3518                 printk(KERN_ERR "alg: cipher: Failed to load transform for "
3519                        "%s: %ld\n", driver, PTR_ERR(tfm));
3520                 return PTR_ERR(tfm);
3521         }
3522
3523         err = test_cipher(tfm, ENCRYPT, suite->vecs, suite->count);
3524         if (!err)
3525                 err = test_cipher(tfm, DECRYPT, suite->vecs, suite->count);
3526
3527         crypto_free_cipher(tfm);
3528         return err;
3529 }
3530
3531 static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
3532                          u32 type, u32 mask)
3533 {
3534         struct crypto_comp *comp;
3535         struct crypto_acomp *acomp;
3536         int err;
3537         u32 algo_type = type & CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
3538
3539         if (algo_type == CRYPTO_ALG_TYPE_ACOMPRESS) {
3540                 acomp = crypto_alloc_acomp(driver, type, mask);
3541                 if (IS_ERR(acomp)) {
3542                         pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
3543                                driver, PTR_ERR(acomp));
3544                         return PTR_ERR(acomp);
3545                 }
3546                 err = test_acomp(acomp, desc->suite.comp.comp.vecs,
3547                                  desc->suite.comp.decomp.vecs,
3548                                  desc->suite.comp.comp.count,
3549                                  desc->suite.comp.decomp.count);
3550                 crypto_free_acomp(acomp);
3551         } else {
3552                 comp = crypto_alloc_comp(driver, type, mask);
3553                 if (IS_ERR(comp)) {
3554                         pr_err("alg: comp: Failed to load transform for %s: %ld\n",
3555                                driver, PTR_ERR(comp));
3556                         return PTR_ERR(comp);
3557                 }
3558
3559                 err = test_comp(comp, desc->suite.comp.comp.vecs,
3560                                 desc->suite.comp.decomp.vecs,
3561                                 desc->suite.comp.comp.count,
3562                                 desc->suite.comp.decomp.count);
3563
3564                 crypto_free_comp(comp);
3565         }
3566         return err;
3567 }
3568
3569 static int alg_test_crc32c(const struct alg_test_desc *desc,
3570                            const char *driver, u32 type, u32 mask)
3571 {
3572         struct crypto_shash *tfm;
3573         __le32 val;
3574         int err;
3575
3576         err = alg_test_hash(desc, driver, type, mask);
3577         if (err)
3578                 return err;
3579
3580         tfm = crypto_alloc_shash(driver, type, mask);
3581         if (IS_ERR(tfm)) {
3582                 if (PTR_ERR(tfm) == -ENOENT) {
3583                         /*
3584                          * This crc32c implementation is only available through
3585                          * ahash API, not the shash API, so the remaining part
3586                          * of the test is not applicable to it.
3587                          */
3588                         return 0;
3589                 }
3590                 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
3591                        "%ld\n", driver, PTR_ERR(tfm));
3592                 return PTR_ERR(tfm);
3593         }
3594         driver = crypto_shash_driver_name(tfm);
3595
3596         do {
3597                 SHASH_DESC_ON_STACK(shash, tfm);
3598                 u32 *ctx = (u32 *)shash_desc_ctx(shash);
3599
3600                 shash->tfm = tfm;
3601
3602                 *ctx = 420553207;
3603                 err = crypto_shash_final(shash, (u8 *)&val);
3604                 if (err) {
3605                         printk(KERN_ERR "alg: crc32c: Operation failed for "
3606                                "%s: %d\n", driver, err);
3607                         break;
3608                 }
3609
3610                 if (val != cpu_to_le32(~420553207)) {
3611                         pr_err("alg: crc32c: Test failed for %s: %u\n",
3612                                driver, le32_to_cpu(val));
3613                         err = -EINVAL;
3614                 }
3615         } while (0);
3616
3617         crypto_free_shash(tfm);
3618
3619         return err;
3620 }
3621
3622 static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
3623                           u32 type, u32 mask)
3624 {
3625         struct crypto_rng *rng;
3626         int err;
3627
3628         rng = crypto_alloc_rng(driver, type, mask);
3629         if (IS_ERR(rng)) {
3630                 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
3631                        "%ld\n", driver, PTR_ERR(rng));
3632                 return PTR_ERR(rng);
3633         }
3634
3635         err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
3636
3637         crypto_free_rng(rng);
3638
3639         return err;
3640 }
3641
3642
3643 static int drbg_cavs_test(const struct drbg_testvec *test, int pr,
3644                           const char *driver, u32 type, u32 mask)
3645 {
3646         int ret = -EAGAIN;
3647         struct crypto_rng *drng;
3648         struct drbg_test_data test_data;
3649         struct drbg_string addtl, pers, testentropy;
3650         unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
3651
3652         if (!buf)
3653                 return -ENOMEM;
3654
3655         drng = crypto_alloc_rng(driver, type, mask);
3656         if (IS_ERR(drng)) {
3657                 printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
3658                        "%s\n", driver);
3659                 kfree_sensitive(buf);
3660                 return -ENOMEM;
3661         }
3662
3663         test_data.testentropy = &testentropy;
3664         drbg_string_fill(&testentropy, test->entropy, test->entropylen);
3665         drbg_string_fill(&pers, test->pers, test->perslen);
3666         ret = crypto_drbg_reset_test(drng, &pers, &test_data);
3667         if (ret) {
3668                 printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
3669                 goto outbuf;
3670         }
3671
3672         drbg_string_fill(&addtl, test->addtla, test->addtllen);
3673         if (pr) {
3674                 drbg_string_fill(&testentropy, test->entpra, test->entprlen);
3675                 ret = crypto_drbg_get_bytes_addtl_test(drng,
3676                         buf, test->expectedlen, &addtl, &test_data);
3677         } else {
3678                 ret = crypto_drbg_get_bytes_addtl(drng,
3679                         buf, test->expectedlen, &addtl);
3680         }
3681         if (ret < 0) {
3682                 printk(KERN_ERR "alg: drbg: could not obtain random data for "
3683                        "driver %s\n", driver);
3684                 goto outbuf;
3685         }
3686
3687         drbg_string_fill(&addtl, test->addtlb, test->addtllen);
3688         if (pr) {
3689                 drbg_string_fill(&testentropy, test->entprb, test->entprlen);
3690                 ret = crypto_drbg_get_bytes_addtl_test(drng,
3691                         buf, test->expectedlen, &addtl, &test_data);
3692         } else {
3693                 ret = crypto_drbg_get_bytes_addtl(drng,
3694                         buf, test->expectedlen, &addtl);
3695         }
3696         if (ret < 0) {
3697                 printk(KERN_ERR "alg: drbg: could not obtain random data for "
3698                        "driver %s\n", driver);
3699                 goto outbuf;
3700         }
3701
3702         ret = memcmp(test->expected, buf, test->expectedlen);
3703
3704 outbuf:
3705         crypto_free_rng(drng);
3706         kfree_sensitive(buf);
3707         return ret;
3708 }
3709
3710
3711 static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
3712                          u32 type, u32 mask)
3713 {
3714         int err = 0;
3715         int pr = 0;
3716         int i = 0;
3717         const struct drbg_testvec *template = desc->suite.drbg.vecs;
3718         unsigned int tcount = desc->suite.drbg.count;
3719
3720         if (0 == memcmp(driver, "drbg_pr_", 8))
3721                 pr = 1;
3722
3723         for (i = 0; i < tcount; i++) {
3724                 err = drbg_cavs_test(&template[i], pr, driver, type, mask);
3725                 if (err) {
3726                         printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
3727                                i, driver);
3728                         err = -EINVAL;
3729                         break;
3730                 }
3731         }
3732         return err;
3733
3734 }
3735
3736 static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
3737                        const char *alg)
3738 {
3739         struct kpp_request *req;
3740         void *input_buf = NULL;
3741         void *output_buf = NULL;
3742         void *a_public = NULL;
3743         void *a_ss = NULL;
3744         void *shared_secret = NULL;
3745         struct crypto_wait wait;
3746         unsigned int out_len_max;
3747         int err = -ENOMEM;
3748         struct scatterlist src, dst;
3749
3750         req = kpp_request_alloc(tfm, GFP_KERNEL);
3751         if (!req)
3752                 return err;
3753
3754         crypto_init_wait(&wait);
3755
3756         err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
3757         if (err < 0)
3758                 goto free_req;
3759
3760         out_len_max = crypto_kpp_maxsize(tfm);
3761         output_buf = kzalloc(out_len_max, GFP_KERNEL);
3762         if (!output_buf) {
3763                 err = -ENOMEM;
3764                 goto free_req;
3765         }
3766
3767         /* Use appropriate parameter as base */
3768         kpp_request_set_input(req, NULL, 0);
3769         sg_init_one(&dst, output_buf, out_len_max);
3770         kpp_request_set_output(req, &dst, out_len_max);
3771         kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3772                                  crypto_req_done, &wait);
3773
3774         /* Compute party A's public key */
3775         err = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
3776         if (err) {
3777                 pr_err("alg: %s: Party A: generate public key test failed. err %d\n",
3778                        alg, err);
3779                 goto free_output;
3780         }
3781
3782         if (vec->genkey) {
3783                 /* Save party A's public key */
3784                 a_public = kmemdup(sg_virt(req->dst), out_len_max, GFP_KERNEL);
3785                 if (!a_public) {
3786                         err = -ENOMEM;
3787                         goto free_output;
3788                 }
3789         } else {
3790                 /* Verify calculated public key */
3791                 if (memcmp(vec->expected_a_public, sg_virt(req->dst),
3792                            vec->expected_a_public_size)) {
3793                         pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n",
3794                                alg);
3795                         err = -EINVAL;
3796                         goto free_output;
3797                 }
3798         }
3799
3800         /* Calculate shared secret key by using counter part (b) public key. */
3801         input_buf = kmemdup(vec->b_public, vec->b_public_size, GFP_KERNEL);
3802         if (!input_buf) {
3803                 err = -ENOMEM;
3804                 goto free_output;
3805         }
3806
3807         sg_init_one(&src, input_buf, vec->b_public_size);
3808         sg_init_one(&dst, output_buf, out_len_max);
3809         kpp_request_set_input(req, &src, vec->b_public_size);
3810         kpp_request_set_output(req, &dst, out_len_max);
3811         kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3812                                  crypto_req_done, &wait);
3813         err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait);
3814         if (err) {
3815                 pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n",
3816                        alg, err);
3817                 goto free_all;
3818         }
3819
3820         if (vec->genkey) {
3821                 /* Save the shared secret obtained by party A */
3822                 a_ss = kmemdup(sg_virt(req->dst), vec->expected_ss_size, GFP_KERNEL);
3823                 if (!a_ss) {
3824                         err = -ENOMEM;
3825                         goto free_all;
3826                 }
3827
3828                 /*
3829                  * Calculate party B's shared secret by using party A's
3830                  * public key.
3831                  */
3832                 err = crypto_kpp_set_secret(tfm, vec->b_secret,
3833                                             vec->b_secret_size);
3834                 if (err < 0)
3835                         goto free_all;
3836
3837                 sg_init_one(&src, a_public, vec->expected_a_public_size);
3838                 sg_init_one(&dst, output_buf, out_len_max);
3839                 kpp_request_set_input(req, &src, vec->expected_a_public_size);
3840                 kpp_request_set_output(req, &dst, out_len_max);
3841                 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3842                                          crypto_req_done, &wait);
3843                 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req),
3844                                       &wait);
3845                 if (err) {
3846                         pr_err("alg: %s: Party B: compute shared secret failed. err %d\n",
3847                                alg, err);
3848                         goto free_all;
3849                 }
3850
3851                 shared_secret = a_ss;
3852         } else {
3853                 shared_secret = (void *)vec->expected_ss;
3854         }
3855
3856         /*
3857          * verify shared secret from which the user will derive
3858          * secret key by executing whatever hash it has chosen
3859          */
3860         if (memcmp(shared_secret, sg_virt(req->dst),
3861                    vec->expected_ss_size)) {
3862                 pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
3863                        alg);
3864                 err = -EINVAL;
3865         }
3866
3867 free_all:
3868         kfree(a_ss);
3869         kfree(input_buf);
3870 free_output:
3871         kfree(a_public);
3872         kfree(output_buf);
3873 free_req:
3874         kpp_request_free(req);
3875         return err;
3876 }
3877
3878 static int test_kpp(struct crypto_kpp *tfm, const char *alg,
3879                     const struct kpp_testvec *vecs, unsigned int tcount)
3880 {
3881         int ret, i;
3882
3883         for (i = 0; i < tcount; i++) {
3884                 ret = do_test_kpp(tfm, vecs++, alg);
3885                 if (ret) {
3886                         pr_err("alg: %s: test failed on vector %d, err=%d\n",
3887                                alg, i + 1, ret);
3888                         return ret;
3889                 }
3890         }
3891         return 0;
3892 }
3893
3894 static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
3895                         u32 type, u32 mask)
3896 {
3897         struct crypto_kpp *tfm;
3898         int err = 0;
3899
3900         tfm = crypto_alloc_kpp(driver, type, mask);
3901         if (IS_ERR(tfm)) {
3902                 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
3903                        driver, PTR_ERR(tfm));
3904                 return PTR_ERR(tfm);
3905         }
3906         if (desc->suite.kpp.vecs)
3907                 err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
3908                                desc->suite.kpp.count);
3909
3910         crypto_free_kpp(tfm);
3911         return err;
3912 }
3913
3914 static u8 *test_pack_u32(u8 *dst, u32 val)
3915 {
3916         memcpy(dst, &val, sizeof(val));
3917         return dst + sizeof(val);
3918 }
3919
3920 static int test_akcipher_one(struct crypto_akcipher *tfm,
3921                              const struct akcipher_testvec *vecs)
3922 {
3923         char *xbuf[XBUFSIZE];
3924         struct akcipher_request *req;
3925         void *outbuf_enc = NULL;
3926         void *outbuf_dec = NULL;
3927         struct crypto_wait wait;
3928         unsigned int out_len_max, out_len = 0;
3929         int err = -ENOMEM;
3930         struct scatterlist src, dst, src_tab[3];
3931         const char *m, *c;
3932         unsigned int m_size, c_size;
3933         const char *op;
3934         u8 *key, *ptr;
3935
3936         if (testmgr_alloc_buf(xbuf))
3937                 return err;
3938
3939         req = akcipher_request_alloc(tfm, GFP_KERNEL);
3940         if (!req)
3941                 goto free_xbuf;
3942
3943         crypto_init_wait(&wait);
3944
3945         key = kmalloc(vecs->key_len + sizeof(u32) * 2 + vecs->param_len,
3946                       GFP_KERNEL);
3947         if (!key)
3948                 goto free_req;
3949         memcpy(key, vecs->key, vecs->key_len);
3950         ptr = key + vecs->key_len;
3951         ptr = test_pack_u32(ptr, vecs->algo);
3952         ptr = test_pack_u32(ptr, vecs->param_len);
3953         memcpy(ptr, vecs->params, vecs->param_len);
3954
3955         if (vecs->public_key_vec)
3956                 err = crypto_akcipher_set_pub_key(tfm, key, vecs->key_len);
3957         else
3958                 err = crypto_akcipher_set_priv_key(tfm, key, vecs->key_len);
3959         if (err)
3960                 goto free_key;
3961
3962         /*
3963          * First run test which do not require a private key, such as
3964          * encrypt or verify.
3965          */
3966         err = -ENOMEM;
3967         out_len_max = crypto_akcipher_maxsize(tfm);
3968         outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
3969         if (!outbuf_enc)
3970                 goto free_key;
3971
3972         if (!vecs->siggen_sigver_test) {
3973                 m = vecs->m;
3974                 m_size = vecs->m_size;
3975                 c = vecs->c;
3976                 c_size = vecs->c_size;
3977                 op = "encrypt";
3978         } else {
3979                 /* Swap args so we could keep plaintext (digest)
3980                  * in vecs->m, and cooked signature in vecs->c.
3981                  */
3982                 m = vecs->c; /* signature */
3983                 m_size = vecs->c_size;
3984                 c = vecs->m; /* digest */
3985                 c_size = vecs->m_size;
3986                 op = "verify";
3987         }
3988
3989         err = -E2BIG;
3990         if (WARN_ON(m_size > PAGE_SIZE))
3991                 goto free_all;
3992         memcpy(xbuf[0], m, m_size);
3993
3994         sg_init_table(src_tab, 3);
3995         sg_set_buf(&src_tab[0], xbuf[0], 8);
3996         sg_set_buf(&src_tab[1], xbuf[0] + 8, m_size - 8);
3997         if (vecs->siggen_sigver_test) {
3998                 if (WARN_ON(c_size > PAGE_SIZE))
3999                         goto free_all;
4000                 memcpy(xbuf[1], c, c_size);
4001                 sg_set_buf(&src_tab[2], xbuf[1], c_size);
4002                 akcipher_request_set_crypt(req, src_tab, NULL, m_size, c_size);
4003         } else {
4004                 sg_init_one(&dst, outbuf_enc, out_len_max);
4005                 akcipher_request_set_crypt(req, src_tab, &dst, m_size,
4006                                            out_len_max);
4007         }
4008         akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
4009                                       crypto_req_done, &wait);
4010
4011         err = crypto_wait_req(vecs->siggen_sigver_test ?
4012                               /* Run asymmetric signature verification */
4013                               crypto_akcipher_verify(req) :
4014                               /* Run asymmetric encrypt */
4015                               crypto_akcipher_encrypt(req), &wait);
4016         if (err) {
4017                 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
4018                 goto free_all;
4019         }
4020         if (!vecs->siggen_sigver_test && c) {
4021                 if (req->dst_len != c_size) {
4022                         pr_err("alg: akcipher: %s test failed. Invalid output len\n",
4023                                op);
4024                         err = -EINVAL;
4025                         goto free_all;
4026                 }
4027                 /* verify that encrypted message is equal to expected */
4028                 if (memcmp(c, outbuf_enc, c_size) != 0) {
4029                         pr_err("alg: akcipher: %s test failed. Invalid output\n",
4030                                op);
4031                         hexdump(outbuf_enc, c_size);
4032                         err = -EINVAL;
4033                         goto free_all;
4034                 }
4035         }
4036
4037         /*
4038          * Don't invoke (decrypt or sign) test which require a private key
4039          * for vectors with only a public key.
4040          */
4041         if (vecs->public_key_vec) {
4042                 err = 0;
4043                 goto free_all;
4044         }
4045         outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
4046         if (!outbuf_dec) {
4047                 err = -ENOMEM;
4048                 goto free_all;
4049         }
4050
4051         if (!vecs->siggen_sigver_test && !c) {
4052                 c = outbuf_enc;
4053                 c_size = req->dst_len;
4054         }
4055
4056         err = -E2BIG;
4057         op = vecs->siggen_sigver_test ? "sign" : "decrypt";
4058         if (WARN_ON(c_size > PAGE_SIZE))
4059                 goto free_all;
4060         memcpy(xbuf[0], c, c_size);
4061
4062         sg_init_one(&src, xbuf[0], c_size);
4063         sg_init_one(&dst, outbuf_dec, out_len_max);
4064         crypto_init_wait(&wait);
4065         akcipher_request_set_crypt(req, &src, &dst, c_size, out_len_max);
4066
4067         err = crypto_wait_req(vecs->siggen_sigver_test ?
4068                               /* Run asymmetric signature generation */
4069                               crypto_akcipher_sign(req) :
4070                               /* Run asymmetric decrypt */
4071                               crypto_akcipher_decrypt(req), &wait);
4072         if (err) {
4073                 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
4074                 goto free_all;
4075         }
4076         out_len = req->dst_len;
4077         if (out_len < m_size) {
4078                 pr_err("alg: akcipher: %s test failed. Invalid output len %u\n",
4079                        op, out_len);
4080                 err = -EINVAL;
4081                 goto free_all;
4082         }
4083         /* verify that decrypted message is equal to the original msg */
4084         if (memchr_inv(outbuf_dec, 0, out_len - m_size) ||
4085             memcmp(m, outbuf_dec + out_len - m_size, m_size)) {
4086                 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
4087                 hexdump(outbuf_dec, out_len);
4088                 err = -EINVAL;
4089         }
4090 free_all:
4091         kfree(outbuf_dec);
4092         kfree(outbuf_enc);
4093 free_key:
4094         kfree(key);
4095 free_req:
4096         akcipher_request_free(req);
4097 free_xbuf:
4098         testmgr_free_buf(xbuf);
4099         return err;
4100 }
4101
4102 static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
4103                          const struct akcipher_testvec *vecs,
4104                          unsigned int tcount)
4105 {
4106         const char *algo =
4107                 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
4108         int ret, i;
4109
4110         for (i = 0; i < tcount; i++) {
4111                 ret = test_akcipher_one(tfm, vecs++);
4112                 if (!ret)
4113                         continue;
4114
4115                 pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
4116                        i + 1, algo, ret);
4117                 return ret;
4118         }
4119         return 0;
4120 }
4121
4122 static int alg_test_akcipher(const struct alg_test_desc *desc,
4123                              const char *driver, u32 type, u32 mask)
4124 {
4125         struct crypto_akcipher *tfm;
4126         int err = 0;
4127
4128         tfm = crypto_alloc_akcipher(driver, type, mask);
4129         if (IS_ERR(tfm)) {
4130                 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
4131                        driver, PTR_ERR(tfm));
4132                 return PTR_ERR(tfm);
4133         }
4134         if (desc->suite.akcipher.vecs)
4135                 err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
4136                                     desc->suite.akcipher.count);
4137
4138         crypto_free_akcipher(tfm);
4139         return err;
4140 }
4141
4142 static int alg_test_null(const struct alg_test_desc *desc,
4143                              const char *driver, u32 type, u32 mask)
4144 {
4145         return 0;
4146 }
4147
4148 #define ____VECS(tv)    .vecs = tv, .count = ARRAY_SIZE(tv)
4149 #define __VECS(tv)      { ____VECS(tv) }
4150
4151 /* Please keep this list sorted by algorithm name. */
4152 static const struct alg_test_desc alg_test_descs[] = {
4153         {
4154                 .alg = "adiantum(xchacha12,aes)",
4155                 .generic_driver = "adiantum(xchacha12-generic,aes-generic,nhpoly1305-generic)",
4156                 .test = alg_test_skcipher,
4157                 .suite = {
4158                         .cipher = __VECS(adiantum_xchacha12_aes_tv_template)
4159                 },
4160         }, {
4161                 .alg = "adiantum(xchacha20,aes)",
4162                 .generic_driver = "adiantum(xchacha20-generic,aes-generic,nhpoly1305-generic)",
4163                 .test = alg_test_skcipher,
4164                 .suite = {
4165                         .cipher = __VECS(adiantum_xchacha20_aes_tv_template)
4166                 },
4167         }, {
4168                 .alg = "aegis128",
4169                 .test = alg_test_aead,
4170                 .suite = {
4171                         .aead = __VECS(aegis128_tv_template)
4172                 }
4173         }, {
4174                 .alg = "ansi_cprng",
4175                 .test = alg_test_cprng,
4176                 .suite = {
4177                         .cprng = __VECS(ansi_cprng_aes_tv_template)
4178                 }
4179         }, {
4180                 .alg = "authenc(hmac(md5),ecb(cipher_null))",
4181                 .test = alg_test_aead,
4182                 .suite = {
4183                         .aead = __VECS(hmac_md5_ecb_cipher_null_tv_template)
4184                 }
4185         }, {
4186                 .alg = "authenc(hmac(sha1),cbc(aes))",
4187                 .test = alg_test_aead,
4188                 .fips_allowed = 1,
4189                 .suite = {
4190                         .aead = __VECS(hmac_sha1_aes_cbc_tv_temp)
4191                 }
4192         }, {
4193                 .alg = "authenc(hmac(sha1),cbc(des))",
4194                 .test = alg_test_aead,
4195                 .suite = {
4196                         .aead = __VECS(hmac_sha1_des_cbc_tv_temp)
4197                 }
4198         }, {
4199                 .alg = "authenc(hmac(sha1),cbc(des3_ede))",
4200                 .test = alg_test_aead,
4201                 .fips_allowed = 1,
4202                 .suite = {
4203                         .aead = __VECS(hmac_sha1_des3_ede_cbc_tv_temp)
4204                 }
4205         }, {
4206                 .alg = "authenc(hmac(sha1),ctr(aes))",
4207                 .test = alg_test_null,
4208                 .fips_allowed = 1,
4209         }, {
4210                 .alg = "authenc(hmac(sha1),ecb(cipher_null))",
4211                 .test = alg_test_aead,
4212                 .suite = {
4213                         .aead = __VECS(hmac_sha1_ecb_cipher_null_tv_temp)
4214                 }
4215         }, {
4216                 .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
4217                 .test = alg_test_null,
4218                 .fips_allowed = 1,
4219         }, {
4220                 .alg = "authenc(hmac(sha224),cbc(des))",
4221                 .test = alg_test_aead,
4222                 .suite = {
4223                         .aead = __VECS(hmac_sha224_des_cbc_tv_temp)
4224                 }
4225         }, {
4226                 .alg = "authenc(hmac(sha224),cbc(des3_ede))",
4227                 .test = alg_test_aead,
4228                 .fips_allowed = 1,
4229                 .suite = {
4230                         .aead = __VECS(hmac_sha224_des3_ede_cbc_tv_temp)
4231                 }
4232         }, {
4233                 .alg = "authenc(hmac(sha256),cbc(aes))",
4234                 .test = alg_test_aead,
4235                 .fips_allowed = 1,
4236                 .suite = {
4237                         .aead = __VECS(hmac_sha256_aes_cbc_tv_temp)
4238                 }
4239         }, {
4240                 .alg = "authenc(hmac(sha256),cbc(des))",
4241                 .test = alg_test_aead,
4242                 .suite = {
4243                         .aead = __VECS(hmac_sha256_des_cbc_tv_temp)
4244                 }
4245         }, {
4246                 .alg = "authenc(hmac(sha256),cbc(des3_ede))",
4247                 .test = alg_test_aead,
4248                 .fips_allowed = 1,
4249                 .suite = {
4250                         .aead = __VECS(hmac_sha256_des3_ede_cbc_tv_temp)
4251                 }
4252         }, {
4253                 .alg = "authenc(hmac(sha256),ctr(aes))",
4254                 .test = alg_test_null,
4255                 .fips_allowed = 1,
4256         }, {
4257                 .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
4258                 .test = alg_test_null,
4259                 .fips_allowed = 1,
4260         }, {
4261                 .alg = "authenc(hmac(sha384),cbc(des))",
4262                 .test = alg_test_aead,
4263                 .suite = {
4264                         .aead = __VECS(hmac_sha384_des_cbc_tv_temp)
4265                 }
4266         }, {
4267                 .alg = "authenc(hmac(sha384),cbc(des3_ede))",
4268                 .test = alg_test_aead,
4269                 .fips_allowed = 1,
4270                 .suite = {
4271                         .aead = __VECS(hmac_sha384_des3_ede_cbc_tv_temp)
4272                 }
4273         }, {
4274                 .alg = "authenc(hmac(sha384),ctr(aes))",
4275                 .test = alg_test_null,
4276                 .fips_allowed = 1,
4277         }, {
4278                 .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
4279                 .test = alg_test_null,
4280                 .fips_allowed = 1,
4281         }, {
4282                 .alg = "authenc(hmac(sha512),cbc(aes))",
4283                 .fips_allowed = 1,
4284                 .test = alg_test_aead,
4285                 .suite = {
4286                         .aead = __VECS(hmac_sha512_aes_cbc_tv_temp)
4287                 }
4288         }, {
4289                 .alg = "authenc(hmac(sha512),cbc(des))",
4290                 .test = alg_test_aead,
4291                 .suite = {
4292                         .aead = __VECS(hmac_sha512_des_cbc_tv_temp)
4293                 }
4294         }, {
4295                 .alg = "authenc(hmac(sha512),cbc(des3_ede))",
4296                 .test = alg_test_aead,
4297                 .fips_allowed = 1,
4298                 .suite = {
4299                         .aead = __VECS(hmac_sha512_des3_ede_cbc_tv_temp)
4300                 }
4301         }, {
4302                 .alg = "authenc(hmac(sha512),ctr(aes))",
4303                 .test = alg_test_null,
4304                 .fips_allowed = 1,
4305         }, {
4306                 .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
4307                 .test = alg_test_null,
4308                 .fips_allowed = 1,
4309         }, {
4310                 .alg = "blake2b-160",
4311                 .test = alg_test_hash,
4312                 .fips_allowed = 0,
4313                 .suite = {
4314                         .hash = __VECS(blake2b_160_tv_template)
4315                 }
4316         }, {
4317                 .alg = "blake2b-256",
4318                 .test = alg_test_hash,
4319                 .fips_allowed = 0,
4320                 .suite = {
4321                         .hash = __VECS(blake2b_256_tv_template)
4322                 }
4323         }, {
4324                 .alg = "blake2b-384",
4325                 .test = alg_test_hash,
4326                 .fips_allowed = 0,
4327                 .suite = {
4328                         .hash = __VECS(blake2b_384_tv_template)
4329                 }
4330         }, {
4331                 .alg = "blake2b-512",
4332                 .test = alg_test_hash,
4333                 .fips_allowed = 0,
4334                 .suite = {
4335                         .hash = __VECS(blake2b_512_tv_template)
4336                 }
4337         }, {
4338                 .alg = "blake2s-128",
4339                 .test = alg_test_hash,
4340                 .suite = {
4341                         .hash = __VECS(blakes2s_128_tv_template)
4342                 }
4343         }, {
4344                 .alg = "blake2s-160",
4345                 .test = alg_test_hash,
4346                 .suite = {
4347                         .hash = __VECS(blakes2s_160_tv_template)
4348                 }
4349         }, {
4350                 .alg = "blake2s-224",
4351                 .test = alg_test_hash,
4352                 .suite = {
4353                         .hash = __VECS(blakes2s_224_tv_template)
4354                 }
4355         }, {
4356                 .alg = "blake2s-256",
4357                 .test = alg_test_hash,
4358                 .suite = {
4359                         .hash = __VECS(blakes2s_256_tv_template)
4360                 }
4361         }, {
4362                 .alg = "cbc(aes)",
4363                 .test = alg_test_skcipher,
4364                 .fips_allowed = 1,
4365                 .suite = {
4366                         .cipher = __VECS(aes_cbc_tv_template)
4367                 },
4368         }, {
4369                 .alg = "cbc(anubis)",
4370                 .test = alg_test_skcipher,
4371                 .suite = {
4372                         .cipher = __VECS(anubis_cbc_tv_template)
4373                 },
4374         }, {
4375                 .alg = "cbc(blowfish)",
4376                 .test = alg_test_skcipher,
4377                 .suite = {
4378                         .cipher = __VECS(bf_cbc_tv_template)
4379                 },
4380         }, {
4381                 .alg = "cbc(camellia)",
4382                 .test = alg_test_skcipher,
4383                 .suite = {
4384                         .cipher = __VECS(camellia_cbc_tv_template)
4385                 },
4386         }, {
4387                 .alg = "cbc(cast5)",
4388                 .test = alg_test_skcipher,
4389                 .suite = {
4390                         .cipher = __VECS(cast5_cbc_tv_template)
4391                 },
4392         }, {
4393                 .alg = "cbc(cast6)",
4394                 .test = alg_test_skcipher,
4395                 .suite = {
4396                         .cipher = __VECS(cast6_cbc_tv_template)
4397                 },
4398         }, {
4399                 .alg = "cbc(des)",
4400                 .test = alg_test_skcipher,
4401                 .suite = {
4402                         .cipher = __VECS(des_cbc_tv_template)
4403                 },
4404         }, {
4405                 .alg = "cbc(des3_ede)",
4406                 .test = alg_test_skcipher,
4407                 .fips_allowed = 1,
4408                 .suite = {
4409                         .cipher = __VECS(des3_ede_cbc_tv_template)
4410                 },
4411         }, {
4412                 /* Same as cbc(aes) except the key is stored in
4413                  * hardware secure memory which we reference by index
4414                  */
4415                 .alg = "cbc(paes)",
4416                 .test = alg_test_null,
4417                 .fips_allowed = 1,
4418         }, {
4419                 /* Same as cbc(sm4) except the key is stored in
4420                  * hardware secure memory which we reference by index
4421                  */
4422                 .alg = "cbc(psm4)",
4423                 .test = alg_test_null,
4424         }, {
4425                 .alg = "cbc(serpent)",
4426                 .test = alg_test_skcipher,
4427                 .suite = {
4428                         .cipher = __VECS(serpent_cbc_tv_template)
4429                 },
4430         }, {
4431                 .alg = "cbc(sm4)",
4432                 .test = alg_test_skcipher,
4433                 .suite = {
4434                         .cipher = __VECS(sm4_cbc_tv_template)
4435                 }
4436         }, {
4437                 .alg = "cbc(twofish)",
4438                 .test = alg_test_skcipher,
4439                 .suite = {
4440                         .cipher = __VECS(tf_cbc_tv_template)
4441                 },
4442         }, {
4443 #if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
4444                 .alg = "cbc-paes-s390",
4445                 .fips_allowed = 1,
4446                 .test = alg_test_skcipher,
4447                 .suite = {
4448                         .cipher = __VECS(aes_cbc_tv_template)
4449                 }
4450         }, {
4451 #endif
4452                 .alg = "cbcmac(aes)",
4453                 .fips_allowed = 1,
4454                 .test = alg_test_hash,
4455                 .suite = {
4456                         .hash = __VECS(aes_cbcmac_tv_template)
4457                 }
4458         }, {
4459                 .alg = "ccm(aes)",
4460                 .generic_driver = "ccm_base(ctr(aes-generic),cbcmac(aes-generic))",
4461                 .test = alg_test_aead,
4462                 .fips_allowed = 1,
4463                 .suite = {
4464                         .aead = {
4465                                 ____VECS(aes_ccm_tv_template),
4466                                 .einval_allowed = 1,
4467                         }
4468                 }
4469         }, {
4470                 .alg = "cfb(aes)",
4471                 .test = alg_test_skcipher,
4472                 .fips_allowed = 1,
4473                 .suite = {
4474                         .cipher = __VECS(aes_cfb_tv_template)
4475                 },
4476         }, {
4477                 .alg = "cfb(sm4)",
4478                 .test = alg_test_skcipher,
4479                 .suite = {
4480                         .cipher = __VECS(sm4_cfb_tv_template)
4481                 }
4482         }, {
4483                 .alg = "chacha20",
4484                 .test = alg_test_skcipher,
4485                 .suite = {
4486                         .cipher = __VECS(chacha20_tv_template)
4487                 },
4488         }, {
4489                 .alg = "cmac(aes)",
4490                 .fips_allowed = 1,
4491                 .test = alg_test_hash,
4492                 .suite = {
4493                         .hash = __VECS(aes_cmac128_tv_template)
4494                 }
4495         }, {
4496                 .alg = "cmac(des3_ede)",
4497                 .fips_allowed = 1,
4498                 .test = alg_test_hash,
4499                 .suite = {
4500                         .hash = __VECS(des3_ede_cmac64_tv_template)
4501                 }
4502         }, {
4503                 .alg = "compress_null",
4504                 .test = alg_test_null,
4505         }, {
4506                 .alg = "crc32",
4507                 .test = alg_test_hash,
4508                 .fips_allowed = 1,
4509                 .suite = {
4510                         .hash = __VECS(crc32_tv_template)
4511                 }
4512         }, {
4513                 .alg = "crc32c",
4514                 .test = alg_test_crc32c,
4515                 .fips_allowed = 1,
4516                 .suite = {
4517                         .hash = __VECS(crc32c_tv_template)
4518                 }
4519         }, {
4520                 .alg = "crct10dif",
4521                 .test = alg_test_hash,
4522                 .fips_allowed = 1,
4523                 .suite = {
4524                         .hash = __VECS(crct10dif_tv_template)
4525                 }
4526         }, {
4527                 .alg = "ctr(aes)",
4528                 .test = alg_test_skcipher,
4529                 .fips_allowed = 1,
4530                 .suite = {
4531                         .cipher = __VECS(aes_ctr_tv_template)
4532                 }
4533         }, {
4534                 .alg = "ctr(blowfish)",
4535                 .test = alg_test_skcipher,
4536                 .suite = {
4537                         .cipher = __VECS(bf_ctr_tv_template)
4538                 }
4539         }, {
4540                 .alg = "ctr(camellia)",
4541                 .test = alg_test_skcipher,
4542                 .suite = {
4543                         .cipher = __VECS(camellia_ctr_tv_template)
4544                 }
4545         }, {
4546                 .alg = "ctr(cast5)",
4547                 .test = alg_test_skcipher,
4548                 .suite = {
4549                         .cipher = __VECS(cast5_ctr_tv_template)
4550                 }
4551         }, {
4552                 .alg = "ctr(cast6)",
4553                 .test = alg_test_skcipher,
4554                 .suite = {
4555                         .cipher = __VECS(cast6_ctr_tv_template)
4556                 }
4557         }, {
4558                 .alg = "ctr(des)",
4559                 .test = alg_test_skcipher,
4560                 .suite = {
4561                         .cipher = __VECS(des_ctr_tv_template)
4562                 }
4563         }, {
4564                 .alg = "ctr(des3_ede)",
4565                 .test = alg_test_skcipher,
4566                 .fips_allowed = 1,
4567                 .suite = {
4568                         .cipher = __VECS(des3_ede_ctr_tv_template)
4569                 }
4570         }, {
4571                 /* Same as ctr(aes) except the key is stored in
4572                  * hardware secure memory which we reference by index
4573                  */
4574                 .alg = "ctr(paes)",
4575                 .test = alg_test_null,
4576                 .fips_allowed = 1,
4577         }, {
4578
4579                 /* Same as ctr(sm4) except the key is stored in
4580                  * hardware secure memory which we reference by index
4581                  */
4582                 .alg = "ctr(psm4)",
4583                 .test = alg_test_null,
4584         }, {
4585                 .alg = "ctr(serpent)",
4586                 .test = alg_test_skcipher,
4587                 .suite = {
4588                         .cipher = __VECS(serpent_ctr_tv_template)
4589                 }
4590         }, {
4591                 .alg = "ctr(sm4)",
4592                 .test = alg_test_skcipher,
4593                 .suite = {
4594                         .cipher = __VECS(sm4_ctr_tv_template)
4595                 }
4596         }, {
4597                 .alg = "ctr(twofish)",
4598                 .test = alg_test_skcipher,
4599                 .suite = {
4600                         .cipher = __VECS(tf_ctr_tv_template)
4601                 }
4602         }, {
4603 #if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
4604                 .alg = "ctr-paes-s390",
4605                 .fips_allowed = 1,
4606                 .test = alg_test_skcipher,
4607                 .suite = {
4608                         .cipher = __VECS(aes_ctr_tv_template)
4609                 }
4610         }, {
4611 #endif
4612                 .alg = "cts(cbc(aes))",
4613                 .test = alg_test_skcipher,
4614                 .fips_allowed = 1,
4615                 .suite = {
4616                         .cipher = __VECS(cts_mode_tv_template)
4617                 }
4618         }, {
4619                 /* Same as cts(cbc((aes)) except the key is stored in
4620                  * hardware secure memory which we reference by index
4621                  */
4622                 .alg = "cts(cbc(paes))",
4623                 .test = alg_test_null,
4624                 .fips_allowed = 1,
4625         }, {
4626                 .alg = "curve25519",
4627                 .test = alg_test_kpp,
4628                 .suite = {
4629                         .kpp = __VECS(curve25519_tv_template)
4630                 }
4631         }, {
4632                 .alg = "deflate",
4633                 .test = alg_test_comp,
4634                 .fips_allowed = 1,
4635                 .suite = {
4636                         .comp = {
4637                                 .comp = __VECS(deflate_comp_tv_template),
4638                                 .decomp = __VECS(deflate_decomp_tv_template)
4639                         }
4640                 }
4641         }, {
4642                 .alg = "dh",
4643                 .test = alg_test_kpp,
4644                 .fips_allowed = 1,
4645                 .suite = {
4646                         .kpp = __VECS(dh_tv_template)
4647                 }
4648         }, {
4649                 .alg = "digest_null",
4650                 .test = alg_test_null,
4651         }, {
4652                 .alg = "drbg_nopr_ctr_aes128",
4653                 .test = alg_test_drbg,
4654                 .fips_allowed = 1,
4655                 .suite = {
4656                         .drbg = __VECS(drbg_nopr_ctr_aes128_tv_template)
4657                 }
4658         }, {
4659                 .alg = "drbg_nopr_ctr_aes192",
4660                 .test = alg_test_drbg,
4661                 .fips_allowed = 1,
4662                 .suite = {
4663                         .drbg = __VECS(drbg_nopr_ctr_aes192_tv_template)
4664                 }
4665         }, {
4666                 .alg = "drbg_nopr_ctr_aes256",
4667                 .test = alg_test_drbg,
4668                 .fips_allowed = 1,
4669                 .suite = {
4670                         .drbg = __VECS(drbg_nopr_ctr_aes256_tv_template)
4671                 }
4672         }, {
4673                 /*
4674                  * There is no need to specifically test the DRBG with every
4675                  * backend cipher -- covered by drbg_nopr_hmac_sha256 test
4676                  */
4677                 .alg = "drbg_nopr_hmac_sha1",
4678                 .fips_allowed = 1,
4679                 .test = alg_test_null,
4680         }, {
4681                 .alg = "drbg_nopr_hmac_sha256",
4682                 .test = alg_test_drbg,
4683                 .fips_allowed = 1,
4684                 .suite = {
4685                         .drbg = __VECS(drbg_nopr_hmac_sha256_tv_template)
4686                 }
4687         }, {
4688                 /* covered by drbg_nopr_hmac_sha256 test */
4689                 .alg = "drbg_nopr_hmac_sha384",
4690                 .fips_allowed = 1,
4691                 .test = alg_test_null,
4692         }, {
4693                 .alg = "drbg_nopr_hmac_sha512",
4694                 .test = alg_test_null,
4695                 .fips_allowed = 1,
4696         }, {
4697                 .alg = "drbg_nopr_sha1",
4698                 .fips_allowed = 1,
4699                 .test = alg_test_null,
4700         }, {
4701                 .alg = "drbg_nopr_sha256",
4702                 .test = alg_test_drbg,
4703                 .fips_allowed = 1,
4704                 .suite = {
4705                         .drbg = __VECS(drbg_nopr_sha256_tv_template)
4706                 }
4707         }, {
4708                 /* covered by drbg_nopr_sha256 test */
4709                 .alg = "drbg_nopr_sha384",
4710                 .fips_allowed = 1,
4711                 .test = alg_test_null,
4712         }, {
4713                 .alg = "drbg_nopr_sha512",
4714                 .fips_allowed = 1,
4715                 .test = alg_test_null,
4716         }, {
4717                 .alg = "drbg_pr_ctr_aes128",
4718                 .test = alg_test_drbg,
4719                 .fips_allowed = 1,
4720                 .suite = {
4721                         .drbg = __VECS(drbg_pr_ctr_aes128_tv_template)
4722                 }
4723         }, {
4724                 /* covered by drbg_pr_ctr_aes128 test */
4725                 .alg = "drbg_pr_ctr_aes192",
4726                 .fips_allowed = 1,
4727                 .test = alg_test_null,
4728         }, {
4729                 .alg = "drbg_pr_ctr_aes256",
4730                 .fips_allowed = 1,
4731                 .test = alg_test_null,
4732         }, {
4733                 .alg = "drbg_pr_hmac_sha1",
4734                 .fips_allowed = 1,
4735                 .test = alg_test_null,
4736         }, {
4737                 .alg = "drbg_pr_hmac_sha256",
4738                 .test = alg_test_drbg,
4739                 .fips_allowed = 1,
4740                 .suite = {
4741                         .drbg = __VECS(drbg_pr_hmac_sha256_tv_template)
4742                 }
4743         }, {
4744                 /* covered by drbg_pr_hmac_sha256 test */
4745                 .alg = "drbg_pr_hmac_sha384",
4746                 .fips_allowed = 1,
4747                 .test = alg_test_null,
4748         }, {
4749                 .alg = "drbg_pr_hmac_sha512",
4750                 .test = alg_test_null,
4751                 .fips_allowed = 1,
4752         }, {
4753                 .alg = "drbg_pr_sha1",
4754                 .fips_allowed = 1,
4755                 .test = alg_test_null,
4756         }, {
4757                 .alg = "drbg_pr_sha256",
4758                 .test = alg_test_drbg,
4759                 .fips_allowed = 1,
4760                 .suite = {
4761                         .drbg = __VECS(drbg_pr_sha256_tv_template)
4762                 }
4763         }, {
4764                 /* covered by drbg_pr_sha256 test */
4765                 .alg = "drbg_pr_sha384",
4766                 .fips_allowed = 1,
4767                 .test = alg_test_null,
4768         }, {
4769                 .alg = "drbg_pr_sha512",
4770                 .fips_allowed = 1,
4771                 .test = alg_test_null,
4772         }, {
4773                 .alg = "ecb(aes)",
4774                 .test = alg_test_skcipher,
4775                 .fips_allowed = 1,
4776                 .suite = {
4777                         .cipher = __VECS(aes_tv_template)
4778                 }
4779         }, {
4780                 .alg = "ecb(anubis)",
4781                 .test = alg_test_skcipher,
4782                 .suite = {
4783                         .cipher = __VECS(anubis_tv_template)
4784                 }
4785         }, {
4786                 .alg = "ecb(arc4)",
4787                 .generic_driver = "ecb(arc4)-generic",
4788                 .test = alg_test_skcipher,
4789                 .suite = {
4790                         .cipher = __VECS(arc4_tv_template)
4791                 }
4792         }, {
4793                 .alg = "ecb(blowfish)",
4794                 .test = alg_test_skcipher,
4795                 .suite = {
4796                         .cipher = __VECS(bf_tv_template)
4797                 }
4798         }, {
4799                 .alg = "ecb(camellia)",
4800                 .test = alg_test_skcipher,
4801                 .suite = {
4802                         .cipher = __VECS(camellia_tv_template)
4803                 }
4804         }, {
4805                 .alg = "ecb(cast5)",
4806                 .test = alg_test_skcipher,
4807                 .suite = {
4808                         .cipher = __VECS(cast5_tv_template)
4809                 }
4810         }, {
4811                 .alg = "ecb(cast6)",
4812                 .test = alg_test_skcipher,
4813                 .suite = {
4814                         .cipher = __VECS(cast6_tv_template)
4815                 }
4816         }, {
4817                 .alg = "ecb(cipher_null)",
4818                 .test = alg_test_null,
4819                 .fips_allowed = 1,
4820         }, {
4821                 .alg = "ecb(des)",
4822                 .test = alg_test_skcipher,
4823                 .suite = {
4824                         .cipher = __VECS(des_tv_template)
4825                 }
4826         }, {
4827                 .alg = "ecb(des3_ede)",
4828                 .test = alg_test_skcipher,
4829                 .fips_allowed = 1,
4830                 .suite = {
4831                         .cipher = __VECS(des3_ede_tv_template)
4832                 }
4833         }, {
4834                 .alg = "ecb(fcrypt)",
4835                 .test = alg_test_skcipher,
4836                 .suite = {
4837                         .cipher = {
4838                                 .vecs = fcrypt_pcbc_tv_template,
4839                                 .count = 1
4840                         }
4841                 }
4842         }, {
4843                 .alg = "ecb(khazad)",
4844                 .test = alg_test_skcipher,
4845                 .suite = {
4846                         .cipher = __VECS(khazad_tv_template)
4847                 }
4848         }, {
4849                 /* Same as ecb(aes) except the key is stored in
4850                  * hardware secure memory which we reference by index
4851                  */
4852                 .alg = "ecb(paes)",
4853                 .test = alg_test_null,
4854                 .fips_allowed = 1,
4855         }, {
4856                 .alg = "ecb(seed)",
4857                 .test = alg_test_skcipher,
4858                 .suite = {
4859                         .cipher = __VECS(seed_tv_template)
4860                 }
4861         }, {
4862                 .alg = "ecb(serpent)",
4863                 .test = alg_test_skcipher,
4864                 .suite = {
4865                         .cipher = __VECS(serpent_tv_template)
4866                 }
4867         }, {
4868                 .alg = "ecb(sm4)",
4869                 .test = alg_test_skcipher,
4870                 .suite = {
4871                         .cipher = __VECS(sm4_tv_template)
4872                 }
4873         }, {
4874                 .alg = "ecb(tea)",
4875                 .test = alg_test_skcipher,
4876                 .suite = {
4877                         .cipher = __VECS(tea_tv_template)
4878                 }
4879         }, {
4880                 .alg = "ecb(tnepres)",
4881                 .test = alg_test_skcipher,
4882                 .suite = {
4883                         .cipher = __VECS(tnepres_tv_template)
4884                 }
4885         }, {
4886                 .alg = "ecb(twofish)",
4887                 .test = alg_test_skcipher,
4888                 .suite = {
4889                         .cipher = __VECS(tf_tv_template)
4890                 }
4891         }, {
4892                 .alg = "ecb(xeta)",
4893                 .test = alg_test_skcipher,
4894                 .suite = {
4895                         .cipher = __VECS(xeta_tv_template)
4896                 }
4897         }, {
4898                 .alg = "ecb(xtea)",
4899                 .test = alg_test_skcipher,
4900                 .suite = {
4901                         .cipher = __VECS(xtea_tv_template)
4902                 }
4903         }, {
4904 #if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
4905                 .alg = "ecb-paes-s390",
4906                 .fips_allowed = 1,
4907                 .test = alg_test_skcipher,
4908                 .suite = {
4909                         .cipher = __VECS(aes_tv_template)
4910                 }
4911         }, {
4912 #endif
4913                 .alg = "ecdh",
4914                 .test = alg_test_kpp,
4915                 .fips_allowed = 1,
4916                 .suite = {
4917                         .kpp = __VECS(ecdh_tv_template)
4918                 }
4919         }, {
4920                 .alg = "ecrdsa",
4921                 .test = alg_test_akcipher,
4922                 .suite = {
4923                         .akcipher = __VECS(ecrdsa_tv_template)
4924                 }
4925         }, {
4926                 .alg = "essiv(authenc(hmac(sha256),cbc(aes)),sha256)",
4927                 .test = alg_test_aead,
4928                 .fips_allowed = 1,
4929                 .suite = {
4930                         .aead = __VECS(essiv_hmac_sha256_aes_cbc_tv_temp)
4931                 }
4932         }, {
4933                 .alg = "essiv(cbc(aes),sha256)",
4934                 .test = alg_test_skcipher,
4935                 .fips_allowed = 1,
4936                 .suite = {
4937                         .cipher = __VECS(essiv_aes_cbc_tv_template)
4938                 }
4939         }, {
4940                 .alg = "gcm(aes)",
4941                 .generic_driver = "gcm_base(ctr(aes-generic),ghash-generic)",
4942                 .test = alg_test_aead,
4943                 .fips_allowed = 1,
4944                 .suite = {
4945                         .aead = __VECS(aes_gcm_tv_template)
4946                 }
4947         }, {
4948                 .alg = "ghash",
4949                 .test = alg_test_hash,
4950                 .fips_allowed = 1,
4951                 .suite = {
4952                         .hash = __VECS(ghash_tv_template)
4953                 }
4954         }, {
4955                 .alg = "hmac(md5)",
4956                 .test = alg_test_hash,
4957                 .suite = {
4958                         .hash = __VECS(hmac_md5_tv_template)
4959                 }
4960         }, {
4961                 .alg = "hmac(rmd128)",
4962                 .test = alg_test_hash,
4963                 .suite = {
4964                         .hash = __VECS(hmac_rmd128_tv_template)
4965                 }
4966         }, {
4967                 .alg = "hmac(rmd160)",
4968                 .test = alg_test_hash,
4969                 .suite = {
4970                         .hash = __VECS(hmac_rmd160_tv_template)
4971                 }
4972         }, {
4973                 .alg = "hmac(sha1)",
4974                 .test = alg_test_hash,
4975                 .fips_allowed = 1,
4976                 .suite = {
4977                         .hash = __VECS(hmac_sha1_tv_template)
4978                 }
4979         }, {
4980                 .alg = "hmac(sha224)",
4981                 .test = alg_test_hash,
4982                 .fips_allowed = 1,
4983                 .suite = {
4984                         .hash = __VECS(hmac_sha224_tv_template)
4985                 }
4986         }, {
4987                 .alg = "hmac(sha256)",
4988                 .test = alg_test_hash,
4989                 .fips_allowed = 1,
4990                 .suite = {
4991                         .hash = __VECS(hmac_sha256_tv_template)
4992                 }
4993         }, {
4994                 .alg = "hmac(sha3-224)",
4995                 .test = alg_test_hash,
4996                 .fips_allowed = 1,
4997                 .suite = {
4998                         .hash = __VECS(hmac_sha3_224_tv_template)
4999                 }
5000         }, {
5001                 .alg = "hmac(sha3-256)",
5002                 .test = alg_test_hash,
5003                 .fips_allowed = 1,
5004                 .suite = {
5005                         .hash = __VECS(hmac_sha3_256_tv_template)
5006                 }
5007         }, {
5008                 .alg = "hmac(sha3-384)",
5009                 .test = alg_test_hash,
5010                 .fips_allowed = 1,
5011                 .suite = {
5012                         .hash = __VECS(hmac_sha3_384_tv_template)
5013                 }
5014         }, {
5015                 .alg = "hmac(sha3-512)",
5016                 .test = alg_test_hash,
5017                 .fips_allowed = 1,
5018                 .suite = {
5019                         .hash = __VECS(hmac_sha3_512_tv_template)
5020                 }
5021         }, {
5022                 .alg = "hmac(sha384)",
5023                 .test = alg_test_hash,
5024                 .fips_allowed = 1,
5025                 .suite = {
5026                         .hash = __VECS(hmac_sha384_tv_template)
5027                 }
5028         }, {
5029                 .alg = "hmac(sha512)",
5030                 .test = alg_test_hash,
5031                 .fips_allowed = 1,
5032                 .suite = {
5033                         .hash = __VECS(hmac_sha512_tv_template)
5034                 }
5035         }, {
5036                 .alg = "hmac(sm3)",
5037                 .test = alg_test_hash,
5038                 .suite = {
5039                         .hash = __VECS(hmac_sm3_tv_template)
5040                 }
5041         }, {
5042                 .alg = "hmac(streebog256)",
5043                 .test = alg_test_hash,
5044                 .suite = {
5045                         .hash = __VECS(hmac_streebog256_tv_template)
5046                 }
5047         }, {
5048                 .alg = "hmac(streebog512)",
5049                 .test = alg_test_hash,
5050                 .suite = {
5051                         .hash = __VECS(hmac_streebog512_tv_template)
5052                 }
5053         }, {
5054                 .alg = "jitterentropy_rng",
5055                 .fips_allowed = 1,
5056                 .test = alg_test_null,
5057         }, {
5058                 .alg = "kw(aes)",
5059                 .test = alg_test_skcipher,
5060                 .fips_allowed = 1,
5061                 .suite = {
5062                         .cipher = __VECS(aes_kw_tv_template)
5063                 }
5064         }, {
5065                 .alg = "lrw(aes)",
5066                 .generic_driver = "lrw(ecb(aes-generic))",
5067                 .test = alg_test_skcipher,
5068                 .suite = {
5069                         .cipher = __VECS(aes_lrw_tv_template)
5070                 }
5071         }, {
5072                 .alg = "lrw(camellia)",
5073                 .generic_driver = "lrw(ecb(camellia-generic))",
5074                 .test = alg_test_skcipher,
5075                 .suite = {
5076                         .cipher = __VECS(camellia_lrw_tv_template)
5077                 }
5078         }, {
5079                 .alg = "lrw(cast6)",
5080                 .generic_driver = "lrw(ecb(cast6-generic))",
5081                 .test = alg_test_skcipher,
5082                 .suite = {
5083                         .cipher = __VECS(cast6_lrw_tv_template)
5084                 }
5085         }, {
5086                 .alg = "lrw(serpent)",
5087                 .generic_driver = "lrw(ecb(serpent-generic))",
5088                 .test = alg_test_skcipher,
5089                 .suite = {
5090                         .cipher = __VECS(serpent_lrw_tv_template)
5091                 }
5092         }, {
5093                 .alg = "lrw(twofish)",
5094                 .generic_driver = "lrw(ecb(twofish-generic))",
5095                 .test = alg_test_skcipher,
5096                 .suite = {
5097                         .cipher = __VECS(tf_lrw_tv_template)
5098                 }
5099         }, {
5100                 .alg = "lz4",
5101                 .test = alg_test_comp,
5102                 .fips_allowed = 1,
5103                 .suite = {
5104                         .comp = {
5105                                 .comp = __VECS(lz4_comp_tv_template),
5106                                 .decomp = __VECS(lz4_decomp_tv_template)
5107                         }
5108                 }
5109         }, {
5110                 .alg = "lz4hc",
5111                 .test = alg_test_comp,
5112                 .fips_allowed = 1,
5113                 .suite = {
5114                         .comp = {
5115                                 .comp = __VECS(lz4hc_comp_tv_template),
5116                                 .decomp = __VECS(lz4hc_decomp_tv_template)
5117                         }
5118                 }
5119         }, {
5120                 .alg = "lzo",
5121                 .test = alg_test_comp,
5122                 .fips_allowed = 1,
5123                 .suite = {
5124                         .comp = {
5125                                 .comp = __VECS(lzo_comp_tv_template),
5126                                 .decomp = __VECS(lzo_decomp_tv_template)
5127                         }
5128                 }
5129         }, {
5130                 .alg = "lzo-rle",
5131                 .test = alg_test_comp,
5132                 .fips_allowed = 1,
5133                 .suite = {
5134                         .comp = {
5135                                 .comp = __VECS(lzorle_comp_tv_template),
5136                                 .decomp = __VECS(lzorle_decomp_tv_template)
5137                         }
5138                 }
5139         }, {
5140                 .alg = "md4",
5141                 .test = alg_test_hash,
5142                 .suite = {
5143                         .hash = __VECS(md4_tv_template)
5144                 }
5145         }, {
5146                 .alg = "md5",
5147                 .test = alg_test_hash,
5148                 .suite = {
5149                         .hash = __VECS(md5_tv_template)
5150                 }
5151         }, {
5152                 .alg = "michael_mic",
5153                 .test = alg_test_hash,
5154                 .suite = {
5155                         .hash = __VECS(michael_mic_tv_template)
5156                 }
5157         }, {
5158                 .alg = "nhpoly1305",
5159                 .test = alg_test_hash,
5160                 .suite = {
5161                         .hash = __VECS(nhpoly1305_tv_template)
5162                 }
5163         }, {
5164                 .alg = "ofb(aes)",
5165                 .test = alg_test_skcipher,
5166                 .fips_allowed = 1,
5167                 .suite = {
5168                         .cipher = __VECS(aes_ofb_tv_template)
5169                 }
5170         }, {
5171                 /* Same as ofb(aes) except the key is stored in
5172                  * hardware secure memory which we reference by index
5173                  */
5174                 .alg = "ofb(paes)",
5175                 .test = alg_test_null,
5176                 .fips_allowed = 1,
5177         }, {
5178                 .alg = "ofb(sm4)",
5179                 .test = alg_test_skcipher,
5180                 .suite = {
5181                         .cipher = __VECS(sm4_ofb_tv_template)
5182                 }
5183         }, {
5184                 .alg = "pcbc(fcrypt)",
5185                 .test = alg_test_skcipher,
5186                 .suite = {
5187                         .cipher = __VECS(fcrypt_pcbc_tv_template)
5188                 }
5189         }, {
5190                 .alg = "pkcs1pad(rsa,sha224)",
5191                 .test = alg_test_null,
5192                 .fips_allowed = 1,
5193         }, {
5194                 .alg = "pkcs1pad(rsa,sha256)",
5195                 .test = alg_test_akcipher,
5196                 .fips_allowed = 1,
5197                 .suite = {
5198                         .akcipher = __VECS(pkcs1pad_rsa_tv_template)
5199                 }
5200         }, {
5201                 .alg = "pkcs1pad(rsa,sha384)",
5202                 .test = alg_test_null,
5203                 .fips_allowed = 1,
5204         }, {
5205                 .alg = "pkcs1pad(rsa,sha512)",
5206                 .test = alg_test_null,
5207                 .fips_allowed = 1,
5208         }, {
5209                 .alg = "poly1305",
5210                 .test = alg_test_hash,
5211                 .suite = {
5212                         .hash = __VECS(poly1305_tv_template)
5213                 }
5214         }, {
5215                 .alg = "rfc3686(ctr(aes))",
5216                 .test = alg_test_skcipher,
5217                 .fips_allowed = 1,
5218                 .suite = {
5219                         .cipher = __VECS(aes_ctr_rfc3686_tv_template)
5220                 }
5221         }, {
5222                 .alg = "rfc3686(ctr(sm4))",
5223                 .test = alg_test_skcipher,
5224                 .suite = {
5225                         .cipher = __VECS(sm4_ctr_rfc3686_tv_template)
5226                 }
5227         }, {
5228                 .alg = "rfc4106(gcm(aes))",
5229                 .generic_driver = "rfc4106(gcm_base(ctr(aes-generic),ghash-generic))",
5230                 .test = alg_test_aead,
5231                 .fips_allowed = 1,
5232                 .suite = {
5233                         .aead = {
5234                                 ____VECS(aes_gcm_rfc4106_tv_template),
5235                                 .einval_allowed = 1,
5236                                 .aad_iv = 1,
5237                         }
5238                 }
5239         }, {
5240                 .alg = "rfc4309(ccm(aes))",
5241                 .generic_driver = "rfc4309(ccm_base(ctr(aes-generic),cbcmac(aes-generic)))",
5242                 .test = alg_test_aead,
5243                 .fips_allowed = 1,
5244                 .suite = {
5245                         .aead = {
5246                                 ____VECS(aes_ccm_rfc4309_tv_template),
5247                                 .einval_allowed = 1,
5248                                 .aad_iv = 1,
5249                         }
5250                 }
5251         }, {
5252                 .alg = "rfc4543(gcm(aes))",
5253                 .generic_driver = "rfc4543(gcm_base(ctr(aes-generic),ghash-generic))",
5254                 .test = alg_test_aead,
5255                 .suite = {
5256                         .aead = {
5257                                 ____VECS(aes_gcm_rfc4543_tv_template),
5258                                 .einval_allowed = 1,
5259                                 .aad_iv = 1,
5260                         }
5261                 }
5262         }, {
5263                 .alg = "rfc7539(chacha20,poly1305)",
5264                 .test = alg_test_aead,
5265                 .suite = {
5266                         .aead = __VECS(rfc7539_tv_template)
5267                 }
5268         }, {
5269                 .alg = "rfc7539esp(chacha20,poly1305)",
5270                 .test = alg_test_aead,
5271                 .suite = {
5272                         .aead = {
5273                                 ____VECS(rfc7539esp_tv_template),
5274                                 .einval_allowed = 1,
5275                                 .aad_iv = 1,
5276                         }
5277                 }
5278         }, {
5279                 .alg = "rmd128",
5280                 .test = alg_test_hash,
5281                 .suite = {
5282                         .hash = __VECS(rmd128_tv_template)
5283                 }
5284         }, {
5285                 .alg = "rmd160",
5286                 .test = alg_test_hash,
5287                 .suite = {
5288                         .hash = __VECS(rmd160_tv_template)
5289                 }
5290         }, {
5291                 .alg = "rmd256",
5292                 .test = alg_test_hash,
5293                 .suite = {
5294                         .hash = __VECS(rmd256_tv_template)
5295                 }
5296         }, {
5297                 .alg = "rmd320",
5298                 .test = alg_test_hash,
5299                 .suite = {
5300                         .hash = __VECS(rmd320_tv_template)
5301                 }
5302         }, {
5303                 .alg = "rsa",
5304                 .test = alg_test_akcipher,
5305                 .fips_allowed = 1,
5306                 .suite = {
5307                         .akcipher = __VECS(rsa_tv_template)
5308                 }
5309         }, {
5310                 .alg = "salsa20",
5311                 .test = alg_test_skcipher,
5312                 .suite = {
5313                         .cipher = __VECS(salsa20_stream_tv_template)
5314                 }
5315         }, {
5316                 .alg = "sha1",
5317                 .test = alg_test_hash,
5318                 .fips_allowed = 1,
5319                 .suite = {
5320                         .hash = __VECS(sha1_tv_template)
5321                 }
5322         }, {
5323                 .alg = "sha224",
5324                 .test = alg_test_hash,
5325                 .fips_allowed = 1,
5326                 .suite = {
5327                         .hash = __VECS(sha224_tv_template)
5328                 }
5329         }, {
5330                 .alg = "sha256",
5331                 .test = alg_test_hash,
5332                 .fips_allowed = 1,
5333                 .suite = {
5334                         .hash = __VECS(sha256_tv_template)
5335                 }
5336         }, {
5337                 .alg = "sha3-224",
5338                 .test = alg_test_hash,
5339                 .fips_allowed = 1,
5340                 .suite = {
5341                         .hash = __VECS(sha3_224_tv_template)
5342                 }
5343         }, {
5344                 .alg = "sha3-256",
5345                 .test = alg_test_hash,
5346                 .fips_allowed = 1,
5347                 .suite = {
5348                         .hash = __VECS(sha3_256_tv_template)
5349                 }
5350         }, {
5351                 .alg = "sha3-384",
5352                 .test = alg_test_hash,
5353                 .fips_allowed = 1,
5354                 .suite = {
5355                         .hash = __VECS(sha3_384_tv_template)
5356                 }
5357         }, {
5358                 .alg = "sha3-512",
5359                 .test = alg_test_hash,
5360                 .fips_allowed = 1,
5361                 .suite = {
5362                         .hash = __VECS(sha3_512_tv_template)
5363                 }
5364         }, {
5365                 .alg = "sha384",
5366                 .test = alg_test_hash,
5367                 .fips_allowed = 1,
5368                 .suite = {
5369                         .hash = __VECS(sha384_tv_template)
5370                 }
5371         }, {
5372                 .alg = "sha512",
5373                 .test = alg_test_hash,
5374                 .fips_allowed = 1,
5375                 .suite = {
5376                         .hash = __VECS(sha512_tv_template)
5377                 }
5378         }, {
5379                 .alg = "sm2",
5380                 .test = alg_test_akcipher,
5381                 .suite = {
5382                         .akcipher = __VECS(sm2_tv_template)
5383                 }
5384         }, {
5385                 .alg = "sm3",
5386                 .test = alg_test_hash,
5387                 .suite = {
5388                         .hash = __VECS(sm3_tv_template)
5389                 }
5390         }, {
5391                 .alg = "streebog256",
5392                 .test = alg_test_hash,
5393                 .suite = {
5394                         .hash = __VECS(streebog256_tv_template)
5395                 }
5396         }, {
5397                 .alg = "streebog512",
5398                 .test = alg_test_hash,
5399                 .suite = {
5400                         .hash = __VECS(streebog512_tv_template)
5401                 }
5402         }, {
5403                 .alg = "tgr128",
5404                 .test = alg_test_hash,
5405                 .suite = {
5406                         .hash = __VECS(tgr128_tv_template)
5407                 }
5408         }, {
5409                 .alg = "tgr160",
5410                 .test = alg_test_hash,
5411                 .suite = {
5412                         .hash = __VECS(tgr160_tv_template)
5413                 }
5414         }, {
5415                 .alg = "tgr192",
5416                 .test = alg_test_hash,
5417                 .suite = {
5418                         .hash = __VECS(tgr192_tv_template)
5419                 }
5420         }, {
5421                 .alg = "vmac64(aes)",
5422                 .test = alg_test_hash,
5423                 .suite = {
5424                         .hash = __VECS(vmac64_aes_tv_template)
5425                 }
5426         }, {
5427                 .alg = "wp256",
5428                 .test = alg_test_hash,
5429                 .suite = {
5430                         .hash = __VECS(wp256_tv_template)
5431                 }
5432         }, {
5433                 .alg = "wp384",
5434                 .test = alg_test_hash,
5435                 .suite = {
5436                         .hash = __VECS(wp384_tv_template)
5437                 }
5438         }, {
5439                 .alg = "wp512",
5440                 .test = alg_test_hash,
5441                 .suite = {
5442                         .hash = __VECS(wp512_tv_template)
5443                 }
5444         }, {
5445                 .alg = "xcbc(aes)",
5446                 .test = alg_test_hash,
5447                 .suite = {
5448                         .hash = __VECS(aes_xcbc128_tv_template)
5449                 }
5450         }, {
5451                 .alg = "xchacha12",
5452                 .test = alg_test_skcipher,
5453                 .suite = {
5454                         .cipher = __VECS(xchacha12_tv_template)
5455                 },
5456         }, {
5457                 .alg = "xchacha20",
5458                 .test = alg_test_skcipher,
5459                 .suite = {
5460                         .cipher = __VECS(xchacha20_tv_template)
5461                 },
5462         }, {
5463                 .alg = "xts(aes)",
5464                 .generic_driver = "xts(ecb(aes-generic))",
5465                 .test = alg_test_skcipher,
5466                 .fips_allowed = 1,
5467                 .suite = {
5468                         .cipher = __VECS(aes_xts_tv_template)
5469                 }
5470         }, {
5471                 .alg = "xts(camellia)",
5472                 .generic_driver = "xts(ecb(camellia-generic))",
5473                 .test = alg_test_skcipher,
5474                 .suite = {
5475                         .cipher = __VECS(camellia_xts_tv_template)
5476                 }
5477         }, {
5478                 .alg = "xts(cast6)",
5479                 .generic_driver = "xts(ecb(cast6-generic))",
5480                 .test = alg_test_skcipher,
5481                 .suite = {
5482                         .cipher = __VECS(cast6_xts_tv_template)
5483                 }
5484         }, {
5485                 /* Same as xts(aes) except the key is stored in
5486                  * hardware secure memory which we reference by index
5487                  */
5488                 .alg = "xts(paes)",
5489                 .test = alg_test_null,
5490                 .fips_allowed = 1,
5491         }, {
5492                 .alg = "xts(serpent)",
5493                 .generic_driver = "xts(ecb(serpent-generic))",
5494                 .test = alg_test_skcipher,
5495                 .suite = {
5496                         .cipher = __VECS(serpent_xts_tv_template)
5497                 }
5498         }, {
5499                 .alg = "xts(twofish)",
5500                 .generic_driver = "xts(ecb(twofish-generic))",
5501                 .test = alg_test_skcipher,
5502                 .suite = {
5503                         .cipher = __VECS(tf_xts_tv_template)
5504                 }
5505         }, {
5506 #if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
5507                 .alg = "xts-paes-s390",
5508                 .fips_allowed = 1,
5509                 .test = alg_test_skcipher,
5510                 .suite = {
5511                         .cipher = __VECS(aes_xts_tv_template)
5512                 }
5513         }, {
5514 #endif
5515                 .alg = "xts4096(paes)",
5516                 .test = alg_test_null,
5517                 .fips_allowed = 1,
5518         }, {
5519                 .alg = "xts512(paes)",
5520                 .test = alg_test_null,
5521                 .fips_allowed = 1,
5522         }, {
5523                 .alg = "xxhash64",
5524                 .test = alg_test_hash,
5525                 .fips_allowed = 1,
5526                 .suite = {
5527                         .hash = __VECS(xxhash64_tv_template)
5528                 }
5529         }, {
5530                 .alg = "zlib-deflate",
5531                 .test = alg_test_comp,
5532                 .fips_allowed = 1,
5533                 .suite = {
5534                         .comp = {
5535                                 .comp = __VECS(zlib_deflate_comp_tv_template),
5536                                 .decomp = __VECS(zlib_deflate_decomp_tv_template)
5537                         }
5538                 }
5539         }, {
5540                 .alg = "zstd",
5541                 .test = alg_test_comp,
5542                 .fips_allowed = 1,
5543                 .suite = {
5544                         .comp = {
5545                                 .comp = __VECS(zstd_comp_tv_template),
5546                                 .decomp = __VECS(zstd_decomp_tv_template)
5547                         }
5548                 }
5549         }
5550 };
5551
5552 static void alg_check_test_descs_order(void)
5553 {
5554         int i;
5555
5556         for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
5557                 int diff = strcmp(alg_test_descs[i - 1].alg,
5558                                   alg_test_descs[i].alg);
5559
5560                 if (WARN_ON(diff > 0)) {
5561                         pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
5562                                 alg_test_descs[i - 1].alg,
5563                                 alg_test_descs[i].alg);
5564                 }
5565
5566                 if (WARN_ON(diff == 0)) {
5567                         pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
5568                                 alg_test_descs[i].alg);
5569                 }
5570         }
5571 }
5572
5573 static void alg_check_testvec_configs(void)
5574 {
5575         int i;
5576
5577         for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++)
5578                 WARN_ON(!valid_testvec_config(
5579                                 &default_cipher_testvec_configs[i]));
5580
5581         for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++)
5582                 WARN_ON(!valid_testvec_config(
5583                                 &default_hash_testvec_configs[i]));
5584 }
5585
5586 static void testmgr_onetime_init(void)
5587 {
5588         alg_check_test_descs_order();
5589         alg_check_testvec_configs();
5590
5591 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
5592         pr_warn("alg: extra crypto tests enabled.  This is intended for developer use only.\n");
5593 #endif
5594 }
5595
5596 static int alg_find_test(const char *alg)
5597 {
5598         int start = 0;
5599         int end = ARRAY_SIZE(alg_test_descs);
5600
5601         while (start < end) {
5602                 int i = (start + end) / 2;
5603                 int diff = strcmp(alg_test_descs[i].alg, alg);
5604
5605                 if (diff > 0) {
5606                         end = i;
5607                         continue;
5608                 }
5609
5610                 if (diff < 0) {
5611                         start = i + 1;
5612                         continue;
5613                 }
5614
5615                 return i;
5616         }
5617
5618         return -1;
5619 }
5620
5621 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
5622 {
5623         int i;
5624         int j;
5625         int rc;
5626
5627         if (!fips_enabled && notests) {
5628                 printk_once(KERN_INFO "alg: self-tests disabled\n");
5629                 return 0;
5630         }
5631
5632         DO_ONCE(testmgr_onetime_init);
5633
5634         if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
5635                 char nalg[CRYPTO_MAX_ALG_NAME];
5636
5637                 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
5638                     sizeof(nalg))
5639                         return -ENAMETOOLONG;
5640
5641                 i = alg_find_test(nalg);
5642                 if (i < 0)
5643                         goto notest;
5644
5645                 if (fips_enabled && !alg_test_descs[i].fips_allowed)
5646                         goto non_fips_alg;
5647
5648                 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
5649                 goto test_done;
5650         }
5651
5652         i = alg_find_test(alg);
5653         j = alg_find_test(driver);
5654         if (i < 0 && j < 0)
5655                 goto notest;
5656
5657         if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
5658                              (j >= 0 && !alg_test_descs[j].fips_allowed)))
5659                 goto non_fips_alg;
5660
5661         rc = 0;
5662         if (i >= 0)
5663                 rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
5664                                              type, mask);
5665         if (j >= 0 && j != i)
5666                 rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
5667                                              type, mask);
5668
5669 test_done:
5670         if (rc) {
5671                 if (fips_enabled || panic_on_fail) {
5672                         fips_fail_notify();
5673                         panic("alg: self-tests for %s (%s) failed in %s mode!\n",
5674                               driver, alg,
5675                               fips_enabled ? "fips" : "panic_on_fail");
5676                 }
5677                 WARN(1, "alg: self-tests for %s (%s) failed (rc=%d)",
5678                      driver, alg, rc);
5679         } else {
5680                 if (fips_enabled)
5681                         pr_info("alg: self-tests for %s (%s) passed\n",
5682                                 driver, alg);
5683         }
5684
5685         return rc;
5686
5687 notest:
5688         printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
5689         return 0;
5690 non_fips_alg:
5691         return -EINVAL;
5692 }
5693
5694 #endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
5695
5696 EXPORT_SYMBOL_GPL(alg_test);