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>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
16 * Rewritten by Kartikey Mahendra Bhatt
19 #include <linux/init.h>
20 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <asm/scatterlist.h>
24 #include <linux/string.h>
25 #include <linux/crypto.h>
26 #include <linux/highmem.h>
30 * Need to kmalloc() memory for testing kmap().
32 #define TVMEMSIZE 4096
33 #define XBUFSIZE 32768
36 * Indexes into the xbuf to simulate cross-page access.
48 * Used by test_cipher()
55 static unsigned int IDX
[8] = { IDX1
, IDX2
, IDX3
, IDX4
, IDX5
, IDX6
, IDX7
, IDX8
};
61 static char *check
[] = {
62 "des", "md5", "des3_ede", "rot13", "sha1", "sha256", "blowfish",
63 "twofish", "serpent", "sha384", "sha512", "md4", "aes", "cast6",
64 "arc4", "michael_mic", "deflate", NULL
68 hexdump(unsigned char *buf
, unsigned int len
)
71 printk("%02x", *buf
++);
77 test_hash (char * algo
, struct hash_testvec
* template, unsigned int tcount
)
80 unsigned int i
, j
, k
, temp
;
81 struct scatterlist sg
[8];
83 struct crypto_tfm
*tfm
;
84 struct hash_testvec
*hash_tv
;
87 printk("\ntesting %s\n", algo
);
89 tsize
= sizeof (struct hash_testvec
);
92 if (tsize
> TVMEMSIZE
) {
93 printk("template (%u) too big for tvmem (%u)\n", tsize
, TVMEMSIZE
);
97 memcpy(tvmem
, template, tsize
);
98 hash_tv
= (void *) tvmem
;
99 tfm
= crypto_alloc_tfm(algo
, 0);
101 printk("failed to load transform for %s\n", algo
);
105 for (i
= 0; i
< tcount
; i
++) {
106 printk ("test %u:\n", i
+ 1);
107 memset (result
, 0, 64);
109 p
= hash_tv
[i
].plaintext
;
110 sg
[0].page
= virt_to_page (p
);
111 sg
[0].offset
= offset_in_page (p
);
112 sg
[0].length
= hash_tv
[i
].psize
;
114 crypto_digest_init (tfm
);
115 if (tfm
->crt_u
.digest
.dit_setkey
) {
116 crypto_digest_setkey (tfm
, hash_tv
[i
].key
,
119 crypto_digest_update (tfm
, sg
, 1);
120 crypto_digest_final (tfm
, result
);
122 hexdump (result
, crypto_tfm_alg_digestsize (tfm
));
124 memcmp(result
, hash_tv
[i
].digest
,
125 crypto_tfm_alg_digestsize(tfm
)) ? "fail" :
129 printk ("testing %s across pages\n", algo
);
131 /* setup the dummy buffer first */
132 memset(xbuf
, 0, XBUFSIZE
);
135 for (i
= 0; i
< tcount
; i
++) {
138 printk ("test %u:\n", j
);
139 memset (result
, 0, 64);
142 for (k
= 0; k
< hash_tv
[i
].np
; k
++) {
143 memcpy (&xbuf
[IDX
[k
]], hash_tv
[i
].plaintext
+ temp
,
145 temp
+= hash_tv
[i
].tap
[k
];
147 sg
[k
].page
= virt_to_page (p
);
148 sg
[k
].offset
= offset_in_page (p
);
149 sg
[k
].length
= hash_tv
[i
].tap
[k
];
152 crypto_digest_digest (tfm
, sg
, hash_tv
[i
].np
, result
);
154 hexdump (result
, crypto_tfm_alg_digestsize (tfm
));
156 memcmp(result
, hash_tv
[i
].digest
,
157 crypto_tfm_alg_digestsize(tfm
)) ? "fail" :
162 crypto_free_tfm (tfm
);
166 #ifdef CONFIG_CRYPTO_HMAC
169 test_hmac(char *algo
, struct hmac_testvec
* template, unsigned int tcount
)
172 unsigned int i
, j
, k
, temp
;
173 struct scatterlist sg
[8];
175 struct crypto_tfm
*tfm
;
176 struct hmac_testvec
*hmac_tv
;
177 unsigned int tsize
, klen
;
179 tfm
= crypto_alloc_tfm(algo
, 0);
181 printk("failed to load transform for %s\n", algo
);
185 printk("\ntesting hmac_%s\n", algo
);
187 tsize
= sizeof (struct hmac_testvec
);
189 if (tsize
> TVMEMSIZE
) {
190 printk("template (%u) too big for tvmem (%u)\n", tsize
,
195 memcpy(tvmem
, template, tsize
);
196 hmac_tv
= (void *) tvmem
;
198 for (i
= 0; i
< tcount
; i
++) {
199 printk("test %u:\n", i
+ 1);
200 memset(result
, 0, sizeof (result
));
202 p
= hmac_tv
[i
].plaintext
;
203 klen
= hmac_tv
[i
].ksize
;
204 sg
[0].page
= virt_to_page(p
);
205 sg
[0].offset
= offset_in_page(p
);
206 sg
[0].length
= hmac_tv
[i
].psize
;
208 crypto_hmac(tfm
, hmac_tv
[i
].key
, &klen
, sg
, 1, result
);
210 hexdump(result
, crypto_tfm_alg_digestsize(tfm
));
212 memcmp(result
, hmac_tv
[i
].digest
,
213 crypto_tfm_alg_digestsize(tfm
)) ? "fail" :
217 printk("\ntesting hmac_%s across pages\n", algo
);
219 memset(xbuf
, 0, XBUFSIZE
);
222 for (i
= 0; i
< tcount
; i
++) {
225 printk ("test %u:\n",j
);
226 memset (result
, 0, 64);
229 klen
= hmac_tv
[i
].ksize
;
230 for (k
= 0; k
< hmac_tv
[i
].np
; k
++) {
231 memcpy (&xbuf
[IDX
[k
]], hmac_tv
[i
].plaintext
+ temp
,
233 temp
+= hmac_tv
[i
].tap
[k
];
235 sg
[k
].page
= virt_to_page (p
);
236 sg
[k
].offset
= offset_in_page (p
);
237 sg
[k
].length
= hmac_tv
[i
].tap
[k
];
240 crypto_hmac(tfm
, hmac_tv
[i
].key
, &klen
, sg
, hmac_tv
[i
].np
,
242 hexdump(result
, crypto_tfm_alg_digestsize(tfm
));
245 memcmp(result
, hmac_tv
[i
].digest
,
246 crypto_tfm_alg_digestsize(tfm
)) ? "fail" :
251 crypto_free_tfm(tfm
);
254 #endif /* CONFIG_CRYPTO_HMAC */
257 test_cipher(char * algo
, int mode
, int enc
, struct cipher_testvec
* template, unsigned int tcount
)
259 unsigned int ret
, i
, j
, k
, temp
;
262 struct crypto_tfm
*tfm
;
264 struct cipher_testvec
*cipher_tv
;
265 struct scatterlist sg
[8];
269 strncpy(e
, "encryption", 11);
271 strncpy(e
, "decryption", 11);
272 if (mode
== MODE_ECB
)
273 strncpy(m
, "ECB", 4);
275 strncpy(m
, "CBC", 4);
277 printk("\ntesting %s %s %s \n", algo
, m
, e
);
279 tsize
= sizeof (struct cipher_testvec
);
282 if (tsize
> TVMEMSIZE
) {
283 printk("template (%u) too big for tvmem (%u)\n", tsize
,
288 memcpy(tvmem
, template, tsize
);
289 cipher_tv
= (void *) tvmem
;
292 tfm
= crypto_alloc_tfm (algo
, 0);
294 tfm
= crypto_alloc_tfm (algo
, CRYPTO_TFM_MODE_CBC
);
297 printk("failed to load transform for %s %s\n", algo
, m
);
302 for (i
= 0; i
< tcount
; i
++) {
303 if (!(cipher_tv
[i
].np
)) {
305 printk("test %u (%d bit key):\n",
306 j
, cipher_tv
[i
].klen
* 8);
310 tfm
->crt_flags
|= CRYPTO_TFM_REQ_WEAK_KEY
;
311 key
= cipher_tv
[i
].key
;
313 ret
= crypto_cipher_setkey(tfm
, key
, cipher_tv
[i
].klen
);
315 printk("setkey() failed flags=%x\n", tfm
->crt_flags
);
317 if (!cipher_tv
[i
].fail
)
321 p
= cipher_tv
[i
].input
;
322 sg
[0].page
= virt_to_page(p
);
323 sg
[0].offset
= offset_in_page(p
);
324 sg
[0].length
= cipher_tv
[i
].ilen
;
327 crypto_cipher_set_iv(tfm
, cipher_tv
[i
].iv
,
328 crypto_tfm_alg_ivsize (tfm
));
332 ret
= crypto_cipher_encrypt(tfm
, sg
, sg
, cipher_tv
[i
].ilen
);
334 ret
= crypto_cipher_decrypt(tfm
, sg
, sg
, cipher_tv
[i
].ilen
);
338 printk("%s () failed flags=%x\n", e
, tfm
->crt_flags
);
342 q
= kmap(sg
[0].page
) + sg
[0].offset
;
343 hexdump(q
, cipher_tv
[i
].rlen
);
346 memcmp(q
, cipher_tv
[i
].result
, cipher_tv
[i
].rlen
) ? "fail" :
351 printk("\ntesting %s %s %s across pages (chunking) \n", algo
, m
, e
);
352 memset(xbuf
, 0, XBUFSIZE
);
355 for (i
= 0; i
< tcount
; i
++) {
356 if (cipher_tv
[i
].np
) {
358 printk("test %u (%d bit key):\n",
359 j
, cipher_tv
[i
].klen
* 8);
363 tfm
->crt_flags
|= CRYPTO_TFM_REQ_WEAK_KEY
;
364 key
= cipher_tv
[i
].key
;
366 ret
= crypto_cipher_setkey(tfm
, key
, cipher_tv
[i
].klen
);
368 printk("setkey() failed flags=%x\n", tfm
->crt_flags
);
370 if (!cipher_tv
[i
].fail
)
375 for (k
= 0; k
< cipher_tv
[i
].np
; k
++) {
376 memcpy (&xbuf
[IDX
[k
]], cipher_tv
[i
].input
+ temp
,
377 cipher_tv
[i
].tap
[k
]);
378 temp
+= cipher_tv
[i
].tap
[k
];
380 sg
[k
].page
= virt_to_page (p
);
381 sg
[k
].offset
= offset_in_page (p
);
382 sg
[k
].length
= cipher_tv
[i
].tap
[k
];
386 crypto_cipher_set_iv(tfm
, cipher_tv
[i
].iv
,
387 crypto_tfm_alg_ivsize (tfm
));
391 ret
= crypto_cipher_encrypt(tfm
, sg
, sg
, cipher_tv
[i
].ilen
);
393 ret
= crypto_cipher_decrypt(tfm
, sg
, sg
, cipher_tv
[i
].ilen
);
396 printk("%s () failed flags=%x\n", e
, tfm
->crt_flags
);
401 for (k
= 0; k
< cipher_tv
[i
].np
; k
++) {
402 printk("page %u\n", k
);
403 q
= kmap(sg
[k
].page
) + sg
[k
].offset
;
404 hexdump(q
, cipher_tv
[i
].tap
[k
]);
406 memcmp(q
, cipher_tv
[i
].result
+ temp
,
407 cipher_tv
[i
].tap
[k
]) ? "fail" :
409 temp
+= cipher_tv
[i
].tap
[k
];
415 crypto_free_tfm(tfm
);
422 char result
[COMP_BUF_SIZE
];
423 struct crypto_tfm
*tfm
;
424 struct comp_testvec
*tv
;
427 printk("\ntesting deflate compression\n");
429 tsize
= sizeof (deflate_comp_tv_template
);
430 if (tsize
> TVMEMSIZE
) {
431 printk("template (%u) too big for tvmem (%u)\n", tsize
,
436 memcpy(tvmem
, deflate_comp_tv_template
, tsize
);
439 tfm
= crypto_alloc_tfm("deflate", 0);
441 printk("failed to load transform for deflate\n");
445 for (i
= 0; i
< DEFLATE_COMP_TEST_VECTORS
; i
++) {
446 int ilen
, ret
, dlen
= COMP_BUF_SIZE
;
448 printk("test %u:\n", i
+ 1);
449 memset(result
, 0, sizeof (result
));
452 ret
= crypto_comp_compress(tfm
, tv
[i
].input
,
453 ilen
, result
, &dlen
);
455 printk("fail: ret=%d\n", ret
);
458 hexdump(result
, dlen
);
459 printk("%s (ratio %d:%d)\n",
460 memcmp(result
, tv
[i
].output
, dlen
) ? "fail" : "pass",
464 printk("\ntesting deflate decompression\n");
466 tsize
= sizeof (deflate_decomp_tv_template
);
467 if (tsize
> TVMEMSIZE
) {
468 printk("template (%u) too big for tvmem (%u)\n", tsize
,
473 memcpy(tvmem
, deflate_decomp_tv_template
, tsize
);
476 for (i
= 0; i
< DEFLATE_DECOMP_TEST_VECTORS
; i
++) {
477 int ilen
, ret
, dlen
= COMP_BUF_SIZE
;
479 printk("test %u:\n", i
+ 1);
480 memset(result
, 0, sizeof (result
));
483 ret
= crypto_comp_decompress(tfm
, tv
[i
].input
,
484 ilen
, result
, &dlen
);
486 printk("fail: ret=%d\n", ret
);
489 hexdump(result
, dlen
);
490 printk("%s (ratio %d:%d)\n",
491 memcmp(result
, tv
[i
].output
, dlen
) ? "fail" : "pass",
495 crypto_free_tfm(tfm
);
504 printk("alg %s ", *name
);
505 printk((crypto_alg_available(*name
, 0)) ?
506 "found\n" : "not found\n");
517 test_hash("md5", md5_tv_template
, MD5_TEST_VECTORS
);
519 test_hash("sha1", sha1_tv_template
, SHA1_TEST_VECTORS
);
522 test_cipher ("des", MODE_ECB
, ENCRYPT
, des_enc_tv_template
, DES_ENC_TEST_VECTORS
);
523 test_cipher ("des", MODE_ECB
, DECRYPT
, des_dec_tv_template
, DES_DEC_TEST_VECTORS
);
524 test_cipher ("des", MODE_CBC
, ENCRYPT
, des_cbc_enc_tv_template
, DES_CBC_ENC_TEST_VECTORS
);
525 test_cipher ("des", MODE_CBC
, DECRYPT
, des_cbc_dec_tv_template
, DES_CBC_DEC_TEST_VECTORS
);
528 test_cipher ("des3_ede", MODE_ECB
, ENCRYPT
, des3_ede_enc_tv_template
, DES3_EDE_ENC_TEST_VECTORS
);
529 test_cipher ("des3_ede", MODE_ECB
, DECRYPT
, des3_ede_dec_tv_template
, DES3_EDE_DEC_TEST_VECTORS
);
531 test_hash("md4", md4_tv_template
, MD4_TEST_VECTORS
);
533 test_hash("sha256", sha256_tv_template
, SHA256_TEST_VECTORS
);
536 test_cipher ("blowfish", MODE_ECB
, ENCRYPT
, bf_enc_tv_template
, BF_ENC_TEST_VECTORS
);
537 test_cipher ("blowfish", MODE_ECB
, DECRYPT
, bf_dec_tv_template
, BF_DEC_TEST_VECTORS
);
538 test_cipher ("blowfish", MODE_CBC
, ENCRYPT
, bf_cbc_enc_tv_template
, BF_CBC_ENC_TEST_VECTORS
);
539 test_cipher ("blowfish", MODE_CBC
, DECRYPT
, bf_cbc_dec_tv_template
, BF_CBC_DEC_TEST_VECTORS
);
542 test_cipher ("twofish", MODE_ECB
, ENCRYPT
, tf_enc_tv_template
, TF_ENC_TEST_VECTORS
);
543 test_cipher ("twofish", MODE_ECB
, DECRYPT
, tf_dec_tv_template
, TF_DEC_TEST_VECTORS
);
544 test_cipher ("twofish", MODE_CBC
, ENCRYPT
, tf_cbc_enc_tv_template
, TF_CBC_ENC_TEST_VECTORS
);
545 test_cipher ("twofish", MODE_CBC
, DECRYPT
, tf_cbc_dec_tv_template
, TF_CBC_DEC_TEST_VECTORS
);
548 test_cipher ("serpent", MODE_ECB
, ENCRYPT
, serpent_enc_tv_template
, SERPENT_ENC_TEST_VECTORS
);
549 test_cipher ("serpent", MODE_ECB
, DECRYPT
, serpent_dec_tv_template
, SERPENT_DEC_TEST_VECTORS
);
552 test_cipher ("aes", MODE_ECB
, ENCRYPT
, aes_enc_tv_template
, AES_ENC_TEST_VECTORS
);
553 test_cipher ("aes", MODE_ECB
, DECRYPT
, aes_dec_tv_template
, AES_DEC_TEST_VECTORS
);
556 test_cipher ("cast5", MODE_ECB
, ENCRYPT
, cast5_enc_tv_template
, CAST5_ENC_TEST_VECTORS
);
557 test_cipher ("cast5", MODE_ECB
, DECRYPT
, cast5_dec_tv_template
, CAST5_DEC_TEST_VECTORS
);
560 test_cipher ("cast6", MODE_ECB
, ENCRYPT
, cast6_enc_tv_template
, CAST6_ENC_TEST_VECTORS
);
561 test_cipher ("cast6", MODE_ECB
, DECRYPT
, cast6_dec_tv_template
, CAST6_DEC_TEST_VECTORS
);
564 test_cipher ("arc4", MODE_ECB
, ENCRYPT
, arc4_enc_tv_template
, ARC4_ENC_TEST_VECTORS
);
565 test_cipher ("arc4", MODE_ECB
, DECRYPT
, arc4_dec_tv_template
, ARC4_DEC_TEST_VECTORS
);
567 test_hash("sha384", sha384_tv_template
, SHA384_TEST_VECTORS
);
568 test_hash("sha512", sha512_tv_template
, SHA512_TEST_VECTORS
);
570 #ifdef CONFIG_CRYPTO_HMAC
571 test_hmac("md5", hmac_md5_tv_template
, HMAC_MD5_TEST_VECTORS
);
572 test_hmac("sha1", hmac_sha1_tv_template
, HMAC_SHA1_TEST_VECTORS
);
573 test_hmac("sha256", hmac_sha256_tv_template
, HMAC_SHA256_TEST_VECTORS
);
576 test_hash("michael_mic", michael_mic_tv_template
, MICHAEL_MIC_TEST_VECTORS
);
580 test_hash("md5", md5_tv_template
, MD5_TEST_VECTORS
);
584 test_hash("sha1", sha1_tv_template
, SHA1_TEST_VECTORS
);
588 test_cipher ("des", MODE_ECB
, ENCRYPT
, des_enc_tv_template
, DES_ENC_TEST_VECTORS
);
589 test_cipher ("des", MODE_ECB
, DECRYPT
, des_dec_tv_template
, DES_DEC_TEST_VECTORS
);
590 test_cipher ("des", MODE_CBC
, ENCRYPT
, des_cbc_enc_tv_template
, DES_CBC_ENC_TEST_VECTORS
);
591 test_cipher ("des", MODE_CBC
, DECRYPT
, des_cbc_dec_tv_template
, DES_CBC_DEC_TEST_VECTORS
);
595 test_cipher ("des3_ede", MODE_ECB
, ENCRYPT
, des3_ede_enc_tv_template
, DES3_EDE_ENC_TEST_VECTORS
);
596 test_cipher ("des3_ede", MODE_ECB
, DECRYPT
, des3_ede_dec_tv_template
, DES3_EDE_DEC_TEST_VECTORS
);
600 test_hash("md4", md4_tv_template
, MD4_TEST_VECTORS
);
604 test_hash("sha256", sha256_tv_template
, SHA256_TEST_VECTORS
);
608 test_cipher ("blowfish", MODE_ECB
, ENCRYPT
, bf_enc_tv_template
, BF_ENC_TEST_VECTORS
);
609 test_cipher ("blowfish", MODE_ECB
, DECRYPT
, bf_dec_tv_template
, BF_DEC_TEST_VECTORS
);
610 test_cipher ("blowfish", MODE_CBC
, ENCRYPT
, bf_cbc_enc_tv_template
, BF_CBC_ENC_TEST_VECTORS
);
611 test_cipher ("blowfish", MODE_CBC
, DECRYPT
, bf_cbc_dec_tv_template
, BF_CBC_DEC_TEST_VECTORS
);
615 test_cipher ("twofish", MODE_ECB
, ENCRYPT
, tf_enc_tv_template
, TF_ENC_TEST_VECTORS
);
616 test_cipher ("twofish", MODE_ECB
, DECRYPT
, tf_dec_tv_template
, TF_DEC_TEST_VECTORS
);
617 test_cipher ("twofish", MODE_CBC
, ENCRYPT
, tf_cbc_enc_tv_template
, TF_CBC_ENC_TEST_VECTORS
);
618 test_cipher ("twofish", MODE_CBC
, DECRYPT
, tf_cbc_dec_tv_template
, TF_CBC_DEC_TEST_VECTORS
);
625 test_cipher ("aes", MODE_ECB
, ENCRYPT
, aes_enc_tv_template
, AES_ENC_TEST_VECTORS
);
626 test_cipher ("aes", MODE_ECB
, DECRYPT
, aes_dec_tv_template
, AES_DEC_TEST_VECTORS
);
630 test_hash("sha384", sha384_tv_template
, SHA384_TEST_VECTORS
);
634 test_hash("sha512", sha512_tv_template
, SHA512_TEST_VECTORS
);
642 test_cipher ("cast5", MODE_ECB
, ENCRYPT
, cast5_enc_tv_template
, CAST5_ENC_TEST_VECTORS
);
643 test_cipher ("cast5", MODE_ECB
, DECRYPT
, cast5_dec_tv_template
, CAST5_DEC_TEST_VECTORS
);
647 test_cipher ("cast6", MODE_ECB
, ENCRYPT
, cast6_enc_tv_template
, CAST6_ENC_TEST_VECTORS
);
648 test_cipher ("cast6", MODE_ECB
, DECRYPT
, cast6_dec_tv_template
, CAST6_DEC_TEST_VECTORS
);
652 test_cipher ("arc4", MODE_ECB
, ENCRYPT
, arc4_enc_tv_template
, ARC4_ENC_TEST_VECTORS
);
653 test_cipher ("arc4", MODE_ECB
, DECRYPT
, arc4_dec_tv_template
, ARC4_DEC_TEST_VECTORS
);
657 test_hash("michael_mic", michael_mic_tv_template
, MICHAEL_MIC_TEST_VECTORS
);
660 #ifdef CONFIG_CRYPTO_HMAC
662 test_hmac("md5", hmac_md5_tv_template
, HMAC_MD5_TEST_VECTORS
);
666 test_hmac("sha1", hmac_sha1_tv_template
, HMAC_SHA1_TEST_VECTORS
);
670 test_hmac("sha256", hmac_sha256_tv_template
, HMAC_SHA256_TEST_VECTORS
);
680 /* useful for debugging */
681 printk("not testing anything\n");
689 tvmem
= kmalloc(TVMEMSIZE
, GFP_KERNEL
);
693 xbuf
= kmalloc(XBUFSIZE
, GFP_KERNEL
);
707 * If an init function is provided, an exit function must also be provided
708 * to allow module unload.
710 static void __exit
fini(void) { }
715 MODULE_PARM(mode
, "i");
717 MODULE_LICENSE("GPL");
718 MODULE_DESCRIPTION("Quick & dirty crypto testing module");
719 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");