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