1/*
2 * Algorithm testing framework and tests.
3 *
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
6 * Copyright (c) 2007 Nokia Siemens Networks
7 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
12 * any later version.
13 *
14 */
15
16#include <crypto/hash.h>
17#include <linux/err.h>
18#include <linux/module.h>
19#include <linux/scatterlist.h>
20#include <linux/slab.h>
21#include <linux/string.h>
22#include <crypto/rng.h>
23
24#include "internal.h"
25
26#ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
27
28/* a perfect nop */
29int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
30{
31	return 0;
32}
33
34#else
35
36#include "testmgr.h"
37
38/*
39 * Need slab memory for testing (size in number of pages).
40 */
41#define XBUFSIZE	8
42
43/*
44 * Indexes into the xbuf to simulate cross-page access.
45 */
46#define IDX1		32
47#define IDX2		32400
48#define IDX3		1
49#define IDX4		8193
50#define IDX5		22222
51#define IDX6		17101
52#define IDX7		27333
53#define IDX8		3000
54
55/*
56* Used by test_cipher()
57*/
58#define ENCRYPT 1
59#define DECRYPT 0
60
61struct tcrypt_result {
62	struct completion completion;
63	int err;
64};
65
66struct aead_test_suite {
67	struct {
68		struct aead_testvec *vecs;
69		unsigned int count;
70	} enc, dec;
71};
72
73struct cipher_test_suite {
74	struct {
75		struct cipher_testvec *vecs;
76		unsigned int count;
77	} enc, dec;
78};
79
80struct comp_test_suite {
81	struct {
82		struct comp_testvec *vecs;
83		unsigned int count;
84	} comp, decomp;
85};
86
87struct pcomp_test_suite {
88	struct {
89		struct pcomp_testvec *vecs;
90		unsigned int count;
91	} comp, decomp;
92};
93
94struct hash_test_suite {
95	struct hash_testvec *vecs;
96	unsigned int count;
97};
98
99struct cprng_test_suite {
100	struct cprng_testvec *vecs;
101	unsigned int count;
102};
103
104struct alg_test_desc {
105	const char *alg;
106	int (*test)(const struct alg_test_desc *desc, const char *driver,
107		    u32 type, u32 mask);
108	int fips_allowed;	/* set if alg is allowed in fips mode */
109
110	union {
111		struct aead_test_suite aead;
112		struct cipher_test_suite cipher;
113		struct comp_test_suite comp;
114		struct pcomp_test_suite pcomp;
115		struct hash_test_suite hash;
116		struct cprng_test_suite cprng;
117	} suite;
118};
119
120static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
121
122static void hexdump(unsigned char *buf, unsigned int len)
123{
124	print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
125			16, 1,
126			buf, len, false);
127}
128
129static void tcrypt_complete(struct crypto_async_request *req, int err)
130{
131	struct tcrypt_result *res = req->data;
132
133	if (err == -EINPROGRESS)
134		return;
135
136	res->err = err;
137	complete(&res->completion);
138}
139
140static int testmgr_alloc_buf(char *buf[XBUFSIZE])
141{
142	int i;
143
144	for (i = 0; i < XBUFSIZE; i++) {
145		buf[i] = (void *)__get_free_page(GFP_KERNEL);
146		if (!buf[i])
147			goto err_free_buf;
148	}
149
150	return 0;
151
152err_free_buf:
153	while (i-- > 0)
154		free_page((unsigned long)buf[i]);
155
156	return -ENOMEM;
157}
158
159static void testmgr_free_buf(char *buf[XBUFSIZE])
160{
161	int i;
162
163	for (i = 0; i < XBUFSIZE; i++)
164		free_page((unsigned long)buf[i]);
165}
166
167static int do_one_async_hash_op(struct ahash_request *req,
168				struct tcrypt_result *tr,
169				int ret)
170{
171	if (ret == -EINPROGRESS || ret == -EBUSY) {
172		ret = wait_for_completion_interruptible(&tr->completion);
173		if (!ret)
174			ret = tr->err;
175		INIT_COMPLETION(tr->completion);
176	}
177	return ret;
178}
179
180static int test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
181		     unsigned int tcount, bool use_digest)
182{
183	const char *algo = crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
184	unsigned int i, j, k, temp;
185	struct scatterlist sg[8];
186	char result[64];
187	struct ahash_request *req;
188	struct tcrypt_result tresult;
189	void *hash_buff;
190	char *xbuf[XBUFSIZE];
191	int ret = -ENOMEM;
192
193	if (testmgr_alloc_buf(xbuf))
194		goto out_nobuf;
195
196	init_completion(&tresult.completion);
197
198	req = ahash_request_alloc(tfm, GFP_KERNEL);
199	if (!req) {
200		printk(KERN_ERR "alg: hash: Failed to allocate request for "
201		       "%s\n", algo);
202		goto out_noreq;
203	}
204	ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
205				   tcrypt_complete, &tresult);
206
207	j = 0;
208	for (i = 0; i < tcount; i++) {
209		if (template[i].np)
210			continue;
211
212		j++;
213		memset(result, 0, 64);
214
215		hash_buff = xbuf[0];
216
217		memcpy(hash_buff, template[i].plaintext, template[i].psize);
218		sg_init_one(&sg[0], hash_buff, template[i].psize);
219
220		if (template[i].ksize) {
221			crypto_ahash_clear_flags(tfm, ~0);
222			ret = crypto_ahash_setkey(tfm, template[i].key,
223						  template[i].ksize);
224			if (ret) {
225				printk(KERN_ERR "alg: hash: setkey failed on "
226				       "test %d for %s: ret=%d\n", j, algo,
227				       -ret);
228				goto out;
229			}
230		}
231
232		ahash_request_set_crypt(req, sg, result, template[i].psize);
233		if (use_digest) {
234			ret = do_one_async_hash_op(req, &tresult,
235						   crypto_ahash_digest(req));
236			if (ret) {
237				pr_err("alg: hash: digest failed on test %d "
238				       "for %s: ret=%d\n", j, algo, -ret);
239				goto out;
240			}
241		} else {
242			ret = do_one_async_hash_op(req, &tresult,
243						   crypto_ahash_init(req));
244			if (ret) {
245				pr_err("alt: hash: init failed on test %d "
246				       "for %s: ret=%d\n", j, algo, -ret);
247				goto out;
248			}
249			ret = do_one_async_hash_op(req, &tresult,
250						   crypto_ahash_update(req));
251			if (ret) {
252				pr_err("alt: hash: update failed on test %d "
253				       "for %s: ret=%d\n", j, algo, -ret);
254				goto out;
255			}
256			ret = do_one_async_hash_op(req, &tresult,
257						   crypto_ahash_final(req));
258			if (ret) {
259				pr_err("alt: hash: final failed on test %d "
260				       "for %s: ret=%d\n", j, algo, -ret);
261				goto out;
262			}
263		}
264
265		if (memcmp(result, template[i].digest,
266			   crypto_ahash_digestsize(tfm))) {
267			printk(KERN_ERR "alg: hash: Test %d failed for %s\n",
268			       j, algo);
269			hexdump(result, crypto_ahash_digestsize(tfm));
270			ret = -EINVAL;
271			goto out;
272		}
273	}
274
275	j = 0;
276	for (i = 0; i < tcount; i++) {
277		if (template[i].np) {
278			j++;
279			memset(result, 0, 64);
280
281			temp = 0;
282			sg_init_table(sg, template[i].np);
283			ret = -EINVAL;
284			for (k = 0; k < template[i].np; k++) {
285				if (WARN_ON(offset_in_page(IDX[k]) +
286					    template[i].tap[k] > PAGE_SIZE))
287					goto out;
288				sg_set_buf(&sg[k],
289					   memcpy(xbuf[IDX[k] >> PAGE_SHIFT] +
290						  offset_in_page(IDX[k]),
291						  template[i].plaintext + temp,
292						  template[i].tap[k]),
293					   template[i].tap[k]);
294				temp += template[i].tap[k];
295			}
296
297			if (template[i].ksize) {
298				crypto_ahash_clear_flags(tfm, ~0);
299				ret = crypto_ahash_setkey(tfm, template[i].key,
300							  template[i].ksize);
301
302				if (ret) {
303					printk(KERN_ERR "alg: hash: setkey "
304					       "failed on chunking test %d "
305					       "for %s: ret=%d\n", j, algo,
306					       -ret);
307					goto out;
308				}
309			}
310
311			ahash_request_set_crypt(req, sg, result,
312						template[i].psize);
313			ret = crypto_ahash_digest(req);
314			switch (ret) {
315			case 0:
316				break;
317			case -EINPROGRESS:
318			case -EBUSY:
319				ret = wait_for_completion_interruptible(
320					&tresult.completion);
321				if (!ret && !(ret = tresult.err)) {
322					INIT_COMPLETION(tresult.completion);
323					break;
324				}
325				/* fall through */
326			default:
327				printk(KERN_ERR "alg: hash: digest failed "
328				       "on chunking test %d for %s: "
329				       "ret=%d\n", j, algo, -ret);
330				goto out;
331			}
332
333			if (memcmp(result, template[i].digest,
334				   crypto_ahash_digestsize(tfm))) {
335				printk(KERN_ERR "alg: hash: Chunking test %d "
336				       "failed for %s\n", j, algo);
337				hexdump(result, crypto_ahash_digestsize(tfm));
338				ret = -EINVAL;
339				goto out;
340			}
341		}
342	}
343
344	ret = 0;
345
346out:
347	ahash_request_free(req);
348out_noreq:
349	testmgr_free_buf(xbuf);
350out_nobuf:
351	return ret;
352}
353
354static int test_aead(struct crypto_aead *tfm, int enc,
355		     struct aead_testvec *template, unsigned int tcount)
356{
357	const char *algo = crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm));
358	unsigned int i, j, k, n, temp;
359	int ret = -ENOMEM;
360	char *q;
361	char *key;
362	struct aead_request *req;
363	struct scatterlist sg[8];
364	struct scatterlist asg[8];
365	const char *e;
366	struct tcrypt_result result;
367	unsigned int authsize;
368	void *input;
369	void *assoc;
370	char iv[MAX_IVLEN];
371	char *xbuf[XBUFSIZE];
372	char *axbuf[XBUFSIZE];
373
374	if (testmgr_alloc_buf(xbuf))
375		goto out_noxbuf;
376	if (testmgr_alloc_buf(axbuf))
377		goto out_noaxbuf;
378
379	if (enc == ENCRYPT)
380		e = "encryption";
381	else
382		e = "decryption";
383
384	init_completion(&result.completion);
385
386	req = aead_request_alloc(tfm, GFP_KERNEL);
387	if (!req) {
388		printk(KERN_ERR "alg: aead: Failed to allocate request for "
389		       "%s\n", algo);
390		goto out;
391	}
392
393	aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
394				  tcrypt_complete, &result);
395
396	for (i = 0, j = 0; i < tcount; i++) {
397		if (!template[i].np) {
398			j++;
399
400			/* some tepmplates have no input data but they will
401			 * touch input
402			 */
403			input = xbuf[0];
404			assoc = axbuf[0];
405
406			ret = -EINVAL;
407			if (WARN_ON(template[i].ilen > PAGE_SIZE ||
408				    template[i].alen > PAGE_SIZE))
409				goto out;
410
411			memcpy(input, template[i].input, template[i].ilen);
412			memcpy(assoc, template[i].assoc, template[i].alen);
413			if (template[i].iv)
414				memcpy(iv, template[i].iv, MAX_IVLEN);
415			else
416				memset(iv, 0, MAX_IVLEN);
417
418			crypto_aead_clear_flags(tfm, ~0);
419			if (template[i].wk)
420				crypto_aead_set_flags(
421					tfm, CRYPTO_TFM_REQ_WEAK_KEY);
422
423			key = template[i].key;
424
425			ret = crypto_aead_setkey(tfm, key,
426						 template[i].klen);
427			if (!ret == template[i].fail) {
428				printk(KERN_ERR "alg: aead: setkey failed on "
429				       "test %d for %s: flags=%x\n", j, algo,
430				       crypto_aead_get_flags(tfm));
431				goto out;
432			} else if (ret)
433				continue;
434
435			authsize = abs(template[i].rlen - template[i].ilen);
436			ret = crypto_aead_setauthsize(tfm, authsize);
437			if (ret) {
438				printk(KERN_ERR "alg: aead: Failed to set "
439				       "authsize to %u on test %d for %s\n",
440				       authsize, j, algo);
441				goto out;
442			}
443
444			sg_init_one(&sg[0], input,
445				    template[i].ilen + (enc ? authsize : 0));
446
447			sg_init_one(&asg[0], assoc, template[i].alen);
448
449			aead_request_set_crypt(req, sg, sg,
450					       template[i].ilen, iv);
451
452			aead_request_set_assoc(req, asg, template[i].alen);
453
454			ret = enc ?
455				crypto_aead_encrypt(req) :
456				crypto_aead_decrypt(req);
457
458			switch (ret) {
459			case 0:
460				if (template[i].novrfy) {
461					/* verification was supposed to fail */
462					printk(KERN_ERR "alg: aead: %s failed "
463					       "on test %d for %s: ret was 0, "
464					       "expected -EBADMSG\n",
465					       e, j, algo);
466					/* so really, we got a bad message */
467					ret = -EBADMSG;
468					goto out;
469				}
470				break;
471			case -EINPROGRESS:
472			case -EBUSY:
473				ret = wait_for_completion_interruptible(
474					&result.completion);
475				if (!ret && !(ret = result.err)) {
476					INIT_COMPLETION(result.completion);
477					break;
478				}
479			case -EBADMSG:
480				if (template[i].novrfy)
481					/* verification failure was expected */
482					continue;
483				/* fall through */
484			default:
485				printk(KERN_ERR "alg: aead: %s failed on test "
486				       "%d for %s: ret=%d\n", e, j, algo, -ret);
487				goto out;
488			}
489
490			q = input;
491			if (memcmp(q, template[i].result, template[i].rlen)) {
492				printk(KERN_ERR "alg: aead: Test %d failed on "
493				       "%s for %s\n", j, e, algo);
494				hexdump(q, template[i].rlen);
495				ret = -EINVAL;
496				goto out;
497			}
498		}
499	}
500
501	for (i = 0, j = 0; i < tcount; i++) {
502		if (template[i].np) {
503			j++;
504
505			if (template[i].iv)
506				memcpy(iv, template[i].iv, MAX_IVLEN);
507			else
508				memset(iv, 0, MAX_IVLEN);
509
510			crypto_aead_clear_flags(tfm, ~0);
511			if (template[i].wk)
512				crypto_aead_set_flags(
513					tfm, CRYPTO_TFM_REQ_WEAK_KEY);
514			key = template[i].key;
515
516			ret = crypto_aead_setkey(tfm, key, template[i].klen);
517			if (!ret == template[i].fail) {
518				printk(KERN_ERR "alg: aead: setkey failed on "
519				       "chunk test %d for %s: flags=%x\n", j,
520				       algo, crypto_aead_get_flags(tfm));
521				goto out;
522			} else if (ret)
523				continue;
524
525			authsize = abs(template[i].rlen - template[i].ilen);
526
527			ret = -EINVAL;
528			sg_init_table(sg, template[i].np);
529			for (k = 0, temp = 0; k < template[i].np; k++) {
530				if (WARN_ON(offset_in_page(IDX[k]) +
531					    template[i].tap[k] > PAGE_SIZE))
532					goto out;
533
534				q = xbuf[IDX[k] >> PAGE_SHIFT] +
535				    offset_in_page(IDX[k]);
536
537				memcpy(q, template[i].input + temp,
538				       template[i].tap[k]);
539
540				n = template[i].tap[k];
541				if (k == template[i].np - 1 && enc)
542					n += authsize;
543				if (offset_in_page(q) + n < PAGE_SIZE)
544					q[n] = 0;
545
546				sg_set_buf(&sg[k], q, template[i].tap[k]);
547				temp += template[i].tap[k];
548			}
549
550			ret = crypto_aead_setauthsize(tfm, authsize);
551			if (ret) {
552				printk(KERN_ERR "alg: aead: Failed to set "
553				       "authsize to %u on chunk test %d for "
554				       "%s\n", authsize, j, algo);
555				goto out;
556			}
557
558			if (enc) {
559				if (WARN_ON(sg[k - 1].offset +
560					    sg[k - 1].length + authsize >
561					    PAGE_SIZE)) {
562					ret = -EINVAL;
563					goto out;
564				}
565
566				sg[k - 1].length += authsize;
567			}
568
569			sg_init_table(asg, template[i].anp);
570			ret = -EINVAL;
571			for (k = 0, temp = 0; k < template[i].anp; k++) {
572				if (WARN_ON(offset_in_page(IDX[k]) +
573					    template[i].atap[k] > PAGE_SIZE))
574					goto out;
575				sg_set_buf(&asg[k],
576					   memcpy(axbuf[IDX[k] >> PAGE_SHIFT] +
577						  offset_in_page(IDX[k]),
578						  template[i].assoc + temp,
579						  template[i].atap[k]),
580					   template[i].atap[k]);
581				temp += template[i].atap[k];
582			}
583
584			aead_request_set_crypt(req, sg, sg,
585					       template[i].ilen,
586					       iv);
587
588			aead_request_set_assoc(req, asg, template[i].alen);
589
590			ret = enc ?
591				crypto_aead_encrypt(req) :
592				crypto_aead_decrypt(req);
593
594			switch (ret) {
595			case 0:
596				if (template[i].novrfy) {
597					/* verification was supposed to fail */
598					printk(KERN_ERR "alg: aead: %s failed "
599					       "on chunk test %d for %s: ret "
600					       "was 0, expected -EBADMSG\n",
601					       e, j, algo);
602					/* so really, we got a bad message */
603					ret = -EBADMSG;
604					goto out;
605				}
606				break;
607			case -EINPROGRESS:
608			case -EBUSY:
609				ret = wait_for_completion_interruptible(
610					&result.completion);
611				if (!ret && !(ret = result.err)) {
612					INIT_COMPLETION(result.completion);
613					break;
614				}
615			case -EBADMSG:
616				if (template[i].novrfy)
617					/* verification failure was expected */
618					continue;
619				/* fall through */
620			default:
621				printk(KERN_ERR "alg: aead: %s failed on "
622				       "chunk test %d for %s: ret=%d\n", e, j,
623				       algo, -ret);
624				goto out;
625			}
626
627			ret = -EINVAL;
628			for (k = 0, temp = 0; k < template[i].np; k++) {
629				q = xbuf[IDX[k] >> PAGE_SHIFT] +
630				    offset_in_page(IDX[k]);
631
632				n = template[i].tap[k];
633				if (k == template[i].np - 1)
634					n += enc ? authsize : -authsize;
635
636				if (memcmp(q, template[i].result + temp, n)) {
637					printk(KERN_ERR "alg: aead: Chunk "
638					       "test %d failed on %s at page "
639					       "%u for %s\n", j, e, k, algo);
640					hexdump(q, n);
641					goto out;
642				}
643
644				q += n;
645				if (k == template[i].np - 1 && !enc) {
646					if (memcmp(q, template[i].input +
647						      temp + n, authsize))
648						n = authsize;
649					else
650						n = 0;
651				} else {
652					for (n = 0; offset_in_page(q + n) &&
653						    q[n]; n++)
654						;
655				}
656				if (n) {
657					printk(KERN_ERR "alg: aead: Result "
658					       "buffer corruption in chunk "
659					       "test %d on %s at page %u for "
660					       "%s: %u bytes:\n", j, e, k,
661					       algo, n);
662					hexdump(q, n);
663					goto out;
664				}
665
666				temp += template[i].tap[k];
667			}
668		}
669	}
670
671	ret = 0;
672
673out:
674	aead_request_free(req);
675	testmgr_free_buf(axbuf);
676out_noaxbuf:
677	testmgr_free_buf(xbuf);
678out_noxbuf:
679	return ret;
680}
681
682static int test_cipher(struct crypto_cipher *tfm, int enc,
683		       struct cipher_testvec *template, unsigned int tcount)
684{
685	const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
686	unsigned int i, j, k;
687	char *q;
688	const char *e;
689	void *data;
690	char *xbuf[XBUFSIZE];
691	int ret = -ENOMEM;
692
693	if (testmgr_alloc_buf(xbuf))
694		goto out_nobuf;
695
696	if (enc == ENCRYPT)
697	        e = "encryption";
698	else
699		e = "decryption";
700
701	j = 0;
702	for (i = 0; i < tcount; i++) {
703		if (template[i].np)
704			continue;
705
706		j++;
707
708		ret = -EINVAL;
709		if (WARN_ON(template[i].ilen > PAGE_SIZE))
710			goto out;
711
712		data = xbuf[0];
713		memcpy(data, template[i].input, template[i].ilen);
714
715		crypto_cipher_clear_flags(tfm, ~0);
716		if (template[i].wk)
717			crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
718
719		ret = crypto_cipher_setkey(tfm, template[i].key,
720					   template[i].klen);
721		if (!ret == template[i].fail) {
722			printk(KERN_ERR "alg: cipher: setkey failed "
723			       "on test %d for %s: flags=%x\n", j,
724			       algo, crypto_cipher_get_flags(tfm));
725			goto out;
726		} else if (ret)
727			continue;
728
729		for (k = 0; k < template[i].ilen;
730		     k += crypto_cipher_blocksize(tfm)) {
731			if (enc)
732				crypto_cipher_encrypt_one(tfm, data + k,
733							  data + k);
734			else
735				crypto_cipher_decrypt_one(tfm, data + k,
736							  data + k);
737		}
738
739		q = data;
740		if (memcmp(q, template[i].result, template[i].rlen)) {
741			printk(KERN_ERR "alg: cipher: Test %d failed "
742			       "on %s for %s\n", j, e, algo);
743			hexdump(q, template[i].rlen);
744			ret = -EINVAL;
745			goto out;
746		}
747	}
748
749	ret = 0;
750
751out:
752	testmgr_free_buf(xbuf);
753out_nobuf:
754	return ret;
755}
756
757static int test_skcipher(struct crypto_ablkcipher *tfm, int enc,
758			 struct cipher_testvec *template, unsigned int tcount)
759{
760	const char *algo =
761		crypto_tfm_alg_driver_name(crypto_ablkcipher_tfm(tfm));
762	unsigned int i, j, k, n, temp;
763	char *q;
764	struct ablkcipher_request *req;
765	struct scatterlist sg[8];
766	const char *e;
767	struct tcrypt_result result;
768	void *data;
769	char iv[MAX_IVLEN];
770	char *xbuf[XBUFSIZE];
771	int ret = -ENOMEM;
772
773	if (testmgr_alloc_buf(xbuf))
774		goto out_nobuf;
775
776	if (enc == ENCRYPT)
777	        e = "encryption";
778	else
779		e = "decryption";
780
781	init_completion(&result.completion);
782
783	req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
784	if (!req) {
785		printk(KERN_ERR "alg: skcipher: Failed to allocate request "
786		       "for %s\n", algo);
787		goto out;
788	}
789
790	ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
791					tcrypt_complete, &result);
792
793	j = 0;
794	for (i = 0; i < tcount; i++) {
795		if (template[i].iv)
796			memcpy(iv, template[i].iv, MAX_IVLEN);
797		else
798			memset(iv, 0, MAX_IVLEN);
799
800		if (!(template[i].np)) {
801			j++;
802
803			ret = -EINVAL;
804			if (WARN_ON(template[i].ilen > PAGE_SIZE))
805				goto out;
806
807			data = xbuf[0];
808			memcpy(data, template[i].input, template[i].ilen);
809
810			crypto_ablkcipher_clear_flags(tfm, ~0);
811			if (template[i].wk)
812				crypto_ablkcipher_set_flags(
813					tfm, CRYPTO_TFM_REQ_WEAK_KEY);
814
815			ret = crypto_ablkcipher_setkey(tfm, template[i].key,
816						       template[i].klen);
817			if (!ret == template[i].fail) {
818				printk(KERN_ERR "alg: skcipher: setkey failed "
819				       "on test %d for %s: flags=%x\n", j,
820				       algo, crypto_ablkcipher_get_flags(tfm));
821				goto out;
822			} else if (ret)
823				continue;
824
825			sg_init_one(&sg[0], data, template[i].ilen);
826
827			ablkcipher_request_set_crypt(req, sg, sg,
828						     template[i].ilen, iv);
829			ret = enc ?
830				crypto_ablkcipher_encrypt(req) :
831				crypto_ablkcipher_decrypt(req);
832
833			switch (ret) {
834			case 0:
835				break;
836			case -EINPROGRESS:
837			case -EBUSY:
838				ret = wait_for_completion_interruptible(
839					&result.completion);
840				if (!ret && !((ret = result.err))) {
841					INIT_COMPLETION(result.completion);
842					break;
843				}
844				/* fall through */
845			default:
846				printk(KERN_ERR "alg: skcipher: %s failed on "
847				       "test %d for %s: ret=%d\n", e, j, algo,
848				       -ret);
849				goto out;
850			}
851
852			q = data;
853			if (memcmp(q, template[i].result, template[i].rlen)) {
854				printk(KERN_ERR "alg: skcipher: Test %d "
855				       "failed on %s for %s\n", j, e, algo);
856				hexdump(q, template[i].rlen);
857				ret = -EINVAL;
858				goto out;
859			}
860		}
861	}
862
863	j = 0;
864	for (i = 0; i < tcount; i++) {
865
866		if (template[i].iv)
867			memcpy(iv, template[i].iv, MAX_IVLEN);
868		else
869			memset(iv, 0, MAX_IVLEN);
870
871		if (template[i].np) {
872			j++;
873
874			crypto_ablkcipher_clear_flags(tfm, ~0);
875			if (template[i].wk)
876				crypto_ablkcipher_set_flags(
877					tfm, CRYPTO_TFM_REQ_WEAK_KEY);
878
879			ret = crypto_ablkcipher_setkey(tfm, template[i].key,
880						       template[i].klen);
881			if (!ret == template[i].fail) {
882				printk(KERN_ERR "alg: skcipher: setkey failed "
883				       "on chunk test %d for %s: flags=%x\n",
884				       j, algo,
885				       crypto_ablkcipher_get_flags(tfm));
886				goto out;
887			} else if (ret)
888				continue;
889
890			temp = 0;
891			ret = -EINVAL;
892			sg_init_table(sg, template[i].np);
893			for (k = 0; k < template[i].np; k++) {
894				if (WARN_ON(offset_in_page(IDX[k]) +
895					    template[i].tap[k] > PAGE_SIZE))
896					goto out;
897
898				q = xbuf[IDX[k] >> PAGE_SHIFT] +
899				    offset_in_page(IDX[k]);
900
901				memcpy(q, template[i].input + temp,
902				       template[i].tap[k]);
903
904				if (offset_in_page(q) + template[i].tap[k] <
905				    PAGE_SIZE)
906					q[template[i].tap[k]] = 0;
907
908				sg_set_buf(&sg[k], q, template[i].tap[k]);
909
910				temp += template[i].tap[k];
911			}
912
913			ablkcipher_request_set_crypt(req, sg, sg,
914					template[i].ilen, iv);
915
916			ret = enc ?
917				crypto_ablkcipher_encrypt(req) :
918				crypto_ablkcipher_decrypt(req);
919
920			switch (ret) {
921			case 0:
922				break;
923			case -EINPROGRESS:
924			case -EBUSY:
925				ret = wait_for_completion_interruptible(
926					&result.completion);
927				if (!ret && !((ret = result.err))) {
928					INIT_COMPLETION(result.completion);
929					break;
930				}
931				/* fall through */
932			default:
933				printk(KERN_ERR "alg: skcipher: %s failed on "
934				       "chunk test %d for %s: ret=%d\n", e, j,
935				       algo, -ret);
936				goto out;
937			}
938
939			temp = 0;
940			ret = -EINVAL;
941			for (k = 0; k < template[i].np; k++) {
942				q = xbuf[IDX[k] >> PAGE_SHIFT] +
943				    offset_in_page(IDX[k]);
944
945				if (memcmp(q, template[i].result + temp,
946					   template[i].tap[k])) {
947					printk(KERN_ERR "alg: skcipher: Chunk "
948					       "test %d failed on %s at page "
949					       "%u for %s\n", j, e, k, algo);
950					hexdump(q, template[i].tap[k]);
951					goto out;
952				}
953
954				q += template[i].tap[k];
955				for (n = 0; offset_in_page(q + n) && q[n]; n++)
956					;
957				if (n) {
958					printk(KERN_ERR "alg: skcipher: "
959					       "Result buffer corruption in "
960					       "chunk test %d on %s at page "
961					       "%u for %s: %u bytes:\n", j, e,
962					       k, algo, n);
963					hexdump(q, n);
964					goto out;
965				}
966				temp += template[i].tap[k];
967			}
968		}
969	}
970
971	ret = 0;
972
973out:
974	ablkcipher_request_free(req);
975	testmgr_free_buf(xbuf);
976out_nobuf:
977	return ret;
978}
979
980static int test_comp(struct crypto_comp *tfm, struct comp_testvec *ctemplate,
981		     struct comp_testvec *dtemplate, int ctcount, int dtcount)
982{
983	const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
984	unsigned int i;
985	char result[COMP_BUF_SIZE];
986	int ret;
987
988	for (i = 0; i < ctcount; i++) {
989		int ilen;
990		unsigned int dlen = COMP_BUF_SIZE;
991
992		memset(result, 0, sizeof (result));
993
994		ilen = ctemplate[i].inlen;
995		ret = crypto_comp_compress(tfm, ctemplate[i].input,
996		                           ilen, result, &dlen);
997		if (ret) {
998			printk(KERN_ERR "alg: comp: compression failed "
999			       "on test %d for %s: ret=%d\n", i + 1, algo,
1000			       -ret);
1001			goto out;
1002		}
1003
1004		if (dlen != ctemplate[i].outlen) {
1005			printk(KERN_ERR "alg: comp: Compression test %d "
1006			       "failed for %s: output len = %d\n", i + 1, algo,
1007			       dlen);
1008			ret = -EINVAL;
1009			goto out;
1010		}
1011
1012		if (memcmp(result, ctemplate[i].output, dlen)) {
1013			printk(KERN_ERR "alg: comp: Compression test %d "
1014			       "failed for %s\n", i + 1, algo);
1015			hexdump(result, dlen);
1016			ret = -EINVAL;
1017			goto out;
1018		}
1019	}
1020
1021	for (i = 0; i < dtcount; i++) {
1022		int ilen;
1023		unsigned int dlen = COMP_BUF_SIZE;
1024
1025		memset(result, 0, sizeof (result));
1026
1027		ilen = dtemplate[i].inlen;
1028		ret = crypto_comp_decompress(tfm, dtemplate[i].input,
1029		                             ilen, result, &dlen);
1030		if (ret) {
1031			printk(KERN_ERR "alg: comp: decompression failed "
1032			       "on test %d for %s: ret=%d\n", i + 1, algo,
1033			       -ret);
1034			goto out;
1035		}
1036
1037		if (dlen != dtemplate[i].outlen) {
1038			printk(KERN_ERR "alg: comp: Decompression test %d "
1039			       "failed for %s: output len = %d\n", i + 1, algo,
1040			       dlen);
1041			ret = -EINVAL;
1042			goto out;
1043		}
1044
1045		if (memcmp(result, dtemplate[i].output, dlen)) {
1046			printk(KERN_ERR "alg: comp: Decompression test %d "
1047			       "failed for %s\n", i + 1, algo);
1048			hexdump(result, dlen);
1049			ret = -EINVAL;
1050			goto out;
1051		}
1052	}
1053
1054	ret = 0;
1055
1056out:
1057	return ret;
1058}
1059
1060static int test_pcomp(struct crypto_pcomp *tfm,
1061		      struct pcomp_testvec *ctemplate,
1062		      struct pcomp_testvec *dtemplate, int ctcount,
1063		      int dtcount)
1064{
1065	const char *algo = crypto_tfm_alg_driver_name(crypto_pcomp_tfm(tfm));
1066	unsigned int i;
1067	char result[COMP_BUF_SIZE];
1068	int res;
1069
1070	for (i = 0; i < ctcount; i++) {
1071		struct comp_request req;
1072		unsigned int produced = 0;
1073
1074		res = crypto_compress_setup(tfm, ctemplate[i].params,
1075					    ctemplate[i].paramsize);
1076		if (res) {
1077			pr_err("alg: pcomp: compression setup failed on test "
1078			       "%d for %s: error=%d\n", i + 1, algo, res);
1079			return res;
1080		}
1081
1082		res = crypto_compress_init(tfm);
1083		if (res) {
1084			pr_err("alg: pcomp: compression init failed on test "
1085			       "%d for %s: error=%d\n", i + 1, algo, res);
1086			return res;
1087		}
1088
1089		memset(result, 0, sizeof(result));
1090
1091		req.next_in = ctemplate[i].input;
1092		req.avail_in = ctemplate[i].inlen / 2;
1093		req.next_out = result;
1094		req.avail_out = ctemplate[i].outlen / 2;
1095
1096		res = crypto_compress_update(tfm, &req);
1097		if (res < 0 && (res != -EAGAIN || req.avail_in)) {
1098			pr_err("alg: pcomp: compression update failed on test "
1099			       "%d for %s: error=%d\n", i + 1, algo, res);
1100			return res;
1101		}
1102		if (res > 0)
1103			produced += res;
1104
1105		/* Add remaining input data */
1106		req.avail_in += (ctemplate[i].inlen + 1) / 2;
1107
1108		res = crypto_compress_update(tfm, &req);
1109		if (res < 0 && (res != -EAGAIN || req.avail_in)) {
1110			pr_err("alg: pcomp: compression update failed on test "
1111			       "%d for %s: error=%d\n", i + 1, algo, res);
1112			return res;
1113		}
1114		if (res > 0)
1115			produced += res;
1116
1117		/* Provide remaining output space */
1118		req.avail_out += COMP_BUF_SIZE - ctemplate[i].outlen / 2;
1119
1120		res = crypto_compress_final(tfm, &req);
1121		if (res < 0) {
1122			pr_err("alg: pcomp: compression final failed on test "
1123			       "%d for %s: error=%d\n", i + 1, algo, res);
1124			return res;
1125		}
1126		produced += res;
1127
1128		if (COMP_BUF_SIZE - req.avail_out != ctemplate[i].outlen) {
1129			pr_err("alg: comp: Compression test %d failed for %s: "
1130			       "output len = %d (expected %d)\n", i + 1, algo,
1131			       COMP_BUF_SIZE - req.avail_out,
1132			       ctemplate[i].outlen);
1133			return -EINVAL;
1134		}
1135
1136		if (produced != ctemplate[i].outlen) {
1137			pr_err("alg: comp: Compression test %d failed for %s: "
1138			       "returned len = %u (expected %d)\n", i + 1,
1139			       algo, produced, ctemplate[i].outlen);
1140			return -EINVAL;
1141		}
1142
1143		if (memcmp(result, ctemplate[i].output, ctemplate[i].outlen)) {
1144			pr_err("alg: pcomp: Compression test %d failed for "
1145			       "%s\n", i + 1, algo);
1146			hexdump(result, ctemplate[i].outlen);
1147			return -EINVAL;
1148		}
1149	}
1150
1151	for (i = 0; i < dtcount; i++) {
1152		struct comp_request req;
1153		unsigned int produced = 0;
1154
1155		res = crypto_decompress_setup(tfm, dtemplate[i].params,
1156					      dtemplate[i].paramsize);
1157		if (res) {
1158			pr_err("alg: pcomp: decompression setup failed on "
1159			       "test %d for %s: error=%d\n", i + 1, algo, res);
1160			return res;
1161		}
1162
1163		res = crypto_decompress_init(tfm);
1164		if (res) {
1165			pr_err("alg: pcomp: decompression init failed on test "
1166			       "%d for %s: error=%d\n", i + 1, algo, res);
1167			return res;
1168		}
1169
1170		memset(result, 0, sizeof(result));
1171
1172		req.next_in = dtemplate[i].input;
1173		req.avail_in = dtemplate[i].inlen / 2;
1174		req.next_out = result;
1175		req.avail_out = dtemplate[i].outlen / 2;
1176
1177		res = crypto_decompress_update(tfm, &req);
1178		if (res < 0 && (res != -EAGAIN || req.avail_in)) {
1179			pr_err("alg: pcomp: decompression update failed on "
1180			       "test %d for %s: error=%d\n", i + 1, algo, res);
1181			return res;
1182		}
1183		if (res > 0)
1184			produced += res;
1185
1186		/* Add remaining input data */
1187		req.avail_in += (dtemplate[i].inlen + 1) / 2;
1188
1189		res = crypto_decompress_update(tfm, &req);
1190		if (res < 0 && (res != -EAGAIN || req.avail_in)) {
1191			pr_err("alg: pcomp: decompression update failed on "
1192			       "test %d for %s: error=%d\n", i + 1, algo, res);
1193			return res;
1194		}
1195		if (res > 0)
1196			produced += res;
1197
1198		/* Provide remaining output space */
1199		req.avail_out += COMP_BUF_SIZE - dtemplate[i].outlen / 2;
1200
1201		res = crypto_decompress_final(tfm, &req);
1202		if (res < 0 && (res != -EAGAIN || req.avail_in)) {
1203			pr_err("alg: pcomp: decompression final failed on "
1204			       "test %d for %s: error=%d\n", i + 1, algo, res);
1205			return res;
1206		}
1207		if (res > 0)
1208			produced += res;
1209
1210		if (COMP_BUF_SIZE - req.avail_out != dtemplate[i].outlen) {
1211			pr_err("alg: comp: Decompression test %d failed for "
1212			       "%s: output len = %d (expected %d)\n", i + 1,
1213			       algo, COMP_BUF_SIZE - req.avail_out,
1214			       dtemplate[i].outlen);
1215			return -EINVAL;
1216		}
1217
1218		if (produced != dtemplate[i].outlen) {
1219			pr_err("alg: comp: Decompression test %d failed for "
1220			       "%s: returned len = %u (expected %d)\n", i + 1,
1221			       algo, produced, dtemplate[i].outlen);
1222			return -EINVAL;
1223		}
1224
1225		if (memcmp(result, dtemplate[i].output, dtemplate[i].outlen)) {
1226			pr_err("alg: pcomp: Decompression test %d failed for "
1227			       "%s\n", i + 1, algo);
1228			hexdump(result, dtemplate[i].outlen);
1229			return -EINVAL;
1230		}
1231	}
1232
1233	return 0;
1234}
1235
1236
1237static int test_cprng(struct crypto_rng *tfm, struct cprng_testvec *template,
1238		      unsigned int tcount)
1239{
1240	const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
1241	int err = 0, i, j, seedsize;
1242	u8 *seed;
1243	char result[32];
1244
1245	seedsize = crypto_rng_seedsize(tfm);
1246
1247	seed = kmalloc(seedsize, GFP_KERNEL);
1248	if (!seed) {
1249		printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
1250		       "for %s\n", algo);
1251		return -ENOMEM;
1252	}
1253
1254	for (i = 0; i < tcount; i++) {
1255		memset(result, 0, 32);
1256
1257		memcpy(seed, template[i].v, template[i].vlen);
1258		memcpy(seed + template[i].vlen, template[i].key,
1259		       template[i].klen);
1260		memcpy(seed + template[i].vlen + template[i].klen,
1261		       template[i].dt, template[i].dtlen);
1262
1263		err = crypto_rng_reset(tfm, seed, seedsize);
1264		if (err) {
1265			printk(KERN_ERR "alg: cprng: Failed to reset rng "
1266			       "for %s\n", algo);
1267			goto out;
1268		}
1269
1270		for (j = 0; j < template[i].loops; j++) {
1271			err = crypto_rng_get_bytes(tfm, result,
1272						   template[i].rlen);
1273			if (err != template[i].rlen) {
1274				printk(KERN_ERR "alg: cprng: Failed to obtain "
1275				       "the correct amount of random data for "
1276				       "%s (requested %d, got %d)\n", algo,
1277				       template[i].rlen, err);
1278				goto out;
1279			}
1280		}
1281
1282		err = memcmp(result, template[i].result,
1283			     template[i].rlen);
1284		if (err) {
1285			printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
1286			       i, algo);
1287			hexdump(result, template[i].rlen);
1288			err = -EINVAL;
1289			goto out;
1290		}
1291	}
1292
1293out:
1294	kfree(seed);
1295	return err;
1296}
1297
1298static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
1299			 u32 type, u32 mask)
1300{
1301	struct crypto_aead *tfm;
1302	int err = 0;
1303
1304	tfm = crypto_alloc_aead(driver, type, mask);
1305	if (IS_ERR(tfm)) {
1306		printk(KERN_ERR "alg: aead: Failed to load transform for %s: "
1307		       "%ld\n", driver, PTR_ERR(tfm));
1308		return PTR_ERR(tfm);
1309	}
1310
1311	if (desc->suite.aead.enc.vecs) {
1312		err = test_aead(tfm, ENCRYPT, desc->suite.aead.enc.vecs,
1313				desc->suite.aead.enc.count);
1314		if (err)
1315			goto out;
1316	}
1317
1318	if (!err && desc->suite.aead.dec.vecs)
1319		err = test_aead(tfm, DECRYPT, desc->suite.aead.dec.vecs,
1320				desc->suite.aead.dec.count);
1321
1322out:
1323	crypto_free_aead(tfm);
1324	return err;
1325}
1326
1327static int alg_test_cipher(const struct alg_test_desc *desc,
1328			   const char *driver, u32 type, u32 mask)
1329{
1330	struct crypto_cipher *tfm;
1331	int err = 0;
1332
1333	tfm = crypto_alloc_cipher(driver, type, mask);
1334	if (IS_ERR(tfm)) {
1335		printk(KERN_ERR "alg: cipher: Failed to load transform for "
1336		       "%s: %ld\n", driver, PTR_ERR(tfm));
1337		return PTR_ERR(tfm);
1338	}
1339
1340	if (desc->suite.cipher.enc.vecs) {
1341		err = test_cipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
1342				  desc->suite.cipher.enc.count);
1343		if (err)
1344			goto out;
1345	}
1346
1347	if (desc->suite.cipher.dec.vecs)
1348		err = test_cipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
1349				  desc->suite.cipher.dec.count);
1350
1351out:
1352	crypto_free_cipher(tfm);
1353	return err;
1354}
1355
1356static int alg_test_skcipher(const struct alg_test_desc *desc,
1357			     const char *driver, u32 type, u32 mask)
1358{
1359	struct crypto_ablkcipher *tfm;
1360	int err = 0;
1361
1362	tfm = crypto_alloc_ablkcipher(driver, type, mask);
1363	if (IS_ERR(tfm)) {
1364		printk(KERN_ERR "alg: skcipher: Failed to load transform for "
1365		       "%s: %ld\n", driver, PTR_ERR(tfm));
1366		return PTR_ERR(tfm);
1367	}
1368
1369	if (desc->suite.cipher.enc.vecs) {
1370		err = test_skcipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
1371				    desc->suite.cipher.enc.count);
1372		if (err)
1373			goto out;
1374	}
1375
1376	if (desc->suite.cipher.dec.vecs)
1377		err = test_skcipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
1378				    desc->suite.cipher.dec.count);
1379
1380out:
1381	crypto_free_ablkcipher(tfm);
1382	return err;
1383}
1384
1385static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
1386			 u32 type, u32 mask)
1387{
1388	struct crypto_comp *tfm;
1389	int err;
1390
1391	tfm = crypto_alloc_comp(driver, type, mask);
1392	if (IS_ERR(tfm)) {
1393		printk(KERN_ERR "alg: comp: Failed to load transform for %s: "
1394		       "%ld\n", driver, PTR_ERR(tfm));
1395		return PTR_ERR(tfm);
1396	}
1397
1398	err = test_comp(tfm, desc->suite.comp.comp.vecs,
1399			desc->suite.comp.decomp.vecs,
1400			desc->suite.comp.comp.count,
1401			desc->suite.comp.decomp.count);
1402
1403	crypto_free_comp(tfm);
1404	return err;
1405}
1406
1407static int alg_test_pcomp(const struct alg_test_desc *desc, const char *driver,
1408			  u32 type, u32 mask)
1409{
1410	struct crypto_pcomp *tfm;
1411	int err;
1412
1413	tfm = crypto_alloc_pcomp(driver, type, mask);
1414	if (IS_ERR(tfm)) {
1415		pr_err("alg: pcomp: Failed to load transform for %s: %ld\n",
1416		       driver, PTR_ERR(tfm));
1417		return PTR_ERR(tfm);
1418	}
1419
1420	err = test_pcomp(tfm, desc->suite.pcomp.comp.vecs,
1421			 desc->suite.pcomp.decomp.vecs,
1422			 desc->suite.pcomp.comp.count,
1423			 desc->suite.pcomp.decomp.count);
1424
1425	crypto_free_pcomp(tfm);
1426	return err;
1427}
1428
1429static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1430			 u32 type, u32 mask)
1431{
1432	struct crypto_ahash *tfm;
1433	int err;
1434
1435	tfm = crypto_alloc_ahash(driver, type, mask);
1436	if (IS_ERR(tfm)) {
1437		printk(KERN_ERR "alg: hash: Failed to load transform for %s: "
1438		       "%ld\n", driver, PTR_ERR(tfm));
1439		return PTR_ERR(tfm);
1440	}
1441
1442	err = test_hash(tfm, desc->suite.hash.vecs,
1443			desc->suite.hash.count, true);
1444	if (!err)
1445		err = test_hash(tfm, desc->suite.hash.vecs,
1446				desc->suite.hash.count, false);
1447
1448	crypto_free_ahash(tfm);
1449	return err;
1450}
1451
1452static int alg_test_crc32c(const struct alg_test_desc *desc,
1453			   const char *driver, u32 type, u32 mask)
1454{
1455	struct crypto_shash *tfm;
1456	u32 val;
1457	int err;
1458
1459	err = alg_test_hash(desc, driver, type, mask);
1460	if (err)
1461		goto out;
1462
1463	tfm = crypto_alloc_shash(driver, type, mask);
1464	if (IS_ERR(tfm)) {
1465		printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
1466		       "%ld\n", driver, PTR_ERR(tfm));
1467		err = PTR_ERR(tfm);
1468		goto out;
1469	}
1470
1471	do {
1472		struct {
1473			struct shash_desc shash;
1474			char ctx[crypto_shash_descsize(tfm)];
1475		} sdesc;
1476
1477		sdesc.shash.tfm = tfm;
1478		sdesc.shash.flags = 0;
1479
1480		*(u32 *)sdesc.ctx = le32_to_cpu(420553207);
1481		err = crypto_shash_final(&sdesc.shash, (u8 *)&val);
1482		if (err) {
1483			printk(KERN_ERR "alg: crc32c: Operation failed for "
1484			       "%s: %d\n", driver, err);
1485			break;
1486		}
1487
1488		if (val != ~420553207) {
1489			printk(KERN_ERR "alg: crc32c: Test failed for %s: "
1490			       "%d\n", driver, val);
1491			err = -EINVAL;
1492		}
1493	} while (0);
1494
1495	crypto_free_shash(tfm);
1496
1497out:
1498	return err;
1499}
1500
1501static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
1502			  u32 type, u32 mask)
1503{
1504	struct crypto_rng *rng;
1505	int err;
1506
1507	rng = crypto_alloc_rng(driver, type, mask);
1508	if (IS_ERR(rng)) {
1509		printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
1510		       "%ld\n", driver, PTR_ERR(rng));
1511		return PTR_ERR(rng);
1512	}
1513
1514	err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
1515
1516	crypto_free_rng(rng);
1517
1518	return err;
1519}
1520
1521static int alg_test_null(const struct alg_test_desc *desc,
1522			     const char *driver, u32 type, u32 mask)
1523{
1524	return 0;
1525}
1526
1527/* Please keep this list sorted by algorithm name. */
1528static const struct alg_test_desc alg_test_descs[] = {
1529	{
1530		.alg = "__driver-cbc-aes-aesni",
1531		.test = alg_test_null,
1532		.suite = {
1533			.cipher = {
1534				.enc = {
1535					.vecs = NULL,
1536					.count = 0
1537				},
1538				.dec = {
1539					.vecs = NULL,
1540					.count = 0
1541				}
1542			}
1543		}
1544	}, {
1545		.alg = "__driver-ecb-aes-aesni",
1546		.test = alg_test_null,
1547		.suite = {
1548			.cipher = {
1549				.enc = {
1550					.vecs = NULL,
1551					.count = 0
1552				},
1553				.dec = {
1554					.vecs = NULL,
1555					.count = 0
1556				}
1557			}
1558		}
1559	}, {
1560		.alg = "__ghash-pclmulqdqni",
1561		.test = alg_test_null,
1562		.suite = {
1563			.hash = {
1564				.vecs = NULL,
1565				.count = 0
1566			}
1567		}
1568	}, {
1569		.alg = "ansi_cprng",
1570		.test = alg_test_cprng,
1571		.fips_allowed = 1,
1572		.suite = {
1573			.cprng = {
1574				.vecs = ansi_cprng_aes_tv_template,
1575				.count = ANSI_CPRNG_AES_TEST_VECTORS
1576			}
1577		}
1578	}, {
1579		.alg = "cbc(aes)",
1580		.test = alg_test_skcipher,
1581		.fips_allowed = 1,
1582		.suite = {
1583			.cipher = {
1584				.enc = {
1585					.vecs = aes_cbc_enc_tv_template,
1586					.count = AES_CBC_ENC_TEST_VECTORS
1587				},
1588				.dec = {
1589					.vecs = aes_cbc_dec_tv_template,
1590					.count = AES_CBC_DEC_TEST_VECTORS
1591				}
1592			}
1593		}
1594	}, {
1595		.alg = "cbc(anubis)",
1596		.test = alg_test_skcipher,
1597		.suite = {
1598			.cipher = {
1599				.enc = {
1600					.vecs = anubis_cbc_enc_tv_template,
1601					.count = ANUBIS_CBC_ENC_TEST_VECTORS
1602				},
1603				.dec = {
1604					.vecs = anubis_cbc_dec_tv_template,
1605					.count = ANUBIS_CBC_DEC_TEST_VECTORS
1606				}
1607			}
1608		}
1609	}, {
1610		.alg = "cbc(blowfish)",
1611		.test = alg_test_skcipher,
1612		.suite = {
1613			.cipher = {
1614				.enc = {
1615					.vecs = bf_cbc_enc_tv_template,
1616					.count = BF_CBC_ENC_TEST_VECTORS
1617				},
1618				.dec = {
1619					.vecs = bf_cbc_dec_tv_template,
1620					.count = BF_CBC_DEC_TEST_VECTORS
1621				}
1622			}
1623		}
1624	}, {
1625		.alg = "cbc(camellia)",
1626		.test = alg_test_skcipher,
1627		.suite = {
1628			.cipher = {
1629				.enc = {
1630					.vecs = camellia_cbc_enc_tv_template,
1631					.count = CAMELLIA_CBC_ENC_TEST_VECTORS
1632				},
1633				.dec = {
1634					.vecs = camellia_cbc_dec_tv_template,
1635					.count = CAMELLIA_CBC_DEC_TEST_VECTORS
1636				}
1637			}
1638		}
1639	}, {
1640		.alg = "cbc(des)",
1641		.test = alg_test_skcipher,
1642		.suite = {
1643			.cipher = {
1644				.enc = {
1645					.vecs = des_cbc_enc_tv_template,
1646					.count = DES_CBC_ENC_TEST_VECTORS
1647				},
1648				.dec = {
1649					.vecs = des_cbc_dec_tv_template,
1650					.count = DES_CBC_DEC_TEST_VECTORS
1651				}
1652			}
1653		}
1654	}, {
1655		.alg = "cbc(des3_ede)",
1656		.test = alg_test_skcipher,
1657		.fips_allowed = 1,
1658		.suite = {
1659			.cipher = {
1660				.enc = {
1661					.vecs = des3_ede_cbc_enc_tv_template,
1662					.count = DES3_EDE_CBC_ENC_TEST_VECTORS
1663				},
1664				.dec = {
1665					.vecs = des3_ede_cbc_dec_tv_template,
1666					.count = DES3_EDE_CBC_DEC_TEST_VECTORS
1667				}
1668			}
1669		}
1670	}, {
1671		.alg = "cbc(twofish)",
1672		.test = alg_test_skcipher,
1673		.suite = {
1674			.cipher = {
1675				.enc = {
1676					.vecs = tf_cbc_enc_tv_template,
1677					.count = TF_CBC_ENC_TEST_VECTORS
1678				},
1679				.dec = {
1680					.vecs = tf_cbc_dec_tv_template,
1681					.count = TF_CBC_DEC_TEST_VECTORS
1682				}
1683			}
1684		}
1685	}, {
1686		.alg = "ccm(aes)",
1687		.test = alg_test_aead,
1688		.fips_allowed = 1,
1689		.suite = {
1690			.aead = {
1691				.enc = {
1692					.vecs = aes_ccm_enc_tv_template,
1693					.count = AES_CCM_ENC_TEST_VECTORS
1694				},
1695				.dec = {
1696					.vecs = aes_ccm_dec_tv_template,
1697					.count = AES_CCM_DEC_TEST_VECTORS
1698				}
1699			}
1700		}
1701	}, {
1702		.alg = "crc32c",
1703		.test = alg_test_crc32c,
1704		.fips_allowed = 1,
1705		.suite = {
1706			.hash = {
1707				.vecs = crc32c_tv_template,
1708				.count = CRC32C_TEST_VECTORS
1709			}
1710		}
1711	}, {
1712		.alg = "cryptd(__driver-ecb-aes-aesni)",
1713		.test = alg_test_null,
1714		.suite = {
1715			.cipher = {
1716				.enc = {
1717					.vecs = NULL,
1718					.count = 0
1719				},
1720				.dec = {
1721					.vecs = NULL,
1722					.count = 0
1723				}
1724			}
1725		}
1726	}, {
1727		.alg = "cryptd(__ghash-pclmulqdqni)",
1728		.test = alg_test_null,
1729		.suite = {
1730			.hash = {
1731				.vecs = NULL,
1732				.count = 0
1733			}
1734		}
1735	}, {
1736		.alg = "ctr(aes)",
1737		.test = alg_test_skcipher,
1738		.fips_allowed = 1,
1739		.suite = {
1740			.cipher = {
1741				.enc = {
1742					.vecs = aes_ctr_enc_tv_template,
1743					.count = AES_CTR_ENC_TEST_VECTORS
1744				},
1745				.dec = {
1746					.vecs = aes_ctr_dec_tv_template,
1747					.count = AES_CTR_DEC_TEST_VECTORS
1748				}
1749			}
1750		}
1751	}, {
1752		.alg = "cts(cbc(aes))",
1753		.test = alg_test_skcipher,
1754		.suite = {
1755			.cipher = {
1756				.enc = {
1757					.vecs = cts_mode_enc_tv_template,
1758					.count = CTS_MODE_ENC_TEST_VECTORS
1759				},
1760				.dec = {
1761					.vecs = cts_mode_dec_tv_template,
1762					.count = CTS_MODE_DEC_TEST_VECTORS
1763				}
1764			}
1765		}
1766	}, {
1767		.alg = "deflate",
1768		.test = alg_test_comp,
1769		.suite = {
1770			.comp = {
1771				.comp = {
1772					.vecs = deflate_comp_tv_template,
1773					.count = DEFLATE_COMP_TEST_VECTORS
1774				},
1775				.decomp = {
1776					.vecs = deflate_decomp_tv_template,
1777					.count = DEFLATE_DECOMP_TEST_VECTORS
1778				}
1779			}
1780		}
1781	}, {
1782		.alg = "ecb(__aes-aesni)",
1783		.test = alg_test_null,
1784		.suite = {
1785			.cipher = {
1786				.enc = {
1787					.vecs = NULL,
1788					.count = 0
1789				},
1790				.dec = {
1791					.vecs = NULL,
1792					.count = 0
1793				}
1794			}
1795		}
1796	}, {
1797		.alg = "ecb(aes)",
1798		.test = alg_test_skcipher,
1799		.fips_allowed = 1,
1800		.suite = {
1801			.cipher = {
1802				.enc = {
1803					.vecs = aes_enc_tv_template,
1804					.count = AES_ENC_TEST_VECTORS
1805				},
1806				.dec = {
1807					.vecs = aes_dec_tv_template,
1808					.count = AES_DEC_TEST_VECTORS
1809				}
1810			}
1811		}
1812	}, {
1813		.alg = "ecb(anubis)",
1814		.test = alg_test_skcipher,
1815		.suite = {
1816			.cipher = {
1817				.enc = {
1818					.vecs = anubis_enc_tv_template,
1819					.count = ANUBIS_ENC_TEST_VECTORS
1820				},
1821				.dec = {
1822					.vecs = anubis_dec_tv_template,
1823					.count = ANUBIS_DEC_TEST_VECTORS
1824				}
1825			}
1826		}
1827	}, {
1828		.alg = "ecb(arc4)",
1829		.test = alg_test_skcipher,
1830		.suite = {
1831			.cipher = {
1832				.enc = {
1833					.vecs = arc4_enc_tv_template,
1834					.count = ARC4_ENC_TEST_VECTORS
1835				},
1836				.dec = {
1837					.vecs = arc4_dec_tv_template,
1838					.count = ARC4_DEC_TEST_VECTORS
1839				}
1840			}
1841		}
1842	}, {
1843		.alg = "ecb(blowfish)",
1844		.test = alg_test_skcipher,
1845		.suite = {
1846			.cipher = {
1847				.enc = {
1848					.vecs = bf_enc_tv_template,
1849					.count = BF_ENC_TEST_VECTORS
1850				},
1851				.dec = {
1852					.vecs = bf_dec_tv_template,
1853					.count = BF_DEC_TEST_VECTORS
1854				}
1855			}
1856		}
1857	}, {
1858		.alg = "ecb(camellia)",
1859		.test = alg_test_skcipher,
1860		.suite = {
1861			.cipher = {
1862				.enc = {
1863					.vecs = camellia_enc_tv_template,
1864					.count = CAMELLIA_ENC_TEST_VECTORS
1865				},
1866				.dec = {
1867					.vecs = camellia_dec_tv_template,
1868					.count = CAMELLIA_DEC_TEST_VECTORS
1869				}
1870			}
1871		}
1872	}, {
1873		.alg = "ecb(cast5)",
1874		.test = alg_test_skcipher,
1875		.suite = {
1876			.cipher = {
1877				.enc = {
1878					.vecs = cast5_enc_tv_template,
1879					.count = CAST5_ENC_TEST_VECTORS
1880				},
1881				.dec = {
1882					.vecs = cast5_dec_tv_template,
1883					.count = CAST5_DEC_TEST_VECTORS
1884				}
1885			}
1886		}
1887	}, {
1888		.alg = "ecb(cast6)",
1889		.test = alg_test_skcipher,
1890		.suite = {
1891			.cipher = {
1892				.enc = {
1893					.vecs = cast6_enc_tv_template,
1894					.count = CAST6_ENC_TEST_VECTORS
1895				},
1896				.dec = {
1897					.vecs = cast6_dec_tv_template,
1898					.count = CAST6_DEC_TEST_VECTORS
1899				}
1900			}
1901		}
1902	}, {
1903		.alg = "ecb(des)",
1904		.test = alg_test_skcipher,
1905		.fips_allowed = 1,
1906		.suite = {
1907			.cipher = {
1908				.enc = {
1909					.vecs = des_enc_tv_template,
1910					.count = DES_ENC_TEST_VECTORS
1911				},
1912				.dec = {
1913					.vecs = des_dec_tv_template,
1914					.count = DES_DEC_TEST_VECTORS
1915				}
1916			}
1917		}
1918	}, {
1919		.alg = "ecb(des3_ede)",
1920		.test = alg_test_skcipher,
1921		.fips_allowed = 1,
1922		.suite = {
1923			.cipher = {
1924				.enc = {
1925					.vecs = des3_ede_enc_tv_template,
1926					.count = DES3_EDE_ENC_TEST_VECTORS
1927				},
1928				.dec = {
1929					.vecs = des3_ede_dec_tv_template,
1930					.count = DES3_EDE_DEC_TEST_VECTORS
1931				}
1932			}
1933		}
1934	}, {
1935		.alg = "ecb(khazad)",
1936		.test = alg_test_skcipher,
1937		.suite = {
1938			.cipher = {
1939				.enc = {
1940					.vecs = khazad_enc_tv_template,
1941					.count = KHAZAD_ENC_TEST_VECTORS
1942				},
1943				.dec = {
1944					.vecs = khazad_dec_tv_template,
1945					.count = KHAZAD_DEC_TEST_VECTORS
1946				}
1947			}
1948		}
1949	}, {
1950		.alg = "ecb(seed)",
1951		.test = alg_test_skcipher,
1952		.suite = {
1953			.cipher = {
1954				.enc = {
1955					.vecs = seed_enc_tv_template,
1956					.count = SEED_ENC_TEST_VECTORS
1957				},
1958				.dec = {
1959					.vecs = seed_dec_tv_template,
1960					.count = SEED_DEC_TEST_VECTORS
1961				}
1962			}
1963		}
1964	}, {
1965		.alg = "ecb(serpent)",
1966		.test = alg_test_skcipher,
1967		.suite = {
1968			.cipher = {
1969				.enc = {
1970					.vecs = serpent_enc_tv_template,
1971					.count = SERPENT_ENC_TEST_VECTORS
1972				},
1973				.dec = {
1974					.vecs = serpent_dec_tv_template,
1975					.count = SERPENT_DEC_TEST_VECTORS
1976				}
1977			}
1978		}
1979	}, {
1980		.alg = "ecb(tea)",
1981		.test = alg_test_skcipher,
1982		.suite = {
1983			.cipher = {
1984				.enc = {
1985					.vecs = tea_enc_tv_template,
1986					.count = TEA_ENC_TEST_VECTORS
1987				},
1988				.dec = {
1989					.vecs = tea_dec_tv_template,
1990					.count = TEA_DEC_TEST_VECTORS
1991				}
1992			}
1993		}
1994	}, {
1995		.alg = "ecb(tnepres)",
1996		.test = alg_test_skcipher,
1997		.suite = {
1998			.cipher = {
1999				.enc = {
2000					.vecs = tnepres_enc_tv_template,
2001					.count = TNEPRES_ENC_TEST_VECTORS
2002				},
2003				.dec = {
2004					.vecs = tnepres_dec_tv_template,
2005					.count = TNEPRES_DEC_TEST_VECTORS
2006				}
2007			}
2008		}
2009	}, {
2010		.alg = "ecb(twofish)",
2011		.test = alg_test_skcipher,
2012		.suite = {
2013			.cipher = {
2014				.enc = {
2015					.vecs = tf_enc_tv_template,
2016					.count = TF_ENC_TEST_VECTORS
2017				},
2018				.dec = {
2019					.vecs = tf_dec_tv_template,
2020					.count = TF_DEC_TEST_VECTORS
2021				}
2022			}
2023		}
2024	}, {
2025		.alg = "ecb(xeta)",
2026		.test = alg_test_skcipher,
2027		.suite = {
2028			.cipher = {
2029				.enc = {
2030					.vecs = xeta_enc_tv_template,
2031					.count = XETA_ENC_TEST_VECTORS
2032				},
2033				.dec = {
2034					.vecs = xeta_dec_tv_template,
2035					.count = XETA_DEC_TEST_VECTORS
2036				}
2037			}
2038		}
2039	}, {
2040		.alg = "ecb(xtea)",
2041		.test = alg_test_skcipher,
2042		.suite = {
2043			.cipher = {
2044				.enc = {
2045					.vecs = xtea_enc_tv_template,
2046					.count = XTEA_ENC_TEST_VECTORS
2047				},
2048				.dec = {
2049					.vecs = xtea_dec_tv_template,
2050					.count = XTEA_DEC_TEST_VECTORS
2051				}
2052			}
2053		}
2054	}, {
2055		.alg = "gcm(aes)",
2056		.test = alg_test_aead,
2057		.fips_allowed = 1,
2058		.suite = {
2059			.aead = {
2060				.enc = {
2061					.vecs = aes_gcm_enc_tv_template,
2062					.count = AES_GCM_ENC_TEST_VECTORS
2063				},
2064				.dec = {
2065					.vecs = aes_gcm_dec_tv_template,
2066					.count = AES_GCM_DEC_TEST_VECTORS
2067				}
2068			}
2069		}
2070	}, {
2071		.alg = "ghash",
2072		.test = alg_test_hash,
2073		.suite = {
2074			.hash = {
2075				.vecs = ghash_tv_template,
2076				.count = GHASH_TEST_VECTORS
2077			}
2078		}
2079	}, {
2080		.alg = "hmac(md5)",
2081		.test = alg_test_hash,
2082		.suite = {
2083			.hash = {
2084				.vecs = hmac_md5_tv_template,
2085				.count = HMAC_MD5_TEST_VECTORS
2086			}
2087		}
2088	}, {
2089		.alg = "hmac(rmd128)",
2090		.test = alg_test_hash,
2091		.suite = {
2092			.hash = {
2093				.vecs = hmac_rmd128_tv_template,
2094				.count = HMAC_RMD128_TEST_VECTORS
2095			}
2096		}
2097	}, {
2098		.alg = "hmac(rmd160)",
2099		.test = alg_test_hash,
2100		.suite = {
2101			.hash = {
2102				.vecs = hmac_rmd160_tv_template,
2103				.count = HMAC_RMD160_TEST_VECTORS
2104			}
2105		}
2106	}, {
2107		.alg = "hmac(sha1)",
2108		.test = alg_test_hash,
2109		.fips_allowed = 1,
2110		.suite = {
2111			.hash = {
2112				.vecs = hmac_sha1_tv_template,
2113				.count = HMAC_SHA1_TEST_VECTORS
2114			}
2115		}
2116	}, {
2117		.alg = "hmac(sha224)",
2118		.test = alg_test_hash,
2119		.fips_allowed = 1,
2120		.suite = {
2121			.hash = {
2122				.vecs = hmac_sha224_tv_template,
2123				.count = HMAC_SHA224_TEST_VECTORS
2124			}
2125		}
2126	}, {
2127		.alg = "hmac(sha256)",
2128		.test = alg_test_hash,
2129		.fips_allowed = 1,
2130		.suite = {
2131			.hash = {
2132				.vecs = hmac_sha256_tv_template,
2133				.count = HMAC_SHA256_TEST_VECTORS
2134			}
2135		}
2136	}, {
2137		.alg = "hmac(sha384)",
2138		.test = alg_test_hash,
2139		.fips_allowed = 1,
2140		.suite = {
2141			.hash = {
2142				.vecs = hmac_sha384_tv_template,
2143				.count = HMAC_SHA384_TEST_VECTORS
2144			}
2145		}
2146	}, {
2147		.alg = "hmac(sha512)",
2148		.test = alg_test_hash,
2149		.fips_allowed = 1,
2150		.suite = {
2151			.hash = {
2152				.vecs = hmac_sha512_tv_template,
2153				.count = HMAC_SHA512_TEST_VECTORS
2154			}
2155		}
2156	}, {
2157		.alg = "lrw(aes)",
2158		.test = alg_test_skcipher,
2159		.suite = {
2160			.cipher = {
2161				.enc = {
2162					.vecs = aes_lrw_enc_tv_template,
2163					.count = AES_LRW_ENC_TEST_VECTORS
2164				},
2165				.dec = {
2166					.vecs = aes_lrw_dec_tv_template,
2167					.count = AES_LRW_DEC_TEST_VECTORS
2168				}
2169			}
2170		}
2171	}, {
2172		.alg = "lzo",
2173		.test = alg_test_comp,
2174		.suite = {
2175			.comp = {
2176				.comp = {
2177					.vecs = lzo_comp_tv_template,
2178					.count = LZO_COMP_TEST_VECTORS
2179				},
2180				.decomp = {
2181					.vecs = lzo_decomp_tv_template,
2182					.count = LZO_DECOMP_TEST_VECTORS
2183				}
2184			}
2185		}
2186	}, {
2187		.alg = "md4",
2188		.test = alg_test_hash,
2189		.suite = {
2190			.hash = {
2191				.vecs = md4_tv_template,
2192				.count = MD4_TEST_VECTORS
2193			}
2194		}
2195	}, {
2196		.alg = "md5",
2197		.test = alg_test_hash,
2198		.suite = {
2199			.hash = {
2200				.vecs = md5_tv_template,
2201				.count = MD5_TEST_VECTORS
2202			}
2203		}
2204	}, {
2205		.alg = "michael_mic",
2206		.test = alg_test_hash,
2207		.suite = {
2208			.hash = {
2209				.vecs = michael_mic_tv_template,
2210				.count = MICHAEL_MIC_TEST_VECTORS
2211			}
2212		}
2213	}, {
2214		.alg = "pcbc(fcrypt)",
2215		.test = alg_test_skcipher,
2216		.suite = {
2217			.cipher = {
2218				.enc = {
2219					.vecs = fcrypt_pcbc_enc_tv_template,
2220					.count = FCRYPT_ENC_TEST_VECTORS
2221				},
2222				.dec = {
2223					.vecs = fcrypt_pcbc_dec_tv_template,
2224					.count = FCRYPT_DEC_TEST_VECTORS
2225				}
2226			}
2227		}
2228	}, {
2229		.alg = "rfc3686(ctr(aes))",
2230		.test = alg_test_skcipher,
2231		.fips_allowed = 1,
2232		.suite = {
2233			.cipher = {
2234				.enc = {
2235					.vecs = aes_ctr_rfc3686_enc_tv_template,
2236					.count = AES_CTR_3686_ENC_TEST_VECTORS
2237				},
2238				.dec = {
2239					.vecs = aes_ctr_rfc3686_dec_tv_template,
2240					.count = AES_CTR_3686_DEC_TEST_VECTORS
2241				}
2242			}
2243		}
2244	}, {
2245		.alg = "rfc4309(ccm(aes))",
2246		.test = alg_test_aead,
2247		.fips_allowed = 1,
2248		.suite = {
2249			.aead = {
2250				.enc = {
2251					.vecs = aes_ccm_rfc4309_enc_tv_template,
2252					.count = AES_CCM_4309_ENC_TEST_VECTORS
2253				},
2254				.dec = {
2255					.vecs = aes_ccm_rfc4309_dec_tv_template,
2256					.count = AES_CCM_4309_DEC_TEST_VECTORS
2257				}
2258			}
2259		}
2260	}, {
2261		.alg = "rmd128",
2262		.test = alg_test_hash,
2263		.suite = {
2264			.hash = {
2265				.vecs = rmd128_tv_template,
2266				.count = RMD128_TEST_VECTORS
2267			}
2268		}
2269	}, {
2270		.alg = "rmd160",
2271		.test = alg_test_hash,
2272		.suite = {
2273			.hash = {
2274				.vecs = rmd160_tv_template,
2275				.count = RMD160_TEST_VECTORS
2276			}
2277		}
2278	}, {
2279		.alg = "rmd256",
2280		.test = alg_test_hash,
2281		.suite = {
2282			.hash = {
2283				.vecs = rmd256_tv_template,
2284				.count = RMD256_TEST_VECTORS
2285			}
2286		}
2287	}, {
2288		.alg = "rmd320",
2289		.test = alg_test_hash,
2290		.suite = {
2291			.hash = {
2292				.vecs = rmd320_tv_template,
2293				.count = RMD320_TEST_VECTORS
2294			}
2295		}
2296	}, {
2297		.alg = "salsa20",
2298		.test = alg_test_skcipher,
2299		.suite = {
2300			.cipher = {
2301				.enc = {
2302					.vecs = salsa20_stream_enc_tv_template,
2303					.count = SALSA20_STREAM_ENC_TEST_VECTORS
2304				}
2305			}
2306		}
2307	}, {
2308		.alg = "sha1",
2309		.test = alg_test_hash,
2310		.fips_allowed = 1,
2311		.suite = {
2312			.hash = {
2313				.vecs = sha1_tv_template,
2314				.count = SHA1_TEST_VECTORS
2315			}
2316		}
2317	}, {
2318		.alg = "sha224",
2319		.test = alg_test_hash,
2320		.fips_allowed = 1,
2321		.suite = {
2322			.hash = {
2323				.vecs = sha224_tv_template,
2324				.count = SHA224_TEST_VECTORS
2325			}
2326		}
2327	}, {
2328		.alg = "sha256",
2329		.test = alg_test_hash,
2330		.fips_allowed = 1,
2331		.suite = {
2332			.hash = {
2333				.vecs = sha256_tv_template,
2334				.count = SHA256_TEST_VECTORS
2335			}
2336		}
2337	}, {
2338		.alg = "sha384",
2339		.test = alg_test_hash,
2340		.fips_allowed = 1,
2341		.suite = {
2342			.hash = {
2343				.vecs = sha384_tv_template,
2344				.count = SHA384_TEST_VECTORS
2345			}
2346		}
2347	}, {
2348		.alg = "sha512",
2349		.test = alg_test_hash,
2350		.fips_allowed = 1,
2351		.suite = {
2352			.hash = {
2353				.vecs = sha512_tv_template,
2354				.count = SHA512_TEST_VECTORS
2355			}
2356		}
2357	}, {
2358		.alg = "tgr128",
2359		.test = alg_test_hash,
2360		.suite = {
2361			.hash = {
2362				.vecs = tgr128_tv_template,
2363				.count = TGR128_TEST_VECTORS
2364			}
2365		}
2366	}, {
2367		.alg = "tgr160",
2368		.test = alg_test_hash,
2369		.suite = {
2370			.hash = {
2371				.vecs = tgr160_tv_template,
2372				.count = TGR160_TEST_VECTORS
2373			}
2374		}
2375	}, {
2376		.alg = "tgr192",
2377		.test = alg_test_hash,
2378		.suite = {
2379			.hash = {
2380				.vecs = tgr192_tv_template,
2381				.count = TGR192_TEST_VECTORS
2382			}
2383		}
2384	}, {
2385		.alg = "vmac(aes)",
2386		.test = alg_test_hash,
2387		.suite = {
2388			.hash = {
2389				.vecs = aes_vmac128_tv_template,
2390				.count = VMAC_AES_TEST_VECTORS
2391			}
2392		}
2393	}, {
2394		.alg = "wp256",
2395		.test = alg_test_hash,
2396		.suite = {
2397			.hash = {
2398				.vecs = wp256_tv_template,
2399				.count = WP256_TEST_VECTORS
2400			}
2401		}
2402	}, {
2403		.alg = "wp384",
2404		.test = alg_test_hash,
2405		.suite = {
2406			.hash = {
2407				.vecs = wp384_tv_template,
2408				.count = WP384_TEST_VECTORS
2409			}
2410		}
2411	}, {
2412		.alg = "wp512",
2413		.test = alg_test_hash,
2414		.suite = {
2415			.hash = {
2416				.vecs = wp512_tv_template,
2417				.count = WP512_TEST_VECTORS
2418			}
2419		}
2420	}, {
2421		.alg = "xcbc(aes)",
2422		.test = alg_test_hash,
2423		.suite = {
2424			.hash = {
2425				.vecs = aes_xcbc128_tv_template,
2426				.count = XCBC_AES_TEST_VECTORS
2427			}
2428		}
2429	}, {
2430		.alg = "xts(aes)",
2431		.test = alg_test_skcipher,
2432		.suite = {
2433			.cipher = {
2434				.enc = {
2435					.vecs = aes_xts_enc_tv_template,
2436					.count = AES_XTS_ENC_TEST_VECTORS
2437				},
2438				.dec = {
2439					.vecs = aes_xts_dec_tv_template,
2440					.count = AES_XTS_DEC_TEST_VECTORS
2441				}
2442			}
2443		}
2444	}, {
2445		.alg = "zlib",
2446		.test = alg_test_pcomp,
2447		.suite = {
2448			.pcomp = {
2449				.comp = {
2450					.vecs = zlib_comp_tv_template,
2451					.count = ZLIB_COMP_TEST_VECTORS
2452				},
2453				.decomp = {
2454					.vecs = zlib_decomp_tv_template,
2455					.count = ZLIB_DECOMP_TEST_VECTORS
2456				}
2457			}
2458		}
2459	}
2460};
2461
2462static int alg_find_test(const char *alg)
2463{
2464	int start = 0;
2465	int end = ARRAY_SIZE(alg_test_descs);
2466
2467	while (start < end) {
2468		int i = (start + end) / 2;
2469		int diff = strcmp(alg_test_descs[i].alg, alg);
2470
2471		if (diff > 0) {
2472			end = i;
2473			continue;
2474		}
2475
2476		if (diff < 0) {
2477			start = i + 1;
2478			continue;
2479		}
2480
2481		return i;
2482	}
2483
2484	return -1;
2485}
2486
2487int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
2488{
2489	int i;
2490	int j;
2491	int rc;
2492
2493	if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
2494		char nalg[CRYPTO_MAX_ALG_NAME];
2495
2496		if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
2497		    sizeof(nalg))
2498			return -ENAMETOOLONG;
2499
2500		i = alg_find_test(nalg);
2501		if (i < 0)
2502			goto notest;
2503
2504		if (fips_enabled && !alg_test_descs[i].fips_allowed)
2505			goto non_fips_alg;
2506
2507		rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
2508		goto test_done;
2509	}
2510
2511	i = alg_find_test(alg);
2512	j = alg_find_test(driver);
2513	if (i < 0 && j < 0)
2514		goto notest;
2515
2516	if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
2517			     (j >= 0 && !alg_test_descs[j].fips_allowed)))
2518		goto non_fips_alg;
2519
2520	rc = 0;
2521	if (i >= 0)
2522		rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
2523					     type, mask);
2524	if (j >= 0)
2525		rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
2526					     type, mask);
2527
2528test_done:
2529	if (fips_enabled && rc)
2530		panic("%s: %s alg self test failed in fips mode!\n", driver, alg);
2531
2532	if (fips_enabled && !rc)
2533		printk(KERN_INFO "alg: self-tests for %s (%s) passed\n",
2534		       driver, alg);
2535
2536	return rc;
2537
2538notest:
2539	printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
2540	return 0;
2541non_fips_alg:
2542	return -EINVAL;
2543}
2544
2545#endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
2546
2547EXPORT_SYMBOL_GPL(alg_test);
2548