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