tcrypt.c 44.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
 * any later version.
 *
M
Mikko Herranen 已提交
16
 * 2007-11-13 Added GCM tests
17
 * 2007-11-13 Added AEAD support
18
 * 2007-11-06 Added SHA-224 and SHA-224-HMAC tests
19
 * 2006-12-07 Added SHA384 HMAC and SHA512 HMAC tests
H
Harald Welte 已提交
20 21 22
 * 2004-08-09 Added cipher speed tests (Reyk Floeter <reyk@vantronix.net>)
 * 2003-09-14 Rewritten by Kartikey Mahendra Bhatt
 *
L
Linus Torvalds 已提交
23 24
 */

25
#include <linux/err.h>
L
Linus Torvalds 已提交
26 27 28 29
#include <linux/init.h>
#include <linux/module.h>
#include <linux/mm.h>
#include <linux/slab.h>
30
#include <linux/scatterlist.h>
L
Linus Torvalds 已提交
31 32 33 34
#include <linux/string.h>
#include <linux/crypto.h>
#include <linux/highmem.h>
#include <linux/moduleparam.h>
H
Harald Welte 已提交
35
#include <linux/jiffies.h>
36 37
#include <linux/timex.h>
#include <linux/interrupt.h>
L
Linus Torvalds 已提交
38 39 40 41 42
#include "tcrypt.h"

/*
 * Need to kmalloc() memory for testing kmap().
 */
H
Harald Welte 已提交
43
#define TVMEMSIZE	16384
L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#define XBUFSIZE	32768

/*
 * Indexes into the xbuf to simulate cross-page access.
 */
#define IDX1		37
#define IDX2		32400
#define IDX3		1
#define IDX4		8193
#define IDX5		22222
#define IDX6		17101
#define IDX7		27333
#define IDX8		3000

/*
* Used by test_cipher()
*/
#define ENCRYPT 1
#define DECRYPT 0

64 65 66 67 68
struct tcrypt_result {
	struct completion completion;
	int err;
};

L
Linus Torvalds 已提交
69 70
static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };

H
Harald Welte 已提交
71 72 73
/*
 * Used by test_cipher_speed()
 */
74
static unsigned int sec;
H
Harald Welte 已提交
75

L
Linus Torvalds 已提交
76 77
static int mode;
static char *xbuf;
78
static char *axbuf;
L
Linus Torvalds 已提交
79 80 81
static char *tvmem;

static char *check[] = {
82 83 84
	"des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256",
	"blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
	"cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
85
	"arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
86
	"khazad", "wp512", "wp384", "wp256", "tnepres", "xeta",  "fcrypt",
87
	"camellia", "seed", "salsa20", "lzo", NULL
L
Linus Torvalds 已提交
88 89
};

90
static void hexdump(unsigned char *buf, unsigned int len)
L
Linus Torvalds 已提交
91
{
92 93 94
	print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
			16, 1,
			buf, len, false);
L
Linus Torvalds 已提交
95 96
}

97 98 99 100 101 102 103 104 105 106 107
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);
}

108 109
static void test_hash(char *algo, struct hash_testvec *template,
		      unsigned int tcount)
L
Linus Torvalds 已提交
110
{
111 112 113
	unsigned int i, j, k, temp;
	struct scatterlist sg[8];
	char result[64];
114 115
	struct crypto_hash *tfm;
	struct hash_desc desc;
116 117
	struct hash_testvec *hash_tv;
	unsigned int tsize;
118
	int ret;
119 120 121 122

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

	tsize = sizeof(struct hash_testvec);
L
Linus Torvalds 已提交
123
	tsize *= tcount;
124

L
Linus Torvalds 已提交
125 126 127 128 129 130
	if (tsize > TVMEMSIZE) {
		printk("template (%u) too big for tvmem (%u)\n", tsize, TVMEMSIZE);
		return;
	}

	memcpy(tvmem, template, tsize);
131
	hash_tv = (void *)tvmem;
132 133 134 135 136

	tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
	if (IS_ERR(tfm)) {
		printk("failed to load transform for %s: %ld\n", algo,
		       PTR_ERR(tfm));
L
Linus Torvalds 已提交
137 138 139
		return;
	}

140 141 142
	desc.tfm = tfm;
	desc.flags = 0;

L
Linus Torvalds 已提交
143
	for (i = 0; i < tcount; i++) {
144 145
		printk("test %u:\n", i + 1);
		memset(result, 0, 64);
L
Linus Torvalds 已提交
146

147
		sg_init_one(&sg[0], hash_tv[i].plaintext, hash_tv[i].psize);
L
Linus Torvalds 已提交
148

149 150 151 152 153 154 155 156 157 158 159 160 161 162
		if (hash_tv[i].ksize) {
			ret = crypto_hash_setkey(tfm, hash_tv[i].key,
						 hash_tv[i].ksize);
			if (ret) {
				printk("setkey() failed ret=%d\n", ret);
				goto out;
			}
		}

		ret = crypto_hash_digest(&desc, sg, hash_tv[i].psize, result);
		if (ret) {
			printk("digest () failed ret=%d\n", ret);
			goto out;
		}
L
Linus Torvalds 已提交
163

164
		hexdump(result, crypto_hash_digestsize(tfm));
L
Linus Torvalds 已提交
165
		printk("%s\n",
166
		       memcmp(result, hash_tv[i].digest,
167
			      crypto_hash_digestsize(tfm)) ?
168
		       "fail" : "pass");
L
Linus Torvalds 已提交
169 170
	}

171
	printk("testing %s across pages\n", algo);
L
Linus Torvalds 已提交
172 173

	/* setup the dummy buffer first */
174
	memset(xbuf, 0, XBUFSIZE);
175
	memset(axbuf, 0, XBUFSIZE);
L
Linus Torvalds 已提交
176 177 178 179 180

	j = 0;
	for (i = 0; i < tcount; i++) {
		if (hash_tv[i].np) {
			j++;
181 182
			printk("test %u:\n", j);
			memset(result, 0, 64);
L
Linus Torvalds 已提交
183 184

			temp = 0;
185
			sg_init_table(sg, hash_tv[i].np);
L
Linus Torvalds 已提交
186
			for (k = 0; k < hash_tv[i].np; k++) {
187 188 189
				memcpy(&xbuf[IDX[k]],
				       hash_tv[i].plaintext + temp,
				       hash_tv[i].tap[k]);
L
Linus Torvalds 已提交
190
				temp += hash_tv[i].tap[k];
191 192
				sg_set_buf(&sg[k], &xbuf[IDX[k]],
					    hash_tv[i].tap[k]);
L
Linus Torvalds 已提交
193 194
			}

195 196 197
			if (hash_tv[i].ksize) {
				ret = crypto_hash_setkey(tfm, hash_tv[i].key,
							 hash_tv[i].ksize);
198

199 200 201 202
				if (ret) {
					printk("setkey() failed ret=%d\n", ret);
					goto out;
				}
L
Linus Torvalds 已提交
203 204
			}

205 206 207 208 209 210
			ret = crypto_hash_digest(&desc, sg, hash_tv[i].psize,
						 result);
			if (ret) {
				printk("digest () failed ret=%d\n", ret);
				goto out;
			}
211

212
			hexdump(result, crypto_hash_digestsize(tfm));
L
Linus Torvalds 已提交
213
			printk("%s\n",
214 215
			       memcmp(result, hash_tv[i].digest,
				      crypto_hash_digestsize(tfm)) ?
216
			       "fail" : "pass");
L
Linus Torvalds 已提交
217 218
		}
	}
219

L
Linus Torvalds 已提交
220
out:
221
	crypto_free_hash(tfm);
L
Linus Torvalds 已提交
222 223
}

224 225 226 227 228 229 230 231 232 233 234 235 236 237
static void test_aead(char *algo, int enc, struct aead_testvec *template,
		      unsigned int tcount)
{
	unsigned int ret, i, j, k, temp;
	unsigned int tsize;
	char *q;
	struct crypto_aead *tfm;
	char *key;
	struct aead_testvec *aead_tv;
	struct aead_request *req;
	struct scatterlist sg[8];
	struct scatterlist asg[8];
	const char *e;
	struct tcrypt_result result;
H
Herbert Xu 已提交
238
	unsigned int authsize;
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268

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

	printk(KERN_INFO "\ntesting %s %s\n", algo, e);

	tsize = sizeof(struct aead_testvec);
	tsize *= tcount;

	if (tsize > TVMEMSIZE) {
		printk(KERN_INFO "template (%u) too big for tvmem (%u)\n",
		       tsize, TVMEMSIZE);
		return;
	}

	memcpy(tvmem, template, tsize);
	aead_tv = (void *)tvmem;

	init_completion(&result.completion);

	tfm = crypto_alloc_aead(algo, 0, 0);

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

H
Herbert Xu 已提交
269 270
	authsize = crypto_aead_authsize(tfm);

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
	req = aead_request_alloc(tfm, GFP_KERNEL);
	if (!req) {
		printk(KERN_INFO "failed to allocate request for %s\n", algo);
		goto out;
	}

	aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
				  tcrypt_complete, &result);

	for (i = 0, j = 0; i < tcount; i++) {
		if (!aead_tv[i].np) {
			printk(KERN_INFO "test %u (%d bit key):\n",
			       ++j, aead_tv[i].klen * 8);

			crypto_aead_clear_flags(tfm, ~0);
			if (aead_tv[i].wk)
				crypto_aead_set_flags(
					tfm, CRYPTO_TFM_REQ_WEAK_KEY);
			key = aead_tv[i].key;

			ret = crypto_aead_setkey(tfm, key,
						 aead_tv[i].klen);
			if (ret) {
				printk(KERN_INFO "setkey() failed flags=%x\n",
				       crypto_aead_get_flags(tfm));

				if (!aead_tv[i].fail)
					goto out;
			}

			sg_init_one(&sg[0], aead_tv[i].input,
H
Herbert Xu 已提交
302
				    aead_tv[i].ilen + (enc ? authsize : 0));
303 304 305 306 307 308 309 310 311 312

			sg_init_one(&asg[0], aead_tv[i].assoc,
				    aead_tv[i].alen);

			aead_request_set_crypt(req, sg, sg,
					       aead_tv[i].ilen,
					       aead_tv[i].iv);

			aead_request_set_assoc(req, asg, aead_tv[i].alen);

H
Herbert Xu 已提交
313 314 315
			ret = enc ?
				crypto_aead_encrypt(req) :
				crypto_aead_decrypt(req);
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376

			switch (ret) {
			case 0:
				break;
			case -EINPROGRESS:
			case -EBUSY:
				ret = wait_for_completion_interruptible(
					&result.completion);
				if (!ret && !(ret = result.err)) {
					INIT_COMPLETION(result.completion);
					break;
				}
				/* fall through */
			default:
				printk(KERN_INFO "%s () failed err=%d\n",
				       e, -ret);
				goto out;
			}

			q = kmap(sg_page(&sg[0])) + sg[0].offset;
			hexdump(q, aead_tv[i].rlen);

			printk(KERN_INFO "enc/dec: %s\n",
			       memcmp(q, aead_tv[i].result,
				      aead_tv[i].rlen) ? "fail" : "pass");
		}
	}

	printk(KERN_INFO "\ntesting %s %s across pages (chunking)\n", algo, e);
	memset(xbuf, 0, XBUFSIZE);

	for (i = 0, j = 0; i < tcount; i++) {
		if (aead_tv[i].np) {
			printk(KERN_INFO "test %u (%d bit key):\n",
			       ++j, aead_tv[i].klen * 8);

			crypto_aead_clear_flags(tfm, ~0);
			if (aead_tv[i].wk)
				crypto_aead_set_flags(
					tfm, CRYPTO_TFM_REQ_WEAK_KEY);
			key = aead_tv[i].key;

			ret = crypto_aead_setkey(tfm, key, aead_tv[i].klen);
			if (ret) {
				printk(KERN_INFO "setkey() failed flags=%x\n",
				       crypto_aead_get_flags(tfm));

				if (!aead_tv[i].fail)
					goto out;
			}

			sg_init_table(sg, aead_tv[i].np);
			for (k = 0, temp = 0; k < aead_tv[i].np; k++) {
				memcpy(&xbuf[IDX[k]],
				       aead_tv[i].input + temp,
				       aead_tv[i].tap[k]);
				temp += aead_tv[i].tap[k];
				sg_set_buf(&sg[k], &xbuf[IDX[k]],
					   aead_tv[i].tap[k]);
			}

H
Herbert Xu 已提交
377 378 379
			if (enc)
				sg[k - 1].length += authsize;

380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
			sg_init_table(asg, aead_tv[i].anp);
			for (k = 0, temp = 0; k < aead_tv[i].anp; k++) {
				memcpy(&axbuf[IDX[k]],
				       aead_tv[i].assoc + temp,
				       aead_tv[i].atap[k]);
				temp += aead_tv[i].atap[k];
				sg_set_buf(&asg[k], &axbuf[IDX[k]],
					   aead_tv[i].atap[k]);
			}

			aead_request_set_crypt(req, sg, sg,
					       aead_tv[i].ilen,
					       aead_tv[i].iv);

			aead_request_set_assoc(req, asg, aead_tv[i].alen);

H
Herbert Xu 已提交
396 397 398
			ret = enc ?
				crypto_aead_encrypt(req) :
				crypto_aead_decrypt(req);
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423

			switch (ret) {
			case 0:
				break;
			case -EINPROGRESS:
			case -EBUSY:
				ret = wait_for_completion_interruptible(
					&result.completion);
				if (!ret && !(ret = result.err)) {
					INIT_COMPLETION(result.completion);
					break;
				}
				/* fall through */
			default:
				printk(KERN_INFO "%s () failed err=%d\n",
				       e, -ret);
				goto out;
			}

			for (k = 0, temp = 0; k < aead_tv[i].np; k++) {
				printk(KERN_INFO "page %u\n", k);
				q = kmap(sg_page(&sg[k])) + sg[k].offset;
				hexdump(q, aead_tv[i].tap[k]);
				printk(KERN_INFO "%s\n",
				       memcmp(q, aead_tv[i].result + temp,
H
Herbert Xu 已提交
424 425 426
					      aead_tv[i].tap[k] -
					      (k < aead_tv[i].np - 1 || enc ?
					       0 : authsize)) ?
427 428 429 430 431 432 433 434 435 436 437 438
				       "fail" : "pass");

				temp += aead_tv[i].tap[k];
			}
		}
	}

out:
	crypto_free_aead(tfm);
	aead_request_free(req);
}

439
static void test_cipher(char *algo, int enc,
440
			struct cipher_testvec *template, unsigned int tcount)
L
Linus Torvalds 已提交
441 442 443
{
	unsigned int ret, i, j, k, temp;
	unsigned int tsize;
444
	char *q;
445
	struct crypto_ablkcipher *tfm;
L
Linus Torvalds 已提交
446 447
	char *key;
	struct cipher_testvec *cipher_tv;
448
	struct ablkcipher_request *req;
L
Linus Torvalds 已提交
449
	struct scatterlist sg[8];
450
	const char *e;
451
	struct tcrypt_result result;
L
Linus Torvalds 已提交
452 453

	if (enc == ENCRYPT)
454
	        e = "encryption";
L
Linus Torvalds 已提交
455
	else
456
		e = "decryption";
L
Linus Torvalds 已提交
457

458
	printk("\ntesting %s %s\n", algo, e);
L
Linus Torvalds 已提交
459

460
	tsize = sizeof (struct cipher_testvec);
L
Linus Torvalds 已提交
461 462 463 464 465
	if (tsize > TVMEMSIZE) {
		printk("template (%u) too big for tvmem (%u)\n", tsize,
		       TVMEMSIZE);
		return;
	}
466 467
	cipher_tv = (void *)tvmem;

468 469 470
	init_completion(&result.completion);

	tfm = crypto_alloc_ablkcipher(algo, 0, 0);
L
Linus Torvalds 已提交
471

472 473 474
	if (IS_ERR(tfm)) {
		printk("failed to load transform for %s: %ld\n", algo,
		       PTR_ERR(tfm));
L
Linus Torvalds 已提交
475 476
		return;
	}
477 478 479 480 481 482 483 484 485

	req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
	if (!req) {
		printk("failed to allocate request for %s\n", algo);
		goto out;
	}

	ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
					tcrypt_complete, &result);
486

L
Linus Torvalds 已提交
487 488
	j = 0;
	for (i = 0; i < tcount; i++) {
489 490
		memcpy(cipher_tv, &template[i], tsize);
		if (!(cipher_tv->np)) {
491
			j++;
L
Linus Torvalds 已提交
492
			printk("test %u (%d bit key):\n",
493
			j, cipher_tv->klen * 8);
L
Linus Torvalds 已提交
494

495
			crypto_ablkcipher_clear_flags(tfm, ~0);
496
			if (cipher_tv->wk)
497
				crypto_ablkcipher_set_flags(
498
					tfm, CRYPTO_TFM_REQ_WEAK_KEY);
499
			key = cipher_tv->key;
500

501
			ret = crypto_ablkcipher_setkey(tfm, key,
502
						       cipher_tv->klen);
L
Linus Torvalds 已提交
503
			if (ret) {
504
				printk("setkey() failed flags=%x\n",
505
				       crypto_ablkcipher_get_flags(tfm));
506

507
				if (!cipher_tv->fail)
L
Linus Torvalds 已提交
508
					goto out;
509
			}
L
Linus Torvalds 已提交
510

511 512
			sg_init_one(&sg[0], cipher_tv->input,
				    cipher_tv->ilen);
513

514
			ablkcipher_request_set_crypt(req, sg, sg,
515 516
						     cipher_tv->ilen,
						     cipher_tv->iv);
517

518
			ret = enc ?
519 520
				crypto_ablkcipher_encrypt(req) :
				crypto_ablkcipher_decrypt(req);
521

522 523 524 525 526 527 528 529 530 531 532 533 534 535
			switch (ret) {
			case 0:
				break;
			case -EINPROGRESS:
			case -EBUSY:
				ret = wait_for_completion_interruptible(
					&result.completion);
				if (!ret && !((ret = result.err))) {
					INIT_COMPLETION(result.completion);
					break;
				}
				/* fall through */
			default:
				printk("%s () failed err=%d\n", e, -ret);
L
Linus Torvalds 已提交
536
				goto out;
537 538
			}

J
Jens Axboe 已提交
539
			q = kmap(sg_page(&sg[0])) + sg[0].offset;
540
			hexdump(q, cipher_tv->rlen);
541 542

			printk("%s\n",
543 544
			       memcmp(q, cipher_tv->result,
				      cipher_tv->rlen) ? "fail" : "pass");
L
Linus Torvalds 已提交
545 546
		}
	}
547

548
	printk("\ntesting %s %s across pages (chunking)\n", algo, e);
L
Linus Torvalds 已提交
549
	memset(xbuf, 0, XBUFSIZE);
550

L
Linus Torvalds 已提交
551 552
	j = 0;
	for (i = 0; i < tcount; i++) {
553 554
		memcpy(cipher_tv, &template[i], tsize);
		if (cipher_tv->np) {
555
			j++;
L
Linus Torvalds 已提交
556
			printk("test %u (%d bit key):\n",
557
			j, cipher_tv->klen * 8);
L
Linus Torvalds 已提交
558

559
			crypto_ablkcipher_clear_flags(tfm, ~0);
560
			if (cipher_tv->wk)
561
				crypto_ablkcipher_set_flags(
562
					tfm, CRYPTO_TFM_REQ_WEAK_KEY);
563
			key = cipher_tv->key;
564

565
			ret = crypto_ablkcipher_setkey(tfm, key,
566
						       cipher_tv->klen);
L
Linus Torvalds 已提交
567
			if (ret) {
568
				printk("setkey() failed flags=%x\n",
569
				       crypto_ablkcipher_get_flags(tfm));
570

571
				if (!cipher_tv->fail)
L
Linus Torvalds 已提交
572 573 574 575
					goto out;
			}

			temp = 0;
576 577
			sg_init_table(sg, cipher_tv->np);
			for (k = 0; k < cipher_tv->np; k++) {
578
				memcpy(&xbuf[IDX[k]],
579 580 581
				       cipher_tv->input + temp,
				       cipher_tv->tap[k]);
				temp += cipher_tv->tap[k];
582
				sg_set_buf(&sg[k], &xbuf[IDX[k]],
583
					   cipher_tv->tap[k]);
L
Linus Torvalds 已提交
584
			}
585

586
			ablkcipher_request_set_crypt(req, sg, sg,
587 588
						     cipher_tv->ilen,
						     cipher_tv->iv);
589

590
			ret = enc ?
591 592
				crypto_ablkcipher_encrypt(req) :
				crypto_ablkcipher_decrypt(req);
593

594 595 596 597 598 599 600 601 602 603 604 605 606 607
			switch (ret) {
			case 0:
				break;
			case -EINPROGRESS:
			case -EBUSY:
				ret = wait_for_completion_interruptible(
					&result.completion);
				if (!ret && !((ret = result.err))) {
					INIT_COMPLETION(result.completion);
					break;
				}
				/* fall through */
			default:
				printk("%s () failed err=%d\n", e, -ret);
L
Linus Torvalds 已提交
608 609 610 611
				goto out;
			}

			temp = 0;
612
			for (k = 0; k < cipher_tv->np; k++) {
L
Linus Torvalds 已提交
613
				printk("page %u\n", k);
J
Jens Axboe 已提交
614
				q = kmap(sg_page(&sg[k])) + sg[k].offset;
615
				hexdump(q, cipher_tv->tap[k]);
616
				printk("%s\n",
617 618
					memcmp(q, cipher_tv->result + temp,
						cipher_tv->tap[k]) ? "fail" :
L
Linus Torvalds 已提交
619
					"pass");
620
				temp += cipher_tv->tap[k];
L
Linus Torvalds 已提交
621 622 623 624 625
			}
		}
	}

out:
626 627
	crypto_free_ablkcipher(tfm);
	ablkcipher_request_free(req);
L
Linus Torvalds 已提交
628 629
}

630
static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc, char *p,
631 632
			       int blen, int sec)
{
633
	struct scatterlist sg[1];
634 635 636 637
	unsigned long start, end;
	int bcount;
	int ret;

638
	sg_init_one(sg, p, blen);
639 640 641 642

	for (start = jiffies, end = start + sec * HZ, bcount = 0;
	     time_before(jiffies, end); bcount++) {
		if (enc)
643
			ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
644
		else
645
			ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
646 647 648 649 650 651 652 653 654 655

		if (ret)
			return ret;
	}

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

656
static int test_cipher_cycles(struct blkcipher_desc *desc, int enc, char *p,
657 658
			      int blen)
{
659
	struct scatterlist sg[1];
660 661 662 663
	unsigned long cycles = 0;
	int ret = 0;
	int i;

664
	sg_init_one(sg, p, blen);
665 666 667 668 669 670 671

	local_bh_disable();
	local_irq_disable();

	/* Warm-up run. */
	for (i = 0; i < 4; i++) {
		if (enc)
672
			ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
673
		else
674
			ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
675 676 677 678 679 680 681 682 683 684 685

		if (ret)
			goto out;
	}

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

		start = get_cycles();
		if (enc)
686
			ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
687
		else
688
			ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
		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;
}

708
static void test_cipher_speed(char *algo, int enc, unsigned int sec,
709 710
			      struct cipher_testvec *template,
			      unsigned int tcount, struct cipher_speed *speed)
H
Harald Welte 已提交
711
{
712
	unsigned int ret, i, j, iv_len;
H
Harald Welte 已提交
713
	unsigned char *key, *p, iv[128];
714 715 716
	struct crypto_blkcipher *tfm;
	struct blkcipher_desc desc;
	const char *e;
H
Harald Welte 已提交
717 718 719 720 721 722

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

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

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

727 728 729
	if (IS_ERR(tfm)) {
		printk("failed to load transform for %s: %ld\n", algo,
		       PTR_ERR(tfm));
H
Harald Welte 已提交
730 731
		return;
	}
732 733
	desc.tfm = tfm;
	desc.flags = 0;
H
Harald Welte 已提交
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748

	for (i = 0; speed[i].klen != 0; i++) {
		if ((speed[i].blen + speed[i].klen) > TVMEMSIZE) {
			printk("template (%u) too big for tvmem (%u)\n",
			       speed[i].blen + speed[i].klen, TVMEMSIZE);
			goto out;
		}

		printk("test %u (%d bit key, %d byte blocks): ", i,
		       speed[i].klen * 8, speed[i].blen);

		memset(tvmem, 0xff, speed[i].klen + speed[i].blen);

		/* set key, plain text and IV */
		key = (unsigned char *)tvmem;
749 750 751 752 753 754
		for (j = 0; j < tcount; j++) {
			if (template[j].klen == speed[i].klen) {
				key = template[j].key;
				break;
			}
		}
H
Harald Welte 已提交
755 756
		p = (unsigned char *)tvmem + speed[i].klen;

757
		ret = crypto_blkcipher_setkey(tfm, key, speed[i].klen);
H
Harald Welte 已提交
758
		if (ret) {
759 760
			printk("setkey() failed flags=%x\n",
			       crypto_blkcipher_get_flags(tfm));
H
Harald Welte 已提交
761 762 763
			goto out;
		}

764 765
		iv_len = crypto_blkcipher_ivsize(tfm);
		if (iv_len) {
H
Harald Welte 已提交
766
			memset(&iv, 0xff, iv_len);
767
			crypto_blkcipher_set_iv(tfm, iv, iv_len);
H
Harald Welte 已提交
768 769
		}

770
		if (sec)
771
			ret = test_cipher_jiffies(&desc, enc, p, speed[i].blen,
772 773
						  sec);
		else
774
			ret = test_cipher_cycles(&desc, enc, p, speed[i].blen);
H
Harald Welte 已提交
775

776
		if (ret) {
777
			printk("%s() failed flags=%x\n", e, desc.flags);
778
			break;
H
Harald Welte 已提交
779 780 781 782
		}
	}

out:
783
	crypto_free_blkcipher(tfm);
H
Harald Welte 已提交
784 785
}

786 787 788 789 790 791 792 793
static int test_hash_jiffies_digest(struct hash_desc *desc, char *p, int blen,
				    char *out, int sec)
{
	struct scatterlist sg[1];
	unsigned long start, end;
	int bcount;
	int ret;

794 795
	sg_init_table(sg, 1);

796 797
	for (start = jiffies, end = start + sec * HZ, bcount = 0;
	     time_before(jiffies, end); bcount++) {
798
		sg_set_buf(sg, p, blen);
799 800 801 802 803 804 805 806 807 808 809 810 811
		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;
}

static int test_hash_jiffies(struct hash_desc *desc, char *p, int blen,
			     int plen, char *out, int sec)
812 813 814 815
{
	struct scatterlist sg[1];
	unsigned long start, end;
	int bcount, pcount;
816 817 818 819
	int ret;

	if (plen == blen)
		return test_hash_jiffies_digest(desc, p, blen, out, sec);
820

821 822
	sg_init_table(sg, 1);

823 824
	for (start = jiffies, end = start + sec * HZ, bcount = 0;
	     time_before(jiffies, end); bcount++) {
825 826 827
		ret = crypto_hash_init(desc);
		if (ret)
			return ret;
828
		for (pcount = 0; pcount < blen; pcount += plen) {
829
			sg_set_buf(sg, p + pcount, plen);
830 831 832
			ret = crypto_hash_update(desc, sg, plen);
			if (ret)
				return ret;
833 834
		}
		/* we assume there is enough space in 'out' for the result */
835 836 837
		ret = crypto_hash_final(desc, out);
		if (ret)
			return ret;
838 839 840 841 842
	}

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

843 844 845 846 847 848 849 850 851 852 853
	return 0;
}

static int test_hash_cycles_digest(struct hash_desc *desc, char *p, int blen,
				   char *out)
{
	struct scatterlist sg[1];
	unsigned long cycles = 0;
	int i;
	int ret;

854 855
	sg_init_table(sg, 1);

856 857 858 859 860
	local_bh_disable();
	local_irq_disable();

	/* Warm-up run. */
	for (i = 0; i < 4; i++) {
861
		sg_set_buf(sg, p, blen);
862 863 864 865 866 867 868 869 870 871 872
		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();

873
		sg_set_buf(sg, p, blen);
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
		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;
894 895
}

896 897
static int test_hash_cycles(struct hash_desc *desc, char *p, int blen,
			    int plen, char *out)
898 899 900 901
{
	struct scatterlist sg[1];
	unsigned long cycles = 0;
	int i, pcount;
902 903 904 905
	int ret;

	if (plen == blen)
		return test_hash_cycles_digest(desc, p, blen, out);
906

907 908
	sg_init_table(sg, 1);

909 910 911 912 913
	local_bh_disable();
	local_irq_disable();

	/* Warm-up run. */
	for (i = 0; i < 4; i++) {
914 915 916
		ret = crypto_hash_init(desc);
		if (ret)
			goto out;
917
		for (pcount = 0; pcount < blen; pcount += plen) {
918
			sg_set_buf(sg, p + pcount, plen);
919 920 921
			ret = crypto_hash_update(desc, sg, plen);
			if (ret)
				goto out;
922
		}
923
		ret = crypto_hash_final(desc, out);
924 925
		if (ret)
			goto out;
926 927 928 929 930 931 932 933
	}

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

		start = get_cycles();

934 935 936
		ret = crypto_hash_init(desc);
		if (ret)
			goto out;
937
		for (pcount = 0; pcount < blen; pcount += plen) {
938
			sg_set_buf(sg, p + pcount, plen);
939 940 941
			ret = crypto_hash_update(desc, sg, plen);
			if (ret)
				goto out;
942
		}
943 944 945
		ret = crypto_hash_final(desc, out);
		if (ret)
			goto out;
946 947 948 949 950 951

		end = get_cycles();

		cycles += end - start;
	}

952
out:
953 954 955
	local_irq_enable();
	local_bh_enable();

956 957 958
	if (ret)
		return ret;

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

962
	return 0;
963 964
}

965 966
static void test_hash_speed(char *algo, unsigned int sec,
			      struct hash_speed *speed)
967
{
968 969
	struct crypto_hash *tfm;
	struct hash_desc desc;
970 971
	char output[1024];
	int i;
972
	int ret;
973 974 975

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

976
	tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
977

978 979 980
	if (IS_ERR(tfm)) {
		printk("failed to load transform for %s: %ld\n", algo,
		       PTR_ERR(tfm));
981 982 983
		return;
	}

984 985 986 987
	desc.tfm = tfm;
	desc.flags = 0;

	if (crypto_hash_digestsize(tfm) > sizeof(output)) {
988
		printk("digestsize(%u) > outputbuffer(%zu)\n",
989
		       crypto_hash_digestsize(tfm), sizeof(output));
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
		goto out;
	}

	for (i = 0; speed[i].blen != 0; i++) {
		if (speed[i].blen > TVMEMSIZE) {
			printk("template (%u) too big for tvmem (%u)\n",
			       speed[i].blen, TVMEMSIZE);
			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);

		memset(tvmem, 0xff, speed[i].blen);

		if (sec)
1006 1007
			ret = test_hash_jiffies(&desc, tvmem, speed[i].blen,
						speed[i].plen, output, sec);
1008
		else
1009 1010 1011 1012 1013 1014 1015
			ret = test_hash_cycles(&desc, tvmem, speed[i].blen,
					       speed[i].plen, output);

		if (ret) {
			printk("hashing failed ret=%d\n", ret);
			break;
		}
1016 1017 1018
	}

out:
1019
	crypto_free_hash(tfm);
1020 1021
}

1022 1023
static void test_comp(char *algo, struct comp_testvec *ctemplate,
		       struct comp_testvec *dtemplate, int ctcount, int dtcount)
L
Linus Torvalds 已提交
1024 1025 1026
{
	unsigned int i;
	char result[COMP_BUF_SIZE];
1027
	struct crypto_comp *tfm;
L
Linus Torvalds 已提交
1028 1029 1030
	struct comp_testvec *tv;
	unsigned int tsize;

1031
	printk("\ntesting %s compression\n", algo);
L
Linus Torvalds 已提交
1032

1033 1034
	tsize = sizeof(struct comp_testvec);
	tsize *= ctcount;
L
Linus Torvalds 已提交
1035 1036 1037 1038 1039 1040
	if (tsize > TVMEMSIZE) {
		printk("template (%u) too big for tvmem (%u)\n", tsize,
		       TVMEMSIZE);
		return;
	}

1041
	memcpy(tvmem, ctemplate, tsize);
1042
	tv = (void *)tvmem;
L
Linus Torvalds 已提交
1043

1044
	tfm = crypto_alloc_comp(algo, 0, CRYPTO_ALG_ASYNC);
1045
	if (IS_ERR(tfm)) {
1046
		printk("failed to load transform for %s\n", algo);
L
Linus Torvalds 已提交
1047 1048 1049
		return;
	}

1050
	for (i = 0; i < ctcount; i++) {
L
Linus Torvalds 已提交
1051
		int ilen, ret, dlen = COMP_BUF_SIZE;
1052

L
Linus Torvalds 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
		printk("test %u:\n", i + 1);
		memset(result, 0, sizeof (result));

		ilen = tv[i].inlen;
		ret = crypto_comp_compress(tfm, tv[i].input,
		                           ilen, result, &dlen);
		if (ret) {
			printk("fail: ret=%d\n", ret);
			continue;
		}
		hexdump(result, dlen);
		printk("%s (ratio %d:%d)\n",
		       memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
		       ilen, dlen);
	}

1069
	printk("\ntesting %s decompression\n", algo);
L
Linus Torvalds 已提交
1070

1071 1072
	tsize = sizeof(struct comp_testvec);
	tsize *= dtcount;
L
Linus Torvalds 已提交
1073 1074 1075 1076 1077 1078
	if (tsize > TVMEMSIZE) {
		printk("template (%u) too big for tvmem (%u)\n", tsize,
		       TVMEMSIZE);
		goto out;
	}

1079
	memcpy(tvmem, dtemplate, tsize);
1080
	tv = (void *)tvmem;
L
Linus Torvalds 已提交
1081

1082
	for (i = 0; i < dtcount; i++) {
L
Linus Torvalds 已提交
1083
		int ilen, ret, dlen = COMP_BUF_SIZE;
1084

L
Linus Torvalds 已提交
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
		printk("test %u:\n", i + 1);
		memset(result, 0, sizeof (result));

		ilen = tv[i].inlen;
		ret = crypto_comp_decompress(tfm, tv[i].input,
		                             ilen, result, &dlen);
		if (ret) {
			printk("fail: ret=%d\n", ret);
			continue;
		}
		hexdump(result, dlen);
		printk("%s (ratio %d:%d)\n",
		       memcmp(result, tv[i].output, dlen) ? "fail" : "pass",
		       ilen, dlen);
	}
out:
1101
	crypto_free_comp(tfm);
L
Linus Torvalds 已提交
1102 1103
}

1104
static void test_available(void)
L
Linus Torvalds 已提交
1105 1106
{
	char **name = check;
1107

L
Linus Torvalds 已提交
1108 1109
	while (*name) {
		printk("alg %s ", *name);
1110
		printk(crypto_has_alg(*name, 0, 0) ?
1111
		       "found\n" : "not found\n");
L
Linus Torvalds 已提交
1112
		name++;
1113
	}
L
Linus Torvalds 已提交
1114 1115
}

1116
static void do_test(void)
L
Linus Torvalds 已提交
1117 1118 1119 1120 1121
{
	switch (mode) {

	case 0:
		test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
1122

L
Linus Torvalds 已提交
1123
		test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
1124

L
Linus Torvalds 已提交
1125
		//DES
1126 1127 1128 1129 1130 1131 1132 1133
		test_cipher("ecb(des)", ENCRYPT, des_enc_tv_template,
			    DES_ENC_TEST_VECTORS);
		test_cipher("ecb(des)", DECRYPT, des_dec_tv_template,
			    DES_DEC_TEST_VECTORS);
		test_cipher("cbc(des)", ENCRYPT, des_cbc_enc_tv_template,
			    DES_CBC_ENC_TEST_VECTORS);
		test_cipher("cbc(des)", DECRYPT, des_cbc_dec_tv_template,
			    DES_CBC_DEC_TEST_VECTORS);
1134

L
Linus Torvalds 已提交
1135
		//DES3_EDE
1136 1137 1138 1139
		test_cipher("ecb(des3_ede)", ENCRYPT, des3_ede_enc_tv_template,
			    DES3_EDE_ENC_TEST_VECTORS);
		test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
			    DES3_EDE_DEC_TEST_VECTORS);
1140

L
Linus Torvalds 已提交
1141
		test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
1142

1143 1144
		test_hash("sha224", sha224_tv_template, SHA224_TEST_VECTORS);

L
Linus Torvalds 已提交
1145
		test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
1146

L
Linus Torvalds 已提交
1147
		//BLOWFISH
1148 1149 1150 1151 1152 1153 1154 1155
		test_cipher("ecb(blowfish)", ENCRYPT, bf_enc_tv_template,
			    BF_ENC_TEST_VECTORS);
		test_cipher("ecb(blowfish)", DECRYPT, bf_dec_tv_template,
			    BF_DEC_TEST_VECTORS);
		test_cipher("cbc(blowfish)", ENCRYPT, bf_cbc_enc_tv_template,
			    BF_CBC_ENC_TEST_VECTORS);
		test_cipher("cbc(blowfish)", DECRYPT, bf_cbc_dec_tv_template,
			    BF_CBC_DEC_TEST_VECTORS);
1156

L
Linus Torvalds 已提交
1157
		//TWOFISH
1158 1159 1160 1161 1162 1163 1164 1165
		test_cipher("ecb(twofish)", ENCRYPT, tf_enc_tv_template,
			    TF_ENC_TEST_VECTORS);
		test_cipher("ecb(twofish)", DECRYPT, tf_dec_tv_template,
			    TF_DEC_TEST_VECTORS);
		test_cipher("cbc(twofish)", ENCRYPT, tf_cbc_enc_tv_template,
			    TF_CBC_ENC_TEST_VECTORS);
		test_cipher("cbc(twofish)", DECRYPT, tf_cbc_dec_tv_template,
			    TF_CBC_DEC_TEST_VECTORS);
1166

L
Linus Torvalds 已提交
1167
		//SERPENT
1168 1169 1170 1171
		test_cipher("ecb(serpent)", ENCRYPT, serpent_enc_tv_template,
			    SERPENT_ENC_TEST_VECTORS);
		test_cipher("ecb(serpent)", DECRYPT, serpent_dec_tv_template,
			    SERPENT_DEC_TEST_VECTORS);
1172

L
Linus Torvalds 已提交
1173
		//TNEPRES
1174 1175 1176 1177
		test_cipher("ecb(tnepres)", ENCRYPT, tnepres_enc_tv_template,
			    TNEPRES_ENC_TEST_VECTORS);
		test_cipher("ecb(tnepres)", DECRYPT, tnepres_dec_tv_template,
			    TNEPRES_DEC_TEST_VECTORS);
L
Linus Torvalds 已提交
1178 1179

		//AES
1180 1181 1182 1183 1184 1185 1186 1187
		test_cipher("ecb(aes)", ENCRYPT, aes_enc_tv_template,
			    AES_ENC_TEST_VECTORS);
		test_cipher("ecb(aes)", DECRYPT, aes_dec_tv_template,
			    AES_DEC_TEST_VECTORS);
		test_cipher("cbc(aes)", ENCRYPT, aes_cbc_enc_tv_template,
			    AES_CBC_ENC_TEST_VECTORS);
		test_cipher("cbc(aes)", DECRYPT, aes_cbc_dec_tv_template,
			    AES_CBC_DEC_TEST_VECTORS);
R
Rik Snel 已提交
1188 1189 1190 1191
		test_cipher("lrw(aes)", ENCRYPT, aes_lrw_enc_tv_template,
			    AES_LRW_ENC_TEST_VECTORS);
		test_cipher("lrw(aes)", DECRYPT, aes_lrw_dec_tv_template,
			    AES_LRW_DEC_TEST_VECTORS);
1192 1193 1194 1195
		test_cipher("xts(aes)", ENCRYPT, aes_xts_enc_tv_template,
			    AES_XTS_ENC_TEST_VECTORS);
		test_cipher("xts(aes)", DECRYPT, aes_xts_dec_tv_template,
			    AES_XTS_DEC_TEST_VECTORS);
1196
		test_cipher("rfc3686(ctr(aes))", ENCRYPT, aes_ctr_enc_tv_template,
1197
			    AES_CTR_ENC_TEST_VECTORS);
1198
		test_cipher("rfc3686(ctr(aes))", DECRYPT, aes_ctr_dec_tv_template,
1199
			    AES_CTR_DEC_TEST_VECTORS);
M
Mikko Herranen 已提交
1200 1201 1202 1203
		test_aead("gcm(aes)", ENCRYPT, aes_gcm_enc_tv_template,
			  AES_GCM_ENC_TEST_VECTORS);
		test_aead("gcm(aes)", DECRYPT, aes_gcm_dec_tv_template,
			  AES_GCM_DEC_TEST_VECTORS);
L
Linus Torvalds 已提交
1204 1205

		//CAST5
1206 1207 1208 1209
		test_cipher("ecb(cast5)", ENCRYPT, cast5_enc_tv_template,
			    CAST5_ENC_TEST_VECTORS);
		test_cipher("ecb(cast5)", DECRYPT, cast5_dec_tv_template,
			    CAST5_DEC_TEST_VECTORS);
1210

L
Linus Torvalds 已提交
1211
		//CAST6
1212 1213 1214 1215
		test_cipher("ecb(cast6)", ENCRYPT, cast6_enc_tv_template,
			    CAST6_ENC_TEST_VECTORS);
		test_cipher("ecb(cast6)", DECRYPT, cast6_dec_tv_template,
			    CAST6_DEC_TEST_VECTORS);
L
Linus Torvalds 已提交
1216 1217

		//ARC4
1218 1219 1220 1221
		test_cipher("ecb(arc4)", ENCRYPT, arc4_enc_tv_template,
			    ARC4_ENC_TEST_VECTORS);
		test_cipher("ecb(arc4)", DECRYPT, arc4_dec_tv_template,
			    ARC4_DEC_TEST_VECTORS);
L
Linus Torvalds 已提交
1222 1223

		//TEA
1224 1225 1226 1227
		test_cipher("ecb(tea)", ENCRYPT, tea_enc_tv_template,
			    TEA_ENC_TEST_VECTORS);
		test_cipher("ecb(tea)", DECRYPT, tea_dec_tv_template,
			    TEA_DEC_TEST_VECTORS);
L
Linus Torvalds 已提交
1228 1229 1230


		//XTEA
1231 1232 1233 1234
		test_cipher("ecb(xtea)", ENCRYPT, xtea_enc_tv_template,
			    XTEA_ENC_TEST_VECTORS);
		test_cipher("ecb(xtea)", DECRYPT, xtea_dec_tv_template,
			    XTEA_DEC_TEST_VECTORS);
L
Linus Torvalds 已提交
1235 1236

		//KHAZAD
1237 1238 1239 1240
		test_cipher("ecb(khazad)", ENCRYPT, khazad_enc_tv_template,
			    KHAZAD_ENC_TEST_VECTORS);
		test_cipher("ecb(khazad)", DECRYPT, khazad_dec_tv_template,
			    KHAZAD_DEC_TEST_VECTORS);
L
Linus Torvalds 已提交
1241 1242

		//ANUBIS
1243 1244 1245 1246 1247 1248 1249 1250
		test_cipher("ecb(anubis)", ENCRYPT, anubis_enc_tv_template,
			    ANUBIS_ENC_TEST_VECTORS);
		test_cipher("ecb(anubis)", DECRYPT, anubis_dec_tv_template,
			    ANUBIS_DEC_TEST_VECTORS);
		test_cipher("cbc(anubis)", ENCRYPT, anubis_cbc_enc_tv_template,
			    ANUBIS_CBC_ENC_TEST_VECTORS);
		test_cipher("cbc(anubis)", DECRYPT, anubis_cbc_dec_tv_template,
			    ANUBIS_CBC_ENC_TEST_VECTORS);
L
Linus Torvalds 已提交
1251

A
Aaron Grothe 已提交
1252
		//XETA
1253 1254 1255 1256
		test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
			    XETA_ENC_TEST_VECTORS);
		test_cipher("ecb(xeta)", DECRYPT, xeta_dec_tv_template,
			    XETA_DEC_TEST_VECTORS);
A
Aaron Grothe 已提交
1257

1258 1259 1260 1261 1262 1263
		//FCrypt
		test_cipher("pcbc(fcrypt)", ENCRYPT, fcrypt_pcbc_enc_tv_template,
			    FCRYPT_ENC_TEST_VECTORS);
		test_cipher("pcbc(fcrypt)", DECRYPT, fcrypt_pcbc_dec_tv_template,
			    FCRYPT_DEC_TEST_VECTORS);

1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
		//CAMELLIA
		test_cipher("ecb(camellia)", ENCRYPT,
			    camellia_enc_tv_template,
			    CAMELLIA_ENC_TEST_VECTORS);
		test_cipher("ecb(camellia)", DECRYPT,
			    camellia_dec_tv_template,
			    CAMELLIA_DEC_TEST_VECTORS);
		test_cipher("cbc(camellia)", ENCRYPT,
			    camellia_cbc_enc_tv_template,
			    CAMELLIA_CBC_ENC_TEST_VECTORS);
		test_cipher("cbc(camellia)", DECRYPT,
			    camellia_cbc_dec_tv_template,
			    CAMELLIA_CBC_DEC_TEST_VECTORS);

1278 1279 1280 1281 1282 1283
		//SEED
		test_cipher("ecb(seed)", ENCRYPT, seed_enc_tv_template,
			    SEED_ENC_TEST_VECTORS);
		test_cipher("ecb(seed)", DECRYPT, seed_dec_tv_template,
			    SEED_DEC_TEST_VECTORS);

L
Linus Torvalds 已提交
1284 1285 1286 1287 1288 1289 1290 1291
		test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
		test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
		test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
		test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
		test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
		test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
		test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
		test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
1292 1293 1294
		test_comp("deflate", deflate_comp_tv_template,
			  deflate_decomp_tv_template, DEFLATE_COMP_TEST_VECTORS,
			  DEFLATE_DECOMP_TEST_VECTORS);
1295 1296
		test_comp("lzo", lzo_comp_tv_template, lzo_decomp_tv_template,
			  LZO_COMP_TEST_VECTORS, LZO_DECOMP_TEST_VECTORS);
1297
		test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
1298 1299 1300 1301
		test_hash("hmac(md5)", hmac_md5_tv_template,
			  HMAC_MD5_TEST_VECTORS);
		test_hash("hmac(sha1)", hmac_sha1_tv_template,
			  HMAC_SHA1_TEST_VECTORS);
1302 1303
		test_hash("hmac(sha224)", hmac_sha224_tv_template,
			  HMAC_SHA224_TEST_VECTORS);
1304 1305
		test_hash("hmac(sha256)", hmac_sha256_tv_template,
			  HMAC_SHA256_TEST_VECTORS);
1306 1307 1308 1309
		test_hash("hmac(sha384)", hmac_sha384_tv_template,
			  HMAC_SHA384_TEST_VECTORS);
		test_hash("hmac(sha512)", hmac_sha512_tv_template,
			  HMAC_SHA512_TEST_VECTORS);
L
Linus Torvalds 已提交
1310

1311 1312 1313
		test_hash("xcbc(aes)", aes_xcbc128_tv_template,
			  XCBC_AES_TEST_VECTORS);

L
Linus Torvalds 已提交
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
		test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
		break;

	case 1:
		test_hash("md5", md5_tv_template, MD5_TEST_VECTORS);
		break;

	case 2:
		test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
		break;

	case 3:
1326 1327 1328 1329 1330 1331 1332 1333
		test_cipher("ecb(des)", ENCRYPT, des_enc_tv_template,
			    DES_ENC_TEST_VECTORS);
		test_cipher("ecb(des)", DECRYPT, des_dec_tv_template,
			    DES_DEC_TEST_VECTORS);
		test_cipher("cbc(des)", ENCRYPT, des_cbc_enc_tv_template,
			    DES_CBC_ENC_TEST_VECTORS);
		test_cipher("cbc(des)", DECRYPT, des_cbc_dec_tv_template,
			    DES_CBC_DEC_TEST_VECTORS);
L
Linus Torvalds 已提交
1334 1335 1336
		break;

	case 4:
1337 1338 1339 1340
		test_cipher("ecb(des3_ede)", ENCRYPT, des3_ede_enc_tv_template,
			    DES3_EDE_ENC_TEST_VECTORS);
		test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
			    DES3_EDE_DEC_TEST_VECTORS);
L
Linus Torvalds 已提交
1341 1342 1343 1344 1345
		break;

	case 5:
		test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
		break;
1346

L
Linus Torvalds 已提交
1347 1348 1349
	case 6:
		test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
		break;
1350

L
Linus Torvalds 已提交
1351
	case 7:
1352 1353 1354 1355 1356 1357 1358 1359
		test_cipher("ecb(blowfish)", ENCRYPT, bf_enc_tv_template,
			    BF_ENC_TEST_VECTORS);
		test_cipher("ecb(blowfish)", DECRYPT, bf_dec_tv_template,
			    BF_DEC_TEST_VECTORS);
		test_cipher("cbc(blowfish)", ENCRYPT, bf_cbc_enc_tv_template,
			    BF_CBC_ENC_TEST_VECTORS);
		test_cipher("cbc(blowfish)", DECRYPT, bf_cbc_dec_tv_template,
			    BF_CBC_DEC_TEST_VECTORS);
L
Linus Torvalds 已提交
1360 1361 1362
		break;

	case 8:
1363 1364 1365 1366 1367 1368 1369 1370
		test_cipher("ecb(twofish)", ENCRYPT, tf_enc_tv_template,
			    TF_ENC_TEST_VECTORS);
		test_cipher("ecb(twofish)", DECRYPT, tf_dec_tv_template,
			    TF_DEC_TEST_VECTORS);
		test_cipher("cbc(twofish)", ENCRYPT, tf_cbc_enc_tv_template,
			    TF_CBC_ENC_TEST_VECTORS);
		test_cipher("cbc(twofish)", DECRYPT, tf_cbc_dec_tv_template,
			    TF_CBC_DEC_TEST_VECTORS);
L
Linus Torvalds 已提交
1371
		break;
1372

L
Linus Torvalds 已提交
1373
	case 9:
1374 1375 1376 1377
		test_cipher("ecb(serpent)", ENCRYPT, serpent_enc_tv_template,
			    SERPENT_ENC_TEST_VECTORS);
		test_cipher("ecb(serpent)", DECRYPT, serpent_dec_tv_template,
			    SERPENT_DEC_TEST_VECTORS);
L
Linus Torvalds 已提交
1378 1379 1380
		break;

	case 10:
1381 1382 1383 1384 1385 1386 1387 1388
		test_cipher("ecb(aes)", ENCRYPT, aes_enc_tv_template,
			    AES_ENC_TEST_VECTORS);
		test_cipher("ecb(aes)", DECRYPT, aes_dec_tv_template,
			    AES_DEC_TEST_VECTORS);
		test_cipher("cbc(aes)", ENCRYPT, aes_cbc_enc_tv_template,
			    AES_CBC_ENC_TEST_VECTORS);
		test_cipher("cbc(aes)", DECRYPT, aes_cbc_dec_tv_template,
			    AES_CBC_DEC_TEST_VECTORS);
R
Rik Snel 已提交
1389 1390 1391 1392
		test_cipher("lrw(aes)", ENCRYPT, aes_lrw_enc_tv_template,
			    AES_LRW_ENC_TEST_VECTORS);
		test_cipher("lrw(aes)", DECRYPT, aes_lrw_dec_tv_template,
			    AES_LRW_DEC_TEST_VECTORS);
1393 1394 1395 1396
		test_cipher("xts(aes)", ENCRYPT, aes_xts_enc_tv_template,
			    AES_XTS_ENC_TEST_VECTORS);
		test_cipher("xts(aes)", DECRYPT, aes_xts_dec_tv_template,
			    AES_XTS_DEC_TEST_VECTORS);
1397
		test_cipher("rfc3686(ctr(aes))", ENCRYPT, aes_ctr_enc_tv_template,
1398
			    AES_CTR_ENC_TEST_VECTORS);
1399
		test_cipher("rfc3686(ctr(aes))", DECRYPT, aes_ctr_dec_tv_template,
1400
			    AES_CTR_DEC_TEST_VECTORS);
L
Linus Torvalds 已提交
1401 1402 1403 1404 1405
		break;

	case 11:
		test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
		break;
1406

L
Linus Torvalds 已提交
1407 1408 1409 1410 1411
	case 12:
		test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
		break;

	case 13:
1412 1413 1414
		test_comp("deflate", deflate_comp_tv_template,
			  deflate_decomp_tv_template, DEFLATE_COMP_TEST_VECTORS,
			  DEFLATE_DECOMP_TEST_VECTORS);
L
Linus Torvalds 已提交
1415 1416 1417
		break;

	case 14:
1418 1419 1420 1421
		test_cipher("ecb(cast5)", ENCRYPT, cast5_enc_tv_template,
			    CAST5_ENC_TEST_VECTORS);
		test_cipher("ecb(cast5)", DECRYPT, cast5_dec_tv_template,
			    CAST5_DEC_TEST_VECTORS);
L
Linus Torvalds 已提交
1422 1423 1424
		break;

	case 15:
1425 1426 1427 1428
		test_cipher("ecb(cast6)", ENCRYPT, cast6_enc_tv_template,
			    CAST6_ENC_TEST_VECTORS);
		test_cipher("ecb(cast6)", DECRYPT, cast6_dec_tv_template,
			    CAST6_DEC_TEST_VECTORS);
L
Linus Torvalds 已提交
1429 1430 1431
		break;

	case 16:
1432 1433 1434 1435
		test_cipher("ecb(arc4)", ENCRYPT, arc4_enc_tv_template,
			    ARC4_ENC_TEST_VECTORS);
		test_cipher("ecb(arc4)", DECRYPT, arc4_dec_tv_template,
			    ARC4_DEC_TEST_VECTORS);
L
Linus Torvalds 已提交
1436 1437 1438 1439 1440 1441 1442
		break;

	case 17:
		test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
		break;

	case 18:
1443
		test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
L
Linus Torvalds 已提交
1444 1445 1446
		break;

	case 19:
1447 1448 1449 1450
		test_cipher("ecb(tea)", ENCRYPT, tea_enc_tv_template,
			    TEA_ENC_TEST_VECTORS);
		test_cipher("ecb(tea)", DECRYPT, tea_dec_tv_template,
			    TEA_DEC_TEST_VECTORS);
L
Linus Torvalds 已提交
1451 1452 1453
		break;

	case 20:
1454 1455 1456 1457
		test_cipher("ecb(xtea)", ENCRYPT, xtea_enc_tv_template,
			    XTEA_ENC_TEST_VECTORS);
		test_cipher("ecb(xtea)", DECRYPT, xtea_dec_tv_template,
			    XTEA_DEC_TEST_VECTORS);
L
Linus Torvalds 已提交
1458 1459 1460
		break;

	case 21:
1461 1462 1463 1464
		test_cipher("ecb(khazad)", ENCRYPT, khazad_enc_tv_template,
			    KHAZAD_ENC_TEST_VECTORS);
		test_cipher("ecb(khazad)", DECRYPT, khazad_dec_tv_template,
			    KHAZAD_DEC_TEST_VECTORS);
L
Linus Torvalds 已提交
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
		break;

	case 22:
		test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
		break;

	case 23:
		test_hash("wp384", wp384_tv_template, WP384_TEST_VECTORS);
		break;

	case 24:
		test_hash("wp256", wp256_tv_template, WP256_TEST_VECTORS);
		break;

	case 25:
1480 1481 1482 1483
		test_cipher("ecb(tnepres)", ENCRYPT, tnepres_enc_tv_template,
			    TNEPRES_ENC_TEST_VECTORS);
		test_cipher("ecb(tnepres)", DECRYPT, tnepres_dec_tv_template,
			    TNEPRES_DEC_TEST_VECTORS);
L
Linus Torvalds 已提交
1484 1485 1486
		break;

	case 26:
1487 1488 1489 1490 1491 1492 1493 1494
		test_cipher("ecb(anubis)", ENCRYPT, anubis_enc_tv_template,
			    ANUBIS_ENC_TEST_VECTORS);
		test_cipher("ecb(anubis)", DECRYPT, anubis_dec_tv_template,
			    ANUBIS_DEC_TEST_VECTORS);
		test_cipher("cbc(anubis)", ENCRYPT, anubis_cbc_enc_tv_template,
			    ANUBIS_CBC_ENC_TEST_VECTORS);
		test_cipher("cbc(anubis)", DECRYPT, anubis_cbc_dec_tv_template,
			    ANUBIS_CBC_ENC_TEST_VECTORS);
L
Linus Torvalds 已提交
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
		break;

	case 27:
		test_hash("tgr192", tgr192_tv_template, TGR192_TEST_VECTORS);
		break;

	case 28:

		test_hash("tgr160", tgr160_tv_template, TGR160_TEST_VECTORS);
		break;

	case 29:
		test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
		break;
A
Aaron Grothe 已提交
1509 1510
		
	case 30:
1511 1512 1513 1514
		test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
			    XETA_ENC_TEST_VECTORS);
		test_cipher("ecb(xeta)", DECRYPT, xeta_dec_tv_template,
			    XETA_DEC_TEST_VECTORS);
A
Aaron Grothe 已提交
1515
		break;
L
Linus Torvalds 已提交
1516

1517 1518 1519 1520 1521 1522 1523
	case 31:
		test_cipher("pcbc(fcrypt)", ENCRYPT, fcrypt_pcbc_enc_tv_template,
			    FCRYPT_ENC_TEST_VECTORS);
		test_cipher("pcbc(fcrypt)", DECRYPT, fcrypt_pcbc_dec_tv_template,
			    FCRYPT_DEC_TEST_VECTORS);
		break;

1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
	case 32:
		test_cipher("ecb(camellia)", ENCRYPT,
			    camellia_enc_tv_template,
			    CAMELLIA_ENC_TEST_VECTORS);
		test_cipher("ecb(camellia)", DECRYPT,
			    camellia_dec_tv_template,
			    CAMELLIA_DEC_TEST_VECTORS);
		test_cipher("cbc(camellia)", ENCRYPT,
			    camellia_cbc_enc_tv_template,
			    CAMELLIA_CBC_ENC_TEST_VECTORS);
		test_cipher("cbc(camellia)", DECRYPT,
			    camellia_cbc_dec_tv_template,
			    CAMELLIA_CBC_DEC_TEST_VECTORS);
		break;
1538 1539 1540
	case 33:
		test_hash("sha224", sha224_tv_template, SHA224_TEST_VECTORS);
		break;
1541

1542 1543 1544 1545 1546 1547
	case 34:
		test_cipher("salsa20", ENCRYPT,
			    salsa20_stream_enc_tv_template,
			    SALSA20_STREAM_ENC_TEST_VECTORS);
		break;

1548 1549 1550 1551 1552 1553 1554
	case 35:
		test_aead("gcm(aes)", ENCRYPT, aes_gcm_enc_tv_template,
			  AES_GCM_ENC_TEST_VECTORS);
		test_aead("gcm(aes)", DECRYPT, aes_gcm_dec_tv_template,
			  AES_GCM_DEC_TEST_VECTORS);
		break;

1555 1556 1557 1558 1559
	case 36:
		test_comp("lzo", lzo_comp_tv_template, lzo_decomp_tv_template,
			  LZO_COMP_TEST_VECTORS, LZO_DECOMP_TEST_VECTORS);
		break;

L
Linus Torvalds 已提交
1560
	case 100:
1561 1562
		test_hash("hmac(md5)", hmac_md5_tv_template,
			  HMAC_MD5_TEST_VECTORS);
L
Linus Torvalds 已提交
1563
		break;
1564

L
Linus Torvalds 已提交
1565
	case 101:
1566 1567
		test_hash("hmac(sha1)", hmac_sha1_tv_template,
			  HMAC_SHA1_TEST_VECTORS);
L
Linus Torvalds 已提交
1568
		break;
1569

L
Linus Torvalds 已提交
1570
	case 102:
1571 1572
		test_hash("hmac(sha256)", hmac_sha256_tv_template,
			  HMAC_SHA256_TEST_VECTORS);
L
Linus Torvalds 已提交
1573 1574
		break;

1575 1576 1577 1578 1579 1580 1581 1582 1583
	case 103:
		test_hash("hmac(sha384)", hmac_sha384_tv_template,
			  HMAC_SHA384_TEST_VECTORS);
		break;

	case 104:
		test_hash("hmac(sha512)", hmac_sha512_tv_template,
			  HMAC_SHA512_TEST_VECTORS);
		break;
1584 1585 1586 1587
	case 105:
		test_hash("hmac(sha224)", hmac_sha224_tv_template,
			  HMAC_SHA224_TEST_VECTORS);
		break;
L
Linus Torvalds 已提交
1588

H
Harald Welte 已提交
1589
	case 200:
1590
		test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
1591
				  aes_speed_template);
1592
		test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
1593
				  aes_speed_template);
1594
		test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
1595
				  aes_speed_template);
1596
		test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
1597
				  aes_speed_template);
R
Rik Snel 已提交
1598 1599 1600 1601
		test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
				  aes_lrw_speed_template);
		test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
				  aes_lrw_speed_template);
1602 1603 1604 1605
		test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
				  aes_xts_speed_template);
		test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
				  aes_xts_speed_template);
H
Harald Welte 已提交
1606 1607 1608
		break;

	case 201:
1609
		test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
1610 1611 1612
				  des3_ede_enc_tv_template,
				  DES3_EDE_ENC_TEST_VECTORS,
				  des3_ede_speed_template);
1613
		test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
1614 1615 1616
				  des3_ede_dec_tv_template,
				  DES3_EDE_DEC_TEST_VECTORS,
				  des3_ede_speed_template);
1617
		test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
1618 1619 1620
				  des3_ede_enc_tv_template,
				  DES3_EDE_ENC_TEST_VECTORS,
				  des3_ede_speed_template);
1621
		test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
1622 1623 1624
				  des3_ede_dec_tv_template,
				  DES3_EDE_DEC_TEST_VECTORS,
				  des3_ede_speed_template);
H
Harald Welte 已提交
1625 1626 1627
		break;

	case 202:
1628
		test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
1629
				  twofish_speed_template);
1630
		test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
1631
				  twofish_speed_template);
1632
		test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
1633
				  twofish_speed_template);
1634
		test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
1635
				  twofish_speed_template);
H
Harald Welte 已提交
1636 1637 1638
		break;

	case 203:
1639
		test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
1640
				  blowfish_speed_template);
1641
		test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
1642
				  blowfish_speed_template);
1643
		test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
1644
				  blowfish_speed_template);
1645
		test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
1646
				  blowfish_speed_template);
H
Harald Welte 已提交
1647 1648 1649
		break;

	case 204:
1650
		test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
1651
				  des_speed_template);
1652
		test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
1653
				  des_speed_template);
1654
		test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
1655
				  des_speed_template);
1656
		test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
1657
				  des_speed_template);
H
Harald Welte 已提交
1658 1659
		break;

1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
	case 205:
		test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
				camellia_speed_template);
		test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
				camellia_speed_template);
		test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
				camellia_speed_template);
		test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
				camellia_speed_template);
		break;

1671 1672 1673 1674 1675
	case 206:
		test_cipher_speed("salsa20", ENCRYPT, sec, NULL, 0,
				  salsa20_speed_template);
		break;

1676 1677 1678 1679
	case 300:
		/* fall through */

	case 301:
1680
		test_hash_speed("md4", sec, generic_hash_speed_template);
1681 1682 1683
		if (mode > 300 && mode < 400) break;

	case 302:
1684
		test_hash_speed("md5", sec, generic_hash_speed_template);
1685 1686 1687
		if (mode > 300 && mode < 400) break;

	case 303:
1688
		test_hash_speed("sha1", sec, generic_hash_speed_template);
1689 1690 1691
		if (mode > 300 && mode < 400) break;

	case 304:
1692
		test_hash_speed("sha256", sec, generic_hash_speed_template);
1693 1694 1695
		if (mode > 300 && mode < 400) break;

	case 305:
1696
		test_hash_speed("sha384", sec, generic_hash_speed_template);
1697 1698 1699
		if (mode > 300 && mode < 400) break;

	case 306:
1700
		test_hash_speed("sha512", sec, generic_hash_speed_template);
1701 1702 1703
		if (mode > 300 && mode < 400) break;

	case 307:
1704
		test_hash_speed("wp256", sec, generic_hash_speed_template);
1705 1706 1707
		if (mode > 300 && mode < 400) break;

	case 308:
1708
		test_hash_speed("wp384", sec, generic_hash_speed_template);
1709 1710 1711
		if (mode > 300 && mode < 400) break;

	case 309:
1712
		test_hash_speed("wp512", sec, generic_hash_speed_template);
1713 1714 1715
		if (mode > 300 && mode < 400) break;

	case 310:
1716
		test_hash_speed("tgr128", sec, generic_hash_speed_template);
1717 1718 1719
		if (mode > 300 && mode < 400) break;

	case 311:
1720
		test_hash_speed("tgr160", sec, generic_hash_speed_template);
1721 1722 1723
		if (mode > 300 && mode < 400) break;

	case 312:
1724
		test_hash_speed("tgr192", sec, generic_hash_speed_template);
1725 1726
		if (mode > 300 && mode < 400) break;

1727 1728 1729 1730
	case 313:
		test_hash_speed("sha224", sec, generic_hash_speed_template);
		if (mode > 300 && mode < 400) break;

1731 1732 1733
	case 399:
		break;

L
Linus Torvalds 已提交
1734 1735 1736
	case 1000:
		test_available();
		break;
1737

L
Linus Torvalds 已提交
1738 1739 1740 1741 1742 1743 1744
	default:
		/* useful for debugging */
		printk("not testing anything\n");
		break;
	}
}

1745
static int __init init(void)
L
Linus Torvalds 已提交
1746
{
1747 1748
	int err = -ENOMEM;

L
Linus Torvalds 已提交
1749 1750
	tvmem = kmalloc(TVMEMSIZE, GFP_KERNEL);
	if (tvmem == NULL)
1751
		return err;
L
Linus Torvalds 已提交
1752 1753

	xbuf = kmalloc(XBUFSIZE, GFP_KERNEL);
1754 1755
	if (xbuf == NULL)
		goto err_free_tv;
L
Linus Torvalds 已提交
1756

1757 1758 1759
	axbuf = kmalloc(XBUFSIZE, GFP_KERNEL);
	if (axbuf == NULL)
		goto err_free_xbuf;
L
Linus Torvalds 已提交
1760

1761
	do_test();
1762 1763 1764 1765 1766 1767 1768

	/* 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
	 */
1769 1770 1771 1772 1773 1774 1775 1776 1777
	err = -EAGAIN;

	kfree(axbuf);
 err_free_xbuf:
	kfree(xbuf);
 err_free_tv:
	kfree(tvmem);

	return err;
L
Linus Torvalds 已提交
1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
}

/*
 * If an init function is provided, an exit function must also be provided
 * to allow module unload.
 */
static void __exit fini(void) { }

module_init(init);
module_exit(fini);

module_param(mode, int, 0);
H
Harald Welte 已提交
1790
module_param(sec, uint, 0);
1791 1792
MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
		      "(defaults to zero which uses CPU cycles instead)");
L
Linus Torvalds 已提交
1793 1794 1795 1796

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