tcrypt.c 42.8 KB
Newer Older
1
/*
L
Linus Torvalds 已提交
2 3 4 5 6 7 8
 * Quick & dirty crypto testing module.
 *
 * This will only exist until we have a better testing mechanism
 * (e.g. a char device).
 *
 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
 * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
9
 * Copyright (c) 2007 Nokia Siemens Networks
L
Linus Torvalds 已提交
10
 *
11 12 13 14 15 16 17
 * Updated RFC4106 AES-GCM testing.
 *    Authors: Aidan O'Mahony (aidan.o.mahony@intel.com)
 *             Adrian Hoban <adrian.hoban@intel.com>
 *             Gabriele Paoloni <gabriele.paoloni@intel.com>
 *             Tadeusz Struk (tadeusz.struk@intel.com)
 *             Copyright (c) 2010, Intel Corporation.
 *
L
Linus Torvalds 已提交
18 19
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
20
 * Software Foundation; either version 2 of the License, or (at your option)
L
Linus Torvalds 已提交
21 22 23 24
 * any later version.
 *
 */

25
#include <crypto/hash.h>
26
#include <linux/err.h>
L
Linus Torvalds 已提交
27
#include <linux/init.h>
28
#include <linux/gfp.h>
L
Linus Torvalds 已提交
29
#include <linux/module.h>
30
#include <linux/scatterlist.h>
L
Linus Torvalds 已提交
31 32
#include <linux/string.h>
#include <linux/moduleparam.h>
H
Harald Welte 已提交
33
#include <linux/jiffies.h>
34 35
#include <linux/timex.h>
#include <linux/interrupt.h>
L
Linus Torvalds 已提交
36
#include "tcrypt.h"
37
#include "internal.h"
L
Linus Torvalds 已提交
38 39

/*
40
 * Need slab memory for testing (size in number of pages).
L
Linus Torvalds 已提交
41
 */
42
#define TVMEMSIZE	4
L
Linus Torvalds 已提交
43 44

/*
45
* Used by test_cipher_speed()
L
Linus Torvalds 已提交
46 47 48 49
*/
#define ENCRYPT 1
#define DECRYPT 0

H
Harald Welte 已提交
50 51 52
/*
 * Used by test_cipher_speed()
 */
53
static unsigned int sec;
H
Harald Welte 已提交
54

55 56
static char *alg = NULL;
static u32 type;
H
Herbert Xu 已提交
57
static u32 mask;
L
Linus Torvalds 已提交
58
static int mode;
59
static char *tvmem[TVMEMSIZE];
L
Linus Torvalds 已提交
60 61

static char *check[] = {
62 63 64
	"des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256",
	"blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
	"cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
65
	"khazad", "wp512", "wp384", "wp256", "tnepres", "xeta",  "fcrypt",
66
	"camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
67
	"lzo", "cts", "zlib", NULL
L
Linus Torvalds 已提交
68 69
};

70 71
static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc,
			       struct scatterlist *sg, int blen, int sec)
72 73 74 75 76 77 78 79
{
	unsigned long start, end;
	int bcount;
	int ret;

	for (start = jiffies, end = start + sec * HZ, bcount = 0;
	     time_before(jiffies, end); bcount++) {
		if (enc)
80
			ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
81
		else
82
			ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
83 84 85 86 87 88 89 90 91 92

		if (ret)
			return ret;
	}

	printk("%d operations in %d seconds (%ld bytes)\n",
	       bcount, sec, (long)bcount * blen);
	return 0;
}

93 94
static int test_cipher_cycles(struct blkcipher_desc *desc, int enc,
			      struct scatterlist *sg, int blen)
95 96 97 98 99 100 101 102 103 104
{
	unsigned long cycles = 0;
	int ret = 0;
	int i;

	local_irq_disable();

	/* Warm-up run. */
	for (i = 0; i < 4; i++) {
		if (enc)
105
			ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
106
		else
107
			ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
108 109 110 111 112 113 114 115 116 117 118

		if (ret)
			goto out;
	}

	/* The real thing. */
	for (i = 0; i < 8; i++) {
		cycles_t start, end;

		start = get_cycles();
		if (enc)
119
			ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
120
		else
121
			ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
		end = get_cycles();

		if (ret)
			goto out;

		cycles += end - start;
	}

out:
	local_irq_enable();

	if (ret == 0)
		printk("1 operation in %lu cycles (%d bytes)\n",
		       (cycles + 4) / 8, blen);

	return ret;
}

140 141
static u32 block_sizes[] = { 16, 64, 256, 1024, 8192, 0 };

142
static void test_cipher_speed(const char *algo, int enc, unsigned int sec,
143
			      struct cipher_speed_template *template,
144
			      unsigned int tcount, u8 *keysize)
H
Harald Welte 已提交
145
{
146
	unsigned int ret, i, j, iv_len;
147 148
	const char *key;
	char iv[128];
149 150 151
	struct crypto_blkcipher *tfm;
	struct blkcipher_desc desc;
	const char *e;
152
	u32 *b_size;
H
Harald Welte 已提交
153 154 155 156 157 158

	if (enc == ENCRYPT)
	        e = "encryption";
	else
		e = "decryption";

159
	printk("\ntesting speed of %s %s\n", algo, e);
H
Harald Welte 已提交
160

161
	tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);
H
Harald Welte 已提交
162

163 164 165
	if (IS_ERR(tfm)) {
		printk("failed to load transform for %s: %ld\n", algo,
		       PTR_ERR(tfm));
H
Harald Welte 已提交
166 167
		return;
	}
168 169
	desc.tfm = tfm;
	desc.flags = 0;
H
Harald Welte 已提交
170

171 172
	i = 0;
	do {
H
Harald Welte 已提交
173

174 175
		b_size = block_sizes;
		do {
176
			struct scatterlist sg[TVMEMSIZE];
H
Harald Welte 已提交
177

178 179 180 181
			if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
				printk("template (%u) too big for "
				       "tvmem (%lu)\n", *keysize + *b_size,
				       TVMEMSIZE * PAGE_SIZE);
182 183
				goto out;
			}
H
Harald Welte 已提交
184

185 186 187
			printk("test %u (%d bit key, %d byte blocks): ", i,
					*keysize * 8, *b_size);

188
			memset(tvmem[0], 0xff, PAGE_SIZE);
189 190

			/* set key, plain text and IV */
191
			key = tvmem[0];
192 193 194 195 196
			for (j = 0; j < tcount; j++) {
				if (template[j].klen == *keysize) {
					key = template[j].key;
					break;
				}
197
			}
H
Harald Welte 已提交
198

199 200 201 202 203 204
			ret = crypto_blkcipher_setkey(tfm, key, *keysize);
			if (ret) {
				printk("setkey() failed flags=%x\n",
						crypto_blkcipher_get_flags(tfm));
				goto out;
			}
H
Harald Welte 已提交
205

206 207 208 209 210 211 212 213
			sg_init_table(sg, TVMEMSIZE);
			sg_set_buf(sg, tvmem[0] + *keysize,
				   PAGE_SIZE - *keysize);
			for (j = 1; j < TVMEMSIZE; j++) {
				sg_set_buf(sg + j, tvmem[j], PAGE_SIZE);
				memset (tvmem[j], 0xff, PAGE_SIZE);
			}

214 215 216 217 218
			iv_len = crypto_blkcipher_ivsize(tfm);
			if (iv_len) {
				memset(&iv, 0xff, iv_len);
				crypto_blkcipher_set_iv(tfm, iv, iv_len);
			}
H
Harald Welte 已提交
219

220
			if (sec)
221 222
				ret = test_cipher_jiffies(&desc, enc, sg,
							  *b_size, sec);
223
			else
224 225
				ret = test_cipher_cycles(&desc, enc, sg,
							 *b_size);
H
Harald Welte 已提交
226

227 228 229 230 231 232 233 234 235
			if (ret) {
				printk("%s() failed flags=%x\n", e, desc.flags);
				break;
			}
			b_size++;
			i++;
		} while (*b_size);
		keysize++;
	} while (*keysize);
H
Harald Welte 已提交
236 237

out:
238
	crypto_free_blkcipher(tfm);
H
Harald Welte 已提交
239 240
}

241 242
static int test_hash_jiffies_digest(struct hash_desc *desc,
				    struct scatterlist *sg, int blen,
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
				    char *out, int sec)
{
	unsigned long start, end;
	int bcount;
	int ret;

	for (start = jiffies, end = start + sec * HZ, bcount = 0;
	     time_before(jiffies, end); bcount++) {
		ret = crypto_hash_digest(desc, sg, blen, out);
		if (ret)
			return ret;
	}

	printk("%6u opers/sec, %9lu bytes/sec\n",
	       bcount / sec, ((long)bcount * blen) / sec);

	return 0;
}

262 263
static int test_hash_jiffies(struct hash_desc *desc, struct scatterlist *sg,
			     int blen, int plen, char *out, int sec)
264 265 266
{
	unsigned long start, end;
	int bcount, pcount;
267 268 269
	int ret;

	if (plen == blen)
270
		return test_hash_jiffies_digest(desc, sg, blen, out, sec);
271

272 273
	for (start = jiffies, end = start + sec * HZ, bcount = 0;
	     time_before(jiffies, end); bcount++) {
274 275 276
		ret = crypto_hash_init(desc);
		if (ret)
			return ret;
277
		for (pcount = 0; pcount < blen; pcount += plen) {
278 279 280
			ret = crypto_hash_update(desc, sg, plen);
			if (ret)
				return ret;
281 282
		}
		/* we assume there is enough space in 'out' for the result */
283 284 285
		ret = crypto_hash_final(desc, out);
		if (ret)
			return ret;
286 287 288 289 290
	}

	printk("%6u opers/sec, %9lu bytes/sec\n",
	       bcount / sec, ((long)bcount * blen) / sec);

291 292 293
	return 0;
}

294 295
static int test_hash_cycles_digest(struct hash_desc *desc,
				   struct scatterlist *sg, int blen, char *out)
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
{
	unsigned long cycles = 0;
	int i;
	int ret;

	local_irq_disable();

	/* Warm-up run. */
	for (i = 0; i < 4; i++) {
		ret = crypto_hash_digest(desc, sg, blen, out);
		if (ret)
			goto out;
	}

	/* The real thing. */
	for (i = 0; i < 8; i++) {
		cycles_t start, end;

		start = get_cycles();

		ret = crypto_hash_digest(desc, sg, blen, out);
		if (ret)
			goto out;

		end = get_cycles();

		cycles += end - start;
	}

out:
	local_irq_enable();

	if (ret)
		return ret;

	printk("%6lu cycles/operation, %4lu cycles/byte\n",
	       cycles / 8, cycles / (8 * blen));

	return 0;
335 336
}

337 338
static int test_hash_cycles(struct hash_desc *desc, struct scatterlist *sg,
			    int blen, int plen, char *out)
339 340 341
{
	unsigned long cycles = 0;
	int i, pcount;
342 343 344
	int ret;

	if (plen == blen)
345
		return test_hash_cycles_digest(desc, sg, blen, out);
346

347 348 349 350
	local_irq_disable();

	/* Warm-up run. */
	for (i = 0; i < 4; i++) {
351 352 353
		ret = crypto_hash_init(desc);
		if (ret)
			goto out;
354
		for (pcount = 0; pcount < blen; pcount += plen) {
355 356 357
			ret = crypto_hash_update(desc, sg, plen);
			if (ret)
				goto out;
358
		}
359
		ret = crypto_hash_final(desc, out);
360 361
		if (ret)
			goto out;
362 363 364 365 366 367 368 369
	}

	/* The real thing. */
	for (i = 0; i < 8; i++) {
		cycles_t start, end;

		start = get_cycles();

370 371 372
		ret = crypto_hash_init(desc);
		if (ret)
			goto out;
373
		for (pcount = 0; pcount < blen; pcount += plen) {
374 375 376
			ret = crypto_hash_update(desc, sg, plen);
			if (ret)
				goto out;
377
		}
378 379 380
		ret = crypto_hash_final(desc, out);
		if (ret)
			goto out;
381 382 383 384 385 386

		end = get_cycles();

		cycles += end - start;
	}

387
out:
388 389
	local_irq_enable();

390 391 392
	if (ret)
		return ret;

393 394 395
	printk("%6lu cycles/operation, %4lu cycles/byte\n",
	       cycles / 8, cycles / (8 * blen));

396
	return 0;
397 398
}

399 400 401 402 403 404 405 406 407 408 409
static void test_hash_sg_init(struct scatterlist *sg)
{
	int i;

	sg_init_table(sg, TVMEMSIZE);
	for (i = 0; i < TVMEMSIZE; i++) {
		sg_set_buf(sg + i, tvmem[i], PAGE_SIZE);
		memset(tvmem[i], 0xff, PAGE_SIZE);
	}
}

410 411
static void test_hash_speed(const char *algo, unsigned int sec,
			    struct hash_speed *speed)
412
{
413
	struct scatterlist sg[TVMEMSIZE];
414 415
	struct crypto_hash *tfm;
	struct hash_desc desc;
416
	static char output[1024];
417
	int i;
418
	int ret;
419

420
	printk(KERN_INFO "\ntesting speed of %s\n", algo);
421

422
	tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
423

424
	if (IS_ERR(tfm)) {
425
		printk(KERN_ERR "failed to load transform for %s: %ld\n", algo,
426
		       PTR_ERR(tfm));
427 428 429
		return;
	}

430 431 432 433
	desc.tfm = tfm;
	desc.flags = 0;

	if (crypto_hash_digestsize(tfm) > sizeof(output)) {
434
		printk(KERN_ERR "digestsize(%u) > outputbuffer(%zu)\n",
435
		       crypto_hash_digestsize(tfm), sizeof(output));
436 437 438
		goto out;
	}

439
	test_hash_sg_init(sg);
440
	for (i = 0; speed[i].blen != 0; i++) {
441
		if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
442 443
			printk(KERN_ERR
			       "template (%u) too big for tvmem (%lu)\n",
444
			       speed[i].blen, TVMEMSIZE * PAGE_SIZE);
445 446 447
			goto out;
		}

448 449 450
		if (speed[i].klen)
			crypto_hash_setkey(tfm, tvmem[0], speed[i].klen);

451 452
		printk(KERN_INFO "test%3u "
		       "(%5u byte blocks,%5u bytes per update,%4u updates): ",
453 454 455
		       i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);

		if (sec)
456
			ret = test_hash_jiffies(&desc, sg, speed[i].blen,
457
						speed[i].plen, output, sec);
458
		else
459
			ret = test_hash_cycles(&desc, sg, speed[i].blen,
460 461 462
					       speed[i].plen, output);

		if (ret) {
463
			printk(KERN_ERR "hashing failed ret=%d\n", ret);
464 465
			break;
		}
466 467 468
	}

out:
469
	crypto_free_hash(tfm);
470 471
}

472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
struct tcrypt_result {
	struct completion completion;
	int err;
};

static void tcrypt_complete(struct crypto_async_request *req, int err)
{
	struct tcrypt_result *res = req->data;

	if (err == -EINPROGRESS)
		return;

	res->err = err;
	complete(&res->completion);
}

static inline int do_one_ahash_op(struct ahash_request *req, int ret)
{
	if (ret == -EINPROGRESS || ret == -EBUSY) {
		struct tcrypt_result *tr = req->base.data;

		ret = wait_for_completion_interruptible(&tr->completion);
		if (!ret)
			ret = tr->err;
		INIT_COMPLETION(tr->completion);
	}
	return ret;
}

static int test_ahash_jiffies_digest(struct ahash_request *req, int blen,
				     char *out, int sec)
{
	unsigned long start, end;
	int bcount;
	int ret;

	for (start = jiffies, end = start + sec * HZ, bcount = 0;
	     time_before(jiffies, end); bcount++) {
		ret = do_one_ahash_op(req, crypto_ahash_digest(req));
		if (ret)
			return ret;
	}

	printk("%6u opers/sec, %9lu bytes/sec\n",
	       bcount / sec, ((long)bcount * blen) / sec);

	return 0;
}

static int test_ahash_jiffies(struct ahash_request *req, int blen,
			      int plen, char *out, int sec)
{
	unsigned long start, end;
	int bcount, pcount;
	int ret;

	if (plen == blen)
		return test_ahash_jiffies_digest(req, blen, out, sec);

	for (start = jiffies, end = start + sec * HZ, bcount = 0;
	     time_before(jiffies, end); bcount++) {
		ret = crypto_ahash_init(req);
		if (ret)
			return ret;
		for (pcount = 0; pcount < blen; pcount += plen) {
			ret = do_one_ahash_op(req, crypto_ahash_update(req));
			if (ret)
				return ret;
		}
		/* we assume there is enough space in 'out' for the result */
		ret = do_one_ahash_op(req, crypto_ahash_final(req));
		if (ret)
			return ret;
	}

	pr_cont("%6u opers/sec, %9lu bytes/sec\n",
		bcount / sec, ((long)bcount * blen) / sec);

	return 0;
}

static int test_ahash_cycles_digest(struct ahash_request *req, int blen,
				    char *out)
{
	unsigned long cycles = 0;
	int ret, i;

	/* Warm-up run. */
	for (i = 0; i < 4; i++) {
		ret = do_one_ahash_op(req, crypto_ahash_digest(req));
		if (ret)
			goto out;
	}

	/* The real thing. */
	for (i = 0; i < 8; i++) {
		cycles_t start, end;

		start = get_cycles();

		ret = do_one_ahash_op(req, crypto_ahash_digest(req));
		if (ret)
			goto out;

		end = get_cycles();

		cycles += end - start;
	}

out:
	if (ret)
		return ret;

	pr_cont("%6lu cycles/operation, %4lu cycles/byte\n",
		cycles / 8, cycles / (8 * blen));

	return 0;
}

static int test_ahash_cycles(struct ahash_request *req, int blen,
			     int plen, char *out)
{
	unsigned long cycles = 0;
	int i, pcount, ret;

	if (plen == blen)
		return test_ahash_cycles_digest(req, blen, out);

	/* Warm-up run. */
	for (i = 0; i < 4; i++) {
		ret = crypto_ahash_init(req);
		if (ret)
			goto out;
		for (pcount = 0; pcount < blen; pcount += plen) {
			ret = do_one_ahash_op(req, crypto_ahash_update(req));
			if (ret)
				goto out;
		}
		ret = do_one_ahash_op(req, crypto_ahash_final(req));
		if (ret)
			goto out;
	}

	/* The real thing. */
	for (i = 0; i < 8; i++) {
		cycles_t start, end;

		start = get_cycles();

		ret = crypto_ahash_init(req);
		if (ret)
			goto out;
		for (pcount = 0; pcount < blen; pcount += plen) {
			ret = do_one_ahash_op(req, crypto_ahash_update(req));
			if (ret)
				goto out;
		}
		ret = do_one_ahash_op(req, crypto_ahash_final(req));
		if (ret)
			goto out;

		end = get_cycles();

		cycles += end - start;
	}

out:
	if (ret)
		return ret;

	pr_cont("%6lu cycles/operation, %4lu cycles/byte\n",
		cycles / 8, cycles / (8 * blen));

	return 0;
}

static void test_ahash_speed(const char *algo, unsigned int sec,
			     struct hash_speed *speed)
{
	struct scatterlist sg[TVMEMSIZE];
	struct tcrypt_result tresult;
	struct ahash_request *req;
	struct crypto_ahash *tfm;
	static char output[1024];
	int i, ret;

	printk(KERN_INFO "\ntesting speed of async %s\n", algo);

	tfm = crypto_alloc_ahash(algo, 0, 0);
	if (IS_ERR(tfm)) {
		pr_err("failed to load transform for %s: %ld\n",
		       algo, PTR_ERR(tfm));
		return;
	}

	if (crypto_ahash_digestsize(tfm) > sizeof(output)) {
		pr_err("digestsize(%u) > outputbuffer(%zu)\n",
		       crypto_ahash_digestsize(tfm), sizeof(output));
		goto out;
	}

	test_hash_sg_init(sg);
	req = ahash_request_alloc(tfm, GFP_KERNEL);
	if (!req) {
		pr_err("ahash request allocation failure\n");
		goto out;
	}

	init_completion(&tresult.completion);
	ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
				   tcrypt_complete, &tresult);

	for (i = 0; speed[i].blen != 0; i++) {
		if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
			pr_err("template (%u) too big for tvmem (%lu)\n",
			       speed[i].blen, TVMEMSIZE * PAGE_SIZE);
			break;
		}

		pr_info("test%3u "
			"(%5u byte blocks,%5u bytes per update,%4u updates): ",
			i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);

		ahash_request_set_crypt(req, sg, output, speed[i].plen);

		if (sec)
			ret = test_ahash_jiffies(req, speed[i].blen,
						 speed[i].plen, output, sec);
		else
			ret = test_ahash_cycles(req, speed[i].blen,
						speed[i].plen, output);

		if (ret) {
			pr_err("hashing failed ret=%d\n", ret);
			break;
		}
	}

	ahash_request_free(req);

out:
	crypto_free_ahash(tfm);
}

716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
static inline int do_one_acipher_op(struct ablkcipher_request *req, int ret)
{
	if (ret == -EINPROGRESS || ret == -EBUSY) {
		struct tcrypt_result *tr = req->base.data;

		ret = wait_for_completion_interruptible(&tr->completion);
		if (!ret)
			ret = tr->err;
		INIT_COMPLETION(tr->completion);
	}

	return ret;
}

static int test_acipher_jiffies(struct ablkcipher_request *req, int enc,
				int blen, int sec)
{
	unsigned long start, end;
	int bcount;
	int ret;

	for (start = jiffies, end = start + sec * HZ, bcount = 0;
	     time_before(jiffies, end); bcount++) {
		if (enc)
			ret = do_one_acipher_op(req,
						crypto_ablkcipher_encrypt(req));
		else
			ret = do_one_acipher_op(req,
						crypto_ablkcipher_decrypt(req));

		if (ret)
			return ret;
	}

	pr_cont("%d operations in %d seconds (%ld bytes)\n",
		bcount, sec, (long)bcount * blen);
	return 0;
}

static int test_acipher_cycles(struct ablkcipher_request *req, int enc,
			       int blen)
{
	unsigned long cycles = 0;
	int ret = 0;
	int i;

	/* Warm-up run. */
	for (i = 0; i < 4; i++) {
		if (enc)
			ret = do_one_acipher_op(req,
						crypto_ablkcipher_encrypt(req));
		else
			ret = do_one_acipher_op(req,
						crypto_ablkcipher_decrypt(req));

		if (ret)
			goto out;
	}

	/* The real thing. */
	for (i = 0; i < 8; i++) {
		cycles_t start, end;

		start = get_cycles();
		if (enc)
			ret = do_one_acipher_op(req,
						crypto_ablkcipher_encrypt(req));
		else
			ret = do_one_acipher_op(req,
						crypto_ablkcipher_decrypt(req));
		end = get_cycles();

		if (ret)
			goto out;

		cycles += end - start;
	}

out:
	if (ret == 0)
		pr_cont("1 operation in %lu cycles (%d bytes)\n",
			(cycles + 4) / 8, blen);

	return ret;
}

static void test_acipher_speed(const char *algo, int enc, unsigned int sec,
			       struct cipher_speed_template *template,
			       unsigned int tcount, u8 *keysize)
{
806
	unsigned int ret, i, j, k, iv_len;
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
	struct tcrypt_result tresult;
	const char *key;
	char iv[128];
	struct ablkcipher_request *req;
	struct crypto_ablkcipher *tfm;
	const char *e;
	u32 *b_size;

	if (enc == ENCRYPT)
		e = "encryption";
	else
		e = "decryption";

	pr_info("\ntesting speed of async %s %s\n", algo, e);

	init_completion(&tresult.completion);

	tfm = crypto_alloc_ablkcipher(algo, 0, 0);

	if (IS_ERR(tfm)) {
		pr_err("failed to load transform for %s: %ld\n", algo,
		       PTR_ERR(tfm));
		return;
	}

	req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
	if (!req) {
		pr_err("tcrypt: skcipher: Failed to allocate request for %s\n",
		       algo);
		goto out;
	}

	ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
					tcrypt_complete, &tresult);

	i = 0;
	do {
		b_size = block_sizes;

		do {
			struct scatterlist sg[TVMEMSIZE];

			if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
				pr_err("template (%u) too big for "
				       "tvmem (%lu)\n", *keysize + *b_size,
				       TVMEMSIZE * PAGE_SIZE);
				goto out_free_req;
			}

			pr_info("test %u (%d bit key, %d byte blocks): ", i,
				*keysize * 8, *b_size);

			memset(tvmem[0], 0xff, PAGE_SIZE);

			/* set key, plain text and IV */
			key = tvmem[0];
			for (j = 0; j < tcount; j++) {
				if (template[j].klen == *keysize) {
					key = template[j].key;
					break;
				}
			}

			crypto_ablkcipher_clear_flags(tfm, ~0);

			ret = crypto_ablkcipher_setkey(tfm, key, *keysize);
			if (ret) {
				pr_err("setkey() failed flags=%x\n",
					crypto_ablkcipher_get_flags(tfm));
				goto out_free_req;
			}

			sg_init_table(sg, TVMEMSIZE);
880 881 882 883

			k = *keysize + *b_size;
			if (k > PAGE_SIZE) {
				sg_set_buf(sg, tvmem[0] + *keysize,
884
				   PAGE_SIZE - *keysize);
885 886 887 888 889 890 891 892 893 894 895 896
				k -= PAGE_SIZE;
				j = 1;
				while (k > PAGE_SIZE) {
					sg_set_buf(sg + j, tvmem[j], PAGE_SIZE);
					memset(tvmem[j], 0xff, PAGE_SIZE);
					j++;
					k -= PAGE_SIZE;
				}
				sg_set_buf(sg + j, tvmem[j], k);
				memset(tvmem[j], 0xff, k);
			} else {
				sg_set_buf(sg, tvmem[0] + *keysize, *b_size);
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
			}

			iv_len = crypto_ablkcipher_ivsize(tfm);
			if (iv_len)
				memset(&iv, 0xff, iv_len);

			ablkcipher_request_set_crypt(req, sg, sg, *b_size, iv);

			if (sec)
				ret = test_acipher_jiffies(req, enc,
							   *b_size, sec);
			else
				ret = test_acipher_cycles(req, enc,
							  *b_size);

			if (ret) {
				pr_err("%s() failed flags=%x\n", e,
					crypto_ablkcipher_get_flags(tfm));
				break;
			}
			b_size++;
			i++;
		} while (*b_size);
		keysize++;
	} while (*keysize);

out_free_req:
	ablkcipher_request_free(req);
out:
	crypto_free_ablkcipher(tfm);
}

929
static void test_available(void)
L
Linus Torvalds 已提交
930 931
{
	char **name = check;
932

L
Linus Torvalds 已提交
933 934
	while (*name) {
		printk("alg %s ", *name);
935
		printk(crypto_has_alg(*name, 0, 0) ?
936
		       "found\n" : "not found\n");
L
Linus Torvalds 已提交
937
		name++;
938
	}
L
Linus Torvalds 已提交
939 940
}

941 942
static inline int tcrypt_test(const char *alg)
{
943 944 945 946 947 948 949
	int ret;

	ret = alg_test(alg, alg, 0, 0);
	/* non-fips algs return -EINVAL in fips mode */
	if (fips_enabled && ret == -EINVAL)
		ret = 0;
	return ret;
950 951
}

952
static int do_test(int m)
953 954
{
	int i;
955
	int ret = 0;
956 957

	switch (m) {
L
Linus Torvalds 已提交
958
	case 0:
959
		for (i = 1; i < 200; i++)
960
			ret += do_test(i);
L
Linus Torvalds 已提交
961 962 963
		break;

	case 1:
964
		ret += tcrypt_test("md5");
L
Linus Torvalds 已提交
965 966 967
		break;

	case 2:
968
		ret += tcrypt_test("sha1");
L
Linus Torvalds 已提交
969 970 971
		break;

	case 3:
972 973
		ret += tcrypt_test("ecb(des)");
		ret += tcrypt_test("cbc(des)");
L
Linus Torvalds 已提交
974 975 976
		break;

	case 4:
977 978
		ret += tcrypt_test("ecb(des3_ede)");
		ret += tcrypt_test("cbc(des3_ede)");
L
Linus Torvalds 已提交
979 980 981
		break;

	case 5:
982
		ret += tcrypt_test("md4");
L
Linus Torvalds 已提交
983
		break;
984

L
Linus Torvalds 已提交
985
	case 6:
986
		ret += tcrypt_test("sha256");
L
Linus Torvalds 已提交
987
		break;
988

L
Linus Torvalds 已提交
989
	case 7:
990 991
		ret += tcrypt_test("ecb(blowfish)");
		ret += tcrypt_test("cbc(blowfish)");
992
		ret += tcrypt_test("ctr(blowfish)");
L
Linus Torvalds 已提交
993 994 995
		break;

	case 8:
996 997
		ret += tcrypt_test("ecb(twofish)");
		ret += tcrypt_test("cbc(twofish)");
998
		ret += tcrypt_test("ctr(twofish)");
999
		ret += tcrypt_test("lrw(twofish)");
1000
		ret += tcrypt_test("xts(twofish)");
L
Linus Torvalds 已提交
1001
		break;
1002

L
Linus Torvalds 已提交
1003
	case 9:
1004
		ret += tcrypt_test("ecb(serpent)");
1005 1006
		ret += tcrypt_test("cbc(serpent)");
		ret += tcrypt_test("ctr(serpent)");
1007
		ret += tcrypt_test("lrw(serpent)");
1008
		ret += tcrypt_test("xts(serpent)");
L
Linus Torvalds 已提交
1009 1010 1011
		break;

	case 10:
1012 1013 1014 1015 1016 1017
		ret += tcrypt_test("ecb(aes)");
		ret += tcrypt_test("cbc(aes)");
		ret += tcrypt_test("lrw(aes)");
		ret += tcrypt_test("xts(aes)");
		ret += tcrypt_test("ctr(aes)");
		ret += tcrypt_test("rfc3686(ctr(aes))");
L
Linus Torvalds 已提交
1018 1019 1020
		break;

	case 11:
1021
		ret += tcrypt_test("sha384");
L
Linus Torvalds 已提交
1022
		break;
1023

L
Linus Torvalds 已提交
1024
	case 12:
1025
		ret += tcrypt_test("sha512");
L
Linus Torvalds 已提交
1026 1027 1028
		break;

	case 13:
1029
		ret += tcrypt_test("deflate");
L
Linus Torvalds 已提交
1030 1031 1032
		break;

	case 14:
1033
		ret += tcrypt_test("ecb(cast5)");
1034 1035
		ret += tcrypt_test("cbc(cast5)");
		ret += tcrypt_test("ctr(cast5)");
L
Linus Torvalds 已提交
1036 1037 1038
		break;

	case 15:
1039
		ret += tcrypt_test("ecb(cast6)");
1040 1041 1042 1043
		ret += tcrypt_test("cbc(cast6)");
		ret += tcrypt_test("ctr(cast6)");
		ret += tcrypt_test("lrw(cast6)");
		ret += tcrypt_test("xts(cast6)");
L
Linus Torvalds 已提交
1044 1045 1046
		break;

	case 16:
1047
		ret += tcrypt_test("ecb(arc4)");
L
Linus Torvalds 已提交
1048 1049 1050
		break;

	case 17:
1051
		ret += tcrypt_test("michael_mic");
L
Linus Torvalds 已提交
1052 1053 1054
		break;

	case 18:
1055
		ret += tcrypt_test("crc32c");
L
Linus Torvalds 已提交
1056 1057 1058
		break;

	case 19:
1059
		ret += tcrypt_test("ecb(tea)");
L
Linus Torvalds 已提交
1060 1061 1062
		break;

	case 20:
1063
		ret += tcrypt_test("ecb(xtea)");
L
Linus Torvalds 已提交
1064 1065 1066
		break;

	case 21:
1067
		ret += tcrypt_test("ecb(khazad)");
L
Linus Torvalds 已提交
1068 1069 1070
		break;

	case 22:
1071
		ret += tcrypt_test("wp512");
L
Linus Torvalds 已提交
1072 1073 1074
		break;

	case 23:
1075
		ret += tcrypt_test("wp384");
L
Linus Torvalds 已提交
1076 1077 1078
		break;

	case 24:
1079
		ret += tcrypt_test("wp256");
L
Linus Torvalds 已提交
1080 1081 1082
		break;

	case 25:
1083
		ret += tcrypt_test("ecb(tnepres)");
L
Linus Torvalds 已提交
1084 1085 1086
		break;

	case 26:
1087 1088
		ret += tcrypt_test("ecb(anubis)");
		ret += tcrypt_test("cbc(anubis)");
L
Linus Torvalds 已提交
1089 1090 1091
		break;

	case 27:
1092
		ret += tcrypt_test("tgr192");
L
Linus Torvalds 已提交
1093 1094 1095 1096
		break;

	case 28:

1097
		ret += tcrypt_test("tgr160");
L
Linus Torvalds 已提交
1098 1099 1100
		break;

	case 29:
1101
		ret += tcrypt_test("tgr128");
L
Linus Torvalds 已提交
1102
		break;
1103

A
Aaron Grothe 已提交
1104
	case 30:
1105
		ret += tcrypt_test("ecb(xeta)");
A
Aaron Grothe 已提交
1106
		break;
L
Linus Torvalds 已提交
1107

1108
	case 31:
1109
		ret += tcrypt_test("pcbc(fcrypt)");
1110 1111
		break;

1112
	case 32:
1113 1114
		ret += tcrypt_test("ecb(camellia)");
		ret += tcrypt_test("cbc(camellia)");
1115 1116 1117
		ret += tcrypt_test("ctr(camellia)");
		ret += tcrypt_test("lrw(camellia)");
		ret += tcrypt_test("xts(camellia)");
1118
		break;
1119
	case 33:
1120
		ret += tcrypt_test("sha224");
1121
		break;
1122

1123
	case 34:
1124
		ret += tcrypt_test("salsa20");
1125 1126
		break;

1127
	case 35:
1128
		ret += tcrypt_test("gcm(aes)");
1129 1130
		break;

1131
	case 36:
1132
		ret += tcrypt_test("lzo");
1133 1134
		break;

J
Joy Latten 已提交
1135
	case 37:
1136
		ret += tcrypt_test("ccm(aes)");
J
Joy Latten 已提交
1137 1138
		break;

1139
	case 38:
1140
		ret += tcrypt_test("cts(cbc(aes))");
1141 1142
		break;

1143
        case 39:
1144
		ret += tcrypt_test("rmd128");
1145 1146 1147
		break;

        case 40:
1148
		ret += tcrypt_test("rmd160");
1149 1150
		break;

1151
	case 41:
1152
		ret += tcrypt_test("rmd256");
1153 1154 1155
		break;

	case 42:
1156
		ret += tcrypt_test("rmd320");
1157 1158 1159
		break;

	case 43:
1160
		ret += tcrypt_test("ecb(seed)");
1161 1162
		break;

1163
	case 44:
1164
		ret += tcrypt_test("zlib");
1165 1166
		break;

1167
	case 45:
1168
		ret += tcrypt_test("rfc4309(ccm(aes))");
1169 1170
		break;

1171 1172 1173 1174
	case 46:
		ret += tcrypt_test("ghash");
		break;

L
Linus Torvalds 已提交
1175
	case 100:
1176
		ret += tcrypt_test("hmac(md5)");
L
Linus Torvalds 已提交
1177
		break;
1178

L
Linus Torvalds 已提交
1179
	case 101:
1180
		ret += tcrypt_test("hmac(sha1)");
L
Linus Torvalds 已提交
1181
		break;
1182

L
Linus Torvalds 已提交
1183
	case 102:
1184
		ret += tcrypt_test("hmac(sha256)");
L
Linus Torvalds 已提交
1185 1186
		break;

1187
	case 103:
1188
		ret += tcrypt_test("hmac(sha384)");
1189 1190 1191
		break;

	case 104:
1192
		ret += tcrypt_test("hmac(sha512)");
1193
		break;
1194

1195
	case 105:
1196
		ret += tcrypt_test("hmac(sha224)");
1197
		break;
L
Linus Torvalds 已提交
1198

1199
	case 106:
1200
		ret += tcrypt_test("xcbc(aes)");
1201 1202
		break;

1203
	case 107:
1204
		ret += tcrypt_test("hmac(rmd128)");
1205 1206 1207
		break;

	case 108:
1208
		ret += tcrypt_test("hmac(rmd160)");
1209 1210
		break;

1211 1212 1213
	case 109:
		ret += tcrypt_test("vmac(aes)");
		break;
1214 1215 1216
	case 110:
		ret += tcrypt_test("hmac(crc32)");
		break;
1217

1218
	case 150:
1219
		ret += tcrypt_test("ansi_cprng");
1220 1221
		break;

1222 1223 1224 1225
	case 151:
		ret += tcrypt_test("rfc4106(gcm(aes))");
		break;

H
Harald Welte 已提交
1226
	case 200:
1227
		test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
1228
				speed_template_16_24_32);
1229
		test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
1230
				speed_template_16_24_32);
1231
		test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
1232
				speed_template_16_24_32);
1233
		test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
1234
				speed_template_16_24_32);
R
Rik Snel 已提交
1235
		test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
1236
				speed_template_32_40_48);
R
Rik Snel 已提交
1237
		test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
1238
				speed_template_32_40_48);
1239
		test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
1240
				speed_template_32_48_64);
1241
		test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
1242
				speed_template_32_48_64);
1243 1244 1245 1246
		test_cipher_speed("ctr(aes)", ENCRYPT, sec, NULL, 0,
				speed_template_16_24_32);
		test_cipher_speed("ctr(aes)", DECRYPT, sec, NULL, 0,
				speed_template_16_24_32);
H
Harald Welte 已提交
1247 1248 1249
		break;

	case 201:
1250
		test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
1251
				des3_speed_template, DES3_SPEED_VECTORS,
1252
				speed_template_24);
1253
		test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
1254
				des3_speed_template, DES3_SPEED_VECTORS,
1255
				speed_template_24);
1256
		test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
1257
				des3_speed_template, DES3_SPEED_VECTORS,
1258
				speed_template_24);
1259
		test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
1260
				des3_speed_template, DES3_SPEED_VECTORS,
1261
				speed_template_24);
H
Harald Welte 已提交
1262 1263 1264
		break;

	case 202:
1265
		test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
1266
				speed_template_16_24_32);
1267
		test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
1268
				speed_template_16_24_32);
1269
		test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
1270
				speed_template_16_24_32);
1271
		test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
1272
				speed_template_16_24_32);
1273 1274 1275 1276
		test_cipher_speed("ctr(twofish)", ENCRYPT, sec, NULL, 0,
				speed_template_16_24_32);
		test_cipher_speed("ctr(twofish)", DECRYPT, sec, NULL, 0,
				speed_template_16_24_32);
1277 1278 1279 1280
		test_cipher_speed("lrw(twofish)", ENCRYPT, sec, NULL, 0,
				speed_template_32_40_48);
		test_cipher_speed("lrw(twofish)", DECRYPT, sec, NULL, 0,
				speed_template_32_40_48);
1281 1282 1283 1284
		test_cipher_speed("xts(twofish)", ENCRYPT, sec, NULL, 0,
				speed_template_32_48_64);
		test_cipher_speed("xts(twofish)", DECRYPT, sec, NULL, 0,
				speed_template_32_48_64);
H
Harald Welte 已提交
1285 1286 1287
		break;

	case 203:
1288
		test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
1289
				  speed_template_8_32);
1290
		test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
1291
				  speed_template_8_32);
1292
		test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
1293
				  speed_template_8_32);
1294
		test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
1295
				  speed_template_8_32);
1296 1297 1298 1299
		test_cipher_speed("ctr(blowfish)", ENCRYPT, sec, NULL, 0,
				  speed_template_8_32);
		test_cipher_speed("ctr(blowfish)", DECRYPT, sec, NULL, 0,
				  speed_template_8_32);
H
Harald Welte 已提交
1300 1301 1302
		break;

	case 204:
1303
		test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
1304
				  speed_template_8);
1305
		test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
1306
				  speed_template_8);
1307
		test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
1308
				  speed_template_8);
1309
		test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
1310
				  speed_template_8);
H
Harald Welte 已提交
1311 1312
		break;

1313 1314
	case 205:
		test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
1315
				speed_template_16_24_32);
1316
		test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
1317
				speed_template_16_24_32);
1318
		test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
1319
				speed_template_16_24_32);
1320
		test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
1321
				speed_template_16_24_32);
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
		test_cipher_speed("ctr(camellia)", ENCRYPT, sec, NULL, 0,
				speed_template_16_24_32);
		test_cipher_speed("ctr(camellia)", DECRYPT, sec, NULL, 0,
				speed_template_16_24_32);
		test_cipher_speed("lrw(camellia)", ENCRYPT, sec, NULL, 0,
				speed_template_32_40_48);
		test_cipher_speed("lrw(camellia)", DECRYPT, sec, NULL, 0,
				speed_template_32_40_48);
		test_cipher_speed("xts(camellia)", ENCRYPT, sec, NULL, 0,
				speed_template_32_48_64);
		test_cipher_speed("xts(camellia)", DECRYPT, sec, NULL, 0,
				speed_template_32_48_64);
1334 1335
		break;

1336 1337
	case 206:
		test_cipher_speed("salsa20", ENCRYPT, sec, NULL, 0,
1338
				  speed_template_16_32);
1339 1340
		break;

1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
	case 207:
		test_cipher_speed("ecb(serpent)", ENCRYPT, sec, NULL, 0,
				  speed_template_16_32);
		test_cipher_speed("ecb(serpent)", DECRYPT, sec, NULL, 0,
				  speed_template_16_32);
		test_cipher_speed("cbc(serpent)", ENCRYPT, sec, NULL, 0,
				  speed_template_16_32);
		test_cipher_speed("cbc(serpent)", DECRYPT, sec, NULL, 0,
				  speed_template_16_32);
		test_cipher_speed("ctr(serpent)", ENCRYPT, sec, NULL, 0,
				  speed_template_16_32);
		test_cipher_speed("ctr(serpent)", DECRYPT, sec, NULL, 0,
				  speed_template_16_32);
1354 1355 1356 1357
		test_cipher_speed("lrw(serpent)", ENCRYPT, sec, NULL, 0,
				  speed_template_32_48);
		test_cipher_speed("lrw(serpent)", DECRYPT, sec, NULL, 0,
				  speed_template_32_48);
1358 1359 1360 1361
		test_cipher_speed("xts(serpent)", ENCRYPT, sec, NULL, 0,
				  speed_template_32_64);
		test_cipher_speed("xts(serpent)", DECRYPT, sec, NULL, 0,
				  speed_template_32_64);
1362 1363
		break;

1364 1365 1366 1367 1368
	case 208:
		test_cipher_speed("ecb(arc4)", ENCRYPT, sec, NULL, 0,
				  speed_template_8);
		break;

1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
	case 209:
		test_cipher_speed("ecb(cast5)", ENCRYPT, sec, NULL, 0,
				  speed_template_8_16);
		test_cipher_speed("ecb(cast5)", DECRYPT, sec, NULL, 0,
				  speed_template_8_16);
		test_cipher_speed("cbc(cast5)", ENCRYPT, sec, NULL, 0,
				  speed_template_8_16);
		test_cipher_speed("cbc(cast5)", DECRYPT, sec, NULL, 0,
				  speed_template_8_16);
		test_cipher_speed("ctr(cast5)", ENCRYPT, sec, NULL, 0,
				  speed_template_8_16);
		test_cipher_speed("ctr(cast5)", DECRYPT, sec, NULL, 0,
				  speed_template_8_16);
		break;

1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
	case 210:
		test_cipher_speed("ecb(cast6)", ENCRYPT, sec, NULL, 0,
				  speed_template_16_32);
		test_cipher_speed("ecb(cast6)", DECRYPT, sec, NULL, 0,
				  speed_template_16_32);
		test_cipher_speed("cbc(cast6)", ENCRYPT, sec, NULL, 0,
				  speed_template_16_32);
		test_cipher_speed("cbc(cast6)", DECRYPT, sec, NULL, 0,
				  speed_template_16_32);
		test_cipher_speed("ctr(cast6)", ENCRYPT, sec, NULL, 0,
				  speed_template_16_32);
		test_cipher_speed("ctr(cast6)", DECRYPT, sec, NULL, 0,
				  speed_template_16_32);
		test_cipher_speed("lrw(cast6)", ENCRYPT, sec, NULL, 0,
				  speed_template_32_48);
		test_cipher_speed("lrw(cast6)", DECRYPT, sec, NULL, 0,
				  speed_template_32_48);
		test_cipher_speed("xts(cast6)", ENCRYPT, sec, NULL, 0,
				  speed_template_32_64);
		test_cipher_speed("xts(cast6)", DECRYPT, sec, NULL, 0,
				  speed_template_32_64);
		break;

1407 1408 1409 1410
	case 300:
		/* fall through */

	case 301:
1411
		test_hash_speed("md4", sec, generic_hash_speed_template);
1412 1413 1414
		if (mode > 300 && mode < 400) break;

	case 302:
1415
		test_hash_speed("md5", sec, generic_hash_speed_template);
1416 1417 1418
		if (mode > 300 && mode < 400) break;

	case 303:
1419
		test_hash_speed("sha1", sec, generic_hash_speed_template);
1420 1421 1422
		if (mode > 300 && mode < 400) break;

	case 304:
1423
		test_hash_speed("sha256", sec, generic_hash_speed_template);
1424 1425 1426
		if (mode > 300 && mode < 400) break;

	case 305:
1427
		test_hash_speed("sha384", sec, generic_hash_speed_template);
1428 1429 1430
		if (mode > 300 && mode < 400) break;

	case 306:
1431
		test_hash_speed("sha512", sec, generic_hash_speed_template);
1432 1433 1434
		if (mode > 300 && mode < 400) break;

	case 307:
1435
		test_hash_speed("wp256", sec, generic_hash_speed_template);
1436 1437 1438
		if (mode > 300 && mode < 400) break;

	case 308:
1439
		test_hash_speed("wp384", sec, generic_hash_speed_template);
1440 1441 1442
		if (mode > 300 && mode < 400) break;

	case 309:
1443
		test_hash_speed("wp512", sec, generic_hash_speed_template);
1444 1445 1446
		if (mode > 300 && mode < 400) break;

	case 310:
1447
		test_hash_speed("tgr128", sec, generic_hash_speed_template);
1448 1449 1450
		if (mode > 300 && mode < 400) break;

	case 311:
1451
		test_hash_speed("tgr160", sec, generic_hash_speed_template);
1452 1453 1454
		if (mode > 300 && mode < 400) break;

	case 312:
1455
		test_hash_speed("tgr192", sec, generic_hash_speed_template);
1456 1457
		if (mode > 300 && mode < 400) break;

1458 1459 1460 1461
	case 313:
		test_hash_speed("sha224", sec, generic_hash_speed_template);
		if (mode > 300 && mode < 400) break;

1462 1463 1464 1465 1466 1467 1468 1469
	case 314:
		test_hash_speed("rmd128", sec, generic_hash_speed_template);
		if (mode > 300 && mode < 400) break;

	case 315:
		test_hash_speed("rmd160", sec, generic_hash_speed_template);
		if (mode > 300 && mode < 400) break;

1470 1471 1472 1473 1474 1475 1476 1477
	case 316:
		test_hash_speed("rmd256", sec, generic_hash_speed_template);
		if (mode > 300 && mode < 400) break;

	case 317:
		test_hash_speed("rmd320", sec, generic_hash_speed_template);
		if (mode > 300 && mode < 400) break;

1478 1479 1480 1481
	case 318:
		test_hash_speed("ghash-generic", sec, hash_speed_template_16);
		if (mode > 300 && mode < 400) break;

1482 1483 1484 1485
	case 319:
		test_hash_speed("crc32c", sec, generic_hash_speed_template);
		if (mode > 300 && mode < 400) break;

1486 1487 1488
	case 399:
		break;

1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
	case 400:
		/* fall through */

	case 401:
		test_ahash_speed("md4", sec, generic_hash_speed_template);
		if (mode > 400 && mode < 500) break;

	case 402:
		test_ahash_speed("md5", sec, generic_hash_speed_template);
		if (mode > 400 && mode < 500) break;

	case 403:
		test_ahash_speed("sha1", sec, generic_hash_speed_template);
		if (mode > 400 && mode < 500) break;

	case 404:
		test_ahash_speed("sha256", sec, generic_hash_speed_template);
		if (mode > 400 && mode < 500) break;

	case 405:
		test_ahash_speed("sha384", sec, generic_hash_speed_template);
		if (mode > 400 && mode < 500) break;

	case 406:
		test_ahash_speed("sha512", sec, generic_hash_speed_template);
		if (mode > 400 && mode < 500) break;

	case 407:
		test_ahash_speed("wp256", sec, generic_hash_speed_template);
		if (mode > 400 && mode < 500) break;

	case 408:
		test_ahash_speed("wp384", sec, generic_hash_speed_template);
		if (mode > 400 && mode < 500) break;

	case 409:
		test_ahash_speed("wp512", sec, generic_hash_speed_template);
		if (mode > 400 && mode < 500) break;

	case 410:
		test_ahash_speed("tgr128", sec, generic_hash_speed_template);
		if (mode > 400 && mode < 500) break;

	case 411:
		test_ahash_speed("tgr160", sec, generic_hash_speed_template);
		if (mode > 400 && mode < 500) break;

	case 412:
		test_ahash_speed("tgr192", sec, generic_hash_speed_template);
		if (mode > 400 && mode < 500) break;

	case 413:
		test_ahash_speed("sha224", sec, generic_hash_speed_template);
		if (mode > 400 && mode < 500) break;

	case 414:
		test_ahash_speed("rmd128", sec, generic_hash_speed_template);
		if (mode > 400 && mode < 500) break;

	case 415:
		test_ahash_speed("rmd160", sec, generic_hash_speed_template);
		if (mode > 400 && mode < 500) break;

	case 416:
		test_ahash_speed("rmd256", sec, generic_hash_speed_template);
		if (mode > 400 && mode < 500) break;

	case 417:
		test_ahash_speed("rmd320", sec, generic_hash_speed_template);
		if (mode > 400 && mode < 500) break;

	case 499:
		break;

1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
	case 500:
		test_acipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
				   speed_template_16_24_32);
		test_acipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
				   speed_template_16_24_32);
		test_acipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
				   speed_template_16_24_32);
		test_acipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
				   speed_template_16_24_32);
		test_acipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
				   speed_template_32_40_48);
		test_acipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
				   speed_template_32_40_48);
		test_acipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
				   speed_template_32_48_64);
		test_acipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
				   speed_template_32_48_64);
		test_acipher_speed("ctr(aes)", ENCRYPT, sec, NULL, 0,
				   speed_template_16_24_32);
		test_acipher_speed("ctr(aes)", DECRYPT, sec, NULL, 0,
				   speed_template_16_24_32);
1584 1585 1586 1587 1588 1589 1590 1591
		test_acipher_speed("cfb(aes)", ENCRYPT, sec, NULL, 0,
				   speed_template_16_24_32);
		test_acipher_speed("cfb(aes)", DECRYPT, sec, NULL, 0,
				   speed_template_16_24_32);
		test_acipher_speed("ofb(aes)", ENCRYPT, sec, NULL, 0,
				   speed_template_16_24_32);
		test_acipher_speed("ofb(aes)", DECRYPT, sec, NULL, 0,
				   speed_template_16_24_32);
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
		break;

	case 501:
		test_acipher_speed("ecb(des3_ede)", ENCRYPT, sec,
				   des3_speed_template, DES3_SPEED_VECTORS,
				   speed_template_24);
		test_acipher_speed("ecb(des3_ede)", DECRYPT, sec,
				   des3_speed_template, DES3_SPEED_VECTORS,
				   speed_template_24);
		test_acipher_speed("cbc(des3_ede)", ENCRYPT, sec,
				   des3_speed_template, DES3_SPEED_VECTORS,
				   speed_template_24);
		test_acipher_speed("cbc(des3_ede)", DECRYPT, sec,
				   des3_speed_template, DES3_SPEED_VECTORS,
				   speed_template_24);
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618
		test_acipher_speed("cfb(des3_ede)", ENCRYPT, sec,
				   des3_speed_template, DES3_SPEED_VECTORS,
				   speed_template_24);
		test_acipher_speed("cfb(des3_ede)", DECRYPT, sec,
				   des3_speed_template, DES3_SPEED_VECTORS,
				   speed_template_24);
		test_acipher_speed("ofb(des3_ede)", ENCRYPT, sec,
				   des3_speed_template, DES3_SPEED_VECTORS,
				   speed_template_24);
		test_acipher_speed("ofb(des3_ede)", DECRYPT, sec,
				   des3_speed_template, DES3_SPEED_VECTORS,
				   speed_template_24);
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629
		break;

	case 502:
		test_acipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
				   speed_template_8);
		test_acipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
				   speed_template_8);
		test_acipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
				   speed_template_8);
		test_acipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
				   speed_template_8);
1630 1631 1632 1633 1634 1635 1636 1637
		test_acipher_speed("cfb(des)", ENCRYPT, sec, NULL, 0,
				   speed_template_8);
		test_acipher_speed("cfb(des)", DECRYPT, sec, NULL, 0,
				   speed_template_8);
		test_acipher_speed("ofb(des)", ENCRYPT, sec, NULL, 0,
				   speed_template_8);
		test_acipher_speed("ofb(des)", DECRYPT, sec, NULL, 0,
				   speed_template_8);
1638 1639
		break;

1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652
	case 503:
		test_acipher_speed("ecb(serpent)", ENCRYPT, sec, NULL, 0,
				   speed_template_16_32);
		test_acipher_speed("ecb(serpent)", DECRYPT, sec, NULL, 0,
				   speed_template_16_32);
		test_acipher_speed("cbc(serpent)", ENCRYPT, sec, NULL, 0,
				   speed_template_16_32);
		test_acipher_speed("cbc(serpent)", DECRYPT, sec, NULL, 0,
				   speed_template_16_32);
		test_acipher_speed("ctr(serpent)", ENCRYPT, sec, NULL, 0,
				   speed_template_16_32);
		test_acipher_speed("ctr(serpent)", DECRYPT, sec, NULL, 0,
				   speed_template_16_32);
1653 1654 1655 1656
		test_acipher_speed("lrw(serpent)", ENCRYPT, sec, NULL, 0,
				   speed_template_32_48);
		test_acipher_speed("lrw(serpent)", DECRYPT, sec, NULL, 0,
				   speed_template_32_48);
1657 1658 1659 1660
		test_acipher_speed("xts(serpent)", ENCRYPT, sec, NULL, 0,
				   speed_template_32_64);
		test_acipher_speed("xts(serpent)", DECRYPT, sec, NULL, 0,
				   speed_template_32_64);
1661 1662
		break;

1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685
	case 504:
		test_acipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
				   speed_template_16_24_32);
		test_acipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
				   speed_template_16_24_32);
		test_acipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
				   speed_template_16_24_32);
		test_acipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
				   speed_template_16_24_32);
		test_acipher_speed("ctr(twofish)", ENCRYPT, sec, NULL, 0,
				   speed_template_16_24_32);
		test_acipher_speed("ctr(twofish)", DECRYPT, sec, NULL, 0,
				   speed_template_16_24_32);
		test_acipher_speed("lrw(twofish)", ENCRYPT, sec, NULL, 0,
				   speed_template_32_40_48);
		test_acipher_speed("lrw(twofish)", DECRYPT, sec, NULL, 0,
				   speed_template_32_40_48);
		test_acipher_speed("xts(twofish)", ENCRYPT, sec, NULL, 0,
				   speed_template_32_48_64);
		test_acipher_speed("xts(twofish)", DECRYPT, sec, NULL, 0,
				   speed_template_32_48_64);
		break;

1686 1687 1688 1689 1690
	case 505:
		test_acipher_speed("ecb(arc4)", ENCRYPT, sec, NULL, 0,
				   speed_template_8);
		break;

1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705
	case 506:
		test_acipher_speed("ecb(cast5)", ENCRYPT, sec, NULL, 0,
				   speed_template_8_16);
		test_acipher_speed("ecb(cast5)", DECRYPT, sec, NULL, 0,
				   speed_template_8_16);
		test_acipher_speed("cbc(cast5)", ENCRYPT, sec, NULL, 0,
				   speed_template_8_16);
		test_acipher_speed("cbc(cast5)", DECRYPT, sec, NULL, 0,
				   speed_template_8_16);
		test_acipher_speed("ctr(cast5)", ENCRYPT, sec, NULL, 0,
				   speed_template_8_16);
		test_acipher_speed("ctr(cast5)", DECRYPT, sec, NULL, 0,
				   speed_template_8_16);
		break;

1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
	case 507:
		test_acipher_speed("ecb(cast6)", ENCRYPT, sec, NULL, 0,
				   speed_template_16_32);
		test_acipher_speed("ecb(cast6)", DECRYPT, sec, NULL, 0,
				   speed_template_16_32);
		test_acipher_speed("cbc(cast6)", ENCRYPT, sec, NULL, 0,
				   speed_template_16_32);
		test_acipher_speed("cbc(cast6)", DECRYPT, sec, NULL, 0,
				   speed_template_16_32);
		test_acipher_speed("ctr(cast6)", ENCRYPT, sec, NULL, 0,
				   speed_template_16_32);
		test_acipher_speed("ctr(cast6)", DECRYPT, sec, NULL, 0,
				   speed_template_16_32);
		test_acipher_speed("lrw(cast6)", ENCRYPT, sec, NULL, 0,
				   speed_template_32_48);
		test_acipher_speed("lrw(cast6)", DECRYPT, sec, NULL, 0,
				   speed_template_32_48);
		test_acipher_speed("xts(cast6)", ENCRYPT, sec, NULL, 0,
				   speed_template_32_64);
		test_acipher_speed("xts(cast6)", DECRYPT, sec, NULL, 0,
				   speed_template_32_64);
		break;

L
Linus Torvalds 已提交
1729 1730 1731 1732
	case 1000:
		test_available();
		break;
	}
1733 1734

	return ret;
L
Linus Torvalds 已提交
1735 1736
}

H
Herbert Xu 已提交
1737
static int do_alg_test(const char *alg, u32 type, u32 mask)
1738
{
H
Herbert Xu 已提交
1739 1740
	return crypto_has_alg(alg, type, mask ?: CRYPTO_ALG_TYPE_MASK) ?
	       0 : -ENOENT;
1741 1742
}

1743
static int __init tcrypt_mod_init(void)
L
Linus Torvalds 已提交
1744
{
1745
	int err = -ENOMEM;
1746
	int i;
1747

1748 1749 1750 1751 1752
	for (i = 0; i < TVMEMSIZE; i++) {
		tvmem[i] = (void *)__get_free_page(GFP_KERNEL);
		if (!tvmem[i])
			goto err_free_tv;
	}
L
Linus Torvalds 已提交
1753

1754
	if (alg)
H
Herbert Xu 已提交
1755
		err = do_alg_test(alg, type, mask);
1756 1757 1758
	else
		err = do_test(mode);

1759 1760 1761 1762
	if (err) {
		printk(KERN_ERR "tcrypt: one or more tests failed!\n");
		goto err_free_tv;
	}
1763

1764 1765 1766 1767
	/* We intentionaly return -EAGAIN to prevent keeping the module,
	 * unless we're running in fips mode. It does all its work from
	 * init() and doesn't offer any runtime functionality, but in
	 * the fips case, checking for a successful load is helpful.
1768 1769 1770
	 * => we don't need it in the memory, do we?
	 *                                        -- mludvig
	 */
1771 1772
	if (!fips_enabled)
		err = -EAGAIN;
1773

1774 1775 1776
err_free_tv:
	for (i = 0; i < TVMEMSIZE && tvmem[i]; i++)
		free_page((unsigned long)tvmem[i]);
1777 1778

	return err;
L
Linus Torvalds 已提交
1779 1780 1781 1782 1783 1784
}

/*
 * If an init function is provided, an exit function must also be provided
 * to allow module unload.
 */
1785
static void __exit tcrypt_mod_fini(void) { }
L
Linus Torvalds 已提交
1786

1787 1788
module_init(tcrypt_mod_init);
module_exit(tcrypt_mod_fini);
L
Linus Torvalds 已提交
1789

1790 1791
module_param(alg, charp, 0);
module_param(type, uint, 0);
H
Herbert Xu 已提交
1792
module_param(mask, uint, 0);
L
Linus Torvalds 已提交
1793
module_param(mode, int, 0);
H
Harald Welte 已提交
1794
module_param(sec, uint, 0);
1795 1796
MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
		      "(defaults to zero which uses CPU cycles instead)");
L
Linus Torvalds 已提交
1797 1798 1799 1800

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Quick & dirty crypto testing module");
MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");