tcrypt.c 51.1 KB
Newer Older
1
/*
Linus Torvalds's avatar
Linus Torvalds committed
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
Linus Torvalds's avatar
Linus Torvalds committed
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.
 *
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
21 22 23 24
 * any later version.
 *
 */

25
#include <crypto/hash.h>
26
#include <linux/err.h>
Linus Torvalds's avatar
Linus Torvalds committed
27
#include <linux/init.h>
28
#include <linux/gfp.h>
Linus Torvalds's avatar
Linus Torvalds committed
29
#include <linux/module.h>
30
#include <linux/scatterlist.h>
Linus Torvalds's avatar
Linus Torvalds committed
31 32
#include <linux/string.h>
#include <linux/moduleparam.h>
33
#include <linux/jiffies.h>
34 35
#include <linux/timex.h>
#include <linux/interrupt.h>
Linus Torvalds's avatar
Linus Torvalds committed
36
#include "tcrypt.h"
37
#include "internal.h"
Linus Torvalds's avatar
Linus Torvalds committed
38 39

/*
40
 * Need slab memory for testing (size in number of pages).
Linus Torvalds's avatar
Linus Torvalds committed
41
 */
42
#define TVMEMSIZE	4
Linus Torvalds's avatar
Linus Torvalds committed
43 44

/*
45
* Used by test_cipher_speed()
Linus Torvalds's avatar
Linus Torvalds committed
46 47 48 49
*/
#define ENCRYPT 1
#define DECRYPT 0

50 51 52
/*
 * Used by test_cipher_speed()
 */
53
static unsigned int sec;
54

55 56
static char *alg = NULL;
static u32 type;
57
static u32 mask;
Linus Torvalds's avatar
Linus Torvalds committed
58
static int mode;
59
static char *tvmem[TVMEMSIZE];
Linus Torvalds's avatar
Linus Torvalds committed
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
Linus Torvalds's avatar
Linus Torvalds committed
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
static int test_aead_jiffies(struct aead_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 = crypto_aead_encrypt(req);
		else
			ret = crypto_aead_decrypt(req);

		if (ret)
			return ret;
	}

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

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

	local_irq_disable();

	/* Warm-up run. */
	for (i = 0; i < 4; i++) {
		if (enc)
			ret = crypto_aead_encrypt(req);
		else
			ret = crypto_aead_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 = crypto_aead_encrypt(req);
		else
			ret = crypto_aead_decrypt(req);
		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;
}

209
static u32 block_sizes[] = { 16, 64, 256, 1024, 8192, 0 };
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
static u32 aead_sizes[] = { 16, 64, 256, 512, 1024, 2048, 4096, 8192, 0 };

#define XBUFSIZE 8
#define MAX_IVLEN 32

static int testmgr_alloc_buf(char *buf[XBUFSIZE])
{
	int i;

	for (i = 0; i < XBUFSIZE; i++) {
		buf[i] = (void *)__get_free_page(GFP_KERNEL);
		if (!buf[i])
			goto err_free_buf;
	}

	return 0;

err_free_buf:
	while (i-- > 0)
		free_page((unsigned long)buf[i]);

	return -ENOMEM;
}

static void testmgr_free_buf(char *buf[XBUFSIZE])
{
	int i;

	for (i = 0; i < XBUFSIZE; i++)
		free_page((unsigned long)buf[i]);
}

static void sg_init_aead(struct scatterlist *sg, char *xbuf[XBUFSIZE],
			unsigned int buflen)
{
	int np = (buflen + PAGE_SIZE - 1)/PAGE_SIZE;
	int k, rem;

	np = (np > XBUFSIZE) ? XBUFSIZE : np;
	rem = buflen % PAGE_SIZE;
	if (np > XBUFSIZE) {
		rem = PAGE_SIZE;
		np = XBUFSIZE;
	}
	sg_init_table(sg, np);
	for (k = 0; k < np; ++k) {
		if (k == (np-1))
			sg_set_buf(&sg[k], xbuf[k], rem);
		else
			sg_set_buf(&sg[k], xbuf[k], PAGE_SIZE);
	}
}

static void test_aead_speed(const char *algo, int enc, unsigned int sec,
			    struct aead_speed_template *template,
			    unsigned int tcount, u8 authsize,
			    unsigned int aad_size, u8 *keysize)
{
	unsigned int i, j;
	struct crypto_aead *tfm;
	int ret = -ENOMEM;
	const char *key;
	struct aead_request *req;
	struct scatterlist *sg;
	struct scatterlist *asg;
	struct scatterlist *sgout;
	const char *e;
	void *assoc;
	char iv[MAX_IVLEN];
	char *xbuf[XBUFSIZE];
	char *xoutbuf[XBUFSIZE];
	char *axbuf[XBUFSIZE];
	unsigned int *b_size;
	unsigned int iv_len;

285 286 287 288 289
	if (aad_size >= PAGE_SIZE) {
		pr_err("associate data length (%u) too big\n", aad_size);
		return;
	}

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
	if (enc == ENCRYPT)
		e = "encryption";
	else
		e = "decryption";

	if (testmgr_alloc_buf(xbuf))
		goto out_noxbuf;
	if (testmgr_alloc_buf(axbuf))
		goto out_noaxbuf;
	if (testmgr_alloc_buf(xoutbuf))
		goto out_nooutbuf;

	sg = kmalloc(sizeof(*sg) * 8 * 3, GFP_KERNEL);
	if (!sg)
		goto out_nosg;
	asg = &sg[8];
	sgout = &asg[8];


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

	tfm = crypto_alloc_aead(algo, 0, 0);

	if (IS_ERR(tfm)) {
		pr_err("alg: aead: Failed to load transform for %s: %ld\n", algo,
		       PTR_ERR(tfm));
316
		goto out_notfm;
317 318 319 320 321 322
	}

	req = aead_request_alloc(tfm, GFP_KERNEL);
	if (!req) {
		pr_err("alg: aead: Failed to allocate request for %s\n",
		       algo);
323
		goto out_noreq;
324 325 326 327 328 329 330
	}

	i = 0;
	do {
		b_size = aead_sizes;
		do {
			assoc = axbuf[0];
331
			memset(assoc, 0xff, aad_size);
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
			sg_init_one(&asg[0], assoc, aad_size);

			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;
			}

			key = tvmem[0];
			for (j = 0; j < tcount; j++) {
				if (template[j].klen == *keysize) {
					key = template[j].key;
					break;
				}
			}
			ret = crypto_aead_setkey(tfm, key, *keysize);
			ret = crypto_aead_setauthsize(tfm, authsize);

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

			crypto_aead_clear_flags(tfm, ~0);
			printk(KERN_INFO "test %u (%d bit key, %d byte blocks): ",
					i, *keysize * 8, *b_size);


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

			if (ret) {
				pr_err("setkey() failed flags=%x\n",
						crypto_aead_get_flags(tfm));
				goto out;
			}

			sg_init_aead(&sg[0], xbuf,
				    *b_size + (enc ? authsize : 0));

			sg_init_aead(&sgout[0], xoutbuf,
				    *b_size + (enc ? authsize : 0));

			aead_request_set_crypt(req, sg, sgout, *b_size, iv);
			aead_request_set_assoc(req, asg, aad_size);

			if (sec)
				ret = test_aead_jiffies(req, enc, *b_size, sec);
			else
				ret = test_aead_cycles(req, enc, *b_size);

			if (ret) {
				pr_err("%s() failed return code=%d\n", e, ret);
				break;
			}
			b_size++;
			i++;
		} while (*b_size);
		keysize++;
	} while (*keysize);

out:
393 394
	aead_request_free(req);
out_noreq:
395
	crypto_free_aead(tfm);
396
out_notfm:
397 398 399 400 401 402 403 404 405 406
	kfree(sg);
out_nosg:
	testmgr_free_buf(xoutbuf);
out_nooutbuf:
	testmgr_free_buf(axbuf);
out_noaxbuf:
	testmgr_free_buf(xbuf);
out_noxbuf:
	return;
}
407

408
static void test_cipher_speed(const char *algo, int enc, unsigned int sec,
409
			      struct cipher_speed_template *template,
410
			      unsigned int tcount, u8 *keysize)
411
{
412
	unsigned int ret, i, j, iv_len;
413 414
	const char *key;
	char iv[128];
415 416 417
	struct crypto_blkcipher *tfm;
	struct blkcipher_desc desc;
	const char *e;
418
	u32 *b_size;
419 420 421 422 423 424

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

425
	printk("\ntesting speed of %s %s\n", algo, e);
426

427
	tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC);
428

429 430 431
	if (IS_ERR(tfm)) {
		printk("failed to load transform for %s: %ld\n", algo,
		       PTR_ERR(tfm));
432 433
		return;
	}
434 435
	desc.tfm = tfm;
	desc.flags = 0;
436

437 438
	i = 0;
	do {
439

440 441
		b_size = block_sizes;
		do {
442
			struct scatterlist sg[TVMEMSIZE];
443

444 445 446 447
			if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
				printk("template (%u) too big for "
				       "tvmem (%lu)\n", *keysize + *b_size,
				       TVMEMSIZE * PAGE_SIZE);
448 449
				goto out;
			}
450

451 452 453
			printk("test %u (%d bit key, %d byte blocks): ", i,
					*keysize * 8, *b_size);

454
			memset(tvmem[0], 0xff, PAGE_SIZE);
455 456

			/* set key, plain text and IV */
457
			key = tvmem[0];
458 459 460 461 462
			for (j = 0; j < tcount; j++) {
				if (template[j].klen == *keysize) {
					key = template[j].key;
					break;
				}
463
			}
464

465 466 467 468 469 470
			ret = crypto_blkcipher_setkey(tfm, key, *keysize);
			if (ret) {
				printk("setkey() failed flags=%x\n",
						crypto_blkcipher_get_flags(tfm));
				goto out;
			}
471

472 473 474 475 476 477 478 479
			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);
			}

480 481 482 483 484
			iv_len = crypto_blkcipher_ivsize(tfm);
			if (iv_len) {
				memset(&iv, 0xff, iv_len);
				crypto_blkcipher_set_iv(tfm, iv, iv_len);
			}
485

486
			if (sec)
487 488
				ret = test_cipher_jiffies(&desc, enc, sg,
							  *b_size, sec);
489
			else
490 491
				ret = test_cipher_cycles(&desc, enc, sg,
							 *b_size);
492

493 494 495 496 497 498 499 500 501
			if (ret) {
				printk("%s() failed flags=%x\n", e, desc.flags);
				break;
			}
			b_size++;
			i++;
		} while (*b_size);
		keysize++;
	} while (*keysize);
502 503

out:
504
	crypto_free_blkcipher(tfm);
505 506
}

507 508
static int test_hash_jiffies_digest(struct hash_desc *desc,
				    struct scatterlist *sg, int blen,
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
				    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;
}

528 529
static int test_hash_jiffies(struct hash_desc *desc, struct scatterlist *sg,
			     int blen, int plen, char *out, int sec)
530 531 532
{
	unsigned long start, end;
	int bcount, pcount;
533 534 535
	int ret;

	if (plen == blen)
536
		return test_hash_jiffies_digest(desc, sg, blen, out, sec);
537

538 539
	for (start = jiffies, end = start + sec * HZ, bcount = 0;
	     time_before(jiffies, end); bcount++) {
540 541 542
		ret = crypto_hash_init(desc);
		if (ret)
			return ret;
543
		for (pcount = 0; pcount < blen; pcount += plen) {
544 545 546
			ret = crypto_hash_update(desc, sg, plen);
			if (ret)
				return ret;
547 548
		}
		/* we assume there is enough space in 'out' for the result */
549 550 551
		ret = crypto_hash_final(desc, out);
		if (ret)
			return ret;
552 553 554 555 556
	}

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

557 558 559
	return 0;
}

560 561
static int test_hash_cycles_digest(struct hash_desc *desc,
				   struct scatterlist *sg, int blen, char *out)
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
{
	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;
601 602
}

603 604
static int test_hash_cycles(struct hash_desc *desc, struct scatterlist *sg,
			    int blen, int plen, char *out)
605 606 607
{
	unsigned long cycles = 0;
	int i, pcount;
608 609 610
	int ret;

	if (plen == blen)
611
		return test_hash_cycles_digest(desc, sg, blen, out);
612

613 614 615 616
	local_irq_disable();

	/* Warm-up run. */
	for (i = 0; i < 4; i++) {
617 618 619
		ret = crypto_hash_init(desc);
		if (ret)
			goto out;
620
		for (pcount = 0; pcount < blen; pcount += plen) {
621 622 623
			ret = crypto_hash_update(desc, sg, plen);
			if (ret)
				goto out;
624
		}
625
		ret = crypto_hash_final(desc, out);
626 627
		if (ret)
			goto out;
628 629 630 631 632 633 634 635
	}

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

		start = get_cycles();

636 637 638
		ret = crypto_hash_init(desc);
		if (ret)
			goto out;
639
		for (pcount = 0; pcount < blen; pcount += plen) {
640 641 642
			ret = crypto_hash_update(desc, sg, plen);
			if (ret)
				goto out;
643
		}
644 645 646
		ret = crypto_hash_final(desc, out);
		if (ret)
			goto out;
647 648 649 650 651 652

		end = get_cycles();

		cycles += end - start;
	}

653
out:
654 655
	local_irq_enable();

656 657 658
	if (ret)
		return ret;

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

662
	return 0;
663 664
}

665 666 667 668 669 670 671 672 673 674 675
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);
	}
}

676 677
static void test_hash_speed(const char *algo, unsigned int sec,
			    struct hash_speed *speed)
678
{
679
	struct scatterlist sg[TVMEMSIZE];
680 681
	struct crypto_hash *tfm;
	struct hash_desc desc;
682
	static char output[1024];
683
	int i;
684
	int ret;
685

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

688
	tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
689

690
	if (IS_ERR(tfm)) {
691
		printk(KERN_ERR "failed to load transform for %s: %ld\n", algo,
692
		       PTR_ERR(tfm));
693 694 695
		return;
	}

696 697 698 699
	desc.tfm = tfm;
	desc.flags = 0;

	if (crypto_hash_digestsize(tfm) > sizeof(output)) {
700
		printk(KERN_ERR "digestsize(%u) > outputbuffer(%zu)\n",
701
		       crypto_hash_digestsize(tfm), sizeof(output));
702 703 704
		goto out;
	}

705
	test_hash_sg_init(sg);
706
	for (i = 0; speed[i].blen != 0; i++) {
707
		if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
708 709
			printk(KERN_ERR
			       "template (%u) too big for tvmem (%lu)\n",
710
			       speed[i].blen, TVMEMSIZE * PAGE_SIZE);
711 712 713
			goto out;
		}

714 715 716
		if (speed[i].klen)
			crypto_hash_setkey(tfm, tvmem[0], speed[i].klen);

717 718
		printk(KERN_INFO "test%3u "
		       "(%5u byte blocks,%5u bytes per update,%4u updates): ",
719 720 721
		       i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);

		if (sec)
722
			ret = test_hash_jiffies(&desc, sg, speed[i].blen,
723
						speed[i].plen, output, sec);
724
		else
725
			ret = test_hash_cycles(&desc, sg, speed[i].blen,
726 727 728
					       speed[i].plen, output);

		if (ret) {
729
			printk(KERN_ERR "hashing failed ret=%d\n", ret);
730 731
			break;
		}
732 733 734
	}

out:
735
	crypto_free_hash(tfm);
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
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;
762
		reinit_completion(&tr->completion);
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 806 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 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 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 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
	}
	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);
}

982 983 984 985 986 987 988 989
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;
990
		reinit_completion(&tr->completion);
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
	}

	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)
{
1072
	unsigned int ret, i, j, k, iv_len;
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
	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);
1146 1147 1148 1149

			k = *keysize + *b_size;
			if (k > PAGE_SIZE) {
				sg_set_buf(sg, tvmem[0] + *keysize,
1150
				   PAGE_SIZE - *keysize);
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
				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);
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
			}

			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);
}

1195
static void test_available(void)
Linus Torvalds's avatar
Linus Torvalds committed
1196 1197
{
	char **name = check;
1198

Linus Torvalds's avatar
Linus Torvalds committed
1199 1200
	while (*name) {
		printk("alg %s ", *name);
1201
		printk(crypto_has_alg(*name, 0, 0) ?
1202
		       "found\n" : "not found\n");
Linus Torvalds's avatar
Linus Torvalds committed
1203
		name++;
1204
	}
Linus Torvalds's avatar
Linus Torvalds committed
1205 1206
}

1207 1208
static inline int tcrypt_test(const char *alg)
{
1209 1210 1211 1212 1213 1214 1215
	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;
1216 1217
}

1218
static int do_test(int m)
1219 1220
{
	int i;
1221
	int ret = 0;
1222 1223

	switch (m) {
Linus Torvalds's avatar
Linus Torvalds committed
1224
	case 0:
1225
		for (i = 1; i < 200; i++)
1226
			ret += do_test(i);
Linus Torvalds's avatar
Linus Torvalds committed
1227 1228 1229
		break;

	case 1:
1230
		ret += tcrypt_test("md5");
Linus Torvalds's avatar
Linus Torvalds committed
1231 1232 1233
		break;

	case 2:
1234
		ret += tcrypt_test("sha1");
Linus Torvalds's avatar
Linus Torvalds committed
1235 1236 1237
		break;

	case 3:
1238 1239
		ret += tcrypt_test("ecb(des)");
		ret += tcrypt_test("cbc(des)");
1240
		ret += tcrypt_test("ctr(des)");
Linus Torvalds's avatar
Linus Torvalds committed
1241 1242 1243
		break;

	case 4:
1244 1245
		ret += tcrypt_test("ecb(des3_ede)");
		ret += tcrypt_test("cbc(des3_ede)");
1246
		ret += tcrypt_test("ctr(des3_ede)");
Linus Torvalds's avatar
Linus Torvalds committed
1247 1248 1249
		break;

	case 5:
1250
		ret += tcrypt_test("md4");
Linus Torvalds's avatar
Linus Torvalds committed
1251
		break;
1252

Linus Torvalds's avatar
Linus Torvalds committed
1253
	case 6:
1254
		ret += tcrypt_test("sha256");
Linus Torvalds's avatar
Linus Torvalds committed
1255
		break;
1256

Linus Torvalds's avatar
Linus Torvalds committed
1257
	case 7:
1258 1259
		ret += tcrypt_test("ecb(blowfish)");
		ret += tcrypt_test("cbc(blowfish)");
1260
		ret += tcrypt_test("ctr(blowfish)");
Linus Torvalds's avatar
Linus Torvalds committed
1261 1262 1263
		break;

	case 8:
1264 1265
		ret += tcrypt_test("ecb(twofish)");
		ret += tcrypt_test("cbc(twofish)");
1266
		ret += tcrypt_test("ctr(twofish)");
1267
		ret += tcrypt_test("lrw(twofish)");
1268
		ret += tcrypt_test("xts(twofish)");
Linus Torvalds's avatar
Linus Torvalds committed
1269
		break;
1270

Linus Torvalds's avatar
Linus Torvalds committed
1271
	case 9:
1272
		ret += tcrypt_test("ecb(serpent)");
1273 1274
		ret += tcrypt_test("cbc(serpent)");
		ret += tcrypt_test("ctr(serpent)");
1275
		ret += tcrypt_test("lrw(serpent)");
1276
		ret += tcrypt_test("xts(serpent)");
Linus Torvalds's avatar
Linus Torvalds committed
1277 1278 1279
		break;

	case 10:
1280 1281 1282 1283 1284 1285
		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))");
Linus Torvalds's avatar
Linus Torvalds committed
1286 1287 1288
		break;

	case 11:
1289
		ret += tcrypt_test("sha384");
Linus Torvalds's avatar
Linus Torvalds committed
1290
		break;
1291

Linus Torvalds's avatar
Linus Torvalds committed
1292
	case 12:
1293
		ret += tcrypt_test("sha512");
Linus Torvalds's avatar
Linus Torvalds committed
1294 1295 1296
		break;

	case 13:
1297
		ret += tcrypt_test("deflate");
Linus Torvalds's avatar
Linus Torvalds committed
1298 1299 1300
		break;

	case 14:
1301
		ret += tcrypt_test("ecb(cast5)");
1302 1303
		ret += tcrypt_test("cbc(cast5)");
		ret += tcrypt_test("ctr(cast5)");
Linus Torvalds's avatar
Linus Torvalds committed
1304 1305 1306
		break;

	case 15:
1307
		ret += tcrypt_test("ecb(cast6)");
1308 1309 1310 1311
		ret += tcrypt_test("cbc(cast6)");
		ret += tcrypt_test("ctr(cast6)");
		ret += tcrypt_test("lrw(cast6)");
		ret += tcrypt_test("xts(cast6)");
Linus Torvalds's avatar
Linus Torvalds committed
1312 1313 1314
		break;

	case 16:
1315
		ret += tcrypt_test("ecb(arc4)");
Linus Torvalds's avatar
Linus Torvalds committed
1316 1317 1318
		break;

	case 17:
1319
		ret += tcrypt_test("michael_mic");
Linus Torvalds's avatar
Linus Torvalds committed
1320 1321 1322
		break;

	case 18:
1323
		ret += tcrypt_test("crc32c");
Linus Torvalds's avatar
Linus Torvalds committed
1324 1325 1326
		break;

	case 19:
1327
		ret += tcrypt_test("ecb(tea)");
Linus Torvalds's avatar
Linus Torvalds committed
1328 1329 1330
		break;

	case 20:
1331
		ret += tcrypt_test("ecb(xtea)");
Linus Torvalds's avatar
Linus Torvalds committed
1332 1333 1334
		break;

	case 21:
1335
		ret += tcrypt_test("ecb(khazad)");
Linus Torvalds's avatar
Linus Torvalds committed
1336 1337 1338
		break;

	case 22:
1339
		ret += tcrypt_test("wp512");
Linus Torvalds's avatar
Linus Torvalds committed
1340 1341 1342
		break;

	case 23:
1343
		ret += tcrypt_test("wp384");
Linus Torvalds's avatar
Linus Torvalds committed
1344 1345 1346
		break;

	case 24:
1347
		ret += tcrypt_test("wp256");
Linus Torvalds's avatar
Linus Torvalds committed
1348 1349 1350
		break;

	case 25:
1351
		ret += tcrypt_test("ecb(tnepres)");
Linus Torvalds's avatar
Linus Torvalds committed
1352 1353 1354
		break;

	case 26:
1355 1356
		ret += tcrypt_test("ecb(anubis)");
		ret += tcrypt_test("cbc(anubis)");
Linus Torvalds's avatar
Linus Torvalds committed
1357 1358 1359
		break;

	case 27:
1360
		ret += tcrypt_test("tgr192");
Linus Torvalds's avatar
Linus Torvalds committed
1361 1362 1363
		break;

	case 28:
1364
		ret += tcrypt_test("tgr160");
Linus Torvalds's avatar
Linus Torvalds committed
1365 1366 1367
		break;

	case 29:
1368
		ret += tcrypt_test("tgr128");
Linus Torvalds's avatar
Linus Torvalds committed
1369
		break;
1370

1371
	case 30:
1372
		ret += tcrypt_test("ecb(xeta)");
1373
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1374

1375
	case 31:
1376
		ret += tcrypt_test("pcbc(fcrypt)");
1377 1378
		break;

1379
	case 32: