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