tcrypt.c 18.5 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 56
	"camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
	"lzo", "cts", 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 400
	char output[1024];
	int i;
401
	int ret;
402 403 404

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

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

407 408 409
	if (IS_ERR(tfm)) {
		printk("failed to load transform for %s: %ld\n", algo,
		       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("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 430 431
		if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
			printk("template (%u) too big for tvmem (%lu)\n",
			       speed[i].blen, TVMEMSIZE * PAGE_SIZE);
432 433 434 435 436 437 438
			goto out;
		}

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

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

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

out:
452
	crypto_free_hash(tfm);
453 454
}

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

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

467 468 469 470 471 472 473 474 475 476
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 已提交
477
	case 0:
478 479
		for (i = 1; i < 200; i++)
			do_test(i);
L
Linus Torvalds 已提交
480 481 482
		break;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	case 28:

601
		tcrypt_test("tgr160");
L
Linus Torvalds 已提交
602 603 604
		break;

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

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

612
	case 31:
613
		tcrypt_test("pcbc(fcrypt)");
614 615
		break;

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

624
	case 34:
625
		tcrypt_test("salsa20");
626 627
		break;

628
	case 35:
629
		tcrypt_test("gcm(aes)");
630 631
		break;

632
	case 36:
633
		tcrypt_test("lzo");
634 635
		break;

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

640
	case 38:
641
		tcrypt_test("cts(cbc(aes))");
642 643
		break;

644
        case 39:
645
		tcrypt_test("rmd128");
646 647 648
		break;

        case 40:
649
		tcrypt_test("rmd160");
650 651
		break;

652
	case 41:
653
		tcrypt_test("rmd256");
654 655 656
		break;

	case 42:
657 658 659 660 661
		tcrypt_test("rmd320");
		break;

	case 43:
		tcrypt_test("ecb(seed)");
662 663
		break;

L
Linus Torvalds 已提交
664
	case 100:
665
		tcrypt_test("hmac(md5)");
L
Linus Torvalds 已提交
666
		break;
667

L
Linus Torvalds 已提交
668
	case 101:
669
		tcrypt_test("hmac(sha1)");
L
Linus Torvalds 已提交
670
		break;
671

L
Linus Torvalds 已提交
672
	case 102:
673
		tcrypt_test("hmac(sha256)");
L
Linus Torvalds 已提交
674 675
		break;

676
	case 103:
677
		tcrypt_test("hmac(sha384)");
678 679 680
		break;

	case 104:
681
		tcrypt_test("hmac(sha512)");
682
		break;
683

684
	case 105:
685
		tcrypt_test("hmac(sha224)");
686
		break;
L
Linus Torvalds 已提交
687

688
	case 106:
689
		tcrypt_test("xcbc(aes)");
690 691
		break;

692
	case 107:
693
		tcrypt_test("hmac(rmd128)");
694 695 696
		break;

	case 108:
697
		tcrypt_test("hmac(rmd160)");
698 699
		break;

H
Harald Welte 已提交
700
	case 200:
701
		test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
702
				speed_template_16_24_32);
703
		test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
704
				speed_template_16_24_32);
705
		test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
706
				speed_template_16_24_32);
707
		test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
708
				speed_template_16_24_32);
R
Rik Snel 已提交
709
		test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
710
				speed_template_32_40_48);
R
Rik Snel 已提交
711
		test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
712
				speed_template_32_40_48);
713
		test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
714
				speed_template_32_48_64);
715
		test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
716
				speed_template_32_48_64);
H
Harald Welte 已提交
717 718 719
		break;

	case 201:
720
		test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
721
				des3_speed_template, DES3_SPEED_VECTORS,
722
				speed_template_24);
723
		test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
724
				des3_speed_template, DES3_SPEED_VECTORS,
725
				speed_template_24);
726
		test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
727
				des3_speed_template, DES3_SPEED_VECTORS,
728
				speed_template_24);
729
		test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
730
				des3_speed_template, DES3_SPEED_VECTORS,
731
				speed_template_24);
H
Harald Welte 已提交
732 733 734
		break;

	case 202:
735
		test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
736
				speed_template_16_24_32);
737
		test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
738
				speed_template_16_24_32);
739
		test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
740
				speed_template_16_24_32);
741
		test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
742
				speed_template_16_24_32);
H
Harald Welte 已提交
743 744 745
		break;

	case 203:
746
		test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
747
				  speed_template_8_32);
748
		test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
749
				  speed_template_8_32);
750
		test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
751
				  speed_template_8_32);
752
		test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
753
				  speed_template_8_32);
H
Harald Welte 已提交
754 755 756
		break;

	case 204:
757
		test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
758
				  speed_template_8);
759
		test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
760
				  speed_template_8);
761
		test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
762
				  speed_template_8);
763
		test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
764
				  speed_template_8);
H
Harald Welte 已提交
765 766
		break;

767 768
	case 205:
		test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
769
				speed_template_16_24_32);
770
		test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
771
				speed_template_16_24_32);
772
		test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
773
				speed_template_16_24_32);
774
		test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
775
				speed_template_16_24_32);
776 777
		break;

778 779
	case 206:
		test_cipher_speed("salsa20", ENCRYPT, sec, NULL, 0,
780
				  speed_template_16_32);
781 782
		break;

783 784 785 786
	case 300:
		/* fall through */

	case 301:
787
		test_hash_speed("md4", sec, generic_hash_speed_template);
788 789 790
		if (mode > 300 && mode < 400) break;

	case 302:
791
		test_hash_speed("md5", sec, generic_hash_speed_template);
792 793 794
		if (mode > 300 && mode < 400) break;

	case 303:
795
		test_hash_speed("sha1", sec, generic_hash_speed_template);
796 797 798
		if (mode > 300 && mode < 400) break;

	case 304:
799
		test_hash_speed("sha256", sec, generic_hash_speed_template);
800 801 802
		if (mode > 300 && mode < 400) break;

	case 305:
803
		test_hash_speed("sha384", sec, generic_hash_speed_template);
804 805 806
		if (mode > 300 && mode < 400) break;

	case 306:
807
		test_hash_speed("sha512", sec, generic_hash_speed_template);
808 809 810
		if (mode > 300 && mode < 400) break;

	case 307:
811
		test_hash_speed("wp256", sec, generic_hash_speed_template);
812 813 814
		if (mode > 300 && mode < 400) break;

	case 308:
815
		test_hash_speed("wp384", sec, generic_hash_speed_template);
816 817 818
		if (mode > 300 && mode < 400) break;

	case 309:
819
		test_hash_speed("wp512", sec, generic_hash_speed_template);
820 821 822
		if (mode > 300 && mode < 400) break;

	case 310:
823
		test_hash_speed("tgr128", sec, generic_hash_speed_template);
824 825 826
		if (mode > 300 && mode < 400) break;

	case 311:
827
		test_hash_speed("tgr160", sec, generic_hash_speed_template);
828 829 830
		if (mode > 300 && mode < 400) break;

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

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

838 839 840 841 842 843 844 845
	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;

846 847 848 849 850 851 852 853
	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;

854 855 856
	case 399:
		break;

L
Linus Torvalds 已提交
857 858 859 860 861 862
	case 1000:
		test_available();
		break;
	}
}

863
static int __init tcrypt_mod_init(void)
L
Linus Torvalds 已提交
864
{
865
	int err = -ENOMEM;
866
	int i;
867

868 869 870 871 872
	for (i = 0; i < TVMEMSIZE; i++) {
		tvmem[i] = (void *)__get_free_page(GFP_KERNEL);
		if (!tvmem[i])
			goto err_free_tv;
	}
L
Linus Torvalds 已提交
873

874
	do_test(mode);
875 876 877 878 879 880 881

	/* 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
	 */
882 883
	err = -EAGAIN;

884 885 886
err_free_tv:
	for (i = 0; i < TVMEMSIZE && tvmem[i]; i++)
		free_page((unsigned long)tvmem[i]);
887 888

	return err;
L
Linus Torvalds 已提交
889 890 891 892 893 894
}

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

897 898
module_init(tcrypt_mod_init);
module_exit(tcrypt_mod_fini);
L
Linus Torvalds 已提交
899 900

module_param(mode, int, 0);
H
Harald Welte 已提交
901
module_param(sec, uint, 0);
902 903
MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
		      "(defaults to zero which uses CPU cycles instead)");
L
Linus Torvalds 已提交
904 905 906 907

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