Linux 4.2.6
[linux/fpc-iii.git] / crypto / tcrypt.c
blob9f6f10b498ba4aab02849c760b1da61bd5a7fd99
1 /*
2 * Quick & dirty crypto testing module.
4 * This will only exist until we have a better testing mechanism
5 * (e.g. a char device).
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
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.
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.
25 #include <crypto/aead.h>
26 #include <crypto/hash.h>
27 #include <linux/err.h>
28 #include <linux/fips.h>
29 #include <linux/init.h>
30 #include <linux/gfp.h>
31 #include <linux/module.h>
32 #include <linux/scatterlist.h>
33 #include <linux/string.h>
34 #include <linux/moduleparam.h>
35 #include <linux/jiffies.h>
36 #include <linux/timex.h>
37 #include <linux/interrupt.h>
38 #include "tcrypt.h"
41 * Need slab memory for testing (size in number of pages).
43 #define TVMEMSIZE 4
46 * Used by test_cipher_speed()
48 #define ENCRYPT 1
49 #define DECRYPT 0
52 * return a string with the driver name
54 #define get_driver_name(tfm_type, tfm) crypto_tfm_alg_driver_name(tfm_type ## _tfm(tfm))
57 * Used by test_cipher_speed()
59 static unsigned int sec;
61 static char *alg = NULL;
62 static u32 type;
63 static u32 mask;
64 static int mode;
65 static char *tvmem[TVMEMSIZE];
67 static char *check[] = {
68 "des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256",
69 "blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
70 "cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
71 "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", "fcrypt",
72 "camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
73 "lzo", "cts", "zlib", NULL
76 static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc,
77 struct scatterlist *sg, int blen, int secs)
79 unsigned long start, end;
80 int bcount;
81 int ret;
83 for (start = jiffies, end = start + secs * HZ, bcount = 0;
84 time_before(jiffies, end); bcount++) {
85 if (enc)
86 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
87 else
88 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
90 if (ret)
91 return ret;
94 printk("%d operations in %d seconds (%ld bytes)\n",
95 bcount, secs, (long)bcount * blen);
96 return 0;
99 static int test_cipher_cycles(struct blkcipher_desc *desc, int enc,
100 struct scatterlist *sg, int blen)
102 unsigned long cycles = 0;
103 int ret = 0;
104 int i;
106 local_irq_disable();
108 /* Warm-up run. */
109 for (i = 0; i < 4; i++) {
110 if (enc)
111 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
112 else
113 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
115 if (ret)
116 goto out;
119 /* The real thing. */
120 for (i = 0; i < 8; i++) {
121 cycles_t start, end;
123 start = get_cycles();
124 if (enc)
125 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
126 else
127 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
128 end = get_cycles();
130 if (ret)
131 goto out;
133 cycles += end - start;
136 out:
137 local_irq_enable();
139 if (ret == 0)
140 printk("1 operation in %lu cycles (%d bytes)\n",
141 (cycles + 4) / 8, blen);
143 return ret;
146 static int test_aead_jiffies(struct aead_request *req, int enc,
147 int blen, int secs)
149 unsigned long start, end;
150 int bcount;
151 int ret;
153 for (start = jiffies, end = start + secs * HZ, bcount = 0;
154 time_before(jiffies, end); bcount++) {
155 if (enc)
156 ret = crypto_aead_encrypt(req);
157 else
158 ret = crypto_aead_decrypt(req);
160 if (ret)
161 return ret;
164 printk("%d operations in %d seconds (%ld bytes)\n",
165 bcount, secs, (long)bcount * blen);
166 return 0;
169 static int test_aead_cycles(struct aead_request *req, int enc, int blen)
171 unsigned long cycles = 0;
172 int ret = 0;
173 int i;
175 local_irq_disable();
177 /* Warm-up run. */
178 for (i = 0; i < 4; i++) {
179 if (enc)
180 ret = crypto_aead_encrypt(req);
181 else
182 ret = crypto_aead_decrypt(req);
184 if (ret)
185 goto out;
188 /* The real thing. */
189 for (i = 0; i < 8; i++) {
190 cycles_t start, end;
192 start = get_cycles();
193 if (enc)
194 ret = crypto_aead_encrypt(req);
195 else
196 ret = crypto_aead_decrypt(req);
197 end = get_cycles();
199 if (ret)
200 goto out;
202 cycles += end - start;
205 out:
206 local_irq_enable();
208 if (ret == 0)
209 printk("1 operation in %lu cycles (%d bytes)\n",
210 (cycles + 4) / 8, blen);
212 return ret;
215 static u32 block_sizes[] = { 16, 64, 256, 1024, 8192, 0 };
216 static u32 aead_sizes[] = { 16, 64, 256, 512, 1024, 2048, 4096, 8192, 0 };
218 #define XBUFSIZE 8
219 #define MAX_IVLEN 32
221 static int testmgr_alloc_buf(char *buf[XBUFSIZE])
223 int i;
225 for (i = 0; i < XBUFSIZE; i++) {
226 buf[i] = (void *)__get_free_page(GFP_KERNEL);
227 if (!buf[i])
228 goto err_free_buf;
231 return 0;
233 err_free_buf:
234 while (i-- > 0)
235 free_page((unsigned long)buf[i]);
237 return -ENOMEM;
240 static void testmgr_free_buf(char *buf[XBUFSIZE])
242 int i;
244 for (i = 0; i < XBUFSIZE; i++)
245 free_page((unsigned long)buf[i]);
248 static void sg_init_aead(struct scatterlist *sg, char *xbuf[XBUFSIZE],
249 unsigned int buflen)
251 int np = (buflen + PAGE_SIZE - 1)/PAGE_SIZE;
252 int k, rem;
254 if (np > XBUFSIZE) {
255 rem = PAGE_SIZE;
256 np = XBUFSIZE;
257 } else {
258 rem = buflen % PAGE_SIZE;
261 sg_init_table(sg, np + 1);
262 np--;
263 for (k = 0; k < np; k++)
264 sg_set_buf(&sg[k + 1], xbuf[k], PAGE_SIZE);
266 sg_set_buf(&sg[k + 1], xbuf[k], rem);
269 static void test_aead_speed(const char *algo, int enc, unsigned int secs,
270 struct aead_speed_template *template,
271 unsigned int tcount, u8 authsize,
272 unsigned int aad_size, u8 *keysize)
274 unsigned int i, j;
275 struct crypto_aead *tfm;
276 int ret = -ENOMEM;
277 const char *key;
278 struct aead_request *req;
279 struct scatterlist *sg;
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;
290 iv = kzalloc(MAX_IVLEN, GFP_KERNEL);
291 if (!iv)
292 return;
294 if (aad_size >= PAGE_SIZE) {
295 pr_err("associate data length (%u) too big\n", aad_size);
296 goto out_noxbuf;
299 if (enc == ENCRYPT)
300 e = "encryption";
301 else
302 e = "decryption";
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;
311 sg = kmalloc(sizeof(*sg) * 9 * 2, GFP_KERNEL);
312 if (!sg)
313 goto out_nosg;
314 sgout = &sg[9];
316 tfm = crypto_alloc_aead(algo, 0, 0);
318 if (IS_ERR(tfm)) {
319 pr_err("alg: aead: Failed to load transform for %s: %ld\n", algo,
320 PTR_ERR(tfm));
321 goto out_notfm;
324 printk(KERN_INFO "\ntesting speed of %s (%s) %s\n", algo,
325 get_driver_name(crypto_aead, tfm), e);
327 req = aead_request_alloc(tfm, GFP_KERNEL);
328 if (!req) {
329 pr_err("alg: aead: Failed to allocate request for %s\n",
330 algo);
331 goto out_noreq;
334 i = 0;
335 do {
336 b_size = aead_sizes;
337 do {
338 assoc = axbuf[0];
339 memset(assoc, 0xff, aad_size);
341 if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
342 pr_err("template (%u) too big for tvmem (%lu)\n",
343 *keysize + *b_size,
344 TVMEMSIZE * PAGE_SIZE);
345 goto out;
348 key = tvmem[0];
349 for (j = 0; j < tcount; j++) {
350 if (template[j].klen == *keysize) {
351 key = template[j].key;
352 break;
355 ret = crypto_aead_setkey(tfm, key, *keysize);
356 ret = crypto_aead_setauthsize(tfm, authsize);
358 iv_len = crypto_aead_ivsize(tfm);
359 if (iv_len)
360 memset(iv, 0xff, iv_len);
362 crypto_aead_clear_flags(tfm, ~0);
363 printk(KERN_INFO "test %u (%d bit key, %d byte blocks): ",
364 i, *keysize * 8, *b_size);
367 memset(tvmem[0], 0xff, PAGE_SIZE);
369 if (ret) {
370 pr_err("setkey() failed flags=%x\n",
371 crypto_aead_get_flags(tfm));
372 goto out;
375 sg_init_aead(sg, xbuf,
376 *b_size + (enc ? authsize : 0));
378 sg_init_aead(sgout, xoutbuf,
379 *b_size + (enc ? authsize : 0));
381 sg_set_buf(&sg[0], assoc, aad_size);
382 sg_set_buf(&sgout[0], assoc, aad_size);
384 aead_request_set_crypt(req, sg, sgout, *b_size, iv);
385 aead_request_set_ad(req, aad_size);
387 if (secs)
388 ret = test_aead_jiffies(req, enc, *b_size,
389 secs);
390 else
391 ret = test_aead_cycles(req, enc, *b_size);
393 if (ret) {
394 pr_err("%s() failed return code=%d\n", e, ret);
395 break;
397 b_size++;
398 i++;
399 } while (*b_size);
400 keysize++;
401 } while (*keysize);
403 out:
404 aead_request_free(req);
405 out_noreq:
406 crypto_free_aead(tfm);
407 out_notfm:
408 kfree(sg);
409 out_nosg:
410 testmgr_free_buf(xoutbuf);
411 out_nooutbuf:
412 testmgr_free_buf(axbuf);
413 out_noaxbuf:
414 testmgr_free_buf(xbuf);
415 out_noxbuf:
416 kfree(iv);
417 return;
420 static void test_cipher_speed(const char *algo, int enc, unsigned int secs,
421 struct cipher_speed_template *template,
422 unsigned int tcount, u8 *keysize)
424 unsigned int ret, i, j, iv_len;
425 const char *key;
426 char iv[128];
427 struct crypto_blkcipher *tfm;
428 struct blkcipher_desc desc;
429 const char *e;
430 u32 *b_size;
432 if (enc == ENCRYPT)
433 e = "encryption";
434 else
435 e = "decryption";
437 tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);
439 if (IS_ERR(tfm)) {
440 printk("failed to load transform for %s: %ld\n", algo,
441 PTR_ERR(tfm));
442 return;
444 desc.tfm = tfm;
445 desc.flags = 0;
447 printk(KERN_INFO "\ntesting speed of %s (%s) %s\n", algo,
448 get_driver_name(crypto_blkcipher, tfm), e);
450 i = 0;
451 do {
453 b_size = block_sizes;
454 do {
455 struct scatterlist sg[TVMEMSIZE];
457 if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
458 printk("template (%u) too big for "
459 "tvmem (%lu)\n", *keysize + *b_size,
460 TVMEMSIZE * PAGE_SIZE);
461 goto out;
464 printk("test %u (%d bit key, %d byte blocks): ", i,
465 *keysize * 8, *b_size);
467 memset(tvmem[0], 0xff, PAGE_SIZE);
469 /* set key, plain text and IV */
470 key = tvmem[0];
471 for (j = 0; j < tcount; j++) {
472 if (template[j].klen == *keysize) {
473 key = template[j].key;
474 break;
478 ret = crypto_blkcipher_setkey(tfm, key, *keysize);
479 if (ret) {
480 printk("setkey() failed flags=%x\n",
481 crypto_blkcipher_get_flags(tfm));
482 goto out;
485 sg_init_table(sg, TVMEMSIZE);
486 sg_set_buf(sg, tvmem[0] + *keysize,
487 PAGE_SIZE - *keysize);
488 for (j = 1; j < TVMEMSIZE; j++) {
489 sg_set_buf(sg + j, tvmem[j], PAGE_SIZE);
490 memset (tvmem[j], 0xff, PAGE_SIZE);
493 iv_len = crypto_blkcipher_ivsize(tfm);
494 if (iv_len) {
495 memset(&iv, 0xff, iv_len);
496 crypto_blkcipher_set_iv(tfm, iv, iv_len);
499 if (secs)
500 ret = test_cipher_jiffies(&desc, enc, sg,
501 *b_size, secs);
502 else
503 ret = test_cipher_cycles(&desc, enc, sg,
504 *b_size);
506 if (ret) {
507 printk("%s() failed flags=%x\n", e, desc.flags);
508 break;
510 b_size++;
511 i++;
512 } while (*b_size);
513 keysize++;
514 } while (*keysize);
516 out:
517 crypto_free_blkcipher(tfm);
520 static int test_hash_jiffies_digest(struct hash_desc *desc,
521 struct scatterlist *sg, int blen,
522 char *out, int secs)
524 unsigned long start, end;
525 int bcount;
526 int ret;
528 for (start = jiffies, end = start + secs * HZ, bcount = 0;
529 time_before(jiffies, end); bcount++) {
530 ret = crypto_hash_digest(desc, sg, blen, out);
531 if (ret)
532 return ret;
535 printk("%6u opers/sec, %9lu bytes/sec\n",
536 bcount / secs, ((long)bcount * blen) / secs);
538 return 0;
541 static int test_hash_jiffies(struct hash_desc *desc, struct scatterlist *sg,
542 int blen, int plen, char *out, int secs)
544 unsigned long start, end;
545 int bcount, pcount;
546 int ret;
548 if (plen == blen)
549 return test_hash_jiffies_digest(desc, sg, blen, out, secs);
551 for (start = jiffies, end = start + secs * HZ, bcount = 0;
552 time_before(jiffies, end); bcount++) {
553 ret = crypto_hash_init(desc);
554 if (ret)
555 return ret;
556 for (pcount = 0; pcount < blen; pcount += plen) {
557 ret = crypto_hash_update(desc, sg, plen);
558 if (ret)
559 return ret;
561 /* we assume there is enough space in 'out' for the result */
562 ret = crypto_hash_final(desc, out);
563 if (ret)
564 return ret;
567 printk("%6u opers/sec, %9lu bytes/sec\n",
568 bcount / secs, ((long)bcount * blen) / secs);
570 return 0;
573 static int test_hash_cycles_digest(struct hash_desc *desc,
574 struct scatterlist *sg, int blen, char *out)
576 unsigned long cycles = 0;
577 int i;
578 int ret;
580 local_irq_disable();
582 /* Warm-up run. */
583 for (i = 0; i < 4; i++) {
584 ret = crypto_hash_digest(desc, sg, blen, out);
585 if (ret)
586 goto out;
589 /* The real thing. */
590 for (i = 0; i < 8; i++) {
591 cycles_t start, end;
593 start = get_cycles();
595 ret = crypto_hash_digest(desc, sg, blen, out);
596 if (ret)
597 goto out;
599 end = get_cycles();
601 cycles += end - start;
604 out:
605 local_irq_enable();
607 if (ret)
608 return ret;
610 printk("%6lu cycles/operation, %4lu cycles/byte\n",
611 cycles / 8, cycles / (8 * blen));
613 return 0;
616 static int test_hash_cycles(struct hash_desc *desc, struct scatterlist *sg,
617 int blen, int plen, char *out)
619 unsigned long cycles = 0;
620 int i, pcount;
621 int ret;
623 if (plen == blen)
624 return test_hash_cycles_digest(desc, sg, blen, out);
626 local_irq_disable();
628 /* Warm-up run. */
629 for (i = 0; i < 4; i++) {
630 ret = crypto_hash_init(desc);
631 if (ret)
632 goto out;
633 for (pcount = 0; pcount < blen; pcount += plen) {
634 ret = crypto_hash_update(desc, sg, plen);
635 if (ret)
636 goto out;
638 ret = crypto_hash_final(desc, out);
639 if (ret)
640 goto out;
643 /* The real thing. */
644 for (i = 0; i < 8; i++) {
645 cycles_t start, end;
647 start = get_cycles();
649 ret = crypto_hash_init(desc);
650 if (ret)
651 goto out;
652 for (pcount = 0; pcount < blen; pcount += plen) {
653 ret = crypto_hash_update(desc, sg, plen);
654 if (ret)
655 goto out;
657 ret = crypto_hash_final(desc, out);
658 if (ret)
659 goto out;
661 end = get_cycles();
663 cycles += end - start;
666 out:
667 local_irq_enable();
669 if (ret)
670 return ret;
672 printk("%6lu cycles/operation, %4lu cycles/byte\n",
673 cycles / 8, cycles / (8 * blen));
675 return 0;
678 static void test_hash_sg_init(struct scatterlist *sg)
680 int i;
682 sg_init_table(sg, TVMEMSIZE);
683 for (i = 0; i < TVMEMSIZE; i++) {
684 sg_set_buf(sg + i, tvmem[i], PAGE_SIZE);
685 memset(tvmem[i], 0xff, PAGE_SIZE);
689 static void test_hash_speed(const char *algo, unsigned int secs,
690 struct hash_speed *speed)
692 struct scatterlist sg[TVMEMSIZE];
693 struct crypto_hash *tfm;
694 struct hash_desc desc;
695 static char output[1024];
696 int i;
697 int ret;
699 tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
701 if (IS_ERR(tfm)) {
702 printk(KERN_ERR "failed to load transform for %s: %ld\n", algo,
703 PTR_ERR(tfm));
704 return;
707 printk(KERN_INFO "\ntesting speed of %s (%s)\n", algo,
708 get_driver_name(crypto_hash, tfm));
710 desc.tfm = tfm;
711 desc.flags = 0;
713 if (crypto_hash_digestsize(tfm) > sizeof(output)) {
714 printk(KERN_ERR "digestsize(%u) > outputbuffer(%zu)\n",
715 crypto_hash_digestsize(tfm), sizeof(output));
716 goto out;
719 test_hash_sg_init(sg);
720 for (i = 0; speed[i].blen != 0; i++) {
721 if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
722 printk(KERN_ERR
723 "template (%u) too big for tvmem (%lu)\n",
724 speed[i].blen, TVMEMSIZE * PAGE_SIZE);
725 goto out;
728 if (speed[i].klen)
729 crypto_hash_setkey(tfm, tvmem[0], speed[i].klen);
731 printk(KERN_INFO "test%3u "
732 "(%5u byte blocks,%5u bytes per update,%4u updates): ",
733 i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
735 if (secs)
736 ret = test_hash_jiffies(&desc, sg, speed[i].blen,
737 speed[i].plen, output, secs);
738 else
739 ret = test_hash_cycles(&desc, sg, speed[i].blen,
740 speed[i].plen, output);
742 if (ret) {
743 printk(KERN_ERR "hashing failed ret=%d\n", ret);
744 break;
748 out:
749 crypto_free_hash(tfm);
752 struct tcrypt_result {
753 struct completion completion;
754 int err;
757 static void tcrypt_complete(struct crypto_async_request *req, int err)
759 struct tcrypt_result *res = req->data;
761 if (err == -EINPROGRESS)
762 return;
764 res->err = err;
765 complete(&res->completion);
768 static inline int do_one_ahash_op(struct ahash_request *req, int ret)
770 if (ret == -EINPROGRESS || ret == -EBUSY) {
771 struct tcrypt_result *tr = req->base.data;
773 wait_for_completion(&tr->completion);
774 reinit_completion(&tr->completion);
775 ret = tr->err;
777 return ret;
780 static int test_ahash_jiffies_digest(struct ahash_request *req, int blen,
781 char *out, int secs)
783 unsigned long start, end;
784 int bcount;
785 int ret;
787 for (start = jiffies, end = start + secs * HZ, bcount = 0;
788 time_before(jiffies, end); bcount++) {
789 ret = do_one_ahash_op(req, crypto_ahash_digest(req));
790 if (ret)
791 return ret;
794 printk("%6u opers/sec, %9lu bytes/sec\n",
795 bcount / secs, ((long)bcount * blen) / secs);
797 return 0;
800 static int test_ahash_jiffies(struct ahash_request *req, int blen,
801 int plen, char *out, int secs)
803 unsigned long start, end;
804 int bcount, pcount;
805 int ret;
807 if (plen == blen)
808 return test_ahash_jiffies_digest(req, blen, out, secs);
810 for (start = jiffies, end = start + secs * HZ, bcount = 0;
811 time_before(jiffies, end); bcount++) {
812 ret = do_one_ahash_op(req, crypto_ahash_init(req));
813 if (ret)
814 return ret;
815 for (pcount = 0; pcount < blen; pcount += plen) {
816 ret = do_one_ahash_op(req, crypto_ahash_update(req));
817 if (ret)
818 return ret;
820 /* we assume there is enough space in 'out' for the result */
821 ret = do_one_ahash_op(req, crypto_ahash_final(req));
822 if (ret)
823 return ret;
826 pr_cont("%6u opers/sec, %9lu bytes/sec\n",
827 bcount / secs, ((long)bcount * blen) / secs);
829 return 0;
832 static int test_ahash_cycles_digest(struct ahash_request *req, int blen,
833 char *out)
835 unsigned long cycles = 0;
836 int ret, i;
838 /* Warm-up run. */
839 for (i = 0; i < 4; i++) {
840 ret = do_one_ahash_op(req, crypto_ahash_digest(req));
841 if (ret)
842 goto out;
845 /* The real thing. */
846 for (i = 0; i < 8; i++) {
847 cycles_t start, end;
849 start = get_cycles();
851 ret = do_one_ahash_op(req, crypto_ahash_digest(req));
852 if (ret)
853 goto out;
855 end = get_cycles();
857 cycles += end - start;
860 out:
861 if (ret)
862 return ret;
864 pr_cont("%6lu cycles/operation, %4lu cycles/byte\n",
865 cycles / 8, cycles / (8 * blen));
867 return 0;
870 static int test_ahash_cycles(struct ahash_request *req, int blen,
871 int plen, char *out)
873 unsigned long cycles = 0;
874 int i, pcount, ret;
876 if (plen == blen)
877 return test_ahash_cycles_digest(req, blen, out);
879 /* Warm-up run. */
880 for (i = 0; i < 4; i++) {
881 ret = do_one_ahash_op(req, crypto_ahash_init(req));
882 if (ret)
883 goto out;
884 for (pcount = 0; pcount < blen; pcount += plen) {
885 ret = do_one_ahash_op(req, crypto_ahash_update(req));
886 if (ret)
887 goto out;
889 ret = do_one_ahash_op(req, crypto_ahash_final(req));
890 if (ret)
891 goto out;
894 /* The real thing. */
895 for (i = 0; i < 8; i++) {
896 cycles_t start, end;
898 start = get_cycles();
900 ret = do_one_ahash_op(req, crypto_ahash_init(req));
901 if (ret)
902 goto out;
903 for (pcount = 0; pcount < blen; pcount += plen) {
904 ret = do_one_ahash_op(req, crypto_ahash_update(req));
905 if (ret)
906 goto out;
908 ret = do_one_ahash_op(req, crypto_ahash_final(req));
909 if (ret)
910 goto out;
912 end = get_cycles();
914 cycles += end - start;
917 out:
918 if (ret)
919 return ret;
921 pr_cont("%6lu cycles/operation, %4lu cycles/byte\n",
922 cycles / 8, cycles / (8 * blen));
924 return 0;
927 static void test_ahash_speed(const char *algo, unsigned int secs,
928 struct hash_speed *speed)
930 struct scatterlist sg[TVMEMSIZE];
931 struct tcrypt_result tresult;
932 struct ahash_request *req;
933 struct crypto_ahash *tfm;
934 static char output[1024];
935 int i, ret;
937 tfm = crypto_alloc_ahash(algo, 0, 0);
938 if (IS_ERR(tfm)) {
939 pr_err("failed to load transform for %s: %ld\n",
940 algo, PTR_ERR(tfm));
941 return;
944 printk(KERN_INFO "\ntesting speed of async %s (%s)\n", algo,
945 get_driver_name(crypto_ahash, tfm));
947 if (crypto_ahash_digestsize(tfm) > sizeof(output)) {
948 pr_err("digestsize(%u) > outputbuffer(%zu)\n",
949 crypto_ahash_digestsize(tfm), sizeof(output));
950 goto out;
953 test_hash_sg_init(sg);
954 req = ahash_request_alloc(tfm, GFP_KERNEL);
955 if (!req) {
956 pr_err("ahash request allocation failure\n");
957 goto out;
960 init_completion(&tresult.completion);
961 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
962 tcrypt_complete, &tresult);
964 for (i = 0; speed[i].blen != 0; i++) {
965 if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
966 pr_err("template (%u) too big for tvmem (%lu)\n",
967 speed[i].blen, TVMEMSIZE * PAGE_SIZE);
968 break;
971 pr_info("test%3u "
972 "(%5u byte blocks,%5u bytes per update,%4u updates): ",
973 i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
975 ahash_request_set_crypt(req, sg, output, speed[i].plen);
977 if (secs)
978 ret = test_ahash_jiffies(req, speed[i].blen,
979 speed[i].plen, output, secs);
980 else
981 ret = test_ahash_cycles(req, speed[i].blen,
982 speed[i].plen, output);
984 if (ret) {
985 pr_err("hashing failed ret=%d\n", ret);
986 break;
990 ahash_request_free(req);
992 out:
993 crypto_free_ahash(tfm);
996 static inline int do_one_acipher_op(struct ablkcipher_request *req, int ret)
998 if (ret == -EINPROGRESS || ret == -EBUSY) {
999 struct tcrypt_result *tr = req->base.data;
1001 wait_for_completion(&tr->completion);
1002 reinit_completion(&tr->completion);
1003 ret = tr->err;
1006 return ret;
1009 static int test_acipher_jiffies(struct ablkcipher_request *req, int enc,
1010 int blen, int secs)
1012 unsigned long start, end;
1013 int bcount;
1014 int ret;
1016 for (start = jiffies, end = start + secs * HZ, bcount = 0;
1017 time_before(jiffies, end); bcount++) {
1018 if (enc)
1019 ret = do_one_acipher_op(req,
1020 crypto_ablkcipher_encrypt(req));
1021 else
1022 ret = do_one_acipher_op(req,
1023 crypto_ablkcipher_decrypt(req));
1025 if (ret)
1026 return ret;
1029 pr_cont("%d operations in %d seconds (%ld bytes)\n",
1030 bcount, secs, (long)bcount * blen);
1031 return 0;
1034 static int test_acipher_cycles(struct ablkcipher_request *req, int enc,
1035 int blen)
1037 unsigned long cycles = 0;
1038 int ret = 0;
1039 int i;
1041 /* Warm-up run. */
1042 for (i = 0; i < 4; i++) {
1043 if (enc)
1044 ret = do_one_acipher_op(req,
1045 crypto_ablkcipher_encrypt(req));
1046 else
1047 ret = do_one_acipher_op(req,
1048 crypto_ablkcipher_decrypt(req));
1050 if (ret)
1051 goto out;
1054 /* The real thing. */
1055 for (i = 0; i < 8; i++) {
1056 cycles_t start, end;
1058 start = get_cycles();
1059 if (enc)
1060 ret = do_one_acipher_op(req,
1061 crypto_ablkcipher_encrypt(req));
1062 else
1063 ret = do_one_acipher_op(req,
1064 crypto_ablkcipher_decrypt(req));
1065 end = get_cycles();
1067 if (ret)
1068 goto out;
1070 cycles += end - start;
1073 out:
1074 if (ret == 0)
1075 pr_cont("1 operation in %lu cycles (%d bytes)\n",
1076 (cycles + 4) / 8, blen);
1078 return ret;
1081 static void test_acipher_speed(const char *algo, int enc, unsigned int secs,
1082 struct cipher_speed_template *template,
1083 unsigned int tcount, u8 *keysize)
1085 unsigned int ret, i, j, k, iv_len;
1086 struct tcrypt_result tresult;
1087 const char *key;
1088 char iv[128];
1089 struct ablkcipher_request *req;
1090 struct crypto_ablkcipher *tfm;
1091 const char *e;
1092 u32 *b_size;
1094 if (enc == ENCRYPT)
1095 e = "encryption";
1096 else
1097 e = "decryption";
1099 init_completion(&tresult.completion);
1101 tfm = crypto_alloc_ablkcipher(algo, 0, 0);
1103 if (IS_ERR(tfm)) {
1104 pr_err("failed to load transform for %s: %ld\n", algo,
1105 PTR_ERR(tfm));
1106 return;
1109 pr_info("\ntesting speed of async %s (%s) %s\n", algo,
1110 get_driver_name(crypto_ablkcipher, tfm), e);
1112 req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
1113 if (!req) {
1114 pr_err("tcrypt: skcipher: Failed to allocate request for %s\n",
1115 algo);
1116 goto out;
1119 ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1120 tcrypt_complete, &tresult);
1122 i = 0;
1123 do {
1124 b_size = block_sizes;
1126 do {
1127 struct scatterlist sg[TVMEMSIZE];
1129 if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
1130 pr_err("template (%u) too big for "
1131 "tvmem (%lu)\n", *keysize + *b_size,
1132 TVMEMSIZE * PAGE_SIZE);
1133 goto out_free_req;
1136 pr_info("test %u (%d bit key, %d byte blocks): ", i,
1137 *keysize * 8, *b_size);
1139 memset(tvmem[0], 0xff, PAGE_SIZE);
1141 /* set key, plain text and IV */
1142 key = tvmem[0];
1143 for (j = 0; j < tcount; j++) {
1144 if (template[j].klen == *keysize) {
1145 key = template[j].key;
1146 break;
1150 crypto_ablkcipher_clear_flags(tfm, ~0);
1152 ret = crypto_ablkcipher_setkey(tfm, key, *keysize);
1153 if (ret) {
1154 pr_err("setkey() failed flags=%x\n",
1155 crypto_ablkcipher_get_flags(tfm));
1156 goto out_free_req;
1159 k = *keysize + *b_size;
1160 sg_init_table(sg, DIV_ROUND_UP(k, PAGE_SIZE));
1162 if (k > PAGE_SIZE) {
1163 sg_set_buf(sg, tvmem[0] + *keysize,
1164 PAGE_SIZE - *keysize);
1165 k -= PAGE_SIZE;
1166 j = 1;
1167 while (k > PAGE_SIZE) {
1168 sg_set_buf(sg + j, tvmem[j], PAGE_SIZE);
1169 memset(tvmem[j], 0xff, PAGE_SIZE);
1170 j++;
1171 k -= PAGE_SIZE;
1173 sg_set_buf(sg + j, tvmem[j], k);
1174 memset(tvmem[j], 0xff, k);
1175 } else {
1176 sg_set_buf(sg, tvmem[0] + *keysize, *b_size);
1179 iv_len = crypto_ablkcipher_ivsize(tfm);
1180 if (iv_len)
1181 memset(&iv, 0xff, iv_len);
1183 ablkcipher_request_set_crypt(req, sg, sg, *b_size, iv);
1185 if (secs)
1186 ret = test_acipher_jiffies(req, enc,
1187 *b_size, secs);
1188 else
1189 ret = test_acipher_cycles(req, enc,
1190 *b_size);
1192 if (ret) {
1193 pr_err("%s() failed flags=%x\n", e,
1194 crypto_ablkcipher_get_flags(tfm));
1195 break;
1197 b_size++;
1198 i++;
1199 } while (*b_size);
1200 keysize++;
1201 } while (*keysize);
1203 out_free_req:
1204 ablkcipher_request_free(req);
1205 out:
1206 crypto_free_ablkcipher(tfm);
1209 static void test_available(void)
1211 char **name = check;
1213 while (*name) {
1214 printk("alg %s ", *name);
1215 printk(crypto_has_alg(*name, 0, 0) ?
1216 "found\n" : "not found\n");
1217 name++;
1221 static inline int tcrypt_test(const char *alg)
1223 int ret;
1225 ret = alg_test(alg, alg, 0, 0);
1226 /* non-fips algs return -EINVAL in fips mode */
1227 if (fips_enabled && ret == -EINVAL)
1228 ret = 0;
1229 return ret;
1232 static int do_test(const char *alg, u32 type, u32 mask, int m)
1234 int i;
1235 int ret = 0;
1237 switch (m) {
1238 case 0:
1239 if (alg) {
1240 if (!crypto_has_alg(alg, type,
1241 mask ?: CRYPTO_ALG_TYPE_MASK))
1242 ret = -ENOENT;
1243 break;
1246 for (i = 1; i < 200; i++)
1247 ret += do_test(NULL, 0, 0, i);
1248 break;
1250 case 1:
1251 ret += tcrypt_test("md5");
1252 break;
1254 case 2:
1255 ret += tcrypt_test("sha1");
1256 break;
1258 case 3:
1259 ret += tcrypt_test("ecb(des)");
1260 ret += tcrypt_test("cbc(des)");
1261 ret += tcrypt_test("ctr(des)");
1262 break;
1264 case 4:
1265 ret += tcrypt_test("ecb(des3_ede)");
1266 ret += tcrypt_test("cbc(des3_ede)");
1267 ret += tcrypt_test("ctr(des3_ede)");
1268 break;
1270 case 5:
1271 ret += tcrypt_test("md4");
1272 break;
1274 case 6:
1275 ret += tcrypt_test("sha256");
1276 break;
1278 case 7:
1279 ret += tcrypt_test("ecb(blowfish)");
1280 ret += tcrypt_test("cbc(blowfish)");
1281 ret += tcrypt_test("ctr(blowfish)");
1282 break;
1284 case 8:
1285 ret += tcrypt_test("ecb(twofish)");
1286 ret += tcrypt_test("cbc(twofish)");
1287 ret += tcrypt_test("ctr(twofish)");
1288 ret += tcrypt_test("lrw(twofish)");
1289 ret += tcrypt_test("xts(twofish)");
1290 break;
1292 case 9:
1293 ret += tcrypt_test("ecb(serpent)");
1294 ret += tcrypt_test("cbc(serpent)");
1295 ret += tcrypt_test("ctr(serpent)");
1296 ret += tcrypt_test("lrw(serpent)");
1297 ret += tcrypt_test("xts(serpent)");
1298 break;
1300 case 10:
1301 ret += tcrypt_test("ecb(aes)");
1302 ret += tcrypt_test("cbc(aes)");
1303 ret += tcrypt_test("lrw(aes)");
1304 ret += tcrypt_test("xts(aes)");
1305 ret += tcrypt_test("ctr(aes)");
1306 ret += tcrypt_test("rfc3686(ctr(aes))");
1307 break;
1309 case 11:
1310 ret += tcrypt_test("sha384");
1311 break;
1313 case 12:
1314 ret += tcrypt_test("sha512");
1315 break;
1317 case 13:
1318 ret += tcrypt_test("deflate");
1319 break;
1321 case 14:
1322 ret += tcrypt_test("ecb(cast5)");
1323 ret += tcrypt_test("cbc(cast5)");
1324 ret += tcrypt_test("ctr(cast5)");
1325 break;
1327 case 15:
1328 ret += tcrypt_test("ecb(cast6)");
1329 ret += tcrypt_test("cbc(cast6)");
1330 ret += tcrypt_test("ctr(cast6)");
1331 ret += tcrypt_test("lrw(cast6)");
1332 ret += tcrypt_test("xts(cast6)");
1333 break;
1335 case 16:
1336 ret += tcrypt_test("ecb(arc4)");
1337 break;
1339 case 17:
1340 ret += tcrypt_test("michael_mic");
1341 break;
1343 case 18:
1344 ret += tcrypt_test("crc32c");
1345 break;
1347 case 19:
1348 ret += tcrypt_test("ecb(tea)");
1349 break;
1351 case 20:
1352 ret += tcrypt_test("ecb(xtea)");
1353 break;
1355 case 21:
1356 ret += tcrypt_test("ecb(khazad)");
1357 break;
1359 case 22:
1360 ret += tcrypt_test("wp512");
1361 break;
1363 case 23:
1364 ret += tcrypt_test("wp384");
1365 break;
1367 case 24:
1368 ret += tcrypt_test("wp256");
1369 break;
1371 case 25:
1372 ret += tcrypt_test("ecb(tnepres)");
1373 break;
1375 case 26:
1376 ret += tcrypt_test("ecb(anubis)");
1377 ret += tcrypt_test("cbc(anubis)");
1378 break;
1380 case 27:
1381 ret += tcrypt_test("tgr192");
1382 break;
1384 case 28:
1385 ret += tcrypt_test("tgr160");
1386 break;
1388 case 29:
1389 ret += tcrypt_test("tgr128");
1390 break;
1392 case 30:
1393 ret += tcrypt_test("ecb(xeta)");
1394 break;
1396 case 31:
1397 ret += tcrypt_test("pcbc(fcrypt)");
1398 break;
1400 case 32:
1401 ret += tcrypt_test("ecb(camellia)");
1402 ret += tcrypt_test("cbc(camellia)");
1403 ret += tcrypt_test("ctr(camellia)");
1404 ret += tcrypt_test("lrw(camellia)");
1405 ret += tcrypt_test("xts(camellia)");
1406 break;
1408 case 33:
1409 ret += tcrypt_test("sha224");
1410 break;
1412 case 34:
1413 ret += tcrypt_test("salsa20");
1414 break;
1416 case 35:
1417 ret += tcrypt_test("gcm(aes)");
1418 break;
1420 case 36:
1421 ret += tcrypt_test("lzo");
1422 break;
1424 case 37:
1425 ret += tcrypt_test("ccm(aes)");
1426 break;
1428 case 38:
1429 ret += tcrypt_test("cts(cbc(aes))");
1430 break;
1432 case 39:
1433 ret += tcrypt_test("rmd128");
1434 break;
1436 case 40:
1437 ret += tcrypt_test("rmd160");
1438 break;
1440 case 41:
1441 ret += tcrypt_test("rmd256");
1442 break;
1444 case 42:
1445 ret += tcrypt_test("rmd320");
1446 break;
1448 case 43:
1449 ret += tcrypt_test("ecb(seed)");
1450 break;
1452 case 44:
1453 ret += tcrypt_test("zlib");
1454 break;
1456 case 45:
1457 ret += tcrypt_test("rfc4309(ccm(aes))");
1458 break;
1460 case 46:
1461 ret += tcrypt_test("ghash");
1462 break;
1464 case 47:
1465 ret += tcrypt_test("crct10dif");
1466 break;
1468 case 100:
1469 ret += tcrypt_test("hmac(md5)");
1470 break;
1472 case 101:
1473 ret += tcrypt_test("hmac(sha1)");
1474 break;
1476 case 102:
1477 ret += tcrypt_test("hmac(sha256)");
1478 break;
1480 case 103:
1481 ret += tcrypt_test("hmac(sha384)");
1482 break;
1484 case 104:
1485 ret += tcrypt_test("hmac(sha512)");
1486 break;
1488 case 105:
1489 ret += tcrypt_test("hmac(sha224)");
1490 break;
1492 case 106:
1493 ret += tcrypt_test("xcbc(aes)");
1494 break;
1496 case 107:
1497 ret += tcrypt_test("hmac(rmd128)");
1498 break;
1500 case 108:
1501 ret += tcrypt_test("hmac(rmd160)");
1502 break;
1504 case 109:
1505 ret += tcrypt_test("vmac(aes)");
1506 break;
1508 case 110:
1509 ret += tcrypt_test("hmac(crc32)");
1510 break;
1512 case 150:
1513 ret += tcrypt_test("ansi_cprng");
1514 break;
1516 case 151:
1517 ret += tcrypt_test("rfc4106(gcm(aes))");
1518 break;
1520 case 152:
1521 ret += tcrypt_test("rfc4543(gcm(aes))");
1522 break;
1524 case 153:
1525 ret += tcrypt_test("cmac(aes)");
1526 break;
1528 case 154:
1529 ret += tcrypt_test("cmac(des3_ede)");
1530 break;
1532 case 155:
1533 ret += tcrypt_test("authenc(hmac(sha1),cbc(aes))");
1534 break;
1536 case 156:
1537 ret += tcrypt_test("authenc(hmac(md5),ecb(cipher_null))");
1538 break;
1540 case 157:
1541 ret += tcrypt_test("authenc(hmac(sha1),ecb(cipher_null))");
1542 break;
1543 case 181:
1544 ret += tcrypt_test("authenc(hmac(sha1),cbc(des))");
1545 break;
1546 case 182:
1547 ret += tcrypt_test("authenc(hmac(sha1),cbc(des3_ede))");
1548 break;
1549 case 183:
1550 ret += tcrypt_test("authenc(hmac(sha224),cbc(des))");
1551 break;
1552 case 184:
1553 ret += tcrypt_test("authenc(hmac(sha224),cbc(des3_ede))");
1554 break;
1555 case 185:
1556 ret += tcrypt_test("authenc(hmac(sha256),cbc(des))");
1557 break;
1558 case 186:
1559 ret += tcrypt_test("authenc(hmac(sha256),cbc(des3_ede))");
1560 break;
1561 case 187:
1562 ret += tcrypt_test("authenc(hmac(sha384),cbc(des))");
1563 break;
1564 case 188:
1565 ret += tcrypt_test("authenc(hmac(sha384),cbc(des3_ede))");
1566 break;
1567 case 189:
1568 ret += tcrypt_test("authenc(hmac(sha512),cbc(des))");
1569 break;
1570 case 190:
1571 ret += tcrypt_test("authenc(hmac(sha512),cbc(des3_ede))");
1572 break;
1573 case 200:
1574 test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
1575 speed_template_16_24_32);
1576 test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
1577 speed_template_16_24_32);
1578 test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
1579 speed_template_16_24_32);
1580 test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
1581 speed_template_16_24_32);
1582 test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
1583 speed_template_32_40_48);
1584 test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
1585 speed_template_32_40_48);
1586 test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
1587 speed_template_32_48_64);
1588 test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
1589 speed_template_32_48_64);
1590 test_cipher_speed("ctr(aes)", ENCRYPT, sec, NULL, 0,
1591 speed_template_16_24_32);
1592 test_cipher_speed("ctr(aes)", DECRYPT, sec, NULL, 0,
1593 speed_template_16_24_32);
1594 break;
1596 case 201:
1597 test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
1598 des3_speed_template, DES3_SPEED_VECTORS,
1599 speed_template_24);
1600 test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
1601 des3_speed_template, DES3_SPEED_VECTORS,
1602 speed_template_24);
1603 test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
1604 des3_speed_template, DES3_SPEED_VECTORS,
1605 speed_template_24);
1606 test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
1607 des3_speed_template, DES3_SPEED_VECTORS,
1608 speed_template_24);
1609 test_cipher_speed("ctr(des3_ede)", ENCRYPT, sec,
1610 des3_speed_template, DES3_SPEED_VECTORS,
1611 speed_template_24);
1612 test_cipher_speed("ctr(des3_ede)", DECRYPT, sec,
1613 des3_speed_template, DES3_SPEED_VECTORS,
1614 speed_template_24);
1615 break;
1617 case 202:
1618 test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
1619 speed_template_16_24_32);
1620 test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
1621 speed_template_16_24_32);
1622 test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
1623 speed_template_16_24_32);
1624 test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
1625 speed_template_16_24_32);
1626 test_cipher_speed("ctr(twofish)", ENCRYPT, sec, NULL, 0,
1627 speed_template_16_24_32);
1628 test_cipher_speed("ctr(twofish)", DECRYPT, sec, NULL, 0,
1629 speed_template_16_24_32);
1630 test_cipher_speed("lrw(twofish)", ENCRYPT, sec, NULL, 0,
1631 speed_template_32_40_48);
1632 test_cipher_speed("lrw(twofish)", DECRYPT, sec, NULL, 0,
1633 speed_template_32_40_48);
1634 test_cipher_speed("xts(twofish)", ENCRYPT, sec, NULL, 0,
1635 speed_template_32_48_64);
1636 test_cipher_speed("xts(twofish)", DECRYPT, sec, NULL, 0,
1637 speed_template_32_48_64);
1638 break;
1640 case 203:
1641 test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
1642 speed_template_8_32);
1643 test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
1644 speed_template_8_32);
1645 test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
1646 speed_template_8_32);
1647 test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
1648 speed_template_8_32);
1649 test_cipher_speed("ctr(blowfish)", ENCRYPT, sec, NULL, 0,
1650 speed_template_8_32);
1651 test_cipher_speed("ctr(blowfish)", DECRYPT, sec, NULL, 0,
1652 speed_template_8_32);
1653 break;
1655 case 204:
1656 test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
1657 speed_template_8);
1658 test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
1659 speed_template_8);
1660 test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
1661 speed_template_8);
1662 test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
1663 speed_template_8);
1664 break;
1666 case 205:
1667 test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
1668 speed_template_16_24_32);
1669 test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
1670 speed_template_16_24_32);
1671 test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
1672 speed_template_16_24_32);
1673 test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
1674 speed_template_16_24_32);
1675 test_cipher_speed("ctr(camellia)", ENCRYPT, sec, NULL, 0,
1676 speed_template_16_24_32);
1677 test_cipher_speed("ctr(camellia)", DECRYPT, sec, NULL, 0,
1678 speed_template_16_24_32);
1679 test_cipher_speed("lrw(camellia)", ENCRYPT, sec, NULL, 0,
1680 speed_template_32_40_48);
1681 test_cipher_speed("lrw(camellia)", DECRYPT, sec, NULL, 0,
1682 speed_template_32_40_48);
1683 test_cipher_speed("xts(camellia)", ENCRYPT, sec, NULL, 0,
1684 speed_template_32_48_64);
1685 test_cipher_speed("xts(camellia)", DECRYPT, sec, NULL, 0,
1686 speed_template_32_48_64);
1687 break;
1689 case 206:
1690 test_cipher_speed("salsa20", ENCRYPT, sec, NULL, 0,
1691 speed_template_16_32);
1692 break;
1694 case 207:
1695 test_cipher_speed("ecb(serpent)", ENCRYPT, sec, NULL, 0,
1696 speed_template_16_32);
1697 test_cipher_speed("ecb(serpent)", DECRYPT, sec, NULL, 0,
1698 speed_template_16_32);
1699 test_cipher_speed("cbc(serpent)", ENCRYPT, sec, NULL, 0,
1700 speed_template_16_32);
1701 test_cipher_speed("cbc(serpent)", DECRYPT, sec, NULL, 0,
1702 speed_template_16_32);
1703 test_cipher_speed("ctr(serpent)", ENCRYPT, sec, NULL, 0,
1704 speed_template_16_32);
1705 test_cipher_speed("ctr(serpent)", DECRYPT, sec, NULL, 0,
1706 speed_template_16_32);
1707 test_cipher_speed("lrw(serpent)", ENCRYPT, sec, NULL, 0,
1708 speed_template_32_48);
1709 test_cipher_speed("lrw(serpent)", DECRYPT, sec, NULL, 0,
1710 speed_template_32_48);
1711 test_cipher_speed("xts(serpent)", ENCRYPT, sec, NULL, 0,
1712 speed_template_32_64);
1713 test_cipher_speed("xts(serpent)", DECRYPT, sec, NULL, 0,
1714 speed_template_32_64);
1715 break;
1717 case 208:
1718 test_cipher_speed("ecb(arc4)", ENCRYPT, sec, NULL, 0,
1719 speed_template_8);
1720 break;
1722 case 209:
1723 test_cipher_speed("ecb(cast5)", ENCRYPT, sec, NULL, 0,
1724 speed_template_8_16);
1725 test_cipher_speed("ecb(cast5)", DECRYPT, sec, NULL, 0,
1726 speed_template_8_16);
1727 test_cipher_speed("cbc(cast5)", ENCRYPT, sec, NULL, 0,
1728 speed_template_8_16);
1729 test_cipher_speed("cbc(cast5)", DECRYPT, sec, NULL, 0,
1730 speed_template_8_16);
1731 test_cipher_speed("ctr(cast5)", ENCRYPT, sec, NULL, 0,
1732 speed_template_8_16);
1733 test_cipher_speed("ctr(cast5)", DECRYPT, sec, NULL, 0,
1734 speed_template_8_16);
1735 break;
1737 case 210:
1738 test_cipher_speed("ecb(cast6)", ENCRYPT, sec, NULL, 0,
1739 speed_template_16_32);
1740 test_cipher_speed("ecb(cast6)", DECRYPT, sec, NULL, 0,
1741 speed_template_16_32);
1742 test_cipher_speed("cbc(cast6)", ENCRYPT, sec, NULL, 0,
1743 speed_template_16_32);
1744 test_cipher_speed("cbc(cast6)", DECRYPT, sec, NULL, 0,
1745 speed_template_16_32);
1746 test_cipher_speed("ctr(cast6)", ENCRYPT, sec, NULL, 0,
1747 speed_template_16_32);
1748 test_cipher_speed("ctr(cast6)", DECRYPT, sec, NULL, 0,
1749 speed_template_16_32);
1750 test_cipher_speed("lrw(cast6)", ENCRYPT, sec, NULL, 0,
1751 speed_template_32_48);
1752 test_cipher_speed("lrw(cast6)", DECRYPT, sec, NULL, 0,
1753 speed_template_32_48);
1754 test_cipher_speed("xts(cast6)", ENCRYPT, sec, NULL, 0,
1755 speed_template_32_64);
1756 test_cipher_speed("xts(cast6)", DECRYPT, sec, NULL, 0,
1757 speed_template_32_64);
1758 break;
1760 case 211:
1761 test_aead_speed("rfc4106(gcm(aes))", ENCRYPT, sec,
1762 NULL, 0, 16, 8, aead_speed_template_20);
1763 break;
1765 case 212:
1766 test_aead_speed("rfc4309(ccm(aes))", ENCRYPT, sec,
1767 NULL, 0, 16, 8, aead_speed_template_19);
1768 break;
1770 case 300:
1771 if (alg) {
1772 test_hash_speed(alg, sec, generic_hash_speed_template);
1773 break;
1776 /* fall through */
1778 case 301:
1779 test_hash_speed("md4", sec, generic_hash_speed_template);
1780 if (mode > 300 && mode < 400) break;
1782 case 302:
1783 test_hash_speed("md5", sec, generic_hash_speed_template);
1784 if (mode > 300 && mode < 400) break;
1786 case 303:
1787 test_hash_speed("sha1", sec, generic_hash_speed_template);
1788 if (mode > 300 && mode < 400) break;
1790 case 304:
1791 test_hash_speed("sha256", sec, generic_hash_speed_template);
1792 if (mode > 300 && mode < 400) break;
1794 case 305:
1795 test_hash_speed("sha384", sec, generic_hash_speed_template);
1796 if (mode > 300 && mode < 400) break;
1798 case 306:
1799 test_hash_speed("sha512", sec, generic_hash_speed_template);
1800 if (mode > 300 && mode < 400) break;
1802 case 307:
1803 test_hash_speed("wp256", sec, generic_hash_speed_template);
1804 if (mode > 300 && mode < 400) break;
1806 case 308:
1807 test_hash_speed("wp384", sec, generic_hash_speed_template);
1808 if (mode > 300 && mode < 400) break;
1810 case 309:
1811 test_hash_speed("wp512", sec, generic_hash_speed_template);
1812 if (mode > 300 && mode < 400) break;
1814 case 310:
1815 test_hash_speed("tgr128", sec, generic_hash_speed_template);
1816 if (mode > 300 && mode < 400) break;
1818 case 311:
1819 test_hash_speed("tgr160", sec, generic_hash_speed_template);
1820 if (mode > 300 && mode < 400) break;
1822 case 312:
1823 test_hash_speed("tgr192", sec, generic_hash_speed_template);
1824 if (mode > 300 && mode < 400) break;
1826 case 313:
1827 test_hash_speed("sha224", sec, generic_hash_speed_template);
1828 if (mode > 300 && mode < 400) break;
1830 case 314:
1831 test_hash_speed("rmd128", sec, generic_hash_speed_template);
1832 if (mode > 300 && mode < 400) break;
1834 case 315:
1835 test_hash_speed("rmd160", sec, generic_hash_speed_template);
1836 if (mode > 300 && mode < 400) break;
1838 case 316:
1839 test_hash_speed("rmd256", sec, generic_hash_speed_template);
1840 if (mode > 300 && mode < 400) break;
1842 case 317:
1843 test_hash_speed("rmd320", sec, generic_hash_speed_template);
1844 if (mode > 300 && mode < 400) break;
1846 case 318:
1847 test_hash_speed("ghash-generic", sec, hash_speed_template_16);
1848 if (mode > 300 && mode < 400) break;
1850 case 319:
1851 test_hash_speed("crc32c", sec, generic_hash_speed_template);
1852 if (mode > 300 && mode < 400) break;
1854 case 320:
1855 test_hash_speed("crct10dif", sec, generic_hash_speed_template);
1856 if (mode > 300 && mode < 400) break;
1858 case 399:
1859 break;
1861 case 400:
1862 if (alg) {
1863 test_ahash_speed(alg, sec, generic_hash_speed_template);
1864 break;
1867 /* fall through */
1869 case 401:
1870 test_ahash_speed("md4", sec, generic_hash_speed_template);
1871 if (mode > 400 && mode < 500) break;
1873 case 402:
1874 test_ahash_speed("md5", sec, generic_hash_speed_template);
1875 if (mode > 400 && mode < 500) break;
1877 case 403:
1878 test_ahash_speed("sha1", sec, generic_hash_speed_template);
1879 if (mode > 400 && mode < 500) break;
1881 case 404:
1882 test_ahash_speed("sha256", sec, generic_hash_speed_template);
1883 if (mode > 400 && mode < 500) break;
1885 case 405:
1886 test_ahash_speed("sha384", sec, generic_hash_speed_template);
1887 if (mode > 400 && mode < 500) break;
1889 case 406:
1890 test_ahash_speed("sha512", sec, generic_hash_speed_template);
1891 if (mode > 400 && mode < 500) break;
1893 case 407:
1894 test_ahash_speed("wp256", sec, generic_hash_speed_template);
1895 if (mode > 400 && mode < 500) break;
1897 case 408:
1898 test_ahash_speed("wp384", sec, generic_hash_speed_template);
1899 if (mode > 400 && mode < 500) break;
1901 case 409:
1902 test_ahash_speed("wp512", sec, generic_hash_speed_template);
1903 if (mode > 400 && mode < 500) break;
1905 case 410:
1906 test_ahash_speed("tgr128", sec, generic_hash_speed_template);
1907 if (mode > 400 && mode < 500) break;
1909 case 411:
1910 test_ahash_speed("tgr160", sec, generic_hash_speed_template);
1911 if (mode > 400 && mode < 500) break;
1913 case 412:
1914 test_ahash_speed("tgr192", sec, generic_hash_speed_template);
1915 if (mode > 400 && mode < 500) break;
1917 case 413:
1918 test_ahash_speed("sha224", sec, generic_hash_speed_template);
1919 if (mode > 400 && mode < 500) break;
1921 case 414:
1922 test_ahash_speed("rmd128", sec, generic_hash_speed_template);
1923 if (mode > 400 && mode < 500) break;
1925 case 415:
1926 test_ahash_speed("rmd160", sec, generic_hash_speed_template);
1927 if (mode > 400 && mode < 500) break;
1929 case 416:
1930 test_ahash_speed("rmd256", sec, generic_hash_speed_template);
1931 if (mode > 400 && mode < 500) break;
1933 case 417:
1934 test_ahash_speed("rmd320", sec, generic_hash_speed_template);
1935 if (mode > 400 && mode < 500) break;
1937 case 499:
1938 break;
1940 case 500:
1941 test_acipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
1942 speed_template_16_24_32);
1943 test_acipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
1944 speed_template_16_24_32);
1945 test_acipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
1946 speed_template_16_24_32);
1947 test_acipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
1948 speed_template_16_24_32);
1949 test_acipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
1950 speed_template_32_40_48);
1951 test_acipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
1952 speed_template_32_40_48);
1953 test_acipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
1954 speed_template_32_48_64);
1955 test_acipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
1956 speed_template_32_48_64);
1957 test_acipher_speed("ctr(aes)", ENCRYPT, sec, NULL, 0,
1958 speed_template_16_24_32);
1959 test_acipher_speed("ctr(aes)", DECRYPT, sec, NULL, 0,
1960 speed_template_16_24_32);
1961 test_acipher_speed("cfb(aes)", ENCRYPT, sec, NULL, 0,
1962 speed_template_16_24_32);
1963 test_acipher_speed("cfb(aes)", DECRYPT, sec, NULL, 0,
1964 speed_template_16_24_32);
1965 test_acipher_speed("ofb(aes)", ENCRYPT, sec, NULL, 0,
1966 speed_template_16_24_32);
1967 test_acipher_speed("ofb(aes)", DECRYPT, sec, NULL, 0,
1968 speed_template_16_24_32);
1969 test_acipher_speed("rfc3686(ctr(aes))", ENCRYPT, sec, NULL, 0,
1970 speed_template_20_28_36);
1971 test_acipher_speed("rfc3686(ctr(aes))", DECRYPT, sec, NULL, 0,
1972 speed_template_20_28_36);
1973 break;
1975 case 501:
1976 test_acipher_speed("ecb(des3_ede)", ENCRYPT, sec,
1977 des3_speed_template, DES3_SPEED_VECTORS,
1978 speed_template_24);
1979 test_acipher_speed("ecb(des3_ede)", DECRYPT, sec,
1980 des3_speed_template, DES3_SPEED_VECTORS,
1981 speed_template_24);
1982 test_acipher_speed("cbc(des3_ede)", ENCRYPT, sec,
1983 des3_speed_template, DES3_SPEED_VECTORS,
1984 speed_template_24);
1985 test_acipher_speed("cbc(des3_ede)", DECRYPT, sec,
1986 des3_speed_template, DES3_SPEED_VECTORS,
1987 speed_template_24);
1988 test_acipher_speed("cfb(des3_ede)", ENCRYPT, sec,
1989 des3_speed_template, DES3_SPEED_VECTORS,
1990 speed_template_24);
1991 test_acipher_speed("cfb(des3_ede)", DECRYPT, sec,
1992 des3_speed_template, DES3_SPEED_VECTORS,
1993 speed_template_24);
1994 test_acipher_speed("ofb(des3_ede)", ENCRYPT, sec,
1995 des3_speed_template, DES3_SPEED_VECTORS,
1996 speed_template_24);
1997 test_acipher_speed("ofb(des3_ede)", DECRYPT, sec,
1998 des3_speed_template, DES3_SPEED_VECTORS,
1999 speed_template_24);
2000 break;
2002 case 502:
2003 test_acipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
2004 speed_template_8);
2005 test_acipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
2006 speed_template_8);
2007 test_acipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
2008 speed_template_8);
2009 test_acipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
2010 speed_template_8);
2011 test_acipher_speed("cfb(des)", ENCRYPT, sec, NULL, 0,
2012 speed_template_8);
2013 test_acipher_speed("cfb(des)", DECRYPT, sec, NULL, 0,
2014 speed_template_8);
2015 test_acipher_speed("ofb(des)", ENCRYPT, sec, NULL, 0,
2016 speed_template_8);
2017 test_acipher_speed("ofb(des)", DECRYPT, sec, NULL, 0,
2018 speed_template_8);
2019 break;
2021 case 503:
2022 test_acipher_speed("ecb(serpent)", ENCRYPT, sec, NULL, 0,
2023 speed_template_16_32);
2024 test_acipher_speed("ecb(serpent)", DECRYPT, sec, NULL, 0,
2025 speed_template_16_32);
2026 test_acipher_speed("cbc(serpent)", ENCRYPT, sec, NULL, 0,
2027 speed_template_16_32);
2028 test_acipher_speed("cbc(serpent)", DECRYPT, sec, NULL, 0,
2029 speed_template_16_32);
2030 test_acipher_speed("ctr(serpent)", ENCRYPT, sec, NULL, 0,
2031 speed_template_16_32);
2032 test_acipher_speed("ctr(serpent)", DECRYPT, sec, NULL, 0,
2033 speed_template_16_32);
2034 test_acipher_speed("lrw(serpent)", ENCRYPT, sec, NULL, 0,
2035 speed_template_32_48);
2036 test_acipher_speed("lrw(serpent)", DECRYPT, sec, NULL, 0,
2037 speed_template_32_48);
2038 test_acipher_speed("xts(serpent)", ENCRYPT, sec, NULL, 0,
2039 speed_template_32_64);
2040 test_acipher_speed("xts(serpent)", DECRYPT, sec, NULL, 0,
2041 speed_template_32_64);
2042 break;
2044 case 504:
2045 test_acipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
2046 speed_template_16_24_32);
2047 test_acipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
2048 speed_template_16_24_32);
2049 test_acipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
2050 speed_template_16_24_32);
2051 test_acipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
2052 speed_template_16_24_32);
2053 test_acipher_speed("ctr(twofish)", ENCRYPT, sec, NULL, 0,
2054 speed_template_16_24_32);
2055 test_acipher_speed("ctr(twofish)", DECRYPT, sec, NULL, 0,
2056 speed_template_16_24_32);
2057 test_acipher_speed("lrw(twofish)", ENCRYPT, sec, NULL, 0,
2058 speed_template_32_40_48);
2059 test_acipher_speed("lrw(twofish)", DECRYPT, sec, NULL, 0,
2060 speed_template_32_40_48);
2061 test_acipher_speed("xts(twofish)", ENCRYPT, sec, NULL, 0,
2062 speed_template_32_48_64);
2063 test_acipher_speed("xts(twofish)", DECRYPT, sec, NULL, 0,
2064 speed_template_32_48_64);
2065 break;
2067 case 505:
2068 test_acipher_speed("ecb(arc4)", ENCRYPT, sec, NULL, 0,
2069 speed_template_8);
2070 break;
2072 case 506:
2073 test_acipher_speed("ecb(cast5)", ENCRYPT, sec, NULL, 0,
2074 speed_template_8_16);
2075 test_acipher_speed("ecb(cast5)", DECRYPT, sec, NULL, 0,
2076 speed_template_8_16);
2077 test_acipher_speed("cbc(cast5)", ENCRYPT, sec, NULL, 0,
2078 speed_template_8_16);
2079 test_acipher_speed("cbc(cast5)", DECRYPT, sec, NULL, 0,
2080 speed_template_8_16);
2081 test_acipher_speed("ctr(cast5)", ENCRYPT, sec, NULL, 0,
2082 speed_template_8_16);
2083 test_acipher_speed("ctr(cast5)", DECRYPT, sec, NULL, 0,
2084 speed_template_8_16);
2085 break;
2087 case 507:
2088 test_acipher_speed("ecb(cast6)", ENCRYPT, sec, NULL, 0,
2089 speed_template_16_32);
2090 test_acipher_speed("ecb(cast6)", DECRYPT, sec, NULL, 0,
2091 speed_template_16_32);
2092 test_acipher_speed("cbc(cast6)", ENCRYPT, sec, NULL, 0,
2093 speed_template_16_32);
2094 test_acipher_speed("cbc(cast6)", DECRYPT, sec, NULL, 0,
2095 speed_template_16_32);
2096 test_acipher_speed("ctr(cast6)", ENCRYPT, sec, NULL, 0,
2097 speed_template_16_32);
2098 test_acipher_speed("ctr(cast6)", DECRYPT, sec, NULL, 0,
2099 speed_template_16_32);
2100 test_acipher_speed("lrw(cast6)", ENCRYPT, sec, NULL, 0,
2101 speed_template_32_48);
2102 test_acipher_speed("lrw(cast6)", DECRYPT, sec, NULL, 0,
2103 speed_template_32_48);
2104 test_acipher_speed("xts(cast6)", ENCRYPT, sec, NULL, 0,
2105 speed_template_32_64);
2106 test_acipher_speed("xts(cast6)", DECRYPT, sec, NULL, 0,
2107 speed_template_32_64);
2108 break;
2110 case 508:
2111 test_acipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
2112 speed_template_16_32);
2113 test_acipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
2114 speed_template_16_32);
2115 test_acipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
2116 speed_template_16_32);
2117 test_acipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
2118 speed_template_16_32);
2119 test_acipher_speed("ctr(camellia)", ENCRYPT, sec, NULL, 0,
2120 speed_template_16_32);
2121 test_acipher_speed("ctr(camellia)", DECRYPT, sec, NULL, 0,
2122 speed_template_16_32);
2123 test_acipher_speed("lrw(camellia)", ENCRYPT, sec, NULL, 0,
2124 speed_template_32_48);
2125 test_acipher_speed("lrw(camellia)", DECRYPT, sec, NULL, 0,
2126 speed_template_32_48);
2127 test_acipher_speed("xts(camellia)", ENCRYPT, sec, NULL, 0,
2128 speed_template_32_64);
2129 test_acipher_speed("xts(camellia)", DECRYPT, sec, NULL, 0,
2130 speed_template_32_64);
2131 break;
2133 case 509:
2134 test_acipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
2135 speed_template_8_32);
2136 test_acipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
2137 speed_template_8_32);
2138 test_acipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
2139 speed_template_8_32);
2140 test_acipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
2141 speed_template_8_32);
2142 test_acipher_speed("ctr(blowfish)", ENCRYPT, sec, NULL, 0,
2143 speed_template_8_32);
2144 test_acipher_speed("ctr(blowfish)", DECRYPT, sec, NULL, 0,
2145 speed_template_8_32);
2146 break;
2148 case 1000:
2149 test_available();
2150 break;
2153 return ret;
2156 static int __init tcrypt_mod_init(void)
2158 int err = -ENOMEM;
2159 int i;
2161 for (i = 0; i < TVMEMSIZE; i++) {
2162 tvmem[i] = (void *)__get_free_page(GFP_KERNEL);
2163 if (!tvmem[i])
2164 goto err_free_tv;
2167 err = do_test(alg, type, mask, mode);
2169 if (err) {
2170 printk(KERN_ERR "tcrypt: one or more tests failed!\n");
2171 goto err_free_tv;
2174 /* We intentionaly return -EAGAIN to prevent keeping the module,
2175 * unless we're running in fips mode. It does all its work from
2176 * init() and doesn't offer any runtime functionality, but in
2177 * the fips case, checking for a successful load is helpful.
2178 * => we don't need it in the memory, do we?
2179 * -- mludvig
2181 if (!fips_enabled)
2182 err = -EAGAIN;
2184 err_free_tv:
2185 for (i = 0; i < TVMEMSIZE && tvmem[i]; i++)
2186 free_page((unsigned long)tvmem[i]);
2188 return err;
2192 * If an init function is provided, an exit function must also be provided
2193 * to allow module unload.
2195 static void __exit tcrypt_mod_fini(void) { }
2197 module_init(tcrypt_mod_init);
2198 module_exit(tcrypt_mod_fini);
2200 module_param(alg, charp, 0);
2201 module_param(type, uint, 0);
2202 module_param(mask, uint, 0);
2203 module_param(mode, int, 0);
2204 module_param(sec, uint, 0);
2205 MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
2206 "(defaults to zero which uses CPU cycles instead)");
2208 MODULE_LICENSE("GPL");
2209 MODULE_DESCRIPTION("Quick & dirty crypto testing module");
2210 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");