Merge tag 'usb-3.11-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb
[platform/upstream/kernel-adaptation-pc.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/hash.h>
24 #include <linux/err.h>
25 #include <linux/module.h>
26 #include <linux/scatterlist.h>
27 #include <linux/slab.h>
28 #include <linux/string.h>
29 #include <crypto/rng.h>
30
31 #include "internal.h"
32
33 #ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
34
35 /* a perfect nop */
36 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
37 {
38         return 0;
39 }
40
41 #else
42
43 #include "testmgr.h"
44
45 /*
46  * Need slab memory for testing (size in number of pages).
47  */
48 #define XBUFSIZE        8
49
50 /*
51  * Indexes into the xbuf to simulate cross-page access.
52  */
53 #define IDX1            32
54 #define IDX2            32400
55 #define IDX3            1
56 #define IDX4            8193
57 #define IDX5            22222
58 #define IDX6            17101
59 #define IDX7            27333
60 #define IDX8            3000
61
62 /*
63 * Used by test_cipher()
64 */
65 #define ENCRYPT 1
66 #define DECRYPT 0
67
68 struct tcrypt_result {
69         struct completion completion;
70         int err;
71 };
72
73 struct aead_test_suite {
74         struct {
75                 struct aead_testvec *vecs;
76                 unsigned int count;
77         } enc, dec;
78 };
79
80 struct cipher_test_suite {
81         struct {
82                 struct cipher_testvec *vecs;
83                 unsigned int count;
84         } enc, dec;
85 };
86
87 struct comp_test_suite {
88         struct {
89                 struct comp_testvec *vecs;
90                 unsigned int count;
91         } comp, decomp;
92 };
93
94 struct pcomp_test_suite {
95         struct {
96                 struct pcomp_testvec *vecs;
97                 unsigned int count;
98         } comp, decomp;
99 };
100
101 struct hash_test_suite {
102         struct hash_testvec *vecs;
103         unsigned int count;
104 };
105
106 struct cprng_test_suite {
107         struct cprng_testvec *vecs;
108         unsigned int count;
109 };
110
111 struct alg_test_desc {
112         const char *alg;
113         int (*test)(const struct alg_test_desc *desc, const char *driver,
114                     u32 type, u32 mask);
115         int fips_allowed;       /* set if alg is allowed in fips mode */
116
117         union {
118                 struct aead_test_suite aead;
119                 struct cipher_test_suite cipher;
120                 struct comp_test_suite comp;
121                 struct pcomp_test_suite pcomp;
122                 struct hash_test_suite hash;
123                 struct cprng_test_suite cprng;
124         } suite;
125 };
126
127 static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
128
129 static void hexdump(unsigned char *buf, unsigned int len)
130 {
131         print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
132                         16, 1,
133                         buf, len, false);
134 }
135
136 static void tcrypt_complete(struct crypto_async_request *req, int err)
137 {
138         struct tcrypt_result *res = req->data;
139
140         if (err == -EINPROGRESS)
141                 return;
142
143         res->err = err;
144         complete(&res->completion);
145 }
146
147 static int testmgr_alloc_buf(char *buf[XBUFSIZE])
148 {
149         int i;
150
151         for (i = 0; i < XBUFSIZE; i++) {
152                 buf[i] = (void *)__get_free_page(GFP_KERNEL);
153                 if (!buf[i])
154                         goto err_free_buf;
155         }
156
157         return 0;
158
159 err_free_buf:
160         while (i-- > 0)
161                 free_page((unsigned long)buf[i]);
162
163         return -ENOMEM;
164 }
165
166 static void testmgr_free_buf(char *buf[XBUFSIZE])
167 {
168         int i;
169
170         for (i = 0; i < XBUFSIZE; i++)
171                 free_page((unsigned long)buf[i]);
172 }
173
174 static int do_one_async_hash_op(struct ahash_request *req,
175                                 struct tcrypt_result *tr,
176                                 int ret)
177 {
178         if (ret == -EINPROGRESS || ret == -EBUSY) {
179                 ret = wait_for_completion_interruptible(&tr->completion);
180                 if (!ret)
181                         ret = tr->err;
182                 INIT_COMPLETION(tr->completion);
183         }
184         return ret;
185 }
186
187 static int __test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
188                        unsigned int tcount, bool use_digest,
189                        const int align_offset)
190 {
191         const char *algo = crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
192         unsigned int i, j, k, temp;
193         struct scatterlist sg[8];
194         char result[64];
195         struct ahash_request *req;
196         struct tcrypt_result tresult;
197         void *hash_buff;
198         char *xbuf[XBUFSIZE];
199         int ret = -ENOMEM;
200
201         if (testmgr_alloc_buf(xbuf))
202                 goto out_nobuf;
203
204         init_completion(&tresult.completion);
205
206         req = ahash_request_alloc(tfm, GFP_KERNEL);
207         if (!req) {
208                 printk(KERN_ERR "alg: hash: Failed to allocate request for "
209                        "%s\n", algo);
210                 goto out_noreq;
211         }
212         ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
213                                    tcrypt_complete, &tresult);
214
215         j = 0;
216         for (i = 0; i < tcount; i++) {
217                 if (template[i].np)
218                         continue;
219
220                 ret = -EINVAL;
221                 if (WARN_ON(align_offset + template[i].psize > PAGE_SIZE))
222                         goto out;
223
224                 j++;
225                 memset(result, 0, 64);
226
227                 hash_buff = xbuf[0];
228                 hash_buff += align_offset;
229
230                 memcpy(hash_buff, template[i].plaintext, template[i].psize);
231                 sg_init_one(&sg[0], hash_buff, template[i].psize);
232
233                 if (template[i].ksize) {
234                         crypto_ahash_clear_flags(tfm, ~0);
235                         ret = crypto_ahash_setkey(tfm, template[i].key,
236                                                   template[i].ksize);
237                         if (ret) {
238                                 printk(KERN_ERR "alg: hash: setkey failed on "
239                                        "test %d for %s: ret=%d\n", j, algo,
240                                        -ret);
241                                 goto out;
242                         }
243                 }
244
245                 ahash_request_set_crypt(req, sg, result, template[i].psize);
246                 if (use_digest) {
247                         ret = do_one_async_hash_op(req, &tresult,
248                                                    crypto_ahash_digest(req));
249                         if (ret) {
250                                 pr_err("alg: hash: digest failed on test %d "
251                                        "for %s: ret=%d\n", j, algo, -ret);
252                                 goto out;
253                         }
254                 } else {
255                         ret = do_one_async_hash_op(req, &tresult,
256                                                    crypto_ahash_init(req));
257                         if (ret) {
258                                 pr_err("alt: hash: init failed on test %d "
259                                        "for %s: ret=%d\n", j, algo, -ret);
260                                 goto out;
261                         }
262                         ret = do_one_async_hash_op(req, &tresult,
263                                                    crypto_ahash_update(req));
264                         if (ret) {
265                                 pr_err("alt: hash: update failed on test %d "
266                                        "for %s: ret=%d\n", j, algo, -ret);
267                                 goto out;
268                         }
269                         ret = do_one_async_hash_op(req, &tresult,
270                                                    crypto_ahash_final(req));
271                         if (ret) {
272                                 pr_err("alt: hash: final failed on test %d "
273                                        "for %s: ret=%d\n", j, algo, -ret);
274                                 goto out;
275                         }
276                 }
277
278                 if (memcmp(result, template[i].digest,
279                            crypto_ahash_digestsize(tfm))) {
280                         printk(KERN_ERR "alg: hash: Test %d failed for %s\n",
281                                j, algo);
282                         hexdump(result, crypto_ahash_digestsize(tfm));
283                         ret = -EINVAL;
284                         goto out;
285                 }
286         }
287
288         j = 0;
289         for (i = 0; i < tcount; i++) {
290                 /* alignment tests are only done with continuous buffers */
291                 if (align_offset != 0)
292                         break;
293
294                 if (template[i].np) {
295                         j++;
296                         memset(result, 0, 64);
297
298                         temp = 0;
299                         sg_init_table(sg, template[i].np);
300                         ret = -EINVAL;
301                         for (k = 0; k < template[i].np; k++) {
302                                 if (WARN_ON(offset_in_page(IDX[k]) +
303                                             template[i].tap[k] > PAGE_SIZE))
304                                         goto out;
305                                 sg_set_buf(&sg[k],
306                                            memcpy(xbuf[IDX[k] >> PAGE_SHIFT] +
307                                                   offset_in_page(IDX[k]),
308                                                   template[i].plaintext + temp,
309                                                   template[i].tap[k]),
310                                            template[i].tap[k]);
311                                 temp += template[i].tap[k];
312                         }
313
314                         if (template[i].ksize) {
315                                 crypto_ahash_clear_flags(tfm, ~0);
316                                 ret = crypto_ahash_setkey(tfm, template[i].key,
317                                                           template[i].ksize);
318
319                                 if (ret) {
320                                         printk(KERN_ERR "alg: hash: setkey "
321                                                "failed on chunking test %d "
322                                                "for %s: ret=%d\n", j, algo,
323                                                -ret);
324                                         goto out;
325                                 }
326                         }
327
328                         ahash_request_set_crypt(req, sg, result,
329                                                 template[i].psize);
330                         ret = crypto_ahash_digest(req);
331                         switch (ret) {
332                         case 0:
333                                 break;
334                         case -EINPROGRESS:
335                         case -EBUSY:
336                                 ret = wait_for_completion_interruptible(
337                                         &tresult.completion);
338                                 if (!ret && !(ret = tresult.err)) {
339                                         INIT_COMPLETION(tresult.completion);
340                                         break;
341                                 }
342                                 /* fall through */
343                         default:
344                                 printk(KERN_ERR "alg: hash: digest failed "
345                                        "on chunking test %d for %s: "
346                                        "ret=%d\n", j, algo, -ret);
347                                 goto out;
348                         }
349
350                         if (memcmp(result, template[i].digest,
351                                    crypto_ahash_digestsize(tfm))) {
352                                 printk(KERN_ERR "alg: hash: Chunking test %d "
353                                        "failed for %s\n", j, algo);
354                                 hexdump(result, crypto_ahash_digestsize(tfm));
355                                 ret = -EINVAL;
356                                 goto out;
357                         }
358                 }
359         }
360
361         ret = 0;
362
363 out:
364         ahash_request_free(req);
365 out_noreq:
366         testmgr_free_buf(xbuf);
367 out_nobuf:
368         return ret;
369 }
370
371 static int test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
372                      unsigned int tcount, bool use_digest)
373 {
374         unsigned int alignmask;
375         int ret;
376
377         ret = __test_hash(tfm, template, tcount, use_digest, 0);
378         if (ret)
379                 return ret;
380
381         /* test unaligned buffers, check with one byte offset */
382         ret = __test_hash(tfm, template, tcount, use_digest, 1);
383         if (ret)
384                 return ret;
385
386         alignmask = crypto_tfm_alg_alignmask(&tfm->base);
387         if (alignmask) {
388                 /* Check if alignment mask for tfm is correctly set. */
389                 ret = __test_hash(tfm, template, tcount, use_digest,
390                                   alignmask + 1);
391                 if (ret)
392                         return ret;
393         }
394
395         return 0;
396 }
397
398 static int __test_aead(struct crypto_aead *tfm, int enc,
399                        struct aead_testvec *template, unsigned int tcount,
400                        const bool diff_dst, const int align_offset)
401 {
402         const char *algo = crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm));
403         unsigned int i, j, k, n, temp;
404         int ret = -ENOMEM;
405         char *q;
406         char *key;
407         struct aead_request *req;
408         struct scatterlist *sg;
409         struct scatterlist *asg;
410         struct scatterlist *sgout;
411         const char *e, *d;
412         struct tcrypt_result result;
413         unsigned int authsize;
414         void *input;
415         void *output;
416         void *assoc;
417         char iv[MAX_IVLEN];
418         char *xbuf[XBUFSIZE];
419         char *xoutbuf[XBUFSIZE];
420         char *axbuf[XBUFSIZE];
421
422         if (testmgr_alloc_buf(xbuf))
423                 goto out_noxbuf;
424         if (testmgr_alloc_buf(axbuf))
425                 goto out_noaxbuf;
426
427         if (diff_dst && testmgr_alloc_buf(xoutbuf))
428                 goto out_nooutbuf;
429
430         /* avoid "the frame size is larger than 1024 bytes" compiler warning */
431         sg = kmalloc(sizeof(*sg) * 8 * (diff_dst ? 3 : 2), GFP_KERNEL);
432         if (!sg)
433                 goto out_nosg;
434         asg = &sg[8];
435         sgout = &asg[8];
436
437         if (diff_dst)
438                 d = "-ddst";
439         else
440                 d = "";
441
442         if (enc == ENCRYPT)
443                 e = "encryption";
444         else
445                 e = "decryption";
446
447         init_completion(&result.completion);
448
449         req = aead_request_alloc(tfm, GFP_KERNEL);
450         if (!req) {
451                 pr_err("alg: aead%s: Failed to allocate request for %s\n",
452                        d, algo);
453                 goto out;
454         }
455
456         aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
457                                   tcrypt_complete, &result);
458
459         for (i = 0, j = 0; i < tcount; i++) {
460                 if (!template[i].np) {
461                         j++;
462
463                         /* some templates have no input data but they will
464                          * touch input
465                          */
466                         input = xbuf[0];
467                         input += align_offset;
468                         assoc = axbuf[0];
469
470                         ret = -EINVAL;
471                         if (WARN_ON(align_offset + template[i].ilen >
472                                     PAGE_SIZE || template[i].alen > PAGE_SIZE))
473                                 goto out;
474
475                         memcpy(input, template[i].input, template[i].ilen);
476                         memcpy(assoc, template[i].assoc, template[i].alen);
477                         if (template[i].iv)
478                                 memcpy(iv, template[i].iv, MAX_IVLEN);
479                         else
480                                 memset(iv, 0, MAX_IVLEN);
481
482                         crypto_aead_clear_flags(tfm, ~0);
483                         if (template[i].wk)
484                                 crypto_aead_set_flags(
485                                         tfm, CRYPTO_TFM_REQ_WEAK_KEY);
486
487                         key = template[i].key;
488
489                         ret = crypto_aead_setkey(tfm, key,
490                                                  template[i].klen);
491                         if (!ret == template[i].fail) {
492                                 pr_err("alg: aead%s: setkey failed on test %d for %s: flags=%x\n",
493                                        d, j, algo, crypto_aead_get_flags(tfm));
494                                 goto out;
495                         } else if (ret)
496                                 continue;
497
498                         authsize = abs(template[i].rlen - template[i].ilen);
499                         ret = crypto_aead_setauthsize(tfm, authsize);
500                         if (ret) {
501                                 pr_err("alg: aead%s: Failed to set authsize to %u on test %d for %s\n",
502                                        d, authsize, j, algo);
503                                 goto out;
504                         }
505
506                         sg_init_one(&sg[0], input,
507                                     template[i].ilen + (enc ? authsize : 0));
508
509                         if (diff_dst) {
510                                 output = xoutbuf[0];
511                                 output += align_offset;
512                                 sg_init_one(&sgout[0], output,
513                                             template[i].ilen +
514                                                 (enc ? authsize : 0));
515                         } else {
516                                 output = input;
517                         }
518
519                         sg_init_one(&asg[0], assoc, template[i].alen);
520
521                         aead_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
522                                                template[i].ilen, iv);
523
524                         aead_request_set_assoc(req, asg, template[i].alen);
525
526                         ret = enc ?
527                                 crypto_aead_encrypt(req) :
528                                 crypto_aead_decrypt(req);
529
530                         switch (ret) {
531                         case 0:
532                                 if (template[i].novrfy) {
533                                         /* verification was supposed to fail */
534                                         pr_err("alg: aead%s: %s failed on test %d for %s: ret was 0, expected -EBADMSG\n",
535                                                d, e, j, algo);
536                                         /* so really, we got a bad message */
537                                         ret = -EBADMSG;
538                                         goto out;
539                                 }
540                                 break;
541                         case -EINPROGRESS:
542                         case -EBUSY:
543                                 ret = wait_for_completion_interruptible(
544                                         &result.completion);
545                                 if (!ret && !(ret = result.err)) {
546                                         INIT_COMPLETION(result.completion);
547                                         break;
548                                 }
549                         case -EBADMSG:
550                                 if (template[i].novrfy)
551                                         /* verification failure was expected */
552                                         continue;
553                                 /* fall through */
554                         default:
555                                 pr_err("alg: aead%s: %s failed on test %d for %s: ret=%d\n",
556                                        d, e, j, algo, -ret);
557                                 goto out;
558                         }
559
560                         q = output;
561                         if (memcmp(q, template[i].result, template[i].rlen)) {
562                                 pr_err("alg: aead%s: Test %d failed on %s for %s\n",
563                                        d, j, e, algo);
564                                 hexdump(q, template[i].rlen);
565                                 ret = -EINVAL;
566                                 goto out;
567                         }
568                 }
569         }
570
571         for (i = 0, j = 0; i < tcount; i++) {
572                 /* alignment tests are only done with continuous buffers */
573                 if (align_offset != 0)
574                         break;
575
576                 if (template[i].np) {
577                         j++;
578
579                         if (template[i].iv)
580                                 memcpy(iv, template[i].iv, MAX_IVLEN);
581                         else
582                                 memset(iv, 0, MAX_IVLEN);
583
584                         crypto_aead_clear_flags(tfm, ~0);
585                         if (template[i].wk)
586                                 crypto_aead_set_flags(
587                                         tfm, CRYPTO_TFM_REQ_WEAK_KEY);
588                         key = template[i].key;
589
590                         ret = crypto_aead_setkey(tfm, key, template[i].klen);
591                         if (!ret == template[i].fail) {
592                                 pr_err("alg: aead%s: setkey failed on chunk test %d for %s: flags=%x\n",
593                                        d, j, algo, crypto_aead_get_flags(tfm));
594                                 goto out;
595                         } else if (ret)
596                                 continue;
597
598                         authsize = abs(template[i].rlen - template[i].ilen);
599
600                         ret = -EINVAL;
601                         sg_init_table(sg, template[i].np);
602                         if (diff_dst)
603                                 sg_init_table(sgout, template[i].np);
604                         for (k = 0, temp = 0; k < template[i].np; k++) {
605                                 if (WARN_ON(offset_in_page(IDX[k]) +
606                                             template[i].tap[k] > PAGE_SIZE))
607                                         goto out;
608
609                                 q = xbuf[IDX[k] >> PAGE_SHIFT] +
610                                     offset_in_page(IDX[k]);
611
612                                 memcpy(q, template[i].input + temp,
613                                        template[i].tap[k]);
614
615                                 n = template[i].tap[k];
616                                 if (k == template[i].np - 1 && enc)
617                                         n += authsize;
618                                 if (offset_in_page(q) + n < PAGE_SIZE)
619                                         q[n] = 0;
620
621                                 sg_set_buf(&sg[k], q, template[i].tap[k]);
622
623                                 if (diff_dst) {
624                                         q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
625                                             offset_in_page(IDX[k]);
626
627                                         memset(q, 0, template[i].tap[k]);
628                                         if (offset_in_page(q) + n < PAGE_SIZE)
629                                                 q[n] = 0;
630
631                                         sg_set_buf(&sgout[k], q,
632                                                    template[i].tap[k]);
633                                 }
634
635                                 temp += template[i].tap[k];
636                         }
637
638                         ret = crypto_aead_setauthsize(tfm, authsize);
639                         if (ret) {
640                                 pr_err("alg: aead%s: Failed to set authsize to %u on chunk test %d for %s\n",
641                                        d, authsize, j, algo);
642                                 goto out;
643                         }
644
645                         if (enc) {
646                                 if (WARN_ON(sg[k - 1].offset +
647                                             sg[k - 1].length + authsize >
648                                             PAGE_SIZE)) {
649                                         ret = -EINVAL;
650                                         goto out;
651                                 }
652
653                                 sg[k - 1].length += authsize;
654
655                                 if (diff_dst)
656                                         sgout[k - 1].length += authsize;
657                         }
658
659                         sg_init_table(asg, template[i].anp);
660                         ret = -EINVAL;
661                         for (k = 0, temp = 0; k < template[i].anp; k++) {
662                                 if (WARN_ON(offset_in_page(IDX[k]) +
663                                             template[i].atap[k] > PAGE_SIZE))
664                                         goto out;
665                                 sg_set_buf(&asg[k],
666                                            memcpy(axbuf[IDX[k] >> PAGE_SHIFT] +
667                                                   offset_in_page(IDX[k]),
668                                                   template[i].assoc + temp,
669                                                   template[i].atap[k]),
670                                            template[i].atap[k]);
671                                 temp += template[i].atap[k];
672                         }
673
674                         aead_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
675                                                template[i].ilen,
676                                                iv);
677
678                         aead_request_set_assoc(req, asg, template[i].alen);
679
680                         ret = enc ?
681                                 crypto_aead_encrypt(req) :
682                                 crypto_aead_decrypt(req);
683
684                         switch (ret) {
685                         case 0:
686                                 if (template[i].novrfy) {
687                                         /* verification was supposed to fail */
688                                         pr_err("alg: aead%s: %s failed on chunk test %d for %s: ret was 0, expected -EBADMSG\n",
689                                                d, e, j, algo);
690                                         /* so really, we got a bad message */
691                                         ret = -EBADMSG;
692                                         goto out;
693                                 }
694                                 break;
695                         case -EINPROGRESS:
696                         case -EBUSY:
697                                 ret = wait_for_completion_interruptible(
698                                         &result.completion);
699                                 if (!ret && !(ret = result.err)) {
700                                         INIT_COMPLETION(result.completion);
701                                         break;
702                                 }
703                         case -EBADMSG:
704                                 if (template[i].novrfy)
705                                         /* verification failure was expected */
706                                         continue;
707                                 /* fall through */
708                         default:
709                                 pr_err("alg: aead%s: %s failed on chunk test %d for %s: ret=%d\n",
710                                        d, e, j, algo, -ret);
711                                 goto out;
712                         }
713
714                         ret = -EINVAL;
715                         for (k = 0, temp = 0; k < template[i].np; k++) {
716                                 if (diff_dst)
717                                         q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
718                                             offset_in_page(IDX[k]);
719                                 else
720                                         q = xbuf[IDX[k] >> PAGE_SHIFT] +
721                                             offset_in_page(IDX[k]);
722
723                                 n = template[i].tap[k];
724                                 if (k == template[i].np - 1)
725                                         n += enc ? authsize : -authsize;
726
727                                 if (memcmp(q, template[i].result + temp, n)) {
728                                         pr_err("alg: aead%s: Chunk test %d failed on %s at page %u for %s\n",
729                                                d, j, e, k, algo);
730                                         hexdump(q, n);
731                                         goto out;
732                                 }
733
734                                 q += n;
735                                 if (k == template[i].np - 1 && !enc) {
736                                         if (!diff_dst &&
737                                                 memcmp(q, template[i].input +
738                                                       temp + n, authsize))
739                                                 n = authsize;
740                                         else
741                                                 n = 0;
742                                 } else {
743                                         for (n = 0; offset_in_page(q + n) &&
744                                                     q[n]; n++)
745                                                 ;
746                                 }
747                                 if (n) {
748                                         pr_err("alg: aead%s: Result buffer corruption in chunk test %d on %s at page %u for %s: %u bytes:\n",
749                                                d, j, e, k, algo, n);
750                                         hexdump(q, n);
751                                         goto out;
752                                 }
753
754                                 temp += template[i].tap[k];
755                         }
756                 }
757         }
758
759         ret = 0;
760
761 out:
762         aead_request_free(req);
763         kfree(sg);
764 out_nosg:
765         if (diff_dst)
766                 testmgr_free_buf(xoutbuf);
767 out_nooutbuf:
768         testmgr_free_buf(axbuf);
769 out_noaxbuf:
770         testmgr_free_buf(xbuf);
771 out_noxbuf:
772         return ret;
773 }
774
775 static int test_aead(struct crypto_aead *tfm, int enc,
776                      struct aead_testvec *template, unsigned int tcount)
777 {
778         unsigned int alignmask;
779         int ret;
780
781         /* test 'dst == src' case */
782         ret = __test_aead(tfm, enc, template, tcount, false, 0);
783         if (ret)
784                 return ret;
785
786         /* test 'dst != src' case */
787         ret = __test_aead(tfm, enc, template, tcount, true, 0);
788         if (ret)
789                 return ret;
790
791         /* test unaligned buffers, check with one byte offset */
792         ret = __test_aead(tfm, enc, template, tcount, true, 1);
793         if (ret)
794                 return ret;
795
796         alignmask = crypto_tfm_alg_alignmask(&tfm->base);
797         if (alignmask) {
798                 /* Check if alignment mask for tfm is correctly set. */
799                 ret = __test_aead(tfm, enc, template, tcount, true,
800                                   alignmask + 1);
801                 if (ret)
802                         return ret;
803         }
804
805         return 0;
806 }
807
808 static int test_cipher(struct crypto_cipher *tfm, int enc,
809                        struct cipher_testvec *template, unsigned int tcount)
810 {
811         const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
812         unsigned int i, j, k;
813         char *q;
814         const char *e;
815         void *data;
816         char *xbuf[XBUFSIZE];
817         int ret = -ENOMEM;
818
819         if (testmgr_alloc_buf(xbuf))
820                 goto out_nobuf;
821
822         if (enc == ENCRYPT)
823                 e = "encryption";
824         else
825                 e = "decryption";
826
827         j = 0;
828         for (i = 0; i < tcount; i++) {
829                 if (template[i].np)
830                         continue;
831
832                 j++;
833
834                 ret = -EINVAL;
835                 if (WARN_ON(template[i].ilen > PAGE_SIZE))
836                         goto out;
837
838                 data = xbuf[0];
839                 memcpy(data, template[i].input, template[i].ilen);
840
841                 crypto_cipher_clear_flags(tfm, ~0);
842                 if (template[i].wk)
843                         crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
844
845                 ret = crypto_cipher_setkey(tfm, template[i].key,
846                                            template[i].klen);
847                 if (!ret == template[i].fail) {
848                         printk(KERN_ERR "alg: cipher: setkey failed "
849                                "on test %d for %s: flags=%x\n", j,
850                                algo, crypto_cipher_get_flags(tfm));
851                         goto out;
852                 } else if (ret)
853                         continue;
854
855                 for (k = 0; k < template[i].ilen;
856                      k += crypto_cipher_blocksize(tfm)) {
857                         if (enc)
858                                 crypto_cipher_encrypt_one(tfm, data + k,
859                                                           data + k);
860                         else
861                                 crypto_cipher_decrypt_one(tfm, data + k,
862                                                           data + k);
863                 }
864
865                 q = data;
866                 if (memcmp(q, template[i].result, template[i].rlen)) {
867                         printk(KERN_ERR "alg: cipher: Test %d failed "
868                                "on %s for %s\n", j, e, algo);
869                         hexdump(q, template[i].rlen);
870                         ret = -EINVAL;
871                         goto out;
872                 }
873         }
874
875         ret = 0;
876
877 out:
878         testmgr_free_buf(xbuf);
879 out_nobuf:
880         return ret;
881 }
882
883 static int __test_skcipher(struct crypto_ablkcipher *tfm, int enc,
884                            struct cipher_testvec *template, unsigned int tcount,
885                            const bool diff_dst, const int align_offset)
886 {
887         const char *algo =
888                 crypto_tfm_alg_driver_name(crypto_ablkcipher_tfm(tfm));
889         unsigned int i, j, k, n, temp;
890         char *q;
891         struct ablkcipher_request *req;
892         struct scatterlist sg[8];
893         struct scatterlist sgout[8];
894         const char *e, *d;
895         struct tcrypt_result result;
896         void *data;
897         char iv[MAX_IVLEN];
898         char *xbuf[XBUFSIZE];
899         char *xoutbuf[XBUFSIZE];
900         int ret = -ENOMEM;
901
902         if (testmgr_alloc_buf(xbuf))
903                 goto out_nobuf;
904
905         if (diff_dst && testmgr_alloc_buf(xoutbuf))
906                 goto out_nooutbuf;
907
908         if (diff_dst)
909                 d = "-ddst";
910         else
911                 d = "";
912
913         if (enc == ENCRYPT)
914                 e = "encryption";
915         else
916                 e = "decryption";
917
918         init_completion(&result.completion);
919
920         req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
921         if (!req) {
922                 pr_err("alg: skcipher%s: Failed to allocate request for %s\n",
923                        d, algo);
924                 goto out;
925         }
926
927         ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
928                                         tcrypt_complete, &result);
929
930         j = 0;
931         for (i = 0; i < tcount; i++) {
932                 if (template[i].iv)
933                         memcpy(iv, template[i].iv, MAX_IVLEN);
934                 else
935                         memset(iv, 0, MAX_IVLEN);
936
937                 if (!(template[i].np) || (template[i].also_non_np)) {
938                         j++;
939
940                         ret = -EINVAL;
941                         if (WARN_ON(align_offset + template[i].ilen >
942                                     PAGE_SIZE))
943                                 goto out;
944
945                         data = xbuf[0];
946                         data += align_offset;
947                         memcpy(data, template[i].input, template[i].ilen);
948
949                         crypto_ablkcipher_clear_flags(tfm, ~0);
950                         if (template[i].wk)
951                                 crypto_ablkcipher_set_flags(
952                                         tfm, CRYPTO_TFM_REQ_WEAK_KEY);
953
954                         ret = crypto_ablkcipher_setkey(tfm, template[i].key,
955                                                        template[i].klen);
956                         if (!ret == template[i].fail) {
957                                 pr_err("alg: skcipher%s: setkey failed on test %d for %s: flags=%x\n",
958                                        d, j, algo,
959                                        crypto_ablkcipher_get_flags(tfm));
960                                 goto out;
961                         } else if (ret)
962                                 continue;
963
964                         sg_init_one(&sg[0], data, template[i].ilen);
965                         if (diff_dst) {
966                                 data = xoutbuf[0];
967                                 data += align_offset;
968                                 sg_init_one(&sgout[0], data, template[i].ilen);
969                         }
970
971                         ablkcipher_request_set_crypt(req, sg,
972                                                      (diff_dst) ? sgout : sg,
973                                                      template[i].ilen, iv);
974                         ret = enc ?
975                                 crypto_ablkcipher_encrypt(req) :
976                                 crypto_ablkcipher_decrypt(req);
977
978                         switch (ret) {
979                         case 0:
980                                 break;
981                         case -EINPROGRESS:
982                         case -EBUSY:
983                                 ret = wait_for_completion_interruptible(
984                                         &result.completion);
985                                 if (!ret && !((ret = result.err))) {
986                                         INIT_COMPLETION(result.completion);
987                                         break;
988                                 }
989                                 /* fall through */
990                         default:
991                                 pr_err("alg: skcipher%s: %s failed on test %d for %s: ret=%d\n",
992                                        d, e, j, algo, -ret);
993                                 goto out;
994                         }
995
996                         q = data;
997                         if (memcmp(q, template[i].result, template[i].rlen)) {
998                                 pr_err("alg: skcipher%s: Test %d failed on %s for %s\n",
999                                        d, j, e, algo);
1000                                 hexdump(q, template[i].rlen);
1001                                 ret = -EINVAL;
1002                                 goto out;
1003                         }
1004                 }
1005         }
1006
1007         j = 0;
1008         for (i = 0; i < tcount; i++) {
1009                 /* alignment tests are only done with continuous buffers */
1010                 if (align_offset != 0)
1011                         break;
1012
1013                 if (template[i].iv)
1014                         memcpy(iv, template[i].iv, MAX_IVLEN);
1015                 else
1016                         memset(iv, 0, MAX_IVLEN);
1017
1018                 if (template[i].np) {
1019                         j++;
1020
1021                         crypto_ablkcipher_clear_flags(tfm, ~0);
1022                         if (template[i].wk)
1023                                 crypto_ablkcipher_set_flags(
1024                                         tfm, CRYPTO_TFM_REQ_WEAK_KEY);
1025
1026                         ret = crypto_ablkcipher_setkey(tfm, template[i].key,
1027                                                        template[i].klen);
1028                         if (!ret == template[i].fail) {
1029                                 pr_err("alg: skcipher%s: setkey failed on chunk test %d for %s: flags=%x\n",
1030                                        d, j, algo,
1031                                        crypto_ablkcipher_get_flags(tfm));
1032                                 goto out;
1033                         } else if (ret)
1034                                 continue;
1035
1036                         temp = 0;
1037                         ret = -EINVAL;
1038                         sg_init_table(sg, template[i].np);
1039                         if (diff_dst)
1040                                 sg_init_table(sgout, template[i].np);
1041                         for (k = 0; k < template[i].np; k++) {
1042                                 if (WARN_ON(offset_in_page(IDX[k]) +
1043                                             template[i].tap[k] > PAGE_SIZE))
1044                                         goto out;
1045
1046                                 q = xbuf[IDX[k] >> PAGE_SHIFT] +
1047                                     offset_in_page(IDX[k]);
1048
1049                                 memcpy(q, template[i].input + temp,
1050                                        template[i].tap[k]);
1051
1052                                 if (offset_in_page(q) + template[i].tap[k] <
1053                                     PAGE_SIZE)
1054                                         q[template[i].tap[k]] = 0;
1055
1056                                 sg_set_buf(&sg[k], q, template[i].tap[k]);
1057                                 if (diff_dst) {
1058                                         q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
1059                                             offset_in_page(IDX[k]);
1060
1061                                         sg_set_buf(&sgout[k], q,
1062                                                    template[i].tap[k]);
1063
1064                                         memset(q, 0, template[i].tap[k]);
1065                                         if (offset_in_page(q) +
1066                                             template[i].tap[k] < PAGE_SIZE)
1067                                                 q[template[i].tap[k]] = 0;
1068                                 }
1069
1070                                 temp += template[i].tap[k];
1071                         }
1072
1073                         ablkcipher_request_set_crypt(req, sg,
1074                                         (diff_dst) ? sgout : sg,
1075                                         template[i].ilen, iv);
1076
1077                         ret = enc ?
1078                                 crypto_ablkcipher_encrypt(req) :
1079                                 crypto_ablkcipher_decrypt(req);
1080
1081                         switch (ret) {
1082                         case 0:
1083                                 break;
1084                         case -EINPROGRESS:
1085                         case -EBUSY:
1086                                 ret = wait_for_completion_interruptible(
1087                                         &result.completion);
1088                                 if (!ret && !((ret = result.err))) {
1089                                         INIT_COMPLETION(result.completion);
1090                                         break;
1091                                 }
1092                                 /* fall through */
1093                         default:
1094                                 pr_err("alg: skcipher%s: %s failed on chunk test %d for %s: ret=%d\n",
1095                                        d, e, j, algo, -ret);
1096                                 goto out;
1097                         }
1098
1099                         temp = 0;
1100                         ret = -EINVAL;
1101                         for (k = 0; k < template[i].np; k++) {
1102                                 if (diff_dst)
1103                                         q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
1104                                             offset_in_page(IDX[k]);
1105                                 else
1106                                         q = xbuf[IDX[k] >> PAGE_SHIFT] +
1107                                             offset_in_page(IDX[k]);
1108
1109                                 if (memcmp(q, template[i].result + temp,
1110                                            template[i].tap[k])) {
1111                                         pr_err("alg: skcipher%s: Chunk test %d failed on %s at page %u for %s\n",
1112                                                d, j, e, k, algo);
1113                                         hexdump(q, template[i].tap[k]);
1114                                         goto out;
1115                                 }
1116
1117                                 q += template[i].tap[k];
1118                                 for (n = 0; offset_in_page(q + n) && q[n]; n++)
1119                                         ;
1120                                 if (n) {
1121                                         pr_err("alg: skcipher%s: Result buffer corruption in chunk test %d on %s at page %u for %s: %u bytes:\n",
1122                                                d, j, e, k, algo, n);
1123                                         hexdump(q, n);
1124                                         goto out;
1125                                 }
1126                                 temp += template[i].tap[k];
1127                         }
1128                 }
1129         }
1130
1131         ret = 0;
1132
1133 out:
1134         ablkcipher_request_free(req);
1135         if (diff_dst)
1136                 testmgr_free_buf(xoutbuf);
1137 out_nooutbuf:
1138         testmgr_free_buf(xbuf);
1139 out_nobuf:
1140         return ret;
1141 }
1142
1143 static int test_skcipher(struct crypto_ablkcipher *tfm, int enc,
1144                          struct cipher_testvec *template, unsigned int tcount)
1145 {
1146         unsigned int alignmask;
1147         int ret;
1148
1149         /* test 'dst == src' case */
1150         ret = __test_skcipher(tfm, enc, template, tcount, false, 0);
1151         if (ret)
1152                 return ret;
1153
1154         /* test 'dst != src' case */
1155         ret = __test_skcipher(tfm, enc, template, tcount, true, 0);
1156         if (ret)
1157                 return ret;
1158
1159         /* test unaligned buffers, check with one byte offset */
1160         ret = __test_skcipher(tfm, enc, template, tcount, true, 1);
1161         if (ret)
1162                 return ret;
1163
1164         alignmask = crypto_tfm_alg_alignmask(&tfm->base);
1165         if (alignmask) {
1166                 /* Check if alignment mask for tfm is correctly set. */
1167                 ret = __test_skcipher(tfm, enc, template, tcount, true,
1168                                       alignmask + 1);
1169                 if (ret)
1170                         return ret;
1171         }
1172
1173         return 0;
1174 }
1175
1176 static int test_comp(struct crypto_comp *tfm, struct comp_testvec *ctemplate,
1177                      struct comp_testvec *dtemplate, int ctcount, int dtcount)
1178 {
1179         const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
1180         unsigned int i;
1181         char result[COMP_BUF_SIZE];
1182         int ret;
1183
1184         for (i = 0; i < ctcount; i++) {
1185                 int ilen;
1186                 unsigned int dlen = COMP_BUF_SIZE;
1187
1188                 memset(result, 0, sizeof (result));
1189
1190                 ilen = ctemplate[i].inlen;
1191                 ret = crypto_comp_compress(tfm, ctemplate[i].input,
1192                                            ilen, result, &dlen);
1193                 if (ret) {
1194                         printk(KERN_ERR "alg: comp: compression failed "
1195                                "on test %d for %s: ret=%d\n", i + 1, algo,
1196                                -ret);
1197                         goto out;
1198                 }
1199
1200                 if (dlen != ctemplate[i].outlen) {
1201                         printk(KERN_ERR "alg: comp: Compression test %d "
1202                                "failed for %s: output len = %d\n", i + 1, algo,
1203                                dlen);
1204                         ret = -EINVAL;
1205                         goto out;
1206                 }
1207
1208                 if (memcmp(result, ctemplate[i].output, dlen)) {
1209                         printk(KERN_ERR "alg: comp: Compression test %d "
1210                                "failed for %s\n", i + 1, algo);
1211                         hexdump(result, dlen);
1212                         ret = -EINVAL;
1213                         goto out;
1214                 }
1215         }
1216
1217         for (i = 0; i < dtcount; i++) {
1218                 int ilen;
1219                 unsigned int dlen = COMP_BUF_SIZE;
1220
1221                 memset(result, 0, sizeof (result));
1222
1223                 ilen = dtemplate[i].inlen;
1224                 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
1225                                              ilen, result, &dlen);
1226                 if (ret) {
1227                         printk(KERN_ERR "alg: comp: decompression failed "
1228                                "on test %d for %s: ret=%d\n", i + 1, algo,
1229                                -ret);
1230                         goto out;
1231                 }
1232
1233                 if (dlen != dtemplate[i].outlen) {
1234                         printk(KERN_ERR "alg: comp: Decompression test %d "
1235                                "failed for %s: output len = %d\n", i + 1, algo,
1236                                dlen);
1237                         ret = -EINVAL;
1238                         goto out;
1239                 }
1240
1241                 if (memcmp(result, dtemplate[i].output, dlen)) {
1242                         printk(KERN_ERR "alg: comp: Decompression test %d "
1243                                "failed for %s\n", i + 1, algo);
1244                         hexdump(result, dlen);
1245                         ret = -EINVAL;
1246                         goto out;
1247                 }
1248         }
1249
1250         ret = 0;
1251
1252 out:
1253         return ret;
1254 }
1255
1256 static int test_pcomp(struct crypto_pcomp *tfm,
1257                       struct pcomp_testvec *ctemplate,
1258                       struct pcomp_testvec *dtemplate, int ctcount,
1259                       int dtcount)
1260 {
1261         const char *algo = crypto_tfm_alg_driver_name(crypto_pcomp_tfm(tfm));
1262         unsigned int i;
1263         char result[COMP_BUF_SIZE];
1264         int res;
1265
1266         for (i = 0; i < ctcount; i++) {
1267                 struct comp_request req;
1268                 unsigned int produced = 0;
1269
1270                 res = crypto_compress_setup(tfm, ctemplate[i].params,
1271                                             ctemplate[i].paramsize);
1272                 if (res) {
1273                         pr_err("alg: pcomp: compression setup failed on test "
1274                                "%d for %s: error=%d\n", i + 1, algo, res);
1275                         return res;
1276                 }
1277
1278                 res = crypto_compress_init(tfm);
1279                 if (res) {
1280                         pr_err("alg: pcomp: compression init failed on test "
1281                                "%d for %s: error=%d\n", i + 1, algo, res);
1282                         return res;
1283                 }
1284
1285                 memset(result, 0, sizeof(result));
1286
1287                 req.next_in = ctemplate[i].input;
1288                 req.avail_in = ctemplate[i].inlen / 2;
1289                 req.next_out = result;
1290                 req.avail_out = ctemplate[i].outlen / 2;
1291
1292                 res = crypto_compress_update(tfm, &req);
1293                 if (res < 0 && (res != -EAGAIN || req.avail_in)) {
1294                         pr_err("alg: pcomp: compression update failed on test "
1295                                "%d for %s: error=%d\n", i + 1, algo, res);
1296                         return res;
1297                 }
1298                 if (res > 0)
1299                         produced += res;
1300
1301                 /* Add remaining input data */
1302                 req.avail_in += (ctemplate[i].inlen + 1) / 2;
1303
1304                 res = crypto_compress_update(tfm, &req);
1305                 if (res < 0 && (res != -EAGAIN || req.avail_in)) {
1306                         pr_err("alg: pcomp: compression update failed on test "
1307                                "%d for %s: error=%d\n", i + 1, algo, res);
1308                         return res;
1309                 }
1310                 if (res > 0)
1311                         produced += res;
1312
1313                 /* Provide remaining output space */
1314                 req.avail_out += COMP_BUF_SIZE - ctemplate[i].outlen / 2;
1315
1316                 res = crypto_compress_final(tfm, &req);
1317                 if (res < 0) {
1318                         pr_err("alg: pcomp: compression final failed on test "
1319                                "%d for %s: error=%d\n", i + 1, algo, res);
1320                         return res;
1321                 }
1322                 produced += res;
1323
1324                 if (COMP_BUF_SIZE - req.avail_out != ctemplate[i].outlen) {
1325                         pr_err("alg: comp: Compression test %d failed for %s: "
1326                                "output len = %d (expected %d)\n", i + 1, algo,
1327                                COMP_BUF_SIZE - req.avail_out,
1328                                ctemplate[i].outlen);
1329                         return -EINVAL;
1330                 }
1331
1332                 if (produced != ctemplate[i].outlen) {
1333                         pr_err("alg: comp: Compression test %d failed for %s: "
1334                                "returned len = %u (expected %d)\n", i + 1,
1335                                algo, produced, ctemplate[i].outlen);
1336                         return -EINVAL;
1337                 }
1338
1339                 if (memcmp(result, ctemplate[i].output, ctemplate[i].outlen)) {
1340                         pr_err("alg: pcomp: Compression test %d failed for "
1341                                "%s\n", i + 1, algo);
1342                         hexdump(result, ctemplate[i].outlen);
1343                         return -EINVAL;
1344                 }
1345         }
1346
1347         for (i = 0; i < dtcount; i++) {
1348                 struct comp_request req;
1349                 unsigned int produced = 0;
1350
1351                 res = crypto_decompress_setup(tfm, dtemplate[i].params,
1352                                               dtemplate[i].paramsize);
1353                 if (res) {
1354                         pr_err("alg: pcomp: decompression setup failed on "
1355                                "test %d for %s: error=%d\n", i + 1, algo, res);
1356                         return res;
1357                 }
1358
1359                 res = crypto_decompress_init(tfm);
1360                 if (res) {
1361                         pr_err("alg: pcomp: decompression init failed on test "
1362                                "%d for %s: error=%d\n", i + 1, algo, res);
1363                         return res;
1364                 }
1365
1366                 memset(result, 0, sizeof(result));
1367
1368                 req.next_in = dtemplate[i].input;
1369                 req.avail_in = dtemplate[i].inlen / 2;
1370                 req.next_out = result;
1371                 req.avail_out = dtemplate[i].outlen / 2;
1372
1373                 res = crypto_decompress_update(tfm, &req);
1374                 if (res < 0 && (res != -EAGAIN || req.avail_in)) {
1375                         pr_err("alg: pcomp: decompression update failed on "
1376                                "test %d for %s: error=%d\n", i + 1, algo, res);
1377                         return res;
1378                 }
1379                 if (res > 0)
1380                         produced += res;
1381
1382                 /* Add remaining input data */
1383                 req.avail_in += (dtemplate[i].inlen + 1) / 2;
1384
1385                 res = crypto_decompress_update(tfm, &req);
1386                 if (res < 0 && (res != -EAGAIN || req.avail_in)) {
1387                         pr_err("alg: pcomp: decompression update failed on "
1388                                "test %d for %s: error=%d\n", i + 1, algo, res);
1389                         return res;
1390                 }
1391                 if (res > 0)
1392                         produced += res;
1393
1394                 /* Provide remaining output space */
1395                 req.avail_out += COMP_BUF_SIZE - dtemplate[i].outlen / 2;
1396
1397                 res = crypto_decompress_final(tfm, &req);
1398                 if (res < 0 && (res != -EAGAIN || req.avail_in)) {
1399                         pr_err("alg: pcomp: decompression final failed on "
1400                                "test %d for %s: error=%d\n", i + 1, algo, res);
1401                         return res;
1402                 }
1403                 if (res > 0)
1404                         produced += res;
1405
1406                 if (COMP_BUF_SIZE - req.avail_out != dtemplate[i].outlen) {
1407                         pr_err("alg: comp: Decompression test %d failed for "
1408                                "%s: output len = %d (expected %d)\n", i + 1,
1409                                algo, COMP_BUF_SIZE - req.avail_out,
1410                                dtemplate[i].outlen);
1411                         return -EINVAL;
1412                 }
1413
1414                 if (produced != dtemplate[i].outlen) {
1415                         pr_err("alg: comp: Decompression test %d failed for "
1416                                "%s: returned len = %u (expected %d)\n", i + 1,
1417                                algo, produced, dtemplate[i].outlen);
1418                         return -EINVAL;
1419                 }
1420
1421                 if (memcmp(result, dtemplate[i].output, dtemplate[i].outlen)) {
1422                         pr_err("alg: pcomp: Decompression test %d failed for "
1423                                "%s\n", i + 1, algo);
1424                         hexdump(result, dtemplate[i].outlen);
1425                         return -EINVAL;
1426                 }
1427         }
1428
1429         return 0;
1430 }
1431
1432
1433 static int test_cprng(struct crypto_rng *tfm, struct cprng_testvec *template,
1434                       unsigned int tcount)
1435 {
1436         const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
1437         int err = 0, i, j, seedsize;
1438         u8 *seed;
1439         char result[32];
1440
1441         seedsize = crypto_rng_seedsize(tfm);
1442
1443         seed = kmalloc(seedsize, GFP_KERNEL);
1444         if (!seed) {
1445                 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
1446                        "for %s\n", algo);
1447                 return -ENOMEM;
1448         }
1449
1450         for (i = 0; i < tcount; i++) {
1451                 memset(result, 0, 32);
1452
1453                 memcpy(seed, template[i].v, template[i].vlen);
1454                 memcpy(seed + template[i].vlen, template[i].key,
1455                        template[i].klen);
1456                 memcpy(seed + template[i].vlen + template[i].klen,
1457                        template[i].dt, template[i].dtlen);
1458
1459                 err = crypto_rng_reset(tfm, seed, seedsize);
1460                 if (err) {
1461                         printk(KERN_ERR "alg: cprng: Failed to reset rng "
1462                                "for %s\n", algo);
1463                         goto out;
1464                 }
1465
1466                 for (j = 0; j < template[i].loops; j++) {
1467                         err = crypto_rng_get_bytes(tfm, result,
1468                                                    template[i].rlen);
1469                         if (err != template[i].rlen) {
1470                                 printk(KERN_ERR "alg: cprng: Failed to obtain "
1471                                        "the correct amount of random data for "
1472                                        "%s (requested %d, got %d)\n", algo,
1473                                        template[i].rlen, err);
1474                                 goto out;
1475                         }
1476                 }
1477
1478                 err = memcmp(result, template[i].result,
1479                              template[i].rlen);
1480                 if (err) {
1481                         printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
1482                                i, algo);
1483                         hexdump(result, template[i].rlen);
1484                         err = -EINVAL;
1485                         goto out;
1486                 }
1487         }
1488
1489 out:
1490         kfree(seed);
1491         return err;
1492 }
1493
1494 static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
1495                          u32 type, u32 mask)
1496 {
1497         struct crypto_aead *tfm;
1498         int err = 0;
1499
1500         tfm = crypto_alloc_aead(driver, type, mask);
1501         if (IS_ERR(tfm)) {
1502                 printk(KERN_ERR "alg: aead: Failed to load transform for %s: "
1503                        "%ld\n", driver, PTR_ERR(tfm));
1504                 return PTR_ERR(tfm);
1505         }
1506
1507         if (desc->suite.aead.enc.vecs) {
1508                 err = test_aead(tfm, ENCRYPT, desc->suite.aead.enc.vecs,
1509                                 desc->suite.aead.enc.count);
1510                 if (err)
1511                         goto out;
1512         }
1513
1514         if (!err && desc->suite.aead.dec.vecs)
1515                 err = test_aead(tfm, DECRYPT, desc->suite.aead.dec.vecs,
1516                                 desc->suite.aead.dec.count);
1517
1518 out:
1519         crypto_free_aead(tfm);
1520         return err;
1521 }
1522
1523 static int alg_test_cipher(const struct alg_test_desc *desc,
1524                            const char *driver, u32 type, u32 mask)
1525 {
1526         struct crypto_cipher *tfm;
1527         int err = 0;
1528
1529         tfm = crypto_alloc_cipher(driver, type, mask);
1530         if (IS_ERR(tfm)) {
1531                 printk(KERN_ERR "alg: cipher: Failed to load transform for "
1532                        "%s: %ld\n", driver, PTR_ERR(tfm));
1533                 return PTR_ERR(tfm);
1534         }
1535
1536         if (desc->suite.cipher.enc.vecs) {
1537                 err = test_cipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
1538                                   desc->suite.cipher.enc.count);
1539                 if (err)
1540                         goto out;
1541         }
1542
1543         if (desc->suite.cipher.dec.vecs)
1544                 err = test_cipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
1545                                   desc->suite.cipher.dec.count);
1546
1547 out:
1548         crypto_free_cipher(tfm);
1549         return err;
1550 }
1551
1552 static int alg_test_skcipher(const struct alg_test_desc *desc,
1553                              const char *driver, u32 type, u32 mask)
1554 {
1555         struct crypto_ablkcipher *tfm;
1556         int err = 0;
1557
1558         tfm = crypto_alloc_ablkcipher(driver, type, mask);
1559         if (IS_ERR(tfm)) {
1560                 printk(KERN_ERR "alg: skcipher: Failed to load transform for "
1561                        "%s: %ld\n", driver, PTR_ERR(tfm));
1562                 return PTR_ERR(tfm);
1563         }
1564
1565         if (desc->suite.cipher.enc.vecs) {
1566                 err = test_skcipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
1567                                     desc->suite.cipher.enc.count);
1568                 if (err)
1569                         goto out;
1570         }
1571
1572         if (desc->suite.cipher.dec.vecs)
1573                 err = test_skcipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
1574                                     desc->suite.cipher.dec.count);
1575
1576 out:
1577         crypto_free_ablkcipher(tfm);
1578         return err;
1579 }
1580
1581 static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
1582                          u32 type, u32 mask)
1583 {
1584         struct crypto_comp *tfm;
1585         int err;
1586
1587         tfm = crypto_alloc_comp(driver, type, mask);
1588         if (IS_ERR(tfm)) {
1589                 printk(KERN_ERR "alg: comp: Failed to load transform for %s: "
1590                        "%ld\n", driver, PTR_ERR(tfm));
1591                 return PTR_ERR(tfm);
1592         }
1593
1594         err = test_comp(tfm, desc->suite.comp.comp.vecs,
1595                         desc->suite.comp.decomp.vecs,
1596                         desc->suite.comp.comp.count,
1597                         desc->suite.comp.decomp.count);
1598
1599         crypto_free_comp(tfm);
1600         return err;
1601 }
1602
1603 static int alg_test_pcomp(const struct alg_test_desc *desc, const char *driver,
1604                           u32 type, u32 mask)
1605 {
1606         struct crypto_pcomp *tfm;
1607         int err;
1608
1609         tfm = crypto_alloc_pcomp(driver, type, mask);
1610         if (IS_ERR(tfm)) {
1611                 pr_err("alg: pcomp: Failed to load transform for %s: %ld\n",
1612                        driver, PTR_ERR(tfm));
1613                 return PTR_ERR(tfm);
1614         }
1615
1616         err = test_pcomp(tfm, desc->suite.pcomp.comp.vecs,
1617                          desc->suite.pcomp.decomp.vecs,
1618                          desc->suite.pcomp.comp.count,
1619                          desc->suite.pcomp.decomp.count);
1620
1621         crypto_free_pcomp(tfm);
1622         return err;
1623 }
1624
1625 static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1626                          u32 type, u32 mask)
1627 {
1628         struct crypto_ahash *tfm;
1629         int err;
1630
1631         tfm = crypto_alloc_ahash(driver, type, mask);
1632         if (IS_ERR(tfm)) {
1633                 printk(KERN_ERR "alg: hash: Failed to load transform for %s: "
1634                        "%ld\n", driver, PTR_ERR(tfm));
1635                 return PTR_ERR(tfm);
1636         }
1637
1638         err = test_hash(tfm, desc->suite.hash.vecs,
1639                         desc->suite.hash.count, true);
1640         if (!err)
1641                 err = test_hash(tfm, desc->suite.hash.vecs,
1642                                 desc->suite.hash.count, false);
1643
1644         crypto_free_ahash(tfm);
1645         return err;
1646 }
1647
1648 static int alg_test_crc32c(const struct alg_test_desc *desc,
1649                            const char *driver, u32 type, u32 mask)
1650 {
1651         struct crypto_shash *tfm;
1652         u32 val;
1653         int err;
1654
1655         err = alg_test_hash(desc, driver, type, mask);
1656         if (err)
1657                 goto out;
1658
1659         tfm = crypto_alloc_shash(driver, type, mask);
1660         if (IS_ERR(tfm)) {
1661                 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
1662                        "%ld\n", driver, PTR_ERR(tfm));
1663                 err = PTR_ERR(tfm);
1664                 goto out;
1665         }
1666
1667         do {
1668                 struct {
1669                         struct shash_desc shash;
1670                         char ctx[crypto_shash_descsize(tfm)];
1671                 } sdesc;
1672
1673                 sdesc.shash.tfm = tfm;
1674                 sdesc.shash.flags = 0;
1675
1676                 *(u32 *)sdesc.ctx = le32_to_cpu(420553207);
1677                 err = crypto_shash_final(&sdesc.shash, (u8 *)&val);
1678                 if (err) {
1679                         printk(KERN_ERR "alg: crc32c: Operation failed for "
1680                                "%s: %d\n", driver, err);
1681                         break;
1682                 }
1683
1684                 if (val != ~420553207) {
1685                         printk(KERN_ERR "alg: crc32c: Test failed for %s: "
1686                                "%d\n", driver, val);
1687                         err = -EINVAL;
1688                 }
1689         } while (0);
1690
1691         crypto_free_shash(tfm);
1692
1693 out:
1694         return err;
1695 }
1696
1697 static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
1698                           u32 type, u32 mask)
1699 {
1700         struct crypto_rng *rng;
1701         int err;
1702
1703         rng = crypto_alloc_rng(driver, type, mask);
1704         if (IS_ERR(rng)) {
1705                 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
1706                        "%ld\n", driver, PTR_ERR(rng));
1707                 return PTR_ERR(rng);
1708         }
1709
1710         err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
1711
1712         crypto_free_rng(rng);
1713
1714         return err;
1715 }
1716
1717 static int alg_test_null(const struct alg_test_desc *desc,
1718                              const char *driver, u32 type, u32 mask)
1719 {
1720         return 0;
1721 }
1722
1723 /* Please keep this list sorted by algorithm name. */
1724 static const struct alg_test_desc alg_test_descs[] = {
1725         {
1726                 .alg = "__cbc-cast5-avx",
1727                 .test = alg_test_null,
1728         }, {
1729                 .alg = "__cbc-cast6-avx",
1730                 .test = alg_test_null,
1731         }, {
1732                 .alg = "__cbc-serpent-avx",
1733                 .test = alg_test_null,
1734         }, {
1735                 .alg = "__cbc-serpent-avx2",
1736                 .test = alg_test_null,
1737         }, {
1738                 .alg = "__cbc-serpent-sse2",
1739                 .test = alg_test_null,
1740         }, {
1741                 .alg = "__cbc-twofish-avx",
1742                 .test = alg_test_null,
1743         }, {
1744                 .alg = "__driver-cbc-aes-aesni",
1745                 .test = alg_test_null,
1746                 .fips_allowed = 1,
1747         }, {
1748                 .alg = "__driver-cbc-camellia-aesni",
1749                 .test = alg_test_null,
1750         }, {
1751                 .alg = "__driver-cbc-camellia-aesni-avx2",
1752                 .test = alg_test_null,
1753         }, {
1754                 .alg = "__driver-cbc-cast5-avx",
1755                 .test = alg_test_null,
1756         }, {
1757                 .alg = "__driver-cbc-cast6-avx",
1758                 .test = alg_test_null,
1759         }, {
1760                 .alg = "__driver-cbc-serpent-avx",
1761                 .test = alg_test_null,
1762         }, {
1763                 .alg = "__driver-cbc-serpent-avx2",
1764                 .test = alg_test_null,
1765         }, {
1766                 .alg = "__driver-cbc-serpent-sse2",
1767                 .test = alg_test_null,
1768         }, {
1769                 .alg = "__driver-cbc-twofish-avx",
1770                 .test = alg_test_null,
1771         }, {
1772                 .alg = "__driver-ecb-aes-aesni",
1773                 .test = alg_test_null,
1774                 .fips_allowed = 1,
1775         }, {
1776                 .alg = "__driver-ecb-camellia-aesni",
1777                 .test = alg_test_null,
1778         }, {
1779                 .alg = "__driver-ecb-camellia-aesni-avx2",
1780                 .test = alg_test_null,
1781         }, {
1782                 .alg = "__driver-ecb-cast5-avx",
1783                 .test = alg_test_null,
1784         }, {
1785                 .alg = "__driver-ecb-cast6-avx",
1786                 .test = alg_test_null,
1787         }, {
1788                 .alg = "__driver-ecb-serpent-avx",
1789                 .test = alg_test_null,
1790         }, {
1791                 .alg = "__driver-ecb-serpent-avx2",
1792                 .test = alg_test_null,
1793         }, {
1794                 .alg = "__driver-ecb-serpent-sse2",
1795                 .test = alg_test_null,
1796         }, {
1797                 .alg = "__driver-ecb-twofish-avx",
1798                 .test = alg_test_null,
1799         }, {
1800                 .alg = "__ghash-pclmulqdqni",
1801                 .test = alg_test_null,
1802                 .fips_allowed = 1,
1803         }, {
1804                 .alg = "ansi_cprng",
1805                 .test = alg_test_cprng,
1806                 .fips_allowed = 1,
1807                 .suite = {
1808                         .cprng = {
1809                                 .vecs = ansi_cprng_aes_tv_template,
1810                                 .count = ANSI_CPRNG_AES_TEST_VECTORS
1811                         }
1812                 }
1813         }, {
1814                 .alg = "authenc(hmac(sha1),cbc(aes))",
1815                 .test = alg_test_aead,
1816                 .fips_allowed = 1,
1817                 .suite = {
1818                         .aead = {
1819                                 .enc = {
1820                                         .vecs = hmac_sha1_aes_cbc_enc_tv_template,
1821                                         .count = HMAC_SHA1_AES_CBC_ENC_TEST_VECTORS
1822                                 }
1823                         }
1824                 }
1825         }, {
1826                 .alg = "authenc(hmac(sha256),cbc(aes))",
1827                 .test = alg_test_aead,
1828                 .fips_allowed = 1,
1829                 .suite = {
1830                         .aead = {
1831                                 .enc = {
1832                                         .vecs = hmac_sha256_aes_cbc_enc_tv_template,
1833                                         .count = HMAC_SHA256_AES_CBC_ENC_TEST_VECTORS
1834                                 }
1835                         }
1836                 }
1837         }, {
1838                 .alg = "authenc(hmac(sha512),cbc(aes))",
1839                 .test = alg_test_aead,
1840                 .fips_allowed = 1,
1841                 .suite = {
1842                         .aead = {
1843                                 .enc = {
1844                                         .vecs = hmac_sha512_aes_cbc_enc_tv_template,
1845                                         .count = HMAC_SHA512_AES_CBC_ENC_TEST_VECTORS
1846                                 }
1847                         }
1848                 }
1849         }, {
1850                 .alg = "cbc(aes)",
1851                 .test = alg_test_skcipher,
1852                 .fips_allowed = 1,
1853                 .suite = {
1854                         .cipher = {
1855                                 .enc = {
1856                                         .vecs = aes_cbc_enc_tv_template,
1857                                         .count = AES_CBC_ENC_TEST_VECTORS
1858                                 },
1859                                 .dec = {
1860                                         .vecs = aes_cbc_dec_tv_template,
1861                                         .count = AES_CBC_DEC_TEST_VECTORS
1862                                 }
1863                         }
1864                 }
1865         }, {
1866                 .alg = "cbc(anubis)",
1867                 .test = alg_test_skcipher,
1868                 .suite = {
1869                         .cipher = {
1870                                 .enc = {
1871                                         .vecs = anubis_cbc_enc_tv_template,
1872                                         .count = ANUBIS_CBC_ENC_TEST_VECTORS
1873                                 },
1874                                 .dec = {
1875                                         .vecs = anubis_cbc_dec_tv_template,
1876                                         .count = ANUBIS_CBC_DEC_TEST_VECTORS
1877                                 }
1878                         }
1879                 }
1880         }, {
1881                 .alg = "cbc(blowfish)",
1882                 .test = alg_test_skcipher,
1883                 .suite = {
1884                         .cipher = {
1885                                 .enc = {
1886                                         .vecs = bf_cbc_enc_tv_template,
1887                                         .count = BF_CBC_ENC_TEST_VECTORS
1888                                 },
1889                                 .dec = {
1890                                         .vecs = bf_cbc_dec_tv_template,
1891                                         .count = BF_CBC_DEC_TEST_VECTORS
1892                                 }
1893                         }
1894                 }
1895         }, {
1896                 .alg = "cbc(camellia)",
1897                 .test = alg_test_skcipher,
1898                 .suite = {
1899                         .cipher = {
1900                                 .enc = {
1901                                         .vecs = camellia_cbc_enc_tv_template,
1902                                         .count = CAMELLIA_CBC_ENC_TEST_VECTORS
1903                                 },
1904                                 .dec = {
1905                                         .vecs = camellia_cbc_dec_tv_template,
1906                                         .count = CAMELLIA_CBC_DEC_TEST_VECTORS
1907                                 }
1908                         }
1909                 }
1910         }, {
1911                 .alg = "cbc(cast5)",
1912                 .test = alg_test_skcipher,
1913                 .suite = {
1914                         .cipher = {
1915                                 .enc = {
1916                                         .vecs = cast5_cbc_enc_tv_template,
1917                                         .count = CAST5_CBC_ENC_TEST_VECTORS
1918                                 },
1919                                 .dec = {
1920                                         .vecs = cast5_cbc_dec_tv_template,
1921                                         .count = CAST5_CBC_DEC_TEST_VECTORS
1922                                 }
1923                         }
1924                 }
1925         }, {
1926                 .alg = "cbc(cast6)",
1927                 .test = alg_test_skcipher,
1928                 .suite = {
1929                         .cipher = {
1930                                 .enc = {
1931                                         .vecs = cast6_cbc_enc_tv_template,
1932                                         .count = CAST6_CBC_ENC_TEST_VECTORS
1933                                 },
1934                                 .dec = {
1935                                         .vecs = cast6_cbc_dec_tv_template,
1936                                         .count = CAST6_CBC_DEC_TEST_VECTORS
1937                                 }
1938                         }
1939                 }
1940         }, {
1941                 .alg = "cbc(des)",
1942                 .test = alg_test_skcipher,
1943                 .suite = {
1944                         .cipher = {
1945                                 .enc = {
1946                                         .vecs = des_cbc_enc_tv_template,
1947                                         .count = DES_CBC_ENC_TEST_VECTORS
1948                                 },
1949                                 .dec = {
1950                                         .vecs = des_cbc_dec_tv_template,
1951                                         .count = DES_CBC_DEC_TEST_VECTORS
1952                                 }
1953                         }
1954                 }
1955         }, {
1956                 .alg = "cbc(des3_ede)",
1957                 .test = alg_test_skcipher,
1958                 .fips_allowed = 1,
1959                 .suite = {
1960                         .cipher = {
1961                                 .enc = {
1962                                         .vecs = des3_ede_cbc_enc_tv_template,
1963                                         .count = DES3_EDE_CBC_ENC_TEST_VECTORS
1964                                 },
1965                                 .dec = {
1966                                         .vecs = des3_ede_cbc_dec_tv_template,
1967                                         .count = DES3_EDE_CBC_DEC_TEST_VECTORS
1968                                 }
1969                         }
1970                 }
1971         }, {
1972                 .alg = "cbc(serpent)",
1973                 .test = alg_test_skcipher,
1974                 .suite = {
1975                         .cipher = {
1976                                 .enc = {
1977                                         .vecs = serpent_cbc_enc_tv_template,
1978                                         .count = SERPENT_CBC_ENC_TEST_VECTORS
1979                                 },
1980                                 .dec = {
1981                                         .vecs = serpent_cbc_dec_tv_template,
1982                                         .count = SERPENT_CBC_DEC_TEST_VECTORS
1983                                 }
1984                         }
1985                 }
1986         }, {
1987                 .alg = "cbc(twofish)",
1988                 .test = alg_test_skcipher,
1989                 .suite = {
1990                         .cipher = {
1991                                 .enc = {
1992                                         .vecs = tf_cbc_enc_tv_template,
1993                                         .count = TF_CBC_ENC_TEST_VECTORS
1994                                 },
1995                                 .dec = {
1996                                         .vecs = tf_cbc_dec_tv_template,
1997                                         .count = TF_CBC_DEC_TEST_VECTORS
1998                                 }
1999                         }
2000                 }
2001         }, {
2002                 .alg = "ccm(aes)",
2003                 .test = alg_test_aead,
2004                 .fips_allowed = 1,
2005                 .suite = {
2006                         .aead = {
2007                                 .enc = {
2008                                         .vecs = aes_ccm_enc_tv_template,
2009                                         .count = AES_CCM_ENC_TEST_VECTORS
2010                                 },
2011                                 .dec = {
2012                                         .vecs = aes_ccm_dec_tv_template,
2013                                         .count = AES_CCM_DEC_TEST_VECTORS
2014                                 }
2015                         }
2016                 }
2017         }, {
2018                 .alg = "cmac(aes)",
2019                 .test = alg_test_hash,
2020                 .suite = {
2021                         .hash = {
2022                                 .vecs = aes_cmac128_tv_template,
2023                                 .count = CMAC_AES_TEST_VECTORS
2024                         }
2025                 }
2026         }, {
2027                 .alg = "cmac(des3_ede)",
2028                 .test = alg_test_hash,
2029                 .suite = {
2030                         .hash = {
2031                                 .vecs = des3_ede_cmac64_tv_template,
2032                                 .count = CMAC_DES3_EDE_TEST_VECTORS
2033                         }
2034                 }
2035         }, {
2036                 .alg = "compress_null",
2037                 .test = alg_test_null,
2038         }, {
2039                 .alg = "crc32c",
2040                 .test = alg_test_crc32c,
2041                 .fips_allowed = 1,
2042                 .suite = {
2043                         .hash = {
2044                                 .vecs = crc32c_tv_template,
2045                                 .count = CRC32C_TEST_VECTORS
2046                         }
2047                 }
2048         }, {
2049                 .alg = "cryptd(__driver-cbc-aes-aesni)",
2050                 .test = alg_test_null,
2051                 .fips_allowed = 1,
2052         }, {
2053                 .alg = "cryptd(__driver-cbc-camellia-aesni)",
2054                 .test = alg_test_null,
2055         }, {
2056                 .alg = "cryptd(__driver-cbc-camellia-aesni-avx2)",
2057                 .test = alg_test_null,
2058         }, {
2059                 .alg = "cryptd(__driver-cbc-serpent-avx2)",
2060                 .test = alg_test_null,
2061         }, {
2062                 .alg = "cryptd(__driver-ecb-aes-aesni)",
2063                 .test = alg_test_null,
2064                 .fips_allowed = 1,
2065         }, {
2066                 .alg = "cryptd(__driver-ecb-camellia-aesni)",
2067                 .test = alg_test_null,
2068         }, {
2069                 .alg = "cryptd(__driver-ecb-camellia-aesni-avx2)",
2070                 .test = alg_test_null,
2071         }, {
2072                 .alg = "cryptd(__driver-ecb-cast5-avx)",
2073                 .test = alg_test_null,
2074         }, {
2075                 .alg = "cryptd(__driver-ecb-cast6-avx)",
2076                 .test = alg_test_null,
2077         }, {
2078                 .alg = "cryptd(__driver-ecb-serpent-avx)",
2079                 .test = alg_test_null,
2080         }, {
2081                 .alg = "cryptd(__driver-ecb-serpent-avx2)",
2082                 .test = alg_test_null,
2083         }, {
2084                 .alg = "cryptd(__driver-ecb-serpent-sse2)",
2085                 .test = alg_test_null,
2086         }, {
2087                 .alg = "cryptd(__driver-ecb-twofish-avx)",
2088                 .test = alg_test_null,
2089         }, {
2090                 .alg = "cryptd(__driver-gcm-aes-aesni)",
2091                 .test = alg_test_null,
2092                 .fips_allowed = 1,
2093         }, {
2094                 .alg = "cryptd(__ghash-pclmulqdqni)",
2095                 .test = alg_test_null,
2096                 .fips_allowed = 1,
2097         }, {
2098                 .alg = "ctr(aes)",
2099                 .test = alg_test_skcipher,
2100                 .fips_allowed = 1,
2101                 .suite = {
2102                         .cipher = {
2103                                 .enc = {
2104                                         .vecs = aes_ctr_enc_tv_template,
2105                                         .count = AES_CTR_ENC_TEST_VECTORS
2106                                 },
2107                                 .dec = {
2108                                         .vecs = aes_ctr_dec_tv_template,
2109                                         .count = AES_CTR_DEC_TEST_VECTORS
2110                                 }
2111                         }
2112                 }
2113         }, {
2114                 .alg = "ctr(blowfish)",
2115                 .test = alg_test_skcipher,
2116                 .suite = {
2117                         .cipher = {
2118                                 .enc = {
2119                                         .vecs = bf_ctr_enc_tv_template,
2120                                         .count = BF_CTR_ENC_TEST_VECTORS
2121                                 },
2122                                 .dec = {
2123                                         .vecs = bf_ctr_dec_tv_template,
2124                                         .count = BF_CTR_DEC_TEST_VECTORS
2125                                 }
2126                         }
2127                 }
2128         }, {
2129                 .alg = "ctr(camellia)",
2130                 .test = alg_test_skcipher,
2131                 .suite = {
2132                         .cipher = {
2133                                 .enc = {
2134                                         .vecs = camellia_ctr_enc_tv_template,
2135                                         .count = CAMELLIA_CTR_ENC_TEST_VECTORS
2136                                 },
2137                                 .dec = {
2138                                         .vecs = camellia_ctr_dec_tv_template,
2139                                         .count = CAMELLIA_CTR_DEC_TEST_VECTORS
2140                                 }
2141                         }
2142                 }
2143         }, {
2144                 .alg = "ctr(cast5)",
2145                 .test = alg_test_skcipher,
2146                 .suite = {
2147                         .cipher = {
2148                                 .enc = {
2149                                         .vecs = cast5_ctr_enc_tv_template,
2150                                         .count = CAST5_CTR_ENC_TEST_VECTORS
2151                                 },
2152                                 .dec = {
2153                                         .vecs = cast5_ctr_dec_tv_template,
2154                                         .count = CAST5_CTR_DEC_TEST_VECTORS
2155                                 }
2156                         }
2157                 }
2158         }, {
2159                 .alg = "ctr(cast6)",
2160                 .test = alg_test_skcipher,
2161                 .suite = {
2162                         .cipher = {
2163                                 .enc = {
2164                                         .vecs = cast6_ctr_enc_tv_template,
2165                                         .count = CAST6_CTR_ENC_TEST_VECTORS
2166                                 },
2167                                 .dec = {
2168                                         .vecs = cast6_ctr_dec_tv_template,
2169                                         .count = CAST6_CTR_DEC_TEST_VECTORS
2170                                 }
2171                         }
2172                 }
2173         }, {
2174                 .alg = "ctr(des)",
2175                 .test = alg_test_skcipher,
2176                 .suite = {
2177                         .cipher = {
2178                                 .enc = {
2179                                         .vecs = des_ctr_enc_tv_template,
2180                                         .count = DES_CTR_ENC_TEST_VECTORS
2181                                 },
2182                                 .dec = {
2183                                         .vecs = des_ctr_dec_tv_template,
2184                                         .count = DES_CTR_DEC_TEST_VECTORS
2185                                 }
2186                         }
2187                 }
2188         }, {
2189                 .alg = "ctr(des3_ede)",
2190                 .test = alg_test_skcipher,
2191                 .suite = {
2192                         .cipher = {
2193                                 .enc = {
2194                                         .vecs = des3_ede_ctr_enc_tv_template,
2195                                         .count = DES3_EDE_CTR_ENC_TEST_VECTORS
2196                                 },
2197                                 .dec = {
2198                                         .vecs = des3_ede_ctr_dec_tv_template,
2199                                         .count = DES3_EDE_CTR_DEC_TEST_VECTORS
2200                                 }
2201                         }
2202                 }
2203         }, {
2204                 .alg = "ctr(serpent)",
2205                 .test = alg_test_skcipher,
2206                 .suite = {
2207                         .cipher = {
2208                                 .enc = {
2209                                         .vecs = serpent_ctr_enc_tv_template,
2210                                         .count = SERPENT_CTR_ENC_TEST_VECTORS
2211                                 },
2212                                 .dec = {
2213                                         .vecs = serpent_ctr_dec_tv_template,
2214                                         .count = SERPENT_CTR_DEC_TEST_VECTORS
2215                                 }
2216                         }
2217                 }
2218         }, {
2219                 .alg = "ctr(twofish)",
2220                 .test = alg_test_skcipher,
2221                 .suite = {
2222                         .cipher = {
2223                                 .enc = {
2224                                         .vecs = tf_ctr_enc_tv_template,
2225                                         .count = TF_CTR_ENC_TEST_VECTORS
2226                                 },
2227                                 .dec = {
2228                                         .vecs = tf_ctr_dec_tv_template,
2229                                         .count = TF_CTR_DEC_TEST_VECTORS
2230                                 }
2231                         }
2232                 }
2233         }, {
2234                 .alg = "cts(cbc(aes))",
2235                 .test = alg_test_skcipher,
2236                 .suite = {
2237                         .cipher = {
2238                                 .enc = {
2239                                         .vecs = cts_mode_enc_tv_template,
2240                                         .count = CTS_MODE_ENC_TEST_VECTORS
2241                                 },
2242                                 .dec = {
2243                                         .vecs = cts_mode_dec_tv_template,
2244                                         .count = CTS_MODE_DEC_TEST_VECTORS
2245                                 }
2246                         }
2247                 }
2248         }, {
2249                 .alg = "deflate",
2250                 .test = alg_test_comp,
2251                 .fips_allowed = 1,
2252                 .suite = {
2253                         .comp = {
2254                                 .comp = {
2255                                         .vecs = deflate_comp_tv_template,
2256                                         .count = DEFLATE_COMP_TEST_VECTORS
2257                                 },
2258                                 .decomp = {
2259                                         .vecs = deflate_decomp_tv_template,
2260                                         .count = DEFLATE_DECOMP_TEST_VECTORS
2261                                 }
2262                         }
2263                 }
2264         }, {
2265                 .alg = "digest_null",
2266                 .test = alg_test_null,
2267         }, {
2268                 .alg = "ecb(__aes-aesni)",
2269                 .test = alg_test_null,
2270                 .fips_allowed = 1,
2271         }, {
2272                 .alg = "ecb(aes)",
2273                 .test = alg_test_skcipher,
2274                 .fips_allowed = 1,
2275                 .suite = {
2276                         .cipher = {
2277                                 .enc = {
2278                                         .vecs = aes_enc_tv_template,
2279                                         .count = AES_ENC_TEST_VECTORS
2280                                 },
2281                                 .dec = {
2282                                         .vecs = aes_dec_tv_template,
2283                                         .count = AES_DEC_TEST_VECTORS
2284                                 }
2285                         }
2286                 }
2287         }, {
2288                 .alg = "ecb(anubis)",
2289                 .test = alg_test_skcipher,
2290                 .suite = {
2291                         .cipher = {
2292                                 .enc = {
2293                                         .vecs = anubis_enc_tv_template,
2294                                         .count = ANUBIS_ENC_TEST_VECTORS
2295                                 },
2296                                 .dec = {
2297                                         .vecs = anubis_dec_tv_template,
2298                                         .count = ANUBIS_DEC_TEST_VECTORS
2299                                 }
2300                         }
2301                 }
2302         }, {
2303                 .alg = "ecb(arc4)",
2304                 .test = alg_test_skcipher,
2305                 .suite = {
2306                         .cipher = {
2307                                 .enc = {
2308                                         .vecs = arc4_enc_tv_template,
2309                                         .count = ARC4_ENC_TEST_VECTORS
2310                                 },
2311                                 .dec = {
2312                                         .vecs = arc4_dec_tv_template,
2313                                         .count = ARC4_DEC_TEST_VECTORS
2314                                 }
2315                         }
2316                 }
2317         }, {
2318                 .alg = "ecb(blowfish)",
2319                 .test = alg_test_skcipher,
2320                 .suite = {
2321                         .cipher = {
2322                                 .enc = {
2323                                         .vecs = bf_enc_tv_template,
2324                                         .count = BF_ENC_TEST_VECTORS
2325                                 },
2326                                 .dec = {
2327                                         .vecs = bf_dec_tv_template,
2328                                         .count = BF_DEC_TEST_VECTORS
2329                                 }
2330                         }
2331                 }
2332         }, {
2333                 .alg = "ecb(camellia)",
2334                 .test = alg_test_skcipher,
2335                 .suite = {
2336                         .cipher = {
2337                                 .enc = {
2338                                         .vecs = camellia_enc_tv_template,
2339                                         .count = CAMELLIA_ENC_TEST_VECTORS
2340                                 },
2341                                 .dec = {
2342                                         .vecs = camellia_dec_tv_template,
2343                                         .count = CAMELLIA_DEC_TEST_VECTORS
2344                                 }
2345                         }
2346                 }
2347         }, {
2348                 .alg = "ecb(cast5)",
2349                 .test = alg_test_skcipher,
2350                 .suite = {
2351                         .cipher = {
2352                                 .enc = {
2353                                         .vecs = cast5_enc_tv_template,
2354                                         .count = CAST5_ENC_TEST_VECTORS
2355                                 },
2356                                 .dec = {
2357                                         .vecs = cast5_dec_tv_template,
2358                                         .count = CAST5_DEC_TEST_VECTORS
2359                                 }
2360                         }
2361                 }
2362         }, {
2363                 .alg = "ecb(cast6)",
2364                 .test = alg_test_skcipher,
2365                 .suite = {
2366                         .cipher = {
2367                                 .enc = {
2368                                         .vecs = cast6_enc_tv_template,
2369                                         .count = CAST6_ENC_TEST_VECTORS
2370                                 },
2371                                 .dec = {
2372                                         .vecs = cast6_dec_tv_template,
2373                                         .count = CAST6_DEC_TEST_VECTORS
2374                                 }
2375                         }
2376                 }
2377         }, {
2378                 .alg = "ecb(cipher_null)",
2379                 .test = alg_test_null,
2380         }, {
2381                 .alg = "ecb(des)",
2382                 .test = alg_test_skcipher,
2383                 .fips_allowed = 1,
2384                 .suite = {
2385                         .cipher = {
2386                                 .enc = {
2387                                         .vecs = des_enc_tv_template,
2388                                         .count = DES_ENC_TEST_VECTORS
2389                                 },
2390                                 .dec = {
2391                                         .vecs = des_dec_tv_template,
2392                                         .count = DES_DEC_TEST_VECTORS
2393                                 }
2394                         }
2395                 }
2396         }, {
2397                 .alg = "ecb(des3_ede)",
2398                 .test = alg_test_skcipher,
2399                 .fips_allowed = 1,
2400                 .suite = {
2401                         .cipher = {
2402                                 .enc = {
2403                                         .vecs = des3_ede_enc_tv_template,
2404                                         .count = DES3_EDE_ENC_TEST_VECTORS
2405                                 },
2406                                 .dec = {
2407                                         .vecs = des3_ede_dec_tv_template,
2408                                         .count = DES3_EDE_DEC_TEST_VECTORS
2409                                 }
2410                         }
2411                 }
2412         }, {
2413                 .alg = "ecb(fcrypt)",
2414                 .test = alg_test_skcipher,
2415                 .suite = {
2416                         .cipher = {
2417                                 .enc = {
2418                                         .vecs = fcrypt_pcbc_enc_tv_template,
2419                                         .count = 1
2420                                 },
2421                                 .dec = {
2422                                         .vecs = fcrypt_pcbc_dec_tv_template,
2423                                         .count = 1
2424                                 }
2425                         }
2426                 }
2427         }, {
2428                 .alg = "ecb(khazad)",
2429                 .test = alg_test_skcipher,
2430                 .suite = {
2431                         .cipher = {
2432                                 .enc = {
2433                                         .vecs = khazad_enc_tv_template,
2434                                         .count = KHAZAD_ENC_TEST_VECTORS
2435                                 },
2436                                 .dec = {
2437                                         .vecs = khazad_dec_tv_template,
2438                                         .count = KHAZAD_DEC_TEST_VECTORS
2439                                 }
2440                         }
2441                 }
2442         }, {
2443                 .alg = "ecb(seed)",
2444                 .test = alg_test_skcipher,
2445                 .suite = {
2446                         .cipher = {
2447                                 .enc = {
2448                                         .vecs = seed_enc_tv_template,
2449                                         .count = SEED_ENC_TEST_VECTORS
2450                                 },
2451                                 .dec = {
2452                                         .vecs = seed_dec_tv_template,
2453                                         .count = SEED_DEC_TEST_VECTORS
2454                                 }
2455                         }
2456                 }
2457         }, {
2458                 .alg = "ecb(serpent)",
2459                 .test = alg_test_skcipher,
2460                 .suite = {
2461                         .cipher = {
2462                                 .enc = {
2463                                         .vecs = serpent_enc_tv_template,
2464                                         .count = SERPENT_ENC_TEST_VECTORS
2465                                 },
2466                                 .dec = {
2467                                         .vecs = serpent_dec_tv_template,
2468                                         .count = SERPENT_DEC_TEST_VECTORS
2469                                 }
2470                         }
2471                 }
2472         }, {
2473                 .alg = "ecb(tea)",
2474                 .test = alg_test_skcipher,
2475                 .suite = {
2476                         .cipher = {
2477                                 .enc = {
2478                                         .vecs = tea_enc_tv_template,
2479                                         .count = TEA_ENC_TEST_VECTORS
2480                                 },
2481                                 .dec = {
2482                                         .vecs = tea_dec_tv_template,
2483                                         .count = TEA_DEC_TEST_VECTORS
2484                                 }
2485                         }
2486                 }
2487         }, {
2488                 .alg = "ecb(tnepres)",
2489                 .test = alg_test_skcipher,
2490                 .suite = {
2491                         .cipher = {
2492                                 .enc = {
2493                                         .vecs = tnepres_enc_tv_template,
2494                                         .count = TNEPRES_ENC_TEST_VECTORS
2495                                 },
2496                                 .dec = {
2497                                         .vecs = tnepres_dec_tv_template,
2498                                         .count = TNEPRES_DEC_TEST_VECTORS
2499                                 }
2500                         }
2501                 }
2502         }, {
2503                 .alg = "ecb(twofish)",
2504                 .test = alg_test_skcipher,
2505                 .suite = {
2506                         .cipher = {
2507                                 .enc = {
2508                                         .vecs = tf_enc_tv_template,
2509                                         .count = TF_ENC_TEST_VECTORS
2510                                 },
2511                                 .dec = {
2512                                         .vecs = tf_dec_tv_template,
2513                                         .count = TF_DEC_TEST_VECTORS
2514                                 }
2515                         }
2516                 }
2517         }, {
2518                 .alg = "ecb(xeta)",
2519                 .test = alg_test_skcipher,
2520                 .suite = {
2521                         .cipher = {
2522                                 .enc = {
2523                                         .vecs = xeta_enc_tv_template,
2524                                         .count = XETA_ENC_TEST_VECTORS
2525                                 },
2526                                 .dec = {
2527                                         .vecs = xeta_dec_tv_template,
2528                                         .count = XETA_DEC_TEST_VECTORS
2529                                 }
2530                         }
2531                 }
2532         }, {
2533                 .alg = "ecb(xtea)",
2534                 .test = alg_test_skcipher,
2535                 .suite = {
2536                         .cipher = {
2537                                 .enc = {
2538                                         .vecs = xtea_enc_tv_template,
2539                                         .count = XTEA_ENC_TEST_VECTORS
2540                                 },
2541                                 .dec = {
2542                                         .vecs = xtea_dec_tv_template,
2543                                         .count = XTEA_DEC_TEST_VECTORS
2544                                 }
2545                         }
2546                 }
2547         }, {
2548                 .alg = "gcm(aes)",
2549                 .test = alg_test_aead,
2550                 .fips_allowed = 1,
2551                 .suite = {
2552                         .aead = {
2553                                 .enc = {
2554                                         .vecs = aes_gcm_enc_tv_template,
2555                                         .count = AES_GCM_ENC_TEST_VECTORS
2556                                 },
2557                                 .dec = {
2558                                         .vecs = aes_gcm_dec_tv_template,
2559                                         .count = AES_GCM_DEC_TEST_VECTORS
2560                                 }
2561                         }
2562                 }
2563         }, {
2564                 .alg = "ghash",
2565                 .test = alg_test_hash,
2566                 .fips_allowed = 1,
2567                 .suite = {
2568                         .hash = {
2569                                 .vecs = ghash_tv_template,
2570                                 .count = GHASH_TEST_VECTORS
2571                         }
2572                 }
2573         }, {
2574                 .alg = "hmac(crc32)",
2575                 .test = alg_test_hash,
2576                 .suite = {
2577                         .hash = {
2578                                 .vecs = bfin_crc_tv_template,
2579                                 .count = BFIN_CRC_TEST_VECTORS
2580                         }
2581                 }
2582         }, {
2583                 .alg = "hmac(md5)",
2584                 .test = alg_test_hash,
2585                 .suite = {
2586                         .hash = {
2587                                 .vecs = hmac_md5_tv_template,
2588                                 .count = HMAC_MD5_TEST_VECTORS
2589                         }
2590                 }
2591         }, {
2592                 .alg = "hmac(rmd128)",
2593                 .test = alg_test_hash,
2594                 .suite = {
2595                         .hash = {
2596                                 .vecs = hmac_rmd128_tv_template,
2597                                 .count = HMAC_RMD128_TEST_VECTORS
2598                         }
2599                 }
2600         }, {
2601                 .alg = "hmac(rmd160)",
2602                 .test = alg_test_hash,
2603                 .suite = {
2604                         .hash = {
2605                                 .vecs = hmac_rmd160_tv_template,
2606                                 .count = HMAC_RMD160_TEST_VECTORS
2607                         }
2608                 }
2609         }, {
2610                 .alg = "hmac(sha1)",
2611                 .test = alg_test_hash,
2612                 .fips_allowed = 1,
2613                 .suite = {
2614                         .hash = {
2615                                 .vecs = hmac_sha1_tv_template,
2616                                 .count = HMAC_SHA1_TEST_VECTORS
2617                         }
2618                 }
2619         }, {
2620                 .alg = "hmac(sha224)",
2621                 .test = alg_test_hash,
2622                 .fips_allowed = 1,
2623                 .suite = {
2624                         .hash = {
2625                                 .vecs = hmac_sha224_tv_template,
2626                                 .count = HMAC_SHA224_TEST_VECTORS
2627                         }
2628                 }
2629         }, {
2630                 .alg = "hmac(sha256)",
2631                 .test = alg_test_hash,
2632                 .fips_allowed = 1,
2633                 .suite = {
2634                         .hash = {
2635                                 .vecs = hmac_sha256_tv_template,
2636                                 .count = HMAC_SHA256_TEST_VECTORS
2637                         }
2638                 }
2639         }, {
2640                 .alg = "hmac(sha384)",
2641                 .test = alg_test_hash,
2642                 .fips_allowed = 1,
2643                 .suite = {
2644                         .hash = {
2645                                 .vecs = hmac_sha384_tv_template,
2646                                 .count = HMAC_SHA384_TEST_VECTORS
2647                         }
2648                 }
2649         }, {
2650                 .alg = "hmac(sha512)",
2651                 .test = alg_test_hash,
2652                 .fips_allowed = 1,
2653                 .suite = {
2654                         .hash = {
2655                                 .vecs = hmac_sha512_tv_template,
2656                                 .count = HMAC_SHA512_TEST_VECTORS
2657                         }
2658                 }
2659         }, {
2660                 .alg = "lrw(aes)",
2661                 .test = alg_test_skcipher,
2662                 .suite = {
2663                         .cipher = {
2664                                 .enc = {
2665                                         .vecs = aes_lrw_enc_tv_template,
2666                                         .count = AES_LRW_ENC_TEST_VECTORS
2667                                 },
2668                                 .dec = {
2669                                         .vecs = aes_lrw_dec_tv_template,
2670                                         .count = AES_LRW_DEC_TEST_VECTORS
2671                                 }
2672                         }
2673                 }
2674         }, {
2675                 .alg = "lrw(camellia)",
2676                 .test = alg_test_skcipher,
2677                 .suite = {
2678                         .cipher = {
2679                                 .enc = {
2680                                         .vecs = camellia_lrw_enc_tv_template,
2681                                         .count = CAMELLIA_LRW_ENC_TEST_VECTORS
2682                                 },
2683                                 .dec = {
2684                                         .vecs = camellia_lrw_dec_tv_template,
2685                                         .count = CAMELLIA_LRW_DEC_TEST_VECTORS
2686                                 }
2687                         }
2688                 }
2689         }, {
2690                 .alg = "lrw(cast6)",
2691                 .test = alg_test_skcipher,
2692                 .suite = {
2693                         .cipher = {
2694                                 .enc = {
2695                                         .vecs = cast6_lrw_enc_tv_template,
2696                                         .count = CAST6_LRW_ENC_TEST_VECTORS
2697                                 },
2698                                 .dec = {
2699                                         .vecs = cast6_lrw_dec_tv_template,
2700                                         .count = CAST6_LRW_DEC_TEST_VECTORS
2701                                 }
2702                         }
2703                 }
2704         }, {
2705                 .alg = "lrw(serpent)",
2706                 .test = alg_test_skcipher,
2707                 .suite = {
2708                         .cipher = {
2709                                 .enc = {
2710                                         .vecs = serpent_lrw_enc_tv_template,
2711                                         .count = SERPENT_LRW_ENC_TEST_VECTORS
2712                                 },
2713                                 .dec = {
2714                                         .vecs = serpent_lrw_dec_tv_template,
2715                                         .count = SERPENT_LRW_DEC_TEST_VECTORS
2716                                 }
2717                         }
2718                 }
2719         }, {
2720                 .alg = "lrw(twofish)",
2721                 .test = alg_test_skcipher,
2722                 .suite = {
2723                         .cipher = {
2724                                 .enc = {
2725                                         .vecs = tf_lrw_enc_tv_template,
2726                                         .count = TF_LRW_ENC_TEST_VECTORS
2727                                 },
2728                                 .dec = {
2729                                         .vecs = tf_lrw_dec_tv_template,
2730                                         .count = TF_LRW_DEC_TEST_VECTORS
2731                                 }
2732                         }
2733                 }
2734         }, {
2735                 .alg = "lzo",
2736                 .test = alg_test_comp,
2737                 .fips_allowed = 1,
2738                 .suite = {
2739                         .comp = {
2740                                 .comp = {
2741                                         .vecs = lzo_comp_tv_template,
2742                                         .count = LZO_COMP_TEST_VECTORS
2743                                 },
2744                                 .decomp = {
2745                                         .vecs = lzo_decomp_tv_template,
2746                                         .count = LZO_DECOMP_TEST_VECTORS
2747                                 }
2748                         }
2749                 }
2750         }, {
2751                 .alg = "md4",
2752                 .test = alg_test_hash,
2753                 .suite = {
2754                         .hash = {
2755                                 .vecs = md4_tv_template,
2756                                 .count = MD4_TEST_VECTORS
2757                         }
2758                 }
2759         }, {
2760                 .alg = "md5",
2761                 .test = alg_test_hash,
2762                 .suite = {
2763                         .hash = {
2764                                 .vecs = md5_tv_template,
2765                                 .count = MD5_TEST_VECTORS
2766                         }
2767                 }
2768         }, {
2769                 .alg = "michael_mic",
2770                 .test = alg_test_hash,
2771                 .suite = {
2772                         .hash = {
2773                                 .vecs = michael_mic_tv_template,
2774                                 .count = MICHAEL_MIC_TEST_VECTORS
2775                         }
2776                 }
2777         }, {
2778                 .alg = "ofb(aes)",
2779                 .test = alg_test_skcipher,
2780                 .fips_allowed = 1,
2781                 .suite = {
2782                         .cipher = {
2783                                 .enc = {
2784                                         .vecs = aes_ofb_enc_tv_template,
2785                                         .count = AES_OFB_ENC_TEST_VECTORS
2786                                 },
2787                                 .dec = {
2788                                         .vecs = aes_ofb_dec_tv_template,
2789                                         .count = AES_OFB_DEC_TEST_VECTORS
2790                                 }
2791                         }
2792                 }
2793         }, {
2794                 .alg = "pcbc(fcrypt)",
2795                 .test = alg_test_skcipher,
2796                 .suite = {
2797                         .cipher = {
2798                                 .enc = {
2799                                         .vecs = fcrypt_pcbc_enc_tv_template,
2800                                         .count = FCRYPT_ENC_TEST_VECTORS
2801                                 },
2802                                 .dec = {
2803                                         .vecs = fcrypt_pcbc_dec_tv_template,
2804                                         .count = FCRYPT_DEC_TEST_VECTORS
2805                                 }
2806                         }
2807                 }
2808         }, {
2809                 .alg = "rfc3686(ctr(aes))",
2810                 .test = alg_test_skcipher,
2811                 .fips_allowed = 1,
2812                 .suite = {
2813                         .cipher = {
2814                                 .enc = {
2815                                         .vecs = aes_ctr_rfc3686_enc_tv_template,
2816                                         .count = AES_CTR_3686_ENC_TEST_VECTORS
2817                                 },
2818                                 .dec = {
2819                                         .vecs = aes_ctr_rfc3686_dec_tv_template,
2820                                         .count = AES_CTR_3686_DEC_TEST_VECTORS
2821                                 }
2822                         }
2823                 }
2824         }, {
2825                 .alg = "rfc4106(gcm(aes))",
2826                 .test = alg_test_aead,
2827                 .suite = {
2828                         .aead = {
2829                                 .enc = {
2830                                         .vecs = aes_gcm_rfc4106_enc_tv_template,
2831                                         .count = AES_GCM_4106_ENC_TEST_VECTORS
2832                                 },
2833                                 .dec = {
2834                                         .vecs = aes_gcm_rfc4106_dec_tv_template,
2835                                         .count = AES_GCM_4106_DEC_TEST_VECTORS
2836                                 }
2837                         }
2838                 }
2839         }, {
2840                 .alg = "rfc4309(ccm(aes))",
2841                 .test = alg_test_aead,
2842                 .fips_allowed = 1,
2843                 .suite = {
2844                         .aead = {
2845                                 .enc = {
2846                                         .vecs = aes_ccm_rfc4309_enc_tv_template,
2847                                         .count = AES_CCM_4309_ENC_TEST_VECTORS
2848                                 },
2849                                 .dec = {
2850                                         .vecs = aes_ccm_rfc4309_dec_tv_template,
2851                                         .count = AES_CCM_4309_DEC_TEST_VECTORS
2852                                 }
2853                         }
2854                 }
2855         }, {
2856                 .alg = "rfc4543(gcm(aes))",
2857                 .test = alg_test_aead,
2858                 .suite = {
2859                         .aead = {
2860                                 .enc = {
2861                                         .vecs = aes_gcm_rfc4543_enc_tv_template,
2862                                         .count = AES_GCM_4543_ENC_TEST_VECTORS
2863                                 },
2864                                 .dec = {
2865                                         .vecs = aes_gcm_rfc4543_dec_tv_template,
2866                                         .count = AES_GCM_4543_DEC_TEST_VECTORS
2867                                 },
2868                         }
2869                 }
2870         }, {
2871                 .alg = "rmd128",
2872                 .test = alg_test_hash,
2873                 .suite = {
2874                         .hash = {
2875                                 .vecs = rmd128_tv_template,
2876                                 .count = RMD128_TEST_VECTORS
2877                         }
2878                 }
2879         }, {
2880                 .alg = "rmd160",
2881                 .test = alg_test_hash,
2882                 .suite = {
2883                         .hash = {
2884                                 .vecs = rmd160_tv_template,
2885                                 .count = RMD160_TEST_VECTORS
2886                         }
2887                 }
2888         }, {
2889                 .alg = "rmd256",
2890                 .test = alg_test_hash,
2891                 .suite = {
2892                         .hash = {
2893                                 .vecs = rmd256_tv_template,
2894                                 .count = RMD256_TEST_VECTORS
2895                         }
2896                 }
2897         }, {
2898                 .alg = "rmd320",
2899                 .test = alg_test_hash,
2900                 .suite = {
2901                         .hash = {
2902                                 .vecs = rmd320_tv_template,
2903                                 .count = RMD320_TEST_VECTORS
2904                         }
2905                 }
2906         }, {
2907                 .alg = "salsa20",
2908                 .test = alg_test_skcipher,
2909                 .suite = {
2910                         .cipher = {
2911                                 .enc = {
2912                                         .vecs = salsa20_stream_enc_tv_template,
2913                                         .count = SALSA20_STREAM_ENC_TEST_VECTORS
2914                                 }
2915                         }
2916                 }
2917         }, {
2918                 .alg = "sha1",
2919                 .test = alg_test_hash,
2920                 .fips_allowed = 1,
2921                 .suite = {
2922                         .hash = {
2923                                 .vecs = sha1_tv_template,
2924                                 .count = SHA1_TEST_VECTORS
2925                         }
2926                 }
2927         }, {
2928                 .alg = "sha224",
2929                 .test = alg_test_hash,
2930                 .fips_allowed = 1,
2931                 .suite = {
2932                         .hash = {
2933                                 .vecs = sha224_tv_template,
2934                                 .count = SHA224_TEST_VECTORS
2935                         }
2936                 }
2937         }, {
2938                 .alg = "sha256",
2939                 .test = alg_test_hash,
2940                 .fips_allowed = 1,
2941                 .suite = {
2942                         .hash = {
2943                                 .vecs = sha256_tv_template,
2944                                 .count = SHA256_TEST_VECTORS
2945                         }
2946                 }
2947         }, {
2948                 .alg = "sha384",
2949                 .test = alg_test_hash,
2950                 .fips_allowed = 1,
2951                 .suite = {
2952                         .hash = {
2953                                 .vecs = sha384_tv_template,
2954                                 .count = SHA384_TEST_VECTORS
2955                         }
2956                 }
2957         }, {
2958                 .alg = "sha512",
2959                 .test = alg_test_hash,
2960                 .fips_allowed = 1,
2961                 .suite = {
2962                         .hash = {
2963                                 .vecs = sha512_tv_template,
2964                                 .count = SHA512_TEST_VECTORS
2965                         }
2966                 }
2967         }, {
2968                 .alg = "tgr128",
2969                 .test = alg_test_hash,
2970                 .suite = {
2971                         .hash = {
2972                                 .vecs = tgr128_tv_template,
2973                                 .count = TGR128_TEST_VECTORS
2974                         }
2975                 }
2976         }, {
2977                 .alg = "tgr160",
2978                 .test = alg_test_hash,
2979                 .suite = {
2980                         .hash = {
2981                                 .vecs = tgr160_tv_template,
2982                                 .count = TGR160_TEST_VECTORS
2983                         }
2984                 }
2985         }, {
2986                 .alg = "tgr192",
2987                 .test = alg_test_hash,
2988                 .suite = {
2989                         .hash = {
2990                                 .vecs = tgr192_tv_template,
2991                                 .count = TGR192_TEST_VECTORS
2992                         }
2993                 }
2994         }, {
2995                 .alg = "vmac(aes)",
2996                 .test = alg_test_hash,
2997                 .suite = {
2998                         .hash = {
2999                                 .vecs = aes_vmac128_tv_template,
3000                                 .count = VMAC_AES_TEST_VECTORS
3001                         }
3002                 }
3003         }, {
3004                 .alg = "wp256",
3005                 .test = alg_test_hash,
3006                 .suite = {
3007                         .hash = {
3008                                 .vecs = wp256_tv_template,
3009                                 .count = WP256_TEST_VECTORS
3010                         }
3011                 }
3012         }, {
3013                 .alg = "wp384",
3014                 .test = alg_test_hash,
3015                 .suite = {
3016                         .hash = {
3017                                 .vecs = wp384_tv_template,
3018                                 .count = WP384_TEST_VECTORS
3019                         }
3020                 }
3021         }, {
3022                 .alg = "wp512",
3023                 .test = alg_test_hash,
3024                 .suite = {
3025                         .hash = {
3026                                 .vecs = wp512_tv_template,
3027                                 .count = WP512_TEST_VECTORS
3028                         }
3029                 }
3030         }, {
3031                 .alg = "xcbc(aes)",
3032                 .test = alg_test_hash,
3033                 .suite = {
3034                         .hash = {
3035                                 .vecs = aes_xcbc128_tv_template,
3036                                 .count = XCBC_AES_TEST_VECTORS
3037                         }
3038                 }
3039         }, {
3040                 .alg = "xts(aes)",
3041                 .test = alg_test_skcipher,
3042                 .fips_allowed = 1,
3043                 .suite = {
3044                         .cipher = {
3045                                 .enc = {
3046                                         .vecs = aes_xts_enc_tv_template,
3047                                         .count = AES_XTS_ENC_TEST_VECTORS
3048                                 },
3049                                 .dec = {
3050                                         .vecs = aes_xts_dec_tv_template,
3051                                         .count = AES_XTS_DEC_TEST_VECTORS
3052                                 }
3053                         }
3054                 }
3055         }, {
3056                 .alg = "xts(camellia)",
3057                 .test = alg_test_skcipher,
3058                 .suite = {
3059                         .cipher = {
3060                                 .enc = {
3061                                         .vecs = camellia_xts_enc_tv_template,
3062                                         .count = CAMELLIA_XTS_ENC_TEST_VECTORS
3063                                 },
3064                                 .dec = {
3065                                         .vecs = camellia_xts_dec_tv_template,
3066                                         .count = CAMELLIA_XTS_DEC_TEST_VECTORS
3067                                 }
3068                         }
3069                 }
3070         }, {
3071                 .alg = "xts(cast6)",
3072                 .test = alg_test_skcipher,
3073                 .suite = {
3074                         .cipher = {
3075                                 .enc = {
3076                                         .vecs = cast6_xts_enc_tv_template,
3077                                         .count = CAST6_XTS_ENC_TEST_VECTORS
3078                                 },
3079                                 .dec = {
3080                                         .vecs = cast6_xts_dec_tv_template,
3081                                         .count = CAST6_XTS_DEC_TEST_VECTORS
3082                                 }
3083                         }
3084                 }
3085         }, {
3086                 .alg = "xts(serpent)",
3087                 .test = alg_test_skcipher,
3088                 .suite = {
3089                         .cipher = {
3090                                 .enc = {
3091                                         .vecs = serpent_xts_enc_tv_template,
3092                                         .count = SERPENT_XTS_ENC_TEST_VECTORS
3093                                 },
3094                                 .dec = {
3095                                         .vecs = serpent_xts_dec_tv_template,
3096                                         .count = SERPENT_XTS_DEC_TEST_VECTORS
3097                                 }
3098                         }
3099                 }
3100         }, {
3101                 .alg = "xts(twofish)",
3102                 .test = alg_test_skcipher,
3103                 .suite = {
3104                         .cipher = {
3105                                 .enc = {
3106                                         .vecs = tf_xts_enc_tv_template,
3107                                         .count = TF_XTS_ENC_TEST_VECTORS
3108                                 },
3109                                 .dec = {
3110                                         .vecs = tf_xts_dec_tv_template,
3111                                         .count = TF_XTS_DEC_TEST_VECTORS
3112                                 }
3113                         }
3114                 }
3115         }, {
3116                 .alg = "zlib",
3117                 .test = alg_test_pcomp,
3118                 .fips_allowed = 1,
3119                 .suite = {
3120                         .pcomp = {
3121                                 .comp = {
3122                                         .vecs = zlib_comp_tv_template,
3123                                         .count = ZLIB_COMP_TEST_VECTORS
3124                                 },
3125                                 .decomp = {
3126                                         .vecs = zlib_decomp_tv_template,
3127                                         .count = ZLIB_DECOMP_TEST_VECTORS
3128                                 }
3129                         }
3130                 }
3131         }
3132 };
3133
3134 static bool alg_test_descs_checked;
3135
3136 static void alg_test_descs_check_order(void)
3137 {
3138         int i;
3139
3140         /* only check once */
3141         if (alg_test_descs_checked)
3142                 return;
3143
3144         alg_test_descs_checked = true;
3145
3146         for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
3147                 int diff = strcmp(alg_test_descs[i - 1].alg,
3148                                   alg_test_descs[i].alg);
3149
3150                 if (WARN_ON(diff > 0)) {
3151                         pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
3152                                 alg_test_descs[i - 1].alg,
3153                                 alg_test_descs[i].alg);
3154                 }
3155
3156                 if (WARN_ON(diff == 0)) {
3157                         pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
3158                                 alg_test_descs[i].alg);
3159                 }
3160         }
3161 }
3162
3163 static int alg_find_test(const char *alg)
3164 {
3165         int start = 0;
3166         int end = ARRAY_SIZE(alg_test_descs);
3167
3168         while (start < end) {
3169                 int i = (start + end) / 2;
3170                 int diff = strcmp(alg_test_descs[i].alg, alg);
3171
3172                 if (diff > 0) {
3173                         end = i;
3174                         continue;
3175                 }
3176
3177                 if (diff < 0) {
3178                         start = i + 1;
3179                         continue;
3180                 }
3181
3182                 return i;
3183         }
3184
3185         return -1;
3186 }
3187
3188 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
3189 {
3190         int i;
3191         int j;
3192         int rc;
3193
3194         alg_test_descs_check_order();
3195
3196         if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
3197                 char nalg[CRYPTO_MAX_ALG_NAME];
3198
3199                 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
3200                     sizeof(nalg))
3201                         return -ENAMETOOLONG;
3202
3203                 i = alg_find_test(nalg);
3204                 if (i < 0)
3205                         goto notest;
3206
3207                 if (fips_enabled && !alg_test_descs[i].fips_allowed)
3208                         goto non_fips_alg;
3209
3210                 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
3211                 goto test_done;
3212         }
3213
3214         i = alg_find_test(alg);
3215         j = alg_find_test(driver);
3216         if (i < 0 && j < 0)
3217                 goto notest;
3218
3219         if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
3220                              (j >= 0 && !alg_test_descs[j].fips_allowed)))
3221                 goto non_fips_alg;
3222
3223         rc = 0;
3224         if (i >= 0)
3225                 rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
3226                                              type, mask);
3227         if (j >= 0)
3228                 rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
3229                                              type, mask);
3230
3231 test_done:
3232         if (fips_enabled && rc)
3233                 panic("%s: %s alg self test failed in fips mode!\n", driver, alg);
3234
3235         if (fips_enabled && !rc)
3236                 printk(KERN_INFO "alg: self-tests for %s (%s) passed\n",
3237                        driver, alg);
3238
3239         return rc;
3240
3241 notest:
3242         printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
3243         return 0;
3244 non_fips_alg:
3245         return -EINVAL;
3246 }
3247
3248 #endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
3249
3250 EXPORT_SYMBOL_GPL(alg_test);