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