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