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>
32 * Need slab memory for testing (size in number of pages).
37 * Used by test_cipher_speed()
43 * Used by test_cipher_speed()
45 static unsigned int sec
;
48 static char *tvmem
[TVMEMSIZE
];
50 static char *check
[] = {
51 "des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256",
52 "blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
53 "cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
54 "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", "fcrypt",
55 "camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
56 "lzo", "cts", "zlib", NULL
59 static int test_cipher_jiffies(struct blkcipher_desc
*desc
, int enc
,
60 struct scatterlist
*sg
, int blen
, int sec
)
62 unsigned long start
, end
;
66 for (start
= jiffies
, end
= start
+ sec
* HZ
, bcount
= 0;
67 time_before(jiffies
, end
); bcount
++) {
69 ret
= crypto_blkcipher_encrypt(desc
, sg
, sg
, blen
);
71 ret
= crypto_blkcipher_decrypt(desc
, sg
, sg
, blen
);
77 printk("%d operations in %d seconds (%ld bytes)\n",
78 bcount
, sec
, (long)bcount
* blen
);
82 static int test_cipher_cycles(struct blkcipher_desc
*desc
, int enc
,
83 struct scatterlist
*sg
, int blen
)
85 unsigned long cycles
= 0;
93 for (i
= 0; i
< 4; i
++) {
95 ret
= crypto_blkcipher_encrypt(desc
, sg
, sg
, blen
);
97 ret
= crypto_blkcipher_decrypt(desc
, sg
, sg
, blen
);
103 /* The real thing. */
104 for (i
= 0; i
< 8; i
++) {
107 start
= get_cycles();
109 ret
= crypto_blkcipher_encrypt(desc
, sg
, sg
, blen
);
111 ret
= crypto_blkcipher_decrypt(desc
, sg
, sg
, blen
);
117 cycles
+= end
- start
;
125 printk("1 operation in %lu cycles (%d bytes)\n",
126 (cycles
+ 4) / 8, blen
);
131 static u32 block_sizes
[] = { 16, 64, 256, 1024, 8192, 0 };
133 static void test_cipher_speed(const char *algo
, int enc
, unsigned int sec
,
134 struct cipher_speed_template
*template,
135 unsigned int tcount
, u8
*keysize
)
137 unsigned int ret
, i
, j
, iv_len
;
138 const char *key
, iv
[128];
139 struct crypto_blkcipher
*tfm
;
140 struct blkcipher_desc desc
;
149 printk("\ntesting speed of %s %s\n", algo
, e
);
151 tfm
= crypto_alloc_blkcipher(algo
, 0, CRYPTO_ALG_ASYNC
);
154 printk("failed to load transform for %s: %ld\n", algo
,
164 b_size
= block_sizes
;
166 struct scatterlist sg
[TVMEMSIZE
];
168 if ((*keysize
+ *b_size
) > TVMEMSIZE
* PAGE_SIZE
) {
169 printk("template (%u) too big for "
170 "tvmem (%lu)\n", *keysize
+ *b_size
,
171 TVMEMSIZE
* PAGE_SIZE
);
175 printk("test %u (%d bit key, %d byte blocks): ", i
,
176 *keysize
* 8, *b_size
);
178 memset(tvmem
[0], 0xff, PAGE_SIZE
);
180 /* set key, plain text and IV */
182 for (j
= 0; j
< tcount
; j
++) {
183 if (template[j
].klen
== *keysize
) {
184 key
= template[j
].key
;
189 ret
= crypto_blkcipher_setkey(tfm
, key
, *keysize
);
191 printk("setkey() failed flags=%x\n",
192 crypto_blkcipher_get_flags(tfm
));
196 sg_init_table(sg
, TVMEMSIZE
);
197 sg_set_buf(sg
, tvmem
[0] + *keysize
,
198 PAGE_SIZE
- *keysize
);
199 for (j
= 1; j
< TVMEMSIZE
; j
++) {
200 sg_set_buf(sg
+ j
, tvmem
[j
], PAGE_SIZE
);
201 memset (tvmem
[j
], 0xff, PAGE_SIZE
);
204 iv_len
= crypto_blkcipher_ivsize(tfm
);
206 memset(&iv
, 0xff, iv_len
);
207 crypto_blkcipher_set_iv(tfm
, iv
, iv_len
);
211 ret
= test_cipher_jiffies(&desc
, enc
, sg
,
214 ret
= test_cipher_cycles(&desc
, enc
, sg
,
218 printk("%s() failed flags=%x\n", e
, desc
.flags
);
228 crypto_free_blkcipher(tfm
);
231 static int test_hash_jiffies_digest(struct hash_desc
*desc
,
232 struct scatterlist
*sg
, int blen
,
235 unsigned long start
, end
;
239 for (start
= jiffies
, end
= start
+ sec
* HZ
, bcount
= 0;
240 time_before(jiffies
, end
); bcount
++) {
241 ret
= crypto_hash_digest(desc
, sg
, blen
, out
);
246 printk("%6u opers/sec, %9lu bytes/sec\n",
247 bcount
/ sec
, ((long)bcount
* blen
) / sec
);
252 static int test_hash_jiffies(struct hash_desc
*desc
, struct scatterlist
*sg
,
253 int blen
, int plen
, char *out
, int sec
)
255 unsigned long start
, end
;
260 return test_hash_jiffies_digest(desc
, sg
, blen
, out
, sec
);
262 for (start
= jiffies
, end
= start
+ sec
* HZ
, bcount
= 0;
263 time_before(jiffies
, end
); bcount
++) {
264 ret
= crypto_hash_init(desc
);
267 for (pcount
= 0; pcount
< blen
; pcount
+= plen
) {
268 ret
= crypto_hash_update(desc
, sg
, plen
);
272 /* we assume there is enough space in 'out' for the result */
273 ret
= crypto_hash_final(desc
, out
);
278 printk("%6u opers/sec, %9lu bytes/sec\n",
279 bcount
/ sec
, ((long)bcount
* blen
) / sec
);
284 static int test_hash_cycles_digest(struct hash_desc
*desc
,
285 struct scatterlist
*sg
, int blen
, char *out
)
287 unsigned long cycles
= 0;
295 for (i
= 0; i
< 4; i
++) {
296 ret
= crypto_hash_digest(desc
, sg
, blen
, out
);
301 /* The real thing. */
302 for (i
= 0; i
< 8; i
++) {
305 start
= get_cycles();
307 ret
= crypto_hash_digest(desc
, sg
, blen
, out
);
313 cycles
+= end
- start
;
323 printk("%6lu cycles/operation, %4lu cycles/byte\n",
324 cycles
/ 8, cycles
/ (8 * blen
));
329 static int test_hash_cycles(struct hash_desc
*desc
, struct scatterlist
*sg
,
330 int blen
, int plen
, char *out
)
332 unsigned long cycles
= 0;
337 return test_hash_cycles_digest(desc
, sg
, blen
, out
);
343 for (i
= 0; i
< 4; i
++) {
344 ret
= crypto_hash_init(desc
);
347 for (pcount
= 0; pcount
< blen
; pcount
+= plen
) {
348 ret
= crypto_hash_update(desc
, sg
, plen
);
352 ret
= crypto_hash_final(desc
, out
);
357 /* The real thing. */
358 for (i
= 0; i
< 8; i
++) {
361 start
= get_cycles();
363 ret
= crypto_hash_init(desc
);
366 for (pcount
= 0; pcount
< blen
; pcount
+= plen
) {
367 ret
= crypto_hash_update(desc
, sg
, plen
);
371 ret
= crypto_hash_final(desc
, out
);
377 cycles
+= end
- start
;
387 printk("%6lu cycles/operation, %4lu cycles/byte\n",
388 cycles
/ 8, cycles
/ (8 * blen
));
393 static void test_hash_speed(const char *algo
, unsigned int sec
,
394 struct hash_speed
*speed
)
396 struct scatterlist sg
[TVMEMSIZE
];
397 struct crypto_hash
*tfm
;
398 struct hash_desc desc
;
399 static char output
[1024];
403 printk(KERN_INFO
"\ntesting speed of %s\n", algo
);
405 tfm
= crypto_alloc_hash(algo
, 0, CRYPTO_ALG_ASYNC
);
408 printk(KERN_ERR
"failed to load transform for %s: %ld\n", algo
,
416 if (crypto_hash_digestsize(tfm
) > sizeof(output
)) {
417 printk(KERN_ERR
"digestsize(%u) > outputbuffer(%zu)\n",
418 crypto_hash_digestsize(tfm
), sizeof(output
));
422 sg_init_table(sg
, TVMEMSIZE
);
423 for (i
= 0; i
< TVMEMSIZE
; i
++) {
424 sg_set_buf(sg
+ i
, tvmem
[i
], PAGE_SIZE
);
425 memset(tvmem
[i
], 0xff, PAGE_SIZE
);
428 for (i
= 0; speed
[i
].blen
!= 0; i
++) {
429 if (speed
[i
].blen
> TVMEMSIZE
* PAGE_SIZE
) {
431 "template (%u) too big for tvmem (%lu)\n",
432 speed
[i
].blen
, TVMEMSIZE
* PAGE_SIZE
);
436 printk(KERN_INFO
"test%3u "
437 "(%5u byte blocks,%5u bytes per update,%4u updates): ",
438 i
, speed
[i
].blen
, speed
[i
].plen
, speed
[i
].blen
/ speed
[i
].plen
);
441 ret
= test_hash_jiffies(&desc
, sg
, speed
[i
].blen
,
442 speed
[i
].plen
, output
, sec
);
444 ret
= test_hash_cycles(&desc
, sg
, speed
[i
].blen
,
445 speed
[i
].plen
, output
);
448 printk(KERN_ERR
"hashing failed ret=%d\n", ret
);
454 crypto_free_hash(tfm
);
457 static void test_available(void)
462 printk("alg %s ", *name
);
463 printk(crypto_has_alg(*name
, 0, 0) ?
464 "found\n" : "not found\n");
469 static inline int tcrypt_test(const char *alg
)
471 return alg_test(alg
, alg
, 0, 0);
474 static void do_test(int m
)
480 for (i
= 1; i
< 200; i
++)
493 tcrypt_test("ecb(des)");
494 tcrypt_test("cbc(des)");
498 tcrypt_test("ecb(des3_ede)");
499 tcrypt_test("cbc(des3_ede)");
507 tcrypt_test("sha256");
511 tcrypt_test("ecb(blowfish)");
512 tcrypt_test("cbc(blowfish)");
516 tcrypt_test("ecb(twofish)");
517 tcrypt_test("cbc(twofish)");
521 tcrypt_test("ecb(serpent)");
525 tcrypt_test("ecb(aes)");
526 tcrypt_test("cbc(aes)");
527 tcrypt_test("lrw(aes)");
528 tcrypt_test("xts(aes)");
529 tcrypt_test("rfc3686(ctr(aes))");
533 tcrypt_test("sha384");
537 tcrypt_test("sha512");
541 tcrypt_test("deflate");
545 tcrypt_test("ecb(cast5)");
549 tcrypt_test("ecb(cast6)");
553 tcrypt_test("ecb(arc4)");
557 tcrypt_test("michael_mic");
561 tcrypt_test("crc32c");
565 tcrypt_test("ecb(tea)");
569 tcrypt_test("ecb(xtea)");
573 tcrypt_test("ecb(khazad)");
577 tcrypt_test("wp512");
581 tcrypt_test("wp384");
585 tcrypt_test("wp256");
589 tcrypt_test("ecb(tnepres)");
593 tcrypt_test("ecb(anubis)");
594 tcrypt_test("cbc(anubis)");
598 tcrypt_test("tgr192");
603 tcrypt_test("tgr160");
607 tcrypt_test("tgr128");
611 tcrypt_test("ecb(xeta)");
615 tcrypt_test("pcbc(fcrypt)");
619 tcrypt_test("ecb(camellia)");
620 tcrypt_test("cbc(camellia)");
623 tcrypt_test("sha224");
627 tcrypt_test("salsa20");
631 tcrypt_test("gcm(aes)");
639 tcrypt_test("ccm(aes)");
643 tcrypt_test("cts(cbc(aes))");
647 tcrypt_test("rmd128");
651 tcrypt_test("rmd160");
655 tcrypt_test("rmd256");
659 tcrypt_test("rmd320");
663 tcrypt_test("ecb(seed)");
671 tcrypt_test("rfc4309(ccm(aes))");
675 tcrypt_test("hmac(md5)");
679 tcrypt_test("hmac(sha1)");
683 tcrypt_test("hmac(sha256)");
687 tcrypt_test("hmac(sha384)");
691 tcrypt_test("hmac(sha512)");
695 tcrypt_test("hmac(sha224)");
699 tcrypt_test("xcbc(aes)");
703 tcrypt_test("hmac(rmd128)");
707 tcrypt_test("hmac(rmd160)");
711 tcrypt_test("ansi_cprng");
715 test_cipher_speed("ecb(aes)", ENCRYPT
, sec
, NULL
, 0,
716 speed_template_16_24_32
);
717 test_cipher_speed("ecb(aes)", DECRYPT
, sec
, NULL
, 0,
718 speed_template_16_24_32
);
719 test_cipher_speed("cbc(aes)", ENCRYPT
, sec
, NULL
, 0,
720 speed_template_16_24_32
);
721 test_cipher_speed("cbc(aes)", DECRYPT
, sec
, NULL
, 0,
722 speed_template_16_24_32
);
723 test_cipher_speed("lrw(aes)", ENCRYPT
, sec
, NULL
, 0,
724 speed_template_32_40_48
);
725 test_cipher_speed("lrw(aes)", DECRYPT
, sec
, NULL
, 0,
726 speed_template_32_40_48
);
727 test_cipher_speed("xts(aes)", ENCRYPT
, sec
, NULL
, 0,
728 speed_template_32_48_64
);
729 test_cipher_speed("xts(aes)", DECRYPT
, sec
, NULL
, 0,
730 speed_template_32_48_64
);
734 test_cipher_speed("ecb(des3_ede)", ENCRYPT
, sec
,
735 des3_speed_template
, DES3_SPEED_VECTORS
,
737 test_cipher_speed("ecb(des3_ede)", DECRYPT
, sec
,
738 des3_speed_template
, DES3_SPEED_VECTORS
,
740 test_cipher_speed("cbc(des3_ede)", ENCRYPT
, sec
,
741 des3_speed_template
, DES3_SPEED_VECTORS
,
743 test_cipher_speed("cbc(des3_ede)", DECRYPT
, sec
,
744 des3_speed_template
, DES3_SPEED_VECTORS
,
749 test_cipher_speed("ecb(twofish)", ENCRYPT
, sec
, NULL
, 0,
750 speed_template_16_24_32
);
751 test_cipher_speed("ecb(twofish)", DECRYPT
, sec
, NULL
, 0,
752 speed_template_16_24_32
);
753 test_cipher_speed("cbc(twofish)", ENCRYPT
, sec
, NULL
, 0,
754 speed_template_16_24_32
);
755 test_cipher_speed("cbc(twofish)", DECRYPT
, sec
, NULL
, 0,
756 speed_template_16_24_32
);
760 test_cipher_speed("ecb(blowfish)", ENCRYPT
, sec
, NULL
, 0,
761 speed_template_8_32
);
762 test_cipher_speed("ecb(blowfish)", DECRYPT
, sec
, NULL
, 0,
763 speed_template_8_32
);
764 test_cipher_speed("cbc(blowfish)", ENCRYPT
, sec
, NULL
, 0,
765 speed_template_8_32
);
766 test_cipher_speed("cbc(blowfish)", DECRYPT
, sec
, NULL
, 0,
767 speed_template_8_32
);
771 test_cipher_speed("ecb(des)", ENCRYPT
, sec
, NULL
, 0,
773 test_cipher_speed("ecb(des)", DECRYPT
, sec
, NULL
, 0,
775 test_cipher_speed("cbc(des)", ENCRYPT
, sec
, NULL
, 0,
777 test_cipher_speed("cbc(des)", DECRYPT
, sec
, NULL
, 0,
782 test_cipher_speed("ecb(camellia)", ENCRYPT
, sec
, NULL
, 0,
783 speed_template_16_24_32
);
784 test_cipher_speed("ecb(camellia)", DECRYPT
, sec
, NULL
, 0,
785 speed_template_16_24_32
);
786 test_cipher_speed("cbc(camellia)", ENCRYPT
, sec
, NULL
, 0,
787 speed_template_16_24_32
);
788 test_cipher_speed("cbc(camellia)", DECRYPT
, sec
, NULL
, 0,
789 speed_template_16_24_32
);
793 test_cipher_speed("salsa20", ENCRYPT
, sec
, NULL
, 0,
794 speed_template_16_32
);
801 test_hash_speed("md4", sec
, generic_hash_speed_template
);
802 if (mode
> 300 && mode
< 400) break;
805 test_hash_speed("md5", sec
, generic_hash_speed_template
);
806 if (mode
> 300 && mode
< 400) break;
809 test_hash_speed("sha1", sec
, generic_hash_speed_template
);
810 if (mode
> 300 && mode
< 400) break;
813 test_hash_speed("sha256", sec
, generic_hash_speed_template
);
814 if (mode
> 300 && mode
< 400) break;
817 test_hash_speed("sha384", sec
, generic_hash_speed_template
);
818 if (mode
> 300 && mode
< 400) break;
821 test_hash_speed("sha512", sec
, generic_hash_speed_template
);
822 if (mode
> 300 && mode
< 400) break;
825 test_hash_speed("wp256", sec
, generic_hash_speed_template
);
826 if (mode
> 300 && mode
< 400) break;
829 test_hash_speed("wp384", sec
, generic_hash_speed_template
);
830 if (mode
> 300 && mode
< 400) break;
833 test_hash_speed("wp512", sec
, generic_hash_speed_template
);
834 if (mode
> 300 && mode
< 400) break;
837 test_hash_speed("tgr128", sec
, generic_hash_speed_template
);
838 if (mode
> 300 && mode
< 400) break;
841 test_hash_speed("tgr160", sec
, generic_hash_speed_template
);
842 if (mode
> 300 && mode
< 400) break;
845 test_hash_speed("tgr192", sec
, generic_hash_speed_template
);
846 if (mode
> 300 && mode
< 400) break;
849 test_hash_speed("sha224", sec
, generic_hash_speed_template
);
850 if (mode
> 300 && mode
< 400) break;
853 test_hash_speed("rmd128", sec
, generic_hash_speed_template
);
854 if (mode
> 300 && mode
< 400) break;
857 test_hash_speed("rmd160", sec
, generic_hash_speed_template
);
858 if (mode
> 300 && mode
< 400) break;
861 test_hash_speed("rmd256", sec
, generic_hash_speed_template
);
862 if (mode
> 300 && mode
< 400) break;
865 test_hash_speed("rmd320", sec
, generic_hash_speed_template
);
866 if (mode
> 300 && mode
< 400) break;
877 static int __init
tcrypt_mod_init(void)
882 for (i
= 0; i
< TVMEMSIZE
; i
++) {
883 tvmem
[i
] = (void *)__get_free_page(GFP_KERNEL
);
890 /* We intentionaly return -EAGAIN to prevent keeping
891 * the module. It does all its work from init()
892 * and doesn't offer any runtime functionality
893 * => we don't need it in the memory, do we?
899 for (i
= 0; i
< TVMEMSIZE
&& tvmem
[i
]; i
++)
900 free_page((unsigned long)tvmem
[i
]);
906 * If an init function is provided, an exit function must also be provided
907 * to allow module unload.
909 static void __exit
tcrypt_mod_fini(void) { }
911 module_init(tcrypt_mod_init
);
912 module_exit(tcrypt_mod_fini
);
914 module_param(mode
, int, 0);
915 module_param(sec
, uint
, 0);
916 MODULE_PARM_DESC(sec
, "Length in seconds of speed tests "
917 "(defaults to zero which uses CPU cycles instead)");
919 MODULE_LICENSE("GPL");
920 MODULE_DESCRIPTION("Quick & dirty crypto testing module");
921 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");