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