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