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 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
18 #include <crypto/hash.h>
19 #include <linux/err.h>
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/scatterlist.h>
24 #include <linux/string.h>
25 #include <linux/moduleparam.h>
26 #include <linux/jiffies.h>
27 #include <linux/timex.h>
28 #include <linux/interrupt.h>
33 * Need slab memory for testing (size in number of pages).
38 * Used by test_cipher_speed()
44 * Used by test_cipher_speed()
46 static unsigned int sec;
49 static char *tvmem[TVMEMSIZE];
51 static char *check[] = {
52 "des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256",
53 "blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
54 "cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
55 "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", "fcrypt",
56 "camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
57 "lzo", "cts", "zlib", NULL
60 static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc,
61 struct scatterlist *sg, int blen, int sec)
63 unsigned long start, end;
67 for (start = jiffies, end = start + sec * HZ, bcount = 0;
68 time_before(jiffies, end); bcount++) {
70 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
72 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
78 printk("%d operations in %d seconds (%ld bytes)\n",
79 bcount, sec, (long)bcount * blen);
83 static int test_cipher_cycles(struct blkcipher_desc *desc, int enc,
84 struct scatterlist *sg, int blen)
86 unsigned long cycles = 0;
94 for (i = 0; i < 4; i++) {
96 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
98 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
104 /* The real thing. */
105 for (i = 0; i < 8; i++) {
108 start = get_cycles();
110 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
112 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
118 cycles += end - start;
126 printk("1 operation in %lu cycles (%d bytes)\n",
127 (cycles + 4) / 8, blen);
132 static u32 block_sizes[] = { 16, 64, 256, 1024, 8192, 0 };
134 static void test_cipher_speed(const char *algo, int enc, unsigned int sec,
135 struct cipher_speed_template *template,
136 unsigned int tcount, u8 *keysize)
138 unsigned int ret, i, j, iv_len;
139 const char *key, iv[128];
140 struct crypto_blkcipher *tfm;
141 struct blkcipher_desc desc;
150 printk("\ntesting speed of %s %s\n", algo, e);
152 tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);
155 printk("failed to load transform for %s: %ld\n", algo,
165 b_size = block_sizes;
167 struct scatterlist sg[TVMEMSIZE];
169 if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
170 printk("template (%u) too big for "
171 "tvmem (%lu)\n", *keysize + *b_size,
172 TVMEMSIZE * PAGE_SIZE);
176 printk("test %u (%d bit key, %d byte blocks): ", i,
177 *keysize * 8, *b_size);
179 memset(tvmem[0], 0xff, PAGE_SIZE);
181 /* set key, plain text and IV */
183 for (j = 0; j < tcount; j++) {
184 if (template[j].klen == *keysize) {
185 key = template[j].key;
190 ret = crypto_blkcipher_setkey(tfm, key, *keysize);
192 printk("setkey() failed flags=%x\n",
193 crypto_blkcipher_get_flags(tfm));
197 sg_init_table(sg, TVMEMSIZE);
198 sg_set_buf(sg, tvmem[0] + *keysize,
199 PAGE_SIZE - *keysize);
200 for (j = 1; j < TVMEMSIZE; j++) {
201 sg_set_buf(sg + j, tvmem[j], PAGE_SIZE);
202 memset (tvmem[j], 0xff, PAGE_SIZE);
205 iv_len = crypto_blkcipher_ivsize(tfm);
207 memset(&iv, 0xff, iv_len);
208 crypto_blkcipher_set_iv(tfm, iv, iv_len);
212 ret = test_cipher_jiffies(&desc, enc, sg,
215 ret = test_cipher_cycles(&desc, enc, sg,
219 printk("%s() failed flags=%x\n", e, desc.flags);
229 crypto_free_blkcipher(tfm);
232 static int test_hash_jiffies_digest(struct hash_desc *desc,
233 struct scatterlist *sg, int blen,
236 unsigned long start, end;
240 for (start = jiffies, end = start + sec * HZ, bcount = 0;
241 time_before(jiffies, end); bcount++) {
242 ret = crypto_hash_digest(desc, sg, blen, out);
247 printk("%6u opers/sec, %9lu bytes/sec\n",
248 bcount / sec, ((long)bcount * blen) / sec);
253 static int test_hash_jiffies(struct hash_desc *desc, struct scatterlist *sg,
254 int blen, int plen, char *out, int sec)
256 unsigned long start, end;
261 return test_hash_jiffies_digest(desc, sg, blen, out, sec);
263 for (start = jiffies, end = start + sec * HZ, bcount = 0;
264 time_before(jiffies, end); bcount++) {
265 ret = crypto_hash_init(desc);
268 for (pcount = 0; pcount < blen; pcount += plen) {
269 ret = crypto_hash_update(desc, sg, plen);
273 /* we assume there is enough space in 'out' for the result */
274 ret = crypto_hash_final(desc, out);
279 printk("%6u opers/sec, %9lu bytes/sec\n",
280 bcount / sec, ((long)bcount * blen) / sec);
285 static int test_hash_cycles_digest(struct hash_desc *desc,
286 struct scatterlist *sg, int blen, char *out)
288 unsigned long cycles = 0;
296 for (i = 0; i < 4; i++) {
297 ret = crypto_hash_digest(desc, sg, blen, out);
302 /* The real thing. */
303 for (i = 0; i < 8; i++) {
306 start = get_cycles();
308 ret = crypto_hash_digest(desc, sg, blen, out);
314 cycles += end - start;
324 printk("%6lu cycles/operation, %4lu cycles/byte\n",
325 cycles / 8, cycles / (8 * blen));
330 static int test_hash_cycles(struct hash_desc *desc, struct scatterlist *sg,
331 int blen, int plen, char *out)
333 unsigned long cycles = 0;
338 return test_hash_cycles_digest(desc, sg, blen, out);
344 for (i = 0; i < 4; i++) {
345 ret = crypto_hash_init(desc);
348 for (pcount = 0; pcount < blen; pcount += plen) {
349 ret = crypto_hash_update(desc, sg, plen);
353 ret = crypto_hash_final(desc, out);
358 /* The real thing. */
359 for (i = 0; i < 8; i++) {
362 start = get_cycles();
364 ret = crypto_hash_init(desc);
367 for (pcount = 0; pcount < blen; pcount += plen) {
368 ret = crypto_hash_update(desc, sg, plen);
372 ret = crypto_hash_final(desc, out);
378 cycles += end - start;
388 printk("%6lu cycles/operation, %4lu cycles/byte\n",
389 cycles / 8, cycles / (8 * blen));
394 static void test_hash_speed(const char *algo, unsigned int sec,
395 struct hash_speed *speed)
397 struct scatterlist sg[TVMEMSIZE];
398 struct crypto_hash *tfm;
399 struct hash_desc desc;
400 static char output[1024];
404 printk(KERN_INFO "\ntesting speed of %s\n", algo);
406 tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
409 printk(KERN_ERR "failed to load transform for %s: %ld\n", algo,
417 if (crypto_hash_digestsize(tfm) > sizeof(output)) {
418 printk(KERN_ERR "digestsize(%u) > outputbuffer(%zu)\n",
419 crypto_hash_digestsize(tfm), sizeof(output));
423 sg_init_table(sg, TVMEMSIZE);
424 for (i = 0; i < TVMEMSIZE; i++) {
425 sg_set_buf(sg + i, tvmem[i], PAGE_SIZE);
426 memset(tvmem[i], 0xff, PAGE_SIZE);
429 for (i = 0; speed[i].blen != 0; i++) {
430 if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
432 "template (%u) too big for tvmem (%lu)\n",
433 speed[i].blen, TVMEMSIZE * PAGE_SIZE);
437 printk(KERN_INFO "test%3u "
438 "(%5u byte blocks,%5u bytes per update,%4u updates): ",
439 i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
442 ret = test_hash_jiffies(&desc, sg, speed[i].blen,
443 speed[i].plen, output, sec);
445 ret = test_hash_cycles(&desc, sg, speed[i].blen,
446 speed[i].plen, output);
449 printk(KERN_ERR "hashing failed ret=%d\n", ret);
455 crypto_free_hash(tfm);
458 static void test_available(void)
463 printk("alg %s ", *name);
464 printk(crypto_has_alg(*name, 0, 0) ?
465 "found\n" : "not found\n");
470 static inline int tcrypt_test(const char *alg)
474 ret = alg_test(alg, alg, 0, 0);
475 /* non-fips algs return -EINVAL in fips mode */
476 if (fips_enabled && ret == -EINVAL)
481 static int do_test(int m)
488 for (i = 1; i < 200; i++)
493 ret += tcrypt_test("md5");
497 ret += tcrypt_test("sha1");
501 ret += tcrypt_test("ecb(des)");
502 ret += tcrypt_test("cbc(des)");
506 ret += tcrypt_test("ecb(des3_ede)");
507 ret += tcrypt_test("cbc(des3_ede)");
511 ret += tcrypt_test("md4");
515 ret += tcrypt_test("sha256");
519 ret += tcrypt_test("ecb(blowfish)");
520 ret += tcrypt_test("cbc(blowfish)");
524 ret += tcrypt_test("ecb(twofish)");
525 ret += tcrypt_test("cbc(twofish)");
529 ret += tcrypt_test("ecb(serpent)");
533 ret += tcrypt_test("ecb(aes)");
534 ret += tcrypt_test("cbc(aes)");
535 ret += tcrypt_test("lrw(aes)");
536 ret += tcrypt_test("xts(aes)");
537 ret += tcrypt_test("ctr(aes)");
538 ret += tcrypt_test("rfc3686(ctr(aes))");
542 ret += tcrypt_test("sha384");
546 ret += tcrypt_test("sha512");
550 ret += tcrypt_test("deflate");
554 ret += tcrypt_test("ecb(cast5)");
558 ret += tcrypt_test("ecb(cast6)");
562 ret += tcrypt_test("ecb(arc4)");
566 ret += tcrypt_test("michael_mic");
570 ret += tcrypt_test("crc32c");
574 ret += tcrypt_test("ecb(tea)");
578 ret += tcrypt_test("ecb(xtea)");
582 ret += tcrypt_test("ecb(khazad)");
586 ret += tcrypt_test("wp512");
590 ret += tcrypt_test("wp384");
594 ret += tcrypt_test("wp256");
598 ret += tcrypt_test("ecb(tnepres)");
602 ret += tcrypt_test("ecb(anubis)");
603 ret += tcrypt_test("cbc(anubis)");
607 ret += tcrypt_test("tgr192");
612 ret += tcrypt_test("tgr160");
616 ret += tcrypt_test("tgr128");
620 ret += tcrypt_test("ecb(xeta)");
624 ret += tcrypt_test("pcbc(fcrypt)");
628 ret += tcrypt_test("ecb(camellia)");
629 ret += tcrypt_test("cbc(camellia)");
632 ret += tcrypt_test("sha224");
636 ret += tcrypt_test("salsa20");
640 ret += tcrypt_test("gcm(aes)");
644 ret += tcrypt_test("lzo");
648 ret += tcrypt_test("ccm(aes)");
652 ret += tcrypt_test("cts(cbc(aes))");
656 ret += tcrypt_test("rmd128");
660 ret += tcrypt_test("rmd160");
664 ret += tcrypt_test("rmd256");
668 ret += tcrypt_test("rmd320");
672 ret += tcrypt_test("ecb(seed)");
676 ret += tcrypt_test("zlib");
680 ret += tcrypt_test("rfc4309(ccm(aes))");
684 ret += tcrypt_test("hmac(md5)");
688 ret += tcrypt_test("hmac(sha1)");
692 ret += tcrypt_test("hmac(sha256)");
696 ret += tcrypt_test("hmac(sha384)");
700 ret += tcrypt_test("hmac(sha512)");
704 ret += tcrypt_test("hmac(sha224)");
708 ret += tcrypt_test("xcbc(aes)");
712 ret += tcrypt_test("hmac(rmd128)");
716 ret += tcrypt_test("hmac(rmd160)");
720 ret += tcrypt_test("ansi_cprng");
724 test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
725 speed_template_16_24_32);
726 test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
727 speed_template_16_24_32);
728 test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
729 speed_template_16_24_32);
730 test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
731 speed_template_16_24_32);
732 test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
733 speed_template_32_40_48);
734 test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
735 speed_template_32_40_48);
736 test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
737 speed_template_32_48_64);
738 test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
739 speed_template_32_48_64);
743 test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
744 des3_speed_template, DES3_SPEED_VECTORS,
746 test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
747 des3_speed_template, DES3_SPEED_VECTORS,
749 test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
750 des3_speed_template, DES3_SPEED_VECTORS,
752 test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
753 des3_speed_template, DES3_SPEED_VECTORS,
758 test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
759 speed_template_16_24_32);
760 test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
761 speed_template_16_24_32);
762 test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
763 speed_template_16_24_32);
764 test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
765 speed_template_16_24_32);
769 test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
770 speed_template_8_32);
771 test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
772 speed_template_8_32);
773 test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
774 speed_template_8_32);
775 test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
776 speed_template_8_32);
780 test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
782 test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
784 test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
786 test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
791 test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
792 speed_template_16_24_32);
793 test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
794 speed_template_16_24_32);
795 test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
796 speed_template_16_24_32);
797 test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
798 speed_template_16_24_32);
802 test_cipher_speed("salsa20", ENCRYPT, sec, NULL, 0,
803 speed_template_16_32);
810 test_hash_speed("md4", sec, generic_hash_speed_template);
811 if (mode > 300 && mode < 400) break;
814 test_hash_speed("md5", sec, generic_hash_speed_template);
815 if (mode > 300 && mode < 400) break;
818 test_hash_speed("sha1", sec, generic_hash_speed_template);
819 if (mode > 300 && mode < 400) break;
822 test_hash_speed("sha256", sec, generic_hash_speed_template);
823 if (mode > 300 && mode < 400) break;
826 test_hash_speed("sha384", sec, generic_hash_speed_template);
827 if (mode > 300 && mode < 400) break;
830 test_hash_speed("sha512", sec, generic_hash_speed_template);
831 if (mode > 300 && mode < 400) break;
834 test_hash_speed("wp256", sec, generic_hash_speed_template);
835 if (mode > 300 && mode < 400) break;
838 test_hash_speed("wp384", sec, generic_hash_speed_template);
839 if (mode > 300 && mode < 400) break;
842 test_hash_speed("wp512", sec, generic_hash_speed_template);
843 if (mode > 300 && mode < 400) break;
846 test_hash_speed("tgr128", sec, generic_hash_speed_template);
847 if (mode > 300 && mode < 400) break;
850 test_hash_speed("tgr160", sec, generic_hash_speed_template);
851 if (mode > 300 && mode < 400) break;
854 test_hash_speed("tgr192", sec, generic_hash_speed_template);
855 if (mode > 300 && mode < 400) break;
858 test_hash_speed("sha224", sec, generic_hash_speed_template);
859 if (mode > 300 && mode < 400) break;
862 test_hash_speed("rmd128", sec, generic_hash_speed_template);
863 if (mode > 300 && mode < 400) break;
866 test_hash_speed("rmd160", sec, generic_hash_speed_template);
867 if (mode > 300 && mode < 400) break;
870 test_hash_speed("rmd256", sec, generic_hash_speed_template);
871 if (mode > 300 && mode < 400) break;
874 test_hash_speed("rmd320", sec, generic_hash_speed_template);
875 if (mode > 300 && mode < 400) break;
888 static int __init tcrypt_mod_init(void)
893 for (i = 0; i < TVMEMSIZE; i++) {
894 tvmem[i] = (void *)__get_free_page(GFP_KERNEL);
901 printk(KERN_ERR "tcrypt: one or more tests failed!\n");
905 /* We intentionaly return -EAGAIN to prevent keeping the module,
906 * unless we're running in fips mode. It does all its work from
907 * init() and doesn't offer any runtime functionality, but in
908 * the fips case, checking for a successful load is helpful.
909 * => we don't need it in the memory, do we?
916 for (i = 0; i < TVMEMSIZE && tvmem[i]; i++)
917 free_page((unsigned long)tvmem[i]);
923 * If an init function is provided, an exit function must also be provided
924 * to allow module unload.
926 static void __exit tcrypt_mod_fini(void) { }
928 module_init(tcrypt_mod_init);
929 module_exit(tcrypt_mod_fini);
931 module_param(mode, int, 0);
932 module_param(sec, uint, 0);
933 MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
934 "(defaults to zero which uses CPU cycles instead)");
936 MODULE_LICENSE("GPL");
937 MODULE_DESCRIPTION("Quick & dirty crypto testing module");
938 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");