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