tcrypt.c 18.7 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
 *
 * 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
13
 * Software Foundation; either version 2 of the License, or (at your option)
L
Linus Torvalds 已提交
14 15 16 17
 * any later version.
 *
 */

18
#include <crypto/hash.h>
19
#include <linux/err.h>
L
Linus Torvalds 已提交
20 21 22
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
23
#include <linux/scatterlist.h>
L
Linus Torvalds 已提交
24 25
#include <linux/string.h>
#include <linux/moduleparam.h>
H
Harald Welte 已提交
26
#include <linux/jiffies.h>
27 28
#include <linux/timex.h>
#include <linux/interrupt.h>
L
Linus Torvalds 已提交
29 30 31
#include "tcrypt.h"

/*
32
 * Need slab memory for testing (size in number of pages).
L
Linus Torvalds 已提交
33
 */
34
#define TVMEMSIZE	4
L
Linus Torvalds 已提交
35 36

/*
37
* Used by test_cipher_speed()
L
Linus Torvalds 已提交
38 39 40 41
*/
#define ENCRYPT 1
#define DECRYPT 0

H
Harald Welte 已提交
42 43 44
/*
 * Used by test_cipher_speed()
 */
45
static unsigned int sec;
H
Harald Welte 已提交
46

L
Linus Torvalds 已提交
47
static int mode;
48
static char *tvmem[TVMEMSIZE];
L
Linus Torvalds 已提交
49 50

static char *check[] = {
51 52 53
	"des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256",
	"blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
	"cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
54
	"khazad", "wp512", "wp384", "wp256", "tnepres", "xeta",  "fcrypt",
55
	"camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
56
	"lzo", "cts", "zlib", NULL
L
Linus Torvalds 已提交
57 58
};

59 60
static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc,
			       struct scatterlist *sg, int blen, int sec)
61 62 63 64 65 66 67 68
{
	unsigned long start, end;
	int bcount;
	int ret;

	for (start = jiffies, end = start + sec * HZ, bcount = 0;
	     time_before(jiffies, end); bcount++) {
		if (enc)
69
			ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
70
		else
71
			ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
72 73 74 75 76 77 78 79 80 81

		if (ret)
			return ret;
	}

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

82 83
static int test_cipher_cycles(struct blkcipher_desc *desc, int enc,
			      struct scatterlist *sg, int blen)
84 85 86 87 88 89 90 91 92 93 94
{
	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)
95
			ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
96
		else
97
			ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
98 99 100 101 102 103 104 105 106 107 108

		if (ret)
			goto out;
	}

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

		start = get_cycles();
		if (enc)
109
			ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
110
		else
111
			ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
		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;
}

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

133
static void test_cipher_speed(const char *algo, int enc, unsigned int sec,
134
			      struct cipher_speed_template *template,
135
			      unsigned int tcount, u8 *keysize)
H
Harald Welte 已提交
136
{
137
	unsigned int ret, i, j, iv_len;
138
	const char *key, iv[128];
139 140 141
	struct crypto_blkcipher *tfm;
	struct blkcipher_desc desc;
	const char *e;
142
	u32 *b_size;
H
Harald Welte 已提交
143 144 145 146 147 148

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

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

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

153 154 155
	if (IS_ERR(tfm)) {
		printk("failed to load transform for %s: %ld\n", algo,
		       PTR_ERR(tfm));
H
Harald Welte 已提交
156 157
		return;
	}
158 159
	desc.tfm = tfm;
	desc.flags = 0;
H
Harald Welte 已提交
160

161 162
	i = 0;
	do {
H
Harald Welte 已提交
163

164 165
		b_size = block_sizes;
		do {
166
			struct scatterlist sg[TVMEMSIZE];
H
Harald Welte 已提交
167

168 169 170 171
			if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
				printk("template (%u) too big for "
				       "tvmem (%lu)\n", *keysize + *b_size,
				       TVMEMSIZE * PAGE_SIZE);
172 173
				goto out;
			}
H
Harald Welte 已提交
174

175 176 177
			printk("test %u (%d bit key, %d byte blocks): ", i,
					*keysize * 8, *b_size);

178
			memset(tvmem[0], 0xff, PAGE_SIZE);
179 180

			/* set key, plain text and IV */
181
			key = tvmem[0];
182 183 184 185 186
			for (j = 0; j < tcount; j++) {
				if (template[j].klen == *keysize) {
					key = template[j].key;
					break;
				}
187
			}
H
Harald Welte 已提交
188

189 190 191 192 193 194
			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 已提交
195

196 197 198 199 200 201 202 203
			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);
			}

204 205 206 207 208
			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 已提交
209

210
			if (sec)
211 212
				ret = test_cipher_jiffies(&desc, enc, sg,
							  *b_size, sec);
213
			else
214 215
				ret = test_cipher_cycles(&desc, enc, sg,
							 *b_size);
H
Harald Welte 已提交
216

217 218 219 220 221 222 223 224 225
			if (ret) {
				printk("%s() failed flags=%x\n", e, desc.flags);
				break;
			}
			b_size++;
			i++;
		} while (*b_size);
		keysize++;
	} while (*keysize);
H
Harald Welte 已提交
226 227

out:
228
	crypto_free_blkcipher(tfm);
H
Harald Welte 已提交
229 230
}

231 232
static int test_hash_jiffies_digest(struct hash_desc *desc,
				    struct scatterlist *sg, int blen,
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
				    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;
}

252 253
static int test_hash_jiffies(struct hash_desc *desc, struct scatterlist *sg,
			     int blen, int plen, char *out, int sec)
254 255 256
{
	unsigned long start, end;
	int bcount, pcount;
257 258 259
	int ret;

	if (plen == blen)
260
		return test_hash_jiffies_digest(desc, sg, blen, out, sec);
261

262 263
	for (start = jiffies, end = start + sec * HZ, bcount = 0;
	     time_before(jiffies, end); bcount++) {
264 265 266
		ret = crypto_hash_init(desc);
		if (ret)
			return ret;
267
		for (pcount = 0; pcount < blen; pcount += plen) {
268 269 270
			ret = crypto_hash_update(desc, sg, plen);
			if (ret)
				return ret;
271 272
		}
		/* we assume there is enough space in 'out' for the result */
273 274 275
		ret = crypto_hash_final(desc, out);
		if (ret)
			return ret;
276 277 278 279 280
	}

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

281 282 283
	return 0;
}

284 285
static int test_hash_cycles_digest(struct hash_desc *desc,
				   struct scatterlist *sg, int blen, char *out)
286 287 288 289 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 316 317 318 319 320 321 322 323 324 325 326
{
	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;
327 328
}

329 330
static int test_hash_cycles(struct hash_desc *desc, struct scatterlist *sg,
			    int blen, int plen, char *out)
331 332 333
{
	unsigned long cycles = 0;
	int i, pcount;
334 335 336
	int ret;

	if (plen == blen)
337
		return test_hash_cycles_digest(desc, sg, blen, out);
338

339 340 341 342 343
	local_bh_disable();
	local_irq_disable();

	/* Warm-up run. */
	for (i = 0; i < 4; i++) {
344 345 346
		ret = crypto_hash_init(desc);
		if (ret)
			goto out;
347
		for (pcount = 0; pcount < blen; pcount += plen) {
348 349 350
			ret = crypto_hash_update(desc, sg, plen);
			if (ret)
				goto out;
351
		}
352
		ret = crypto_hash_final(desc, out);
353 354
		if (ret)
			goto out;
355 356 357 358 359 360 361 362
	}

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

		start = get_cycles();

363 364 365
		ret = crypto_hash_init(desc);
		if (ret)
			goto out;
366
		for (pcount = 0; pcount < blen; pcount += plen) {
367 368 369
			ret = crypto_hash_update(desc, sg, plen);
			if (ret)
				goto out;
370
		}
371 372 373
		ret = crypto_hash_final(desc, out);
		if (ret)
			goto out;
374 375 376 377 378 379

		end = get_cycles();

		cycles += end - start;
	}

380
out:
381 382 383
	local_irq_enable();
	local_bh_enable();

384 385 386
	if (ret)
		return ret;

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

390
	return 0;
391 392
}

393 394
static void test_hash_speed(const char *algo, unsigned int sec,
			    struct hash_speed *speed)
395
{
396
	struct scatterlist sg[TVMEMSIZE];
397 398
	struct crypto_hash *tfm;
	struct hash_desc desc;
399
	static char output[1024];
400
	int i;
401
	int ret;
402

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

405
	tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
406

407
	if (IS_ERR(tfm)) {
408
		printk(KERN_ERR "failed to load transform for %s: %ld\n", algo,
409
		       PTR_ERR(tfm));
410 411 412
		return;
	}

413 414 415 416
	desc.tfm = tfm;
	desc.flags = 0;

	if (crypto_hash_digestsize(tfm) > sizeof(output)) {
417
		printk(KERN_ERR "digestsize(%u) > outputbuffer(%zu)\n",
418
		       crypto_hash_digestsize(tfm), sizeof(output));
419 420 421
		goto out;
	}

422 423 424 425 426 427
	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);
	}

428
	for (i = 0; speed[i].blen != 0; i++) {
429
		if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
430 431
			printk(KERN_ERR
			       "template (%u) too big for tvmem (%lu)\n",
432
			       speed[i].blen, TVMEMSIZE * PAGE_SIZE);
433 434 435
			goto out;
		}

436 437
		printk(KERN_INFO "test%3u "
		       "(%5u byte blocks,%5u bytes per update,%4u updates): ",
438 439 440
		       i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);

		if (sec)
441
			ret = test_hash_jiffies(&desc, sg, speed[i].blen,
442
						speed[i].plen, output, sec);
443
		else
444
			ret = test_hash_cycles(&desc, sg, speed[i].blen,
445 446 447
					       speed[i].plen, output);

		if (ret) {
448
			printk(KERN_ERR "hashing failed ret=%d\n", ret);
449 450
			break;
		}
451 452 453
	}

out:
454
	crypto_free_hash(tfm);
455 456
}

457
static void test_available(void)
L
Linus Torvalds 已提交
458 459
{
	char **name = check;
460

L
Linus Torvalds 已提交
461 462
	while (*name) {
		printk("alg %s ", *name);
463
		printk(crypto_has_alg(*name, 0, 0) ?
464
		       "found\n" : "not found\n");
L
Linus Torvalds 已提交
465
		name++;
466
	}
L
Linus Torvalds 已提交
467 468
}

469 470 471 472 473 474 475 476 477 478
static inline int tcrypt_test(const char *alg)
{
	return alg_test(alg, alg, 0, 0);
}

static void do_test(int m)
{
	int i;

	switch (m) {
L
Linus Torvalds 已提交
479
	case 0:
480 481
		for (i = 1; i < 200; i++)
			do_test(i);
L
Linus Torvalds 已提交
482 483 484
		break;

	case 1:
485
		tcrypt_test("md5");
L
Linus Torvalds 已提交
486 487 488
		break;

	case 2:
489
		tcrypt_test("sha1");
L
Linus Torvalds 已提交
490 491 492
		break;

	case 3:
493 494
		tcrypt_test("ecb(des)");
		tcrypt_test("cbc(des)");
L
Linus Torvalds 已提交
495 496 497
		break;

	case 4:
498 499
		tcrypt_test("ecb(des3_ede)");
		tcrypt_test("cbc(des3_ede)");
L
Linus Torvalds 已提交
500 501 502
		break;

	case 5:
503
		tcrypt_test("md4");
L
Linus Torvalds 已提交
504
		break;
505

L
Linus Torvalds 已提交
506
	case 6:
507
		tcrypt_test("sha256");
L
Linus Torvalds 已提交
508
		break;
509

L
Linus Torvalds 已提交
510
	case 7:
511 512
		tcrypt_test("ecb(blowfish)");
		tcrypt_test("cbc(blowfish)");
L
Linus Torvalds 已提交
513 514 515
		break;

	case 8:
516 517
		tcrypt_test("ecb(twofish)");
		tcrypt_test("cbc(twofish)");
L
Linus Torvalds 已提交
518
		break;
519

L
Linus Torvalds 已提交
520
	case 9:
521
		tcrypt_test("ecb(serpent)");
L
Linus Torvalds 已提交
522 523 524
		break;

	case 10:
525 526 527 528
		tcrypt_test("ecb(aes)");
		tcrypt_test("cbc(aes)");
		tcrypt_test("lrw(aes)");
		tcrypt_test("xts(aes)");
529
		tcrypt_test("ctr(aes)");
530
		tcrypt_test("rfc3686(ctr(aes))");
L
Linus Torvalds 已提交
531 532 533
		break;

	case 11:
534
		tcrypt_test("sha384");
L
Linus Torvalds 已提交
535
		break;
536

L
Linus Torvalds 已提交
537
	case 12:
538
		tcrypt_test("sha512");
L
Linus Torvalds 已提交
539 540 541
		break;

	case 13:
542
		tcrypt_test("deflate");
L
Linus Torvalds 已提交
543 544 545
		break;

	case 14:
546
		tcrypt_test("ecb(cast5)");
L
Linus Torvalds 已提交
547 548 549
		break;

	case 15:
550
		tcrypt_test("ecb(cast6)");
L
Linus Torvalds 已提交
551 552 553
		break;

	case 16:
554
		tcrypt_test("ecb(arc4)");
L
Linus Torvalds 已提交
555 556 557
		break;

	case 17:
558
		tcrypt_test("michael_mic");
L
Linus Torvalds 已提交
559 560 561
		break;

	case 18:
562
		tcrypt_test("crc32c");
L
Linus Torvalds 已提交
563 564 565
		break;

	case 19:
566
		tcrypt_test("ecb(tea)");
L
Linus Torvalds 已提交
567 568 569
		break;

	case 20:
570
		tcrypt_test("ecb(xtea)");
L
Linus Torvalds 已提交
571 572 573
		break;

	case 21:
574
		tcrypt_test("ecb(khazad)");
L
Linus Torvalds 已提交
575 576 577
		break;

	case 22:
578
		tcrypt_test("wp512");
L
Linus Torvalds 已提交
579 580 581
		break;

	case 23:
582
		tcrypt_test("wp384");
L
Linus Torvalds 已提交
583 584 585
		break;

	case 24:
586
		tcrypt_test("wp256");
L
Linus Torvalds 已提交
587 588 589
		break;

	case 25:
590
		tcrypt_test("ecb(tnepres)");
L
Linus Torvalds 已提交
591 592 593
		break;

	case 26:
594 595
		tcrypt_test("ecb(anubis)");
		tcrypt_test("cbc(anubis)");
L
Linus Torvalds 已提交
596 597 598
		break;

	case 27:
599
		tcrypt_test("tgr192");
L
Linus Torvalds 已提交
600 601 602 603
		break;

	case 28:

604
		tcrypt_test("tgr160");
L
Linus Torvalds 已提交
605 606 607
		break;

	case 29:
608
		tcrypt_test("tgr128");
L
Linus Torvalds 已提交
609
		break;
610

A
Aaron Grothe 已提交
611
	case 30:
612
		tcrypt_test("ecb(xeta)");
A
Aaron Grothe 已提交
613
		break;
L
Linus Torvalds 已提交
614

615
	case 31:
616
		tcrypt_test("pcbc(fcrypt)");
617 618
		break;

619
	case 32:
620 621
		tcrypt_test("ecb(camellia)");
		tcrypt_test("cbc(camellia)");
622
		break;
623
	case 33:
624
		tcrypt_test("sha224");
625
		break;
626

627
	case 34:
628
		tcrypt_test("salsa20");
629 630
		break;

631
	case 35:
632
		tcrypt_test("gcm(aes)");
633 634
		break;

635
	case 36:
636
		tcrypt_test("lzo");
637 638
		break;

J
Joy Latten 已提交
639
	case 37:
640
		tcrypt_test("ccm(aes)");
J
Joy Latten 已提交
641 642
		break;

643
	case 38:
644
		tcrypt_test("cts(cbc(aes))");
645 646
		break;

647
        case 39:
648
		tcrypt_test("rmd128");
649 650 651
		break;

        case 40:
652
		tcrypt_test("rmd160");
653 654
		break;

655
	case 41:
656
		tcrypt_test("rmd256");
657 658 659
		break;

	case 42:
660 661 662 663 664
		tcrypt_test("rmd320");
		break;

	case 43:
		tcrypt_test("ecb(seed)");
665 666
		break;

667 668 669 670
	case 44:
		tcrypt_test("zlib");
		break;

671 672 673 674
	case 45:
		tcrypt_test("rfc4309(ccm(aes))");
		break;

L
Linus Torvalds 已提交
675
	case 100:
676
		tcrypt_test("hmac(md5)");
L
Linus Torvalds 已提交
677
		break;
678

L
Linus Torvalds 已提交
679
	case 101:
680
		tcrypt_test("hmac(sha1)");
L
Linus Torvalds 已提交
681
		break;
682

L
Linus Torvalds 已提交
683
	case 102:
684
		tcrypt_test("hmac(sha256)");
L
Linus Torvalds 已提交
685 686
		break;

687
	case 103:
688
		tcrypt_test("hmac(sha384)");
689 690 691
		break;

	case 104:
692
		tcrypt_test("hmac(sha512)");
693
		break;
694

695
	case 105:
696
		tcrypt_test("hmac(sha224)");
697
		break;
L
Linus Torvalds 已提交
698

699
	case 106:
700
		tcrypt_test("xcbc(aes)");
701 702
		break;

703
	case 107:
704
		tcrypt_test("hmac(rmd128)");
705 706 707
		break;

	case 108:
708
		tcrypt_test("hmac(rmd160)");
709 710
		break;

711 712 713 714
	case 150:
		tcrypt_test("ansi_cprng");
		break;

H
Harald Welte 已提交
715
	case 200:
716
		test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
717
				speed_template_16_24_32);
718
		test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
719
				speed_template_16_24_32);
720
		test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
721
				speed_template_16_24_32);
722
		test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
723
				speed_template_16_24_32);
R
Rik Snel 已提交
724
		test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
725
				speed_template_32_40_48);
R
Rik Snel 已提交
726
		test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
727
				speed_template_32_40_48);
728
		test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
729
				speed_template_32_48_64);
730
		test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
731
				speed_template_32_48_64);
H
Harald Welte 已提交
732 733 734
		break;

	case 201:
735
		test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
736
				des3_speed_template, DES3_SPEED_VECTORS,
737
				speed_template_24);
738
		test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
739
				des3_speed_template, DES3_SPEED_VECTORS,
740
				speed_template_24);
741
		test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
742
				des3_speed_template, DES3_SPEED_VECTORS,
743
				speed_template_24);
744
		test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
745
				des3_speed_template, DES3_SPEED_VECTORS,
746
				speed_template_24);
H
Harald Welte 已提交
747 748 749
		break;

	case 202:
750
		test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
751
				speed_template_16_24_32);
752
		test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
753
				speed_template_16_24_32);
754
		test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
755
				speed_template_16_24_32);
756
		test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
757
				speed_template_16_24_32);
H
Harald Welte 已提交
758 759 760
		break;

	case 203:
761
		test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
762
				  speed_template_8_32);
763
		test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
764
				  speed_template_8_32);
765
		test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
766
				  speed_template_8_32);
767
		test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
768
				  speed_template_8_32);
H
Harald Welte 已提交
769 770 771
		break;

	case 204:
772
		test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
773
				  speed_template_8);
774
		test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
775
				  speed_template_8);
776
		test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
777
				  speed_template_8);
778
		test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
779
				  speed_template_8);
H
Harald Welte 已提交
780 781
		break;

782 783
	case 205:
		test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
784
				speed_template_16_24_32);
785
		test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
786
				speed_template_16_24_32);
787
		test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
788
				speed_template_16_24_32);
789
		test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
790
				speed_template_16_24_32);
791 792
		break;

793 794
	case 206:
		test_cipher_speed("salsa20", ENCRYPT, sec, NULL, 0,
795
				  speed_template_16_32);
796 797
		break;

798 799 800 801
	case 300:
		/* fall through */

	case 301:
802
		test_hash_speed("md4", sec, generic_hash_speed_template);
803 804 805
		if (mode > 300 && mode < 400) break;

	case 302:
806
		test_hash_speed("md5", sec, generic_hash_speed_template);
807 808 809
		if (mode > 300 && mode < 400) break;

	case 303:
810
		test_hash_speed("sha1", sec, generic_hash_speed_template);
811 812 813
		if (mode > 300 && mode < 400) break;

	case 304:
814
		test_hash_speed("sha256", sec, generic_hash_speed_template);
815 816 817
		if (mode > 300 && mode < 400) break;

	case 305:
818
		test_hash_speed("sha384", sec, generic_hash_speed_template);
819 820 821
		if (mode > 300 && mode < 400) break;

	case 306:
822
		test_hash_speed("sha512", sec, generic_hash_speed_template);
823 824 825
		if (mode > 300 && mode < 400) break;

	case 307:
826
		test_hash_speed("wp256", sec, generic_hash_speed_template);
827 828 829
		if (mode > 300 && mode < 400) break;

	case 308:
830
		test_hash_speed("wp384", sec, generic_hash_speed_template);
831 832 833
		if (mode > 300 && mode < 400) break;

	case 309:
834
		test_hash_speed("wp512", sec, generic_hash_speed_template);
835 836 837
		if (mode > 300 && mode < 400) break;

	case 310:
838
		test_hash_speed("tgr128", sec, generic_hash_speed_template);
839 840 841
		if (mode > 300 && mode < 400) break;

	case 311:
842
		test_hash_speed("tgr160", sec, generic_hash_speed_template);
843 844 845
		if (mode > 300 && mode < 400) break;

	case 312:
846
		test_hash_speed("tgr192", sec, generic_hash_speed_template);
847 848
		if (mode > 300 && mode < 400) break;

849 850 851 852
	case 313:
		test_hash_speed("sha224", sec, generic_hash_speed_template);
		if (mode > 300 && mode < 400) break;

853 854 855 856 857 858 859 860
	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;

861 862 863 864 865 866 867 868
	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;

869 870 871
	case 399:
		break;

L
Linus Torvalds 已提交
872 873 874 875 876 877
	case 1000:
		test_available();
		break;
	}
}

878
static int __init tcrypt_mod_init(void)
L
Linus Torvalds 已提交
879
{
880
	int err = -ENOMEM;
881
	int i;
882

883 884 885 886 887
	for (i = 0; i < TVMEMSIZE; i++) {
		tvmem[i] = (void *)__get_free_page(GFP_KERNEL);
		if (!tvmem[i])
			goto err_free_tv;
	}
L
Linus Torvalds 已提交
888

889
	do_test(mode);
890 891 892 893 894 895 896

	/* We intentionaly return -EAGAIN to prevent keeping
	 * the module. It does all its work from init()
	 * and doesn't offer any runtime functionality 
	 * => we don't need it in the memory, do we?
	 *                                        -- mludvig
	 */
897 898
	err = -EAGAIN;

899 900 901
err_free_tv:
	for (i = 0; i < TVMEMSIZE && tvmem[i]; i++)
		free_page((unsigned long)tvmem[i]);
902 903

	return err;
L
Linus Torvalds 已提交
904 905 906 907 908 909
}

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

912 913
module_init(tcrypt_mod_init);
module_exit(tcrypt_mod_fini);
L
Linus Torvalds 已提交
914 915

module_param(mode, int, 0);
H
Harald Welte 已提交
916
module_param(sec, uint, 0);
917 918
MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
		      "(defaults to zero which uses CPU cycles instead)");
L
Linus Torvalds 已提交
919 920 921 922

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