tcrypt.c 46.3 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
	"khazad", "wp512", "wp384", "wp256", "tnepres", "xeta",  "fcrypt",
86
	"camellia", "seed", "salsa20", "lzo", "cts", NULL
L
Linus Torvalds 已提交
87 88
};

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

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

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

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

120 121 122 123
	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 已提交
124 125 126
		return;
	}

127 128 129
	desc.tfm = tfm;
	desc.flags = 0;

L
Linus Torvalds 已提交
130
	for (i = 0; i < tcount; i++) {
131 132
		printk("test %u:\n", i + 1);
		memset(result, 0, 64);
L
Linus Torvalds 已提交
133

134 135 136
		hash_buff = kzalloc(template[i].psize, GFP_KERNEL);
		if (!hash_buff)
			continue;
L
Linus Torvalds 已提交
137

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

		if (template[i].ksize) {
			ret = crypto_hash_setkey(tfm, template[i].key,
						 template[i].ksize);
144 145
			if (ret) {
				printk("setkey() failed ret=%d\n", ret);
146
				kfree(hash_buff);
147 148 149 150
				goto out;
			}
		}

151
		ret = crypto_hash_digest(&desc, sg, template[i].psize, result);
152 153
		if (ret) {
			printk("digest () failed ret=%d\n", ret);
154
			kfree(hash_buff);
155 156
			goto out;
		}
L
Linus Torvalds 已提交
157

158
		hexdump(result, crypto_hash_digestsize(tfm));
L
Linus Torvalds 已提交
159
		printk("%s\n",
160
		       memcmp(result, template[i].digest,
161
			      crypto_hash_digestsize(tfm)) ?
162
		       "fail" : "pass");
163
		kfree(hash_buff);
L
Linus Torvalds 已提交
164 165
	}

166
	printk("testing %s across pages\n", algo);
L
Linus Torvalds 已提交
167 168

	/* setup the dummy buffer first */
169
	memset(xbuf, 0, XBUFSIZE);
L
Linus Torvalds 已提交
170 171 172

	j = 0;
	for (i = 0; i < tcount; i++) {
173
		if (template[i].np) {
L
Linus Torvalds 已提交
174
			j++;
175 176
			printk("test %u:\n", j);
			memset(result, 0, 64);
L
Linus Torvalds 已提交
177 178

			temp = 0;
179 180
			sg_init_table(sg, template[i].np);
			for (k = 0; k < template[i].np; k++) {
181
				memcpy(&xbuf[IDX[k]],
182 183 184
				       template[i].plaintext + temp,
				       template[i].tap[k]);
				temp += template[i].tap[k];
185
				sg_set_buf(&sg[k], &xbuf[IDX[k]],
186
					    template[i].tap[k]);
L
Linus Torvalds 已提交
187 188
			}

189 190 191
			if (template[i].ksize) {
				ret = crypto_hash_setkey(tfm, template[i].key,
							 template[i].ksize);
192

193 194 195 196
				if (ret) {
					printk("setkey() failed ret=%d\n", ret);
					goto out;
				}
L
Linus Torvalds 已提交
197 198
			}

199
			ret = crypto_hash_digest(&desc, sg, template[i].psize,
200 201 202 203 204
						 result);
			if (ret) {
				printk("digest () failed ret=%d\n", ret);
				goto out;
			}
205

206
			hexdump(result, crypto_hash_digestsize(tfm));
L
Linus Torvalds 已提交
207
			printk("%s\n",
208
			       memcmp(result, template[i].digest,
209
				      crypto_hash_digestsize(tfm)) ?
210
			       "fail" : "pass");
L
Linus Torvalds 已提交
211 212
		}
	}
213

L
Linus Torvalds 已提交
214
out:
215
	crypto_free_hash(tfm);
L
Linus Torvalds 已提交
216 217
}

218 219 220 221 222 223 224 225 226 227 228 229
static void test_aead(char *algo, int enc, struct aead_testvec *template,
		      unsigned int tcount)
{
	unsigned int ret, i, j, k, temp;
	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 已提交
230
	unsigned int authsize;
231 232 233
	void *input;
	void *assoc;
	char iv[MAX_IVLEN];
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261

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

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

	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;
	}

	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++) {
262
		if (!template[i].np) {
263
			printk(KERN_INFO "test %u (%d bit key):\n",
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
			       ++j, template[i].klen * 8);

			/* some tepmplates have no input data but they will
			 * touch input
			 */
			input = kzalloc(template[i].ilen + template[i].rlen, GFP_KERNEL);
			if (!input)
				continue;

			assoc = kzalloc(template[i].alen, GFP_KERNEL);
			if (!assoc) {
				kfree(input);
				continue;
			}

			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);
285 286

			crypto_aead_clear_flags(tfm, ~0);
287
			if (template[i].wk)
288 289
				crypto_aead_set_flags(
					tfm, CRYPTO_TFM_REQ_WEAK_KEY);
290 291 292 293 294

			if (template[i].key)
				key = template[i].key;
			else
				key = kzalloc(template[i].klen, GFP_KERNEL);
295 296

			ret = crypto_aead_setkey(tfm, key,
297
						 template[i].klen);
298 299 300 301
			if (ret) {
				printk(KERN_INFO "setkey() failed flags=%x\n",
				       crypto_aead_get_flags(tfm));

302 303
				if (!template[i].fail)
					goto next_one;
304 305
			}

306
			authsize = abs(template[i].rlen - template[i].ilen);
J
Joy Latten 已提交
307 308 309 310 311
			ret = crypto_aead_setauthsize(tfm, authsize);
			if (ret) {
				printk(KERN_INFO
				       "failed to set authsize = %u\n",
				       authsize);
312
				goto next_one;
J
Joy Latten 已提交
313 314
			}

315 316
			sg_init_one(&sg[0], input,
				    template[i].ilen + (enc ? authsize : 0));
317

318
			sg_init_one(&asg[0], assoc, template[i].alen);
319 320

			aead_request_set_crypt(req, sg, sg,
321
					       template[i].ilen, iv);
322

323
			aead_request_set_assoc(req, asg, template[i].alen);
324

H
Herbert Xu 已提交
325 326 327
			ret = enc ?
				crypto_aead_encrypt(req) :
				crypto_aead_decrypt(req);
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

			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);
344
				goto next_one;
345 346 347
			}

			q = kmap(sg_page(&sg[0])) + sg[0].offset;
348
			hexdump(q, template[i].rlen);
349 350

			printk(KERN_INFO "enc/dec: %s\n",
351 352 353 354 355 356 357 358
			       memcmp(q, template[i].result,
				      template[i].rlen) ? "fail" : "pass");
			kunmap(sg_page(&sg[0]));
next_one:
			if (!template[i].key)
				kfree(key);
			kfree(assoc);
			kfree(input);
359 360 361 362 363
		}
	}

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

	for (i = 0, j = 0; i < tcount; i++) {
367
		if (template[i].np) {
368
			printk(KERN_INFO "test %u (%d bit key):\n",
369 370 371 372 373 374
			       ++j, template[i].klen * 8);

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

			crypto_aead_clear_flags(tfm, ~0);
377
			if (template[i].wk)
378 379
				crypto_aead_set_flags(
					tfm, CRYPTO_TFM_REQ_WEAK_KEY);
380
			key = template[i].key;
381

382
			ret = crypto_aead_setkey(tfm, key, template[i].klen);
383 384 385 386
			if (ret) {
				printk(KERN_INFO "setkey() failed flags=%x\n",
				       crypto_aead_get_flags(tfm));

387
				if (!template[i].fail)
388 389 390
					goto out;
			}

391 392
			sg_init_table(sg, template[i].np);
			for (k = 0, temp = 0; k < template[i].np; k++) {
393
				memcpy(&xbuf[IDX[k]],
394 395 396
				       template[i].input + temp,
				       template[i].tap[k]);
				temp += template[i].tap[k];
397
				sg_set_buf(&sg[k], &xbuf[IDX[k]],
398
					   template[i].tap[k]);
399 400
			}

401
			authsize = abs(template[i].rlen - template[i].ilen);
J
Joy Latten 已提交
402 403 404 405 406 407 408 409
			ret = crypto_aead_setauthsize(tfm, authsize);
			if (ret) {
				printk(KERN_INFO
				       "failed to set authsize = %u\n",
				       authsize);
				goto out;
			}

H
Herbert Xu 已提交
410 411 412
			if (enc)
				sg[k - 1].length += authsize;

413 414
			sg_init_table(asg, template[i].anp);
			for (k = 0, temp = 0; k < template[i].anp; k++) {
415
				memcpy(&axbuf[IDX[k]],
416 417 418
				       template[i].assoc + temp,
				       template[i].atap[k]);
				temp += template[i].atap[k];
419
				sg_set_buf(&asg[k], &axbuf[IDX[k]],
420
					   template[i].atap[k]);
421 422 423
			}

			aead_request_set_crypt(req, sg, sg,
424 425
					       template[i].ilen,
					       iv);
426

427
			aead_request_set_assoc(req, asg, template[i].alen);
428

H
Herbert Xu 已提交
429 430 431
			ret = enc ?
				crypto_aead_encrypt(req) :
				crypto_aead_decrypt(req);
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

			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;
			}

451
			for (k = 0, temp = 0; k < template[i].np; k++) {
452 453
				printk(KERN_INFO "page %u\n", k);
				q = kmap(sg_page(&sg[k])) + sg[k].offset;
454
				hexdump(q, template[i].tap[k]);
455
				printk(KERN_INFO "%s\n",
456 457 458
				       memcmp(q, template[i].result + temp,
					      template[i].tap[k] -
					      (k < template[i].np - 1 || enc ?
H
Herbert Xu 已提交
459
					       0 : authsize)) ?
460 461
				       "fail" : "pass");

462 463
				temp += template[i].tap[k];
				kunmap(sg_page(&sg[k]));
464 465 466 467 468 469 470 471 472
			}
		}
	}

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

473
static void test_cipher(char *algo, int enc,
474
			struct cipher_testvec *template, unsigned int tcount)
L
Linus Torvalds 已提交
475 476
{
	unsigned int ret, i, j, k, temp;
477
	char *q;
478 479
	struct crypto_ablkcipher *tfm;
	struct ablkcipher_request *req;
L
Linus Torvalds 已提交
480
	struct scatterlist sg[8];
481
	const char *e;
482
	struct tcrypt_result result;
483 484
	void *data;
	char iv[MAX_IVLEN];
L
Linus Torvalds 已提交
485 486

	if (enc == ENCRYPT)
487
	        e = "encryption";
L
Linus Torvalds 已提交
488
	else
489
		e = "decryption";
L
Linus Torvalds 已提交
490

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

493 494
	init_completion(&result.completion);
	tfm = crypto_alloc_ablkcipher(algo, 0, 0);
L
Linus Torvalds 已提交
495

496 497 498
	if (IS_ERR(tfm)) {
		printk("failed to load transform for %s: %ld\n", algo,
		       PTR_ERR(tfm));
L
Linus Torvalds 已提交
499 500
		return;
	}
501 502 503 504 505 506 507 508 509

	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);
510

L
Linus Torvalds 已提交
511 512
	j = 0;
	for (i = 0; i < tcount; i++) {
513 514 515 516 517 518 519 520 521 522 523 524

		data = kzalloc(template[i].ilen, GFP_KERNEL);
		if (!data)
			continue;

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

		if (!(template[i].np)) {
525
			j++;
L
Linus Torvalds 已提交
526
			printk("test %u (%d bit key):\n",
527
			j, template[i].klen * 8);
L
Linus Torvalds 已提交
528

529
			crypto_ablkcipher_clear_flags(tfm, ~0);
530
			if (template[i].wk)
531
				crypto_ablkcipher_set_flags(
532
					tfm, CRYPTO_TFM_REQ_WEAK_KEY);
533

534 535
			ret = crypto_ablkcipher_setkey(tfm, template[i].key,
						       template[i].klen);
L
Linus Torvalds 已提交
536
			if (ret) {
537
				printk("setkey() failed flags=%x\n",
538
				       crypto_ablkcipher_get_flags(tfm));
539

540 541
				if (!template[i].fail) {
					kfree(data);
L
Linus Torvalds 已提交
542
					goto out;
543
				}
544
			}
L
Linus Torvalds 已提交
545

546
			sg_init_one(&sg[0], data, template[i].ilen);
547

548
			ablkcipher_request_set_crypt(req, sg, sg,
549
						     template[i].ilen, iv);
550
			ret = enc ?
551 552
				crypto_ablkcipher_encrypt(req) :
				crypto_ablkcipher_decrypt(req);
553

554 555 556 557 558 559 560 561 562 563 564 565 566 567
			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);
568
				kfree(data);
L
Linus Torvalds 已提交
569
				goto out;
570 571
			}

J
Jens Axboe 已提交
572
			q = kmap(sg_page(&sg[0])) + sg[0].offset;
573
			hexdump(q, template[i].rlen);
574 575

			printk("%s\n",
576 577 578
			       memcmp(q, template[i].result,
				      template[i].rlen) ? "fail" : "pass");
			kunmap(sg_page(&sg[0]));
L
Linus Torvalds 已提交
579
		}
580
		kfree(data);
L
Linus Torvalds 已提交
581
	}
582

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

L
Linus Torvalds 已提交
586 587
	j = 0;
	for (i = 0; i < tcount; i++) {
588 589 590 591 592 593 594 595 596 597 598 599 600

		data = kzalloc(template[i].ilen, GFP_KERNEL);
		if (!data)
			continue;

		memcpy(data, template[i].input, template[i].ilen);

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

		if (template[i].np) {
601
			j++;
L
Linus Torvalds 已提交
602
			printk("test %u (%d bit key):\n",
603
			j, template[i].klen * 8);
L
Linus Torvalds 已提交
604

605
			crypto_ablkcipher_clear_flags(tfm, ~0);
606
			if (template[i].wk)
607
				crypto_ablkcipher_set_flags(
608
					tfm, CRYPTO_TFM_REQ_WEAK_KEY);
609

610 611
			ret = crypto_ablkcipher_setkey(tfm, template[i].key,
						       template[i].klen);
L
Linus Torvalds 已提交
612
			if (ret) {
613
				printk("setkey() failed flags=%x\n",
614
						crypto_ablkcipher_get_flags(tfm));
615

616 617
				if (!template[i].fail) {
					kfree(data);
L
Linus Torvalds 已提交
618
					goto out;
619
				}
L
Linus Torvalds 已提交
620 621 622
			}

			temp = 0;
623 624
			sg_init_table(sg, template[i].np);
			for (k = 0; k < template[i].np; k++) {
625
				memcpy(&xbuf[IDX[k]],
626 627 628
						template[i].input + temp,
						template[i].tap[k]);
				temp += template[i].tap[k];
629
				sg_set_buf(&sg[k], &xbuf[IDX[k]],
630
						template[i].tap[k]);
L
Linus Torvalds 已提交
631
			}
632

633
			ablkcipher_request_set_crypt(req, sg, sg,
634
					template[i].ilen, iv);
635

636
			ret = enc ?
637 638
				crypto_ablkcipher_encrypt(req) :
				crypto_ablkcipher_decrypt(req);
639

640 641 642 643 644 645 646 647 648 649 650 651 652 653
			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 已提交
654 655 656 657
				goto out;
			}

			temp = 0;
658
			for (k = 0; k < template[i].np; k++) {
L
Linus Torvalds 已提交
659
				printk("page %u\n", k);
J
Jens Axboe 已提交
660
				q = kmap(sg_page(&sg[k])) + sg[k].offset;
661
				hexdump(q, template[i].tap[k]);
662
				printk("%s\n",
663 664
					memcmp(q, template[i].result + temp,
						template[i].tap[k]) ? "fail" :
L
Linus Torvalds 已提交
665
					"pass");
666 667
				temp += template[i].tap[k];
				kunmap(sg_page(&sg[k]));
L
Linus Torvalds 已提交
668 669 670 671
			}
		}
	}
out:
672 673
	crypto_free_ablkcipher(tfm);
	ablkcipher_request_free(req);
L
Linus Torvalds 已提交
674 675
}

676
static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc, char *p,
677 678
			       int blen, int sec)
{
679
	struct scatterlist sg[1];
680 681 682 683
	unsigned long start, end;
	int bcount;
	int ret;

684
	sg_init_one(sg, p, blen);
685 686 687 688

	for (start = jiffies, end = start + sec * HZ, bcount = 0;
	     time_before(jiffies, end); bcount++) {
		if (enc)
689
			ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
690
		else
691
			ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
692 693 694 695 696 697 698 699 700 701

		if (ret)
			return ret;
	}

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

702
static int test_cipher_cycles(struct blkcipher_desc *desc, int enc, char *p,
703 704
			      int blen)
{
705
	struct scatterlist sg[1];
706 707 708 709
	unsigned long cycles = 0;
	int ret = 0;
	int i;

710
	sg_init_one(sg, p, blen);
711 712 713 714 715 716 717

	local_bh_disable();
	local_irq_disable();

	/* Warm-up run. */
	for (i = 0; i < 4; i++) {
		if (enc)
718
			ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
719
		else
720
			ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
721 722 723 724 725 726 727 728 729 730 731

		if (ret)
			goto out;
	}

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

		start = get_cycles();
		if (enc)
732
			ret = crypto_blkcipher_encrypt(desc, sg, sg, blen);
733
		else
734
			ret = crypto_blkcipher_decrypt(desc, sg, sg, blen);
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
		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;
}

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

756
static void test_cipher_speed(char *algo, int enc, unsigned int sec,
757
			      struct cipher_testvec *template,
758
			      unsigned int tcount, u8 *keysize)
H
Harald Welte 已提交
759
{
760
	unsigned int ret, i, j, iv_len;
H
Harald Welte 已提交
761
	unsigned char *key, *p, iv[128];
762 763 764
	struct crypto_blkcipher *tfm;
	struct blkcipher_desc desc;
	const char *e;
765
	u32 *b_size;
H
Harald Welte 已提交
766 767 768 769 770 771

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

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

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

776 777 778
	if (IS_ERR(tfm)) {
		printk("failed to load transform for %s: %ld\n", algo,
		       PTR_ERR(tfm));
H
Harald Welte 已提交
779 780
		return;
	}
781 782
	desc.tfm = tfm;
	desc.flags = 0;
H
Harald Welte 已提交
783

784 785
	i = 0;
	do {
H
Harald Welte 已提交
786

787 788
		b_size = block_sizes;
		do {
H
Harald Welte 已提交
789

790 791 792 793 794
			if ((*keysize + *b_size) > TVMEMSIZE) {
				printk("template (%u) too big for tvmem (%u)\n",
						*keysize + *b_size, TVMEMSIZE);
				goto out;
			}
H
Harald Welte 已提交
795

796 797 798 799 800 801 802 803 804 805 806 807
			printk("test %u (%d bit key, %d byte blocks): ", i,
					*keysize * 8, *b_size);

			memset(tvmem, 0xff, *keysize + *b_size);

			/* set key, plain text and IV */
			key = (unsigned char *)tvmem;
			for (j = 0; j < tcount; j++) {
				if (template[j].klen == *keysize) {
					key = template[j].key;
					break;
				}
808
			}
809
			p = (unsigned char *)tvmem + *keysize;
H
Harald Welte 已提交
810

811 812 813 814 815 816
			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 已提交
817

818 819 820 821 822
			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 已提交
823

824 825 826 827
			if (sec)
				ret = test_cipher_jiffies(&desc, enc, p, *b_size, sec);
			else
				ret = test_cipher_cycles(&desc, enc, p, *b_size);
H
Harald Welte 已提交
828

829 830 831 832 833 834 835 836 837
			if (ret) {
				printk("%s() failed flags=%x\n", e, desc.flags);
				break;
			}
			b_size++;
			i++;
		} while (*b_size);
		keysize++;
	} while (*keysize);
H
Harald Welte 已提交
838 839

out:
840
	crypto_free_blkcipher(tfm);
H
Harald Welte 已提交
841 842
}

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

851 852
	sg_init_table(sg, 1);

853 854
	for (start = jiffies, end = start + sec * HZ, bcount = 0;
	     time_before(jiffies, end); bcount++) {
855
		sg_set_buf(sg, p, blen);
856 857 858 859 860 861 862 863 864 865 866 867 868
		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)
869 870 871 872
{
	struct scatterlist sg[1];
	unsigned long start, end;
	int bcount, pcount;
873 874 875 876
	int ret;

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

878 879
	sg_init_table(sg, 1);

880 881
	for (start = jiffies, end = start + sec * HZ, bcount = 0;
	     time_before(jiffies, end); bcount++) {
882 883 884
		ret = crypto_hash_init(desc);
		if (ret)
			return ret;
885
		for (pcount = 0; pcount < blen; pcount += plen) {
886
			sg_set_buf(sg, p + pcount, plen);
887 888 889
			ret = crypto_hash_update(desc, sg, plen);
			if (ret)
				return ret;
890 891
		}
		/* we assume there is enough space in 'out' for the result */
892 893 894
		ret = crypto_hash_final(desc, out);
		if (ret)
			return ret;
895 896 897 898 899
	}

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

900 901 902 903 904 905 906 907 908 909 910
	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;

911 912
	sg_init_table(sg, 1);

913 914 915 916 917
	local_bh_disable();
	local_irq_disable();

	/* Warm-up run. */
	for (i = 0; i < 4; i++) {
918
		sg_set_buf(sg, p, blen);
919 920 921 922 923 924 925 926 927 928 929
		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();

930
		sg_set_buf(sg, p, blen);
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
		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;
951 952
}

953 954
static int test_hash_cycles(struct hash_desc *desc, char *p, int blen,
			    int plen, char *out)
955 956 957 958
{
	struct scatterlist sg[1];
	unsigned long cycles = 0;
	int i, pcount;
959 960 961 962
	int ret;

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

964 965
	sg_init_table(sg, 1);

966 967 968 969 970
	local_bh_disable();
	local_irq_disable();

	/* Warm-up run. */
	for (i = 0; i < 4; i++) {
971 972 973
		ret = crypto_hash_init(desc);
		if (ret)
			goto out;
974
		for (pcount = 0; pcount < blen; pcount += plen) {
975
			sg_set_buf(sg, p + pcount, plen);
976 977 978
			ret = crypto_hash_update(desc, sg, plen);
			if (ret)
				goto out;
979
		}
980
		ret = crypto_hash_final(desc, out);
981 982
		if (ret)
			goto out;
983 984 985 986 987 988 989 990
	}

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

		start = get_cycles();

991 992 993
		ret = crypto_hash_init(desc);
		if (ret)
			goto out;
994
		for (pcount = 0; pcount < blen; pcount += plen) {
995
			sg_set_buf(sg, p + pcount, plen);
996 997 998
			ret = crypto_hash_update(desc, sg, plen);
			if (ret)
				goto out;
999
		}
1000 1001 1002
		ret = crypto_hash_final(desc, out);
		if (ret)
			goto out;
1003 1004 1005 1006 1007 1008

		end = get_cycles();

		cycles += end - start;
	}

1009
out:
1010 1011 1012
	local_irq_enable();
	local_bh_enable();

1013 1014 1015
	if (ret)
		return ret;

1016 1017 1018
	printk("%6lu cycles/operation, %4lu cycles/byte\n",
	       cycles / 8, cycles / (8 * blen));

1019
	return 0;
1020 1021
}

1022 1023
static void test_hash_speed(char *algo, unsigned int sec,
			      struct hash_speed *speed)
1024
{
1025 1026
	struct crypto_hash *tfm;
	struct hash_desc desc;
1027 1028
	char output[1024];
	int i;
1029
	int ret;
1030 1031 1032

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

1033
	tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
1034

1035 1036 1037
	if (IS_ERR(tfm)) {
		printk("failed to load transform for %s: %ld\n", algo,
		       PTR_ERR(tfm));
1038 1039 1040
		return;
	}

1041 1042 1043 1044
	desc.tfm = tfm;
	desc.flags = 0;

	if (crypto_hash_digestsize(tfm) > sizeof(output)) {
1045
		printk("digestsize(%u) > outputbuffer(%zu)\n",
1046
		       crypto_hash_digestsize(tfm), sizeof(output));
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
		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)
1063 1064
			ret = test_hash_jiffies(&desc, tvmem, speed[i].blen,
						speed[i].plen, output, sec);
1065
		else
1066 1067 1068 1069 1070 1071 1072
			ret = test_hash_cycles(&desc, tvmem, speed[i].blen,
					       speed[i].plen, output);

		if (ret) {
			printk("hashing failed ret=%d\n", ret);
			break;
		}
1073 1074 1075
	}

out:
1076
	crypto_free_hash(tfm);
1077 1078
}

1079 1080
static void test_comp(char *algo, struct comp_testvec *ctemplate,
		       struct comp_testvec *dtemplate, int ctcount, int dtcount)
L
Linus Torvalds 已提交
1081 1082 1083
{
	unsigned int i;
	char result[COMP_BUF_SIZE];
1084
	struct crypto_comp *tfm;
L
Linus Torvalds 已提交
1085 1086
	unsigned int tsize;

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

1089
	tfm = crypto_alloc_comp(algo, 0, CRYPTO_ALG_ASYNC);
1090
	if (IS_ERR(tfm)) {
1091
		printk("failed to load transform for %s\n", algo);
L
Linus Torvalds 已提交
1092 1093 1094
		return;
	}

1095
	for (i = 0; i < ctcount; i++) {
L
Linus Torvalds 已提交
1096
		int ilen, ret, dlen = COMP_BUF_SIZE;
1097

L
Linus Torvalds 已提交
1098 1099 1100
		printk("test %u:\n", i + 1);
		memset(result, 0, sizeof (result));

1101 1102
		ilen = ctemplate[i].inlen;
		ret = crypto_comp_compress(tfm, ctemplate[i].input,
L
Linus Torvalds 已提交
1103 1104 1105 1106 1107 1108 1109
		                           ilen, result, &dlen);
		if (ret) {
			printk("fail: ret=%d\n", ret);
			continue;
		}
		hexdump(result, dlen);
		printk("%s (ratio %d:%d)\n",
1110
		       memcmp(result, ctemplate[i].output, dlen) ? "fail" : "pass",
L
Linus Torvalds 已提交
1111 1112 1113
		       ilen, dlen);
	}

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

1116 1117
	tsize = sizeof(struct comp_testvec);
	tsize *= dtcount;
L
Linus Torvalds 已提交
1118 1119 1120 1121 1122 1123
	if (tsize > TVMEMSIZE) {
		printk("template (%u) too big for tvmem (%u)\n", tsize,
		       TVMEMSIZE);
		goto out;
	}

1124
	for (i = 0; i < dtcount; i++) {
L
Linus Torvalds 已提交
1125
		int ilen, ret, dlen = COMP_BUF_SIZE;
1126

L
Linus Torvalds 已提交
1127 1128 1129
		printk("test %u:\n", i + 1);
		memset(result, 0, sizeof (result));

1130 1131
		ilen = dtemplate[i].inlen;
		ret = crypto_comp_decompress(tfm, dtemplate[i].input,
L
Linus Torvalds 已提交
1132 1133 1134 1135 1136 1137 1138
		                             ilen, result, &dlen);
		if (ret) {
			printk("fail: ret=%d\n", ret);
			continue;
		}
		hexdump(result, dlen);
		printk("%s (ratio %d:%d)\n",
1139
		       memcmp(result, dtemplate[i].output, dlen) ? "fail" : "pass",
L
Linus Torvalds 已提交
1140 1141 1142
		       ilen, dlen);
	}
out:
1143
	crypto_free_comp(tfm);
L
Linus Torvalds 已提交
1144 1145
}

1146
static void test_available(void)
L
Linus Torvalds 已提交
1147 1148
{
	char **name = check;
1149

L
Linus Torvalds 已提交
1150 1151
	while (*name) {
		printk("alg %s ", *name);
1152
		printk(crypto_has_alg(*name, 0, 0) ?
1153
		       "found\n" : "not found\n");
L
Linus Torvalds 已提交
1154
		name++;
1155
	}
L
Linus Torvalds 已提交
1156 1157
}

1158
static void do_test(void)
L
Linus Torvalds 已提交
1159 1160 1161 1162 1163
{
	switch (mode) {

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

L
Linus Torvalds 已提交
1165
		test_hash("sha1", sha1_tv_template, SHA1_TEST_VECTORS);
1166

L
Linus Torvalds 已提交
1167
		//DES
1168 1169 1170 1171 1172 1173 1174 1175
		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);
1176

L
Linus Torvalds 已提交
1177
		//DES3_EDE
1178 1179 1180 1181
		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);
1182

L
Linus Torvalds 已提交
1183
		test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
1184

1185 1186
		test_hash("sha224", sha224_tv_template, SHA224_TEST_VECTORS);

L
Linus Torvalds 已提交
1187
		test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
1188

L
Linus Torvalds 已提交
1189
		//BLOWFISH
1190 1191 1192 1193 1194 1195 1196 1197
		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);
1198

L
Linus Torvalds 已提交
1199
		//TWOFISH
1200 1201 1202 1203 1204 1205 1206 1207
		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);
1208

L
Linus Torvalds 已提交
1209
		//SERPENT
1210 1211 1212 1213
		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);
1214

L
Linus Torvalds 已提交
1215
		//TNEPRES
1216 1217 1218 1219
		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 已提交
1220 1221

		//AES
1222 1223 1224 1225 1226 1227 1228 1229
		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 已提交
1230 1231 1232 1233
		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);
1234 1235 1236 1237
		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);
1238
		test_cipher("rfc3686(ctr(aes))", ENCRYPT, aes_ctr_enc_tv_template,
1239
			    AES_CTR_ENC_TEST_VECTORS);
1240
		test_cipher("rfc3686(ctr(aes))", DECRYPT, aes_ctr_dec_tv_template,
1241
			    AES_CTR_DEC_TEST_VECTORS);
M
Mikko Herranen 已提交
1242 1243 1244 1245
		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 已提交
1246 1247 1248 1249
		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 已提交
1250 1251

		//CAST5
1252 1253 1254 1255
		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);
1256

L
Linus Torvalds 已提交
1257
		//CAST6
1258 1259 1260 1261
		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 已提交
1262 1263

		//ARC4
1264 1265 1266 1267
		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 已提交
1268 1269

		//TEA
1270 1271 1272 1273
		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 已提交
1274 1275 1276


		//XTEA
1277 1278 1279 1280
		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 已提交
1281 1282

		//KHAZAD
1283 1284 1285 1286
		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 已提交
1287 1288

		//ANUBIS
1289 1290 1291 1292 1293 1294 1295 1296
		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 已提交
1297

A
Aaron Grothe 已提交
1298
		//XETA
1299 1300 1301 1302
		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 已提交
1303

1304 1305 1306 1307 1308 1309
		//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);

1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
		//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);

1324 1325 1326 1327 1328 1329
		//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);

1330 1331 1332 1333 1334 1335
		//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 已提交
1336 1337 1338 1339 1340 1341 1342 1343
		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);
1344 1345 1346
		test_comp("deflate", deflate_comp_tv_template,
			  deflate_decomp_tv_template, DEFLATE_COMP_TEST_VECTORS,
			  DEFLATE_DECOMP_TEST_VECTORS);
1347 1348
		test_comp("lzo", lzo_comp_tv_template, lzo_decomp_tv_template,
			  LZO_COMP_TEST_VECTORS, LZO_DECOMP_TEST_VECTORS);
1349
		test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
1350 1351 1352 1353
		test_hash("hmac(md5)", hmac_md5_tv_template,
			  HMAC_MD5_TEST_VECTORS);
		test_hash("hmac(sha1)", hmac_sha1_tv_template,
			  HMAC_SHA1_TEST_VECTORS);
1354 1355
		test_hash("hmac(sha224)", hmac_sha224_tv_template,
			  HMAC_SHA224_TEST_VECTORS);
1356 1357
		test_hash("hmac(sha256)", hmac_sha256_tv_template,
			  HMAC_SHA256_TEST_VECTORS);
1358 1359 1360 1361
		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 已提交
1362

1363 1364 1365
		test_hash("xcbc(aes)", aes_xcbc128_tv_template,
			  XCBC_AES_TEST_VECTORS);

L
Linus Torvalds 已提交
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
		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:
1378 1379 1380 1381 1382 1383 1384 1385
		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 已提交
1386 1387 1388
		break;

	case 4:
1389 1390 1391 1392
		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 已提交
1393 1394 1395 1396 1397
		break;

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

L
Linus Torvalds 已提交
1399 1400 1401
	case 6:
		test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
		break;
1402

L
Linus Torvalds 已提交
1403
	case 7:
1404 1405 1406 1407 1408 1409 1410 1411
		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 已提交
1412 1413 1414
		break;

	case 8:
1415 1416 1417 1418 1419 1420 1421 1422
		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 已提交
1423
		break;
1424

L
Linus Torvalds 已提交
1425
	case 9:
1426 1427 1428 1429
		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 已提交
1430 1431 1432
		break;

	case 10:
1433 1434 1435 1436 1437 1438 1439 1440
		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 已提交
1441 1442 1443 1444
		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);
1445 1446 1447 1448
		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);
1449
		test_cipher("rfc3686(ctr(aes))", ENCRYPT, aes_ctr_enc_tv_template,
1450
			    AES_CTR_ENC_TEST_VECTORS);
1451
		test_cipher("rfc3686(ctr(aes))", DECRYPT, aes_ctr_dec_tv_template,
1452
			    AES_CTR_DEC_TEST_VECTORS);
L
Linus Torvalds 已提交
1453 1454 1455 1456 1457
		break;

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

L
Linus Torvalds 已提交
1459 1460 1461 1462 1463
	case 12:
		test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
		break;

	case 13:
1464 1465 1466
		test_comp("deflate", deflate_comp_tv_template,
			  deflate_decomp_tv_template, DEFLATE_COMP_TEST_VECTORS,
			  DEFLATE_DECOMP_TEST_VECTORS);
L
Linus Torvalds 已提交
1467 1468 1469
		break;

	case 14:
1470 1471 1472 1473
		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 已提交
1474 1475 1476
		break;

	case 15:
1477 1478 1479 1480
		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 已提交
1481 1482 1483
		break;

	case 16:
1484 1485 1486 1487
		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 已提交
1488 1489 1490 1491 1492 1493 1494
		break;

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

	case 18:
1495
		test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
L
Linus Torvalds 已提交
1496 1497 1498
		break;

	case 19:
1499 1500 1501 1502
		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 已提交
1503 1504 1505
		break;

	case 20:
1506 1507 1508 1509
		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 已提交
1510 1511 1512
		break;

	case 21:
1513 1514 1515 1516
		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 已提交
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531
		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:
1532 1533 1534 1535
		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 已提交
1536 1537 1538
		break;

	case 26:
1539 1540 1541 1542 1543 1544 1545 1546
		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 已提交
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
		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 已提交
1561 1562
		
	case 30:
1563 1564 1565 1566
		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 已提交
1567
		break;
L
Linus Torvalds 已提交
1568

1569 1570 1571 1572 1573 1574 1575
	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;

1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
	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;
1590 1591 1592
	case 33:
		test_hash("sha224", sha224_tv_template, SHA224_TEST_VECTORS);
		break;
1593

1594 1595 1596 1597 1598 1599
	case 34:
		test_cipher("salsa20", ENCRYPT,
			    salsa20_stream_enc_tv_template,
			    SALSA20_STREAM_ENC_TEST_VECTORS);
		break;

1600 1601 1602 1603 1604 1605 1606
	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;

1607 1608 1609 1610 1611
	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 已提交
1612 1613 1614 1615 1616 1617 1618
	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;

1619 1620 1621 1622 1623 1624 1625
	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;

L
Linus Torvalds 已提交
1626
	case 100:
1627 1628
		test_hash("hmac(md5)", hmac_md5_tv_template,
			  HMAC_MD5_TEST_VECTORS);
L
Linus Torvalds 已提交
1629
		break;
1630

L
Linus Torvalds 已提交
1631
	case 101:
1632 1633
		test_hash("hmac(sha1)", hmac_sha1_tv_template,
			  HMAC_SHA1_TEST_VECTORS);
L
Linus Torvalds 已提交
1634
		break;
1635

L
Linus Torvalds 已提交
1636
	case 102:
1637 1638
		test_hash("hmac(sha256)", hmac_sha256_tv_template,
			  HMAC_SHA256_TEST_VECTORS);
L
Linus Torvalds 已提交
1639 1640
		break;

1641 1642 1643 1644 1645 1646 1647 1648 1649
	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;
1650

1651 1652 1653 1654
	case 105:
		test_hash("hmac(sha224)", hmac_sha224_tv_template,
			  HMAC_SHA224_TEST_VECTORS);
		break;
L
Linus Torvalds 已提交
1655

1656 1657 1658 1659 1660
	case 106:
		test_hash("xcbc(aes)", aes_xcbc128_tv_template,
			  XCBC_AES_TEST_VECTORS);
		break;

H
Harald Welte 已提交
1661
	case 200:
1662
		test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
1663
				speed_template_16_24_32);
1664
		test_cipher_speed("ecb(aes)", DECRYPT, sec, NULL, 0,
1665
				speed_template_16_24_32);
1666
		test_cipher_speed("cbc(aes)", ENCRYPT, sec, NULL, 0,
1667
				speed_template_16_24_32);
1668
		test_cipher_speed("cbc(aes)", DECRYPT, sec, NULL, 0,
1669
				speed_template_16_24_32);
R
Rik Snel 已提交
1670
		test_cipher_speed("lrw(aes)", ENCRYPT, sec, NULL, 0,
1671
				speed_template_32_40_48);
R
Rik Snel 已提交
1672
		test_cipher_speed("lrw(aes)", DECRYPT, sec, NULL, 0,
1673
				speed_template_32_40_48);
1674
		test_cipher_speed("xts(aes)", ENCRYPT, sec, NULL, 0,
1675
				speed_template_32_48_64);
1676
		test_cipher_speed("xts(aes)", DECRYPT, sec, NULL, 0,
1677
				speed_template_32_48_64);
H
Harald Welte 已提交
1678 1679 1680
		break;

	case 201:
1681
		test_cipher_speed("ecb(des3_ede)", ENCRYPT, sec,
1682 1683
				des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS,
				speed_template_24);
1684
		test_cipher_speed("ecb(des3_ede)", DECRYPT, sec,
1685 1686
				des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS,
				speed_template_24);
1687
		test_cipher_speed("cbc(des3_ede)", ENCRYPT, sec,
1688 1689
				des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS,
				speed_template_24);
1690
		test_cipher_speed("cbc(des3_ede)", DECRYPT, sec,
1691 1692
				des3_ede_enc_tv_template, DES3_EDE_ENC_TEST_VECTORS,
				speed_template_24);
H
Harald Welte 已提交
1693 1694 1695
		break;

	case 202:
1696
		test_cipher_speed("ecb(twofish)", ENCRYPT, sec, NULL, 0,
1697
				speed_template_16_24_32);
1698
		test_cipher_speed("ecb(twofish)", DECRYPT, sec, NULL, 0,
1699
				speed_template_16_24_32);
1700
		test_cipher_speed("cbc(twofish)", ENCRYPT, sec, NULL, 0,
1701
				speed_template_16_24_32);
1702
		test_cipher_speed("cbc(twofish)", DECRYPT, sec, NULL, 0,
1703
				speed_template_16_24_32);
H
Harald Welte 已提交
1704 1705 1706
		break;

	case 203:
1707
		test_cipher_speed("ecb(blowfish)", ENCRYPT, sec, NULL, 0,
1708
				  speed_template_8_32);
1709
		test_cipher_speed("ecb(blowfish)", DECRYPT, sec, NULL, 0,
1710
				  speed_template_8_32);
1711
		test_cipher_speed("cbc(blowfish)", ENCRYPT, sec, NULL, 0,
1712
				  speed_template_8_32);
1713
		test_cipher_speed("cbc(blowfish)", DECRYPT, sec, NULL, 0,
1714
				  speed_template_8_32);
H
Harald Welte 已提交
1715 1716 1717
		break;

	case 204:
1718
		test_cipher_speed("ecb(des)", ENCRYPT, sec, NULL, 0,
1719
				  speed_template_8);
1720
		test_cipher_speed("ecb(des)", DECRYPT, sec, NULL, 0,
1721
				  speed_template_8);
1722
		test_cipher_speed("cbc(des)", ENCRYPT, sec, NULL, 0,
1723
				  speed_template_8);
1724
		test_cipher_speed("cbc(des)", DECRYPT, sec, NULL, 0,
1725
				  speed_template_8);
H
Harald Welte 已提交
1726 1727
		break;

1728 1729
	case 205:
		test_cipher_speed("ecb(camellia)", ENCRYPT, sec, NULL, 0,
1730
				speed_template_16_24_32);
1731
		test_cipher_speed("ecb(camellia)", DECRYPT, sec, NULL, 0,
1732
				speed_template_16_24_32);
1733
		test_cipher_speed("cbc(camellia)", ENCRYPT, sec, NULL, 0,
1734
				speed_template_16_24_32);
1735
		test_cipher_speed("cbc(camellia)", DECRYPT, sec, NULL, 0,
1736
				speed_template_16_24_32);
1737 1738
		break;

1739 1740
	case 206:
		test_cipher_speed("salsa20", ENCRYPT, sec, NULL, 0,
1741
				  speed_template_16_32);
1742 1743
		break;

1744 1745 1746 1747
	case 300:
		/* fall through */

	case 301:
1748
		test_hash_speed("md4", sec, generic_hash_speed_template);
1749 1750 1751
		if (mode > 300 && mode < 400) break;

	case 302:
1752
		test_hash_speed("md5", sec, generic_hash_speed_template);
1753 1754 1755
		if (mode > 300 && mode < 400) break;

	case 303:
1756
		test_hash_speed("sha1", sec, generic_hash_speed_template);
1757 1758 1759
		if (mode > 300 && mode < 400) break;

	case 304:
1760
		test_hash_speed("sha256", sec, generic_hash_speed_template);
1761 1762 1763
		if (mode > 300 && mode < 400) break;

	case 305:
1764
		test_hash_speed("sha384", sec, generic_hash_speed_template);
1765 1766 1767
		if (mode > 300 && mode < 400) break;

	case 306:
1768
		test_hash_speed("sha512", sec, generic_hash_speed_template);
1769 1770 1771
		if (mode > 300 && mode < 400) break;

	case 307:
1772
		test_hash_speed("wp256", sec, generic_hash_speed_template);
1773 1774 1775
		if (mode > 300 && mode < 400) break;

	case 308:
1776
		test_hash_speed("wp384", sec, generic_hash_speed_template);
1777 1778 1779
		if (mode > 300 && mode < 400) break;

	case 309:
1780
		test_hash_speed("wp512", sec, generic_hash_speed_template);
1781 1782 1783
		if (mode > 300 && mode < 400) break;

	case 310:
1784
		test_hash_speed("tgr128", sec, generic_hash_speed_template);
1785 1786 1787
		if (mode > 300 && mode < 400) break;

	case 311:
1788
		test_hash_speed("tgr160", sec, generic_hash_speed_template);
1789 1790 1791
		if (mode > 300 && mode < 400) break;

	case 312:
1792
		test_hash_speed("tgr192", sec, generic_hash_speed_template);
1793 1794
		if (mode > 300 && mode < 400) break;

1795 1796 1797 1798
	case 313:
		test_hash_speed("sha224", sec, generic_hash_speed_template);
		if (mode > 300 && mode < 400) break;

1799 1800 1801
	case 399:
		break;

L
Linus Torvalds 已提交
1802 1803 1804
	case 1000:
		test_available();
		break;
1805

L
Linus Torvalds 已提交
1806 1807 1808 1809 1810 1811 1812
	default:
		/* useful for debugging */
		printk("not testing anything\n");
		break;
	}
}

1813
static int __init tcrypt_mod_init(void)
L
Linus Torvalds 已提交
1814
{
1815 1816
	int err = -ENOMEM;

L
Linus Torvalds 已提交
1817 1818
	tvmem = kmalloc(TVMEMSIZE, GFP_KERNEL);
	if (tvmem == NULL)
1819
		return err;
L
Linus Torvalds 已提交
1820 1821

	xbuf = kmalloc(XBUFSIZE, GFP_KERNEL);
1822 1823
	if (xbuf == NULL)
		goto err_free_tv;
L
Linus Torvalds 已提交
1824

1825 1826 1827
	axbuf = kmalloc(XBUFSIZE, GFP_KERNEL);
	if (axbuf == NULL)
		goto err_free_xbuf;
L
Linus Torvalds 已提交
1828

1829
	do_test();
1830 1831 1832 1833 1834 1835 1836

	/* 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
	 */
1837 1838 1839 1840 1841 1842 1843 1844 1845
	err = -EAGAIN;

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

	return err;
L
Linus Torvalds 已提交
1846 1847 1848 1849 1850 1851
}

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

1854 1855
module_init(tcrypt_mod_init);
module_exit(tcrypt_mod_fini);
L
Linus Torvalds 已提交
1856 1857

module_param(mode, int, 0);
H
Harald Welte 已提交
1858
module_param(sec, uint, 0);
1859 1860
MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
		      "(defaults to zero which uses CPU cycles instead)");
L
Linus Torvalds 已提交
1861 1862 1863 1864

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