Merge branch 'next' of git://git.monstr.eu/linux-2.6-microblaze
[platform/adaptation/renesas_rcar/renesas_kernel.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  * Updated RFC4106 AES-GCM testing.
12  *    Authors: Aidan O'Mahony (aidan.o.mahony@intel.com)
13  *             Adrian Hoban <adrian.hoban@intel.com>
14  *             Gabriele Paoloni <gabriele.paoloni@intel.com>
15  *             Tadeusz Struk (tadeusz.struk@intel.com)
16  *             Copyright (c) 2010, Intel Corporation.
17  *
18  * This program is free software; you can redistribute it and/or modify it
19  * under the terms of the GNU General Public License as published by the Free
20  * Software Foundation; either version 2 of the License, or (at your option)
21  * any later version.
22  *
23  */
24
25 #include <crypto/hash.h>
26 #include <linux/err.h>
27 #include <linux/init.h>
28 #include <linux/gfp.h>
29 #include <linux/module.h>
30 #include <linux/scatterlist.h>
31 #include <linux/string.h>
32 #include <linux/moduleparam.h>
33 #include <linux/jiffies.h>
34 #include <linux/timex.h>
35 #include <linux/interrupt.h>
36 #include "tcrypt.h"
37 #include "internal.h"
38
39 /*
40  * Need slab memory for testing (size in number of pages).
41  */
42 #define TVMEMSIZE       4
43
44 /*
45 * Used by test_cipher_speed()
46 */
47 #define ENCRYPT 1
48 #define DECRYPT 0
49
50 /*
51  * Used by test_cipher_speed()
52  */
53 static unsigned int sec;
54
55 static char *alg = NULL;
56 static u32 type;
57 static u32 mask;
58 static int mode;
59 static char *tvmem[TVMEMSIZE];
60
61 static char *check[] = {
62         "des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256",
63         "blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
64         "cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
65         "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta",  "fcrypt",
66         "camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
67         "lzo", "cts", "zlib", NULL
68 };
69
70 static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc,
71                                struct scatterlist *sg, int blen, int sec)
72 {
73         unsigned long start, end;
74         int bcount;
75         int ret;
76
77         for (start = jiffies, end = start + sec * HZ, bcount = 0;
78              time_before(jiffies, end); bcount++) {
79                 if (enc)
80                         ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
81                 else
82                         ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
83
84                 if (ret)
85                         return ret;
86         }
87
88         printk("%d operations in %d seconds (%ld bytes)\n",
89                bcount, sec, (long)bcount * blen);
90         return 0;
91 }
92
93 static int test_cipher_cycles(struct blkcipher_desc *desc, int enc,
94                               struct scatterlist *sg, int blen)
95 {
96         unsigned long cycles = 0;
97         int ret = 0;
98         int i;
99
100         local_bh_disable();
101         local_irq_disable();
102
103         /* Warm-up run. */
104         for (i = 0; i < 4; i++) {
105                 if (enc)
106                         ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
107                 else
108                         ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
109
110                 if (ret)
111                         goto out;
112         }
113
114         /* The real thing. */
115         for (i = 0; i < 8; i++) {
116                 cycles_t start, end;
117
118                 start = get_cycles();
119                 if (enc)
120                         ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
121                 else
122                         ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
123                 end = get_cycles();
124
125                 if (ret)
126                         goto out;
127
128                 cycles += end - start;
129         }
130
131 out:
132         local_irq_enable();
133         local_bh_enable();
134
135         if (ret == 0)
136                 printk("1 operation in %lu cycles (%d bytes)\n",
137                        (cycles + 4) / 8, blen);
138
139         return ret;
140 }
141
142 static u32 block_sizes[] = { 16, 64, 256, 1024, 8192, 0 };
143
144 static void test_cipher_speed(const char *algo, int enc, unsigned int sec,
145                               struct cipher_speed_template *template,
146                               unsigned int tcount, u8 *keysize)
147 {
148         unsigned int ret, i, j, iv_len;
149         const char *key;
150         char iv[128];
151         struct crypto_blkcipher *tfm;
152         struct blkcipher_desc desc;
153         const char *e;
154         u32 *b_size;
155
156         if (enc == ENCRYPT)
157                 e = "encryption";
158         else
159                 e = "decryption";
160
161         printk("\ntesting speed of %s %s\n", algo, e);
162
163         tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);
164
165         if (IS_ERR(tfm)) {
166                 printk("failed to load transform for %s: %ld\n", algo,
167                        PTR_ERR(tfm));
168                 return;
169         }
170         desc.tfm = tfm;
171         desc.flags = 0;
172
173         i = 0;
174         do {
175
176                 b_size = block_sizes;
177                 do {
178                         struct scatterlist sg[TVMEMSIZE];
179
180                         if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
181                                 printk("template (%u) too big for "
182                                        "tvmem (%lu)\n", *keysize + *b_size,
183                                        TVMEMSIZE * PAGE_SIZE);
184                                 goto out;
185                         }
186
187                         printk("test %u (%d bit key, %d byte blocks): ", i,
188                                         *keysize * 8, *b_size);
189
190                         memset(tvmem[0], 0xff, PAGE_SIZE);
191
192                         /* set key, plain text and IV */
193                         key = tvmem[0];
194                         for (j = 0; j < tcount; j++) {
195                                 if (template[j].klen == *keysize) {
196                                         key = template[j].key;
197                                         break;
198                                 }
199                         }
200
201                         ret = crypto_blkcipher_setkey(tfm, key, *keysize);
202                         if (ret) {
203                                 printk("setkey() failed flags=%x\n",
204                                                 crypto_blkcipher_get_flags(tfm));
205                                 goto out;
206                         }
207
208                         sg_init_table(sg, TVMEMSIZE);
209                         sg_set_buf(sg, tvmem[0] + *keysize,
210                                    PAGE_SIZE - *keysize);
211                         for (j = 1; j < TVMEMSIZE; j++) {
212                                 sg_set_buf(sg + j, tvmem[j], PAGE_SIZE);
213                                 memset (tvmem[j], 0xff, PAGE_SIZE);
214                         }
215
216                         iv_len = crypto_blkcipher_ivsize(tfm);
217                         if (iv_len) {
218                                 memset(&iv, 0xff, iv_len);
219                                 crypto_blkcipher_set_iv(tfm, iv, iv_len);
220                         }
221
222                         if (sec)
223                                 ret = test_cipher_jiffies(&desc, enc, sg,
224                                                           *b_size, sec);
225                         else
226                                 ret = test_cipher_cycles(&desc, enc, sg,
227                                                          *b_size);
228
229                         if (ret) {
230                                 printk("%s() failed flags=%x\n", e, desc.flags);
231                                 break;
232                         }
233                         b_size++;
234                         i++;
235                 } while (*b_size);
236                 keysize++;
237         } while (*keysize);
238
239 out:
240         crypto_free_blkcipher(tfm);
241 }
242
243 static int test_hash_jiffies_digest(struct hash_desc *desc,
244                                     struct scatterlist *sg, int blen,
245                                     char *out, int sec)
246 {
247         unsigned long start, end;
248         int bcount;
249         int ret;
250
251         for (start = jiffies, end = start + sec * HZ, bcount = 0;
252              time_before(jiffies, end); bcount++) {
253                 ret = crypto_hash_digest(desc, sg, blen, out);
254                 if (ret)
255                         return ret;
256         }
257
258         printk("%6u opers/sec, %9lu bytes/sec\n",
259                bcount / sec, ((long)bcount * blen) / sec);
260
261         return 0;
262 }
263
264 static int test_hash_jiffies(struct hash_desc *desc, struct scatterlist *sg,
265                              int blen, int plen, char *out, int sec)
266 {
267         unsigned long start, end;
268         int bcount, pcount;
269         int ret;
270
271         if (plen == blen)
272                 return test_hash_jiffies_digest(desc, sg, blen, out, sec);
273
274         for (start = jiffies, end = start + sec * HZ, bcount = 0;
275              time_before(jiffies, end); bcount++) {
276                 ret = crypto_hash_init(desc);
277                 if (ret)
278                         return ret;
279                 for (pcount = 0; pcount < blen; pcount += plen) {
280                         ret = crypto_hash_update(desc, sg, plen);
281                         if (ret)
282                                 return ret;
283                 }
284                 /* we assume there is enough space in 'out' for the result */
285                 ret = crypto_hash_final(desc, out);
286                 if (ret)
287                         return ret;
288         }
289
290         printk("%6u opers/sec, %9lu bytes/sec\n",
291                bcount / sec, ((long)bcount * blen) / sec);
292
293         return 0;
294 }
295
296 static int test_hash_cycles_digest(struct hash_desc *desc,
297                                    struct scatterlist *sg, int blen, char *out)
298 {
299         unsigned long cycles = 0;
300         int i;
301         int ret;
302
303         local_bh_disable();
304         local_irq_disable();
305
306         /* Warm-up run. */
307         for (i = 0; i < 4; i++) {
308                 ret = crypto_hash_digest(desc, sg, blen, out);
309                 if (ret)
310                         goto out;
311         }
312
313         /* The real thing. */
314         for (i = 0; i < 8; i++) {
315                 cycles_t start, end;
316
317                 start = get_cycles();
318
319                 ret = crypto_hash_digest(desc, sg, blen, out);
320                 if (ret)
321                         goto out;
322
323                 end = get_cycles();
324
325                 cycles += end - start;
326         }
327
328 out:
329         local_irq_enable();
330         local_bh_enable();
331
332         if (ret)
333                 return ret;
334
335         printk("%6lu cycles/operation, %4lu cycles/byte\n",
336                cycles / 8, cycles / (8 * blen));
337
338         return 0;
339 }
340
341 static int test_hash_cycles(struct hash_desc *desc, struct scatterlist *sg,
342                             int blen, int plen, char *out)
343 {
344         unsigned long cycles = 0;
345         int i, pcount;
346         int ret;
347
348         if (plen == blen)
349                 return test_hash_cycles_digest(desc, sg, blen, out);
350
351         local_bh_disable();
352         local_irq_disable();
353
354         /* Warm-up run. */
355         for (i = 0; i < 4; i++) {
356                 ret = crypto_hash_init(desc);
357                 if (ret)
358                         goto out;
359                 for (pcount = 0; pcount < blen; pcount += plen) {
360                         ret = crypto_hash_update(desc, sg, plen);
361                         if (ret)
362                                 goto out;
363                 }
364                 ret = crypto_hash_final(desc, out);
365                 if (ret)
366                         goto out;
367         }
368
369         /* The real thing. */
370         for (i = 0; i < 8; i++) {
371                 cycles_t start, end;
372
373                 start = get_cycles();
374
375                 ret = crypto_hash_init(desc);
376                 if (ret)
377                         goto out;
378                 for (pcount = 0; pcount < blen; pcount += plen) {
379                         ret = crypto_hash_update(desc, sg, plen);
380                         if (ret)
381                                 goto out;
382                 }
383                 ret = crypto_hash_final(desc, out);
384                 if (ret)
385                         goto out;
386
387                 end = get_cycles();
388
389                 cycles += end - start;
390         }
391
392 out:
393         local_irq_enable();
394         local_bh_enable();
395
396         if (ret)
397                 return ret;
398
399         printk("%6lu cycles/operation, %4lu cycles/byte\n",
400                cycles / 8, cycles / (8 * blen));
401
402         return 0;
403 }
404
405 static void test_hash_sg_init(struct scatterlist *sg)
406 {
407         int i;
408
409         sg_init_table(sg, TVMEMSIZE);
410         for (i = 0; i < TVMEMSIZE; i++) {
411                 sg_set_buf(sg + i, tvmem[i], PAGE_SIZE);
412                 memset(tvmem[i], 0xff, PAGE_SIZE);
413         }
414 }
415
416 static void test_hash_speed(const char *algo, unsigned int sec,
417                             struct hash_speed *speed)
418 {
419         struct scatterlist sg[TVMEMSIZE];
420         struct crypto_hash *tfm;
421         struct hash_desc desc;
422         static char output[1024];
423         int i;
424         int ret;
425
426         printk(KERN_INFO "\ntesting speed of %s\n", algo);
427
428         tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
429
430         if (IS_ERR(tfm)) {
431                 printk(KERN_ERR "failed to load transform for %s: %ld\n", algo,
432                        PTR_ERR(tfm));
433                 return;
434         }
435
436         desc.tfm = tfm;
437         desc.flags = 0;
438
439         if (crypto_hash_digestsize(tfm) > sizeof(output)) {
440                 printk(KERN_ERR "digestsize(%u) > outputbuffer(%zu)\n",
441                        crypto_hash_digestsize(tfm), sizeof(output));
442                 goto out;
443         }
444
445         test_hash_sg_init(sg);
446         for (i = 0; speed[i].blen != 0; i++) {
447                 if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
448                         printk(KERN_ERR
449                                "template (%u) too big for tvmem (%lu)\n",
450                                speed[i].blen, TVMEMSIZE * PAGE_SIZE);
451                         goto out;
452                 }
453
454                 if (speed[i].klen)
455                         crypto_hash_setkey(tfm, tvmem[0], speed[i].klen);
456
457                 printk(KERN_INFO "test%3u "
458                        "(%5u byte blocks,%5u bytes per update,%4u updates): ",
459                        i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
460
461                 if (sec)
462                         ret = test_hash_jiffies(&desc, sg, speed[i].blen,
463                                                 speed[i].plen, output, sec);
464                 else
465                         ret = test_hash_cycles(&desc, sg, speed[i].blen,
466                                                speed[i].plen, output);
467
468                 if (ret) {
469                         printk(KERN_ERR "hashing failed ret=%d\n", ret);
470                         break;
471                 }
472         }
473
474 out:
475         crypto_free_hash(tfm);
476 }
477
478 struct tcrypt_result {
479         struct completion completion;
480         int err;
481 };
482
483 static void tcrypt_complete(struct crypto_async_request *req, int err)
484 {
485         struct tcrypt_result *res = req->data;
486
487         if (err == -EINPROGRESS)
488                 return;
489
490         res->err = err;
491         complete(&res->completion);
492 }
493
494 static inline int do_one_ahash_op(struct ahash_request *req, int ret)
495 {
496         if (ret == -EINPROGRESS || ret == -EBUSY) {
497                 struct tcrypt_result *tr = req->base.data;
498
499                 ret = wait_for_completion_interruptible(&tr->completion);
500                 if (!ret)
501                         ret = tr->err;
502                 INIT_COMPLETION(tr->completion);
503         }
504         return ret;
505 }
506
507 static int test_ahash_jiffies_digest(struct ahash_request *req, int blen,
508                                      char *out, int sec)
509 {
510         unsigned long start, end;
511         int bcount;
512         int ret;
513
514         for (start = jiffies, end = start + sec * HZ, bcount = 0;
515              time_before(jiffies, end); bcount++) {
516                 ret = do_one_ahash_op(req, crypto_ahash_digest(req));
517                 if (ret)
518                         return ret;
519         }
520
521         printk("%6u opers/sec, %9lu bytes/sec\n",
522                bcount / sec, ((long)bcount * blen) / sec);
523
524         return 0;
525 }
526
527 static int test_ahash_jiffies(struct ahash_request *req, int blen,
528                               int plen, char *out, int sec)
529 {
530         unsigned long start, end;
531         int bcount, pcount;
532         int ret;
533
534         if (plen == blen)
535                 return test_ahash_jiffies_digest(req, blen, out, sec);
536
537         for (start = jiffies, end = start + sec * HZ, bcount = 0;
538              time_before(jiffies, end); bcount++) {
539                 ret = crypto_ahash_init(req);
540                 if (ret)
541                         return ret;
542                 for (pcount = 0; pcount < blen; pcount += plen) {
543                         ret = do_one_ahash_op(req, crypto_ahash_update(req));
544                         if (ret)
545                                 return ret;
546                 }
547                 /* we assume there is enough space in 'out' for the result */
548                 ret = do_one_ahash_op(req, crypto_ahash_final(req));
549                 if (ret)
550                         return ret;
551         }
552
553         pr_cont("%6u opers/sec, %9lu bytes/sec\n",
554                 bcount / sec, ((long)bcount * blen) / sec);
555
556         return 0;
557 }
558
559 static int test_ahash_cycles_digest(struct ahash_request *req, int blen,
560                                     char *out)
561 {
562         unsigned long cycles = 0;
563         int ret, i;
564
565         /* Warm-up run. */
566         for (i = 0; i < 4; i++) {
567                 ret = do_one_ahash_op(req, crypto_ahash_digest(req));
568                 if (ret)
569                         goto out;
570         }
571
572         /* The real thing. */
573         for (i = 0; i < 8; i++) {
574                 cycles_t start, end;
575
576                 start = get_cycles();
577
578                 ret = do_one_ahash_op(req, crypto_ahash_digest(req));
579                 if (ret)
580                         goto out;
581
582                 end = get_cycles();
583
584                 cycles += end - start;
585         }
586
587 out:
588         if (ret)
589                 return ret;
590
591         pr_cont("%6lu cycles/operation, %4lu cycles/byte\n",
592                 cycles / 8, cycles / (8 * blen));
593
594         return 0;
595 }
596
597 static int test_ahash_cycles(struct ahash_request *req, int blen,
598                              int plen, char *out)
599 {
600         unsigned long cycles = 0;
601         int i, pcount, ret;
602
603         if (plen == blen)
604                 return test_ahash_cycles_digest(req, blen, out);
605
606         /* Warm-up run. */
607         for (i = 0; i < 4; i++) {
608                 ret = crypto_ahash_init(req);
609                 if (ret)
610                         goto out;
611                 for (pcount = 0; pcount < blen; pcount += plen) {
612                         ret = do_one_ahash_op(req, crypto_ahash_update(req));
613                         if (ret)
614                                 goto out;
615                 }
616                 ret = do_one_ahash_op(req, crypto_ahash_final(req));
617                 if (ret)
618                         goto out;
619         }
620
621         /* The real thing. */
622         for (i = 0; i < 8; i++) {
623                 cycles_t start, end;
624
625                 start = get_cycles();
626
627                 ret = crypto_ahash_init(req);
628                 if (ret)
629                         goto out;
630                 for (pcount = 0; pcount < blen; pcount += plen) {
631                         ret = do_one_ahash_op(req, crypto_ahash_update(req));
632                         if (ret)
633                                 goto out;
634                 }
635                 ret = do_one_ahash_op(req, crypto_ahash_final(req));
636                 if (ret)
637                         goto out;
638
639                 end = get_cycles();
640
641                 cycles += end - start;
642         }
643
644 out:
645         if (ret)
646                 return ret;
647
648         pr_cont("%6lu cycles/operation, %4lu cycles/byte\n",
649                 cycles / 8, cycles / (8 * blen));
650
651         return 0;
652 }
653
654 static void test_ahash_speed(const char *algo, unsigned int sec,
655                              struct hash_speed *speed)
656 {
657         struct scatterlist sg[TVMEMSIZE];
658         struct tcrypt_result tresult;
659         struct ahash_request *req;
660         struct crypto_ahash *tfm;
661         static char output[1024];
662         int i, ret;
663
664         printk(KERN_INFO "\ntesting speed of async %s\n", algo);
665
666         tfm = crypto_alloc_ahash(algo, 0, 0);
667         if (IS_ERR(tfm)) {
668                 pr_err("failed to load transform for %s: %ld\n",
669                        algo, PTR_ERR(tfm));
670                 return;
671         }
672
673         if (crypto_ahash_digestsize(tfm) > sizeof(output)) {
674                 pr_err("digestsize(%u) > outputbuffer(%zu)\n",
675                        crypto_ahash_digestsize(tfm), sizeof(output));
676                 goto out;
677         }
678
679         test_hash_sg_init(sg);
680         req = ahash_request_alloc(tfm, GFP_KERNEL);
681         if (!req) {
682                 pr_err("ahash request allocation failure\n");
683                 goto out;
684         }
685
686         init_completion(&tresult.completion);
687         ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
688                                    tcrypt_complete, &tresult);
689
690         for (i = 0; speed[i].blen != 0; i++) {
691                 if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
692                         pr_err("template (%u) too big for tvmem (%lu)\n",
693                                speed[i].blen, TVMEMSIZE * PAGE_SIZE);
694                         break;
695                 }
696
697                 pr_info("test%3u "
698                         "(%5u byte blocks,%5u bytes per update,%4u updates): ",
699                         i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
700
701                 ahash_request_set_crypt(req, sg, output, speed[i].plen);
702
703                 if (sec)
704                         ret = test_ahash_jiffies(req, speed[i].blen,
705                                                  speed[i].plen, output, sec);
706                 else
707                         ret = test_ahash_cycles(req, speed[i].blen,
708                                                 speed[i].plen, output);
709
710                 if (ret) {
711                         pr_err("hashing failed ret=%d\n", ret);
712                         break;
713                 }
714         }
715
716         ahash_request_free(req);
717
718 out:
719         crypto_free_ahash(tfm);
720 }
721
722 static void test_available(void)
723 {
724         char **name = check;
725
726         while (*name) {
727                 printk("alg %s ", *name);
728                 printk(crypto_has_alg(*name, 0, 0) ?
729                        "found\n" : "not found\n");
730                 name++;
731         }
732 }
733
734 static inline int tcrypt_test(const char *alg)
735 {
736         int ret;
737
738         ret = alg_test(alg, alg, 0, 0);
739         /* non-fips algs return -EINVAL in fips mode */
740         if (fips_enabled && ret == -EINVAL)
741                 ret = 0;
742         return ret;
743 }
744
745 static int do_test(int m)
746 {
747         int i;
748         int ret = 0;
749
750         switch (m) {
751         case 0:
752                 for (i = 1; i < 200; i++)
753                         ret += do_test(i);
754                 break;
755
756         case 1:
757                 ret += tcrypt_test("md5");
758                 break;
759
760         case 2:
761                 ret += tcrypt_test("sha1");
762                 break;
763
764         case 3:
765                 ret += tcrypt_test("ecb(des)");
766                 ret += tcrypt_test("cbc(des)");
767                 break;
768
769         case 4:
770                 ret += tcrypt_test("ecb(des3_ede)");
771                 ret += tcrypt_test("cbc(des3_ede)");
772                 break;
773
774         case 5:
775                 ret += tcrypt_test("md4");
776                 break;
777
778         case 6:
779                 ret += tcrypt_test("sha256");
780                 break;
781
782         case 7:
783                 ret += tcrypt_test("ecb(blowfish)");
784                 ret += tcrypt_test("cbc(blowfish)");
785                 break;
786
787         case 8:
788                 ret += tcrypt_test("ecb(twofish)");
789                 ret += tcrypt_test("cbc(twofish)");
790                 break;
791
792         case 9:
793                 ret += tcrypt_test("ecb(serpent)");
794                 break;
795
796         case 10:
797                 ret += tcrypt_test("ecb(aes)");
798                 ret += tcrypt_test("cbc(aes)");
799                 ret += tcrypt_test("lrw(aes)");
800                 ret += tcrypt_test("xts(aes)");
801                 ret += tcrypt_test("ctr(aes)");
802                 ret += tcrypt_test("rfc3686(ctr(aes))");
803                 break;
804
805         case 11:
806                 ret += tcrypt_test("sha384");
807                 break;
808
809         case 12:
810                 ret += tcrypt_test("sha512");
811                 break;
812
813         case 13:
814                 ret += tcrypt_test("deflate");
815                 break;
816
817         case 14:
818                 ret += tcrypt_test("ecb(cast5)");
819                 break;
820
821         case 15:
822                 ret += tcrypt_test("ecb(cast6)");
823                 break;
824
825         case 16:
826                 ret += tcrypt_test("ecb(arc4)");
827                 break;
828
829         case 17:
830                 ret += tcrypt_test("michael_mic");
831                 break;
832
833         case 18:
834                 ret += tcrypt_test("crc32c");
835                 break;
836
837         case 19:
838                 ret += tcrypt_test("ecb(tea)");
839                 break;
840
841         case 20:
842                 ret += tcrypt_test("ecb(xtea)");
843                 break;
844
845         case 21:
846                 ret += tcrypt_test("ecb(khazad)");
847                 break;
848
849         case 22:
850                 ret += tcrypt_test("wp512");
851                 break;
852
853         case 23:
854                 ret += tcrypt_test("wp384");
855                 break;
856
857         case 24:
858                 ret += tcrypt_test("wp256");
859                 break;
860
861         case 25:
862                 ret += tcrypt_test("ecb(tnepres)");
863                 break;
864
865         case 26:
866                 ret += tcrypt_test("ecb(anubis)");
867                 ret += tcrypt_test("cbc(anubis)");
868                 break;
869
870         case 27:
871                 ret += tcrypt_test("tgr192");
872                 break;
873
874         case 28:
875
876                 ret += tcrypt_test("tgr160");
877                 break;
878
879         case 29:
880                 ret += tcrypt_test("tgr128");
881                 break;
882
883         case 30:
884                 ret += tcrypt_test("ecb(xeta)");
885                 break;
886
887         case 31:
888                 ret += tcrypt_test("pcbc(fcrypt)");
889                 break;
890
891         case 32:
892                 ret += tcrypt_test("ecb(camellia)");
893                 ret += tcrypt_test("cbc(camellia)");
894                 break;
895         case 33:
896                 ret += tcrypt_test("sha224");
897                 break;
898
899         case 34:
900                 ret += tcrypt_test("salsa20");
901                 break;
902
903         case 35:
904                 ret += tcrypt_test("gcm(aes)");
905                 break;
906
907         case 36:
908                 ret += tcrypt_test("lzo");
909                 break;
910
911         case 37:
912                 ret += tcrypt_test("ccm(aes)");
913                 break;
914
915         case 38:
916                 ret += tcrypt_test("cts(cbc(aes))");
917                 break;
918
919         case 39:
920                 ret += tcrypt_test("rmd128");
921                 break;
922
923         case 40:
924                 ret += tcrypt_test("rmd160");
925                 break;
926
927         case 41:
928                 ret += tcrypt_test("rmd256");
929                 break;
930
931         case 42:
932                 ret += tcrypt_test("rmd320");
933                 break;
934
935         case 43:
936                 ret += tcrypt_test("ecb(seed)");
937                 break;
938
939         case 44:
940                 ret += tcrypt_test("zlib");
941                 break;
942
943         case 45:
944                 ret += tcrypt_test("rfc4309(ccm(aes))");
945                 break;
946
947         case 100:
948                 ret += tcrypt_test("hmac(md5)");
949                 break;
950
951         case 101:
952                 ret += tcrypt_test("hmac(sha1)");
953                 break;
954
955         case 102:
956                 ret += tcrypt_test("hmac(sha256)");
957                 break;
958
959         case 103:
960                 ret += tcrypt_test("hmac(sha384)");
961                 break;
962
963         case 104:
964                 ret += tcrypt_test("hmac(sha512)");
965                 break;
966
967         case 105:
968                 ret += tcrypt_test("hmac(sha224)");
969                 break;
970
971         case 106:
972                 ret += tcrypt_test("xcbc(aes)");
973                 break;
974
975         case 107:
976                 ret += tcrypt_test("hmac(rmd128)");
977                 break;
978
979         case 108:
980                 ret += tcrypt_test("hmac(rmd160)");
981                 break;
982
983         case 109:
984                 ret += tcrypt_test("vmac(aes)");
985                 break;
986
987         case 150:
988                 ret += tcrypt_test("ansi_cprng");
989                 break;
990
991         case 151:
992                 ret += tcrypt_test("rfc4106(gcm(aes))");
993                 break;
994
995         case 200:
996                 test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
997                                 speed_template_16_24_32);
998                 test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
999                                 speed_template_16_24_32);
1000                 test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
1001                                 speed_template_16_24_32);
1002                 test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
1003                                 speed_template_16_24_32);
1004                 test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
1005                                 speed_template_32_40_48);
1006                 test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
1007                                 speed_template_32_40_48);
1008                 test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
1009                                 speed_template_32_48_64);
1010                 test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
1011                                 speed_template_32_48_64);
1012                 break;
1013
1014         case 201:
1015                 test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
1016                                 des3_speed_template, DES3_SPEED_VECTORS,
1017                                 speed_template_24);
1018                 test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
1019                                 des3_speed_template, DES3_SPEED_VECTORS,
1020                                 speed_template_24);
1021                 test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
1022                                 des3_speed_template, DES3_SPEED_VECTORS,
1023                                 speed_template_24);
1024                 test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
1025                                 des3_speed_template, DES3_SPEED_VECTORS,
1026                                 speed_template_24);
1027                 break;
1028
1029         case 202:
1030                 test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
1031                                 speed_template_16_24_32);
1032                 test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
1033                                 speed_template_16_24_32);
1034                 test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
1035                                 speed_template_16_24_32);
1036                 test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
1037                                 speed_template_16_24_32);
1038                 break;
1039
1040         case 203:
1041                 test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
1042                                   speed_template_8_32);
1043                 test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
1044                                   speed_template_8_32);
1045                 test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
1046                                   speed_template_8_32);
1047                 test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
1048                                   speed_template_8_32);
1049                 break;
1050
1051         case 204:
1052                 test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
1053                                   speed_template_8);
1054                 test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
1055                                   speed_template_8);
1056                 test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
1057                                   speed_template_8);
1058                 test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
1059                                   speed_template_8);
1060                 break;
1061
1062         case 205:
1063                 test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
1064                                 speed_template_16_24_32);
1065                 test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
1066                                 speed_template_16_24_32);
1067                 test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
1068                                 speed_template_16_24_32);
1069                 test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
1070                                 speed_template_16_24_32);
1071                 break;
1072
1073         case 206:
1074                 test_cipher_speed("salsa20", ENCRYPT, sec, NULL, 0,
1075                                   speed_template_16_32);
1076                 break;
1077
1078         case 300:
1079                 /* fall through */
1080
1081         case 301:
1082                 test_hash_speed("md4", sec, generic_hash_speed_template);
1083                 if (mode > 300 && mode < 400) break;
1084
1085         case 302:
1086                 test_hash_speed("md5", sec, generic_hash_speed_template);
1087                 if (mode > 300 && mode < 400) break;
1088
1089         case 303:
1090                 test_hash_speed("sha1", sec, generic_hash_speed_template);
1091                 if (mode > 300 && mode < 400) break;
1092
1093         case 304:
1094                 test_hash_speed("sha256", sec, generic_hash_speed_template);
1095                 if (mode > 300 && mode < 400) break;
1096
1097         case 305:
1098                 test_hash_speed("sha384", sec, generic_hash_speed_template);
1099                 if (mode > 300 && mode < 400) break;
1100
1101         case 306:
1102                 test_hash_speed("sha512", sec, generic_hash_speed_template);
1103                 if (mode > 300 && mode < 400) break;
1104
1105         case 307:
1106                 test_hash_speed("wp256", sec, generic_hash_speed_template);
1107                 if (mode > 300 && mode < 400) break;
1108
1109         case 308:
1110                 test_hash_speed("wp384", sec, generic_hash_speed_template);
1111                 if (mode > 300 && mode < 400) break;
1112
1113         case 309:
1114                 test_hash_speed("wp512", sec, generic_hash_speed_template);
1115                 if (mode > 300 && mode < 400) break;
1116
1117         case 310:
1118                 test_hash_speed("tgr128", sec, generic_hash_speed_template);
1119                 if (mode > 300 && mode < 400) break;
1120
1121         case 311:
1122                 test_hash_speed("tgr160", sec, generic_hash_speed_template);
1123                 if (mode > 300 && mode < 400) break;
1124
1125         case 312:
1126                 test_hash_speed("tgr192", sec, generic_hash_speed_template);
1127                 if (mode > 300 && mode < 400) break;
1128
1129         case 313:
1130                 test_hash_speed("sha224", sec, generic_hash_speed_template);
1131                 if (mode > 300 && mode < 400) break;
1132
1133         case 314:
1134                 test_hash_speed("rmd128", sec, generic_hash_speed_template);
1135                 if (mode > 300 && mode < 400) break;
1136
1137         case 315:
1138                 test_hash_speed("rmd160", sec, generic_hash_speed_template);
1139                 if (mode > 300 && mode < 400) break;
1140
1141         case 316:
1142                 test_hash_speed("rmd256", sec, generic_hash_speed_template);
1143                 if (mode > 300 && mode < 400) break;
1144
1145         case 317:
1146                 test_hash_speed("rmd320", sec, generic_hash_speed_template);
1147                 if (mode > 300 && mode < 400) break;
1148
1149         case 318:
1150                 test_hash_speed("ghash-generic", sec, hash_speed_template_16);
1151                 if (mode > 300 && mode < 400) break;
1152
1153         case 399:
1154                 break;
1155
1156         case 400:
1157                 /* fall through */
1158
1159         case 401:
1160                 test_ahash_speed("md4", sec, generic_hash_speed_template);
1161                 if (mode > 400 && mode < 500) break;
1162
1163         case 402:
1164                 test_ahash_speed("md5", sec, generic_hash_speed_template);
1165                 if (mode > 400 && mode < 500) break;
1166
1167         case 403:
1168                 test_ahash_speed("sha1", sec, generic_hash_speed_template);
1169                 if (mode > 400 && mode < 500) break;
1170
1171         case 404:
1172                 test_ahash_speed("sha256", sec, generic_hash_speed_template);
1173                 if (mode > 400 && mode < 500) break;
1174
1175         case 405:
1176                 test_ahash_speed("sha384", sec, generic_hash_speed_template);
1177                 if (mode > 400 && mode < 500) break;
1178
1179         case 406:
1180                 test_ahash_speed("sha512", sec, generic_hash_speed_template);
1181                 if (mode > 400 && mode < 500) break;
1182
1183         case 407:
1184                 test_ahash_speed("wp256", sec, generic_hash_speed_template);
1185                 if (mode > 400 && mode < 500) break;
1186
1187         case 408:
1188                 test_ahash_speed("wp384", sec, generic_hash_speed_template);
1189                 if (mode > 400 && mode < 500) break;
1190
1191         case 409:
1192                 test_ahash_speed("wp512", sec, generic_hash_speed_template);
1193                 if (mode > 400 && mode < 500) break;
1194
1195         case 410:
1196                 test_ahash_speed("tgr128", sec, generic_hash_speed_template);
1197                 if (mode > 400 && mode < 500) break;
1198
1199         case 411:
1200                 test_ahash_speed("tgr160", sec, generic_hash_speed_template);
1201                 if (mode > 400 && mode < 500) break;
1202
1203         case 412:
1204                 test_ahash_speed("tgr192", sec, generic_hash_speed_template);
1205                 if (mode > 400 && mode < 500) break;
1206
1207         case 413:
1208                 test_ahash_speed("sha224", sec, generic_hash_speed_template);
1209                 if (mode > 400 && mode < 500) break;
1210
1211         case 414:
1212                 test_ahash_speed("rmd128", sec, generic_hash_speed_template);
1213                 if (mode > 400 && mode < 500) break;
1214
1215         case 415:
1216                 test_ahash_speed("rmd160", sec, generic_hash_speed_template);
1217                 if (mode > 400 && mode < 500) break;
1218
1219         case 416:
1220                 test_ahash_speed("rmd256", sec, generic_hash_speed_template);
1221                 if (mode > 400 && mode < 500) break;
1222
1223         case 417:
1224                 test_ahash_speed("rmd320", sec, generic_hash_speed_template);
1225                 if (mode > 400 && mode < 500) break;
1226
1227         case 499:
1228                 break;
1229
1230         case 1000:
1231                 test_available();
1232                 break;
1233         }
1234
1235         return ret;
1236 }
1237
1238 static int do_alg_test(const char *alg, u32 type, u32 mask)
1239 {
1240         return crypto_has_alg(alg, type, mask ?: CRYPTO_ALG_TYPE_MASK) ?
1241                0 : -ENOENT;
1242 }
1243
1244 static int __init tcrypt_mod_init(void)
1245 {
1246         int err = -ENOMEM;
1247         int i;
1248
1249         for (i = 0; i < TVMEMSIZE; i++) {
1250                 tvmem[i] = (void *)__get_free_page(GFP_KERNEL);
1251                 if (!tvmem[i])
1252                         goto err_free_tv;
1253         }
1254
1255         if (alg)
1256                 err = do_alg_test(alg, type, mask);
1257         else
1258                 err = do_test(mode);
1259
1260         if (err) {
1261                 printk(KERN_ERR "tcrypt: one or more tests failed!\n");
1262                 goto err_free_tv;
1263         }
1264
1265         /* We intentionaly return -EAGAIN to prevent keeping the module,
1266          * unless we're running in fips mode. It does all its work from
1267          * init() and doesn't offer any runtime functionality, but in
1268          * the fips case, checking for a successful load is helpful.
1269          * => we don't need it in the memory, do we?
1270          *                                        -- mludvig
1271          */
1272         if (!fips_enabled)
1273                 err = -EAGAIN;
1274
1275 err_free_tv:
1276         for (i = 0; i < TVMEMSIZE && tvmem[i]; i++)
1277                 free_page((unsigned long)tvmem[i]);
1278
1279         return err;
1280 }
1281
1282 /*
1283  * If an init function is provided, an exit function must also be provided
1284  * to allow module unload.
1285  */
1286 static void __exit tcrypt_mod_fini(void) { }
1287
1288 module_init(tcrypt_mod_init);
1289 module_exit(tcrypt_mod_fini);
1290
1291 module_param(alg, charp, 0);
1292 module_param(type, uint, 0);
1293 module_param(mask, uint, 0);
1294 module_param(mode, int, 0);
1295 module_param(sec, uint, 0);
1296 MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
1297                       "(defaults to zero which uses CPU cycles instead)");
1298
1299 MODULE_LICENSE("GPL");
1300 MODULE_DESCRIPTION("Quick & dirty crypto testing module");
1301 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");