ARM: qcom: add description of KPSS WDT for IPQ8064
[linux-2.6-microblaze.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  * return a string with the driver name
52  */
53 #define get_driver_name(tfm_type, tfm) crypto_tfm_alg_driver_name(tfm_type ## _tfm(tfm))
54
55 /*
56  * Used by test_cipher_speed()
57  */
58 static unsigned int sec;
59
60 static char *alg = NULL;
61 static u32 type;
62 static u32 mask;
63 static int mode;
64 static char *tvmem[TVMEMSIZE];
65
66 static char *check[] = {
67         "des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256",
68         "blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
69         "cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
70         "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta",  "fcrypt",
71         "camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
72         "lzo", "cts", "zlib", NULL
73 };
74
75 static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc,
76                                struct scatterlist *sg, int blen, int secs)
77 {
78         unsigned long start, end;
79         int bcount;
80         int ret;
81
82         for (start = jiffies, end = start + secs * HZ, bcount = 0;
83              time_before(jiffies, end); bcount++) {
84                 if (enc)
85                         ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
86                 else
87                         ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
88
89                 if (ret)
90                         return ret;
91         }
92
93         printk("%d operations in %d seconds (%ld bytes)\n",
94                bcount, secs, (long)bcount * blen);
95         return 0;
96 }
97
98 static int test_cipher_cycles(struct blkcipher_desc *desc, int enc,
99                               struct scatterlist *sg, int blen)
100 {
101         unsigned long cycles = 0;
102         int ret = 0;
103         int i;
104
105         local_irq_disable();
106
107         /* Warm-up run. */
108         for (i = 0; i < 4; i++) {
109                 if (enc)
110                         ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
111                 else
112                         ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
113
114                 if (ret)
115                         goto out;
116         }
117
118         /* The real thing. */
119         for (i = 0; i < 8; i++) {
120                 cycles_t start, end;
121
122                 start = get_cycles();
123                 if (enc)
124                         ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
125                 else
126                         ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
127                 end = get_cycles();
128
129                 if (ret)
130                         goto out;
131
132                 cycles += end - start;
133         }
134
135 out:
136         local_irq_enable();
137
138         if (ret == 0)
139                 printk("1 operation in %lu cycles (%d bytes)\n",
140                        (cycles + 4) / 8, blen);
141
142         return ret;
143 }
144
145 static int test_aead_jiffies(struct aead_request *req, int enc,
146                                 int blen, int secs)
147 {
148         unsigned long start, end;
149         int bcount;
150         int ret;
151
152         for (start = jiffies, end = start + secs * HZ, bcount = 0;
153              time_before(jiffies, end); bcount++) {
154                 if (enc)
155                         ret = crypto_aead_encrypt(req);
156                 else
157                         ret = crypto_aead_decrypt(req);
158
159                 if (ret)
160                         return ret;
161         }
162
163         printk("%d operations in %d seconds (%ld bytes)\n",
164                bcount, secs, (long)bcount * blen);
165         return 0;
166 }
167
168 static int test_aead_cycles(struct aead_request *req, int enc, int blen)
169 {
170         unsigned long cycles = 0;
171         int ret = 0;
172         int i;
173
174         local_irq_disable();
175
176         /* Warm-up run. */
177         for (i = 0; i < 4; i++) {
178                 if (enc)
179                         ret = crypto_aead_encrypt(req);
180                 else
181                         ret = crypto_aead_decrypt(req);
182
183                 if (ret)
184                         goto out;
185         }
186
187         /* The real thing. */
188         for (i = 0; i < 8; i++) {
189                 cycles_t start, end;
190
191                 start = get_cycles();
192                 if (enc)
193                         ret = crypto_aead_encrypt(req);
194                 else
195                         ret = crypto_aead_decrypt(req);
196                 end = get_cycles();
197
198                 if (ret)
199                         goto out;
200
201                 cycles += end - start;
202         }
203
204 out:
205         local_irq_enable();
206
207         if (ret == 0)
208                 printk("1 operation in %lu cycles (%d bytes)\n",
209                        (cycles + 4) / 8, blen);
210
211         return ret;
212 }
213
214 static u32 block_sizes[] = { 16, 64, 256, 1024, 8192, 0 };
215 static u32 aead_sizes[] = { 16, 64, 256, 512, 1024, 2048, 4096, 8192, 0 };
216
217 #define XBUFSIZE 8
218 #define MAX_IVLEN 32
219
220 static int testmgr_alloc_buf(char *buf[XBUFSIZE])
221 {
222         int i;
223
224         for (i = 0; i < XBUFSIZE; i++) {
225                 buf[i] = (void *)__get_free_page(GFP_KERNEL);
226                 if (!buf[i])
227                         goto err_free_buf;
228         }
229
230         return 0;
231
232 err_free_buf:
233         while (i-- > 0)
234                 free_page((unsigned long)buf[i]);
235
236         return -ENOMEM;
237 }
238
239 static void testmgr_free_buf(char *buf[XBUFSIZE])
240 {
241         int i;
242
243         for (i = 0; i < XBUFSIZE; i++)
244                 free_page((unsigned long)buf[i]);
245 }
246
247 static void sg_init_aead(struct scatterlist *sg, char *xbuf[XBUFSIZE],
248                         unsigned int buflen)
249 {
250         int np = (buflen + PAGE_SIZE - 1)/PAGE_SIZE;
251         int k, rem;
252
253         if (np > XBUFSIZE) {
254                 rem = PAGE_SIZE;
255                 np = XBUFSIZE;
256         } else {
257                 rem = buflen % PAGE_SIZE;
258         }
259
260         sg_init_table(sg, np);
261         np--;
262         for (k = 0; k < np; k++)
263                 sg_set_buf(&sg[k], xbuf[k], PAGE_SIZE);
264
265         sg_set_buf(&sg[k], xbuf[k], rem);
266 }
267
268 static void test_aead_speed(const char *algo, int enc, unsigned int secs,
269                             struct aead_speed_template *template,
270                             unsigned int tcount, u8 authsize,
271                             unsigned int aad_size, u8 *keysize)
272 {
273         unsigned int i, j;
274         struct crypto_aead *tfm;
275         int ret = -ENOMEM;
276         const char *key;
277         struct aead_request *req;
278         struct scatterlist *sg;
279         struct scatterlist *asg;
280         struct scatterlist *sgout;
281         const char *e;
282         void *assoc;
283         char *iv;
284         char *xbuf[XBUFSIZE];
285         char *xoutbuf[XBUFSIZE];
286         char *axbuf[XBUFSIZE];
287         unsigned int *b_size;
288         unsigned int iv_len;
289
290         iv = kzalloc(MAX_IVLEN, GFP_KERNEL);
291         if (!iv)
292                 return;
293
294         if (aad_size >= PAGE_SIZE) {
295                 pr_err("associate data length (%u) too big\n", aad_size);
296                 goto out_noxbuf;
297         }
298
299         if (enc == ENCRYPT)
300                 e = "encryption";
301         else
302                 e = "decryption";
303
304         if (testmgr_alloc_buf(xbuf))
305                 goto out_noxbuf;
306         if (testmgr_alloc_buf(axbuf))
307                 goto out_noaxbuf;
308         if (testmgr_alloc_buf(xoutbuf))
309                 goto out_nooutbuf;
310
311         sg = kmalloc(sizeof(*sg) * 8 * 3, GFP_KERNEL);
312         if (!sg)
313                 goto out_nosg;
314         asg = &sg[8];
315         sgout = &asg[8];
316
317         tfm = crypto_alloc_aead(algo, 0, 0);
318
319         if (IS_ERR(tfm)) {
320                 pr_err("alg: aead: Failed to load transform for %s: %ld\n", algo,
321                        PTR_ERR(tfm));
322                 goto out_notfm;
323         }
324
325         printk(KERN_INFO "\ntesting speed of %s (%s) %s\n", algo,
326                         get_driver_name(crypto_aead, tfm), e);
327
328         req = aead_request_alloc(tfm, GFP_KERNEL);
329         if (!req) {
330                 pr_err("alg: aead: Failed to allocate request for %s\n",
331                        algo);
332                 goto out_noreq;
333         }
334
335         i = 0;
336         do {
337                 b_size = aead_sizes;
338                 do {
339                         assoc = axbuf[0];
340                         memset(assoc, 0xff, aad_size);
341                         sg_init_one(&asg[0], assoc, aad_size);
342
343                         if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
344                                 pr_err("template (%u) too big for tvmem (%lu)\n",
345                                        *keysize + *b_size,
346                                         TVMEMSIZE * PAGE_SIZE);
347                                 goto out;
348                         }
349
350                         key = tvmem[0];
351                         for (j = 0; j < tcount; j++) {
352                                 if (template[j].klen == *keysize) {
353                                         key = template[j].key;
354                                         break;
355                                 }
356                         }
357                         ret = crypto_aead_setkey(tfm, key, *keysize);
358                         ret = crypto_aead_setauthsize(tfm, authsize);
359
360                         iv_len = crypto_aead_ivsize(tfm);
361                         if (iv_len)
362                                 memset(iv, 0xff, iv_len);
363
364                         crypto_aead_clear_flags(tfm, ~0);
365                         printk(KERN_INFO "test %u (%d bit key, %d byte blocks): ",
366                                         i, *keysize * 8, *b_size);
367
368
369                         memset(tvmem[0], 0xff, PAGE_SIZE);
370
371                         if (ret) {
372                                 pr_err("setkey() failed flags=%x\n",
373                                                 crypto_aead_get_flags(tfm));
374                                 goto out;
375                         }
376
377                         sg_init_aead(&sg[0], xbuf,
378                                     *b_size + (enc ? authsize : 0));
379
380                         sg_init_aead(&sgout[0], xoutbuf,
381                                     *b_size + (enc ? authsize : 0));
382
383                         aead_request_set_crypt(req, sg, sgout, *b_size, iv);
384                         aead_request_set_assoc(req, asg, aad_size);
385
386                         if (secs)
387                                 ret = test_aead_jiffies(req, enc, *b_size,
388                                                         secs);
389                         else
390                                 ret = test_aead_cycles(req, enc, *b_size);
391
392                         if (ret) {
393                                 pr_err("%s() failed return code=%d\n", e, ret);
394                                 break;
395                         }
396                         b_size++;
397                         i++;
398                 } while (*b_size);
399                 keysize++;
400         } while (*keysize);
401
402 out:
403         aead_request_free(req);
404 out_noreq:
405         crypto_free_aead(tfm);
406 out_notfm:
407         kfree(sg);
408 out_nosg:
409         testmgr_free_buf(xoutbuf);
410 out_nooutbuf:
411         testmgr_free_buf(axbuf);
412 out_noaxbuf:
413         testmgr_free_buf(xbuf);
414 out_noxbuf:
415         kfree(iv);
416         return;
417 }
418
419 static void test_cipher_speed(const char *algo, int enc, unsigned int secs,
420                               struct cipher_speed_template *template,
421                               unsigned int tcount, u8 *keysize)
422 {
423         unsigned int ret, i, j, iv_len;
424         const char *key;
425         char iv[128];
426         struct crypto_blkcipher *tfm;
427         struct blkcipher_desc desc;
428         const char *e;
429         u32 *b_size;
430
431         if (enc == ENCRYPT)
432                 e = "encryption";
433         else
434                 e = "decryption";
435
436         tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);
437
438         if (IS_ERR(tfm)) {
439                 printk("failed to load transform for %s: %ld\n", algo,
440                        PTR_ERR(tfm));
441                 return;
442         }
443         desc.tfm = tfm;
444         desc.flags = 0;
445
446         printk(KERN_INFO "\ntesting speed of %s (%s) %s\n", algo,
447                         get_driver_name(crypto_blkcipher, tfm), e);
448
449         i = 0;
450         do {
451
452                 b_size = block_sizes;
453                 do {
454                         struct scatterlist sg[TVMEMSIZE];
455
456                         if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
457                                 printk("template (%u) too big for "
458                                        "tvmem (%lu)\n", *keysize + *b_size,
459                                        TVMEMSIZE * PAGE_SIZE);
460                                 goto out;
461                         }
462
463                         printk("test %u (%d bit key, %d byte blocks): ", i,
464                                         *keysize * 8, *b_size);
465
466                         memset(tvmem[0], 0xff, PAGE_SIZE);
467
468                         /* set key, plain text and IV */
469                         key = tvmem[0];
470                         for (j = 0; j < tcount; j++) {
471                                 if (template[j].klen == *keysize) {
472                                         key = template[j].key;
473                                         break;
474                                 }
475                         }
476
477                         ret = crypto_blkcipher_setkey(tfm, key, *keysize);
478                         if (ret) {
479                                 printk("setkey() failed flags=%x\n",
480                                                 crypto_blkcipher_get_flags(tfm));
481                                 goto out;
482                         }
483
484                         sg_init_table(sg, TVMEMSIZE);
485                         sg_set_buf(sg, tvmem[0] + *keysize,
486                                    PAGE_SIZE - *keysize);
487                         for (j = 1; j < TVMEMSIZE; j++) {
488                                 sg_set_buf(sg + j, tvmem[j], PAGE_SIZE);
489                                 memset (tvmem[j], 0xff, PAGE_SIZE);
490                         }
491
492                         iv_len = crypto_blkcipher_ivsize(tfm);
493                         if (iv_len) {
494                                 memset(&iv, 0xff, iv_len);
495                                 crypto_blkcipher_set_iv(tfm, iv, iv_len);
496                         }
497
498                         if (secs)
499                                 ret = test_cipher_jiffies(&desc, enc, sg,
500                                                           *b_size, secs);
501                         else
502                                 ret = test_cipher_cycles(&desc, enc, sg,
503                                                          *b_size);
504
505                         if (ret) {
506                                 printk("%s() failed flags=%x\n", e, desc.flags);
507                                 break;
508                         }
509                         b_size++;
510                         i++;
511                 } while (*b_size);
512                 keysize++;
513         } while (*keysize);
514
515 out:
516         crypto_free_blkcipher(tfm);
517 }
518
519 static int test_hash_jiffies_digest(struct hash_desc *desc,
520                                     struct scatterlist *sg, int blen,
521                                     char *out, int secs)
522 {
523         unsigned long start, end;
524         int bcount;
525         int ret;
526
527         for (start = jiffies, end = start + secs * HZ, bcount = 0;
528              time_before(jiffies, end); bcount++) {
529                 ret = crypto_hash_digest(desc, sg, blen, out);
530                 if (ret)
531                         return ret;
532         }
533
534         printk("%6u opers/sec, %9lu bytes/sec\n",
535                bcount / secs, ((long)bcount * blen) / secs);
536
537         return 0;
538 }
539
540 static int test_hash_jiffies(struct hash_desc *desc, struct scatterlist *sg,
541                              int blen, int plen, char *out, int secs)
542 {
543         unsigned long start, end;
544         int bcount, pcount;
545         int ret;
546
547         if (plen == blen)
548                 return test_hash_jiffies_digest(desc, sg, blen, out, secs);
549
550         for (start = jiffies, end = start + secs * HZ, bcount = 0;
551              time_before(jiffies, end); bcount++) {
552                 ret = crypto_hash_init(desc);
553                 if (ret)
554                         return ret;
555                 for (pcount = 0; pcount < blen; pcount += plen) {
556                         ret = crypto_hash_update(desc, sg, plen);
557                         if (ret)
558                                 return ret;
559                 }
560                 /* we assume there is enough space in 'out' for the result */
561                 ret = crypto_hash_final(desc, out);
562                 if (ret)
563                         return ret;
564         }
565
566         printk("%6u opers/sec, %9lu bytes/sec\n",
567                bcount / secs, ((long)bcount * blen) / secs);
568
569         return 0;
570 }
571
572 static int test_hash_cycles_digest(struct hash_desc *desc,
573                                    struct scatterlist *sg, int blen, char *out)
574 {
575         unsigned long cycles = 0;
576         int i;
577         int ret;
578
579         local_irq_disable();
580
581         /* Warm-up run. */
582         for (i = 0; i < 4; i++) {
583                 ret = crypto_hash_digest(desc, sg, blen, out);
584                 if (ret)
585                         goto out;
586         }
587
588         /* The real thing. */
589         for (i = 0; i < 8; i++) {
590                 cycles_t start, end;
591
592                 start = get_cycles();
593
594                 ret = crypto_hash_digest(desc, sg, blen, out);
595                 if (ret)
596                         goto out;
597
598                 end = get_cycles();
599
600                 cycles += end - start;
601         }
602
603 out:
604         local_irq_enable();
605
606         if (ret)
607                 return ret;
608
609         printk("%6lu cycles/operation, %4lu cycles/byte\n",
610                cycles / 8, cycles / (8 * blen));
611
612         return 0;
613 }
614
615 static int test_hash_cycles(struct hash_desc *desc, struct scatterlist *sg,
616                             int blen, int plen, char *out)
617 {
618         unsigned long cycles = 0;
619         int i, pcount;
620         int ret;
621
622         if (plen == blen)
623                 return test_hash_cycles_digest(desc, sg, blen, out);
624
625         local_irq_disable();
626
627         /* Warm-up run. */
628         for (i = 0; i < 4; i++) {
629                 ret = crypto_hash_init(desc);
630                 if (ret)
631                         goto out;
632                 for (pcount = 0; pcount < blen; pcount += plen) {
633                         ret = crypto_hash_update(desc, sg, plen);
634                         if (ret)
635                                 goto out;
636                 }
637                 ret = crypto_hash_final(desc, out);
638                 if (ret)
639                         goto out;
640         }
641
642         /* The real thing. */
643         for (i = 0; i < 8; i++) {
644                 cycles_t start, end;
645
646                 start = get_cycles();
647
648                 ret = crypto_hash_init(desc);
649                 if (ret)
650                         goto out;
651                 for (pcount = 0; pcount < blen; pcount += plen) {
652                         ret = crypto_hash_update(desc, sg, plen);
653                         if (ret)
654                                 goto out;
655                 }
656                 ret = crypto_hash_final(desc, out);
657                 if (ret)
658                         goto out;
659
660                 end = get_cycles();
661
662                 cycles += end - start;
663         }
664
665 out:
666         local_irq_enable();
667
668         if (ret)
669                 return ret;
670
671         printk("%6lu cycles/operation, %4lu cycles/byte\n",
672                cycles / 8, cycles / (8 * blen));
673
674         return 0;
675 }
676
677 static void test_hash_sg_init(struct scatterlist *sg)
678 {
679         int i;
680
681         sg_init_table(sg, TVMEMSIZE);
682         for (i = 0; i < TVMEMSIZE; i++) {
683                 sg_set_buf(sg + i, tvmem[i], PAGE_SIZE);
684                 memset(tvmem[i], 0xff, PAGE_SIZE);
685         }
686 }
687
688 static void test_hash_speed(const char *algo, unsigned int secs,
689                             struct hash_speed *speed)
690 {
691         struct scatterlist sg[TVMEMSIZE];
692         struct crypto_hash *tfm;
693         struct hash_desc desc;
694         static char output[1024];
695         int i;
696         int ret;
697
698         tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
699
700         if (IS_ERR(tfm)) {
701                 printk(KERN_ERR "failed to load transform for %s: %ld\n", algo,
702                        PTR_ERR(tfm));
703                 return;
704         }
705
706         printk(KERN_INFO "\ntesting speed of %s (%s)\n", algo,
707                         get_driver_name(crypto_hash, tfm));
708
709         desc.tfm = tfm;
710         desc.flags = 0;
711
712         if (crypto_hash_digestsize(tfm) > sizeof(output)) {
713                 printk(KERN_ERR "digestsize(%u) > outputbuffer(%zu)\n",
714                        crypto_hash_digestsize(tfm), sizeof(output));
715                 goto out;
716         }
717
718         test_hash_sg_init(sg);
719         for (i = 0; speed[i].blen != 0; i++) {
720                 if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
721                         printk(KERN_ERR
722                                "template (%u) too big for tvmem (%lu)\n",
723                                speed[i].blen, TVMEMSIZE * PAGE_SIZE);
724                         goto out;
725                 }
726
727                 if (speed[i].klen)
728                         crypto_hash_setkey(tfm, tvmem[0], speed[i].klen);
729
730                 printk(KERN_INFO "test%3u "
731                        "(%5u byte blocks,%5u bytes per update,%4u updates): ",
732                        i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
733
734                 if (secs)
735                         ret = test_hash_jiffies(&desc, sg, speed[i].blen,
736                                                 speed[i].plen, output, secs);
737                 else
738                         ret = test_hash_cycles(&desc, sg, speed[i].blen,
739                                                speed[i].plen, output);
740
741                 if (ret) {
742                         printk(KERN_ERR "hashing failed ret=%d\n", ret);
743                         break;
744                 }
745         }
746
747 out:
748         crypto_free_hash(tfm);
749 }
750
751 struct tcrypt_result {
752         struct completion completion;
753         int err;
754 };
755
756 static void tcrypt_complete(struct crypto_async_request *req, int err)
757 {
758         struct tcrypt_result *res = req->data;
759
760         if (err == -EINPROGRESS)
761                 return;
762
763         res->err = err;
764         complete(&res->completion);
765 }
766
767 static inline int do_one_ahash_op(struct ahash_request *req, int ret)
768 {
769         if (ret == -EINPROGRESS || ret == -EBUSY) {
770                 struct tcrypt_result *tr = req->base.data;
771
772                 wait_for_completion(&tr->completion);
773                 reinit_completion(&tr->completion);
774                 ret = tr->err;
775         }
776         return ret;
777 }
778
779 static int test_ahash_jiffies_digest(struct ahash_request *req, int blen,
780                                      char *out, int secs)
781 {
782         unsigned long start, end;
783         int bcount;
784         int ret;
785
786         for (start = jiffies, end = start + secs * HZ, bcount = 0;
787              time_before(jiffies, end); bcount++) {
788                 ret = do_one_ahash_op(req, crypto_ahash_digest(req));
789                 if (ret)
790                         return ret;
791         }
792
793         printk("%6u opers/sec, %9lu bytes/sec\n",
794                bcount / secs, ((long)bcount * blen) / secs);
795
796         return 0;
797 }
798
799 static int test_ahash_jiffies(struct ahash_request *req, int blen,
800                               int plen, char *out, int secs)
801 {
802         unsigned long start, end;
803         int bcount, pcount;
804         int ret;
805
806         if (plen == blen)
807                 return test_ahash_jiffies_digest(req, blen, out, secs);
808
809         for (start = jiffies, end = start + secs * HZ, bcount = 0;
810              time_before(jiffies, end); bcount++) {
811                 ret = crypto_ahash_init(req);
812                 if (ret)
813                         return ret;
814                 for (pcount = 0; pcount < blen; pcount += plen) {
815                         ret = do_one_ahash_op(req, crypto_ahash_update(req));
816                         if (ret)
817                                 return ret;
818                 }
819                 /* we assume there is enough space in 'out' for the result */
820                 ret = do_one_ahash_op(req, crypto_ahash_final(req));
821                 if (ret)
822                         return ret;
823         }
824
825         pr_cont("%6u opers/sec, %9lu bytes/sec\n",
826                 bcount / secs, ((long)bcount * blen) / secs);
827
828         return 0;
829 }
830
831 static int test_ahash_cycles_digest(struct ahash_request *req, int blen,
832                                     char *out)
833 {
834         unsigned long cycles = 0;
835         int ret, i;
836
837         /* Warm-up run. */
838         for (i = 0; i < 4; i++) {
839                 ret = do_one_ahash_op(req, crypto_ahash_digest(req));
840                 if (ret)
841                         goto out;
842         }
843
844         /* The real thing. */
845         for (i = 0; i < 8; i++) {
846                 cycles_t start, end;
847
848                 start = get_cycles();
849
850                 ret = do_one_ahash_op(req, crypto_ahash_digest(req));
851                 if (ret)
852                         goto out;
853
854                 end = get_cycles();
855
856                 cycles += end - start;
857         }
858
859 out:
860         if (ret)
861                 return ret;
862
863         pr_cont("%6lu cycles/operation, %4lu cycles/byte\n",
864                 cycles / 8, cycles / (8 * blen));
865
866         return 0;
867 }
868
869 static int test_ahash_cycles(struct ahash_request *req, int blen,
870                              int plen, char *out)
871 {
872         unsigned long cycles = 0;
873         int i, pcount, ret;
874
875         if (plen == blen)
876                 return test_ahash_cycles_digest(req, blen, out);
877
878         /* Warm-up run. */
879         for (i = 0; i < 4; i++) {
880                 ret = crypto_ahash_init(req);
881                 if (ret)
882                         goto out;
883                 for (pcount = 0; pcount < blen; pcount += plen) {
884                         ret = do_one_ahash_op(req, crypto_ahash_update(req));
885                         if (ret)
886                                 goto out;
887                 }
888                 ret = do_one_ahash_op(req, crypto_ahash_final(req));
889                 if (ret)
890                         goto out;
891         }
892
893         /* The real thing. */
894         for (i = 0; i < 8; i++) {
895                 cycles_t start, end;
896
897                 start = get_cycles();
898
899                 ret = crypto_ahash_init(req);
900                 if (ret)
901                         goto out;
902                 for (pcount = 0; pcount < blen; pcount += plen) {
903                         ret = do_one_ahash_op(req, crypto_ahash_update(req));
904                         if (ret)
905                                 goto out;
906                 }
907                 ret = do_one_ahash_op(req, crypto_ahash_final(req));
908                 if (ret)
909                         goto out;
910
911                 end = get_cycles();
912
913                 cycles += end - start;
914         }
915
916 out:
917         if (ret)
918                 return ret;
919
920         pr_cont("%6lu cycles/operation, %4lu cycles/byte\n",
921                 cycles / 8, cycles / (8 * blen));
922
923         return 0;
924 }
925
926 static void test_ahash_speed(const char *algo, unsigned int secs,
927                              struct hash_speed *speed)
928 {
929         struct scatterlist sg[TVMEMSIZE];
930         struct tcrypt_result tresult;
931         struct ahash_request *req;
932         struct crypto_ahash *tfm;
933         static char output[1024];
934         int i, ret;
935
936         tfm = crypto_alloc_ahash(algo, 0, 0);
937         if (IS_ERR(tfm)) {
938                 pr_err("failed to load transform for %s: %ld\n",
939                        algo, PTR_ERR(tfm));
940                 return;
941         }
942
943         printk(KERN_INFO "\ntesting speed of async %s (%s)\n", algo,
944                         get_driver_name(crypto_ahash, tfm));
945
946         if (crypto_ahash_digestsize(tfm) > sizeof(output)) {
947                 pr_err("digestsize(%u) > outputbuffer(%zu)\n",
948                        crypto_ahash_digestsize(tfm), sizeof(output));
949                 goto out;
950         }
951
952         test_hash_sg_init(sg);
953         req = ahash_request_alloc(tfm, GFP_KERNEL);
954         if (!req) {
955                 pr_err("ahash request allocation failure\n");
956                 goto out;
957         }
958
959         init_completion(&tresult.completion);
960         ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
961                                    tcrypt_complete, &tresult);
962
963         for (i = 0; speed[i].blen != 0; i++) {
964                 if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
965                         pr_err("template (%u) too big for tvmem (%lu)\n",
966                                speed[i].blen, TVMEMSIZE * PAGE_SIZE);
967                         break;
968                 }
969
970                 pr_info("test%3u "
971                         "(%5u byte blocks,%5u bytes per update,%4u updates): ",
972                         i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
973
974                 ahash_request_set_crypt(req, sg, output, speed[i].plen);
975
976                 if (secs)
977                         ret = test_ahash_jiffies(req, speed[i].blen,
978                                                  speed[i].plen, output, secs);
979                 else
980                         ret = test_ahash_cycles(req, speed[i].blen,
981                                                 speed[i].plen, output);
982
983                 if (ret) {
984                         pr_err("hashing failed ret=%d\n", ret);
985                         break;
986                 }
987         }
988
989         ahash_request_free(req);
990
991 out:
992         crypto_free_ahash(tfm);
993 }
994
995 static inline int do_one_acipher_op(struct ablkcipher_request *req, int ret)
996 {
997         if (ret == -EINPROGRESS || ret == -EBUSY) {
998                 struct tcrypt_result *tr = req->base.data;
999
1000                 wait_for_completion(&tr->completion);
1001                 reinit_completion(&tr->completion);
1002                 ret = tr->err;
1003         }
1004
1005         return ret;
1006 }
1007
1008 static int test_acipher_jiffies(struct ablkcipher_request *req, int enc,
1009                                 int blen, int secs)
1010 {
1011         unsigned long start, end;
1012         int bcount;
1013         int ret;
1014
1015         for (start = jiffies, end = start + secs * HZ, bcount = 0;
1016              time_before(jiffies, end); bcount++) {
1017                 if (enc)
1018                         ret = do_one_acipher_op(req,
1019                                                 crypto_ablkcipher_encrypt(req));
1020                 else
1021                         ret = do_one_acipher_op(req,
1022                                                 crypto_ablkcipher_decrypt(req));
1023
1024                 if (ret)
1025                         return ret;
1026         }
1027
1028         pr_cont("%d operations in %d seconds (%ld bytes)\n",
1029                 bcount, secs, (long)bcount * blen);
1030         return 0;
1031 }
1032
1033 static int test_acipher_cycles(struct ablkcipher_request *req, int enc,
1034                                int blen)
1035 {
1036         unsigned long cycles = 0;
1037         int ret = 0;
1038         int i;
1039
1040         /* Warm-up run. */
1041         for (i = 0; i < 4; i++) {
1042                 if (enc)
1043                         ret = do_one_acipher_op(req,
1044                                                 crypto_ablkcipher_encrypt(req));
1045                 else
1046                         ret = do_one_acipher_op(req,
1047                                                 crypto_ablkcipher_decrypt(req));
1048
1049                 if (ret)
1050                         goto out;
1051         }
1052
1053         /* The real thing. */
1054         for (i = 0; i < 8; i++) {
1055                 cycles_t start, end;
1056
1057                 start = get_cycles();
1058                 if (enc)
1059                         ret = do_one_acipher_op(req,
1060                                                 crypto_ablkcipher_encrypt(req));
1061                 else
1062                         ret = do_one_acipher_op(req,
1063                                                 crypto_ablkcipher_decrypt(req));
1064                 end = get_cycles();
1065
1066                 if (ret)
1067                         goto out;
1068
1069                 cycles += end - start;
1070         }
1071
1072 out:
1073         if (ret == 0)
1074                 pr_cont("1 operation in %lu cycles (%d bytes)\n",
1075                         (cycles + 4) / 8, blen);
1076
1077         return ret;
1078 }
1079
1080 static void test_acipher_speed(const char *algo, int enc, unsigned int secs,
1081                                struct cipher_speed_template *template,
1082                                unsigned int tcount, u8 *keysize)
1083 {
1084         unsigned int ret, i, j, k, iv_len;
1085         struct tcrypt_result tresult;
1086         const char *key;
1087         char iv[128];
1088         struct ablkcipher_request *req;
1089         struct crypto_ablkcipher *tfm;
1090         const char *e;
1091         u32 *b_size;
1092
1093         if (enc == ENCRYPT)
1094                 e = "encryption";
1095         else
1096                 e = "decryption";
1097
1098         init_completion(&tresult.completion);
1099
1100         tfm = crypto_alloc_ablkcipher(algo, 0, 0);
1101
1102         if (IS_ERR(tfm)) {
1103                 pr_err("failed to load transform for %s: %ld\n", algo,
1104                        PTR_ERR(tfm));
1105                 return;
1106         }
1107
1108         pr_info("\ntesting speed of async %s (%s) %s\n", algo,
1109                         get_driver_name(crypto_ablkcipher, tfm), e);
1110
1111         req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
1112         if (!req) {
1113                 pr_err("tcrypt: skcipher: Failed to allocate request for %s\n",
1114                        algo);
1115                 goto out;
1116         }
1117
1118         ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1119                                         tcrypt_complete, &tresult);
1120
1121         i = 0;
1122         do {
1123                 b_size = block_sizes;
1124
1125                 do {
1126                         struct scatterlist sg[TVMEMSIZE];
1127
1128                         if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
1129                                 pr_err("template (%u) too big for "
1130                                        "tvmem (%lu)\n", *keysize + *b_size,
1131                                        TVMEMSIZE * PAGE_SIZE);
1132                                 goto out_free_req;
1133                         }
1134
1135                         pr_info("test %u (%d bit key, %d byte blocks): ", i,
1136                                 *keysize * 8, *b_size);
1137
1138                         memset(tvmem[0], 0xff, PAGE_SIZE);
1139
1140                         /* set key, plain text and IV */
1141                         key = tvmem[0];
1142                         for (j = 0; j < tcount; j++) {
1143                                 if (template[j].klen == *keysize) {
1144                                         key = template[j].key;
1145                                         break;
1146                                 }
1147                         }
1148
1149                         crypto_ablkcipher_clear_flags(tfm, ~0);
1150
1151                         ret = crypto_ablkcipher_setkey(tfm, key, *keysize);
1152                         if (ret) {
1153                                 pr_err("setkey() failed flags=%x\n",
1154                                         crypto_ablkcipher_get_flags(tfm));
1155                                 goto out_free_req;
1156                         }
1157
1158                         k = *keysize + *b_size;
1159                         sg_init_table(sg, DIV_ROUND_UP(k, PAGE_SIZE));
1160
1161                         if (k > PAGE_SIZE) {
1162                                 sg_set_buf(sg, tvmem[0] + *keysize,
1163                                    PAGE_SIZE - *keysize);
1164                                 k -= PAGE_SIZE;
1165                                 j = 1;
1166                                 while (k > PAGE_SIZE) {
1167                                         sg_set_buf(sg + j, tvmem[j], PAGE_SIZE);
1168                                         memset(tvmem[j], 0xff, PAGE_SIZE);
1169                                         j++;
1170                                         k -= PAGE_SIZE;
1171                                 }
1172                                 sg_set_buf(sg + j, tvmem[j], k);
1173                                 memset(tvmem[j], 0xff, k);
1174                         } else {
1175                                 sg_set_buf(sg, tvmem[0] + *keysize, *b_size);
1176                         }
1177
1178                         iv_len = crypto_ablkcipher_ivsize(tfm);
1179                         if (iv_len)
1180                                 memset(&iv, 0xff, iv_len);
1181
1182                         ablkcipher_request_set_crypt(req, sg, sg, *b_size, iv);
1183
1184                         if (secs)
1185                                 ret = test_acipher_jiffies(req, enc,
1186                                                            *b_size, secs);
1187                         else
1188                                 ret = test_acipher_cycles(req, enc,
1189                                                           *b_size);
1190
1191                         if (ret) {
1192                                 pr_err("%s() failed flags=%x\n", e,
1193                                         crypto_ablkcipher_get_flags(tfm));
1194                                 break;
1195                         }
1196                         b_size++;
1197                         i++;
1198                 } while (*b_size);
1199                 keysize++;
1200         } while (*keysize);
1201
1202 out_free_req:
1203         ablkcipher_request_free(req);
1204 out:
1205         crypto_free_ablkcipher(tfm);
1206 }
1207
1208 static void test_available(void)
1209 {
1210         char **name = check;
1211
1212         while (*name) {
1213                 printk("alg %s ", *name);
1214                 printk(crypto_has_alg(*name, 0, 0) ?
1215                        "found\n" : "not found\n");
1216                 name++;
1217         }
1218 }
1219
1220 static inline int tcrypt_test(const char *alg)
1221 {
1222         int ret;
1223
1224         ret = alg_test(alg, alg, 0, 0);
1225         /* non-fips algs return -EINVAL in fips mode */
1226         if (fips_enabled && ret == -EINVAL)
1227                 ret = 0;
1228         return ret;
1229 }
1230
1231 static int do_test(const char *alg, u32 type, u32 mask, int m)
1232 {
1233         int i;
1234         int ret = 0;
1235
1236         switch (m) {
1237         case 0:
1238                 if (alg) {
1239                         if (!crypto_has_alg(alg, type,
1240                                             mask ?: CRYPTO_ALG_TYPE_MASK))
1241                                 ret = -ENOENT;
1242                         break;
1243                 }
1244
1245                 for (i = 1; i < 200; i++)
1246                         ret += do_test(NULL, 0, 0, i);
1247                 break;
1248
1249         case 1:
1250                 ret += tcrypt_test("md5");
1251                 break;
1252
1253         case 2:
1254                 ret += tcrypt_test("sha1");
1255                 break;
1256
1257         case 3:
1258                 ret += tcrypt_test("ecb(des)");
1259                 ret += tcrypt_test("cbc(des)");
1260                 ret += tcrypt_test("ctr(des)");
1261                 break;
1262
1263         case 4:
1264                 ret += tcrypt_test("ecb(des3_ede)");
1265                 ret += tcrypt_test("cbc(des3_ede)");
1266                 ret += tcrypt_test("ctr(des3_ede)");
1267                 break;
1268
1269         case 5:
1270                 ret += tcrypt_test("md4");
1271                 break;
1272
1273         case 6:
1274                 ret += tcrypt_test("sha256");
1275                 break;
1276
1277         case 7:
1278                 ret += tcrypt_test("ecb(blowfish)");
1279                 ret += tcrypt_test("cbc(blowfish)");
1280                 ret += tcrypt_test("ctr(blowfish)");
1281                 break;
1282
1283         case 8:
1284                 ret += tcrypt_test("ecb(twofish)");
1285                 ret += tcrypt_test("cbc(twofish)");
1286                 ret += tcrypt_test("ctr(twofish)");
1287                 ret += tcrypt_test("lrw(twofish)");
1288                 ret += tcrypt_test("xts(twofish)");
1289                 break;
1290
1291         case 9:
1292                 ret += tcrypt_test("ecb(serpent)");
1293                 ret += tcrypt_test("cbc(serpent)");
1294                 ret += tcrypt_test("ctr(serpent)");
1295                 ret += tcrypt_test("lrw(serpent)");
1296                 ret += tcrypt_test("xts(serpent)");
1297                 break;
1298
1299         case 10:
1300                 ret += tcrypt_test("ecb(aes)");
1301                 ret += tcrypt_test("cbc(aes)");
1302                 ret += tcrypt_test("lrw(aes)");
1303                 ret += tcrypt_test("xts(aes)");
1304                 ret += tcrypt_test("ctr(aes)");
1305                 ret += tcrypt_test("rfc3686(ctr(aes))");
1306                 break;
1307
1308         case 11:
1309                 ret += tcrypt_test("sha384");
1310                 break;
1311
1312         case 12:
1313                 ret += tcrypt_test("sha512");
1314                 break;
1315
1316         case 13:
1317                 ret += tcrypt_test("deflate");
1318                 break;
1319
1320         case 14:
1321                 ret += tcrypt_test("ecb(cast5)");
1322                 ret += tcrypt_test("cbc(cast5)");
1323                 ret += tcrypt_test("ctr(cast5)");
1324                 break;
1325
1326         case 15:
1327                 ret += tcrypt_test("ecb(cast6)");
1328                 ret += tcrypt_test("cbc(cast6)");
1329                 ret += tcrypt_test("ctr(cast6)");
1330                 ret += tcrypt_test("lrw(cast6)");
1331                 ret += tcrypt_test("xts(cast6)");
1332                 break;
1333
1334         case 16:
1335                 ret += tcrypt_test("ecb(arc4)");
1336                 break;
1337
1338         case 17:
1339                 ret += tcrypt_test("michael_mic");
1340                 break;
1341
1342         case 18:
1343                 ret += tcrypt_test("crc32c");
1344                 break;
1345
1346         case 19:
1347                 ret += tcrypt_test("ecb(tea)");
1348                 break;
1349
1350         case 20:
1351                 ret += tcrypt_test("ecb(xtea)");
1352                 break;
1353
1354         case 21:
1355                 ret += tcrypt_test("ecb(khazad)");
1356                 break;
1357
1358         case 22:
1359                 ret += tcrypt_test("wp512");
1360                 break;
1361
1362         case 23:
1363                 ret += tcrypt_test("wp384");
1364                 break;
1365
1366         case 24:
1367                 ret += tcrypt_test("wp256");
1368                 break;
1369
1370         case 25:
1371                 ret += tcrypt_test("ecb(tnepres)");
1372                 break;
1373
1374         case 26:
1375                 ret += tcrypt_test("ecb(anubis)");
1376                 ret += tcrypt_test("cbc(anubis)");
1377                 break;
1378
1379         case 27:
1380                 ret += tcrypt_test("tgr192");
1381                 break;
1382
1383         case 28:
1384                 ret += tcrypt_test("tgr160");
1385                 break;
1386
1387         case 29:
1388                 ret += tcrypt_test("tgr128");
1389                 break;
1390
1391         case 30:
1392                 ret += tcrypt_test("ecb(xeta)");
1393                 break;
1394
1395         case 31:
1396                 ret += tcrypt_test("pcbc(fcrypt)");
1397                 break;
1398
1399         case 32:
1400                 ret += tcrypt_test("ecb(camellia)");
1401                 ret += tcrypt_test("cbc(camellia)");
1402                 ret += tcrypt_test("ctr(camellia)");
1403                 ret += tcrypt_test("lrw(camellia)");
1404                 ret += tcrypt_test("xts(camellia)");
1405                 break;
1406
1407         case 33:
1408                 ret += tcrypt_test("sha224");
1409                 break;
1410
1411         case 34:
1412                 ret += tcrypt_test("salsa20");
1413                 break;
1414
1415         case 35:
1416                 ret += tcrypt_test("gcm(aes)");
1417                 break;
1418
1419         case 36:
1420                 ret += tcrypt_test("lzo");
1421                 break;
1422
1423         case 37:
1424                 ret += tcrypt_test("ccm(aes)");
1425                 break;
1426
1427         case 38:
1428                 ret += tcrypt_test("cts(cbc(aes))");
1429                 break;
1430
1431         case 39:
1432                 ret += tcrypt_test("rmd128");
1433                 break;
1434
1435         case 40:
1436                 ret += tcrypt_test("rmd160");
1437                 break;
1438
1439         case 41:
1440                 ret += tcrypt_test("rmd256");
1441                 break;
1442
1443         case 42:
1444                 ret += tcrypt_test("rmd320");
1445                 break;
1446
1447         case 43:
1448                 ret += tcrypt_test("ecb(seed)");
1449                 break;
1450
1451         case 44:
1452                 ret += tcrypt_test("zlib");
1453                 break;
1454
1455         case 45:
1456                 ret += tcrypt_test("rfc4309(ccm(aes))");
1457                 break;
1458
1459         case 46:
1460                 ret += tcrypt_test("ghash");
1461                 break;
1462
1463         case 47:
1464                 ret += tcrypt_test("crct10dif");
1465                 break;
1466
1467         case 100:
1468                 ret += tcrypt_test("hmac(md5)");
1469                 break;
1470
1471         case 101:
1472                 ret += tcrypt_test("hmac(sha1)");
1473                 break;
1474
1475         case 102:
1476                 ret += tcrypt_test("hmac(sha256)");
1477                 break;
1478
1479         case 103:
1480                 ret += tcrypt_test("hmac(sha384)");
1481                 break;
1482
1483         case 104:
1484                 ret += tcrypt_test("hmac(sha512)");
1485                 break;
1486
1487         case 105:
1488                 ret += tcrypt_test("hmac(sha224)");
1489                 break;
1490
1491         case 106:
1492                 ret += tcrypt_test("xcbc(aes)");
1493                 break;
1494
1495         case 107:
1496                 ret += tcrypt_test("hmac(rmd128)");
1497                 break;
1498
1499         case 108:
1500                 ret += tcrypt_test("hmac(rmd160)");
1501                 break;
1502
1503         case 109:
1504                 ret += tcrypt_test("vmac(aes)");
1505                 break;
1506
1507         case 110:
1508                 ret += tcrypt_test("hmac(crc32)");
1509                 break;
1510
1511         case 150:
1512                 ret += tcrypt_test("ansi_cprng");
1513                 break;
1514
1515         case 151:
1516                 ret += tcrypt_test("rfc4106(gcm(aes))");
1517                 break;
1518
1519         case 152:
1520                 ret += tcrypt_test("rfc4543(gcm(aes))");
1521                 break;
1522
1523         case 153:
1524                 ret += tcrypt_test("cmac(aes)");
1525                 break;
1526
1527         case 154:
1528                 ret += tcrypt_test("cmac(des3_ede)");
1529                 break;
1530
1531         case 155:
1532                 ret += tcrypt_test("authenc(hmac(sha1),cbc(aes))");
1533                 break;
1534
1535         case 156:
1536                 ret += tcrypt_test("authenc(hmac(md5),ecb(cipher_null))");
1537                 break;
1538
1539         case 157:
1540                 ret += tcrypt_test("authenc(hmac(sha1),ecb(cipher_null))");
1541                 break;
1542         case 181:
1543                 ret += tcrypt_test("authenc(hmac(sha1),cbc(des))");
1544                 break;
1545         case 182:
1546                 ret += tcrypt_test("authenc(hmac(sha1),cbc(des3_ede))");
1547                 break;
1548         case 183:
1549                 ret += tcrypt_test("authenc(hmac(sha224),cbc(des))");
1550                 break;
1551         case 184:
1552                 ret += tcrypt_test("authenc(hmac(sha224),cbc(des3_ede))");
1553                 break;
1554         case 185:
1555                 ret += tcrypt_test("authenc(hmac(sha256),cbc(des))");
1556                 break;
1557         case 186:
1558                 ret += tcrypt_test("authenc(hmac(sha256),cbc(des3_ede))");
1559                 break;
1560         case 187:
1561                 ret += tcrypt_test("authenc(hmac(sha384),cbc(des))");
1562                 break;
1563         case 188:
1564                 ret += tcrypt_test("authenc(hmac(sha384),cbc(des3_ede))");
1565                 break;
1566         case 189:
1567                 ret += tcrypt_test("authenc(hmac(sha512),cbc(des))");
1568                 break;
1569         case 190:
1570                 ret += tcrypt_test("authenc(hmac(sha512),cbc(des3_ede))");
1571                 break;
1572         case 200:
1573                 test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
1574                                 speed_template_16_24_32);
1575                 test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
1576                                 speed_template_16_24_32);
1577                 test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
1578                                 speed_template_16_24_32);
1579                 test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
1580                                 speed_template_16_24_32);
1581                 test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
1582                                 speed_template_32_40_48);
1583                 test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
1584                                 speed_template_32_40_48);
1585                 test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
1586                                 speed_template_32_48_64);
1587                 test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
1588                                 speed_template_32_48_64);
1589                 test_cipher_speed("ctr(aes)", ENCRYPT, sec, NULL, 0,
1590                                 speed_template_16_24_32);
1591                 test_cipher_speed("ctr(aes)", DECRYPT, sec, NULL, 0,
1592                                 speed_template_16_24_32);
1593                 break;
1594
1595         case 201:
1596                 test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
1597                                 des3_speed_template, DES3_SPEED_VECTORS,
1598                                 speed_template_24);
1599                 test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
1600                                 des3_speed_template, DES3_SPEED_VECTORS,
1601                                 speed_template_24);
1602                 test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
1603                                 des3_speed_template, DES3_SPEED_VECTORS,
1604                                 speed_template_24);
1605                 test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
1606                                 des3_speed_template, DES3_SPEED_VECTORS,
1607                                 speed_template_24);
1608                 test_cipher_speed("ctr(des3_ede)", ENCRYPT, sec,
1609                                 des3_speed_template, DES3_SPEED_VECTORS,
1610                                 speed_template_24);
1611                 test_cipher_speed("ctr(des3_ede)", DECRYPT, sec,
1612                                 des3_speed_template, DES3_SPEED_VECTORS,
1613                                 speed_template_24);
1614                 break;
1615
1616         case 202:
1617                 test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
1618                                 speed_template_16_24_32);
1619                 test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
1620                                 speed_template_16_24_32);
1621                 test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
1622                                 speed_template_16_24_32);
1623                 test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
1624                                 speed_template_16_24_32);
1625                 test_cipher_speed("ctr(twofish)", ENCRYPT, sec, NULL, 0,
1626                                 speed_template_16_24_32);
1627                 test_cipher_speed("ctr(twofish)", DECRYPT, sec, NULL, 0,
1628                                 speed_template_16_24_32);
1629                 test_cipher_speed("lrw(twofish)", ENCRYPT, sec, NULL, 0,
1630                                 speed_template_32_40_48);
1631                 test_cipher_speed("lrw(twofish)", DECRYPT, sec, NULL, 0,
1632                                 speed_template_32_40_48);
1633                 test_cipher_speed("xts(twofish)", ENCRYPT, sec, NULL, 0,
1634                                 speed_template_32_48_64);
1635                 test_cipher_speed("xts(twofish)", DECRYPT, sec, NULL, 0,
1636                                 speed_template_32_48_64);
1637                 break;
1638
1639         case 203:
1640                 test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
1641                                   speed_template_8_32);
1642                 test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
1643                                   speed_template_8_32);
1644                 test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
1645                                   speed_template_8_32);
1646                 test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
1647                                   speed_template_8_32);
1648                 test_cipher_speed("ctr(blowfish)", ENCRYPT, sec, NULL, 0,
1649                                   speed_template_8_32);
1650                 test_cipher_speed("ctr(blowfish)", DECRYPT, sec, NULL, 0,
1651                                   speed_template_8_32);
1652                 break;
1653
1654         case 204:
1655                 test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
1656                                   speed_template_8);
1657                 test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
1658                                   speed_template_8);
1659                 test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
1660                                   speed_template_8);
1661                 test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
1662                                   speed_template_8);
1663                 break;
1664
1665         case 205:
1666                 test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
1667                                 speed_template_16_24_32);
1668                 test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
1669                                 speed_template_16_24_32);
1670                 test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
1671                                 speed_template_16_24_32);
1672                 test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
1673                                 speed_template_16_24_32);
1674                 test_cipher_speed("ctr(camellia)", ENCRYPT, sec, NULL, 0,
1675                                 speed_template_16_24_32);
1676                 test_cipher_speed("ctr(camellia)", DECRYPT, sec, NULL, 0,
1677                                 speed_template_16_24_32);
1678                 test_cipher_speed("lrw(camellia)", ENCRYPT, sec, NULL, 0,
1679                                 speed_template_32_40_48);
1680                 test_cipher_speed("lrw(camellia)", DECRYPT, sec, NULL, 0,
1681                                 speed_template_32_40_48);
1682                 test_cipher_speed("xts(camellia)", ENCRYPT, sec, NULL, 0,
1683                                 speed_template_32_48_64);
1684                 test_cipher_speed("xts(camellia)", DECRYPT, sec, NULL, 0,
1685                                 speed_template_32_48_64);
1686                 break;
1687
1688         case 206:
1689                 test_cipher_speed("salsa20", ENCRYPT, sec, NULL, 0,
1690                                   speed_template_16_32);
1691                 break;
1692
1693         case 207:
1694                 test_cipher_speed("ecb(serpent)", ENCRYPT, sec, NULL, 0,
1695                                   speed_template_16_32);
1696                 test_cipher_speed("ecb(serpent)", DECRYPT, sec, NULL, 0,
1697                                   speed_template_16_32);
1698                 test_cipher_speed("cbc(serpent)", ENCRYPT, sec, NULL, 0,
1699                                   speed_template_16_32);
1700                 test_cipher_speed("cbc(serpent)", DECRYPT, sec, NULL, 0,
1701                                   speed_template_16_32);
1702                 test_cipher_speed("ctr(serpent)", ENCRYPT, sec, NULL, 0,
1703                                   speed_template_16_32);
1704                 test_cipher_speed("ctr(serpent)", DECRYPT, sec, NULL, 0,
1705                                   speed_template_16_32);
1706                 test_cipher_speed("lrw(serpent)", ENCRYPT, sec, NULL, 0,
1707                                   speed_template_32_48);
1708                 test_cipher_speed("lrw(serpent)", DECRYPT, sec, NULL, 0,
1709                                   speed_template_32_48);
1710                 test_cipher_speed("xts(serpent)", ENCRYPT, sec, NULL, 0,
1711                                   speed_template_32_64);
1712                 test_cipher_speed("xts(serpent)", DECRYPT, sec, NULL, 0,
1713                                   speed_template_32_64);
1714                 break;
1715
1716         case 208:
1717                 test_cipher_speed("ecb(arc4)", ENCRYPT, sec, NULL, 0,
1718                                   speed_template_8);
1719                 break;
1720
1721         case 209:
1722                 test_cipher_speed("ecb(cast5)", ENCRYPT, sec, NULL, 0,
1723                                   speed_template_8_16);
1724                 test_cipher_speed("ecb(cast5)", DECRYPT, sec, NULL, 0,
1725                                   speed_template_8_16);
1726                 test_cipher_speed("cbc(cast5)", ENCRYPT, sec, NULL, 0,
1727                                   speed_template_8_16);
1728                 test_cipher_speed("cbc(cast5)", DECRYPT, sec, NULL, 0,
1729                                   speed_template_8_16);
1730                 test_cipher_speed("ctr(cast5)", ENCRYPT, sec, NULL, 0,
1731                                   speed_template_8_16);
1732                 test_cipher_speed("ctr(cast5)", DECRYPT, sec, NULL, 0,
1733                                   speed_template_8_16);
1734                 break;
1735
1736         case 210:
1737                 test_cipher_speed("ecb(cast6)", ENCRYPT, sec, NULL, 0,
1738                                   speed_template_16_32);
1739                 test_cipher_speed("ecb(cast6)", DECRYPT, sec, NULL, 0,
1740                                   speed_template_16_32);
1741                 test_cipher_speed("cbc(cast6)", ENCRYPT, sec, NULL, 0,
1742                                   speed_template_16_32);
1743                 test_cipher_speed("cbc(cast6)", DECRYPT, sec, NULL, 0,
1744                                   speed_template_16_32);
1745                 test_cipher_speed("ctr(cast6)", ENCRYPT, sec, NULL, 0,
1746                                   speed_template_16_32);
1747                 test_cipher_speed("ctr(cast6)", DECRYPT, sec, NULL, 0,
1748                                   speed_template_16_32);
1749                 test_cipher_speed("lrw(cast6)", ENCRYPT, sec, NULL, 0,
1750                                   speed_template_32_48);
1751                 test_cipher_speed("lrw(cast6)", DECRYPT, sec, NULL, 0,
1752                                   speed_template_32_48);
1753                 test_cipher_speed("xts(cast6)", ENCRYPT, sec, NULL, 0,
1754                                   speed_template_32_64);
1755                 test_cipher_speed("xts(cast6)", DECRYPT, sec, NULL, 0,
1756                                   speed_template_32_64);
1757                 break;
1758
1759         case 211:
1760                 test_aead_speed("rfc4106(gcm(aes))", ENCRYPT, sec,
1761                                 NULL, 0, 16, 8, aead_speed_template_20);
1762                 break;
1763
1764         case 300:
1765                 if (alg) {
1766                         test_hash_speed(alg, sec, generic_hash_speed_template);
1767                         break;
1768                 }
1769
1770                 /* fall through */
1771
1772         case 301:
1773                 test_hash_speed("md4", sec, generic_hash_speed_template);
1774                 if (mode > 300 && mode < 400) break;
1775
1776         case 302:
1777                 test_hash_speed("md5", sec, generic_hash_speed_template);
1778                 if (mode > 300 && mode < 400) break;
1779
1780         case 303:
1781                 test_hash_speed("sha1", sec, generic_hash_speed_template);
1782                 if (mode > 300 && mode < 400) break;
1783
1784         case 304:
1785                 test_hash_speed("sha256", sec, generic_hash_speed_template);
1786                 if (mode > 300 && mode < 400) break;
1787
1788         case 305:
1789                 test_hash_speed("sha384", sec, generic_hash_speed_template);
1790                 if (mode > 300 && mode < 400) break;
1791
1792         case 306:
1793                 test_hash_speed("sha512", sec, generic_hash_speed_template);
1794                 if (mode > 300 && mode < 400) break;
1795
1796         case 307:
1797                 test_hash_speed("wp256", sec, generic_hash_speed_template);
1798                 if (mode > 300 && mode < 400) break;
1799
1800         case 308:
1801                 test_hash_speed("wp384", sec, generic_hash_speed_template);
1802                 if (mode > 300 && mode < 400) break;
1803
1804         case 309:
1805                 test_hash_speed("wp512", sec, generic_hash_speed_template);
1806                 if (mode > 300 && mode < 400) break;
1807
1808         case 310:
1809                 test_hash_speed("tgr128", sec, generic_hash_speed_template);
1810                 if (mode > 300 && mode < 400) break;
1811
1812         case 311:
1813                 test_hash_speed("tgr160", sec, generic_hash_speed_template);
1814                 if (mode > 300 && mode < 400) break;
1815
1816         case 312:
1817                 test_hash_speed("tgr192", sec, generic_hash_speed_template);
1818                 if (mode > 300 && mode < 400) break;
1819
1820         case 313:
1821                 test_hash_speed("sha224", sec, generic_hash_speed_template);
1822                 if (mode > 300 && mode < 400) break;
1823
1824         case 314:
1825                 test_hash_speed("rmd128", sec, generic_hash_speed_template);
1826                 if (mode > 300 && mode < 400) break;
1827
1828         case 315:
1829                 test_hash_speed("rmd160", sec, generic_hash_speed_template);
1830                 if (mode > 300 && mode < 400) break;
1831
1832         case 316:
1833                 test_hash_speed("rmd256", sec, generic_hash_speed_template);
1834                 if (mode > 300 && mode < 400) break;
1835
1836         case 317:
1837                 test_hash_speed("rmd320", sec, generic_hash_speed_template);
1838                 if (mode > 300 && mode < 400) break;
1839
1840         case 318:
1841                 test_hash_speed("ghash-generic", sec, hash_speed_template_16);
1842                 if (mode > 300 && mode < 400) break;
1843
1844         case 319:
1845                 test_hash_speed("crc32c", sec, generic_hash_speed_template);
1846                 if (mode > 300 && mode < 400) break;
1847
1848         case 320:
1849                 test_hash_speed("crct10dif", sec, generic_hash_speed_template);
1850                 if (mode > 300 && mode < 400) break;
1851
1852         case 399:
1853                 break;
1854
1855         case 400:
1856                 if (alg) {
1857                         test_ahash_speed(alg, sec, generic_hash_speed_template);
1858                         break;
1859                 }
1860
1861                 /* fall through */
1862
1863         case 401:
1864                 test_ahash_speed("md4", sec, generic_hash_speed_template);
1865                 if (mode > 400 && mode < 500) break;
1866
1867         case 402:
1868                 test_ahash_speed("md5", sec, generic_hash_speed_template);
1869                 if (mode > 400 && mode < 500) break;
1870
1871         case 403:
1872                 test_ahash_speed("sha1", sec, generic_hash_speed_template);
1873                 if (mode > 400 && mode < 500) break;
1874
1875         case 404:
1876                 test_ahash_speed("sha256", sec, generic_hash_speed_template);
1877                 if (mode > 400 && mode < 500) break;
1878
1879         case 405:
1880                 test_ahash_speed("sha384", sec, generic_hash_speed_template);
1881                 if (mode > 400 && mode < 500) break;
1882
1883         case 406:
1884                 test_ahash_speed("sha512", sec, generic_hash_speed_template);
1885                 if (mode > 400 && mode < 500) break;
1886
1887         case 407:
1888                 test_ahash_speed("wp256", sec, generic_hash_speed_template);
1889                 if (mode > 400 && mode < 500) break;
1890
1891         case 408:
1892                 test_ahash_speed("wp384", sec, generic_hash_speed_template);
1893                 if (mode > 400 && mode < 500) break;
1894
1895         case 409:
1896                 test_ahash_speed("wp512", sec, generic_hash_speed_template);
1897                 if (mode > 400 && mode < 500) break;
1898
1899         case 410:
1900                 test_ahash_speed("tgr128", sec, generic_hash_speed_template);
1901                 if (mode > 400 && mode < 500) break;
1902
1903         case 411:
1904                 test_ahash_speed("tgr160", sec, generic_hash_speed_template);
1905                 if (mode > 400 && mode < 500) break;
1906
1907         case 412:
1908                 test_ahash_speed("tgr192", sec, generic_hash_speed_template);
1909                 if (mode > 400 && mode < 500) break;
1910
1911         case 413:
1912                 test_ahash_speed("sha224", sec, generic_hash_speed_template);
1913                 if (mode > 400 && mode < 500) break;
1914
1915         case 414:
1916                 test_ahash_speed("rmd128", sec, generic_hash_speed_template);
1917                 if (mode > 400 && mode < 500) break;
1918
1919         case 415:
1920                 test_ahash_speed("rmd160", sec, generic_hash_speed_template);
1921                 if (mode > 400 && mode < 500) break;
1922
1923         case 416:
1924                 test_ahash_speed("rmd256", sec, generic_hash_speed_template);
1925                 if (mode > 400 && mode < 500) break;
1926
1927         case 417:
1928                 test_ahash_speed("rmd320", sec, generic_hash_speed_template);
1929                 if (mode > 400 && mode < 500) break;
1930
1931         case 499:
1932                 break;
1933
1934         case 500:
1935                 test_acipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
1936                                    speed_template_16_24_32);
1937                 test_acipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
1938                                    speed_template_16_24_32);
1939                 test_acipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
1940                                    speed_template_16_24_32);
1941                 test_acipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
1942                                    speed_template_16_24_32);
1943                 test_acipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
1944                                    speed_template_32_40_48);
1945                 test_acipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
1946                                    speed_template_32_40_48);
1947                 test_acipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
1948                                    speed_template_32_48_64);
1949                 test_acipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
1950                                    speed_template_32_48_64);
1951                 test_acipher_speed("ctr(aes)", ENCRYPT, sec, NULL, 0,
1952                                    speed_template_16_24_32);
1953                 test_acipher_speed("ctr(aes)", DECRYPT, sec, NULL, 0,
1954                                    speed_template_16_24_32);
1955                 test_acipher_speed("cfb(aes)", ENCRYPT, sec, NULL, 0,
1956                                    speed_template_16_24_32);
1957                 test_acipher_speed("cfb(aes)", DECRYPT, sec, NULL, 0,
1958                                    speed_template_16_24_32);
1959                 test_acipher_speed("ofb(aes)", ENCRYPT, sec, NULL, 0,
1960                                    speed_template_16_24_32);
1961                 test_acipher_speed("ofb(aes)", DECRYPT, sec, NULL, 0,
1962                                    speed_template_16_24_32);
1963                 test_acipher_speed("rfc3686(ctr(aes))", ENCRYPT, sec, NULL, 0,
1964                                    speed_template_20_28_36);
1965                 test_acipher_speed("rfc3686(ctr(aes))", DECRYPT, sec, NULL, 0,
1966                                    speed_template_20_28_36);
1967                 break;
1968
1969         case 501:
1970                 test_acipher_speed("ecb(des3_ede)", ENCRYPT, sec,
1971                                    des3_speed_template, DES3_SPEED_VECTORS,
1972                                    speed_template_24);
1973                 test_acipher_speed("ecb(des3_ede)", DECRYPT, sec,
1974                                    des3_speed_template, DES3_SPEED_VECTORS,
1975                                    speed_template_24);
1976                 test_acipher_speed("cbc(des3_ede)", ENCRYPT, sec,
1977                                    des3_speed_template, DES3_SPEED_VECTORS,
1978                                    speed_template_24);
1979                 test_acipher_speed("cbc(des3_ede)", DECRYPT, sec,
1980                                    des3_speed_template, DES3_SPEED_VECTORS,
1981                                    speed_template_24);
1982                 test_acipher_speed("cfb(des3_ede)", ENCRYPT, sec,
1983                                    des3_speed_template, DES3_SPEED_VECTORS,
1984                                    speed_template_24);
1985                 test_acipher_speed("cfb(des3_ede)", DECRYPT, sec,
1986                                    des3_speed_template, DES3_SPEED_VECTORS,
1987                                    speed_template_24);
1988                 test_acipher_speed("ofb(des3_ede)", ENCRYPT, sec,
1989                                    des3_speed_template, DES3_SPEED_VECTORS,
1990                                    speed_template_24);
1991                 test_acipher_speed("ofb(des3_ede)", DECRYPT, sec,
1992                                    des3_speed_template, DES3_SPEED_VECTORS,
1993                                    speed_template_24);
1994                 break;
1995
1996         case 502:
1997                 test_acipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
1998                                    speed_template_8);
1999                 test_acipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
2000                                    speed_template_8);
2001                 test_acipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
2002                                    speed_template_8);
2003                 test_acipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
2004                                    speed_template_8);
2005                 test_acipher_speed("cfb(des)", ENCRYPT, sec, NULL, 0,
2006                                    speed_template_8);
2007                 test_acipher_speed("cfb(des)", DECRYPT, sec, NULL, 0,
2008                                    speed_template_8);
2009                 test_acipher_speed("ofb(des)", ENCRYPT, sec, NULL, 0,
2010                                    speed_template_8);
2011                 test_acipher_speed("ofb(des)", DECRYPT, sec, NULL, 0,
2012                                    speed_template_8);
2013                 break;
2014
2015         case 503:
2016                 test_acipher_speed("ecb(serpent)", ENCRYPT, sec, NULL, 0,
2017                                    speed_template_16_32);
2018                 test_acipher_speed("ecb(serpent)", DECRYPT, sec, NULL, 0,
2019                                    speed_template_16_32);
2020                 test_acipher_speed("cbc(serpent)", ENCRYPT, sec, NULL, 0,
2021                                    speed_template_16_32);
2022                 test_acipher_speed("cbc(serpent)", DECRYPT, sec, NULL, 0,
2023                                    speed_template_16_32);
2024                 test_acipher_speed("ctr(serpent)", ENCRYPT, sec, NULL, 0,
2025                                    speed_template_16_32);
2026                 test_acipher_speed("ctr(serpent)", DECRYPT, sec, NULL, 0,
2027                                    speed_template_16_32);
2028                 test_acipher_speed("lrw(serpent)", ENCRYPT, sec, NULL, 0,
2029                                    speed_template_32_48);
2030                 test_acipher_speed("lrw(serpent)", DECRYPT, sec, NULL, 0,
2031                                    speed_template_32_48);
2032                 test_acipher_speed("xts(serpent)", ENCRYPT, sec, NULL, 0,
2033                                    speed_template_32_64);
2034                 test_acipher_speed("xts(serpent)", DECRYPT, sec, NULL, 0,
2035                                    speed_template_32_64);
2036                 break;
2037
2038         case 504:
2039                 test_acipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
2040                                    speed_template_16_24_32);
2041                 test_acipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
2042                                    speed_template_16_24_32);
2043                 test_acipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
2044                                    speed_template_16_24_32);
2045                 test_acipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
2046                                    speed_template_16_24_32);
2047                 test_acipher_speed("ctr(twofish)", ENCRYPT, sec, NULL, 0,
2048                                    speed_template_16_24_32);
2049                 test_acipher_speed("ctr(twofish)", DECRYPT, sec, NULL, 0,
2050                                    speed_template_16_24_32);
2051                 test_acipher_speed("lrw(twofish)", ENCRYPT, sec, NULL, 0,
2052                                    speed_template_32_40_48);
2053                 test_acipher_speed("lrw(twofish)", DECRYPT, sec, NULL, 0,
2054                                    speed_template_32_40_48);
2055                 test_acipher_speed("xts(twofish)", ENCRYPT, sec, NULL, 0,
2056                                    speed_template_32_48_64);
2057                 test_acipher_speed("xts(twofish)", DECRYPT, sec, NULL, 0,
2058                                    speed_template_32_48_64);
2059                 break;
2060
2061         case 505:
2062                 test_acipher_speed("ecb(arc4)", ENCRYPT, sec, NULL, 0,
2063                                    speed_template_8);
2064                 break;
2065
2066         case 506:
2067                 test_acipher_speed("ecb(cast5)", ENCRYPT, sec, NULL, 0,
2068                                    speed_template_8_16);
2069                 test_acipher_speed("ecb(cast5)", DECRYPT, sec, NULL, 0,
2070                                    speed_template_8_16);
2071                 test_acipher_speed("cbc(cast5)", ENCRYPT, sec, NULL, 0,
2072                                    speed_template_8_16);
2073                 test_acipher_speed("cbc(cast5)", DECRYPT, sec, NULL, 0,
2074                                    speed_template_8_16);
2075                 test_acipher_speed("ctr(cast5)", ENCRYPT, sec, NULL, 0,
2076                                    speed_template_8_16);
2077                 test_acipher_speed("ctr(cast5)", DECRYPT, sec, NULL, 0,
2078                                    speed_template_8_16);
2079                 break;
2080
2081         case 507:
2082                 test_acipher_speed("ecb(cast6)", ENCRYPT, sec, NULL, 0,
2083                                    speed_template_16_32);
2084                 test_acipher_speed("ecb(cast6)", DECRYPT, sec, NULL, 0,
2085                                    speed_template_16_32);
2086                 test_acipher_speed("cbc(cast6)", ENCRYPT, sec, NULL, 0,
2087                                    speed_template_16_32);
2088                 test_acipher_speed("cbc(cast6)", DECRYPT, sec, NULL, 0,
2089                                    speed_template_16_32);
2090                 test_acipher_speed("ctr(cast6)", ENCRYPT, sec, NULL, 0,
2091                                    speed_template_16_32);
2092                 test_acipher_speed("ctr(cast6)", DECRYPT, sec, NULL, 0,
2093                                    speed_template_16_32);
2094                 test_acipher_speed("lrw(cast6)", ENCRYPT, sec, NULL, 0,
2095                                    speed_template_32_48);
2096                 test_acipher_speed("lrw(cast6)", DECRYPT, sec, NULL, 0,
2097                                    speed_template_32_48);
2098                 test_acipher_speed("xts(cast6)", ENCRYPT, sec, NULL, 0,
2099                                    speed_template_32_64);
2100                 test_acipher_speed("xts(cast6)", DECRYPT, sec, NULL, 0,
2101                                    speed_template_32_64);
2102                 break;
2103
2104         case 508:
2105                 test_acipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
2106                                    speed_template_16_32);
2107                 test_acipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
2108                                    speed_template_16_32);
2109                 test_acipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
2110                                    speed_template_16_32);
2111                 test_acipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
2112                                    speed_template_16_32);
2113                 test_acipher_speed("ctr(camellia)", ENCRYPT, sec, NULL, 0,
2114                                    speed_template_16_32);
2115                 test_acipher_speed("ctr(camellia)", DECRYPT, sec, NULL, 0,
2116                                    speed_template_16_32);
2117                 test_acipher_speed("lrw(camellia)", ENCRYPT, sec, NULL, 0,
2118                                    speed_template_32_48);
2119                 test_acipher_speed("lrw(camellia)", DECRYPT, sec, NULL, 0,
2120                                    speed_template_32_48);
2121                 test_acipher_speed("xts(camellia)", ENCRYPT, sec, NULL, 0,
2122                                    speed_template_32_64);
2123                 test_acipher_speed("xts(camellia)", DECRYPT, sec, NULL, 0,
2124                                    speed_template_32_64);
2125                 break;
2126
2127         case 509:
2128                 test_acipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
2129                                    speed_template_8_32);
2130                 test_acipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
2131                                    speed_template_8_32);
2132                 test_acipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
2133                                    speed_template_8_32);
2134                 test_acipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
2135                                    speed_template_8_32);
2136                 test_acipher_speed("ctr(blowfish)", ENCRYPT, sec, NULL, 0,
2137                                    speed_template_8_32);
2138                 test_acipher_speed("ctr(blowfish)", DECRYPT, sec, NULL, 0,
2139                                    speed_template_8_32);
2140                 break;
2141
2142         case 1000:
2143                 test_available();
2144                 break;
2145         }
2146
2147         return ret;
2148 }
2149
2150 static int __init tcrypt_mod_init(void)
2151 {
2152         int err = -ENOMEM;
2153         int i;
2154
2155         for (i = 0; i < TVMEMSIZE; i++) {
2156                 tvmem[i] = (void *)__get_free_page(GFP_KERNEL);
2157                 if (!tvmem[i])
2158                         goto err_free_tv;
2159         }
2160
2161         err = do_test(alg, type, mask, mode);
2162
2163         if (err) {
2164                 printk(KERN_ERR "tcrypt: one or more tests failed!\n");
2165                 goto err_free_tv;
2166         }
2167
2168         /* We intentionaly return -EAGAIN to prevent keeping the module,
2169          * unless we're running in fips mode. It does all its work from
2170          * init() and doesn't offer any runtime functionality, but in
2171          * the fips case, checking for a successful load is helpful.
2172          * => we don't need it in the memory, do we?
2173          *                                        -- mludvig
2174          */
2175         if (!fips_enabled)
2176                 err = -EAGAIN;
2177
2178 err_free_tv:
2179         for (i = 0; i < TVMEMSIZE && tvmem[i]; i++)
2180                 free_page((unsigned long)tvmem[i]);
2181
2182         return err;
2183 }
2184
2185 /*
2186  * If an init function is provided, an exit function must also be provided
2187  * to allow module unload.
2188  */
2189 static void __exit tcrypt_mod_fini(void) { }
2190
2191 module_init(tcrypt_mod_init);
2192 module_exit(tcrypt_mod_fini);
2193
2194 module_param(alg, charp, 0);
2195 module_param(type, uint, 0);
2196 module_param(mask, uint, 0);
2197 module_param(mode, int, 0);
2198 module_param(sec, uint, 0);
2199 MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
2200                       "(defaults to zero which uses CPU cycles instead)");
2201
2202 MODULE_LICENSE("GPL");
2203 MODULE_DESCRIPTION("Quick & dirty crypto testing module");
2204 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");