Loading...
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 * Copyright (c) 2007 Nokia Siemens Networks
10 *
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.
17 *
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.
22 *
23 */
24
25#include <crypto/hash.h>
26#include <linux/err.h>
27#include <linux/init.h>
28#include <linux/gfp.h>
29#include <linux/module.h>
30#include <linux/scatterlist.h>
31#include <linux/string.h>
32#include <linux/moduleparam.h>
33#include <linux/jiffies.h>
34#include <linux/timex.h>
35#include <linux/interrupt.h>
36#include "tcrypt.h"
37#include "internal.h"
38
39/*
40 * Need slab memory for testing (size in number of pages).
41 */
42#define TVMEMSIZE 4
43
44/*
45* Used by test_cipher_speed()
46*/
47#define ENCRYPT 1
48#define DECRYPT 0
49
50/*
51 * Used by test_cipher_speed()
52 */
53static unsigned int sec;
54
55static char *alg = NULL;
56static u32 type;
57static u32 mask;
58static int mode;
59static char *tvmem[TVMEMSIZE];
60
61static char *check[] = {
62 "des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256",
63 "blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
64 "cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
65 "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", "fcrypt",
66 "camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
67 "lzo", "cts", "zlib", NULL
68};
69
70static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc,
71 struct scatterlist *sg, int blen, int sec)
72{
73 unsigned long start, end;
74 int bcount;
75 int ret;
76
77 for (start = jiffies, end = start + sec * HZ, bcount = 0;
78 time_before(jiffies, end); bcount++) {
79 if (enc)
80 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
81 else
82 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
83
84 if (ret)
85 return ret;
86 }
87
88 printk("%d operations in %d seconds (%ld bytes)\n",
89 bcount, sec, (long)bcount * blen);
90 return 0;
91}
92
93static int test_cipher_cycles(struct blkcipher_desc *desc, int enc,
94 struct scatterlist *sg, int blen)
95{
96 unsigned long cycles = 0;
97 int ret = 0;
98 int i;
99
100 local_irq_disable();
101
102 /* Warm-up run. */
103 for (i = 0; i < 4; i++) {
104 if (enc)
105 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
106 else
107 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
108
109 if (ret)
110 goto out;
111 }
112
113 /* The real thing. */
114 for (i = 0; i < 8; i++) {
115 cycles_t start, end;
116
117 start = get_cycles();
118 if (enc)
119 ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
120 else
121 ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
122 end = get_cycles();
123
124 if (ret)
125 goto out;
126
127 cycles += end - start;
128 }
129
130out:
131 local_irq_enable();
132
133 if (ret == 0)
134 printk("1 operation in %lu cycles (%d bytes)\n",
135 (cycles + 4) / 8, blen);
136
137 return ret;
138}
139
140static int test_aead_jiffies(struct aead_request *req, int enc,
141 int blen, int sec)
142{
143 unsigned long start, end;
144 int bcount;
145 int ret;
146
147 for (start = jiffies, end = start + sec * HZ, bcount = 0;
148 time_before(jiffies, end); bcount++) {
149 if (enc)
150 ret = crypto_aead_encrypt(req);
151 else
152 ret = crypto_aead_decrypt(req);
153
154 if (ret)
155 return ret;
156 }
157
158 printk("%d operations in %d seconds (%ld bytes)\n",
159 bcount, sec, (long)bcount * blen);
160 return 0;
161}
162
163static int test_aead_cycles(struct aead_request *req, int enc, int blen)
164{
165 unsigned long cycles = 0;
166 int ret = 0;
167 int i;
168
169 local_irq_disable();
170
171 /* Warm-up run. */
172 for (i = 0; i < 4; i++) {
173 if (enc)
174 ret = crypto_aead_encrypt(req);
175 else
176 ret = crypto_aead_decrypt(req);
177
178 if (ret)
179 goto out;
180 }
181
182 /* The real thing. */
183 for (i = 0; i < 8; i++) {
184 cycles_t start, end;
185
186 start = get_cycles();
187 if (enc)
188 ret = crypto_aead_encrypt(req);
189 else
190 ret = crypto_aead_decrypt(req);
191 end = get_cycles();
192
193 if (ret)
194 goto out;
195
196 cycles += end - start;
197 }
198
199out:
200 local_irq_enable();
201
202 if (ret == 0)
203 printk("1 operation in %lu cycles (%d bytes)\n",
204 (cycles + 4) / 8, blen);
205
206 return ret;
207}
208
209static u32 block_sizes[] = { 16, 64, 256, 1024, 8192, 0 };
210static u32 aead_sizes[] = { 16, 64, 256, 512, 1024, 2048, 4096, 8192, 0 };
211
212#define XBUFSIZE 8
213#define MAX_IVLEN 32
214
215static int testmgr_alloc_buf(char *buf[XBUFSIZE])
216{
217 int i;
218
219 for (i = 0; i < XBUFSIZE; i++) {
220 buf[i] = (void *)__get_free_page(GFP_KERNEL);
221 if (!buf[i])
222 goto err_free_buf;
223 }
224
225 return 0;
226
227err_free_buf:
228 while (i-- > 0)
229 free_page((unsigned long)buf[i]);
230
231 return -ENOMEM;
232}
233
234static void testmgr_free_buf(char *buf[XBUFSIZE])
235{
236 int i;
237
238 for (i = 0; i < XBUFSIZE; i++)
239 free_page((unsigned long)buf[i]);
240}
241
242static void sg_init_aead(struct scatterlist *sg, char *xbuf[XBUFSIZE],
243 unsigned int buflen)
244{
245 int np = (buflen + PAGE_SIZE - 1)/PAGE_SIZE;
246 int k, rem;
247
248 np = (np > XBUFSIZE) ? XBUFSIZE : np;
249 rem = buflen % PAGE_SIZE;
250 if (np > XBUFSIZE) {
251 rem = PAGE_SIZE;
252 np = XBUFSIZE;
253 }
254 sg_init_table(sg, np);
255 for (k = 0; k < np; ++k) {
256 if (k == (np-1))
257 sg_set_buf(&sg[k], xbuf[k], rem);
258 else
259 sg_set_buf(&sg[k], xbuf[k], PAGE_SIZE);
260 }
261}
262
263static void test_aead_speed(const char *algo, int enc, unsigned int sec,
264 struct aead_speed_template *template,
265 unsigned int tcount, u8 authsize,
266 unsigned int aad_size, u8 *keysize)
267{
268 unsigned int i, j;
269 struct crypto_aead *tfm;
270 int ret = -ENOMEM;
271 const char *key;
272 struct aead_request *req;
273 struct scatterlist *sg;
274 struct scatterlist *asg;
275 struct scatterlist *sgout;
276 const char *e;
277 void *assoc;
278 char iv[MAX_IVLEN];
279 char *xbuf[XBUFSIZE];
280 char *xoutbuf[XBUFSIZE];
281 char *axbuf[XBUFSIZE];
282 unsigned int *b_size;
283 unsigned int iv_len;
284
285 if (enc == ENCRYPT)
286 e = "encryption";
287 else
288 e = "decryption";
289
290 if (testmgr_alloc_buf(xbuf))
291 goto out_noxbuf;
292 if (testmgr_alloc_buf(axbuf))
293 goto out_noaxbuf;
294 if (testmgr_alloc_buf(xoutbuf))
295 goto out_nooutbuf;
296
297 sg = kmalloc(sizeof(*sg) * 8 * 3, GFP_KERNEL);
298 if (!sg)
299 goto out_nosg;
300 asg = &sg[8];
301 sgout = &asg[8];
302
303
304 printk(KERN_INFO "\ntesting speed of %s %s\n", algo, e);
305
306 tfm = crypto_alloc_aead(algo, 0, 0);
307
308 if (IS_ERR(tfm)) {
309 pr_err("alg: aead: Failed to load transform for %s: %ld\n", algo,
310 PTR_ERR(tfm));
311 return;
312 }
313
314 req = aead_request_alloc(tfm, GFP_KERNEL);
315 if (!req) {
316 pr_err("alg: aead: Failed to allocate request for %s\n",
317 algo);
318 goto out;
319 }
320
321 i = 0;
322 do {
323 b_size = aead_sizes;
324 do {
325 assoc = axbuf[0];
326
327 if (aad_size < PAGE_SIZE)
328 memset(assoc, 0xff, aad_size);
329 else {
330 pr_err("associate data length (%u) too big\n",
331 aad_size);
332 goto out_nosg;
333 }
334 sg_init_one(&asg[0], assoc, aad_size);
335
336 if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
337 pr_err("template (%u) too big for tvmem (%lu)\n",
338 *keysize + *b_size,
339 TVMEMSIZE * PAGE_SIZE);
340 goto out;
341 }
342
343 key = tvmem[0];
344 for (j = 0; j < tcount; j++) {
345 if (template[j].klen == *keysize) {
346 key = template[j].key;
347 break;
348 }
349 }
350 ret = crypto_aead_setkey(tfm, key, *keysize);
351 ret = crypto_aead_setauthsize(tfm, authsize);
352
353 iv_len = crypto_aead_ivsize(tfm);
354 if (iv_len)
355 memset(&iv, 0xff, iv_len);
356
357 crypto_aead_clear_flags(tfm, ~0);
358 printk(KERN_INFO "test %u (%d bit key, %d byte blocks): ",
359 i, *keysize * 8, *b_size);
360
361
362 memset(tvmem[0], 0xff, PAGE_SIZE);
363
364 if (ret) {
365 pr_err("setkey() failed flags=%x\n",
366 crypto_aead_get_flags(tfm));
367 goto out;
368 }
369
370 sg_init_aead(&sg[0], xbuf,
371 *b_size + (enc ? authsize : 0));
372
373 sg_init_aead(&sgout[0], xoutbuf,
374 *b_size + (enc ? authsize : 0));
375
376 aead_request_set_crypt(req, sg, sgout, *b_size, iv);
377 aead_request_set_assoc(req, asg, aad_size);
378
379 if (sec)
380 ret = test_aead_jiffies(req, enc, *b_size, sec);
381 else
382 ret = test_aead_cycles(req, enc, *b_size);
383
384 if (ret) {
385 pr_err("%s() failed return code=%d\n", e, ret);
386 break;
387 }
388 b_size++;
389 i++;
390 } while (*b_size);
391 keysize++;
392 } while (*keysize);
393
394out:
395 crypto_free_aead(tfm);
396 kfree(sg);
397out_nosg:
398 testmgr_free_buf(xoutbuf);
399out_nooutbuf:
400 testmgr_free_buf(axbuf);
401out_noaxbuf:
402 testmgr_free_buf(xbuf);
403out_noxbuf:
404 return;
405}
406
407static void test_cipher_speed(const char *algo, int enc, unsigned int sec,
408 struct cipher_speed_template *template,
409 unsigned int tcount, u8 *keysize)
410{
411 unsigned int ret, i, j, iv_len;
412 const char *key;
413 char iv[128];
414 struct crypto_blkcipher *tfm;
415 struct blkcipher_desc desc;
416 const char *e;
417 u32 *b_size;
418
419 if (enc == ENCRYPT)
420 e = "encryption";
421 else
422 e = "decryption";
423
424 printk("\ntesting speed of %s %s\n", algo, e);
425
426 tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);
427
428 if (IS_ERR(tfm)) {
429 printk("failed to load transform for %s: %ld\n", algo,
430 PTR_ERR(tfm));
431 return;
432 }
433 desc.tfm = tfm;
434 desc.flags = 0;
435
436 i = 0;
437 do {
438
439 b_size = block_sizes;
440 do {
441 struct scatterlist sg[TVMEMSIZE];
442
443 if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
444 printk("template (%u) too big for "
445 "tvmem (%lu)\n", *keysize + *b_size,
446 TVMEMSIZE * PAGE_SIZE);
447 goto out;
448 }
449
450 printk("test %u (%d bit key, %d byte blocks): ", i,
451 *keysize * 8, *b_size);
452
453 memset(tvmem[0], 0xff, PAGE_SIZE);
454
455 /* set key, plain text and IV */
456 key = tvmem[0];
457 for (j = 0; j < tcount; j++) {
458 if (template[j].klen == *keysize) {
459 key = template[j].key;
460 break;
461 }
462 }
463
464 ret = crypto_blkcipher_setkey(tfm, key, *keysize);
465 if (ret) {
466 printk("setkey() failed flags=%x\n",
467 crypto_blkcipher_get_flags(tfm));
468 goto out;
469 }
470
471 sg_init_table(sg, TVMEMSIZE);
472 sg_set_buf(sg, tvmem[0] + *keysize,
473 PAGE_SIZE - *keysize);
474 for (j = 1; j < TVMEMSIZE; j++) {
475 sg_set_buf(sg + j, tvmem[j], PAGE_SIZE);
476 memset (tvmem[j], 0xff, PAGE_SIZE);
477 }
478
479 iv_len = crypto_blkcipher_ivsize(tfm);
480 if (iv_len) {
481 memset(&iv, 0xff, iv_len);
482 crypto_blkcipher_set_iv(tfm, iv, iv_len);
483 }
484
485 if (sec)
486 ret = test_cipher_jiffies(&desc, enc, sg,
487 *b_size, sec);
488 else
489 ret = test_cipher_cycles(&desc, enc, sg,
490 *b_size);
491
492 if (ret) {
493 printk("%s() failed flags=%x\n", e, desc.flags);
494 break;
495 }
496 b_size++;
497 i++;
498 } while (*b_size);
499 keysize++;
500 } while (*keysize);
501
502out:
503 crypto_free_blkcipher(tfm);
504}
505
506static int test_hash_jiffies_digest(struct hash_desc *desc,
507 struct scatterlist *sg, int blen,
508 char *out, int sec)
509{
510 unsigned long start, end;
511 int bcount;
512 int ret;
513
514 for (start = jiffies, end = start + sec * HZ, bcount = 0;
515 time_before(jiffies, end); bcount++) {
516 ret = crypto_hash_digest(desc, sg, blen, out);
517 if (ret)
518 return ret;
519 }
520
521 printk("%6u opers/sec, %9lu bytes/sec\n",
522 bcount / sec, ((long)bcount * blen) / sec);
523
524 return 0;
525}
526
527static int test_hash_jiffies(struct hash_desc *desc, struct scatterlist *sg,
528 int blen, int plen, char *out, int sec)
529{
530 unsigned long start, end;
531 int bcount, pcount;
532 int ret;
533
534 if (plen == blen)
535 return test_hash_jiffies_digest(desc, sg, blen, out, sec);
536
537 for (start = jiffies, end = start + sec * HZ, bcount = 0;
538 time_before(jiffies, end); bcount++) {
539 ret = crypto_hash_init(desc);
540 if (ret)
541 return ret;
542 for (pcount = 0; pcount < blen; pcount += plen) {
543 ret = crypto_hash_update(desc, sg, plen);
544 if (ret)
545 return ret;
546 }
547 /* we assume there is enough space in 'out' for the result */
548 ret = crypto_hash_final(desc, out);
549 if (ret)
550 return ret;
551 }
552
553 printk("%6u opers/sec, %9lu bytes/sec\n",
554 bcount / sec, ((long)bcount * blen) / sec);
555
556 return 0;
557}
558
559static int test_hash_cycles_digest(struct hash_desc *desc,
560 struct scatterlist *sg, int blen, char *out)
561{
562 unsigned long cycles = 0;
563 int i;
564 int ret;
565
566 local_irq_disable();
567
568 /* Warm-up run. */
569 for (i = 0; i < 4; i++) {
570 ret = crypto_hash_digest(desc, sg, blen, out);
571 if (ret)
572 goto out;
573 }
574
575 /* The real thing. */
576 for (i = 0; i < 8; i++) {
577 cycles_t start, end;
578
579 start = get_cycles();
580
581 ret = crypto_hash_digest(desc, sg, blen, out);
582 if (ret)
583 goto out;
584
585 end = get_cycles();
586
587 cycles += end - start;
588 }
589
590out:
591 local_irq_enable();
592
593 if (ret)
594 return ret;
595
596 printk("%6lu cycles/operation, %4lu cycles/byte\n",
597 cycles / 8, cycles / (8 * blen));
598
599 return 0;
600}
601
602static int test_hash_cycles(struct hash_desc *desc, struct scatterlist *sg,
603 int blen, int plen, char *out)
604{
605 unsigned long cycles = 0;
606 int i, pcount;
607 int ret;
608
609 if (plen == blen)
610 return test_hash_cycles_digest(desc, sg, blen, out);
611
612 local_irq_disable();
613
614 /* Warm-up run. */
615 for (i = 0; i < 4; i++) {
616 ret = crypto_hash_init(desc);
617 if (ret)
618 goto out;
619 for (pcount = 0; pcount < blen; pcount += plen) {
620 ret = crypto_hash_update(desc, sg, plen);
621 if (ret)
622 goto out;
623 }
624 ret = crypto_hash_final(desc, out);
625 if (ret)
626 goto out;
627 }
628
629 /* The real thing. */
630 for (i = 0; i < 8; i++) {
631 cycles_t start, end;
632
633 start = get_cycles();
634
635 ret = crypto_hash_init(desc);
636 if (ret)
637 goto out;
638 for (pcount = 0; pcount < blen; pcount += plen) {
639 ret = crypto_hash_update(desc, sg, plen);
640 if (ret)
641 goto out;
642 }
643 ret = crypto_hash_final(desc, out);
644 if (ret)
645 goto out;
646
647 end = get_cycles();
648
649 cycles += end - start;
650 }
651
652out:
653 local_irq_enable();
654
655 if (ret)
656 return ret;
657
658 printk("%6lu cycles/operation, %4lu cycles/byte\n",
659 cycles / 8, cycles / (8 * blen));
660
661 return 0;
662}
663
664static void test_hash_sg_init(struct scatterlist *sg)
665{
666 int i;
667
668 sg_init_table(sg, TVMEMSIZE);
669 for (i = 0; i < TVMEMSIZE; i++) {
670 sg_set_buf(sg + i, tvmem[i], PAGE_SIZE);
671 memset(tvmem[i], 0xff, PAGE_SIZE);
672 }
673}
674
675static void test_hash_speed(const char *algo, unsigned int sec,
676 struct hash_speed *speed)
677{
678 struct scatterlist sg[TVMEMSIZE];
679 struct crypto_hash *tfm;
680 struct hash_desc desc;
681 static char output[1024];
682 int i;
683 int ret;
684
685 printk(KERN_INFO "\ntesting speed of %s\n", algo);
686
687 tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
688
689 if (IS_ERR(tfm)) {
690 printk(KERN_ERR "failed to load transform for %s: %ld\n", algo,
691 PTR_ERR(tfm));
692 return;
693 }
694
695 desc.tfm = tfm;
696 desc.flags = 0;
697
698 if (crypto_hash_digestsize(tfm) > sizeof(output)) {
699 printk(KERN_ERR "digestsize(%u) > outputbuffer(%zu)\n",
700 crypto_hash_digestsize(tfm), sizeof(output));
701 goto out;
702 }
703
704 test_hash_sg_init(sg);
705 for (i = 0; speed[i].blen != 0; i++) {
706 if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
707 printk(KERN_ERR
708 "template (%u) too big for tvmem (%lu)\n",
709 speed[i].blen, TVMEMSIZE * PAGE_SIZE);
710 goto out;
711 }
712
713 if (speed[i].klen)
714 crypto_hash_setkey(tfm, tvmem[0], speed[i].klen);
715
716 printk(KERN_INFO "test%3u "
717 "(%5u byte blocks,%5u bytes per update,%4u updates): ",
718 i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
719
720 if (sec)
721 ret = test_hash_jiffies(&desc, sg, speed[i].blen,
722 speed[i].plen, output, sec);
723 else
724 ret = test_hash_cycles(&desc, sg, speed[i].blen,
725 speed[i].plen, output);
726
727 if (ret) {
728 printk(KERN_ERR "hashing failed ret=%d\n", ret);
729 break;
730 }
731 }
732
733out:
734 crypto_free_hash(tfm);
735}
736
737struct tcrypt_result {
738 struct completion completion;
739 int err;
740};
741
742static void tcrypt_complete(struct crypto_async_request *req, int err)
743{
744 struct tcrypt_result *res = req->data;
745
746 if (err == -EINPROGRESS)
747 return;
748
749 res->err = err;
750 complete(&res->completion);
751}
752
753static inline int do_one_ahash_op(struct ahash_request *req, int ret)
754{
755 if (ret == -EINPROGRESS || ret == -EBUSY) {
756 struct tcrypt_result *tr = req->base.data;
757
758 ret = wait_for_completion_interruptible(&tr->completion);
759 if (!ret)
760 ret = tr->err;
761 reinit_completion(&tr->completion);
762 }
763 return ret;
764}
765
766static int test_ahash_jiffies_digest(struct ahash_request *req, int blen,
767 char *out, int sec)
768{
769 unsigned long start, end;
770 int bcount;
771 int ret;
772
773 for (start = jiffies, end = start + sec * HZ, bcount = 0;
774 time_before(jiffies, end); bcount++) {
775 ret = do_one_ahash_op(req, crypto_ahash_digest(req));
776 if (ret)
777 return ret;
778 }
779
780 printk("%6u opers/sec, %9lu bytes/sec\n",
781 bcount / sec, ((long)bcount * blen) / sec);
782
783 return 0;
784}
785
786static int test_ahash_jiffies(struct ahash_request *req, int blen,
787 int plen, char *out, int sec)
788{
789 unsigned long start, end;
790 int bcount, pcount;
791 int ret;
792
793 if (plen == blen)
794 return test_ahash_jiffies_digest(req, blen, out, sec);
795
796 for (start = jiffies, end = start + sec * HZ, bcount = 0;
797 time_before(jiffies, end); bcount++) {
798 ret = crypto_ahash_init(req);
799 if (ret)
800 return ret;
801 for (pcount = 0; pcount < blen; pcount += plen) {
802 ret = do_one_ahash_op(req, crypto_ahash_update(req));
803 if (ret)
804 return ret;
805 }
806 /* we assume there is enough space in 'out' for the result */
807 ret = do_one_ahash_op(req, crypto_ahash_final(req));
808 if (ret)
809 return ret;
810 }
811
812 pr_cont("%6u opers/sec, %9lu bytes/sec\n",
813 bcount / sec, ((long)bcount * blen) / sec);
814
815 return 0;
816}
817
818static int test_ahash_cycles_digest(struct ahash_request *req, int blen,
819 char *out)
820{
821 unsigned long cycles = 0;
822 int ret, i;
823
824 /* Warm-up run. */
825 for (i = 0; i < 4; i++) {
826 ret = do_one_ahash_op(req, crypto_ahash_digest(req));
827 if (ret)
828 goto out;
829 }
830
831 /* The real thing. */
832 for (i = 0; i < 8; i++) {
833 cycles_t start, end;
834
835 start = get_cycles();
836
837 ret = do_one_ahash_op(req, crypto_ahash_digest(req));
838 if (ret)
839 goto out;
840
841 end = get_cycles();
842
843 cycles += end - start;
844 }
845
846out:
847 if (ret)
848 return ret;
849
850 pr_cont("%6lu cycles/operation, %4lu cycles/byte\n",
851 cycles / 8, cycles / (8 * blen));
852
853 return 0;
854}
855
856static int test_ahash_cycles(struct ahash_request *req, int blen,
857 int plen, char *out)
858{
859 unsigned long cycles = 0;
860 int i, pcount, ret;
861
862 if (plen == blen)
863 return test_ahash_cycles_digest(req, blen, out);
864
865 /* Warm-up run. */
866 for (i = 0; i < 4; i++) {
867 ret = crypto_ahash_init(req);
868 if (ret)
869 goto out;
870 for (pcount = 0; pcount < blen; pcount += plen) {
871 ret = do_one_ahash_op(req, crypto_ahash_update(req));
872 if (ret)
873 goto out;
874 }
875 ret = do_one_ahash_op(req, crypto_ahash_final(req));
876 if (ret)
877 goto out;
878 }
879
880 /* The real thing. */
881 for (i = 0; i < 8; i++) {
882 cycles_t start, end;
883
884 start = get_cycles();
885
886 ret = crypto_ahash_init(req);
887 if (ret)
888 goto out;
889 for (pcount = 0; pcount < blen; pcount += plen) {
890 ret = do_one_ahash_op(req, crypto_ahash_update(req));
891 if (ret)
892 goto out;
893 }
894 ret = do_one_ahash_op(req, crypto_ahash_final(req));
895 if (ret)
896 goto out;
897
898 end = get_cycles();
899
900 cycles += end - start;
901 }
902
903out:
904 if (ret)
905 return ret;
906
907 pr_cont("%6lu cycles/operation, %4lu cycles/byte\n",
908 cycles / 8, cycles / (8 * blen));
909
910 return 0;
911}
912
913static void test_ahash_speed(const char *algo, unsigned int sec,
914 struct hash_speed *speed)
915{
916 struct scatterlist sg[TVMEMSIZE];
917 struct tcrypt_result tresult;
918 struct ahash_request *req;
919 struct crypto_ahash *tfm;
920 static char output[1024];
921 int i, ret;
922
923 printk(KERN_INFO "\ntesting speed of async %s\n", algo);
924
925 tfm = crypto_alloc_ahash(algo, 0, 0);
926 if (IS_ERR(tfm)) {
927 pr_err("failed to load transform for %s: %ld\n",
928 algo, PTR_ERR(tfm));
929 return;
930 }
931
932 if (crypto_ahash_digestsize(tfm) > sizeof(output)) {
933 pr_err("digestsize(%u) > outputbuffer(%zu)\n",
934 crypto_ahash_digestsize(tfm), sizeof(output));
935 goto out;
936 }
937
938 test_hash_sg_init(sg);
939 req = ahash_request_alloc(tfm, GFP_KERNEL);
940 if (!req) {
941 pr_err("ahash request allocation failure\n");
942 goto out;
943 }
944
945 init_completion(&tresult.completion);
946 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
947 tcrypt_complete, &tresult);
948
949 for (i = 0; speed[i].blen != 0; i++) {
950 if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
951 pr_err("template (%u) too big for tvmem (%lu)\n",
952 speed[i].blen, TVMEMSIZE * PAGE_SIZE);
953 break;
954 }
955
956 pr_info("test%3u "
957 "(%5u byte blocks,%5u bytes per update,%4u updates): ",
958 i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
959
960 ahash_request_set_crypt(req, sg, output, speed[i].plen);
961
962 if (sec)
963 ret = test_ahash_jiffies(req, speed[i].blen,
964 speed[i].plen, output, sec);
965 else
966 ret = test_ahash_cycles(req, speed[i].blen,
967 speed[i].plen, output);
968
969 if (ret) {
970 pr_err("hashing failed ret=%d\n", ret);
971 break;
972 }
973 }
974
975 ahash_request_free(req);
976
977out:
978 crypto_free_ahash(tfm);
979}
980
981static inline int do_one_acipher_op(struct ablkcipher_request *req, int ret)
982{
983 if (ret == -EINPROGRESS || ret == -EBUSY) {
984 struct tcrypt_result *tr = req->base.data;
985
986 ret = wait_for_completion_interruptible(&tr->completion);
987 if (!ret)
988 ret = tr->err;
989 reinit_completion(&tr->completion);
990 }
991
992 return ret;
993}
994
995static int test_acipher_jiffies(struct ablkcipher_request *req, int enc,
996 int blen, int sec)
997{
998 unsigned long start, end;
999 int bcount;
1000 int ret;
1001
1002 for (start = jiffies, end = start + sec * HZ, bcount = 0;
1003 time_before(jiffies, end); bcount++) {
1004 if (enc)
1005 ret = do_one_acipher_op(req,
1006 crypto_ablkcipher_encrypt(req));
1007 else
1008 ret = do_one_acipher_op(req,
1009 crypto_ablkcipher_decrypt(req));
1010
1011 if (ret)
1012 return ret;
1013 }
1014
1015 pr_cont("%d operations in %d seconds (%ld bytes)\n",
1016 bcount, sec, (long)bcount * blen);
1017 return 0;
1018}
1019
1020static int test_acipher_cycles(struct ablkcipher_request *req, int enc,
1021 int blen)
1022{
1023 unsigned long cycles = 0;
1024 int ret = 0;
1025 int i;
1026
1027 /* Warm-up run. */
1028 for (i = 0; i < 4; i++) {
1029 if (enc)
1030 ret = do_one_acipher_op(req,
1031 crypto_ablkcipher_encrypt(req));
1032 else
1033 ret = do_one_acipher_op(req,
1034 crypto_ablkcipher_decrypt(req));
1035
1036 if (ret)
1037 goto out;
1038 }
1039
1040 /* The real thing. */
1041 for (i = 0; i < 8; i++) {
1042 cycles_t start, end;
1043
1044 start = get_cycles();
1045 if (enc)
1046 ret = do_one_acipher_op(req,
1047 crypto_ablkcipher_encrypt(req));
1048 else
1049 ret = do_one_acipher_op(req,
1050 crypto_ablkcipher_decrypt(req));
1051 end = get_cycles();
1052
1053 if (ret)
1054 goto out;
1055
1056 cycles += end - start;
1057 }
1058
1059out:
1060 if (ret == 0)
1061 pr_cont("1 operation in %lu cycles (%d bytes)\n",
1062 (cycles + 4) / 8, blen);
1063
1064 return ret;
1065}
1066
1067static void test_acipher_speed(const char *algo, int enc, unsigned int sec,
1068 struct cipher_speed_template *template,
1069 unsigned int tcount, u8 *keysize)
1070{
1071 unsigned int ret, i, j, k, iv_len;
1072 struct tcrypt_result tresult;
1073 const char *key;
1074 char iv[128];
1075 struct ablkcipher_request *req;
1076 struct crypto_ablkcipher *tfm;
1077 const char *e;
1078 u32 *b_size;
1079
1080 if (enc == ENCRYPT)
1081 e = "encryption";
1082 else
1083 e = "decryption";
1084
1085 pr_info("\ntesting speed of async %s %s\n", algo, e);
1086
1087 init_completion(&tresult.completion);
1088
1089 tfm = crypto_alloc_ablkcipher(algo, 0, 0);
1090
1091 if (IS_ERR(tfm)) {
1092 pr_err("failed to load transform for %s: %ld\n", algo,
1093 PTR_ERR(tfm));
1094 return;
1095 }
1096
1097 req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
1098 if (!req) {
1099 pr_err("tcrypt: skcipher: Failed to allocate request for %s\n",
1100 algo);
1101 goto out;
1102 }
1103
1104 ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1105 tcrypt_complete, &tresult);
1106
1107 i = 0;
1108 do {
1109 b_size = block_sizes;
1110
1111 do {
1112 struct scatterlist sg[TVMEMSIZE];
1113
1114 if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
1115 pr_err("template (%u) too big for "
1116 "tvmem (%lu)\n", *keysize + *b_size,
1117 TVMEMSIZE * PAGE_SIZE);
1118 goto out_free_req;
1119 }
1120
1121 pr_info("test %u (%d bit key, %d byte blocks): ", i,
1122 *keysize * 8, *b_size);
1123
1124 memset(tvmem[0], 0xff, PAGE_SIZE);
1125
1126 /* set key, plain text and IV */
1127 key = tvmem[0];
1128 for (j = 0; j < tcount; j++) {
1129 if (template[j].klen == *keysize) {
1130 key = template[j].key;
1131 break;
1132 }
1133 }
1134
1135 crypto_ablkcipher_clear_flags(tfm, ~0);
1136
1137 ret = crypto_ablkcipher_setkey(tfm, key, *keysize);
1138 if (ret) {
1139 pr_err("setkey() failed flags=%x\n",
1140 crypto_ablkcipher_get_flags(tfm));
1141 goto out_free_req;
1142 }
1143
1144 sg_init_table(sg, TVMEMSIZE);
1145
1146 k = *keysize + *b_size;
1147 if (k > PAGE_SIZE) {
1148 sg_set_buf(sg, tvmem[0] + *keysize,
1149 PAGE_SIZE - *keysize);
1150 k -= PAGE_SIZE;
1151 j = 1;
1152 while (k > PAGE_SIZE) {
1153 sg_set_buf(sg + j, tvmem[j], PAGE_SIZE);
1154 memset(tvmem[j], 0xff, PAGE_SIZE);
1155 j++;
1156 k -= PAGE_SIZE;
1157 }
1158 sg_set_buf(sg + j, tvmem[j], k);
1159 memset(tvmem[j], 0xff, k);
1160 } else {
1161 sg_set_buf(sg, tvmem[0] + *keysize, *b_size);
1162 }
1163
1164 iv_len = crypto_ablkcipher_ivsize(tfm);
1165 if (iv_len)
1166 memset(&iv, 0xff, iv_len);
1167
1168 ablkcipher_request_set_crypt(req, sg, sg, *b_size, iv);
1169
1170 if (sec)
1171 ret = test_acipher_jiffies(req, enc,
1172 *b_size, sec);
1173 else
1174 ret = test_acipher_cycles(req, enc,
1175 *b_size);
1176
1177 if (ret) {
1178 pr_err("%s() failed flags=%x\n", e,
1179 crypto_ablkcipher_get_flags(tfm));
1180 break;
1181 }
1182 b_size++;
1183 i++;
1184 } while (*b_size);
1185 keysize++;
1186 } while (*keysize);
1187
1188out_free_req:
1189 ablkcipher_request_free(req);
1190out:
1191 crypto_free_ablkcipher(tfm);
1192}
1193
1194static void test_available(void)
1195{
1196 char **name = check;
1197
1198 while (*name) {
1199 printk("alg %s ", *name);
1200 printk(crypto_has_alg(*name, 0, 0) ?
1201 "found\n" : "not found\n");
1202 name++;
1203 }
1204}
1205
1206static inline int tcrypt_test(const char *alg)
1207{
1208 int ret;
1209
1210 ret = alg_test(alg, alg, 0, 0);
1211 /* non-fips algs return -EINVAL in fips mode */
1212 if (fips_enabled && ret == -EINVAL)
1213 ret = 0;
1214 return ret;
1215}
1216
1217static int do_test(int m)
1218{
1219 int i;
1220 int ret = 0;
1221
1222 switch (m) {
1223 case 0:
1224 for (i = 1; i < 200; i++)
1225 ret += do_test(i);
1226 break;
1227
1228 case 1:
1229 ret += tcrypt_test("md5");
1230 break;
1231
1232 case 2:
1233 ret += tcrypt_test("sha1");
1234 break;
1235
1236 case 3:
1237 ret += tcrypt_test("ecb(des)");
1238 ret += tcrypt_test("cbc(des)");
1239 ret += tcrypt_test("ctr(des)");
1240 break;
1241
1242 case 4:
1243 ret += tcrypt_test("ecb(des3_ede)");
1244 ret += tcrypt_test("cbc(des3_ede)");
1245 ret += tcrypt_test("ctr(des3_ede)");
1246 break;
1247
1248 case 5:
1249 ret += tcrypt_test("md4");
1250 break;
1251
1252 case 6:
1253 ret += tcrypt_test("sha256");
1254 break;
1255
1256 case 7:
1257 ret += tcrypt_test("ecb(blowfish)");
1258 ret += tcrypt_test("cbc(blowfish)");
1259 ret += tcrypt_test("ctr(blowfish)");
1260 break;
1261
1262 case 8:
1263 ret += tcrypt_test("ecb(twofish)");
1264 ret += tcrypt_test("cbc(twofish)");
1265 ret += tcrypt_test("ctr(twofish)");
1266 ret += tcrypt_test("lrw(twofish)");
1267 ret += tcrypt_test("xts(twofish)");
1268 break;
1269
1270 case 9:
1271 ret += tcrypt_test("ecb(serpent)");
1272 ret += tcrypt_test("cbc(serpent)");
1273 ret += tcrypt_test("ctr(serpent)");
1274 ret += tcrypt_test("lrw(serpent)");
1275 ret += tcrypt_test("xts(serpent)");
1276 break;
1277
1278 case 10:
1279 ret += tcrypt_test("ecb(aes)");
1280 ret += tcrypt_test("cbc(aes)");
1281 ret += tcrypt_test("lrw(aes)");
1282 ret += tcrypt_test("xts(aes)");
1283 ret += tcrypt_test("ctr(aes)");
1284 ret += tcrypt_test("rfc3686(ctr(aes))");
1285 break;
1286
1287 case 11:
1288 ret += tcrypt_test("sha384");
1289 break;
1290
1291 case 12:
1292 ret += tcrypt_test("sha512");
1293 break;
1294
1295 case 13:
1296 ret += tcrypt_test("deflate");
1297 break;
1298
1299 case 14:
1300 ret += tcrypt_test("ecb(cast5)");
1301 ret += tcrypt_test("cbc(cast5)");
1302 ret += tcrypt_test("ctr(cast5)");
1303 break;
1304
1305 case 15:
1306 ret += tcrypt_test("ecb(cast6)");
1307 ret += tcrypt_test("cbc(cast6)");
1308 ret += tcrypt_test("ctr(cast6)");
1309 ret += tcrypt_test("lrw(cast6)");
1310 ret += tcrypt_test("xts(cast6)");
1311 break;
1312
1313 case 16:
1314 ret += tcrypt_test("ecb(arc4)");
1315 break;
1316
1317 case 17:
1318 ret += tcrypt_test("michael_mic");
1319 break;
1320
1321 case 18:
1322 ret += tcrypt_test("crc32c");
1323 break;
1324
1325 case 19:
1326 ret += tcrypt_test("ecb(tea)");
1327 break;
1328
1329 case 20:
1330 ret += tcrypt_test("ecb(xtea)");
1331 break;
1332
1333 case 21:
1334 ret += tcrypt_test("ecb(khazad)");
1335 break;
1336
1337 case 22:
1338 ret += tcrypt_test("wp512");
1339 break;
1340
1341 case 23:
1342 ret += tcrypt_test("wp384");
1343 break;
1344
1345 case 24:
1346 ret += tcrypt_test("wp256");
1347 break;
1348
1349 case 25:
1350 ret += tcrypt_test("ecb(tnepres)");
1351 break;
1352
1353 case 26:
1354 ret += tcrypt_test("ecb(anubis)");
1355 ret += tcrypt_test("cbc(anubis)");
1356 break;
1357
1358 case 27:
1359 ret += tcrypt_test("tgr192");
1360 break;
1361
1362 case 28:
1363 ret += tcrypt_test("tgr160");
1364 break;
1365
1366 case 29:
1367 ret += tcrypt_test("tgr128");
1368 break;
1369
1370 case 30:
1371 ret += tcrypt_test("ecb(xeta)");
1372 break;
1373
1374 case 31:
1375 ret += tcrypt_test("pcbc(fcrypt)");
1376 break;
1377
1378 case 32:
1379 ret += tcrypt_test("ecb(camellia)");
1380 ret += tcrypt_test("cbc(camellia)");
1381 ret += tcrypt_test("ctr(camellia)");
1382 ret += tcrypt_test("lrw(camellia)");
1383 ret += tcrypt_test("xts(camellia)");
1384 break;
1385
1386 case 33:
1387 ret += tcrypt_test("sha224");
1388 break;
1389
1390 case 34:
1391 ret += tcrypt_test("salsa20");
1392 break;
1393
1394 case 35:
1395 ret += tcrypt_test("gcm(aes)");
1396 break;
1397
1398 case 36:
1399 ret += tcrypt_test("lzo");
1400 break;
1401
1402 case 37:
1403 ret += tcrypt_test("ccm(aes)");
1404 break;
1405
1406 case 38:
1407 ret += tcrypt_test("cts(cbc(aes))");
1408 break;
1409
1410 case 39:
1411 ret += tcrypt_test("rmd128");
1412 break;
1413
1414 case 40:
1415 ret += tcrypt_test("rmd160");
1416 break;
1417
1418 case 41:
1419 ret += tcrypt_test("rmd256");
1420 break;
1421
1422 case 42:
1423 ret += tcrypt_test("rmd320");
1424 break;
1425
1426 case 43:
1427 ret += tcrypt_test("ecb(seed)");
1428 break;
1429
1430 case 44:
1431 ret += tcrypt_test("zlib");
1432 break;
1433
1434 case 45:
1435 ret += tcrypt_test("rfc4309(ccm(aes))");
1436 break;
1437
1438 case 46:
1439 ret += tcrypt_test("ghash");
1440 break;
1441
1442 case 47:
1443 ret += tcrypt_test("crct10dif");
1444 break;
1445
1446 case 100:
1447 ret += tcrypt_test("hmac(md5)");
1448 break;
1449
1450 case 101:
1451 ret += tcrypt_test("hmac(sha1)");
1452 break;
1453
1454 case 102:
1455 ret += tcrypt_test("hmac(sha256)");
1456 break;
1457
1458 case 103:
1459 ret += tcrypt_test("hmac(sha384)");
1460 break;
1461
1462 case 104:
1463 ret += tcrypt_test("hmac(sha512)");
1464 break;
1465
1466 case 105:
1467 ret += tcrypt_test("hmac(sha224)");
1468 break;
1469
1470 case 106:
1471 ret += tcrypt_test("xcbc(aes)");
1472 break;
1473
1474 case 107:
1475 ret += tcrypt_test("hmac(rmd128)");
1476 break;
1477
1478 case 108:
1479 ret += tcrypt_test("hmac(rmd160)");
1480 break;
1481
1482 case 109:
1483 ret += tcrypt_test("vmac(aes)");
1484 break;
1485
1486 case 110:
1487 ret += tcrypt_test("hmac(crc32)");
1488 break;
1489
1490 case 150:
1491 ret += tcrypt_test("ansi_cprng");
1492 break;
1493
1494 case 151:
1495 ret += tcrypt_test("rfc4106(gcm(aes))");
1496 break;
1497
1498 case 152:
1499 ret += tcrypt_test("rfc4543(gcm(aes))");
1500 break;
1501
1502 case 153:
1503 ret += tcrypt_test("cmac(aes)");
1504 break;
1505
1506 case 154:
1507 ret += tcrypt_test("cmac(des3_ede)");
1508 break;
1509
1510 case 155:
1511 ret += tcrypt_test("authenc(hmac(sha1),cbc(aes))");
1512 break;
1513
1514 case 156:
1515 ret += tcrypt_test("authenc(hmac(md5),ecb(cipher_null))");
1516 break;
1517
1518 case 157:
1519 ret += tcrypt_test("authenc(hmac(sha1),ecb(cipher_null))");
1520 break;
1521
1522 case 200:
1523 test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
1524 speed_template_16_24_32);
1525 test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
1526 speed_template_16_24_32);
1527 test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
1528 speed_template_16_24_32);
1529 test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
1530 speed_template_16_24_32);
1531 test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
1532 speed_template_32_40_48);
1533 test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
1534 speed_template_32_40_48);
1535 test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
1536 speed_template_32_48_64);
1537 test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
1538 speed_template_32_48_64);
1539 test_cipher_speed("ctr(aes)", ENCRYPT, sec, NULL, 0,
1540 speed_template_16_24_32);
1541 test_cipher_speed("ctr(aes)", DECRYPT, sec, NULL, 0,
1542 speed_template_16_24_32);
1543 break;
1544
1545 case 201:
1546 test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
1547 des3_speed_template, DES3_SPEED_VECTORS,
1548 speed_template_24);
1549 test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
1550 des3_speed_template, DES3_SPEED_VECTORS,
1551 speed_template_24);
1552 test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
1553 des3_speed_template, DES3_SPEED_VECTORS,
1554 speed_template_24);
1555 test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
1556 des3_speed_template, DES3_SPEED_VECTORS,
1557 speed_template_24);
1558 break;
1559
1560 case 202:
1561 test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
1562 speed_template_16_24_32);
1563 test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
1564 speed_template_16_24_32);
1565 test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
1566 speed_template_16_24_32);
1567 test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
1568 speed_template_16_24_32);
1569 test_cipher_speed("ctr(twofish)", ENCRYPT, sec, NULL, 0,
1570 speed_template_16_24_32);
1571 test_cipher_speed("ctr(twofish)", DECRYPT, sec, NULL, 0,
1572 speed_template_16_24_32);
1573 test_cipher_speed("lrw(twofish)", ENCRYPT, sec, NULL, 0,
1574 speed_template_32_40_48);
1575 test_cipher_speed("lrw(twofish)", DECRYPT, sec, NULL, 0,
1576 speed_template_32_40_48);
1577 test_cipher_speed("xts(twofish)", ENCRYPT, sec, NULL, 0,
1578 speed_template_32_48_64);
1579 test_cipher_speed("xts(twofish)", DECRYPT, sec, NULL, 0,
1580 speed_template_32_48_64);
1581 break;
1582
1583 case 203:
1584 test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
1585 speed_template_8_32);
1586 test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
1587 speed_template_8_32);
1588 test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
1589 speed_template_8_32);
1590 test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
1591 speed_template_8_32);
1592 test_cipher_speed("ctr(blowfish)", ENCRYPT, sec, NULL, 0,
1593 speed_template_8_32);
1594 test_cipher_speed("ctr(blowfish)", DECRYPT, sec, NULL, 0,
1595 speed_template_8_32);
1596 break;
1597
1598 case 204:
1599 test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
1600 speed_template_8);
1601 test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
1602 speed_template_8);
1603 test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
1604 speed_template_8);
1605 test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
1606 speed_template_8);
1607 break;
1608
1609 case 205:
1610 test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
1611 speed_template_16_24_32);
1612 test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
1613 speed_template_16_24_32);
1614 test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
1615 speed_template_16_24_32);
1616 test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
1617 speed_template_16_24_32);
1618 test_cipher_speed("ctr(camellia)", ENCRYPT, sec, NULL, 0,
1619 speed_template_16_24_32);
1620 test_cipher_speed("ctr(camellia)", DECRYPT, sec, NULL, 0,
1621 speed_template_16_24_32);
1622 test_cipher_speed("lrw(camellia)", ENCRYPT, sec, NULL, 0,
1623 speed_template_32_40_48);
1624 test_cipher_speed("lrw(camellia)", DECRYPT, sec, NULL, 0,
1625 speed_template_32_40_48);
1626 test_cipher_speed("xts(camellia)", ENCRYPT, sec, NULL, 0,
1627 speed_template_32_48_64);
1628 test_cipher_speed("xts(camellia)", DECRYPT, sec, NULL, 0,
1629 speed_template_32_48_64);
1630 break;
1631
1632 case 206:
1633 test_cipher_speed("salsa20", ENCRYPT, sec, NULL, 0,
1634 speed_template_16_32);
1635 break;
1636
1637 case 207:
1638 test_cipher_speed("ecb(serpent)", ENCRYPT, sec, NULL, 0,
1639 speed_template_16_32);
1640 test_cipher_speed("ecb(serpent)", DECRYPT, sec, NULL, 0,
1641 speed_template_16_32);
1642 test_cipher_speed("cbc(serpent)", ENCRYPT, sec, NULL, 0,
1643 speed_template_16_32);
1644 test_cipher_speed("cbc(serpent)", DECRYPT, sec, NULL, 0,
1645 speed_template_16_32);
1646 test_cipher_speed("ctr(serpent)", ENCRYPT, sec, NULL, 0,
1647 speed_template_16_32);
1648 test_cipher_speed("ctr(serpent)", DECRYPT, sec, NULL, 0,
1649 speed_template_16_32);
1650 test_cipher_speed("lrw(serpent)", ENCRYPT, sec, NULL, 0,
1651 speed_template_32_48);
1652 test_cipher_speed("lrw(serpent)", DECRYPT, sec, NULL, 0,
1653 speed_template_32_48);
1654 test_cipher_speed("xts(serpent)", ENCRYPT, sec, NULL, 0,
1655 speed_template_32_64);
1656 test_cipher_speed("xts(serpent)", DECRYPT, sec, NULL, 0,
1657 speed_template_32_64);
1658 break;
1659
1660 case 208:
1661 test_cipher_speed("ecb(arc4)", ENCRYPT, sec, NULL, 0,
1662 speed_template_8);
1663 break;
1664
1665 case 209:
1666 test_cipher_speed("ecb(cast5)", ENCRYPT, sec, NULL, 0,
1667 speed_template_8_16);
1668 test_cipher_speed("ecb(cast5)", DECRYPT, sec, NULL, 0,
1669 speed_template_8_16);
1670 test_cipher_speed("cbc(cast5)", ENCRYPT, sec, NULL, 0,
1671 speed_template_8_16);
1672 test_cipher_speed("cbc(cast5)", DECRYPT, sec, NULL, 0,
1673 speed_template_8_16);
1674 test_cipher_speed("ctr(cast5)", ENCRYPT, sec, NULL, 0,
1675 speed_template_8_16);
1676 test_cipher_speed("ctr(cast5)", DECRYPT, sec, NULL, 0,
1677 speed_template_8_16);
1678 break;
1679
1680 case 210:
1681 test_cipher_speed("ecb(cast6)", ENCRYPT, sec, NULL, 0,
1682 speed_template_16_32);
1683 test_cipher_speed("ecb(cast6)", DECRYPT, sec, NULL, 0,
1684 speed_template_16_32);
1685 test_cipher_speed("cbc(cast6)", ENCRYPT, sec, NULL, 0,
1686 speed_template_16_32);
1687 test_cipher_speed("cbc(cast6)", DECRYPT, sec, NULL, 0,
1688 speed_template_16_32);
1689 test_cipher_speed("ctr(cast6)", ENCRYPT, sec, NULL, 0,
1690 speed_template_16_32);
1691 test_cipher_speed("ctr(cast6)", DECRYPT, sec, NULL, 0,
1692 speed_template_16_32);
1693 test_cipher_speed("lrw(cast6)", ENCRYPT, sec, NULL, 0,
1694 speed_template_32_48);
1695 test_cipher_speed("lrw(cast6)", DECRYPT, sec, NULL, 0,
1696 speed_template_32_48);
1697 test_cipher_speed("xts(cast6)", ENCRYPT, sec, NULL, 0,
1698 speed_template_32_64);
1699 test_cipher_speed("xts(cast6)", DECRYPT, sec, NULL, 0,
1700 speed_template_32_64);
1701 break;
1702
1703 case 211:
1704 test_aead_speed("rfc4106(gcm(aes))", ENCRYPT, sec,
1705 NULL, 0, 16, 8, aead_speed_template_20);
1706 break;
1707
1708 case 300:
1709 /* fall through */
1710
1711 case 301:
1712 test_hash_speed("md4", sec, generic_hash_speed_template);
1713 if (mode > 300 && mode < 400) break;
1714
1715 case 302:
1716 test_hash_speed("md5", sec, generic_hash_speed_template);
1717 if (mode > 300 && mode < 400) break;
1718
1719 case 303:
1720 test_hash_speed("sha1", sec, generic_hash_speed_template);
1721 if (mode > 300 && mode < 400) break;
1722
1723 case 304:
1724 test_hash_speed("sha256", sec, generic_hash_speed_template);
1725 if (mode > 300 && mode < 400) break;
1726
1727 case 305:
1728 test_hash_speed("sha384", sec, generic_hash_speed_template);
1729 if (mode > 300 && mode < 400) break;
1730
1731 case 306:
1732 test_hash_speed("sha512", sec, generic_hash_speed_template);
1733 if (mode > 300 && mode < 400) break;
1734
1735 case 307:
1736 test_hash_speed("wp256", sec, generic_hash_speed_template);
1737 if (mode > 300 && mode < 400) break;
1738
1739 case 308:
1740 test_hash_speed("wp384", sec, generic_hash_speed_template);
1741 if (mode > 300 && mode < 400) break;
1742
1743 case 309:
1744 test_hash_speed("wp512", sec, generic_hash_speed_template);
1745 if (mode > 300 && mode < 400) break;
1746
1747 case 310:
1748 test_hash_speed("tgr128", sec, generic_hash_speed_template);
1749 if (mode > 300 && mode < 400) break;
1750
1751 case 311:
1752 test_hash_speed("tgr160", sec, generic_hash_speed_template);
1753 if (mode > 300 && mode < 400) break;
1754
1755 case 312:
1756 test_hash_speed("tgr192", sec, generic_hash_speed_template);
1757 if (mode > 300 && mode < 400) break;
1758
1759 case 313:
1760 test_hash_speed("sha224", sec, generic_hash_speed_template);
1761 if (mode > 300 && mode < 400) break;
1762
1763 case 314:
1764 test_hash_speed("rmd128", sec, generic_hash_speed_template);
1765 if (mode > 300 && mode < 400) break;
1766
1767 case 315:
1768 test_hash_speed("rmd160", sec, generic_hash_speed_template);
1769 if (mode > 300 && mode < 400) break;
1770
1771 case 316:
1772 test_hash_speed("rmd256", sec, generic_hash_speed_template);
1773 if (mode > 300 && mode < 400) break;
1774
1775 case 317:
1776 test_hash_speed("rmd320", sec, generic_hash_speed_template);
1777 if (mode > 300 && mode < 400) break;
1778
1779 case 318:
1780 test_hash_speed("ghash-generic", sec, hash_speed_template_16);
1781 if (mode > 300 && mode < 400) break;
1782
1783 case 319:
1784 test_hash_speed("crc32c", sec, generic_hash_speed_template);
1785 if (mode > 300 && mode < 400) break;
1786
1787 case 320:
1788 test_hash_speed("crct10dif", sec, generic_hash_speed_template);
1789 if (mode > 300 && mode < 400) break;
1790
1791 case 399:
1792 break;
1793
1794 case 400:
1795 /* fall through */
1796
1797 case 401:
1798 test_ahash_speed("md4", sec, generic_hash_speed_template);
1799 if (mode > 400 && mode < 500) break;
1800
1801 case 402:
1802 test_ahash_speed("md5", sec, generic_hash_speed_template);
1803 if (mode > 400 && mode < 500) break;
1804
1805 case 403:
1806 test_ahash_speed("sha1", sec, generic_hash_speed_template);
1807 if (mode > 400 && mode < 500) break;
1808
1809 case 404:
1810 test_ahash_speed("sha256", sec, generic_hash_speed_template);
1811 if (mode > 400 && mode < 500) break;
1812
1813 case 405:
1814 test_ahash_speed("sha384", sec, generic_hash_speed_template);
1815 if (mode > 400 && mode < 500) break;
1816
1817 case 406:
1818 test_ahash_speed("sha512", sec, generic_hash_speed_template);
1819 if (mode > 400 && mode < 500) break;
1820
1821 case 407:
1822 test_ahash_speed("wp256", sec, generic_hash_speed_template);
1823 if (mode > 400 && mode < 500) break;
1824
1825 case 408:
1826 test_ahash_speed("wp384", sec, generic_hash_speed_template);
1827 if (mode > 400 && mode < 500) break;
1828
1829 case 409:
1830 test_ahash_speed("wp512", sec, generic_hash_speed_template);
1831 if (mode > 400 && mode < 500) break;
1832
1833 case 410:
1834 test_ahash_speed("tgr128", sec, generic_hash_speed_template);
1835 if (mode > 400 && mode < 500) break;
1836
1837 case 411:
1838 test_ahash_speed("tgr160", sec, generic_hash_speed_template);
1839 if (mode > 400 && mode < 500) break;
1840
1841 case 412:
1842 test_ahash_speed("tgr192", sec, generic_hash_speed_template);
1843 if (mode > 400 && mode < 500) break;
1844
1845 case 413:
1846 test_ahash_speed("sha224", sec, generic_hash_speed_template);
1847 if (mode > 400 && mode < 500) break;
1848
1849 case 414:
1850 test_ahash_speed("rmd128", sec, generic_hash_speed_template);
1851 if (mode > 400 && mode < 500) break;
1852
1853 case 415:
1854 test_ahash_speed("rmd160", sec, generic_hash_speed_template);
1855 if (mode > 400 && mode < 500) break;
1856
1857 case 416:
1858 test_ahash_speed("rmd256", sec, generic_hash_speed_template);
1859 if (mode > 400 && mode < 500) break;
1860
1861 case 417:
1862 test_ahash_speed("rmd320", sec, generic_hash_speed_template);
1863 if (mode > 400 && mode < 500) break;
1864
1865 case 499:
1866 break;
1867
1868 case 500:
1869 test_acipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
1870 speed_template_16_24_32);
1871 test_acipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
1872 speed_template_16_24_32);
1873 test_acipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
1874 speed_template_16_24_32);
1875 test_acipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
1876 speed_template_16_24_32);
1877 test_acipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
1878 speed_template_32_40_48);
1879 test_acipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
1880 speed_template_32_40_48);
1881 test_acipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
1882 speed_template_32_48_64);
1883 test_acipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
1884 speed_template_32_48_64);
1885 test_acipher_speed("ctr(aes)", ENCRYPT, sec, NULL, 0,
1886 speed_template_16_24_32);
1887 test_acipher_speed("ctr(aes)", DECRYPT, sec, NULL, 0,
1888 speed_template_16_24_32);
1889 test_acipher_speed("cfb(aes)", ENCRYPT, sec, NULL, 0,
1890 speed_template_16_24_32);
1891 test_acipher_speed("cfb(aes)", DECRYPT, sec, NULL, 0,
1892 speed_template_16_24_32);
1893 test_acipher_speed("ofb(aes)", ENCRYPT, sec, NULL, 0,
1894 speed_template_16_24_32);
1895 test_acipher_speed("ofb(aes)", DECRYPT, sec, NULL, 0,
1896 speed_template_16_24_32);
1897 test_acipher_speed("rfc3686(ctr(aes))", ENCRYPT, sec, NULL, 0,
1898 speed_template_20_28_36);
1899 test_acipher_speed("rfc3686(ctr(aes))", DECRYPT, sec, NULL, 0,
1900 speed_template_20_28_36);
1901 break;
1902
1903 case 501:
1904 test_acipher_speed("ecb(des3_ede)", ENCRYPT, sec,
1905 des3_speed_template, DES3_SPEED_VECTORS,
1906 speed_template_24);
1907 test_acipher_speed("ecb(des3_ede)", DECRYPT, sec,
1908 des3_speed_template, DES3_SPEED_VECTORS,
1909 speed_template_24);
1910 test_acipher_speed("cbc(des3_ede)", ENCRYPT, sec,
1911 des3_speed_template, DES3_SPEED_VECTORS,
1912 speed_template_24);
1913 test_acipher_speed("cbc(des3_ede)", DECRYPT, sec,
1914 des3_speed_template, DES3_SPEED_VECTORS,
1915 speed_template_24);
1916 test_acipher_speed("cfb(des3_ede)", ENCRYPT, sec,
1917 des3_speed_template, DES3_SPEED_VECTORS,
1918 speed_template_24);
1919 test_acipher_speed("cfb(des3_ede)", DECRYPT, sec,
1920 des3_speed_template, DES3_SPEED_VECTORS,
1921 speed_template_24);
1922 test_acipher_speed("ofb(des3_ede)", ENCRYPT, sec,
1923 des3_speed_template, DES3_SPEED_VECTORS,
1924 speed_template_24);
1925 test_acipher_speed("ofb(des3_ede)", DECRYPT, sec,
1926 des3_speed_template, DES3_SPEED_VECTORS,
1927 speed_template_24);
1928 break;
1929
1930 case 502:
1931 test_acipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
1932 speed_template_8);
1933 test_acipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
1934 speed_template_8);
1935 test_acipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
1936 speed_template_8);
1937 test_acipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
1938 speed_template_8);
1939 test_acipher_speed("cfb(des)", ENCRYPT, sec, NULL, 0,
1940 speed_template_8);
1941 test_acipher_speed("cfb(des)", DECRYPT, sec, NULL, 0,
1942 speed_template_8);
1943 test_acipher_speed("ofb(des)", ENCRYPT, sec, NULL, 0,
1944 speed_template_8);
1945 test_acipher_speed("ofb(des)", DECRYPT, sec, NULL, 0,
1946 speed_template_8);
1947 break;
1948
1949 case 503:
1950 test_acipher_speed("ecb(serpent)", ENCRYPT, sec, NULL, 0,
1951 speed_template_16_32);
1952 test_acipher_speed("ecb(serpent)", DECRYPT, sec, NULL, 0,
1953 speed_template_16_32);
1954 test_acipher_speed("cbc(serpent)", ENCRYPT, sec, NULL, 0,
1955 speed_template_16_32);
1956 test_acipher_speed("cbc(serpent)", DECRYPT, sec, NULL, 0,
1957 speed_template_16_32);
1958 test_acipher_speed("ctr(serpent)", ENCRYPT, sec, NULL, 0,
1959 speed_template_16_32);
1960 test_acipher_speed("ctr(serpent)", DECRYPT, sec, NULL, 0,
1961 speed_template_16_32);
1962 test_acipher_speed("lrw(serpent)", ENCRYPT, sec, NULL, 0,
1963 speed_template_32_48);
1964 test_acipher_speed("lrw(serpent)", DECRYPT, sec, NULL, 0,
1965 speed_template_32_48);
1966 test_acipher_speed("xts(serpent)", ENCRYPT, sec, NULL, 0,
1967 speed_template_32_64);
1968 test_acipher_speed("xts(serpent)", DECRYPT, sec, NULL, 0,
1969 speed_template_32_64);
1970 break;
1971
1972 case 504:
1973 test_acipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
1974 speed_template_16_24_32);
1975 test_acipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
1976 speed_template_16_24_32);
1977 test_acipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
1978 speed_template_16_24_32);
1979 test_acipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
1980 speed_template_16_24_32);
1981 test_acipher_speed("ctr(twofish)", ENCRYPT, sec, NULL, 0,
1982 speed_template_16_24_32);
1983 test_acipher_speed("ctr(twofish)", DECRYPT, sec, NULL, 0,
1984 speed_template_16_24_32);
1985 test_acipher_speed("lrw(twofish)", ENCRYPT, sec, NULL, 0,
1986 speed_template_32_40_48);
1987 test_acipher_speed("lrw(twofish)", DECRYPT, sec, NULL, 0,
1988 speed_template_32_40_48);
1989 test_acipher_speed("xts(twofish)", ENCRYPT, sec, NULL, 0,
1990 speed_template_32_48_64);
1991 test_acipher_speed("xts(twofish)", DECRYPT, sec, NULL, 0,
1992 speed_template_32_48_64);
1993 break;
1994
1995 case 505:
1996 test_acipher_speed("ecb(arc4)", ENCRYPT, sec, NULL, 0,
1997 speed_template_8);
1998 break;
1999
2000 case 506:
2001 test_acipher_speed("ecb(cast5)", ENCRYPT, sec, NULL, 0,
2002 speed_template_8_16);
2003 test_acipher_speed("ecb(cast5)", DECRYPT, sec, NULL, 0,
2004 speed_template_8_16);
2005 test_acipher_speed("cbc(cast5)", ENCRYPT, sec, NULL, 0,
2006 speed_template_8_16);
2007 test_acipher_speed("cbc(cast5)", DECRYPT, sec, NULL, 0,
2008 speed_template_8_16);
2009 test_acipher_speed("ctr(cast5)", ENCRYPT, sec, NULL, 0,
2010 speed_template_8_16);
2011 test_acipher_speed("ctr(cast5)", DECRYPT, sec, NULL, 0,
2012 speed_template_8_16);
2013 break;
2014
2015 case 507:
2016 test_acipher_speed("ecb(cast6)", ENCRYPT, sec, NULL, 0,
2017 speed_template_16_32);
2018 test_acipher_speed("ecb(cast6)", DECRYPT, sec, NULL, 0,
2019 speed_template_16_32);
2020 test_acipher_speed("cbc(cast6)", ENCRYPT, sec, NULL, 0,
2021 speed_template_16_32);
2022 test_acipher_speed("cbc(cast6)", DECRYPT, sec, NULL, 0,
2023 speed_template_16_32);
2024 test_acipher_speed("ctr(cast6)", ENCRYPT, sec, NULL, 0,
2025 speed_template_16_32);
2026 test_acipher_speed("ctr(cast6)", DECRYPT, sec, NULL, 0,
2027 speed_template_16_32);
2028 test_acipher_speed("lrw(cast6)", ENCRYPT, sec, NULL, 0,
2029 speed_template_32_48);
2030 test_acipher_speed("lrw(cast6)", DECRYPT, sec, NULL, 0,
2031 speed_template_32_48);
2032 test_acipher_speed("xts(cast6)", ENCRYPT, sec, NULL, 0,
2033 speed_template_32_64);
2034 test_acipher_speed("xts(cast6)", DECRYPT, sec, NULL, 0,
2035 speed_template_32_64);
2036 break;
2037
2038 case 508:
2039 test_acipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
2040 speed_template_16_32);
2041 test_acipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
2042 speed_template_16_32);
2043 test_acipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
2044 speed_template_16_32);
2045 test_acipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
2046 speed_template_16_32);
2047 test_acipher_speed("ctr(camellia)", ENCRYPT, sec, NULL, 0,
2048 speed_template_16_32);
2049 test_acipher_speed("ctr(camellia)", DECRYPT, sec, NULL, 0,
2050 speed_template_16_32);
2051 test_acipher_speed("lrw(camellia)", ENCRYPT, sec, NULL, 0,
2052 speed_template_32_48);
2053 test_acipher_speed("lrw(camellia)", DECRYPT, sec, NULL, 0,
2054 speed_template_32_48);
2055 test_acipher_speed("xts(camellia)", ENCRYPT, sec, NULL, 0,
2056 speed_template_32_64);
2057 test_acipher_speed("xts(camellia)", DECRYPT, sec, NULL, 0,
2058 speed_template_32_64);
2059 break;
2060
2061 case 509:
2062 test_acipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
2063 speed_template_8_32);
2064 test_acipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
2065 speed_template_8_32);
2066 test_acipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
2067 speed_template_8_32);
2068 test_acipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
2069 speed_template_8_32);
2070 test_acipher_speed("ctr(blowfish)", ENCRYPT, sec, NULL, 0,
2071 speed_template_8_32);
2072 test_acipher_speed("ctr(blowfish)", DECRYPT, sec, NULL, 0,
2073 speed_template_8_32);
2074 break;
2075
2076 case 1000:
2077 test_available();
2078 break;
2079 }
2080
2081 return ret;
2082}
2083
2084static int do_alg_test(const char *alg, u32 type, u32 mask)
2085{
2086 return crypto_has_alg(alg, type, mask ?: CRYPTO_ALG_TYPE_MASK) ?
2087 0 : -ENOENT;
2088}
2089
2090static int __init tcrypt_mod_init(void)
2091{
2092 int err = -ENOMEM;
2093 int i;
2094
2095 for (i = 0; i < TVMEMSIZE; i++) {
2096 tvmem[i] = (void *)__get_free_page(GFP_KERNEL);
2097 if (!tvmem[i])
2098 goto err_free_tv;
2099 }
2100
2101 if (alg)
2102 err = do_alg_test(alg, type, mask);
2103 else
2104 err = do_test(mode);
2105
2106 if (err) {
2107 printk(KERN_ERR "tcrypt: one or more tests failed!\n");
2108 goto err_free_tv;
2109 }
2110
2111 /* We intentionaly return -EAGAIN to prevent keeping the module,
2112 * unless we're running in fips mode. It does all its work from
2113 * init() and doesn't offer any runtime functionality, but in
2114 * the fips case, checking for a successful load is helpful.
2115 * => we don't need it in the memory, do we?
2116 * -- mludvig
2117 */
2118 if (!fips_enabled)
2119 err = -EAGAIN;
2120
2121err_free_tv:
2122 for (i = 0; i < TVMEMSIZE && tvmem[i]; i++)
2123 free_page((unsigned long)tvmem[i]);
2124
2125 return err;
2126}
2127
2128/*
2129 * If an init function is provided, an exit function must also be provided
2130 * to allow module unload.
2131 */
2132static void __exit tcrypt_mod_fini(void) { }
2133
2134module_init(tcrypt_mod_init);
2135module_exit(tcrypt_mod_fini);
2136
2137module_param(alg, charp, 0);
2138module_param(type, uint, 0);
2139module_param(mask, uint, 0);
2140module_param(mode, int, 0);
2141module_param(sec, uint, 0);
2142MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
2143 "(defaults to zero which uses CPU cycles instead)");
2144
2145MODULE_LICENSE("GPL");
2146MODULE_DESCRIPTION("Quick & dirty crypto testing module");
2147MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Quick & dirty crypto testing module.
4 *
5 * This will only exist until we have a better testing mechanism
6 * (e.g. a char device).
7 *
8 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
9 * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
10 * Copyright (c) 2007 Nokia Siemens Networks
11 *
12 * Updated RFC4106 AES-GCM testing.
13 * Authors: Aidan O'Mahony (aidan.o.mahony@intel.com)
14 * Adrian Hoban <adrian.hoban@intel.com>
15 * Gabriele Paoloni <gabriele.paoloni@intel.com>
16 * Tadeusz Struk (tadeusz.struk@intel.com)
17 * Copyright (c) 2010, Intel Corporation.
18 */
19
20#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22#include <crypto/aead.h>
23#include <crypto/hash.h>
24#include <crypto/skcipher.h>
25#include <linux/err.h>
26#include <linux/fips.h>
27#include <linux/init.h>
28#include <linux/gfp.h>
29#include <linux/module.h>
30#include <linux/scatterlist.h>
31#include <linux/string.h>
32#include <linux/moduleparam.h>
33#include <linux/jiffies.h>
34#include <linux/timex.h>
35#include <linux/interrupt.h>
36#include "tcrypt.h"
37
38/*
39 * Need slab memory for testing (size in number of pages).
40 */
41#define TVMEMSIZE 4
42
43/*
44* Used by test_cipher_speed()
45*/
46#define ENCRYPT 1
47#define DECRYPT 0
48
49#define MAX_DIGEST_SIZE 64
50
51/*
52 * return a string with the driver name
53 */
54#define get_driver_name(tfm_type, tfm) crypto_tfm_alg_driver_name(tfm_type ## _tfm(tfm))
55
56/*
57 * Used by test_cipher_speed()
58 */
59static unsigned int sec;
60
61static char *alg = NULL;
62static u32 type;
63static u32 mask;
64static int mode;
65static u32 num_mb = 8;
66static unsigned int klen;
67static char *tvmem[TVMEMSIZE];
68
69static const char *check[] = {
70 "des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256", "sm3",
71 "blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
72 "cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
73 "khazad", "wp512", "wp384", "wp256", "xeta", "fcrypt",
74 "camellia", "seed", "rmd160",
75 "lzo", "lzo-rle", "cts", "sha3-224", "sha3-256", "sha3-384",
76 "sha3-512", "streebog256", "streebog512",
77 NULL
78};
79
80static const int block_sizes[] = { 16, 64, 256, 1024, 1420, 4096, 0 };
81static const int aead_sizes[] = { 16, 64, 256, 512, 1024, 1420, 4096, 8192, 0 };
82
83#define XBUFSIZE 8
84#define MAX_IVLEN 32
85
86static int testmgr_alloc_buf(char *buf[XBUFSIZE])
87{
88 int i;
89
90 for (i = 0; i < XBUFSIZE; i++) {
91 buf[i] = (void *)__get_free_page(GFP_KERNEL);
92 if (!buf[i])
93 goto err_free_buf;
94 }
95
96 return 0;
97
98err_free_buf:
99 while (i-- > 0)
100 free_page((unsigned long)buf[i]);
101
102 return -ENOMEM;
103}
104
105static void testmgr_free_buf(char *buf[XBUFSIZE])
106{
107 int i;
108
109 for (i = 0; i < XBUFSIZE; i++)
110 free_page((unsigned long)buf[i]);
111}
112
113static void sg_init_aead(struct scatterlist *sg, char *xbuf[XBUFSIZE],
114 unsigned int buflen, const void *assoc,
115 unsigned int aad_size)
116{
117 int np = (buflen + PAGE_SIZE - 1)/PAGE_SIZE;
118 int k, rem;
119
120 if (np > XBUFSIZE) {
121 rem = PAGE_SIZE;
122 np = XBUFSIZE;
123 } else {
124 rem = buflen % PAGE_SIZE;
125 }
126
127 sg_init_table(sg, np + 1);
128
129 sg_set_buf(&sg[0], assoc, aad_size);
130
131 if (rem)
132 np--;
133 for (k = 0; k < np; k++)
134 sg_set_buf(&sg[k + 1], xbuf[k], PAGE_SIZE);
135
136 if (rem)
137 sg_set_buf(&sg[k + 1], xbuf[k], rem);
138}
139
140static inline int do_one_aead_op(struct aead_request *req, int ret)
141{
142 struct crypto_wait *wait = req->base.data;
143
144 return crypto_wait_req(ret, wait);
145}
146
147struct test_mb_aead_data {
148 struct scatterlist sg[XBUFSIZE];
149 struct scatterlist sgout[XBUFSIZE];
150 struct aead_request *req;
151 struct crypto_wait wait;
152 char *xbuf[XBUFSIZE];
153 char *xoutbuf[XBUFSIZE];
154 char *axbuf[XBUFSIZE];
155};
156
157static int do_mult_aead_op(struct test_mb_aead_data *data, int enc,
158 u32 num_mb, int *rc)
159{
160 int i, err = 0;
161
162 /* Fire up a bunch of concurrent requests */
163 for (i = 0; i < num_mb; i++) {
164 if (enc == ENCRYPT)
165 rc[i] = crypto_aead_encrypt(data[i].req);
166 else
167 rc[i] = crypto_aead_decrypt(data[i].req);
168 }
169
170 /* Wait for all requests to finish */
171 for (i = 0; i < num_mb; i++) {
172 rc[i] = crypto_wait_req(rc[i], &data[i].wait);
173
174 if (rc[i]) {
175 pr_info("concurrent request %d error %d\n", i, rc[i]);
176 err = rc[i];
177 }
178 }
179
180 return err;
181}
182
183static int test_mb_aead_jiffies(struct test_mb_aead_data *data, int enc,
184 int blen, int secs, u32 num_mb)
185{
186 unsigned long start, end;
187 int bcount;
188 int ret = 0;
189 int *rc;
190
191 rc = kcalloc(num_mb, sizeof(*rc), GFP_KERNEL);
192 if (!rc)
193 return -ENOMEM;
194
195 for (start = jiffies, end = start + secs * HZ, bcount = 0;
196 time_before(jiffies, end); bcount++) {
197 ret = do_mult_aead_op(data, enc, num_mb, rc);
198 if (ret)
199 goto out;
200 }
201
202 pr_cont("%d operations in %d seconds (%llu bytes)\n",
203 bcount * num_mb, secs, (u64)bcount * blen * num_mb);
204
205out:
206 kfree(rc);
207 return ret;
208}
209
210static int test_mb_aead_cycles(struct test_mb_aead_data *data, int enc,
211 int blen, u32 num_mb)
212{
213 unsigned long cycles = 0;
214 int ret = 0;
215 int i;
216 int *rc;
217
218 rc = kcalloc(num_mb, sizeof(*rc), GFP_KERNEL);
219 if (!rc)
220 return -ENOMEM;
221
222 /* Warm-up run. */
223 for (i = 0; i < 4; i++) {
224 ret = do_mult_aead_op(data, enc, num_mb, rc);
225 if (ret)
226 goto out;
227 }
228
229 /* The real thing. */
230 for (i = 0; i < 8; i++) {
231 cycles_t start, end;
232
233 start = get_cycles();
234 ret = do_mult_aead_op(data, enc, num_mb, rc);
235 end = get_cycles();
236
237 if (ret)
238 goto out;
239
240 cycles += end - start;
241 }
242
243 pr_cont("1 operation in %lu cycles (%d bytes)\n",
244 (cycles + 4) / (8 * num_mb), blen);
245
246out:
247 kfree(rc);
248 return ret;
249}
250
251static void test_mb_aead_speed(const char *algo, int enc, int secs,
252 struct aead_speed_template *template,
253 unsigned int tcount, u8 authsize,
254 unsigned int aad_size, u8 *keysize, u32 num_mb)
255{
256 struct test_mb_aead_data *data;
257 struct crypto_aead *tfm;
258 unsigned int i, j, iv_len;
259 const int *b_size;
260 const char *key;
261 const char *e;
262 void *assoc;
263 char *iv;
264 int ret;
265
266
267 if (aad_size >= PAGE_SIZE) {
268 pr_err("associate data length (%u) too big\n", aad_size);
269 return;
270 }
271
272 iv = kzalloc(MAX_IVLEN, GFP_KERNEL);
273 if (!iv)
274 return;
275
276 if (enc == ENCRYPT)
277 e = "encryption";
278 else
279 e = "decryption";
280
281 data = kcalloc(num_mb, sizeof(*data), GFP_KERNEL);
282 if (!data)
283 goto out_free_iv;
284
285 tfm = crypto_alloc_aead(algo, 0, 0);
286 if (IS_ERR(tfm)) {
287 pr_err("failed to load transform for %s: %ld\n",
288 algo, PTR_ERR(tfm));
289 goto out_free_data;
290 }
291
292 ret = crypto_aead_setauthsize(tfm, authsize);
293 if (ret) {
294 pr_err("alg: aead: Failed to setauthsize for %s: %d\n", algo,
295 ret);
296 goto out_free_tfm;
297 }
298
299 for (i = 0; i < num_mb; ++i)
300 if (testmgr_alloc_buf(data[i].xbuf)) {
301 while (i--)
302 testmgr_free_buf(data[i].xbuf);
303 goto out_free_tfm;
304 }
305
306 for (i = 0; i < num_mb; ++i)
307 if (testmgr_alloc_buf(data[i].axbuf)) {
308 while (i--)
309 testmgr_free_buf(data[i].axbuf);
310 goto out_free_xbuf;
311 }
312
313 for (i = 0; i < num_mb; ++i)
314 if (testmgr_alloc_buf(data[i].xoutbuf)) {
315 while (i--)
316 testmgr_free_buf(data[i].xoutbuf);
317 goto out_free_axbuf;
318 }
319
320 for (i = 0; i < num_mb; ++i) {
321 data[i].req = aead_request_alloc(tfm, GFP_KERNEL);
322 if (!data[i].req) {
323 pr_err("alg: aead: Failed to allocate request for %s\n",
324 algo);
325 while (i--)
326 aead_request_free(data[i].req);
327 goto out_free_xoutbuf;
328 }
329 }
330
331 for (i = 0; i < num_mb; ++i) {
332 crypto_init_wait(&data[i].wait);
333 aead_request_set_callback(data[i].req,
334 CRYPTO_TFM_REQ_MAY_BACKLOG,
335 crypto_req_done, &data[i].wait);
336 }
337
338 pr_info("\ntesting speed of multibuffer %s (%s) %s\n", algo,
339 get_driver_name(crypto_aead, tfm), e);
340
341 i = 0;
342 do {
343 b_size = aead_sizes;
344 do {
345 int bs = round_up(*b_size, crypto_aead_blocksize(tfm));
346
347 if (bs + authsize > XBUFSIZE * PAGE_SIZE) {
348 pr_err("template (%u) too big for buffer (%lu)\n",
349 authsize + bs,
350 XBUFSIZE * PAGE_SIZE);
351 goto out;
352 }
353
354 pr_info("test %u (%d bit key, %d byte blocks): ", i,
355 *keysize * 8, bs);
356
357 /* Set up tfm global state, i.e. the key */
358
359 memset(tvmem[0], 0xff, PAGE_SIZE);
360 key = tvmem[0];
361 for (j = 0; j < tcount; j++) {
362 if (template[j].klen == *keysize) {
363 key = template[j].key;
364 break;
365 }
366 }
367
368 crypto_aead_clear_flags(tfm, ~0);
369
370 ret = crypto_aead_setkey(tfm, key, *keysize);
371 if (ret) {
372 pr_err("setkey() failed flags=%x\n",
373 crypto_aead_get_flags(tfm));
374 goto out;
375 }
376
377 iv_len = crypto_aead_ivsize(tfm);
378 if (iv_len)
379 memset(iv, 0xff, iv_len);
380
381 /* Now setup per request stuff, i.e. buffers */
382
383 for (j = 0; j < num_mb; ++j) {
384 struct test_mb_aead_data *cur = &data[j];
385
386 assoc = cur->axbuf[0];
387 memset(assoc, 0xff, aad_size);
388
389 sg_init_aead(cur->sg, cur->xbuf,
390 bs + (enc ? 0 : authsize),
391 assoc, aad_size);
392
393 sg_init_aead(cur->sgout, cur->xoutbuf,
394 bs + (enc ? authsize : 0),
395 assoc, aad_size);
396
397 aead_request_set_ad(cur->req, aad_size);
398
399 if (!enc) {
400
401 aead_request_set_crypt(cur->req,
402 cur->sgout,
403 cur->sg,
404 bs, iv);
405 ret = crypto_aead_encrypt(cur->req);
406 ret = do_one_aead_op(cur->req, ret);
407
408 if (ret) {
409 pr_err("calculating auth failed (%d)\n",
410 ret);
411 break;
412 }
413 }
414
415 aead_request_set_crypt(cur->req, cur->sg,
416 cur->sgout, bs +
417 (enc ? 0 : authsize),
418 iv);
419
420 }
421
422 if (secs) {
423 ret = test_mb_aead_jiffies(data, enc, bs,
424 secs, num_mb);
425 cond_resched();
426 } else {
427 ret = test_mb_aead_cycles(data, enc, bs,
428 num_mb);
429 }
430
431 if (ret) {
432 pr_err("%s() failed return code=%d\n", e, ret);
433 break;
434 }
435 b_size++;
436 i++;
437 } while (*b_size);
438 keysize++;
439 } while (*keysize);
440
441out:
442 for (i = 0; i < num_mb; ++i)
443 aead_request_free(data[i].req);
444out_free_xoutbuf:
445 for (i = 0; i < num_mb; ++i)
446 testmgr_free_buf(data[i].xoutbuf);
447out_free_axbuf:
448 for (i = 0; i < num_mb; ++i)
449 testmgr_free_buf(data[i].axbuf);
450out_free_xbuf:
451 for (i = 0; i < num_mb; ++i)
452 testmgr_free_buf(data[i].xbuf);
453out_free_tfm:
454 crypto_free_aead(tfm);
455out_free_data:
456 kfree(data);
457out_free_iv:
458 kfree(iv);
459}
460
461static int test_aead_jiffies(struct aead_request *req, int enc,
462 int blen, int secs)
463{
464 unsigned long start, end;
465 int bcount;
466 int ret;
467
468 for (start = jiffies, end = start + secs * HZ, bcount = 0;
469 time_before(jiffies, end); bcount++) {
470 if (enc)
471 ret = do_one_aead_op(req, crypto_aead_encrypt(req));
472 else
473 ret = do_one_aead_op(req, crypto_aead_decrypt(req));
474
475 if (ret)
476 return ret;
477 }
478
479 pr_cont("%d operations in %d seconds (%llu bytes)\n",
480 bcount, secs, (u64)bcount * blen);
481 return 0;
482}
483
484static int test_aead_cycles(struct aead_request *req, int enc, int blen)
485{
486 unsigned long cycles = 0;
487 int ret = 0;
488 int i;
489
490 /* Warm-up run. */
491 for (i = 0; i < 4; i++) {
492 if (enc)
493 ret = do_one_aead_op(req, crypto_aead_encrypt(req));
494 else
495 ret = do_one_aead_op(req, crypto_aead_decrypt(req));
496
497 if (ret)
498 goto out;
499 }
500
501 /* The real thing. */
502 for (i = 0; i < 8; i++) {
503 cycles_t start, end;
504
505 start = get_cycles();
506 if (enc)
507 ret = do_one_aead_op(req, crypto_aead_encrypt(req));
508 else
509 ret = do_one_aead_op(req, crypto_aead_decrypt(req));
510 end = get_cycles();
511
512 if (ret)
513 goto out;
514
515 cycles += end - start;
516 }
517
518out:
519 if (ret == 0)
520 printk("1 operation in %lu cycles (%d bytes)\n",
521 (cycles + 4) / 8, blen);
522
523 return ret;
524}
525
526static void test_aead_speed(const char *algo, int enc, unsigned int secs,
527 struct aead_speed_template *template,
528 unsigned int tcount, u8 authsize,
529 unsigned int aad_size, u8 *keysize)
530{
531 unsigned int i, j;
532 struct crypto_aead *tfm;
533 int ret = -ENOMEM;
534 const char *key;
535 struct aead_request *req;
536 struct scatterlist *sg;
537 struct scatterlist *sgout;
538 const char *e;
539 void *assoc;
540 char *iv;
541 char *xbuf[XBUFSIZE];
542 char *xoutbuf[XBUFSIZE];
543 char *axbuf[XBUFSIZE];
544 const int *b_size;
545 unsigned int iv_len;
546 struct crypto_wait wait;
547
548 iv = kzalloc(MAX_IVLEN, GFP_KERNEL);
549 if (!iv)
550 return;
551
552 if (aad_size >= PAGE_SIZE) {
553 pr_err("associate data length (%u) too big\n", aad_size);
554 goto out_noxbuf;
555 }
556
557 if (enc == ENCRYPT)
558 e = "encryption";
559 else
560 e = "decryption";
561
562 if (testmgr_alloc_buf(xbuf))
563 goto out_noxbuf;
564 if (testmgr_alloc_buf(axbuf))
565 goto out_noaxbuf;
566 if (testmgr_alloc_buf(xoutbuf))
567 goto out_nooutbuf;
568
569 sg = kmalloc(sizeof(*sg) * 9 * 2, GFP_KERNEL);
570 if (!sg)
571 goto out_nosg;
572 sgout = &sg[9];
573
574 tfm = crypto_alloc_aead(algo, 0, 0);
575 if (IS_ERR(tfm)) {
576 pr_err("alg: aead: Failed to load transform for %s: %ld\n", algo,
577 PTR_ERR(tfm));
578 goto out_notfm;
579 }
580
581 ret = crypto_aead_setauthsize(tfm, authsize);
582 if (ret) {
583 pr_err("alg: aead: Failed to setauthsize for %s: %d\n", algo,
584 ret);
585 goto out_noreq;
586 }
587
588 crypto_init_wait(&wait);
589 printk(KERN_INFO "\ntesting speed of %s (%s) %s\n", algo,
590 get_driver_name(crypto_aead, tfm), e);
591
592 req = aead_request_alloc(tfm, GFP_KERNEL);
593 if (!req) {
594 pr_err("alg: aead: Failed to allocate request for %s\n",
595 algo);
596 goto out_noreq;
597 }
598
599 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
600 crypto_req_done, &wait);
601
602 i = 0;
603 do {
604 b_size = aead_sizes;
605 do {
606 u32 bs = round_up(*b_size, crypto_aead_blocksize(tfm));
607
608 assoc = axbuf[0];
609 memset(assoc, 0xff, aad_size);
610
611 if ((*keysize + bs) > TVMEMSIZE * PAGE_SIZE) {
612 pr_err("template (%u) too big for tvmem (%lu)\n",
613 *keysize + bs,
614 TVMEMSIZE * PAGE_SIZE);
615 goto out;
616 }
617
618 key = tvmem[0];
619 for (j = 0; j < tcount; j++) {
620 if (template[j].klen == *keysize) {
621 key = template[j].key;
622 break;
623 }
624 }
625
626 ret = crypto_aead_setkey(tfm, key, *keysize);
627 if (ret) {
628 pr_err("setkey() failed flags=%x: %d\n",
629 crypto_aead_get_flags(tfm), ret);
630 goto out;
631 }
632
633 iv_len = crypto_aead_ivsize(tfm);
634 if (iv_len)
635 memset(iv, 0xff, iv_len);
636
637 crypto_aead_clear_flags(tfm, ~0);
638 printk(KERN_INFO "test %u (%d bit key, %d byte blocks): ",
639 i, *keysize * 8, bs);
640
641 memset(tvmem[0], 0xff, PAGE_SIZE);
642
643 sg_init_aead(sg, xbuf, bs + (enc ? 0 : authsize),
644 assoc, aad_size);
645
646 sg_init_aead(sgout, xoutbuf,
647 bs + (enc ? authsize : 0), assoc,
648 aad_size);
649
650 aead_request_set_ad(req, aad_size);
651
652 if (!enc) {
653
654 /*
655 * For decryption we need a proper auth so
656 * we do the encryption path once with buffers
657 * reversed (input <-> output) to calculate it
658 */
659 aead_request_set_crypt(req, sgout, sg,
660 bs, iv);
661 ret = do_one_aead_op(req,
662 crypto_aead_encrypt(req));
663
664 if (ret) {
665 pr_err("calculating auth failed (%d)\n",
666 ret);
667 break;
668 }
669 }
670
671 aead_request_set_crypt(req, sg, sgout,
672 bs + (enc ? 0 : authsize),
673 iv);
674
675 if (secs) {
676 ret = test_aead_jiffies(req, enc, bs,
677 secs);
678 cond_resched();
679 } else {
680 ret = test_aead_cycles(req, enc, bs);
681 }
682
683 if (ret) {
684 pr_err("%s() failed return code=%d\n", e, ret);
685 break;
686 }
687 b_size++;
688 i++;
689 } while (*b_size);
690 keysize++;
691 } while (*keysize);
692
693out:
694 aead_request_free(req);
695out_noreq:
696 crypto_free_aead(tfm);
697out_notfm:
698 kfree(sg);
699out_nosg:
700 testmgr_free_buf(xoutbuf);
701out_nooutbuf:
702 testmgr_free_buf(axbuf);
703out_noaxbuf:
704 testmgr_free_buf(xbuf);
705out_noxbuf:
706 kfree(iv);
707}
708
709static void test_hash_sg_init(struct scatterlist *sg)
710{
711 int i;
712
713 sg_init_table(sg, TVMEMSIZE);
714 for (i = 0; i < TVMEMSIZE; i++) {
715 sg_set_buf(sg + i, tvmem[i], PAGE_SIZE);
716 memset(tvmem[i], 0xff, PAGE_SIZE);
717 }
718}
719
720static inline int do_one_ahash_op(struct ahash_request *req, int ret)
721{
722 struct crypto_wait *wait = req->base.data;
723
724 return crypto_wait_req(ret, wait);
725}
726
727struct test_mb_ahash_data {
728 struct scatterlist sg[XBUFSIZE];
729 char result[64];
730 struct ahash_request *req;
731 struct crypto_wait wait;
732 char *xbuf[XBUFSIZE];
733};
734
735static inline int do_mult_ahash_op(struct test_mb_ahash_data *data, u32 num_mb,
736 int *rc)
737{
738 int i, err = 0;
739
740 /* Fire up a bunch of concurrent requests */
741 for (i = 0; i < num_mb; i++)
742 rc[i] = crypto_ahash_digest(data[i].req);
743
744 /* Wait for all requests to finish */
745 for (i = 0; i < num_mb; i++) {
746 rc[i] = crypto_wait_req(rc[i], &data[i].wait);
747
748 if (rc[i]) {
749 pr_info("concurrent request %d error %d\n", i, rc[i]);
750 err = rc[i];
751 }
752 }
753
754 return err;
755}
756
757static int test_mb_ahash_jiffies(struct test_mb_ahash_data *data, int blen,
758 int secs, u32 num_mb)
759{
760 unsigned long start, end;
761 int bcount;
762 int ret = 0;
763 int *rc;
764
765 rc = kcalloc(num_mb, sizeof(*rc), GFP_KERNEL);
766 if (!rc)
767 return -ENOMEM;
768
769 for (start = jiffies, end = start + secs * HZ, bcount = 0;
770 time_before(jiffies, end); bcount++) {
771 ret = do_mult_ahash_op(data, num_mb, rc);
772 if (ret)
773 goto out;
774 }
775
776 pr_cont("%d operations in %d seconds (%llu bytes)\n",
777 bcount * num_mb, secs, (u64)bcount * blen * num_mb);
778
779out:
780 kfree(rc);
781 return ret;
782}
783
784static int test_mb_ahash_cycles(struct test_mb_ahash_data *data, int blen,
785 u32 num_mb)
786{
787 unsigned long cycles = 0;
788 int ret = 0;
789 int i;
790 int *rc;
791
792 rc = kcalloc(num_mb, sizeof(*rc), GFP_KERNEL);
793 if (!rc)
794 return -ENOMEM;
795
796 /* Warm-up run. */
797 for (i = 0; i < 4; i++) {
798 ret = do_mult_ahash_op(data, num_mb, rc);
799 if (ret)
800 goto out;
801 }
802
803 /* The real thing. */
804 for (i = 0; i < 8; i++) {
805 cycles_t start, end;
806
807 start = get_cycles();
808 ret = do_mult_ahash_op(data, num_mb, rc);
809 end = get_cycles();
810
811 if (ret)
812 goto out;
813
814 cycles += end - start;
815 }
816
817 pr_cont("1 operation in %lu cycles (%d bytes)\n",
818 (cycles + 4) / (8 * num_mb), blen);
819
820out:
821 kfree(rc);
822 return ret;
823}
824
825static void test_mb_ahash_speed(const char *algo, unsigned int secs,
826 struct hash_speed *speed, u32 num_mb)
827{
828 struct test_mb_ahash_data *data;
829 struct crypto_ahash *tfm;
830 unsigned int i, j, k;
831 int ret;
832
833 data = kcalloc(num_mb, sizeof(*data), GFP_KERNEL);
834 if (!data)
835 return;
836
837 tfm = crypto_alloc_ahash(algo, 0, 0);
838 if (IS_ERR(tfm)) {
839 pr_err("failed to load transform for %s: %ld\n",
840 algo, PTR_ERR(tfm));
841 goto free_data;
842 }
843
844 for (i = 0; i < num_mb; ++i) {
845 if (testmgr_alloc_buf(data[i].xbuf))
846 goto out;
847
848 crypto_init_wait(&data[i].wait);
849
850 data[i].req = ahash_request_alloc(tfm, GFP_KERNEL);
851 if (!data[i].req) {
852 pr_err("alg: hash: Failed to allocate request for %s\n",
853 algo);
854 goto out;
855 }
856
857 ahash_request_set_callback(data[i].req, 0, crypto_req_done,
858 &data[i].wait);
859
860 sg_init_table(data[i].sg, XBUFSIZE);
861 for (j = 0; j < XBUFSIZE; j++) {
862 sg_set_buf(data[i].sg + j, data[i].xbuf[j], PAGE_SIZE);
863 memset(data[i].xbuf[j], 0xff, PAGE_SIZE);
864 }
865 }
866
867 pr_info("\ntesting speed of multibuffer %s (%s)\n", algo,
868 get_driver_name(crypto_ahash, tfm));
869
870 for (i = 0; speed[i].blen != 0; i++) {
871 /* For some reason this only tests digests. */
872 if (speed[i].blen != speed[i].plen)
873 continue;
874
875 if (speed[i].blen > XBUFSIZE * PAGE_SIZE) {
876 pr_err("template (%u) too big for tvmem (%lu)\n",
877 speed[i].blen, XBUFSIZE * PAGE_SIZE);
878 goto out;
879 }
880
881 if (klen)
882 crypto_ahash_setkey(tfm, tvmem[0], klen);
883
884 for (k = 0; k < num_mb; k++)
885 ahash_request_set_crypt(data[k].req, data[k].sg,
886 data[k].result, speed[i].blen);
887
888 pr_info("test%3u "
889 "(%5u byte blocks,%5u bytes per update,%4u updates): ",
890 i, speed[i].blen, speed[i].plen,
891 speed[i].blen / speed[i].plen);
892
893 if (secs) {
894 ret = test_mb_ahash_jiffies(data, speed[i].blen, secs,
895 num_mb);
896 cond_resched();
897 } else {
898 ret = test_mb_ahash_cycles(data, speed[i].blen, num_mb);
899 }
900
901
902 if (ret) {
903 pr_err("At least one hashing failed ret=%d\n", ret);
904 break;
905 }
906 }
907
908out:
909 for (k = 0; k < num_mb; ++k)
910 ahash_request_free(data[k].req);
911
912 for (k = 0; k < num_mb; ++k)
913 testmgr_free_buf(data[k].xbuf);
914
915 crypto_free_ahash(tfm);
916
917free_data:
918 kfree(data);
919}
920
921static int test_ahash_jiffies_digest(struct ahash_request *req, int blen,
922 char *out, int secs)
923{
924 unsigned long start, end;
925 int bcount;
926 int ret;
927
928 for (start = jiffies, end = start + secs * HZ, bcount = 0;
929 time_before(jiffies, end); bcount++) {
930 ret = do_one_ahash_op(req, crypto_ahash_digest(req));
931 if (ret)
932 return ret;
933 }
934
935 printk("%6u opers/sec, %9lu bytes/sec\n",
936 bcount / secs, ((long)bcount * blen) / secs);
937
938 return 0;
939}
940
941static int test_ahash_jiffies(struct ahash_request *req, int blen,
942 int plen, char *out, int secs)
943{
944 unsigned long start, end;
945 int bcount, pcount;
946 int ret;
947
948 if (plen == blen)
949 return test_ahash_jiffies_digest(req, blen, out, secs);
950
951 for (start = jiffies, end = start + secs * HZ, bcount = 0;
952 time_before(jiffies, end); bcount++) {
953 ret = do_one_ahash_op(req, crypto_ahash_init(req));
954 if (ret)
955 return ret;
956 for (pcount = 0; pcount < blen; pcount += plen) {
957 ret = do_one_ahash_op(req, crypto_ahash_update(req));
958 if (ret)
959 return ret;
960 }
961 /* we assume there is enough space in 'out' for the result */
962 ret = do_one_ahash_op(req, crypto_ahash_final(req));
963 if (ret)
964 return ret;
965 }
966
967 pr_cont("%6u opers/sec, %9lu bytes/sec\n",
968 bcount / secs, ((long)bcount * blen) / secs);
969
970 return 0;
971}
972
973static int test_ahash_cycles_digest(struct ahash_request *req, int blen,
974 char *out)
975{
976 unsigned long cycles = 0;
977 int ret, i;
978
979 /* Warm-up run. */
980 for (i = 0; i < 4; i++) {
981 ret = do_one_ahash_op(req, crypto_ahash_digest(req));
982 if (ret)
983 goto out;
984 }
985
986 /* The real thing. */
987 for (i = 0; i < 8; i++) {
988 cycles_t start, end;
989
990 start = get_cycles();
991
992 ret = do_one_ahash_op(req, crypto_ahash_digest(req));
993 if (ret)
994 goto out;
995
996 end = get_cycles();
997
998 cycles += end - start;
999 }
1000
1001out:
1002 if (ret)
1003 return ret;
1004
1005 pr_cont("%6lu cycles/operation, %4lu cycles/byte\n",
1006 cycles / 8, cycles / (8 * blen));
1007
1008 return 0;
1009}
1010
1011static int test_ahash_cycles(struct ahash_request *req, int blen,
1012 int plen, char *out)
1013{
1014 unsigned long cycles = 0;
1015 int i, pcount, ret;
1016
1017 if (plen == blen)
1018 return test_ahash_cycles_digest(req, blen, out);
1019
1020 /* Warm-up run. */
1021 for (i = 0; i < 4; i++) {
1022 ret = do_one_ahash_op(req, crypto_ahash_init(req));
1023 if (ret)
1024 goto out;
1025 for (pcount = 0; pcount < blen; pcount += plen) {
1026 ret = do_one_ahash_op(req, crypto_ahash_update(req));
1027 if (ret)
1028 goto out;
1029 }
1030 ret = do_one_ahash_op(req, crypto_ahash_final(req));
1031 if (ret)
1032 goto out;
1033 }
1034
1035 /* The real thing. */
1036 for (i = 0; i < 8; i++) {
1037 cycles_t start, end;
1038
1039 start = get_cycles();
1040
1041 ret = do_one_ahash_op(req, crypto_ahash_init(req));
1042 if (ret)
1043 goto out;
1044 for (pcount = 0; pcount < blen; pcount += plen) {
1045 ret = do_one_ahash_op(req, crypto_ahash_update(req));
1046 if (ret)
1047 goto out;
1048 }
1049 ret = do_one_ahash_op(req, crypto_ahash_final(req));
1050 if (ret)
1051 goto out;
1052
1053 end = get_cycles();
1054
1055 cycles += end - start;
1056 }
1057
1058out:
1059 if (ret)
1060 return ret;
1061
1062 pr_cont("%6lu cycles/operation, %4lu cycles/byte\n",
1063 cycles / 8, cycles / (8 * blen));
1064
1065 return 0;
1066}
1067
1068static void test_ahash_speed_common(const char *algo, unsigned int secs,
1069 struct hash_speed *speed, unsigned mask)
1070{
1071 struct scatterlist sg[TVMEMSIZE];
1072 struct crypto_wait wait;
1073 struct ahash_request *req;
1074 struct crypto_ahash *tfm;
1075 char *output;
1076 int i, ret;
1077
1078 tfm = crypto_alloc_ahash(algo, 0, mask);
1079 if (IS_ERR(tfm)) {
1080 pr_err("failed to load transform for %s: %ld\n",
1081 algo, PTR_ERR(tfm));
1082 return;
1083 }
1084
1085 printk(KERN_INFO "\ntesting speed of async %s (%s)\n", algo,
1086 get_driver_name(crypto_ahash, tfm));
1087
1088 if (crypto_ahash_digestsize(tfm) > MAX_DIGEST_SIZE) {
1089 pr_err("digestsize(%u) > %d\n", crypto_ahash_digestsize(tfm),
1090 MAX_DIGEST_SIZE);
1091 goto out;
1092 }
1093
1094 test_hash_sg_init(sg);
1095 req = ahash_request_alloc(tfm, GFP_KERNEL);
1096 if (!req) {
1097 pr_err("ahash request allocation failure\n");
1098 goto out;
1099 }
1100
1101 crypto_init_wait(&wait);
1102 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1103 crypto_req_done, &wait);
1104
1105 output = kmalloc(MAX_DIGEST_SIZE, GFP_KERNEL);
1106 if (!output)
1107 goto out_nomem;
1108
1109 for (i = 0; speed[i].blen != 0; i++) {
1110 if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
1111 pr_err("template (%u) too big for tvmem (%lu)\n",
1112 speed[i].blen, TVMEMSIZE * PAGE_SIZE);
1113 break;
1114 }
1115
1116 if (klen)
1117 crypto_ahash_setkey(tfm, tvmem[0], klen);
1118
1119 pr_info("test%3u "
1120 "(%5u byte blocks,%5u bytes per update,%4u updates): ",
1121 i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
1122
1123 ahash_request_set_crypt(req, sg, output, speed[i].plen);
1124
1125 if (secs) {
1126 ret = test_ahash_jiffies(req, speed[i].blen,
1127 speed[i].plen, output, secs);
1128 cond_resched();
1129 } else {
1130 ret = test_ahash_cycles(req, speed[i].blen,
1131 speed[i].plen, output);
1132 }
1133
1134 if (ret) {
1135 pr_err("hashing failed ret=%d\n", ret);
1136 break;
1137 }
1138 }
1139
1140 kfree(output);
1141
1142out_nomem:
1143 ahash_request_free(req);
1144
1145out:
1146 crypto_free_ahash(tfm);
1147}
1148
1149static void test_ahash_speed(const char *algo, unsigned int secs,
1150 struct hash_speed *speed)
1151{
1152 return test_ahash_speed_common(algo, secs, speed, 0);
1153}
1154
1155static void test_hash_speed(const char *algo, unsigned int secs,
1156 struct hash_speed *speed)
1157{
1158 return test_ahash_speed_common(algo, secs, speed, CRYPTO_ALG_ASYNC);
1159}
1160
1161struct test_mb_skcipher_data {
1162 struct scatterlist sg[XBUFSIZE];
1163 struct skcipher_request *req;
1164 struct crypto_wait wait;
1165 char *xbuf[XBUFSIZE];
1166};
1167
1168static int do_mult_acipher_op(struct test_mb_skcipher_data *data, int enc,
1169 u32 num_mb, int *rc)
1170{
1171 int i, err = 0;
1172
1173 /* Fire up a bunch of concurrent requests */
1174 for (i = 0; i < num_mb; i++) {
1175 if (enc == ENCRYPT)
1176 rc[i] = crypto_skcipher_encrypt(data[i].req);
1177 else
1178 rc[i] = crypto_skcipher_decrypt(data[i].req);
1179 }
1180
1181 /* Wait for all requests to finish */
1182 for (i = 0; i < num_mb; i++) {
1183 rc[i] = crypto_wait_req(rc[i], &data[i].wait);
1184
1185 if (rc[i]) {
1186 pr_info("concurrent request %d error %d\n", i, rc[i]);
1187 err = rc[i];
1188 }
1189 }
1190
1191 return err;
1192}
1193
1194static int test_mb_acipher_jiffies(struct test_mb_skcipher_data *data, int enc,
1195 int blen, int secs, u32 num_mb)
1196{
1197 unsigned long start, end;
1198 int bcount;
1199 int ret = 0;
1200 int *rc;
1201
1202 rc = kcalloc(num_mb, sizeof(*rc), GFP_KERNEL);
1203 if (!rc)
1204 return -ENOMEM;
1205
1206 for (start = jiffies, end = start + secs * HZ, bcount = 0;
1207 time_before(jiffies, end); bcount++) {
1208 ret = do_mult_acipher_op(data, enc, num_mb, rc);
1209 if (ret)
1210 goto out;
1211 }
1212
1213 pr_cont("%d operations in %d seconds (%llu bytes)\n",
1214 bcount * num_mb, secs, (u64)bcount * blen * num_mb);
1215
1216out:
1217 kfree(rc);
1218 return ret;
1219}
1220
1221static int test_mb_acipher_cycles(struct test_mb_skcipher_data *data, int enc,
1222 int blen, u32 num_mb)
1223{
1224 unsigned long cycles = 0;
1225 int ret = 0;
1226 int i;
1227 int *rc;
1228
1229 rc = kcalloc(num_mb, sizeof(*rc), GFP_KERNEL);
1230 if (!rc)
1231 return -ENOMEM;
1232
1233 /* Warm-up run. */
1234 for (i = 0; i < 4; i++) {
1235 ret = do_mult_acipher_op(data, enc, num_mb, rc);
1236 if (ret)
1237 goto out;
1238 }
1239
1240 /* The real thing. */
1241 for (i = 0; i < 8; i++) {
1242 cycles_t start, end;
1243
1244 start = get_cycles();
1245 ret = do_mult_acipher_op(data, enc, num_mb, rc);
1246 end = get_cycles();
1247
1248 if (ret)
1249 goto out;
1250
1251 cycles += end - start;
1252 }
1253
1254 pr_cont("1 operation in %lu cycles (%d bytes)\n",
1255 (cycles + 4) / (8 * num_mb), blen);
1256
1257out:
1258 kfree(rc);
1259 return ret;
1260}
1261
1262static void test_mb_skcipher_speed(const char *algo, int enc, int secs,
1263 struct cipher_speed_template *template,
1264 unsigned int tcount, u8 *keysize, u32 num_mb)
1265{
1266 struct test_mb_skcipher_data *data;
1267 struct crypto_skcipher *tfm;
1268 unsigned int i, j, iv_len;
1269 const int *b_size;
1270 const char *key;
1271 const char *e;
1272 char iv[128];
1273 int ret;
1274
1275 if (enc == ENCRYPT)
1276 e = "encryption";
1277 else
1278 e = "decryption";
1279
1280 data = kcalloc(num_mb, sizeof(*data), GFP_KERNEL);
1281 if (!data)
1282 return;
1283
1284 tfm = crypto_alloc_skcipher(algo, 0, 0);
1285 if (IS_ERR(tfm)) {
1286 pr_err("failed to load transform for %s: %ld\n",
1287 algo, PTR_ERR(tfm));
1288 goto out_free_data;
1289 }
1290
1291 for (i = 0; i < num_mb; ++i)
1292 if (testmgr_alloc_buf(data[i].xbuf)) {
1293 while (i--)
1294 testmgr_free_buf(data[i].xbuf);
1295 goto out_free_tfm;
1296 }
1297
1298
1299 for (i = 0; i < num_mb; ++i)
1300 if (testmgr_alloc_buf(data[i].xbuf)) {
1301 while (i--)
1302 testmgr_free_buf(data[i].xbuf);
1303 goto out_free_tfm;
1304 }
1305
1306
1307 for (i = 0; i < num_mb; ++i) {
1308 data[i].req = skcipher_request_alloc(tfm, GFP_KERNEL);
1309 if (!data[i].req) {
1310 pr_err("alg: skcipher: Failed to allocate request for %s\n",
1311 algo);
1312 while (i--)
1313 skcipher_request_free(data[i].req);
1314 goto out_free_xbuf;
1315 }
1316 }
1317
1318 for (i = 0; i < num_mb; ++i) {
1319 skcipher_request_set_callback(data[i].req,
1320 CRYPTO_TFM_REQ_MAY_BACKLOG,
1321 crypto_req_done, &data[i].wait);
1322 crypto_init_wait(&data[i].wait);
1323 }
1324
1325 pr_info("\ntesting speed of multibuffer %s (%s) %s\n", algo,
1326 get_driver_name(crypto_skcipher, tfm), e);
1327
1328 i = 0;
1329 do {
1330 b_size = block_sizes;
1331 do {
1332 u32 bs = round_up(*b_size, crypto_skcipher_blocksize(tfm));
1333
1334 if (bs > XBUFSIZE * PAGE_SIZE) {
1335 pr_err("template (%u) too big for buffer (%lu)\n",
1336 *b_size, XBUFSIZE * PAGE_SIZE);
1337 goto out;
1338 }
1339
1340 pr_info("test %u (%d bit key, %d byte blocks): ", i,
1341 *keysize * 8, bs);
1342
1343 /* Set up tfm global state, i.e. the key */
1344
1345 memset(tvmem[0], 0xff, PAGE_SIZE);
1346 key = tvmem[0];
1347 for (j = 0; j < tcount; j++) {
1348 if (template[j].klen == *keysize) {
1349 key = template[j].key;
1350 break;
1351 }
1352 }
1353
1354 crypto_skcipher_clear_flags(tfm, ~0);
1355
1356 ret = crypto_skcipher_setkey(tfm, key, *keysize);
1357 if (ret) {
1358 pr_err("setkey() failed flags=%x\n",
1359 crypto_skcipher_get_flags(tfm));
1360 goto out;
1361 }
1362
1363 iv_len = crypto_skcipher_ivsize(tfm);
1364 if (iv_len)
1365 memset(&iv, 0xff, iv_len);
1366
1367 /* Now setup per request stuff, i.e. buffers */
1368
1369 for (j = 0; j < num_mb; ++j) {
1370 struct test_mb_skcipher_data *cur = &data[j];
1371 unsigned int k = bs;
1372 unsigned int pages = DIV_ROUND_UP(k, PAGE_SIZE);
1373 unsigned int p = 0;
1374
1375 sg_init_table(cur->sg, pages);
1376
1377 while (k > PAGE_SIZE) {
1378 sg_set_buf(cur->sg + p, cur->xbuf[p],
1379 PAGE_SIZE);
1380 memset(cur->xbuf[p], 0xff, PAGE_SIZE);
1381 p++;
1382 k -= PAGE_SIZE;
1383 }
1384
1385 sg_set_buf(cur->sg + p, cur->xbuf[p], k);
1386 memset(cur->xbuf[p], 0xff, k);
1387
1388 skcipher_request_set_crypt(cur->req, cur->sg,
1389 cur->sg, *b_size,
1390 iv);
1391 }
1392
1393 if (secs) {
1394 ret = test_mb_acipher_jiffies(data, enc,
1395 bs, secs,
1396 num_mb);
1397 cond_resched();
1398 } else {
1399 ret = test_mb_acipher_cycles(data, enc,
1400 bs, num_mb);
1401 }
1402
1403 if (ret) {
1404 pr_err("%s() failed flags=%x\n", e,
1405 crypto_skcipher_get_flags(tfm));
1406 break;
1407 }
1408 b_size++;
1409 i++;
1410 } while (*b_size);
1411 keysize++;
1412 } while (*keysize);
1413
1414out:
1415 for (i = 0; i < num_mb; ++i)
1416 skcipher_request_free(data[i].req);
1417out_free_xbuf:
1418 for (i = 0; i < num_mb; ++i)
1419 testmgr_free_buf(data[i].xbuf);
1420out_free_tfm:
1421 crypto_free_skcipher(tfm);
1422out_free_data:
1423 kfree(data);
1424}
1425
1426static inline int do_one_acipher_op(struct skcipher_request *req, int ret)
1427{
1428 struct crypto_wait *wait = req->base.data;
1429
1430 return crypto_wait_req(ret, wait);
1431}
1432
1433static int test_acipher_jiffies(struct skcipher_request *req, int enc,
1434 int blen, int secs)
1435{
1436 unsigned long start, end;
1437 int bcount;
1438 int ret;
1439
1440 for (start = jiffies, end = start + secs * HZ, bcount = 0;
1441 time_before(jiffies, end); bcount++) {
1442 if (enc)
1443 ret = do_one_acipher_op(req,
1444 crypto_skcipher_encrypt(req));
1445 else
1446 ret = do_one_acipher_op(req,
1447 crypto_skcipher_decrypt(req));
1448
1449 if (ret)
1450 return ret;
1451 }
1452
1453 pr_cont("%d operations in %d seconds (%llu bytes)\n",
1454 bcount, secs, (u64)bcount * blen);
1455 return 0;
1456}
1457
1458static int test_acipher_cycles(struct skcipher_request *req, int enc,
1459 int blen)
1460{
1461 unsigned long cycles = 0;
1462 int ret = 0;
1463 int i;
1464
1465 /* Warm-up run. */
1466 for (i = 0; i < 4; i++) {
1467 if (enc)
1468 ret = do_one_acipher_op(req,
1469 crypto_skcipher_encrypt(req));
1470 else
1471 ret = do_one_acipher_op(req,
1472 crypto_skcipher_decrypt(req));
1473
1474 if (ret)
1475 goto out;
1476 }
1477
1478 /* The real thing. */
1479 for (i = 0; i < 8; i++) {
1480 cycles_t start, end;
1481
1482 start = get_cycles();
1483 if (enc)
1484 ret = do_one_acipher_op(req,
1485 crypto_skcipher_encrypt(req));
1486 else
1487 ret = do_one_acipher_op(req,
1488 crypto_skcipher_decrypt(req));
1489 end = get_cycles();
1490
1491 if (ret)
1492 goto out;
1493
1494 cycles += end - start;
1495 }
1496
1497out:
1498 if (ret == 0)
1499 pr_cont("1 operation in %lu cycles (%d bytes)\n",
1500 (cycles + 4) / 8, blen);
1501
1502 return ret;
1503}
1504
1505static void test_skcipher_speed(const char *algo, int enc, unsigned int secs,
1506 struct cipher_speed_template *template,
1507 unsigned int tcount, u8 *keysize, bool async)
1508{
1509 unsigned int ret, i, j, k, iv_len;
1510 struct crypto_wait wait;
1511 const char *key;
1512 char iv[128];
1513 struct skcipher_request *req;
1514 struct crypto_skcipher *tfm;
1515 const int *b_size;
1516 const char *e;
1517
1518 if (enc == ENCRYPT)
1519 e = "encryption";
1520 else
1521 e = "decryption";
1522
1523 crypto_init_wait(&wait);
1524
1525 tfm = crypto_alloc_skcipher(algo, 0, async ? 0 : CRYPTO_ALG_ASYNC);
1526
1527 if (IS_ERR(tfm)) {
1528 pr_err("failed to load transform for %s: %ld\n", algo,
1529 PTR_ERR(tfm));
1530 return;
1531 }
1532
1533 pr_info("\ntesting speed of %s %s (%s) %s\n", async ? "async" : "sync",
1534 algo, get_driver_name(crypto_skcipher, tfm), e);
1535
1536 req = skcipher_request_alloc(tfm, GFP_KERNEL);
1537 if (!req) {
1538 pr_err("tcrypt: skcipher: Failed to allocate request for %s\n",
1539 algo);
1540 goto out;
1541 }
1542
1543 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1544 crypto_req_done, &wait);
1545
1546 i = 0;
1547 do {
1548 b_size = block_sizes;
1549
1550 do {
1551 u32 bs = round_up(*b_size, crypto_skcipher_blocksize(tfm));
1552 struct scatterlist sg[TVMEMSIZE];
1553
1554 if ((*keysize + bs) > TVMEMSIZE * PAGE_SIZE) {
1555 pr_err("template (%u) too big for "
1556 "tvmem (%lu)\n", *keysize + bs,
1557 TVMEMSIZE * PAGE_SIZE);
1558 goto out_free_req;
1559 }
1560
1561 pr_info("test %u (%d bit key, %d byte blocks): ", i,
1562 *keysize * 8, bs);
1563
1564 memset(tvmem[0], 0xff, PAGE_SIZE);
1565
1566 /* set key, plain text and IV */
1567 key = tvmem[0];
1568 for (j = 0; j < tcount; j++) {
1569 if (template[j].klen == *keysize) {
1570 key = template[j].key;
1571 break;
1572 }
1573 }
1574
1575 crypto_skcipher_clear_flags(tfm, ~0);
1576
1577 ret = crypto_skcipher_setkey(tfm, key, *keysize);
1578 if (ret) {
1579 pr_err("setkey() failed flags=%x\n",
1580 crypto_skcipher_get_flags(tfm));
1581 goto out_free_req;
1582 }
1583
1584 k = *keysize + bs;
1585 sg_init_table(sg, DIV_ROUND_UP(k, PAGE_SIZE));
1586
1587 if (k > PAGE_SIZE) {
1588 sg_set_buf(sg, tvmem[0] + *keysize,
1589 PAGE_SIZE - *keysize);
1590 k -= PAGE_SIZE;
1591 j = 1;
1592 while (k > PAGE_SIZE) {
1593 sg_set_buf(sg + j, tvmem[j], PAGE_SIZE);
1594 memset(tvmem[j], 0xff, PAGE_SIZE);
1595 j++;
1596 k -= PAGE_SIZE;
1597 }
1598 sg_set_buf(sg + j, tvmem[j], k);
1599 memset(tvmem[j], 0xff, k);
1600 } else {
1601 sg_set_buf(sg, tvmem[0] + *keysize, bs);
1602 }
1603
1604 iv_len = crypto_skcipher_ivsize(tfm);
1605 if (iv_len)
1606 memset(&iv, 0xff, iv_len);
1607
1608 skcipher_request_set_crypt(req, sg, sg, bs, iv);
1609
1610 if (secs) {
1611 ret = test_acipher_jiffies(req, enc,
1612 bs, secs);
1613 cond_resched();
1614 } else {
1615 ret = test_acipher_cycles(req, enc,
1616 bs);
1617 }
1618
1619 if (ret) {
1620 pr_err("%s() failed flags=%x\n", e,
1621 crypto_skcipher_get_flags(tfm));
1622 break;
1623 }
1624 b_size++;
1625 i++;
1626 } while (*b_size);
1627 keysize++;
1628 } while (*keysize);
1629
1630out_free_req:
1631 skcipher_request_free(req);
1632out:
1633 crypto_free_skcipher(tfm);
1634}
1635
1636static void test_acipher_speed(const char *algo, int enc, unsigned int secs,
1637 struct cipher_speed_template *template,
1638 unsigned int tcount, u8 *keysize)
1639{
1640 return test_skcipher_speed(algo, enc, secs, template, tcount, keysize,
1641 true);
1642}
1643
1644static void test_cipher_speed(const char *algo, int enc, unsigned int secs,
1645 struct cipher_speed_template *template,
1646 unsigned int tcount, u8 *keysize)
1647{
1648 return test_skcipher_speed(algo, enc, secs, template, tcount, keysize,
1649 false);
1650}
1651
1652static void test_available(void)
1653{
1654 const char **name = check;
1655
1656 while (*name) {
1657 printk("alg %s ", *name);
1658 printk(crypto_has_alg(*name, 0, 0) ?
1659 "found\n" : "not found\n");
1660 name++;
1661 }
1662}
1663
1664static inline int tcrypt_test(const char *alg)
1665{
1666 int ret;
1667
1668 pr_debug("testing %s\n", alg);
1669
1670 ret = alg_test(alg, alg, 0, 0);
1671 /* non-fips algs return -EINVAL in fips mode */
1672 if (fips_enabled && ret == -EINVAL)
1673 ret = 0;
1674 return ret;
1675}
1676
1677static int do_test(const char *alg, u32 type, u32 mask, int m, u32 num_mb)
1678{
1679 int i;
1680 int ret = 0;
1681
1682 switch (m) {
1683 case 0:
1684 if (alg) {
1685 if (!crypto_has_alg(alg, type,
1686 mask ?: CRYPTO_ALG_TYPE_MASK))
1687 ret = -ENOENT;
1688 break;
1689 }
1690
1691 for (i = 1; i < 200; i++)
1692 ret += do_test(NULL, 0, 0, i, num_mb);
1693 break;
1694
1695 case 1:
1696 ret += tcrypt_test("md5");
1697 break;
1698
1699 case 2:
1700 ret += tcrypt_test("sha1");
1701 break;
1702
1703 case 3:
1704 ret += tcrypt_test("ecb(des)");
1705 ret += tcrypt_test("cbc(des)");
1706 ret += tcrypt_test("ctr(des)");
1707 break;
1708
1709 case 4:
1710 ret += tcrypt_test("ecb(des3_ede)");
1711 ret += tcrypt_test("cbc(des3_ede)");
1712 ret += tcrypt_test("ctr(des3_ede)");
1713 break;
1714
1715 case 5:
1716 ret += tcrypt_test("md4");
1717 break;
1718
1719 case 6:
1720 ret += tcrypt_test("sha256");
1721 break;
1722
1723 case 7:
1724 ret += tcrypt_test("ecb(blowfish)");
1725 ret += tcrypt_test("cbc(blowfish)");
1726 ret += tcrypt_test("ctr(blowfish)");
1727 break;
1728
1729 case 8:
1730 ret += tcrypt_test("ecb(twofish)");
1731 ret += tcrypt_test("cbc(twofish)");
1732 ret += tcrypt_test("ctr(twofish)");
1733 ret += tcrypt_test("lrw(twofish)");
1734 ret += tcrypt_test("xts(twofish)");
1735 break;
1736
1737 case 9:
1738 ret += tcrypt_test("ecb(serpent)");
1739 ret += tcrypt_test("cbc(serpent)");
1740 ret += tcrypt_test("ctr(serpent)");
1741 ret += tcrypt_test("lrw(serpent)");
1742 ret += tcrypt_test("xts(serpent)");
1743 break;
1744
1745 case 10:
1746 ret += tcrypt_test("ecb(aes)");
1747 ret += tcrypt_test("cbc(aes)");
1748 ret += tcrypt_test("lrw(aes)");
1749 ret += tcrypt_test("xts(aes)");
1750 ret += tcrypt_test("ctr(aes)");
1751 ret += tcrypt_test("rfc3686(ctr(aes))");
1752 ret += tcrypt_test("ofb(aes)");
1753 ret += tcrypt_test("cfb(aes)");
1754 break;
1755
1756 case 11:
1757 ret += tcrypt_test("sha384");
1758 break;
1759
1760 case 12:
1761 ret += tcrypt_test("sha512");
1762 break;
1763
1764 case 13:
1765 ret += tcrypt_test("deflate");
1766 break;
1767
1768 case 14:
1769 ret += tcrypt_test("ecb(cast5)");
1770 ret += tcrypt_test("cbc(cast5)");
1771 ret += tcrypt_test("ctr(cast5)");
1772 break;
1773
1774 case 15:
1775 ret += tcrypt_test("ecb(cast6)");
1776 ret += tcrypt_test("cbc(cast6)");
1777 ret += tcrypt_test("ctr(cast6)");
1778 ret += tcrypt_test("lrw(cast6)");
1779 ret += tcrypt_test("xts(cast6)");
1780 break;
1781
1782 case 16:
1783 ret += tcrypt_test("ecb(arc4)");
1784 break;
1785
1786 case 17:
1787 ret += tcrypt_test("michael_mic");
1788 break;
1789
1790 case 18:
1791 ret += tcrypt_test("crc32c");
1792 break;
1793
1794 case 19:
1795 ret += tcrypt_test("ecb(tea)");
1796 break;
1797
1798 case 20:
1799 ret += tcrypt_test("ecb(xtea)");
1800 break;
1801
1802 case 21:
1803 ret += tcrypt_test("ecb(khazad)");
1804 break;
1805
1806 case 22:
1807 ret += tcrypt_test("wp512");
1808 break;
1809
1810 case 23:
1811 ret += tcrypt_test("wp384");
1812 break;
1813
1814 case 24:
1815 ret += tcrypt_test("wp256");
1816 break;
1817
1818 case 26:
1819 ret += tcrypt_test("ecb(anubis)");
1820 ret += tcrypt_test("cbc(anubis)");
1821 break;
1822
1823 case 30:
1824 ret += tcrypt_test("ecb(xeta)");
1825 break;
1826
1827 case 31:
1828 ret += tcrypt_test("pcbc(fcrypt)");
1829 break;
1830
1831 case 32:
1832 ret += tcrypt_test("ecb(camellia)");
1833 ret += tcrypt_test("cbc(camellia)");
1834 ret += tcrypt_test("ctr(camellia)");
1835 ret += tcrypt_test("lrw(camellia)");
1836 ret += tcrypt_test("xts(camellia)");
1837 break;
1838
1839 case 33:
1840 ret += tcrypt_test("sha224");
1841 break;
1842
1843 case 35:
1844 ret += tcrypt_test("gcm(aes)");
1845 break;
1846
1847 case 36:
1848 ret += tcrypt_test("lzo");
1849 break;
1850
1851 case 37:
1852 ret += tcrypt_test("ccm(aes)");
1853 break;
1854
1855 case 38:
1856 ret += tcrypt_test("cts(cbc(aes))");
1857 break;
1858
1859 case 39:
1860 ret += tcrypt_test("xxhash64");
1861 break;
1862
1863 case 40:
1864 ret += tcrypt_test("rmd160");
1865 break;
1866
1867 case 41:
1868 ret += tcrypt_test("blake2s-256");
1869 break;
1870
1871 case 42:
1872 ret += tcrypt_test("blake2b-512");
1873 break;
1874
1875 case 43:
1876 ret += tcrypt_test("ecb(seed)");
1877 break;
1878
1879 case 45:
1880 ret += tcrypt_test("rfc4309(ccm(aes))");
1881 break;
1882
1883 case 46:
1884 ret += tcrypt_test("ghash");
1885 break;
1886
1887 case 47:
1888 ret += tcrypt_test("crct10dif");
1889 break;
1890
1891 case 48:
1892 ret += tcrypt_test("sha3-224");
1893 break;
1894
1895 case 49:
1896 ret += tcrypt_test("sha3-256");
1897 break;
1898
1899 case 50:
1900 ret += tcrypt_test("sha3-384");
1901 break;
1902
1903 case 51:
1904 ret += tcrypt_test("sha3-512");
1905 break;
1906
1907 case 52:
1908 ret += tcrypt_test("sm3");
1909 break;
1910
1911 case 53:
1912 ret += tcrypt_test("streebog256");
1913 break;
1914
1915 case 54:
1916 ret += tcrypt_test("streebog512");
1917 break;
1918
1919 case 100:
1920 ret += tcrypt_test("hmac(md5)");
1921 break;
1922
1923 case 101:
1924 ret += tcrypt_test("hmac(sha1)");
1925 break;
1926
1927 case 102:
1928 ret += tcrypt_test("hmac(sha256)");
1929 break;
1930
1931 case 103:
1932 ret += tcrypt_test("hmac(sha384)");
1933 break;
1934
1935 case 104:
1936 ret += tcrypt_test("hmac(sha512)");
1937 break;
1938
1939 case 105:
1940 ret += tcrypt_test("hmac(sha224)");
1941 break;
1942
1943 case 106:
1944 ret += tcrypt_test("xcbc(aes)");
1945 break;
1946
1947 case 108:
1948 ret += tcrypt_test("hmac(rmd160)");
1949 break;
1950
1951 case 109:
1952 ret += tcrypt_test("vmac64(aes)");
1953 break;
1954
1955 case 111:
1956 ret += tcrypt_test("hmac(sha3-224)");
1957 break;
1958
1959 case 112:
1960 ret += tcrypt_test("hmac(sha3-256)");
1961 break;
1962
1963 case 113:
1964 ret += tcrypt_test("hmac(sha3-384)");
1965 break;
1966
1967 case 114:
1968 ret += tcrypt_test("hmac(sha3-512)");
1969 break;
1970
1971 case 115:
1972 ret += tcrypt_test("hmac(streebog256)");
1973 break;
1974
1975 case 116:
1976 ret += tcrypt_test("hmac(streebog512)");
1977 break;
1978
1979 case 150:
1980 ret += tcrypt_test("ansi_cprng");
1981 break;
1982
1983 case 151:
1984 ret += tcrypt_test("rfc4106(gcm(aes))");
1985 break;
1986
1987 case 152:
1988 ret += tcrypt_test("rfc4543(gcm(aes))");
1989 break;
1990
1991 case 153:
1992 ret += tcrypt_test("cmac(aes)");
1993 break;
1994
1995 case 154:
1996 ret += tcrypt_test("cmac(des3_ede)");
1997 break;
1998
1999 case 155:
2000 ret += tcrypt_test("authenc(hmac(sha1),cbc(aes))");
2001 break;
2002
2003 case 156:
2004 ret += tcrypt_test("authenc(hmac(md5),ecb(cipher_null))");
2005 break;
2006
2007 case 157:
2008 ret += tcrypt_test("authenc(hmac(sha1),ecb(cipher_null))");
2009 break;
2010 case 181:
2011 ret += tcrypt_test("authenc(hmac(sha1),cbc(des))");
2012 break;
2013 case 182:
2014 ret += tcrypt_test("authenc(hmac(sha1),cbc(des3_ede))");
2015 break;
2016 case 183:
2017 ret += tcrypt_test("authenc(hmac(sha224),cbc(des))");
2018 break;
2019 case 184:
2020 ret += tcrypt_test("authenc(hmac(sha224),cbc(des3_ede))");
2021 break;
2022 case 185:
2023 ret += tcrypt_test("authenc(hmac(sha256),cbc(des))");
2024 break;
2025 case 186:
2026 ret += tcrypt_test("authenc(hmac(sha256),cbc(des3_ede))");
2027 break;
2028 case 187:
2029 ret += tcrypt_test("authenc(hmac(sha384),cbc(des))");
2030 break;
2031 case 188:
2032 ret += tcrypt_test("authenc(hmac(sha384),cbc(des3_ede))");
2033 break;
2034 case 189:
2035 ret += tcrypt_test("authenc(hmac(sha512),cbc(des))");
2036 break;
2037 case 190:
2038 ret += tcrypt_test("authenc(hmac(sha512),cbc(des3_ede))");
2039 break;
2040 case 191:
2041 ret += tcrypt_test("ecb(sm4)");
2042 ret += tcrypt_test("cbc(sm4)");
2043 ret += tcrypt_test("ctr(sm4)");
2044 break;
2045 case 200:
2046 test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
2047 speed_template_16_24_32);
2048 test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
2049 speed_template_16_24_32);
2050 test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
2051 speed_template_16_24_32);
2052 test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
2053 speed_template_16_24_32);
2054 test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
2055 speed_template_32_40_48);
2056 test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
2057 speed_template_32_40_48);
2058 test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
2059 speed_template_32_64);
2060 test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
2061 speed_template_32_64);
2062 test_cipher_speed("cts(cbc(aes))", ENCRYPT, sec, NULL, 0,
2063 speed_template_16_24_32);
2064 test_cipher_speed("cts(cbc(aes))", DECRYPT, sec, NULL, 0,
2065 speed_template_16_24_32);
2066 test_cipher_speed("ctr(aes)", ENCRYPT, sec, NULL, 0,
2067 speed_template_16_24_32);
2068 test_cipher_speed("ctr(aes)", DECRYPT, sec, NULL, 0,
2069 speed_template_16_24_32);
2070 test_cipher_speed("cfb(aes)", ENCRYPT, sec, NULL, 0,
2071 speed_template_16_24_32);
2072 test_cipher_speed("cfb(aes)", DECRYPT, sec, NULL, 0,
2073 speed_template_16_24_32);
2074 break;
2075
2076 case 201:
2077 test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
2078 des3_speed_template, DES3_SPEED_VECTORS,
2079 speed_template_24);
2080 test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
2081 des3_speed_template, DES3_SPEED_VECTORS,
2082 speed_template_24);
2083 test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
2084 des3_speed_template, DES3_SPEED_VECTORS,
2085 speed_template_24);
2086 test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
2087 des3_speed_template, DES3_SPEED_VECTORS,
2088 speed_template_24);
2089 test_cipher_speed("ctr(des3_ede)", ENCRYPT, sec,
2090 des3_speed_template, DES3_SPEED_VECTORS,
2091 speed_template_24);
2092 test_cipher_speed("ctr(des3_ede)", DECRYPT, sec,
2093 des3_speed_template, DES3_SPEED_VECTORS,
2094 speed_template_24);
2095 break;
2096
2097 case 202:
2098 test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
2099 speed_template_16_24_32);
2100 test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
2101 speed_template_16_24_32);
2102 test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
2103 speed_template_16_24_32);
2104 test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
2105 speed_template_16_24_32);
2106 test_cipher_speed("ctr(twofish)", ENCRYPT, sec, NULL, 0,
2107 speed_template_16_24_32);
2108 test_cipher_speed("ctr(twofish)", DECRYPT, sec, NULL, 0,
2109 speed_template_16_24_32);
2110 test_cipher_speed("lrw(twofish)", ENCRYPT, sec, NULL, 0,
2111 speed_template_32_40_48);
2112 test_cipher_speed("lrw(twofish)", DECRYPT, sec, NULL, 0,
2113 speed_template_32_40_48);
2114 test_cipher_speed("xts(twofish)", ENCRYPT, sec, NULL, 0,
2115 speed_template_32_48_64);
2116 test_cipher_speed("xts(twofish)", DECRYPT, sec, NULL, 0,
2117 speed_template_32_48_64);
2118 break;
2119
2120 case 203:
2121 test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
2122 speed_template_8_32);
2123 test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
2124 speed_template_8_32);
2125 test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
2126 speed_template_8_32);
2127 test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
2128 speed_template_8_32);
2129 test_cipher_speed("ctr(blowfish)", ENCRYPT, sec, NULL, 0,
2130 speed_template_8_32);
2131 test_cipher_speed("ctr(blowfish)", DECRYPT, sec, NULL, 0,
2132 speed_template_8_32);
2133 break;
2134
2135 case 204:
2136 test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
2137 speed_template_8);
2138 test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
2139 speed_template_8);
2140 test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
2141 speed_template_8);
2142 test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
2143 speed_template_8);
2144 break;
2145
2146 case 205:
2147 test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
2148 speed_template_16_24_32);
2149 test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
2150 speed_template_16_24_32);
2151 test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
2152 speed_template_16_24_32);
2153 test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
2154 speed_template_16_24_32);
2155 test_cipher_speed("ctr(camellia)", ENCRYPT, sec, NULL, 0,
2156 speed_template_16_24_32);
2157 test_cipher_speed("ctr(camellia)", DECRYPT, sec, NULL, 0,
2158 speed_template_16_24_32);
2159 test_cipher_speed("lrw(camellia)", ENCRYPT, sec, NULL, 0,
2160 speed_template_32_40_48);
2161 test_cipher_speed("lrw(camellia)", DECRYPT, sec, NULL, 0,
2162 speed_template_32_40_48);
2163 test_cipher_speed("xts(camellia)", ENCRYPT, sec, NULL, 0,
2164 speed_template_32_48_64);
2165 test_cipher_speed("xts(camellia)", DECRYPT, sec, NULL, 0,
2166 speed_template_32_48_64);
2167 break;
2168
2169 case 207:
2170 test_cipher_speed("ecb(serpent)", ENCRYPT, sec, NULL, 0,
2171 speed_template_16_32);
2172 test_cipher_speed("ecb(serpent)", DECRYPT, sec, NULL, 0,
2173 speed_template_16_32);
2174 test_cipher_speed("cbc(serpent)", ENCRYPT, sec, NULL, 0,
2175 speed_template_16_32);
2176 test_cipher_speed("cbc(serpent)", DECRYPT, sec, NULL, 0,
2177 speed_template_16_32);
2178 test_cipher_speed("ctr(serpent)", ENCRYPT, sec, NULL, 0,
2179 speed_template_16_32);
2180 test_cipher_speed("ctr(serpent)", DECRYPT, sec, NULL, 0,
2181 speed_template_16_32);
2182 test_cipher_speed("lrw(serpent)", ENCRYPT, sec, NULL, 0,
2183 speed_template_32_48);
2184 test_cipher_speed("lrw(serpent)", DECRYPT, sec, NULL, 0,
2185 speed_template_32_48);
2186 test_cipher_speed("xts(serpent)", ENCRYPT, sec, NULL, 0,
2187 speed_template_32_64);
2188 test_cipher_speed("xts(serpent)", DECRYPT, sec, NULL, 0,
2189 speed_template_32_64);
2190 break;
2191
2192 case 208:
2193 test_cipher_speed("ecb(arc4)", ENCRYPT, sec, NULL, 0,
2194 speed_template_8);
2195 break;
2196
2197 case 209:
2198 test_cipher_speed("ecb(cast5)", ENCRYPT, sec, NULL, 0,
2199 speed_template_8_16);
2200 test_cipher_speed("ecb(cast5)", DECRYPT, sec, NULL, 0,
2201 speed_template_8_16);
2202 test_cipher_speed("cbc(cast5)", ENCRYPT, sec, NULL, 0,
2203 speed_template_8_16);
2204 test_cipher_speed("cbc(cast5)", DECRYPT, sec, NULL, 0,
2205 speed_template_8_16);
2206 test_cipher_speed("ctr(cast5)", ENCRYPT, sec, NULL, 0,
2207 speed_template_8_16);
2208 test_cipher_speed("ctr(cast5)", DECRYPT, sec, NULL, 0,
2209 speed_template_8_16);
2210 break;
2211
2212 case 210:
2213 test_cipher_speed("ecb(cast6)", ENCRYPT, sec, NULL, 0,
2214 speed_template_16_32);
2215 test_cipher_speed("ecb(cast6)", DECRYPT, sec, NULL, 0,
2216 speed_template_16_32);
2217 test_cipher_speed("cbc(cast6)", ENCRYPT, sec, NULL, 0,
2218 speed_template_16_32);
2219 test_cipher_speed("cbc(cast6)", DECRYPT, sec, NULL, 0,
2220 speed_template_16_32);
2221 test_cipher_speed("ctr(cast6)", ENCRYPT, sec, NULL, 0,
2222 speed_template_16_32);
2223 test_cipher_speed("ctr(cast6)", DECRYPT, sec, NULL, 0,
2224 speed_template_16_32);
2225 test_cipher_speed("lrw(cast6)", ENCRYPT, sec, NULL, 0,
2226 speed_template_32_48);
2227 test_cipher_speed("lrw(cast6)", DECRYPT, sec, NULL, 0,
2228 speed_template_32_48);
2229 test_cipher_speed("xts(cast6)", ENCRYPT, sec, NULL, 0,
2230 speed_template_32_64);
2231 test_cipher_speed("xts(cast6)", DECRYPT, sec, NULL, 0,
2232 speed_template_32_64);
2233 break;
2234
2235 case 211:
2236 test_aead_speed("rfc4106(gcm(aes))", ENCRYPT, sec,
2237 NULL, 0, 16, 16, aead_speed_template_20);
2238 test_aead_speed("gcm(aes)", ENCRYPT, sec,
2239 NULL, 0, 16, 8, speed_template_16_24_32);
2240 test_aead_speed("rfc4106(gcm(aes))", DECRYPT, sec,
2241 NULL, 0, 16, 16, aead_speed_template_20);
2242 test_aead_speed("gcm(aes)", DECRYPT, sec,
2243 NULL, 0, 16, 8, speed_template_16_24_32);
2244 break;
2245
2246 case 212:
2247 test_aead_speed("rfc4309(ccm(aes))", ENCRYPT, sec,
2248 NULL, 0, 16, 16, aead_speed_template_19);
2249 test_aead_speed("rfc4309(ccm(aes))", DECRYPT, sec,
2250 NULL, 0, 16, 16, aead_speed_template_19);
2251 break;
2252
2253 case 213:
2254 test_aead_speed("rfc7539esp(chacha20,poly1305)", ENCRYPT, sec,
2255 NULL, 0, 16, 8, aead_speed_template_36);
2256 test_aead_speed("rfc7539esp(chacha20,poly1305)", DECRYPT, sec,
2257 NULL, 0, 16, 8, aead_speed_template_36);
2258 break;
2259
2260 case 214:
2261 test_cipher_speed("chacha20", ENCRYPT, sec, NULL, 0,
2262 speed_template_32);
2263 break;
2264
2265 case 215:
2266 test_mb_aead_speed("rfc4106(gcm(aes))", ENCRYPT, sec, NULL,
2267 0, 16, 16, aead_speed_template_20, num_mb);
2268 test_mb_aead_speed("gcm(aes)", ENCRYPT, sec, NULL, 0, 16, 8,
2269 speed_template_16_24_32, num_mb);
2270 test_mb_aead_speed("rfc4106(gcm(aes))", DECRYPT, sec, NULL,
2271 0, 16, 16, aead_speed_template_20, num_mb);
2272 test_mb_aead_speed("gcm(aes)", DECRYPT, sec, NULL, 0, 16, 8,
2273 speed_template_16_24_32, num_mb);
2274 break;
2275
2276 case 216:
2277 test_mb_aead_speed("rfc4309(ccm(aes))", ENCRYPT, sec, NULL, 0,
2278 16, 16, aead_speed_template_19, num_mb);
2279 test_mb_aead_speed("rfc4309(ccm(aes))", DECRYPT, sec, NULL, 0,
2280 16, 16, aead_speed_template_19, num_mb);
2281 break;
2282
2283 case 217:
2284 test_mb_aead_speed("rfc7539esp(chacha20,poly1305)", ENCRYPT,
2285 sec, NULL, 0, 16, 8, aead_speed_template_36,
2286 num_mb);
2287 test_mb_aead_speed("rfc7539esp(chacha20,poly1305)", DECRYPT,
2288 sec, NULL, 0, 16, 8, aead_speed_template_36,
2289 num_mb);
2290 break;
2291
2292 case 218:
2293 test_cipher_speed("ecb(sm4)", ENCRYPT, sec, NULL, 0,
2294 speed_template_16);
2295 test_cipher_speed("ecb(sm4)", DECRYPT, sec, NULL, 0,
2296 speed_template_16);
2297 test_cipher_speed("cbc(sm4)", ENCRYPT, sec, NULL, 0,
2298 speed_template_16);
2299 test_cipher_speed("cbc(sm4)", DECRYPT, sec, NULL, 0,
2300 speed_template_16);
2301 test_cipher_speed("ctr(sm4)", ENCRYPT, sec, NULL, 0,
2302 speed_template_16);
2303 test_cipher_speed("ctr(sm4)", DECRYPT, sec, NULL, 0,
2304 speed_template_16);
2305 break;
2306
2307 case 219:
2308 test_cipher_speed("adiantum(xchacha12,aes)", ENCRYPT, sec, NULL,
2309 0, speed_template_32);
2310 test_cipher_speed("adiantum(xchacha12,aes)", DECRYPT, sec, NULL,
2311 0, speed_template_32);
2312 test_cipher_speed("adiantum(xchacha20,aes)", ENCRYPT, sec, NULL,
2313 0, speed_template_32);
2314 test_cipher_speed("adiantum(xchacha20,aes)", DECRYPT, sec, NULL,
2315 0, speed_template_32);
2316 break;
2317
2318 case 220:
2319 test_acipher_speed("essiv(cbc(aes),sha256)",
2320 ENCRYPT, sec, NULL, 0,
2321 speed_template_16_24_32);
2322 test_acipher_speed("essiv(cbc(aes),sha256)",
2323 DECRYPT, sec, NULL, 0,
2324 speed_template_16_24_32);
2325 break;
2326
2327 case 221:
2328 test_aead_speed("aegis128", ENCRYPT, sec,
2329 NULL, 0, 16, 8, speed_template_16);
2330 test_aead_speed("aegis128", DECRYPT, sec,
2331 NULL, 0, 16, 8, speed_template_16);
2332 break;
2333
2334 case 300:
2335 if (alg) {
2336 test_hash_speed(alg, sec, generic_hash_speed_template);
2337 break;
2338 }
2339 fallthrough;
2340 case 301:
2341 test_hash_speed("md4", sec, generic_hash_speed_template);
2342 if (mode > 300 && mode < 400) break;
2343 fallthrough;
2344 case 302:
2345 test_hash_speed("md5", sec, generic_hash_speed_template);
2346 if (mode > 300 && mode < 400) break;
2347 fallthrough;
2348 case 303:
2349 test_hash_speed("sha1", sec, generic_hash_speed_template);
2350 if (mode > 300 && mode < 400) break;
2351 fallthrough;
2352 case 304:
2353 test_hash_speed("sha256", sec, generic_hash_speed_template);
2354 if (mode > 300 && mode < 400) break;
2355 fallthrough;
2356 case 305:
2357 test_hash_speed("sha384", sec, generic_hash_speed_template);
2358 if (mode > 300 && mode < 400) break;
2359 fallthrough;
2360 case 306:
2361 test_hash_speed("sha512", sec, generic_hash_speed_template);
2362 if (mode > 300 && mode < 400) break;
2363 fallthrough;
2364 case 307:
2365 test_hash_speed("wp256", sec, generic_hash_speed_template);
2366 if (mode > 300 && mode < 400) break;
2367 fallthrough;
2368 case 308:
2369 test_hash_speed("wp384", sec, generic_hash_speed_template);
2370 if (mode > 300 && mode < 400) break;
2371 fallthrough;
2372 case 309:
2373 test_hash_speed("wp512", sec, generic_hash_speed_template);
2374 if (mode > 300 && mode < 400) break;
2375 fallthrough;
2376 case 313:
2377 test_hash_speed("sha224", sec, generic_hash_speed_template);
2378 if (mode > 300 && mode < 400) break;
2379 fallthrough;
2380 case 314:
2381 test_hash_speed("xxhash64", sec, generic_hash_speed_template);
2382 if (mode > 300 && mode < 400) break;
2383 fallthrough;
2384 case 315:
2385 test_hash_speed("rmd160", sec, generic_hash_speed_template);
2386 if (mode > 300 && mode < 400) break;
2387 fallthrough;
2388 case 316:
2389 test_hash_speed("blake2s-256", sec, generic_hash_speed_template);
2390 if (mode > 300 && mode < 400) break;
2391 fallthrough;
2392 case 317:
2393 test_hash_speed("blake2b-512", sec, generic_hash_speed_template);
2394 if (mode > 300 && mode < 400) break;
2395 fallthrough;
2396 case 318:
2397 klen = 16;
2398 test_hash_speed("ghash", sec, generic_hash_speed_template);
2399 if (mode > 300 && mode < 400) break;
2400 fallthrough;
2401 case 319:
2402 test_hash_speed("crc32c", sec, generic_hash_speed_template);
2403 if (mode > 300 && mode < 400) break;
2404 fallthrough;
2405 case 320:
2406 test_hash_speed("crct10dif", sec, generic_hash_speed_template);
2407 if (mode > 300 && mode < 400) break;
2408 fallthrough;
2409 case 321:
2410 test_hash_speed("poly1305", sec, poly1305_speed_template);
2411 if (mode > 300 && mode < 400) break;
2412 fallthrough;
2413 case 322:
2414 test_hash_speed("sha3-224", sec, generic_hash_speed_template);
2415 if (mode > 300 && mode < 400) break;
2416 fallthrough;
2417 case 323:
2418 test_hash_speed("sha3-256", sec, generic_hash_speed_template);
2419 if (mode > 300 && mode < 400) break;
2420 fallthrough;
2421 case 324:
2422 test_hash_speed("sha3-384", sec, generic_hash_speed_template);
2423 if (mode > 300 && mode < 400) break;
2424 fallthrough;
2425 case 325:
2426 test_hash_speed("sha3-512", sec, generic_hash_speed_template);
2427 if (mode > 300 && mode < 400) break;
2428 fallthrough;
2429 case 326:
2430 test_hash_speed("sm3", sec, generic_hash_speed_template);
2431 if (mode > 300 && mode < 400) break;
2432 fallthrough;
2433 case 327:
2434 test_hash_speed("streebog256", sec,
2435 generic_hash_speed_template);
2436 if (mode > 300 && mode < 400) break;
2437 fallthrough;
2438 case 328:
2439 test_hash_speed("streebog512", sec,
2440 generic_hash_speed_template);
2441 if (mode > 300 && mode < 400) break;
2442 fallthrough;
2443 case 399:
2444 break;
2445
2446 case 400:
2447 if (alg) {
2448 test_ahash_speed(alg, sec, generic_hash_speed_template);
2449 break;
2450 }
2451 fallthrough;
2452 case 401:
2453 test_ahash_speed("md4", sec, generic_hash_speed_template);
2454 if (mode > 400 && mode < 500) break;
2455 fallthrough;
2456 case 402:
2457 test_ahash_speed("md5", sec, generic_hash_speed_template);
2458 if (mode > 400 && mode < 500) break;
2459 fallthrough;
2460 case 403:
2461 test_ahash_speed("sha1", sec, generic_hash_speed_template);
2462 if (mode > 400 && mode < 500) break;
2463 fallthrough;
2464 case 404:
2465 test_ahash_speed("sha256", sec, generic_hash_speed_template);
2466 if (mode > 400 && mode < 500) break;
2467 fallthrough;
2468 case 405:
2469 test_ahash_speed("sha384", sec, generic_hash_speed_template);
2470 if (mode > 400 && mode < 500) break;
2471 fallthrough;
2472 case 406:
2473 test_ahash_speed("sha512", sec, generic_hash_speed_template);
2474 if (mode > 400 && mode < 500) break;
2475 fallthrough;
2476 case 407:
2477 test_ahash_speed("wp256", sec, generic_hash_speed_template);
2478 if (mode > 400 && mode < 500) break;
2479 fallthrough;
2480 case 408:
2481 test_ahash_speed("wp384", sec, generic_hash_speed_template);
2482 if (mode > 400 && mode < 500) break;
2483 fallthrough;
2484 case 409:
2485 test_ahash_speed("wp512", sec, generic_hash_speed_template);
2486 if (mode > 400 && mode < 500) break;
2487 fallthrough;
2488 case 413:
2489 test_ahash_speed("sha224", sec, generic_hash_speed_template);
2490 if (mode > 400 && mode < 500) break;
2491 fallthrough;
2492 case 414:
2493 test_ahash_speed("xxhash64", sec, generic_hash_speed_template);
2494 if (mode > 400 && mode < 500) break;
2495 fallthrough;
2496 case 415:
2497 test_ahash_speed("rmd160", sec, generic_hash_speed_template);
2498 if (mode > 400 && mode < 500) break;
2499 fallthrough;
2500 case 416:
2501 test_ahash_speed("blake2s-256", sec, generic_hash_speed_template);
2502 if (mode > 400 && mode < 500) break;
2503 fallthrough;
2504 case 417:
2505 test_ahash_speed("blake2b-512", sec, generic_hash_speed_template);
2506 if (mode > 400 && mode < 500) break;
2507 fallthrough;
2508 case 418:
2509 test_ahash_speed("sha3-224", sec, generic_hash_speed_template);
2510 if (mode > 400 && mode < 500) break;
2511 fallthrough;
2512 case 419:
2513 test_ahash_speed("sha3-256", sec, generic_hash_speed_template);
2514 if (mode > 400 && mode < 500) break;
2515 fallthrough;
2516 case 420:
2517 test_ahash_speed("sha3-384", sec, generic_hash_speed_template);
2518 if (mode > 400 && mode < 500) break;
2519 fallthrough;
2520 case 421:
2521 test_ahash_speed("sha3-512", sec, generic_hash_speed_template);
2522 if (mode > 400 && mode < 500) break;
2523 fallthrough;
2524 case 422:
2525 test_mb_ahash_speed("sha1", sec, generic_hash_speed_template,
2526 num_mb);
2527 if (mode > 400 && mode < 500) break;
2528 fallthrough;
2529 case 423:
2530 test_mb_ahash_speed("sha256", sec, generic_hash_speed_template,
2531 num_mb);
2532 if (mode > 400 && mode < 500) break;
2533 fallthrough;
2534 case 424:
2535 test_mb_ahash_speed("sha512", sec, generic_hash_speed_template,
2536 num_mb);
2537 if (mode > 400 && mode < 500) break;
2538 fallthrough;
2539 case 425:
2540 test_mb_ahash_speed("sm3", sec, generic_hash_speed_template,
2541 num_mb);
2542 if (mode > 400 && mode < 500) break;
2543 fallthrough;
2544 case 426:
2545 test_mb_ahash_speed("streebog256", sec,
2546 generic_hash_speed_template, num_mb);
2547 if (mode > 400 && mode < 500) break;
2548 fallthrough;
2549 case 427:
2550 test_mb_ahash_speed("streebog512", sec,
2551 generic_hash_speed_template, num_mb);
2552 if (mode > 400 && mode < 500) break;
2553 fallthrough;
2554 case 499:
2555 break;
2556
2557 case 500:
2558 test_acipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
2559 speed_template_16_24_32);
2560 test_acipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
2561 speed_template_16_24_32);
2562 test_acipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
2563 speed_template_16_24_32);
2564 test_acipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
2565 speed_template_16_24_32);
2566 test_acipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
2567 speed_template_32_40_48);
2568 test_acipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
2569 speed_template_32_40_48);
2570 test_acipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
2571 speed_template_32_64);
2572 test_acipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
2573 speed_template_32_64);
2574 test_acipher_speed("cts(cbc(aes))", ENCRYPT, sec, NULL, 0,
2575 speed_template_16_24_32);
2576 test_acipher_speed("cts(cbc(aes))", DECRYPT, sec, NULL, 0,
2577 speed_template_16_24_32);
2578 test_acipher_speed("ctr(aes)", ENCRYPT, sec, NULL, 0,
2579 speed_template_16_24_32);
2580 test_acipher_speed("ctr(aes)", DECRYPT, sec, NULL, 0,
2581 speed_template_16_24_32);
2582 test_acipher_speed("cfb(aes)", ENCRYPT, sec, NULL, 0,
2583 speed_template_16_24_32);
2584 test_acipher_speed("cfb(aes)", DECRYPT, sec, NULL, 0,
2585 speed_template_16_24_32);
2586 test_acipher_speed("ofb(aes)", ENCRYPT, sec, NULL, 0,
2587 speed_template_16_24_32);
2588 test_acipher_speed("ofb(aes)", DECRYPT, sec, NULL, 0,
2589 speed_template_16_24_32);
2590 test_acipher_speed("rfc3686(ctr(aes))", ENCRYPT, sec, NULL, 0,
2591 speed_template_20_28_36);
2592 test_acipher_speed("rfc3686(ctr(aes))", DECRYPT, sec, NULL, 0,
2593 speed_template_20_28_36);
2594 break;
2595
2596 case 501:
2597 test_acipher_speed("ecb(des3_ede)", ENCRYPT, sec,
2598 des3_speed_template, DES3_SPEED_VECTORS,
2599 speed_template_24);
2600 test_acipher_speed("ecb(des3_ede)", DECRYPT, sec,
2601 des3_speed_template, DES3_SPEED_VECTORS,
2602 speed_template_24);
2603 test_acipher_speed("cbc(des3_ede)", ENCRYPT, sec,
2604 des3_speed_template, DES3_SPEED_VECTORS,
2605 speed_template_24);
2606 test_acipher_speed("cbc(des3_ede)", DECRYPT, sec,
2607 des3_speed_template, DES3_SPEED_VECTORS,
2608 speed_template_24);
2609 test_acipher_speed("cfb(des3_ede)", ENCRYPT, sec,
2610 des3_speed_template, DES3_SPEED_VECTORS,
2611 speed_template_24);
2612 test_acipher_speed("cfb(des3_ede)", DECRYPT, sec,
2613 des3_speed_template, DES3_SPEED_VECTORS,
2614 speed_template_24);
2615 test_acipher_speed("ofb(des3_ede)", ENCRYPT, sec,
2616 des3_speed_template, DES3_SPEED_VECTORS,
2617 speed_template_24);
2618 test_acipher_speed("ofb(des3_ede)", DECRYPT, sec,
2619 des3_speed_template, DES3_SPEED_VECTORS,
2620 speed_template_24);
2621 break;
2622
2623 case 502:
2624 test_acipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
2625 speed_template_8);
2626 test_acipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
2627 speed_template_8);
2628 test_acipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
2629 speed_template_8);
2630 test_acipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
2631 speed_template_8);
2632 test_acipher_speed("cfb(des)", ENCRYPT, sec, NULL, 0,
2633 speed_template_8);
2634 test_acipher_speed("cfb(des)", DECRYPT, sec, NULL, 0,
2635 speed_template_8);
2636 test_acipher_speed("ofb(des)", ENCRYPT, sec, NULL, 0,
2637 speed_template_8);
2638 test_acipher_speed("ofb(des)", DECRYPT, sec, NULL, 0,
2639 speed_template_8);
2640 break;
2641
2642 case 503:
2643 test_acipher_speed("ecb(serpent)", ENCRYPT, sec, NULL, 0,
2644 speed_template_16_32);
2645 test_acipher_speed("ecb(serpent)", DECRYPT, sec, NULL, 0,
2646 speed_template_16_32);
2647 test_acipher_speed("cbc(serpent)", ENCRYPT, sec, NULL, 0,
2648 speed_template_16_32);
2649 test_acipher_speed("cbc(serpent)", DECRYPT, sec, NULL, 0,
2650 speed_template_16_32);
2651 test_acipher_speed("ctr(serpent)", ENCRYPT, sec, NULL, 0,
2652 speed_template_16_32);
2653 test_acipher_speed("ctr(serpent)", DECRYPT, sec, NULL, 0,
2654 speed_template_16_32);
2655 test_acipher_speed("lrw(serpent)", ENCRYPT, sec, NULL, 0,
2656 speed_template_32_48);
2657 test_acipher_speed("lrw(serpent)", DECRYPT, sec, NULL, 0,
2658 speed_template_32_48);
2659 test_acipher_speed("xts(serpent)", ENCRYPT, sec, NULL, 0,
2660 speed_template_32_64);
2661 test_acipher_speed("xts(serpent)", DECRYPT, sec, NULL, 0,
2662 speed_template_32_64);
2663 break;
2664
2665 case 504:
2666 test_acipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
2667 speed_template_16_24_32);
2668 test_acipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
2669 speed_template_16_24_32);
2670 test_acipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
2671 speed_template_16_24_32);
2672 test_acipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
2673 speed_template_16_24_32);
2674 test_acipher_speed("ctr(twofish)", ENCRYPT, sec, NULL, 0,
2675 speed_template_16_24_32);
2676 test_acipher_speed("ctr(twofish)", DECRYPT, sec, NULL, 0,
2677 speed_template_16_24_32);
2678 test_acipher_speed("lrw(twofish)", ENCRYPT, sec, NULL, 0,
2679 speed_template_32_40_48);
2680 test_acipher_speed("lrw(twofish)", DECRYPT, sec, NULL, 0,
2681 speed_template_32_40_48);
2682 test_acipher_speed("xts(twofish)", ENCRYPT, sec, NULL, 0,
2683 speed_template_32_48_64);
2684 test_acipher_speed("xts(twofish)", DECRYPT, sec, NULL, 0,
2685 speed_template_32_48_64);
2686 break;
2687
2688 case 505:
2689 test_acipher_speed("ecb(arc4)", ENCRYPT, sec, NULL, 0,
2690 speed_template_8);
2691 break;
2692
2693 case 506:
2694 test_acipher_speed("ecb(cast5)", ENCRYPT, sec, NULL, 0,
2695 speed_template_8_16);
2696 test_acipher_speed("ecb(cast5)", DECRYPT, sec, NULL, 0,
2697 speed_template_8_16);
2698 test_acipher_speed("cbc(cast5)", ENCRYPT, sec, NULL, 0,
2699 speed_template_8_16);
2700 test_acipher_speed("cbc(cast5)", DECRYPT, sec, NULL, 0,
2701 speed_template_8_16);
2702 test_acipher_speed("ctr(cast5)", ENCRYPT, sec, NULL, 0,
2703 speed_template_8_16);
2704 test_acipher_speed("ctr(cast5)", DECRYPT, sec, NULL, 0,
2705 speed_template_8_16);
2706 break;
2707
2708 case 507:
2709 test_acipher_speed("ecb(cast6)", ENCRYPT, sec, NULL, 0,
2710 speed_template_16_32);
2711 test_acipher_speed("ecb(cast6)", DECRYPT, sec, NULL, 0,
2712 speed_template_16_32);
2713 test_acipher_speed("cbc(cast6)", ENCRYPT, sec, NULL, 0,
2714 speed_template_16_32);
2715 test_acipher_speed("cbc(cast6)", DECRYPT, sec, NULL, 0,
2716 speed_template_16_32);
2717 test_acipher_speed("ctr(cast6)", ENCRYPT, sec, NULL, 0,
2718 speed_template_16_32);
2719 test_acipher_speed("ctr(cast6)", DECRYPT, sec, NULL, 0,
2720 speed_template_16_32);
2721 test_acipher_speed("lrw(cast6)", ENCRYPT, sec, NULL, 0,
2722 speed_template_32_48);
2723 test_acipher_speed("lrw(cast6)", DECRYPT, sec, NULL, 0,
2724 speed_template_32_48);
2725 test_acipher_speed("xts(cast6)", ENCRYPT, sec, NULL, 0,
2726 speed_template_32_64);
2727 test_acipher_speed("xts(cast6)", DECRYPT, sec, NULL, 0,
2728 speed_template_32_64);
2729 break;
2730
2731 case 508:
2732 test_acipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
2733 speed_template_16_32);
2734 test_acipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
2735 speed_template_16_32);
2736 test_acipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
2737 speed_template_16_32);
2738 test_acipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
2739 speed_template_16_32);
2740 test_acipher_speed("ctr(camellia)", ENCRYPT, sec, NULL, 0,
2741 speed_template_16_32);
2742 test_acipher_speed("ctr(camellia)", DECRYPT, sec, NULL, 0,
2743 speed_template_16_32);
2744 test_acipher_speed("lrw(camellia)", ENCRYPT, sec, NULL, 0,
2745 speed_template_32_48);
2746 test_acipher_speed("lrw(camellia)", DECRYPT, sec, NULL, 0,
2747 speed_template_32_48);
2748 test_acipher_speed("xts(camellia)", ENCRYPT, sec, NULL, 0,
2749 speed_template_32_64);
2750 test_acipher_speed("xts(camellia)", DECRYPT, sec, NULL, 0,
2751 speed_template_32_64);
2752 break;
2753
2754 case 509:
2755 test_acipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
2756 speed_template_8_32);
2757 test_acipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
2758 speed_template_8_32);
2759 test_acipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
2760 speed_template_8_32);
2761 test_acipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
2762 speed_template_8_32);
2763 test_acipher_speed("ctr(blowfish)", ENCRYPT, sec, NULL, 0,
2764 speed_template_8_32);
2765 test_acipher_speed("ctr(blowfish)", DECRYPT, sec, NULL, 0,
2766 speed_template_8_32);
2767 break;
2768
2769 case 600:
2770 test_mb_skcipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
2771 speed_template_16_24_32, num_mb);
2772 test_mb_skcipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
2773 speed_template_16_24_32, num_mb);
2774 test_mb_skcipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
2775 speed_template_16_24_32, num_mb);
2776 test_mb_skcipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
2777 speed_template_16_24_32, num_mb);
2778 test_mb_skcipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
2779 speed_template_32_40_48, num_mb);
2780 test_mb_skcipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
2781 speed_template_32_40_48, num_mb);
2782 test_mb_skcipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
2783 speed_template_32_64, num_mb);
2784 test_mb_skcipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
2785 speed_template_32_64, num_mb);
2786 test_mb_skcipher_speed("cts(cbc(aes))", ENCRYPT, sec, NULL, 0,
2787 speed_template_16_24_32, num_mb);
2788 test_mb_skcipher_speed("cts(cbc(aes))", DECRYPT, sec, NULL, 0,
2789 speed_template_16_24_32, num_mb);
2790 test_mb_skcipher_speed("ctr(aes)", ENCRYPT, sec, NULL, 0,
2791 speed_template_16_24_32, num_mb);
2792 test_mb_skcipher_speed("ctr(aes)", DECRYPT, sec, NULL, 0,
2793 speed_template_16_24_32, num_mb);
2794 test_mb_skcipher_speed("cfb(aes)", ENCRYPT, sec, NULL, 0,
2795 speed_template_16_24_32, num_mb);
2796 test_mb_skcipher_speed("cfb(aes)", DECRYPT, sec, NULL, 0,
2797 speed_template_16_24_32, num_mb);
2798 test_mb_skcipher_speed("ofb(aes)", ENCRYPT, sec, NULL, 0,
2799 speed_template_16_24_32, num_mb);
2800 test_mb_skcipher_speed("ofb(aes)", DECRYPT, sec, NULL, 0,
2801 speed_template_16_24_32, num_mb);
2802 test_mb_skcipher_speed("rfc3686(ctr(aes))", ENCRYPT, sec, NULL,
2803 0, speed_template_20_28_36, num_mb);
2804 test_mb_skcipher_speed("rfc3686(ctr(aes))", DECRYPT, sec, NULL,
2805 0, speed_template_20_28_36, num_mb);
2806 break;
2807
2808 case 601:
2809 test_mb_skcipher_speed("ecb(des3_ede)", ENCRYPT, sec,
2810 des3_speed_template, DES3_SPEED_VECTORS,
2811 speed_template_24, num_mb);
2812 test_mb_skcipher_speed("ecb(des3_ede)", DECRYPT, sec,
2813 des3_speed_template, DES3_SPEED_VECTORS,
2814 speed_template_24, num_mb);
2815 test_mb_skcipher_speed("cbc(des3_ede)", ENCRYPT, sec,
2816 des3_speed_template, DES3_SPEED_VECTORS,
2817 speed_template_24, num_mb);
2818 test_mb_skcipher_speed("cbc(des3_ede)", DECRYPT, sec,
2819 des3_speed_template, DES3_SPEED_VECTORS,
2820 speed_template_24, num_mb);
2821 test_mb_skcipher_speed("cfb(des3_ede)", ENCRYPT, sec,
2822 des3_speed_template, DES3_SPEED_VECTORS,
2823 speed_template_24, num_mb);
2824 test_mb_skcipher_speed("cfb(des3_ede)", DECRYPT, sec,
2825 des3_speed_template, DES3_SPEED_VECTORS,
2826 speed_template_24, num_mb);
2827 test_mb_skcipher_speed("ofb(des3_ede)", ENCRYPT, sec,
2828 des3_speed_template, DES3_SPEED_VECTORS,
2829 speed_template_24, num_mb);
2830 test_mb_skcipher_speed("ofb(des3_ede)", DECRYPT, sec,
2831 des3_speed_template, DES3_SPEED_VECTORS,
2832 speed_template_24, num_mb);
2833 break;
2834
2835 case 602:
2836 test_mb_skcipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
2837 speed_template_8, num_mb);
2838 test_mb_skcipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
2839 speed_template_8, num_mb);
2840 test_mb_skcipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
2841 speed_template_8, num_mb);
2842 test_mb_skcipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
2843 speed_template_8, num_mb);
2844 test_mb_skcipher_speed("cfb(des)", ENCRYPT, sec, NULL, 0,
2845 speed_template_8, num_mb);
2846 test_mb_skcipher_speed("cfb(des)", DECRYPT, sec, NULL, 0,
2847 speed_template_8, num_mb);
2848 test_mb_skcipher_speed("ofb(des)", ENCRYPT, sec, NULL, 0,
2849 speed_template_8, num_mb);
2850 test_mb_skcipher_speed("ofb(des)", DECRYPT, sec, NULL, 0,
2851 speed_template_8, num_mb);
2852 break;
2853
2854 case 603:
2855 test_mb_skcipher_speed("ecb(serpent)", ENCRYPT, sec, NULL, 0,
2856 speed_template_16_32, num_mb);
2857 test_mb_skcipher_speed("ecb(serpent)", DECRYPT, sec, NULL, 0,
2858 speed_template_16_32, num_mb);
2859 test_mb_skcipher_speed("cbc(serpent)", ENCRYPT, sec, NULL, 0,
2860 speed_template_16_32, num_mb);
2861 test_mb_skcipher_speed("cbc(serpent)", DECRYPT, sec, NULL, 0,
2862 speed_template_16_32, num_mb);
2863 test_mb_skcipher_speed("ctr(serpent)", ENCRYPT, sec, NULL, 0,
2864 speed_template_16_32, num_mb);
2865 test_mb_skcipher_speed("ctr(serpent)", DECRYPT, sec, NULL, 0,
2866 speed_template_16_32, num_mb);
2867 test_mb_skcipher_speed("lrw(serpent)", ENCRYPT, sec, NULL, 0,
2868 speed_template_32_48, num_mb);
2869 test_mb_skcipher_speed("lrw(serpent)", DECRYPT, sec, NULL, 0,
2870 speed_template_32_48, num_mb);
2871 test_mb_skcipher_speed("xts(serpent)", ENCRYPT, sec, NULL, 0,
2872 speed_template_32_64, num_mb);
2873 test_mb_skcipher_speed("xts(serpent)", DECRYPT, sec, NULL, 0,
2874 speed_template_32_64, num_mb);
2875 break;
2876
2877 case 604:
2878 test_mb_skcipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
2879 speed_template_16_24_32, num_mb);
2880 test_mb_skcipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
2881 speed_template_16_24_32, num_mb);
2882 test_mb_skcipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
2883 speed_template_16_24_32, num_mb);
2884 test_mb_skcipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
2885 speed_template_16_24_32, num_mb);
2886 test_mb_skcipher_speed("ctr(twofish)", ENCRYPT, sec, NULL, 0,
2887 speed_template_16_24_32, num_mb);
2888 test_mb_skcipher_speed("ctr(twofish)", DECRYPT, sec, NULL, 0,
2889 speed_template_16_24_32, num_mb);
2890 test_mb_skcipher_speed("lrw(twofish)", ENCRYPT, sec, NULL, 0,
2891 speed_template_32_40_48, num_mb);
2892 test_mb_skcipher_speed("lrw(twofish)", DECRYPT, sec, NULL, 0,
2893 speed_template_32_40_48, num_mb);
2894 test_mb_skcipher_speed("xts(twofish)", ENCRYPT, sec, NULL, 0,
2895 speed_template_32_48_64, num_mb);
2896 test_mb_skcipher_speed("xts(twofish)", DECRYPT, sec, NULL, 0,
2897 speed_template_32_48_64, num_mb);
2898 break;
2899
2900 case 605:
2901 test_mb_skcipher_speed("ecb(arc4)", ENCRYPT, sec, NULL, 0,
2902 speed_template_8, num_mb);
2903 break;
2904
2905 case 606:
2906 test_mb_skcipher_speed("ecb(cast5)", ENCRYPT, sec, NULL, 0,
2907 speed_template_8_16, num_mb);
2908 test_mb_skcipher_speed("ecb(cast5)", DECRYPT, sec, NULL, 0,
2909 speed_template_8_16, num_mb);
2910 test_mb_skcipher_speed("cbc(cast5)", ENCRYPT, sec, NULL, 0,
2911 speed_template_8_16, num_mb);
2912 test_mb_skcipher_speed("cbc(cast5)", DECRYPT, sec, NULL, 0,
2913 speed_template_8_16, num_mb);
2914 test_mb_skcipher_speed("ctr(cast5)", ENCRYPT, sec, NULL, 0,
2915 speed_template_8_16, num_mb);
2916 test_mb_skcipher_speed("ctr(cast5)", DECRYPT, sec, NULL, 0,
2917 speed_template_8_16, num_mb);
2918 break;
2919
2920 case 607:
2921 test_mb_skcipher_speed("ecb(cast6)", ENCRYPT, sec, NULL, 0,
2922 speed_template_16_32, num_mb);
2923 test_mb_skcipher_speed("ecb(cast6)", DECRYPT, sec, NULL, 0,
2924 speed_template_16_32, num_mb);
2925 test_mb_skcipher_speed("cbc(cast6)", ENCRYPT, sec, NULL, 0,
2926 speed_template_16_32, num_mb);
2927 test_mb_skcipher_speed("cbc(cast6)", DECRYPT, sec, NULL, 0,
2928 speed_template_16_32, num_mb);
2929 test_mb_skcipher_speed("ctr(cast6)", ENCRYPT, sec, NULL, 0,
2930 speed_template_16_32, num_mb);
2931 test_mb_skcipher_speed("ctr(cast6)", DECRYPT, sec, NULL, 0,
2932 speed_template_16_32, num_mb);
2933 test_mb_skcipher_speed("lrw(cast6)", ENCRYPT, sec, NULL, 0,
2934 speed_template_32_48, num_mb);
2935 test_mb_skcipher_speed("lrw(cast6)", DECRYPT, sec, NULL, 0,
2936 speed_template_32_48, num_mb);
2937 test_mb_skcipher_speed("xts(cast6)", ENCRYPT, sec, NULL, 0,
2938 speed_template_32_64, num_mb);
2939 test_mb_skcipher_speed("xts(cast6)", DECRYPT, sec, NULL, 0,
2940 speed_template_32_64, num_mb);
2941 break;
2942
2943 case 608:
2944 test_mb_skcipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
2945 speed_template_16_32, num_mb);
2946 test_mb_skcipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
2947 speed_template_16_32, num_mb);
2948 test_mb_skcipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
2949 speed_template_16_32, num_mb);
2950 test_mb_skcipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
2951 speed_template_16_32, num_mb);
2952 test_mb_skcipher_speed("ctr(camellia)", ENCRYPT, sec, NULL, 0,
2953 speed_template_16_32, num_mb);
2954 test_mb_skcipher_speed("ctr(camellia)", DECRYPT, sec, NULL, 0,
2955 speed_template_16_32, num_mb);
2956 test_mb_skcipher_speed("lrw(camellia)", ENCRYPT, sec, NULL, 0,
2957 speed_template_32_48, num_mb);
2958 test_mb_skcipher_speed("lrw(camellia)", DECRYPT, sec, NULL, 0,
2959 speed_template_32_48, num_mb);
2960 test_mb_skcipher_speed("xts(camellia)", ENCRYPT, sec, NULL, 0,
2961 speed_template_32_64, num_mb);
2962 test_mb_skcipher_speed("xts(camellia)", DECRYPT, sec, NULL, 0,
2963 speed_template_32_64, num_mb);
2964 break;
2965
2966 case 609:
2967 test_mb_skcipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
2968 speed_template_8_32, num_mb);
2969 test_mb_skcipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
2970 speed_template_8_32, num_mb);
2971 test_mb_skcipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
2972 speed_template_8_32, num_mb);
2973 test_mb_skcipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
2974 speed_template_8_32, num_mb);
2975 test_mb_skcipher_speed("ctr(blowfish)", ENCRYPT, sec, NULL, 0,
2976 speed_template_8_32, num_mb);
2977 test_mb_skcipher_speed("ctr(blowfish)", DECRYPT, sec, NULL, 0,
2978 speed_template_8_32, num_mb);
2979 break;
2980
2981 case 1000:
2982 test_available();
2983 break;
2984 }
2985
2986 return ret;
2987}
2988
2989static int __init tcrypt_mod_init(void)
2990{
2991 int err = -ENOMEM;
2992 int i;
2993
2994 for (i = 0; i < TVMEMSIZE; i++) {
2995 tvmem[i] = (void *)__get_free_page(GFP_KERNEL);
2996 if (!tvmem[i])
2997 goto err_free_tv;
2998 }
2999
3000 err = do_test(alg, type, mask, mode, num_mb);
3001
3002 if (err) {
3003 printk(KERN_ERR "tcrypt: one or more tests failed!\n");
3004 goto err_free_tv;
3005 } else {
3006 pr_debug("all tests passed\n");
3007 }
3008
3009 /* We intentionaly return -EAGAIN to prevent keeping the module,
3010 * unless we're running in fips mode. It does all its work from
3011 * init() and doesn't offer any runtime functionality, but in
3012 * the fips case, checking for a successful load is helpful.
3013 * => we don't need it in the memory, do we?
3014 * -- mludvig
3015 */
3016 if (!fips_enabled)
3017 err = -EAGAIN;
3018
3019err_free_tv:
3020 for (i = 0; i < TVMEMSIZE && tvmem[i]; i++)
3021 free_page((unsigned long)tvmem[i]);
3022
3023 return err;
3024}
3025
3026/*
3027 * If an init function is provided, an exit function must also be provided
3028 * to allow module unload.
3029 */
3030static void __exit tcrypt_mod_fini(void) { }
3031
3032late_initcall(tcrypt_mod_init);
3033module_exit(tcrypt_mod_fini);
3034
3035module_param(alg, charp, 0);
3036module_param(type, uint, 0);
3037module_param(mask, uint, 0);
3038module_param(mode, int, 0);
3039module_param(sec, uint, 0);
3040MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
3041 "(defaults to zero which uses CPU cycles instead)");
3042module_param(num_mb, uint, 0000);
3043MODULE_PARM_DESC(num_mb, "Number of concurrent requests to be used in mb speed tests (defaults to 8)");
3044module_param(klen, uint, 0);
3045MODULE_PARM_DESC(klen, "Key length (defaults to 0)");
3046
3047MODULE_LICENSE("GPL");
3048MODULE_DESCRIPTION("Quick & dirty crypto testing module");
3049MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");