• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
59 static unsigned int sec;
60 
61 static char *alg = NULL;
62 static u32 type;
63 static u32 mask;
64 static int mode;
65 static u32 num_mb = 8;
66 static unsigned int klen;
67 static char *tvmem[TVMEMSIZE];
68 
69 static 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 
80 static const int block_sizes[] = { 16, 64, 128, 256, 1024, 1420, 4096, 0 };
81 static const int aead_sizes[] = { 16, 64, 256, 512, 1024, 1420, 4096, 8192, 0 };
82 
83 #define XBUFSIZE 8
84 #define MAX_IVLEN 32
85 
testmgr_alloc_buf(char * buf[XBUFSIZE])86 static 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 
98 err_free_buf:
99 	while (i-- > 0)
100 		free_page((unsigned long)buf[i]);
101 
102 	return -ENOMEM;
103 }
104 
testmgr_free_buf(char * buf[XBUFSIZE])105 static 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 
sg_init_aead(struct scatterlist * sg,char * xbuf[XBUFSIZE],unsigned int buflen,const void * assoc,unsigned int aad_size)113 static 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 
do_one_aead_op(struct aead_request * req,int ret)140 static 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 
147 struct 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 
do_mult_aead_op(struct test_mb_aead_data * data,int enc,u32 num_mb,int * rc)157 static 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 
test_mb_aead_jiffies(struct test_mb_aead_data * data,int enc,int blen,int secs,u32 num_mb)183 static 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 
205 out:
206 	kfree(rc);
207 	return ret;
208 }
209 
test_mb_aead_cycles(struct test_mb_aead_data * data,int enc,int blen,u32 num_mb)210 static 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 
246 out:
247 	kfree(rc);
248 	return ret;
249 }
250 
test_mb_aead_speed(const char * algo,int enc,int secs,struct aead_speed_template * template,unsigned int tcount,u8 authsize,unsigned int aad_size,u8 * keysize,u32 num_mb)251 static 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 
441 out:
442 	for (i = 0; i < num_mb; ++i)
443 		aead_request_free(data[i].req);
444 out_free_xoutbuf:
445 	for (i = 0; i < num_mb; ++i)
446 		testmgr_free_buf(data[i].xoutbuf);
447 out_free_axbuf:
448 	for (i = 0; i < num_mb; ++i)
449 		testmgr_free_buf(data[i].axbuf);
450 out_free_xbuf:
451 	for (i = 0; i < num_mb; ++i)
452 		testmgr_free_buf(data[i].xbuf);
453 out_free_tfm:
454 	crypto_free_aead(tfm);
455 out_free_data:
456 	kfree(data);
457 out_free_iv:
458 	kfree(iv);
459 }
460 
test_aead_jiffies(struct aead_request * req,int enc,int blen,int secs)461 static 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 
test_aead_cycles(struct aead_request * req,int enc,int blen)484 static 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 
518 out:
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 
test_aead_speed(const char * algo,int enc,unsigned int secs,struct aead_speed_template * template,unsigned int tcount,u8 authsize,unsigned int aad_size,u8 * keysize)526 static 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 
693 out:
694 	aead_request_free(req);
695 out_noreq:
696 	crypto_free_aead(tfm);
697 out_notfm:
698 	kfree(sg);
699 out_nosg:
700 	testmgr_free_buf(xoutbuf);
701 out_nooutbuf:
702 	testmgr_free_buf(axbuf);
703 out_noaxbuf:
704 	testmgr_free_buf(xbuf);
705 out_noxbuf:
706 	kfree(iv);
707 }
708 
test_hash_sg_init(struct scatterlist * sg)709 static 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 
do_one_ahash_op(struct ahash_request * req,int ret)720 static 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 
727 struct 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 
do_mult_ahash_op(struct test_mb_ahash_data * data,u32 num_mb,int * rc)735 static 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 
test_mb_ahash_jiffies(struct test_mb_ahash_data * data,int blen,int secs,u32 num_mb)757 static 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 
779 out:
780 	kfree(rc);
781 	return ret;
782 }
783 
test_mb_ahash_cycles(struct test_mb_ahash_data * data,int blen,u32 num_mb)784 static 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 
820 out:
821 	kfree(rc);
822 	return ret;
823 }
824 
test_mb_ahash_speed(const char * algo,unsigned int secs,struct hash_speed * speed,u32 num_mb)825 static 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 
908 out:
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 
917 free_data:
918 	kfree(data);
919 }
920 
test_ahash_jiffies_digest(struct ahash_request * req,int blen,char * out,int secs)921 static 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 
test_ahash_jiffies(struct ahash_request * req,int blen,int plen,char * out,int secs)941 static 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 
test_ahash_cycles_digest(struct ahash_request * req,int blen,char * out)973 static 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 
1001 out:
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 
test_ahash_cycles(struct ahash_request * req,int blen,int plen,char * out)1011 static 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 
1058 out:
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 
test_ahash_speed_common(const char * algo,unsigned int secs,struct hash_speed * speed,unsigned mask)1068 static 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 
1142 out_nomem:
1143 	ahash_request_free(req);
1144 
1145 out:
1146 	crypto_free_ahash(tfm);
1147 }
1148 
test_ahash_speed(const char * algo,unsigned int secs,struct hash_speed * speed)1149 static 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 
test_hash_speed(const char * algo,unsigned int secs,struct hash_speed * speed)1155 static 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 
1161 struct 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 
do_mult_acipher_op(struct test_mb_skcipher_data * data,int enc,u32 num_mb,int * rc)1168 static 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 
test_mb_acipher_jiffies(struct test_mb_skcipher_data * data,int enc,int blen,int secs,u32 num_mb)1194 static 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 
1216 out:
1217 	kfree(rc);
1218 	return ret;
1219 }
1220 
test_mb_acipher_cycles(struct test_mb_skcipher_data * data,int enc,int blen,u32 num_mb)1221 static 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 
1257 out:
1258 	kfree(rc);
1259 	return ret;
1260 }
1261 
test_mb_skcipher_speed(const char * algo,int enc,int secs,struct cipher_speed_template * template,unsigned int tcount,u8 * keysize,u32 num_mb)1262 static 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 	for (i = 0; i < num_mb; ++i) {
1299 		data[i].req = skcipher_request_alloc(tfm, GFP_KERNEL);
1300 		if (!data[i].req) {
1301 			pr_err("alg: skcipher: Failed to allocate request for %s\n",
1302 			       algo);
1303 			while (i--)
1304 				skcipher_request_free(data[i].req);
1305 			goto out_free_xbuf;
1306 		}
1307 	}
1308 
1309 	for (i = 0; i < num_mb; ++i) {
1310 		skcipher_request_set_callback(data[i].req,
1311 					      CRYPTO_TFM_REQ_MAY_BACKLOG,
1312 					      crypto_req_done, &data[i].wait);
1313 		crypto_init_wait(&data[i].wait);
1314 	}
1315 
1316 	pr_info("\ntesting speed of multibuffer %s (%s) %s\n", algo,
1317 		get_driver_name(crypto_skcipher, tfm), e);
1318 
1319 	i = 0;
1320 	do {
1321 		b_size = block_sizes;
1322 		do {
1323 			u32 bs = round_up(*b_size, crypto_skcipher_blocksize(tfm));
1324 
1325 			if (bs > XBUFSIZE * PAGE_SIZE) {
1326 				pr_err("template (%u) too big for buffer (%lu)\n",
1327 				       bs, XBUFSIZE * PAGE_SIZE);
1328 				goto out;
1329 			}
1330 
1331 			pr_info("test %u (%d bit key, %d byte blocks): ", i,
1332 				*keysize * 8, bs);
1333 
1334 			/* Set up tfm global state, i.e. the key */
1335 
1336 			memset(tvmem[0], 0xff, PAGE_SIZE);
1337 			key = tvmem[0];
1338 			for (j = 0; j < tcount; j++) {
1339 				if (template[j].klen == *keysize) {
1340 					key = template[j].key;
1341 					break;
1342 				}
1343 			}
1344 
1345 			crypto_skcipher_clear_flags(tfm, ~0);
1346 
1347 			ret = crypto_skcipher_setkey(tfm, key, *keysize);
1348 			if (ret) {
1349 				pr_err("setkey() failed flags=%x\n",
1350 				       crypto_skcipher_get_flags(tfm));
1351 				goto out;
1352 			}
1353 
1354 			iv_len = crypto_skcipher_ivsize(tfm);
1355 			if (iv_len)
1356 				memset(&iv, 0xff, iv_len);
1357 
1358 			/* Now setup per request stuff, i.e. buffers */
1359 
1360 			for (j = 0; j < num_mb; ++j) {
1361 				struct test_mb_skcipher_data *cur = &data[j];
1362 				unsigned int k = bs;
1363 				unsigned int pages = DIV_ROUND_UP(k, PAGE_SIZE);
1364 				unsigned int p = 0;
1365 
1366 				sg_init_table(cur->sg, pages);
1367 
1368 				while (k > PAGE_SIZE) {
1369 					sg_set_buf(cur->sg + p, cur->xbuf[p],
1370 						   PAGE_SIZE);
1371 					memset(cur->xbuf[p], 0xff, PAGE_SIZE);
1372 					p++;
1373 					k -= PAGE_SIZE;
1374 				}
1375 
1376 				sg_set_buf(cur->sg + p, cur->xbuf[p], k);
1377 				memset(cur->xbuf[p], 0xff, k);
1378 
1379 				skcipher_request_set_crypt(cur->req, cur->sg,
1380 							   cur->sg, bs, iv);
1381 			}
1382 
1383 			if (secs) {
1384 				ret = test_mb_acipher_jiffies(data, enc,
1385 							      bs, secs,
1386 							      num_mb);
1387 				cond_resched();
1388 			} else {
1389 				ret = test_mb_acipher_cycles(data, enc,
1390 							     bs, num_mb);
1391 			}
1392 
1393 			if (ret) {
1394 				pr_err("%s() failed flags=%x\n", e,
1395 				       crypto_skcipher_get_flags(tfm));
1396 				break;
1397 			}
1398 			b_size++;
1399 			i++;
1400 		} while (*b_size);
1401 		keysize++;
1402 	} while (*keysize);
1403 
1404 out:
1405 	for (i = 0; i < num_mb; ++i)
1406 		skcipher_request_free(data[i].req);
1407 out_free_xbuf:
1408 	for (i = 0; i < num_mb; ++i)
1409 		testmgr_free_buf(data[i].xbuf);
1410 out_free_tfm:
1411 	crypto_free_skcipher(tfm);
1412 out_free_data:
1413 	kfree(data);
1414 }
1415 
do_one_acipher_op(struct skcipher_request * req,int ret)1416 static inline int do_one_acipher_op(struct skcipher_request *req, int ret)
1417 {
1418 	struct crypto_wait *wait = req->base.data;
1419 
1420 	return crypto_wait_req(ret, wait);
1421 }
1422 
test_acipher_jiffies(struct skcipher_request * req,int enc,int blen,int secs)1423 static int test_acipher_jiffies(struct skcipher_request *req, int enc,
1424 				int blen, int secs)
1425 {
1426 	unsigned long start, end;
1427 	int bcount;
1428 	int ret;
1429 
1430 	for (start = jiffies, end = start + secs * HZ, bcount = 0;
1431 	     time_before(jiffies, end); bcount++) {
1432 		if (enc)
1433 			ret = do_one_acipher_op(req,
1434 						crypto_skcipher_encrypt(req));
1435 		else
1436 			ret = do_one_acipher_op(req,
1437 						crypto_skcipher_decrypt(req));
1438 
1439 		if (ret)
1440 			return ret;
1441 	}
1442 
1443 	pr_cont("%d operations in %d seconds (%llu bytes)\n",
1444 		bcount, secs, (u64)bcount * blen);
1445 	return 0;
1446 }
1447 
test_acipher_cycles(struct skcipher_request * req,int enc,int blen)1448 static int test_acipher_cycles(struct skcipher_request *req, int enc,
1449 			       int blen)
1450 {
1451 	unsigned long cycles = 0;
1452 	int ret = 0;
1453 	int i;
1454 
1455 	/* Warm-up run. */
1456 	for (i = 0; i < 4; i++) {
1457 		if (enc)
1458 			ret = do_one_acipher_op(req,
1459 						crypto_skcipher_encrypt(req));
1460 		else
1461 			ret = do_one_acipher_op(req,
1462 						crypto_skcipher_decrypt(req));
1463 
1464 		if (ret)
1465 			goto out;
1466 	}
1467 
1468 	/* The real thing. */
1469 	for (i = 0; i < 8; i++) {
1470 		cycles_t start, end;
1471 
1472 		start = get_cycles();
1473 		if (enc)
1474 			ret = do_one_acipher_op(req,
1475 						crypto_skcipher_encrypt(req));
1476 		else
1477 			ret = do_one_acipher_op(req,
1478 						crypto_skcipher_decrypt(req));
1479 		end = get_cycles();
1480 
1481 		if (ret)
1482 			goto out;
1483 
1484 		cycles += end - start;
1485 	}
1486 
1487 out:
1488 	if (ret == 0)
1489 		pr_cont("1 operation in %lu cycles (%d bytes)\n",
1490 			(cycles + 4) / 8, blen);
1491 
1492 	return ret;
1493 }
1494 
test_skcipher_speed(const char * algo,int enc,unsigned int secs,struct cipher_speed_template * template,unsigned int tcount,u8 * keysize,bool async)1495 static void test_skcipher_speed(const char *algo, int enc, unsigned int secs,
1496 				struct cipher_speed_template *template,
1497 				unsigned int tcount, u8 *keysize, bool async)
1498 {
1499 	unsigned int ret, i, j, k, iv_len;
1500 	struct crypto_wait wait;
1501 	const char *key;
1502 	char iv[128];
1503 	struct skcipher_request *req;
1504 	struct crypto_skcipher *tfm;
1505 	const int *b_size;
1506 	const char *e;
1507 
1508 	if (enc == ENCRYPT)
1509 		e = "encryption";
1510 	else
1511 		e = "decryption";
1512 
1513 	crypto_init_wait(&wait);
1514 
1515 	tfm = crypto_alloc_skcipher(algo, 0, async ? 0 : CRYPTO_ALG_ASYNC);
1516 
1517 	if (IS_ERR(tfm)) {
1518 		pr_err("failed to load transform for %s: %ld\n", algo,
1519 		       PTR_ERR(tfm));
1520 		return;
1521 	}
1522 
1523 	pr_info("\ntesting speed of %s %s (%s) %s\n", async ? "async" : "sync",
1524 		algo, get_driver_name(crypto_skcipher, tfm), e);
1525 
1526 	req = skcipher_request_alloc(tfm, GFP_KERNEL);
1527 	if (!req) {
1528 		pr_err("tcrypt: skcipher: Failed to allocate request for %s\n",
1529 		       algo);
1530 		goto out;
1531 	}
1532 
1533 	skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1534 				      crypto_req_done, &wait);
1535 
1536 	i = 0;
1537 	do {
1538 		b_size = block_sizes;
1539 
1540 		do {
1541 			u32 bs = round_up(*b_size, crypto_skcipher_blocksize(tfm));
1542 			struct scatterlist sg[TVMEMSIZE];
1543 
1544 			if ((*keysize + bs) > TVMEMSIZE * PAGE_SIZE) {
1545 				pr_err("template (%u) too big for "
1546 				       "tvmem (%lu)\n", *keysize + bs,
1547 				       TVMEMSIZE * PAGE_SIZE);
1548 				goto out_free_req;
1549 			}
1550 
1551 			pr_info("test %u (%d bit key, %d byte blocks): ", i,
1552 				*keysize * 8, bs);
1553 
1554 			memset(tvmem[0], 0xff, PAGE_SIZE);
1555 
1556 			/* set key, plain text and IV */
1557 			key = tvmem[0];
1558 			for (j = 0; j < tcount; j++) {
1559 				if (template[j].klen == *keysize) {
1560 					key = template[j].key;
1561 					break;
1562 				}
1563 			}
1564 
1565 			crypto_skcipher_clear_flags(tfm, ~0);
1566 
1567 			ret = crypto_skcipher_setkey(tfm, key, *keysize);
1568 			if (ret) {
1569 				pr_err("setkey() failed flags=%x\n",
1570 					crypto_skcipher_get_flags(tfm));
1571 				goto out_free_req;
1572 			}
1573 
1574 			k = *keysize + bs;
1575 			sg_init_table(sg, DIV_ROUND_UP(k, PAGE_SIZE));
1576 
1577 			if (k > PAGE_SIZE) {
1578 				sg_set_buf(sg, tvmem[0] + *keysize,
1579 				   PAGE_SIZE - *keysize);
1580 				k -= PAGE_SIZE;
1581 				j = 1;
1582 				while (k > PAGE_SIZE) {
1583 					sg_set_buf(sg + j, tvmem[j], PAGE_SIZE);
1584 					memset(tvmem[j], 0xff, PAGE_SIZE);
1585 					j++;
1586 					k -= PAGE_SIZE;
1587 				}
1588 				sg_set_buf(sg + j, tvmem[j], k);
1589 				memset(tvmem[j], 0xff, k);
1590 			} else {
1591 				sg_set_buf(sg, tvmem[0] + *keysize, bs);
1592 			}
1593 
1594 			iv_len = crypto_skcipher_ivsize(tfm);
1595 			if (iv_len)
1596 				memset(&iv, 0xff, iv_len);
1597 
1598 			skcipher_request_set_crypt(req, sg, sg, bs, iv);
1599 
1600 			if (secs) {
1601 				ret = test_acipher_jiffies(req, enc,
1602 							   bs, secs);
1603 				cond_resched();
1604 			} else {
1605 				ret = test_acipher_cycles(req, enc,
1606 							  bs);
1607 			}
1608 
1609 			if (ret) {
1610 				pr_err("%s() failed flags=%x\n", e,
1611 				       crypto_skcipher_get_flags(tfm));
1612 				break;
1613 			}
1614 			b_size++;
1615 			i++;
1616 		} while (*b_size);
1617 		keysize++;
1618 	} while (*keysize);
1619 
1620 out_free_req:
1621 	skcipher_request_free(req);
1622 out:
1623 	crypto_free_skcipher(tfm);
1624 }
1625 
test_acipher_speed(const char * algo,int enc,unsigned int secs,struct cipher_speed_template * template,unsigned int tcount,u8 * keysize)1626 static void test_acipher_speed(const char *algo, int enc, unsigned int secs,
1627 			       struct cipher_speed_template *template,
1628 			       unsigned int tcount, u8 *keysize)
1629 {
1630 	return test_skcipher_speed(algo, enc, secs, template, tcount, keysize,
1631 				   true);
1632 }
1633 
test_cipher_speed(const char * algo,int enc,unsigned int secs,struct cipher_speed_template * template,unsigned int tcount,u8 * keysize)1634 static void test_cipher_speed(const char *algo, int enc, unsigned int secs,
1635 			      struct cipher_speed_template *template,
1636 			      unsigned int tcount, u8 *keysize)
1637 {
1638 	return test_skcipher_speed(algo, enc, secs, template, tcount, keysize,
1639 				   false);
1640 }
1641 
test_available(void)1642 static void test_available(void)
1643 {
1644 	const char **name = check;
1645 
1646 	while (*name) {
1647 		printk("alg %s ", *name);
1648 		printk(crypto_has_alg(*name, 0, 0) ?
1649 		       "found\n" : "not found\n");
1650 		name++;
1651 	}
1652 }
1653 
tcrypt_test(const char * alg)1654 static inline int tcrypt_test(const char *alg)
1655 {
1656 	int ret;
1657 
1658 	pr_debug("testing %s\n", alg);
1659 
1660 	ret = alg_test(alg, alg, 0, 0);
1661 	/* non-fips algs return -EINVAL in fips mode */
1662 	if (fips_enabled && ret == -EINVAL)
1663 		ret = 0;
1664 	return ret;
1665 }
1666 
do_test(const char * alg,u32 type,u32 mask,int m,u32 num_mb)1667 static int do_test(const char *alg, u32 type, u32 mask, int m, u32 num_mb)
1668 {
1669 	int i;
1670 	int ret = 0;
1671 
1672 	switch (m) {
1673 	case 0:
1674 		if (alg) {
1675 			if (!crypto_has_alg(alg, type,
1676 					    mask ?: CRYPTO_ALG_TYPE_MASK))
1677 				ret = -ENOENT;
1678 			break;
1679 		}
1680 
1681 		for (i = 1; i < 200; i++)
1682 			ret += do_test(NULL, 0, 0, i, num_mb);
1683 		break;
1684 
1685 	case 1:
1686 		ret += tcrypt_test("md5");
1687 		break;
1688 
1689 	case 2:
1690 		ret += tcrypt_test("sha1");
1691 		break;
1692 
1693 	case 3:
1694 		ret += tcrypt_test("ecb(des)");
1695 		ret += tcrypt_test("cbc(des)");
1696 		ret += tcrypt_test("ctr(des)");
1697 		break;
1698 
1699 	case 4:
1700 		ret += tcrypt_test("ecb(des3_ede)");
1701 		ret += tcrypt_test("cbc(des3_ede)");
1702 		ret += tcrypt_test("ctr(des3_ede)");
1703 		break;
1704 
1705 	case 5:
1706 		ret += tcrypt_test("md4");
1707 		break;
1708 
1709 	case 6:
1710 		ret += tcrypt_test("sha256");
1711 		break;
1712 
1713 	case 7:
1714 		ret += tcrypt_test("ecb(blowfish)");
1715 		ret += tcrypt_test("cbc(blowfish)");
1716 		ret += tcrypt_test("ctr(blowfish)");
1717 		break;
1718 
1719 	case 8:
1720 		ret += tcrypt_test("ecb(twofish)");
1721 		ret += tcrypt_test("cbc(twofish)");
1722 		ret += tcrypt_test("ctr(twofish)");
1723 		ret += tcrypt_test("lrw(twofish)");
1724 		ret += tcrypt_test("xts(twofish)");
1725 		break;
1726 
1727 	case 9:
1728 		ret += tcrypt_test("ecb(serpent)");
1729 		ret += tcrypt_test("cbc(serpent)");
1730 		ret += tcrypt_test("ctr(serpent)");
1731 		ret += tcrypt_test("lrw(serpent)");
1732 		ret += tcrypt_test("xts(serpent)");
1733 		break;
1734 
1735 	case 10:
1736 		ret += tcrypt_test("ecb(aes)");
1737 		ret += tcrypt_test("cbc(aes)");
1738 		ret += tcrypt_test("lrw(aes)");
1739 		ret += tcrypt_test("xts(aes)");
1740 		ret += tcrypt_test("ctr(aes)");
1741 		ret += tcrypt_test("rfc3686(ctr(aes))");
1742 		ret += tcrypt_test("ofb(aes)");
1743 		ret += tcrypt_test("cfb(aes)");
1744 		ret += tcrypt_test("xctr(aes)");
1745 		break;
1746 
1747 	case 11:
1748 		ret += tcrypt_test("sha384");
1749 		break;
1750 
1751 	case 12:
1752 		ret += tcrypt_test("sha512");
1753 		break;
1754 
1755 	case 13:
1756 		ret += tcrypt_test("deflate");
1757 		break;
1758 
1759 	case 14:
1760 		ret += tcrypt_test("ecb(cast5)");
1761 		ret += tcrypt_test("cbc(cast5)");
1762 		ret += tcrypt_test("ctr(cast5)");
1763 		break;
1764 
1765 	case 15:
1766 		ret += tcrypt_test("ecb(cast6)");
1767 		ret += tcrypt_test("cbc(cast6)");
1768 		ret += tcrypt_test("ctr(cast6)");
1769 		ret += tcrypt_test("lrw(cast6)");
1770 		ret += tcrypt_test("xts(cast6)");
1771 		break;
1772 
1773 	case 16:
1774 		ret += tcrypt_test("ecb(arc4)");
1775 		break;
1776 
1777 	case 17:
1778 		ret += tcrypt_test("michael_mic");
1779 		break;
1780 
1781 	case 18:
1782 		ret += tcrypt_test("crc32c");
1783 		break;
1784 
1785 	case 19:
1786 		ret += tcrypt_test("ecb(tea)");
1787 		break;
1788 
1789 	case 20:
1790 		ret += tcrypt_test("ecb(xtea)");
1791 		break;
1792 
1793 	case 21:
1794 		ret += tcrypt_test("ecb(khazad)");
1795 		break;
1796 
1797 	case 22:
1798 		ret += tcrypt_test("wp512");
1799 		break;
1800 
1801 	case 23:
1802 		ret += tcrypt_test("wp384");
1803 		break;
1804 
1805 	case 24:
1806 		ret += tcrypt_test("wp256");
1807 		break;
1808 
1809 	case 26:
1810 		ret += tcrypt_test("ecb(anubis)");
1811 		ret += tcrypt_test("cbc(anubis)");
1812 		break;
1813 
1814 	case 30:
1815 		ret += tcrypt_test("ecb(xeta)");
1816 		break;
1817 
1818 	case 31:
1819 		ret += tcrypt_test("pcbc(fcrypt)");
1820 		break;
1821 
1822 	case 32:
1823 		ret += tcrypt_test("ecb(camellia)");
1824 		ret += tcrypt_test("cbc(camellia)");
1825 		ret += tcrypt_test("ctr(camellia)");
1826 		ret += tcrypt_test("lrw(camellia)");
1827 		ret += tcrypt_test("xts(camellia)");
1828 		break;
1829 
1830 	case 33:
1831 		ret += tcrypt_test("sha224");
1832 		break;
1833 
1834 	case 35:
1835 		ret += tcrypt_test("gcm(aes)");
1836 		break;
1837 
1838 	case 36:
1839 		ret += tcrypt_test("lzo");
1840 		break;
1841 
1842 	case 37:
1843 		ret += tcrypt_test("ccm(aes)");
1844 		break;
1845 
1846 	case 38:
1847 		ret += tcrypt_test("cts(cbc(aes))");
1848 		break;
1849 
1850         case 39:
1851 		ret += tcrypt_test("xxhash64");
1852 		break;
1853 
1854         case 40:
1855 		ret += tcrypt_test("rmd160");
1856 		break;
1857 
1858 	case 42:
1859 		ret += tcrypt_test("blake2b-512");
1860 		break;
1861 
1862 	case 43:
1863 		ret += tcrypt_test("ecb(seed)");
1864 		break;
1865 
1866 	case 45:
1867 		ret += tcrypt_test("rfc4309(ccm(aes))");
1868 		break;
1869 
1870 	case 46:
1871 		ret += tcrypt_test("ghash");
1872 		break;
1873 
1874 	case 47:
1875 		ret += tcrypt_test("crct10dif");
1876 		break;
1877 
1878 	case 48:
1879 		ret += tcrypt_test("sha3-224");
1880 		break;
1881 
1882 	case 49:
1883 		ret += tcrypt_test("sha3-256");
1884 		break;
1885 
1886 	case 50:
1887 		ret += tcrypt_test("sha3-384");
1888 		break;
1889 
1890 	case 51:
1891 		ret += tcrypt_test("sha3-512");
1892 		break;
1893 
1894 	case 52:
1895 		ret += tcrypt_test("sm3");
1896 		break;
1897 
1898 	case 53:
1899 		ret += tcrypt_test("streebog256");
1900 		break;
1901 
1902 	case 54:
1903 		ret += tcrypt_test("streebog512");
1904 		break;
1905 
1906 	case 55:
1907 		ret += tcrypt_test("gcm(sm4)");
1908 		break;
1909 
1910 	case 56:
1911 		ret += tcrypt_test("ccm(sm4)");
1912 		break;
1913 
1914 	case 57:
1915 		ret += tcrypt_test("polyval");
1916 		break;
1917 
1918 	case 100:
1919 		ret += tcrypt_test("hmac(md5)");
1920 		break;
1921 
1922 	case 101:
1923 		ret += tcrypt_test("hmac(sha1)");
1924 		break;
1925 
1926 	case 102:
1927 		ret += tcrypt_test("hmac(sha256)");
1928 		break;
1929 
1930 	case 103:
1931 		ret += tcrypt_test("hmac(sha384)");
1932 		break;
1933 
1934 	case 104:
1935 		ret += tcrypt_test("hmac(sha512)");
1936 		break;
1937 
1938 	case 105:
1939 		ret += tcrypt_test("hmac(sha224)");
1940 		break;
1941 
1942 	case 106:
1943 		ret += tcrypt_test("xcbc(aes)");
1944 		break;
1945 
1946 	case 108:
1947 		ret += tcrypt_test("hmac(rmd160)");
1948 		break;
1949 
1950 	case 109:
1951 		ret += tcrypt_test("vmac64(aes)");
1952 		break;
1953 
1954 	case 111:
1955 		ret += tcrypt_test("hmac(sha3-224)");
1956 		break;
1957 
1958 	case 112:
1959 		ret += tcrypt_test("hmac(sha3-256)");
1960 		break;
1961 
1962 	case 113:
1963 		ret += tcrypt_test("hmac(sha3-384)");
1964 		break;
1965 
1966 	case 114:
1967 		ret += tcrypt_test("hmac(sha3-512)");
1968 		break;
1969 
1970 	case 115:
1971 		ret += tcrypt_test("hmac(streebog256)");
1972 		break;
1973 
1974 	case 116:
1975 		ret += tcrypt_test("hmac(streebog512)");
1976 		break;
1977 
1978 	case 150:
1979 		ret += tcrypt_test("ansi_cprng");
1980 		break;
1981 
1982 	case 151:
1983 		ret += tcrypt_test("rfc4106(gcm(aes))");
1984 		break;
1985 
1986 	case 152:
1987 		ret += tcrypt_test("rfc4543(gcm(aes))");
1988 		break;
1989 
1990 	case 153:
1991 		ret += tcrypt_test("cmac(aes)");
1992 		break;
1993 
1994 	case 154:
1995 		ret += tcrypt_test("cmac(des3_ede)");
1996 		break;
1997 
1998 	case 155:
1999 		ret += tcrypt_test("authenc(hmac(sha1),cbc(aes))");
2000 		break;
2001 
2002 	case 156:
2003 		ret += tcrypt_test("authenc(hmac(md5),ecb(cipher_null))");
2004 		break;
2005 
2006 	case 157:
2007 		ret += tcrypt_test("authenc(hmac(sha1),ecb(cipher_null))");
2008 		break;
2009 
2010 	case 158:
2011 		ret += tcrypt_test("cbcmac(sm4)");
2012 		break;
2013 
2014 	case 159:
2015 		ret += tcrypt_test("cmac(sm4)");
2016 		break;
2017 
2018 	case 181:
2019 		ret += tcrypt_test("authenc(hmac(sha1),cbc(des))");
2020 		break;
2021 	case 182:
2022 		ret += tcrypt_test("authenc(hmac(sha1),cbc(des3_ede))");
2023 		break;
2024 	case 183:
2025 		ret += tcrypt_test("authenc(hmac(sha224),cbc(des))");
2026 		break;
2027 	case 184:
2028 		ret += tcrypt_test("authenc(hmac(sha224),cbc(des3_ede))");
2029 		break;
2030 	case 185:
2031 		ret += tcrypt_test("authenc(hmac(sha256),cbc(des))");
2032 		break;
2033 	case 186:
2034 		ret += tcrypt_test("authenc(hmac(sha256),cbc(des3_ede))");
2035 		break;
2036 	case 187:
2037 		ret += tcrypt_test("authenc(hmac(sha384),cbc(des))");
2038 		break;
2039 	case 188:
2040 		ret += tcrypt_test("authenc(hmac(sha384),cbc(des3_ede))");
2041 		break;
2042 	case 189:
2043 		ret += tcrypt_test("authenc(hmac(sha512),cbc(des))");
2044 		break;
2045 	case 190:
2046 		ret += tcrypt_test("authenc(hmac(sha512),cbc(des3_ede))");
2047 		break;
2048 	case 191:
2049 		ret += tcrypt_test("ecb(sm4)");
2050 		ret += tcrypt_test("cbc(sm4)");
2051 		ret += tcrypt_test("cfb(sm4)");
2052 		ret += tcrypt_test("ctr(sm4)");
2053 		break;
2054 	case 200:
2055 		test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
2056 				speed_template_16_24_32);
2057 		test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
2058 				speed_template_16_24_32);
2059 		test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
2060 				speed_template_16_24_32);
2061 		test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
2062 				speed_template_16_24_32);
2063 		test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
2064 				speed_template_32_40_48);
2065 		test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
2066 				speed_template_32_40_48);
2067 		test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
2068 				speed_template_32_64);
2069 		test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
2070 				speed_template_32_64);
2071 		test_cipher_speed("cts(cbc(aes))", ENCRYPT, sec, NULL, 0,
2072 				speed_template_16_24_32);
2073 		test_cipher_speed("cts(cbc(aes))", DECRYPT, sec, NULL, 0,
2074 				speed_template_16_24_32);
2075 		test_cipher_speed("ctr(aes)", ENCRYPT, sec, NULL, 0,
2076 				speed_template_16_24_32);
2077 		test_cipher_speed("ctr(aes)", DECRYPT, sec, NULL, 0,
2078 				speed_template_16_24_32);
2079 		test_cipher_speed("cfb(aes)", ENCRYPT, sec, NULL, 0,
2080 				speed_template_16_24_32);
2081 		test_cipher_speed("cfb(aes)", DECRYPT, sec, NULL, 0,
2082 				speed_template_16_24_32);
2083 		break;
2084 
2085 	case 201:
2086 		test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
2087 				des3_speed_template, DES3_SPEED_VECTORS,
2088 				speed_template_24);
2089 		test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
2090 				des3_speed_template, DES3_SPEED_VECTORS,
2091 				speed_template_24);
2092 		test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
2093 				des3_speed_template, DES3_SPEED_VECTORS,
2094 				speed_template_24);
2095 		test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
2096 				des3_speed_template, DES3_SPEED_VECTORS,
2097 				speed_template_24);
2098 		test_cipher_speed("ctr(des3_ede)", ENCRYPT, sec,
2099 				des3_speed_template, DES3_SPEED_VECTORS,
2100 				speed_template_24);
2101 		test_cipher_speed("ctr(des3_ede)", DECRYPT, sec,
2102 				des3_speed_template, DES3_SPEED_VECTORS,
2103 				speed_template_24);
2104 		break;
2105 
2106 	case 202:
2107 		test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
2108 				speed_template_16_24_32);
2109 		test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
2110 				speed_template_16_24_32);
2111 		test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
2112 				speed_template_16_24_32);
2113 		test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
2114 				speed_template_16_24_32);
2115 		test_cipher_speed("ctr(twofish)", ENCRYPT, sec, NULL, 0,
2116 				speed_template_16_24_32);
2117 		test_cipher_speed("ctr(twofish)", DECRYPT, sec, NULL, 0,
2118 				speed_template_16_24_32);
2119 		test_cipher_speed("lrw(twofish)", ENCRYPT, sec, NULL, 0,
2120 				speed_template_32_40_48);
2121 		test_cipher_speed("lrw(twofish)", DECRYPT, sec, NULL, 0,
2122 				speed_template_32_40_48);
2123 		test_cipher_speed("xts(twofish)", ENCRYPT, sec, NULL, 0,
2124 				speed_template_32_48_64);
2125 		test_cipher_speed("xts(twofish)", DECRYPT, sec, NULL, 0,
2126 				speed_template_32_48_64);
2127 		break;
2128 
2129 	case 203:
2130 		test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
2131 				  speed_template_8_32);
2132 		test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
2133 				  speed_template_8_32);
2134 		test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
2135 				  speed_template_8_32);
2136 		test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
2137 				  speed_template_8_32);
2138 		test_cipher_speed("ctr(blowfish)", ENCRYPT, sec, NULL, 0,
2139 				  speed_template_8_32);
2140 		test_cipher_speed("ctr(blowfish)", DECRYPT, sec, NULL, 0,
2141 				  speed_template_8_32);
2142 		break;
2143 
2144 	case 204:
2145 		test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
2146 				  speed_template_8);
2147 		test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
2148 				  speed_template_8);
2149 		test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
2150 				  speed_template_8);
2151 		test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
2152 				  speed_template_8);
2153 		break;
2154 
2155 	case 205:
2156 		test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
2157 				speed_template_16_24_32);
2158 		test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
2159 				speed_template_16_24_32);
2160 		test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
2161 				speed_template_16_24_32);
2162 		test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
2163 				speed_template_16_24_32);
2164 		test_cipher_speed("ctr(camellia)", ENCRYPT, sec, NULL, 0,
2165 				speed_template_16_24_32);
2166 		test_cipher_speed("ctr(camellia)", DECRYPT, sec, NULL, 0,
2167 				speed_template_16_24_32);
2168 		test_cipher_speed("lrw(camellia)", ENCRYPT, sec, NULL, 0,
2169 				speed_template_32_40_48);
2170 		test_cipher_speed("lrw(camellia)", DECRYPT, sec, NULL, 0,
2171 				speed_template_32_40_48);
2172 		test_cipher_speed("xts(camellia)", ENCRYPT, sec, NULL, 0,
2173 				speed_template_32_48_64);
2174 		test_cipher_speed("xts(camellia)", DECRYPT, sec, NULL, 0,
2175 				speed_template_32_48_64);
2176 		break;
2177 
2178 	case 207:
2179 		test_cipher_speed("ecb(serpent)", ENCRYPT, sec, NULL, 0,
2180 				  speed_template_16_32);
2181 		test_cipher_speed("ecb(serpent)", DECRYPT, sec, NULL, 0,
2182 				  speed_template_16_32);
2183 		test_cipher_speed("cbc(serpent)", ENCRYPT, sec, NULL, 0,
2184 				  speed_template_16_32);
2185 		test_cipher_speed("cbc(serpent)", DECRYPT, sec, NULL, 0,
2186 				  speed_template_16_32);
2187 		test_cipher_speed("ctr(serpent)", ENCRYPT, sec, NULL, 0,
2188 				  speed_template_16_32);
2189 		test_cipher_speed("ctr(serpent)", DECRYPT, sec, NULL, 0,
2190 				  speed_template_16_32);
2191 		test_cipher_speed("lrw(serpent)", ENCRYPT, sec, NULL, 0,
2192 				  speed_template_32_48);
2193 		test_cipher_speed("lrw(serpent)", DECRYPT, sec, NULL, 0,
2194 				  speed_template_32_48);
2195 		test_cipher_speed("xts(serpent)", ENCRYPT, sec, NULL, 0,
2196 				  speed_template_32_64);
2197 		test_cipher_speed("xts(serpent)", DECRYPT, sec, NULL, 0,
2198 				  speed_template_32_64);
2199 		break;
2200 
2201 	case 208:
2202 		test_cipher_speed("ecb(arc4)", ENCRYPT, sec, NULL, 0,
2203 				  speed_template_8);
2204 		break;
2205 
2206 	case 209:
2207 		test_cipher_speed("ecb(cast5)", ENCRYPT, sec, NULL, 0,
2208 				  speed_template_8_16);
2209 		test_cipher_speed("ecb(cast5)", DECRYPT, sec, NULL, 0,
2210 				  speed_template_8_16);
2211 		test_cipher_speed("cbc(cast5)", ENCRYPT, sec, NULL, 0,
2212 				  speed_template_8_16);
2213 		test_cipher_speed("cbc(cast5)", DECRYPT, sec, NULL, 0,
2214 				  speed_template_8_16);
2215 		test_cipher_speed("ctr(cast5)", ENCRYPT, sec, NULL, 0,
2216 				  speed_template_8_16);
2217 		test_cipher_speed("ctr(cast5)", DECRYPT, sec, NULL, 0,
2218 				  speed_template_8_16);
2219 		break;
2220 
2221 	case 210:
2222 		test_cipher_speed("ecb(cast6)", ENCRYPT, sec, NULL, 0,
2223 				  speed_template_16_32);
2224 		test_cipher_speed("ecb(cast6)", DECRYPT, sec, NULL, 0,
2225 				  speed_template_16_32);
2226 		test_cipher_speed("cbc(cast6)", ENCRYPT, sec, NULL, 0,
2227 				  speed_template_16_32);
2228 		test_cipher_speed("cbc(cast6)", DECRYPT, sec, NULL, 0,
2229 				  speed_template_16_32);
2230 		test_cipher_speed("ctr(cast6)", ENCRYPT, sec, NULL, 0,
2231 				  speed_template_16_32);
2232 		test_cipher_speed("ctr(cast6)", DECRYPT, sec, NULL, 0,
2233 				  speed_template_16_32);
2234 		test_cipher_speed("lrw(cast6)", ENCRYPT, sec, NULL, 0,
2235 				  speed_template_32_48);
2236 		test_cipher_speed("lrw(cast6)", DECRYPT, sec, NULL, 0,
2237 				  speed_template_32_48);
2238 		test_cipher_speed("xts(cast6)", ENCRYPT, sec, NULL, 0,
2239 				  speed_template_32_64);
2240 		test_cipher_speed("xts(cast6)", DECRYPT, sec, NULL, 0,
2241 				  speed_template_32_64);
2242 		break;
2243 
2244 	case 211:
2245 		test_aead_speed("rfc4106(gcm(aes))", ENCRYPT, sec,
2246 				NULL, 0, 16, 16, aead_speed_template_20);
2247 		test_aead_speed("gcm(aes)", ENCRYPT, sec,
2248 				NULL, 0, 16, 8, speed_template_16_24_32);
2249 		test_aead_speed("rfc4106(gcm(aes))", DECRYPT, sec,
2250 				NULL, 0, 16, 16, aead_speed_template_20);
2251 		test_aead_speed("gcm(aes)", DECRYPT, sec,
2252 				NULL, 0, 16, 8, speed_template_16_24_32);
2253 		break;
2254 
2255 	case 212:
2256 		test_aead_speed("rfc4309(ccm(aes))", ENCRYPT, sec,
2257 				NULL, 0, 16, 16, aead_speed_template_19);
2258 		test_aead_speed("rfc4309(ccm(aes))", DECRYPT, sec,
2259 				NULL, 0, 16, 16, aead_speed_template_19);
2260 		break;
2261 
2262 	case 213:
2263 		test_aead_speed("rfc7539esp(chacha20,poly1305)", ENCRYPT, sec,
2264 				NULL, 0, 16, 8, aead_speed_template_36);
2265 		test_aead_speed("rfc7539esp(chacha20,poly1305)", DECRYPT, sec,
2266 				NULL, 0, 16, 8, aead_speed_template_36);
2267 		break;
2268 
2269 	case 214:
2270 		test_cipher_speed("chacha20", ENCRYPT, sec, NULL, 0,
2271 				  speed_template_32);
2272 		break;
2273 
2274 	case 215:
2275 		test_mb_aead_speed("rfc4106(gcm(aes))", ENCRYPT, sec, NULL,
2276 				   0, 16, 16, aead_speed_template_20, num_mb);
2277 		test_mb_aead_speed("gcm(aes)", ENCRYPT, sec, NULL, 0, 16, 8,
2278 				   speed_template_16_24_32, num_mb);
2279 		test_mb_aead_speed("rfc4106(gcm(aes))", DECRYPT, sec, NULL,
2280 				   0, 16, 16, aead_speed_template_20, num_mb);
2281 		test_mb_aead_speed("gcm(aes)", DECRYPT, sec, NULL, 0, 16, 8,
2282 				   speed_template_16_24_32, num_mb);
2283 		break;
2284 
2285 	case 216:
2286 		test_mb_aead_speed("rfc4309(ccm(aes))", ENCRYPT, sec, NULL, 0,
2287 				   16, 16, aead_speed_template_19, num_mb);
2288 		test_mb_aead_speed("rfc4309(ccm(aes))", DECRYPT, sec, NULL, 0,
2289 				   16, 16, aead_speed_template_19, num_mb);
2290 		break;
2291 
2292 	case 217:
2293 		test_mb_aead_speed("rfc7539esp(chacha20,poly1305)", ENCRYPT,
2294 				   sec, NULL, 0, 16, 8, aead_speed_template_36,
2295 				   num_mb);
2296 		test_mb_aead_speed("rfc7539esp(chacha20,poly1305)", DECRYPT,
2297 				   sec, NULL, 0, 16, 8, aead_speed_template_36,
2298 				   num_mb);
2299 		break;
2300 
2301 	case 218:
2302 		test_cipher_speed("ecb(sm4)", ENCRYPT, sec, NULL, 0,
2303 				speed_template_16);
2304 		test_cipher_speed("ecb(sm4)", DECRYPT, sec, NULL, 0,
2305 				speed_template_16);
2306 		test_cipher_speed("cbc(sm4)", ENCRYPT, sec, NULL, 0,
2307 				speed_template_16);
2308 		test_cipher_speed("cbc(sm4)", DECRYPT, sec, NULL, 0,
2309 				speed_template_16);
2310 		test_cipher_speed("cfb(sm4)", ENCRYPT, sec, NULL, 0,
2311 				speed_template_16);
2312 		test_cipher_speed("cfb(sm4)", DECRYPT, sec, NULL, 0,
2313 				speed_template_16);
2314 		test_cipher_speed("ctr(sm4)", ENCRYPT, sec, NULL, 0,
2315 				speed_template_16);
2316 		test_cipher_speed("ctr(sm4)", DECRYPT, sec, NULL, 0,
2317 				speed_template_16);
2318 		break;
2319 
2320 	case 219:
2321 		test_cipher_speed("adiantum(xchacha12,aes)", ENCRYPT, sec, NULL,
2322 				  0, speed_template_32);
2323 		test_cipher_speed("adiantum(xchacha12,aes)", DECRYPT, sec, NULL,
2324 				  0, speed_template_32);
2325 		test_cipher_speed("adiantum(xchacha20,aes)", ENCRYPT, sec, NULL,
2326 				  0, speed_template_32);
2327 		test_cipher_speed("adiantum(xchacha20,aes)", DECRYPT, sec, NULL,
2328 				  0, speed_template_32);
2329 		break;
2330 
2331 	case 220:
2332 		test_acipher_speed("essiv(cbc(aes),sha256)",
2333 				  ENCRYPT, sec, NULL, 0,
2334 				  speed_template_16_24_32);
2335 		test_acipher_speed("essiv(cbc(aes),sha256)",
2336 				  DECRYPT, sec, NULL, 0,
2337 				  speed_template_16_24_32);
2338 		break;
2339 
2340 	case 221:
2341 		test_aead_speed("aegis128", ENCRYPT, sec,
2342 				NULL, 0, 16, 8, speed_template_16);
2343 		test_aead_speed("aegis128", DECRYPT, sec,
2344 				NULL, 0, 16, 8, speed_template_16);
2345 		break;
2346 
2347 	case 222:
2348 		test_aead_speed("gcm(sm4)", ENCRYPT, sec,
2349 				NULL, 0, 16, 8, speed_template_16);
2350 		test_aead_speed("gcm(sm4)", DECRYPT, sec,
2351 				NULL, 0, 16, 8, speed_template_16);
2352 		break;
2353 
2354 	case 223:
2355 		test_aead_speed("rfc4309(ccm(sm4))", ENCRYPT, sec,
2356 				NULL, 0, 16, 16, aead_speed_template_19);
2357 		test_aead_speed("rfc4309(ccm(sm4))", DECRYPT, sec,
2358 				NULL, 0, 16, 16, aead_speed_template_19);
2359 		break;
2360 
2361 	case 224:
2362 		test_mb_aead_speed("gcm(sm4)", ENCRYPT, sec, NULL, 0, 16, 8,
2363 				   speed_template_16, num_mb);
2364 		test_mb_aead_speed("gcm(sm4)", DECRYPT, sec, NULL, 0, 16, 8,
2365 				   speed_template_16, num_mb);
2366 		break;
2367 
2368 	case 225:
2369 		test_mb_aead_speed("rfc4309(ccm(sm4))", ENCRYPT, sec, NULL, 0,
2370 				   16, 16, aead_speed_template_19, num_mb);
2371 		test_mb_aead_speed("rfc4309(ccm(sm4))", DECRYPT, sec, NULL, 0,
2372 				   16, 16, aead_speed_template_19, num_mb);
2373 		break;
2374 
2375 	case 226:
2376 		test_cipher_speed("hctr2(aes)", ENCRYPT, sec, NULL,
2377 				  0, speed_template_32);
2378 		break;
2379 
2380 	case 300:
2381 		if (alg) {
2382 			test_hash_speed(alg, sec, generic_hash_speed_template);
2383 			break;
2384 		}
2385 		fallthrough;
2386 	case 301:
2387 		test_hash_speed("md4", sec, generic_hash_speed_template);
2388 		if (mode > 300 && mode < 400) break;
2389 		fallthrough;
2390 	case 302:
2391 		test_hash_speed("md5", sec, generic_hash_speed_template);
2392 		if (mode > 300 && mode < 400) break;
2393 		fallthrough;
2394 	case 303:
2395 		test_hash_speed("sha1", sec, generic_hash_speed_template);
2396 		if (mode > 300 && mode < 400) break;
2397 		fallthrough;
2398 	case 304:
2399 		test_hash_speed("sha256", sec, generic_hash_speed_template);
2400 		if (mode > 300 && mode < 400) break;
2401 		fallthrough;
2402 	case 305:
2403 		test_hash_speed("sha384", sec, generic_hash_speed_template);
2404 		if (mode > 300 && mode < 400) break;
2405 		fallthrough;
2406 	case 306:
2407 		test_hash_speed("sha512", sec, generic_hash_speed_template);
2408 		if (mode > 300 && mode < 400) break;
2409 		fallthrough;
2410 	case 307:
2411 		test_hash_speed("wp256", sec, generic_hash_speed_template);
2412 		if (mode > 300 && mode < 400) break;
2413 		fallthrough;
2414 	case 308:
2415 		test_hash_speed("wp384", sec, generic_hash_speed_template);
2416 		if (mode > 300 && mode < 400) break;
2417 		fallthrough;
2418 	case 309:
2419 		test_hash_speed("wp512", sec, generic_hash_speed_template);
2420 		if (mode > 300 && mode < 400) break;
2421 		fallthrough;
2422 	case 313:
2423 		test_hash_speed("sha224", sec, generic_hash_speed_template);
2424 		if (mode > 300 && mode < 400) break;
2425 		fallthrough;
2426 	case 314:
2427 		test_hash_speed("xxhash64", sec, generic_hash_speed_template);
2428 		if (mode > 300 && mode < 400) break;
2429 		fallthrough;
2430 	case 315:
2431 		test_hash_speed("rmd160", sec, generic_hash_speed_template);
2432 		if (mode > 300 && mode < 400) break;
2433 		fallthrough;
2434 	case 317:
2435 		test_hash_speed("blake2b-512", sec, generic_hash_speed_template);
2436 		if (mode > 300 && mode < 400) break;
2437 		fallthrough;
2438 	case 318:
2439 		klen = 16;
2440 		test_hash_speed("ghash", sec, generic_hash_speed_template);
2441 		if (mode > 300 && mode < 400) break;
2442 		fallthrough;
2443 	case 319:
2444 		test_hash_speed("crc32c", sec, generic_hash_speed_template);
2445 		if (mode > 300 && mode < 400) break;
2446 		fallthrough;
2447 	case 320:
2448 		test_hash_speed("crct10dif", sec, generic_hash_speed_template);
2449 		if (mode > 300 && mode < 400) break;
2450 		fallthrough;
2451 	case 321:
2452 		test_hash_speed("poly1305", sec, poly1305_speed_template);
2453 		if (mode > 300 && mode < 400) break;
2454 		fallthrough;
2455 	case 322:
2456 		test_hash_speed("sha3-224", sec, generic_hash_speed_template);
2457 		if (mode > 300 && mode < 400) break;
2458 		fallthrough;
2459 	case 323:
2460 		test_hash_speed("sha3-256", sec, generic_hash_speed_template);
2461 		if (mode > 300 && mode < 400) break;
2462 		fallthrough;
2463 	case 324:
2464 		test_hash_speed("sha3-384", sec, generic_hash_speed_template);
2465 		if (mode > 300 && mode < 400) break;
2466 		fallthrough;
2467 	case 325:
2468 		test_hash_speed("sha3-512", sec, generic_hash_speed_template);
2469 		if (mode > 300 && mode < 400) break;
2470 		fallthrough;
2471 	case 326:
2472 		test_hash_speed("sm3", sec, generic_hash_speed_template);
2473 		if (mode > 300 && mode < 400) break;
2474 		fallthrough;
2475 	case 327:
2476 		test_hash_speed("streebog256", sec,
2477 				generic_hash_speed_template);
2478 		if (mode > 300 && mode < 400) break;
2479 		fallthrough;
2480 	case 328:
2481 		test_hash_speed("streebog512", sec,
2482 				generic_hash_speed_template);
2483 		if (mode > 300 && mode < 400) break;
2484 		fallthrough;
2485 	case 399:
2486 		break;
2487 
2488 	case 400:
2489 		if (alg) {
2490 			test_ahash_speed(alg, sec, generic_hash_speed_template);
2491 			break;
2492 		}
2493 		fallthrough;
2494 	case 401:
2495 		test_ahash_speed("md4", sec, generic_hash_speed_template);
2496 		if (mode > 400 && mode < 500) break;
2497 		fallthrough;
2498 	case 402:
2499 		test_ahash_speed("md5", sec, generic_hash_speed_template);
2500 		if (mode > 400 && mode < 500) break;
2501 		fallthrough;
2502 	case 403:
2503 		test_ahash_speed("sha1", sec, generic_hash_speed_template);
2504 		if (mode > 400 && mode < 500) break;
2505 		fallthrough;
2506 	case 404:
2507 		test_ahash_speed("sha256", sec, generic_hash_speed_template);
2508 		if (mode > 400 && mode < 500) break;
2509 		fallthrough;
2510 	case 405:
2511 		test_ahash_speed("sha384", sec, generic_hash_speed_template);
2512 		if (mode > 400 && mode < 500) break;
2513 		fallthrough;
2514 	case 406:
2515 		test_ahash_speed("sha512", sec, generic_hash_speed_template);
2516 		if (mode > 400 && mode < 500) break;
2517 		fallthrough;
2518 	case 407:
2519 		test_ahash_speed("wp256", sec, generic_hash_speed_template);
2520 		if (mode > 400 && mode < 500) break;
2521 		fallthrough;
2522 	case 408:
2523 		test_ahash_speed("wp384", sec, generic_hash_speed_template);
2524 		if (mode > 400 && mode < 500) break;
2525 		fallthrough;
2526 	case 409:
2527 		test_ahash_speed("wp512", sec, generic_hash_speed_template);
2528 		if (mode > 400 && mode < 500) break;
2529 		fallthrough;
2530 	case 413:
2531 		test_ahash_speed("sha224", sec, generic_hash_speed_template);
2532 		if (mode > 400 && mode < 500) break;
2533 		fallthrough;
2534 	case 414:
2535 		test_ahash_speed("xxhash64", sec, generic_hash_speed_template);
2536 		if (mode > 400 && mode < 500) break;
2537 		fallthrough;
2538 	case 415:
2539 		test_ahash_speed("rmd160", sec, generic_hash_speed_template);
2540 		if (mode > 400 && mode < 500) break;
2541 		fallthrough;
2542 	case 417:
2543 		test_ahash_speed("blake2b-512", sec, generic_hash_speed_template);
2544 		if (mode > 400 && mode < 500) break;
2545 		fallthrough;
2546 	case 418:
2547 		test_ahash_speed("sha3-224", sec, generic_hash_speed_template);
2548 		if (mode > 400 && mode < 500) break;
2549 		fallthrough;
2550 	case 419:
2551 		test_ahash_speed("sha3-256", sec, generic_hash_speed_template);
2552 		if (mode > 400 && mode < 500) break;
2553 		fallthrough;
2554 	case 420:
2555 		test_ahash_speed("sha3-384", sec, generic_hash_speed_template);
2556 		if (mode > 400 && mode < 500) break;
2557 		fallthrough;
2558 	case 421:
2559 		test_ahash_speed("sha3-512", sec, generic_hash_speed_template);
2560 		if (mode > 400 && mode < 500) break;
2561 		fallthrough;
2562 	case 422:
2563 		test_mb_ahash_speed("sha1", sec, generic_hash_speed_template,
2564 				    num_mb);
2565 		if (mode > 400 && mode < 500) break;
2566 		fallthrough;
2567 	case 423:
2568 		test_mb_ahash_speed("sha256", sec, generic_hash_speed_template,
2569 				    num_mb);
2570 		if (mode > 400 && mode < 500) break;
2571 		fallthrough;
2572 	case 424:
2573 		test_mb_ahash_speed("sha512", sec, generic_hash_speed_template,
2574 				    num_mb);
2575 		if (mode > 400 && mode < 500) break;
2576 		fallthrough;
2577 	case 425:
2578 		test_mb_ahash_speed("sm3", sec, generic_hash_speed_template,
2579 				    num_mb);
2580 		if (mode > 400 && mode < 500) break;
2581 		fallthrough;
2582 	case 426:
2583 		test_mb_ahash_speed("streebog256", sec,
2584 				    generic_hash_speed_template, num_mb);
2585 		if (mode > 400 && mode < 500) break;
2586 		fallthrough;
2587 	case 427:
2588 		test_mb_ahash_speed("streebog512", sec,
2589 				    generic_hash_speed_template, num_mb);
2590 		if (mode > 400 && mode < 500) break;
2591 		fallthrough;
2592 	case 499:
2593 		break;
2594 
2595 	case 500:
2596 		test_acipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
2597 				   speed_template_16_24_32);
2598 		test_acipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
2599 				   speed_template_16_24_32);
2600 		test_acipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
2601 				   speed_template_16_24_32);
2602 		test_acipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
2603 				   speed_template_16_24_32);
2604 		test_acipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
2605 				   speed_template_32_40_48);
2606 		test_acipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
2607 				   speed_template_32_40_48);
2608 		test_acipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
2609 				   speed_template_32_64);
2610 		test_acipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
2611 				   speed_template_32_64);
2612 		test_acipher_speed("cts(cbc(aes))", ENCRYPT, sec, NULL, 0,
2613 				   speed_template_16_24_32);
2614 		test_acipher_speed("cts(cbc(aes))", DECRYPT, sec, NULL, 0,
2615 				   speed_template_16_24_32);
2616 		test_acipher_speed("ctr(aes)", ENCRYPT, sec, NULL, 0,
2617 				   speed_template_16_24_32);
2618 		test_acipher_speed("ctr(aes)", DECRYPT, sec, NULL, 0,
2619 				   speed_template_16_24_32);
2620 		test_acipher_speed("cfb(aes)", ENCRYPT, sec, NULL, 0,
2621 				   speed_template_16_24_32);
2622 		test_acipher_speed("cfb(aes)", DECRYPT, sec, NULL, 0,
2623 				   speed_template_16_24_32);
2624 		test_acipher_speed("ofb(aes)", ENCRYPT, sec, NULL, 0,
2625 				   speed_template_16_24_32);
2626 		test_acipher_speed("ofb(aes)", DECRYPT, sec, NULL, 0,
2627 				   speed_template_16_24_32);
2628 		test_acipher_speed("rfc3686(ctr(aes))", ENCRYPT, sec, NULL, 0,
2629 				   speed_template_20_28_36);
2630 		test_acipher_speed("rfc3686(ctr(aes))", DECRYPT, sec, NULL, 0,
2631 				   speed_template_20_28_36);
2632 		break;
2633 
2634 	case 501:
2635 		test_acipher_speed("ecb(des3_ede)", ENCRYPT, sec,
2636 				   des3_speed_template, DES3_SPEED_VECTORS,
2637 				   speed_template_24);
2638 		test_acipher_speed("ecb(des3_ede)", DECRYPT, sec,
2639 				   des3_speed_template, DES3_SPEED_VECTORS,
2640 				   speed_template_24);
2641 		test_acipher_speed("cbc(des3_ede)", ENCRYPT, sec,
2642 				   des3_speed_template, DES3_SPEED_VECTORS,
2643 				   speed_template_24);
2644 		test_acipher_speed("cbc(des3_ede)", DECRYPT, sec,
2645 				   des3_speed_template, DES3_SPEED_VECTORS,
2646 				   speed_template_24);
2647 		test_acipher_speed("cfb(des3_ede)", ENCRYPT, sec,
2648 				   des3_speed_template, DES3_SPEED_VECTORS,
2649 				   speed_template_24);
2650 		test_acipher_speed("cfb(des3_ede)", DECRYPT, sec,
2651 				   des3_speed_template, DES3_SPEED_VECTORS,
2652 				   speed_template_24);
2653 		test_acipher_speed("ofb(des3_ede)", ENCRYPT, sec,
2654 				   des3_speed_template, DES3_SPEED_VECTORS,
2655 				   speed_template_24);
2656 		test_acipher_speed("ofb(des3_ede)", DECRYPT, sec,
2657 				   des3_speed_template, DES3_SPEED_VECTORS,
2658 				   speed_template_24);
2659 		break;
2660 
2661 	case 502:
2662 		test_acipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
2663 				   speed_template_8);
2664 		test_acipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
2665 				   speed_template_8);
2666 		test_acipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
2667 				   speed_template_8);
2668 		test_acipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
2669 				   speed_template_8);
2670 		test_acipher_speed("cfb(des)", ENCRYPT, sec, NULL, 0,
2671 				   speed_template_8);
2672 		test_acipher_speed("cfb(des)", DECRYPT, sec, NULL, 0,
2673 				   speed_template_8);
2674 		test_acipher_speed("ofb(des)", ENCRYPT, sec, NULL, 0,
2675 				   speed_template_8);
2676 		test_acipher_speed("ofb(des)", DECRYPT, sec, NULL, 0,
2677 				   speed_template_8);
2678 		break;
2679 
2680 	case 503:
2681 		test_acipher_speed("ecb(serpent)", ENCRYPT, sec, NULL, 0,
2682 				   speed_template_16_32);
2683 		test_acipher_speed("ecb(serpent)", DECRYPT, sec, NULL, 0,
2684 				   speed_template_16_32);
2685 		test_acipher_speed("cbc(serpent)", ENCRYPT, sec, NULL, 0,
2686 				   speed_template_16_32);
2687 		test_acipher_speed("cbc(serpent)", DECRYPT, sec, NULL, 0,
2688 				   speed_template_16_32);
2689 		test_acipher_speed("ctr(serpent)", ENCRYPT, sec, NULL, 0,
2690 				   speed_template_16_32);
2691 		test_acipher_speed("ctr(serpent)", DECRYPT, sec, NULL, 0,
2692 				   speed_template_16_32);
2693 		test_acipher_speed("lrw(serpent)", ENCRYPT, sec, NULL, 0,
2694 				   speed_template_32_48);
2695 		test_acipher_speed("lrw(serpent)", DECRYPT, sec, NULL, 0,
2696 				   speed_template_32_48);
2697 		test_acipher_speed("xts(serpent)", ENCRYPT, sec, NULL, 0,
2698 				   speed_template_32_64);
2699 		test_acipher_speed("xts(serpent)", DECRYPT, sec, NULL, 0,
2700 				   speed_template_32_64);
2701 		break;
2702 
2703 	case 504:
2704 		test_acipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
2705 				   speed_template_16_24_32);
2706 		test_acipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
2707 				   speed_template_16_24_32);
2708 		test_acipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
2709 				   speed_template_16_24_32);
2710 		test_acipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
2711 				   speed_template_16_24_32);
2712 		test_acipher_speed("ctr(twofish)", ENCRYPT, sec, NULL, 0,
2713 				   speed_template_16_24_32);
2714 		test_acipher_speed("ctr(twofish)", DECRYPT, sec, NULL, 0,
2715 				   speed_template_16_24_32);
2716 		test_acipher_speed("lrw(twofish)", ENCRYPT, sec, NULL, 0,
2717 				   speed_template_32_40_48);
2718 		test_acipher_speed("lrw(twofish)", DECRYPT, sec, NULL, 0,
2719 				   speed_template_32_40_48);
2720 		test_acipher_speed("xts(twofish)", ENCRYPT, sec, NULL, 0,
2721 				   speed_template_32_48_64);
2722 		test_acipher_speed("xts(twofish)", DECRYPT, sec, NULL, 0,
2723 				   speed_template_32_48_64);
2724 		break;
2725 
2726 	case 505:
2727 		test_acipher_speed("ecb(arc4)", ENCRYPT, sec, NULL, 0,
2728 				   speed_template_8);
2729 		break;
2730 
2731 	case 506:
2732 		test_acipher_speed("ecb(cast5)", ENCRYPT, sec, NULL, 0,
2733 				   speed_template_8_16);
2734 		test_acipher_speed("ecb(cast5)", DECRYPT, sec, NULL, 0,
2735 				   speed_template_8_16);
2736 		test_acipher_speed("cbc(cast5)", ENCRYPT, sec, NULL, 0,
2737 				   speed_template_8_16);
2738 		test_acipher_speed("cbc(cast5)", DECRYPT, sec, NULL, 0,
2739 				   speed_template_8_16);
2740 		test_acipher_speed("ctr(cast5)", ENCRYPT, sec, NULL, 0,
2741 				   speed_template_8_16);
2742 		test_acipher_speed("ctr(cast5)", DECRYPT, sec, NULL, 0,
2743 				   speed_template_8_16);
2744 		break;
2745 
2746 	case 507:
2747 		test_acipher_speed("ecb(cast6)", ENCRYPT, sec, NULL, 0,
2748 				   speed_template_16_32);
2749 		test_acipher_speed("ecb(cast6)", DECRYPT, sec, NULL, 0,
2750 				   speed_template_16_32);
2751 		test_acipher_speed("cbc(cast6)", ENCRYPT, sec, NULL, 0,
2752 				   speed_template_16_32);
2753 		test_acipher_speed("cbc(cast6)", DECRYPT, sec, NULL, 0,
2754 				   speed_template_16_32);
2755 		test_acipher_speed("ctr(cast6)", ENCRYPT, sec, NULL, 0,
2756 				   speed_template_16_32);
2757 		test_acipher_speed("ctr(cast6)", DECRYPT, sec, NULL, 0,
2758 				   speed_template_16_32);
2759 		test_acipher_speed("lrw(cast6)", ENCRYPT, sec, NULL, 0,
2760 				   speed_template_32_48);
2761 		test_acipher_speed("lrw(cast6)", DECRYPT, sec, NULL, 0,
2762 				   speed_template_32_48);
2763 		test_acipher_speed("xts(cast6)", ENCRYPT, sec, NULL, 0,
2764 				   speed_template_32_64);
2765 		test_acipher_speed("xts(cast6)", DECRYPT, sec, NULL, 0,
2766 				   speed_template_32_64);
2767 		break;
2768 
2769 	case 508:
2770 		test_acipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
2771 				   speed_template_16_32);
2772 		test_acipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
2773 				   speed_template_16_32);
2774 		test_acipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
2775 				   speed_template_16_32);
2776 		test_acipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
2777 				   speed_template_16_32);
2778 		test_acipher_speed("ctr(camellia)", ENCRYPT, sec, NULL, 0,
2779 				   speed_template_16_32);
2780 		test_acipher_speed("ctr(camellia)", DECRYPT, sec, NULL, 0,
2781 				   speed_template_16_32);
2782 		test_acipher_speed("lrw(camellia)", ENCRYPT, sec, NULL, 0,
2783 				   speed_template_32_48);
2784 		test_acipher_speed("lrw(camellia)", DECRYPT, sec, NULL, 0,
2785 				   speed_template_32_48);
2786 		test_acipher_speed("xts(camellia)", ENCRYPT, sec, NULL, 0,
2787 				   speed_template_32_64);
2788 		test_acipher_speed("xts(camellia)", DECRYPT, sec, NULL, 0,
2789 				   speed_template_32_64);
2790 		break;
2791 
2792 	case 509:
2793 		test_acipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
2794 				   speed_template_8_32);
2795 		test_acipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
2796 				   speed_template_8_32);
2797 		test_acipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
2798 				   speed_template_8_32);
2799 		test_acipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
2800 				   speed_template_8_32);
2801 		test_acipher_speed("ctr(blowfish)", ENCRYPT, sec, NULL, 0,
2802 				   speed_template_8_32);
2803 		test_acipher_speed("ctr(blowfish)", DECRYPT, sec, NULL, 0,
2804 				   speed_template_8_32);
2805 		break;
2806 
2807 	case 518:
2808 		test_acipher_speed("ecb(sm4)", ENCRYPT, sec, NULL, 0,
2809 				speed_template_16);
2810 		test_acipher_speed("ecb(sm4)", DECRYPT, sec, NULL, 0,
2811 				speed_template_16);
2812 		test_acipher_speed("cbc(sm4)", ENCRYPT, sec, NULL, 0,
2813 				speed_template_16);
2814 		test_acipher_speed("cbc(sm4)", DECRYPT, sec, NULL, 0,
2815 				speed_template_16);
2816 		test_acipher_speed("cfb(sm4)", ENCRYPT, sec, NULL, 0,
2817 				speed_template_16);
2818 		test_acipher_speed("cfb(sm4)", DECRYPT, sec, NULL, 0,
2819 				speed_template_16);
2820 		test_acipher_speed("ctr(sm4)", ENCRYPT, sec, NULL, 0,
2821 				speed_template_16);
2822 		test_acipher_speed("ctr(sm4)", DECRYPT, sec, NULL, 0,
2823 				speed_template_16);
2824 		break;
2825 
2826 	case 600:
2827 		test_mb_skcipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
2828 				       speed_template_16_24_32, num_mb);
2829 		test_mb_skcipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
2830 				       speed_template_16_24_32, num_mb);
2831 		test_mb_skcipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
2832 				       speed_template_16_24_32, num_mb);
2833 		test_mb_skcipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
2834 				       speed_template_16_24_32, num_mb);
2835 		test_mb_skcipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
2836 				       speed_template_32_40_48, num_mb);
2837 		test_mb_skcipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
2838 				       speed_template_32_40_48, num_mb);
2839 		test_mb_skcipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
2840 				       speed_template_32_64, num_mb);
2841 		test_mb_skcipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
2842 				       speed_template_32_64, num_mb);
2843 		test_mb_skcipher_speed("cts(cbc(aes))", ENCRYPT, sec, NULL, 0,
2844 				       speed_template_16_24_32, num_mb);
2845 		test_mb_skcipher_speed("cts(cbc(aes))", DECRYPT, sec, NULL, 0,
2846 				       speed_template_16_24_32, num_mb);
2847 		test_mb_skcipher_speed("ctr(aes)", ENCRYPT, sec, NULL, 0,
2848 				       speed_template_16_24_32, num_mb);
2849 		test_mb_skcipher_speed("ctr(aes)", DECRYPT, sec, NULL, 0,
2850 				       speed_template_16_24_32, num_mb);
2851 		test_mb_skcipher_speed("cfb(aes)", ENCRYPT, sec, NULL, 0,
2852 				       speed_template_16_24_32, num_mb);
2853 		test_mb_skcipher_speed("cfb(aes)", DECRYPT, sec, NULL, 0,
2854 				       speed_template_16_24_32, num_mb);
2855 		test_mb_skcipher_speed("ofb(aes)", ENCRYPT, sec, NULL, 0,
2856 				       speed_template_16_24_32, num_mb);
2857 		test_mb_skcipher_speed("ofb(aes)", DECRYPT, sec, NULL, 0,
2858 				       speed_template_16_24_32, num_mb);
2859 		test_mb_skcipher_speed("rfc3686(ctr(aes))", ENCRYPT, sec, NULL,
2860 				       0, speed_template_20_28_36, num_mb);
2861 		test_mb_skcipher_speed("rfc3686(ctr(aes))", DECRYPT, sec, NULL,
2862 				       0, speed_template_20_28_36, num_mb);
2863 		break;
2864 
2865 	case 601:
2866 		test_mb_skcipher_speed("ecb(des3_ede)", ENCRYPT, sec,
2867 				       des3_speed_template, DES3_SPEED_VECTORS,
2868 				       speed_template_24, num_mb);
2869 		test_mb_skcipher_speed("ecb(des3_ede)", DECRYPT, sec,
2870 				       des3_speed_template, DES3_SPEED_VECTORS,
2871 				       speed_template_24, num_mb);
2872 		test_mb_skcipher_speed("cbc(des3_ede)", ENCRYPT, sec,
2873 				       des3_speed_template, DES3_SPEED_VECTORS,
2874 				       speed_template_24, num_mb);
2875 		test_mb_skcipher_speed("cbc(des3_ede)", DECRYPT, sec,
2876 				       des3_speed_template, DES3_SPEED_VECTORS,
2877 				       speed_template_24, num_mb);
2878 		test_mb_skcipher_speed("cfb(des3_ede)", ENCRYPT, sec,
2879 				       des3_speed_template, DES3_SPEED_VECTORS,
2880 				       speed_template_24, num_mb);
2881 		test_mb_skcipher_speed("cfb(des3_ede)", DECRYPT, sec,
2882 				       des3_speed_template, DES3_SPEED_VECTORS,
2883 				       speed_template_24, num_mb);
2884 		test_mb_skcipher_speed("ofb(des3_ede)", ENCRYPT, sec,
2885 				       des3_speed_template, DES3_SPEED_VECTORS,
2886 				       speed_template_24, num_mb);
2887 		test_mb_skcipher_speed("ofb(des3_ede)", DECRYPT, sec,
2888 				       des3_speed_template, DES3_SPEED_VECTORS,
2889 				       speed_template_24, num_mb);
2890 		break;
2891 
2892 	case 602:
2893 		test_mb_skcipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
2894 				       speed_template_8, num_mb);
2895 		test_mb_skcipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
2896 				       speed_template_8, num_mb);
2897 		test_mb_skcipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
2898 				       speed_template_8, num_mb);
2899 		test_mb_skcipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
2900 				       speed_template_8, num_mb);
2901 		test_mb_skcipher_speed("cfb(des)", ENCRYPT, sec, NULL, 0,
2902 				       speed_template_8, num_mb);
2903 		test_mb_skcipher_speed("cfb(des)", DECRYPT, sec, NULL, 0,
2904 				       speed_template_8, num_mb);
2905 		test_mb_skcipher_speed("ofb(des)", ENCRYPT, sec, NULL, 0,
2906 				       speed_template_8, num_mb);
2907 		test_mb_skcipher_speed("ofb(des)", DECRYPT, sec, NULL, 0,
2908 				       speed_template_8, num_mb);
2909 		break;
2910 
2911 	case 603:
2912 		test_mb_skcipher_speed("ecb(serpent)", ENCRYPT, sec, NULL, 0,
2913 				       speed_template_16_32, num_mb);
2914 		test_mb_skcipher_speed("ecb(serpent)", DECRYPT, sec, NULL, 0,
2915 				       speed_template_16_32, num_mb);
2916 		test_mb_skcipher_speed("cbc(serpent)", ENCRYPT, sec, NULL, 0,
2917 				       speed_template_16_32, num_mb);
2918 		test_mb_skcipher_speed("cbc(serpent)", DECRYPT, sec, NULL, 0,
2919 				       speed_template_16_32, num_mb);
2920 		test_mb_skcipher_speed("ctr(serpent)", ENCRYPT, sec, NULL, 0,
2921 				       speed_template_16_32, num_mb);
2922 		test_mb_skcipher_speed("ctr(serpent)", DECRYPT, sec, NULL, 0,
2923 				       speed_template_16_32, num_mb);
2924 		test_mb_skcipher_speed("lrw(serpent)", ENCRYPT, sec, NULL, 0,
2925 				       speed_template_32_48, num_mb);
2926 		test_mb_skcipher_speed("lrw(serpent)", DECRYPT, sec, NULL, 0,
2927 				       speed_template_32_48, num_mb);
2928 		test_mb_skcipher_speed("xts(serpent)", ENCRYPT, sec, NULL, 0,
2929 				       speed_template_32_64, num_mb);
2930 		test_mb_skcipher_speed("xts(serpent)", DECRYPT, sec, NULL, 0,
2931 				       speed_template_32_64, num_mb);
2932 		break;
2933 
2934 	case 604:
2935 		test_mb_skcipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
2936 				       speed_template_16_24_32, num_mb);
2937 		test_mb_skcipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
2938 				       speed_template_16_24_32, num_mb);
2939 		test_mb_skcipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
2940 				       speed_template_16_24_32, num_mb);
2941 		test_mb_skcipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
2942 				       speed_template_16_24_32, num_mb);
2943 		test_mb_skcipher_speed("ctr(twofish)", ENCRYPT, sec, NULL, 0,
2944 				       speed_template_16_24_32, num_mb);
2945 		test_mb_skcipher_speed("ctr(twofish)", DECRYPT, sec, NULL, 0,
2946 				       speed_template_16_24_32, num_mb);
2947 		test_mb_skcipher_speed("lrw(twofish)", ENCRYPT, sec, NULL, 0,
2948 				       speed_template_32_40_48, num_mb);
2949 		test_mb_skcipher_speed("lrw(twofish)", DECRYPT, sec, NULL, 0,
2950 				       speed_template_32_40_48, num_mb);
2951 		test_mb_skcipher_speed("xts(twofish)", ENCRYPT, sec, NULL, 0,
2952 				       speed_template_32_48_64, num_mb);
2953 		test_mb_skcipher_speed("xts(twofish)", DECRYPT, sec, NULL, 0,
2954 				       speed_template_32_48_64, num_mb);
2955 		break;
2956 
2957 	case 605:
2958 		test_mb_skcipher_speed("ecb(arc4)", ENCRYPT, sec, NULL, 0,
2959 				       speed_template_8, num_mb);
2960 		break;
2961 
2962 	case 606:
2963 		test_mb_skcipher_speed("ecb(cast5)", ENCRYPT, sec, NULL, 0,
2964 				       speed_template_8_16, num_mb);
2965 		test_mb_skcipher_speed("ecb(cast5)", DECRYPT, sec, NULL, 0,
2966 				       speed_template_8_16, num_mb);
2967 		test_mb_skcipher_speed("cbc(cast5)", ENCRYPT, sec, NULL, 0,
2968 				       speed_template_8_16, num_mb);
2969 		test_mb_skcipher_speed("cbc(cast5)", DECRYPT, sec, NULL, 0,
2970 				       speed_template_8_16, num_mb);
2971 		test_mb_skcipher_speed("ctr(cast5)", ENCRYPT, sec, NULL, 0,
2972 				       speed_template_8_16, num_mb);
2973 		test_mb_skcipher_speed("ctr(cast5)", DECRYPT, sec, NULL, 0,
2974 				       speed_template_8_16, num_mb);
2975 		break;
2976 
2977 	case 607:
2978 		test_mb_skcipher_speed("ecb(cast6)", ENCRYPT, sec, NULL, 0,
2979 				       speed_template_16_32, num_mb);
2980 		test_mb_skcipher_speed("ecb(cast6)", DECRYPT, sec, NULL, 0,
2981 				       speed_template_16_32, num_mb);
2982 		test_mb_skcipher_speed("cbc(cast6)", ENCRYPT, sec, NULL, 0,
2983 				       speed_template_16_32, num_mb);
2984 		test_mb_skcipher_speed("cbc(cast6)", DECRYPT, sec, NULL, 0,
2985 				       speed_template_16_32, num_mb);
2986 		test_mb_skcipher_speed("ctr(cast6)", ENCRYPT, sec, NULL, 0,
2987 				       speed_template_16_32, num_mb);
2988 		test_mb_skcipher_speed("ctr(cast6)", DECRYPT, sec, NULL, 0,
2989 				       speed_template_16_32, num_mb);
2990 		test_mb_skcipher_speed("lrw(cast6)", ENCRYPT, sec, NULL, 0,
2991 				       speed_template_32_48, num_mb);
2992 		test_mb_skcipher_speed("lrw(cast6)", DECRYPT, sec, NULL, 0,
2993 				       speed_template_32_48, num_mb);
2994 		test_mb_skcipher_speed("xts(cast6)", ENCRYPT, sec, NULL, 0,
2995 				       speed_template_32_64, num_mb);
2996 		test_mb_skcipher_speed("xts(cast6)", DECRYPT, sec, NULL, 0,
2997 				       speed_template_32_64, num_mb);
2998 		break;
2999 
3000 	case 608:
3001 		test_mb_skcipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
3002 				       speed_template_16_32, num_mb);
3003 		test_mb_skcipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
3004 				       speed_template_16_32, num_mb);
3005 		test_mb_skcipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
3006 				       speed_template_16_32, num_mb);
3007 		test_mb_skcipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
3008 				       speed_template_16_32, num_mb);
3009 		test_mb_skcipher_speed("ctr(camellia)", ENCRYPT, sec, NULL, 0,
3010 				       speed_template_16_32, num_mb);
3011 		test_mb_skcipher_speed("ctr(camellia)", DECRYPT, sec, NULL, 0,
3012 				       speed_template_16_32, num_mb);
3013 		test_mb_skcipher_speed("lrw(camellia)", ENCRYPT, sec, NULL, 0,
3014 				       speed_template_32_48, num_mb);
3015 		test_mb_skcipher_speed("lrw(camellia)", DECRYPT, sec, NULL, 0,
3016 				       speed_template_32_48, num_mb);
3017 		test_mb_skcipher_speed("xts(camellia)", ENCRYPT, sec, NULL, 0,
3018 				       speed_template_32_64, num_mb);
3019 		test_mb_skcipher_speed("xts(camellia)", DECRYPT, sec, NULL, 0,
3020 				       speed_template_32_64, num_mb);
3021 		break;
3022 
3023 	case 609:
3024 		test_mb_skcipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
3025 				       speed_template_8_32, num_mb);
3026 		test_mb_skcipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
3027 				       speed_template_8_32, num_mb);
3028 		test_mb_skcipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
3029 				       speed_template_8_32, num_mb);
3030 		test_mb_skcipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
3031 				       speed_template_8_32, num_mb);
3032 		test_mb_skcipher_speed("ctr(blowfish)", ENCRYPT, sec, NULL, 0,
3033 				       speed_template_8_32, num_mb);
3034 		test_mb_skcipher_speed("ctr(blowfish)", DECRYPT, sec, NULL, 0,
3035 				       speed_template_8_32, num_mb);
3036 		break;
3037 
3038 	case 1000:
3039 		test_available();
3040 		break;
3041 	}
3042 
3043 	return ret;
3044 }
3045 
tcrypt_mod_init(void)3046 static int __init tcrypt_mod_init(void)
3047 {
3048 	int err = -ENOMEM;
3049 	int i;
3050 
3051 	for (i = 0; i < TVMEMSIZE; i++) {
3052 		tvmem[i] = (void *)__get_free_page(GFP_KERNEL);
3053 		if (!tvmem[i])
3054 			goto err_free_tv;
3055 	}
3056 
3057 	err = do_test(alg, type, mask, mode, num_mb);
3058 
3059 	if (err) {
3060 		printk(KERN_ERR "tcrypt: one or more tests failed!\n");
3061 		goto err_free_tv;
3062 	} else {
3063 		pr_debug("all tests passed\n");
3064 	}
3065 
3066 	/* We intentionaly return -EAGAIN to prevent keeping the module,
3067 	 * unless we're running in fips mode. It does all its work from
3068 	 * init() and doesn't offer any runtime functionality, but in
3069 	 * the fips case, checking for a successful load is helpful.
3070 	 * => we don't need it in the memory, do we?
3071 	 *                                        -- mludvig
3072 	 */
3073 	if (!fips_enabled)
3074 		err = -EAGAIN;
3075 
3076 err_free_tv:
3077 	for (i = 0; i < TVMEMSIZE && tvmem[i]; i++)
3078 		free_page((unsigned long)tvmem[i]);
3079 
3080 	return err;
3081 }
3082 
3083 /*
3084  * If an init function is provided, an exit function must also be provided
3085  * to allow module unload.
3086  */
tcrypt_mod_fini(void)3087 static void __exit tcrypt_mod_fini(void) { }
3088 
3089 late_initcall(tcrypt_mod_init);
3090 module_exit(tcrypt_mod_fini);
3091 
3092 module_param(alg, charp, 0);
3093 module_param(type, uint, 0);
3094 module_param(mask, uint, 0);
3095 module_param(mode, int, 0);
3096 module_param(sec, uint, 0);
3097 MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
3098 		      "(defaults to zero which uses CPU cycles instead)");
3099 module_param(num_mb, uint, 0000);
3100 MODULE_PARM_DESC(num_mb, "Number of concurrent requests to be used in mb speed tests (defaults to 8)");
3101 module_param(klen, uint, 0);
3102 MODULE_PARM_DESC(klen, "Key length (defaults to 0)");
3103 
3104 MODULE_LICENSE("GPL");
3105 MODULE_DESCRIPTION("Quick & dirty crypto testing module");
3106 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
3107