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