]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - crypto/tcrypt.c
[CRYPTO] ctr: Add countersize
[mirror_ubuntu-artful-kernel.git] / crypto / tcrypt.c
1 /*
2 * Quick & dirty crypto testing module.
3 *
4 * This will only exist until we have a better testing mechanism
5 * (e.g. a char device).
6 *
7 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
8 * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
9 *
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)
13 * any later version.
14 *
15 * 2006-12-07 Added SHA384 HMAC and SHA512 HMAC tests
16 * 2004-08-09 Added cipher speed tests (Reyk Floeter <reyk@vantronix.net>)
17 * 2003-09-14 Rewritten by Kartikey Mahendra Bhatt
18 *
19 */
20
21 #include <linux/err.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/mm.h>
25 #include <linux/slab.h>
26 #include <linux/scatterlist.h>
27 #include <linux/string.h>
28 #include <linux/crypto.h>
29 #include <linux/highmem.h>
30 #include <linux/moduleparam.h>
31 #include <linux/jiffies.h>
32 #include <linux/timex.h>
33 #include <linux/interrupt.h>
34 #include "tcrypt.h"
35
36 /*
37 * Need to kmalloc() memory for testing kmap().
38 */
39 #define TVMEMSIZE 16384
40 #define XBUFSIZE 32768
41
42 /*
43 * Indexes into the xbuf to simulate cross-page access.
44 */
45 #define IDX1 37
46 #define IDX2 32400
47 #define IDX3 1
48 #define IDX4 8193
49 #define IDX5 22222
50 #define IDX6 17101
51 #define IDX7 27333
52 #define IDX8 3000
53
54 /*
55 * Used by test_cipher()
56 */
57 #define ENCRYPT 1
58 #define DECRYPT 0
59
60 struct tcrypt_result {
61 struct completion completion;
62 int err;
63 };
64
65 static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
66
67 /*
68 * Used by test_cipher_speed()
69 */
70 static unsigned int sec;
71
72 static int mode;
73 static char *xbuf;
74 static char *tvmem;
75
76 static char *check[] = {
77 "des", "md5", "des3_ede", "rot13", "sha1", "sha256", "blowfish",
78 "twofish", "serpent", "sha384", "sha512", "md4", "aes", "cast6",
79 "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
80 "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", "fcrypt",
81 "camellia", "seed", NULL
82 };
83
84 static void hexdump(unsigned char *buf, unsigned int len)
85 {
86 while (len--)
87 printk("%02x", *buf++);
88
89 printk("\n");
90 }
91
92 static void tcrypt_complete(struct crypto_async_request *req, int err)
93 {
94 struct tcrypt_result *res = req->data;
95
96 if (err == -EINPROGRESS)
97 return;
98
99 res->err = err;
100 complete(&res->completion);
101 }
102
103 static void test_hash(char *algo, struct hash_testvec *template,
104 unsigned int tcount)
105 {
106 unsigned int i, j, k, temp;
107 struct scatterlist sg[8];
108 char result[64];
109 struct crypto_hash *tfm;
110 struct hash_desc desc;
111 struct hash_testvec *hash_tv;
112 unsigned int tsize;
113 int ret;
114
115 printk("\ntesting %s\n", algo);
116
117 tsize = sizeof(struct hash_testvec);
118 tsize *= tcount;
119
120 if (tsize > TVMEMSIZE) {
121 printk("template (%u) too big for tvmem (%u)\n", tsize, TVMEMSIZE);
122 return;
123 }
124
125 memcpy(tvmem, template, tsize);
126 hash_tv = (void *)tvmem;
127
128 tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
129 if (IS_ERR(tfm)) {
130 printk("failed to load transform for %s: %ld\n", algo,
131 PTR_ERR(tfm));
132 return;
133 }
134
135 desc.tfm = tfm;
136 desc.flags = 0;
137
138 for (i = 0; i < tcount; i++) {
139 printk("test %u:\n", i + 1);
140 memset(result, 0, 64);
141
142 sg_init_one(&sg[0], hash_tv[i].plaintext, hash_tv[i].psize);
143
144 if (hash_tv[i].ksize) {
145 ret = crypto_hash_setkey(tfm, hash_tv[i].key,
146 hash_tv[i].ksize);
147 if (ret) {
148 printk("setkey() failed ret=%d\n", ret);
149 goto out;
150 }
151 }
152
153 ret = crypto_hash_digest(&desc, sg, hash_tv[i].psize, result);
154 if (ret) {
155 printk("digest () failed ret=%d\n", ret);
156 goto out;
157 }
158
159 hexdump(result, crypto_hash_digestsize(tfm));
160 printk("%s\n",
161 memcmp(result, hash_tv[i].digest,
162 crypto_hash_digestsize(tfm)) ?
163 "fail" : "pass");
164 }
165
166 printk("testing %s across pages\n", algo);
167
168 /* setup the dummy buffer first */
169 memset(xbuf, 0, XBUFSIZE);
170
171 j = 0;
172 for (i = 0; i < tcount; i++) {
173 if (hash_tv[i].np) {
174 j++;
175 printk("test %u:\n", j);
176 memset(result, 0, 64);
177
178 temp = 0;
179 sg_init_table(sg, hash_tv[i].np);
180 for (k = 0; k < hash_tv[i].np; k++) {
181 memcpy(&xbuf[IDX[k]],
182 hash_tv[i].plaintext + temp,
183 hash_tv[i].tap[k]);
184 temp += hash_tv[i].tap[k];
185 sg_set_buf(&sg[k], &xbuf[IDX[k]],
186 hash_tv[i].tap[k]);
187 }
188
189 if (hash_tv[i].ksize) {
190 ret = crypto_hash_setkey(tfm, hash_tv[i].key,
191 hash_tv[i].ksize);
192
193 if (ret) {
194 printk("setkey() failed ret=%d\n", ret);
195 goto out;
196 }
197 }
198
199 ret = crypto_hash_digest(&desc, sg, hash_tv[i].psize,
200 result);
201 if (ret) {
202 printk("digest () failed ret=%d\n", ret);
203 goto out;
204 }
205
206 hexdump(result, crypto_hash_digestsize(tfm));
207 printk("%s\n",
208 memcmp(result, hash_tv[i].digest,
209 crypto_hash_digestsize(tfm)) ?
210 "fail" : "pass");
211 }
212 }
213
214 out:
215 crypto_free_hash(tfm);
216 }
217
218 static void test_cipher(char *algo, int enc,
219 struct cipher_testvec *template, unsigned int tcount)
220 {
221 unsigned int ret, i, j, k, temp;
222 unsigned int tsize;
223 char *q;
224 struct crypto_ablkcipher *tfm;
225 char *key;
226 struct cipher_testvec *cipher_tv;
227 struct ablkcipher_request *req;
228 struct scatterlist sg[8];
229 const char *e;
230 struct tcrypt_result result;
231
232 if (enc == ENCRYPT)
233 e = "encryption";
234 else
235 e = "decryption";
236
237 printk("\ntesting %s %s\n", algo, e);
238
239 tsize = sizeof (struct cipher_testvec);
240 tsize *= tcount;
241
242 if (tsize > TVMEMSIZE) {
243 printk("template (%u) too big for tvmem (%u)\n", tsize,
244 TVMEMSIZE);
245 return;
246 }
247
248 memcpy(tvmem, template, tsize);
249 cipher_tv = (void *)tvmem;
250
251 init_completion(&result.completion);
252
253 tfm = crypto_alloc_ablkcipher(algo, 0, 0);
254
255 if (IS_ERR(tfm)) {
256 printk("failed to load transform for %s: %ld\n", algo,
257 PTR_ERR(tfm));
258 return;
259 }
260
261 req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
262 if (!req) {
263 printk("failed to allocate request for %s\n", algo);
264 goto out;
265 }
266
267 ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
268 tcrypt_complete, &result);
269
270 j = 0;
271 for (i = 0; i < tcount; i++) {
272 if (!(cipher_tv[i].np)) {
273 j++;
274 printk("test %u (%d bit key):\n",
275 j, cipher_tv[i].klen * 8);
276
277 crypto_ablkcipher_clear_flags(tfm, ~0);
278 if (cipher_tv[i].wk)
279 crypto_ablkcipher_set_flags(
280 tfm, CRYPTO_TFM_REQ_WEAK_KEY);
281 key = cipher_tv[i].key;
282
283 ret = crypto_ablkcipher_setkey(tfm, key,
284 cipher_tv[i].klen);
285 if (ret) {
286 printk("setkey() failed flags=%x\n",
287 crypto_ablkcipher_get_flags(tfm));
288
289 if (!cipher_tv[i].fail)
290 goto out;
291 }
292
293 sg_init_one(&sg[0], cipher_tv[i].input,
294 cipher_tv[i].ilen);
295
296 ablkcipher_request_set_crypt(req, sg, sg,
297 cipher_tv[i].ilen,
298 cipher_tv[i].iv);
299
300 ret = enc ?
301 crypto_ablkcipher_encrypt(req) :
302 crypto_ablkcipher_decrypt(req);
303
304 switch (ret) {
305 case 0:
306 break;
307 case -EINPROGRESS:
308 case -EBUSY:
309 ret = wait_for_completion_interruptible(
310 &result.completion);
311 if (!ret && !((ret = result.err))) {
312 INIT_COMPLETION(result.completion);
313 break;
314 }
315 /* fall through */
316 default:
317 printk("%s () failed err=%d\n", e, -ret);
318 goto out;
319 }
320
321 q = kmap(sg_page(&sg[0])) + sg[0].offset;
322 hexdump(q, cipher_tv[i].rlen);
323
324 printk("%s\n",
325 memcmp(q, cipher_tv[i].result,
326 cipher_tv[i].rlen) ? "fail" : "pass");
327 }
328 }
329
330 printk("\ntesting %s %s across pages (chunking)\n", algo, e);
331 memset(xbuf, 0, XBUFSIZE);
332
333 j = 0;
334 for (i = 0; i < tcount; i++) {
335 if (cipher_tv[i].np) {
336 j++;
337 printk("test %u (%d bit key):\n",
338 j, cipher_tv[i].klen * 8);
339
340 crypto_ablkcipher_clear_flags(tfm, ~0);
341 if (cipher_tv[i].wk)
342 crypto_ablkcipher_set_flags(
343 tfm, CRYPTO_TFM_REQ_WEAK_KEY);
344 key = cipher_tv[i].key;
345
346 ret = crypto_ablkcipher_setkey(tfm, key,
347 cipher_tv[i].klen);
348 if (ret) {
349 printk("setkey() failed flags=%x\n",
350 crypto_ablkcipher_get_flags(tfm));
351
352 if (!cipher_tv[i].fail)
353 goto out;
354 }
355
356 temp = 0;
357 sg_init_table(sg, cipher_tv[i].np);
358 for (k = 0; k < cipher_tv[i].np; k++) {
359 memcpy(&xbuf[IDX[k]],
360 cipher_tv[i].input + temp,
361 cipher_tv[i].tap[k]);
362 temp += cipher_tv[i].tap[k];
363 sg_set_buf(&sg[k], &xbuf[IDX[k]],
364 cipher_tv[i].tap[k]);
365 }
366
367 ablkcipher_request_set_crypt(req, sg, sg,
368 cipher_tv[i].ilen,
369 cipher_tv[i].iv);
370
371 ret = enc ?
372 crypto_ablkcipher_encrypt(req) :
373 crypto_ablkcipher_decrypt(req);
374
375 switch (ret) {
376 case 0:
377 break;
378 case -EINPROGRESS:
379 case -EBUSY:
380 ret = wait_for_completion_interruptible(
381 &result.completion);
382 if (!ret && !((ret = result.err))) {
383 INIT_COMPLETION(result.completion);
384 break;
385 }
386 /* fall through */
387 default:
388 printk("%s () failed err=%d\n", e, -ret);
389 goto out;
390 }
391
392 temp = 0;
393 for (k = 0; k < cipher_tv[i].np; k++) {
394 printk("page %u\n", k);
395 q = kmap(sg_page(&sg[k])) + sg[k].offset;
396 hexdump(q, cipher_tv[i].tap[k]);
397 printk("%s\n",
398 memcmp(q, cipher_tv[i].result + temp,
399 cipher_tv[i].tap[k]) ? "fail" :
400 "pass");
401 temp += cipher_tv[i].tap[k];
402 }
403 }
404 }
405
406 out:
407 crypto_free_ablkcipher(tfm);
408 ablkcipher_request_free(req);
409 }
410
411 static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc, char *p,
412 int blen, int sec)
413 {
414 struct scatterlist sg[1];
415 unsigned long start, end;
416 int bcount;
417 int ret;
418
419 sg_init_one(sg, p, blen);
420
421 for (start = jiffies, end = start + sec * HZ, bcount = 0;
422 time_before(jiffies, end); bcount++) {
423 if (enc)
424 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
425 else
426 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
427
428 if (ret)
429 return ret;
430 }
431
432 printk("%d operations in %d seconds (%ld bytes)\n",
433 bcount, sec, (long)bcount * blen);
434 return 0;
435 }
436
437 static int test_cipher_cycles(struct blkcipher_desc *desc, int enc, char *p,
438 int blen)
439 {
440 struct scatterlist sg[1];
441 unsigned long cycles = 0;
442 int ret = 0;
443 int i;
444
445 sg_init_one(sg, p, blen);
446
447 local_bh_disable();
448 local_irq_disable();
449
450 /* Warm-up run. */
451 for (i = 0; i < 4; i++) {
452 if (enc)
453 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
454 else
455 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
456
457 if (ret)
458 goto out;
459 }
460
461 /* The real thing. */
462 for (i = 0; i < 8; i++) {
463 cycles_t start, end;
464
465 start = get_cycles();
466 if (enc)
467 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
468 else
469 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
470 end = get_cycles();
471
472 if (ret)
473 goto out;
474
475 cycles += end - start;
476 }
477
478 out:
479 local_irq_enable();
480 local_bh_enable();
481
482 if (ret == 0)
483 printk("1 operation in %lu cycles (%d bytes)\n",
484 (cycles + 4) / 8, blen);
485
486 return ret;
487 }
488
489 static void test_cipher_speed(char *algo, int enc, unsigned int sec,
490 struct cipher_testvec *template,
491 unsigned int tcount, struct cipher_speed *speed)
492 {
493 unsigned int ret, i, j, iv_len;
494 unsigned char *key, *p, iv[128];
495 struct crypto_blkcipher *tfm;
496 struct blkcipher_desc desc;
497 const char *e;
498
499 if (enc == ENCRYPT)
500 e = "encryption";
501 else
502 e = "decryption";
503
504 printk("\ntesting speed of %s %s\n", algo, e);
505
506 tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);
507
508 if (IS_ERR(tfm)) {
509 printk("failed to load transform for %s: %ld\n", algo,
510 PTR_ERR(tfm));
511 return;
512 }
513 desc.tfm = tfm;
514 desc.flags = 0;
515
516 for (i = 0; speed[i].klen != 0; i++) {
517 if ((speed[i].blen + speed[i].klen) > TVMEMSIZE) {
518 printk("template (%u) too big for tvmem (%u)\n",
519 speed[i].blen + speed[i].klen, TVMEMSIZE);
520 goto out;
521 }
522
523 printk("test %u (%d bit key, %d byte blocks): ", i,
524 speed[i].klen * 8, speed[i].blen);
525
526 memset(tvmem, 0xff, speed[i].klen + speed[i].blen);
527
528 /* set key, plain text and IV */
529 key = (unsigned char *)tvmem;
530 for (j = 0; j < tcount; j++) {
531 if (template[j].klen == speed[i].klen) {
532 key = template[j].key;
533 break;
534 }
535 }
536 p = (unsigned char *)tvmem + speed[i].klen;
537
538 ret = crypto_blkcipher_setkey(tfm, key, speed[i].klen);
539 if (ret) {
540 printk("setkey() failed flags=%x\n",
541 crypto_blkcipher_get_flags(tfm));
542 goto out;
543 }
544
545 iv_len = crypto_blkcipher_ivsize(tfm);
546 if (iv_len) {
547 memset(&iv, 0xff, iv_len);
548 crypto_blkcipher_set_iv(tfm, iv, iv_len);
549 }
550
551 if (sec)
552 ret = test_cipher_jiffies(&desc, enc, p, speed[i].blen,
553 sec);
554 else
555 ret = test_cipher_cycles(&desc, enc, p, speed[i].blen);
556
557 if (ret) {
558 printk("%s() failed flags=%x\n", e, desc.flags);
559 break;
560 }
561 }
562
563 out:
564 crypto_free_blkcipher(tfm);
565 }
566
567 static int test_hash_jiffies_digest(struct hash_desc *desc, char *p, int blen,
568 char *out, int sec)
569 {
570 struct scatterlist sg[1];
571 unsigned long start, end;
572 int bcount;
573 int ret;
574
575 sg_init_table(sg, 1);
576
577 for (start = jiffies, end = start + sec * HZ, bcount = 0;
578 time_before(jiffies, end); bcount++) {
579 sg_set_buf(sg, p, blen);
580 ret = crypto_hash_digest(desc, sg, blen, out);
581 if (ret)
582 return ret;
583 }
584
585 printk("%6u opers/sec, %9lu bytes/sec\n",
586 bcount / sec, ((long)bcount * blen) / sec);
587
588 return 0;
589 }
590
591 static int test_hash_jiffies(struct hash_desc *desc, char *p, int blen,
592 int plen, char *out, int sec)
593 {
594 struct scatterlist sg[1];
595 unsigned long start, end;
596 int bcount, pcount;
597 int ret;
598
599 if (plen == blen)
600 return test_hash_jiffies_digest(desc, p, blen, out, sec);
601
602 sg_init_table(sg, 1);
603
604 for (start = jiffies, end = start + sec * HZ, bcount = 0;
605 time_before(jiffies, end); bcount++) {
606 ret = crypto_hash_init(desc);
607 if (ret)
608 return ret;
609 for (pcount = 0; pcount < blen; pcount += plen) {
610 sg_set_buf(sg, p + pcount, plen);
611 ret = crypto_hash_update(desc, sg, plen);
612 if (ret)
613 return ret;
614 }
615 /* we assume there is enough space in 'out' for the result */
616 ret = crypto_hash_final(desc, out);
617 if (ret)
618 return ret;
619 }
620
621 printk("%6u opers/sec, %9lu bytes/sec\n",
622 bcount / sec, ((long)bcount * blen) / sec);
623
624 return 0;
625 }
626
627 static int test_hash_cycles_digest(struct hash_desc *desc, char *p, int blen,
628 char *out)
629 {
630 struct scatterlist sg[1];
631 unsigned long cycles = 0;
632 int i;
633 int ret;
634
635 sg_init_table(sg, 1);
636
637 local_bh_disable();
638 local_irq_disable();
639
640 /* Warm-up run. */
641 for (i = 0; i < 4; i++) {
642 sg_set_buf(sg, p, blen);
643 ret = crypto_hash_digest(desc, sg, blen, out);
644 if (ret)
645 goto out;
646 }
647
648 /* The real thing. */
649 for (i = 0; i < 8; i++) {
650 cycles_t start, end;
651
652 start = get_cycles();
653
654 sg_set_buf(sg, p, blen);
655 ret = crypto_hash_digest(desc, sg, blen, out);
656 if (ret)
657 goto out;
658
659 end = get_cycles();
660
661 cycles += end - start;
662 }
663
664 out:
665 local_irq_enable();
666 local_bh_enable();
667
668 if (ret)
669 return ret;
670
671 printk("%6lu cycles/operation, %4lu cycles/byte\n",
672 cycles / 8, cycles / (8 * blen));
673
674 return 0;
675 }
676
677 static int test_hash_cycles(struct hash_desc *desc, char *p, int blen,
678 int plen, char *out)
679 {
680 struct scatterlist sg[1];
681 unsigned long cycles = 0;
682 int i, pcount;
683 int ret;
684
685 if (plen == blen)
686 return test_hash_cycles_digest(desc, p, blen, out);
687
688 sg_init_table(sg, 1);
689
690 local_bh_disable();
691 local_irq_disable();
692
693 /* Warm-up run. */
694 for (i = 0; i < 4; i++) {
695 ret = crypto_hash_init(desc);
696 if (ret)
697 goto out;
698 for (pcount = 0; pcount < blen; pcount += plen) {
699 sg_set_buf(sg, p + pcount, plen);
700 ret = crypto_hash_update(desc, sg, plen);
701 if (ret)
702 goto out;
703 }
704 ret = crypto_hash_final(desc, out);
705 if (ret)
706 goto out;
707 }
708
709 /* The real thing. */
710 for (i = 0; i < 8; i++) {
711 cycles_t start, end;
712
713 start = get_cycles();
714
715 ret = crypto_hash_init(desc);
716 if (ret)
717 goto out;
718 for (pcount = 0; pcount < blen; pcount += plen) {
719 sg_set_buf(sg, p + pcount, plen);
720 ret = crypto_hash_update(desc, sg, plen);
721 if (ret)
722 goto out;
723 }
724 ret = crypto_hash_final(desc, out);
725 if (ret)
726 goto out;
727
728 end = get_cycles();
729
730 cycles += end - start;
731 }
732
733 out:
734 local_irq_enable();
735 local_bh_enable();
736
737 if (ret)
738 return ret;
739
740 printk("%6lu cycles/operation, %4lu cycles/byte\n",
741 cycles / 8, cycles / (8 * blen));
742
743 return 0;
744 }
745
746 static void test_hash_speed(char *algo, unsigned int sec,
747 struct hash_speed *speed)
748 {
749 struct crypto_hash *tfm;
750 struct hash_desc desc;
751 char output[1024];
752 int i;
753 int ret;
754
755 printk("\ntesting speed of %s\n", algo);
756
757 tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
758
759 if (IS_ERR(tfm)) {
760 printk("failed to load transform for %s: %ld\n", algo,
761 PTR_ERR(tfm));
762 return;
763 }
764
765 desc.tfm = tfm;
766 desc.flags = 0;
767
768 if (crypto_hash_digestsize(tfm) > sizeof(output)) {
769 printk("digestsize(%u) > outputbuffer(%zu)\n",
770 crypto_hash_digestsize(tfm), sizeof(output));
771 goto out;
772 }
773
774 for (i = 0; speed[i].blen != 0; i++) {
775 if (speed[i].blen > TVMEMSIZE) {
776 printk("template (%u) too big for tvmem (%u)\n",
777 speed[i].blen, TVMEMSIZE);
778 goto out;
779 }
780
781 printk("test%3u (%5u byte blocks,%5u bytes per update,%4u updates): ",
782 i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
783
784 memset(tvmem, 0xff, speed[i].blen);
785
786 if (sec)
787 ret = test_hash_jiffies(&desc, tvmem, speed[i].blen,
788 speed[i].plen, output, sec);
789 else
790 ret = test_hash_cycles(&desc, tvmem, speed[i].blen,
791 speed[i].plen, output);
792
793 if (ret) {
794 printk("hashing failed ret=%d\n", ret);
795 break;
796 }
797 }
798
799 out:
800 crypto_free_hash(tfm);
801 }
802
803 static void test_deflate(void)
804 {
805 unsigned int i;
806 char result[COMP_BUF_SIZE];
807 struct crypto_comp *tfm;
808 struct comp_testvec *tv;
809 unsigned int tsize;
810
811 printk("\ntesting deflate compression\n");
812
813 tsize = sizeof (deflate_comp_tv_template);
814 if (tsize > TVMEMSIZE) {
815 printk("template (%u) too big for tvmem (%u)\n", tsize,
816 TVMEMSIZE);
817 return;
818 }
819
820 memcpy(tvmem, deflate_comp_tv_template, tsize);
821 tv = (void *)tvmem;
822
823 tfm = crypto_alloc_comp("deflate", 0, CRYPTO_ALG_ASYNC);
824 if (IS_ERR(tfm)) {
825 printk("failed to load transform for deflate\n");
826 return;
827 }
828
829 for (i = 0; i < DEFLATE_COMP_TEST_VECTORS; i++) {
830 int ilen, ret, dlen = COMP_BUF_SIZE;
831
832 printk("test %u:\n", i + 1);
833 memset(result, 0, sizeof (result));
834
835 ilen = tv[i].inlen;
836 ret = crypto_comp_compress(tfm, tv[i].input,
837 ilen, result, &dlen);
838 if (ret) {
839 printk("fail: ret=%d\n", ret);
840 continue;
841 }
842 hexdump(result, dlen);
843 printk("%s (ratio %d:%d)\n",
844 memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
845 ilen, dlen);
846 }
847
848 printk("\ntesting deflate decompression\n");
849
850 tsize = sizeof (deflate_decomp_tv_template);
851 if (tsize > TVMEMSIZE) {
852 printk("template (%u) too big for tvmem (%u)\n", tsize,
853 TVMEMSIZE);
854 goto out;
855 }
856
857 memcpy(tvmem, deflate_decomp_tv_template, tsize);
858 tv = (void *)tvmem;
859
860 for (i = 0; i < DEFLATE_DECOMP_TEST_VECTORS; i++) {
861 int ilen, ret, dlen = COMP_BUF_SIZE;
862
863 printk("test %u:\n", i + 1);
864 memset(result, 0, sizeof (result));
865
866 ilen = tv[i].inlen;
867 ret = crypto_comp_decompress(tfm, tv[i].input,
868 ilen, result, &dlen);
869 if (ret) {
870 printk("fail: ret=%d\n", ret);
871 continue;
872 }
873 hexdump(result, dlen);
874 printk("%s (ratio %d:%d)\n",
875 memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
876 ilen, dlen);
877 }
878 out:
879 crypto_free_comp(tfm);
880 }
881
882 static void test_available(void)
883 {
884 char **name = check;
885
886 while (*name) {
887 printk("alg %s ", *name);
888 printk(crypto_has_alg(*name, 0, 0) ?
889 "found\n" : "not found\n");
890 name++;
891 }
892 }
893
894 static void do_test(void)
895 {
896 switch (mode) {
897
898 case 0:
899 test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
900
901 test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
902
903 //DES
904 test_cipher("ecb(des)", ENCRYPT, des_enc_tv_template,
905 DES_ENC_TEST_VECTORS);
906 test_cipher("ecb(des)", DECRYPT, des_dec_tv_template,
907 DES_DEC_TEST_VECTORS);
908 test_cipher("cbc(des)", ENCRYPT, des_cbc_enc_tv_template,
909 DES_CBC_ENC_TEST_VECTORS);
910 test_cipher("cbc(des)", DECRYPT, des_cbc_dec_tv_template,
911 DES_CBC_DEC_TEST_VECTORS);
912
913 //DES3_EDE
914 test_cipher("ecb(des3_ede)", ENCRYPT, des3_ede_enc_tv_template,
915 DES3_EDE_ENC_TEST_VECTORS);
916 test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
917 DES3_EDE_DEC_TEST_VECTORS);
918
919 test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
920
921 test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
922
923 //BLOWFISH
924 test_cipher("ecb(blowfish)", ENCRYPT, bf_enc_tv_template,
925 BF_ENC_TEST_VECTORS);
926 test_cipher("ecb(blowfish)", DECRYPT, bf_dec_tv_template,
927 BF_DEC_TEST_VECTORS);
928 test_cipher("cbc(blowfish)", ENCRYPT, bf_cbc_enc_tv_template,
929 BF_CBC_ENC_TEST_VECTORS);
930 test_cipher("cbc(blowfish)", DECRYPT, bf_cbc_dec_tv_template,
931 BF_CBC_DEC_TEST_VECTORS);
932
933 //TWOFISH
934 test_cipher("ecb(twofish)", ENCRYPT, tf_enc_tv_template,
935 TF_ENC_TEST_VECTORS);
936 test_cipher("ecb(twofish)", DECRYPT, tf_dec_tv_template,
937 TF_DEC_TEST_VECTORS);
938 test_cipher("cbc(twofish)", ENCRYPT, tf_cbc_enc_tv_template,
939 TF_CBC_ENC_TEST_VECTORS);
940 test_cipher("cbc(twofish)", DECRYPT, tf_cbc_dec_tv_template,
941 TF_CBC_DEC_TEST_VECTORS);
942
943 //SERPENT
944 test_cipher("ecb(serpent)", ENCRYPT, serpent_enc_tv_template,
945 SERPENT_ENC_TEST_VECTORS);
946 test_cipher("ecb(serpent)", DECRYPT, serpent_dec_tv_template,
947 SERPENT_DEC_TEST_VECTORS);
948
949 //TNEPRES
950 test_cipher("ecb(tnepres)", ENCRYPT, tnepres_enc_tv_template,
951 TNEPRES_ENC_TEST_VECTORS);
952 test_cipher("ecb(tnepres)", DECRYPT, tnepres_dec_tv_template,
953 TNEPRES_DEC_TEST_VECTORS);
954
955 //AES
956 test_cipher("ecb(aes)", ENCRYPT, aes_enc_tv_template,
957 AES_ENC_TEST_VECTORS);
958 test_cipher("ecb(aes)", DECRYPT, aes_dec_tv_template,
959 AES_DEC_TEST_VECTORS);
960 test_cipher("cbc(aes)", ENCRYPT, aes_cbc_enc_tv_template,
961 AES_CBC_ENC_TEST_VECTORS);
962 test_cipher("cbc(aes)", DECRYPT, aes_cbc_dec_tv_template,
963 AES_CBC_DEC_TEST_VECTORS);
964 test_cipher("lrw(aes)", ENCRYPT, aes_lrw_enc_tv_template,
965 AES_LRW_ENC_TEST_VECTORS);
966 test_cipher("lrw(aes)", DECRYPT, aes_lrw_dec_tv_template,
967 AES_LRW_DEC_TEST_VECTORS);
968 test_cipher("xts(aes)", ENCRYPT, aes_xts_enc_tv_template,
969 AES_XTS_ENC_TEST_VECTORS);
970 test_cipher("xts(aes)", DECRYPT, aes_xts_dec_tv_template,
971 AES_XTS_DEC_TEST_VECTORS);
972 test_cipher("ctr(aes,4,8,4)", ENCRYPT, aes_ctr_enc_tv_template,
973 AES_CTR_ENC_TEST_VECTORS);
974 test_cipher("ctr(aes,4,8,4)", DECRYPT, aes_ctr_dec_tv_template,
975 AES_CTR_DEC_TEST_VECTORS);
976
977 //CAST5
978 test_cipher("ecb(cast5)", ENCRYPT, cast5_enc_tv_template,
979 CAST5_ENC_TEST_VECTORS);
980 test_cipher("ecb(cast5)", DECRYPT, cast5_dec_tv_template,
981 CAST5_DEC_TEST_VECTORS);
982
983 //CAST6
984 test_cipher("ecb(cast6)", ENCRYPT, cast6_enc_tv_template,
985 CAST6_ENC_TEST_VECTORS);
986 test_cipher("ecb(cast6)", DECRYPT, cast6_dec_tv_template,
987 CAST6_DEC_TEST_VECTORS);
988
989 //ARC4
990 test_cipher("ecb(arc4)", ENCRYPT, arc4_enc_tv_template,
991 ARC4_ENC_TEST_VECTORS);
992 test_cipher("ecb(arc4)", DECRYPT, arc4_dec_tv_template,
993 ARC4_DEC_TEST_VECTORS);
994
995 //TEA
996 test_cipher("ecb(tea)", ENCRYPT, tea_enc_tv_template,
997 TEA_ENC_TEST_VECTORS);
998 test_cipher("ecb(tea)", DECRYPT, tea_dec_tv_template,
999 TEA_DEC_TEST_VECTORS);
1000
1001
1002 //XTEA
1003 test_cipher("ecb(xtea)", ENCRYPT, xtea_enc_tv_template,
1004 XTEA_ENC_TEST_VECTORS);
1005 test_cipher("ecb(xtea)", DECRYPT, xtea_dec_tv_template,
1006 XTEA_DEC_TEST_VECTORS);
1007
1008 //KHAZAD
1009 test_cipher("ecb(khazad)", ENCRYPT, khazad_enc_tv_template,
1010 KHAZAD_ENC_TEST_VECTORS);
1011 test_cipher("ecb(khazad)", DECRYPT, khazad_dec_tv_template,
1012 KHAZAD_DEC_TEST_VECTORS);
1013
1014 //ANUBIS
1015 test_cipher("ecb(anubis)", ENCRYPT, anubis_enc_tv_template,
1016 ANUBIS_ENC_TEST_VECTORS);
1017 test_cipher("ecb(anubis)", DECRYPT, anubis_dec_tv_template,
1018 ANUBIS_DEC_TEST_VECTORS);
1019 test_cipher("cbc(anubis)", ENCRYPT, anubis_cbc_enc_tv_template,
1020 ANUBIS_CBC_ENC_TEST_VECTORS);
1021 test_cipher("cbc(anubis)", DECRYPT, anubis_cbc_dec_tv_template,
1022 ANUBIS_CBC_ENC_TEST_VECTORS);
1023
1024 //XETA
1025 test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
1026 XETA_ENC_TEST_VECTORS);
1027 test_cipher("ecb(xeta)", DECRYPT, xeta_dec_tv_template,
1028 XETA_DEC_TEST_VECTORS);
1029
1030 //FCrypt
1031 test_cipher("pcbc(fcrypt)", ENCRYPT, fcrypt_pcbc_enc_tv_template,
1032 FCRYPT_ENC_TEST_VECTORS);
1033 test_cipher("pcbc(fcrypt)", DECRYPT, fcrypt_pcbc_dec_tv_template,
1034 FCRYPT_DEC_TEST_VECTORS);
1035
1036 //CAMELLIA
1037 test_cipher("ecb(camellia)", ENCRYPT,
1038 camellia_enc_tv_template,
1039 CAMELLIA_ENC_TEST_VECTORS);
1040 test_cipher("ecb(camellia)", DECRYPT,
1041 camellia_dec_tv_template,
1042 CAMELLIA_DEC_TEST_VECTORS);
1043 test_cipher("cbc(camellia)", ENCRYPT,
1044 camellia_cbc_enc_tv_template,
1045 CAMELLIA_CBC_ENC_TEST_VECTORS);
1046 test_cipher("cbc(camellia)", DECRYPT,
1047 camellia_cbc_dec_tv_template,
1048 CAMELLIA_CBC_DEC_TEST_VECTORS);
1049
1050 //SEED
1051 test_cipher("ecb(seed)", ENCRYPT, seed_enc_tv_template,
1052 SEED_ENC_TEST_VECTORS);
1053 test_cipher("ecb(seed)", DECRYPT, seed_dec_tv_template,
1054 SEED_DEC_TEST_VECTORS);
1055
1056 test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
1057 test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
1058 test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
1059 test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
1060 test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
1061 test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
1062 test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
1063 test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
1064 test_deflate();
1065 test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
1066 test_hash("hmac(md5)", hmac_md5_tv_template,
1067 HMAC_MD5_TEST_VECTORS);
1068 test_hash("hmac(sha1)", hmac_sha1_tv_template,
1069 HMAC_SHA1_TEST_VECTORS);
1070 test_hash("hmac(sha256)", hmac_sha256_tv_template,
1071 HMAC_SHA256_TEST_VECTORS);
1072 test_hash("hmac(sha384)", hmac_sha384_tv_template,
1073 HMAC_SHA384_TEST_VECTORS);
1074 test_hash("hmac(sha512)", hmac_sha512_tv_template,
1075 HMAC_SHA512_TEST_VECTORS);
1076
1077 test_hash("xcbc(aes)", aes_xcbc128_tv_template,
1078 XCBC_AES_TEST_VECTORS);
1079
1080 test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
1081 break;
1082
1083 case 1:
1084 test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
1085 break;
1086
1087 case 2:
1088 test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
1089 break;
1090
1091 case 3:
1092 test_cipher("ecb(des)", ENCRYPT, des_enc_tv_template,
1093 DES_ENC_TEST_VECTORS);
1094 test_cipher("ecb(des)", DECRYPT, des_dec_tv_template,
1095 DES_DEC_TEST_VECTORS);
1096 test_cipher("cbc(des)", ENCRYPT, des_cbc_enc_tv_template,
1097 DES_CBC_ENC_TEST_VECTORS);
1098 test_cipher("cbc(des)", DECRYPT, des_cbc_dec_tv_template,
1099 DES_CBC_DEC_TEST_VECTORS);
1100 break;
1101
1102 case 4:
1103 test_cipher("ecb(des3_ede)", ENCRYPT, des3_ede_enc_tv_template,
1104 DES3_EDE_ENC_TEST_VECTORS);
1105 test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
1106 DES3_EDE_DEC_TEST_VECTORS);
1107 break;
1108
1109 case 5:
1110 test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
1111 break;
1112
1113 case 6:
1114 test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
1115 break;
1116
1117 case 7:
1118 test_cipher("ecb(blowfish)", ENCRYPT, bf_enc_tv_template,
1119 BF_ENC_TEST_VECTORS);
1120 test_cipher("ecb(blowfish)", DECRYPT, bf_dec_tv_template,
1121 BF_DEC_TEST_VECTORS);
1122 test_cipher("cbc(blowfish)", ENCRYPT, bf_cbc_enc_tv_template,
1123 BF_CBC_ENC_TEST_VECTORS);
1124 test_cipher("cbc(blowfish)", DECRYPT, bf_cbc_dec_tv_template,
1125 BF_CBC_DEC_TEST_VECTORS);
1126 break;
1127
1128 case 8:
1129 test_cipher("ecb(twofish)", ENCRYPT, tf_enc_tv_template,
1130 TF_ENC_TEST_VECTORS);
1131 test_cipher("ecb(twofish)", DECRYPT, tf_dec_tv_template,
1132 TF_DEC_TEST_VECTORS);
1133 test_cipher("cbc(twofish)", ENCRYPT, tf_cbc_enc_tv_template,
1134 TF_CBC_ENC_TEST_VECTORS);
1135 test_cipher("cbc(twofish)", DECRYPT, tf_cbc_dec_tv_template,
1136 TF_CBC_DEC_TEST_VECTORS);
1137 break;
1138
1139 case 9:
1140 test_cipher("ecb(serpent)", ENCRYPT, serpent_enc_tv_template,
1141 SERPENT_ENC_TEST_VECTORS);
1142 test_cipher("ecb(serpent)", DECRYPT, serpent_dec_tv_template,
1143 SERPENT_DEC_TEST_VECTORS);
1144 break;
1145
1146 case 10:
1147 test_cipher("ecb(aes)", ENCRYPT, aes_enc_tv_template,
1148 AES_ENC_TEST_VECTORS);
1149 test_cipher("ecb(aes)", DECRYPT, aes_dec_tv_template,
1150 AES_DEC_TEST_VECTORS);
1151 test_cipher("cbc(aes)", ENCRYPT, aes_cbc_enc_tv_template,
1152 AES_CBC_ENC_TEST_VECTORS);
1153 test_cipher("cbc(aes)", DECRYPT, aes_cbc_dec_tv_template,
1154 AES_CBC_DEC_TEST_VECTORS);
1155 test_cipher("lrw(aes)", ENCRYPT, aes_lrw_enc_tv_template,
1156 AES_LRW_ENC_TEST_VECTORS);
1157 test_cipher("lrw(aes)", DECRYPT, aes_lrw_dec_tv_template,
1158 AES_LRW_DEC_TEST_VECTORS);
1159 test_cipher("xts(aes)", ENCRYPT, aes_xts_enc_tv_template,
1160 AES_XTS_ENC_TEST_VECTORS);
1161 test_cipher("xts(aes)", DECRYPT, aes_xts_dec_tv_template,
1162 AES_XTS_DEC_TEST_VECTORS);
1163 test_cipher("ctr(aes,4,8,4)", ENCRYPT, aes_ctr_enc_tv_template,
1164 AES_CTR_ENC_TEST_VECTORS);
1165 test_cipher("ctr(aes,4,8,4)", DECRYPT, aes_ctr_dec_tv_template,
1166 AES_CTR_DEC_TEST_VECTORS);
1167 break;
1168
1169 case 11:
1170 test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
1171 break;
1172
1173 case 12:
1174 test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
1175 break;
1176
1177 case 13:
1178 test_deflate();
1179 break;
1180
1181 case 14:
1182 test_cipher("ecb(cast5)", ENCRYPT, cast5_enc_tv_template,
1183 CAST5_ENC_TEST_VECTORS);
1184 test_cipher("ecb(cast5)", DECRYPT, cast5_dec_tv_template,
1185 CAST5_DEC_TEST_VECTORS);
1186 break;
1187
1188 case 15:
1189 test_cipher("ecb(cast6)", ENCRYPT, cast6_enc_tv_template,
1190 CAST6_ENC_TEST_VECTORS);
1191 test_cipher("ecb(cast6)", DECRYPT, cast6_dec_tv_template,
1192 CAST6_DEC_TEST_VECTORS);
1193 break;
1194
1195 case 16:
1196 test_cipher("ecb(arc4)", ENCRYPT, arc4_enc_tv_template,
1197 ARC4_ENC_TEST_VECTORS);
1198 test_cipher("ecb(arc4)", DECRYPT, arc4_dec_tv_template,
1199 ARC4_DEC_TEST_VECTORS);
1200 break;
1201
1202 case 17:
1203 test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
1204 break;
1205
1206 case 18:
1207 test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
1208 break;
1209
1210 case 19:
1211 test_cipher("ecb(tea)", ENCRYPT, tea_enc_tv_template,
1212 TEA_ENC_TEST_VECTORS);
1213 test_cipher("ecb(tea)", DECRYPT, tea_dec_tv_template,
1214 TEA_DEC_TEST_VECTORS);
1215 break;
1216
1217 case 20:
1218 test_cipher("ecb(xtea)", ENCRYPT, xtea_enc_tv_template,
1219 XTEA_ENC_TEST_VECTORS);
1220 test_cipher("ecb(xtea)", DECRYPT, xtea_dec_tv_template,
1221 XTEA_DEC_TEST_VECTORS);
1222 break;
1223
1224 case 21:
1225 test_cipher("ecb(khazad)", ENCRYPT, khazad_enc_tv_template,
1226 KHAZAD_ENC_TEST_VECTORS);
1227 test_cipher("ecb(khazad)", DECRYPT, khazad_dec_tv_template,
1228 KHAZAD_DEC_TEST_VECTORS);
1229 break;
1230
1231 case 22:
1232 test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
1233 break;
1234
1235 case 23:
1236 test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
1237 break;
1238
1239 case 24:
1240 test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
1241 break;
1242
1243 case 25:
1244 test_cipher("ecb(tnepres)", ENCRYPT, tnepres_enc_tv_template,
1245 TNEPRES_ENC_TEST_VECTORS);
1246 test_cipher("ecb(tnepres)", DECRYPT, tnepres_dec_tv_template,
1247 TNEPRES_DEC_TEST_VECTORS);
1248 break;
1249
1250 case 26:
1251 test_cipher("ecb(anubis)", ENCRYPT, anubis_enc_tv_template,
1252 ANUBIS_ENC_TEST_VECTORS);
1253 test_cipher("ecb(anubis)", DECRYPT, anubis_dec_tv_template,
1254 ANUBIS_DEC_TEST_VECTORS);
1255 test_cipher("cbc(anubis)", ENCRYPT, anubis_cbc_enc_tv_template,
1256 ANUBIS_CBC_ENC_TEST_VECTORS);
1257 test_cipher("cbc(anubis)", DECRYPT, anubis_cbc_dec_tv_template,
1258 ANUBIS_CBC_ENC_TEST_VECTORS);
1259 break;
1260
1261 case 27:
1262 test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
1263 break;
1264
1265 case 28:
1266
1267 test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
1268 break;
1269
1270 case 29:
1271 test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
1272 break;
1273
1274 case 30:
1275 test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
1276 XETA_ENC_TEST_VECTORS);
1277 test_cipher("ecb(xeta)", DECRYPT, xeta_dec_tv_template,
1278 XETA_DEC_TEST_VECTORS);
1279 break;
1280
1281 case 31:
1282 test_cipher("pcbc(fcrypt)", ENCRYPT, fcrypt_pcbc_enc_tv_template,
1283 FCRYPT_ENC_TEST_VECTORS);
1284 test_cipher("pcbc(fcrypt)", DECRYPT, fcrypt_pcbc_dec_tv_template,
1285 FCRYPT_DEC_TEST_VECTORS);
1286 break;
1287
1288 case 32:
1289 test_cipher("ecb(camellia)", ENCRYPT,
1290 camellia_enc_tv_template,
1291 CAMELLIA_ENC_TEST_VECTORS);
1292 test_cipher("ecb(camellia)", DECRYPT,
1293 camellia_dec_tv_template,
1294 CAMELLIA_DEC_TEST_VECTORS);
1295 test_cipher("cbc(camellia)", ENCRYPT,
1296 camellia_cbc_enc_tv_template,
1297 CAMELLIA_CBC_ENC_TEST_VECTORS);
1298 test_cipher("cbc(camellia)", DECRYPT,
1299 camellia_cbc_dec_tv_template,
1300 CAMELLIA_CBC_DEC_TEST_VECTORS);
1301 break;
1302
1303 case 100:
1304 test_hash("hmac(md5)", hmac_md5_tv_template,
1305 HMAC_MD5_TEST_VECTORS);
1306 break;
1307
1308 case 101:
1309 test_hash("hmac(sha1)", hmac_sha1_tv_template,
1310 HMAC_SHA1_TEST_VECTORS);
1311 break;
1312
1313 case 102:
1314 test_hash("hmac(sha256)", hmac_sha256_tv_template,
1315 HMAC_SHA256_TEST_VECTORS);
1316 break;
1317
1318 case 103:
1319 test_hash("hmac(sha384)", hmac_sha384_tv_template,
1320 HMAC_SHA384_TEST_VECTORS);
1321 break;
1322
1323 case 104:
1324 test_hash("hmac(sha512)", hmac_sha512_tv_template,
1325 HMAC_SHA512_TEST_VECTORS);
1326 break;
1327
1328
1329 case 200:
1330 test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
1331 aes_speed_template);
1332 test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
1333 aes_speed_template);
1334 test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
1335 aes_speed_template);
1336 test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
1337 aes_speed_template);
1338 test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
1339 aes_lrw_speed_template);
1340 test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
1341 aes_lrw_speed_template);
1342 test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
1343 aes_xts_speed_template);
1344 test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
1345 aes_xts_speed_template);
1346 break;
1347
1348 case 201:
1349 test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
1350 des3_ede_enc_tv_template,
1351 DES3_EDE_ENC_TEST_VECTORS,
1352 des3_ede_speed_template);
1353 test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
1354 des3_ede_dec_tv_template,
1355 DES3_EDE_DEC_TEST_VECTORS,
1356 des3_ede_speed_template);
1357 test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
1358 des3_ede_enc_tv_template,
1359 DES3_EDE_ENC_TEST_VECTORS,
1360 des3_ede_speed_template);
1361 test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
1362 des3_ede_dec_tv_template,
1363 DES3_EDE_DEC_TEST_VECTORS,
1364 des3_ede_speed_template);
1365 break;
1366
1367 case 202:
1368 test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
1369 twofish_speed_template);
1370 test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
1371 twofish_speed_template);
1372 test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
1373 twofish_speed_template);
1374 test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
1375 twofish_speed_template);
1376 break;
1377
1378 case 203:
1379 test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
1380 blowfish_speed_template);
1381 test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
1382 blowfish_speed_template);
1383 test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
1384 blowfish_speed_template);
1385 test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
1386 blowfish_speed_template);
1387 break;
1388
1389 case 204:
1390 test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
1391 des_speed_template);
1392 test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
1393 des_speed_template);
1394 test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
1395 des_speed_template);
1396 test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
1397 des_speed_template);
1398 break;
1399
1400 case 205:
1401 test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
1402 camellia_speed_template);
1403 test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
1404 camellia_speed_template);
1405 test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
1406 camellia_speed_template);
1407 test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
1408 camellia_speed_template);
1409 break;
1410
1411 case 300:
1412 /* fall through */
1413
1414 case 301:
1415 test_hash_speed("md4", sec, generic_hash_speed_template);
1416 if (mode > 300 && mode < 400) break;
1417
1418 case 302:
1419 test_hash_speed("md5", sec, generic_hash_speed_template);
1420 if (mode > 300 && mode < 400) break;
1421
1422 case 303:
1423 test_hash_speed("sha1", sec, generic_hash_speed_template);
1424 if (mode > 300 && mode < 400) break;
1425
1426 case 304:
1427 test_hash_speed("sha256", sec, generic_hash_speed_template);
1428 if (mode > 300 && mode < 400) break;
1429
1430 case 305:
1431 test_hash_speed("sha384", sec, generic_hash_speed_template);
1432 if (mode > 300 && mode < 400) break;
1433
1434 case 306:
1435 test_hash_speed("sha512", sec, generic_hash_speed_template);
1436 if (mode > 300 && mode < 400) break;
1437
1438 case 307:
1439 test_hash_speed("wp256", sec, generic_hash_speed_template);
1440 if (mode > 300 && mode < 400) break;
1441
1442 case 308:
1443 test_hash_speed("wp384", sec, generic_hash_speed_template);
1444 if (mode > 300 && mode < 400) break;
1445
1446 case 309:
1447 test_hash_speed("wp512", sec, generic_hash_speed_template);
1448 if (mode > 300 && mode < 400) break;
1449
1450 case 310:
1451 test_hash_speed("tgr128", sec, generic_hash_speed_template);
1452 if (mode > 300 && mode < 400) break;
1453
1454 case 311:
1455 test_hash_speed("tgr160", sec, generic_hash_speed_template);
1456 if (mode > 300 && mode < 400) break;
1457
1458 case 312:
1459 test_hash_speed("tgr192", sec, generic_hash_speed_template);
1460 if (mode > 300 && mode < 400) break;
1461
1462 case 399:
1463 break;
1464
1465 case 1000:
1466 test_available();
1467 break;
1468
1469 default:
1470 /* useful for debugging */
1471 printk("not testing anything\n");
1472 break;
1473 }
1474 }
1475
1476 static int __init init(void)
1477 {
1478 tvmem = kmalloc(TVMEMSIZE, GFP_KERNEL);
1479 if (tvmem == NULL)
1480 return -ENOMEM;
1481
1482 xbuf = kmalloc(XBUFSIZE, GFP_KERNEL);
1483 if (xbuf == NULL) {
1484 kfree(tvmem);
1485 return -ENOMEM;
1486 }
1487
1488 do_test();
1489
1490 kfree(xbuf);
1491 kfree(tvmem);
1492
1493 /* We intentionaly return -EAGAIN to prevent keeping
1494 * the module. It does all its work from init()
1495 * and doesn't offer any runtime functionality
1496 * => we don't need it in the memory, do we?
1497 * -- mludvig
1498 */
1499 return -EAGAIN;
1500 }
1501
1502 /*
1503 * If an init function is provided, an exit function must also be provided
1504 * to allow module unload.
1505 */
1506 static void __exit fini(void) { }
1507
1508 module_init(init);
1509 module_exit(fini);
1510
1511 module_param(mode, int, 0);
1512 module_param(sec, uint, 0);
1513 MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
1514 "(defaults to zero which uses CPU cycles instead)");
1515
1516 MODULE_LICENSE("GPL");
1517 MODULE_DESCRIPTION("Quick & dirty crypto testing module");
1518 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");