tcrypt.c 76.2 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 26
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

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

/*
44
 * Need slab memory for testing (size in number of pages).
Linus Torvalds's avatar
Linus Torvalds committed
45
 */
46
#define TVMEMSIZE	4
Linus Torvalds's avatar
Linus Torvalds committed
47 48

/*
49
* Used by test_cipher_speed()
Linus Torvalds's avatar
Linus Torvalds committed
50 51 52 53
*/
#define ENCRYPT 1
#define DECRYPT 0

54 55
#define MAX_DIGEST_SIZE		64

56 57 58 59 60
/*
 * return a string with the driver name
 */
#define get_driver_name(tfm_type, tfm) crypto_tfm_alg_driver_name(tfm_type ## _tfm(tfm))

61 62 63
/*
 * Used by test_cipher_speed()
 */
64
static unsigned int sec;
65

66 67
static char *alg = NULL;
static u32 type;
68
static u32 mask;
Linus Torvalds's avatar
Linus Torvalds committed
69
static int mode;
70
static u32 num_mb = 8;
71
static char *tvmem[TVMEMSIZE];
Linus Torvalds's avatar
Linus Torvalds committed
72 73

static char *check[] = {
74
	"des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256", "sm3",
75 76
	"blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
	"cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
77
	"khazad", "wp512", "wp384", "wp256", "tnepres", "xeta",  "fcrypt",
78
	"camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
79 80
	"lzo", "cts", "zlib", "sha3-224", "sha3-256", "sha3-384", "sha3-512",
	NULL
Linus Torvalds's avatar
Linus Torvalds committed
81 82
};

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
static u32 block_sizes[] = { 16, 64, 256, 1024, 8192, 0 };
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, const void *assoc,
			 unsigned int aad_size)
{
	int np = (buflen + PAGE_SIZE - 1)/PAGE_SIZE;
	int k, rem;

	if (np > XBUFSIZE) {
		rem = PAGE_SIZE;
		np = XBUFSIZE;
	} else {
		rem = buflen % PAGE_SIZE;
	}

	sg_init_table(sg, np + 1);

	sg_set_buf(&sg[0], assoc, aad_size);

	if (rem)
		np--;
	for (k = 0; k < np; k++)
		sg_set_buf(&sg[k + 1], xbuf[k], PAGE_SIZE);

	if (rem)
		sg_set_buf(&sg[k + 1], xbuf[k], rem);
}

143 144
static inline int do_one_aead_op(struct aead_request *req, int ret)
{
145
	struct crypto_wait *wait = req->base.data;
146

147
	return crypto_wait_req(ret, wait);
148 149
}

150 151 152 153 154 155 156 157 158 159 160
struct test_mb_aead_data {
	struct scatterlist sg[XBUFSIZE];
	struct scatterlist sgout[XBUFSIZE];
	struct aead_request *req;
	struct crypto_wait wait;
	char *xbuf[XBUFSIZE];
	char *xoutbuf[XBUFSIZE];
	char *axbuf[XBUFSIZE];
};

static int do_mult_aead_op(struct test_mb_aead_data *data, int enc,
161
				u32 num_mb, int *rc)
162
{
163
	int i, err = 0;
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

	/* Fire up a bunch of concurrent requests */
	for (i = 0; i < num_mb; i++) {
		if (enc == ENCRYPT)
			rc[i] = crypto_aead_encrypt(data[i].req);
		else
			rc[i] = crypto_aead_decrypt(data[i].req);
	}

	/* Wait for all requests to finish */
	for (i = 0; i < num_mb; i++) {
		rc[i] = crypto_wait_req(rc[i], &data[i].wait);

		if (rc[i]) {
			pr_info("concurrent request %d error %d\n", i, rc[i]);
			err = rc[i];
		}
	}

	return err;
}

static int test_mb_aead_jiffies(struct test_mb_aead_data *data, int enc,
				int blen, int secs, u32 num_mb)
188 189 190
{
	unsigned long start, end;
	int bcount;
191 192 193 194 195 196
	int ret = 0;
	int *rc;

	rc = kcalloc(num_mb, sizeof(*rc), GFP_KERNEL);
	if (!rc)
		return -ENOMEM;
197

198
	for (start = jiffies, end = start + secs * HZ, bcount = 0;
199
	     time_before(jiffies, end); bcount++) {
200
		ret = do_mult_aead_op(data, enc, num_mb, rc);
201
		if (ret)
202
			goto out;
203 204
	}

205 206
	pr_cont("%d operations in %d seconds (%ld bytes)\n",
		bcount * num_mb, secs, (long)bcount * blen * num_mb);
207 208 209 210

out:
	kfree(rc);
	return ret;
211 212
}

213 214
static int test_mb_aead_cycles(struct test_mb_aead_data *data, int enc,
			       int blen, u32 num_mb)
215 216 217 218
{
	unsigned long cycles = 0;
	int ret = 0;
	int i;
219 220 221 222 223
	int *rc;

	rc = kcalloc(num_mb, sizeof(*rc), GFP_KERNEL);
	if (!rc)
		return -ENOMEM;
224 225 226

	/* Warm-up run. */
	for (i = 0; i < 4; i++) {
227
		ret = do_mult_aead_op(data, enc, num_mb, rc);
228 229 230 231 232 233 234 235 236
		if (ret)
			goto out;
	}

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

		start = get_cycles();
237
		ret = do_mult_aead_op(data, enc, num_mb, rc);
238 239 240 241 242 243 244 245
		end = get_cycles();

		if (ret)
			goto out;

		cycles += end - start;
	}

246 247
	pr_cont("1 operation in %lu cycles (%d bytes)\n",
		(cycles + 4) / (8 * num_mb), blen);
248

249 250
out:
	kfree(rc);
251 252 253
	return ret;
}

254 255 256 257 258 259 260 261 262 263 264 265 266 267
static void 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)
{
	struct test_mb_aead_data *data;
	struct crypto_aead *tfm;
	unsigned int i, j, iv_len;
	const char *key;
	const char *e;
	void *assoc;
	u32 *b_size;
	char *iv;
	int ret;
268 269


270 271 272 273
	if (aad_size >= PAGE_SIZE) {
		pr_err("associate data length (%u) too big\n", aad_size);
		return;
	}
274

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
	iv = kzalloc(MAX_IVLEN, GFP_KERNEL);
	if (!iv)
		return;

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

	data = kcalloc(num_mb, sizeof(*data), GFP_KERNEL);
	if (!data)
		goto out_free_iv;

	tfm = crypto_alloc_aead(algo, 0, 0);
	if (IS_ERR(tfm)) {
		pr_err("failed to load transform for %s: %ld\n",
			algo, PTR_ERR(tfm));
		goto out_free_data;
293 294
	}

295
	ret = crypto_aead_setauthsize(tfm, authsize);
296

297 298 299 300 301 302
	for (i = 0; i < num_mb; ++i)
		if (testmgr_alloc_buf(data[i].xbuf)) {
			while (i--)
				testmgr_free_buf(data[i].xbuf);
			goto out_free_tfm;
		}
303

304 305 306 307 308 309 310 311 312 313
	for (i = 0; i < num_mb; ++i)
		if (testmgr_alloc_buf(data[i].axbuf)) {
			while (i--)
				testmgr_free_buf(data[i].axbuf);
			goto out_free_xbuf;
		}

	for (i = 0; i < num_mb; ++i)
		if (testmgr_alloc_buf(data[i].xoutbuf)) {
			while (i--)
314
				testmgr_free_buf(data[i].xoutbuf);
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
			goto out_free_axbuf;
		}

	for (i = 0; i < num_mb; ++i) {
		data[i].req = aead_request_alloc(tfm, GFP_KERNEL);
		if (!data[i].req) {
			pr_err("alg: skcipher: Failed to allocate request for %s\n",
			       algo);
			while (i--)
				aead_request_free(data[i].req);
			goto out_free_xoutbuf;
		}
	}

	for (i = 0; i < num_mb; ++i) {
		crypto_init_wait(&data[i].wait);
		aead_request_set_callback(data[i].req,
					  CRYPTO_TFM_REQ_MAY_BACKLOG,
					  crypto_req_done, &data[i].wait);
	}

	pr_info("\ntesting speed of multibuffer %s (%s) %s\n", algo,
		get_driver_name(crypto_aead, tfm), e);

	i = 0;
	do {
		b_size = aead_sizes;
		do {
			if (*b_size + authsize > XBUFSIZE * PAGE_SIZE) {
344
				pr_err("template (%u) too big for buffer (%lu)\n",
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 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
				       authsize + *b_size,
				       XBUFSIZE * PAGE_SIZE);
				goto out;
			}

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

			/* Set up tfm global state, i.e. the key */

			memset(tvmem[0], 0xff, PAGE_SIZE);
			key = tvmem[0];
			for (j = 0; j < tcount; j++) {
				if (template[j].klen == *keysize) {
					key = template[j].key;
					break;
				}
			}

			crypto_aead_clear_flags(tfm, ~0);

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

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

			/* Now setup per request stuff, i.e. buffers */

			for (j = 0; j < num_mb; ++j) {
				struct test_mb_aead_data *cur = &data[j];

				assoc = cur->axbuf[0];
				memset(assoc, 0xff, aad_size);

				sg_init_aead(cur->sg, cur->xbuf,
					     *b_size + (enc ? 0 : authsize),
					     assoc, aad_size);

				sg_init_aead(cur->sgout, cur->xoutbuf,
					     *b_size + (enc ? authsize : 0),
					     assoc, aad_size);

				aead_request_set_ad(cur->req, aad_size);

				if (!enc) {

					aead_request_set_crypt(cur->req,
							       cur->sgout,
							       cur->sg,
							       *b_size, iv);
					ret = crypto_aead_encrypt(cur->req);
					ret = do_one_aead_op(cur->req, ret);

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

				aead_request_set_crypt(cur->req, cur->sg,
						       cur->sgout, *b_size +
						       (enc ? 0 : authsize),
						       iv);

			}

			if (secs)
				ret = test_mb_aead_jiffies(data, enc, *b_size,
							   secs, num_mb);
			else
				ret = test_mb_aead_cycles(data, enc, *b_size,
							  num_mb);

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

out:
	for (i = 0; i < num_mb; ++i)
		aead_request_free(data[i].req);
out_free_xoutbuf:
	for (i = 0; i < num_mb; ++i)
		testmgr_free_buf(data[i].xoutbuf);
out_free_axbuf:
	for (i = 0; i < num_mb; ++i)
		testmgr_free_buf(data[i].axbuf);
out_free_xbuf:
	for (i = 0; i < num_mb; ++i)
		testmgr_free_buf(data[i].xbuf);
out_free_tfm:
	crypto_free_aead(tfm);
out_free_data:
	kfree(data);
out_free_iv:
	kfree(iv);
453 454
}

455 456
static int test_aead_jiffies(struct aead_request *req, int enc,
				int blen, int secs)
457
{
458 459 460
	unsigned long start, end;
	int bcount;
	int ret;
461

462 463 464 465 466 467 468 469 470 471 472 473 474 475
	for (start = jiffies, end = start + secs * HZ, bcount = 0;
	     time_before(jiffies, end); bcount++) {
		if (enc)
			ret = do_one_aead_op(req, crypto_aead_encrypt(req));
		else
			ret = do_one_aead_op(req, crypto_aead_decrypt(req));

		if (ret)
			return ret;
	}

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

478
static int test_aead_cycles(struct aead_request *req, int enc, int blen)
479
{
480 481 482
	unsigned long cycles = 0;
	int ret = 0;
	int i;
483

484 485 486 487 488 489 490 491 492
	/* Warm-up run. */
	for (i = 0; i < 4; i++) {
		if (enc)
			ret = do_one_aead_op(req, crypto_aead_encrypt(req));
		else
			ret = do_one_aead_op(req, crypto_aead_decrypt(req));

		if (ret)
			goto out;
493
	}
494

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

499 500 501 502 503 504
		start = get_cycles();
		if (enc)
			ret = do_one_aead_op(req, crypto_aead_encrypt(req));
		else
			ret = do_one_aead_op(req, crypto_aead_decrypt(req));
		end = get_cycles();
505

506 507
		if (ret)
			goto out;
508

509 510 511 512 513 514 515 516 517
		cycles += end - start;
	}

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

	return ret;
518 519
}

520
static void test_aead_speed(const char *algo, int enc, unsigned int secs,
521 522 523 524 525 526 527 528 529 530 531 532 533
			    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 *sgout;
	const char *e;
	void *assoc;
534
	char *iv;
535 536 537 538 539
	char *xbuf[XBUFSIZE];
	char *xoutbuf[XBUFSIZE];
	char *axbuf[XBUFSIZE];
	unsigned int *b_size;
	unsigned int iv_len;
540
	struct crypto_wait wait;
541

542 543 544 545
	iv = kzalloc(MAX_IVLEN, GFP_KERNEL);
	if (!iv)
		return;

546 547
	if (aad_size >= PAGE_SIZE) {
		pr_err("associate data length (%u) too big\n", aad_size);
548
		goto out_noxbuf;
549 550
	}

551 552 553 554 555 556 557 558 559 560 561 562
	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;

563
	sg = kmalloc(sizeof(*sg) * 9 * 2, GFP_KERNEL);
564 565
	if (!sg)
		goto out_nosg;
566
	sgout = &sg[9];
567

568
	tfm = crypto_alloc_aead(algo, 0, 0);
569 570 571 572

	if (IS_ERR(tfm)) {
		pr_err("alg: aead: Failed to load transform for %s: %ld\n", algo,
		       PTR_ERR(tfm));
573
		goto out_notfm;
574 575
	}

576
	crypto_init_wait(&wait);
577 578 579
	printk(KERN_INFO "\ntesting speed of %s (%s) %s\n", algo,
			get_driver_name(crypto_aead, tfm), e);

580 581 582 583
	req = aead_request_alloc(tfm, GFP_KERNEL);
	if (!req) {
		pr_err("alg: aead: Failed to allocate request for %s\n",
		       algo);
584
		goto out_noreq;
585 586
	}

587
	aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
588
				  crypto_req_done, &wait);
589

590 591 592 593 594
	i = 0;
	do {
		b_size = aead_sizes;
		do {
			assoc = axbuf[0];
595
			memset(assoc, 0xff, aad_size);
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615

			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)
616
				memset(iv, 0xff, iv_len);
617 618 619 620 621 622 623 624 625 626 627 628 629 630

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

631 632
			sg_init_aead(sg, xbuf, *b_size + (enc ? 0 : authsize),
				     assoc, aad_size);
633

634
			sg_init_aead(sgout, xoutbuf,
635 636
				     *b_size + (enc ? authsize : 0), assoc,
				     aad_size);
637

638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
			aead_request_set_ad(req, aad_size);

			if (!enc) {

				/*
				 * For decryption we need a proper auth so
				 * we do the encryption path once with buffers
				 * reversed (input <-> output) to calculate it
				 */
				aead_request_set_crypt(req, sgout, sg,
						       *b_size, iv);
				ret = do_one_aead_op(req,
						     crypto_aead_encrypt(req));

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

659 660 661
			aead_request_set_crypt(req, sg, sgout,
					       *b_size + (enc ? 0 : authsize),
					       iv);
662

663 664 665
			if (secs)
				ret = test_aead_jiffies(req, enc, *b_size,
							secs);
666 667 668 669 670 671 672 673 674 675 676 677 678 679
			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:
680 681
	aead_request_free(req);
out_noreq:
682
	crypto_free_aead(tfm);
683
out_notfm:
684 685 686 687 688 689 690 691
	kfree(sg);
out_nosg:
	testmgr_free_buf(xoutbuf);
out_nooutbuf:
	testmgr_free_buf(axbuf);
out_noaxbuf:
	testmgr_free_buf(xbuf);
out_noxbuf:
692
	kfree(iv);
693
}
694

695 696 697 698 699 700 701 702 703 704 705 706 707
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);
	}
}

static inline int do_one_ahash_op(struct ahash_request *req, int ret)
{
708
	struct crypto_wait *wait = req->base.data;
709

710
	return crypto_wait_req(ret, wait);
711 712
}

713
struct test_mb_ahash_data {
714
	struct scatterlist sg[XBUFSIZE];
715 716
	char result[64];
	struct ahash_request *req;
717
	struct crypto_wait wait;
718 719
	char *xbuf[XBUFSIZE];
};
720

721 722
static inline int do_mult_ahash_op(struct test_mb_ahash_data *data, u32 num_mb,
				   int *rc)
723
{
724
	int i, err = 0;
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747

	/* Fire up a bunch of concurrent requests */
	for (i = 0; i < num_mb; i++)
		rc[i] = crypto_ahash_digest(data[i].req);

	/* Wait for all requests to finish */
	for (i = 0; i < num_mb; i++) {
		rc[i] = crypto_wait_req(rc[i], &data[i].wait);

		if (rc[i]) {
			pr_info("concurrent request %d error %d\n", i, rc[i]);
			err = rc[i];
		}
	}

	return err;
}

static int test_mb_ahash_jiffies(struct test_mb_ahash_data *data, int blen,
				 int secs, u32 num_mb)
{
	unsigned long start, end;
	int bcount;
748 749 750 751 752 753
	int ret = 0;
	int *rc;

	rc = kcalloc(num_mb, sizeof(*rc), GFP_KERNEL);
	if (!rc)
		return -ENOMEM;
754 755 756

	for (start = jiffies, end = start + secs * HZ, bcount = 0;
	     time_before(jiffies, end); bcount++) {
757
		ret = do_mult_ahash_op(data, num_mb, rc);
758
		if (ret)
759
			goto out;
760 761 762 763
	}

	pr_cont("%d operations in %d seconds (%ld bytes)\n",
		bcount * num_mb, secs, (long)bcount * blen * num_mb);
764 765 766 767

out:
	kfree(rc);
	return ret;
768 769 770 771 772 773 774 775
}

static int test_mb_ahash_cycles(struct test_mb_ahash_data *data, int blen,
				u32 num_mb)
{
	unsigned long cycles = 0;
	int ret = 0;
	int i;
776 777 778 779 780
	int *rc;

	rc = kcalloc(num_mb, sizeof(*rc), GFP_KERNEL);
	if (!rc)
		return -ENOMEM;
781 782 783

	/* Warm-up run. */
	for (i = 0; i < 4; i++) {
784
		ret = do_mult_ahash_op(data, num_mb, rc);
785 786 787 788 789 790 791 792 793
		if (ret)
			goto out;
	}

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

		start = get_cycles();
794
		ret = do_mult_ahash_op(data, num_mb, rc);
795 796 797 798 799 800 801 802
		end = get_cycles();

		if (ret)
			goto out;

		cycles += end - start;
	}

803 804
	pr_cont("1 operation in %lu cycles (%d bytes)\n",
		(cycles + 4) / (8 * num_mb), blen);
805

806 807
out:
	kfree(rc);
808 809 810 811
	return ret;
}

static void test_mb_ahash_speed(const char *algo, unsigned int secs,
812
				struct hash_speed *speed, u32 num_mb)
813
{
814
	struct test_mb_ahash_data *data;
815
	struct crypto_ahash *tfm;
816 817 818
	unsigned int i, j, k;
	int ret;

819
	data = kcalloc(num_mb, sizeof(*data), GFP_KERNEL);
820 821
	if (!data)
		return;
822 823 824 825 826

	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));
827
		goto free_data;
828
	}
829

830
	for (i = 0; i < num_mb; ++i) {
831 832
		if (testmgr_alloc_buf(data[i].xbuf))
			goto out;
833

834
		crypto_init_wait(&data[i].wait);
835

836 837
		data[i].req = ahash_request_alloc(tfm, GFP_KERNEL);
		if (!data[i].req) {
838 839
			pr_err("alg: hash: Failed to allocate request for %s\n",
			       algo);
840
			goto out;
841 842
		}

843 844
		ahash_request_set_callback(data[i].req, 0, crypto_req_done,
					   &data[i].wait);
845 846 847 848 849 850

		sg_init_table(data[i].sg, XBUFSIZE);
		for (j = 0; j < XBUFSIZE; j++) {
			sg_set_buf(data[i].sg + j, data[i].xbuf[j], PAGE_SIZE);
			memset(data[i].xbuf[j], 0xff, PAGE_SIZE);
		}
851 852
	}

853 854
	pr_info("\ntesting speed of multibuffer %s (%s)\n", algo,
		get_driver_name(crypto_ahash, tfm));
855 856

	for (i = 0; speed[i].blen != 0; i++) {
857 858 859 860
		/* For some reason this only tests digests. */
		if (speed[i].blen != speed[i].plen)
			continue;

861
		if (speed[i].blen > XBUFSIZE * PAGE_SIZE) {
862
			pr_err("template (%u) too big for tvmem (%lu)\n",
863
			       speed[i].blen, XBUFSIZE * PAGE_SIZE);
864
			goto out;
865 866 867 868 869
		}

		if (speed[i].klen)
			crypto_ahash_setkey(tfm, tvmem[0], speed[i].klen);

870
		for (k = 0; k < num_mb; k++)
871 872
			ahash_request_set_crypt(data[k].req, data[k].sg,
						data[k].result, speed[i].blen);
873

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

879 880 881 882 883
		if (secs)
			ret = test_mb_ahash_jiffies(data, speed[i].blen, secs,
						    num_mb);
		else
			ret = test_mb_ahash_cycles(data, speed[i].blen, num_mb);
884

885 886 887 888 889

		if (ret) {
			pr_err("At least one hashing failed ret=%d\n", ret);
			break;
		}
890 891 892
	}

out:
893
	for (k = 0; k < num_mb; ++k)
894 895
		ahash_request_free(data[k].req);

896
	for (k = 0; k < num_mb; ++k)
897 898 899 900 901 902
		testmgr_free_buf(data[k].xbuf);

	crypto_free_ahash(tfm);

free_data:
	kfree(data);
903 904
}

905
static int test_ahash_jiffies_digest(struct ahash_request *req, int blen,
906
				     char *out, int secs)
907 908 909 910 911
{
	unsigned long start, end;
	int bcount;
	int ret;

912
	for (start = jiffies, end = start + secs * HZ, bcount = 0;
913 914 915 916 917 918 919
	     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",
920
	       bcount / secs, ((long)bcount * blen) / secs);
921 922 923 924 925

	return 0;
}

static int test_ahash_jiffies(struct ahash_request *req, int blen,
926
			      int plen, char *out, int secs)
927 928 929 930 931 932
{
	unsigned long start, end;
	int bcount, pcount;
	int ret;

	if (plen == blen)
933
		return test_ahash_jiffies_digest(req, blen, out, secs);
934

935
	for (start = jiffies, end = start + secs * HZ, bcount = 0;
936
	     time_before(jiffies, end); bcount++) {
937
		ret = do_one_ahash_op(req, crypto_ahash_init(req));
938 939 940 941 942 943 944 945 946 947 948 949 950 951
		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",
952
		bcount / secs, ((long)bcount * blen) / secs);
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 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005

	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++) {
1006
		ret = do_one_ahash_op(req, crypto_ahash_init(req));
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
		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();

1025
		ret = do_one_ahash_op(req, crypto_ahash_init(req));
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
		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;
}

Herbert Xu's avatar
Herbert Xu committed
1052 1053
static void test_ahash_speed_common(const char *algo, unsigned int secs,
				    struct hash_speed *speed, unsigned mask)
1054 1055
{
	struct scatterlist sg[TVMEMSIZE];
1056
	struct crypto_wait wait;
1057 1058
	struct ahash_request *req;
	struct crypto_ahash *tfm;
1059
	char *output;
1060 1061
	int i, ret;

Herbert Xu's avatar
Herbert Xu committed
1062
	tfm = crypto_alloc_ahash(algo, 0, mask);
1063 1064 1065 1066 1067 1068
	if (IS_ERR(tfm)) {
		pr_err("failed to load transform for %s: %ld\n",
		       algo, PTR_ERR(tfm));
		return;
	}

1069 1070 1071
	printk(KERN_INFO "\ntesting speed of async %s (%s)\n", algo,
			get_driver_name(crypto_ahash, tfm));

1072 1073 1074
	if (crypto_ahash_digestsize(tfm) > MAX_DIGEST_SIZE) {
		pr_err("digestsize(%u) > %d\n", crypto_ahash_digestsize(tfm),
		       MAX_DIGEST_SIZE);
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
		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;
	}

1085
	crypto_init_wait(&wait);
1086
	ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1087
				   crypto_req_done, &wait);
1088

1089 1090 1091 1092
	output = kmalloc(MAX_DIGEST_SIZE, GFP_KERNEL);
	if (!output)
		goto out_nomem;

1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
	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);

1106
		if (secs)
1107
			ret = test_ahash_jiffies(req, speed[i].blen,
1108
						 speed[i].plen, output, secs);
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
		else
			ret = test_ahash_cycles(req, speed[i].blen,
						speed[i].plen, output);

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

1119 1120 1121
	kfree(output);

out_nomem:
1122 1123 1124 1125 1126 1127
	ahash_request_free(req);

out:
	crypto_free_ahash(tfm);
}

Herbert Xu's avatar
Herbert Xu committed
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
static void test_ahash_speed(const char *algo, unsigned int secs,
			     struct hash_speed *speed)
{
	return test_ahash_speed_common(algo, secs, speed, 0);
}

static void test_hash_speed(const char *algo, unsigned int secs,
			    struct hash_speed *speed)
{
	return test_ahash_speed_common(algo, secs, speed, CRYPTO_ALG_ASYNC);
}

1140 1141 1142 1143 1144 1145 1146 1147
struct test_mb_skcipher_data {
	struct scatterlist sg[XBUFSIZE];
	struct skcipher_request *req;
	struct crypto_wait wait;
	char *xbuf[XBUFSIZE];
};

static int do_mult_acipher_op(struct test_mb_skcipher_data *data, int enc,
1148
				u32 num_mb, int *rc)
1149
{
1150
	int i, err = 0;
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177

	/* Fire up a bunch of concurrent requests */
	for (i = 0; i < num_mb; i++) {
		if (enc == ENCRYPT)
			rc[i] = crypto_skcipher_encrypt(data[i].req);
		else
			rc[i] = crypto_skcipher_decrypt(data[i].req);
	}

	/* Wait for all requests to finish */
	for (i = 0; i < num_mb; i++) {
		rc[i] = crypto_wait_req(rc[i], &data[i].wait);

		if (rc[i]) {
			pr_info("concurrent request %d error %d\n", i, rc[i]);
			err = rc[i];
		}
	}

	return err;
}

static int test_mb_acipher_jiffies(struct test_mb_skcipher_data *data, int enc,
				int blen, int secs, u32 num_mb)
{
	unsigned long start, end;
	int bcount;
1178 1179 1180 1181 1182 1183
	int ret = 0;
	int *rc;

	rc = kcalloc(num_mb, sizeof(*rc), GFP_KERNEL);
	if (!rc)
		return -ENOMEM;
1184 1185 1186

	for (start = jiffies, end = start + secs * HZ, bcount = 0;
	     time_before(jiffies, end); bcount++) {
1187
		ret = do_mult_acipher_op(data, enc, num_mb, rc);
1188
		if (ret)
1189
			goto out;
1190 1191 1192 1193
	}

	pr_cont("%d operations in %d seconds (%ld bytes)\n",
		bcount * num_mb, secs, (long)bcount * blen * num_mb);
1194 1195 1196 1197

out:
	kfree(rc);
	return ret;
1198 1199 1200 1201 1202 1203 1204 1205
}

static int test_mb_acipher_cycles(struct test_mb_skcipher_data *data, int enc,
			       int blen, u32 num_mb)
{
	unsigned long cycles = 0;
	int ret = 0;
	int i;
1206 1207 1208 1209 1210
	int *rc;

	rc = kcalloc(num_mb, sizeof(*rc), GFP_KERNEL);
	if (!rc)
		return -ENOMEM;
1211 1212 1213

	/* Warm-up run. */
	for (i = 0; i < 4; i++) {
1214
		ret = do_mult_acipher_op(data, enc, num_mb, rc);
1215 1216 1217 1218 1219 1220 1221 1222 1223
		if (ret)
			goto out;
	}

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

		start = get_cycles();
1224
		ret = do_mult_acipher_op(data, enc, num_mb, rc);
1225 1226 1227 1228 1229 1230 1231 1232
		end = get_cycles();

		if (ret)
			goto out;

		cycles += end - start;
	}

1233 1234
	pr_cont("1 operation in %lu cycles (%d bytes)\n",
		(cycles + 4) / (8 * num_mb), blen);
1235

1236 1237
out:
	kfree(rc);
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
	return ret;
}

static void test_mb_skcipher_speed(const char *algo, int enc, int secs,
				   struct cipher_speed_template *template,
				   unsigned int tcount, u8 *keysize, u32 num_mb)
{
	struct test_mb_skcipher_data *data;
	struct crypto_skcipher *tfm;
	unsigned int i, j, iv_len;
	const char *key;
	const char *e;
	u32 *b_size;
	char iv[128];
	int ret;

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

	data = kcalloc(num_mb, sizeof(*data), GFP_KERNEL);
	if (!data)
		return;

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

	for (i = 0; i < num_mb; ++i)
		if (testmgr_alloc_buf(data[i].xbuf)) {
			while (i--)
				testmgr_free_buf(data[i].xbuf);
			goto out_free_tfm;
		}


	for (i = 0; i < num_mb; ++i)
		if (testmgr_alloc_buf(data[i].xbuf)) {
			while (i--)
				testmgr_free_buf(data[i].xbuf);
			goto out_free_tfm;
		}


	for (i = 0; i < num_mb; ++i) {
		data[i].req = skcipher_request_alloc(tfm, GFP_KERNEL);
		if (!data[i].req) {
			pr_err("alg: skcipher: Failed to allocate request for %s\n",
			       algo);
			while (i--)
				skcipher_request_free(data[i].req);
			goto out_free_xbuf;
		}
	}

	for (i = 0; i < num_mb; ++i) {
		skcipher_request_set_callback(data[i].req,
					      CRYPTO_TFM_REQ_MAY_BACKLOG,
					      crypto_req_done, &data[i].wait);
		crypto_init_wait(&data[i].wait);
	}

	pr_info("\ntesting speed of multibuffer %s (%s) %s\n", algo,
		get_driver_name(crypto_skcipher, tfm), e);

	i = 0;
	do {
		b_size = block_sizes;
		do {
			if (*b_size > XBUFSIZE * PAGE_SIZE) {
1312
				pr_err("template (%u) too big for buffer (%lu)\n",
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
				       *b_size, XBUFSIZE * PAGE_SIZE);
				goto out;
			}

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

			/* Set up tfm global state, i.e. the key */

			memset(tvmem[0], 0xff, PAGE_SIZE);
			key = tvmem[0];
			for (j = 0; j < tcount; j++) {
				if (template[j].klen == *keysize) {
					key = template[j].key;
					break;
				}
			}

			crypto_skcipher_clear_flags(tfm, ~0);

			ret = crypto_skcipher_setkey(tfm, key, *keysize);
			if (ret) {
				pr_err("setkey() failed flags=%x\n",
				       crypto_skcipher_get_flags(tfm));
				goto out;
			}

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

			/* Now setup per request stuff, i.e. buffers */

			for (j = 0; j < num_mb; ++j) {
				struct test_mb_skcipher_data *cur = &data[j];
				unsigned int k = *b_size;
				unsigned int pages = DIV_ROUND_UP(k, PAGE_SIZE);
				unsigned int p = 0;

				sg_init_table(cur->sg, pages);

				while (k > PAGE_SIZE) {
					sg_set_buf(cur->sg + p, cur->xbuf[p],
						   PAGE_SIZE);
					memset(cur->xbuf[p], 0xff, PAGE_SIZE);
					p++;
					k -= PAGE_SIZE;
				}

				sg_set_buf(cur->sg + p, cur->xbuf[p], k);
				memset(cur->xbuf[p], 0xff, k);

				skcipher_request_set_crypt(cur->req, cur->sg,
							   cur->sg, *b_size,
							   iv);
			}

			if (secs)
				ret = test_mb_acipher_jiffies(data, enc,
							      *b_size, secs,
							      num_mb);
			else
				ret = test_mb_acipher_cycles(data, enc,
							     *b_size, num_mb);

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

out:
	for (i = 0; i < num_mb; ++i)
		skcipher_request_free(data[i].req);
out_free_xbuf:
	for (i = 0; i < num_mb; ++i)
		testmgr_free_buf(data[i].xbuf);
out_free_tfm:
	crypto_free_skcipher(tfm);
out_free_data:
	kfree(data);
}

1401
static inline int do_one_acipher_op(struct skcipher_request *req, int ret)
1402
{
1403
	struct crypto_wait *wait = req->base.data;
1404

1405
	return crypto_wait_req(ret, wait);
1406 1407
}

1408
static int test_acipher_jiffies(struct skcipher_request *req, int enc,
1409
				int blen, int secs)
1410 1411 1412 1413 1414
{
	unsigned long start, end;
	int bcount;
	int ret;

1415
	for (start = jiffies, end = start + secs * HZ, bcount = 0;
1416 1417 1418
	     time_before(jiffies, end); bcount++) {
		if (enc)
			ret = do_one_acipher_op(req,
1419
						crypto_skcipher_encrypt(req));
1420 1421
		else
			ret = do_one_acipher_op(req,
1422
						crypto_skcipher_decrypt(req));
1423 1424 1425 1426 1427 1428

		if (ret)
			return ret;
	}

	pr_cont("%d operations in %d seconds (%ld bytes)\n",
1429
		bcount, secs, (long)bcount * blen);
1430 1431 1432
	return 0;
}

1433
static int test_acipher_cycles(struct skcipher_request *req, int enc,
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
			       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,
1444
						crypto_skcipher_encrypt(req));
1445 1446
		else
			ret = do_one_acipher_op(req,
1447
						crypto_skcipher_decrypt(req));
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459

		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,
1460
						crypto_skcipher_encrypt(req));
1461 1462
		else
			ret = do_one_acipher_op(req,
1463
						crypto_skcipher_decrypt(req));
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
		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;
}

1480 1481 1482
static void test_skcipher_speed(const char *algo, int enc, unsigned int secs,
				struct cipher_speed_template *template,
				unsigned int tcount, u8 *keysize, bool async)
1483
{
1484
	unsigned int ret, i, j, k, iv_len;
1485
	struct crypto_wait wait;
1486 1487
	const char *key;
	char iv[128];
1488 1489
	struct skcipher_request *req;
	struct crypto_skcipher *tfm;
1490 1491 1492 1493 1494 1495 1496 1497
	const char *e;
	u32 *b_size;

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

1498
	crypto_init_wait(&wait);
1499

1500
	tfm = crypto_alloc_skcipher(algo, 0, async ? 0 : CRYPTO_ALG_ASYNC);
1501 1502 1503 1504 1505 1506 1507

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

1508
	pr_info("\ntesting speed of async %s (%s) %s\n", algo,
1509
			get_driver_name(crypto_skcipher, tfm), e);
1510

1511
	req = skcipher_request_alloc(tfm, GFP_KERNEL);
1512 1513 1514 1515 1516 1517
	if (!req) {
		pr_err("tcrypt: skcipher: Failed to allocate request for %s\n",
		       algo);
		goto out;
	}

1518
	skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1519
				      crypto_req_done, &wait);
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

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

1549
			crypto_skcipher_clear_flags(tfm, ~0);
1550

1551
			ret = crypto_skcipher_setkey(tfm, key, *keysize);
1552 1553
			if (ret) {
				pr_err("setkey() failed flags=%x\n",
1554
					crypto_skcipher_get_flags(tfm));
1555 1556 1557
				goto out_free_req;
			}

1558
			k = *keysize + *b_size;
1559 1560
			sg_init_table(sg, DIV_ROUND_UP(k, PAGE_SIZE));

1561 1562
			if (k > PAGE_SIZE) {
				sg_set_buf(sg, tvmem[0] + *keysize,
1563
				   PAGE_SIZE - *keysize);
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
				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);
1576 1577
			}

1578
			iv_len = crypto_skcipher_ivsize(tfm);
1579 1580 1581
			if (iv_len)
				memset(&iv, 0xff, iv_len);

1582
			skcipher_request_set_crypt(req, sg, sg, *b_size, iv);
1583

1584
			if (secs)
1585
				ret = test_acipher_jiffies(req, enc,
1586
							   *b_size, secs);
1587 1588 1589 1590 1591 1592
			else
				ret = test_acipher_cycles(req, enc,
							  *b_size);

			if (ret) {
				pr_err("%s() failed flags=%x\n", e,
1593
				       crypto_skcipher_get_flags(tfm));
1594 1595 1596 1597 1598 1599 1600 1601 1602
				break;
			}
			b_size++;
			i++;
		} while (*b_size);
		keysize++;
	} while (*keysize);

out_free_req:
1603
	skcipher_request_free(req);
1604
out:
1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621
	crypto_free_skcipher(tfm);
}

static void test_acipher_speed(const char *algo, int enc, unsigned int secs,
			       struct cipher_speed_template *template,
			       unsigned int tcount, u8 *keysize)
{
	return test_skcipher_speed(algo, enc, secs, template, tcount, keysize,
				   true);
}

static void test_cipher_speed(const char *algo, int enc, unsigned int secs,
			      struct cipher_speed_template *template,
			      unsigned int tcount, u8 *keysize)
{
	return test_skcipher_speed(algo, enc, secs, template, tcount, keysize,
				   false);
1622 1623
}

1624
static void test_available(void)
Linus Torvalds's avatar
Linus Torvalds committed
1625 1626
{
	char **name = check;
1627

Linus Torvalds's avatar
Linus Torvalds committed
1628 1629
	while (*name) {
		printk("alg %s ", *name);
1630
		printk(crypto_has_alg(*name, 0, 0) ?
1631
		       "found\n" : "not found\n");
Linus Torvalds's avatar
Linus Torvalds committed
1632
		name++;
1633
	}
Linus Torvalds's avatar
Linus Torvalds committed
1634 1635
}

1636 1637
static inline int tcrypt_test(const char *alg)
{
1638 1639
	int ret;

1640 1641
	pr_debug("testing %s\n", alg);

1642 1643 1644 1645 1646
	ret = alg_test(alg, alg, 0, 0);
	/* non-fips algs return -EINVAL in fips mode */
	if (fips_enabled && ret == -EINVAL)
		ret = 0;
	return ret;
1647 1648
}

1649
static int do_test(const char *alg, u32 type, u32 mask, int m, u32 num_mb)
1650 1651
{
	int i;
1652
	int ret = 0;
1653 1654

	switch (m) {
Linus Torvalds's avatar
Linus Torvalds committed
1655
	case 0:
1656 1657 1658 1659 1660 1661 1662
		if (alg) {
			if (!crypto_has_alg(alg, type,
					    mask ?: CRYPTO_ALG_TYPE_MASK))
				ret = -ENOENT;
			break;
		}

1663
		for (i = 1; i < 200; i++)
1664
			ret += do_test(NULL, 0, 0, i, num_mb);
Linus Torvalds's avatar
Linus Torvalds committed
1665 1666 1667
		break;

	case 1:
1668
		ret += tcrypt_test("md5");
Linus Torvalds's avatar
Linus Torvalds committed
1669 1670 1671
		break;

	case 2: