crypto: testmgr - convert aead testing to use testvec_configs
[linux-2.6-microblaze.git] / crypto / testmgr.c
1 /*
2  * Algorithm testing framework and tests.
3  *
4  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5  * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
6  * Copyright (c) 2007 Nokia Siemens Networks
7  * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
8  * Copyright (c) 2019 Google LLC
9  *
10  * Updated RFC4106 AES-GCM testing.
11  *    Authors: Aidan O'Mahony (aidan.o.mahony@intel.com)
12  *             Adrian Hoban <adrian.hoban@intel.com>
13  *             Gabriele Paoloni <gabriele.paoloni@intel.com>
14  *             Tadeusz Struk (tadeusz.struk@intel.com)
15  *    Copyright (c) 2010, Intel Corporation.
16  *
17  * This program is free software; you can redistribute it and/or modify it
18  * under the terms of the GNU General Public License as published by the Free
19  * Software Foundation; either version 2 of the License, or (at your option)
20  * any later version.
21  *
22  */
23
24 #include <crypto/aead.h>
25 #include <crypto/hash.h>
26 #include <crypto/skcipher.h>
27 #include <linux/err.h>
28 #include <linux/fips.h>
29 #include <linux/module.h>
30 #include <linux/once.h>
31 #include <linux/random.h>
32 #include <linux/scatterlist.h>
33 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <crypto/rng.h>
36 #include <crypto/drbg.h>
37 #include <crypto/akcipher.h>
38 #include <crypto/kpp.h>
39 #include <crypto/acompress.h>
40
41 #include "internal.h"
42
43 static bool notests;
44 module_param(notests, bool, 0644);
45 MODULE_PARM_DESC(notests, "disable crypto self-tests");
46
47 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
48 static bool noextratests;
49 module_param(noextratests, bool, 0644);
50 MODULE_PARM_DESC(noextratests, "disable expensive crypto self-tests");
51
52 static unsigned int fuzz_iterations = 100;
53 module_param(fuzz_iterations, uint, 0644);
54 MODULE_PARM_DESC(fuzz_iterations, "number of fuzz test iterations");
55 #endif
56
57 #ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
58
59 /* a perfect nop */
60 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
61 {
62         return 0;
63 }
64
65 #else
66
67 #include "testmgr.h"
68
69 /*
70  * Need slab memory for testing (size in number of pages).
71  */
72 #define XBUFSIZE        8
73
74 /*
75  * Indexes into the xbuf to simulate cross-page access.
76  */
77 #define IDX1            32
78 #define IDX2            32400
79 #define IDX3            1511
80 #define IDX4            8193
81 #define IDX5            22222
82 #define IDX6            17101
83 #define IDX7            27333
84 #define IDX8            3000
85
86 /*
87 * Used by test_cipher()
88 */
89 #define ENCRYPT 1
90 #define DECRYPT 0
91
92 struct aead_test_suite {
93         const struct aead_testvec *vecs;
94         unsigned int count;
95 };
96
97 struct cipher_test_suite {
98         const struct cipher_testvec *vecs;
99         unsigned int count;
100 };
101
102 struct comp_test_suite {
103         struct {
104                 const struct comp_testvec *vecs;
105                 unsigned int count;
106         } comp, decomp;
107 };
108
109 struct hash_test_suite {
110         const struct hash_testvec *vecs;
111         unsigned int count;
112 };
113
114 struct cprng_test_suite {
115         const struct cprng_testvec *vecs;
116         unsigned int count;
117 };
118
119 struct drbg_test_suite {
120         const struct drbg_testvec *vecs;
121         unsigned int count;
122 };
123
124 struct akcipher_test_suite {
125         const struct akcipher_testvec *vecs;
126         unsigned int count;
127 };
128
129 struct kpp_test_suite {
130         const struct kpp_testvec *vecs;
131         unsigned int count;
132 };
133
134 struct alg_test_desc {
135         const char *alg;
136         int (*test)(const struct alg_test_desc *desc, const char *driver,
137                     u32 type, u32 mask);
138         int fips_allowed;       /* set if alg is allowed in fips mode */
139
140         union {
141                 struct aead_test_suite aead;
142                 struct cipher_test_suite cipher;
143                 struct comp_test_suite comp;
144                 struct hash_test_suite hash;
145                 struct cprng_test_suite cprng;
146                 struct drbg_test_suite drbg;
147                 struct akcipher_test_suite akcipher;
148                 struct kpp_test_suite kpp;
149         } suite;
150 };
151
152 static const unsigned int IDX[8] = {
153         IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
154
155 static void hexdump(unsigned char *buf, unsigned int len)
156 {
157         print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
158                         16, 1,
159                         buf, len, false);
160 }
161
162 static int __testmgr_alloc_buf(char *buf[XBUFSIZE], int order)
163 {
164         int i;
165
166         for (i = 0; i < XBUFSIZE; i++) {
167                 buf[i] = (char *)__get_free_pages(GFP_KERNEL, order);
168                 if (!buf[i])
169                         goto err_free_buf;
170         }
171
172         return 0;
173
174 err_free_buf:
175         while (i-- > 0)
176                 free_pages((unsigned long)buf[i], order);
177
178         return -ENOMEM;
179 }
180
181 static int testmgr_alloc_buf(char *buf[XBUFSIZE])
182 {
183         return __testmgr_alloc_buf(buf, 0);
184 }
185
186 static void __testmgr_free_buf(char *buf[XBUFSIZE], int order)
187 {
188         int i;
189
190         for (i = 0; i < XBUFSIZE; i++)
191                 free_pages((unsigned long)buf[i], order);
192 }
193
194 static void testmgr_free_buf(char *buf[XBUFSIZE])
195 {
196         __testmgr_free_buf(buf, 0);
197 }
198
199 #define TESTMGR_POISON_BYTE     0xfe
200 #define TESTMGR_POISON_LEN      16
201
202 static inline void testmgr_poison(void *addr, size_t len)
203 {
204         memset(addr, TESTMGR_POISON_BYTE, len);
205 }
206
207 /* Is the memory region still fully poisoned? */
208 static inline bool testmgr_is_poison(const void *addr, size_t len)
209 {
210         return memchr_inv(addr, TESTMGR_POISON_BYTE, len) == NULL;
211 }
212
213 /* flush type for hash algorithms */
214 enum flush_type {
215         /* merge with update of previous buffer(s) */
216         FLUSH_TYPE_NONE = 0,
217
218         /* update with previous buffer(s) before doing this one */
219         FLUSH_TYPE_FLUSH,
220
221         /* likewise, but also export and re-import the intermediate state */
222         FLUSH_TYPE_REIMPORT,
223 };
224
225 /* finalization function for hash algorithms */
226 enum finalization_type {
227         FINALIZATION_TYPE_FINAL,        /* use final() */
228         FINALIZATION_TYPE_FINUP,        /* use finup() */
229         FINALIZATION_TYPE_DIGEST,       /* use digest() */
230 };
231
232 #define TEST_SG_TOTAL   10000
233
234 /**
235  * struct test_sg_division - description of a scatterlist entry
236  *
237  * This struct describes one entry of a scatterlist being constructed to check a
238  * crypto test vector.
239  *
240  * @proportion_of_total: length of this chunk relative to the total length,
241  *                       given as a proportion out of TEST_SG_TOTAL so that it
242  *                       scales to fit any test vector
243  * @offset: byte offset into a 2-page buffer at which this chunk will start
244  * @offset_relative_to_alignmask: if true, add the algorithm's alignmask to the
245  *                                @offset
246  * @flush_type: for hashes, whether an update() should be done now vs.
247  *              continuing to accumulate data
248  */
249 struct test_sg_division {
250         unsigned int proportion_of_total;
251         unsigned int offset;
252         bool offset_relative_to_alignmask;
253         enum flush_type flush_type;
254 };
255
256 /**
257  * struct testvec_config - configuration for testing a crypto test vector
258  *
259  * This struct describes the data layout and other parameters with which each
260  * crypto test vector can be tested.
261  *
262  * @name: name of this config, logged for debugging purposes if a test fails
263  * @inplace: operate on the data in-place, if applicable for the algorithm type?
264  * @req_flags: extra request_flags, e.g. CRYPTO_TFM_REQ_MAY_SLEEP
265  * @src_divs: description of how to arrange the source scatterlist
266  * @dst_divs: description of how to arrange the dst scatterlist, if applicable
267  *            for the algorithm type.  Defaults to @src_divs if unset.
268  * @iv_offset: misalignment of the IV in the range [0..MAX_ALGAPI_ALIGNMASK+1],
269  *             where 0 is aligned to a 2*(MAX_ALGAPI_ALIGNMASK+1) byte boundary
270  * @iv_offset_relative_to_alignmask: if true, add the algorithm's alignmask to
271  *                                   the @iv_offset
272  * @finalization_type: what finalization function to use for hashes
273  */
274 struct testvec_config {
275         const char *name;
276         bool inplace;
277         u32 req_flags;
278         struct test_sg_division src_divs[XBUFSIZE];
279         struct test_sg_division dst_divs[XBUFSIZE];
280         unsigned int iv_offset;
281         bool iv_offset_relative_to_alignmask;
282         enum finalization_type finalization_type;
283 };
284
285 #define TESTVEC_CONFIG_NAMELEN  192
286
287 /*
288  * The following are the lists of testvec_configs to test for each algorithm
289  * type when the basic crypto self-tests are enabled, i.e. when
290  * CONFIG_CRYPTO_MANAGER_DISABLE_TESTS is unset.  They aim to provide good test
291  * coverage, while keeping the test time much shorter than the full fuzz tests
292  * so that the basic tests can be enabled in a wider range of circumstances.
293  */
294
295 /* Configs for skciphers and aeads */
296 static const struct testvec_config default_cipher_testvec_configs[] = {
297         {
298                 .name = "in-place",
299                 .inplace = true,
300                 .src_divs = { { .proportion_of_total = 10000 } },
301         }, {
302                 .name = "out-of-place",
303                 .src_divs = { { .proportion_of_total = 10000 } },
304         }, {
305                 .name = "unaligned buffer, offset=1",
306                 .src_divs = { { .proportion_of_total = 10000, .offset = 1 } },
307                 .iv_offset = 1,
308         }, {
309                 .name = "buffer aligned only to alignmask",
310                 .src_divs = {
311                         {
312                                 .proportion_of_total = 10000,
313                                 .offset = 1,
314                                 .offset_relative_to_alignmask = true,
315                         },
316                 },
317                 .iv_offset = 1,
318                 .iv_offset_relative_to_alignmask = true,
319         }, {
320                 .name = "two even aligned splits",
321                 .src_divs = {
322                         { .proportion_of_total = 5000 },
323                         { .proportion_of_total = 5000 },
324                 },
325         }, {
326                 .name = "uneven misaligned splits, may sleep",
327                 .req_flags = CRYPTO_TFM_REQ_MAY_SLEEP,
328                 .src_divs = {
329                         { .proportion_of_total = 1900, .offset = 33 },
330                         { .proportion_of_total = 3300, .offset = 7  },
331                         { .proportion_of_total = 4800, .offset = 18 },
332                 },
333                 .iv_offset = 3,
334         }, {
335                 .name = "misaligned splits crossing pages, inplace",
336                 .inplace = true,
337                 .src_divs = {
338                         {
339                                 .proportion_of_total = 7500,
340                                 .offset = PAGE_SIZE - 32
341                         }, {
342                                 .proportion_of_total = 2500,
343                                 .offset = PAGE_SIZE - 7
344                         },
345                 },
346         }
347 };
348
349 static unsigned int count_test_sg_divisions(const struct test_sg_division *divs)
350 {
351         unsigned int remaining = TEST_SG_TOTAL;
352         unsigned int ndivs = 0;
353
354         do {
355                 remaining -= divs[ndivs++].proportion_of_total;
356         } while (remaining);
357
358         return ndivs;
359 }
360
361 static bool valid_sg_divisions(const struct test_sg_division *divs,
362                                unsigned int count, bool *any_flushes_ret)
363 {
364         unsigned int total = 0;
365         unsigned int i;
366
367         for (i = 0; i < count && total != TEST_SG_TOTAL; i++) {
368                 if (divs[i].proportion_of_total <= 0 ||
369                     divs[i].proportion_of_total > TEST_SG_TOTAL - total)
370                         return false;
371                 total += divs[i].proportion_of_total;
372                 if (divs[i].flush_type != FLUSH_TYPE_NONE)
373                         *any_flushes_ret = true;
374         }
375         return total == TEST_SG_TOTAL &&
376                 memchr_inv(&divs[i], 0, (count - i) * sizeof(divs[0])) == NULL;
377 }
378
379 /*
380  * Check whether the given testvec_config is valid.  This isn't strictly needed
381  * since every testvec_config should be valid, but check anyway so that people
382  * don't unknowingly add broken configs that don't do what they wanted.
383  */
384 static bool valid_testvec_config(const struct testvec_config *cfg)
385 {
386         bool any_flushes = false;
387
388         if (cfg->name == NULL)
389                 return false;
390
391         if (!valid_sg_divisions(cfg->src_divs, ARRAY_SIZE(cfg->src_divs),
392                                 &any_flushes))
393                 return false;
394
395         if (cfg->dst_divs[0].proportion_of_total) {
396                 if (!valid_sg_divisions(cfg->dst_divs,
397                                         ARRAY_SIZE(cfg->dst_divs),
398                                         &any_flushes))
399                         return false;
400         } else {
401                 if (memchr_inv(cfg->dst_divs, 0, sizeof(cfg->dst_divs)))
402                         return false;
403                 /* defaults to dst_divs=src_divs */
404         }
405
406         if (cfg->iv_offset +
407             (cfg->iv_offset_relative_to_alignmask ? MAX_ALGAPI_ALIGNMASK : 0) >
408             MAX_ALGAPI_ALIGNMASK + 1)
409                 return false;
410
411         if (any_flushes && cfg->finalization_type == FINALIZATION_TYPE_DIGEST)
412                 return false;
413
414         return true;
415 }
416
417 struct test_sglist {
418         char *bufs[XBUFSIZE];
419         struct scatterlist sgl[XBUFSIZE];
420         struct scatterlist sgl_saved[XBUFSIZE];
421         struct scatterlist *sgl_ptr;
422         unsigned int nents;
423 };
424
425 static int init_test_sglist(struct test_sglist *tsgl)
426 {
427         return __testmgr_alloc_buf(tsgl->bufs, 1 /* two pages per buffer */);
428 }
429
430 static void destroy_test_sglist(struct test_sglist *tsgl)
431 {
432         return __testmgr_free_buf(tsgl->bufs, 1 /* two pages per buffer */);
433 }
434
435 /**
436  * build_test_sglist() - build a scatterlist for a crypto test
437  *
438  * @tsgl: the scatterlist to build.  @tsgl->bufs[] contains an array of 2-page
439  *        buffers which the scatterlist @tsgl->sgl[] will be made to point into.
440  * @divs: the layout specification on which the scatterlist will be based
441  * @alignmask: the algorithm's alignmask
442  * @total_len: the total length of the scatterlist to build in bytes
443  * @data: if non-NULL, the buffers will be filled with this data until it ends.
444  *        Otherwise the buffers will be poisoned.  In both cases, some bytes
445  *        past the end of each buffer will be poisoned to help detect overruns.
446  * @out_divs: if non-NULL, the test_sg_division to which each scatterlist entry
447  *            corresponds will be returned here.  This will match @divs except
448  *            that divisions resolving to a length of 0 are omitted as they are
449  *            not included in the scatterlist.
450  *
451  * Return: 0 or a -errno value
452  */
453 static int build_test_sglist(struct test_sglist *tsgl,
454                              const struct test_sg_division *divs,
455                              const unsigned int alignmask,
456                              const unsigned int total_len,
457                              struct iov_iter *data,
458                              const struct test_sg_division *out_divs[XBUFSIZE])
459 {
460         struct {
461                 const struct test_sg_division *div;
462                 size_t length;
463         } partitions[XBUFSIZE];
464         const unsigned int ndivs = count_test_sg_divisions(divs);
465         unsigned int len_remaining = total_len;
466         unsigned int i;
467
468         BUILD_BUG_ON(ARRAY_SIZE(partitions) != ARRAY_SIZE(tsgl->sgl));
469         if (WARN_ON(ndivs > ARRAY_SIZE(partitions)))
470                 return -EINVAL;
471
472         /* Calculate the (div, length) pairs */
473         tsgl->nents = 0;
474         for (i = 0; i < ndivs; i++) {
475                 unsigned int len_this_sg =
476                         min(len_remaining,
477                             (total_len * divs[i].proportion_of_total +
478                              TEST_SG_TOTAL / 2) / TEST_SG_TOTAL);
479
480                 if (len_this_sg != 0) {
481                         partitions[tsgl->nents].div = &divs[i];
482                         partitions[tsgl->nents].length = len_this_sg;
483                         tsgl->nents++;
484                         len_remaining -= len_this_sg;
485                 }
486         }
487         if (tsgl->nents == 0) {
488                 partitions[tsgl->nents].div = &divs[0];
489                 partitions[tsgl->nents].length = 0;
490                 tsgl->nents++;
491         }
492         partitions[tsgl->nents - 1].length += len_remaining;
493
494         /* Set up the sgl entries and fill the data or poison */
495         sg_init_table(tsgl->sgl, tsgl->nents);
496         for (i = 0; i < tsgl->nents; i++) {
497                 unsigned int offset = partitions[i].div->offset;
498                 void *addr;
499
500                 if (partitions[i].div->offset_relative_to_alignmask)
501                         offset += alignmask;
502
503                 while (offset + partitions[i].length + TESTMGR_POISON_LEN >
504                        2 * PAGE_SIZE) {
505                         if (WARN_ON(offset <= 0))
506                                 return -EINVAL;
507                         offset /= 2;
508                 }
509
510                 addr = &tsgl->bufs[i][offset];
511                 sg_set_buf(&tsgl->sgl[i], addr, partitions[i].length);
512
513                 if (out_divs)
514                         out_divs[i] = partitions[i].div;
515
516                 if (data) {
517                         size_t copy_len, copied;
518
519                         copy_len = min(partitions[i].length, data->count);
520                         copied = copy_from_iter(addr, copy_len, data);
521                         if (WARN_ON(copied != copy_len))
522                                 return -EINVAL;
523                         testmgr_poison(addr + copy_len, partitions[i].length +
524                                        TESTMGR_POISON_LEN - copy_len);
525                 } else {
526                         testmgr_poison(addr, partitions[i].length +
527                                        TESTMGR_POISON_LEN);
528                 }
529         }
530
531         sg_mark_end(&tsgl->sgl[tsgl->nents - 1]);
532         tsgl->sgl_ptr = tsgl->sgl;
533         memcpy(tsgl->sgl_saved, tsgl->sgl, tsgl->nents * sizeof(tsgl->sgl[0]));
534         return 0;
535 }
536
537 /*
538  * Verify that a scatterlist crypto operation produced the correct output.
539  *
540  * @tsgl: scatterlist containing the actual output
541  * @expected_output: buffer containing the expected output
542  * @len_to_check: length of @expected_output in bytes
543  * @unchecked_prefix_len: number of ignored bytes in @tsgl prior to real result
544  * @check_poison: verify that the poison bytes after each chunk are intact?
545  *
546  * Return: 0 if correct, -EINVAL if incorrect, -EOVERFLOW if buffer overrun.
547  */
548 static int verify_correct_output(const struct test_sglist *tsgl,
549                                  const char *expected_output,
550                                  unsigned int len_to_check,
551                                  unsigned int unchecked_prefix_len,
552                                  bool check_poison)
553 {
554         unsigned int i;
555
556         for (i = 0; i < tsgl->nents; i++) {
557                 struct scatterlist *sg = &tsgl->sgl_ptr[i];
558                 unsigned int len = sg->length;
559                 unsigned int offset = sg->offset;
560                 const char *actual_output;
561
562                 if (unchecked_prefix_len) {
563                         if (unchecked_prefix_len >= len) {
564                                 unchecked_prefix_len -= len;
565                                 continue;
566                         }
567                         offset += unchecked_prefix_len;
568                         len -= unchecked_prefix_len;
569                         unchecked_prefix_len = 0;
570                 }
571                 len = min(len, len_to_check);
572                 actual_output = page_address(sg_page(sg)) + offset;
573                 if (memcmp(expected_output, actual_output, len) != 0)
574                         return -EINVAL;
575                 if (check_poison &&
576                     !testmgr_is_poison(actual_output + len, TESTMGR_POISON_LEN))
577                         return -EOVERFLOW;
578                 len_to_check -= len;
579                 expected_output += len;
580         }
581         if (WARN_ON(len_to_check != 0))
582                 return -EINVAL;
583         return 0;
584 }
585
586 static bool is_test_sglist_corrupted(const struct test_sglist *tsgl)
587 {
588         unsigned int i;
589
590         for (i = 0; i < tsgl->nents; i++) {
591                 if (tsgl->sgl[i].page_link != tsgl->sgl_saved[i].page_link)
592                         return true;
593                 if (tsgl->sgl[i].offset != tsgl->sgl_saved[i].offset)
594                         return true;
595                 if (tsgl->sgl[i].length != tsgl->sgl_saved[i].length)
596                         return true;
597         }
598         return false;
599 }
600
601 struct cipher_test_sglists {
602         struct test_sglist src;
603         struct test_sglist dst;
604 };
605
606 static struct cipher_test_sglists *alloc_cipher_test_sglists(void)
607 {
608         struct cipher_test_sglists *tsgls;
609
610         tsgls = kmalloc(sizeof(*tsgls), GFP_KERNEL);
611         if (!tsgls)
612                 return NULL;
613
614         if (init_test_sglist(&tsgls->src) != 0)
615                 goto fail_kfree;
616         if (init_test_sglist(&tsgls->dst) != 0)
617                 goto fail_destroy_src;
618
619         return tsgls;
620
621 fail_destroy_src:
622         destroy_test_sglist(&tsgls->src);
623 fail_kfree:
624         kfree(tsgls);
625         return NULL;
626 }
627
628 static void free_cipher_test_sglists(struct cipher_test_sglists *tsgls)
629 {
630         if (tsgls) {
631                 destroy_test_sglist(&tsgls->src);
632                 destroy_test_sglist(&tsgls->dst);
633                 kfree(tsgls);
634         }
635 }
636
637 /* Build the src and dst scatterlists for an skcipher or AEAD test */
638 static int build_cipher_test_sglists(struct cipher_test_sglists *tsgls,
639                                      const struct testvec_config *cfg,
640                                      unsigned int alignmask,
641                                      unsigned int src_total_len,
642                                      unsigned int dst_total_len,
643                                      const struct kvec *inputs,
644                                      unsigned int nr_inputs)
645 {
646         struct iov_iter input;
647         int err;
648
649         iov_iter_kvec(&input, WRITE, inputs, nr_inputs, src_total_len);
650         err = build_test_sglist(&tsgls->src, cfg->src_divs, alignmask,
651                                 cfg->inplace ?
652                                         max(dst_total_len, src_total_len) :
653                                         src_total_len,
654                                 &input, NULL);
655         if (err)
656                 return err;
657
658         if (cfg->inplace) {
659                 tsgls->dst.sgl_ptr = tsgls->src.sgl;
660                 tsgls->dst.nents = tsgls->src.nents;
661                 return 0;
662         }
663         return build_test_sglist(&tsgls->dst,
664                                  cfg->dst_divs[0].proportion_of_total ?
665                                         cfg->dst_divs : cfg->src_divs,
666                                  alignmask, dst_total_len, NULL, NULL);
667 }
668
669 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
670 static char *generate_random_sgl_divisions(struct test_sg_division *divs,
671                                            size_t max_divs, char *p, char *end,
672                                            bool gen_flushes)
673 {
674         struct test_sg_division *div = divs;
675         unsigned int remaining = TEST_SG_TOTAL;
676
677         do {
678                 unsigned int this_len;
679
680                 if (div == &divs[max_divs - 1] || prandom_u32() % 2 == 0)
681                         this_len = remaining;
682                 else
683                         this_len = 1 + (prandom_u32() % remaining);
684                 div->proportion_of_total = this_len;
685
686                 if (prandom_u32() % 4 == 0)
687                         div->offset = (PAGE_SIZE - 128) + (prandom_u32() % 128);
688                 else if (prandom_u32() % 2 == 0)
689                         div->offset = prandom_u32() % 32;
690                 else
691                         div->offset = prandom_u32() % PAGE_SIZE;
692                 if (prandom_u32() % 8 == 0)
693                         div->offset_relative_to_alignmask = true;
694
695                 div->flush_type = FLUSH_TYPE_NONE;
696                 if (gen_flushes) {
697                         switch (prandom_u32() % 4) {
698                         case 0:
699                                 div->flush_type = FLUSH_TYPE_REIMPORT;
700                                 break;
701                         case 1:
702                                 div->flush_type = FLUSH_TYPE_FLUSH;
703                                 break;
704                         }
705                 }
706
707                 BUILD_BUG_ON(TEST_SG_TOTAL != 10000); /* for "%u.%u%%" */
708                 p += scnprintf(p, end - p, "%s%u.%u%%@%s+%u%s",
709                                div->flush_type == FLUSH_TYPE_NONE ? "" :
710                                div->flush_type == FLUSH_TYPE_FLUSH ?
711                                "<flush> " : "<reimport> ",
712                                this_len / 100, this_len % 100,
713                                div->offset_relative_to_alignmask ?
714                                         "alignmask" : "",
715                                div->offset, this_len == remaining ? "" : ", ");
716                 remaining -= this_len;
717                 div++;
718         } while (remaining);
719
720         return p;
721 }
722
723 /* Generate a random testvec_config for fuzz testing */
724 static void generate_random_testvec_config(struct testvec_config *cfg,
725                                            char *name, size_t max_namelen)
726 {
727         char *p = name;
728         char * const end = name + max_namelen;
729
730         memset(cfg, 0, sizeof(*cfg));
731
732         cfg->name = name;
733
734         p += scnprintf(p, end - p, "random:");
735
736         if (prandom_u32() % 2 == 0) {
737                 cfg->inplace = true;
738                 p += scnprintf(p, end - p, " inplace");
739         }
740
741         if (prandom_u32() % 2 == 0) {
742                 cfg->req_flags |= CRYPTO_TFM_REQ_MAY_SLEEP;
743                 p += scnprintf(p, end - p, " may_sleep");
744         }
745
746         switch (prandom_u32() % 4) {
747         case 0:
748                 cfg->finalization_type = FINALIZATION_TYPE_FINAL;
749                 p += scnprintf(p, end - p, " use_final");
750                 break;
751         case 1:
752                 cfg->finalization_type = FINALIZATION_TYPE_FINUP;
753                 p += scnprintf(p, end - p, " use_finup");
754                 break;
755         default:
756                 cfg->finalization_type = FINALIZATION_TYPE_DIGEST;
757                 p += scnprintf(p, end - p, " use_digest");
758                 break;
759         }
760
761         p += scnprintf(p, end - p, " src_divs=[");
762         p = generate_random_sgl_divisions(cfg->src_divs,
763                                           ARRAY_SIZE(cfg->src_divs), p, end,
764                                           (cfg->finalization_type !=
765                                            FINALIZATION_TYPE_DIGEST));
766         p += scnprintf(p, end - p, "]");
767
768         if (!cfg->inplace && prandom_u32() % 2 == 0) {
769                 p += scnprintf(p, end - p, " dst_divs=[");
770                 p = generate_random_sgl_divisions(cfg->dst_divs,
771                                                   ARRAY_SIZE(cfg->dst_divs),
772                                                   p, end, false);
773                 p += scnprintf(p, end - p, "]");
774         }
775
776         if (prandom_u32() % 2 == 0) {
777                 cfg->iv_offset = 1 + (prandom_u32() % MAX_ALGAPI_ALIGNMASK);
778                 p += scnprintf(p, end - p, " iv_offset=%u", cfg->iv_offset);
779         }
780
781         WARN_ON_ONCE(!valid_testvec_config(cfg));
782 }
783 #endif /* CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
784
785 static int ahash_guard_result(char *result, char c, int size)
786 {
787         int i;
788
789         for (i = 0; i < size; i++) {
790                 if (result[i] != c)
791                         return -EINVAL;
792         }
793
794         return 0;
795 }
796
797 static int ahash_partial_update(struct ahash_request **preq,
798         struct crypto_ahash *tfm, const struct hash_testvec *template,
799         void *hash_buff, int k, int temp, struct scatterlist *sg,
800         const char *algo, char *result, struct crypto_wait *wait)
801 {
802         char *state;
803         struct ahash_request *req;
804         int statesize, ret = -EINVAL;
805         static const unsigned char guard[] = { 0x00, 0xba, 0xad, 0x00 };
806         int digestsize = crypto_ahash_digestsize(tfm);
807
808         req = *preq;
809         statesize = crypto_ahash_statesize(
810                         crypto_ahash_reqtfm(req));
811         state = kmalloc(statesize + sizeof(guard), GFP_KERNEL);
812         if (!state) {
813                 pr_err("alg: hash: Failed to alloc state for %s\n", algo);
814                 goto out_nostate;
815         }
816         memcpy(state + statesize, guard, sizeof(guard));
817         memset(result, 1, digestsize);
818         ret = crypto_ahash_export(req, state);
819         WARN_ON(memcmp(state + statesize, guard, sizeof(guard)));
820         if (ret) {
821                 pr_err("alg: hash: Failed to export() for %s\n", algo);
822                 goto out;
823         }
824         ret = ahash_guard_result(result, 1, digestsize);
825         if (ret) {
826                 pr_err("alg: hash: Failed, export used req->result for %s\n",
827                        algo);
828                 goto out;
829         }
830         ahash_request_free(req);
831         req = ahash_request_alloc(tfm, GFP_KERNEL);
832         if (!req) {
833                 pr_err("alg: hash: Failed to alloc request for %s\n", algo);
834                 goto out_noreq;
835         }
836         ahash_request_set_callback(req,
837                 CRYPTO_TFM_REQ_MAY_BACKLOG,
838                 crypto_req_done, wait);
839
840         memcpy(hash_buff, template->plaintext + temp,
841                 template->tap[k]);
842         sg_init_one(&sg[0], hash_buff, template->tap[k]);
843         ahash_request_set_crypt(req, sg, result, template->tap[k]);
844         ret = crypto_ahash_import(req, state);
845         if (ret) {
846                 pr_err("alg: hash: Failed to import() for %s\n", algo);
847                 goto out;
848         }
849         ret = ahash_guard_result(result, 1, digestsize);
850         if (ret) {
851                 pr_err("alg: hash: Failed, import used req->result for %s\n",
852                        algo);
853                 goto out;
854         }
855         ret = crypto_wait_req(crypto_ahash_update(req), wait);
856         if (ret)
857                 goto out;
858         *preq = req;
859         ret = 0;
860         goto out_noreq;
861 out:
862         ahash_request_free(req);
863 out_noreq:
864         kfree(state);
865 out_nostate:
866         return ret;
867 }
868
869 enum hash_test {
870         HASH_TEST_DIGEST,
871         HASH_TEST_FINAL,
872         HASH_TEST_FINUP
873 };
874
875 static int __test_hash(struct crypto_ahash *tfm,
876                        const struct hash_testvec *template, unsigned int tcount,
877                        enum hash_test test_type, const int align_offset)
878 {
879         const char *algo = crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
880         size_t digest_size = crypto_ahash_digestsize(tfm);
881         unsigned int i, j, k, temp;
882         struct scatterlist sg[8];
883         char *result;
884         char *key;
885         struct ahash_request *req;
886         struct crypto_wait wait;
887         void *hash_buff;
888         char *xbuf[XBUFSIZE];
889         int ret = -ENOMEM;
890
891         result = kmalloc(digest_size, GFP_KERNEL);
892         if (!result)
893                 return ret;
894         key = kmalloc(MAX_KEYLEN, GFP_KERNEL);
895         if (!key)
896                 goto out_nobuf;
897         if (testmgr_alloc_buf(xbuf))
898                 goto out_nobuf;
899
900         crypto_init_wait(&wait);
901
902         req = ahash_request_alloc(tfm, GFP_KERNEL);
903         if (!req) {
904                 printk(KERN_ERR "alg: hash: Failed to allocate request for "
905                        "%s\n", algo);
906                 goto out_noreq;
907         }
908         ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
909                                    crypto_req_done, &wait);
910
911         j = 0;
912         for (i = 0; i < tcount; i++) {
913                 if (template[i].np)
914                         continue;
915
916                 ret = -EINVAL;
917                 if (WARN_ON(align_offset + template[i].psize > PAGE_SIZE))
918                         goto out;
919
920                 j++;
921                 memset(result, 0, digest_size);
922
923                 hash_buff = xbuf[0];
924                 hash_buff += align_offset;
925
926                 memcpy(hash_buff, template[i].plaintext, template[i].psize);
927                 sg_init_one(&sg[0], hash_buff, template[i].psize);
928
929                 if (template[i].ksize) {
930                         crypto_ahash_clear_flags(tfm, ~0);
931                         if (template[i].ksize > MAX_KEYLEN) {
932                                 pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
933                                        j, algo, template[i].ksize, MAX_KEYLEN);
934                                 ret = -EINVAL;
935                                 goto out;
936                         }
937                         memcpy(key, template[i].key, template[i].ksize);
938                         ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
939                         if (ret) {
940                                 printk(KERN_ERR "alg: hash: setkey failed on "
941                                        "test %d for %s: ret=%d\n", j, algo,
942                                        -ret);
943                                 goto out;
944                         }
945                 }
946
947                 ahash_request_set_crypt(req, sg, result, template[i].psize);
948                 switch (test_type) {
949                 case HASH_TEST_DIGEST:
950                         ret = crypto_wait_req(crypto_ahash_digest(req), &wait);
951                         if (ret) {
952                                 pr_err("alg: hash: digest failed on test %d "
953                                        "for %s: ret=%d\n", j, algo, -ret);
954                                 goto out;
955                         }
956                         break;
957
958                 case HASH_TEST_FINAL:
959                         memset(result, 1, digest_size);
960                         ret = crypto_wait_req(crypto_ahash_init(req), &wait);
961                         if (ret) {
962                                 pr_err("alg: hash: init failed on test %d "
963                                        "for %s: ret=%d\n", j, algo, -ret);
964                                 goto out;
965                         }
966                         ret = ahash_guard_result(result, 1, digest_size);
967                         if (ret) {
968                                 pr_err("alg: hash: init failed on test %d "
969                                        "for %s: used req->result\n", j, algo);
970                                 goto out;
971                         }
972                         ret = crypto_wait_req(crypto_ahash_update(req), &wait);
973                         if (ret) {
974                                 pr_err("alg: hash: update failed on test %d "
975                                        "for %s: ret=%d\n", j, algo, -ret);
976                                 goto out;
977                         }
978                         ret = ahash_guard_result(result, 1, digest_size);
979                         if (ret) {
980                                 pr_err("alg: hash: update failed on test %d "
981                                        "for %s: used req->result\n", j, algo);
982                                 goto out;
983                         }
984                         ret = crypto_wait_req(crypto_ahash_final(req), &wait);
985                         if (ret) {
986                                 pr_err("alg: hash: final failed on test %d "
987                                        "for %s: ret=%d\n", j, algo, -ret);
988                                 goto out;
989                         }
990                         break;
991
992                 case HASH_TEST_FINUP:
993                         memset(result, 1, digest_size);
994                         ret = crypto_wait_req(crypto_ahash_init(req), &wait);
995                         if (ret) {
996                                 pr_err("alg: hash: init failed on test %d "
997                                        "for %s: ret=%d\n", j, algo, -ret);
998                                 goto out;
999                         }
1000                         ret = ahash_guard_result(result, 1, digest_size);
1001                         if (ret) {
1002                                 pr_err("alg: hash: init failed on test %d "
1003                                        "for %s: used req->result\n", j, algo);
1004                                 goto out;
1005                         }
1006                         ret = crypto_wait_req(crypto_ahash_finup(req), &wait);
1007                         if (ret) {
1008                                 pr_err("alg: hash: final failed on test %d "
1009                                        "for %s: ret=%d\n", j, algo, -ret);
1010                                 goto out;
1011                         }
1012                         break;
1013                 }
1014
1015                 if (memcmp(result, template[i].digest,
1016                            crypto_ahash_digestsize(tfm))) {
1017                         printk(KERN_ERR "alg: hash: Test %d failed for %s\n",
1018                                j, algo);
1019                         hexdump(result, crypto_ahash_digestsize(tfm));
1020                         ret = -EINVAL;
1021                         goto out;
1022                 }
1023         }
1024
1025         if (test_type)
1026                 goto out;
1027
1028         j = 0;
1029         for (i = 0; i < tcount; i++) {
1030                 /* alignment tests are only done with continuous buffers */
1031                 if (align_offset != 0)
1032                         break;
1033
1034                 if (!template[i].np)
1035                         continue;
1036
1037                 j++;
1038                 memset(result, 0, digest_size);
1039
1040                 temp = 0;
1041                 sg_init_table(sg, template[i].np);
1042                 ret = -EINVAL;
1043                 for (k = 0; k < template[i].np; k++) {
1044                         if (WARN_ON(offset_in_page(IDX[k]) +
1045                                     template[i].tap[k] > PAGE_SIZE))
1046                                 goto out;
1047                         sg_set_buf(&sg[k],
1048                                    memcpy(xbuf[IDX[k] >> PAGE_SHIFT] +
1049                                           offset_in_page(IDX[k]),
1050                                           template[i].plaintext + temp,
1051                                           template[i].tap[k]),
1052                                    template[i].tap[k]);
1053                         temp += template[i].tap[k];
1054                 }
1055
1056                 if (template[i].ksize) {
1057                         if (template[i].ksize > MAX_KEYLEN) {
1058                                 pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
1059                                        j, algo, template[i].ksize, MAX_KEYLEN);
1060                                 ret = -EINVAL;
1061                                 goto out;
1062                         }
1063                         crypto_ahash_clear_flags(tfm, ~0);
1064                         memcpy(key, template[i].key, template[i].ksize);
1065                         ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
1066
1067                         if (ret) {
1068                                 printk(KERN_ERR "alg: hash: setkey "
1069                                        "failed on chunking test %d "
1070                                        "for %s: ret=%d\n", j, algo, -ret);
1071                                 goto out;
1072                         }
1073                 }
1074
1075                 ahash_request_set_crypt(req, sg, result, template[i].psize);
1076                 ret = crypto_wait_req(crypto_ahash_digest(req), &wait);
1077                 if (ret) {
1078                         pr_err("alg: hash: digest failed on chunking test %d for %s: ret=%d\n",
1079                                j, algo, -ret);
1080                         goto out;
1081                 }
1082
1083                 if (memcmp(result, template[i].digest,
1084                            crypto_ahash_digestsize(tfm))) {
1085                         printk(KERN_ERR "alg: hash: Chunking test %d "
1086                                "failed for %s\n", j, algo);
1087                         hexdump(result, crypto_ahash_digestsize(tfm));
1088                         ret = -EINVAL;
1089                         goto out;
1090                 }
1091         }
1092
1093         /* partial update exercise */
1094         j = 0;
1095         for (i = 0; i < tcount; i++) {
1096                 /* alignment tests are only done with continuous buffers */
1097                 if (align_offset != 0)
1098                         break;
1099
1100                 if (template[i].np < 2)
1101                         continue;
1102
1103                 j++;
1104                 memset(result, 0, digest_size);
1105
1106                 ret = -EINVAL;
1107                 hash_buff = xbuf[0];
1108                 memcpy(hash_buff, template[i].plaintext,
1109                         template[i].tap[0]);
1110                 sg_init_one(&sg[0], hash_buff, template[i].tap[0]);
1111
1112                 if (template[i].ksize) {
1113                         crypto_ahash_clear_flags(tfm, ~0);
1114                         if (template[i].ksize > MAX_KEYLEN) {
1115                                 pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
1116                                         j, algo, template[i].ksize, MAX_KEYLEN);
1117                                 ret = -EINVAL;
1118                                 goto out;
1119                         }
1120                         memcpy(key, template[i].key, template[i].ksize);
1121                         ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
1122                         if (ret) {
1123                                 pr_err("alg: hash: setkey failed on test %d for %s: ret=%d\n",
1124                                         j, algo, -ret);
1125                                 goto out;
1126                         }
1127                 }
1128
1129                 ahash_request_set_crypt(req, sg, result, template[i].tap[0]);
1130                 ret = crypto_wait_req(crypto_ahash_init(req), &wait);
1131                 if (ret) {
1132                         pr_err("alg: hash: init failed on test %d for %s: ret=%d\n",
1133                                 j, algo, -ret);
1134                         goto out;
1135                 }
1136                 ret = crypto_wait_req(crypto_ahash_update(req), &wait);
1137                 if (ret) {
1138                         pr_err("alg: hash: update failed on test %d for %s: ret=%d\n",
1139                                 j, algo, -ret);
1140                         goto out;
1141                 }
1142
1143                 temp = template[i].tap[0];
1144                 for (k = 1; k < template[i].np; k++) {
1145                         ret = ahash_partial_update(&req, tfm, &template[i],
1146                                 hash_buff, k, temp, &sg[0], algo, result,
1147                                 &wait);
1148                         if (ret) {
1149                                 pr_err("alg: hash: partial update failed on test %d for %s: ret=%d\n",
1150                                         j, algo, -ret);
1151                                 goto out_noreq;
1152                         }
1153                         temp += template[i].tap[k];
1154                 }
1155                 ret = crypto_wait_req(crypto_ahash_final(req), &wait);
1156                 if (ret) {
1157                         pr_err("alg: hash: final failed on test %d for %s: ret=%d\n",
1158                                 j, algo, -ret);
1159                         goto out;
1160                 }
1161                 if (memcmp(result, template[i].digest,
1162                            crypto_ahash_digestsize(tfm))) {
1163                         pr_err("alg: hash: Partial Test %d failed for %s\n",
1164                                j, algo);
1165                         hexdump(result, crypto_ahash_digestsize(tfm));
1166                         ret = -EINVAL;
1167                         goto out;
1168                 }
1169         }
1170
1171         ret = 0;
1172
1173 out:
1174         ahash_request_free(req);
1175 out_noreq:
1176         testmgr_free_buf(xbuf);
1177 out_nobuf:
1178         kfree(key);
1179         kfree(result);
1180         return ret;
1181 }
1182
1183 static int test_hash(struct crypto_ahash *tfm,
1184                      const struct hash_testvec *template,
1185                      unsigned int tcount, enum hash_test test_type)
1186 {
1187         unsigned int alignmask;
1188         int ret;
1189
1190         ret = __test_hash(tfm, template, tcount, test_type, 0);
1191         if (ret)
1192                 return ret;
1193
1194         /* test unaligned buffers, check with one byte offset */
1195         ret = __test_hash(tfm, template, tcount, test_type, 1);
1196         if (ret)
1197                 return ret;
1198
1199         alignmask = crypto_tfm_alg_alignmask(&tfm->base);
1200         if (alignmask) {
1201                 /* Check if alignment mask for tfm is correctly set. */
1202                 ret = __test_hash(tfm, template, tcount, test_type,
1203                                   alignmask + 1);
1204                 if (ret)
1205                         return ret;
1206         }
1207
1208         return 0;
1209 }
1210
1211 static int test_aead_vec_cfg(const char *driver, int enc,
1212                              const struct aead_testvec *vec,
1213                              unsigned int vec_num,
1214                              const struct testvec_config *cfg,
1215                              struct aead_request *req,
1216                              struct cipher_test_sglists *tsgls)
1217 {
1218         struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1219         const unsigned int alignmask = crypto_aead_alignmask(tfm);
1220         const unsigned int ivsize = crypto_aead_ivsize(tfm);
1221         const unsigned int authsize = vec->clen - vec->plen;
1222         const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1223         const char *op = enc ? "encryption" : "decryption";
1224         DECLARE_CRYPTO_WAIT(wait);
1225         u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
1226         u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
1227                  cfg->iv_offset +
1228                  (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
1229         struct kvec input[2];
1230         int err;
1231
1232         /* Set the key */
1233         if (vec->wk)
1234                 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1235         else
1236                 crypto_aead_clear_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1237         err = crypto_aead_setkey(tfm, vec->key, vec->klen);
1238         if (err) {
1239                 if (vec->fail) /* expectedly failed to set key? */
1240                         return 0;
1241                 pr_err("alg: aead: %s setkey failed with err %d on test vector %u; flags=%#x\n",
1242                        driver, err, vec_num, crypto_aead_get_flags(tfm));
1243                 return err;
1244         }
1245         if (vec->fail) {
1246                 pr_err("alg: aead: %s setkey unexpectedly succeeded on test vector %u\n",
1247                        driver, vec_num);
1248                 return -EINVAL;
1249         }
1250
1251         /* Set the authentication tag size */
1252         err = crypto_aead_setauthsize(tfm, authsize);
1253         if (err) {
1254                 pr_err("alg: aead: %s setauthsize failed with err %d on test vector %u\n",
1255                        driver, err, vec_num);
1256                 return err;
1257         }
1258
1259         /* The IV must be copied to a buffer, as the algorithm may modify it */
1260         if (WARN_ON(ivsize > MAX_IVLEN))
1261                 return -EINVAL;
1262         if (vec->iv)
1263                 memcpy(iv, vec->iv, ivsize);
1264         else
1265                 memset(iv, 0, ivsize);
1266
1267         /* Build the src/dst scatterlists */
1268         input[0].iov_base = (void *)vec->assoc;
1269         input[0].iov_len = vec->alen;
1270         input[1].iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
1271         input[1].iov_len = enc ? vec->plen : vec->clen;
1272         err = build_cipher_test_sglists(tsgls, cfg, alignmask,
1273                                         vec->alen + (enc ? vec->plen :
1274                                                      vec->clen),
1275                                         vec->alen + (enc ? vec->clen :
1276                                                      vec->plen),
1277                                         input, 2);
1278         if (err) {
1279                 pr_err("alg: aead: %s %s: error preparing scatterlists for test vector %u, cfg=\"%s\"\n",
1280                        driver, op, vec_num, cfg->name);
1281                 return err;
1282         }
1283
1284         /* Do the actual encryption or decryption */
1285         testmgr_poison(req->__ctx, crypto_aead_reqsize(tfm));
1286         aead_request_set_callback(req, req_flags, crypto_req_done, &wait);
1287         aead_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
1288                                enc ? vec->plen : vec->clen, iv);
1289         aead_request_set_ad(req, vec->alen);
1290         err = crypto_wait_req(enc ? crypto_aead_encrypt(req) :
1291                               crypto_aead_decrypt(req), &wait);
1292
1293         aead_request_set_tfm(req, tfm); /* TODO: get rid of this */
1294
1295         if (err) {
1296                 if (err == -EBADMSG && vec->novrfy)
1297                         return 0;
1298                 pr_err("alg: aead: %s %s failed with err %d on test vector %u, cfg=\"%s\"\n",
1299                        driver, op, err, vec_num, cfg->name);
1300                 return err;
1301         }
1302         if (vec->novrfy) {
1303                 pr_err("alg: aead: %s %s unexpectedly succeeded on test vector %u, cfg=\"%s\"\n",
1304                        driver, op, vec_num, cfg->name);
1305                 return -EINVAL;
1306         }
1307
1308         /* Check for the correct output (ciphertext or plaintext) */
1309         err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
1310                                     enc ? vec->clen : vec->plen,
1311                                     vec->alen, enc || !cfg->inplace);
1312         if (err == -EOVERFLOW) {
1313                 pr_err("alg: aead: %s %s overran dst buffer on test vector %u, cfg=\"%s\"\n",
1314                        driver, op, vec_num, cfg->name);
1315                 return err;
1316         }
1317         if (err) {
1318                 pr_err("alg: aead: %s %s test failed (wrong result) on test vector %u, cfg=\"%s\"\n",
1319                        driver, op, vec_num, cfg->name);
1320                 return err;
1321         }
1322
1323         return 0;
1324 }
1325
1326 static int test_aead_vec(const char *driver, int enc,
1327                          const struct aead_testvec *vec, unsigned int vec_num,
1328                          struct aead_request *req,
1329                          struct cipher_test_sglists *tsgls)
1330 {
1331         unsigned int i;
1332         int err;
1333
1334         if (enc && vec->novrfy)
1335                 return 0;
1336
1337         for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
1338                 err = test_aead_vec_cfg(driver, enc, vec, vec_num,
1339                                         &default_cipher_testvec_configs[i],
1340                                         req, tsgls);
1341                 if (err)
1342                         return err;
1343         }
1344
1345 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1346         if (!noextratests) {
1347                 struct testvec_config cfg;
1348                 char cfgname[TESTVEC_CONFIG_NAMELEN];
1349
1350                 for (i = 0; i < fuzz_iterations; i++) {
1351                         generate_random_testvec_config(&cfg, cfgname,
1352                                                        sizeof(cfgname));
1353                         err = test_aead_vec_cfg(driver, enc, vec, vec_num,
1354                                                 &cfg, req, tsgls);
1355                         if (err)
1356                                 return err;
1357                 }
1358         }
1359 #endif
1360         return 0;
1361 }
1362
1363 static int test_aead(const char *driver, int enc,
1364                      const struct aead_test_suite *suite,
1365                      struct aead_request *req,
1366                      struct cipher_test_sglists *tsgls)
1367 {
1368         unsigned int i;
1369         int err;
1370
1371         for (i = 0; i < suite->count; i++) {
1372                 err = test_aead_vec(driver, enc, &suite->vecs[i], i, req,
1373                                     tsgls);
1374                 if (err)
1375                         return err;
1376         }
1377         return 0;
1378 }
1379
1380 static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
1381                          u32 type, u32 mask)
1382 {
1383         const struct aead_test_suite *suite = &desc->suite.aead;
1384         struct crypto_aead *tfm;
1385         struct aead_request *req = NULL;
1386         struct cipher_test_sglists *tsgls = NULL;
1387         int err;
1388
1389         if (suite->count <= 0) {
1390                 pr_err("alg: aead: empty test suite for %s\n", driver);
1391                 return -EINVAL;
1392         }
1393
1394         tfm = crypto_alloc_aead(driver, type, mask);
1395         if (IS_ERR(tfm)) {
1396                 pr_err("alg: aead: failed to allocate transform for %s: %ld\n",
1397                        driver, PTR_ERR(tfm));
1398                 return PTR_ERR(tfm);
1399         }
1400
1401         req = aead_request_alloc(tfm, GFP_KERNEL);
1402         if (!req) {
1403                 pr_err("alg: aead: failed to allocate request for %s\n",
1404                        driver);
1405                 err = -ENOMEM;
1406                 goto out;
1407         }
1408
1409         tsgls = alloc_cipher_test_sglists();
1410         if (!tsgls) {
1411                 pr_err("alg: aead: failed to allocate test buffers for %s\n",
1412                        driver);
1413                 err = -ENOMEM;
1414                 goto out;
1415         }
1416
1417         err = test_aead(driver, ENCRYPT, suite, req, tsgls);
1418         if (err)
1419                 goto out;
1420
1421         err = test_aead(driver, DECRYPT, suite, req, tsgls);
1422 out:
1423         free_cipher_test_sglists(tsgls);
1424         aead_request_free(req);
1425         crypto_free_aead(tfm);
1426         return err;
1427 }
1428
1429 static int test_cipher(struct crypto_cipher *tfm, int enc,
1430                        const struct cipher_testvec *template,
1431                        unsigned int tcount)
1432 {
1433         const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
1434         unsigned int i, j, k;
1435         char *q;
1436         const char *e;
1437         const char *input, *result;
1438         void *data;
1439         char *xbuf[XBUFSIZE];
1440         int ret = -ENOMEM;
1441
1442         if (testmgr_alloc_buf(xbuf))
1443                 goto out_nobuf;
1444
1445         if (enc == ENCRYPT)
1446                 e = "encryption";
1447         else
1448                 e = "decryption";
1449
1450         j = 0;
1451         for (i = 0; i < tcount; i++) {
1452
1453                 if (fips_enabled && template[i].fips_skip)
1454                         continue;
1455
1456                 input  = enc ? template[i].ptext : template[i].ctext;
1457                 result = enc ? template[i].ctext : template[i].ptext;
1458                 j++;
1459
1460                 ret = -EINVAL;
1461                 if (WARN_ON(template[i].len > PAGE_SIZE))
1462                         goto out;
1463
1464                 data = xbuf[0];
1465                 memcpy(data, input, template[i].len);
1466
1467                 crypto_cipher_clear_flags(tfm, ~0);
1468                 if (template[i].wk)
1469                         crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1470
1471                 ret = crypto_cipher_setkey(tfm, template[i].key,
1472                                            template[i].klen);
1473                 if (template[i].fail == !ret) {
1474                         printk(KERN_ERR "alg: cipher: setkey failed "
1475                                "on test %d for %s: flags=%x\n", j,
1476                                algo, crypto_cipher_get_flags(tfm));
1477                         goto out;
1478                 } else if (ret)
1479                         continue;
1480
1481                 for (k = 0; k < template[i].len;
1482                      k += crypto_cipher_blocksize(tfm)) {
1483                         if (enc)
1484                                 crypto_cipher_encrypt_one(tfm, data + k,
1485                                                           data + k);
1486                         else
1487                                 crypto_cipher_decrypt_one(tfm, data + k,
1488                                                           data + k);
1489                 }
1490
1491                 q = data;
1492                 if (memcmp(q, result, template[i].len)) {
1493                         printk(KERN_ERR "alg: cipher: Test %d failed "
1494                                "on %s for %s\n", j, e, algo);
1495                         hexdump(q, template[i].len);
1496                         ret = -EINVAL;
1497                         goto out;
1498                 }
1499         }
1500
1501         ret = 0;
1502
1503 out:
1504         testmgr_free_buf(xbuf);
1505 out_nobuf:
1506         return ret;
1507 }
1508
1509 static int test_skcipher_vec_cfg(const char *driver, int enc,
1510                                  const struct cipher_testvec *vec,
1511                                  unsigned int vec_num,
1512                                  const struct testvec_config *cfg,
1513                                  struct skcipher_request *req,
1514                                  struct cipher_test_sglists *tsgls)
1515 {
1516         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1517         const unsigned int alignmask = crypto_skcipher_alignmask(tfm);
1518         const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
1519         const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1520         const char *op = enc ? "encryption" : "decryption";
1521         DECLARE_CRYPTO_WAIT(wait);
1522         u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
1523         u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
1524                  cfg->iv_offset +
1525                  (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
1526         struct kvec input;
1527         int err;
1528
1529         /* Set the key */
1530         if (vec->wk)
1531                 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1532         else
1533                 crypto_skcipher_clear_flags(tfm,
1534                                             CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1535         err = crypto_skcipher_setkey(tfm, vec->key, vec->klen);
1536         if (err) {
1537                 if (vec->fail) /* expectedly failed to set key? */
1538                         return 0;
1539                 pr_err("alg: skcipher: %s setkey failed with err %d on test vector %u; flags=%#x\n",
1540                        driver, err, vec_num, crypto_skcipher_get_flags(tfm));
1541                 return err;
1542         }
1543         if (vec->fail) {
1544                 pr_err("alg: skcipher: %s setkey unexpectedly succeeded on test vector %u\n",
1545                        driver, vec_num);
1546                 return -EINVAL;
1547         }
1548
1549         /* The IV must be copied to a buffer, as the algorithm may modify it */
1550         if (ivsize) {
1551                 if (WARN_ON(ivsize > MAX_IVLEN))
1552                         return -EINVAL;
1553                 if (vec->iv && !(vec->generates_iv && enc))
1554                         memcpy(iv, vec->iv, ivsize);
1555                 else
1556                         memset(iv, 0, ivsize);
1557         } else {
1558                 if (vec->generates_iv) {
1559                         pr_err("alg: skcipher: %s has ivsize=0 but test vector %u generates IV!\n",
1560                                driver, vec_num);
1561                         return -EINVAL;
1562                 }
1563                 iv = NULL;
1564         }
1565
1566         /* Build the src/dst scatterlists */
1567         input.iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
1568         input.iov_len = vec->len;
1569         err = build_cipher_test_sglists(tsgls, cfg, alignmask,
1570                                         vec->len, vec->len, &input, 1);
1571         if (err) {
1572                 pr_err("alg: skcipher: %s %s: error preparing scatterlists for test vector %u, cfg=\"%s\"\n",
1573                        driver, op, vec_num, cfg->name);
1574                 return err;
1575         }
1576
1577         /* Do the actual encryption or decryption */
1578         testmgr_poison(req->__ctx, crypto_skcipher_reqsize(tfm));
1579         skcipher_request_set_callback(req, req_flags, crypto_req_done, &wait);
1580         skcipher_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
1581                                    vec->len, iv);
1582         err = crypto_wait_req(enc ? crypto_skcipher_encrypt(req) :
1583                               crypto_skcipher_decrypt(req), &wait);
1584         if (err) {
1585                 pr_err("alg: skcipher: %s %s failed with err %d on test vector %u, cfg=\"%s\"\n",
1586                        driver, op, err, vec_num, cfg->name);
1587                 return err;
1588         }
1589
1590         /* Check for the correct output (ciphertext or plaintext) */
1591         err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
1592                                     vec->len, 0, true);
1593         if (err == -EOVERFLOW) {
1594                 pr_err("alg: skcipher: %s %s overran dst buffer on test vector %u, cfg=\"%s\"\n",
1595                        driver, op, vec_num, cfg->name);
1596                 return err;
1597         }
1598         if (err) {
1599                 pr_err("alg: skcipher: %s %s test failed (wrong result) on test vector %u, cfg=\"%s\"\n",
1600                        driver, op, vec_num, cfg->name);
1601                 return err;
1602         }
1603
1604         /* If applicable, check that the algorithm generated the correct IV */
1605         if (vec->generates_iv && enc && memcmp(iv, vec->iv, ivsize) != 0) {
1606                 pr_err("alg: skcipher: %s %s test failed (wrong output IV) on test vector %u, cfg=\"%s\"\n",
1607                        driver, op, vec_num, cfg->name);
1608                 hexdump(iv, ivsize);
1609                 return -EINVAL;
1610         }
1611
1612         return 0;
1613 }
1614
1615 static int test_skcipher_vec(const char *driver, int enc,
1616                              const struct cipher_testvec *vec,
1617                              unsigned int vec_num,
1618                              struct skcipher_request *req,
1619                              struct cipher_test_sglists *tsgls)
1620 {
1621         unsigned int i;
1622         int err;
1623
1624         if (fips_enabled && vec->fips_skip)
1625                 return 0;
1626
1627         for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
1628                 err = test_skcipher_vec_cfg(driver, enc, vec, vec_num,
1629                                             &default_cipher_testvec_configs[i],
1630                                             req, tsgls);
1631                 if (err)
1632                         return err;
1633         }
1634
1635 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1636         if (!noextratests) {
1637                 struct testvec_config cfg;
1638                 char cfgname[TESTVEC_CONFIG_NAMELEN];
1639
1640                 for (i = 0; i < fuzz_iterations; i++) {
1641                         generate_random_testvec_config(&cfg, cfgname,
1642                                                        sizeof(cfgname));
1643                         err = test_skcipher_vec_cfg(driver, enc, vec, vec_num,
1644                                                     &cfg, req, tsgls);
1645                         if (err)
1646                                 return err;
1647                 }
1648         }
1649 #endif
1650         return 0;
1651 }
1652
1653 static int test_skcipher(const char *driver, int enc,
1654                          const struct cipher_test_suite *suite,
1655                          struct skcipher_request *req,
1656                          struct cipher_test_sglists *tsgls)
1657 {
1658         unsigned int i;
1659         int err;
1660
1661         for (i = 0; i < suite->count; i++) {
1662                 err = test_skcipher_vec(driver, enc, &suite->vecs[i], i, req,
1663                                         tsgls);
1664                 if (err)
1665                         return err;
1666         }
1667         return 0;
1668 }
1669
1670 static int alg_test_skcipher(const struct alg_test_desc *desc,
1671                              const char *driver, u32 type, u32 mask)
1672 {
1673         const struct cipher_test_suite *suite = &desc->suite.cipher;
1674         struct crypto_skcipher *tfm;
1675         struct skcipher_request *req = NULL;
1676         struct cipher_test_sglists *tsgls = NULL;
1677         int err;
1678
1679         if (suite->count <= 0) {
1680                 pr_err("alg: skcipher: empty test suite for %s\n", driver);
1681                 return -EINVAL;
1682         }
1683
1684         tfm = crypto_alloc_skcipher(driver, type, mask);
1685         if (IS_ERR(tfm)) {
1686                 pr_err("alg: skcipher: failed to allocate transform for %s: %ld\n",
1687                        driver, PTR_ERR(tfm));
1688                 return PTR_ERR(tfm);
1689         }
1690
1691         req = skcipher_request_alloc(tfm, GFP_KERNEL);
1692         if (!req) {
1693                 pr_err("alg: skcipher: failed to allocate request for %s\n",
1694                        driver);
1695                 err = -ENOMEM;
1696                 goto out;
1697         }
1698
1699         tsgls = alloc_cipher_test_sglists();
1700         if (!tsgls) {
1701                 pr_err("alg: skcipher: failed to allocate test buffers for %s\n",
1702                        driver);
1703                 err = -ENOMEM;
1704                 goto out;
1705         }
1706
1707         err = test_skcipher(driver, ENCRYPT, suite, req, tsgls);
1708         if (err)
1709                 goto out;
1710
1711         err = test_skcipher(driver, DECRYPT, suite, req, tsgls);
1712 out:
1713         free_cipher_test_sglists(tsgls);
1714         skcipher_request_free(req);
1715         crypto_free_skcipher(tfm);
1716         return err;
1717 }
1718
1719 static int test_comp(struct crypto_comp *tfm,
1720                      const struct comp_testvec *ctemplate,
1721                      const struct comp_testvec *dtemplate,
1722                      int ctcount, int dtcount)
1723 {
1724         const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
1725         char *output, *decomp_output;
1726         unsigned int i;
1727         int ret;
1728
1729         output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1730         if (!output)
1731                 return -ENOMEM;
1732
1733         decomp_output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1734         if (!decomp_output) {
1735                 kfree(output);
1736                 return -ENOMEM;
1737         }
1738
1739         for (i = 0; i < ctcount; i++) {
1740                 int ilen;
1741                 unsigned int dlen = COMP_BUF_SIZE;
1742
1743                 memset(output, 0, COMP_BUF_SIZE);
1744                 memset(decomp_output, 0, COMP_BUF_SIZE);
1745
1746                 ilen = ctemplate[i].inlen;
1747                 ret = crypto_comp_compress(tfm, ctemplate[i].input,
1748                                            ilen, output, &dlen);
1749                 if (ret) {
1750                         printk(KERN_ERR "alg: comp: compression failed "
1751                                "on test %d for %s: ret=%d\n", i + 1, algo,
1752                                -ret);
1753                         goto out;
1754                 }
1755
1756                 ilen = dlen;
1757                 dlen = COMP_BUF_SIZE;
1758                 ret = crypto_comp_decompress(tfm, output,
1759                                              ilen, decomp_output, &dlen);
1760                 if (ret) {
1761                         pr_err("alg: comp: compression failed: decompress: on test %d for %s failed: ret=%d\n",
1762                                i + 1, algo, -ret);
1763                         goto out;
1764                 }
1765
1766                 if (dlen != ctemplate[i].inlen) {
1767                         printk(KERN_ERR "alg: comp: Compression test %d "
1768                                "failed for %s: output len = %d\n", i + 1, algo,
1769                                dlen);
1770                         ret = -EINVAL;
1771                         goto out;
1772                 }
1773
1774                 if (memcmp(decomp_output, ctemplate[i].input,
1775                            ctemplate[i].inlen)) {
1776                         pr_err("alg: comp: compression failed: output differs: on test %d for %s\n",
1777                                i + 1, algo);
1778                         hexdump(decomp_output, dlen);
1779                         ret = -EINVAL;
1780                         goto out;
1781                 }
1782         }
1783
1784         for (i = 0; i < dtcount; i++) {
1785                 int ilen;
1786                 unsigned int dlen = COMP_BUF_SIZE;
1787
1788                 memset(decomp_output, 0, COMP_BUF_SIZE);
1789
1790                 ilen = dtemplate[i].inlen;
1791                 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
1792                                              ilen, decomp_output, &dlen);
1793                 if (ret) {
1794                         printk(KERN_ERR "alg: comp: decompression failed "
1795                                "on test %d for %s: ret=%d\n", i + 1, algo,
1796                                -ret);
1797                         goto out;
1798                 }
1799
1800                 if (dlen != dtemplate[i].outlen) {
1801                         printk(KERN_ERR "alg: comp: Decompression test %d "
1802                                "failed for %s: output len = %d\n", i + 1, algo,
1803                                dlen);
1804                         ret = -EINVAL;
1805                         goto out;
1806                 }
1807
1808                 if (memcmp(decomp_output, dtemplate[i].output, dlen)) {
1809                         printk(KERN_ERR "alg: comp: Decompression test %d "
1810                                "failed for %s\n", i + 1, algo);
1811                         hexdump(decomp_output, dlen);
1812                         ret = -EINVAL;
1813                         goto out;
1814                 }
1815         }
1816
1817         ret = 0;
1818
1819 out:
1820         kfree(decomp_output);
1821         kfree(output);
1822         return ret;
1823 }
1824
1825 static int test_acomp(struct crypto_acomp *tfm,
1826                               const struct comp_testvec *ctemplate,
1827                       const struct comp_testvec *dtemplate,
1828                       int ctcount, int dtcount)
1829 {
1830         const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
1831         unsigned int i;
1832         char *output, *decomp_out;
1833         int ret;
1834         struct scatterlist src, dst;
1835         struct acomp_req *req;
1836         struct crypto_wait wait;
1837
1838         output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1839         if (!output)
1840                 return -ENOMEM;
1841
1842         decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1843         if (!decomp_out) {
1844                 kfree(output);
1845                 return -ENOMEM;
1846         }
1847
1848         for (i = 0; i < ctcount; i++) {
1849                 unsigned int dlen = COMP_BUF_SIZE;
1850                 int ilen = ctemplate[i].inlen;
1851                 void *input_vec;
1852
1853                 input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL);
1854                 if (!input_vec) {
1855                         ret = -ENOMEM;
1856                         goto out;
1857                 }
1858
1859                 memset(output, 0, dlen);
1860                 crypto_init_wait(&wait);
1861                 sg_init_one(&src, input_vec, ilen);
1862                 sg_init_one(&dst, output, dlen);
1863
1864                 req = acomp_request_alloc(tfm);
1865                 if (!req) {
1866                         pr_err("alg: acomp: request alloc failed for %s\n",
1867                                algo);
1868                         kfree(input_vec);
1869                         ret = -ENOMEM;
1870                         goto out;
1871                 }
1872
1873                 acomp_request_set_params(req, &src, &dst, ilen, dlen);
1874                 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1875                                            crypto_req_done, &wait);
1876
1877                 ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
1878                 if (ret) {
1879                         pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
1880                                i + 1, algo, -ret);
1881                         kfree(input_vec);
1882                         acomp_request_free(req);
1883                         goto out;
1884                 }
1885
1886                 ilen = req->dlen;
1887                 dlen = COMP_BUF_SIZE;
1888                 sg_init_one(&src, output, ilen);
1889                 sg_init_one(&dst, decomp_out, dlen);
1890                 crypto_init_wait(&wait);
1891                 acomp_request_set_params(req, &src, &dst, ilen, dlen);
1892
1893                 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
1894                 if (ret) {
1895                         pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
1896                                i + 1, algo, -ret);
1897                         kfree(input_vec);
1898                         acomp_request_free(req);
1899                         goto out;
1900                 }
1901
1902                 if (req->dlen != ctemplate[i].inlen) {
1903                         pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
1904                                i + 1, algo, req->dlen);
1905                         ret = -EINVAL;
1906                         kfree(input_vec);
1907                         acomp_request_free(req);
1908                         goto out;
1909                 }
1910
1911                 if (memcmp(input_vec, decomp_out, req->dlen)) {
1912                         pr_err("alg: acomp: Compression test %d failed for %s\n",
1913                                i + 1, algo);
1914                         hexdump(output, req->dlen);
1915                         ret = -EINVAL;
1916                         kfree(input_vec);
1917                         acomp_request_free(req);
1918                         goto out;
1919                 }
1920
1921                 kfree(input_vec);
1922                 acomp_request_free(req);
1923         }
1924
1925         for (i = 0; i < dtcount; i++) {
1926                 unsigned int dlen = COMP_BUF_SIZE;
1927                 int ilen = dtemplate[i].inlen;
1928                 void *input_vec;
1929
1930                 input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL);
1931                 if (!input_vec) {
1932                         ret = -ENOMEM;
1933                         goto out;
1934                 }
1935
1936                 memset(output, 0, dlen);
1937                 crypto_init_wait(&wait);
1938                 sg_init_one(&src, input_vec, ilen);
1939                 sg_init_one(&dst, output, dlen);
1940
1941                 req = acomp_request_alloc(tfm);
1942                 if (!req) {
1943                         pr_err("alg: acomp: request alloc failed for %s\n",
1944                                algo);
1945                         kfree(input_vec);
1946                         ret = -ENOMEM;
1947                         goto out;
1948                 }
1949
1950                 acomp_request_set_params(req, &src, &dst, ilen, dlen);
1951                 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1952                                            crypto_req_done, &wait);
1953
1954                 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
1955                 if (ret) {
1956                         pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
1957                                i + 1, algo, -ret);
1958                         kfree(input_vec);
1959                         acomp_request_free(req);
1960                         goto out;
1961                 }
1962
1963                 if (req->dlen != dtemplate[i].outlen) {
1964                         pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
1965                                i + 1, algo, req->dlen);
1966                         ret = -EINVAL;
1967                         kfree(input_vec);
1968                         acomp_request_free(req);
1969                         goto out;
1970                 }
1971
1972                 if (memcmp(output, dtemplate[i].output, req->dlen)) {
1973                         pr_err("alg: acomp: Decompression test %d failed for %s\n",
1974                                i + 1, algo);
1975                         hexdump(output, req->dlen);
1976                         ret = -EINVAL;
1977                         kfree(input_vec);
1978                         acomp_request_free(req);
1979                         goto out;
1980                 }
1981
1982                 kfree(input_vec);
1983                 acomp_request_free(req);
1984         }
1985
1986         ret = 0;
1987
1988 out:
1989         kfree(decomp_out);
1990         kfree(output);
1991         return ret;
1992 }
1993
1994 static int test_cprng(struct crypto_rng *tfm,
1995                       const struct cprng_testvec *template,
1996                       unsigned int tcount)
1997 {
1998         const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
1999         int err = 0, i, j, seedsize;
2000         u8 *seed;
2001         char result[32];
2002
2003         seedsize = crypto_rng_seedsize(tfm);
2004
2005         seed = kmalloc(seedsize, GFP_KERNEL);
2006         if (!seed) {
2007                 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
2008                        "for %s\n", algo);
2009                 return -ENOMEM;
2010         }
2011
2012         for (i = 0; i < tcount; i++) {
2013                 memset(result, 0, 32);
2014
2015                 memcpy(seed, template[i].v, template[i].vlen);
2016                 memcpy(seed + template[i].vlen, template[i].key,
2017                        template[i].klen);
2018                 memcpy(seed + template[i].vlen + template[i].klen,
2019                        template[i].dt, template[i].dtlen);
2020
2021                 err = crypto_rng_reset(tfm, seed, seedsize);
2022                 if (err) {
2023                         printk(KERN_ERR "alg: cprng: Failed to reset rng "
2024                                "for %s\n", algo);
2025                         goto out;
2026                 }
2027
2028                 for (j = 0; j < template[i].loops; j++) {
2029                         err = crypto_rng_get_bytes(tfm, result,
2030                                                    template[i].rlen);
2031                         if (err < 0) {
2032                                 printk(KERN_ERR "alg: cprng: Failed to obtain "
2033                                        "the correct amount of random data for "
2034                                        "%s (requested %d)\n", algo,
2035                                        template[i].rlen);
2036                                 goto out;
2037                         }
2038                 }
2039
2040                 err = memcmp(result, template[i].result,
2041                              template[i].rlen);
2042                 if (err) {
2043                         printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
2044                                i, algo);
2045                         hexdump(result, template[i].rlen);
2046                         err = -EINVAL;
2047                         goto out;
2048                 }
2049         }
2050
2051 out:
2052         kfree(seed);
2053         return err;
2054 }
2055
2056 static int alg_test_cipher(const struct alg_test_desc *desc,
2057                            const char *driver, u32 type, u32 mask)
2058 {
2059         const struct cipher_test_suite *suite = &desc->suite.cipher;
2060         struct crypto_cipher *tfm;
2061         int err;
2062
2063         tfm = crypto_alloc_cipher(driver, type, mask);
2064         if (IS_ERR(tfm)) {
2065                 printk(KERN_ERR "alg: cipher: Failed to load transform for "
2066                        "%s: %ld\n", driver, PTR_ERR(tfm));
2067                 return PTR_ERR(tfm);
2068         }
2069
2070         err = test_cipher(tfm, ENCRYPT, suite->vecs, suite->count);
2071         if (!err)
2072                 err = test_cipher(tfm, DECRYPT, suite->vecs, suite->count);
2073
2074         crypto_free_cipher(tfm);
2075         return err;
2076 }
2077
2078 static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
2079                          u32 type, u32 mask)
2080 {
2081         struct crypto_comp *comp;
2082         struct crypto_acomp *acomp;
2083         int err;
2084         u32 algo_type = type & CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
2085
2086         if (algo_type == CRYPTO_ALG_TYPE_ACOMPRESS) {
2087                 acomp = crypto_alloc_acomp(driver, type, mask);
2088                 if (IS_ERR(acomp)) {
2089                         pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
2090                                driver, PTR_ERR(acomp));
2091                         return PTR_ERR(acomp);
2092                 }
2093                 err = test_acomp(acomp, desc->suite.comp.comp.vecs,
2094                                  desc->suite.comp.decomp.vecs,
2095                                  desc->suite.comp.comp.count,
2096                                  desc->suite.comp.decomp.count);
2097                 crypto_free_acomp(acomp);
2098         } else {
2099                 comp = crypto_alloc_comp(driver, type, mask);
2100                 if (IS_ERR(comp)) {
2101                         pr_err("alg: comp: Failed to load transform for %s: %ld\n",
2102                                driver, PTR_ERR(comp));
2103                         return PTR_ERR(comp);
2104                 }
2105
2106                 err = test_comp(comp, desc->suite.comp.comp.vecs,
2107                                 desc->suite.comp.decomp.vecs,
2108                                 desc->suite.comp.comp.count,
2109                                 desc->suite.comp.decomp.count);
2110
2111                 crypto_free_comp(comp);
2112         }
2113         return err;
2114 }
2115
2116 static int __alg_test_hash(const struct hash_testvec *template,
2117                            unsigned int tcount, const char *driver,
2118                            u32 type, u32 mask)
2119 {
2120         struct crypto_ahash *tfm;
2121         int err;
2122
2123         tfm = crypto_alloc_ahash(driver, type, mask);
2124         if (IS_ERR(tfm)) {
2125                 printk(KERN_ERR "alg: hash: Failed to load transform for %s: "
2126                        "%ld\n", driver, PTR_ERR(tfm));
2127                 return PTR_ERR(tfm);
2128         }
2129
2130         err = test_hash(tfm, template, tcount, HASH_TEST_DIGEST);
2131         if (!err)
2132                 err = test_hash(tfm, template, tcount, HASH_TEST_FINAL);
2133         if (!err)
2134                 err = test_hash(tfm, template, tcount, HASH_TEST_FINUP);
2135         crypto_free_ahash(tfm);
2136         return err;
2137 }
2138
2139 static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
2140                          u32 type, u32 mask)
2141 {
2142         const struct hash_testvec *template = desc->suite.hash.vecs;
2143         unsigned int tcount = desc->suite.hash.count;
2144         unsigned int nr_unkeyed, nr_keyed;
2145         int err;
2146
2147         /*
2148          * For OPTIONAL_KEY algorithms, we have to do all the unkeyed tests
2149          * first, before setting a key on the tfm.  To make this easier, we
2150          * require that the unkeyed test vectors (if any) are listed first.
2151          */
2152
2153         for (nr_unkeyed = 0; nr_unkeyed < tcount; nr_unkeyed++) {
2154                 if (template[nr_unkeyed].ksize)
2155                         break;
2156         }
2157         for (nr_keyed = 0; nr_unkeyed + nr_keyed < tcount; nr_keyed++) {
2158                 if (!template[nr_unkeyed + nr_keyed].ksize) {
2159                         pr_err("alg: hash: test vectors for %s out of order, "
2160                                "unkeyed ones must come first\n", desc->alg);
2161                         return -EINVAL;
2162                 }
2163         }
2164
2165         err = 0;
2166         if (nr_unkeyed) {
2167                 err = __alg_test_hash(template, nr_unkeyed, driver, type, mask);
2168                 template += nr_unkeyed;
2169         }
2170
2171         if (!err && nr_keyed)
2172                 err = __alg_test_hash(template, nr_keyed, driver, type, mask);
2173
2174         return err;
2175 }
2176
2177 static int alg_test_crc32c(const struct alg_test_desc *desc,
2178                            const char *driver, u32 type, u32 mask)
2179 {
2180         struct crypto_shash *tfm;
2181         __le32 val;
2182         int err;
2183
2184         err = alg_test_hash(desc, driver, type, mask);
2185         if (err)
2186                 return err;
2187
2188         tfm = crypto_alloc_shash(driver, type, mask);
2189         if (IS_ERR(tfm)) {
2190                 if (PTR_ERR(tfm) == -ENOENT) {
2191                         /*
2192                          * This crc32c implementation is only available through
2193                          * ahash API, not the shash API, so the remaining part
2194                          * of the test is not applicable to it.
2195                          */
2196                         return 0;
2197                 }
2198                 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
2199                        "%ld\n", driver, PTR_ERR(tfm));
2200                 return PTR_ERR(tfm);
2201         }
2202
2203         do {
2204                 SHASH_DESC_ON_STACK(shash, tfm);
2205                 u32 *ctx = (u32 *)shash_desc_ctx(shash);
2206
2207                 shash->tfm = tfm;
2208                 shash->flags = 0;
2209
2210                 *ctx = 420553207;
2211                 err = crypto_shash_final(shash, (u8 *)&val);
2212                 if (err) {
2213                         printk(KERN_ERR "alg: crc32c: Operation failed for "
2214                                "%s: %d\n", driver, err);
2215                         break;
2216                 }
2217
2218                 if (val != cpu_to_le32(~420553207)) {
2219                         pr_err("alg: crc32c: Test failed for %s: %u\n",
2220                                driver, le32_to_cpu(val));
2221                         err = -EINVAL;
2222                 }
2223         } while (0);
2224
2225         crypto_free_shash(tfm);
2226
2227         return err;
2228 }
2229
2230 static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
2231                           u32 type, u32 mask)
2232 {
2233         struct crypto_rng *rng;
2234         int err;
2235
2236         rng = crypto_alloc_rng(driver, type, mask);
2237         if (IS_ERR(rng)) {
2238                 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
2239                        "%ld\n", driver, PTR_ERR(rng));
2240                 return PTR_ERR(rng);
2241         }
2242
2243         err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
2244
2245         crypto_free_rng(rng);
2246
2247         return err;
2248 }
2249
2250
2251 static int drbg_cavs_test(const struct drbg_testvec *test, int pr,
2252                           const char *driver, u32 type, u32 mask)
2253 {
2254         int ret = -EAGAIN;
2255         struct crypto_rng *drng;
2256         struct drbg_test_data test_data;
2257         struct drbg_string addtl, pers, testentropy;
2258         unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
2259
2260         if (!buf)
2261                 return -ENOMEM;
2262
2263         drng = crypto_alloc_rng(driver, type, mask);
2264         if (IS_ERR(drng)) {
2265                 printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
2266                        "%s\n", driver);
2267                 kzfree(buf);
2268                 return -ENOMEM;
2269         }
2270
2271         test_data.testentropy = &testentropy;
2272         drbg_string_fill(&testentropy, test->entropy, test->entropylen);
2273         drbg_string_fill(&pers, test->pers, test->perslen);
2274         ret = crypto_drbg_reset_test(drng, &pers, &test_data);
2275         if (ret) {
2276                 printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
2277                 goto outbuf;
2278         }
2279
2280         drbg_string_fill(&addtl, test->addtla, test->addtllen);
2281         if (pr) {
2282                 drbg_string_fill(&testentropy, test->entpra, test->entprlen);
2283                 ret = crypto_drbg_get_bytes_addtl_test(drng,
2284                         buf, test->expectedlen, &addtl, &test_data);
2285         } else {
2286                 ret = crypto_drbg_get_bytes_addtl(drng,
2287                         buf, test->expectedlen, &addtl);
2288         }
2289         if (ret < 0) {
2290                 printk(KERN_ERR "alg: drbg: could not obtain random data for "
2291                        "driver %s\n", driver);
2292                 goto outbuf;
2293         }
2294
2295         drbg_string_fill(&addtl, test->addtlb, test->addtllen);
2296         if (pr) {
2297                 drbg_string_fill(&testentropy, test->entprb, test->entprlen);
2298                 ret = crypto_drbg_get_bytes_addtl_test(drng,
2299                         buf, test->expectedlen, &addtl, &test_data);
2300         } else {
2301                 ret = crypto_drbg_get_bytes_addtl(drng,
2302                         buf, test->expectedlen, &addtl);
2303         }
2304         if (ret < 0) {
2305                 printk(KERN_ERR "alg: drbg: could not obtain random data for "
2306                        "driver %s\n", driver);
2307                 goto outbuf;
2308         }
2309
2310         ret = memcmp(test->expected, buf, test->expectedlen);
2311
2312 outbuf:
2313         crypto_free_rng(drng);
2314         kzfree(buf);
2315         return ret;
2316 }
2317
2318
2319 static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
2320                          u32 type, u32 mask)
2321 {
2322         int err = 0;
2323         int pr = 0;
2324         int i = 0;
2325         const struct drbg_testvec *template = desc->suite.drbg.vecs;
2326         unsigned int tcount = desc->suite.drbg.count;
2327
2328         if (0 == memcmp(driver, "drbg_pr_", 8))
2329                 pr = 1;
2330
2331         for (i = 0; i < tcount; i++) {
2332                 err = drbg_cavs_test(&template[i], pr, driver, type, mask);
2333                 if (err) {
2334                         printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
2335                                i, driver);
2336                         err = -EINVAL;
2337                         break;
2338                 }
2339         }
2340         return err;
2341
2342 }
2343
2344 static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
2345                        const char *alg)
2346 {
2347         struct kpp_request *req;
2348         void *input_buf = NULL;
2349         void *output_buf = NULL;
2350         void *a_public = NULL;
2351         void *a_ss = NULL;
2352         void *shared_secret = NULL;
2353         struct crypto_wait wait;
2354         unsigned int out_len_max;
2355         int err = -ENOMEM;
2356         struct scatterlist src, dst;
2357
2358         req = kpp_request_alloc(tfm, GFP_KERNEL);
2359         if (!req)
2360                 return err;
2361
2362         crypto_init_wait(&wait);
2363
2364         err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
2365         if (err < 0)
2366                 goto free_req;
2367
2368         out_len_max = crypto_kpp_maxsize(tfm);
2369         output_buf = kzalloc(out_len_max, GFP_KERNEL);
2370         if (!output_buf) {
2371                 err = -ENOMEM;
2372                 goto free_req;
2373         }
2374
2375         /* Use appropriate parameter as base */
2376         kpp_request_set_input(req, NULL, 0);
2377         sg_init_one(&dst, output_buf, out_len_max);
2378         kpp_request_set_output(req, &dst, out_len_max);
2379         kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2380                                  crypto_req_done, &wait);
2381
2382         /* Compute party A's public key */
2383         err = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
2384         if (err) {
2385                 pr_err("alg: %s: Party A: generate public key test failed. err %d\n",
2386                        alg, err);
2387                 goto free_output;
2388         }
2389
2390         if (vec->genkey) {
2391                 /* Save party A's public key */
2392                 a_public = kmemdup(sg_virt(req->dst), out_len_max, GFP_KERNEL);
2393                 if (!a_public) {
2394                         err = -ENOMEM;
2395                         goto free_output;
2396                 }
2397         } else {
2398                 /* Verify calculated public key */
2399                 if (memcmp(vec->expected_a_public, sg_virt(req->dst),
2400                            vec->expected_a_public_size)) {
2401                         pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n",
2402                                alg);
2403                         err = -EINVAL;
2404                         goto free_output;
2405                 }
2406         }
2407
2408         /* Calculate shared secret key by using counter part (b) public key. */
2409         input_buf = kmemdup(vec->b_public, vec->b_public_size, GFP_KERNEL);
2410         if (!input_buf) {
2411                 err = -ENOMEM;
2412                 goto free_output;
2413         }
2414
2415         sg_init_one(&src, input_buf, vec->b_public_size);
2416         sg_init_one(&dst, output_buf, out_len_max);
2417         kpp_request_set_input(req, &src, vec->b_public_size);
2418         kpp_request_set_output(req, &dst, out_len_max);
2419         kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2420                                  crypto_req_done, &wait);
2421         err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait);
2422         if (err) {
2423                 pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n",
2424                        alg, err);
2425                 goto free_all;
2426         }
2427
2428         if (vec->genkey) {
2429                 /* Save the shared secret obtained by party A */
2430                 a_ss = kmemdup(sg_virt(req->dst), vec->expected_ss_size, GFP_KERNEL);
2431                 if (!a_ss) {
2432                         err = -ENOMEM;
2433                         goto free_all;
2434                 }
2435
2436                 /*
2437                  * Calculate party B's shared secret by using party A's
2438                  * public key.
2439                  */
2440                 err = crypto_kpp_set_secret(tfm, vec->b_secret,
2441                                             vec->b_secret_size);
2442                 if (err < 0)
2443                         goto free_all;
2444
2445                 sg_init_one(&src, a_public, vec->expected_a_public_size);
2446                 sg_init_one(&dst, output_buf, out_len_max);
2447                 kpp_request_set_input(req, &src, vec->expected_a_public_size);
2448                 kpp_request_set_output(req, &dst, out_len_max);
2449                 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2450                                          crypto_req_done, &wait);
2451                 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req),
2452                                       &wait);
2453                 if (err) {
2454                         pr_err("alg: %s: Party B: compute shared secret failed. err %d\n",
2455                                alg, err);
2456                         goto free_all;
2457                 }
2458
2459                 shared_secret = a_ss;
2460         } else {
2461                 shared_secret = (void *)vec->expected_ss;
2462         }
2463
2464         /*
2465          * verify shared secret from which the user will derive
2466          * secret key by executing whatever hash it has chosen
2467          */
2468         if (memcmp(shared_secret, sg_virt(req->dst),
2469                    vec->expected_ss_size)) {
2470                 pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
2471                        alg);
2472                 err = -EINVAL;
2473         }
2474
2475 free_all:
2476         kfree(a_ss);
2477         kfree(input_buf);
2478 free_output:
2479         kfree(a_public);
2480         kfree(output_buf);
2481 free_req:
2482         kpp_request_free(req);
2483         return err;
2484 }
2485
2486 static int test_kpp(struct crypto_kpp *tfm, const char *alg,
2487                     const struct kpp_testvec *vecs, unsigned int tcount)
2488 {
2489         int ret, i;
2490
2491         for (i = 0; i < tcount; i++) {
2492                 ret = do_test_kpp(tfm, vecs++, alg);
2493                 if (ret) {
2494                         pr_err("alg: %s: test failed on vector %d, err=%d\n",
2495                                alg, i + 1, ret);
2496                         return ret;
2497                 }
2498         }
2499         return 0;
2500 }
2501
2502 static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
2503                         u32 type, u32 mask)
2504 {
2505         struct crypto_kpp *tfm;
2506         int err = 0;
2507
2508         tfm = crypto_alloc_kpp(driver, type, mask);
2509         if (IS_ERR(tfm)) {
2510                 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
2511                        driver, PTR_ERR(tfm));
2512                 return PTR_ERR(tfm);
2513         }
2514         if (desc->suite.kpp.vecs)
2515                 err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
2516                                desc->suite.kpp.count);
2517
2518         crypto_free_kpp(tfm);
2519         return err;
2520 }
2521
2522 static int test_akcipher_one(struct crypto_akcipher *tfm,
2523                              const struct akcipher_testvec *vecs)
2524 {
2525         char *xbuf[XBUFSIZE];
2526         struct akcipher_request *req;
2527         void *outbuf_enc = NULL;
2528         void *outbuf_dec = NULL;
2529         struct crypto_wait wait;
2530         unsigned int out_len_max, out_len = 0;
2531         int err = -ENOMEM;
2532         struct scatterlist src, dst, src_tab[2];
2533         const char *m, *c;
2534         unsigned int m_size, c_size;
2535         const char *op;
2536
2537         if (testmgr_alloc_buf(xbuf))
2538                 return err;
2539
2540         req = akcipher_request_alloc(tfm, GFP_KERNEL);
2541         if (!req)
2542                 goto free_xbuf;
2543
2544         crypto_init_wait(&wait);
2545
2546         if (vecs->public_key_vec)
2547                 err = crypto_akcipher_set_pub_key(tfm, vecs->key,
2548                                                   vecs->key_len);
2549         else
2550                 err = crypto_akcipher_set_priv_key(tfm, vecs->key,
2551                                                    vecs->key_len);
2552         if (err)
2553                 goto free_req;
2554
2555         err = -ENOMEM;
2556         out_len_max = crypto_akcipher_maxsize(tfm);
2557
2558         /*
2559          * First run test which do not require a private key, such as
2560          * encrypt or verify.
2561          */
2562         outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
2563         if (!outbuf_enc)
2564                 goto free_req;
2565
2566         if (!vecs->siggen_sigver_test) {
2567                 m = vecs->m;
2568                 m_size = vecs->m_size;
2569                 c = vecs->c;
2570                 c_size = vecs->c_size;
2571                 op = "encrypt";
2572         } else {
2573                 /* Swap args so we could keep plaintext (digest)
2574                  * in vecs->m, and cooked signature in vecs->c.
2575                  */
2576                 m = vecs->c; /* signature */
2577                 m_size = vecs->c_size;
2578                 c = vecs->m; /* digest */
2579                 c_size = vecs->m_size;
2580                 op = "verify";
2581         }
2582
2583         if (WARN_ON(m_size > PAGE_SIZE))
2584                 goto free_all;
2585         memcpy(xbuf[0], m, m_size);
2586
2587         sg_init_table(src_tab, 2);
2588         sg_set_buf(&src_tab[0], xbuf[0], 8);
2589         sg_set_buf(&src_tab[1], xbuf[0] + 8, m_size - 8);
2590         sg_init_one(&dst, outbuf_enc, out_len_max);
2591         akcipher_request_set_crypt(req, src_tab, &dst, m_size,
2592                                    out_len_max);
2593         akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2594                                       crypto_req_done, &wait);
2595
2596         err = crypto_wait_req(vecs->siggen_sigver_test ?
2597                               /* Run asymmetric signature verification */
2598                               crypto_akcipher_verify(req) :
2599                               /* Run asymmetric encrypt */
2600                               crypto_akcipher_encrypt(req), &wait);
2601         if (err) {
2602                 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
2603                 goto free_all;
2604         }
2605         if (req->dst_len != c_size) {
2606                 pr_err("alg: akcipher: %s test failed. Invalid output len\n",
2607                        op);
2608                 err = -EINVAL;
2609                 goto free_all;
2610         }
2611         /* verify that encrypted message is equal to expected */
2612         if (memcmp(c, outbuf_enc, c_size)) {
2613                 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
2614                 hexdump(outbuf_enc, c_size);
2615                 err = -EINVAL;
2616                 goto free_all;
2617         }
2618
2619         /*
2620          * Don't invoke (decrypt or sign) test which require a private key
2621          * for vectors with only a public key.
2622          */
2623         if (vecs->public_key_vec) {
2624                 err = 0;
2625                 goto free_all;
2626         }
2627         outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
2628         if (!outbuf_dec) {
2629                 err = -ENOMEM;
2630                 goto free_all;
2631         }
2632
2633         op = vecs->siggen_sigver_test ? "sign" : "decrypt";
2634         if (WARN_ON(c_size > PAGE_SIZE))
2635                 goto free_all;
2636         memcpy(xbuf[0], c, c_size);
2637
2638         sg_init_one(&src, xbuf[0], c_size);
2639         sg_init_one(&dst, outbuf_dec, out_len_max);
2640         crypto_init_wait(&wait);
2641         akcipher_request_set_crypt(req, &src, &dst, c_size, out_len_max);
2642
2643         err = crypto_wait_req(vecs->siggen_sigver_test ?
2644                               /* Run asymmetric signature generation */
2645                               crypto_akcipher_sign(req) :
2646                               /* Run asymmetric decrypt */
2647                               crypto_akcipher_decrypt(req), &wait);
2648         if (err) {
2649                 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
2650                 goto free_all;
2651         }
2652         out_len = req->dst_len;
2653         if (out_len < m_size) {
2654                 pr_err("alg: akcipher: %s test failed. Invalid output len %u\n",
2655                        op, out_len);
2656                 err = -EINVAL;
2657                 goto free_all;
2658         }
2659         /* verify that decrypted message is equal to the original msg */
2660         if (memchr_inv(outbuf_dec, 0, out_len - m_size) ||
2661             memcmp(m, outbuf_dec + out_len - m_size, m_size)) {
2662                 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
2663                 hexdump(outbuf_dec, out_len);
2664                 err = -EINVAL;
2665         }
2666 free_all:
2667         kfree(outbuf_dec);
2668         kfree(outbuf_enc);
2669 free_req:
2670         akcipher_request_free(req);
2671 free_xbuf:
2672         testmgr_free_buf(xbuf);
2673         return err;
2674 }
2675
2676 static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
2677                          const struct akcipher_testvec *vecs,
2678                          unsigned int tcount)
2679 {
2680         const char *algo =
2681                 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
2682         int ret, i;
2683
2684         for (i = 0; i < tcount; i++) {
2685                 ret = test_akcipher_one(tfm, vecs++);
2686                 if (!ret)
2687                         continue;
2688
2689                 pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
2690                        i + 1, algo, ret);
2691                 return ret;
2692         }
2693         return 0;
2694 }
2695
2696 static int alg_test_akcipher(const struct alg_test_desc *desc,
2697                              const char *driver, u32 type, u32 mask)
2698 {
2699         struct crypto_akcipher *tfm;
2700         int err = 0;
2701
2702         tfm = crypto_alloc_akcipher(driver, type, mask);
2703         if (IS_ERR(tfm)) {
2704                 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
2705                        driver, PTR_ERR(tfm));
2706                 return PTR_ERR(tfm);
2707         }
2708         if (desc->suite.akcipher.vecs)
2709                 err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
2710                                     desc->suite.akcipher.count);
2711
2712         crypto_free_akcipher(tfm);
2713         return err;
2714 }
2715
2716 static int alg_test_null(const struct alg_test_desc *desc,
2717                              const char *driver, u32 type, u32 mask)
2718 {
2719         return 0;
2720 }
2721
2722 #define __VECS(tv)      { .vecs = tv, .count = ARRAY_SIZE(tv) }
2723
2724 /* Please keep this list sorted by algorithm name. */
2725 static const struct alg_test_desc alg_test_descs[] = {
2726         {
2727                 .alg = "adiantum(xchacha12,aes)",
2728                 .test = alg_test_skcipher,
2729                 .suite = {
2730                         .cipher = __VECS(adiantum_xchacha12_aes_tv_template)
2731                 },
2732         }, {
2733                 .alg = "adiantum(xchacha20,aes)",
2734                 .test = alg_test_skcipher,
2735                 .suite = {
2736                         .cipher = __VECS(adiantum_xchacha20_aes_tv_template)
2737                 },
2738         }, {
2739                 .alg = "aegis128",
2740                 .test = alg_test_aead,
2741                 .suite = {
2742                         .aead = __VECS(aegis128_tv_template)
2743                 }
2744         }, {
2745                 .alg = "aegis128l",
2746                 .test = alg_test_aead,
2747                 .suite = {
2748                         .aead = __VECS(aegis128l_tv_template)
2749                 }
2750         }, {
2751                 .alg = "aegis256",
2752                 .test = alg_test_aead,
2753                 .suite = {
2754                         .aead = __VECS(aegis256_tv_template)
2755                 }
2756         }, {
2757                 .alg = "ansi_cprng",
2758                 .test = alg_test_cprng,
2759                 .suite = {
2760                         .cprng = __VECS(ansi_cprng_aes_tv_template)
2761                 }
2762         }, {
2763                 .alg = "authenc(hmac(md5),ecb(cipher_null))",
2764                 .test = alg_test_aead,
2765                 .suite = {
2766                         .aead = __VECS(hmac_md5_ecb_cipher_null_tv_template)
2767                 }
2768         }, {
2769                 .alg = "authenc(hmac(sha1),cbc(aes))",
2770                 .test = alg_test_aead,
2771                 .fips_allowed = 1,
2772                 .suite = {
2773                         .aead = __VECS(hmac_sha1_aes_cbc_tv_temp)
2774                 }
2775         }, {
2776                 .alg = "authenc(hmac(sha1),cbc(des))",
2777                 .test = alg_test_aead,
2778                 .suite = {
2779                         .aead = __VECS(hmac_sha1_des_cbc_tv_temp)
2780                 }
2781         }, {
2782                 .alg = "authenc(hmac(sha1),cbc(des3_ede))",
2783                 .test = alg_test_aead,
2784                 .fips_allowed = 1,
2785                 .suite = {
2786                         .aead = __VECS(hmac_sha1_des3_ede_cbc_tv_temp)
2787                 }
2788         }, {
2789                 .alg = "authenc(hmac(sha1),ctr(aes))",
2790                 .test = alg_test_null,
2791                 .fips_allowed = 1,
2792         }, {
2793                 .alg = "authenc(hmac(sha1),ecb(cipher_null))",
2794                 .test = alg_test_aead,
2795                 .suite = {
2796                         .aead = __VECS(hmac_sha1_ecb_cipher_null_tv_temp)
2797                 }
2798         }, {
2799                 .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
2800                 .test = alg_test_null,
2801                 .fips_allowed = 1,
2802         }, {
2803                 .alg = "authenc(hmac(sha224),cbc(des))",
2804                 .test = alg_test_aead,
2805                 .suite = {
2806                         .aead = __VECS(hmac_sha224_des_cbc_tv_temp)
2807                 }
2808         }, {
2809                 .alg = "authenc(hmac(sha224),cbc(des3_ede))",
2810                 .test = alg_test_aead,
2811                 .fips_allowed = 1,
2812                 .suite = {
2813                         .aead = __VECS(hmac_sha224_des3_ede_cbc_tv_temp)
2814                 }
2815         }, {
2816                 .alg = "authenc(hmac(sha256),cbc(aes))",
2817                 .test = alg_test_aead,
2818                 .fips_allowed = 1,
2819                 .suite = {
2820                         .aead = __VECS(hmac_sha256_aes_cbc_tv_temp)
2821                 }
2822         }, {
2823                 .alg = "authenc(hmac(sha256),cbc(des))",
2824                 .test = alg_test_aead,
2825                 .suite = {
2826                         .aead = __VECS(hmac_sha256_des_cbc_tv_temp)
2827                 }
2828         }, {
2829                 .alg = "authenc(hmac(sha256),cbc(des3_ede))",
2830                 .test = alg_test_aead,
2831                 .fips_allowed = 1,
2832                 .suite = {
2833                         .aead = __VECS(hmac_sha256_des3_ede_cbc_tv_temp)
2834                 }
2835         }, {
2836                 .alg = "authenc(hmac(sha256),ctr(aes))",
2837                 .test = alg_test_null,
2838                 .fips_allowed = 1,
2839         }, {
2840                 .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
2841                 .test = alg_test_null,
2842                 .fips_allowed = 1,
2843         }, {
2844                 .alg = "authenc(hmac(sha384),cbc(des))",
2845                 .test = alg_test_aead,
2846                 .suite = {
2847                         .aead = __VECS(hmac_sha384_des_cbc_tv_temp)
2848                 }
2849         }, {
2850                 .alg = "authenc(hmac(sha384),cbc(des3_ede))",
2851                 .test = alg_test_aead,
2852                 .fips_allowed = 1,
2853                 .suite = {
2854                         .aead = __VECS(hmac_sha384_des3_ede_cbc_tv_temp)
2855                 }
2856         }, {
2857                 .alg = "authenc(hmac(sha384),ctr(aes))",
2858                 .test = alg_test_null,
2859                 .fips_allowed = 1,
2860         }, {
2861                 .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
2862                 .test = alg_test_null,
2863                 .fips_allowed = 1,
2864         }, {
2865                 .alg = "authenc(hmac(sha512),cbc(aes))",
2866                 .fips_allowed = 1,
2867                 .test = alg_test_aead,
2868                 .suite = {
2869                         .aead = __VECS(hmac_sha512_aes_cbc_tv_temp)
2870                 }
2871         }, {
2872                 .alg = "authenc(hmac(sha512),cbc(des))",
2873                 .test = alg_test_aead,
2874                 .suite = {
2875                         .aead = __VECS(hmac_sha512_des_cbc_tv_temp)
2876                 }
2877         }, {
2878                 .alg = "authenc(hmac(sha512),cbc(des3_ede))",
2879                 .test = alg_test_aead,
2880                 .fips_allowed = 1,
2881                 .suite = {
2882                         .aead = __VECS(hmac_sha512_des3_ede_cbc_tv_temp)
2883                 }
2884         }, {
2885                 .alg = "authenc(hmac(sha512),ctr(aes))",
2886                 .test = alg_test_null,
2887                 .fips_allowed = 1,
2888         }, {
2889                 .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
2890                 .test = alg_test_null,
2891                 .fips_allowed = 1,
2892         }, {
2893                 .alg = "cbc(aes)",
2894                 .test = alg_test_skcipher,
2895                 .fips_allowed = 1,
2896                 .suite = {
2897                         .cipher = __VECS(aes_cbc_tv_template)
2898                 },
2899         }, {
2900                 .alg = "cbc(anubis)",
2901                 .test = alg_test_skcipher,
2902                 .suite = {
2903                         .cipher = __VECS(anubis_cbc_tv_template)
2904                 },
2905         }, {
2906                 .alg = "cbc(blowfish)",
2907                 .test = alg_test_skcipher,
2908                 .suite = {
2909                         .cipher = __VECS(bf_cbc_tv_template)
2910                 },
2911         }, {
2912                 .alg = "cbc(camellia)",
2913                 .test = alg_test_skcipher,
2914                 .suite = {
2915                         .cipher = __VECS(camellia_cbc_tv_template)
2916                 },
2917         }, {
2918                 .alg = "cbc(cast5)",
2919                 .test = alg_test_skcipher,
2920                 .suite = {
2921                         .cipher = __VECS(cast5_cbc_tv_template)
2922                 },
2923         }, {
2924                 .alg = "cbc(cast6)",
2925                 .test = alg_test_skcipher,
2926                 .suite = {
2927                         .cipher = __VECS(cast6_cbc_tv_template)
2928                 },
2929         }, {
2930                 .alg = "cbc(des)",
2931                 .test = alg_test_skcipher,
2932                 .suite = {
2933                         .cipher = __VECS(des_cbc_tv_template)
2934                 },
2935         }, {
2936                 .alg = "cbc(des3_ede)",
2937                 .test = alg_test_skcipher,
2938                 .fips_allowed = 1,
2939                 .suite = {
2940                         .cipher = __VECS(des3_ede_cbc_tv_template)
2941                 },
2942         }, {
2943                 /* Same as cbc(aes) except the key is stored in
2944                  * hardware secure memory which we reference by index
2945                  */
2946                 .alg = "cbc(paes)",
2947                 .test = alg_test_null,
2948                 .fips_allowed = 1,
2949         }, {
2950                 .alg = "cbc(serpent)",
2951                 .test = alg_test_skcipher,
2952                 .suite = {
2953                         .cipher = __VECS(serpent_cbc_tv_template)
2954                 },
2955         }, {
2956                 .alg = "cbc(sm4)",
2957                 .test = alg_test_skcipher,
2958                 .suite = {
2959                         .cipher = __VECS(sm4_cbc_tv_template)
2960                 }
2961         }, {
2962                 .alg = "cbc(twofish)",
2963                 .test = alg_test_skcipher,
2964                 .suite = {
2965                         .cipher = __VECS(tf_cbc_tv_template)
2966                 },
2967         }, {
2968                 .alg = "cbcmac(aes)",
2969                 .fips_allowed = 1,
2970                 .test = alg_test_hash,
2971                 .suite = {
2972                         .hash = __VECS(aes_cbcmac_tv_template)
2973                 }
2974         }, {
2975                 .alg = "ccm(aes)",
2976                 .test = alg_test_aead,
2977                 .fips_allowed = 1,
2978                 .suite = {
2979                         .aead = __VECS(aes_ccm_tv_template)
2980                 }
2981         }, {
2982                 .alg = "cfb(aes)",
2983                 .test = alg_test_skcipher,
2984                 .fips_allowed = 1,
2985                 .suite = {
2986                         .cipher = __VECS(aes_cfb_tv_template)
2987                 },
2988         }, {
2989                 .alg = "chacha20",
2990                 .test = alg_test_skcipher,
2991                 .suite = {
2992                         .cipher = __VECS(chacha20_tv_template)
2993                 },
2994         }, {
2995                 .alg = "cmac(aes)",
2996                 .fips_allowed = 1,
2997                 .test = alg_test_hash,
2998                 .suite = {
2999                         .hash = __VECS(aes_cmac128_tv_template)
3000                 }
3001         }, {
3002                 .alg = "cmac(des3_ede)",
3003                 .fips_allowed = 1,
3004                 .test = alg_test_hash,
3005                 .suite = {
3006                         .hash = __VECS(des3_ede_cmac64_tv_template)
3007                 }
3008         }, {
3009                 .alg = "compress_null",
3010                 .test = alg_test_null,
3011         }, {
3012                 .alg = "crc32",
3013                 .test = alg_test_hash,
3014                 .fips_allowed = 1,
3015                 .suite = {
3016                         .hash = __VECS(crc32_tv_template)
3017                 }
3018         }, {
3019                 .alg = "crc32c",
3020                 .test = alg_test_crc32c,
3021                 .fips_allowed = 1,
3022                 .suite = {
3023                         .hash = __VECS(crc32c_tv_template)
3024                 }
3025         }, {
3026                 .alg = "crct10dif",
3027                 .test = alg_test_hash,
3028                 .fips_allowed = 1,
3029                 .suite = {
3030                         .hash = __VECS(crct10dif_tv_template)
3031                 }
3032         }, {
3033                 .alg = "ctr(aes)",
3034                 .test = alg_test_skcipher,
3035                 .fips_allowed = 1,
3036                 .suite = {
3037                         .cipher = __VECS(aes_ctr_tv_template)
3038                 }
3039         }, {
3040                 .alg = "ctr(blowfish)",
3041                 .test = alg_test_skcipher,
3042                 .suite = {
3043                         .cipher = __VECS(bf_ctr_tv_template)
3044                 }
3045         }, {
3046                 .alg = "ctr(camellia)",
3047                 .test = alg_test_skcipher,
3048                 .suite = {
3049                         .cipher = __VECS(camellia_ctr_tv_template)
3050                 }
3051         }, {
3052                 .alg = "ctr(cast5)",
3053                 .test = alg_test_skcipher,
3054                 .suite = {
3055                         .cipher = __VECS(cast5_ctr_tv_template)
3056                 }
3057         }, {
3058                 .alg = "ctr(cast6)",
3059                 .test = alg_test_skcipher,
3060                 .suite = {
3061                         .cipher = __VECS(cast6_ctr_tv_template)
3062                 }
3063         }, {
3064                 .alg = "ctr(des)",
3065                 .test = alg_test_skcipher,
3066                 .suite = {
3067                         .cipher = __VECS(des_ctr_tv_template)
3068                 }
3069         }, {
3070                 .alg = "ctr(des3_ede)",
3071                 .test = alg_test_skcipher,
3072                 .fips_allowed = 1,
3073                 .suite = {
3074                         .cipher = __VECS(des3_ede_ctr_tv_template)
3075                 }
3076         }, {
3077                 /* Same as ctr(aes) except the key is stored in
3078                  * hardware secure memory which we reference by index
3079                  */
3080                 .alg = "ctr(paes)",
3081                 .test = alg_test_null,
3082                 .fips_allowed = 1,
3083         }, {
3084                 .alg = "ctr(serpent)",
3085                 .test = alg_test_skcipher,
3086                 .suite = {
3087                         .cipher = __VECS(serpent_ctr_tv_template)
3088                 }
3089         }, {
3090                 .alg = "ctr(sm4)",
3091                 .test = alg_test_skcipher,
3092                 .suite = {
3093                         .cipher = __VECS(sm4_ctr_tv_template)
3094                 }
3095         }, {
3096                 .alg = "ctr(twofish)",
3097                 .test = alg_test_skcipher,
3098                 .suite = {
3099                         .cipher = __VECS(tf_ctr_tv_template)
3100                 }
3101         }, {
3102                 .alg = "cts(cbc(aes))",
3103                 .test = alg_test_skcipher,
3104                 .fips_allowed = 1,
3105                 .suite = {
3106                         .cipher = __VECS(cts_mode_tv_template)
3107                 }
3108         }, {
3109                 .alg = "deflate",
3110                 .test = alg_test_comp,
3111                 .fips_allowed = 1,
3112                 .suite = {
3113                         .comp = {
3114                                 .comp = __VECS(deflate_comp_tv_template),
3115                                 .decomp = __VECS(deflate_decomp_tv_template)
3116                         }
3117                 }
3118         }, {
3119                 .alg = "dh",
3120                 .test = alg_test_kpp,
3121                 .fips_allowed = 1,
3122                 .suite = {
3123                         .kpp = __VECS(dh_tv_template)
3124                 }
3125         }, {
3126                 .alg = "digest_null",
3127                 .test = alg_test_null,
3128         }, {
3129                 .alg = "drbg_nopr_ctr_aes128",
3130                 .test = alg_test_drbg,
3131                 .fips_allowed = 1,
3132                 .suite = {
3133                         .drbg = __VECS(drbg_nopr_ctr_aes128_tv_template)
3134                 }
3135         }, {
3136                 .alg = "drbg_nopr_ctr_aes192",
3137                 .test = alg_test_drbg,
3138                 .fips_allowed = 1,
3139                 .suite = {
3140                         .drbg = __VECS(drbg_nopr_ctr_aes192_tv_template)
3141                 }
3142         }, {
3143                 .alg = "drbg_nopr_ctr_aes256",
3144                 .test = alg_test_drbg,
3145                 .fips_allowed = 1,
3146                 .suite = {
3147                         .drbg = __VECS(drbg_nopr_ctr_aes256_tv_template)
3148                 }
3149         }, {
3150                 /*
3151                  * There is no need to specifically test the DRBG with every
3152                  * backend cipher -- covered by drbg_nopr_hmac_sha256 test
3153                  */
3154                 .alg = "drbg_nopr_hmac_sha1",
3155                 .fips_allowed = 1,
3156                 .test = alg_test_null,
3157         }, {
3158                 .alg = "drbg_nopr_hmac_sha256",
3159                 .test = alg_test_drbg,
3160                 .fips_allowed = 1,
3161                 .suite = {
3162                         .drbg = __VECS(drbg_nopr_hmac_sha256_tv_template)
3163                 }
3164         }, {
3165                 /* covered by drbg_nopr_hmac_sha256 test */
3166                 .alg = "drbg_nopr_hmac_sha384",
3167                 .fips_allowed = 1,
3168                 .test = alg_test_null,
3169         }, {
3170                 .alg = "drbg_nopr_hmac_sha512",
3171                 .test = alg_test_null,
3172                 .fips_allowed = 1,
3173         }, {
3174                 .alg = "drbg_nopr_sha1",
3175                 .fips_allowed = 1,
3176                 .test = alg_test_null,
3177         }, {
3178                 .alg = "drbg_nopr_sha256",
3179                 .test = alg_test_drbg,
3180                 .fips_allowed = 1,
3181                 .suite = {
3182                         .drbg = __VECS(drbg_nopr_sha256_tv_template)
3183                 }
3184         }, {
3185                 /* covered by drbg_nopr_sha256 test */
3186                 .alg = "drbg_nopr_sha384",
3187                 .fips_allowed = 1,
3188                 .test = alg_test_null,
3189         }, {
3190                 .alg = "drbg_nopr_sha512",
3191                 .fips_allowed = 1,
3192                 .test = alg_test_null,
3193         }, {
3194                 .alg = "drbg_pr_ctr_aes128",
3195                 .test = alg_test_drbg,
3196                 .fips_allowed = 1,
3197                 .suite = {
3198                         .drbg = __VECS(drbg_pr_ctr_aes128_tv_template)
3199                 }
3200         }, {
3201                 /* covered by drbg_pr_ctr_aes128 test */
3202                 .alg = "drbg_pr_ctr_aes192",
3203                 .fips_allowed = 1,
3204                 .test = alg_test_null,
3205         }, {
3206                 .alg = "drbg_pr_ctr_aes256",
3207                 .fips_allowed = 1,
3208                 .test = alg_test_null,
3209         }, {
3210                 .alg = "drbg_pr_hmac_sha1",
3211                 .fips_allowed = 1,
3212                 .test = alg_test_null,
3213         }, {
3214                 .alg = "drbg_pr_hmac_sha256",
3215                 .test = alg_test_drbg,
3216                 .fips_allowed = 1,
3217                 .suite = {
3218                         .drbg = __VECS(drbg_pr_hmac_sha256_tv_template)
3219                 }
3220         }, {
3221                 /* covered by drbg_pr_hmac_sha256 test */
3222                 .alg = "drbg_pr_hmac_sha384",
3223                 .fips_allowed = 1,
3224                 .test = alg_test_null,
3225         }, {
3226                 .alg = "drbg_pr_hmac_sha512",
3227                 .test = alg_test_null,
3228                 .fips_allowed = 1,
3229         }, {
3230                 .alg = "drbg_pr_sha1",
3231                 .fips_allowed = 1,
3232                 .test = alg_test_null,
3233         }, {
3234                 .alg = "drbg_pr_sha256",
3235                 .test = alg_test_drbg,
3236                 .fips_allowed = 1,
3237                 .suite = {
3238                         .drbg = __VECS(drbg_pr_sha256_tv_template)
3239                 }
3240         }, {
3241                 /* covered by drbg_pr_sha256 test */
3242                 .alg = "drbg_pr_sha384",
3243                 .fips_allowed = 1,
3244                 .test = alg_test_null,
3245         }, {
3246                 .alg = "drbg_pr_sha512",
3247                 .fips_allowed = 1,
3248                 .test = alg_test_null,
3249         }, {
3250                 .alg = "ecb(aes)",
3251                 .test = alg_test_skcipher,
3252                 .fips_allowed = 1,
3253                 .suite = {
3254                         .cipher = __VECS(aes_tv_template)
3255                 }
3256         }, {
3257                 .alg = "ecb(anubis)",
3258                 .test = alg_test_skcipher,
3259                 .suite = {
3260                         .cipher = __VECS(anubis_tv_template)
3261                 }
3262         }, {
3263                 .alg = "ecb(arc4)",
3264                 .test = alg_test_skcipher,
3265                 .suite = {
3266                         .cipher = __VECS(arc4_tv_template)
3267                 }
3268         }, {
3269                 .alg = "ecb(blowfish)",
3270                 .test = alg_test_skcipher,
3271                 .suite = {
3272                         .cipher = __VECS(bf_tv_template)
3273                 }
3274         }, {
3275                 .alg = "ecb(camellia)",
3276                 .test = alg_test_skcipher,
3277                 .suite = {
3278                         .cipher = __VECS(camellia_tv_template)
3279                 }
3280         }, {
3281                 .alg = "ecb(cast5)",
3282                 .test = alg_test_skcipher,
3283                 .suite = {
3284                         .cipher = __VECS(cast5_tv_template)
3285                 }
3286         }, {
3287                 .alg = "ecb(cast6)",
3288                 .test = alg_test_skcipher,
3289                 .suite = {
3290                         .cipher = __VECS(cast6_tv_template)
3291                 }
3292         }, {
3293                 .alg = "ecb(cipher_null)",
3294                 .test = alg_test_null,
3295                 .fips_allowed = 1,
3296         }, {
3297                 .alg = "ecb(des)",
3298                 .test = alg_test_skcipher,
3299                 .suite = {
3300                         .cipher = __VECS(des_tv_template)
3301                 }
3302         }, {
3303                 .alg = "ecb(des3_ede)",
3304                 .test = alg_test_skcipher,
3305                 .fips_allowed = 1,
3306                 .suite = {
3307                         .cipher = __VECS(des3_ede_tv_template)
3308                 }
3309         }, {
3310                 .alg = "ecb(fcrypt)",
3311                 .test = alg_test_skcipher,
3312                 .suite = {
3313                         .cipher = {
3314                                 .vecs = fcrypt_pcbc_tv_template,
3315                                 .count = 1
3316                         }
3317                 }
3318         }, {
3319                 .alg = "ecb(khazad)",
3320                 .test = alg_test_skcipher,
3321                 .suite = {
3322                         .cipher = __VECS(khazad_tv_template)
3323                 }
3324         }, {
3325                 /* Same as ecb(aes) except the key is stored in
3326                  * hardware secure memory which we reference by index
3327                  */
3328                 .alg = "ecb(paes)",
3329                 .test = alg_test_null,
3330                 .fips_allowed = 1,
3331         }, {
3332                 .alg = "ecb(seed)",
3333                 .test = alg_test_skcipher,
3334                 .suite = {
3335                         .cipher = __VECS(seed_tv_template)
3336                 }
3337         }, {
3338                 .alg = "ecb(serpent)",
3339                 .test = alg_test_skcipher,
3340                 .suite = {
3341                         .cipher = __VECS(serpent_tv_template)
3342                 }
3343         }, {
3344                 .alg = "ecb(sm4)",
3345                 .test = alg_test_skcipher,
3346                 .suite = {
3347                         .cipher = __VECS(sm4_tv_template)
3348                 }
3349         }, {
3350                 .alg = "ecb(tea)",
3351                 .test = alg_test_skcipher,
3352                 .suite = {
3353                         .cipher = __VECS(tea_tv_template)
3354                 }
3355         }, {
3356                 .alg = "ecb(tnepres)",
3357                 .test = alg_test_skcipher,
3358                 .suite = {
3359                         .cipher = __VECS(tnepres_tv_template)
3360                 }
3361         }, {
3362                 .alg = "ecb(twofish)",
3363                 .test = alg_test_skcipher,
3364                 .suite = {
3365                         .cipher = __VECS(tf_tv_template)
3366                 }
3367         }, {
3368                 .alg = "ecb(xeta)",
3369                 .test = alg_test_skcipher,
3370                 .suite = {
3371                         .cipher = __VECS(xeta_tv_template)
3372                 }
3373         }, {
3374                 .alg = "ecb(xtea)",
3375                 .test = alg_test_skcipher,
3376                 .suite = {
3377                         .cipher = __VECS(xtea_tv_template)
3378                 }
3379         }, {
3380                 .alg = "ecdh",
3381                 .test = alg_test_kpp,
3382                 .fips_allowed = 1,
3383                 .suite = {
3384                         .kpp = __VECS(ecdh_tv_template)
3385                 }
3386         }, {
3387                 .alg = "gcm(aes)",
3388                 .test = alg_test_aead,
3389                 .fips_allowed = 1,
3390                 .suite = {
3391                         .aead = __VECS(aes_gcm_tv_template)
3392                 }
3393         }, {
3394                 .alg = "ghash",
3395                 .test = alg_test_hash,
3396                 .fips_allowed = 1,
3397                 .suite = {
3398                         .hash = __VECS(ghash_tv_template)
3399                 }
3400         }, {
3401                 .alg = "hmac(md5)",
3402                 .test = alg_test_hash,
3403                 .suite = {
3404                         .hash = __VECS(hmac_md5_tv_template)
3405                 }
3406         }, {
3407                 .alg = "hmac(rmd128)",
3408                 .test = alg_test_hash,
3409                 .suite = {
3410                         .hash = __VECS(hmac_rmd128_tv_template)
3411                 }
3412         }, {
3413                 .alg = "hmac(rmd160)",
3414                 .test = alg_test_hash,
3415                 .suite = {
3416                         .hash = __VECS(hmac_rmd160_tv_template)
3417                 }
3418         }, {
3419                 .alg = "hmac(sha1)",
3420                 .test = alg_test_hash,
3421                 .fips_allowed = 1,
3422                 .suite = {
3423                         .hash = __VECS(hmac_sha1_tv_template)
3424                 }
3425         }, {
3426                 .alg = "hmac(sha224)",
3427                 .test = alg_test_hash,
3428                 .fips_allowed = 1,
3429                 .suite = {
3430                         .hash = __VECS(hmac_sha224_tv_template)
3431                 }
3432         }, {
3433                 .alg = "hmac(sha256)",
3434                 .test = alg_test_hash,
3435                 .fips_allowed = 1,
3436                 .suite = {
3437                         .hash = __VECS(hmac_sha256_tv_template)
3438                 }
3439         }, {
3440                 .alg = "hmac(sha3-224)",
3441                 .test = alg_test_hash,
3442                 .fips_allowed = 1,
3443                 .suite = {
3444                         .hash = __VECS(hmac_sha3_224_tv_template)
3445                 }
3446         }, {
3447                 .alg = "hmac(sha3-256)",
3448                 .test = alg_test_hash,
3449                 .fips_allowed = 1,
3450                 .suite = {
3451                         .hash = __VECS(hmac_sha3_256_tv_template)
3452                 }
3453         }, {
3454                 .alg = "hmac(sha3-384)",
3455                 .test = alg_test_hash,
3456                 .fips_allowed = 1,
3457                 .suite = {
3458                         .hash = __VECS(hmac_sha3_384_tv_template)
3459                 }
3460         }, {
3461                 .alg = "hmac(sha3-512)",
3462                 .test = alg_test_hash,
3463                 .fips_allowed = 1,
3464                 .suite = {
3465                         .hash = __VECS(hmac_sha3_512_tv_template)
3466                 }
3467         }, {
3468                 .alg = "hmac(sha384)",
3469                 .test = alg_test_hash,
3470                 .fips_allowed = 1,
3471                 .suite = {
3472                         .hash = __VECS(hmac_sha384_tv_template)
3473                 }
3474         }, {
3475                 .alg = "hmac(sha512)",
3476                 .test = alg_test_hash,
3477                 .fips_allowed = 1,
3478                 .suite = {
3479                         .hash = __VECS(hmac_sha512_tv_template)
3480                 }
3481         }, {
3482                 .alg = "hmac(streebog256)",
3483                 .test = alg_test_hash,
3484                 .suite = {
3485                         .hash = __VECS(hmac_streebog256_tv_template)
3486                 }
3487         }, {
3488                 .alg = "hmac(streebog512)",
3489                 .test = alg_test_hash,
3490                 .suite = {
3491                         .hash = __VECS(hmac_streebog512_tv_template)
3492                 }
3493         }, {
3494                 .alg = "jitterentropy_rng",
3495                 .fips_allowed = 1,
3496                 .test = alg_test_null,
3497         }, {
3498                 .alg = "kw(aes)",
3499                 .test = alg_test_skcipher,
3500                 .fips_allowed = 1,
3501                 .suite = {
3502                         .cipher = __VECS(aes_kw_tv_template)
3503                 }
3504         }, {
3505                 .alg = "lrw(aes)",
3506                 .test = alg_test_skcipher,
3507                 .suite = {
3508                         .cipher = __VECS(aes_lrw_tv_template)
3509                 }
3510         }, {
3511                 .alg = "lrw(camellia)",
3512                 .test = alg_test_skcipher,
3513                 .suite = {
3514                         .cipher = __VECS(camellia_lrw_tv_template)
3515                 }
3516         }, {
3517                 .alg = "lrw(cast6)",
3518                 .test = alg_test_skcipher,
3519                 .suite = {
3520                         .cipher = __VECS(cast6_lrw_tv_template)
3521                 }
3522         }, {
3523                 .alg = "lrw(serpent)",
3524                 .test = alg_test_skcipher,
3525                 .suite = {
3526                         .cipher = __VECS(serpent_lrw_tv_template)
3527                 }
3528         }, {
3529                 .alg = "lrw(twofish)",
3530                 .test = alg_test_skcipher,
3531                 .suite = {
3532                         .cipher = __VECS(tf_lrw_tv_template)
3533                 }
3534         }, {
3535                 .alg = "lz4",
3536                 .test = alg_test_comp,
3537                 .fips_allowed = 1,
3538                 .suite = {
3539                         .comp = {
3540                                 .comp = __VECS(lz4_comp_tv_template),
3541                                 .decomp = __VECS(lz4_decomp_tv_template)
3542                         }
3543                 }
3544         }, {
3545                 .alg = "lz4hc",
3546                 .test = alg_test_comp,
3547                 .fips_allowed = 1,
3548                 .suite = {
3549                         .comp = {
3550                                 .comp = __VECS(lz4hc_comp_tv_template),
3551                                 .decomp = __VECS(lz4hc_decomp_tv_template)
3552                         }
3553                 }
3554         }, {
3555                 .alg = "lzo",
3556                 .test = alg_test_comp,
3557                 .fips_allowed = 1,
3558                 .suite = {
3559                         .comp = {
3560                                 .comp = __VECS(lzo_comp_tv_template),
3561                                 .decomp = __VECS(lzo_decomp_tv_template)
3562                         }
3563                 }
3564         }, {
3565                 .alg = "md4",
3566                 .test = alg_test_hash,
3567                 .suite = {
3568                         .hash = __VECS(md4_tv_template)
3569                 }
3570         }, {
3571                 .alg = "md5",
3572                 .test = alg_test_hash,
3573                 .suite = {
3574                         .hash = __VECS(md5_tv_template)
3575                 }
3576         }, {
3577                 .alg = "michael_mic",
3578                 .test = alg_test_hash,
3579                 .suite = {
3580                         .hash = __VECS(michael_mic_tv_template)
3581                 }
3582         }, {
3583                 .alg = "morus1280",
3584                 .test = alg_test_aead,
3585                 .suite = {
3586                         .aead = __VECS(morus1280_tv_template)
3587                 }
3588         }, {
3589                 .alg = "morus640",
3590                 .test = alg_test_aead,
3591                 .suite = {
3592                         .aead = __VECS(morus640_tv_template)
3593                 }
3594         }, {
3595                 .alg = "nhpoly1305",
3596                 .test = alg_test_hash,
3597                 .suite = {
3598                         .hash = __VECS(nhpoly1305_tv_template)
3599                 }
3600         }, {
3601                 .alg = "ofb(aes)",
3602                 .test = alg_test_skcipher,
3603                 .fips_allowed = 1,
3604                 .suite = {
3605                         .cipher = __VECS(aes_ofb_tv_template)
3606                 }
3607         }, {
3608                 /* Same as ofb(aes) except the key is stored in
3609                  * hardware secure memory which we reference by index
3610                  */
3611                 .alg = "ofb(paes)",
3612                 .test = alg_test_null,
3613                 .fips_allowed = 1,
3614         }, {
3615                 .alg = "pcbc(fcrypt)",
3616                 .test = alg_test_skcipher,
3617                 .suite = {
3618                         .cipher = __VECS(fcrypt_pcbc_tv_template)
3619                 }
3620         }, {
3621                 .alg = "pkcs1pad(rsa,sha224)",
3622                 .test = alg_test_null,
3623                 .fips_allowed = 1,
3624         }, {
3625                 .alg = "pkcs1pad(rsa,sha256)",
3626                 .test = alg_test_akcipher,
3627                 .fips_allowed = 1,
3628                 .suite = {
3629                         .akcipher = __VECS(pkcs1pad_rsa_tv_template)
3630                 }
3631         }, {
3632                 .alg = "pkcs1pad(rsa,sha384)",
3633                 .test = alg_test_null,
3634                 .fips_allowed = 1,
3635         }, {
3636                 .alg = "pkcs1pad(rsa,sha512)",
3637                 .test = alg_test_null,
3638                 .fips_allowed = 1,
3639         }, {
3640                 .alg = "poly1305",
3641                 .test = alg_test_hash,
3642                 .suite = {
3643                         .hash = __VECS(poly1305_tv_template)
3644                 }
3645         }, {
3646                 .alg = "rfc3686(ctr(aes))",
3647                 .test = alg_test_skcipher,
3648                 .fips_allowed = 1,
3649                 .suite = {
3650                         .cipher = __VECS(aes_ctr_rfc3686_tv_template)
3651                 }
3652         }, {
3653                 .alg = "rfc4106(gcm(aes))",
3654                 .test = alg_test_aead,
3655                 .fips_allowed = 1,
3656                 .suite = {
3657                         .aead = __VECS(aes_gcm_rfc4106_tv_template)
3658                 }
3659         }, {
3660                 .alg = "rfc4309(ccm(aes))",
3661                 .test = alg_test_aead,
3662                 .fips_allowed = 1,
3663                 .suite = {
3664                         .aead = __VECS(aes_ccm_rfc4309_tv_template)
3665                 }
3666         }, {
3667                 .alg = "rfc4543(gcm(aes))",
3668                 .test = alg_test_aead,
3669                 .suite = {
3670                         .aead = __VECS(aes_gcm_rfc4543_tv_template)
3671                 }
3672         }, {
3673                 .alg = "rfc7539(chacha20,poly1305)",
3674                 .test = alg_test_aead,
3675                 .suite = {
3676                         .aead = __VECS(rfc7539_tv_template)
3677                 }
3678         }, {
3679                 .alg = "rfc7539esp(chacha20,poly1305)",
3680                 .test = alg_test_aead,
3681                 .suite = {
3682                         .aead = __VECS(rfc7539esp_tv_template)
3683                 }
3684         }, {
3685                 .alg = "rmd128",
3686                 .test = alg_test_hash,
3687                 .suite = {
3688                         .hash = __VECS(rmd128_tv_template)
3689                 }
3690         }, {
3691                 .alg = "rmd160",
3692                 .test = alg_test_hash,
3693                 .suite = {
3694                         .hash = __VECS(rmd160_tv_template)
3695                 }
3696         }, {
3697                 .alg = "rmd256",
3698                 .test = alg_test_hash,
3699                 .suite = {
3700                         .hash = __VECS(rmd256_tv_template)
3701                 }
3702         }, {
3703                 .alg = "rmd320",
3704                 .test = alg_test_hash,
3705                 .suite = {
3706                         .hash = __VECS(rmd320_tv_template)
3707                 }
3708         }, {
3709                 .alg = "rsa",
3710                 .test = alg_test_akcipher,
3711                 .fips_allowed = 1,
3712                 .suite = {
3713                         .akcipher = __VECS(rsa_tv_template)
3714                 }
3715         }, {
3716                 .alg = "salsa20",
3717                 .test = alg_test_skcipher,
3718                 .suite = {
3719                         .cipher = __VECS(salsa20_stream_tv_template)
3720                 }
3721         }, {
3722                 .alg = "sha1",
3723                 .test = alg_test_hash,
3724                 .fips_allowed = 1,
3725                 .suite = {
3726                         .hash = __VECS(sha1_tv_template)
3727                 }
3728         }, {
3729                 .alg = "sha224",
3730                 .test = alg_test_hash,
3731                 .fips_allowed = 1,
3732                 .suite = {
3733                         .hash = __VECS(sha224_tv_template)
3734                 }
3735         }, {
3736                 .alg = "sha256",
3737                 .test = alg_test_hash,
3738                 .fips_allowed = 1,
3739                 .suite = {
3740                         .hash = __VECS(sha256_tv_template)
3741                 }
3742         }, {
3743                 .alg = "sha3-224",
3744                 .test = alg_test_hash,
3745                 .fips_allowed = 1,
3746                 .suite = {
3747                         .hash = __VECS(sha3_224_tv_template)
3748                 }
3749         }, {
3750                 .alg = "sha3-256",
3751                 .test = alg_test_hash,
3752                 .fips_allowed = 1,
3753                 .suite = {
3754                         .hash = __VECS(sha3_256_tv_template)
3755                 }
3756         }, {
3757                 .alg = "sha3-384",
3758                 .test = alg_test_hash,
3759                 .fips_allowed = 1,
3760                 .suite = {
3761                         .hash = __VECS(sha3_384_tv_template)
3762                 }
3763         }, {
3764                 .alg = "sha3-512",
3765                 .test = alg_test_hash,
3766                 .fips_allowed = 1,
3767                 .suite = {
3768                         .hash = __VECS(sha3_512_tv_template)
3769                 }
3770         }, {
3771                 .alg = "sha384",
3772                 .test = alg_test_hash,
3773                 .fips_allowed = 1,
3774                 .suite = {
3775                         .hash = __VECS(sha384_tv_template)
3776                 }
3777         }, {
3778                 .alg = "sha512",
3779                 .test = alg_test_hash,
3780                 .fips_allowed = 1,
3781                 .suite = {
3782                         .hash = __VECS(sha512_tv_template)
3783                 }
3784         }, {
3785                 .alg = "sm3",
3786                 .test = alg_test_hash,
3787                 .suite = {
3788                         .hash = __VECS(sm3_tv_template)
3789                 }
3790         }, {
3791                 .alg = "streebog256",
3792                 .test = alg_test_hash,
3793                 .suite = {
3794                         .hash = __VECS(streebog256_tv_template)
3795                 }
3796         }, {
3797                 .alg = "streebog512",
3798                 .test = alg_test_hash,
3799                 .suite = {
3800                         .hash = __VECS(streebog512_tv_template)
3801                 }
3802         }, {
3803                 .alg = "tgr128",
3804                 .test = alg_test_hash,
3805                 .suite = {
3806                         .hash = __VECS(tgr128_tv_template)
3807                 }
3808         }, {
3809                 .alg = "tgr160",
3810                 .test = alg_test_hash,
3811                 .suite = {
3812                         .hash = __VECS(tgr160_tv_template)
3813                 }
3814         }, {
3815                 .alg = "tgr192",
3816                 .test = alg_test_hash,
3817                 .suite = {
3818                         .hash = __VECS(tgr192_tv_template)
3819                 }
3820         }, {
3821                 .alg = "vmac64(aes)",
3822                 .test = alg_test_hash,
3823                 .suite = {
3824                         .hash = __VECS(vmac64_aes_tv_template)
3825                 }
3826         }, {
3827                 .alg = "wp256",
3828                 .test = alg_test_hash,
3829                 .suite = {
3830                         .hash = __VECS(wp256_tv_template)
3831                 }
3832         }, {
3833                 .alg = "wp384",
3834                 .test = alg_test_hash,
3835                 .suite = {
3836                         .hash = __VECS(wp384_tv_template)
3837                 }
3838         }, {
3839                 .alg = "wp512",
3840                 .test = alg_test_hash,
3841                 .suite = {
3842                         .hash = __VECS(wp512_tv_template)
3843                 }
3844         }, {
3845                 .alg = "xcbc(aes)",
3846                 .test = alg_test_hash,
3847                 .suite = {
3848                         .hash = __VECS(aes_xcbc128_tv_template)
3849                 }
3850         }, {
3851                 .alg = "xchacha12",
3852                 .test = alg_test_skcipher,
3853                 .suite = {
3854                         .cipher = __VECS(xchacha12_tv_template)
3855                 },
3856         }, {
3857                 .alg = "xchacha20",
3858                 .test = alg_test_skcipher,
3859                 .suite = {
3860                         .cipher = __VECS(xchacha20_tv_template)
3861                 },
3862         }, {
3863                 .alg = "xts(aes)",
3864                 .test = alg_test_skcipher,
3865                 .fips_allowed = 1,
3866                 .suite = {
3867                         .cipher = __VECS(aes_xts_tv_template)
3868                 }
3869         }, {
3870                 .alg = "xts(camellia)",
3871                 .test = alg_test_skcipher,
3872                 .suite = {
3873                         .cipher = __VECS(camellia_xts_tv_template)
3874                 }
3875         }, {
3876                 .alg = "xts(cast6)",
3877                 .test = alg_test_skcipher,
3878                 .suite = {
3879                         .cipher = __VECS(cast6_xts_tv_template)
3880                 }
3881         }, {
3882                 /* Same as xts(aes) except the key is stored in
3883                  * hardware secure memory which we reference by index
3884                  */
3885                 .alg = "xts(paes)",
3886                 .test = alg_test_null,
3887                 .fips_allowed = 1,
3888         }, {
3889                 .alg = "xts(serpent)",
3890                 .test = alg_test_skcipher,
3891                 .suite = {
3892                         .cipher = __VECS(serpent_xts_tv_template)
3893                 }
3894         }, {
3895                 .alg = "xts(twofish)",
3896                 .test = alg_test_skcipher,
3897                 .suite = {
3898                         .cipher = __VECS(tf_xts_tv_template)
3899                 }
3900         }, {
3901                 .alg = "xts4096(paes)",
3902                 .test = alg_test_null,
3903                 .fips_allowed = 1,
3904         }, {
3905                 .alg = "xts512(paes)",
3906                 .test = alg_test_null,
3907                 .fips_allowed = 1,
3908         }, {
3909                 .alg = "zlib-deflate",
3910                 .test = alg_test_comp,
3911                 .fips_allowed = 1,
3912                 .suite = {
3913                         .comp = {
3914                                 .comp = __VECS(zlib_deflate_comp_tv_template),
3915                                 .decomp = __VECS(zlib_deflate_decomp_tv_template)
3916                         }
3917                 }
3918         }, {
3919                 .alg = "zstd",
3920                 .test = alg_test_comp,
3921                 .fips_allowed = 1,
3922                 .suite = {
3923                         .comp = {
3924                                 .comp = __VECS(zstd_comp_tv_template),
3925                                 .decomp = __VECS(zstd_decomp_tv_template)
3926                         }
3927                 }
3928         }
3929 };
3930
3931 static void alg_check_test_descs_order(void)
3932 {
3933         int i;
3934
3935         for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
3936                 int diff = strcmp(alg_test_descs[i - 1].alg,
3937                                   alg_test_descs[i].alg);
3938
3939                 if (WARN_ON(diff > 0)) {
3940                         pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
3941                                 alg_test_descs[i - 1].alg,
3942                                 alg_test_descs[i].alg);
3943                 }
3944
3945                 if (WARN_ON(diff == 0)) {
3946                         pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
3947                                 alg_test_descs[i].alg);
3948                 }
3949         }
3950 }
3951
3952 static void alg_check_testvec_configs(void)
3953 {
3954         int i;
3955
3956         for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++)
3957                 WARN_ON(!valid_testvec_config(
3958                                 &default_cipher_testvec_configs[i]));
3959 }
3960
3961 static void testmgr_onetime_init(void)
3962 {
3963         alg_check_test_descs_order();
3964         alg_check_testvec_configs();
3965
3966 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
3967         pr_warn("alg: extra crypto tests enabled.  This is intended for developer use only.\n");
3968 #endif
3969 }
3970
3971 static int alg_find_test(const char *alg)
3972 {
3973         int start = 0;
3974         int end = ARRAY_SIZE(alg_test_descs);
3975
3976         while (start < end) {
3977                 int i = (start + end) / 2;
3978                 int diff = strcmp(alg_test_descs[i].alg, alg);
3979
3980                 if (diff > 0) {
3981                         end = i;
3982                         continue;
3983                 }
3984
3985                 if (diff < 0) {
3986                         start = i + 1;
3987                         continue;
3988                 }
3989
3990                 return i;
3991         }
3992
3993         return -1;
3994 }
3995
3996 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
3997 {
3998         int i;
3999         int j;
4000         int rc;
4001
4002         if (!fips_enabled && notests) {
4003                 printk_once(KERN_INFO "alg: self-tests disabled\n");
4004                 return 0;
4005         }
4006
4007         DO_ONCE(testmgr_onetime_init);
4008
4009         if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
4010                 char nalg[CRYPTO_MAX_ALG_NAME];
4011
4012                 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
4013                     sizeof(nalg))
4014                         return -ENAMETOOLONG;
4015
4016                 i = alg_find_test(nalg);
4017                 if (i < 0)
4018                         goto notest;
4019
4020                 if (fips_enabled && !alg_test_descs[i].fips_allowed)
4021                         goto non_fips_alg;
4022
4023                 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
4024                 goto test_done;
4025         }
4026
4027         i = alg_find_test(alg);
4028         j = alg_find_test(driver);
4029         if (i < 0 && j < 0)
4030                 goto notest;
4031
4032         if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
4033                              (j >= 0 && !alg_test_descs[j].fips_allowed)))
4034                 goto non_fips_alg;
4035
4036         rc = 0;
4037         if (i >= 0)
4038                 rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
4039                                              type, mask);
4040         if (j >= 0 && j != i)
4041                 rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
4042                                              type, mask);
4043
4044 test_done:
4045         if (fips_enabled && rc)
4046                 panic("%s: %s alg self test failed in fips mode!\n", driver, alg);
4047
4048         if (fips_enabled && !rc)
4049                 pr_info("alg: self-tests for %s (%s) passed\n", driver, alg);
4050
4051         return rc;
4052
4053 notest:
4054         printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
4055         return 0;
4056 non_fips_alg:
4057         return -EINVAL;
4058 }
4059
4060 #endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
4061
4062 EXPORT_SYMBOL_GPL(alg_test);