tcrypt.c 18.6 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 529
		tcrypt_test("ecb(aes)");
		tcrypt_test("cbc(aes)");
		tcrypt_test("lrw(aes)");
		tcrypt_test("xts(aes)");
		tcrypt_test("rfc3686(ctr(aes))");
L
Linus Torvalds 已提交
530 531 532
		break;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	case 28:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

L
Linus Torvalds 已提交
670
	case 100:
671
		tcrypt_test("hmac(md5)");
L
Linus Torvalds 已提交
672
		break;
673

L
Linus Torvalds 已提交
674
	case 101:
675
		tcrypt_test("hmac(sha1)");
L
Linus Torvalds 已提交
676
		break;
677

L
Linus Torvalds 已提交
678
	case 102:
679
		tcrypt_test("hmac(sha256)");
L
Linus Torvalds 已提交
680 681
		break;

682
	case 103:
683
		tcrypt_test("hmac(sha384)");
684 685 686
		break;

	case 104:
687
		tcrypt_test("hmac(sha512)");
688
		break;
689

690
	case 105:
691
		tcrypt_test("hmac(sha224)");
692
		break;
L
Linus Torvalds 已提交
693

694
	case 106:
695
		tcrypt_test("xcbc(aes)");
696 697
		break;

698
	case 107:
699
		tcrypt_test("hmac(rmd128)");
700 701 702
		break;

	case 108:
703
		tcrypt_test("hmac(rmd160)");
704 705
		break;

H
Harald Welte 已提交
706
	case 200:
707
		test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
708
				speed_template_16_24_32);
709
		test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
710
				speed_template_16_24_32);
711
		test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
712
				speed_template_16_24_32);
713
		test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
714
				speed_template_16_24_32);
R
Rik Snel 已提交
715
		test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
716
				speed_template_32_40_48);
R
Rik Snel 已提交
717
		test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
718
				speed_template_32_40_48);
719
		test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
720
				speed_template_32_48_64);
721
		test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
722
				speed_template_32_48_64);
H
Harald Welte 已提交
723 724 725
		break;

	case 201:
726
		test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
727
				des3_speed_template, DES3_SPEED_VECTORS,
728
				speed_template_24);
729
		test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
730
				des3_speed_template, DES3_SPEED_VECTORS,
731
				speed_template_24);
732
		test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
733
				des3_speed_template, DES3_SPEED_VECTORS,
734
				speed_template_24);
735
		test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
736
				des3_speed_template, DES3_SPEED_VECTORS,
737
				speed_template_24);
H
Harald Welte 已提交
738 739 740
		break;

	case 202:
741
		test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
742
				speed_template_16_24_32);
743
		test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
744
				speed_template_16_24_32);
745
		test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
746
				speed_template_16_24_32);
747
		test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
748
				speed_template_16_24_32);
H
Harald Welte 已提交
749 750 751
		break;

	case 203:
752
		test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
753
				  speed_template_8_32);
754
		test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
755
				  speed_template_8_32);
756
		test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
757
				  speed_template_8_32);
758
		test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
759
				  speed_template_8_32);
H
Harald Welte 已提交
760 761 762
		break;

	case 204:
763
		test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
764
				  speed_template_8);
765
		test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
766
				  speed_template_8);
767
		test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
768
				  speed_template_8);
769
		test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
770
				  speed_template_8);
H
Harald Welte 已提交
771 772
		break;

773 774
	case 205:
		test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
775
				speed_template_16_24_32);
776
		test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
777
				speed_template_16_24_32);
778
		test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
779
				speed_template_16_24_32);
780
		test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
781
				speed_template_16_24_32);
782 783
		break;

784 785
	case 206:
		test_cipher_speed("salsa20", ENCRYPT, sec, NULL, 0,
786
				  speed_template_16_32);
787 788
		break;

789 790 791 792
	case 300:
		/* fall through */

	case 301:
793
		test_hash_speed("md4", sec, generic_hash_speed_template);
794 795 796
		if (mode > 300 && mode < 400) break;

	case 302:
797
		test_hash_speed("md5", sec, generic_hash_speed_template);
798 799 800
		if (mode > 300 && mode < 400) break;

	case 303:
801
		test_hash_speed("sha1", sec, generic_hash_speed_template);
802 803 804
		if (mode > 300 && mode < 400) break;

	case 304:
805
		test_hash_speed("sha256", sec, generic_hash_speed_template);
806 807 808
		if (mode > 300 && mode < 400) break;

	case 305:
809
		test_hash_speed("sha384", sec, generic_hash_speed_template);
810 811 812
		if (mode > 300 && mode < 400) break;

	case 306:
813
		test_hash_speed("sha512", sec, generic_hash_speed_template);
814 815 816
		if (mode > 300 && mode < 400) break;

	case 307:
817
		test_hash_speed("wp256", sec, generic_hash_speed_template);
818 819 820
		if (mode > 300 && mode < 400) break;

	case 308:
821
		test_hash_speed("wp384", sec, generic_hash_speed_template);
822 823 824
		if (mode > 300 && mode < 400) break;

	case 309:
825
		test_hash_speed("wp512", sec, generic_hash_speed_template);
826 827 828
		if (mode > 300 && mode < 400) break;

	case 310:
829
		test_hash_speed("tgr128", sec, generic_hash_speed_template);
830 831 832
		if (mode > 300 && mode < 400) break;

	case 311:
833
		test_hash_speed("tgr160", sec, generic_hash_speed_template);
834 835 836
		if (mode > 300 && mode < 400) break;

	case 312:
837
		test_hash_speed("tgr192", sec, generic_hash_speed_template);
838 839
		if (mode > 300 && mode < 400) break;

840 841 842 843
	case 313:
		test_hash_speed("sha224", sec, generic_hash_speed_template);
		if (mode > 300 && mode < 400) break;

844 845 846 847 848 849 850 851
	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;

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

860 861 862
	case 399:
		break;

L
Linus Torvalds 已提交
863 864 865 866 867 868
	case 1000:
		test_available();
		break;
	}
}

869
static int __init tcrypt_mod_init(void)
L
Linus Torvalds 已提交
870
{
871
	int err = -ENOMEM;
872
	int i;
873

874 875 876 877 878
	for (i = 0; i < TVMEMSIZE; i++) {
		tvmem[i] = (void *)__get_free_page(GFP_KERNEL);
		if (!tvmem[i])
			goto err_free_tv;
	}
L
Linus Torvalds 已提交
879

880
	do_test(mode);
881 882 883 884 885 886 887

	/* 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
	 */
888 889
	err = -EAGAIN;

890 891 892
err_free_tv:
	for (i = 0; i < TVMEMSIZE && tvmem[i]; i++)
		free_page((unsigned long)tvmem[i]);
893 894

	return err;
L
Linus Torvalds 已提交
895 896 897 898 899 900
}

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

903 904
module_init(tcrypt_mod_init);
module_exit(tcrypt_mod_fini);
L
Linus Torvalds 已提交
905 906

module_param(mode, int, 0);
H
Harald Welte 已提交
907
module_param(sec, uint, 0);
908 909
MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
		      "(defaults to zero which uses CPU cycles instead)");
L
Linus Torvalds 已提交
910 911 912 913

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