c5550943411d4e45594fea73af6439de820a58f7
[linux-2.6.git] / crypto / testmgr.c
1 /*
2  * Algorithm testing framework and tests.
3  *
4  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5  * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
6  * Copyright (c) 2007 Nokia Siemens Networks
7  * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the Free
11  * Software Foundation; either version 2 of the License, or (at your option)
12  * any later version.
13  *
14  */
15
16 #include <crypto/hash.h>
17 #include <linux/err.h>
18 #include <linux/module.h>
19 #include <linux/scatterlist.h>
20 #include <linux/slab.h>
21 #include <linux/string.h>
22 #include <crypto/rng.h>
23
24 #include "internal.h"
25 #include "testmgr.h"
26
27 /*
28  * Need slab memory for testing (size in number of pages).
29  */
30 #define XBUFSIZE        8
31
32 /*
33  * Indexes into the xbuf to simulate cross-page access.
34  */
35 #define IDX1            32
36 #define IDX2            32400
37 #define IDX3            1
38 #define IDX4            8193
39 #define IDX5            22222
40 #define IDX6            17101
41 #define IDX7            27333
42 #define IDX8            3000
43
44 /*
45 * Used by test_cipher()
46 */
47 #define ENCRYPT 1
48 #define DECRYPT 0
49
50 struct tcrypt_result {
51         struct completion completion;
52         int err;
53 };
54
55 struct aead_test_suite {
56         struct {
57                 struct aead_testvec *vecs;
58                 unsigned int count;
59         } enc, dec;
60 };
61
62 struct cipher_test_suite {
63         struct {
64                 struct cipher_testvec *vecs;
65                 unsigned int count;
66         } enc, dec;
67 };
68
69 struct comp_test_suite {
70         struct {
71                 struct comp_testvec *vecs;
72                 unsigned int count;
73         } comp, decomp;
74 };
75
76 struct pcomp_test_suite {
77         struct {
78                 struct pcomp_testvec *vecs;
79                 unsigned int count;
80         } comp, decomp;
81 };
82
83 struct hash_test_suite {
84         struct hash_testvec *vecs;
85         unsigned int count;
86 };
87
88 struct cprng_test_suite {
89         struct cprng_testvec *vecs;
90         unsigned int count;
91 };
92
93 struct alg_test_desc {
94         const char *alg;
95         int (*test)(const struct alg_test_desc *desc, const char *driver,
96                     u32 type, u32 mask);
97
98         union {
99                 struct aead_test_suite aead;
100                 struct cipher_test_suite cipher;
101                 struct comp_test_suite comp;
102                 struct pcomp_test_suite pcomp;
103                 struct hash_test_suite hash;
104                 struct cprng_test_suite cprng;
105         } suite;
106 };
107
108 static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
109
110 static void hexdump(unsigned char *buf, unsigned int len)
111 {
112         print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
113                         16, 1,
114                         buf, len, false);
115 }
116
117 static void tcrypt_complete(struct crypto_async_request *req, int err)
118 {
119         struct tcrypt_result *res = req->data;
120
121         if (err == -EINPROGRESS)
122                 return;
123
124         res->err = err;
125         complete(&res->completion);
126 }
127
128 static int testmgr_alloc_buf(char *buf[XBUFSIZE])
129 {
130         int i;
131
132         for (i = 0; i < XBUFSIZE; i++) {
133                 buf[i] = (void *)__get_free_page(GFP_KERNEL);
134                 if (!buf[i])
135                         goto err_free_buf;
136         }
137
138         return 0;
139
140 err_free_buf:
141         while (i-- > 0)
142                 free_page((unsigned long)buf[i]);
143
144         return -ENOMEM;
145 }
146
147 static void testmgr_free_buf(char *buf[XBUFSIZE])
148 {
149         int i;
150
151         for (i = 0; i < XBUFSIZE; i++)
152                 free_page((unsigned long)buf[i]);
153 }
154
155 static int test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
156                      unsigned int tcount)
157 {
158         const char *algo = crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
159         unsigned int i, j, k, temp;
160         struct scatterlist sg[8];
161         char result[64];
162         struct ahash_request *req;
163         struct tcrypt_result tresult;
164         void *hash_buff;
165         char *xbuf[XBUFSIZE];
166         int ret = -ENOMEM;
167
168         if (testmgr_alloc_buf(xbuf))
169                 goto out_nobuf;
170
171         init_completion(&tresult.completion);
172
173         req = ahash_request_alloc(tfm, GFP_KERNEL);
174         if (!req) {
175                 printk(KERN_ERR "alg: hash: Failed to allocate request for "
176                        "%s\n", algo);
177                 goto out_noreq;
178         }
179         ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
180                                    tcrypt_complete, &tresult);
181
182         for (i = 0; i < tcount; i++) {
183                 memset(result, 0, 64);
184
185                 hash_buff = xbuf[0];
186
187                 memcpy(hash_buff, template[i].plaintext, template[i].psize);
188                 sg_init_one(&sg[0], hash_buff, template[i].psize);
189
190                 if (template[i].ksize) {
191                         crypto_ahash_clear_flags(tfm, ~0);
192                         ret = crypto_ahash_setkey(tfm, template[i].key,
193                                                   template[i].ksize);
194                         if (ret) {
195                                 printk(KERN_ERR "alg: hash: setkey failed on "
196                                        "test %d for %s: ret=%d\n", i + 1, algo,
197                                        -ret);
198                                 goto out;
199                         }
200                 }
201
202                 ahash_request_set_crypt(req, sg, result, template[i].psize);
203                 ret = crypto_ahash_digest(req);
204                 switch (ret) {
205                 case 0:
206                         break;
207                 case -EINPROGRESS:
208                 case -EBUSY:
209                         ret = wait_for_completion_interruptible(
210                                 &tresult.completion);
211                         if (!ret && !(ret = tresult.err)) {
212                                 INIT_COMPLETION(tresult.completion);
213                                 break;
214                         }
215                         /* fall through */
216                 default:
217                         printk(KERN_ERR "alg: hash: digest failed on test %d "
218                                "for %s: ret=%d\n", i + 1, algo, -ret);
219                         goto out;
220                 }
221
222                 if (memcmp(result, template[i].digest,
223                            crypto_ahash_digestsize(tfm))) {
224                         printk(KERN_ERR "alg: hash: Test %d failed for %s\n",
225                                i + 1, algo);
226                         hexdump(result, crypto_ahash_digestsize(tfm));
227                         ret = -EINVAL;
228                         goto out;
229                 }
230         }
231
232         j = 0;
233         for (i = 0; i < tcount; i++) {
234                 if (template[i].np) {
235                         j++;
236                         memset(result, 0, 64);
237
238                         temp = 0;
239                         sg_init_table(sg, template[i].np);
240                         for (k = 0; k < template[i].np; k++) {
241                                 sg_set_buf(&sg[k],
242                                            memcpy(xbuf[IDX[k] >> PAGE_SHIFT] +
243                                                   offset_in_page(IDX[k]),
244                                                   template[i].plaintext + temp,
245                                                   template[i].tap[k]),
246                                            template[i].tap[k]);
247                                 temp += template[i].tap[k];
248                         }
249
250                         if (template[i].ksize) {
251                                 crypto_ahash_clear_flags(tfm, ~0);
252                                 ret = crypto_ahash_setkey(tfm, template[i].key,
253                                                           template[i].ksize);
254
255                                 if (ret) {
256                                         printk(KERN_ERR "alg: hash: setkey "
257                                                "failed on chunking test %d "
258                                                "for %s: ret=%d\n", j, algo,
259                                                -ret);
260                                         goto out;
261                                 }
262                         }
263
264                         ahash_request_set_crypt(req, sg, result,
265                                                 template[i].psize);
266                         ret = crypto_ahash_digest(req);
267                         switch (ret) {
268                         case 0:
269                                 break;
270                         case -EINPROGRESS:
271                         case -EBUSY:
272                                 ret = wait_for_completion_interruptible(
273                                         &tresult.completion);
274                                 if (!ret && !(ret = tresult.err)) {
275                                         INIT_COMPLETION(tresult.completion);
276                                         break;
277                                 }
278                                 /* fall through */
279                         default:
280                                 printk(KERN_ERR "alg: hash: digest failed "
281                                        "on chunking test %d for %s: "
282                                        "ret=%d\n", j, algo, -ret);
283                                 goto out;
284                         }
285
286                         if (memcmp(result, template[i].digest,
287                                    crypto_ahash_digestsize(tfm))) {
288                                 printk(KERN_ERR "alg: hash: Chunking test %d "
289                                        "failed for %s\n", j, algo);
290                                 hexdump(result, crypto_ahash_digestsize(tfm));
291                                 ret = -EINVAL;
292                                 goto out;
293                         }
294                 }
295         }
296
297         ret = 0;
298
299 out:
300         ahash_request_free(req);
301 out_noreq:
302         testmgr_free_buf(xbuf);
303 out_nobuf:
304         return ret;
305 }
306
307 static int test_aead(struct crypto_aead *tfm, int enc,
308                      struct aead_testvec *template, unsigned int tcount)
309 {
310         const char *algo = crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm));
311         unsigned int i, j, k, n, temp;
312         int ret = -ENOMEM;
313         char *q;
314         char *key;
315         struct aead_request *req;
316         struct scatterlist sg[8];
317         struct scatterlist asg[8];
318         const char *e;
319         struct tcrypt_result result;
320         unsigned int authsize;
321         void *input;
322         void *assoc;
323         char iv[MAX_IVLEN];
324         char *xbuf[XBUFSIZE];
325         char *axbuf[XBUFSIZE];
326
327         if (testmgr_alloc_buf(xbuf))
328                 goto out_noxbuf;
329         if (testmgr_alloc_buf(axbuf))
330                 goto out_noaxbuf;
331
332         if (enc == ENCRYPT)
333                 e = "encryption";
334         else
335                 e = "decryption";
336
337         init_completion(&result.completion);
338
339         req = aead_request_alloc(tfm, GFP_KERNEL);
340         if (!req) {
341                 printk(KERN_ERR "alg: aead: Failed to allocate request for "
342                        "%s\n", algo);
343                 goto out;
344         }
345
346         aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
347                                   tcrypt_complete, &result);
348
349         for (i = 0, j = 0; i < tcount; i++) {
350                 if (!template[i].np) {
351                         j++;
352
353                         /* some tepmplates have no input data but they will
354                          * touch input
355                          */
356                         input = xbuf[0];
357                         assoc = axbuf[0];
358
359                         memcpy(input, template[i].input, template[i].ilen);
360                         memcpy(assoc, template[i].assoc, template[i].alen);
361                         if (template[i].iv)
362                                 memcpy(iv, template[i].iv, MAX_IVLEN);
363                         else
364                                 memset(iv, 0, MAX_IVLEN);
365
366                         crypto_aead_clear_flags(tfm, ~0);
367                         if (template[i].wk)
368                                 crypto_aead_set_flags(
369                                         tfm, CRYPTO_TFM_REQ_WEAK_KEY);
370
371                         key = template[i].key;
372
373                         ret = crypto_aead_setkey(tfm, key,
374                                                  template[i].klen);
375                         if (!ret == template[i].fail) {
376                                 printk(KERN_ERR "alg: aead: setkey failed on "
377                                        "test %d for %s: flags=%x\n", j, algo,
378                                        crypto_aead_get_flags(tfm));
379                                 goto out;
380                         } else if (ret)
381                                 continue;
382
383                         authsize = abs(template[i].rlen - template[i].ilen);
384                         ret = crypto_aead_setauthsize(tfm, authsize);
385                         if (ret) {
386                                 printk(KERN_ERR "alg: aead: Failed to set "
387                                        "authsize to %u on test %d for %s\n",
388                                        authsize, j, algo);
389                                 goto out;
390                         }
391
392                         sg_init_one(&sg[0], input,
393                                     template[i].ilen + (enc ? authsize : 0));
394
395                         sg_init_one(&asg[0], assoc, template[i].alen);
396
397                         aead_request_set_crypt(req, sg, sg,
398                                                template[i].ilen, iv);
399
400                         aead_request_set_assoc(req, asg, template[i].alen);
401
402                         ret = enc ?
403                                 crypto_aead_encrypt(req) :
404                                 crypto_aead_decrypt(req);
405
406                         switch (ret) {
407                         case 0:
408                                 if (template[i].novrfy) {
409                                         /* verification was supposed to fail */
410                                         printk(KERN_ERR "alg: aead: %s failed "
411                                                "on test %d for %s: ret was 0, "
412                                                "expected -EBADMSG\n",
413                                                e, j, algo);
414                                         /* so really, we got a bad message */
415                                         ret = -EBADMSG;
416                                         goto out;
417                                 }
418                                 break;
419                         case -EINPROGRESS:
420                         case -EBUSY:
421                                 ret = wait_for_completion_interruptible(
422                                         &result.completion);
423                                 if (!ret && !(ret = result.err)) {
424                                         INIT_COMPLETION(result.completion);
425                                         break;
426                                 }
427                         case -EBADMSG:
428                                 if (template[i].novrfy)
429                                         /* verification failure was expected */
430                                         continue;
431                                 /* fall through */
432                         default:
433                                 printk(KERN_ERR "alg: aead: %s failed on test "
434                                        "%d for %s: ret=%d\n", e, j, algo, -ret);
435                                 goto out;
436                         }
437
438                         q = input;
439                         if (memcmp(q, template[i].result, template[i].rlen)) {
440                                 printk(KERN_ERR "alg: aead: Test %d failed on "
441                                        "%s for %s\n", j, e, algo);
442                                 hexdump(q, template[i].rlen);
443                                 ret = -EINVAL;
444                                 goto out;
445                         }
446                 }
447         }
448
449         for (i = 0, j = 0; i < tcount; i++) {
450                 if (template[i].np) {
451                         j++;
452
453                         if (template[i].iv)
454                                 memcpy(iv, template[i].iv, MAX_IVLEN);
455                         else
456                                 memset(iv, 0, MAX_IVLEN);
457
458                         crypto_aead_clear_flags(tfm, ~0);
459                         if (template[i].wk)
460                                 crypto_aead_set_flags(
461                                         tfm, CRYPTO_TFM_REQ_WEAK_KEY);
462                         key = template[i].key;
463
464                         ret = crypto_aead_setkey(tfm, key, template[i].klen);
465                         if (!ret == template[i].fail) {
466                                 printk(KERN_ERR "alg: aead: setkey failed on "
467                                        "chunk test %d for %s: flags=%x\n", j,
468                                        algo, crypto_aead_get_flags(tfm));
469                                 goto out;
470                         } else if (ret)
471                                 continue;
472
473                         authsize = abs(template[i].rlen - template[i].ilen);
474
475                         ret = -EINVAL;
476                         sg_init_table(sg, template[i].np);
477                         for (k = 0, temp = 0; k < template[i].np; k++) {
478                                 if (WARN_ON(offset_in_page(IDX[k]) +
479                                             template[i].tap[k] > PAGE_SIZE))
480                                         goto out;
481
482                                 q = xbuf[IDX[k] >> PAGE_SHIFT] +
483                                     offset_in_page(IDX[k]);
484
485                                 memcpy(q, template[i].input + temp,
486                                        template[i].tap[k]);
487
488                                 n = template[i].tap[k];
489                                 if (k == template[i].np - 1 && enc)
490                                         n += authsize;
491                                 if (offset_in_page(q) + n < PAGE_SIZE)
492                                         q[n] = 0;
493
494                                 sg_set_buf(&sg[k], q, template[i].tap[k]);
495                                 temp += template[i].tap[k];
496                         }
497
498                         ret = crypto_aead_setauthsize(tfm, authsize);
499                         if (ret) {
500                                 printk(KERN_ERR "alg: aead: Failed to set "
501                                        "authsize to %u on chunk test %d for "
502                                        "%s\n", authsize, j, algo);
503                                 goto out;
504                         }
505
506                         if (enc) {
507                                 if (WARN_ON(sg[k - 1].offset +
508                                             sg[k - 1].length + authsize >
509                                             PAGE_SIZE)) {
510                                         ret = -EINVAL;
511                                         goto out;
512                                 }
513
514                                 sg[k - 1].length += authsize;
515                         }
516
517                         sg_init_table(asg, template[i].anp);
518                         for (k = 0, temp = 0; k < template[i].anp; k++) {
519                                 sg_set_buf(&asg[k],
520                                            memcpy(axbuf[IDX[k] >> PAGE_SHIFT] +
521                                                   offset_in_page(IDX[k]),
522                                                   template[i].assoc + temp,
523                                                   template[i].atap[k]),
524                                            template[i].atap[k]);
525                                 temp += template[i].atap[k];
526                         }
527
528                         aead_request_set_crypt(req, sg, sg,
529                                                template[i].ilen,
530                                                iv);
531
532                         aead_request_set_assoc(req, asg, template[i].alen);
533
534                         ret = enc ?
535                                 crypto_aead_encrypt(req) :
536                                 crypto_aead_decrypt(req);
537
538                         switch (ret) {
539                         case 0:
540                                 if (template[i].novrfy) {
541                                         /* verification was supposed to fail */
542                                         printk(KERN_ERR "alg: aead: %s failed "
543                                                "on chunk test %d for %s: ret "
544                                                "was 0, expected -EBADMSG\n",
545                                                e, j, algo);
546                                         /* so really, we got a bad message */
547                                         ret = -EBADMSG;
548                                         goto out;
549                                 }
550                                 break;
551                         case -EINPROGRESS:
552                         case -EBUSY:
553                                 ret = wait_for_completion_interruptible(
554                                         &result.completion);
555                                 if (!ret && !(ret = result.err)) {
556                                         INIT_COMPLETION(result.completion);
557                                         break;
558                                 }
559                         case -EBADMSG:
560                                 if (template[i].novrfy)
561                                         /* verification failure was expected */
562                                         continue;
563                                 /* fall through */
564                         default:
565                                 printk(KERN_ERR "alg: aead: %s failed on "
566                                        "chunk test %d for %s: ret=%d\n", e, j,
567                                        algo, -ret);
568                                 goto out;
569                         }
570
571                         ret = -EINVAL;
572                         for (k = 0, temp = 0; k < template[i].np; k++) {
573                                 q = xbuf[IDX[k] >> PAGE_SHIFT] +
574                                     offset_in_page(IDX[k]);
575
576                                 n = template[i].tap[k];
577                                 if (k == template[i].np - 1)
578                                         n += enc ? authsize : -authsize;
579
580                                 if (memcmp(q, template[i].result + temp, n)) {
581                                         printk(KERN_ERR "alg: aead: Chunk "
582                                                "test %d failed on %s at page "
583                                                "%u for %s\n", j, e, k, algo);
584                                         hexdump(q, n);
585                                         goto out;
586                                 }
587
588                                 q += n;
589                                 if (k == template[i].np - 1 && !enc) {
590                                         if (memcmp(q, template[i].input +
591                                                       temp + n, authsize))
592                                                 n = authsize;
593                                         else
594                                                 n = 0;
595                                 } else {
596                                         for (n = 0; offset_in_page(q + n) &&
597                                                     q[n]; n++)
598                                                 ;
599                                 }
600                                 if (n) {
601                                         printk(KERN_ERR "alg: aead: Result "
602                                                "buffer corruption in chunk "
603                                                "test %d on %s at page %u for "
604                                                "%s: %u bytes:\n", j, e, k,
605                                                algo, n);
606                                         hexdump(q, n);
607                                         goto out;
608                                 }
609
610                                 temp += template[i].tap[k];
611                         }
612                 }
613         }
614
615         ret = 0;
616
617 out:
618         aead_request_free(req);
619         testmgr_free_buf(axbuf);
620 out_noaxbuf:
621         testmgr_free_buf(xbuf);
622 out_noxbuf:
623         return ret;
624 }
625
626 static int test_cipher(struct crypto_cipher *tfm, int enc,
627                        struct cipher_testvec *template, unsigned int tcount)
628 {
629         const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
630         unsigned int i, j, k;
631         char *q;
632         const char *e;
633         void *data;
634         char *xbuf[XBUFSIZE];
635         int ret = -ENOMEM;
636
637         if (testmgr_alloc_buf(xbuf))
638                 goto out_nobuf;
639
640         if (enc == ENCRYPT)
641                 e = "encryption";
642         else
643                 e = "decryption";
644
645         j = 0;
646         for (i = 0; i < tcount; i++) {
647                 if (template[i].np)
648                         continue;
649
650                 j++;
651
652                 data = xbuf[0];
653                 memcpy(data, template[i].input, template[i].ilen);
654
655                 crypto_cipher_clear_flags(tfm, ~0);
656                 if (template[i].wk)
657                         crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
658
659                 ret = crypto_cipher_setkey(tfm, template[i].key,
660                                            template[i].klen);
661                 if (!ret == template[i].fail) {
662                         printk(KERN_ERR "alg: cipher: setkey failed "
663                                "on test %d for %s: flags=%x\n", j,
664                                algo, crypto_cipher_get_flags(tfm));
665                         goto out;
666                 } else if (ret)
667                         continue;
668
669                 for (k = 0; k < template[i].ilen;
670                      k += crypto_cipher_blocksize(tfm)) {
671                         if (enc)
672                                 crypto_cipher_encrypt_one(tfm, data + k,
673                                                           data + k);
674                         else
675                                 crypto_cipher_decrypt_one(tfm, data + k,
676                                                           data + k);
677                 }
678
679                 q = data;
680                 if (memcmp(q, template[i].result, template[i].rlen)) {
681                         printk(KERN_ERR "alg: cipher: Test %d failed "
682                                "on %s for %s\n", j, e, algo);
683                         hexdump(q, template[i].rlen);
684                         ret = -EINVAL;
685                         goto out;
686                 }
687         }
688
689         ret = 0;
690
691 out:
692         testmgr_free_buf(xbuf);
693 out_nobuf:
694         return ret;
695 }
696
697 static int test_skcipher(struct crypto_ablkcipher *tfm, int enc,
698                          struct cipher_testvec *template, unsigned int tcount)
699 {
700         const char *algo =
701                 crypto_tfm_alg_driver_name(crypto_ablkcipher_tfm(tfm));
702         unsigned int i, j, k, n, temp;
703         char *q;
704         struct ablkcipher_request *req;
705         struct scatterlist sg[8];
706         const char *e;
707         struct tcrypt_result result;
708         void *data;
709         char iv[MAX_IVLEN];
710         char *xbuf[XBUFSIZE];
711         int ret = -ENOMEM;
712
713         if (testmgr_alloc_buf(xbuf))
714                 goto out_nobuf;
715
716         if (enc == ENCRYPT)
717                 e = "encryption";
718         else
719                 e = "decryption";
720
721         init_completion(&result.completion);
722
723         req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
724         if (!req) {
725                 printk(KERN_ERR "alg: skcipher: Failed to allocate request "
726                        "for %s\n", algo);
727                 goto out;
728         }
729
730         ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
731                                         tcrypt_complete, &result);
732
733         j = 0;
734         for (i = 0; i < tcount; i++) {
735                 if (template[i].iv)
736                         memcpy(iv, template[i].iv, MAX_IVLEN);
737                 else
738                         memset(iv, 0, MAX_IVLEN);
739
740                 if (!(template[i].np)) {
741                         j++;
742
743                         data = xbuf[0];
744                         memcpy(data, template[i].input, template[i].ilen);
745
746                         crypto_ablkcipher_clear_flags(tfm, ~0);
747                         if (template[i].wk)
748                                 crypto_ablkcipher_set_flags(
749                                         tfm, CRYPTO_TFM_REQ_WEAK_KEY);
750
751                         ret = crypto_ablkcipher_setkey(tfm, template[i].key,
752                                                        template[i].klen);
753                         if (!ret == template[i].fail) {
754                                 printk(KERN_ERR "alg: skcipher: setkey failed "
755                                        "on test %d for %s: flags=%x\n", j,
756                                        algo, crypto_ablkcipher_get_flags(tfm));
757                                 goto out;
758                         } else if (ret)
759                                 continue;
760
761                         sg_init_one(&sg[0], data, template[i].ilen);
762
763                         ablkcipher_request_set_crypt(req, sg, sg,
764                                                      template[i].ilen, iv);
765                         ret = enc ?
766                                 crypto_ablkcipher_encrypt(req) :
767                                 crypto_ablkcipher_decrypt(req);
768
769                         switch (ret) {
770                         case 0:
771                                 break;
772                         case -EINPROGRESS:
773                         case -EBUSY:
774                                 ret = wait_for_completion_interruptible(
775                                         &result.completion);
776                                 if (!ret && !((ret = result.err))) {
777                                         INIT_COMPLETION(result.completion);
778                                         break;
779                                 }
780                                 /* fall through */
781                         default:
782                                 printk(KERN_ERR "alg: skcipher: %s failed on "
783                                        "test %d for %s: ret=%d\n", e, j, algo,
784                                        -ret);
785                                 goto out;
786                         }
787
788                         q = data;
789                         if (memcmp(q, template[i].result, template[i].rlen)) {
790                                 printk(KERN_ERR "alg: skcipher: Test %d "
791                                        "failed on %s for %s\n", j, e, algo);
792                                 hexdump(q, template[i].rlen);
793                                 ret = -EINVAL;
794                                 goto out;
795                         }
796                 }
797         }
798
799         j = 0;
800         for (i = 0; i < tcount; i++) {
801
802                 if (template[i].iv)
803                         memcpy(iv, template[i].iv, MAX_IVLEN);
804                 else
805                         memset(iv, 0, MAX_IVLEN);
806
807                 if (template[i].np) {
808                         j++;
809
810                         crypto_ablkcipher_clear_flags(tfm, ~0);
811                         if (template[i].wk)
812                                 crypto_ablkcipher_set_flags(
813                                         tfm, CRYPTO_TFM_REQ_WEAK_KEY);
814
815                         ret = crypto_ablkcipher_setkey(tfm, template[i].key,
816                                                        template[i].klen);
817                         if (!ret == template[i].fail) {
818                                 printk(KERN_ERR "alg: skcipher: setkey failed "
819                                        "on chunk test %d for %s: flags=%x\n",
820                                        j, algo,
821                                        crypto_ablkcipher_get_flags(tfm));
822                                 goto out;
823                         } else if (ret)
824                                 continue;
825
826                         temp = 0;
827                         ret = -EINVAL;
828                         sg_init_table(sg, template[i].np);
829                         for (k = 0; k < template[i].np; k++) {
830                                 if (WARN_ON(offset_in_page(IDX[k]) +
831                                             template[i].tap[k] > PAGE_SIZE))
832                                         goto out;
833
834                                 q = xbuf[IDX[k] >> PAGE_SHIFT] +
835                                     offset_in_page(IDX[k]);
836
837                                 memcpy(q, template[i].input + temp,
838                                        template[i].tap[k]);
839
840                                 if (offset_in_page(q) + template[i].tap[k] <
841                                     PAGE_SIZE)
842                                         q[template[i].tap[k]] = 0;
843
844                                 sg_set_buf(&sg[k], q, template[i].tap[k]);
845
846                                 temp += template[i].tap[k];
847                         }
848
849                         ablkcipher_request_set_crypt(req, sg, sg,
850                                         template[i].ilen, iv);
851
852                         ret = enc ?
853                                 crypto_ablkcipher_encrypt(req) :
854                                 crypto_ablkcipher_decrypt(req);
855
856                         switch (ret) {
857                         case 0:
858                                 break;
859                         case -EINPROGRESS:
860                         case -EBUSY:
861                                 ret = wait_for_completion_interruptible(
862                                         &result.completion);
863                                 if (!ret && !((ret = result.err))) {
864                                         INIT_COMPLETION(result.completion);
865                                         break;
866                                 }
867                                 /* fall through */
868                         default:
869                                 printk(KERN_ERR "alg: skcipher: %s failed on "
870                                        "chunk test %d for %s: ret=%d\n", e, j,
871                                        algo, -ret);
872                                 goto out;
873                         }
874
875                         temp = 0;
876                         ret = -EINVAL;
877                         for (k = 0; k < template[i].np; k++) {
878                                 q = xbuf[IDX[k] >> PAGE_SHIFT] +
879                                     offset_in_page(IDX[k]);
880
881                                 if (memcmp(q, template[i].result + temp,
882                                            template[i].tap[k])) {
883                                         printk(KERN_ERR "alg: skcipher: Chunk "
884                                                "test %d failed on %s at page "
885                                                "%u for %s\n", j, e, k, algo);
886                                         hexdump(q, template[i].tap[k]);
887                                         goto out;
888                                 }
889
890                                 q += template[i].tap[k];
891                                 for (n = 0; offset_in_page(q + n) && q[n]; n++)
892                                         ;
893                                 if (n) {
894                                         printk(KERN_ERR "alg: skcipher: "
895                                                "Result buffer corruption in "
896                                                "chunk test %d on %s at page "
897                                                "%u for %s: %u bytes:\n", j, e,
898                                                k, algo, n);
899                                         hexdump(q, n);
900                                         goto out;
901                                 }
902                                 temp += template[i].tap[k];
903                         }
904                 }
905         }
906
907         ret = 0;
908
909 out:
910         ablkcipher_request_free(req);
911         testmgr_free_buf(xbuf);
912 out_nobuf:
913         return ret;
914 }
915
916 static int test_comp(struct crypto_comp *tfm, struct comp_testvec *ctemplate,
917                      struct comp_testvec *dtemplate, int ctcount, int dtcount)
918 {
919         const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
920         unsigned int i;
921         char result[COMP_BUF_SIZE];
922         int ret;
923
924         for (i = 0; i < ctcount; i++) {
925                 int ilen;
926                 unsigned int dlen = COMP_BUF_SIZE;
927
928                 memset(result, 0, sizeof (result));
929
930                 ilen = ctemplate[i].inlen;
931                 ret = crypto_comp_compress(tfm, ctemplate[i].input,
932                                            ilen, result, &dlen);
933                 if (ret) {
934                         printk(KERN_ERR "alg: comp: compression failed "
935                                "on test %d for %s: ret=%d\n", i + 1, algo,
936                                -ret);
937                         goto out;
938                 }
939
940                 if (dlen != ctemplate[i].outlen) {
941                         printk(KERN_ERR "alg: comp: Compression test %d "
942                                "failed for %s: output len = %d\n", i + 1, algo,
943                                dlen);
944                         ret = -EINVAL;
945                         goto out;
946                 }
947
948                 if (memcmp(result, ctemplate[i].output, dlen)) {
949                         printk(KERN_ERR "alg: comp: Compression test %d "
950                                "failed for %s\n", i + 1, algo);
951                         hexdump(result, dlen);
952                         ret = -EINVAL;
953                         goto out;
954                 }
955         }
956
957         for (i = 0; i < dtcount; i++) {
958                 int ilen;
959                 unsigned int dlen = COMP_BUF_SIZE;
960
961                 memset(result, 0, sizeof (result));
962
963                 ilen = dtemplate[i].inlen;
964                 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
965                                              ilen, result, &dlen);
966                 if (ret) {
967                         printk(KERN_ERR "alg: comp: decompression failed "
968                                "on test %d for %s: ret=%d\n", i + 1, algo,
969                                -ret);
970                         goto out;
971                 }
972
973                 if (dlen != dtemplate[i].outlen) {
974                         printk(KERN_ERR "alg: comp: Decompression test %d "
975                                "failed for %s: output len = %d\n", i + 1, algo,
976                                dlen);
977                         ret = -EINVAL;
978                         goto out;
979                 }
980
981                 if (memcmp(result, dtemplate[i].output, dlen)) {
982                         printk(KERN_ERR "alg: comp: Decompression test %d "
983                                "failed for %s\n", i + 1, algo);
984                         hexdump(result, dlen);
985                         ret = -EINVAL;
986                         goto out;
987                 }
988         }
989
990         ret = 0;
991
992 out:
993         return ret;
994 }
995
996 static int test_pcomp(struct crypto_pcomp *tfm,
997                       struct pcomp_testvec *ctemplate,
998                       struct pcomp_testvec *dtemplate, int ctcount,
999                       int dtcount)
1000 {
1001         const char *algo = crypto_tfm_alg_driver_name(crypto_pcomp_tfm(tfm));
1002         unsigned int i;
1003         char result[COMP_BUF_SIZE];
1004         int error;
1005
1006         for (i = 0; i < ctcount; i++) {
1007                 struct comp_request req;
1008
1009                 error = crypto_compress_setup(tfm, ctemplate[i].params,
1010                                               ctemplate[i].paramsize);
1011                 if (error) {
1012                         pr_err("alg: pcomp: compression setup failed on test "
1013                                "%d for %s: error=%d\n", i + 1, algo, error);
1014                         return error;
1015                 }
1016
1017                 error = crypto_compress_init(tfm);
1018                 if (error) {
1019                         pr_err("alg: pcomp: compression init failed on test "
1020                                "%d for %s: error=%d\n", i + 1, algo, error);
1021                         return error;
1022                 }
1023
1024                 memset(result, 0, sizeof(result));
1025
1026                 req.next_in = ctemplate[i].input;
1027                 req.avail_in = ctemplate[i].inlen / 2;
1028                 req.next_out = result;
1029                 req.avail_out = ctemplate[i].outlen / 2;
1030
1031                 error = crypto_compress_update(tfm, &req);
1032                 if (error && (error != -EAGAIN || req.avail_in)) {
1033                         pr_err("alg: pcomp: compression update failed on test "
1034                                "%d for %s: error=%d\n", i + 1, algo, error);
1035                         return error;
1036                 }
1037
1038                 /* Add remaining input data */
1039                 req.avail_in += (ctemplate[i].inlen + 1) / 2;
1040
1041                 error = crypto_compress_update(tfm, &req);
1042                 if (error && (error != -EAGAIN || req.avail_in)) {
1043                         pr_err("alg: pcomp: compression update failed on test "
1044                                "%d for %s: error=%d\n", i + 1, algo, error);
1045                         return error;
1046                 }
1047
1048                 /* Provide remaining output space */
1049                 req.avail_out += COMP_BUF_SIZE - ctemplate[i].outlen / 2;
1050
1051                 error = crypto_compress_final(tfm, &req);
1052                 if (error) {
1053                         pr_err("alg: pcomp: compression final failed on test "
1054                                "%d for %s: error=%d\n", i + 1, algo, error);
1055                         return error;
1056                 }
1057
1058                 if (COMP_BUF_SIZE - req.avail_out != ctemplate[i].outlen) {
1059                         pr_err("alg: comp: Compression test %d failed for %s: "
1060                                "output len = %d (expected %d)\n", i + 1, algo,
1061                                COMP_BUF_SIZE - req.avail_out,
1062                                ctemplate[i].outlen);
1063                         return -EINVAL;
1064                 }
1065
1066                 if (memcmp(result, ctemplate[i].output, ctemplate[i].outlen)) {
1067                         pr_err("alg: pcomp: Compression test %d failed for "
1068                                "%s\n", i + 1, algo);
1069                         hexdump(result, ctemplate[i].outlen);
1070                         return -EINVAL;
1071                 }
1072         }
1073
1074         for (i = 0; i < dtcount; i++) {
1075                 struct comp_request req;
1076
1077                 error = crypto_decompress_setup(tfm, dtemplate[i].params,
1078                                                 dtemplate[i].paramsize);
1079                 if (error) {
1080                         pr_err("alg: pcomp: decompression setup failed on "
1081                                "test %d for %s: error=%d\n", i + 1, algo,
1082                                error);
1083                         return error;
1084                 }
1085
1086                 error = crypto_decompress_init(tfm);
1087                 if (error) {
1088                         pr_err("alg: pcomp: decompression init failed on test "
1089                                "%d for %s: error=%d\n", i + 1, algo, error);
1090                         return error;
1091                 }
1092
1093                 memset(result, 0, sizeof(result));
1094
1095                 req.next_in = dtemplate[i].input;
1096                 req.avail_in = dtemplate[i].inlen / 2;
1097                 req.next_out = result;
1098                 req.avail_out = dtemplate[i].outlen / 2;
1099
1100                 error = crypto_decompress_update(tfm, &req);
1101                 if (error  && (error != -EAGAIN || req.avail_in)) {
1102                         pr_err("alg: pcomp: decompression update failed on "
1103                                "test %d for %s: error=%d\n", i + 1, algo,
1104                                error);
1105                         return error;
1106                 }
1107
1108                 /* Add remaining input data */
1109                 req.avail_in += (dtemplate[i].inlen + 1) / 2;
1110
1111                 error = crypto_decompress_update(tfm, &req);
1112                 if (error  && (error != -EAGAIN || req.avail_in)) {
1113                         pr_err("alg: pcomp: decompression update failed on "
1114                                "test %d for %s: error=%d\n", i + 1, algo,
1115                                error);
1116                         return error;
1117                 }
1118
1119                 /* Provide remaining output space */
1120                 req.avail_out += COMP_BUF_SIZE - dtemplate[i].outlen / 2;
1121
1122                 error = crypto_decompress_final(tfm, &req);
1123                 if (error  && (error != -EAGAIN || req.avail_in)) {
1124                         pr_err("alg: pcomp: decompression final failed on "
1125                                "test %d for %s: error=%d\n", i + 1, algo,
1126                                error);
1127                         return error;
1128                 }
1129
1130                 if (COMP_BUF_SIZE - req.avail_out != dtemplate[i].outlen) {
1131                         pr_err("alg: comp: Decompression test %d failed for "
1132                                "%s: output len = %d (expected %d)\n", i + 1,
1133                                algo, COMP_BUF_SIZE - req.avail_out,
1134                                dtemplate[i].outlen);
1135                         return -EINVAL;
1136                 }
1137
1138                 if (memcmp(result, dtemplate[i].output, dtemplate[i].outlen)) {
1139                         pr_err("alg: pcomp: Decompression test %d failed for "
1140                                "%s\n", i + 1, algo);
1141                         hexdump(result, dtemplate[i].outlen);
1142                         return -EINVAL;
1143                 }
1144         }
1145
1146         return 0;
1147 }
1148
1149
1150 static int test_cprng(struct crypto_rng *tfm, struct cprng_testvec *template,
1151                       unsigned int tcount)
1152 {
1153         const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
1154         int err, i, j, seedsize;
1155         u8 *seed;
1156         char result[32];
1157
1158         seedsize = crypto_rng_seedsize(tfm);
1159
1160         seed = kmalloc(seedsize, GFP_KERNEL);
1161         if (!seed) {
1162                 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
1163                        "for %s\n", algo);
1164                 return -ENOMEM;
1165         }
1166
1167         for (i = 0; i < tcount; i++) {
1168                 memset(result, 0, 32);
1169
1170                 memcpy(seed, template[i].v, template[i].vlen);
1171                 memcpy(seed + template[i].vlen, template[i].key,
1172                        template[i].klen);
1173                 memcpy(seed + template[i].vlen + template[i].klen,
1174                        template[i].dt, template[i].dtlen);
1175
1176                 err = crypto_rng_reset(tfm, seed, seedsize);
1177                 if (err) {
1178                         printk(KERN_ERR "alg: cprng: Failed to reset rng "
1179                                "for %s\n", algo);
1180                         goto out;
1181                 }
1182
1183                 for (j = 0; j < template[i].loops; j++) {
1184                         err = crypto_rng_get_bytes(tfm, result,
1185                                                    template[i].rlen);
1186                         if (err != template[i].rlen) {
1187                                 printk(KERN_ERR "alg: cprng: Failed to obtain "
1188                                        "the correct amount of random data for "
1189                                        "%s (requested %d, got %d)\n", algo,
1190                                        template[i].rlen, err);
1191                                 goto out;
1192                         }
1193                 }
1194
1195                 err = memcmp(result, template[i].result,
1196                              template[i].rlen);
1197                 if (err) {
1198                         printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
1199                                i, algo);
1200                         hexdump(result, template[i].rlen);
1201                         err = -EINVAL;
1202                         goto out;
1203                 }
1204         }
1205
1206 out:
1207         kfree(seed);
1208         return err;
1209 }
1210
1211 static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
1212                          u32 type, u32 mask)
1213 {
1214         struct crypto_aead *tfm;
1215         int err = 0;
1216
1217         tfm = crypto_alloc_aead(driver, type, mask);
1218         if (IS_ERR(tfm)) {
1219                 printk(KERN_ERR "alg: aead: Failed to load transform for %s: "
1220                        "%ld\n", driver, PTR_ERR(tfm));
1221                 return PTR_ERR(tfm);
1222         }
1223
1224         if (desc->suite.aead.enc.vecs) {
1225                 err = test_aead(tfm, ENCRYPT, desc->suite.aead.enc.vecs,
1226                                 desc->suite.aead.enc.count);
1227                 if (err)
1228                         goto out;
1229         }
1230
1231         if (!err && desc->suite.aead.dec.vecs)
1232                 err = test_aead(tfm, DECRYPT, desc->suite.aead.dec.vecs,
1233                                 desc->suite.aead.dec.count);
1234
1235 out:
1236         crypto_free_aead(tfm);
1237         return err;
1238 }
1239
1240 static int alg_test_cipher(const struct alg_test_desc *desc,
1241                            const char *driver, u32 type, u32 mask)
1242 {
1243         struct crypto_cipher *tfm;
1244         int err = 0;
1245
1246         tfm = crypto_alloc_cipher(driver, type, mask);
1247         if (IS_ERR(tfm)) {
1248                 printk(KERN_ERR "alg: cipher: Failed to load transform for "
1249                        "%s: %ld\n", driver, PTR_ERR(tfm));
1250                 return PTR_ERR(tfm);
1251         }
1252
1253         if (desc->suite.cipher.enc.vecs) {
1254                 err = test_cipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
1255                                   desc->suite.cipher.enc.count);
1256                 if (err)
1257                         goto out;
1258         }
1259
1260         if (desc->suite.cipher.dec.vecs)
1261                 err = test_cipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
1262                                   desc->suite.cipher.dec.count);
1263
1264 out:
1265         crypto_free_cipher(tfm);
1266         return err;
1267 }
1268
1269 static int alg_test_skcipher(const struct alg_test_desc *desc,
1270                              const char *driver, u32 type, u32 mask)
1271 {
1272         struct crypto_ablkcipher *tfm;
1273         int err = 0;
1274
1275         tfm = crypto_alloc_ablkcipher(driver, type, mask);
1276         if (IS_ERR(tfm)) {
1277                 printk(KERN_ERR "alg: skcipher: Failed to load transform for "
1278                        "%s: %ld\n", driver, PTR_ERR(tfm));
1279                 return PTR_ERR(tfm);
1280         }
1281
1282         if (desc->suite.cipher.enc.vecs) {
1283                 err = test_skcipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
1284                                     desc->suite.cipher.enc.count);
1285                 if (err)
1286                         goto out;
1287         }
1288
1289         if (desc->suite.cipher.dec.vecs)
1290                 err = test_skcipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
1291                                     desc->suite.cipher.dec.count);
1292
1293 out:
1294         crypto_free_ablkcipher(tfm);
1295         return err;
1296 }
1297
1298 static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
1299                          u32 type, u32 mask)
1300 {
1301         struct crypto_comp *tfm;
1302         int err;
1303
1304         tfm = crypto_alloc_comp(driver, type, mask);
1305         if (IS_ERR(tfm)) {
1306                 printk(KERN_ERR "alg: comp: Failed to load transform for %s: "
1307                        "%ld\n", driver, PTR_ERR(tfm));
1308                 return PTR_ERR(tfm);
1309         }
1310
1311         err = test_comp(tfm, desc->suite.comp.comp.vecs,
1312                         desc->suite.comp.decomp.vecs,
1313                         desc->suite.comp.comp.count,
1314                         desc->suite.comp.decomp.count);
1315
1316         crypto_free_comp(tfm);
1317         return err;
1318 }
1319
1320 static int alg_test_pcomp(const struct alg_test_desc *desc, const char *driver,
1321                           u32 type, u32 mask)
1322 {
1323         struct crypto_pcomp *tfm;
1324         int err;
1325
1326         tfm = crypto_alloc_pcomp(driver, type, mask);
1327         if (IS_ERR(tfm)) {
1328                 pr_err("alg: pcomp: Failed to load transform for %s: %ld\n",
1329                        driver, PTR_ERR(tfm));
1330                 return PTR_ERR(tfm);
1331         }
1332
1333         err = test_pcomp(tfm, desc->suite.pcomp.comp.vecs,
1334                          desc->suite.pcomp.decomp.vecs,
1335                          desc->suite.pcomp.comp.count,
1336                          desc->suite.pcomp.decomp.count);
1337
1338         crypto_free_pcomp(tfm);
1339         return err;
1340 }
1341
1342 static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1343                          u32 type, u32 mask)
1344 {
1345         struct crypto_ahash *tfm;
1346         int err;
1347
1348         tfm = crypto_alloc_ahash(driver, type, mask);
1349         if (IS_ERR(tfm)) {
1350                 printk(KERN_ERR "alg: hash: Failed to load transform for %s: "
1351                        "%ld\n", driver, PTR_ERR(tfm));
1352                 return PTR_ERR(tfm);
1353         }
1354
1355         err = test_hash(tfm, desc->suite.hash.vecs, desc->suite.hash.count);
1356
1357         crypto_free_ahash(tfm);
1358         return err;
1359 }
1360
1361 static int alg_test_crc32c(const struct alg_test_desc *desc,
1362                            const char *driver, u32 type, u32 mask)
1363 {
1364         struct crypto_shash *tfm;
1365         u32 val;
1366         int err;
1367
1368         err = alg_test_hash(desc, driver, type, mask);
1369         if (err)
1370                 goto out;
1371
1372         tfm = crypto_alloc_shash(driver, type, mask);
1373         if (IS_ERR(tfm)) {
1374                 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
1375                        "%ld\n", driver, PTR_ERR(tfm));
1376                 err = PTR_ERR(tfm);
1377                 goto out;
1378         }
1379
1380         do {
1381                 struct {
1382                         struct shash_desc shash;
1383                         char ctx[crypto_shash_descsize(tfm)];
1384                 } sdesc;
1385
1386                 sdesc.shash.tfm = tfm;
1387                 sdesc.shash.flags = 0;
1388
1389                 *(u32 *)sdesc.ctx = le32_to_cpu(420553207);
1390                 err = crypto_shash_final(&sdesc.shash, (u8 *)&val);
1391                 if (err) {
1392                         printk(KERN_ERR "alg: crc32c: Operation failed for "
1393                                "%s: %d\n", driver, err);
1394                         break;
1395                 }
1396
1397                 if (val != ~420553207) {
1398                         printk(KERN_ERR "alg: crc32c: Test failed for %s: "
1399                                "%d\n", driver, val);
1400                         err = -EINVAL;
1401                 }
1402         } while (0);
1403
1404         crypto_free_shash(tfm);
1405
1406 out:
1407         return err;
1408 }
1409
1410 static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
1411                           u32 type, u32 mask)
1412 {
1413         struct crypto_rng *rng;
1414         int err;
1415
1416         rng = crypto_alloc_rng(driver, type, mask);
1417         if (IS_ERR(rng)) {
1418                 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
1419                        "%ld\n", driver, PTR_ERR(rng));
1420                 return PTR_ERR(rng);
1421         }
1422
1423         err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
1424
1425         crypto_free_rng(rng);
1426
1427         return err;
1428 }
1429
1430 /* Please keep this list sorted by algorithm name. */
1431 static const struct alg_test_desc alg_test_descs[] = {
1432         {
1433                 .alg = "ansi_cprng",
1434                 .test = alg_test_cprng,
1435                 .suite = {
1436                         .cprng = {
1437                                 .vecs = ansi_cprng_aes_tv_template,
1438                                 .count = ANSI_CPRNG_AES_TEST_VECTORS
1439                         }
1440                 }
1441         }, {
1442                 .alg = "cbc(aes)",
1443                 .test = alg_test_skcipher,
1444                 .suite = {
1445                         .cipher = {
1446                                 .enc = {
1447                                         .vecs = aes_cbc_enc_tv_template,
1448                                         .count = AES_CBC_ENC_TEST_VECTORS
1449                                 },
1450                                 .dec = {
1451                                         .vecs = aes_cbc_dec_tv_template,
1452                                         .count = AES_CBC_DEC_TEST_VECTORS
1453                                 }
1454                         }
1455                 }
1456         }, {
1457                 .alg = "cbc(anubis)",
1458                 .test = alg_test_skcipher,
1459                 .suite = {
1460                         .cipher = {
1461                                 .enc = {
1462                                         .vecs = anubis_cbc_enc_tv_template,
1463                                         .count = ANUBIS_CBC_ENC_TEST_VECTORS
1464                                 },
1465                                 .dec = {
1466                                         .vecs = anubis_cbc_dec_tv_template,
1467                                         .count = ANUBIS_CBC_DEC_TEST_VECTORS
1468                                 }
1469                         }
1470                 }
1471         }, {
1472                 .alg = "cbc(blowfish)",
1473                 .test = alg_test_skcipher,
1474                 .suite = {
1475                         .cipher = {
1476                                 .enc = {
1477                                         .vecs = bf_cbc_enc_tv_template,
1478                                         .count = BF_CBC_ENC_TEST_VECTORS
1479                                 },
1480                                 .dec = {
1481                                         .vecs = bf_cbc_dec_tv_template,
1482                                         .count = BF_CBC_DEC_TEST_VECTORS
1483                                 }
1484                         }
1485                 }
1486         }, {
1487                 .alg = "cbc(camellia)",
1488                 .test = alg_test_skcipher,
1489                 .suite = {
1490                         .cipher = {
1491                                 .enc = {
1492                                         .vecs = camellia_cbc_enc_tv_template,
1493                                         .count = CAMELLIA_CBC_ENC_TEST_VECTORS
1494                                 },
1495                                 .dec = {
1496                                         .vecs = camellia_cbc_dec_tv_template,
1497                                         .count = CAMELLIA_CBC_DEC_TEST_VECTORS
1498                                 }
1499                         }
1500                 }
1501         }, {
1502                 .alg = "cbc(des)",
1503                 .test = alg_test_skcipher,
1504                 .suite = {
1505                         .cipher = {
1506                                 .enc = {
1507                                         .vecs = des_cbc_enc_tv_template,
1508                                         .count = DES_CBC_ENC_TEST_VECTORS
1509                                 },
1510                                 .dec = {
1511                                         .vecs = des_cbc_dec_tv_template,
1512                                         .count = DES_CBC_DEC_TEST_VECTORS
1513                                 }
1514                         }
1515                 }
1516         }, {
1517                 .alg = "cbc(des3_ede)",
1518                 .test = alg_test_skcipher,
1519                 .suite = {
1520                         .cipher = {
1521                                 .enc = {
1522                                         .vecs = des3_ede_cbc_enc_tv_template,
1523                                         .count = DES3_EDE_CBC_ENC_TEST_VECTORS
1524                                 },
1525                                 .dec = {
1526                                         .vecs = des3_ede_cbc_dec_tv_template,
1527                                         .count = DES3_EDE_CBC_DEC_TEST_VECTORS
1528                                 }
1529                         }
1530                 }
1531         }, {
1532                 .alg = "cbc(twofish)",
1533                 .test = alg_test_skcipher,
1534                 .suite = {
1535                         .cipher = {
1536                                 .enc = {
1537                                         .vecs = tf_cbc_enc_tv_template,
1538                                         .count = TF_CBC_ENC_TEST_VECTORS
1539                                 },
1540                                 .dec = {
1541                                         .vecs = tf_cbc_dec_tv_template,
1542                                         .count = TF_CBC_DEC_TEST_VECTORS
1543                                 }
1544                         }
1545                 }
1546         }, {
1547                 .alg = "ccm(aes)",
1548                 .test = alg_test_aead,
1549                 .suite = {
1550                         .aead = {
1551                                 .enc = {
1552                                         .vecs = aes_ccm_enc_tv_template,
1553                                         .count = AES_CCM_ENC_TEST_VECTORS
1554                                 },
1555                                 .dec = {
1556                                         .vecs = aes_ccm_dec_tv_template,
1557                                         .count = AES_CCM_DEC_TEST_VECTORS
1558                                 }
1559                         }
1560                 }
1561         }, {
1562                 .alg = "crc32c",
1563                 .test = alg_test_crc32c,
1564                 .suite = {
1565                         .hash = {
1566                                 .vecs = crc32c_tv_template,
1567                                 .count = CRC32C_TEST_VECTORS
1568                         }
1569                 }
1570         }, {
1571                 .alg = "cts(cbc(aes))",
1572                 .test = alg_test_skcipher,
1573                 .suite = {
1574                         .cipher = {
1575                                 .enc = {
1576                                         .vecs = cts_mode_enc_tv_template,
1577                                         .count = CTS_MODE_ENC_TEST_VECTORS
1578                                 },
1579                                 .dec = {
1580                                         .vecs = cts_mode_dec_tv_template,
1581                                         .count = CTS_MODE_DEC_TEST_VECTORS
1582                                 }
1583                         }
1584                 }
1585         }, {
1586                 .alg = "deflate",
1587                 .test = alg_test_comp,
1588                 .suite = {
1589                         .comp = {
1590                                 .comp = {
1591                                         .vecs = deflate_comp_tv_template,
1592                                         .count = DEFLATE_COMP_TEST_VECTORS
1593                                 },
1594                                 .decomp = {
1595                                         .vecs = deflate_decomp_tv_template,
1596                                         .count = DEFLATE_DECOMP_TEST_VECTORS
1597                                 }
1598                         }
1599                 }
1600         }, {
1601                 .alg = "ecb(aes)",
1602                 .test = alg_test_skcipher,
1603                 .suite = {
1604                         .cipher = {
1605                                 .enc = {
1606                                         .vecs = aes_enc_tv_template,
1607                                         .count = AES_ENC_TEST_VECTORS
1608                                 },
1609                                 .dec = {
1610                                         .vecs = aes_dec_tv_template,
1611                                         .count = AES_DEC_TEST_VECTORS
1612                                 }
1613                         }
1614                 }
1615         }, {
1616                 .alg = "ecb(anubis)",
1617                 .test = alg_test_skcipher,
1618                 .suite = {
1619                         .cipher = {
1620                                 .enc = {
1621                                         .vecs = anubis_enc_tv_template,
1622                                         .count = ANUBIS_ENC_TEST_VECTORS
1623                                 },
1624                                 .dec = {
1625                                         .vecs = anubis_dec_tv_template,
1626                                         .count = ANUBIS_DEC_TEST_VECTORS
1627                                 }
1628                         }
1629                 }
1630         }, {
1631                 .alg = "ecb(arc4)",
1632                 .test = alg_test_skcipher,
1633                 .suite = {
1634                         .cipher = {
1635                                 .enc = {
1636                                         .vecs = arc4_enc_tv_template,
1637                                         .count = ARC4_ENC_TEST_VECTORS
1638                                 },
1639                                 .dec = {
1640                                         .vecs = arc4_dec_tv_template,
1641                                         .count = ARC4_DEC_TEST_VECTORS
1642                                 }
1643                         }
1644                 }
1645         }, {
1646                 .alg = "ecb(blowfish)",
1647                 .test = alg_test_skcipher,
1648                 .suite = {
1649                         .cipher = {
1650                                 .enc = {
1651                                         .vecs = bf_enc_tv_template,
1652                                         .count = BF_ENC_TEST_VECTORS
1653                                 },
1654                                 .dec = {
1655                                         .vecs = bf_dec_tv_template,
1656                                         .count = BF_DEC_TEST_VECTORS
1657                                 }
1658                         }
1659                 }
1660         }, {
1661                 .alg = "ecb(camellia)",
1662                 .test = alg_test_skcipher,
1663                 .suite = {
1664                         .cipher = {
1665                                 .enc = {
1666                                         .vecs = camellia_enc_tv_template,
1667                                         .count = CAMELLIA_ENC_TEST_VECTORS
1668                                 },
1669                                 .dec = {
1670                                         .vecs = camellia_dec_tv_template,
1671                                         .count = CAMELLIA_DEC_TEST_VECTORS
1672                                 }
1673                         }
1674                 }
1675         }, {
1676                 .alg = "ecb(cast5)",
1677                 .test = alg_test_skcipher,
1678                 .suite = {
1679                         .cipher = {
1680                                 .enc = {
1681                                         .vecs = cast5_enc_tv_template,
1682                                         .count = CAST5_ENC_TEST_VECTORS
1683                                 },
1684                                 .dec = {
1685                                         .vecs = cast5_dec_tv_template,
1686                                         .count = CAST5_DEC_TEST_VECTORS
1687                                 }
1688                         }
1689                 }
1690         }, {
1691                 .alg = "ecb(cast6)",
1692                 .test = alg_test_skcipher,
1693                 .suite = {
1694                         .cipher = {
1695                                 .enc = {
1696                                         .vecs = cast6_enc_tv_template,
1697                                         .count = CAST6_ENC_TEST_VECTORS
1698                                 },
1699                                 .dec = {
1700                                         .vecs = cast6_dec_tv_template,
1701                                         .count = CAST6_DEC_TEST_VECTORS
1702                                 }
1703                         }
1704                 }
1705         }, {
1706                 .alg = "ecb(des)",
1707                 .test = alg_test_skcipher,
1708                 .suite = {
1709                         .cipher = {
1710                                 .enc = {
1711                                         .vecs = des_enc_tv_template,
1712                                         .count = DES_ENC_TEST_VECTORS
1713                                 },
1714                                 .dec = {
1715                                         .vecs = des_dec_tv_template,
1716                                         .count = DES_DEC_TEST_VECTORS
1717                                 }
1718                         }
1719                 }
1720         }, {
1721                 .alg = "ecb(des3_ede)",
1722                 .test = alg_test_skcipher,
1723                 .suite = {
1724                         .cipher = {
1725                                 .enc = {
1726                                         .vecs = des3_ede_enc_tv_template,
1727                                         .count = DES3_EDE_ENC_TEST_VECTORS
1728                                 },
1729                                 .dec = {
1730                                         .vecs = des3_ede_dec_tv_template,
1731                                         .count = DES3_EDE_DEC_TEST_VECTORS
1732                                 }
1733                         }
1734                 }
1735         }, {
1736                 .alg = "ecb(khazad)",
1737                 .test = alg_test_skcipher,
1738                 .suite = {
1739                         .cipher = {
1740                                 .enc = {
1741                                         .vecs = khazad_enc_tv_template,
1742                                         .count = KHAZAD_ENC_TEST_VECTORS
1743                                 },
1744                                 .dec = {
1745                                         .vecs = khazad_dec_tv_template,
1746                                         .count = KHAZAD_DEC_TEST_VECTORS
1747                                 }
1748                         }
1749                 }
1750         }, {
1751                 .alg = "ecb(seed)",
1752                 .test = alg_test_skcipher,
1753                 .suite = {
1754                         .cipher = {
1755                                 .enc = {
1756                                         .vecs = seed_enc_tv_template,
1757                                         .count = SEED_ENC_TEST_VECTORS
1758                                 },
1759                                 .dec = {
1760                                         .vecs = seed_dec_tv_template,
1761                                         .count = SEED_DEC_TEST_VECTORS
1762                                 }
1763                         }
1764                 }
1765         }, {
1766                 .alg = "ecb(serpent)",
1767                 .test = alg_test_skcipher,
1768                 .suite = {
1769                         .cipher = {
1770                                 .enc = {
1771                                         .vecs = serpent_enc_tv_template,
1772                                         .count = SERPENT_ENC_TEST_VECTORS
1773                                 },
1774                                 .dec = {
1775                                         .vecs = serpent_dec_tv_template,
1776                                         .count = SERPENT_DEC_TEST_VECTORS
1777                                 }
1778                         }
1779                 }
1780         }, {
1781                 .alg = "ecb(tea)",
1782                 .test = alg_test_skcipher,
1783                 .suite = {
1784                         .cipher = {
1785                                 .enc = {
1786                                         .vecs = tea_enc_tv_template,
1787                                         .count = TEA_ENC_TEST_VECTORS
1788                                 },
1789                                 .dec = {
1790                                         .vecs = tea_dec_tv_template,
1791                                         .count = TEA_DEC_TEST_VECTORS
1792                                 }
1793                         }
1794                 }
1795         }, {
1796                 .alg = "ecb(tnepres)",
1797                 .test = alg_test_skcipher,
1798                 .suite = {
1799                         .cipher = {
1800                                 .enc = {
1801                                         .vecs = tnepres_enc_tv_template,
1802                                         .count = TNEPRES_ENC_TEST_VECTORS
1803                                 },
1804                                 .dec = {
1805                                         .vecs = tnepres_dec_tv_template,
1806                                         .count = TNEPRES_DEC_TEST_VECTORS
1807                                 }
1808                         }
1809                 }
1810         }, {
1811                 .alg = "ecb(twofish)",
1812                 .test = alg_test_skcipher,
1813                 .suite = {
1814                         .cipher = {
1815                                 .enc = {
1816                                         .vecs = tf_enc_tv_template,
1817                                         .count = TF_ENC_TEST_VECTORS
1818                                 },
1819                                 .dec = {
1820                                         .vecs = tf_dec_tv_template,
1821                                         .count = TF_DEC_TEST_VECTORS
1822                                 }
1823                         }
1824                 }
1825         }, {
1826                 .alg = "ecb(xeta)",
1827                 .test = alg_test_skcipher,
1828                 .suite = {
1829                         .cipher = {
1830                                 .enc = {
1831                                         .vecs = xeta_enc_tv_template,
1832                                         .count = XETA_ENC_TEST_VECTORS
1833                                 },
1834                                 .dec = {
1835                                         .vecs = xeta_dec_tv_template,
1836                                         .count = XETA_DEC_TEST_VECTORS
1837                                 }
1838                         }
1839                 }
1840         }, {
1841                 .alg = "ecb(xtea)",
1842                 .test = alg_test_skcipher,
1843                 .suite = {
1844                         .cipher = {
1845                                 .enc = {
1846                                         .vecs = xtea_enc_tv_template,
1847                                         .count = XTEA_ENC_TEST_VECTORS
1848                                 },
1849                                 .dec = {
1850                                         .vecs = xtea_dec_tv_template,
1851                                         .count = XTEA_DEC_TEST_VECTORS
1852                                 }
1853                         }
1854                 }
1855         }, {
1856                 .alg = "gcm(aes)",
1857                 .test = alg_test_aead,
1858                 .suite = {
1859                         .aead = {
1860                                 .enc = {
1861                                         .vecs = aes_gcm_enc_tv_template,
1862                                         .count = AES_GCM_ENC_TEST_VECTORS
1863                                 },
1864                                 .dec = {
1865                                         .vecs = aes_gcm_dec_tv_template,
1866                                         .count = AES_GCM_DEC_TEST_VECTORS
1867                                 }
1868                         }
1869                 }
1870         }, {
1871                 .alg = "hmac(md5)",
1872                 .test = alg_test_hash,
1873                 .suite = {
1874                         .hash = {
1875                                 .vecs = hmac_md5_tv_template,
1876                                 .count = HMAC_MD5_TEST_VECTORS
1877                         }
1878                 }
1879         }, {
1880                 .alg = "hmac(rmd128)",
1881                 .test = alg_test_hash,
1882                 .suite = {
1883                         .hash = {
1884                                 .vecs = hmac_rmd128_tv_template,
1885                                 .count = HMAC_RMD128_TEST_VECTORS
1886                         }
1887                 }
1888         }, {
1889                 .alg = "hmac(rmd160)",
1890                 .test = alg_test_hash,
1891                 .suite = {
1892                         .hash = {
1893                                 .vecs = hmac_rmd160_tv_template,
1894                                 .count = HMAC_RMD160_TEST_VECTORS
1895                         }
1896                 }
1897         }, {
1898                 .alg = "hmac(sha1)",
1899                 .test = alg_test_hash,
1900                 .suite = {
1901                         .hash = {
1902                                 .vecs = hmac_sha1_tv_template,
1903                                 .count = HMAC_SHA1_TEST_VECTORS
1904                         }
1905                 }
1906         }, {
1907                 .alg = "hmac(sha224)",
1908                 .test = alg_test_hash,
1909                 .suite = {
1910                         .hash = {
1911                                 .vecs = hmac_sha224_tv_template,
1912                                 .count = HMAC_SHA224_TEST_VECTORS
1913                         }
1914                 }
1915         }, {
1916                 .alg = "hmac(sha256)",
1917                 .test = alg_test_hash,
1918                 .suite = {
1919                         .hash = {
1920                                 .vecs = hmac_sha256_tv_template,
1921                                 .count = HMAC_SHA256_TEST_VECTORS
1922                         }
1923                 }
1924         }, {
1925                 .alg = "hmac(sha384)",
1926                 .test = alg_test_hash,
1927                 .suite = {
1928                         .hash = {
1929                                 .vecs = hmac_sha384_tv_template,
1930                                 .count = HMAC_SHA384_TEST_VECTORS
1931                         }
1932                 }
1933         }, {
1934                 .alg = "hmac(sha512)",
1935                 .test = alg_test_hash,
1936                 .suite = {
1937                         .hash = {
1938                                 .vecs = hmac_sha512_tv_template,
1939                                 .count = HMAC_SHA512_TEST_VECTORS
1940                         }
1941                 }
1942         }, {
1943                 .alg = "lrw(aes)",
1944                 .test = alg_test_skcipher,
1945                 .suite = {
1946                         .cipher = {
1947                                 .enc = {
1948                                         .vecs = aes_lrw_enc_tv_template,
1949                                         .count = AES_LRW_ENC_TEST_VECTORS
1950                                 },
1951                                 .dec = {
1952                                         .vecs = aes_lrw_dec_tv_template,
1953                                         .count = AES_LRW_DEC_TEST_VECTORS
1954                                 }
1955                         }
1956                 }
1957         }, {
1958                 .alg = "lzo",
1959                 .test = alg_test_comp,
1960                 .suite = {
1961                         .comp = {
1962                                 .comp = {
1963                                         .vecs = lzo_comp_tv_template,
1964                                         .count = LZO_COMP_TEST_VECTORS
1965                                 },
1966                                 .decomp = {
1967                                         .vecs = lzo_decomp_tv_template,
1968                                         .count = LZO_DECOMP_TEST_VECTORS
1969                                 }
1970                         }
1971                 }
1972         }, {
1973                 .alg = "md4",
1974                 .test = alg_test_hash,
1975                 .suite = {
1976                         .hash = {
1977                                 .vecs = md4_tv_template,
1978                                 .count = MD4_TEST_VECTORS
1979                         }
1980                 }
1981         }, {
1982                 .alg = "md5",
1983                 .test = alg_test_hash,
1984                 .suite = {
1985                         .hash = {
1986                                 .vecs = md5_tv_template,
1987                                 .count = MD5_TEST_VECTORS
1988                         }
1989                 }
1990         }, {
1991                 .alg = "michael_mic",
1992                 .test = alg_test_hash,
1993                 .suite = {
1994                         .hash = {
1995                                 .vecs = michael_mic_tv_template,
1996                                 .count = MICHAEL_MIC_TEST_VECTORS
1997                         }
1998                 }
1999         }, {
2000                 .alg = "pcbc(fcrypt)",
2001                 .test = alg_test_skcipher,
2002                 .suite = {
2003                         .cipher = {
2004                                 .enc = {
2005                                         .vecs = fcrypt_pcbc_enc_tv_template,
2006                                         .count = FCRYPT_ENC_TEST_VECTORS
2007                                 },
2008                                 .dec = {
2009                                         .vecs = fcrypt_pcbc_dec_tv_template,
2010                                         .count = FCRYPT_DEC_TEST_VECTORS
2011                                 }
2012                         }
2013                 }
2014         }, {
2015                 .alg = "rfc3686(ctr(aes))",
2016                 .test = alg_test_skcipher,
2017                 .suite = {
2018                         .cipher = {
2019                                 .enc = {
2020                                         .vecs = aes_ctr_enc_tv_template,
2021                                         .count = AES_CTR_ENC_TEST_VECTORS
2022                                 },
2023                                 .dec = {
2024                                         .vecs = aes_ctr_dec_tv_template,
2025                                         .count = AES_CTR_DEC_TEST_VECTORS
2026                                 }
2027                         }
2028                 }
2029         }, {
2030                 .alg = "rfc4309(ccm(aes))",
2031                 .test = alg_test_aead,
2032                 .suite = {
2033                         .aead = {
2034                                 .enc = {
2035                                         .vecs = aes_ccm_rfc4309_enc_tv_template,
2036                                         .count = AES_CCM_4309_ENC_TEST_VECTORS
2037                                 },
2038                                 .dec = {
2039                                         .vecs = aes_ccm_rfc4309_dec_tv_template,
2040                                         .count = AES_CCM_4309_DEC_TEST_VECTORS
2041                                 }
2042                         }
2043                 }
2044         }, {
2045                 .alg = "rmd128",
2046                 .test = alg_test_hash,
2047                 .suite = {
2048                         .hash = {
2049                                 .vecs = rmd128_tv_template,
2050                                 .count = RMD128_TEST_VECTORS
2051                         }
2052                 }
2053         }, {
2054                 .alg = "rmd160",
2055                 .test = alg_test_hash,
2056                 .suite = {
2057                         .hash = {
2058                                 .vecs = rmd160_tv_template,
2059                                 .count = RMD160_TEST_VECTORS
2060                         }
2061                 }
2062         }, {
2063                 .alg = "rmd256",
2064                 .test = alg_test_hash,
2065                 .suite = {
2066                         .hash = {
2067                                 .vecs = rmd256_tv_template,
2068                                 .count = RMD256_TEST_VECTORS
2069                         }
2070                 }
2071         }, {
2072                 .alg = "rmd320",
2073                 .test = alg_test_hash,
2074                 .suite = {
2075                         .hash = {
2076                                 .vecs = rmd320_tv_template,
2077                                 .count = RMD320_TEST_VECTORS
2078                         }
2079                 }
2080         }, {
2081                 .alg = "salsa20",
2082                 .test = alg_test_skcipher,
2083                 .suite = {
2084                         .cipher = {
2085                                 .enc = {
2086                                         .vecs = salsa20_stream_enc_tv_template,
2087                                         .count = SALSA20_STREAM_ENC_TEST_VECTORS
2088                                 }
2089                         }
2090                 }
2091         }, {
2092                 .alg = "sha1",
2093                 .test = alg_test_hash,
2094                 .suite = {
2095                         .hash = {
2096                                 .vecs = sha1_tv_template,
2097                                 .count = SHA1_TEST_VECTORS
2098                         }
2099                 }
2100         }, {
2101                 .alg = "sha224",
2102                 .test = alg_test_hash,
2103                 .suite = {
2104                         .hash = {
2105                                 .vecs = sha224_tv_template,
2106                                 .count = SHA224_TEST_VECTORS
2107                         }
2108                 }
2109         }, {
2110                 .alg = "sha256",
2111                 .test = alg_test_hash,
2112                 .suite = {
2113                         .hash = {
2114                                 .vecs = sha256_tv_template,
2115                                 .count = SHA256_TEST_VECTORS
2116                         }
2117                 }
2118         }, {
2119                 .alg = "sha384",
2120                 .test = alg_test_hash,
2121                 .suite = {
2122                         .hash = {
2123                                 .vecs = sha384_tv_template,
2124                                 .count = SHA384_TEST_VECTORS
2125                         }
2126                 }
2127         }, {
2128                 .alg = "sha512",
2129                 .test = alg_test_hash,
2130                 .suite = {
2131                         .hash = {
2132                                 .vecs = sha512_tv_template,
2133                                 .count = SHA512_TEST_VECTORS
2134                         }
2135                 }
2136         }, {
2137                 .alg = "tgr128",
2138                 .test = alg_test_hash,
2139                 .suite = {
2140                         .hash = {
2141                                 .vecs = tgr128_tv_template,
2142                                 .count = TGR128_TEST_VECTORS
2143                         }
2144                 }
2145         }, {
2146                 .alg = "tgr160",
2147                 .test = alg_test_hash,
2148                 .suite = {
2149                         .hash = {
2150                                 .vecs = tgr160_tv_template,
2151                                 .count = TGR160_TEST_VECTORS
2152                         }
2153                 }
2154         }, {
2155                 .alg = "tgr192",
2156                 .test = alg_test_hash,
2157                 .suite = {
2158                         .hash = {
2159                                 .vecs = tgr192_tv_template,
2160                                 .count = TGR192_TEST_VECTORS
2161                         }
2162                 }
2163         }, {
2164                 .alg = "wp256",
2165                 .test = alg_test_hash,
2166                 .suite = {
2167                         .hash = {
2168                                 .vecs = wp256_tv_template,
2169                                 .count = WP256_TEST_VECTORS
2170                         }
2171                 }
2172         }, {
2173                 .alg = "wp384",
2174                 .test = alg_test_hash,
2175                 .suite = {
2176                         .hash = {
2177                                 .vecs = wp384_tv_template,
2178                                 .count = WP384_TEST_VECTORS
2179                         }
2180                 }
2181         }, {
2182                 .alg = "wp512",
2183                 .test = alg_test_hash,
2184                 .suite = {
2185                         .hash = {
2186                                 .vecs = wp512_tv_template,
2187                                 .count = WP512_TEST_VECTORS
2188                         }
2189                 }
2190         }, {
2191                 .alg = "xcbc(aes)",
2192                 .test = alg_test_hash,
2193                 .suite = {
2194                         .hash = {
2195                                 .vecs = aes_xcbc128_tv_template,
2196                                 .count = XCBC_AES_TEST_VECTORS
2197                         }
2198                 }
2199         }, {
2200                 .alg = "xts(aes)",
2201                 .test = alg_test_skcipher,
2202                 .suite = {
2203                         .cipher = {
2204                                 .enc = {
2205                                         .vecs = aes_xts_enc_tv_template,
2206                                         .count = AES_XTS_ENC_TEST_VECTORS
2207                                 },
2208                                 .dec = {
2209                                         .vecs = aes_xts_dec_tv_template,
2210                                         .count = AES_XTS_DEC_TEST_VECTORS
2211                                 }
2212                         }
2213                 }
2214         }, {
2215                 .alg = "zlib",
2216                 .test = alg_test_pcomp,
2217                 .suite = {
2218                         .pcomp = {
2219                                 .comp = {
2220                                         .vecs = zlib_comp_tv_template,
2221                                         .count = ZLIB_COMP_TEST_VECTORS
2222                                 },
2223                                 .decomp = {
2224                                         .vecs = zlib_decomp_tv_template,
2225                                         .count = ZLIB_DECOMP_TEST_VECTORS
2226                                 }
2227                         }
2228                 }
2229         }
2230 };
2231
2232 static int alg_find_test(const char *alg)
2233 {
2234         int start = 0;
2235         int end = ARRAY_SIZE(alg_test_descs);
2236
2237         while (start < end) {
2238                 int i = (start + end) / 2;
2239                 int diff = strcmp(alg_test_descs[i].alg, alg);
2240
2241                 if (diff > 0) {
2242                         end = i;
2243                         continue;
2244                 }
2245
2246                 if (diff < 0) {
2247                         start = i + 1;
2248                         continue;
2249                 }
2250
2251                 return i;
2252         }
2253
2254         return -1;
2255 }
2256
2257 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
2258 {
2259         int i;
2260         int rc;
2261
2262         if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
2263                 char nalg[CRYPTO_MAX_ALG_NAME];
2264
2265                 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
2266                     sizeof(nalg))
2267                         return -ENAMETOOLONG;
2268
2269                 i = alg_find_test(nalg);
2270                 if (i < 0)
2271                         goto notest;
2272
2273                 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
2274                 goto test_done;
2275         }
2276
2277         i = alg_find_test(alg);
2278         if (i < 0)
2279                 goto notest;
2280
2281         rc = alg_test_descs[i].test(alg_test_descs + i, driver,
2282                                       type, mask);
2283 test_done:
2284         if (fips_enabled && rc)
2285                 panic("%s: %s alg self test failed in fips mode!\n", driver, alg);
2286
2287         if (fips_enabled && !rc)
2288                 printk(KERN_INFO "alg: self-tests for %s (%s) passed\n",
2289                        driver, alg);
2290
2291         return rc;
2292
2293 notest:
2294         printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
2295         return 0;
2296 }
2297 EXPORT_SYMBOL_GPL(alg_test);