tcrypt.c 50.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 23
#include <linux/init.h>
#include <linux/module.h>
#include <linux/mm.h>
#include <linux/slab.h>
24
#include <linux/scatterlist.h>
L
Linus Torvalds 已提交
25 26 27
#include <linux/string.h>
#include <linux/crypto.h>
#include <linux/moduleparam.h>
H
Harald Welte 已提交
28
#include <linux/jiffies.h>
29 30
#include <linux/timex.h>
#include <linux/interrupt.h>
L
Linus Torvalds 已提交
31 32 33
#include "tcrypt.h"

/*
34
 * Need slab memory for testing (size in number of pages).
L
Linus Torvalds 已提交
35
 */
36 37
#define TVMEMSIZE	4
#define XBUFSIZE	8
L
Linus Torvalds 已提交
38 39 40 41

/*
 * Indexes into the xbuf to simulate cross-page access.
 */
42
#define IDX1		32
L
Linus Torvalds 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56
#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

57 58 59 60 61
struct tcrypt_result {
	struct completion completion;
	int err;
};

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

H
Harald Welte 已提交
64 65 66
/*
 * Used by test_cipher_speed()
 */
67
static unsigned int sec;
H
Harald Welte 已提交
68

L
Linus Torvalds 已提交
69
static int mode;
70 71 72
static char *xbuf[XBUFSIZE];
static char *axbuf[XBUFSIZE];
static char *tvmem[TVMEMSIZE];
L
Linus Torvalds 已提交
73 74

static char *check[] = {
75 76 77
	"des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256",
	"blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
	"cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
78
	"khazad", "wp512", "wp384", "wp256", "tnepres", "xeta",  "fcrypt",
79 80
	"camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
	"lzo", "cts", NULL
L
Linus Torvalds 已提交
81 82
};

83
static void hexdump(unsigned char *buf, unsigned int len)
L
Linus Torvalds 已提交
84
{
85 86 87
	print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
			16, 1,
			buf, len, false);
L
Linus Torvalds 已提交
88 89
}

90 91 92 93 94 95 96 97 98 99 100
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);
}

101 102
static int test_hash(char *algo, struct hash_testvec *template,
		     unsigned int tcount)
L
Linus Torvalds 已提交
103
{
104 105 106
	unsigned int i, j, k, temp;
	struct scatterlist sg[8];
	char result[64];
107 108 109
	struct crypto_ahash *tfm;
	struct ahash_request *req;
	struct tcrypt_result tresult;
110
	int ret;
111
	void *hash_buff;
112

113 114 115
	init_completion(&tresult.completion);

	tfm = crypto_alloc_ahash(algo, 0, 0);
116
	if (IS_ERR(tfm)) {
117 118 119
		printk(KERN_ERR "alg: hash: Failed to load transform for %s: "
		       "%ld\n", algo, PTR_ERR(tfm));
		return PTR_ERR(tfm);
L
Linus Torvalds 已提交
120 121
	}

122 123
	req = ahash_request_alloc(tfm, GFP_KERNEL);
	if (!req) {
124 125 126
		printk(KERN_ERR "alg: hash: Failed to allocate request for "
		       "%s\n", algo);
		ret = -ENOMEM;
127 128 129 130
		goto out_noreq;
	}
	ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
				   tcrypt_complete, &tresult);
131

L
Linus Torvalds 已提交
132
	for (i = 0; i < tcount; i++) {
133
		memset(result, 0, 64);
L
Linus Torvalds 已提交
134

135
		hash_buff = xbuf[0];
L
Linus Torvalds 已提交
136

137 138 139 140
		memcpy(hash_buff, template[i].plaintext, template[i].psize);
		sg_init_one(&sg[0], hash_buff, template[i].psize);

		if (template[i].ksize) {
141 142 143
			crypto_ahash_clear_flags(tfm, ~0);
			ret = crypto_ahash_setkey(tfm, template[i].key,
						  template[i].ksize);
144
			if (ret) {
145 146 147
				printk(KERN_ERR "alg: hash: setkey failed on "
				       "test %d for %s: ret=%d\n", i + 1, algo,
				       -ret);
148 149 150 151
				goto out;
			}
		}

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
		ahash_request_set_crypt(req, sg, result, template[i].psize);
		ret = crypto_ahash_digest(req);
		switch (ret) {
		case 0:
			break;
		case -EINPROGRESS:
		case -EBUSY:
			ret = wait_for_completion_interruptible(
				&tresult.completion);
			if (!ret && !(ret = tresult.err)) {
				INIT_COMPLETION(tresult.completion);
				break;
			}
			/* fall through */
		default:
167 168
			printk(KERN_ERR "alg: hash: digest failed on test %d "
			       "for %s: ret=%d\n", i + 1, algo, -ret);
169 170
			goto out;
		}
L
Linus Torvalds 已提交
171

172 173 174 175 176 177 178 179
		if (memcmp(result, template[i].digest,
			   crypto_ahash_digestsize(tfm))) {
			printk(KERN_ERR "alg: hash: Test %d failed for %s\n",
			       i + 1, algo);
			hexdump(result, crypto_ahash_digestsize(tfm));
			ret = -EINVAL;
			goto out;
		}
L
Linus Torvalds 已提交
180 181 182 183
	}

	j = 0;
	for (i = 0; i < tcount; i++) {
184
		if (template[i].np) {
L
Linus Torvalds 已提交
185
			j++;
186
			memset(result, 0, 64);
L
Linus Torvalds 已提交
187 188

			temp = 0;
189 190
			sg_init_table(sg, template[i].np);
			for (k = 0; k < template[i].np; k++) {
191 192 193 194 195 196
				sg_set_buf(&sg[k],
					   memcpy(xbuf[IDX[k] >> PAGE_SHIFT] +
						  offset_in_page(IDX[k]),
						  template[i].plaintext + temp,
						  template[i].tap[k]),
					   template[i].tap[k]);
197
				temp += template[i].tap[k];
L
Linus Torvalds 已提交
198 199
			}

200
			if (template[i].ksize) {
201 202 203
				crypto_ahash_clear_flags(tfm, ~0);
				ret = crypto_ahash_setkey(tfm, template[i].key,
							  template[i].ksize);
204

205
				if (ret) {
206 207 208 209
					printk(KERN_ERR "alg: hash: setkey "
					       "failed on chunking test %d "
					       "for %s: ret=%d\n", j, algo,
					       -ret);
210 211
					goto out;
				}
L
Linus Torvalds 已提交
212 213
			}

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
			ahash_request_set_crypt(req, sg, result,
						template[i].psize);
			ret = crypto_ahash_digest(req);
			switch (ret) {
			case 0:
				break;
			case -EINPROGRESS:
			case -EBUSY:
				ret = wait_for_completion_interruptible(
					&tresult.completion);
				if (!ret && !(ret = tresult.err)) {
					INIT_COMPLETION(tresult.completion);
					break;
				}
				/* fall through */
			default:
230 231 232
				printk(KERN_ERR "alg: hash: digest failed "
				       "on chunking test %d for %s: "
				       "ret=%d\n", j, algo, -ret);
233 234
				goto out;
			}
235

236 237 238 239 240 241 242 243
			if (memcmp(result, template[i].digest,
				   crypto_ahash_digestsize(tfm))) {
				printk(KERN_ERR "alg: hash: Chunking test %d "
				       "failed for %s\n", j, algo);
				hexdump(result, crypto_ahash_digestsize(tfm));
				ret = -EINVAL;
				goto out;
			}
L
Linus Torvalds 已提交
244 245
		}
	}
246

247 248
	ret = 0;

L
Linus Torvalds 已提交
249
out:
250 251 252
	ahash_request_free(req);
out_noreq:
	crypto_free_ahash(tfm);
253
	return ret;
L
Linus Torvalds 已提交
254 255
}

256 257
static int test_aead(char *algo, int enc, struct aead_testvec *template,
		     unsigned int tcount)
258
{
259 260
	unsigned int i, j, k, n, temp;
	int ret = 0;
261 262 263 264 265 266 267 268
	char *q;
	struct crypto_aead *tfm;
	char *key;
	struct aead_request *req;
	struct scatterlist sg[8];
	struct scatterlist asg[8];
	const char *e;
	struct tcrypt_result result;
H
Herbert Xu 已提交
269
	unsigned int authsize;
270 271 272
	void *input;
	void *assoc;
	char iv[MAX_IVLEN];
273 274 275 276 277 278 279 280 281 282 283

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

	init_completion(&result.completion);

	tfm = crypto_alloc_aead(algo, 0, 0);

	if (IS_ERR(tfm)) {
284 285 286
		printk(KERN_ERR "alg: aead: Failed to load transform for %s: "
		       "%ld\n", algo, PTR_ERR(tfm));
		return PTR_ERR(tfm);
287 288 289 290
	}

	req = aead_request_alloc(tfm, GFP_KERNEL);
	if (!req) {
291 292 293
		printk(KERN_ERR "alg: aead: Failed to allocate request for "
		       "%s\n", algo);
		ret = -ENOMEM;
294 295 296 297 298 299 300
		goto out;
	}

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

	for (i = 0, j = 0; i < tcount; i++) {
301
		if (!template[i].np) {
302
			j++;
303 304 305 306

			/* some tepmplates have no input data but they will
			 * touch input
			 */
307 308
			input = xbuf[0];
			assoc = axbuf[0];
309 310 311 312 313 314 315

			memcpy(input, template[i].input, template[i].ilen);
			memcpy(assoc, template[i].assoc, template[i].alen);
			if (template[i].iv)
				memcpy(iv, template[i].iv, MAX_IVLEN);
			else
				memset(iv, 0, MAX_IVLEN);
316 317

			crypto_aead_clear_flags(tfm, ~0);
318
			if (template[i].wk)
319 320
				crypto_aead_set_flags(
					tfm, CRYPTO_TFM_REQ_WEAK_KEY);
321

322
			key = template[i].key;
323 324

			ret = crypto_aead_setkey(tfm, key,
325
						 template[i].klen);
326 327 328
			if (!ret == template[i].fail) {
				printk(KERN_ERR "alg: aead: setkey failed on "
				       "test %d for %s: flags=%x\n", j, algo,
329
				       crypto_aead_get_flags(tfm));
330 331 332
				goto out;
			} else if (ret)
				continue;
333

334
			authsize = abs(template[i].rlen - template[i].ilen);
J
Joy Latten 已提交
335 336
			ret = crypto_aead_setauthsize(tfm, authsize);
			if (ret) {
337 338 339 340
				printk(KERN_ERR "alg: aead: Failed to set "
				       "authsize to %u on test %d for %s\n",
				       authsize, j, algo);
				goto out;
J
Joy Latten 已提交
341 342
			}

343 344
			sg_init_one(&sg[0], input,
				    template[i].ilen + (enc ? authsize : 0));
345

346
			sg_init_one(&asg[0], assoc, template[i].alen);
347 348

			aead_request_set_crypt(req, sg, sg,
349
					       template[i].ilen, iv);
350

351
			aead_request_set_assoc(req, asg, template[i].alen);
352

H
Herbert Xu 已提交
353 354 355
			ret = enc ?
				crypto_aead_encrypt(req) :
				crypto_aead_decrypt(req);
356 357 358 359 360 361 362 363 364 365 366 367 368 369

			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:
370 371 372
				printk(KERN_ERR "alg: aead: %s failed on test "
				       "%d for %s: ret=%d\n", e, j, algo, -ret);
				goto out;
373 374
			}

375
			q = input;
376 377 378 379 380 381 382
			if (memcmp(q, template[i].result, template[i].rlen)) {
				printk(KERN_ERR "alg: aead: Test %d failed on "
				       "%s for %s\n", j, e, algo);
				hexdump(q, template[i].rlen);
				ret = -EINVAL;
				goto out;
			}
383 384 385 386
		}
	}

	for (i = 0, j = 0; i < tcount; i++) {
387
		if (template[i].np) {
388
			j++;
389 390 391 392 393

			if (template[i].iv)
				memcpy(iv, template[i].iv, MAX_IVLEN);
			else
				memset(iv, 0, MAX_IVLEN);
394 395

			crypto_aead_clear_flags(tfm, ~0);
396
			if (template[i].wk)
397 398
				crypto_aead_set_flags(
					tfm, CRYPTO_TFM_REQ_WEAK_KEY);
399
			key = template[i].key;
400

401
			ret = crypto_aead_setkey(tfm, key, template[i].klen);
402 403 404 405 406 407 408
			if (!ret == template[i].fail) {
				printk(KERN_ERR "alg: aead: setkey failed on "
				       "chunk test %d for %s: flags=%x\n", j,
				       algo, crypto_aead_get_flags(tfm));
				goto out;
			} else if (ret)
				continue;
409

410 411
			authsize = abs(template[i].rlen - template[i].ilen);

412
			ret = -EINVAL;
413 414
			sg_init_table(sg, template[i].np);
			for (k = 0, temp = 0; k < template[i].np; k++) {
415 416 417 418 419 420 421 422
				if (WARN_ON(offset_in_page(IDX[k]) +
					    template[i].tap[k] > PAGE_SIZE))
					goto out;

				q = xbuf[IDX[k] >> PAGE_SHIFT] +
				    offset_in_page(IDX[k]);

				memcpy(q, template[i].input + temp,
423
				       template[i].tap[k]);
424 425 426 427 428 429 430 431

				n = template[i].tap[k];
				if (k == template[i].np - 1 && enc)
					n += authsize;
				if (offset_in_page(q) + n < PAGE_SIZE)
					q[n] = 0;

				sg_set_buf(&sg[k], q, template[i].tap[k]);
432
				temp += template[i].tap[k];
433 434
			}

J
Joy Latten 已提交
435 436
			ret = crypto_aead_setauthsize(tfm, authsize);
			if (ret) {
437 438 439
				printk(KERN_ERR "alg: aead: Failed to set "
				       "authsize to %u on chunk test %d for "
				       "%s\n", authsize, j, algo);
J
Joy Latten 已提交
440 441 442
				goto out;
			}

443 444 445
			if (enc) {
				if (WARN_ON(sg[k - 1].offset +
					    sg[k - 1].length + authsize >
446 447
					    PAGE_SIZE)) {
					ret = -EINVAL;
448
					goto out;
449
				}
450

H
Herbert Xu 已提交
451
				sg[k - 1].length += authsize;
452
			}
H
Herbert Xu 已提交
453

454 455
			sg_init_table(asg, template[i].anp);
			for (k = 0, temp = 0; k < template[i].anp; k++) {
456 457 458 459 460
				sg_set_buf(&asg[k],
					   memcpy(axbuf[IDX[k] >> PAGE_SHIFT] +
						  offset_in_page(IDX[k]),
						  template[i].assoc + temp,
						  template[i].atap[k]),
461
					   template[i].atap[k]);
462
				temp += template[i].atap[k];
463 464 465
			}

			aead_request_set_crypt(req, sg, sg,
466 467
					       template[i].ilen,
					       iv);
468

469
			aead_request_set_assoc(req, asg, template[i].alen);
470

H
Herbert Xu 已提交
471 472 473
			ret = enc ?
				crypto_aead_encrypt(req) :
				crypto_aead_decrypt(req);
474 475 476 477 478 479 480 481 482 483 484 485 486 487

			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:
488 489 490
				printk(KERN_ERR "alg: aead: %s failed on "
				       "chunk test %d for %s: ret=%d\n", e, j,
				       algo, -ret);
491 492 493
				goto out;
			}

494
			ret = -EINVAL;
495
			for (k = 0, temp = 0; k < template[i].np; k++) {
496 497
				q = xbuf[IDX[k] >> PAGE_SHIFT] +
				    offset_in_page(IDX[k]);
498 499 500 501

				n = template[i].tap[k];
				if (k == template[i].np - 1)
					n += enc ? authsize : -authsize;
502 503 504 505 506 507 508 509

				if (memcmp(q, template[i].result + temp, n)) {
					printk(KERN_ERR "alg: aead: Chunk "
					       "test %d failed on %s at page "
					       "%u for %s\n", j, e, k, algo);
					hexdump(q, n);
					goto out;
				}
510

511 512 513 514 515 516 517 518
				q += n;
				if (k == template[i].np - 1 && !enc) {
					if (memcmp(q, template[i].input +
						      temp + n, authsize))
						n = authsize;
					else
						n = 0;
				} else {
519 520
					for (n = 0; offset_in_page(q + n) &&
						    q[n]; n++)
521 522
						;
				}
523
				if (n) {
524 525 526 527 528
					printk(KERN_ERR "alg: aead: Result "
					       "buffer corruption in chunk "
					       "test %d on %s at page %u for "
					       "%s: %u bytes:\n", j, e, k,
					       algo, n);
529
					hexdump(q, n);
530
					goto out;
531 532
				}

533
				temp += template[i].tap[k];
534 535 536 537
			}
		}
	}

538 539
	ret = 0;

540 541 542
out:
	crypto_free_aead(tfm);
	aead_request_free(req);
543
	return ret;
544 545
}

546 547
static int test_cipher(char *algo, int enc,
		       struct cipher_testvec *template, unsigned int tcount)
L
Linus Torvalds 已提交
548
{
549 550
	unsigned int i, j, k, n, temp;
	int ret;
551
	char *q;
552 553
	struct crypto_ablkcipher *tfm;
	struct ablkcipher_request *req;
L
Linus Torvalds 已提交
554
	struct scatterlist sg[8];
555
	const char *e;
556
	struct tcrypt_result result;
557 558
	void *data;
	char iv[MAX_IVLEN];
L
Linus Torvalds 已提交
559 560

	if (enc == ENCRYPT)
561
	        e = "encryption";
L
Linus Torvalds 已提交
562
	else
563
		e = "decryption";
L
Linus Torvalds 已提交
564

565 566
	init_completion(&result.completion);
	tfm = crypto_alloc_ablkcipher(algo, 0, 0);
L
Linus Torvalds 已提交
567

568
	if (IS_ERR(tfm)) {
569 570 571
		printk(KERN_ERR "alg: cipher: Failed to load transform for "
		       "%s: %ld\n", algo, PTR_ERR(tfm));
		return PTR_ERR(tfm);
L
Linus Torvalds 已提交
572
	}
573 574 575

	req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
	if (!req) {
576 577 578
		printk(KERN_ERR "alg: cipher: Failed to allocate request for "
		       "%s\n", algo);
		ret = -ENOMEM;
579 580 581 582 583
		goto out;
	}

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

L
Linus Torvalds 已提交
585 586
	j = 0;
	for (i = 0; i < tcount; i++) {
587 588 589 590 591 592
		if (template[i].iv)
			memcpy(iv, template[i].iv, MAX_IVLEN);
		else
			memset(iv, 0, MAX_IVLEN);

		if (!(template[i].np)) {
593
			j++;
L
Linus Torvalds 已提交
594

595 596 597
			data = xbuf[0];
			memcpy(data, template[i].input, template[i].ilen);

598
			crypto_ablkcipher_clear_flags(tfm, ~0);
599
			if (template[i].wk)
600
				crypto_ablkcipher_set_flags(
601
					tfm, CRYPTO_TFM_REQ_WEAK_KEY);
602

603 604
			ret = crypto_ablkcipher_setkey(tfm, template[i].key,
						       template[i].klen);
605 606 607 608 609 610 611
			if (!ret == template[i].fail) {
				printk(KERN_ERR "alg: cipher: setkey failed "
				       "on test %d for %s: flags=%x\n", j,
				       algo, crypto_ablkcipher_get_flags(tfm));
				goto out;
			} else if (ret)
				continue;
L
Linus Torvalds 已提交
612

613
			sg_init_one(&sg[0], data, template[i].ilen);
614

615
			ablkcipher_request_set_crypt(req, sg, sg,
616
						     template[i].ilen, iv);
617
			ret = enc ?
618 619
				crypto_ablkcipher_encrypt(req) :
				crypto_ablkcipher_decrypt(req);
620

621 622 623 624 625 626 627 628 629 630 631 632 633
			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:
634 635 636
				printk(KERN_ERR "alg: cipher: %s failed on "
				       "test %d for %s: ret=%d\n", e, j, algo,
				       -ret);
L
Linus Torvalds 已提交
637
				goto out;
638 639
			}

640
			q = data;
641 642 643 644 645 646 647
			if (memcmp(q, template[i].result, template[i].rlen)) {
				printk(KERN_ERR "alg: cipher: Test %d failed "
				       "on %s for %s\n", j, e, algo);
				hexdump(q, template[i].rlen);
				ret = -EINVAL;
				goto out;
			}
L
Linus Torvalds 已提交
648 649
		}
	}
650

L
Linus Torvalds 已提交
651 652
	j = 0;
	for (i = 0; i < tcount; i++) {
653 654 655 656 657 658 659

		if (template[i].iv)
			memcpy(iv, template[i].iv, MAX_IVLEN);
		else
			memset(iv, 0, MAX_IVLEN);

		if (template[i].np) {
660
			j++;
L
Linus Torvalds 已提交
661

662
			crypto_ablkcipher_clear_flags(tfm, ~0);
663
			if (template[i].wk)
664
				crypto_ablkcipher_set_flags(
665
					tfm, CRYPTO_TFM_REQ_WEAK_KEY);
666

667 668
			ret = crypto_ablkcipher_setkey(tfm, template[i].key,
						       template[i].klen);
669 670 671 672 673 674 675 676
			if (!ret == template[i].fail) {
				printk(KERN_ERR "alg: cipher: setkey failed "
				       "on chunk test %d for %s: flags=%x\n",
				       j, algo,
				       crypto_ablkcipher_get_flags(tfm));
				goto out;
			} else if (ret)
				continue;
L
Linus Torvalds 已提交
677 678

			temp = 0;
679
			ret = -EINVAL;
680 681
			sg_init_table(sg, template[i].np);
			for (k = 0; k < template[i].np; k++) {
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
				if (WARN_ON(offset_in_page(IDX[k]) +
					    template[i].tap[k] > PAGE_SIZE))
					goto out;

				q = xbuf[IDX[k] >> PAGE_SHIFT] +
				    offset_in_page(IDX[k]);

				memcpy(q, template[i].input + temp,
				       template[i].tap[k]);

				if (offset_in_page(q) + template[i].tap[k] <
				    PAGE_SIZE)
					q[template[i].tap[k]] = 0;

				sg_set_buf(&sg[k], q, template[i].tap[k]);

698
				temp += template[i].tap[k];
L
Linus Torvalds 已提交
699
			}
700

701
			ablkcipher_request_set_crypt(req, sg, sg,
702
					template[i].ilen, iv);
703

704
			ret = enc ?
705 706
				crypto_ablkcipher_encrypt(req) :
				crypto_ablkcipher_decrypt(req);
707

708 709 710 711 712 713 714 715 716 717 718 719 720
			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:
721 722 723
				printk(KERN_ERR "alg: cipher: %s failed on "
				       "chunk test %d for %s: ret=%d\n", e, j,
				       algo, -ret);
L
Linus Torvalds 已提交
724 725 726 727
				goto out;
			}

			temp = 0;
728
			ret = -EINVAL;
729
			for (k = 0; k < template[i].np; k++) {
730 731
				q = xbuf[IDX[k] >> PAGE_SHIFT] +
				    offset_in_page(IDX[k]);
732 733 734 735 736 737 738 739 740

				if (memcmp(q, template[i].result + temp,
					   template[i].tap[k])) {
					printk(KERN_ERR "alg: cipher: Chunk "
					       "test %d failed on %s at page "
					       "%u for %s\n", j, e, k, algo);
					hexdump(q, template[i].tap[k]);
					goto out;
				}
741

742 743
				q += template[i].tap[k];
				for (n = 0; offset_in_page(q + n) && q[n]; n++)
744 745
					;
				if (n) {
746 747 748 749 750
					printk(KERN_ERR "alg: cipher: "
					       "Result buffer corruption in "
					       "chunk test %d on %s at page "
					       "%u for %s: %u bytes:\n", j, e,
					       k, algo, n);
751
					hexdump(q, n);
752
					goto out;
753
				}
754
				temp += template[i].tap[k];
L
Linus Torvalds 已提交
755 756 757
			}
		}
	}
758 759 760

	ret = 0;

L
Linus Torvalds 已提交
761
out:
762 763
	crypto_free_ablkcipher(tfm);
	ablkcipher_request_free(req);
764
	return ret;
L
Linus Torvalds 已提交
765 766
}

767 768
static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc,
			       struct scatterlist *sg, int blen, int sec)
769 770 771 772 773 774 775 776
{
	unsigned long start, end;
	int bcount;
	int ret;

	for (start = jiffies, end = start + sec * HZ, bcount = 0;
	     time_before(jiffies, end); bcount++) {
		if (enc)
777
			ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
778
		else
779
			ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
780 781 782 783 784 785 786 787 788 789

		if (ret)
			return ret;
	}

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

790 791
static int test_cipher_cycles(struct blkcipher_desc *desc, int enc,
			      struct scatterlist *sg, int blen)
792 793 794 795 796 797 798 799 800 801 802
{
	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)
803
			ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
804
		else
805
			ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
806 807 808 809 810 811 812 813 814 815 816

		if (ret)
			goto out;
	}

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

		start = get_cycles();
		if (enc)
817
			ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
818
		else
819
			ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
		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;
}

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

841
static void test_cipher_speed(char *algo, int enc, unsigned int sec,
842
			      struct cipher_testvec *template,
843
			      unsigned int tcount, u8 *keysize)
H
Harald Welte 已提交
844
{
845
	unsigned int ret, i, j, iv_len;
846
	unsigned char *key, iv[128];
847 848 849
	struct crypto_blkcipher *tfm;
	struct blkcipher_desc desc;
	const char *e;
850
	u32 *b_size;
H
Harald Welte 已提交
851 852 853 854 855 856

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

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

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

861 862 863
	if (IS_ERR(tfm)) {
		printk("failed to load transform for %s: %ld\n", algo,
		       PTR_ERR(tfm));
H
Harald Welte 已提交
864 865
		return;
	}
866 867
	desc.tfm = tfm;
	desc.flags = 0;
H
Harald Welte 已提交
868

869 870
	i = 0;
	do {
H
Harald Welte 已提交
871

872 873
		b_size = block_sizes;
		do {
874
			struct scatterlist sg[TVMEMSIZE];
H
Harald Welte 已提交
875

876 877 878 879
			if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) {
				printk("template (%u) too big for "
				       "tvmem (%lu)\n", *keysize + *b_size,
				       TVMEMSIZE * PAGE_SIZE);
880 881
				goto out;
			}
H
Harald Welte 已提交
882

883 884 885
			printk("test %u (%d bit key, %d byte blocks): ", i,
					*keysize * 8, *b_size);

886
			memset(tvmem[0], 0xff, PAGE_SIZE);
887 888

			/* set key, plain text and IV */
889
			key = (unsigned char *)tvmem[0];
890 891 892 893 894
			for (j = 0; j < tcount; j++) {
				if (template[j].klen == *keysize) {
					key = template[j].key;
					break;
				}
895
			}
H
Harald Welte 已提交
896

897 898 899 900 901 902
			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 已提交
903

904 905 906 907 908 909 910 911
			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);
			}

912 913 914 915 916
			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 已提交
917

918
			if (sec)
919 920
				ret = test_cipher_jiffies(&desc, enc, sg,
							  *b_size, sec);
921
			else
922 923
				ret = test_cipher_cycles(&desc, enc, sg,
							 *b_size);
H
Harald Welte 已提交
924

925 926 927 928 929 930 931 932 933
			if (ret) {
				printk("%s() failed flags=%x\n", e, desc.flags);
				break;
			}
			b_size++;
			i++;
		} while (*b_size);
		keysize++;
	} while (*keysize);
H
Harald Welte 已提交
934 935

out:
936
	crypto_free_blkcipher(tfm);
H
Harald Welte 已提交
937 938
}

939 940
static int test_hash_jiffies_digest(struct hash_desc *desc,
				    struct scatterlist *sg, int blen,
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
				    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;
}

960 961
static int test_hash_jiffies(struct hash_desc *desc, struct scatterlist *sg,
			     int blen, int plen, char *out, int sec)
962 963 964
{
	unsigned long start, end;
	int bcount, pcount;
965 966 967
	int ret;

	if (plen == blen)
968
		return test_hash_jiffies_digest(desc, sg, blen, out, sec);
969

970 971
	for (start = jiffies, end = start + sec * HZ, bcount = 0;
	     time_before(jiffies, end); bcount++) {
972 973 974
		ret = crypto_hash_init(desc);
		if (ret)
			return ret;
975
		for (pcount = 0; pcount < blen; pcount += plen) {
976 977 978
			ret = crypto_hash_update(desc, sg, plen);
			if (ret)
				return ret;
979 980
		}
		/* we assume there is enough space in 'out' for the result */
981 982 983
		ret = crypto_hash_final(desc, out);
		if (ret)
			return ret;
984 985 986 987 988
	}

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

989 990 991
	return 0;
}

992 993
static int test_hash_cycles_digest(struct hash_desc *desc,
				   struct scatterlist *sg, int blen, char *out)
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
{
	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;
1035 1036
}

1037 1038
static int test_hash_cycles(struct hash_desc *desc, struct scatterlist *sg,
			    int blen, int plen, char *out)
1039 1040 1041
{
	unsigned long cycles = 0;
	int i, pcount;
1042 1043 1044
	int ret;

	if (plen == blen)
1045
		return test_hash_cycles_digest(desc, sg, blen, out);
1046

1047 1048 1049 1050 1051
	local_bh_disable();
	local_irq_disable();

	/* Warm-up run. */
	for (i = 0; i < 4; i++) {
1052 1053 1054
		ret = crypto_hash_init(desc);
		if (ret)
			goto out;
1055
		for (pcount = 0; pcount < blen; pcount += plen) {
1056 1057 1058
			ret = crypto_hash_update(desc, sg, plen);
			if (ret)
				goto out;
1059
		}
1060
		ret = crypto_hash_final(desc, out);
1061 1062
		if (ret)
			goto out;
1063 1064 1065 1066 1067 1068 1069 1070
	}

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

		start = get_cycles();

1071 1072 1073
		ret = crypto_hash_init(desc);
		if (ret)
			goto out;
1074
		for (pcount = 0; pcount < blen; pcount += plen) {
1075 1076 1077
			ret = crypto_hash_update(desc, sg, plen);
			if (ret)
				goto out;
1078
		}
1079 1080 1081
		ret = crypto_hash_final(desc, out);
		if (ret)
			goto out;
1082 1083 1084 1085 1086 1087

		end = get_cycles();

		cycles += end - start;
	}

1088
out:
1089 1090 1091
	local_irq_enable();
	local_bh_enable();

1092 1093 1094
	if (ret)
		return ret;

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

1098
	return 0;
1099 1100
}

1101 1102
static void test_hash_speed(char *algo, unsigned int sec,
			      struct hash_speed *speed)
1103
{
1104
	struct scatterlist sg[TVMEMSIZE];
1105 1106
	struct crypto_hash *tfm;
	struct hash_desc desc;
1107 1108
	char output[1024];
	int i;
1109
	int ret;
1110 1111 1112

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

1113
	tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
1114

1115 1116 1117
	if (IS_ERR(tfm)) {
		printk("failed to load transform for %s: %ld\n", algo,
		       PTR_ERR(tfm));
1118 1119 1120
		return;
	}

1121 1122 1123 1124
	desc.tfm = tfm;
	desc.flags = 0;

	if (crypto_hash_digestsize(tfm) > sizeof(output)) {
1125
		printk("digestsize(%u) > outputbuffer(%zu)\n",
1126
		       crypto_hash_digestsize(tfm), sizeof(output));
1127 1128 1129
		goto out;
	}

1130 1131 1132 1133 1134 1135
	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);
	}

1136
	for (i = 0; speed[i].blen != 0; i++) {
1137 1138 1139
		if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) {
			printk("template (%u) too big for tvmem (%lu)\n",
			       speed[i].blen, TVMEMSIZE * PAGE_SIZE);
1140 1141 1142 1143 1144 1145 1146
			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)
1147
			ret = test_hash_jiffies(&desc, sg, speed[i].blen,
1148
						speed[i].plen, output, sec);
1149
		else
1150
			ret = test_hash_cycles(&desc, sg, speed[i].blen,
1151 1152 1153 1154 1155 1156
					       speed[i].plen, output);

		if (ret) {
			printk("hashing failed ret=%d\n", ret);
			break;
		}
1157 1158 1159
	}

out:
1160
	crypto_free_hash(tfm);
1161 1162
}

1163 1164
static int test_comp(char *algo, struct comp_testvec *ctemplate,
		     struct comp_testvec *dtemplate, int ctcount, int dtcount)
L
Linus Torvalds 已提交
1165 1166 1167
{
	unsigned int i;
	char result[COMP_BUF_SIZE];
1168
	struct crypto_comp *tfm;
1169
	int ret;
L
Linus Torvalds 已提交
1170

1171
	tfm = crypto_alloc_comp(algo, 0, CRYPTO_ALG_ASYNC);
1172
	if (IS_ERR(tfm)) {
1173 1174 1175
		printk(KERN_ERR "alg: comp: Failed to load transform for %s: "
		       "%ld\n", algo, PTR_ERR(tfm));
		return PTR_ERR(tfm);
L
Linus Torvalds 已提交
1176 1177
	}

1178
	for (i = 0; i < ctcount; i++) {
1179
		int ilen, dlen = COMP_BUF_SIZE;
1180

L
Linus Torvalds 已提交
1181 1182
		memset(result, 0, sizeof (result));

1183 1184
		ilen = ctemplate[i].inlen;
		ret = crypto_comp_compress(tfm, ctemplate[i].input,
L
Linus Torvalds 已提交
1185 1186
		                           ilen, result, &dlen);
		if (ret) {
1187 1188 1189 1190
			printk(KERN_ERR "alg: comp: compression failed "
			       "on test %d for %s: ret=%d\n", i + 1, algo,
			       -ret);
			goto out;
L
Linus Torvalds 已提交
1191 1192
		}

1193 1194 1195 1196 1197 1198 1199 1200
		if (memcmp(result, ctemplate[i].output, dlen)) {
			printk(KERN_ERR "alg: comp: Compression test %d "
			       "failed for %s\n", i + 1, algo);
			hexdump(result, dlen);
			ret = -EINVAL;
			goto out;
		}
	}
L
Linus Torvalds 已提交
1201

1202
	for (i = 0; i < dtcount; i++) {
L
Linus Torvalds 已提交
1203
		int ilen, ret, dlen = COMP_BUF_SIZE;
1204

L
Linus Torvalds 已提交
1205 1206
		memset(result, 0, sizeof (result));

1207 1208
		ilen = dtemplate[i].inlen;
		ret = crypto_comp_decompress(tfm, dtemplate[i].input,
L
Linus Torvalds 已提交
1209 1210
		                             ilen, result, &dlen);
		if (ret) {
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
			printk(KERN_ERR "alg: comp: decompression failed "
			       "on test %d for %s: ret=%d\n", i + 1, algo,
			       -ret);
			goto out;
		}

		if (memcmp(result, dtemplate[i].output, dlen)) {
			printk(KERN_ERR "alg: comp: Decompression test %d "
			       "failed for %s\n", i + 1, algo);
			hexdump(result, dlen);
			ret = -EINVAL;
			goto out;
L
Linus Torvalds 已提交
1223 1224
		}
	}
1225

1226 1227 1228
	ret = 0;

out:
1229
	crypto_free_comp(tfm);
1230
	return ret;
L
Linus Torvalds 已提交
1231 1232
}

1233
static void test_available(void)
L
Linus Torvalds 已提交
1234 1235
{
	char **name = check;
1236

L
Linus Torvalds 已提交
1237 1238
	while (*name) {
		printk("alg %s ", *name);
1239
		printk(crypto_has_alg(*name, 0, 0) ?
1240
		       "found\n" : "not found\n");
L
Linus Torvalds 已提交
1241
		name++;
1242
	}
L
Linus Torvalds 已提交
1243 1244
}

1245
static void do_test(void)
L
Linus Torvalds 已提交
1246 1247 1248 1249 1250
{
	switch (mode) {

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

L
Linus Torvalds 已提交
1252
		test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
1253

L
Linus Torvalds 已提交
1254
		//DES
1255 1256 1257 1258 1259 1260 1261 1262
		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);
1263

L
Linus Torvalds 已提交
1264
		//DES3_EDE
1265 1266 1267 1268
		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);
1269

1270 1271 1272 1273 1274 1275 1276 1277
		test_cipher("cbc(des3_ede)", ENCRYPT,
			    des3_ede_cbc_enc_tv_template,
			    DES3_EDE_CBC_ENC_TEST_VECTORS);

		test_cipher("cbc(des3_ede)", DECRYPT,
			    des3_ede_cbc_dec_tv_template,
			    DES3_EDE_CBC_DEC_TEST_VECTORS);

L
Linus Torvalds 已提交
1278
		test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
1279

1280 1281
		test_hash("sha224", sha224_tv_template, SHA224_TEST_VECTORS);

L
Linus Torvalds 已提交
1282
		test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
1283

L
Linus Torvalds 已提交
1284
		//BLOWFISH
1285 1286 1287 1288 1289 1290 1291 1292
		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);
1293

L
Linus Torvalds 已提交
1294
		//TWOFISH
1295 1296 1297 1298 1299 1300 1301 1302
		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);
1303

L
Linus Torvalds 已提交
1304
		//SERPENT
1305 1306 1307 1308
		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);
1309

L
Linus Torvalds 已提交
1310
		//TNEPRES
1311 1312 1313 1314
		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 已提交
1315 1316

		//AES
1317 1318 1319 1320 1321 1322 1323 1324
		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 已提交
1325 1326 1327 1328
		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);
1329 1330 1331 1332
		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);
1333
		test_cipher("rfc3686(ctr(aes))", ENCRYPT, aes_ctr_enc_tv_template,
1334
			    AES_CTR_ENC_TEST_VECTORS);
1335
		test_cipher("rfc3686(ctr(aes))", DECRYPT, aes_ctr_dec_tv_template,
1336
			    AES_CTR_DEC_TEST_VECTORS);
M
Mikko Herranen 已提交
1337 1338 1339 1340
		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);
J
Joy Latten 已提交
1341 1342 1343 1344
		test_aead("ccm(aes)", ENCRYPT, aes_ccm_enc_tv_template,
			  AES_CCM_ENC_TEST_VECTORS);
		test_aead("ccm(aes)", DECRYPT, aes_ccm_dec_tv_template,
			  AES_CCM_DEC_TEST_VECTORS);
L
Linus Torvalds 已提交
1345 1346

		//CAST5
1347 1348 1349 1350
		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);
1351

L
Linus Torvalds 已提交
1352
		//CAST6
1353 1354 1355 1356
		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 已提交
1357 1358

		//ARC4
1359 1360 1361 1362
		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 已提交
1363 1364

		//TEA
1365 1366 1367 1368
		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 已提交
1369 1370 1371


		//XTEA
1372 1373 1374 1375
		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 已提交
1376 1377

		//KHAZAD
1378 1379 1380 1381
		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 已提交
1382 1383

		//ANUBIS
1384 1385 1386 1387 1388 1389 1390 1391
		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 已提交
1392

A
Aaron Grothe 已提交
1393
		//XETA
1394 1395 1396 1397
		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 已提交
1398

1399 1400 1401 1402 1403 1404
		//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);

1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
		//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);

1419 1420 1421 1422 1423 1424
		//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);

1425 1426 1427 1428 1429 1430
		//CTS
		test_cipher("cts(cbc(aes))", ENCRYPT, cts_mode_enc_tv_template,
			    CTS_MODE_ENC_TEST_VECTORS);
		test_cipher("cts(cbc(aes))", DECRYPT, cts_mode_dec_tv_template,
			    CTS_MODE_DEC_TEST_VECTORS);

L
Linus Torvalds 已提交
1431 1432 1433 1434 1435 1436 1437 1438
		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);
1439 1440 1441
		test_comp("deflate", deflate_comp_tv_template,
			  deflate_decomp_tv_template, DEFLATE_COMP_TEST_VECTORS,
			  DEFLATE_DECOMP_TEST_VECTORS);
1442 1443
		test_comp("lzo", lzo_comp_tv_template, lzo_decomp_tv_template,
			  LZO_COMP_TEST_VECTORS, LZO_DECOMP_TEST_VECTORS);
1444
		test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
1445 1446 1447 1448
		test_hash("hmac(md5)", hmac_md5_tv_template,
			  HMAC_MD5_TEST_VECTORS);
		test_hash("hmac(sha1)", hmac_sha1_tv_template,
			  HMAC_SHA1_TEST_VECTORS);
1449 1450
		test_hash("hmac(sha224)", hmac_sha224_tv_template,
			  HMAC_SHA224_TEST_VECTORS);
1451 1452
		test_hash("hmac(sha256)", hmac_sha256_tv_template,
			  HMAC_SHA256_TEST_VECTORS);
1453 1454 1455 1456
		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 已提交
1457

1458 1459 1460
		test_hash("xcbc(aes)", aes_xcbc128_tv_template,
			  XCBC_AES_TEST_VECTORS);

L
Linus Torvalds 已提交
1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
		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:
1473 1474 1475 1476 1477 1478 1479 1480
		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 已提交
1481 1482 1483
		break;

	case 4:
1484 1485 1486 1487
		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);
1488 1489 1490 1491 1492 1493 1494 1495

		test_cipher("cbc(des3_ede)", ENCRYPT,
			    des3_ede_cbc_enc_tv_template,
			    DES3_EDE_CBC_ENC_TEST_VECTORS);

		test_cipher("cbc(des3_ede)", DECRYPT,
			    des3_ede_cbc_dec_tv_template,
			    DES3_EDE_CBC_DEC_TEST_VECTORS);
L
Linus Torvalds 已提交
1496 1497 1498 1499 1500
		break;

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

L
Linus Torvalds 已提交
1502 1503 1504
	case 6:
		test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
		break;
1505

L
Linus Torvalds 已提交
1506
	case 7:
1507 1508 1509 1510 1511 1512 1513 1514
		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 已提交
1515 1516 1517
		break;

	case 8:
1518 1519 1520 1521 1522 1523 1524 1525
		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 已提交
1526
		break;
1527

L
Linus Torvalds 已提交
1528
	case 9:
1529 1530 1531 1532
		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 已提交
1533 1534 1535
		break;

	case 10:
1536 1537 1538 1539 1540 1541 1542 1543
		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 已提交
1544 1545 1546 1547
		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);
1548 1549 1550 1551
		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);
1552
		test_cipher("rfc3686(ctr(aes))", ENCRYPT, aes_ctr_enc_tv_template,
1553
			    AES_CTR_ENC_TEST_VECTORS);
1554
		test_cipher("rfc3686(ctr(aes))", DECRYPT, aes_ctr_dec_tv_template,
1555
			    AES_CTR_DEC_TEST_VECTORS);
L
Linus Torvalds 已提交
1556 1557 1558 1559 1560
		break;

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

L
Linus Torvalds 已提交
1562 1563 1564 1565 1566
	case 12:
		test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
		break;

	case 13:
1567 1568 1569
		test_comp("deflate", deflate_comp_tv_template,
			  deflate_decomp_tv_template, DEFLATE_COMP_TEST_VECTORS,
			  DEFLATE_DECOMP_TEST_VECTORS);
L
Linus Torvalds 已提交
1570 1571 1572
		break;

	case 14:
1573 1574 1575 1576
		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 已提交
1577 1578 1579
		break;

	case 15:
1580 1581 1582 1583
		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 已提交
1584 1585 1586
		break;

	case 16:
1587 1588 1589 1590
		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 已提交
1591 1592 1593 1594 1595 1596 1597
		break;

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

	case 18:
1598
		test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
L
Linus Torvalds 已提交
1599 1600 1601
		break;

	case 19:
1602 1603 1604 1605
		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 已提交
1606 1607 1608
		break;

	case 20:
1609 1610 1611 1612
		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 已提交
1613 1614 1615
		break;

	case 21:
1616 1617 1618 1619
		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 已提交
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
		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:
1635 1636 1637 1638
		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 已提交
1639 1640 1641
		break;

	case 26:
1642 1643 1644 1645 1646 1647 1648 1649
		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 已提交
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
		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;
1664

A
Aaron Grothe 已提交
1665
	case 30:
1666 1667 1668 1669
		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 已提交
1670
		break;
L
Linus Torvalds 已提交
1671

1672 1673 1674 1675 1676 1677 1678
	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;

1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692
	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;
1693 1694 1695
	case 33:
		test_hash("sha224", sha224_tv_template, SHA224_TEST_VECTORS);
		break;
1696

1697 1698 1699 1700 1701 1702
	case 34:
		test_cipher("salsa20", ENCRYPT,
			    salsa20_stream_enc_tv_template,
			    SALSA20_STREAM_ENC_TEST_VECTORS);
		break;

1703 1704 1705 1706 1707 1708 1709
	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;

1710 1711 1712 1713 1714
	case 36:
		test_comp("lzo", lzo_comp_tv_template, lzo_decomp_tv_template,
			  LZO_COMP_TEST_VECTORS, LZO_DECOMP_TEST_VECTORS);
		break;

J
Joy Latten 已提交
1715 1716 1717 1718 1719 1720 1721
	case 37:
		test_aead("ccm(aes)", ENCRYPT, aes_ccm_enc_tv_template,
			  AES_CCM_ENC_TEST_VECTORS);
		test_aead("ccm(aes)", DECRYPT, aes_ccm_dec_tv_template,
			  AES_CCM_DEC_TEST_VECTORS);
		break;

1722 1723 1724 1725 1726 1727 1728
	case 38:
		test_cipher("cts(cbc(aes))", ENCRYPT, cts_mode_enc_tv_template,
			    CTS_MODE_ENC_TEST_VECTORS);
		test_cipher("cts(cbc(aes))", DECRYPT, cts_mode_dec_tv_template,
			    CTS_MODE_DEC_TEST_VECTORS);
		break;

1729 1730 1731 1732 1733 1734 1735 1736
        case 39:
		test_hash("rmd128", rmd128_tv_template, RMD128_TEST_VECTORS);
		break;

        case 40:
		test_hash("rmd160", rmd160_tv_template, RMD160_TEST_VECTORS);
		break;

1737 1738 1739 1740 1741 1742 1743 1744
	case 41:
		test_hash("rmd256", rmd256_tv_template, RMD256_TEST_VECTORS);
		break;

	case 42:
		test_hash("rmd320", rmd320_tv_template, RMD320_TEST_VECTORS);
		break;

L
Linus Torvalds 已提交
1745
	case 100:
1746 1747
		test_hash("hmac(md5)", hmac_md5_tv_template,
			  HMAC_MD5_TEST_VECTORS);
L
Linus Torvalds 已提交
1748
		break;
1749

L
Linus Torvalds 已提交
1750
	case 101:
1751 1752
		test_hash("hmac(sha1)", hmac_sha1_tv_template,
			  HMAC_SHA1_TEST_VECTORS);
L
Linus Torvalds 已提交
1753
		break;
1754

L
Linus Torvalds 已提交
1755
	case 102:
1756 1757
		test_hash("hmac(sha256)", hmac_sha256_tv_template,
			  HMAC_SHA256_TEST_VECTORS);
L
Linus Torvalds 已提交
1758 1759
		break;

1760 1761 1762 1763 1764 1765 1766 1767 1768
	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;
1769

1770 1771 1772 1773
	case 105:
		test_hash("hmac(sha224)", hmac_sha224_tv_template,
			  HMAC_SHA224_TEST_VECTORS);
		break;
L
Linus Torvalds 已提交
1774

1775 1776 1777 1778 1779
	case 106:
		test_hash("xcbc(aes)", aes_xcbc128_tv_template,
			  XCBC_AES_TEST_VECTORS);
		break;

1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
	case 107:
		test_hash("hmac(rmd128)", hmac_rmd128_tv_template,
			  HMAC_RMD128_TEST_VECTORS);
		break;

	case 108:
		test_hash("hmac(rmd160)", hmac_rmd160_tv_template,
			  HMAC_RMD160_TEST_VECTORS);
		break;

H
Harald Welte 已提交
1790
	case 200:
1791
		test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
1792
				speed_template_16_24_32);
1793
		test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
1794
				speed_template_16_24_32);
1795
		test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
1796
				speed_template_16_24_32);
1797
		test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
1798
				speed_template_16_24_32);
R
Rik Snel 已提交
1799
		test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
1800
				speed_template_32_40_48);
R
Rik Snel 已提交
1801
		test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
1802
				speed_template_32_40_48);
1803
		test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
1804
				speed_template_32_48_64);
1805
		test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
1806
				speed_template_32_48_64);
H
Harald Welte 已提交
1807 1808 1809
		break;

	case 201:
1810
		test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
1811 1812
				des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS,
				speed_template_24);
1813
		test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
1814 1815
				des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS,
				speed_template_24);
1816
		test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
1817 1818
				des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS,
				speed_template_24);
1819
		test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
1820 1821
				des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS,
				speed_template_24);
H
Harald Welte 已提交
1822 1823 1824
		break;

	case 202:
1825
		test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
1826
				speed_template_16_24_32);
1827
		test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
1828
				speed_template_16_24_32);
1829
		test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
1830
				speed_template_16_24_32);
1831
		test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
1832
				speed_template_16_24_32);
H
Harald Welte 已提交
1833 1834 1835
		break;

	case 203:
1836
		test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
1837
				  speed_template_8_32);
1838
		test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
1839
				  speed_template_8_32);
1840
		test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
1841
				  speed_template_8_32);
1842
		test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
1843
				  speed_template_8_32);
H
Harald Welte 已提交
1844 1845 1846
		break;

	case 204:
1847
		test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
1848
				  speed_template_8);
1849
		test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
1850
				  speed_template_8);
1851
		test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
1852
				  speed_template_8);
1853
		test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
1854
				  speed_template_8);
H
Harald Welte 已提交
1855 1856
		break;

1857 1858
	case 205:
		test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
1859
				speed_template_16_24_32);
1860
		test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
1861
				speed_template_16_24_32);
1862
		test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
1863
				speed_template_16_24_32);
1864
		test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
1865
				speed_template_16_24_32);
1866 1867
		break;

1868 1869
	case 206:
		test_cipher_speed("salsa20", ENCRYPT, sec, NULL, 0,
1870
				  speed_template_16_32);
1871 1872
		break;

1873 1874 1875 1876
	case 300:
		/* fall through */

	case 301:
1877
		test_hash_speed("md4", sec, generic_hash_speed_template);
1878 1879 1880
		if (mode > 300 && mode < 400) break;

	case 302:
1881
		test_hash_speed("md5", sec, generic_hash_speed_template);
1882 1883 1884
		if (mode > 300 && mode < 400) break;

	case 303:
1885
		test_hash_speed("sha1", sec, generic_hash_speed_template);
1886 1887 1888
		if (mode > 300 && mode < 400) break;

	case 304:
1889
		test_hash_speed("sha256", sec, generic_hash_speed_template);
1890 1891 1892
		if (mode > 300 && mode < 400) break;

	case 305:
1893
		test_hash_speed("sha384", sec, generic_hash_speed_template);
1894 1895 1896
		if (mode > 300 && mode < 400) break;

	case 306:
1897
		test_hash_speed("sha512", sec, generic_hash_speed_template);
1898 1899 1900
		if (mode > 300 && mode < 400) break;

	case 307:
1901
		test_hash_speed("wp256", sec, generic_hash_speed_template);
1902 1903 1904
		if (mode > 300 && mode < 400) break;

	case 308:
1905
		test_hash_speed("wp384", sec, generic_hash_speed_template);
1906 1907 1908
		if (mode > 300 && mode < 400) break;

	case 309:
1909
		test_hash_speed("wp512", sec, generic_hash_speed_template);
1910 1911 1912
		if (mode > 300 && mode < 400) break;

	case 310:
1913
		test_hash_speed("tgr128", sec, generic_hash_speed_template);
1914 1915 1916
		if (mode > 300 && mode < 400) break;

	case 311:
1917
		test_hash_speed("tgr160", sec, generic_hash_speed_template);
1918 1919 1920
		if (mode > 300 && mode < 400) break;

	case 312:
1921
		test_hash_speed("tgr192", sec, generic_hash_speed_template);
1922 1923
		if (mode > 300 && mode < 400) break;

1924 1925 1926 1927
	case 313:
		test_hash_speed("sha224", sec, generic_hash_speed_template);
		if (mode > 300 && mode < 400) break;

1928 1929 1930 1931 1932 1933 1934 1935
	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;

1936 1937 1938 1939 1940 1941 1942 1943
	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;

1944 1945 1946
	case 399:
		break;

L
Linus Torvalds 已提交
1947 1948 1949
	case 1000:
		test_available();
		break;
1950

L
Linus Torvalds 已提交
1951 1952 1953 1954 1955 1956 1957
	default:
		/* useful for debugging */
		printk("not testing anything\n");
		break;
	}
}

1958
static int __init tcrypt_mod_init(void)
L
Linus Torvalds 已提交
1959
{
1960
	int err = -ENOMEM;
1961
	int i;
1962

1963 1964 1965 1966 1967
	for (i = 0; i < TVMEMSIZE; i++) {
		tvmem[i] = (void *)__get_free_page(GFP_KERNEL);
		if (!tvmem[i])
			goto err_free_tv;
	}
L
Linus Torvalds 已提交
1968

1969 1970 1971 1972 1973
	for (i = 0; i < XBUFSIZE; i++) {
		xbuf[i] = (void *)__get_free_page(GFP_KERNEL);
		if (!xbuf[i])
			goto err_free_xbuf;
	}
L
Linus Torvalds 已提交
1974

1975 1976 1977 1978 1979
	for (i = 0; i < XBUFSIZE; i++) {
		axbuf[i] = (void *)__get_free_page(GFP_KERNEL);
		if (!axbuf[i])
			goto err_free_axbuf;
	}
L
Linus Torvalds 已提交
1980

1981
	do_test();
1982 1983 1984 1985 1986 1987 1988

	/* 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
	 */
1989 1990
	err = -EAGAIN;

1991 1992 1993 1994 1995 1996 1997 1998 1999
err_free_axbuf:
	for (i = 0; i < XBUFSIZE && axbuf[i]; i++)
		free_page((unsigned long)axbuf[i]);
err_free_xbuf:
	for (i = 0; i < XBUFSIZE && xbuf[i]; i++)
		free_page((unsigned long)xbuf[i]);
err_free_tv:
	for (i = 0; i < TVMEMSIZE && tvmem[i]; i++)
		free_page((unsigned long)tvmem[i]);
2000 2001

	return err;
L
Linus Torvalds 已提交
2002 2003 2004 2005 2006 2007
}

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

2010 2011
module_init(tcrypt_mod_init);
module_exit(tcrypt_mod_fini);
L
Linus Torvalds 已提交
2012 2013

module_param(mode, int, 0);
H
Harald Welte 已提交
2014
module_param(sec, uint, 0);
2015 2016
MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
		      "(defaults to zero which uses CPU cycles instead)");
L
Linus Torvalds 已提交
2017 2018 2019 2020

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