crypto: tcrypt - Fix module return code when testing by name
[linux-2.6.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  */
17
18 #include <crypto/hash.h>
19 #include <linux/err.h>
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/scatterlist.h>
24 #include <linux/string.h>
25 #include <linux/moduleparam.h>
26 #include <linux/jiffies.h>
27 #include <linux/timex.h>
28 #include <linux/interrupt.h>
29 #include "tcrypt.h"
30 #include "internal.h"
31
32 /*
33  * Need slab memory for testing (size in number of pages).
34  */
35 #define TVMEMSIZE       4
36
37 /*
38 * Used by test_cipher_speed()
39 */
40 #define ENCRYPT 1
41 #define DECRYPT 0
42
43 /*
44  * Used by test_cipher_speed()
45  */
46 static unsigned int sec;
47
48 static char *alg = NULL;
49 static u32 type;
50 static int mode;
51 static char *tvmem[TVMEMSIZE];
52
53 static char *check[] = {
54         "des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256",
55         "blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
56         "cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
57         "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta",  "fcrypt",
58         "camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
59         "lzo", "cts", "zlib", NULL
60 };
61
62 static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc,
63                                struct scatterlist *sg, int blen, int sec)
64 {
65         unsigned long start, end;
66         int bcount;
67         int ret;
68
69         for (start = jiffies, end = start + sec * HZ, bcount = 0;
70              time_before(jiffies, end); bcount++) {
71                 if (enc)
72                         ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
73                 else
74                         ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
75
76                 if (ret)
77                         return ret;
78         }
79
80         printk("%d operations in %d seconds (%ld bytes)\n",
81                bcount, sec, (long)bcount * blen);
82         return 0;
83 }
84
85 static int test_cipher_cycles(struct blkcipher_desc *desc, int enc,
86                               struct scatterlist *sg, int blen)
87 {
88         unsigned long cycles = 0;
89         int ret = 0;
90         int i;
91
92         local_bh_disable();
93         local_irq_disable();
94
95         /* Warm-up run. */
96         for (i = 0; i < 4; i++) {
97                 if (enc)
98                         ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
99                 else
100                         ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
101
102                 if (ret)
103                         goto out;
104         }
105
106         /* The real thing. */
107         for (i = 0; i < 8; i++) {
108                 cycles_t start, end;
109
110                 start = get_cycles();
111                 if (enc)
112                         ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
113                 else
114                         ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
115                 end = get_cycles();
116
117                 if (ret)
118                         goto out;
119
120                 cycles += end - start;
121         }
122
123 out:
124         local_irq_enable();
125         local_bh_enable();
126
127         if (ret == 0)
128                 printk("1 operation in %lu cycles (%d bytes)\n",
129                        (cycles + 4) / 8, blen);
130
131         return ret;
132 }
133
134 static u32 block_sizes[] = { 16, 64, 256, 1024, 8192, 0 };
135
136 static void test_cipher_speed(const char *algo, int enc, unsigned int sec,
137                               struct cipher_speed_template *template,
138                               unsigned int tcount, u8 *keysize)
139 {
140         unsigned int ret, i, j, iv_len;
141         const char *key, iv[128];
142         struct crypto_blkcipher *tfm;
143         struct blkcipher_desc desc;
144         const char *e;
145         u32 *b_size;
146
147         if (enc == ENCRYPT)
148                 e = "encryption";
149         else
150                 e = "decryption";
151
152         printk("\ntesting speed of %s %s\n", algo, e);
153
154         tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);
155
156         if (IS_ERR(tfm)) {
157                 printk("failed to load transform for %s: %ld\n", algo,
158                        PTR_ERR(tfm));
159                 return;
160         }
161         desc.tfm = tfm;
162         desc.flags = 0;
163
164         i = 0;
165         do {
166
167                 b_size = block_sizes;
168                 do {
169                         struct scatterlist sg[TVMEMSIZE];
170
171                         if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
172                                 printk("template (%u) too big for "
173                                        "tvmem (%lu)\n", *keysize + *b_size,
174                                        TVMEMSIZE * PAGE_SIZE);
175                                 goto out;
176                         }
177
178                         printk("test %u (%d bit key, %d byte blocks): ", i,
179                                         *keysize * 8, *b_size);
180
181                         memset(tvmem[0], 0xff, PAGE_SIZE);
182
183                         /* set key, plain text and IV */
184                         key = tvmem[0];
185                         for (j = 0; j < tcount; j++) {
186                                 if (template[j].klen == *keysize) {
187                                         key = template[j].key;
188                                         break;
189                                 }
190                         }
191
192                         ret = crypto_blkcipher_setkey(tfm, key, *keysize);
193                         if (ret) {
194                                 printk("setkey() failed flags=%x\n",
195                                                 crypto_blkcipher_get_flags(tfm));
196                                 goto out;
197                         }
198
199                         sg_init_table(sg, TVMEMSIZE);
200                         sg_set_buf(sg, tvmem[0] + *keysize,
201                                    PAGE_SIZE - *keysize);
202                         for (j = 1; j < TVMEMSIZE; j++) {
203                                 sg_set_buf(sg + j, tvmem[j], PAGE_SIZE);
204                                 memset (tvmem[j], 0xff, PAGE_SIZE);
205                         }
206
207                         iv_len = crypto_blkcipher_ivsize(tfm);
208                         if (iv_len) {
209                                 memset(&iv, 0xff, iv_len);
210                                 crypto_blkcipher_set_iv(tfm, iv, iv_len);
211                         }
212
213                         if (sec)
214                                 ret = test_cipher_jiffies(&desc, enc, sg,
215                                                           *b_size, sec);
216                         else
217                                 ret = test_cipher_cycles(&desc, enc, sg,
218                                                          *b_size);
219
220                         if (ret) {
221                                 printk("%s() failed flags=%x\n", e, desc.flags);
222                                 break;
223                         }
224                         b_size++;
225                         i++;
226                 } while (*b_size);
227                 keysize++;
228         } while (*keysize);
229
230 out:
231         crypto_free_blkcipher(tfm);
232 }
233
234 static int test_hash_jiffies_digest(struct hash_desc *desc,
235                                     struct scatterlist *sg, int blen,
236                                     char *out, int sec)
237 {
238         unsigned long start, end;
239         int bcount;
240         int ret;
241
242         for (start = jiffies, end = start + sec * HZ, bcount = 0;
243              time_before(jiffies, end); bcount++) {
244                 ret = crypto_hash_digest(desc, sg, blen, out);
245                 if (ret)
246                         return ret;
247         }
248
249         printk("%6u opers/sec, %9lu bytes/sec\n",
250                bcount / sec, ((long)bcount * blen) / sec);
251
252         return 0;
253 }
254
255 static int test_hash_jiffies(struct hash_desc *desc, struct scatterlist *sg,
256                              int blen, int plen, char *out, int sec)
257 {
258         unsigned long start, end;
259         int bcount, pcount;
260         int ret;
261
262         if (plen == blen)
263                 return test_hash_jiffies_digest(desc, sg, blen, out, sec);
264
265         for (start = jiffies, end = start + sec * HZ, bcount = 0;
266              time_before(jiffies, end); bcount++) {
267                 ret = crypto_hash_init(desc);
268                 if (ret)
269                         return ret;
270                 for (pcount = 0; pcount < blen; pcount += plen) {
271                         ret = crypto_hash_update(desc, sg, plen);
272                         if (ret)
273                                 return ret;
274                 }
275                 /* we assume there is enough space in 'out' for the result */
276                 ret = crypto_hash_final(desc, out);
277                 if (ret)
278                         return ret;
279         }
280
281         printk("%6u opers/sec, %9lu bytes/sec\n",
282                bcount / sec, ((long)bcount * blen) / sec);
283
284         return 0;
285 }
286
287 static int test_hash_cycles_digest(struct hash_desc *desc,
288                                    struct scatterlist *sg, int blen, char *out)
289 {
290         unsigned long cycles = 0;
291         int i;
292         int ret;
293
294         local_bh_disable();
295         local_irq_disable();
296
297         /* Warm-up run. */
298         for (i = 0; i < 4; i++) {
299                 ret = crypto_hash_digest(desc, sg, blen, out);
300                 if (ret)
301                         goto out;
302         }
303
304         /* The real thing. */
305         for (i = 0; i < 8; i++) {
306                 cycles_t start, end;
307
308                 start = get_cycles();
309
310                 ret = crypto_hash_digest(desc, sg, blen, out);
311                 if (ret)
312                         goto out;
313
314                 end = get_cycles();
315
316                 cycles += end - start;
317         }
318
319 out:
320         local_irq_enable();
321         local_bh_enable();
322
323         if (ret)
324                 return ret;
325
326         printk("%6lu cycles/operation, %4lu cycles/byte\n",
327                cycles / 8, cycles / (8 * blen));
328
329         return 0;
330 }
331
332 static int test_hash_cycles(struct hash_desc *desc, struct scatterlist *sg,
333                             int blen, int plen, char *out)
334 {
335         unsigned long cycles = 0;
336         int i, pcount;
337         int ret;
338
339         if (plen == blen)
340                 return test_hash_cycles_digest(desc, sg, blen, out);
341
342         local_bh_disable();
343         local_irq_disable();
344
345         /* Warm-up run. */
346         for (i = 0; i < 4; i++) {
347                 ret = crypto_hash_init(desc);
348                 if (ret)
349                         goto out;
350                 for (pcount = 0; pcount < blen; pcount += plen) {
351                         ret = crypto_hash_update(desc, sg, plen);
352                         if (ret)
353                                 goto out;
354                 }
355                 ret = crypto_hash_final(desc, out);
356                 if (ret)
357                         goto out;
358         }
359
360         /* The real thing. */
361         for (i = 0; i < 8; i++) {
362                 cycles_t start, end;
363
364                 start = get_cycles();
365
366                 ret = crypto_hash_init(desc);
367                 if (ret)
368                         goto out;
369                 for (pcount = 0; pcount < blen; pcount += plen) {
370                         ret = crypto_hash_update(desc, sg, plen);
371                         if (ret)
372                                 goto out;
373                 }
374                 ret = crypto_hash_final(desc, out);
375                 if (ret)
376                         goto out;
377
378                 end = get_cycles();
379
380                 cycles += end - start;
381         }
382
383 out:
384         local_irq_enable();
385         local_bh_enable();
386
387         if (ret)
388                 return ret;
389
390         printk("%6lu cycles/operation, %4lu cycles/byte\n",
391                cycles / 8, cycles / (8 * blen));
392
393         return 0;
394 }
395
396 static void test_hash_speed(const char *algo, unsigned int sec,
397                             struct hash_speed *speed)
398 {
399         struct scatterlist sg[TVMEMSIZE];
400         struct crypto_hash *tfm;
401         struct hash_desc desc;
402         static char output[1024];
403         int i;
404         int ret;
405
406         printk(KERN_INFO "\ntesting speed of %s\n", algo);
407
408         tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
409
410         if (IS_ERR(tfm)) {
411                 printk(KERN_ERR "failed to load transform for %s: %ld\n", algo,
412                        PTR_ERR(tfm));
413                 return;
414         }
415
416         desc.tfm = tfm;
417         desc.flags = 0;
418
419         if (crypto_hash_digestsize(tfm) > sizeof(output)) {
420                 printk(KERN_ERR "digestsize(%u) > outputbuffer(%zu)\n",
421                        crypto_hash_digestsize(tfm), sizeof(output));
422                 goto out;
423         }
424
425         sg_init_table(sg, TVMEMSIZE);
426         for (i = 0; i < TVMEMSIZE; i++) {
427                 sg_set_buf(sg + i, tvmem[i], PAGE_SIZE);
428                 memset(tvmem[i], 0xff, PAGE_SIZE);
429         }
430
431         for (i = 0; speed[i].blen != 0; i++) {
432                 if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
433                         printk(KERN_ERR
434                                "template (%u) too big for tvmem (%lu)\n",
435                                speed[i].blen, TVMEMSIZE * PAGE_SIZE);
436                         goto out;
437                 }
438
439                 printk(KERN_INFO "test%3u "
440                        "(%5u byte blocks,%5u bytes per update,%4u updates): ",
441                        i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
442
443                 if (sec)
444                         ret = test_hash_jiffies(&desc, sg, speed[i].blen,
445                                                 speed[i].plen, output, sec);
446                 else
447                         ret = test_hash_cycles(&desc, sg, speed[i].blen,
448                                                speed[i].plen, output);
449
450                 if (ret) {
451                         printk(KERN_ERR "hashing failed ret=%d\n", ret);
452                         break;
453                 }
454         }
455
456 out:
457         crypto_free_hash(tfm);
458 }
459
460 static void test_available(void)
461 {
462         char **name = check;
463
464         while (*name) {
465                 printk("alg %s ", *name);
466                 printk(crypto_has_alg(*name, 0, 0) ?
467                        "found\n" : "not found\n");
468                 name++;
469         }
470 }
471
472 static inline int tcrypt_test(const char *alg)
473 {
474         int ret;
475
476         ret = alg_test(alg, alg, 0, 0);
477         /* non-fips algs return -EINVAL in fips mode */
478         if (fips_enabled && ret == -EINVAL)
479                 ret = 0;
480         return ret;
481 }
482
483 static int do_test(int m)
484 {
485         int i;
486         int ret = 0;
487
488         switch (m) {
489         case 0:
490                 for (i = 1; i < 200; i++)
491                         ret += do_test(i);
492                 break;
493
494         case 1:
495                 ret += tcrypt_test("md5");
496                 break;
497
498         case 2:
499                 ret += tcrypt_test("sha1");
500                 break;
501
502         case 3:
503                 ret += tcrypt_test("ecb(des)");
504                 ret += tcrypt_test("cbc(des)");
505                 break;
506
507         case 4:
508                 ret += tcrypt_test("ecb(des3_ede)");
509                 ret += tcrypt_test("cbc(des3_ede)");
510                 break;
511
512         case 5:
513                 ret += tcrypt_test("md4");
514                 break;
515
516         case 6:
517                 ret += tcrypt_test("sha256");
518                 break;
519
520         case 7:
521                 ret += tcrypt_test("ecb(blowfish)");
522                 ret += tcrypt_test("cbc(blowfish)");
523                 break;
524
525         case 8:
526                 ret += tcrypt_test("ecb(twofish)");
527                 ret += tcrypt_test("cbc(twofish)");
528                 break;
529
530         case 9:
531                 ret += tcrypt_test("ecb(serpent)");
532                 break;
533
534         case 10:
535                 ret += tcrypt_test("ecb(aes)");
536                 ret += tcrypt_test("cbc(aes)");
537                 ret += tcrypt_test("lrw(aes)");
538                 ret += tcrypt_test("xts(aes)");
539                 ret += tcrypt_test("ctr(aes)");
540                 ret += tcrypt_test("rfc3686(ctr(aes))");
541                 break;
542
543         case 11:
544                 ret += tcrypt_test("sha384");
545                 break;
546
547         case 12:
548                 ret += tcrypt_test("sha512");
549                 break;
550
551         case 13:
552                 ret += tcrypt_test("deflate");
553                 break;
554
555         case 14:
556                 ret += tcrypt_test("ecb(cast5)");
557                 break;
558
559         case 15:
560                 ret += tcrypt_test("ecb(cast6)");
561                 break;
562
563         case 16:
564                 ret += tcrypt_test("ecb(arc4)");
565                 break;
566
567         case 17:
568                 ret += tcrypt_test("michael_mic");
569                 break;
570
571         case 18:
572                 ret += tcrypt_test("crc32c");
573                 break;
574
575         case 19:
576                 ret += tcrypt_test("ecb(tea)");
577                 break;
578
579         case 20:
580                 ret += tcrypt_test("ecb(xtea)");
581                 break;
582
583         case 21:
584                 ret += tcrypt_test("ecb(khazad)");
585                 break;
586
587         case 22:
588                 ret += tcrypt_test("wp512");
589                 break;
590
591         case 23:
592                 ret += tcrypt_test("wp384");
593                 break;
594
595         case 24:
596                 ret += tcrypt_test("wp256");
597                 break;
598
599         case 25:
600                 ret += tcrypt_test("ecb(tnepres)");
601                 break;
602
603         case 26:
604                 ret += tcrypt_test("ecb(anubis)");
605                 ret += tcrypt_test("cbc(anubis)");
606                 break;
607
608         case 27:
609                 ret += tcrypt_test("tgr192");
610                 break;
611
612         case 28:
613
614                 ret += tcrypt_test("tgr160");
615                 break;
616
617         case 29:
618                 ret += tcrypt_test("tgr128");
619                 break;
620
621         case 30:
622                 ret += tcrypt_test("ecb(xeta)");
623                 break;
624
625         case 31:
626                 ret += tcrypt_test("pcbc(fcrypt)");
627                 break;
628
629         case 32:
630                 ret += tcrypt_test("ecb(camellia)");
631                 ret += tcrypt_test("cbc(camellia)");
632                 break;
633         case 33:
634                 ret += tcrypt_test("sha224");
635                 break;
636
637         case 34:
638                 ret += tcrypt_test("salsa20");
639                 break;
640
641         case 35:
642                 ret += tcrypt_test("gcm(aes)");
643                 break;
644
645         case 36:
646                 ret += tcrypt_test("lzo");
647                 break;
648
649         case 37:
650                 ret += tcrypt_test("ccm(aes)");
651                 break;
652
653         case 38:
654                 ret += tcrypt_test("cts(cbc(aes))");
655                 break;
656
657         case 39:
658                 ret += tcrypt_test("rmd128");
659                 break;
660
661         case 40:
662                 ret += tcrypt_test("rmd160");
663                 break;
664
665         case 41:
666                 ret += tcrypt_test("rmd256");
667                 break;
668
669         case 42:
670                 ret += tcrypt_test("rmd320");
671                 break;
672
673         case 43:
674                 ret += tcrypt_test("ecb(seed)");
675                 break;
676
677         case 44:
678                 ret += tcrypt_test("zlib");
679                 break;
680
681         case 45:
682                 ret += tcrypt_test("rfc4309(ccm(aes))");
683                 break;
684
685         case 100:
686                 ret += tcrypt_test("hmac(md5)");
687                 break;
688
689         case 101:
690                 ret += tcrypt_test("hmac(sha1)");
691                 break;
692
693         case 102:
694                 ret += tcrypt_test("hmac(sha256)");
695                 break;
696
697         case 103:
698                 ret += tcrypt_test("hmac(sha384)");
699                 break;
700
701         case 104:
702                 ret += tcrypt_test("hmac(sha512)");
703                 break;
704
705         case 105:
706                 ret += tcrypt_test("hmac(sha224)");
707                 break;
708
709         case 106:
710                 ret += tcrypt_test("xcbc(aes)");
711                 break;
712
713         case 107:
714                 ret += tcrypt_test("hmac(rmd128)");
715                 break;
716
717         case 108:
718                 ret += tcrypt_test("hmac(rmd160)");
719                 break;
720
721         case 150:
722                 ret += tcrypt_test("ansi_cprng");
723                 break;
724
725         case 200:
726                 test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
727                                 speed_template_16_24_32);
728                 test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
729                                 speed_template_16_24_32);
730                 test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
731                                 speed_template_16_24_32);
732                 test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
733                                 speed_template_16_24_32);
734                 test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
735                                 speed_template_32_40_48);
736                 test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
737                                 speed_template_32_40_48);
738                 test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
739                                 speed_template_32_48_64);
740                 test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
741                                 speed_template_32_48_64);
742                 break;
743
744         case 201:
745                 test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
746                                 des3_speed_template, DES3_SPEED_VECTORS,
747                                 speed_template_24);
748                 test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
749                                 des3_speed_template, DES3_SPEED_VECTORS,
750                                 speed_template_24);
751                 test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
752                                 des3_speed_template, DES3_SPEED_VECTORS,
753                                 speed_template_24);
754                 test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
755                                 des3_speed_template, DES3_SPEED_VECTORS,
756                                 speed_template_24);
757                 break;
758
759         case 202:
760                 test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
761                                 speed_template_16_24_32);
762                 test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
763                                 speed_template_16_24_32);
764                 test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
765                                 speed_template_16_24_32);
766                 test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
767                                 speed_template_16_24_32);
768                 break;
769
770         case 203:
771                 test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
772                                   speed_template_8_32);
773                 test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
774                                   speed_template_8_32);
775                 test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
776                                   speed_template_8_32);
777                 test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
778                                   speed_template_8_32);
779                 break;
780
781         case 204:
782                 test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
783                                   speed_template_8);
784                 test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
785                                   speed_template_8);
786                 test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
787                                   speed_template_8);
788                 test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
789                                   speed_template_8);
790                 break;
791
792         case 205:
793                 test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
794                                 speed_template_16_24_32);
795                 test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
796                                 speed_template_16_24_32);
797                 test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
798                                 speed_template_16_24_32);
799                 test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
800                                 speed_template_16_24_32);
801                 break;
802
803         case 206:
804                 test_cipher_speed("salsa20", ENCRYPT, sec, NULL, 0,
805                                   speed_template_16_32);
806                 break;
807
808         case 300:
809                 /* fall through */
810
811         case 301:
812                 test_hash_speed("md4", sec, generic_hash_speed_template);
813                 if (mode > 300 && mode < 400) break;
814
815         case 302:
816                 test_hash_speed("md5", sec, generic_hash_speed_template);
817                 if (mode > 300 && mode < 400) break;
818
819         case 303:
820                 test_hash_speed("sha1", sec, generic_hash_speed_template);
821                 if (mode > 300 && mode < 400) break;
822
823         case 304:
824                 test_hash_speed("sha256", sec, generic_hash_speed_template);
825                 if (mode > 300 && mode < 400) break;
826
827         case 305:
828                 test_hash_speed("sha384", sec, generic_hash_speed_template);
829                 if (mode > 300 && mode < 400) break;
830
831         case 306:
832                 test_hash_speed("sha512", sec, generic_hash_speed_template);
833                 if (mode > 300 && mode < 400) break;
834
835         case 307:
836                 test_hash_speed("wp256", sec, generic_hash_speed_template);
837                 if (mode > 300 && mode < 400) break;
838
839         case 308:
840                 test_hash_speed("wp384", sec, generic_hash_speed_template);
841                 if (mode > 300 && mode < 400) break;
842
843         case 309:
844                 test_hash_speed("wp512", sec, generic_hash_speed_template);
845                 if (mode > 300 && mode < 400) break;
846
847         case 310:
848                 test_hash_speed("tgr128", sec, generic_hash_speed_template);
849                 if (mode > 300 && mode < 400) break;
850
851         case 311:
852                 test_hash_speed("tgr160", sec, generic_hash_speed_template);
853                 if (mode > 300 && mode < 400) break;
854
855         case 312:
856                 test_hash_speed("tgr192", sec, generic_hash_speed_template);
857                 if (mode > 300 && mode < 400) break;
858
859         case 313:
860                 test_hash_speed("sha224", sec, generic_hash_speed_template);
861                 if (mode > 300 && mode < 400) break;
862
863         case 314:
864                 test_hash_speed("rmd128", sec, generic_hash_speed_template);
865                 if (mode > 300 && mode < 400) break;
866
867         case 315:
868                 test_hash_speed("rmd160", sec, generic_hash_speed_template);
869                 if (mode > 300 && mode < 400) break;
870
871         case 316:
872                 test_hash_speed("rmd256", sec, generic_hash_speed_template);
873                 if (mode > 300 && mode < 400) break;
874
875         case 317:
876                 test_hash_speed("rmd320", sec, generic_hash_speed_template);
877                 if (mode > 300 && mode < 400) break;
878
879         case 399:
880                 break;
881
882         case 1000:
883                 test_available();
884                 break;
885         }
886
887         return ret;
888 }
889
890 static int do_alg_test(const char *alg, u32 type)
891 {
892         return crypto_has_alg(alg, type, CRYPTO_ALG_TYPE_MASK) ? 0 : -ENOENT;
893 }
894
895 static int __init tcrypt_mod_init(void)
896 {
897         int err = -ENOMEM;
898         int i;
899
900         for (i = 0; i < TVMEMSIZE; i++) {
901                 tvmem[i] = (void *)__get_free_page(GFP_KERNEL);
902                 if (!tvmem[i])
903                         goto err_free_tv;
904         }
905
906         if (alg)
907                 err = do_alg_test(alg, type);
908         else
909                 err = do_test(mode);
910
911         if (err) {
912                 printk(KERN_ERR "tcrypt: one or more tests failed!\n");
913                 goto err_free_tv;
914         }
915
916         /* We intentionaly return -EAGAIN to prevent keeping the module,
917          * unless we're running in fips mode. It does all its work from
918          * init() and doesn't offer any runtime functionality, but in
919          * the fips case, checking for a successful load is helpful.
920          * => we don't need it in the memory, do we?
921          *                                        -- mludvig
922          */
923         if (!fips_enabled)
924                 err = -EAGAIN;
925
926 err_free_tv:
927         for (i = 0; i < TVMEMSIZE && tvmem[i]; i++)
928                 free_page((unsigned long)tvmem[i]);
929
930         return err;
931 }
932
933 /*
934  * If an init function is provided, an exit function must also be provided
935  * to allow module unload.
936  */
937 static void __exit tcrypt_mod_fini(void) { }
938
939 module_init(tcrypt_mod_init);
940 module_exit(tcrypt_mod_fini);
941
942 module_param(alg, charp, 0);
943 module_param(type, uint, 0);
944 module_param(mode, int, 0);
945 module_param(sec, uint, 0);
946 MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
947                       "(defaults to zero which uses CPU cycles instead)");
948
949 MODULE_LICENSE("GPL");
950 MODULE_DESCRIPTION("Quick & dirty crypto testing module");
951 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");