[CRYPTO] tcrypt: Add aead support
[linux-3.10.git] / crypto / tcrypt.c
1 /*
2  * Quick & dirty crypto testing module.
3  *
4  * This will only exist until we have a better testing mechanism
5  * (e.g. a char device).
6  *
7  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
8  * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
9  * Copyright (c) 2007 Nokia Siemens Networks
10  *
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU General Public License as published by the Free
13  * Software Foundation; either version 2 of the License, or (at your option)
14  * any later version.
15  *
16  * 2007-11-13 Added AEAD support
17  * 2007-11-06 Added SHA-224 and SHA-224-HMAC tests
18  * 2006-12-07 Added SHA384 HMAC and SHA512 HMAC tests
19  * 2004-08-09 Added cipher speed tests (Reyk Floeter <reyk@vantronix.net>)
20  * 2003-09-14 Rewritten by Kartikey Mahendra Bhatt
21  *
22  */
23
24 #include <linux/err.h>
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/mm.h>
28 #include <linux/slab.h>
29 #include <linux/scatterlist.h>
30 #include <linux/string.h>
31 #include <linux/crypto.h>
32 #include <linux/highmem.h>
33 #include <linux/moduleparam.h>
34 #include <linux/jiffies.h>
35 #include <linux/timex.h>
36 #include <linux/interrupt.h>
37 #include "tcrypt.h"
38
39 /*
40  * Need to kmalloc() memory for testing kmap().
41  */
42 #define TVMEMSIZE       16384
43 #define XBUFSIZE        32768
44
45 /*
46  * Indexes into the xbuf to simulate cross-page access.
47  */
48 #define IDX1            37
49 #define IDX2            32400
50 #define IDX3            1
51 #define IDX4            8193
52 #define IDX5            22222
53 #define IDX6            17101
54 #define IDX7            27333
55 #define IDX8            3000
56
57 /*
58 * Used by test_cipher()
59 */
60 #define ENCRYPT 1
61 #define DECRYPT 0
62
63 struct tcrypt_result {
64         struct completion completion;
65         int err;
66 };
67
68 static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
69
70 /*
71  * Used by test_cipher_speed()
72  */
73 static unsigned int sec;
74
75 static int mode;
76 static char *xbuf;
77 static char *axbuf;
78 static char *tvmem;
79
80 static char *check[] = {
81         "des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256",
82         "blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
83         "cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
84         "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
85         "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta",  "fcrypt",
86         "camellia", "seed", "salsa20", NULL
87 };
88
89 static void hexdump(unsigned char *buf, unsigned int len)
90 {
91         while (len--)
92                 printk("%02x", *buf++);
93
94         printk("\n");
95 }
96
97 static void tcrypt_complete(struct crypto_async_request *req, int err)
98 {
99         struct tcrypt_result *res = req->data;
100
101         if (err == -EINPROGRESS)
102                 return;
103
104         res->err = err;
105         complete(&res->completion);
106 }
107
108 static void test_hash(char *algo, struct hash_testvec *template,
109                       unsigned int tcount)
110 {
111         unsigned int i, j, k, temp;
112         struct scatterlist sg[8];
113         char result[64];
114         struct crypto_hash *tfm;
115         struct hash_desc desc;
116         struct hash_testvec *hash_tv;
117         unsigned int tsize;
118         int ret;
119
120         printk("\ntesting %s\n", algo);
121
122         tsize = sizeof(struct hash_testvec);
123         tsize *= tcount;
124
125         if (tsize > TVMEMSIZE) {
126                 printk("template (%u) too big for tvmem (%u)\n", tsize, TVMEMSIZE);
127                 return;
128         }
129
130         memcpy(tvmem, template, tsize);
131         hash_tv = (void *)tvmem;
132
133         tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
134         if (IS_ERR(tfm)) {
135                 printk("failed to load transform for %s: %ld\n", algo,
136                        PTR_ERR(tfm));
137                 return;
138         }
139
140         desc.tfm = tfm;
141         desc.flags = 0;
142
143         for (i = 0; i < tcount; i++) {
144                 printk("test %u:\n", i + 1);
145                 memset(result, 0, 64);
146
147                 sg_init_one(&sg[0], hash_tv[i].plaintext, hash_tv[i].psize);
148
149                 if (hash_tv[i].ksize) {
150                         ret = crypto_hash_setkey(tfm, hash_tv[i].key,
151                                                  hash_tv[i].ksize);
152                         if (ret) {
153                                 printk("setkey() failed ret=%d\n", ret);
154                                 goto out;
155                         }
156                 }
157
158                 ret = crypto_hash_digest(&desc, sg, hash_tv[i].psize, result);
159                 if (ret) {
160                         printk("digest () failed ret=%d\n", ret);
161                         goto out;
162                 }
163
164                 hexdump(result, crypto_hash_digestsize(tfm));
165                 printk("%s\n",
166                        memcmp(result, hash_tv[i].digest,
167                               crypto_hash_digestsize(tfm)) ?
168                        "fail" : "pass");
169         }
170
171         printk("testing %s across pages\n", algo);
172
173         /* setup the dummy buffer first */
174         memset(xbuf, 0, XBUFSIZE);
175         memset(axbuf, 0, XBUFSIZE);
176
177         j = 0;
178         for (i = 0; i < tcount; i++) {
179                 if (hash_tv[i].np) {
180                         j++;
181                         printk("test %u:\n", j);
182                         memset(result, 0, 64);
183
184                         temp = 0;
185                         sg_init_table(sg, hash_tv[i].np);
186                         for (k = 0; k < hash_tv[i].np; k++) {
187                                 memcpy(&xbuf[IDX[k]],
188                                        hash_tv[i].plaintext + temp,
189                                        hash_tv[i].tap[k]);
190                                 temp += hash_tv[i].tap[k];
191                                 sg_set_buf(&sg[k], &xbuf[IDX[k]],
192                                             hash_tv[i].tap[k]);
193                         }
194
195                         if (hash_tv[i].ksize) {
196                                 ret = crypto_hash_setkey(tfm, hash_tv[i].key,
197                                                          hash_tv[i].ksize);
198
199                                 if (ret) {
200                                         printk("setkey() failed ret=%d\n", ret);
201                                         goto out;
202                                 }
203                         }
204
205                         ret = crypto_hash_digest(&desc, sg, hash_tv[i].psize,
206                                                  result);
207                         if (ret) {
208                                 printk("digest () failed ret=%d\n", ret);
209                                 goto out;
210                         }
211
212                         hexdump(result, crypto_hash_digestsize(tfm));
213                         printk("%s\n",
214                                memcmp(result, hash_tv[i].digest,
215                                       crypto_hash_digestsize(tfm)) ?
216                                "fail" : "pass");
217                 }
218         }
219
220 out:
221         crypto_free_hash(tfm);
222 }
223
224 static void test_aead(char *algo, int enc, struct aead_testvec *template,
225                       unsigned int tcount)
226 {
227         unsigned int ret, i, j, k, temp;
228         unsigned int tsize;
229         char *q;
230         struct crypto_aead *tfm;
231         char *key;
232         struct aead_testvec *aead_tv;
233         struct aead_request *req;
234         struct scatterlist sg[8];
235         struct scatterlist asg[8];
236         const char *e;
237         struct tcrypt_result result;
238
239         if (enc == ENCRYPT)
240                 e = "encryption";
241         else
242                 e = "decryption";
243
244         printk(KERN_INFO "\ntesting %s %s\n", algo, e);
245
246         tsize = sizeof(struct aead_testvec);
247         tsize *= tcount;
248
249         if (tsize > TVMEMSIZE) {
250                 printk(KERN_INFO "template (%u) too big for tvmem (%u)\n",
251                        tsize, TVMEMSIZE);
252                 return;
253         }
254
255         memcpy(tvmem, template, tsize);
256         aead_tv = (void *)tvmem;
257
258         init_completion(&result.completion);
259
260         tfm = crypto_alloc_aead(algo, 0, 0);
261
262         if (IS_ERR(tfm)) {
263                 printk(KERN_INFO "failed to load transform for %s: %ld\n",
264                        algo, PTR_ERR(tfm));
265                 return;
266         }
267
268         req = aead_request_alloc(tfm, GFP_KERNEL);
269         if (!req) {
270                 printk(KERN_INFO "failed to allocate request for %s\n", algo);
271                 goto out;
272         }
273
274         aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
275                                   tcrypt_complete, &result);
276
277         for (i = 0, j = 0; i < tcount; i++) {
278                 if (!aead_tv[i].np) {
279                         printk(KERN_INFO "test %u (%d bit key):\n",
280                                ++j, aead_tv[i].klen * 8);
281
282                         crypto_aead_clear_flags(tfm, ~0);
283                         if (aead_tv[i].wk)
284                                 crypto_aead_set_flags(
285                                         tfm, CRYPTO_TFM_REQ_WEAK_KEY);
286                         key = aead_tv[i].key;
287
288                         ret = crypto_aead_setkey(tfm, key,
289                                                  aead_tv[i].klen);
290                         if (ret) {
291                                 printk(KERN_INFO "setkey() failed flags=%x\n",
292                                        crypto_aead_get_flags(tfm));
293
294                                 if (!aead_tv[i].fail)
295                                         goto out;
296                         }
297
298                         sg_init_one(&sg[0], aead_tv[i].input,
299                                     aead_tv[i].ilen);
300
301                         sg_init_one(&asg[0], aead_tv[i].assoc,
302                                     aead_tv[i].alen);
303
304                         aead_request_set_crypt(req, sg, sg,
305                                                aead_tv[i].ilen,
306                                                aead_tv[i].iv);
307
308                         aead_request_set_assoc(req, asg, aead_tv[i].alen);
309
310                         if (enc) {
311                                 ret = crypto_aead_encrypt(req);
312                         } else {
313                                 memcpy(req->__ctx, aead_tv[i].tag,
314                                        aead_tv[i].tlen);
315                                 ret = crypto_aead_decrypt(req);
316                         }
317
318                         switch (ret) {
319                         case 0:
320                                 break;
321                         case -EINPROGRESS:
322                         case -EBUSY:
323                                 ret = wait_for_completion_interruptible(
324                                         &result.completion);
325                                 if (!ret && !(ret = result.err)) {
326                                         INIT_COMPLETION(result.completion);
327                                         break;
328                                 }
329                                 /* fall through */
330                         default:
331                                 printk(KERN_INFO "%s () failed err=%d\n",
332                                        e, -ret);
333                                 goto out;
334                         }
335
336                         q = kmap(sg_page(&sg[0])) + sg[0].offset;
337                         hexdump(q, aead_tv[i].rlen);
338                         printk(KERN_INFO "auth tag: ");
339                         hexdump((unsigned char *)req->__ctx, aead_tv[i].tlen);
340
341                         printk(KERN_INFO "enc/dec: %s\n",
342                                memcmp(q, aead_tv[i].result,
343                                       aead_tv[i].rlen) ? "fail" : "pass");
344
345                         printk(KERN_INFO "auth tag: %s\n",
346                                memcmp(req->__ctx, aead_tv[i].tag,
347                                       aead_tv[i].tlen) ? "fail" : "pass");
348                 }
349         }
350
351         printk(KERN_INFO "\ntesting %s %s across pages (chunking)\n", algo, e);
352         memset(xbuf, 0, XBUFSIZE);
353
354         for (i = 0, j = 0; i < tcount; i++) {
355                 if (aead_tv[i].np) {
356                         printk(KERN_INFO "test %u (%d bit key):\n",
357                                ++j, aead_tv[i].klen * 8);
358
359                         crypto_aead_clear_flags(tfm, ~0);
360                         if (aead_tv[i].wk)
361                                 crypto_aead_set_flags(
362                                         tfm, CRYPTO_TFM_REQ_WEAK_KEY);
363                         key = aead_tv[i].key;
364
365                         ret = crypto_aead_setkey(tfm, key, aead_tv[i].klen);
366                         if (ret) {
367                                 printk(KERN_INFO "setkey() failed flags=%x\n",
368                                        crypto_aead_get_flags(tfm));
369
370                                 if (!aead_tv[i].fail)
371                                         goto out;
372                         }
373
374                         sg_init_table(sg, aead_tv[i].np);
375                         for (k = 0, temp = 0; k < aead_tv[i].np; k++) {
376                                 memcpy(&xbuf[IDX[k]],
377                                        aead_tv[i].input + temp,
378                                        aead_tv[i].tap[k]);
379                                 temp += aead_tv[i].tap[k];
380                                 sg_set_buf(&sg[k], &xbuf[IDX[k]],
381                                            aead_tv[i].tap[k]);
382                         }
383
384                         sg_init_table(asg, aead_tv[i].anp);
385                         for (k = 0, temp = 0; k < aead_tv[i].anp; k++) {
386                                 memcpy(&axbuf[IDX[k]],
387                                        aead_tv[i].assoc + temp,
388                                        aead_tv[i].atap[k]);
389                                 temp += aead_tv[i].atap[k];
390                                 sg_set_buf(&asg[k], &axbuf[IDX[k]],
391                                            aead_tv[i].atap[k]);
392                         }
393
394                         aead_request_set_crypt(req, sg, sg,
395                                                aead_tv[i].ilen,
396                                                aead_tv[i].iv);
397
398                         aead_request_set_assoc(req, asg, aead_tv[i].alen);
399
400                         if (enc) {
401                                 ret = crypto_aead_encrypt(req);
402                         } else {
403                                 memcpy(req->__ctx, aead_tv[i].tag,
404                                        aead_tv[i].tlen);
405                                 ret = crypto_aead_decrypt(req);
406                         }
407
408                         switch (ret) {
409                         case 0:
410                                 break;
411                         case -EINPROGRESS:
412                         case -EBUSY:
413                                 ret = wait_for_completion_interruptible(
414                                         &result.completion);
415                                 if (!ret && !(ret = result.err)) {
416                                         INIT_COMPLETION(result.completion);
417                                         break;
418                                 }
419                                 /* fall through */
420                         default:
421                                 printk(KERN_INFO "%s () failed err=%d\n",
422                                        e, -ret);
423                                 goto out;
424                         }
425
426                         for (k = 0, temp = 0; k < aead_tv[i].np; k++) {
427                                 printk(KERN_INFO "page %u\n", k);
428                                 q = kmap(sg_page(&sg[k])) + sg[k].offset;
429                                 hexdump(q, aead_tv[i].tap[k]);
430                                 printk(KERN_INFO "%s\n",
431                                        memcmp(q, aead_tv[i].result + temp,
432                                               aead_tv[i].tap[k]) ?
433                                        "fail" : "pass");
434
435                                 temp += aead_tv[i].tap[k];
436                         }
437                         printk(KERN_INFO "auth tag: ");
438                         hexdump((unsigned char *)req->__ctx, aead_tv[i].tlen);
439
440                         printk(KERN_INFO "auth tag: %s\n",
441                                memcmp(req->__ctx, aead_tv[i].tag,
442                                       aead_tv[i].tlen) ? "fail" : "pass");
443                 }
444         }
445
446 out:
447         crypto_free_aead(tfm);
448         aead_request_free(req);
449 }
450
451 static void test_cipher(char *algo, int enc,
452                         struct cipher_testvec *template, unsigned int tcount)
453 {
454         unsigned int ret, i, j, k, temp;
455         unsigned int tsize;
456         char *q;
457         struct crypto_ablkcipher *tfm;
458         char *key;
459         struct cipher_testvec *cipher_tv;
460         struct ablkcipher_request *req;
461         struct scatterlist sg[8];
462         const char *e;
463         struct tcrypt_result result;
464
465         if (enc == ENCRYPT)
466                 e = "encryption";
467         else
468                 e = "decryption";
469
470         printk("\ntesting %s %s\n", algo, e);
471
472         tsize = sizeof (struct cipher_testvec);
473         tsize *= tcount;
474
475         if (tsize > TVMEMSIZE) {
476                 printk("template (%u) too big for tvmem (%u)\n", tsize,
477                        TVMEMSIZE);
478                 return;
479         }
480
481         memcpy(tvmem, template, tsize);
482         cipher_tv = (void *)tvmem;
483
484         init_completion(&result.completion);
485
486         tfm = crypto_alloc_ablkcipher(algo, 0, 0);
487
488         if (IS_ERR(tfm)) {
489                 printk("failed to load transform for %s: %ld\n", algo,
490                        PTR_ERR(tfm));
491                 return;
492         }
493
494         req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
495         if (!req) {
496                 printk("failed to allocate request for %s\n", algo);
497                 goto out;
498         }
499
500         ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
501                                         tcrypt_complete, &result);
502
503         j = 0;
504         for (i = 0; i < tcount; i++) {
505                 if (!(cipher_tv[i].np)) {
506                         j++;
507                         printk("test %u (%d bit key):\n",
508                         j, cipher_tv[i].klen * 8);
509
510                         crypto_ablkcipher_clear_flags(tfm, ~0);
511                         if (cipher_tv[i].wk)
512                                 crypto_ablkcipher_set_flags(
513                                         tfm, CRYPTO_TFM_REQ_WEAK_KEY);
514                         key = cipher_tv[i].key;
515
516                         ret = crypto_ablkcipher_setkey(tfm, key,
517                                                        cipher_tv[i].klen);
518                         if (ret) {
519                                 printk("setkey() failed flags=%x\n",
520                                        crypto_ablkcipher_get_flags(tfm));
521
522                                 if (!cipher_tv[i].fail)
523                                         goto out;
524                         }
525
526                         sg_init_one(&sg[0], cipher_tv[i].input,
527                                     cipher_tv[i].ilen);
528
529                         ablkcipher_request_set_crypt(req, sg, sg,
530                                                      cipher_tv[i].ilen,
531                                                      cipher_tv[i].iv);
532
533                         ret = enc ?
534                                 crypto_ablkcipher_encrypt(req) :
535                                 crypto_ablkcipher_decrypt(req);
536
537                         switch (ret) {
538                         case 0:
539                                 break;
540                         case -EINPROGRESS:
541                         case -EBUSY:
542                                 ret = wait_for_completion_interruptible(
543                                         &result.completion);
544                                 if (!ret && !((ret = result.err))) {
545                                         INIT_COMPLETION(result.completion);
546                                         break;
547                                 }
548                                 /* fall through */
549                         default:
550                                 printk("%s () failed err=%d\n", e, -ret);
551                                 goto out;
552                         }
553
554                         q = kmap(sg_page(&sg[0])) + sg[0].offset;
555                         hexdump(q, cipher_tv[i].rlen);
556
557                         printk("%s\n",
558                                memcmp(q, cipher_tv[i].result,
559                                       cipher_tv[i].rlen) ? "fail" : "pass");
560                 }
561         }
562
563         printk("\ntesting %s %s across pages (chunking)\n", algo, e);
564         memset(xbuf, 0, XBUFSIZE);
565
566         j = 0;
567         for (i = 0; i < tcount; i++) {
568                 if (cipher_tv[i].np) {
569                         j++;
570                         printk("test %u (%d bit key):\n",
571                         j, cipher_tv[i].klen * 8);
572
573                         crypto_ablkcipher_clear_flags(tfm, ~0);
574                         if (cipher_tv[i].wk)
575                                 crypto_ablkcipher_set_flags(
576                                         tfm, CRYPTO_TFM_REQ_WEAK_KEY);
577                         key = cipher_tv[i].key;
578
579                         ret = crypto_ablkcipher_setkey(tfm, key,
580                                                        cipher_tv[i].klen);
581                         if (ret) {
582                                 printk("setkey() failed flags=%x\n",
583                                        crypto_ablkcipher_get_flags(tfm));
584
585                                 if (!cipher_tv[i].fail)
586                                         goto out;
587                         }
588
589                         temp = 0;
590                         sg_init_table(sg, cipher_tv[i].np);
591                         for (k = 0; k < cipher_tv[i].np; k++) {
592                                 memcpy(&xbuf[IDX[k]],
593                                        cipher_tv[i].input + temp,
594                                        cipher_tv[i].tap[k]);
595                                 temp += cipher_tv[i].tap[k];
596                                 sg_set_buf(&sg[k], &xbuf[IDX[k]],
597                                            cipher_tv[i].tap[k]);
598                         }
599
600                         ablkcipher_request_set_crypt(req, sg, sg,
601                                                      cipher_tv[i].ilen,
602                                                      cipher_tv[i].iv);
603
604                         ret = enc ?
605                                 crypto_ablkcipher_encrypt(req) :
606                                 crypto_ablkcipher_decrypt(req);
607
608                         switch (ret) {
609                         case 0:
610                                 break;
611                         case -EINPROGRESS:
612                         case -EBUSY:
613                                 ret = wait_for_completion_interruptible(
614                                         &result.completion);
615                                 if (!ret && !((ret = result.err))) {
616                                         INIT_COMPLETION(result.completion);
617                                         break;
618                                 }
619                                 /* fall through */
620                         default:
621                                 printk("%s () failed err=%d\n", e, -ret);
622                                 goto out;
623                         }
624
625                         temp = 0;
626                         for (k = 0; k < cipher_tv[i].np; k++) {
627                                 printk("page %u\n", k);
628                                 q = kmap(sg_page(&sg[k])) + sg[k].offset;
629                                 hexdump(q, cipher_tv[i].tap[k]);
630                                 printk("%s\n",
631                                         memcmp(q, cipher_tv[i].result + temp,
632                                                 cipher_tv[i].tap[k]) ? "fail" :
633                                         "pass");
634                                 temp += cipher_tv[i].tap[k];
635                         }
636                 }
637         }
638
639 out:
640         crypto_free_ablkcipher(tfm);
641         ablkcipher_request_free(req);
642 }
643
644 static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc, char *p,
645                                int blen, int sec)
646 {
647         struct scatterlist sg[1];
648         unsigned long start, end;
649         int bcount;
650         int ret;
651
652         sg_init_one(sg, p, blen);
653
654         for (start = jiffies, end = start + sec * HZ, bcount = 0;
655              time_before(jiffies, end); bcount++) {
656                 if (enc)
657                         ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
658                 else
659                         ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
660
661                 if (ret)
662                         return ret;
663         }
664
665         printk("%d operations in %d seconds (%ld bytes)\n",
666                bcount, sec, (long)bcount * blen);
667         return 0;
668 }
669
670 static int test_cipher_cycles(struct blkcipher_desc *desc, int enc, char *p,
671                               int blen)
672 {
673         struct scatterlist sg[1];
674         unsigned long cycles = 0;
675         int ret = 0;
676         int i;
677
678         sg_init_one(sg, p, blen);
679
680         local_bh_disable();
681         local_irq_disable();
682
683         /* Warm-up run. */
684         for (i = 0; i < 4; i++) {
685                 if (enc)
686                         ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
687                 else
688                         ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
689
690                 if (ret)
691                         goto out;
692         }
693
694         /* The real thing. */
695         for (i = 0; i < 8; i++) {
696                 cycles_t start, end;
697
698                 start = get_cycles();
699                 if (enc)
700                         ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
701                 else
702                         ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
703                 end = get_cycles();
704
705                 if (ret)
706                         goto out;
707
708                 cycles += end - start;
709         }
710
711 out:
712         local_irq_enable();
713         local_bh_enable();
714
715         if (ret == 0)
716                 printk("1 operation in %lu cycles (%d bytes)\n",
717                        (cycles + 4) / 8, blen);
718
719         return ret;
720 }
721
722 static void test_cipher_speed(char *algo, int enc, unsigned int sec,
723                               struct cipher_testvec *template,
724                               unsigned int tcount, struct cipher_speed *speed)
725 {
726         unsigned int ret, i, j, iv_len;
727         unsigned char *key, *p, iv[128];
728         struct crypto_blkcipher *tfm;
729         struct blkcipher_desc desc;
730         const char *e;
731
732         if (enc == ENCRYPT)
733                 e = "encryption";
734         else
735                 e = "decryption";
736
737         printk("\ntesting speed of %s %s\n", algo, e);
738
739         tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);
740
741         if (IS_ERR(tfm)) {
742                 printk("failed to load transform for %s: %ld\n", algo,
743                        PTR_ERR(tfm));
744                 return;
745         }
746         desc.tfm = tfm;
747         desc.flags = 0;
748
749         for (i = 0; speed[i].klen != 0; i++) {
750                 if ((speed[i].blen + speed[i].klen) > TVMEMSIZE) {
751                         printk("template (%u) too big for tvmem (%u)\n",
752                                speed[i].blen + speed[i].klen, TVMEMSIZE);
753                         goto out;
754                 }
755
756                 printk("test %u (%d bit key, %d byte blocks): ", i,
757                        speed[i].klen * 8, speed[i].blen);
758
759                 memset(tvmem, 0xff, speed[i].klen + speed[i].blen);
760
761                 /* set key, plain text and IV */
762                 key = (unsigned char *)tvmem;
763                 for (j = 0; j < tcount; j++) {
764                         if (template[j].klen == speed[i].klen) {
765                                 key = template[j].key;
766                                 break;
767                         }
768                 }
769                 p = (unsigned char *)tvmem + speed[i].klen;
770
771                 ret = crypto_blkcipher_setkey(tfm, key, speed[i].klen);
772                 if (ret) {
773                         printk("setkey() failed flags=%x\n",
774                                crypto_blkcipher_get_flags(tfm));
775                         goto out;
776                 }
777
778                 iv_len = crypto_blkcipher_ivsize(tfm);
779                 if (iv_len) {
780                         memset(&iv, 0xff, iv_len);
781                         crypto_blkcipher_set_iv(tfm, iv, iv_len);
782                 }
783
784                 if (sec)
785                         ret = test_cipher_jiffies(&desc, enc, p, speed[i].blen,
786                                                   sec);
787                 else
788                         ret = test_cipher_cycles(&desc, enc, p, speed[i].blen);
789
790                 if (ret) {
791                         printk("%s() failed flags=%x\n", e, desc.flags);
792                         break;
793                 }
794         }
795
796 out:
797         crypto_free_blkcipher(tfm);
798 }
799
800 static int test_hash_jiffies_digest(struct hash_desc *desc, char *p, int blen,
801                                     char *out, int sec)
802 {
803         struct scatterlist sg[1];
804         unsigned long start, end;
805         int bcount;
806         int ret;
807
808         sg_init_table(sg, 1);
809
810         for (start = jiffies, end = start + sec * HZ, bcount = 0;
811              time_before(jiffies, end); bcount++) {
812                 sg_set_buf(sg, p, blen);
813                 ret = crypto_hash_digest(desc, sg, blen, out);
814                 if (ret)
815                         return ret;
816         }
817
818         printk("%6u opers/sec, %9lu bytes/sec\n",
819                bcount / sec, ((long)bcount * blen) / sec);
820
821         return 0;
822 }
823
824 static int test_hash_jiffies(struct hash_desc *desc, char *p, int blen,
825                              int plen, char *out, int sec)
826 {
827         struct scatterlist sg[1];
828         unsigned long start, end;
829         int bcount, pcount;
830         int ret;
831
832         if (plen == blen)
833                 return test_hash_jiffies_digest(desc, p, blen, out, sec);
834
835         sg_init_table(sg, 1);
836
837         for (start = jiffies, end = start + sec * HZ, bcount = 0;
838              time_before(jiffies, end); bcount++) {
839                 ret = crypto_hash_init(desc);
840                 if (ret)
841                         return ret;
842                 for (pcount = 0; pcount < blen; pcount += plen) {
843                         sg_set_buf(sg, p + pcount, plen);
844                         ret = crypto_hash_update(desc, sg, plen);
845                         if (ret)
846                                 return ret;
847                 }
848                 /* we assume there is enough space in 'out' for the result */
849                 ret = crypto_hash_final(desc, out);
850                 if (ret)
851                         return ret;
852         }
853
854         printk("%6u opers/sec, %9lu bytes/sec\n",
855                bcount / sec, ((long)bcount * blen) / sec);
856
857         return 0;
858 }
859
860 static int test_hash_cycles_digest(struct hash_desc *desc, char *p, int blen,
861                                    char *out)
862 {
863         struct scatterlist sg[1];
864         unsigned long cycles = 0;
865         int i;
866         int ret;
867
868         sg_init_table(sg, 1);
869
870         local_bh_disable();
871         local_irq_disable();
872
873         /* Warm-up run. */
874         for (i = 0; i < 4; i++) {
875                 sg_set_buf(sg, p, blen);
876                 ret = crypto_hash_digest(desc, sg, blen, out);
877                 if (ret)
878                         goto out;
879         }
880
881         /* The real thing. */
882         for (i = 0; i < 8; i++) {
883                 cycles_t start, end;
884
885                 start = get_cycles();
886
887                 sg_set_buf(sg, p, blen);
888                 ret = crypto_hash_digest(desc, sg, blen, out);
889                 if (ret)
890                         goto out;
891
892                 end = get_cycles();
893
894                 cycles += end - start;
895         }
896
897 out:
898         local_irq_enable();
899         local_bh_enable();
900
901         if (ret)
902                 return ret;
903
904         printk("%6lu cycles/operation, %4lu cycles/byte\n",
905                cycles / 8, cycles / (8 * blen));
906
907         return 0;
908 }
909
910 static int test_hash_cycles(struct hash_desc *desc, char *p, int blen,
911                             int plen, char *out)
912 {
913         struct scatterlist sg[1];
914         unsigned long cycles = 0;
915         int i, pcount;
916         int ret;
917
918         if (plen == blen)
919                 return test_hash_cycles_digest(desc, p, blen, out);
920
921         sg_init_table(sg, 1);
922
923         local_bh_disable();
924         local_irq_disable();
925
926         /* Warm-up run. */
927         for (i = 0; i < 4; i++) {
928                 ret = crypto_hash_init(desc);
929                 if (ret)
930                         goto out;
931                 for (pcount = 0; pcount < blen; pcount += plen) {
932                         sg_set_buf(sg, p + pcount, plen);
933                         ret = crypto_hash_update(desc, sg, plen);
934                         if (ret)
935                                 goto out;
936                 }
937                 ret = crypto_hash_final(desc, out);
938                 if (ret)
939                         goto out;
940         }
941
942         /* The real thing. */
943         for (i = 0; i < 8; i++) {
944                 cycles_t start, end;
945
946                 start = get_cycles();
947
948                 ret = crypto_hash_init(desc);
949                 if (ret)
950                         goto out;
951                 for (pcount = 0; pcount < blen; pcount += plen) {
952                         sg_set_buf(sg, p + pcount, plen);
953                         ret = crypto_hash_update(desc, sg, plen);
954                         if (ret)
955                                 goto out;
956                 }
957                 ret = crypto_hash_final(desc, out);
958                 if (ret)
959                         goto out;
960
961                 end = get_cycles();
962
963                 cycles += end - start;
964         }
965
966 out:
967         local_irq_enable();
968         local_bh_enable();
969
970         if (ret)
971                 return ret;
972
973         printk("%6lu cycles/operation, %4lu cycles/byte\n",
974                cycles / 8, cycles / (8 * blen));
975
976         return 0;
977 }
978
979 static void test_hash_speed(char *algo, unsigned int sec,
980                               struct hash_speed *speed)
981 {
982         struct crypto_hash *tfm;
983         struct hash_desc desc;
984         char output[1024];
985         int i;
986         int ret;
987
988         printk("\ntesting speed of %s\n", algo);
989
990         tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
991
992         if (IS_ERR(tfm)) {
993                 printk("failed to load transform for %s: %ld\n", algo,
994                        PTR_ERR(tfm));
995                 return;
996         }
997
998         desc.tfm = tfm;
999         desc.flags = 0;
1000
1001         if (crypto_hash_digestsize(tfm) > sizeof(output)) {
1002                 printk("digestsize(%u) > outputbuffer(%zu)\n",
1003                        crypto_hash_digestsize(tfm), sizeof(output));
1004                 goto out;
1005         }
1006
1007         for (i = 0; speed[i].blen != 0; i++) {
1008                 if (speed[i].blen > TVMEMSIZE) {
1009                         printk("template (%u) too big for tvmem (%u)\n",
1010                                speed[i].blen, TVMEMSIZE);
1011                         goto out;
1012                 }
1013
1014                 printk("test%3u (%5u byte blocks,%5u bytes per update,%4u updates): ",
1015                        i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
1016
1017                 memset(tvmem, 0xff, speed[i].blen);
1018
1019                 if (sec)
1020                         ret = test_hash_jiffies(&desc, tvmem, speed[i].blen,
1021                                                 speed[i].plen, output, sec);
1022                 else
1023                         ret = test_hash_cycles(&desc, tvmem, speed[i].blen,
1024                                                speed[i].plen, output);
1025
1026                 if (ret) {
1027                         printk("hashing failed ret=%d\n", ret);
1028                         break;
1029                 }
1030         }
1031
1032 out:
1033         crypto_free_hash(tfm);
1034 }
1035
1036 static void test_deflate(void)
1037 {
1038         unsigned int i;
1039         char result[COMP_BUF_SIZE];
1040         struct crypto_comp *tfm;
1041         struct comp_testvec *tv;
1042         unsigned int tsize;
1043
1044         printk("\ntesting deflate compression\n");
1045
1046         tsize = sizeof (deflate_comp_tv_template);
1047         if (tsize > TVMEMSIZE) {
1048                 printk("template (%u) too big for tvmem (%u)\n", tsize,
1049                        TVMEMSIZE);
1050                 return;
1051         }
1052
1053         memcpy(tvmem, deflate_comp_tv_template, tsize);
1054         tv = (void *)tvmem;
1055
1056         tfm = crypto_alloc_comp("deflate", 0, CRYPTO_ALG_ASYNC);
1057         if (IS_ERR(tfm)) {
1058                 printk("failed to load transform for deflate\n");
1059                 return;
1060         }
1061
1062         for (i = 0; i < DEFLATE_COMP_TEST_VECTORS; i++) {
1063                 int ilen, ret, dlen = COMP_BUF_SIZE;
1064
1065                 printk("test %u:\n", i + 1);
1066                 memset(result, 0, sizeof (result));
1067
1068                 ilen = tv[i].inlen;
1069                 ret = crypto_comp_compress(tfm, tv[i].input,
1070                                            ilen, result, &dlen);
1071                 if (ret) {
1072                         printk("fail: ret=%d\n", ret);
1073                         continue;
1074                 }
1075                 hexdump(result, dlen);
1076                 printk("%s (ratio %d:%d)\n",
1077                        memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
1078                        ilen, dlen);
1079         }
1080
1081         printk("\ntesting deflate decompression\n");
1082
1083         tsize = sizeof (deflate_decomp_tv_template);
1084         if (tsize > TVMEMSIZE) {
1085                 printk("template (%u) too big for tvmem (%u)\n", tsize,
1086                        TVMEMSIZE);
1087                 goto out;
1088         }
1089
1090         memcpy(tvmem, deflate_decomp_tv_template, tsize);
1091         tv = (void *)tvmem;
1092
1093         for (i = 0; i < DEFLATE_DECOMP_TEST_VECTORS; i++) {
1094                 int ilen, ret, dlen = COMP_BUF_SIZE;
1095
1096                 printk("test %u:\n", i + 1);
1097                 memset(result, 0, sizeof (result));
1098
1099                 ilen = tv[i].inlen;
1100                 ret = crypto_comp_decompress(tfm, tv[i].input,
1101                                              ilen, result, &dlen);
1102                 if (ret) {
1103                         printk("fail: ret=%d\n", ret);
1104                         continue;
1105                 }
1106                 hexdump(result, dlen);
1107                 printk("%s (ratio %d:%d)\n",
1108                        memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
1109                        ilen, dlen);
1110         }
1111 out:
1112         crypto_free_comp(tfm);
1113 }
1114
1115 static void test_available(void)
1116 {
1117         char **name = check;
1118
1119         while (*name) {
1120                 printk("alg %s ", *name);
1121                 printk(crypto_has_alg(*name, 0, 0) ?
1122                        "found\n" : "not found\n");
1123                 name++;
1124         }
1125 }
1126
1127 static void do_test(void)
1128 {
1129         switch (mode) {
1130
1131         case 0:
1132                 test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
1133
1134                 test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
1135
1136                 //DES
1137                 test_cipher("ecb(des)", ENCRYPT, des_enc_tv_template,
1138                             DES_ENC_TEST_VECTORS);
1139                 test_cipher("ecb(des)", DECRYPT, des_dec_tv_template,
1140                             DES_DEC_TEST_VECTORS);
1141                 test_cipher("cbc(des)", ENCRYPT, des_cbc_enc_tv_template,
1142                             DES_CBC_ENC_TEST_VECTORS);
1143                 test_cipher("cbc(des)", DECRYPT, des_cbc_dec_tv_template,
1144                             DES_CBC_DEC_TEST_VECTORS);
1145
1146                 //DES3_EDE
1147                 test_cipher("ecb(des3_ede)", ENCRYPT, des3_ede_enc_tv_template,
1148                             DES3_EDE_ENC_TEST_VECTORS);
1149                 test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
1150                             DES3_EDE_DEC_TEST_VECTORS);
1151
1152                 test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
1153
1154                 test_hash("sha224", sha224_tv_template, SHA224_TEST_VECTORS);
1155
1156                 test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
1157
1158                 //BLOWFISH
1159                 test_cipher("ecb(blowfish)", ENCRYPT, bf_enc_tv_template,
1160                             BF_ENC_TEST_VECTORS);
1161                 test_cipher("ecb(blowfish)", DECRYPT, bf_dec_tv_template,
1162                             BF_DEC_TEST_VECTORS);
1163                 test_cipher("cbc(blowfish)", ENCRYPT, bf_cbc_enc_tv_template,
1164                             BF_CBC_ENC_TEST_VECTORS);
1165                 test_cipher("cbc(blowfish)", DECRYPT, bf_cbc_dec_tv_template,
1166                             BF_CBC_DEC_TEST_VECTORS);
1167
1168                 //TWOFISH
1169                 test_cipher("ecb(twofish)", ENCRYPT, tf_enc_tv_template,
1170                             TF_ENC_TEST_VECTORS);
1171                 test_cipher("ecb(twofish)", DECRYPT, tf_dec_tv_template,
1172                             TF_DEC_TEST_VECTORS);
1173                 test_cipher("cbc(twofish)", ENCRYPT, tf_cbc_enc_tv_template,
1174                             TF_CBC_ENC_TEST_VECTORS);
1175                 test_cipher("cbc(twofish)", DECRYPT, tf_cbc_dec_tv_template,
1176                             TF_CBC_DEC_TEST_VECTORS);
1177
1178                 //SERPENT
1179                 test_cipher("ecb(serpent)", ENCRYPT, serpent_enc_tv_template,
1180                             SERPENT_ENC_TEST_VECTORS);
1181                 test_cipher("ecb(serpent)", DECRYPT, serpent_dec_tv_template,
1182                             SERPENT_DEC_TEST_VECTORS);
1183
1184                 //TNEPRES
1185                 test_cipher("ecb(tnepres)", ENCRYPT, tnepres_enc_tv_template,
1186                             TNEPRES_ENC_TEST_VECTORS);
1187                 test_cipher("ecb(tnepres)", DECRYPT, tnepres_dec_tv_template,
1188                             TNEPRES_DEC_TEST_VECTORS);
1189
1190                 //AES
1191                 test_cipher("ecb(aes)", ENCRYPT, aes_enc_tv_template,
1192                             AES_ENC_TEST_VECTORS);
1193                 test_cipher("ecb(aes)", DECRYPT, aes_dec_tv_template,
1194                             AES_DEC_TEST_VECTORS);
1195                 test_cipher("cbc(aes)", ENCRYPT, aes_cbc_enc_tv_template,
1196                             AES_CBC_ENC_TEST_VECTORS);
1197                 test_cipher("cbc(aes)", DECRYPT, aes_cbc_dec_tv_template,
1198                             AES_CBC_DEC_TEST_VECTORS);
1199                 test_cipher("lrw(aes)", ENCRYPT, aes_lrw_enc_tv_template,
1200                             AES_LRW_ENC_TEST_VECTORS);
1201                 test_cipher("lrw(aes)", DECRYPT, aes_lrw_dec_tv_template,
1202                             AES_LRW_DEC_TEST_VECTORS);
1203                 test_cipher("xts(aes)", ENCRYPT, aes_xts_enc_tv_template,
1204                             AES_XTS_ENC_TEST_VECTORS);
1205                 test_cipher("xts(aes)", DECRYPT, aes_xts_dec_tv_template,
1206                             AES_XTS_DEC_TEST_VECTORS);
1207                 test_cipher("ctr(aes,4,8,4)", ENCRYPT, aes_ctr_enc_tv_template,
1208                             AES_CTR_ENC_TEST_VECTORS);
1209                 test_cipher("ctr(aes,4,8,4)", DECRYPT, aes_ctr_dec_tv_template,
1210                             AES_CTR_DEC_TEST_VECTORS);
1211
1212                 //CAST5
1213                 test_cipher("ecb(cast5)", ENCRYPT, cast5_enc_tv_template,
1214                             CAST5_ENC_TEST_VECTORS);
1215                 test_cipher("ecb(cast5)", DECRYPT, cast5_dec_tv_template,
1216                             CAST5_DEC_TEST_VECTORS);
1217
1218                 //CAST6
1219                 test_cipher("ecb(cast6)", ENCRYPT, cast6_enc_tv_template,
1220                             CAST6_ENC_TEST_VECTORS);
1221                 test_cipher("ecb(cast6)", DECRYPT, cast6_dec_tv_template,
1222                             CAST6_DEC_TEST_VECTORS);
1223
1224                 //ARC4
1225                 test_cipher("ecb(arc4)", ENCRYPT, arc4_enc_tv_template,
1226                             ARC4_ENC_TEST_VECTORS);
1227                 test_cipher("ecb(arc4)", DECRYPT, arc4_dec_tv_template,
1228                             ARC4_DEC_TEST_VECTORS);
1229
1230                 //TEA
1231                 test_cipher("ecb(tea)", ENCRYPT, tea_enc_tv_template,
1232                             TEA_ENC_TEST_VECTORS);
1233                 test_cipher("ecb(tea)", DECRYPT, tea_dec_tv_template,
1234                             TEA_DEC_TEST_VECTORS);
1235
1236
1237                 //XTEA
1238                 test_cipher("ecb(xtea)", ENCRYPT, xtea_enc_tv_template,
1239                             XTEA_ENC_TEST_VECTORS);
1240                 test_cipher("ecb(xtea)", DECRYPT, xtea_dec_tv_template,
1241                             XTEA_DEC_TEST_VECTORS);
1242
1243                 //KHAZAD
1244                 test_cipher("ecb(khazad)", ENCRYPT, khazad_enc_tv_template,
1245                             KHAZAD_ENC_TEST_VECTORS);
1246                 test_cipher("ecb(khazad)", DECRYPT, khazad_dec_tv_template,
1247                             KHAZAD_DEC_TEST_VECTORS);
1248
1249                 //ANUBIS
1250                 test_cipher("ecb(anubis)", ENCRYPT, anubis_enc_tv_template,
1251                             ANUBIS_ENC_TEST_VECTORS);
1252                 test_cipher("ecb(anubis)", DECRYPT, anubis_dec_tv_template,
1253                             ANUBIS_DEC_TEST_VECTORS);
1254                 test_cipher("cbc(anubis)", ENCRYPT, anubis_cbc_enc_tv_template,
1255                             ANUBIS_CBC_ENC_TEST_VECTORS);
1256                 test_cipher("cbc(anubis)", DECRYPT, anubis_cbc_dec_tv_template,
1257                             ANUBIS_CBC_ENC_TEST_VECTORS);
1258
1259                 //XETA
1260                 test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
1261                             XETA_ENC_TEST_VECTORS);
1262                 test_cipher("ecb(xeta)", DECRYPT, xeta_dec_tv_template,
1263                             XETA_DEC_TEST_VECTORS);
1264
1265                 //FCrypt
1266                 test_cipher("pcbc(fcrypt)", ENCRYPT, fcrypt_pcbc_enc_tv_template,
1267                             FCRYPT_ENC_TEST_VECTORS);
1268                 test_cipher("pcbc(fcrypt)", DECRYPT, fcrypt_pcbc_dec_tv_template,
1269                             FCRYPT_DEC_TEST_VECTORS);
1270
1271                 //CAMELLIA
1272                 test_cipher("ecb(camellia)", ENCRYPT,
1273                             camellia_enc_tv_template,
1274                             CAMELLIA_ENC_TEST_VECTORS);
1275                 test_cipher("ecb(camellia)", DECRYPT,
1276                             camellia_dec_tv_template,
1277                             CAMELLIA_DEC_TEST_VECTORS);
1278                 test_cipher("cbc(camellia)", ENCRYPT,
1279                             camellia_cbc_enc_tv_template,
1280                             CAMELLIA_CBC_ENC_TEST_VECTORS);
1281                 test_cipher("cbc(camellia)", DECRYPT,
1282                             camellia_cbc_dec_tv_template,
1283                             CAMELLIA_CBC_DEC_TEST_VECTORS);
1284
1285                 //SEED
1286                 test_cipher("ecb(seed)", ENCRYPT, seed_enc_tv_template,
1287                             SEED_ENC_TEST_VECTORS);
1288                 test_cipher("ecb(seed)", DECRYPT, seed_dec_tv_template,
1289                             SEED_DEC_TEST_VECTORS);
1290
1291                 test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
1292                 test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
1293                 test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
1294                 test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
1295                 test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
1296                 test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
1297                 test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
1298                 test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
1299                 test_deflate();
1300                 test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
1301                 test_hash("hmac(md5)", hmac_md5_tv_template,
1302                           HMAC_MD5_TEST_VECTORS);
1303                 test_hash("hmac(sha1)", hmac_sha1_tv_template,
1304                           HMAC_SHA1_TEST_VECTORS);
1305                 test_hash("hmac(sha224)", hmac_sha224_tv_template,
1306                           HMAC_SHA224_TEST_VECTORS);
1307                 test_hash("hmac(sha256)", hmac_sha256_tv_template,
1308                           HMAC_SHA256_TEST_VECTORS);
1309                 test_hash("hmac(sha384)", hmac_sha384_tv_template,
1310                           HMAC_SHA384_TEST_VECTORS);
1311                 test_hash("hmac(sha512)", hmac_sha512_tv_template,
1312                           HMAC_SHA512_TEST_VECTORS);
1313
1314                 test_hash("xcbc(aes)", aes_xcbc128_tv_template,
1315                           XCBC_AES_TEST_VECTORS);
1316
1317                 test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
1318                 break;
1319
1320         case 1:
1321                 test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
1322                 break;
1323
1324         case 2:
1325                 test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
1326                 break;
1327
1328         case 3:
1329                 test_cipher("ecb(des)", ENCRYPT, des_enc_tv_template,
1330                             DES_ENC_TEST_VECTORS);
1331                 test_cipher("ecb(des)", DECRYPT, des_dec_tv_template,
1332                             DES_DEC_TEST_VECTORS);
1333                 test_cipher("cbc(des)", ENCRYPT, des_cbc_enc_tv_template,
1334                             DES_CBC_ENC_TEST_VECTORS);
1335                 test_cipher("cbc(des)", DECRYPT, des_cbc_dec_tv_template,
1336                             DES_CBC_DEC_TEST_VECTORS);
1337                 break;
1338
1339         case 4:
1340                 test_cipher("ecb(des3_ede)", ENCRYPT, des3_ede_enc_tv_template,
1341                             DES3_EDE_ENC_TEST_VECTORS);
1342                 test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
1343                             DES3_EDE_DEC_TEST_VECTORS);
1344                 break;
1345
1346         case 5:
1347                 test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
1348                 break;
1349
1350         case 6:
1351                 test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
1352                 break;
1353
1354         case 7:
1355                 test_cipher("ecb(blowfish)", ENCRYPT, bf_enc_tv_template,
1356                             BF_ENC_TEST_VECTORS);
1357                 test_cipher("ecb(blowfish)", DECRYPT, bf_dec_tv_template,
1358                             BF_DEC_TEST_VECTORS);
1359                 test_cipher("cbc(blowfish)", ENCRYPT, bf_cbc_enc_tv_template,
1360                             BF_CBC_ENC_TEST_VECTORS);
1361                 test_cipher("cbc(blowfish)", DECRYPT, bf_cbc_dec_tv_template,
1362                             BF_CBC_DEC_TEST_VECTORS);
1363                 break;
1364
1365         case 8:
1366                 test_cipher("ecb(twofish)", ENCRYPT, tf_enc_tv_template,
1367                             TF_ENC_TEST_VECTORS);
1368                 test_cipher("ecb(twofish)", DECRYPT, tf_dec_tv_template,
1369                             TF_DEC_TEST_VECTORS);
1370                 test_cipher("cbc(twofish)", ENCRYPT, tf_cbc_enc_tv_template,
1371                             TF_CBC_ENC_TEST_VECTORS);
1372                 test_cipher("cbc(twofish)", DECRYPT, tf_cbc_dec_tv_template,
1373                             TF_CBC_DEC_TEST_VECTORS);
1374                 break;
1375
1376         case 9:
1377                 test_cipher("ecb(serpent)", ENCRYPT, serpent_enc_tv_template,
1378                             SERPENT_ENC_TEST_VECTORS);
1379                 test_cipher("ecb(serpent)", DECRYPT, serpent_dec_tv_template,
1380                             SERPENT_DEC_TEST_VECTORS);
1381                 break;
1382
1383         case 10:
1384                 test_cipher("ecb(aes)", ENCRYPT, aes_enc_tv_template,
1385                             AES_ENC_TEST_VECTORS);
1386                 test_cipher("ecb(aes)", DECRYPT, aes_dec_tv_template,
1387                             AES_DEC_TEST_VECTORS);
1388                 test_cipher("cbc(aes)", ENCRYPT, aes_cbc_enc_tv_template,
1389                             AES_CBC_ENC_TEST_VECTORS);
1390                 test_cipher("cbc(aes)", DECRYPT, aes_cbc_dec_tv_template,
1391                             AES_CBC_DEC_TEST_VECTORS);
1392                 test_cipher("lrw(aes)", ENCRYPT, aes_lrw_enc_tv_template,
1393                             AES_LRW_ENC_TEST_VECTORS);
1394                 test_cipher("lrw(aes)", DECRYPT, aes_lrw_dec_tv_template,
1395                             AES_LRW_DEC_TEST_VECTORS);
1396                 test_cipher("xts(aes)", ENCRYPT, aes_xts_enc_tv_template,
1397                             AES_XTS_ENC_TEST_VECTORS);
1398                 test_cipher("xts(aes)", DECRYPT, aes_xts_dec_tv_template,
1399                             AES_XTS_DEC_TEST_VECTORS);
1400                 test_cipher("ctr(aes,4,8,4)", ENCRYPT, aes_ctr_enc_tv_template,
1401                             AES_CTR_ENC_TEST_VECTORS);
1402                 test_cipher("ctr(aes,4,8,4)", DECRYPT, aes_ctr_dec_tv_template,
1403                             AES_CTR_DEC_TEST_VECTORS);
1404                 break;
1405
1406         case 11:
1407                 test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
1408                 break;
1409
1410         case 12:
1411                 test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
1412                 break;
1413
1414         case 13:
1415                 test_deflate();
1416                 break;
1417
1418         case 14:
1419                 test_cipher("ecb(cast5)", ENCRYPT, cast5_enc_tv_template,
1420                             CAST5_ENC_TEST_VECTORS);
1421                 test_cipher("ecb(cast5)", DECRYPT, cast5_dec_tv_template,
1422                             CAST5_DEC_TEST_VECTORS);
1423                 break;
1424
1425         case 15:
1426                 test_cipher("ecb(cast6)", ENCRYPT, cast6_enc_tv_template,
1427                             CAST6_ENC_TEST_VECTORS);
1428                 test_cipher("ecb(cast6)", DECRYPT, cast6_dec_tv_template,
1429                             CAST6_DEC_TEST_VECTORS);
1430                 break;
1431
1432         case 16:
1433                 test_cipher("ecb(arc4)", ENCRYPT, arc4_enc_tv_template,
1434                             ARC4_ENC_TEST_VECTORS);
1435                 test_cipher("ecb(arc4)", DECRYPT, arc4_dec_tv_template,
1436                             ARC4_DEC_TEST_VECTORS);
1437                 break;
1438
1439         case 17:
1440                 test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
1441                 break;
1442
1443         case 18:
1444                 test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
1445                 break;
1446
1447         case 19:
1448                 test_cipher("ecb(tea)", ENCRYPT, tea_enc_tv_template,
1449                             TEA_ENC_TEST_VECTORS);
1450                 test_cipher("ecb(tea)", DECRYPT, tea_dec_tv_template,
1451                             TEA_DEC_TEST_VECTORS);
1452                 break;
1453
1454         case 20:
1455                 test_cipher("ecb(xtea)", ENCRYPT, xtea_enc_tv_template,
1456                             XTEA_ENC_TEST_VECTORS);
1457                 test_cipher("ecb(xtea)", DECRYPT, xtea_dec_tv_template,
1458                             XTEA_DEC_TEST_VECTORS);
1459                 break;
1460
1461         case 21:
1462                 test_cipher("ecb(khazad)", ENCRYPT, khazad_enc_tv_template,
1463                             KHAZAD_ENC_TEST_VECTORS);
1464                 test_cipher("ecb(khazad)", DECRYPT, khazad_dec_tv_template,
1465                             KHAZAD_DEC_TEST_VECTORS);
1466                 break;
1467
1468         case 22:
1469                 test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
1470                 break;
1471
1472         case 23:
1473                 test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
1474                 break;
1475
1476         case 24:
1477                 test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
1478                 break;
1479
1480         case 25:
1481                 test_cipher("ecb(tnepres)", ENCRYPT, tnepres_enc_tv_template,
1482                             TNEPRES_ENC_TEST_VECTORS);
1483                 test_cipher("ecb(tnepres)", DECRYPT, tnepres_dec_tv_template,
1484                             TNEPRES_DEC_TEST_VECTORS);
1485                 break;
1486
1487         case 26:
1488                 test_cipher("ecb(anubis)", ENCRYPT, anubis_enc_tv_template,
1489                             ANUBIS_ENC_TEST_VECTORS);
1490                 test_cipher("ecb(anubis)", DECRYPT, anubis_dec_tv_template,
1491                             ANUBIS_DEC_TEST_VECTORS);
1492                 test_cipher("cbc(anubis)", ENCRYPT, anubis_cbc_enc_tv_template,
1493                             ANUBIS_CBC_ENC_TEST_VECTORS);
1494                 test_cipher("cbc(anubis)", DECRYPT, anubis_cbc_dec_tv_template,
1495                             ANUBIS_CBC_ENC_TEST_VECTORS);
1496                 break;
1497
1498         case 27:
1499                 test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
1500                 break;
1501
1502         case 28:
1503
1504                 test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
1505                 break;
1506
1507         case 29:
1508                 test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
1509                 break;
1510                 
1511         case 30:
1512                 test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
1513                             XETA_ENC_TEST_VECTORS);
1514                 test_cipher("ecb(xeta)", DECRYPT, xeta_dec_tv_template,
1515                             XETA_DEC_TEST_VECTORS);
1516                 break;
1517
1518         case 31:
1519                 test_cipher("pcbc(fcrypt)", ENCRYPT, fcrypt_pcbc_enc_tv_template,
1520                             FCRYPT_ENC_TEST_VECTORS);
1521                 test_cipher("pcbc(fcrypt)", DECRYPT, fcrypt_pcbc_dec_tv_template,
1522                             FCRYPT_DEC_TEST_VECTORS);
1523                 break;
1524
1525         case 32:
1526                 test_cipher("ecb(camellia)", ENCRYPT,
1527                             camellia_enc_tv_template,
1528                             CAMELLIA_ENC_TEST_VECTORS);
1529                 test_cipher("ecb(camellia)", DECRYPT,
1530                             camellia_dec_tv_template,
1531                             CAMELLIA_DEC_TEST_VECTORS);
1532                 test_cipher("cbc(camellia)", ENCRYPT,
1533                             camellia_cbc_enc_tv_template,
1534                             CAMELLIA_CBC_ENC_TEST_VECTORS);
1535                 test_cipher("cbc(camellia)", DECRYPT,
1536                             camellia_cbc_dec_tv_template,
1537                             CAMELLIA_CBC_DEC_TEST_VECTORS);
1538                 break;
1539         case 33:
1540                 test_hash("sha224", sha224_tv_template, SHA224_TEST_VECTORS);
1541                 break;
1542
1543         case 34:
1544                 test_cipher("salsa20", ENCRYPT,
1545                             salsa20_stream_enc_tv_template,
1546                             SALSA20_STREAM_ENC_TEST_VECTORS);
1547                 break;
1548
1549         case 100:
1550                 test_hash("hmac(md5)", hmac_md5_tv_template,
1551                           HMAC_MD5_TEST_VECTORS);
1552                 break;
1553
1554         case 101:
1555                 test_hash("hmac(sha1)", hmac_sha1_tv_template,
1556                           HMAC_SHA1_TEST_VECTORS);
1557                 break;
1558
1559         case 102:
1560                 test_hash("hmac(sha256)", hmac_sha256_tv_template,
1561                           HMAC_SHA256_TEST_VECTORS);
1562                 break;
1563
1564         case 103:
1565                 test_hash("hmac(sha384)", hmac_sha384_tv_template,
1566                           HMAC_SHA384_TEST_VECTORS);
1567                 break;
1568
1569         case 104:
1570                 test_hash("hmac(sha512)", hmac_sha512_tv_template,
1571                           HMAC_SHA512_TEST_VECTORS);
1572                 break;
1573         case 105:
1574                 test_hash("hmac(sha224)", hmac_sha224_tv_template,
1575                           HMAC_SHA224_TEST_VECTORS);
1576                 break;
1577
1578         case 200:
1579                 test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
1580                                   aes_speed_template);
1581                 test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
1582                                   aes_speed_template);
1583                 test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
1584                                   aes_speed_template);
1585                 test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
1586                                   aes_speed_template);
1587                 test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
1588                                   aes_lrw_speed_template);
1589                 test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
1590                                   aes_lrw_speed_template);
1591                 test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
1592                                   aes_xts_speed_template);
1593                 test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
1594                                   aes_xts_speed_template);
1595                 break;
1596
1597         case 201:
1598                 test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
1599                                   des3_ede_enc_tv_template,
1600                                   DES3_EDE_ENC_TEST_VECTORS,
1601                                   des3_ede_speed_template);
1602                 test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
1603                                   des3_ede_dec_tv_template,
1604                                   DES3_EDE_DEC_TEST_VECTORS,
1605                                   des3_ede_speed_template);
1606                 test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
1607                                   des3_ede_enc_tv_template,
1608                                   DES3_EDE_ENC_TEST_VECTORS,
1609                                   des3_ede_speed_template);
1610                 test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
1611                                   des3_ede_dec_tv_template,
1612                                   DES3_EDE_DEC_TEST_VECTORS,
1613                                   des3_ede_speed_template);
1614                 break;
1615
1616         case 202:
1617                 test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
1618                                   twofish_speed_template);
1619                 test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
1620                                   twofish_speed_template);
1621                 test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
1622                                   twofish_speed_template);
1623                 test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
1624                                   twofish_speed_template);
1625                 break;
1626
1627         case 203:
1628                 test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
1629                                   blowfish_speed_template);
1630                 test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
1631                                   blowfish_speed_template);
1632                 test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
1633                                   blowfish_speed_template);
1634                 test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
1635                                   blowfish_speed_template);
1636                 break;
1637
1638         case 204:
1639                 test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
1640                                   des_speed_template);
1641                 test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
1642                                   des_speed_template);
1643                 test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
1644                                   des_speed_template);
1645                 test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
1646                                   des_speed_template);
1647                 break;
1648
1649         case 205:
1650                 test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
1651                                 camellia_speed_template);
1652                 test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
1653                                 camellia_speed_template);
1654                 test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
1655                                 camellia_speed_template);
1656                 test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
1657                                 camellia_speed_template);
1658                 break;
1659
1660         case 300:
1661                 /* fall through */
1662
1663         case 301:
1664                 test_hash_speed("md4", sec, generic_hash_speed_template);
1665                 if (mode > 300 && mode < 400) break;
1666
1667         case 302:
1668                 test_hash_speed("md5", sec, generic_hash_speed_template);
1669                 if (mode > 300 && mode < 400) break;
1670
1671         case 303:
1672                 test_hash_speed("sha1", sec, generic_hash_speed_template);
1673                 if (mode > 300 && mode < 400) break;
1674
1675         case 304:
1676                 test_hash_speed("sha256", sec, generic_hash_speed_template);
1677                 if (mode > 300 && mode < 400) break;
1678
1679         case 305:
1680                 test_hash_speed("sha384", sec, generic_hash_speed_template);
1681                 if (mode > 300 && mode < 400) break;
1682
1683         case 306:
1684                 test_hash_speed("sha512", sec, generic_hash_speed_template);
1685                 if (mode > 300 && mode < 400) break;
1686
1687         case 307:
1688                 test_hash_speed("wp256", sec, generic_hash_speed_template);
1689                 if (mode > 300 && mode < 400) break;
1690
1691         case 308:
1692                 test_hash_speed("wp384", sec, generic_hash_speed_template);
1693                 if (mode > 300 && mode < 400) break;
1694
1695         case 309:
1696                 test_hash_speed("wp512", sec, generic_hash_speed_template);
1697                 if (mode > 300 && mode < 400) break;
1698
1699         case 310:
1700                 test_hash_speed("tgr128", sec, generic_hash_speed_template);
1701                 if (mode > 300 && mode < 400) break;
1702
1703         case 311:
1704                 test_hash_speed("tgr160", sec, generic_hash_speed_template);
1705                 if (mode > 300 && mode < 400) break;
1706
1707         case 312:
1708                 test_hash_speed("tgr192", sec, generic_hash_speed_template);
1709                 if (mode > 300 && mode < 400) break;
1710
1711         case 313:
1712                 test_hash_speed("sha224", sec, generic_hash_speed_template);
1713                 if (mode > 300 && mode < 400) break;
1714
1715         case 399:
1716                 break;
1717
1718         case 1000:
1719                 test_available();
1720                 break;
1721
1722         default:
1723                 /* useful for debugging */
1724                 printk("not testing anything\n");
1725                 break;
1726         }
1727 }
1728
1729 static int __init init(void)
1730 {
1731         int err = -ENOMEM;
1732
1733         tvmem = kmalloc(TVMEMSIZE, GFP_KERNEL);
1734         if (tvmem == NULL)
1735                 return err;
1736
1737         xbuf = kmalloc(XBUFSIZE, GFP_KERNEL);
1738         if (xbuf == NULL)
1739                 goto err_free_tv;
1740
1741         axbuf = kmalloc(XBUFSIZE, GFP_KERNEL);
1742         if (axbuf == NULL)
1743                 goto err_free_xbuf;
1744
1745         do_test();
1746
1747         /* We intentionaly return -EAGAIN to prevent keeping
1748          * the module. It does all its work from init()
1749          * and doesn't offer any runtime functionality 
1750          * => we don't need it in the memory, do we?
1751          *                                        -- mludvig
1752          */
1753         err = -EAGAIN;
1754
1755         kfree(axbuf);
1756  err_free_xbuf:
1757         kfree(xbuf);
1758  err_free_tv:
1759         kfree(tvmem);
1760
1761         return err;
1762 }
1763
1764 /*
1765  * If an init function is provided, an exit function must also be provided
1766  * to allow module unload.
1767  */
1768 static void __exit fini(void) { }
1769
1770 module_init(init);
1771 module_exit(fini);
1772
1773 module_param(mode, int, 0);
1774 module_param(sec, uint, 0);
1775 MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
1776                       "(defaults to zero which uses CPU cycles instead)");
1777
1778 MODULE_LICENSE("GPL");
1779 MODULE_DESCRIPTION("Quick & dirty crypto testing module");
1780 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");