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