tcrypt.c 28.3 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 105
{
	unsigned long cycles = 0;
	int ret = 0;
	int i;

	local_bh_disable();
	local_irq_disable();

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

		if (ret)
			goto out;
	}

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

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

		if (ret)
			goto out;

		cycles += end - start;
	}

out:
	local_irq_enable();
	local_bh_enable();

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

	return ret;
}

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

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

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

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

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

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

173 174
	i = 0;
	do {
H
Harald Welte 已提交
175

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

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

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

190
			memset(tvmem[0], 0xff, PAGE_SIZE);
191 192

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

201 202 203 204 205 206
			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 已提交
207

208 209 210 211 212 213 214 215
			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);
			}

216 217 218 219 220
			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 已提交
221

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

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

out:
240
	crypto_free_blkcipher(tfm);
H
Harald Welte 已提交
241 242
}

243 244
static int test_hash_jiffies_digest(struct hash_desc *desc,
				    struct scatterlist *sg, int blen,
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
				    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;
}

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

	if (plen == blen)
272
		return test_hash_jiffies_digest(desc, sg, blen, out, sec);
273

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

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

293 294 295
	return 0;
}

296 297
static int test_hash_cycles_digest(struct hash_desc *desc,
				   struct scatterlist *sg, int blen, char *out)
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 335 336 337 338
{
	unsigned long cycles = 0;
	int i;
	int ret;

	local_bh_disable();
	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();
	local_bh_enable();

	if (ret)
		return ret;

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

	return 0;
339 340
}

341 342
static int test_hash_cycles(struct hash_desc *desc, struct scatterlist *sg,
			    int blen, int plen, char *out)
343 344 345
{
	unsigned long cycles = 0;
	int i, pcount;
346 347 348
	int ret;

	if (plen == blen)
349
		return test_hash_cycles_digest(desc, sg, blen, out);
350

351 352 353 354 355
	local_bh_disable();
	local_irq_disable();

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

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

		start = get_cycles();

375 376 377
		ret = crypto_hash_init(desc);
		if (ret)
			goto out;
378
		for (pcount = 0; pcount < blen; pcount += plen) {
379 380 381
			ret = crypto_hash_update(desc, sg, plen);
			if (ret)
				goto out;
382
		}
383 384 385
		ret = crypto_hash_final(desc, out);
		if (ret)
			goto out;
386 387 388 389 390 391

		end = get_cycles();

		cycles += end - start;
	}

392
out:
393 394 395
	local_irq_enable();
	local_bh_enable();

396 397 398
	if (ret)
		return ret;

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

402
	return 0;
403 404
}

405 406 407 408 409 410 411 412 413 414 415
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);
	}
}

416 417
static void test_hash_speed(const char *algo, unsigned int sec,
			    struct hash_speed *speed)
418
{
419
	struct scatterlist sg[TVMEMSIZE];
420 421
	struct crypto_hash *tfm;
	struct hash_desc desc;
422
	static char output[1024];
423
	int i;
424
	int ret;
425

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

428
	tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
429

430
	if (IS_ERR(tfm)) {
431
		printk(KERN_ERR "failed to load transform for %s: %ld\n", algo,
432
		       PTR_ERR(tfm));
433 434 435
		return;
	}

436 437 438 439
	desc.tfm = tfm;
	desc.flags = 0;

	if (crypto_hash_digestsize(tfm) > sizeof(output)) {
440
		printk(KERN_ERR "digestsize(%u) > outputbuffer(%zu)\n",
441
		       crypto_hash_digestsize(tfm), sizeof(output));
442 443 444
		goto out;
	}

445
	test_hash_sg_init(sg);
446
	for (i = 0; speed[i].blen != 0; i++) {
447
		if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
448 449
			printk(KERN_ERR
			       "template (%u) too big for tvmem (%lu)\n",
450
			       speed[i].blen, TVMEMSIZE * PAGE_SIZE);
451 452 453
			goto out;
		}

454 455 456
		if (speed[i].klen)
			crypto_hash_setkey(tfm, tvmem[0], speed[i].klen);

457 458
		printk(KERN_INFO "test%3u "
		       "(%5u byte blocks,%5u bytes per update,%4u updates): ",
459 460 461
		       i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);

		if (sec)
462
			ret = test_hash_jiffies(&desc, sg, speed[i].blen,
463
						speed[i].plen, output, sec);
464
		else
465
			ret = test_hash_cycles(&desc, sg, speed[i].blen,
466 467 468
					       speed[i].plen, output);

		if (ret) {
469
			printk(KERN_ERR "hashing failed ret=%d\n", ret);
470 471
			break;
		}
472 473 474
	}

out:
475
	crypto_free_hash(tfm);
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 716 717 718 719 720 721
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);
}

722
static void test_available(void)
L
Linus Torvalds 已提交
723 724
{
	char **name = check;
725

L
Linus Torvalds 已提交
726 727
	while (*name) {
		printk("alg %s ", *name);
728
		printk(crypto_has_alg(*name, 0, 0) ?
729
		       "found\n" : "not found\n");
L
Linus Torvalds 已提交
730
		name++;
731
	}
L
Linus Torvalds 已提交
732 733
}

734 735
static inline int tcrypt_test(const char *alg)
{
736 737 738 739 740 741 742
	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;
743 744
}

745
static int do_test(int m)
746 747
{
	int i;
748
	int ret = 0;
749 750

	switch (m) {
L
Linus Torvalds 已提交
751
	case 0:
752
		for (i = 1; i < 200; i++)
753
			ret += do_test(i);
L
Linus Torvalds 已提交
754 755 756
		break;

	case 1:
757
		ret += tcrypt_test("md5");
L
Linus Torvalds 已提交
758 759 760
		break;

	case 2:
761
		ret += tcrypt_test("sha1");
L
Linus Torvalds 已提交
762 763 764
		break;

	case 3:
765 766
		ret += tcrypt_test("ecb(des)");
		ret += tcrypt_test("cbc(des)");
L
Linus Torvalds 已提交
767 768 769
		break;

	case 4:
770 771
		ret += tcrypt_test("ecb(des3_ede)");
		ret += tcrypt_test("cbc(des3_ede)");
L
Linus Torvalds 已提交
772 773 774
		break;

	case 5:
775
		ret += tcrypt_test("md4");
L
Linus Torvalds 已提交
776
		break;
777

L
Linus Torvalds 已提交
778
	case 6:
779
		ret += tcrypt_test("sha256");
L
Linus Torvalds 已提交
780
		break;
781

L
Linus Torvalds 已提交
782
	case 7:
783 784
		ret += tcrypt_test("ecb(blowfish)");
		ret += tcrypt_test("cbc(blowfish)");
785
		ret += tcrypt_test("ctr(blowfish)");
L
Linus Torvalds 已提交
786 787 788
		break;

	case 8:
789 790
		ret += tcrypt_test("ecb(twofish)");
		ret += tcrypt_test("cbc(twofish)");
791
		ret += tcrypt_test("ctr(twofish)");
L
Linus Torvalds 已提交
792
		break;
793

L
Linus Torvalds 已提交
794
	case 9:
795
		ret += tcrypt_test("ecb(serpent)");
L
Linus Torvalds 已提交
796 797 798
		break;

	case 10:
799 800 801 802 803 804
		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 已提交
805 806 807
		break;

	case 11:
808
		ret += tcrypt_test("sha384");
L
Linus Torvalds 已提交
809
		break;
810

L
Linus Torvalds 已提交
811
	case 12:
812
		ret += tcrypt_test("sha512");
L
Linus Torvalds 已提交
813 814 815
		break;

	case 13:
816
		ret += tcrypt_test("deflate");
L
Linus Torvalds 已提交
817 818 819
		break;

	case 14:
820
		ret += tcrypt_test("ecb(cast5)");
L
Linus Torvalds 已提交
821 822 823
		break;

	case 15:
824
		ret += tcrypt_test("ecb(cast6)");
L
Linus Torvalds 已提交
825 826 827
		break;

	case 16:
828
		ret += tcrypt_test("ecb(arc4)");
L
Linus Torvalds 已提交
829 830 831
		break;

	case 17:
832
		ret += tcrypt_test("michael_mic");
L
Linus Torvalds 已提交
833 834 835
		break;

	case 18:
836
		ret += tcrypt_test("crc32c");
L
Linus Torvalds 已提交
837 838 839
		break;

	case 19:
840
		ret += tcrypt_test("ecb(tea)");
L
Linus Torvalds 已提交
841 842 843
		break;

	case 20:
844
		ret += tcrypt_test("ecb(xtea)");
L
Linus Torvalds 已提交
845 846 847
		break;

	case 21:
848
		ret += tcrypt_test("ecb(khazad)");
L
Linus Torvalds 已提交
849 850 851
		break;

	case 22:
852
		ret += tcrypt_test("wp512");
L
Linus Torvalds 已提交
853 854 855
		break;

	case 23:
856
		ret += tcrypt_test("wp384");
L
Linus Torvalds 已提交
857 858 859
		break;

	case 24:
860
		ret += tcrypt_test("wp256");
L
Linus Torvalds 已提交
861 862 863
		break;

	case 25:
864
		ret += tcrypt_test("ecb(tnepres)");
L
Linus Torvalds 已提交
865 866 867
		break;

	case 26:
868 869
		ret += tcrypt_test("ecb(anubis)");
		ret += tcrypt_test("cbc(anubis)");
L
Linus Torvalds 已提交
870 871 872
		break;

	case 27:
873
		ret += tcrypt_test("tgr192");
L
Linus Torvalds 已提交
874 875 876 877
		break;

	case 28:

878
		ret += tcrypt_test("tgr160");
L
Linus Torvalds 已提交
879 880 881
		break;

	case 29:
882
		ret += tcrypt_test("tgr128");
L
Linus Torvalds 已提交
883
		break;
884

A
Aaron Grothe 已提交
885
	case 30:
886
		ret += tcrypt_test("ecb(xeta)");
A
Aaron Grothe 已提交
887
		break;
L
Linus Torvalds 已提交
888

889
	case 31:
890
		ret += tcrypt_test("pcbc(fcrypt)");
891 892
		break;

893
	case 32:
894 895
		ret += tcrypt_test("ecb(camellia)");
		ret += tcrypt_test("cbc(camellia)");
896
		break;
897
	case 33:
898
		ret += tcrypt_test("sha224");
899
		break;
900

901
	case 34:
902
		ret += tcrypt_test("salsa20");
903 904
		break;

905
	case 35:
906
		ret += tcrypt_test("gcm(aes)");
907 908
		break;

909
	case 36:
910
		ret += tcrypt_test("lzo");
911 912
		break;

J
Joy Latten 已提交
913
	case 37:
914
		ret += tcrypt_test("ccm(aes)");
J
Joy Latten 已提交
915 916
		break;

917
	case 38:
918
		ret += tcrypt_test("cts(cbc(aes))");
919 920
		break;

921
        case 39:
922
		ret += tcrypt_test("rmd128");
923 924 925
		break;

        case 40:
926
		ret += tcrypt_test("rmd160");
927 928
		break;

929
	case 41:
930
		ret += tcrypt_test("rmd256");
931 932 933
		break;

	case 42:
934
		ret += tcrypt_test("rmd320");
935 936 937
		break;

	case 43:
938
		ret += tcrypt_test("ecb(seed)");
939 940
		break;

941
	case 44:
942
		ret += tcrypt_test("zlib");
943 944
		break;

945
	case 45:
946
		ret += tcrypt_test("rfc4309(ccm(aes))");
947 948
		break;

L
Linus Torvalds 已提交
949
	case 100:
950
		ret += tcrypt_test("hmac(md5)");
L
Linus Torvalds 已提交
951
		break;
952

L
Linus Torvalds 已提交
953
	case 101:
954
		ret += tcrypt_test("hmac(sha1)");
L
Linus Torvalds 已提交
955
		break;
956

L
Linus Torvalds 已提交
957
	case 102:
958
		ret += tcrypt_test("hmac(sha256)");
L
Linus Torvalds 已提交
959 960
		break;

961
	case 103:
962
		ret += tcrypt_test("hmac(sha384)");
963 964 965
		break;

	case 104:
966
		ret += tcrypt_test("hmac(sha512)");
967
		break;
968

969
	case 105:
970
		ret += tcrypt_test("hmac(sha224)");
971
		break;
L
Linus Torvalds 已提交
972

973
	case 106:
974
		ret += tcrypt_test("xcbc(aes)");
975 976
		break;

977
	case 107:
978
		ret += tcrypt_test("hmac(rmd128)");
979 980 981
		break;

	case 108:
982
		ret += tcrypt_test("hmac(rmd160)");
983 984
		break;

985 986 987 988
	case 109:
		ret += tcrypt_test("vmac(aes)");
		break;

989
	case 150:
990
		ret += tcrypt_test("ansi_cprng");
991 992
		break;

993 994 995 996
	case 151:
		ret += tcrypt_test("rfc4106(gcm(aes))");
		break;

H
Harald Welte 已提交
997
	case 200:
998
		test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
999
				speed_template_16_24_32);
1000
		test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
1001
				speed_template_16_24_32);
1002
		test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
1003
				speed_template_16_24_32);
1004
		test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
1005
				speed_template_16_24_32);
R
Rik Snel 已提交
1006
		test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
1007
				speed_template_32_40_48);
R
Rik Snel 已提交
1008
		test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
1009
				speed_template_32_40_48);
1010
		test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
1011
				speed_template_32_48_64);
1012
		test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
1013
				speed_template_32_48_64);
1014 1015 1016 1017
		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 已提交
1018 1019 1020
		break;

	case 201:
1021
		test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
1022
				des3_speed_template, DES3_SPEED_VECTORS,
1023
				speed_template_24);
1024
		test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
1025
				des3_speed_template, DES3_SPEED_VECTORS,
1026
				speed_template_24);
1027
		test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
1028
				des3_speed_template, DES3_SPEED_VECTORS,
1029
				speed_template_24);
1030
		test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
1031
				des3_speed_template, DES3_SPEED_VECTORS,
1032
				speed_template_24);
H
Harald Welte 已提交
1033 1034 1035
		break;

	case 202:
1036
		test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
1037
				speed_template_16_24_32);
1038
		test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
1039
				speed_template_16_24_32);
1040
		test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
1041
				speed_template_16_24_32);
1042
		test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
1043
				speed_template_16_24_32);
1044 1045 1046 1047
		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);
H
Harald Welte 已提交
1048 1049 1050
		break;

	case 203:
1051
		test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
1052
				  speed_template_8_32);
1053
		test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
1054
				  speed_template_8_32);
1055
		test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
1056
				  speed_template_8_32);
1057
		test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
1058
				  speed_template_8_32);
1059 1060 1061 1062
		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 已提交
1063 1064 1065
		break;

	case 204:
1066
		test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
1067
				  speed_template_8);
1068
		test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
1069
				  speed_template_8);
1070
		test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
1071
				  speed_template_8);
1072
		test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
1073
				  speed_template_8);
H
Harald Welte 已提交
1074 1075
		break;

1076 1077
	case 205:
		test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
1078
				speed_template_16_24_32);
1079
		test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
1080
				speed_template_16_24_32);
1081
		test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
1082
				speed_template_16_24_32);
1083
		test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
1084
				speed_template_16_24_32);
1085 1086
		break;

1087 1088
	case 206:
		test_cipher_speed("salsa20", ENCRYPT, sec, NULL, 0,
1089
				  speed_template_16_32);
1090 1091
		break;

1092 1093 1094 1095
	case 300:
		/* fall through */

	case 301:
1096
		test_hash_speed("md4", sec, generic_hash_speed_template);
1097 1098 1099
		if (mode > 300 && mode < 400) break;

	case 302:
1100
		test_hash_speed("md5", sec, generic_hash_speed_template);
1101 1102 1103
		if (mode > 300 && mode < 400) break;

	case 303:
1104
		test_hash_speed("sha1", sec, generic_hash_speed_template);
1105 1106 1107
		if (mode > 300 && mode < 400) break;

	case 304:
1108
		test_hash_speed("sha256", sec, generic_hash_speed_template);
1109 1110 1111
		if (mode > 300 && mode < 400) break;

	case 305:
1112
		test_hash_speed("sha384", sec, generic_hash_speed_template);
1113 1114 1115
		if (mode > 300 && mode < 400) break;

	case 306:
1116
		test_hash_speed("sha512", sec, generic_hash_speed_template);
1117 1118 1119
		if (mode > 300 && mode < 400) break;

	case 307:
1120
		test_hash_speed("wp256", sec, generic_hash_speed_template);
1121 1122 1123
		if (mode > 300 && mode < 400) break;

	case 308:
1124
		test_hash_speed("wp384", sec, generic_hash_speed_template);
1125 1126 1127
		if (mode > 300 && mode < 400) break;

	case 309:
1128
		test_hash_speed("wp512", sec, generic_hash_speed_template);
1129 1130 1131
		if (mode > 300 && mode < 400) break;

	case 310:
1132
		test_hash_speed("tgr128", sec, generic_hash_speed_template);
1133 1134 1135
		if (mode > 300 && mode < 400) break;

	case 311:
1136
		test_hash_speed("tgr160", sec, generic_hash_speed_template);
1137 1138 1139
		if (mode > 300 && mode < 400) break;

	case 312:
1140
		test_hash_speed("tgr192", sec, generic_hash_speed_template);
1141 1142
		if (mode > 300 && mode < 400) break;

1143 1144 1145 1146
	case 313:
		test_hash_speed("sha224", sec, generic_hash_speed_template);
		if (mode > 300 && mode < 400) break;

1147 1148 1149 1150 1151 1152 1153 1154
	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;

1155 1156 1157 1158 1159 1160 1161 1162
	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;

1163 1164 1165 1166
	case 318:
		test_hash_speed("ghash-generic", sec, hash_speed_template_16);
		if (mode > 300 && mode < 400) break;

1167 1168 1169
	case 399:
		break;

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 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
	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;

L
Linus Torvalds 已提交
1244 1245 1246 1247
	case 1000:
		test_available();
		break;
	}
1248 1249

	return ret;
L
Linus Torvalds 已提交
1250 1251
}

H
Herbert Xu 已提交
1252
static int do_alg_test(const char *alg, u32 type, u32 mask)
1253
{
H
Herbert Xu 已提交
1254 1255
	return crypto_has_alg(alg, type, mask ?: CRYPTO_ALG_TYPE_MASK) ?
	       0 : -ENOENT;
1256 1257
}

1258
static int __init tcrypt_mod_init(void)
L
Linus Torvalds 已提交
1259
{
1260
	int err = -ENOMEM;
1261
	int i;
1262

1263 1264 1265 1266 1267
	for (i = 0; i < TVMEMSIZE; i++) {
		tvmem[i] = (void *)__get_free_page(GFP_KERNEL);
		if (!tvmem[i])
			goto err_free_tv;
	}
L
Linus Torvalds 已提交
1268

1269
	if (alg)
H
Herbert Xu 已提交
1270
		err = do_alg_test(alg, type, mask);
1271 1272 1273
	else
		err = do_test(mode);

1274 1275 1276 1277
	if (err) {
		printk(KERN_ERR "tcrypt: one or more tests failed!\n");
		goto err_free_tv;
	}
1278

1279 1280 1281 1282
	/* 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.
1283 1284 1285
	 * => we don't need it in the memory, do we?
	 *                                        -- mludvig
	 */
1286 1287
	if (!fips_enabled)
		err = -EAGAIN;
1288

1289 1290 1291
err_free_tv:
	for (i = 0; i < TVMEMSIZE && tvmem[i]; i++)
		free_page((unsigned long)tvmem[i]);
1292 1293

	return err;
L
Linus Torvalds 已提交
1294 1295 1296 1297 1298 1299
}

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

1302 1303
module_init(tcrypt_mod_init);
module_exit(tcrypt_mod_fini);
L
Linus Torvalds 已提交
1304

1305 1306
module_param(alg, charp, 0);
module_param(type, uint, 0);
H
Herbert Xu 已提交
1307
module_param(mask, uint, 0);
L
Linus Torvalds 已提交
1308
module_param(mode, int, 0);
H
Harald Welte 已提交
1309
module_param(sec, uint, 0);
1310 1311
MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
		      "(defaults to zero which uses CPU cycles instead)");
L
Linus Torvalds 已提交
1312 1313 1314 1315

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