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