testmgr.c 85.8 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * Algorithm testing framework and tests.
 *
 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
 * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
 * Copyright (c) 2007 Nokia Siemens Networks
 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
 *
9 10 11 12 13 14 15
 * Updated RFC4106 AES-GCM testing.
 *    Authors: Aidan O'Mahony (aidan.o.mahony@intel.com)
 *             Adrian Hoban <adrian.hoban@intel.com>
 *             Gabriele Paoloni <gabriele.paoloni@intel.com>
 *             Tadeusz Struk (tadeusz.struk@intel.com)
 *    Copyright (c) 2010, Intel Corporation.
 *
16 17 18 19 20 21 22
 * 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
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 */

23
#include <crypto/aead.h>
24
#include <crypto/hash.h>
25
#include <crypto/skcipher.h>
26
#include <linux/err.h>
27
#include <linux/fips.h>
28 29 30 31
#include <linux/module.h>
#include <linux/scatterlist.h>
#include <linux/slab.h>
#include <linux/string.h>
32
#include <crypto/rng.h>
33
#include <crypto/drbg.h>
34
#include <crypto/akcipher.h>
35
#include <crypto/kpp.h>
36
#include <crypto/acompress.h>
37 38

#include "internal.h"
39

40 41 42 43
static bool notests;
module_param(notests, bool, 0644);
MODULE_PARM_DESC(notests, "disable crypto self-tests");

44
#ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
45 46 47 48 49 50 51 52 53

/* a perfect nop */
int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
{
	return 0;
}

#else

54 55 56 57 58 59 60 61 62 63 64 65
#include "testmgr.h"

/*
 * Need slab memory for testing (size in number of pages).
 */
#define XBUFSIZE	8

/*
 * Indexes into the xbuf to simulate cross-page access.
 */
#define IDX1		32
#define IDX2		32400
66
#define IDX3		1511
67 68 69 70 71 72 73 74 75 76 77 78 79 80
#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

struct aead_test_suite {
	struct {
81
		const struct aead_testvec *vecs;
82 83 84 85 86
		unsigned int count;
	} enc, dec;
};

struct cipher_test_suite {
87 88
	const struct cipher_testvec *vecs;
	unsigned int count;
89 90 91 92
};

struct comp_test_suite {
	struct {
93
		const struct comp_testvec *vecs;
94 95 96 97 98
		unsigned int count;
	} comp, decomp;
};

struct hash_test_suite {
99
	const struct hash_testvec *vecs;
100 101 102
	unsigned int count;
};

103
struct cprng_test_suite {
104
	const struct cprng_testvec *vecs;
105 106 107
	unsigned int count;
};

108
struct drbg_test_suite {
109
	const struct drbg_testvec *vecs;
110 111 112
	unsigned int count;
};

113
struct akcipher_test_suite {
114
	const struct akcipher_testvec *vecs;
115 116 117
	unsigned int count;
};

118
struct kpp_test_suite {
119
	const struct kpp_testvec *vecs;
120 121 122
	unsigned int count;
};

123 124 125 126
struct alg_test_desc {
	const char *alg;
	int (*test)(const struct alg_test_desc *desc, const char *driver,
		    u32 type, u32 mask);
127
	int fips_allowed;	/* set if alg is allowed in fips mode */
128 129 130 131 132 133

	union {
		struct aead_test_suite aead;
		struct cipher_test_suite cipher;
		struct comp_test_suite comp;
		struct hash_test_suite hash;
134
		struct cprng_test_suite cprng;
135
		struct drbg_test_suite drbg;
136
		struct akcipher_test_suite akcipher;
137
		struct kpp_test_suite kpp;
138 139 140
	} suite;
};

141 142
static const unsigned int IDX[8] = {
	IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
143 144 145 146 147 148 149 150

static void hexdump(unsigned char *buf, unsigned int len)
{
	print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
			16, 1,
			buf, len, false);
}

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
static int testmgr_alloc_buf(char *buf[XBUFSIZE])
{
	int i;

	for (i = 0; i < XBUFSIZE; i++) {
		buf[i] = (void *)__get_free_page(GFP_KERNEL);
		if (!buf[i])
			goto err_free_buf;
	}

	return 0;

err_free_buf:
	while (i-- > 0)
		free_page((unsigned long)buf[i]);

	return -ENOMEM;
}

static void testmgr_free_buf(char *buf[XBUFSIZE])
{
	int i;

	for (i = 0; i < XBUFSIZE; i++)
		free_page((unsigned long)buf[i]);
}

178 179 180 181 182 183 184 185 186 187 188 189
static int ahash_guard_result(char *result, char c, int size)
{
	int i;

	for (i = 0; i < size; i++) {
		if (result[i] != c)
			return -EINVAL;
	}

	return 0;
}

190
static int ahash_partial_update(struct ahash_request **preq,
191
	struct crypto_ahash *tfm, const struct hash_testvec *template,
192
	void *hash_buff, int k, int temp, struct scatterlist *sg,
193
	const char *algo, char *result, struct crypto_wait *wait)
194 195 196 197
{
	char *state;
	struct ahash_request *req;
	int statesize, ret = -EINVAL;
198
	static const unsigned char guard[] = { 0x00, 0xba, 0xad, 0x00 };
199
	int digestsize = crypto_ahash_digestsize(tfm);
200 201 202 203

	req = *preq;
	statesize = crypto_ahash_statesize(
			crypto_ahash_reqtfm(req));
204
	state = kmalloc(statesize + sizeof(guard), GFP_KERNEL);
205
	if (!state) {
206
		pr_err("alg: hash: Failed to alloc state for %s\n", algo);
207 208
		goto out_nostate;
	}
209
	memcpy(state + statesize, guard, sizeof(guard));
210
	memset(result, 1, digestsize);
211
	ret = crypto_ahash_export(req, state);
212
	WARN_ON(memcmp(state + statesize, guard, sizeof(guard)));
213
	if (ret) {
214
		pr_err("alg: hash: Failed to export() for %s\n", algo);
215 216
		goto out;
	}
217 218 219 220 221 222
	ret = ahash_guard_result(result, 1, digestsize);
	if (ret) {
		pr_err("alg: hash: Failed, export used req->result for %s\n",
		       algo);
		goto out;
	}
223 224 225 226 227 228 229 230
	ahash_request_free(req);
	req = ahash_request_alloc(tfm, GFP_KERNEL);
	if (!req) {
		pr_err("alg: hash: Failed to alloc request for %s\n", algo);
		goto out_noreq;
	}
	ahash_request_set_callback(req,
		CRYPTO_TFM_REQ_MAY_BACKLOG,
231
		crypto_req_done, wait);
232 233 234 235 236 237 238 239 240 241

	memcpy(hash_buff, template->plaintext + temp,
		template->tap[k]);
	sg_init_one(&sg[0], hash_buff, template->tap[k]);
	ahash_request_set_crypt(req, sg, result, template->tap[k]);
	ret = crypto_ahash_import(req, state);
	if (ret) {
		pr_err("alg: hash: Failed to import() for %s\n", algo);
		goto out;
	}
242 243 244 245 246 247
	ret = ahash_guard_result(result, 1, digestsize);
	if (ret) {
		pr_err("alg: hash: Failed, import used req->result for %s\n",
		       algo);
		goto out;
	}
248
	ret = crypto_wait_req(crypto_ahash_update(req), wait);
249 250 251 252 253 254 255 256 257 258 259 260 261
	if (ret)
		goto out;
	*preq = req;
	ret = 0;
	goto out_noreq;
out:
	ahash_request_free(req);
out_noreq:
	kfree(state);
out_nostate:
	return ret;
}

262 263 264 265 266 267
enum hash_test {
	HASH_TEST_DIGEST,
	HASH_TEST_FINAL,
	HASH_TEST_FINUP
};

268 269
static int __test_hash(struct crypto_ahash *tfm,
		       const struct hash_testvec *template, unsigned int tcount,
270
		       enum hash_test test_type, const int align_offset)
271 272
{
	const char *algo = crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
273
	size_t digest_size = crypto_ahash_digestsize(tfm);
274 275
	unsigned int i, j, k, temp;
	struct scatterlist sg[8];
276 277
	char *result;
	char *key;
278
	struct ahash_request *req;
279
	struct crypto_wait wait;
280
	void *hash_buff;
281 282 283
	char *xbuf[XBUFSIZE];
	int ret = -ENOMEM;

284
	result = kmalloc(digest_size, GFP_KERNEL);
285 286 287 288 289
	if (!result)
		return ret;
	key = kmalloc(MAX_KEYLEN, GFP_KERNEL);
	if (!key)
		goto out_nobuf;
290 291
	if (testmgr_alloc_buf(xbuf))
		goto out_nobuf;
292

293
	crypto_init_wait(&wait);
294 295 296 297 298 299 300 301

	req = ahash_request_alloc(tfm, GFP_KERNEL);
	if (!req) {
		printk(KERN_ERR "alg: hash: Failed to allocate request for "
		       "%s\n", algo);
		goto out_noreq;
	}
	ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
302
				   crypto_req_done, &wait);
303

304
	j = 0;
305
	for (i = 0; i < tcount; i++) {
306 307 308
		if (template[i].np)
			continue;

309 310 311 312
		ret = -EINVAL;
		if (WARN_ON(align_offset + template[i].psize > PAGE_SIZE))
			goto out;

313
		j++;
314
		memset(result, 0, digest_size);
315 316

		hash_buff = xbuf[0];
317
		hash_buff += align_offset;
318 319 320 321 322 323

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

		if (template[i].ksize) {
			crypto_ahash_clear_flags(tfm, ~0);
324 325 326 327 328 329 330 331
			if (template[i].ksize > MAX_KEYLEN) {
				pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
				       j, algo, template[i].ksize, MAX_KEYLEN);
				ret = -EINVAL;
				goto out;
			}
			memcpy(key, template[i].key, template[i].ksize);
			ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
332 333
			if (ret) {
				printk(KERN_ERR "alg: hash: setkey failed on "
334
				       "test %d for %s: ret=%d\n", j, algo,
335 336 337 338 339 340
				       -ret);
				goto out;
			}
		}

		ahash_request_set_crypt(req, sg, result, template[i].psize);
341 342
		switch (test_type) {
		case HASH_TEST_DIGEST:
343
			ret = crypto_wait_req(crypto_ahash_digest(req), &wait);
344 345 346 347 348
			if (ret) {
				pr_err("alg: hash: digest failed on test %d "
				       "for %s: ret=%d\n", j, algo, -ret);
				goto out;
			}
349 350 351
			break;

		case HASH_TEST_FINAL:
352
			memset(result, 1, digest_size);
353
			ret = crypto_wait_req(crypto_ahash_init(req), &wait);
354
			if (ret) {
355
				pr_err("alg: hash: init failed on test %d "
356 357 358
				       "for %s: ret=%d\n", j, algo, -ret);
				goto out;
			}
359 360 361 362 363 364
			ret = ahash_guard_result(result, 1, digest_size);
			if (ret) {
				pr_err("alg: hash: init failed on test %d "
				       "for %s: used req->result\n", j, algo);
				goto out;
			}
365
			ret = crypto_wait_req(crypto_ahash_update(req), &wait);
366
			if (ret) {
367
				pr_err("alg: hash: update failed on test %d "
368 369 370
				       "for %s: ret=%d\n", j, algo, -ret);
				goto out;
			}
371 372 373 374 375 376
			ret = ahash_guard_result(result, 1, digest_size);
			if (ret) {
				pr_err("alg: hash: update failed on test %d "
				       "for %s: used req->result\n", j, algo);
				goto out;
			}
377
			ret = crypto_wait_req(crypto_ahash_final(req), &wait);
378
			if (ret) {
379
				pr_err("alg: hash: final failed on test %d "
380 381
				       "for %s: ret=%d\n", j, algo, -ret);
				goto out;
382
			}
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
			break;

		case HASH_TEST_FINUP:
			memset(result, 1, digest_size);
			ret = crypto_wait_req(crypto_ahash_init(req), &wait);
			if (ret) {
				pr_err("alg: hash: init failed on test %d "
				       "for %s: ret=%d\n", j, algo, -ret);
				goto out;
			}
			ret = ahash_guard_result(result, 1, digest_size);
			if (ret) {
				pr_err("alg: hash: init failed on test %d "
				       "for %s: used req->result\n", j, algo);
				goto out;
			}
			ret = crypto_wait_req(crypto_ahash_finup(req), &wait);
			if (ret) {
				pr_err("alg: hash: final failed on test %d "
				       "for %s: ret=%d\n", j, algo, -ret);
				goto out;
			}
			break;
406 407 408 409 410
		}

		if (memcmp(result, template[i].digest,
			   crypto_ahash_digestsize(tfm))) {
			printk(KERN_ERR "alg: hash: Test %d failed for %s\n",
411
			       j, algo);
412 413 414 415 416 417
			hexdump(result, crypto_ahash_digestsize(tfm));
			ret = -EINVAL;
			goto out;
		}
	}

418 419 420
	if (test_type)
		goto out;

421 422
	j = 0;
	for (i = 0; i < tcount; i++) {
423 424 425 426
		/* alignment tests are only done with continuous buffers */
		if (align_offset != 0)
			break;

427 428
		if (!template[i].np)
			continue;
429

430
		j++;
431
		memset(result, 0, digest_size);
432

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
		temp = 0;
		sg_init_table(sg, template[i].np);
		ret = -EINVAL;
		for (k = 0; k < template[i].np; k++) {
			if (WARN_ON(offset_in_page(IDX[k]) +
				    template[i].tap[k] > PAGE_SIZE))
				goto out;
			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]);
			temp += template[i].tap[k];
		}
448

449 450 451 452 453
		if (template[i].ksize) {
			if (template[i].ksize > MAX_KEYLEN) {
				pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
				       j, algo, template[i].ksize, MAX_KEYLEN);
				ret = -EINVAL;
454 455
				goto out;
			}
456 457 458
			crypto_ahash_clear_flags(tfm, ~0);
			memcpy(key, template[i].key, template[i].ksize);
			ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
459

460 461 462 463
			if (ret) {
				printk(KERN_ERR "alg: hash: setkey "
				       "failed on chunking test %d "
				       "for %s: ret=%d\n", j, algo, -ret);
464 465 466
				goto out;
			}
		}
467 468

		ahash_request_set_crypt(req, sg, result, template[i].psize);
469 470 471 472
		ret = crypto_wait_req(crypto_ahash_digest(req), &wait);
		if (ret) {
			pr_err("alg: hash: digest failed on chunking test %d for %s: ret=%d\n",
			       j, algo, -ret);
473 474 475 476 477 478 479 480 481
			goto out;
		}

		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;
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
			goto out;
		}
	}

	/* partial update exercise */
	j = 0;
	for (i = 0; i < tcount; i++) {
		/* alignment tests are only done with continuous buffers */
		if (align_offset != 0)
			break;

		if (template[i].np < 2)
			continue;

		j++;
497
		memset(result, 0, digest_size);
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522

		ret = -EINVAL;
		hash_buff = xbuf[0];
		memcpy(hash_buff, template[i].plaintext,
			template[i].tap[0]);
		sg_init_one(&sg[0], hash_buff, template[i].tap[0]);

		if (template[i].ksize) {
			crypto_ahash_clear_flags(tfm, ~0);
			if (template[i].ksize > MAX_KEYLEN) {
				pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
					j, algo, template[i].ksize, MAX_KEYLEN);
				ret = -EINVAL;
				goto out;
			}
			memcpy(key, template[i].key, template[i].ksize);
			ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
			if (ret) {
				pr_err("alg: hash: setkey failed on test %d for %s: ret=%d\n",
					j, algo, -ret);
				goto out;
			}
		}

		ahash_request_set_crypt(req, sg, result, template[i].tap[0]);
523
		ret = crypto_wait_req(crypto_ahash_init(req), &wait);
524
		if (ret) {
525
			pr_err("alg: hash: init failed on test %d for %s: ret=%d\n",
526 527 528
				j, algo, -ret);
			goto out;
		}
529
		ret = crypto_wait_req(crypto_ahash_update(req), &wait);
530
		if (ret) {
531
			pr_err("alg: hash: update failed on test %d for %s: ret=%d\n",
532 533 534 535 536 537 538 539
				j, algo, -ret);
			goto out;
		}

		temp = template[i].tap[0];
		for (k = 1; k < template[i].np; k++) {
			ret = ahash_partial_update(&req, tfm, &template[i],
				hash_buff, k, temp, &sg[0], algo, result,
540
				&wait);
541
			if (ret) {
542
				pr_err("alg: hash: partial update failed on test %d for %s: ret=%d\n",
543 544 545 546 547
					j, algo, -ret);
				goto out_noreq;
			}
			temp += template[i].tap[k];
		}
548
		ret = crypto_wait_req(crypto_ahash_final(req), &wait);
549
		if (ret) {
550
			pr_err("alg: hash: final failed on test %d for %s: ret=%d\n",
551 552 553 554 555 556 557 558 559
				j, algo, -ret);
			goto out;
		}
		if (memcmp(result, template[i].digest,
			   crypto_ahash_digestsize(tfm))) {
			pr_err("alg: hash: Partial Test %d failed for %s\n",
			       j, algo);
			hexdump(result, crypto_ahash_digestsize(tfm));
			ret = -EINVAL;
560 561
			goto out;
		}
562 563 564 565 566 567 568
	}

	ret = 0;

out:
	ahash_request_free(req);
out_noreq:
569 570
	testmgr_free_buf(xbuf);
out_nobuf:
571 572
	kfree(key);
	kfree(result);
573 574 575
	return ret;
}

576 577
static int test_hash(struct crypto_ahash *tfm,
		     const struct hash_testvec *template,
578
		     unsigned int tcount, enum hash_test test_type)
579 580 581 582
{
	unsigned int alignmask;
	int ret;

583
	ret = __test_hash(tfm, template, tcount, test_type, 0);
584 585 586 587
	if (ret)
		return ret;

	/* test unaligned buffers, check with one byte offset */
588
	ret = __test_hash(tfm, template, tcount, test_type, 1);
589 590 591 592 593 594
	if (ret)
		return ret;

	alignmask = crypto_tfm_alg_alignmask(&tfm->base);
	if (alignmask) {
		/* Check if alignment mask for tfm is correctly set. */
595
		ret = __test_hash(tfm, template, tcount, test_type,
596 597 598 599 600 601 602 603
				  alignmask + 1);
		if (ret)
			return ret;
	}

	return 0;
}

604
static int __test_aead(struct crypto_aead *tfm, int enc,
605
		       const struct aead_testvec *template, unsigned int tcount,
606
		       const bool diff_dst, const int align_offset)
607 608 609
{
	const char *algo = crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm));
	unsigned int i, j, k, n, temp;
610
	int ret = -ENOMEM;
611 612 613
	char *q;
	char *key;
	struct aead_request *req;
614 615 616
	struct scatterlist *sg;
	struct scatterlist *sgout;
	const char *e, *d;
617
	struct crypto_wait wait;
618
	unsigned int authsize, iv_len;
619
	void *input;
620
	void *output;
621
	void *assoc;
622
	char *iv;
623
	char *xbuf[XBUFSIZE];
624
	char *xoutbuf[XBUFSIZE];
625 626
	char *axbuf[XBUFSIZE];

627 628 629
	iv = kzalloc(MAX_IVLEN, GFP_KERNEL);
	if (!iv)
		return ret;
630 631 632
	key = kmalloc(MAX_KEYLEN, GFP_KERNEL);
	if (!key)
		goto out_noxbuf;
633 634 635 636
	if (testmgr_alloc_buf(xbuf))
		goto out_noxbuf;
	if (testmgr_alloc_buf(axbuf))
		goto out_noaxbuf;
637 638 639 640
	if (diff_dst && testmgr_alloc_buf(xoutbuf))
		goto out_nooutbuf;

	/* avoid "the frame size is larger than 1024 bytes" compiler warning */
641 642
	sg = kmalloc(array3_size(sizeof(*sg), 8, (diff_dst ? 4 : 2)),
		     GFP_KERNEL);
643 644
	if (!sg)
		goto out_nosg;
645
	sgout = &sg[16];
646 647 648 649 650 651

	if (diff_dst)
		d = "-ddst";
	else
		d = "";

652 653 654 655 656
	if (enc == ENCRYPT)
		e = "encryption";
	else
		e = "decryption";

657
	crypto_init_wait(&wait);
658 659 660

	req = aead_request_alloc(tfm, GFP_KERNEL);
	if (!req) {
661 662
		pr_err("alg: aead%s: Failed to allocate request for %s\n",
		       d, algo);
663 664 665 666
		goto out;
	}

	aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
667
				  crypto_req_done, &wait);
668

669 670
	iv_len = crypto_aead_ivsize(tfm);

671
	for (i = 0, j = 0; i < tcount; i++) {
672 673
		if (template[i].np)
			continue;
674

675
		j++;
676

677 678 679 680 681 682
		/* some templates have no input data but they will
		 * touch input
		 */
		input = xbuf[0];
		input += align_offset;
		assoc = axbuf[0];
683

684 685 686 687
		ret = -EINVAL;
		if (WARN_ON(align_offset + template[i].ilen >
			    PAGE_SIZE || template[i].alen > PAGE_SIZE))
			goto out;
688

689 690 691
		memcpy(input, template[i].input, template[i].ilen);
		memcpy(assoc, template[i].assoc, template[i].alen);
		if (template[i].iv)
692
			memcpy(iv, template[i].iv, iv_len);
693
		else
694
			memset(iv, 0, iv_len);
695 696 697 698 699 700 701 702 703 704 705 706 707

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

		if (template[i].klen > MAX_KEYLEN) {
			pr_err("alg: aead%s: setkey failed on test %d for %s: key size %d > %d\n",
			       d, j, algo, template[i].klen,
			       MAX_KEYLEN);
			ret = -EINVAL;
			goto out;
		}
		memcpy(key, template[i].key, template[i].klen);
708

709
		ret = crypto_aead_setkey(tfm, key, template[i].klen);
710
		if (template[i].fail == !ret) {
711 712 713 714 715
			pr_err("alg: aead%s: setkey failed on test %d for %s: flags=%x\n",
			       d, j, algo, crypto_aead_get_flags(tfm));
			goto out;
		} else if (ret)
			continue;
716

717 718 719 720 721 722 723
		authsize = abs(template[i].rlen - template[i].ilen);
		ret = crypto_aead_setauthsize(tfm, authsize);
		if (ret) {
			pr_err("alg: aead%s: Failed to set authsize to %u on test %d for %s\n",
			       d, authsize, j, algo);
			goto out;
		}
724

725 726 727 728 729 730 731
		k = !!template[i].alen;
		sg_init_table(sg, k + 1);
		sg_set_buf(&sg[0], assoc, template[i].alen);
		sg_set_buf(&sg[k], input,
			   template[i].ilen + (enc ? authsize : 0));
		output = input;

732
		if (diff_dst) {
733 734 735
			sg_init_table(sgout, k + 1);
			sg_set_buf(&sgout[0], assoc, template[i].alen);

736 737
			output = xoutbuf[0];
			output += align_offset;
738 739
			sg_set_buf(&sgout[k], output,
				   template[i].rlen + (enc ? 0 : authsize));
740
		}
741

742 743
		aead_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
				       template[i].ilen, iv);
744

745
		aead_request_set_ad(req, template[i].alen);
746

747 748
		ret = crypto_wait_req(enc ? crypto_aead_encrypt(req)
				      : crypto_aead_decrypt(req), &wait);
749

750 751 752 753 754 755 756 757
		switch (ret) {
		case 0:
			if (template[i].novrfy) {
				/* verification was supposed to fail */
				pr_err("alg: aead%s: %s failed on test %d for %s: ret was 0, expected -EBADMSG\n",
				       d, e, j, algo);
				/* so really, we got a bad message */
				ret = -EBADMSG;
758 759
				goto out;
			}
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
			break;
		case -EBADMSG:
			if (template[i].novrfy)
				/* verification failure was expected */
				continue;
			/* fall through */
		default:
			pr_err("alg: aead%s: %s failed on test %d for %s: ret=%d\n",
			       d, e, j, algo, -ret);
			goto out;
		}

		q = output;
		if (memcmp(q, template[i].result, template[i].rlen)) {
			pr_err("alg: aead%s: Test %d failed on %s for %s\n",
			       d, j, e, algo);
			hexdump(q, template[i].rlen);
			ret = -EINVAL;
			goto out;
779 780 781 782
		}
	}

	for (i = 0, j = 0; i < tcount; i++) {
783 784 785 786
		/* alignment tests are only done with continuous buffers */
		if (align_offset != 0)
			break;

787 788
		if (!template[i].np)
			continue;
789

790
		j++;
791

792
		if (template[i].iv)
793
			memcpy(iv, template[i].iv, iv_len);
794 795 796 797 798 799 800 801 802 803 804 805 806
		else
			memset(iv, 0, MAX_IVLEN);

		crypto_aead_clear_flags(tfm, ~0);
		if (template[i].wk)
			crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
		if (template[i].klen > MAX_KEYLEN) {
			pr_err("alg: aead%s: setkey failed on test %d for %s: key size %d > %d\n",
			       d, j, algo, template[i].klen, MAX_KEYLEN);
			ret = -EINVAL;
			goto out;
		}
		memcpy(key, template[i].key, template[i].klen);
807

808
		ret = crypto_aead_setkey(tfm, key, template[i].klen);
809
		if (template[i].fail == !ret) {
810 811 812 813 814
			pr_err("alg: aead%s: setkey failed on chunk test %d for %s: flags=%x\n",
			       d, j, algo, crypto_aead_get_flags(tfm));
			goto out;
		} else if (ret)
			continue;
815

816
		authsize = abs(template[i].rlen - template[i].ilen);
817

818
		ret = -EINVAL;
819
		sg_init_table(sg, template[i].anp + template[i].np);
820
		if (diff_dst)
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
			sg_init_table(sgout, template[i].anp + template[i].np);

		ret = -EINVAL;
		for (k = 0, temp = 0; k < template[i].anp; k++) {
			if (WARN_ON(offset_in_page(IDX[k]) +
				    template[i].atap[k] > PAGE_SIZE))
				goto out;
			sg_set_buf(&sg[k],
				   memcpy(axbuf[IDX[k] >> PAGE_SHIFT] +
					  offset_in_page(IDX[k]),
					  template[i].assoc + temp,
					  template[i].atap[k]),
				   template[i].atap[k]);
			if (diff_dst)
				sg_set_buf(&sgout[k],
					   axbuf[IDX[k] >> PAGE_SHIFT] +
					   offset_in_page(IDX[k]),
					   template[i].atap[k]);
			temp += template[i].atap[k];
		}

842 843 844 845
		for (k = 0, temp = 0; k < template[i].np; k++) {
			if (WARN_ON(offset_in_page(IDX[k]) +
				    template[i].tap[k] > PAGE_SIZE))
				goto out;
846

847 848
			q = xbuf[IDX[k] >> PAGE_SHIFT] + offset_in_page(IDX[k]);
			memcpy(q, template[i].input + temp, template[i].tap[k]);
849 850
			sg_set_buf(&sg[template[i].anp + k],
				   q, template[i].tap[k]);
851

852 853 854
			if (diff_dst) {
				q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
				    offset_in_page(IDX[k]);
855

856
				memset(q, 0, template[i].tap[k]);
857

858 859
				sg_set_buf(&sgout[template[i].anp + k],
					   q, template[i].tap[k]);
860
			}
861

862 863 864 865 866
			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;
867

868 869
			temp += template[i].tap[k];
		}
870

871 872 873 874 875 876
		ret = crypto_aead_setauthsize(tfm, authsize);
		if (ret) {
			pr_err("alg: aead%s: Failed to set authsize to %u on chunk test %d for %s\n",
			       d, authsize, j, algo);
			goto out;
		}
877

878
		if (enc) {
879 880 881
			if (WARN_ON(sg[template[i].anp + k - 1].offset +
				    sg[template[i].anp + k - 1].length +
				    authsize > PAGE_SIZE)) {
882
				ret = -EINVAL;
883 884 885
				goto out;
			}

886
			if (diff_dst)
887 888 889
				sgout[template[i].anp + k - 1].length +=
					authsize;
			sg[template[i].anp + k - 1].length += authsize;
890
		}
891

892 893 894
		aead_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
				       template[i].ilen,
				       iv);
895

896
		aead_request_set_ad(req, template[i].alen);
897

898 899
		ret = crypto_wait_req(enc ? crypto_aead_encrypt(req)
				      : crypto_aead_decrypt(req), &wait);
900

901 902 903 904 905 906 907 908
		switch (ret) {
		case 0:
			if (template[i].novrfy) {
				/* verification was supposed to fail */
				pr_err("alg: aead%s: %s failed on chunk test %d for %s: ret was 0, expected -EBADMSG\n",
				       d, e, j, algo);
				/* so really, we got a bad message */
				ret = -EBADMSG;
909 910
				goto out;
			}
911 912 913 914 915 916 917 918 919 920 921
			break;
		case -EBADMSG:
			if (template[i].novrfy)
				/* verification failure was expected */
				continue;
			/* fall through */
		default:
			pr_err("alg: aead%s: %s failed on chunk test %d for %s: ret=%d\n",
			       d, e, j, algo, -ret);
			goto out;
		}
922

923 924 925 926 927 928 929 930
		ret = -EINVAL;
		for (k = 0, temp = 0; k < template[i].np; k++) {
			if (diff_dst)
				q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
				    offset_in_page(IDX[k]);
			else
				q = xbuf[IDX[k] >> PAGE_SHIFT] +
				    offset_in_page(IDX[k]);
931

932 933 934
			n = template[i].tap[k];
			if (k == template[i].np - 1)
				n += enc ? authsize : -authsize;
935

936 937 938 939 940 941
			if (memcmp(q, template[i].result + temp, n)) {
				pr_err("alg: aead%s: Chunk test %d failed on %s at page %u for %s\n",
				       d, j, e, k, algo);
				hexdump(q, n);
				goto out;
			}
942

943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
			q += n;
			if (k == template[i].np - 1 && !enc) {
				if (!diff_dst &&
					memcmp(q, template[i].input +
					      temp + n, authsize))
					n = authsize;
				else
					n = 0;
			} else {
				for (n = 0; offset_in_page(q + n) && q[n]; n++)
					;
			}
			if (n) {
				pr_err("alg: aead%s: Result buffer corruption in chunk test %d on %s at page %u for %s: %u bytes:\n",
				       d, j, e, k, algo, n);
				hexdump(q, n);
				goto out;
960
			}
961 962

			temp += template[i].tap[k];
963 964 965 966 967 968 969
		}
	}

	ret = 0;

out:
	aead_request_free(req);
970 971 972 973 974
	kfree(sg);
out_nosg:
	if (diff_dst)
		testmgr_free_buf(xoutbuf);
out_nooutbuf:
975 976 977 978
	testmgr_free_buf(axbuf);
out_noaxbuf:
	testmgr_free_buf(xbuf);
out_noxbuf:
979
	kfree(key);
980
	kfree(iv);
981 982 983
	return ret;
}

984
static int test_aead(struct crypto_aead *tfm, int enc,
985
		     const struct aead_testvec *template, unsigned int tcount)
986
{
987
	unsigned int alignmask;
988 989 990
	int ret;

	/* test 'dst == src' case */
991
	ret = __test_aead(tfm, enc, template, tcount, false, 0);
992 993 994 995
	if (ret)
		return ret;

	/* test 'dst != src' case */
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
	ret = __test_aead(tfm, enc, template, tcount, true, 0);
	if (ret)
		return ret;

	/* test unaligned buffers, check with one byte offset */
	ret = __test_aead(tfm, enc, template, tcount, true, 1);
	if (ret)
		return ret;

	alignmask = crypto_tfm_alg_alignmask(&tfm->base);
	if (alignmask) {
		/* Check if alignment mask for tfm is correctly set. */
		ret = __test_aead(tfm, enc, template, tcount, true,
				  alignmask + 1);
		if (ret)
			return ret;
	}

	return 0;
1015 1016
}

1017
static int test_cipher(struct crypto_cipher *tfm, int enc,
1018 1019
		       const struct cipher_testvec *template,
		       unsigned int tcount)
1020 1021 1022 1023 1024
{
	const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
	unsigned int i, j, k;
	char *q;
	const char *e;
1025
	const char *input, *result;
1026
	void *data;
1027 1028 1029 1030 1031
	char *xbuf[XBUFSIZE];
	int ret = -ENOMEM;

	if (testmgr_alloc_buf(xbuf))
		goto out_nobuf;
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042

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

	j = 0;
	for (i = 0; i < tcount; i++) {
		if (template[i].np)
			continue;

1043 1044 1045
		if (fips_enabled && template[i].fips_skip)
			continue;

1046 1047
		input  = enc ? template[i].ptext : template[i].ctext;
		result = enc ? template[i].ctext : template[i].ptext;
1048 1049
		j++;

1050
		ret = -EINVAL;
1051
		if (WARN_ON(template[i].len > PAGE_SIZE))
1052 1053
			goto out;

1054
		data = xbuf[0];
1055
		memcpy(data, input, template[i].len);
1056 1057 1058 1059 1060 1061 1062

		crypto_cipher_clear_flags(tfm, ~0);
		if (template[i].wk)
			crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);

		ret = crypto_cipher_setkey(tfm, template[i].key,
					   template[i].klen);
1063
		if (template[i].fail == !ret) {
1064 1065 1066 1067 1068 1069 1070
			printk(KERN_ERR "alg: cipher: setkey failed "
			       "on test %d for %s: flags=%x\n", j,
			       algo, crypto_cipher_get_flags(tfm));
			goto out;
		} else if (ret)
			continue;

1071
		for (k = 0; k < template[i].len;
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
		     k += crypto_cipher_blocksize(tfm)) {
			if (enc)
				crypto_cipher_encrypt_one(tfm, data + k,
							  data + k);
			else
				crypto_cipher_decrypt_one(tfm, data + k,
							  data + k);
		}

		q = data;
1082
		if (memcmp(q, result, template[i].len)) {
1083 1084
			printk(KERN_ERR "alg: cipher: Test %d failed "
			       "on %s for %s\n", j, e, algo);
1085
			hexdump(q, template[i].len);
1086 1087 1088 1089 1090 1091 1092 1093
			ret = -EINVAL;
			goto out;
		}
	}

	ret = 0;

out:
1094 1095
	testmgr_free_buf(xbuf);
out_nobuf:
1096 1097 1098
	return ret;
}

1099
static int __test_skcipher(struct crypto_skcipher *tfm, int enc,
1100 1101
			   const struct cipher_testvec *template,
			   unsigned int tcount,
1102
			   const bool diff_dst, const int align_offset)
1103 1104
{
	const char *algo =
1105
		crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm));
1106 1107
	unsigned int i, j, k, n, temp;
	char *q;
1108
	struct skcipher_request *req;
1109
	struct scatterlist sg[8];
1110 1111
	struct scatterlist sgout[8];
	const char *e, *d;
1112
	struct crypto_wait wait;
1113
	const char *input, *result;
1114 1115
	void *data;
	char iv[MAX_IVLEN];
1116
	char *xbuf[XBUFSIZE];
1117
	char *xoutbuf[XBUFSIZE];
1118
	int ret = -ENOMEM;
1119
	unsigned int ivsize = crypto_skcipher_ivsize(tfm);
1120 1121 1122

	if (testmgr_alloc_buf(xbuf))
		goto out_nobuf;
1123

1124 1125 1126 1127 1128 1129 1130 1131
	if (diff_dst && testmgr_alloc_buf(xoutbuf))
		goto out_nooutbuf;

	if (diff_dst)
		d = "-ddst";
	else
		d = "";

1132 1133 1134 1135 1136
	if (enc == ENCRYPT)
	        e = "encryption";
	else
		e = "decryption";

1137
	crypto_init_wait(&wait);
1138

1139
	req = skcipher_request_alloc(tfm, GFP_KERNEL);
1140
	if (!req) {
1141 1142
		pr_err("alg: skcipher%s: Failed to allocate request for %s\n",
		       d, algo);
1143 1144 1145
		goto out;
	}

1146
	skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1147
				      crypto_req_done, &wait);
1148 1149 1150

	j = 0;
	for (i = 0; i < tcount; i++) {
1151 1152 1153
		if (template[i].np && !template[i].also_non_np)
			continue;

1154 1155 1156
		if (fips_enabled && template[i].fips_skip)
			continue;

1157
		if (template[i].iv && !(template[i].generates_iv && enc))
1158
			memcpy(iv, template[i].iv, ivsize);
1159 1160 1161
		else
			memset(iv, 0, MAX_IVLEN);

1162 1163
		input  = enc ? template[i].ptext : template[i].ctext;
		result = enc ? template[i].ctext : template[i].ptext;
1164 1165
		j++;
		ret = -EINVAL;
1166
		if (WARN_ON(align_offset + template[i].len > PAGE_SIZE))
1167
			goto out;
1168

1169 1170
		data = xbuf[0];
		data += align_offset;
1171
		memcpy(data, input, template[i].len);
1172

1173
		crypto_skcipher_clear_flags(tfm, ~0);
1174
		if (template[i].wk)
1175 1176
			crypto_skcipher_set_flags(tfm,
						  CRYPTO_TFM_REQ_WEAK_KEY);
1177

1178 1179
		ret = crypto_skcipher_setkey(tfm, template[i].key,
					     template[i].klen);
1180
		if (template[i].fail == !ret) {
1181
			pr_err("alg: skcipher%s: setkey failed on test %d for %s: flags=%x\n",
1182
			       d, j, algo, crypto_skcipher_get_flags(tfm));
1183 1184 1185 1186
			goto out;
		} else if (ret)
			continue;

1187
		sg_init_one(&sg[0], data, template[i].len);
1188 1189 1190
		if (diff_dst) {
			data = xoutbuf[0];
			data += align_offset;
1191
			sg_init_one(&sgout[0], data, template[i].len);
1192
		}
1193

1194
		skcipher_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
1195
					   template[i].len, iv);
1196 1197
		ret = crypto_wait_req(enc ? crypto_skcipher_encrypt(req) :
				      crypto_skcipher_decrypt(req), &wait);
1198

1199
		if (ret) {
1200 1201 1202 1203
			pr_err("alg: skcipher%s: %s failed on test %d for %s: ret=%d\n",
			       d, e, j, algo, -ret);
			goto out;
		}
1204

1205
		q = data;
1206
		if (memcmp(q, result, template[i].len)) {
1207
			pr_err("alg: skcipher%s: Test %d failed (invalid result) on %s for %s\n",
1208
			       d, j, e, algo);
1209
			hexdump(q, template[i].len);
1210 1211
			ret = -EINVAL;
			goto out;
1212
		}
1213

1214 1215
		if (template[i].generates_iv && enc &&
		    memcmp(iv, template[i].iv, crypto_skcipher_ivsize(tfm))) {
1216 1217 1218 1219 1220 1221
			pr_err("alg: skcipher%s: Test %d failed (invalid output IV) on %s for %s\n",
			       d, j, e, algo);
			hexdump(iv, crypto_skcipher_ivsize(tfm));
			ret = -EINVAL;
			goto out;
		}
1222 1223 1224 1225
	}

	j = 0;
	for (i = 0; i < tcount; i++) {
1226 1227 1228
		/* alignment tests are only done with continuous buffers */
		if (align_offset != 0)
			break;
1229

1230 1231 1232
		if (!template[i].np)
			continue;

1233 1234 1235
		if (fips_enabled && template[i].fips_skip)
			continue;

1236
		if (template[i].iv && !(template[i].generates_iv && enc))
1237
			memcpy(iv, template[i].iv, ivsize);
1238 1239 1240
		else
			memset(iv, 0, MAX_IVLEN);

1241 1242
		input  = enc ? template[i].ptext : template[i].ctext;
		result = enc ? template[i].ctext : template[i].ptext;
1243
		j++;
1244
		crypto_skcipher_clear_flags(tfm, ~0);
1245
		if (template[i].wk)
1246 1247
			crypto_skcipher_set_flags(tfm,
						  CRYPTO_TFM_REQ_WEAK_KEY);
1248

1249 1250
		ret = crypto_skcipher_setkey(tfm, template[i].key,
					     template[i].klen);
1251
		if (template[i].fail == !ret) {
1252
			pr_err("alg: skcipher%s: setkey failed on chunk test %d for %s: flags=%x\n",
1253
			       d, j, algo, crypto_skcipher_get_flags(tfm));
1254 1255 1256
			goto out;
		} else if (ret)
			continue;
1257

1258 1259 1260 1261 1262 1263 1264 1265
		temp = 0;
		ret = -EINVAL;
		sg_init_table(sg, template[i].np);
		if (diff_dst)
			sg_init_table(sgout, template[i].np);
		for (k = 0; k < template[i].np; k++) {
			if (WARN_ON(offset_in_page(IDX[k]) +
				    template[i].tap[k] > PAGE_SIZE))
1266 1267
				goto out;

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

1270
			memcpy(q, input + temp, template[i].tap[k]);
1271 1272 1273 1274 1275 1276 1277

			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]);
			if (diff_dst) {
				q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
1278 1279
				    offset_in_page(IDX[k]);

1280
				sg_set_buf(&sgout[k], q, template[i].tap[k]);
1281

1282 1283 1284
				memset(q, 0, template[i].tap[k]);
				if (offset_in_page(q) +
				    template[i].tap[k] < PAGE_SIZE)
1285
					q[template[i].tap[k]] = 0;
1286
			}
1287

1288 1289
			temp += template[i].tap[k];
		}
1290

1291
		skcipher_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
1292
					   template[i].len, iv);
1293

1294 1295
		ret = crypto_wait_req(enc ? crypto_skcipher_encrypt(req) :
				      crypto_skcipher_decrypt(req), &wait);
1296

1297
		if (ret) {
1298 1299 1300 1301
			pr_err("alg: skcipher%s: %s failed on chunk test %d for %s: ret=%d\n",
			       d, e, j, algo, -ret);
			goto out;
		}
1302

1303 1304 1305 1306 1307 1308 1309 1310 1311
		temp = 0;
		ret = -EINVAL;
		for (k = 0; k < template[i].np; k++) {
			if (diff_dst)
				q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
				    offset_in_page(IDX[k]);
			else
				q = xbuf[IDX[k] >> PAGE_SHIFT] +
				    offset_in_page(IDX[k]);
1312

1313
			if (memcmp(q, result + temp, template[i].tap[k])) {
1314 1315 1316
				pr_err("alg: skcipher%s: Chunk test %d failed on %s at page %u for %s\n",
				       d, j, e, k, algo);
				hexdump(q, template[i].tap[k]);
1317 1318 1319
				goto out;
			}

1320 1321 1322 1323 1324 1325 1326 1327
			q += template[i].tap[k];
			for (n = 0; offset_in_page(q + n) && q[n]; n++)
				;
			if (n) {
				pr_err("alg: skcipher%s: Result buffer corruption in chunk test %d on %s at page %u for %s: %u bytes:\n",
				       d,