code style scripts/checkpatch.pl (linux-3.9-rc1) formatting
[linux-2.6.34.14-moxart.git] / crypto / testmgr.c
blob73e8a46f88ff144d09191e1c95a56f486466d459
1 /*
2 * Algorithm testing framework and tests.
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>
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.
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>
24 #include "internal.h"
26 #ifndef CONFIG_CRYPTO_MANAGER_TESTS
28 /* a perfect nop */
29 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
31 return 0;
34 #else
36 #include "testmgr.h"
39 * Need slab memory for testing (size in number of pages).
41 #define XBUFSIZE 8
44 * Indexes into the xbuf to simulate cross-page access.
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
56 * Used by test_cipher()
58 #define ENCRYPT 1
59 #define DECRYPT 0
61 struct tcrypt_result {
62 struct completion completion;
63 int err;
66 struct aead_test_suite {
67 struct {
68 struct aead_testvec *vecs;
69 unsigned int count;
70 } enc, dec;
73 struct cipher_test_suite {
74 struct {
75 struct cipher_testvec *vecs;
76 unsigned int count;
77 } enc, dec;
80 struct comp_test_suite {
81 struct {
82 struct comp_testvec *vecs;
83 unsigned int count;
84 } comp, decomp;
87 struct pcomp_test_suite {
88 struct {
89 struct pcomp_testvec *vecs;
90 unsigned int count;
91 } comp, decomp;
94 struct hash_test_suite {
95 struct hash_testvec *vecs;
96 unsigned int count;
99 struct cprng_test_suite {
100 struct cprng_testvec *vecs;
101 unsigned int count;
104 struct 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 */
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;
120 static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
122 static void hexdump(unsigned char *buf, unsigned int len)
124 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
125 16, 1,
126 buf, len, false);
129 static void tcrypt_complete(struct crypto_async_request *req, int err)
131 struct tcrypt_result *res = req->data;
133 if (err == -EINPROGRESS)
134 return;
136 res->err = err;
137 complete(&res->completion);
140 static int testmgr_alloc_buf(char *buf[XBUFSIZE])
142 int i;
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;
150 return 0;
152 err_free_buf:
153 while (i-- > 0)
154 free_page((unsigned long)buf[i]);
156 return -ENOMEM;
159 static void testmgr_free_buf(char *buf[XBUFSIZE])
161 int i;
163 for (i = 0; i < XBUFSIZE; i++)
164 free_page((unsigned long)buf[i]);
167 static int test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
168 unsigned int tcount)
170 const char *algo = crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
171 unsigned int i, j, k, temp;
172 struct scatterlist sg[8];
173 char result[64];
174 struct ahash_request *req;
175 struct tcrypt_result tresult;
176 void *hash_buff;
177 char *xbuf[XBUFSIZE];
178 int ret = -ENOMEM;
180 if (testmgr_alloc_buf(xbuf))
181 goto out_nobuf;
183 init_completion(&tresult.completion);
185 req = ahash_request_alloc(tfm, GFP_KERNEL);
186 if (!req) {
187 printk(KERN_ERR "alg: hash: Failed to allocate request for "
188 "%s\n", algo);
189 goto out_noreq;
191 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
192 tcrypt_complete, &tresult);
194 j = 0;
195 for (i = 0; i < tcount; i++) {
196 if (template[i].np)
197 continue;
199 j++;
200 memset(result, 0, 64);
202 hash_buff = xbuf[0];
204 memcpy(hash_buff, template[i].plaintext, template[i].psize);
205 sg_init_one(&sg[0], hash_buff, template[i].psize);
207 if (template[i].ksize) {
208 crypto_ahash_clear_flags(tfm, ~0);
209 ret = crypto_ahash_setkey(tfm, template[i].key,
210 template[i].ksize);
211 if (ret) {
212 printk(KERN_ERR "alg: hash: setkey failed on "
213 "test %d for %s: ret=%d\n", j, algo,
214 -ret);
215 goto out;
219 ahash_request_set_crypt(req, sg, result, template[i].psize);
220 ret = crypto_ahash_digest(req);
221 switch (ret) {
222 case 0:
223 break;
224 case -EINPROGRESS:
225 case -EBUSY:
226 ret = wait_for_completion_interruptible(
227 &tresult.completion);
228 if (!ret && !(ret = tresult.err)) {
229 INIT_COMPLETION(tresult.completion);
230 break;
232 /* fall through */
233 default:
234 printk(KERN_ERR "alg: hash: digest failed on test %d "
235 "for %s: ret=%d\n", j, algo, -ret);
236 goto out;
239 if (memcmp(result, template[i].digest,
240 crypto_ahash_digestsize(tfm))) {
241 printk(KERN_ERR "alg: hash: Test %d failed for %s\n",
242 j, algo);
243 hexdump(result, crypto_ahash_digestsize(tfm));
244 ret = -EINVAL;
245 goto out;
249 j = 0;
250 for (i = 0; i < tcount; i++) {
251 if (template[i].np) {
252 j++;
253 memset(result, 0, 64);
255 temp = 0;
256 sg_init_table(sg, template[i].np);
257 ret = -EINVAL;
258 for (k = 0; k < template[i].np; k++) {
259 if (WARN_ON(offset_in_page(IDX[k]) +
260 template[i].tap[k] > PAGE_SIZE))
261 goto out;
262 sg_set_buf(&sg[k],
263 memcpy(xbuf[IDX[k] >> PAGE_SHIFT] +
264 offset_in_page(IDX[k]),
265 template[i].plaintext + temp,
266 template[i].tap[k]),
267 template[i].tap[k]);
268 temp += template[i].tap[k];
271 if (template[i].ksize) {
272 crypto_ahash_clear_flags(tfm, ~0);
273 ret = crypto_ahash_setkey(tfm, template[i].key,
274 template[i].ksize);
276 if (ret) {
277 printk(KERN_ERR "alg: hash: setkey "
278 "failed on chunking test %d "
279 "for %s: ret=%d\n", j, algo,
280 -ret);
281 goto out;
285 ahash_request_set_crypt(req, sg, result,
286 template[i].psize);
287 ret = crypto_ahash_digest(req);
288 switch (ret) {
289 case 0:
290 break;
291 case -EINPROGRESS:
292 case -EBUSY:
293 ret = wait_for_completion_interruptible(
294 &tresult.completion);
295 if (!ret && !(ret = tresult.err)) {
296 INIT_COMPLETION(tresult.completion);
297 break;
299 /* fall through */
300 default:
301 printk(KERN_ERR "alg: hash: digest failed "
302 "on chunking test %d for %s: "
303 "ret=%d\n", j, algo, -ret);
304 goto out;
307 if (memcmp(result, template[i].digest,
308 crypto_ahash_digestsize(tfm))) {
309 printk(KERN_ERR "alg: hash: Chunking test %d "
310 "failed for %s\n", j, algo);
311 hexdump(result, crypto_ahash_digestsize(tfm));
312 ret = -EINVAL;
313 goto out;
318 ret = 0;
320 out:
321 ahash_request_free(req);
322 out_noreq:
323 testmgr_free_buf(xbuf);
324 out_nobuf:
325 return ret;
328 static int test_aead(struct crypto_aead *tfm, int enc,
329 struct aead_testvec *template, unsigned int tcount)
331 const char *algo = crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm));
332 unsigned int i, j, k, n, temp;
333 int ret = -ENOMEM;
334 char *q;
335 char *key;
336 struct aead_request *req;
337 struct scatterlist sg[8];
338 struct scatterlist asg[8];
339 const char *e;
340 struct tcrypt_result result;
341 unsigned int authsize;
342 void *input;
343 void *assoc;
344 char iv[MAX_IVLEN];
345 char *xbuf[XBUFSIZE];
346 char *axbuf[XBUFSIZE];
348 if (testmgr_alloc_buf(xbuf))
349 goto out_noxbuf;
350 if (testmgr_alloc_buf(axbuf))
351 goto out_noaxbuf;
353 if (enc == ENCRYPT)
354 e = "encryption";
355 else
356 e = "decryption";
358 init_completion(&result.completion);
360 req = aead_request_alloc(tfm, GFP_KERNEL);
361 if (!req) {
362 printk(KERN_ERR "alg: aead: Failed to allocate request for "
363 "%s\n", algo);
364 goto out;
367 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
368 tcrypt_complete, &result);
370 for (i = 0, j = 0; i < tcount; i++) {
371 if (!template[i].np) {
372 j++;
374 /* some tepmplates have no input data but they will
375 * touch input
377 input = xbuf[0];
378 assoc = axbuf[0];
380 ret = -EINVAL;
381 if (WARN_ON(template[i].ilen > PAGE_SIZE ||
382 template[i].alen > PAGE_SIZE))
383 goto out;
385 memcpy(input, template[i].input, template[i].ilen);
386 memcpy(assoc, template[i].assoc, template[i].alen);
387 if (template[i].iv)
388 memcpy(iv, template[i].iv, MAX_IVLEN);
389 else
390 memset(iv, 0, MAX_IVLEN);
392 crypto_aead_clear_flags(tfm, ~0);
393 if (template[i].wk)
394 crypto_aead_set_flags(
395 tfm, CRYPTO_TFM_REQ_WEAK_KEY);
397 key = template[i].key;
399 ret = crypto_aead_setkey(tfm, key,
400 template[i].klen);
401 if (!ret == template[i].fail) {
402 printk(KERN_ERR "alg: aead: setkey failed on "
403 "test %d for %s: flags=%x\n", j, algo,
404 crypto_aead_get_flags(tfm));
405 goto out;
406 } else if (ret)
407 continue;
409 authsize = abs(template[i].rlen - template[i].ilen);
410 ret = crypto_aead_setauthsize(tfm, authsize);
411 if (ret) {
412 printk(KERN_ERR "alg: aead: Failed to set "
413 "authsize to %u on test %d for %s\n",
414 authsize, j, algo);
415 goto out;
418 sg_init_one(&sg[0], input,
419 template[i].ilen + (enc ? authsize : 0));
421 sg_init_one(&asg[0], assoc, template[i].alen);
423 aead_request_set_crypt(req, sg, sg,
424 template[i].ilen, iv);
426 aead_request_set_assoc(req, asg, template[i].alen);
428 ret = enc ?
429 crypto_aead_encrypt(req) :
430 crypto_aead_decrypt(req);
432 switch (ret) {
433 case 0:
434 if (template[i].novrfy) {
435 /* verification was supposed to fail */
436 printk(KERN_ERR "alg: aead: %s failed "
437 "on test %d for %s: ret was 0, "
438 "expected -EBADMSG\n",
439 e, j, algo);
440 /* so really, we got a bad message */
441 ret = -EBADMSG;
442 goto out;
444 break;
445 case -EINPROGRESS:
446 case -EBUSY:
447 ret = wait_for_completion_interruptible(
448 &result.completion);
449 if (!ret && !(ret = result.err)) {
450 INIT_COMPLETION(result.completion);
451 break;
453 case -EBADMSG:
454 if (template[i].novrfy)
455 /* verification failure was expected */
456 continue;
457 /* fall through */
458 default:
459 printk(KERN_ERR "alg: aead: %s failed on test "
460 "%d for %s: ret=%d\n", e, j, algo, -ret);
461 goto out;
464 q = input;
465 if (memcmp(q, template[i].result, template[i].rlen)) {
466 printk(KERN_ERR "alg: aead: Test %d failed on "
467 "%s for %s\n", j, e, algo);
468 hexdump(q, template[i].rlen);
469 ret = -EINVAL;
470 goto out;
475 for (i = 0, j = 0; i < tcount; i++) {
476 if (template[i].np) {
477 j++;
479 if (template[i].iv)
480 memcpy(iv, template[i].iv, MAX_IVLEN);
481 else
482 memset(iv, 0, MAX_IVLEN);
484 crypto_aead_clear_flags(tfm, ~0);
485 if (template[i].wk)
486 crypto_aead_set_flags(
487 tfm, CRYPTO_TFM_REQ_WEAK_KEY);
488 key = template[i].key;
490 ret = crypto_aead_setkey(tfm, key, template[i].klen);
491 if (!ret == template[i].fail) {
492 printk(KERN_ERR "alg: aead: setkey failed on "
493 "chunk test %d for %s: flags=%x\n", j,
494 algo, crypto_aead_get_flags(tfm));
495 goto out;
496 } else if (ret)
497 continue;
499 authsize = abs(template[i].rlen - template[i].ilen);
501 ret = -EINVAL;
502 sg_init_table(sg, template[i].np);
503 for (k = 0, temp = 0; k < template[i].np; k++) {
504 if (WARN_ON(offset_in_page(IDX[k]) +
505 template[i].tap[k] > PAGE_SIZE))
506 goto out;
508 q = xbuf[IDX[k] >> PAGE_SHIFT] +
509 offset_in_page(IDX[k]);
511 memcpy(q, template[i].input + temp,
512 template[i].tap[k]);
514 n = template[i].tap[k];
515 if (k == template[i].np - 1 && enc)
516 n += authsize;
517 if (offset_in_page(q) + n < PAGE_SIZE)
518 q[n] = 0;
520 sg_set_buf(&sg[k], q, template[i].tap[k]);
521 temp += template[i].tap[k];
524 ret = crypto_aead_setauthsize(tfm, authsize);
525 if (ret) {
526 printk(KERN_ERR "alg: aead: Failed to set "
527 "authsize to %u on chunk test %d for "
528 "%s\n", authsize, j, algo);
529 goto out;
532 if (enc) {
533 if (WARN_ON(sg[k - 1].offset +
534 sg[k - 1].length + authsize >
535 PAGE_SIZE)) {
536 ret = -EINVAL;
537 goto out;
540 sg[k - 1].length += authsize;
543 sg_init_table(asg, template[i].anp);
544 ret = -EINVAL;
545 for (k = 0, temp = 0; k < template[i].anp; k++) {
546 if (WARN_ON(offset_in_page(IDX[k]) +
547 template[i].atap[k] > PAGE_SIZE))
548 goto out;
549 sg_set_buf(&asg[k],
550 memcpy(axbuf[IDX[k] >> PAGE_SHIFT] +
551 offset_in_page(IDX[k]),
552 template[i].assoc + temp,
553 template[i].atap[k]),
554 template[i].atap[k]);
555 temp += template[i].atap[k];
558 aead_request_set_crypt(req, sg, sg,
559 template[i].ilen,
560 iv);
562 aead_request_set_assoc(req, asg, template[i].alen);
564 ret = enc ?
565 crypto_aead_encrypt(req) :
566 crypto_aead_decrypt(req);
568 switch (ret) {
569 case 0:
570 if (template[i].novrfy) {
571 /* verification was supposed to fail */
572 printk(KERN_ERR "alg: aead: %s failed "
573 "on chunk test %d for %s: ret "
574 "was 0, expected -EBADMSG\n",
575 e, j, algo);
576 /* so really, we got a bad message */
577 ret = -EBADMSG;
578 goto out;
580 break;
581 case -EINPROGRESS:
582 case -EBUSY:
583 ret = wait_for_completion_interruptible(
584 &result.completion);
585 if (!ret && !(ret = result.err)) {
586 INIT_COMPLETION(result.completion);
587 break;
589 case -EBADMSG:
590 if (template[i].novrfy)
591 /* verification failure was expected */
592 continue;
593 /* fall through */
594 default:
595 printk(KERN_ERR "alg: aead: %s failed on "
596 "chunk test %d for %s: ret=%d\n", e, j,
597 algo, -ret);
598 goto out;
601 ret = -EINVAL;
602 for (k = 0, temp = 0; k < template[i].np; k++) {
603 q = xbuf[IDX[k] >> PAGE_SHIFT] +
604 offset_in_page(IDX[k]);
606 n = template[i].tap[k];
607 if (k == template[i].np - 1)
608 n += enc ? authsize : -authsize;
610 if (memcmp(q, template[i].result + temp, n)) {
611 printk(KERN_ERR "alg: aead: Chunk "
612 "test %d failed on %s at page "
613 "%u for %s\n", j, e, k, algo);
614 hexdump(q, n);
615 goto out;
618 q += n;
619 if (k == template[i].np - 1 && !enc) {
620 if (memcmp(q, template[i].input +
621 temp + n, authsize))
622 n = authsize;
623 else
624 n = 0;
625 } else {
626 for (n = 0; offset_in_page(q + n) &&
627 q[n]; n++)
630 if (n) {
631 printk(KERN_ERR "alg: aead: Result "
632 "buffer corruption in chunk "
633 "test %d on %s at page %u for "
634 "%s: %u bytes:\n", j, e, k,
635 algo, n);
636 hexdump(q, n);
637 goto out;
640 temp += template[i].tap[k];
645 ret = 0;
647 out:
648 aead_request_free(req);
649 testmgr_free_buf(axbuf);
650 out_noaxbuf:
651 testmgr_free_buf(xbuf);
652 out_noxbuf:
653 return ret;
656 static int test_cipher(struct crypto_cipher *tfm, int enc,
657 struct cipher_testvec *template, unsigned int tcount)
659 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
660 unsigned int i, j, k;
661 char *q;
662 const char *e;
663 void *data;
664 char *xbuf[XBUFSIZE];
665 int ret = -ENOMEM;
667 if (testmgr_alloc_buf(xbuf))
668 goto out_nobuf;
670 if (enc == ENCRYPT)
671 e = "encryption";
672 else
673 e = "decryption";
675 j = 0;
676 for (i = 0; i < tcount; i++) {
677 if (template[i].np)
678 continue;
680 j++;
682 ret = -EINVAL;
683 if (WARN_ON(template[i].ilen > PAGE_SIZE))
684 goto out;
686 data = xbuf[0];
687 memcpy(data, template[i].input, template[i].ilen);
689 crypto_cipher_clear_flags(tfm, ~0);
690 if (template[i].wk)
691 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
693 ret = crypto_cipher_setkey(tfm, template[i].key,
694 template[i].klen);
695 if (!ret == template[i].fail) {
696 printk(KERN_ERR "alg: cipher: setkey failed "
697 "on test %d for %s: flags=%x\n", j,
698 algo, crypto_cipher_get_flags(tfm));
699 goto out;
700 } else if (ret)
701 continue;
703 for (k = 0; k < template[i].ilen;
704 k += crypto_cipher_blocksize(tfm)) {
705 if (enc)
706 crypto_cipher_encrypt_one(tfm, data + k,
707 data + k);
708 else
709 crypto_cipher_decrypt_one(tfm, data + k,
710 data + k);
713 q = data;
714 if (memcmp(q, template[i].result, template[i].rlen)) {
715 printk(KERN_ERR "alg: cipher: Test %d failed "
716 "on %s for %s\n", j, e, algo);
717 hexdump(q, template[i].rlen);
718 ret = -EINVAL;
719 goto out;
723 ret = 0;
725 out:
726 testmgr_free_buf(xbuf);
727 out_nobuf:
728 return ret;
731 static int test_skcipher(struct crypto_ablkcipher *tfm, int enc,
732 struct cipher_testvec *template, unsigned int tcount)
734 const char *algo =
735 crypto_tfm_alg_driver_name(crypto_ablkcipher_tfm(tfm));
736 unsigned int i, j, k, n, temp;
737 char *q;
738 struct ablkcipher_request *req;
739 struct scatterlist sg[8];
740 const char *e;
741 struct tcrypt_result result;
742 void *data;
743 char iv[MAX_IVLEN];
744 char *xbuf[XBUFSIZE];
745 int ret = -ENOMEM;
747 if (testmgr_alloc_buf(xbuf))
748 goto out_nobuf;
750 if (enc == ENCRYPT)
751 e = "encryption";
752 else
753 e = "decryption";
755 init_completion(&result.completion);
757 req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
758 if (!req) {
759 printk(KERN_ERR "alg: skcipher: Failed to allocate request "
760 "for %s\n", algo);
761 goto out;
764 ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
765 tcrypt_complete, &result);
767 j = 0;
768 for (i = 0; i < tcount; i++) {
769 if (template[i].iv)
770 memcpy(iv, template[i].iv, MAX_IVLEN);
771 else
772 memset(iv, 0, MAX_IVLEN);
774 if (!(template[i].np)) {
775 j++;
777 ret = -EINVAL;
778 if (WARN_ON(template[i].ilen > PAGE_SIZE))
779 goto out;
781 data = xbuf[0];
782 memcpy(data, template[i].input, template[i].ilen);
784 crypto_ablkcipher_clear_flags(tfm, ~0);
785 if (template[i].wk)
786 crypto_ablkcipher_set_flags(
787 tfm, CRYPTO_TFM_REQ_WEAK_KEY);
789 ret = crypto_ablkcipher_setkey(tfm, template[i].key,
790 template[i].klen);
791 if (!ret == template[i].fail) {
792 printk(KERN_ERR "alg: skcipher: setkey failed "
793 "on test %d for %s: flags=%x\n", j,
794 algo, crypto_ablkcipher_get_flags(tfm));
795 goto out;
796 } else if (ret)
797 continue;
799 sg_init_one(&sg[0], data, template[i].ilen);
801 ablkcipher_request_set_crypt(req, sg, sg,
802 template[i].ilen, iv);
803 ret = enc ?
804 crypto_ablkcipher_encrypt(req) :
805 crypto_ablkcipher_decrypt(req);
807 switch (ret) {
808 case 0:
809 break;
810 case -EINPROGRESS:
811 case -EBUSY:
812 ret = wait_for_completion_interruptible(
813 &result.completion);
814 if (!ret && !((ret = result.err))) {
815 INIT_COMPLETION(result.completion);
816 break;
818 /* fall through */
819 default:
820 printk(KERN_ERR "alg: skcipher: %s failed on "
821 "test %d for %s: ret=%d\n", e, j, algo,
822 -ret);
823 goto out;
826 q = data;
827 if (memcmp(q, template[i].result, template[i].rlen)) {
828 printk(KERN_ERR "alg: skcipher: Test %d "
829 "failed on %s for %s\n", j, e, algo);
830 hexdump(q, template[i].rlen);
831 ret = -EINVAL;
832 goto out;
837 j = 0;
838 for (i = 0; i < tcount; i++) {
840 if (template[i].iv)
841 memcpy(iv, template[i].iv, MAX_IVLEN);
842 else
843 memset(iv, 0, MAX_IVLEN);
845 if (template[i].np) {
846 j++;
848 crypto_ablkcipher_clear_flags(tfm, ~0);
849 if (template[i].wk)
850 crypto_ablkcipher_set_flags(
851 tfm, CRYPTO_TFM_REQ_WEAK_KEY);
853 ret = crypto_ablkcipher_setkey(tfm, template[i].key,
854 template[i].klen);
855 if (!ret == template[i].fail) {
856 printk(KERN_ERR "alg: skcipher: setkey failed "
857 "on chunk test %d for %s: flags=%x\n",
858 j, algo,
859 crypto_ablkcipher_get_flags(tfm));
860 goto out;
861 } else if (ret)
862 continue;
864 temp = 0;
865 ret = -EINVAL;
866 sg_init_table(sg, template[i].np);
867 for (k = 0; k < template[i].np; k++) {
868 if (WARN_ON(offset_in_page(IDX[k]) +
869 template[i].tap[k] > PAGE_SIZE))
870 goto out;
872 q = xbuf[IDX[k] >> PAGE_SHIFT] +
873 offset_in_page(IDX[k]);
875 memcpy(q, template[i].input + temp,
876 template[i].tap[k]);
878 if (offset_in_page(q) + template[i].tap[k] <
879 PAGE_SIZE)
880 q[template[i].tap[k]] = 0;
882 sg_set_buf(&sg[k], q, template[i].tap[k]);
884 temp += template[i].tap[k];
887 ablkcipher_request_set_crypt(req, sg, sg,
888 template[i].ilen, iv);
890 ret = enc ?
891 crypto_ablkcipher_encrypt(req) :
892 crypto_ablkcipher_decrypt(req);
894 switch (ret) {
895 case 0:
896 break;
897 case -EINPROGRESS:
898 case -EBUSY:
899 ret = wait_for_completion_interruptible(
900 &result.completion);
901 if (!ret && !((ret = result.err))) {
902 INIT_COMPLETION(result.completion);
903 break;
905 /* fall through */
906 default:
907 printk(KERN_ERR "alg: skcipher: %s failed on "
908 "chunk test %d for %s: ret=%d\n", e, j,
909 algo, -ret);
910 goto out;
913 temp = 0;
914 ret = -EINVAL;
915 for (k = 0; k < template[i].np; k++) {
916 q = xbuf[IDX[k] >> PAGE_SHIFT] +
917 offset_in_page(IDX[k]);
919 if (memcmp(q, template[i].result + temp,
920 template[i].tap[k])) {
921 printk(KERN_ERR "alg: skcipher: Chunk "
922 "test %d failed on %s at page "
923 "%u for %s\n", j, e, k, algo);
924 hexdump(q, template[i].tap[k]);
925 goto out;
928 q += template[i].tap[k];
929 for (n = 0; offset_in_page(q + n) && q[n]; n++)
931 if (n) {
932 printk(KERN_ERR "alg: skcipher: "
933 "Result buffer corruption in "
934 "chunk test %d on %s at page "
935 "%u for %s: %u bytes:\n", j, e,
936 k, algo, n);
937 hexdump(q, n);
938 goto out;
940 temp += template[i].tap[k];
945 ret = 0;
947 out:
948 ablkcipher_request_free(req);
949 testmgr_free_buf(xbuf);
950 out_nobuf:
951 return ret;
954 static int test_comp(struct crypto_comp *tfm, struct comp_testvec *ctemplate,
955 struct comp_testvec *dtemplate, int ctcount, int dtcount)
957 const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
958 unsigned int i;
959 char result[COMP_BUF_SIZE];
960 int ret;
962 for (i = 0; i < ctcount; i++) {
963 int ilen;
964 unsigned int dlen = COMP_BUF_SIZE;
966 memset(result, 0, sizeof (result));
968 ilen = ctemplate[i].inlen;
969 ret = crypto_comp_compress(tfm, ctemplate[i].input,
970 ilen, result, &dlen);
971 if (ret) {
972 printk(KERN_ERR "alg: comp: compression failed "
973 "on test %d for %s: ret=%d\n", i + 1, algo,
974 -ret);
975 goto out;
978 if (dlen != ctemplate[i].outlen) {
979 printk(KERN_ERR "alg: comp: Compression test %d "
980 "failed for %s: output len = %d\n", i + 1, algo,
981 dlen);
982 ret = -EINVAL;
983 goto out;
986 if (memcmp(result, ctemplate[i].output, dlen)) {
987 printk(KERN_ERR "alg: comp: Compression test %d "
988 "failed for %s\n", i + 1, algo);
989 hexdump(result, dlen);
990 ret = -EINVAL;
991 goto out;
995 for (i = 0; i < dtcount; i++) {
996 int ilen;
997 unsigned int dlen = COMP_BUF_SIZE;
999 memset(result, 0, sizeof (result));
1001 ilen = dtemplate[i].inlen;
1002 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
1003 ilen, result, &dlen);
1004 if (ret) {
1005 printk(KERN_ERR "alg: comp: decompression failed "
1006 "on test %d for %s: ret=%d\n", i + 1, algo,
1007 -ret);
1008 goto out;
1011 if (dlen != dtemplate[i].outlen) {
1012 printk(KERN_ERR "alg: comp: Decompression test %d "
1013 "failed for %s: output len = %d\n", i + 1, algo,
1014 dlen);
1015 ret = -EINVAL;
1016 goto out;
1019 if (memcmp(result, dtemplate[i].output, dlen)) {
1020 printk(KERN_ERR "alg: comp: Decompression test %d "
1021 "failed for %s\n", i + 1, algo);
1022 hexdump(result, dlen);
1023 ret = -EINVAL;
1024 goto out;
1028 ret = 0;
1030 out:
1031 return ret;
1034 static int test_pcomp(struct crypto_pcomp *tfm,
1035 struct pcomp_testvec *ctemplate,
1036 struct pcomp_testvec *dtemplate, int ctcount,
1037 int dtcount)
1039 const char *algo = crypto_tfm_alg_driver_name(crypto_pcomp_tfm(tfm));
1040 unsigned int i;
1041 char result[COMP_BUF_SIZE];
1042 int res;
1044 for (i = 0; i < ctcount; i++) {
1045 struct comp_request req;
1046 unsigned int produced = 0;
1048 res = crypto_compress_setup(tfm, ctemplate[i].params,
1049 ctemplate[i].paramsize);
1050 if (res) {
1051 pr_err("alg: pcomp: compression setup failed on test "
1052 "%d for %s: error=%d\n", i + 1, algo, res);
1053 return res;
1056 res = crypto_compress_init(tfm);
1057 if (res) {
1058 pr_err("alg: pcomp: compression init failed on test "
1059 "%d for %s: error=%d\n", i + 1, algo, res);
1060 return res;
1063 memset(result, 0, sizeof(result));
1065 req.next_in = ctemplate[i].input;
1066 req.avail_in = ctemplate[i].inlen / 2;
1067 req.next_out = result;
1068 req.avail_out = ctemplate[i].outlen / 2;
1070 res = crypto_compress_update(tfm, &req);
1071 if (res < 0 && (res != -EAGAIN || req.avail_in)) {
1072 pr_err("alg: pcomp: compression update failed on test "
1073 "%d for %s: error=%d\n", i + 1, algo, res);
1074 return res;
1076 if (res > 0)
1077 produced += res;
1079 /* Add remaining input data */
1080 req.avail_in += (ctemplate[i].inlen + 1) / 2;
1082 res = crypto_compress_update(tfm, &req);
1083 if (res < 0 && (res != -EAGAIN || req.avail_in)) {
1084 pr_err("alg: pcomp: compression update failed on test "
1085 "%d for %s: error=%d\n", i + 1, algo, res);
1086 return res;
1088 if (res > 0)
1089 produced += res;
1091 /* Provide remaining output space */
1092 req.avail_out += COMP_BUF_SIZE - ctemplate[i].outlen / 2;
1094 res = crypto_compress_final(tfm, &req);
1095 if (res < 0) {
1096 pr_err("alg: pcomp: compression final failed on test "
1097 "%d for %s: error=%d\n", i + 1, algo, res);
1098 return res;
1100 produced += res;
1102 if (COMP_BUF_SIZE - req.avail_out != ctemplate[i].outlen) {
1103 pr_err("alg: comp: Compression test %d failed for %s: "
1104 "output len = %d (expected %d)\n", i + 1, algo,
1105 COMP_BUF_SIZE - req.avail_out,
1106 ctemplate[i].outlen);
1107 return -EINVAL;
1110 if (produced != ctemplate[i].outlen) {
1111 pr_err("alg: comp: Compression test %d failed for %s: "
1112 "returned len = %u (expected %d)\n", i + 1,
1113 algo, produced, ctemplate[i].outlen);
1114 return -EINVAL;
1117 if (memcmp(result, ctemplate[i].output, ctemplate[i].outlen)) {
1118 pr_err("alg: pcomp: Compression test %d failed for "
1119 "%s\n", i + 1, algo);
1120 hexdump(result, ctemplate[i].outlen);
1121 return -EINVAL;
1125 for (i = 0; i < dtcount; i++) {
1126 struct comp_request req;
1127 unsigned int produced = 0;
1129 res = crypto_decompress_setup(tfm, dtemplate[i].params,
1130 dtemplate[i].paramsize);
1131 if (res) {
1132 pr_err("alg: pcomp: decompression setup failed on "
1133 "test %d for %s: error=%d\n", i + 1, algo, res);
1134 return res;
1137 res = crypto_decompress_init(tfm);
1138 if (res) {
1139 pr_err("alg: pcomp: decompression init failed on test "
1140 "%d for %s: error=%d\n", i + 1, algo, res);
1141 return res;
1144 memset(result, 0, sizeof(result));
1146 req.next_in = dtemplate[i].input;
1147 req.avail_in = dtemplate[i].inlen / 2;
1148 req.next_out = result;
1149 req.avail_out = dtemplate[i].outlen / 2;
1151 res = crypto_decompress_update(tfm, &req);
1152 if (res < 0 && (res != -EAGAIN || req.avail_in)) {
1153 pr_err("alg: pcomp: decompression update failed on "
1154 "test %d for %s: error=%d\n", i + 1, algo, res);
1155 return res;
1157 if (res > 0)
1158 produced += res;
1160 /* Add remaining input data */
1161 req.avail_in += (dtemplate[i].inlen + 1) / 2;
1163 res = crypto_decompress_update(tfm, &req);
1164 if (res < 0 && (res != -EAGAIN || req.avail_in)) {
1165 pr_err("alg: pcomp: decompression update failed on "
1166 "test %d for %s: error=%d\n", i + 1, algo, res);
1167 return res;
1169 if (res > 0)
1170 produced += res;
1172 /* Provide remaining output space */
1173 req.avail_out += COMP_BUF_SIZE - dtemplate[i].outlen / 2;
1175 res = crypto_decompress_final(tfm, &req);
1176 if (res < 0 && (res != -EAGAIN || req.avail_in)) {
1177 pr_err("alg: pcomp: decompression final failed on "
1178 "test %d for %s: error=%d\n", i + 1, algo, res);
1179 return res;
1181 if (res > 0)
1182 produced += res;
1184 if (COMP_BUF_SIZE - req.avail_out != dtemplate[i].outlen) {
1185 pr_err("alg: comp: Decompression test %d failed for "
1186 "%s: output len = %d (expected %d)\n", i + 1,
1187 algo, COMP_BUF_SIZE - req.avail_out,
1188 dtemplate[i].outlen);
1189 return -EINVAL;
1192 if (produced != dtemplate[i].outlen) {
1193 pr_err("alg: comp: Decompression test %d failed for "
1194 "%s: returned len = %u (expected %d)\n", i + 1,
1195 algo, produced, dtemplate[i].outlen);
1196 return -EINVAL;
1199 if (memcmp(result, dtemplate[i].output, dtemplate[i].outlen)) {
1200 pr_err("alg: pcomp: Decompression test %d failed for "
1201 "%s\n", i + 1, algo);
1202 hexdump(result, dtemplate[i].outlen);
1203 return -EINVAL;
1207 return 0;
1211 static int test_cprng(struct crypto_rng *tfm, struct cprng_testvec *template,
1212 unsigned int tcount)
1214 const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
1215 int err = 0, i, j, seedsize;
1216 u8 *seed;
1217 char result[32];
1219 seedsize = crypto_rng_seedsize(tfm);
1221 seed = kmalloc(seedsize, GFP_KERNEL);
1222 if (!seed) {
1223 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
1224 "for %s\n", algo);
1225 return -ENOMEM;
1228 for (i = 0; i < tcount; i++) {
1229 memset(result, 0, 32);
1231 memcpy(seed, template[i].v, template[i].vlen);
1232 memcpy(seed + template[i].vlen, template[i].key,
1233 template[i].klen);
1234 memcpy(seed + template[i].vlen + template[i].klen,
1235 template[i].dt, template[i].dtlen);
1237 err = crypto_rng_reset(tfm, seed, seedsize);
1238 if (err) {
1239 printk(KERN_ERR "alg: cprng: Failed to reset rng "
1240 "for %s\n", algo);
1241 goto out;
1244 for (j = 0; j < template[i].loops; j++) {
1245 err = crypto_rng_get_bytes(tfm, result,
1246 template[i].rlen);
1247 if (err != template[i].rlen) {
1248 printk(KERN_ERR "alg: cprng: Failed to obtain "
1249 "the correct amount of random data for "
1250 "%s (requested %d, got %d)\n", algo,
1251 template[i].rlen, err);
1252 goto out;
1256 err = memcmp(result, template[i].result,
1257 template[i].rlen);
1258 if (err) {
1259 printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
1260 i, algo);
1261 hexdump(result, template[i].rlen);
1262 err = -EINVAL;
1263 goto out;
1267 out:
1268 kfree(seed);
1269 return err;
1272 static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
1273 u32 type, u32 mask)
1275 struct crypto_aead *tfm;
1276 int err = 0;
1278 tfm = crypto_alloc_aead(driver, type, mask);
1279 if (IS_ERR(tfm)) {
1280 printk(KERN_ERR "alg: aead: Failed to load transform for %s: "
1281 "%ld\n", driver, PTR_ERR(tfm));
1282 return PTR_ERR(tfm);
1285 if (desc->suite.aead.enc.vecs) {
1286 err = test_aead(tfm, ENCRYPT, desc->suite.aead.enc.vecs,
1287 desc->suite.aead.enc.count);
1288 if (err)
1289 goto out;
1292 if (!err && desc->suite.aead.dec.vecs)
1293 err = test_aead(tfm, DECRYPT, desc->suite.aead.dec.vecs,
1294 desc->suite.aead.dec.count);
1296 out:
1297 crypto_free_aead(tfm);
1298 return err;
1301 static int alg_test_cipher(const struct alg_test_desc *desc,
1302 const char *driver, u32 type, u32 mask)
1304 struct crypto_cipher *tfm;
1305 int err = 0;
1307 tfm = crypto_alloc_cipher(driver, type, mask);
1308 if (IS_ERR(tfm)) {
1309 printk(KERN_ERR "alg: cipher: Failed to load transform for "
1310 "%s: %ld\n", driver, PTR_ERR(tfm));
1311 return PTR_ERR(tfm);
1314 if (desc->suite.cipher.enc.vecs) {
1315 err = test_cipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
1316 desc->suite.cipher.enc.count);
1317 if (err)
1318 goto out;
1321 if (desc->suite.cipher.dec.vecs)
1322 err = test_cipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
1323 desc->suite.cipher.dec.count);
1325 out:
1326 crypto_free_cipher(tfm);
1327 return err;
1330 static int alg_test_skcipher(const struct alg_test_desc *desc,
1331 const char *driver, u32 type, u32 mask)
1333 struct crypto_ablkcipher *tfm;
1334 int err = 0;
1336 tfm = crypto_alloc_ablkcipher(driver, type, mask);
1337 if (IS_ERR(tfm)) {
1338 printk(KERN_ERR "alg: skcipher: Failed to load transform for "
1339 "%s: %ld\n", driver, PTR_ERR(tfm));
1340 return PTR_ERR(tfm);
1343 if (desc->suite.cipher.enc.vecs) {
1344 err = test_skcipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
1345 desc->suite.cipher.enc.count);
1346 if (err)
1347 goto out;
1350 if (desc->suite.cipher.dec.vecs)
1351 err = test_skcipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
1352 desc->suite.cipher.dec.count);
1354 out:
1355 crypto_free_ablkcipher(tfm);
1356 return err;
1359 static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
1360 u32 type, u32 mask)
1362 struct crypto_comp *tfm;
1363 int err;
1365 tfm = crypto_alloc_comp(driver, type, mask);
1366 if (IS_ERR(tfm)) {
1367 printk(KERN_ERR "alg: comp: Failed to load transform for %s: "
1368 "%ld\n", driver, PTR_ERR(tfm));
1369 return PTR_ERR(tfm);
1372 err = test_comp(tfm, desc->suite.comp.comp.vecs,
1373 desc->suite.comp.decomp.vecs,
1374 desc->suite.comp.comp.count,
1375 desc->suite.comp.decomp.count);
1377 crypto_free_comp(tfm);
1378 return err;
1381 static int alg_test_pcomp(const struct alg_test_desc *desc, const char *driver,
1382 u32 type, u32 mask)
1384 struct crypto_pcomp *tfm;
1385 int err;
1387 tfm = crypto_alloc_pcomp(driver, type, mask);
1388 if (IS_ERR(tfm)) {
1389 pr_err("alg: pcomp: Failed to load transform for %s: %ld\n",
1390 driver, PTR_ERR(tfm));
1391 return PTR_ERR(tfm);
1394 err = test_pcomp(tfm, desc->suite.pcomp.comp.vecs,
1395 desc->suite.pcomp.decomp.vecs,
1396 desc->suite.pcomp.comp.count,
1397 desc->suite.pcomp.decomp.count);
1399 crypto_free_pcomp(tfm);
1400 return err;
1403 static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1404 u32 type, u32 mask)
1406 struct crypto_ahash *tfm;
1407 int err;
1409 tfm = crypto_alloc_ahash(driver, type, mask);
1410 if (IS_ERR(tfm)) {
1411 printk(KERN_ERR "alg: hash: Failed to load transform for %s: "
1412 "%ld\n", driver, PTR_ERR(tfm));
1413 return PTR_ERR(tfm);
1416 err = test_hash(tfm, desc->suite.hash.vecs, desc->suite.hash.count);
1418 crypto_free_ahash(tfm);
1419 return err;
1422 static int alg_test_crc32c(const struct alg_test_desc *desc,
1423 const char *driver, u32 type, u32 mask)
1425 struct crypto_shash *tfm;
1426 u32 val;
1427 int err;
1429 err = alg_test_hash(desc, driver, type, mask);
1430 if (err)
1431 goto out;
1433 tfm = crypto_alloc_shash(driver, type, mask);
1434 if (IS_ERR(tfm)) {
1435 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
1436 "%ld\n", driver, PTR_ERR(tfm));
1437 err = PTR_ERR(tfm);
1438 goto out;
1441 do {
1442 struct {
1443 struct shash_desc shash;
1444 char ctx[crypto_shash_descsize(tfm)];
1445 } sdesc;
1447 sdesc.shash.tfm = tfm;
1448 sdesc.shash.flags = 0;
1450 *(u32 *)sdesc.ctx = le32_to_cpu(420553207);
1451 err = crypto_shash_final(&sdesc.shash, (u8 *)&val);
1452 if (err) {
1453 printk(KERN_ERR "alg: crc32c: Operation failed for "
1454 "%s: %d\n", driver, err);
1455 break;
1458 if (val != ~420553207) {
1459 printk(KERN_ERR "alg: crc32c: Test failed for %s: "
1460 "%d\n", driver, val);
1461 err = -EINVAL;
1463 } while (0);
1465 crypto_free_shash(tfm);
1467 out:
1468 return err;
1471 static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
1472 u32 type, u32 mask)
1474 struct crypto_rng *rng;
1475 int err;
1477 rng = crypto_alloc_rng(driver, type, mask);
1478 if (IS_ERR(rng)) {
1479 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
1480 "%ld\n", driver, PTR_ERR(rng));
1481 return PTR_ERR(rng);
1484 err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
1486 crypto_free_rng(rng);
1488 return err;
1491 static int alg_test_null(const struct alg_test_desc *desc,
1492 const char *driver, u32 type, u32 mask)
1494 return 0;
1497 /* Please keep this list sorted by algorithm name. */
1498 static const struct alg_test_desc alg_test_descs[] = {
1500 .alg = "__driver-cbc-aes-aesni",
1501 .test = alg_test_null,
1502 .suite = {
1503 .cipher = {
1504 .enc = {
1505 .vecs = NULL,
1506 .count = 0
1508 .dec = {
1509 .vecs = NULL,
1510 .count = 0
1514 }, {
1515 .alg = "__driver-ecb-aes-aesni",
1516 .test = alg_test_null,
1517 .suite = {
1518 .cipher = {
1519 .enc = {
1520 .vecs = NULL,
1521 .count = 0
1523 .dec = {
1524 .vecs = NULL,
1525 .count = 0
1529 }, {
1530 .alg = "__ghash-pclmulqdqni",
1531 .test = alg_test_null,
1532 .suite = {
1533 .hash = {
1534 .vecs = NULL,
1535 .count = 0
1538 }, {
1539 .alg = "ansi_cprng",
1540 .test = alg_test_cprng,
1541 .fips_allowed = 1,
1542 .suite = {
1543 .cprng = {
1544 .vecs = ansi_cprng_aes_tv_template,
1545 .count = ANSI_CPRNG_AES_TEST_VECTORS
1548 }, {
1549 .alg = "cbc(aes)",
1550 .test = alg_test_skcipher,
1551 .fips_allowed = 1,
1552 .suite = {
1553 .cipher = {
1554 .enc = {
1555 .vecs = aes_cbc_enc_tv_template,
1556 .count = AES_CBC_ENC_TEST_VECTORS
1558 .dec = {
1559 .vecs = aes_cbc_dec_tv_template,
1560 .count = AES_CBC_DEC_TEST_VECTORS
1564 }, {
1565 .alg = "cbc(anubis)",
1566 .test = alg_test_skcipher,
1567 .suite = {
1568 .cipher = {
1569 .enc = {
1570 .vecs = anubis_cbc_enc_tv_template,
1571 .count = ANUBIS_CBC_ENC_TEST_VECTORS
1573 .dec = {
1574 .vecs = anubis_cbc_dec_tv_template,
1575 .count = ANUBIS_CBC_DEC_TEST_VECTORS
1579 }, {
1580 .alg = "cbc(blowfish)",
1581 .test = alg_test_skcipher,
1582 .suite = {
1583 .cipher = {
1584 .enc = {
1585 .vecs = bf_cbc_enc_tv_template,
1586 .count = BF_CBC_ENC_TEST_VECTORS
1588 .dec = {
1589 .vecs = bf_cbc_dec_tv_template,
1590 .count = BF_CBC_DEC_TEST_VECTORS
1594 }, {
1595 .alg = "cbc(camellia)",
1596 .test = alg_test_skcipher,
1597 .suite = {
1598 .cipher = {
1599 .enc = {
1600 .vecs = camellia_cbc_enc_tv_template,
1601 .count = CAMELLIA_CBC_ENC_TEST_VECTORS
1603 .dec = {
1604 .vecs = camellia_cbc_dec_tv_template,
1605 .count = CAMELLIA_CBC_DEC_TEST_VECTORS
1609 }, {
1610 .alg = "cbc(des)",
1611 .test = alg_test_skcipher,
1612 .suite = {
1613 .cipher = {
1614 .enc = {
1615 .vecs = des_cbc_enc_tv_template,
1616 .count = DES_CBC_ENC_TEST_VECTORS
1618 .dec = {
1619 .vecs = des_cbc_dec_tv_template,
1620 .count = DES_CBC_DEC_TEST_VECTORS
1624 }, {
1625 .alg = "cbc(des3_ede)",
1626 .test = alg_test_skcipher,
1627 .fips_allowed = 1,
1628 .suite = {
1629 .cipher = {
1630 .enc = {
1631 .vecs = des3_ede_cbc_enc_tv_template,
1632 .count = DES3_EDE_CBC_ENC_TEST_VECTORS
1634 .dec = {
1635 .vecs = des3_ede_cbc_dec_tv_template,
1636 .count = DES3_EDE_CBC_DEC_TEST_VECTORS
1640 }, {
1641 .alg = "cbc(twofish)",
1642 .test = alg_test_skcipher,
1643 .suite = {
1644 .cipher = {
1645 .enc = {
1646 .vecs = tf_cbc_enc_tv_template,
1647 .count = TF_CBC_ENC_TEST_VECTORS
1649 .dec = {
1650 .vecs = tf_cbc_dec_tv_template,
1651 .count = TF_CBC_DEC_TEST_VECTORS
1655 }, {
1656 .alg = "ccm(aes)",
1657 .test = alg_test_aead,
1658 .fips_allowed = 1,
1659 .suite = {
1660 .aead = {
1661 .enc = {
1662 .vecs = aes_ccm_enc_tv_template,
1663 .count = AES_CCM_ENC_TEST_VECTORS
1665 .dec = {
1666 .vecs = aes_ccm_dec_tv_template,
1667 .count = AES_CCM_DEC_TEST_VECTORS
1671 }, {
1672 .alg = "crc32c",
1673 .test = alg_test_crc32c,
1674 .fips_allowed = 1,
1675 .suite = {
1676 .hash = {
1677 .vecs = crc32c_tv_template,
1678 .count = CRC32C_TEST_VECTORS
1681 }, {
1682 .alg = "cryptd(__driver-ecb-aes-aesni)",
1683 .test = alg_test_null,
1684 .suite = {
1685 .cipher = {
1686 .enc = {
1687 .vecs = NULL,
1688 .count = 0
1690 .dec = {
1691 .vecs = NULL,
1692 .count = 0
1696 }, {
1697 .alg = "cryptd(__ghash-pclmulqdqni)",
1698 .test = alg_test_null,
1699 .suite = {
1700 .hash = {
1701 .vecs = NULL,
1702 .count = 0
1705 }, {
1706 .alg = "ctr(aes)",
1707 .test = alg_test_skcipher,
1708 .fips_allowed = 1,
1709 .suite = {
1710 .cipher = {
1711 .enc = {
1712 .vecs = aes_ctr_enc_tv_template,
1713 .count = AES_CTR_ENC_TEST_VECTORS
1715 .dec = {
1716 .vecs = aes_ctr_dec_tv_template,
1717 .count = AES_CTR_DEC_TEST_VECTORS
1721 }, {
1722 .alg = "cts(cbc(aes))",
1723 .test = alg_test_skcipher,
1724 .suite = {
1725 .cipher = {
1726 .enc = {
1727 .vecs = cts_mode_enc_tv_template,
1728 .count = CTS_MODE_ENC_TEST_VECTORS
1730 .dec = {
1731 .vecs = cts_mode_dec_tv_template,
1732 .count = CTS_MODE_DEC_TEST_VECTORS
1736 }, {
1737 .alg = "deflate",
1738 .test = alg_test_comp,
1739 .suite = {
1740 .comp = {
1741 .comp = {
1742 .vecs = deflate_comp_tv_template,
1743 .count = DEFLATE_COMP_TEST_VECTORS
1745 .decomp = {
1746 .vecs = deflate_decomp_tv_template,
1747 .count = DEFLATE_DECOMP_TEST_VECTORS
1751 }, {
1752 .alg = "ecb(__aes-aesni)",
1753 .test = alg_test_null,
1754 .suite = {
1755 .cipher = {
1756 .enc = {
1757 .vecs = NULL,
1758 .count = 0
1760 .dec = {
1761 .vecs = NULL,
1762 .count = 0
1766 }, {
1767 .alg = "ecb(aes)",
1768 .test = alg_test_skcipher,
1769 .fips_allowed = 1,
1770 .suite = {
1771 .cipher = {
1772 .enc = {
1773 .vecs = aes_enc_tv_template,
1774 .count = AES_ENC_TEST_VECTORS
1776 .dec = {
1777 .vecs = aes_dec_tv_template,
1778 .count = AES_DEC_TEST_VECTORS
1782 }, {
1783 .alg = "ecb(anubis)",
1784 .test = alg_test_skcipher,
1785 .suite = {
1786 .cipher = {
1787 .enc = {
1788 .vecs = anubis_enc_tv_template,
1789 .count = ANUBIS_ENC_TEST_VECTORS
1791 .dec = {
1792 .vecs = anubis_dec_tv_template,
1793 .count = ANUBIS_DEC_TEST_VECTORS
1797 }, {
1798 .alg = "ecb(arc4)",
1799 .test = alg_test_skcipher,
1800 .suite = {
1801 .cipher = {
1802 .enc = {
1803 .vecs = arc4_enc_tv_template,
1804 .count = ARC4_ENC_TEST_VECTORS
1806 .dec = {
1807 .vecs = arc4_dec_tv_template,
1808 .count = ARC4_DEC_TEST_VECTORS
1812 }, {
1813 .alg = "ecb(blowfish)",
1814 .test = alg_test_skcipher,
1815 .suite = {
1816 .cipher = {
1817 .enc = {
1818 .vecs = bf_enc_tv_template,
1819 .count = BF_ENC_TEST_VECTORS
1821 .dec = {
1822 .vecs = bf_dec_tv_template,
1823 .count = BF_DEC_TEST_VECTORS
1827 }, {
1828 .alg = "ecb(camellia)",
1829 .test = alg_test_skcipher,
1830 .suite = {
1831 .cipher = {
1832 .enc = {
1833 .vecs = camellia_enc_tv_template,
1834 .count = CAMELLIA_ENC_TEST_VECTORS
1836 .dec = {
1837 .vecs = camellia_dec_tv_template,
1838 .count = CAMELLIA_DEC_TEST_VECTORS
1842 }, {
1843 .alg = "ecb(cast5)",
1844 .test = alg_test_skcipher,
1845 .suite = {
1846 .cipher = {
1847 .enc = {
1848 .vecs = cast5_enc_tv_template,
1849 .count = CAST5_ENC_TEST_VECTORS
1851 .dec = {
1852 .vecs = cast5_dec_tv_template,
1853 .count = CAST5_DEC_TEST_VECTORS
1857 }, {
1858 .alg = "ecb(cast6)",
1859 .test = alg_test_skcipher,
1860 .suite = {
1861 .cipher = {
1862 .enc = {
1863 .vecs = cast6_enc_tv_template,
1864 .count = CAST6_ENC_TEST_VECTORS
1866 .dec = {
1867 .vecs = cast6_dec_tv_template,
1868 .count = CAST6_DEC_TEST_VECTORS
1872 }, {
1873 .alg = "ecb(des)",
1874 .test = alg_test_skcipher,
1875 .fips_allowed = 1,
1876 .suite = {
1877 .cipher = {
1878 .enc = {
1879 .vecs = des_enc_tv_template,
1880 .count = DES_ENC_TEST_VECTORS
1882 .dec = {
1883 .vecs = des_dec_tv_template,
1884 .count = DES_DEC_TEST_VECTORS
1888 }, {
1889 .alg = "ecb(des3_ede)",
1890 .test = alg_test_skcipher,
1891 .fips_allowed = 1,
1892 .suite = {
1893 .cipher = {
1894 .enc = {
1895 .vecs = des3_ede_enc_tv_template,
1896 .count = DES3_EDE_ENC_TEST_VECTORS
1898 .dec = {
1899 .vecs = des3_ede_dec_tv_template,
1900 .count = DES3_EDE_DEC_TEST_VECTORS
1904 }, {
1905 .alg = "ecb(khazad)",
1906 .test = alg_test_skcipher,
1907 .suite = {
1908 .cipher = {
1909 .enc = {
1910 .vecs = khazad_enc_tv_template,
1911 .count = KHAZAD_ENC_TEST_VECTORS
1913 .dec = {
1914 .vecs = khazad_dec_tv_template,
1915 .count = KHAZAD_DEC_TEST_VECTORS
1919 }, {
1920 .alg = "ecb(seed)",
1921 .test = alg_test_skcipher,
1922 .suite = {
1923 .cipher = {
1924 .enc = {
1925 .vecs = seed_enc_tv_template,
1926 .count = SEED_ENC_TEST_VECTORS
1928 .dec = {
1929 .vecs = seed_dec_tv_template,
1930 .count = SEED_DEC_TEST_VECTORS
1934 }, {
1935 .alg = "ecb(serpent)",
1936 .test = alg_test_skcipher,
1937 .suite = {
1938 .cipher = {
1939 .enc = {
1940 .vecs = serpent_enc_tv_template,
1941 .count = SERPENT_ENC_TEST_VECTORS
1943 .dec = {
1944 .vecs = serpent_dec_tv_template,
1945 .count = SERPENT_DEC_TEST_VECTORS
1949 }, {
1950 .alg = "ecb(tea)",
1951 .test = alg_test_skcipher,
1952 .suite = {
1953 .cipher = {
1954 .enc = {
1955 .vecs = tea_enc_tv_template,
1956 .count = TEA_ENC_TEST_VECTORS
1958 .dec = {
1959 .vecs = tea_dec_tv_template,
1960 .count = TEA_DEC_TEST_VECTORS
1964 }, {
1965 .alg = "ecb(tnepres)",
1966 .test = alg_test_skcipher,
1967 .suite = {
1968 .cipher = {
1969 .enc = {
1970 .vecs = tnepres_enc_tv_template,
1971 .count = TNEPRES_ENC_TEST_VECTORS
1973 .dec = {
1974 .vecs = tnepres_dec_tv_template,
1975 .count = TNEPRES_DEC_TEST_VECTORS
1979 }, {
1980 .alg = "ecb(twofish)",
1981 .test = alg_test_skcipher,
1982 .suite = {
1983 .cipher = {
1984 .enc = {
1985 .vecs = tf_enc_tv_template,
1986 .count = TF_ENC_TEST_VECTORS
1988 .dec = {
1989 .vecs = tf_dec_tv_template,
1990 .count = TF_DEC_TEST_VECTORS
1994 }, {
1995 .alg = "ecb(xeta)",
1996 .test = alg_test_skcipher,
1997 .suite = {
1998 .cipher = {
1999 .enc = {
2000 .vecs = xeta_enc_tv_template,
2001 .count = XETA_ENC_TEST_VECTORS
2003 .dec = {
2004 .vecs = xeta_dec_tv_template,
2005 .count = XETA_DEC_TEST_VECTORS
2009 }, {
2010 .alg = "ecb(xtea)",
2011 .test = alg_test_skcipher,
2012 .suite = {
2013 .cipher = {
2014 .enc = {
2015 .vecs = xtea_enc_tv_template,
2016 .count = XTEA_ENC_TEST_VECTORS
2018 .dec = {
2019 .vecs = xtea_dec_tv_template,
2020 .count = XTEA_DEC_TEST_VECTORS
2024 }, {
2025 .alg = "gcm(aes)",
2026 .test = alg_test_aead,
2027 .fips_allowed = 1,
2028 .suite = {
2029 .aead = {
2030 .enc = {
2031 .vecs = aes_gcm_enc_tv_template,
2032 .count = AES_GCM_ENC_TEST_VECTORS
2034 .dec = {
2035 .vecs = aes_gcm_dec_tv_template,
2036 .count = AES_GCM_DEC_TEST_VECTORS
2040 }, {
2041 .alg = "ghash",
2042 .test = alg_test_hash,
2043 .suite = {
2044 .hash = {
2045 .vecs = ghash_tv_template,
2046 .count = GHASH_TEST_VECTORS
2049 }, {
2050 .alg = "hmac(md5)",
2051 .test = alg_test_hash,
2052 .suite = {
2053 .hash = {
2054 .vecs = hmac_md5_tv_template,
2055 .count = HMAC_MD5_TEST_VECTORS
2058 }, {
2059 .alg = "hmac(rmd128)",
2060 .test = alg_test_hash,
2061 .suite = {
2062 .hash = {
2063 .vecs = hmac_rmd128_tv_template,
2064 .count = HMAC_RMD128_TEST_VECTORS
2067 }, {
2068 .alg = "hmac(rmd160)",
2069 .test = alg_test_hash,
2070 .suite = {
2071 .hash = {
2072 .vecs = hmac_rmd160_tv_template,
2073 .count = HMAC_RMD160_TEST_VECTORS
2076 }, {
2077 .alg = "hmac(sha1)",
2078 .test = alg_test_hash,
2079 .fips_allowed = 1,
2080 .suite = {
2081 .hash = {
2082 .vecs = hmac_sha1_tv_template,
2083 .count = HMAC_SHA1_TEST_VECTORS
2086 }, {
2087 .alg = "hmac(sha224)",
2088 .test = alg_test_hash,
2089 .fips_allowed = 1,
2090 .suite = {
2091 .hash = {
2092 .vecs = hmac_sha224_tv_template,
2093 .count = HMAC_SHA224_TEST_VECTORS
2096 }, {
2097 .alg = "hmac(sha256)",
2098 .test = alg_test_hash,
2099 .fips_allowed = 1,
2100 .suite = {
2101 .hash = {
2102 .vecs = hmac_sha256_tv_template,
2103 .count = HMAC_SHA256_TEST_VECTORS
2106 }, {
2107 .alg = "hmac(sha384)",
2108 .test = alg_test_hash,
2109 .fips_allowed = 1,
2110 .suite = {
2111 .hash = {
2112 .vecs = hmac_sha384_tv_template,
2113 .count = HMAC_SHA384_TEST_VECTORS
2116 }, {
2117 .alg = "hmac(sha512)",
2118 .test = alg_test_hash,
2119 .fips_allowed = 1,
2120 .suite = {
2121 .hash = {
2122 .vecs = hmac_sha512_tv_template,
2123 .count = HMAC_SHA512_TEST_VECTORS
2126 }, {
2127 .alg = "lrw(aes)",
2128 .test = alg_test_skcipher,
2129 .suite = {
2130 .cipher = {
2131 .enc = {
2132 .vecs = aes_lrw_enc_tv_template,
2133 .count = AES_LRW_ENC_TEST_VECTORS
2135 .dec = {
2136 .vecs = aes_lrw_dec_tv_template,
2137 .count = AES_LRW_DEC_TEST_VECTORS
2141 }, {
2142 .alg = "lzo",
2143 .test = alg_test_comp,
2144 .suite = {
2145 .comp = {
2146 .comp = {
2147 .vecs = lzo_comp_tv_template,
2148 .count = LZO_COMP_TEST_VECTORS
2150 .decomp = {
2151 .vecs = lzo_decomp_tv_template,
2152 .count = LZO_DECOMP_TEST_VECTORS
2156 }, {
2157 .alg = "md4",
2158 .test = alg_test_hash,
2159 .suite = {
2160 .hash = {
2161 .vecs = md4_tv_template,
2162 .count = MD4_TEST_VECTORS
2165 }, {
2166 .alg = "md5",
2167 .test = alg_test_hash,
2168 .suite = {
2169 .hash = {
2170 .vecs = md5_tv_template,
2171 .count = MD5_TEST_VECTORS
2174 }, {
2175 .alg = "michael_mic",
2176 .test = alg_test_hash,
2177 .suite = {
2178 .hash = {
2179 .vecs = michael_mic_tv_template,
2180 .count = MICHAEL_MIC_TEST_VECTORS
2183 }, {
2184 .alg = "pcbc(fcrypt)",
2185 .test = alg_test_skcipher,
2186 .suite = {
2187 .cipher = {
2188 .enc = {
2189 .vecs = fcrypt_pcbc_enc_tv_template,
2190 .count = FCRYPT_ENC_TEST_VECTORS
2192 .dec = {
2193 .vecs = fcrypt_pcbc_dec_tv_template,
2194 .count = FCRYPT_DEC_TEST_VECTORS
2198 }, {
2199 .alg = "rfc3686(ctr(aes))",
2200 .test = alg_test_skcipher,
2201 .fips_allowed = 1,
2202 .suite = {
2203 .cipher = {
2204 .enc = {
2205 .vecs = aes_ctr_rfc3686_enc_tv_template,
2206 .count = AES_CTR_3686_ENC_TEST_VECTORS
2208 .dec = {
2209 .vecs = aes_ctr_rfc3686_dec_tv_template,
2210 .count = AES_CTR_3686_DEC_TEST_VECTORS
2214 }, {
2215 .alg = "rfc4309(ccm(aes))",
2216 .test = alg_test_aead,
2217 .fips_allowed = 1,
2218 .suite = {
2219 .aead = {
2220 .enc = {
2221 .vecs = aes_ccm_rfc4309_enc_tv_template,
2222 .count = AES_CCM_4309_ENC_TEST_VECTORS
2224 .dec = {
2225 .vecs = aes_ccm_rfc4309_dec_tv_template,
2226 .count = AES_CCM_4309_DEC_TEST_VECTORS
2230 }, {
2231 .alg = "rmd128",
2232 .test = alg_test_hash,
2233 .suite = {
2234 .hash = {
2235 .vecs = rmd128_tv_template,
2236 .count = RMD128_TEST_VECTORS
2239 }, {
2240 .alg = "rmd160",
2241 .test = alg_test_hash,
2242 .suite = {
2243 .hash = {
2244 .vecs = rmd160_tv_template,
2245 .count = RMD160_TEST_VECTORS
2248 }, {
2249 .alg = "rmd256",
2250 .test = alg_test_hash,
2251 .suite = {
2252 .hash = {
2253 .vecs = rmd256_tv_template,
2254 .count = RMD256_TEST_VECTORS
2257 }, {
2258 .alg = "rmd320",
2259 .test = alg_test_hash,
2260 .suite = {
2261 .hash = {
2262 .vecs = rmd320_tv_template,
2263 .count = RMD320_TEST_VECTORS
2266 }, {
2267 .alg = "salsa20",
2268 .test = alg_test_skcipher,
2269 .suite = {
2270 .cipher = {
2271 .enc = {
2272 .vecs = salsa20_stream_enc_tv_template,
2273 .count = SALSA20_STREAM_ENC_TEST_VECTORS
2277 }, {
2278 .alg = "sha1",
2279 .test = alg_test_hash,
2280 .fips_allowed = 1,
2281 .suite = {
2282 .hash = {
2283 .vecs = sha1_tv_template,
2284 .count = SHA1_TEST_VECTORS
2287 }, {
2288 .alg = "sha224",
2289 .test = alg_test_hash,
2290 .fips_allowed = 1,
2291 .suite = {
2292 .hash = {
2293 .vecs = sha224_tv_template,
2294 .count = SHA224_TEST_VECTORS
2297 }, {
2298 .alg = "sha256",
2299 .test = alg_test_hash,
2300 .fips_allowed = 1,
2301 .suite = {
2302 .hash = {
2303 .vecs = sha256_tv_template,
2304 .count = SHA256_TEST_VECTORS
2307 }, {
2308 .alg = "sha384",
2309 .test = alg_test_hash,
2310 .fips_allowed = 1,
2311 .suite = {
2312 .hash = {
2313 .vecs = sha384_tv_template,
2314 .count = SHA384_TEST_VECTORS
2317 }, {
2318 .alg = "sha512",
2319 .test = alg_test_hash,
2320 .fips_allowed = 1,
2321 .suite = {
2322 .hash = {
2323 .vecs = sha512_tv_template,
2324 .count = SHA512_TEST_VECTORS
2327 }, {
2328 .alg = "tgr128",
2329 .test = alg_test_hash,
2330 .suite = {
2331 .hash = {
2332 .vecs = tgr128_tv_template,
2333 .count = TGR128_TEST_VECTORS
2336 }, {
2337 .alg = "tgr160",
2338 .test = alg_test_hash,
2339 .suite = {
2340 .hash = {
2341 .vecs = tgr160_tv_template,
2342 .count = TGR160_TEST_VECTORS
2345 }, {
2346 .alg = "tgr192",
2347 .test = alg_test_hash,
2348 .suite = {
2349 .hash = {
2350 .vecs = tgr192_tv_template,
2351 .count = TGR192_TEST_VECTORS
2354 }, {
2355 .alg = "vmac(aes)",
2356 .test = alg_test_hash,
2357 .suite = {
2358 .hash = {
2359 .vecs = aes_vmac128_tv_template,
2360 .count = VMAC_AES_TEST_VECTORS
2363 }, {
2364 .alg = "wp256",
2365 .test = alg_test_hash,
2366 .suite = {
2367 .hash = {
2368 .vecs = wp256_tv_template,
2369 .count = WP256_TEST_VECTORS
2372 }, {
2373 .alg = "wp384",
2374 .test = alg_test_hash,
2375 .suite = {
2376 .hash = {
2377 .vecs = wp384_tv_template,
2378 .count = WP384_TEST_VECTORS
2381 }, {
2382 .alg = "wp512",
2383 .test = alg_test_hash,
2384 .suite = {
2385 .hash = {
2386 .vecs = wp512_tv_template,
2387 .count = WP512_TEST_VECTORS
2390 }, {
2391 .alg = "xcbc(aes)",
2392 .test = alg_test_hash,
2393 .suite = {
2394 .hash = {
2395 .vecs = aes_xcbc128_tv_template,
2396 .count = XCBC_AES_TEST_VECTORS
2399 }, {
2400 .alg = "xts(aes)",
2401 .test = alg_test_skcipher,
2402 .suite = {
2403 .cipher = {
2404 .enc = {
2405 .vecs = aes_xts_enc_tv_template,
2406 .count = AES_XTS_ENC_TEST_VECTORS
2408 .dec = {
2409 .vecs = aes_xts_dec_tv_template,
2410 .count = AES_XTS_DEC_TEST_VECTORS
2414 }, {
2415 .alg = "zlib",
2416 .test = alg_test_pcomp,
2417 .suite = {
2418 .pcomp = {
2419 .comp = {
2420 .vecs = zlib_comp_tv_template,
2421 .count = ZLIB_COMP_TEST_VECTORS
2423 .decomp = {
2424 .vecs = zlib_decomp_tv_template,
2425 .count = ZLIB_DECOMP_TEST_VECTORS
2432 static int alg_find_test(const char *alg)
2434 int start = 0;
2435 int end = ARRAY_SIZE(alg_test_descs);
2437 while (start < end) {
2438 int i = (start + end) / 2;
2439 int diff = strcmp(alg_test_descs[i].alg, alg);
2441 if (diff > 0) {
2442 end = i;
2443 continue;
2446 if (diff < 0) {
2447 start = i + 1;
2448 continue;
2451 return i;
2454 return -1;
2457 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
2459 int i;
2460 int j;
2461 int rc;
2463 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
2464 char nalg[CRYPTO_MAX_ALG_NAME];
2466 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
2467 sizeof(nalg))
2468 return -ENAMETOOLONG;
2470 i = alg_find_test(nalg);
2471 if (i < 0)
2472 goto notest;
2474 if (fips_enabled && !alg_test_descs[i].fips_allowed)
2475 goto non_fips_alg;
2477 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
2478 goto test_done;
2481 i = alg_find_test(alg);
2482 j = alg_find_test(driver);
2483 if (i < 0 && j < 0)
2484 goto notest;
2486 if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
2487 (j >= 0 && !alg_test_descs[j].fips_allowed)))
2488 goto non_fips_alg;
2490 rc = 0;
2491 if (i >= 0)
2492 rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
2493 type, mask);
2494 if (j >= 0)
2495 rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
2496 type, mask);
2498 test_done:
2499 if (fips_enabled && rc)
2500 panic("%s: %s alg self test failed in fips mode!\n", driver, alg);
2502 if (fips_enabled && !rc)
2503 printk(KERN_INFO "alg: self-tests for %s (%s) passed\n",
2504 driver, alg);
2506 return rc;
2508 notest:
2509 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
2510 return 0;
2511 non_fips_alg:
2512 return -EINVAL;
2515 #endif /* CONFIG_CRYPTO_MANAGER_TESTS */
2517 EXPORT_SYMBOL_GPL(alg_test);