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 * Updated RFC4106 AES-GCM testing.
10 * Authors: Aidan O'Mahony (aidan.o.mahony@intel.com)
11 * Adrian Hoban <adrian.hoban@intel.com>
12 * Gabriele Paoloni <gabriele.paoloni@intel.com>
13 * Tadeusz Struk (tadeusz.struk@intel.com)
14 * Copyright (c) 2010, Intel Corporation.
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the Free
18 * Software Foundation; either version 2 of the License, or (at your option)
23 #include <crypto/aead.h>
24 #include <crypto/hash.h>
25 #include <crypto/skcipher.h>
26 #include <linux/err.h>
27 #include <linux/fips.h>
28 #include <linux/module.h>
29 #include <linux/scatterlist.h>
30 #include <linux/slab.h>
31 #include <linux/string.h>
32 #include <crypto/rng.h>
33 #include <crypto/drbg.h>
34 #include <crypto/akcipher.h>
35 #include <crypto/kpp.h>
36 #include <crypto/acompress.h>
41 module_param(notests, bool, 0644);
42 MODULE_PARM_DESC(notests, "disable crypto self-tests");
44 #ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
47 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
57 * Need slab memory for testing (size in number of pages).
62 * Indexes into the xbuf to simulate cross-page access.
74 * Used by test_cipher()
79 struct tcrypt_result {
80 struct completion completion;
84 struct aead_test_suite {
86 struct aead_testvec *vecs;
91 struct cipher_test_suite {
93 struct cipher_testvec *vecs;
98 struct comp_test_suite {
100 struct comp_testvec *vecs;
105 struct hash_test_suite {
106 struct hash_testvec *vecs;
110 struct cprng_test_suite {
111 struct cprng_testvec *vecs;
115 struct drbg_test_suite {
116 struct drbg_testvec *vecs;
120 struct akcipher_test_suite {
121 struct akcipher_testvec *vecs;
125 struct kpp_test_suite {
126 struct kpp_testvec *vecs;
130 struct alg_test_desc {
132 int (*test)(const struct alg_test_desc *desc, const char *driver,
134 int fips_allowed; /* set if alg is allowed in fips mode */
137 struct aead_test_suite aead;
138 struct cipher_test_suite cipher;
139 struct comp_test_suite comp;
140 struct hash_test_suite hash;
141 struct cprng_test_suite cprng;
142 struct drbg_test_suite drbg;
143 struct akcipher_test_suite akcipher;
144 struct kpp_test_suite kpp;
148 static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
150 static void hexdump(unsigned char *buf, unsigned int len)
152 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
157 static void tcrypt_complete(struct crypto_async_request *req, int err)
159 struct tcrypt_result *res = req->data;
161 if (err == -EINPROGRESS)
165 complete(&res->completion);
168 static int testmgr_alloc_buf(char *buf[XBUFSIZE])
172 for (i = 0; i < XBUFSIZE; i++) {
173 buf[i] = (void *)__get_free_page(GFP_KERNEL);
182 free_page((unsigned long)buf[i]);
187 static void testmgr_free_buf(char *buf[XBUFSIZE])
191 for (i = 0; i < XBUFSIZE; i++)
192 free_page((unsigned long)buf[i]);
195 static int wait_async_op(struct tcrypt_result *tr, int ret)
197 if (ret == -EINPROGRESS || ret == -EBUSY) {
198 wait_for_completion(&tr->completion);
199 reinit_completion(&tr->completion);
205 static int ahash_partial_update(struct ahash_request **preq,
206 struct crypto_ahash *tfm, struct hash_testvec *template,
207 void *hash_buff, int k, int temp, struct scatterlist *sg,
208 const char *algo, char *result, struct tcrypt_result *tresult)
211 struct ahash_request *req;
212 int statesize, ret = -EINVAL;
213 const char guard[] = { 0x00, 0xba, 0xad, 0x00 };
216 statesize = crypto_ahash_statesize(
217 crypto_ahash_reqtfm(req));
218 state = kmalloc(statesize + sizeof(guard), GFP_KERNEL);
220 pr_err("alt: hash: Failed to alloc state for %s\n", algo);
223 memcpy(state + statesize, guard, sizeof(guard));
224 ret = crypto_ahash_export(req, state);
225 WARN_ON(memcmp(state + statesize, guard, sizeof(guard)));
227 pr_err("alt: hash: Failed to export() for %s\n", algo);
230 ahash_request_free(req);
231 req = ahash_request_alloc(tfm, GFP_KERNEL);
233 pr_err("alg: hash: Failed to alloc request for %s\n", algo);
236 ahash_request_set_callback(req,
237 CRYPTO_TFM_REQ_MAY_BACKLOG,
238 tcrypt_complete, tresult);
240 memcpy(hash_buff, template->plaintext + temp,
242 sg_init_one(&sg[0], hash_buff, template->tap[k]);
243 ahash_request_set_crypt(req, sg, result, template->tap[k]);
244 ret = crypto_ahash_import(req, state);
246 pr_err("alg: hash: Failed to import() for %s\n", algo);
249 ret = wait_async_op(tresult, crypto_ahash_update(req));
256 ahash_request_free(req);
263 static int __test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
264 unsigned int tcount, bool use_digest,
265 const int align_offset)
267 const char *algo = crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
268 unsigned int i, j, k, temp;
269 struct scatterlist sg[8];
272 struct ahash_request *req;
273 struct tcrypt_result tresult;
275 char *xbuf[XBUFSIZE];
278 result = kmalloc(MAX_DIGEST_SIZE, GFP_KERNEL);
281 key = kmalloc(MAX_KEYLEN, GFP_KERNEL);
284 if (testmgr_alloc_buf(xbuf))
287 init_completion(&tresult.completion);
289 req = ahash_request_alloc(tfm, GFP_KERNEL);
291 printk(KERN_ERR "alg: hash: Failed to allocate request for "
295 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
296 tcrypt_complete, &tresult);
299 for (i = 0; i < tcount; i++) {
304 if (WARN_ON(align_offset + template[i].psize > PAGE_SIZE))
308 memset(result, 0, MAX_DIGEST_SIZE);
311 hash_buff += align_offset;
313 memcpy(hash_buff, template[i].plaintext, template[i].psize);
314 sg_init_one(&sg[0], hash_buff, template[i].psize);
316 if (template[i].ksize) {
317 crypto_ahash_clear_flags(tfm, ~0);
318 if (template[i].ksize > MAX_KEYLEN) {
319 pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
320 j, algo, template[i].ksize, MAX_KEYLEN);
324 memcpy(key, template[i].key, template[i].ksize);
325 ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
327 printk(KERN_ERR "alg: hash: setkey failed on "
328 "test %d for %s: ret=%d\n", j, algo,
334 ahash_request_set_crypt(req, sg, result, template[i].psize);
336 ret = wait_async_op(&tresult, crypto_ahash_digest(req));
338 pr_err("alg: hash: digest failed on test %d "
339 "for %s: ret=%d\n", j, algo, -ret);
343 ret = wait_async_op(&tresult, crypto_ahash_init(req));
345 pr_err("alt: hash: init failed on test %d "
346 "for %s: ret=%d\n", j, algo, -ret);
349 ret = wait_async_op(&tresult, crypto_ahash_update(req));
351 pr_err("alt: hash: update failed on test %d "
352 "for %s: ret=%d\n", j, algo, -ret);
355 ret = wait_async_op(&tresult, crypto_ahash_final(req));
357 pr_err("alt: hash: final failed on test %d "
358 "for %s: ret=%d\n", j, algo, -ret);
363 if (memcmp(result, template[i].digest,
364 crypto_ahash_digestsize(tfm))) {
365 printk(KERN_ERR "alg: hash: Test %d failed for %s\n",
367 hexdump(result, crypto_ahash_digestsize(tfm));
374 for (i = 0; i < tcount; i++) {
375 /* alignment tests are only done with continuous buffers */
376 if (align_offset != 0)
383 memset(result, 0, MAX_DIGEST_SIZE);
386 sg_init_table(sg, template[i].np);
388 for (k = 0; k < template[i].np; k++) {
389 if (WARN_ON(offset_in_page(IDX[k]) +
390 template[i].tap[k] > PAGE_SIZE))
393 memcpy(xbuf[IDX[k] >> PAGE_SHIFT] +
394 offset_in_page(IDX[k]),
395 template[i].plaintext + temp,
398 temp += template[i].tap[k];
401 if (template[i].ksize) {
402 if (template[i].ksize > MAX_KEYLEN) {
403 pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
404 j, algo, template[i].ksize, MAX_KEYLEN);
408 crypto_ahash_clear_flags(tfm, ~0);
409 memcpy(key, template[i].key, template[i].ksize);
410 ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
413 printk(KERN_ERR "alg: hash: setkey "
414 "failed on chunking test %d "
415 "for %s: ret=%d\n", j, algo, -ret);
420 ahash_request_set_crypt(req, sg, result, template[i].psize);
421 ret = crypto_ahash_digest(req);
427 wait_for_completion(&tresult.completion);
428 reinit_completion(&tresult.completion);
434 printk(KERN_ERR "alg: hash: digest failed "
435 "on chunking test %d for %s: "
436 "ret=%d\n", j, algo, -ret);
440 if (memcmp(result, template[i].digest,
441 crypto_ahash_digestsize(tfm))) {
442 printk(KERN_ERR "alg: hash: Chunking test %d "
443 "failed for %s\n", j, algo);
444 hexdump(result, crypto_ahash_digestsize(tfm));
450 /* partial update exercise */
452 for (i = 0; i < tcount; i++) {
453 /* alignment tests are only done with continuous buffers */
454 if (align_offset != 0)
457 if (template[i].np < 2)
461 memset(result, 0, MAX_DIGEST_SIZE);
465 memcpy(hash_buff, template[i].plaintext,
467 sg_init_one(&sg[0], hash_buff, template[i].tap[0]);
469 if (template[i].ksize) {
470 crypto_ahash_clear_flags(tfm, ~0);
471 if (template[i].ksize > MAX_KEYLEN) {
472 pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
473 j, algo, template[i].ksize, MAX_KEYLEN);
477 memcpy(key, template[i].key, template[i].ksize);
478 ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
480 pr_err("alg: hash: setkey failed on test %d for %s: ret=%d\n",
486 ahash_request_set_crypt(req, sg, result, template[i].tap[0]);
487 ret = wait_async_op(&tresult, crypto_ahash_init(req));
489 pr_err("alt: hash: init failed on test %d for %s: ret=%d\n",
493 ret = wait_async_op(&tresult, crypto_ahash_update(req));
495 pr_err("alt: hash: update failed on test %d for %s: ret=%d\n",
500 temp = template[i].tap[0];
501 for (k = 1; k < template[i].np; k++) {
502 ret = ahash_partial_update(&req, tfm, &template[i],
503 hash_buff, k, temp, &sg[0], algo, result,
506 pr_err("hash: partial update failed on test %d for %s: ret=%d\n",
510 temp += template[i].tap[k];
512 ret = wait_async_op(&tresult, crypto_ahash_final(req));
514 pr_err("alt: hash: final failed on test %d for %s: ret=%d\n",
518 if (memcmp(result, template[i].digest,
519 crypto_ahash_digestsize(tfm))) {
520 pr_err("alg: hash: Partial Test %d failed for %s\n",
522 hexdump(result, crypto_ahash_digestsize(tfm));
531 ahash_request_free(req);
533 testmgr_free_buf(xbuf);
540 static int test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
541 unsigned int tcount, bool use_digest)
543 unsigned int alignmask;
546 ret = __test_hash(tfm, template, tcount, use_digest, 0);
550 /* test unaligned buffers, check with one byte offset */
551 ret = __test_hash(tfm, template, tcount, use_digest, 1);
555 alignmask = crypto_tfm_alg_alignmask(&tfm->base);
557 /* Check if alignment mask for tfm is correctly set. */
558 ret = __test_hash(tfm, template, tcount, use_digest,
567 static int __test_aead(struct crypto_aead *tfm, int enc,
568 struct aead_testvec *template, unsigned int tcount,
569 const bool diff_dst, const int align_offset)
571 const char *algo = crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm));
572 unsigned int i, j, k, n, temp;
576 struct aead_request *req;
577 struct scatterlist *sg;
578 struct scatterlist *sgout;
580 struct tcrypt_result result;
581 unsigned int authsize, iv_len;
586 char *xbuf[XBUFSIZE];
587 char *xoutbuf[XBUFSIZE];
588 char *axbuf[XBUFSIZE];
590 iv = kzalloc(MAX_IVLEN, GFP_KERNEL);
593 key = kmalloc(MAX_KEYLEN, GFP_KERNEL);
596 if (testmgr_alloc_buf(xbuf))
598 if (testmgr_alloc_buf(axbuf))
600 if (diff_dst && testmgr_alloc_buf(xoutbuf))
603 /* avoid "the frame size is larger than 1024 bytes" compiler warning */
604 sg = kmalloc(sizeof(*sg) * 8 * (diff_dst ? 4 : 2), GFP_KERNEL);
619 init_completion(&result.completion);
621 req = aead_request_alloc(tfm, GFP_KERNEL);
623 pr_err("alg: aead%s: Failed to allocate request for %s\n",
628 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
629 tcrypt_complete, &result);
631 iv_len = crypto_aead_ivsize(tfm);
633 for (i = 0, j = 0; i < tcount; i++) {
639 /* some templates have no input data but they will
643 input += align_offset;
647 if (WARN_ON(align_offset + template[i].ilen >
648 PAGE_SIZE || template[i].alen > PAGE_SIZE))
651 memcpy(input, template[i].input, template[i].ilen);
652 memcpy(assoc, template[i].assoc, template[i].alen);
654 memcpy(iv, template[i].iv, iv_len);
656 memset(iv, 0, iv_len);
658 crypto_aead_clear_flags(tfm, ~0);
660 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
662 if (template[i].klen > MAX_KEYLEN) {
663 pr_err("alg: aead%s: setkey failed on test %d for %s: key size %d > %d\n",
664 d, j, algo, template[i].klen,
669 memcpy(key, template[i].key, template[i].klen);
671 ret = crypto_aead_setkey(tfm, key, template[i].klen);
672 if (template[i].fail == !ret) {
673 pr_err("alg: aead%s: setkey failed on test %d for %s: flags=%x\n",
674 d, j, algo, crypto_aead_get_flags(tfm));
679 authsize = abs(template[i].rlen - template[i].ilen);
680 ret = crypto_aead_setauthsize(tfm, authsize);
682 pr_err("alg: aead%s: Failed to set authsize to %u on test %d for %s\n",
683 d, authsize, j, algo);
687 k = !!template[i].alen;
688 sg_init_table(sg, k + 1);
689 sg_set_buf(&sg[0], assoc, template[i].alen);
690 sg_set_buf(&sg[k], input,
691 template[i].ilen + (enc ? authsize : 0));
695 sg_init_table(sgout, k + 1);
696 sg_set_buf(&sgout[0], assoc, template[i].alen);
699 output += align_offset;
700 sg_set_buf(&sgout[k], output,
701 template[i].rlen + (enc ? 0 : authsize));
704 aead_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
705 template[i].ilen, iv);
707 aead_request_set_ad(req, template[i].alen);
709 ret = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
713 if (template[i].novrfy) {
714 /* verification was supposed to fail */
715 pr_err("alg: aead%s: %s failed on test %d for %s: ret was 0, expected -EBADMSG\n",
717 /* so really, we got a bad message */
724 wait_for_completion(&result.completion);
725 reinit_completion(&result.completion);
730 if (template[i].novrfy)
731 /* verification failure was expected */
735 pr_err("alg: aead%s: %s failed on test %d for %s: ret=%d\n",
736 d, e, j, algo, -ret);
741 if (memcmp(q, template[i].result, template[i].rlen)) {
742 pr_err("alg: aead%s: Test %d failed on %s for %s\n",
744 hexdump(q, template[i].rlen);
750 for (i = 0, j = 0; i < tcount; i++) {
751 /* alignment tests are only done with continuous buffers */
752 if (align_offset != 0)
761 memcpy(iv, template[i].iv, iv_len);
763 memset(iv, 0, MAX_IVLEN);
765 crypto_aead_clear_flags(tfm, ~0);
767 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
768 if (template[i].klen > MAX_KEYLEN) {
769 pr_err("alg: aead%s: setkey failed on test %d for %s: key size %d > %d\n",
770 d, j, algo, template[i].klen, MAX_KEYLEN);
774 memcpy(key, template[i].key, template[i].klen);
776 ret = crypto_aead_setkey(tfm, key, template[i].klen);
777 if (template[i].fail == !ret) {
778 pr_err("alg: aead%s: setkey failed on chunk test %d for %s: flags=%x\n",
779 d, j, algo, crypto_aead_get_flags(tfm));
784 authsize = abs(template[i].rlen - template[i].ilen);
787 sg_init_table(sg, template[i].anp + template[i].np);
789 sg_init_table(sgout, template[i].anp + template[i].np);
792 for (k = 0, temp = 0; k < template[i].anp; k++) {
793 if (WARN_ON(offset_in_page(IDX[k]) +
794 template[i].atap[k] > PAGE_SIZE))
797 memcpy(axbuf[IDX[k] >> PAGE_SHIFT] +
798 offset_in_page(IDX[k]),
799 template[i].assoc + temp,
800 template[i].atap[k]),
801 template[i].atap[k]);
803 sg_set_buf(&sgout[k],
804 axbuf[IDX[k] >> PAGE_SHIFT] +
805 offset_in_page(IDX[k]),
806 template[i].atap[k]);
807 temp += template[i].atap[k];
810 for (k = 0, temp = 0; k < template[i].np; k++) {
811 if (WARN_ON(offset_in_page(IDX[k]) +
812 template[i].tap[k] > PAGE_SIZE))
815 q = xbuf[IDX[k] >> PAGE_SHIFT] + offset_in_page(IDX[k]);
816 memcpy(q, template[i].input + temp, template[i].tap[k]);
817 sg_set_buf(&sg[template[i].anp + k],
818 q, template[i].tap[k]);
821 q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
822 offset_in_page(IDX[k]);
824 memset(q, 0, template[i].tap[k]);
826 sg_set_buf(&sgout[template[i].anp + k],
827 q, template[i].tap[k]);
830 n = template[i].tap[k];
831 if (k == template[i].np - 1 && enc)
833 if (offset_in_page(q) + n < PAGE_SIZE)
836 temp += template[i].tap[k];
839 ret = crypto_aead_setauthsize(tfm, authsize);
841 pr_err("alg: aead%s: Failed to set authsize to %u on chunk test %d for %s\n",
842 d, authsize, j, algo);
847 if (WARN_ON(sg[template[i].anp + k - 1].offset +
848 sg[template[i].anp + k - 1].length +
849 authsize > PAGE_SIZE)) {
855 sgout[template[i].anp + k - 1].length +=
857 sg[template[i].anp + k - 1].length += authsize;
860 aead_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
864 aead_request_set_ad(req, template[i].alen);
866 ret = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
870 if (template[i].novrfy) {
871 /* verification was supposed to fail */
872 pr_err("alg: aead%s: %s failed on chunk test %d for %s: ret was 0, expected -EBADMSG\n",
874 /* so really, we got a bad message */
881 wait_for_completion(&result.completion);
882 reinit_completion(&result.completion);
887 if (template[i].novrfy)
888 /* verification failure was expected */
892 pr_err("alg: aead%s: %s failed on chunk test %d for %s: ret=%d\n",
893 d, e, j, algo, -ret);
898 for (k = 0, temp = 0; k < template[i].np; k++) {
900 q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
901 offset_in_page(IDX[k]);
903 q = xbuf[IDX[k] >> PAGE_SHIFT] +
904 offset_in_page(IDX[k]);
906 n = template[i].tap[k];
907 if (k == template[i].np - 1)
908 n += enc ? authsize : -authsize;
910 if (memcmp(q, template[i].result + temp, n)) {
911 pr_err("alg: aead%s: Chunk test %d failed on %s at page %u for %s\n",
918 if (k == template[i].np - 1 && !enc) {
920 memcmp(q, template[i].input +
926 for (n = 0; offset_in_page(q + n) && q[n]; n++)
930 pr_err("alg: aead%s: Result buffer corruption in chunk test %d on %s at page %u for %s: %u bytes:\n",
931 d, j, e, k, algo, n);
936 temp += template[i].tap[k];
943 aead_request_free(req);
947 testmgr_free_buf(xoutbuf);
949 testmgr_free_buf(axbuf);
951 testmgr_free_buf(xbuf);
958 static int test_aead(struct crypto_aead *tfm, int enc,
959 struct aead_testvec *template, unsigned int tcount)
961 unsigned int alignmask;
964 /* test 'dst == src' case */
965 ret = __test_aead(tfm, enc, template, tcount, false, 0);
969 /* test 'dst != src' case */
970 ret = __test_aead(tfm, enc, template, tcount, true, 0);
974 /* test unaligned buffers, check with one byte offset */
975 ret = __test_aead(tfm, enc, template, tcount, true, 1);
979 alignmask = crypto_tfm_alg_alignmask(&tfm->base);
981 /* Check if alignment mask for tfm is correctly set. */
982 ret = __test_aead(tfm, enc, template, tcount, true,
991 static int test_cipher(struct crypto_cipher *tfm, int enc,
992 struct cipher_testvec *template, unsigned int tcount)
994 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
995 unsigned int i, j, k;
999 char *xbuf[XBUFSIZE];
1002 if (testmgr_alloc_buf(xbuf))
1011 for (i = 0; i < tcount; i++) {
1015 if (fips_enabled && template[i].fips_skip)
1021 if (WARN_ON(template[i].ilen > PAGE_SIZE))
1025 memcpy(data, template[i].input, template[i].ilen);
1027 crypto_cipher_clear_flags(tfm, ~0);
1029 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
1031 ret = crypto_cipher_setkey(tfm, template[i].key,
1033 if (template[i].fail == !ret) {
1034 printk(KERN_ERR "alg: cipher: setkey failed "
1035 "on test %d for %s: flags=%x\n", j,
1036 algo, crypto_cipher_get_flags(tfm));
1041 for (k = 0; k < template[i].ilen;
1042 k += crypto_cipher_blocksize(tfm)) {
1044 crypto_cipher_encrypt_one(tfm, data + k,
1047 crypto_cipher_decrypt_one(tfm, data + k,
1052 if (memcmp(q, template[i].result, template[i].rlen)) {
1053 printk(KERN_ERR "alg: cipher: Test %d failed "
1054 "on %s for %s\n", j, e, algo);
1055 hexdump(q, template[i].rlen);
1064 testmgr_free_buf(xbuf);
1069 static int __test_skcipher(struct crypto_skcipher *tfm, int enc,
1070 struct cipher_testvec *template, unsigned int tcount,
1071 const bool diff_dst, const int align_offset)
1074 crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm));
1075 unsigned int i, j, k, n, temp;
1077 struct skcipher_request *req;
1078 struct scatterlist sg[8];
1079 struct scatterlist sgout[8];
1081 struct tcrypt_result result;
1084 char *xbuf[XBUFSIZE];
1085 char *xoutbuf[XBUFSIZE];
1087 unsigned int ivsize = crypto_skcipher_ivsize(tfm);
1089 if (testmgr_alloc_buf(xbuf))
1092 if (diff_dst && testmgr_alloc_buf(xoutbuf))
1105 init_completion(&result.completion);
1107 req = skcipher_request_alloc(tfm, GFP_KERNEL);
1109 pr_err("alg: skcipher%s: Failed to allocate request for %s\n",
1114 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1115 tcrypt_complete, &result);
1118 for (i = 0; i < tcount; i++) {
1119 if (template[i].np && !template[i].also_non_np)
1122 if (fips_enabled && template[i].fips_skip)
1126 memcpy(iv, template[i].iv, ivsize);
1128 memset(iv, 0, MAX_IVLEN);
1132 if (WARN_ON(align_offset + template[i].ilen > PAGE_SIZE))
1136 data += align_offset;
1137 memcpy(data, template[i].input, template[i].ilen);
1139 crypto_skcipher_clear_flags(tfm, ~0);
1141 crypto_skcipher_set_flags(tfm,
1142 CRYPTO_TFM_REQ_WEAK_KEY);
1144 ret = crypto_skcipher_setkey(tfm, template[i].key,
1146 if (template[i].fail == !ret) {
1147 pr_err("alg: skcipher%s: setkey failed on test %d for %s: flags=%x\n",
1148 d, j, algo, crypto_skcipher_get_flags(tfm));
1153 sg_init_one(&sg[0], data, template[i].ilen);
1156 data += align_offset;
1157 sg_init_one(&sgout[0], data, template[i].ilen);
1160 skcipher_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
1161 template[i].ilen, iv);
1162 ret = enc ? crypto_skcipher_encrypt(req) :
1163 crypto_skcipher_decrypt(req);
1170 wait_for_completion(&result.completion);
1171 reinit_completion(&result.completion);
1177 pr_err("alg: skcipher%s: %s failed on test %d for %s: ret=%d\n",
1178 d, e, j, algo, -ret);
1183 if (memcmp(q, template[i].result, template[i].rlen)) {
1184 pr_err("alg: skcipher%s: Test %d failed (invalid result) on %s for %s\n",
1186 hexdump(q, template[i].rlen);
1191 if (template[i].iv_out &&
1192 memcmp(iv, template[i].iv_out,
1193 crypto_skcipher_ivsize(tfm))) {
1194 pr_err("alg: skcipher%s: Test %d failed (invalid output IV) on %s for %s\n",
1196 hexdump(iv, crypto_skcipher_ivsize(tfm));
1203 for (i = 0; i < tcount; i++) {
1204 /* alignment tests are only done with continuous buffers */
1205 if (align_offset != 0)
1208 if (!template[i].np)
1211 if (fips_enabled && template[i].fips_skip)
1215 memcpy(iv, template[i].iv, ivsize);
1217 memset(iv, 0, MAX_IVLEN);
1220 crypto_skcipher_clear_flags(tfm, ~0);
1222 crypto_skcipher_set_flags(tfm,
1223 CRYPTO_TFM_REQ_WEAK_KEY);
1225 ret = crypto_skcipher_setkey(tfm, template[i].key,
1227 if (template[i].fail == !ret) {
1228 pr_err("alg: skcipher%s: setkey failed on chunk test %d for %s: flags=%x\n",
1229 d, j, algo, crypto_skcipher_get_flags(tfm));
1236 sg_init_table(sg, template[i].np);
1238 sg_init_table(sgout, template[i].np);
1239 for (k = 0; k < template[i].np; k++) {
1240 if (WARN_ON(offset_in_page(IDX[k]) +
1241 template[i].tap[k] > PAGE_SIZE))
1244 q = xbuf[IDX[k] >> PAGE_SHIFT] + offset_in_page(IDX[k]);
1246 memcpy(q, template[i].input + temp, template[i].tap[k]);
1248 if (offset_in_page(q) + template[i].tap[k] < PAGE_SIZE)
1249 q[template[i].tap[k]] = 0;
1251 sg_set_buf(&sg[k], q, template[i].tap[k]);
1253 q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
1254 offset_in_page(IDX[k]);
1256 sg_set_buf(&sgout[k], q, template[i].tap[k]);
1258 memset(q, 0, template[i].tap[k]);
1259 if (offset_in_page(q) +
1260 template[i].tap[k] < PAGE_SIZE)
1261 q[template[i].tap[k]] = 0;
1264 temp += template[i].tap[k];
1267 skcipher_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
1268 template[i].ilen, iv);
1270 ret = enc ? crypto_skcipher_encrypt(req) :
1271 crypto_skcipher_decrypt(req);
1278 wait_for_completion(&result.completion);
1279 reinit_completion(&result.completion);
1285 pr_err("alg: skcipher%s: %s failed on chunk test %d for %s: ret=%d\n",
1286 d, e, j, algo, -ret);
1292 for (k = 0; k < template[i].np; k++) {
1294 q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
1295 offset_in_page(IDX[k]);
1297 q = xbuf[IDX[k] >> PAGE_SHIFT] +
1298 offset_in_page(IDX[k]);
1300 if (memcmp(q, template[i].result + temp,
1301 template[i].tap[k])) {
1302 pr_err("alg: skcipher%s: Chunk test %d failed on %s at page %u for %s\n",
1304 hexdump(q, template[i].tap[k]);
1308 q += template[i].tap[k];
1309 for (n = 0; offset_in_page(q + n) && q[n]; n++)
1312 pr_err("alg: skcipher%s: Result buffer corruption in chunk test %d on %s at page %u for %s: %u bytes:\n",
1313 d, j, e, k, algo, n);
1317 temp += template[i].tap[k];
1324 skcipher_request_free(req);
1326 testmgr_free_buf(xoutbuf);
1328 testmgr_free_buf(xbuf);
1333 static int test_skcipher(struct crypto_skcipher *tfm, int enc,
1334 struct cipher_testvec *template, unsigned int tcount)
1336 unsigned int alignmask;
1339 /* test 'dst == src' case */
1340 ret = __test_skcipher(tfm, enc, template, tcount, false, 0);
1344 /* test 'dst != src' case */
1345 ret = __test_skcipher(tfm, enc, template, tcount, true, 0);
1349 /* test unaligned buffers, check with one byte offset */
1350 ret = __test_skcipher(tfm, enc, template, tcount, true, 1);
1354 alignmask = crypto_tfm_alg_alignmask(&tfm->base);
1356 /* Check if alignment mask for tfm is correctly set. */
1357 ret = __test_skcipher(tfm, enc, template, tcount, true,
1366 static int test_comp(struct crypto_comp *tfm, struct comp_testvec *ctemplate,
1367 struct comp_testvec *dtemplate, int ctcount, int dtcount)
1369 const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
1371 char result[COMP_BUF_SIZE];
1374 for (i = 0; i < ctcount; i++) {
1376 unsigned int dlen = COMP_BUF_SIZE;
1378 memset(result, 0, sizeof (result));
1380 ilen = ctemplate[i].inlen;
1381 ret = crypto_comp_compress(tfm, ctemplate[i].input,
1382 ilen, result, &dlen);
1384 printk(KERN_ERR "alg: comp: compression failed "
1385 "on test %d for %s: ret=%d\n", i + 1, algo,
1390 if (dlen != ctemplate[i].outlen) {
1391 printk(KERN_ERR "alg: comp: Compression test %d "
1392 "failed for %s: output len = %d\n", i + 1, algo,
1398 if (memcmp(result, ctemplate[i].output, dlen)) {
1399 printk(KERN_ERR "alg: comp: Compression test %d "
1400 "failed for %s\n", i + 1, algo);
1401 hexdump(result, dlen);
1407 for (i = 0; i < dtcount; i++) {
1409 unsigned int dlen = COMP_BUF_SIZE;
1411 memset(result, 0, sizeof (result));
1413 ilen = dtemplate[i].inlen;
1414 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
1415 ilen, result, &dlen);
1417 printk(KERN_ERR "alg: comp: decompression failed "
1418 "on test %d for %s: ret=%d\n", i + 1, algo,
1423 if (dlen != dtemplate[i].outlen) {
1424 printk(KERN_ERR "alg: comp: Decompression test %d "
1425 "failed for %s: output len = %d\n", i + 1, algo,
1431 if (memcmp(result, dtemplate[i].output, dlen)) {
1432 printk(KERN_ERR "alg: comp: Decompression test %d "
1433 "failed for %s\n", i + 1, algo);
1434 hexdump(result, dlen);
1446 static int test_acomp(struct crypto_acomp *tfm, struct comp_testvec *ctemplate,
1447 struct comp_testvec *dtemplate, int ctcount, int dtcount)
1449 const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
1453 struct scatterlist src, dst;
1454 struct acomp_req *req;
1455 struct tcrypt_result result;
1457 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1461 for (i = 0; i < ctcount; i++) {
1462 unsigned int dlen = COMP_BUF_SIZE;
1463 int ilen = ctemplate[i].inlen;
1465 memset(output, 0, dlen);
1466 init_completion(&result.completion);
1467 sg_init_one(&src, ctemplate[i].input, ilen);
1468 sg_init_one(&dst, output, dlen);
1470 req = acomp_request_alloc(tfm);
1472 pr_err("alg: acomp: request alloc failed for %s\n",
1478 acomp_request_set_params(req, &src, &dst, ilen, dlen);
1479 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1480 tcrypt_complete, &result);
1482 ret = wait_async_op(&result, crypto_acomp_compress(req));
1484 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
1486 acomp_request_free(req);
1490 if (req->dlen != ctemplate[i].outlen) {
1491 pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
1492 i + 1, algo, req->dlen);
1494 acomp_request_free(req);
1498 if (memcmp(output, ctemplate[i].output, req->dlen)) {
1499 pr_err("alg: acomp: Compression test %d failed for %s\n",
1501 hexdump(output, req->dlen);
1503 acomp_request_free(req);
1507 acomp_request_free(req);
1510 for (i = 0; i < dtcount; i++) {
1511 unsigned int dlen = COMP_BUF_SIZE;
1512 int ilen = dtemplate[i].inlen;
1514 memset(output, 0, dlen);
1515 init_completion(&result.completion);
1516 sg_init_one(&src, dtemplate[i].input, ilen);
1517 sg_init_one(&dst, output, dlen);
1519 req = acomp_request_alloc(tfm);
1521 pr_err("alg: acomp: request alloc failed for %s\n",
1527 acomp_request_set_params(req, &src, &dst, ilen, dlen);
1528 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1529 tcrypt_complete, &result);
1531 ret = wait_async_op(&result, crypto_acomp_decompress(req));
1533 pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
1535 acomp_request_free(req);
1539 if (req->dlen != dtemplate[i].outlen) {
1540 pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
1541 i + 1, algo, req->dlen);
1543 acomp_request_free(req);
1547 if (memcmp(output, dtemplate[i].output, req->dlen)) {
1548 pr_err("alg: acomp: Decompression test %d failed for %s\n",
1550 hexdump(output, req->dlen);
1552 acomp_request_free(req);
1556 acomp_request_free(req);
1566 static int test_cprng(struct crypto_rng *tfm, struct cprng_testvec *template,
1567 unsigned int tcount)
1569 const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
1570 int err = 0, i, j, seedsize;
1574 seedsize = crypto_rng_seedsize(tfm);
1576 seed = kmalloc(seedsize, GFP_KERNEL);
1578 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
1583 for (i = 0; i < tcount; i++) {
1584 memset(result, 0, 32);
1586 memcpy(seed, template[i].v, template[i].vlen);
1587 memcpy(seed + template[i].vlen, template[i].key,
1589 memcpy(seed + template[i].vlen + template[i].klen,
1590 template[i].dt, template[i].dtlen);
1592 err = crypto_rng_reset(tfm, seed, seedsize);
1594 printk(KERN_ERR "alg: cprng: Failed to reset rng "
1599 for (j = 0; j < template[i].loops; j++) {
1600 err = crypto_rng_get_bytes(tfm, result,
1603 printk(KERN_ERR "alg: cprng: Failed to obtain "
1604 "the correct amount of random data for "
1605 "%s (requested %d)\n", algo,
1611 err = memcmp(result, template[i].result,
1614 printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
1616 hexdump(result, template[i].rlen);
1627 static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
1630 struct crypto_aead *tfm;
1633 tfm = crypto_alloc_aead(driver, type, mask);
1635 printk(KERN_ERR "alg: aead: Failed to load transform for %s: "
1636 "%ld\n", driver, PTR_ERR(tfm));
1637 return PTR_ERR(tfm);
1640 if (desc->suite.aead.enc.vecs) {
1641 err = test_aead(tfm, ENCRYPT, desc->suite.aead.enc.vecs,
1642 desc->suite.aead.enc.count);
1647 if (!err && desc->suite.aead.dec.vecs)
1648 err = test_aead(tfm, DECRYPT, desc->suite.aead.dec.vecs,
1649 desc->suite.aead.dec.count);
1652 crypto_free_aead(tfm);
1656 static int alg_test_cipher(const struct alg_test_desc *desc,
1657 const char *driver, u32 type, u32 mask)
1659 struct crypto_cipher *tfm;
1662 tfm = crypto_alloc_cipher(driver, type, mask);
1664 printk(KERN_ERR "alg: cipher: Failed to load transform for "
1665 "%s: %ld\n", driver, PTR_ERR(tfm));
1666 return PTR_ERR(tfm);
1669 if (desc->suite.cipher.enc.vecs) {
1670 err = test_cipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
1671 desc->suite.cipher.enc.count);
1676 if (desc->suite.cipher.dec.vecs)
1677 err = test_cipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
1678 desc->suite.cipher.dec.count);
1681 crypto_free_cipher(tfm);
1685 static int alg_test_skcipher(const struct alg_test_desc *desc,
1686 const char *driver, u32 type, u32 mask)
1688 struct crypto_skcipher *tfm;
1691 tfm = crypto_alloc_skcipher(driver, type, mask);
1693 printk(KERN_ERR "alg: skcipher: Failed to load transform for "
1694 "%s: %ld\n", driver, PTR_ERR(tfm));
1695 return PTR_ERR(tfm);
1698 if (desc->suite.cipher.enc.vecs) {
1699 err = test_skcipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
1700 desc->suite.cipher.enc.count);
1705 if (desc->suite.cipher.dec.vecs)
1706 err = test_skcipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
1707 desc->suite.cipher.dec.count);
1710 crypto_free_skcipher(tfm);
1714 static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
1717 struct crypto_comp *comp;
1718 struct crypto_acomp *acomp;
1720 u32 algo_type = type & CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
1722 if (algo_type == CRYPTO_ALG_TYPE_ACOMPRESS) {
1723 acomp = crypto_alloc_acomp(driver, type, mask);
1724 if (IS_ERR(acomp)) {
1725 pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
1726 driver, PTR_ERR(acomp));
1727 return PTR_ERR(acomp);
1729 err = test_acomp(acomp, desc->suite.comp.comp.vecs,
1730 desc->suite.comp.decomp.vecs,
1731 desc->suite.comp.comp.count,
1732 desc->suite.comp.decomp.count);
1733 crypto_free_acomp(acomp);
1735 comp = crypto_alloc_comp(driver, type, mask);
1737 pr_err("alg: comp: Failed to load transform for %s: %ld\n",
1738 driver, PTR_ERR(comp));
1739 return PTR_ERR(comp);
1742 err = test_comp(comp, desc->suite.comp.comp.vecs,
1743 desc->suite.comp.decomp.vecs,
1744 desc->suite.comp.comp.count,
1745 desc->suite.comp.decomp.count);
1747 crypto_free_comp(comp);
1752 static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1755 struct crypto_ahash *tfm;
1758 tfm = crypto_alloc_ahash(driver, type, mask);
1760 printk(KERN_ERR "alg: hash: Failed to load transform for %s: "
1761 "%ld\n", driver, PTR_ERR(tfm));
1762 return PTR_ERR(tfm);
1765 err = test_hash(tfm, desc->suite.hash.vecs,
1766 desc->suite.hash.count, true);
1768 err = test_hash(tfm, desc->suite.hash.vecs,
1769 desc->suite.hash.count, false);
1771 crypto_free_ahash(tfm);
1775 static int alg_test_crc32c(const struct alg_test_desc *desc,
1776 const char *driver, u32 type, u32 mask)
1778 struct crypto_shash *tfm;
1782 err = alg_test_hash(desc, driver, type, mask);
1786 tfm = crypto_alloc_shash(driver, type, mask);
1788 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
1789 "%ld\n", driver, PTR_ERR(tfm));
1795 SHASH_DESC_ON_STACK(shash, tfm);
1796 u32 *ctx = (u32 *)shash_desc_ctx(shash);
1801 *ctx = le32_to_cpu(420553207);
1802 err = crypto_shash_final(shash, (u8 *)&val);
1804 printk(KERN_ERR "alg: crc32c: Operation failed for "
1805 "%s: %d\n", driver, err);
1809 if (val != ~420553207) {
1810 printk(KERN_ERR "alg: crc32c: Test failed for %s: "
1811 "%d\n", driver, val);
1816 crypto_free_shash(tfm);
1822 static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
1825 struct crypto_rng *rng;
1828 rng = crypto_alloc_rng(driver, type, mask);
1830 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
1831 "%ld\n", driver, PTR_ERR(rng));
1832 return PTR_ERR(rng);
1835 err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
1837 crypto_free_rng(rng);
1843 static int drbg_cavs_test(struct drbg_testvec *test, int pr,
1844 const char *driver, u32 type, u32 mask)
1847 struct crypto_rng *drng;
1848 struct drbg_test_data test_data;
1849 struct drbg_string addtl, pers, testentropy;
1850 unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
1855 drng = crypto_alloc_rng(driver, type, mask);
1857 printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
1863 test_data.testentropy = &testentropy;
1864 drbg_string_fill(&testentropy, test->entropy, test->entropylen);
1865 drbg_string_fill(&pers, test->pers, test->perslen);
1866 ret = crypto_drbg_reset_test(drng, &pers, &test_data);
1868 printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
1872 drbg_string_fill(&addtl, test->addtla, test->addtllen);
1874 drbg_string_fill(&testentropy, test->entpra, test->entprlen);
1875 ret = crypto_drbg_get_bytes_addtl_test(drng,
1876 buf, test->expectedlen, &addtl, &test_data);
1878 ret = crypto_drbg_get_bytes_addtl(drng,
1879 buf, test->expectedlen, &addtl);
1882 printk(KERN_ERR "alg: drbg: could not obtain random data for "
1883 "driver %s\n", driver);
1887 drbg_string_fill(&addtl, test->addtlb, test->addtllen);
1889 drbg_string_fill(&testentropy, test->entprb, test->entprlen);
1890 ret = crypto_drbg_get_bytes_addtl_test(drng,
1891 buf, test->expectedlen, &addtl, &test_data);
1893 ret = crypto_drbg_get_bytes_addtl(drng,
1894 buf, test->expectedlen, &addtl);
1897 printk(KERN_ERR "alg: drbg: could not obtain random data for "
1898 "driver %s\n", driver);
1902 ret = memcmp(test->expected, buf, test->expectedlen);
1905 crypto_free_rng(drng);
1911 static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
1917 struct drbg_testvec *template = desc->suite.drbg.vecs;
1918 unsigned int tcount = desc->suite.drbg.count;
1920 if (0 == memcmp(driver, "drbg_pr_", 8))
1923 for (i = 0; i < tcount; i++) {
1924 err = drbg_cavs_test(&template[i], pr, driver, type, mask);
1926 printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
1936 static int do_test_kpp(struct crypto_kpp *tfm, struct kpp_testvec *vec,
1939 struct kpp_request *req;
1940 void *input_buf = NULL;
1941 void *output_buf = NULL;
1942 struct tcrypt_result result;
1943 unsigned int out_len_max;
1945 struct scatterlist src, dst;
1947 req = kpp_request_alloc(tfm, GFP_KERNEL);
1951 init_completion(&result.completion);
1953 err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
1957 out_len_max = crypto_kpp_maxsize(tfm);
1958 output_buf = kzalloc(out_len_max, GFP_KERNEL);
1964 /* Use appropriate parameter as base */
1965 kpp_request_set_input(req, NULL, 0);
1966 sg_init_one(&dst, output_buf, out_len_max);
1967 kpp_request_set_output(req, &dst, out_len_max);
1968 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1969 tcrypt_complete, &result);
1971 /* Compute public key */
1972 err = wait_async_op(&result, crypto_kpp_generate_public_key(req));
1974 pr_err("alg: %s: generate public key test failed. err %d\n",
1978 /* Verify calculated public key */
1979 if (memcmp(vec->expected_a_public, sg_virt(req->dst),
1980 vec->expected_a_public_size)) {
1981 pr_err("alg: %s: generate public key test failed. Invalid output\n",
1987 /* Calculate shared secret key by using counter part (b) public key. */
1988 input_buf = kzalloc(vec->b_public_size, GFP_KERNEL);
1994 memcpy(input_buf, vec->b_public, vec->b_public_size);
1995 sg_init_one(&src, input_buf, vec->b_public_size);
1996 sg_init_one(&dst, output_buf, out_len_max);
1997 kpp_request_set_input(req, &src, vec->b_public_size);
1998 kpp_request_set_output(req, &dst, out_len_max);
1999 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2000 tcrypt_complete, &result);
2001 err = wait_async_op(&result, crypto_kpp_compute_shared_secret(req));
2003 pr_err("alg: %s: compute shard secret test failed. err %d\n",
2008 * verify shared secret from which the user will derive
2009 * secret key by executing whatever hash it has chosen
2011 if (memcmp(vec->expected_ss, sg_virt(req->dst),
2012 vec->expected_ss_size)) {
2013 pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
2023 kpp_request_free(req);
2027 static int test_kpp(struct crypto_kpp *tfm, const char *alg,
2028 struct kpp_testvec *vecs, unsigned int tcount)
2032 for (i = 0; i < tcount; i++) {
2033 ret = do_test_kpp(tfm, vecs++, alg);
2035 pr_err("alg: %s: test failed on vector %d, err=%d\n",
2043 static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
2046 struct crypto_kpp *tfm;
2049 tfm = crypto_alloc_kpp(driver, type, mask);
2051 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
2052 driver, PTR_ERR(tfm));
2053 return PTR_ERR(tfm);
2055 if (desc->suite.kpp.vecs)
2056 err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
2057 desc->suite.kpp.count);
2059 crypto_free_kpp(tfm);
2063 static int test_akcipher_one(struct crypto_akcipher *tfm,
2064 struct akcipher_testvec *vecs)
2066 char *xbuf[XBUFSIZE];
2067 struct akcipher_request *req;
2068 void *outbuf_enc = NULL;
2069 void *outbuf_dec = NULL;
2070 struct tcrypt_result result;
2071 unsigned int out_len_max, out_len = 0;
2073 struct scatterlist src, dst, src_tab[2];
2075 if (testmgr_alloc_buf(xbuf))
2078 req = akcipher_request_alloc(tfm, GFP_KERNEL);
2082 init_completion(&result.completion);
2084 if (vecs->public_key_vec)
2085 err = crypto_akcipher_set_pub_key(tfm, vecs->key,
2088 err = crypto_akcipher_set_priv_key(tfm, vecs->key,
2094 out_len_max = crypto_akcipher_maxsize(tfm);
2095 outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
2099 if (WARN_ON(vecs->m_size > PAGE_SIZE))
2102 memcpy(xbuf[0], vecs->m, vecs->m_size);
2104 sg_init_table(src_tab, 2);
2105 sg_set_buf(&src_tab[0], xbuf[0], 8);
2106 sg_set_buf(&src_tab[1], xbuf[0] + 8, vecs->m_size - 8);
2107 sg_init_one(&dst, outbuf_enc, out_len_max);
2108 akcipher_request_set_crypt(req, src_tab, &dst, vecs->m_size,
2110 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2111 tcrypt_complete, &result);
2113 /* Run RSA encrypt - c = m^e mod n;*/
2114 err = wait_async_op(&result, crypto_akcipher_encrypt(req));
2116 pr_err("alg: akcipher: encrypt test failed. err %d\n", err);
2119 if (req->dst_len != vecs->c_size) {
2120 pr_err("alg: akcipher: encrypt test failed. Invalid output len\n");
2124 /* verify that encrypted message is equal to expected */
2125 if (memcmp(vecs->c, outbuf_enc, vecs->c_size)) {
2126 pr_err("alg: akcipher: encrypt test failed. Invalid output\n");
2127 hexdump(outbuf_enc, vecs->c_size);
2131 /* Don't invoke decrypt for vectors with public key */
2132 if (vecs->public_key_vec) {
2136 outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
2142 if (WARN_ON(vecs->c_size > PAGE_SIZE))
2145 memcpy(xbuf[0], vecs->c, vecs->c_size);
2147 sg_init_one(&src, xbuf[0], vecs->c_size);
2148 sg_init_one(&dst, outbuf_dec, out_len_max);
2149 init_completion(&result.completion);
2150 akcipher_request_set_crypt(req, &src, &dst, vecs->c_size, out_len_max);
2152 /* Run RSA decrypt - m = c^d mod n;*/
2153 err = wait_async_op(&result, crypto_akcipher_decrypt(req));
2155 pr_err("alg: akcipher: decrypt test failed. err %d\n", err);
2158 out_len = req->dst_len;
2159 if (out_len < vecs->m_size) {
2160 pr_err("alg: akcipher: decrypt test failed. "
2161 "Invalid output len %u\n", out_len);
2165 /* verify that decrypted message is equal to the original msg */
2166 if (memchr_inv(outbuf_dec, 0, out_len - vecs->m_size) ||
2167 memcmp(vecs->m, outbuf_dec + out_len - vecs->m_size,
2169 pr_err("alg: akcipher: decrypt test failed. Invalid output\n");
2170 hexdump(outbuf_dec, out_len);
2177 akcipher_request_free(req);
2179 testmgr_free_buf(xbuf);
2183 static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
2184 struct akcipher_testvec *vecs, unsigned int tcount)
2187 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
2190 for (i = 0; i < tcount; i++) {
2191 ret = test_akcipher_one(tfm, vecs++);
2195 pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
2202 static int alg_test_akcipher(const struct alg_test_desc *desc,
2203 const char *driver, u32 type, u32 mask)
2205 struct crypto_akcipher *tfm;
2208 tfm = crypto_alloc_akcipher(driver, type, mask);
2210 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
2211 driver, PTR_ERR(tfm));
2212 return PTR_ERR(tfm);
2214 if (desc->suite.akcipher.vecs)
2215 err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
2216 desc->suite.akcipher.count);
2218 crypto_free_akcipher(tfm);
2222 static int alg_test_null(const struct alg_test_desc *desc,
2223 const char *driver, u32 type, u32 mask)
2228 /* Please keep this list sorted by algorithm name. */
2229 static const struct alg_test_desc alg_test_descs[] = {
2231 .alg = "ansi_cprng",
2232 .test = alg_test_cprng,
2235 .vecs = ansi_cprng_aes_tv_template,
2236 .count = ANSI_CPRNG_AES_TEST_VECTORS
2240 .alg = "authenc(hmac(md5),ecb(cipher_null))",
2241 .test = alg_test_aead,
2245 .vecs = hmac_md5_ecb_cipher_null_enc_tv_template,
2246 .count = HMAC_MD5_ECB_CIPHER_NULL_ENC_TEST_VECTORS
2249 .vecs = hmac_md5_ecb_cipher_null_dec_tv_template,
2250 .count = HMAC_MD5_ECB_CIPHER_NULL_DEC_TEST_VECTORS
2255 .alg = "authenc(hmac(sha1),cbc(aes))",
2256 .test = alg_test_aead,
2261 hmac_sha1_aes_cbc_enc_tv_temp,
2263 HMAC_SHA1_AES_CBC_ENC_TEST_VEC
2268 .alg = "authenc(hmac(sha1),cbc(des))",
2269 .test = alg_test_aead,
2274 hmac_sha1_des_cbc_enc_tv_temp,
2276 HMAC_SHA1_DES_CBC_ENC_TEST_VEC
2281 .alg = "authenc(hmac(sha1),cbc(des3_ede))",
2282 .test = alg_test_aead,
2288 hmac_sha1_des3_ede_cbc_enc_tv_temp,
2290 HMAC_SHA1_DES3_EDE_CBC_ENC_TEST_VEC
2295 .alg = "authenc(hmac(sha1),ctr(aes))",
2296 .test = alg_test_null,
2299 .alg = "authenc(hmac(sha1),ecb(cipher_null))",
2300 .test = alg_test_aead,
2305 hmac_sha1_ecb_cipher_null_enc_tv_temp,
2307 HMAC_SHA1_ECB_CIPHER_NULL_ENC_TEST_VEC
2311 hmac_sha1_ecb_cipher_null_dec_tv_temp,
2313 HMAC_SHA1_ECB_CIPHER_NULL_DEC_TEST_VEC
2318 .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
2319 .test = alg_test_null,
2322 .alg = "authenc(hmac(sha224),cbc(des))",
2323 .test = alg_test_aead,
2328 hmac_sha224_des_cbc_enc_tv_temp,
2330 HMAC_SHA224_DES_CBC_ENC_TEST_VEC
2335 .alg = "authenc(hmac(sha224),cbc(des3_ede))",
2336 .test = alg_test_aead,
2342 hmac_sha224_des3_ede_cbc_enc_tv_temp,
2344 HMAC_SHA224_DES3_EDE_CBC_ENC_TEST_VEC
2349 .alg = "authenc(hmac(sha256),cbc(aes))",
2350 .test = alg_test_aead,
2356 hmac_sha256_aes_cbc_enc_tv_temp,
2358 HMAC_SHA256_AES_CBC_ENC_TEST_VEC
2363 .alg = "authenc(hmac(sha256),cbc(des))",
2364 .test = alg_test_aead,
2369 hmac_sha256_des_cbc_enc_tv_temp,
2371 HMAC_SHA256_DES_CBC_ENC_TEST_VEC
2376 .alg = "authenc(hmac(sha256),cbc(des3_ede))",
2377 .test = alg_test_aead,
2383 hmac_sha256_des3_ede_cbc_enc_tv_temp,
2385 HMAC_SHA256_DES3_EDE_CBC_ENC_TEST_VEC
2390 .alg = "authenc(hmac(sha256),ctr(aes))",
2391 .test = alg_test_null,
2394 .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
2395 .test = alg_test_null,
2398 .alg = "authenc(hmac(sha384),cbc(des))",
2399 .test = alg_test_aead,
2404 hmac_sha384_des_cbc_enc_tv_temp,
2406 HMAC_SHA384_DES_CBC_ENC_TEST_VEC
2411 .alg = "authenc(hmac(sha384),cbc(des3_ede))",
2412 .test = alg_test_aead,
2418 hmac_sha384_des3_ede_cbc_enc_tv_temp,
2420 HMAC_SHA384_DES3_EDE_CBC_ENC_TEST_VEC
2425 .alg = "authenc(hmac(sha384),ctr(aes))",
2426 .test = alg_test_null,
2429 .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
2430 .test = alg_test_null,
2433 .alg = "authenc(hmac(sha512),cbc(aes))",
2435 .test = alg_test_aead,
2440 hmac_sha512_aes_cbc_enc_tv_temp,
2442 HMAC_SHA512_AES_CBC_ENC_TEST_VEC
2447 .alg = "authenc(hmac(sha512),cbc(des))",
2448 .test = alg_test_aead,
2453 hmac_sha512_des_cbc_enc_tv_temp,
2455 HMAC_SHA512_DES_CBC_ENC_TEST_VEC
2460 .alg = "authenc(hmac(sha512),cbc(des3_ede))",
2461 .test = alg_test_aead,
2467 hmac_sha512_des3_ede_cbc_enc_tv_temp,
2469 HMAC_SHA512_DES3_EDE_CBC_ENC_TEST_VEC
2474 .alg = "authenc(hmac(sha512),ctr(aes))",
2475 .test = alg_test_null,
2478 .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
2479 .test = alg_test_null,
2483 .test = alg_test_skcipher,
2488 .vecs = aes_cbc_enc_tv_template,
2489 .count = AES_CBC_ENC_TEST_VECTORS
2492 .vecs = aes_cbc_dec_tv_template,
2493 .count = AES_CBC_DEC_TEST_VECTORS
2498 .alg = "cbc(anubis)",
2499 .test = alg_test_skcipher,
2503 .vecs = anubis_cbc_enc_tv_template,
2504 .count = ANUBIS_CBC_ENC_TEST_VECTORS
2507 .vecs = anubis_cbc_dec_tv_template,
2508 .count = ANUBIS_CBC_DEC_TEST_VECTORS
2513 .alg = "cbc(blowfish)",
2514 .test = alg_test_skcipher,
2518 .vecs = bf_cbc_enc_tv_template,
2519 .count = BF_CBC_ENC_TEST_VECTORS
2522 .vecs = bf_cbc_dec_tv_template,
2523 .count = BF_CBC_DEC_TEST_VECTORS
2528 .alg = "cbc(camellia)",
2529 .test = alg_test_skcipher,
2533 .vecs = camellia_cbc_enc_tv_template,
2534 .count = CAMELLIA_CBC_ENC_TEST_VECTORS
2537 .vecs = camellia_cbc_dec_tv_template,
2538 .count = CAMELLIA_CBC_DEC_TEST_VECTORS
2543 .alg = "cbc(cast5)",
2544 .test = alg_test_skcipher,
2548 .vecs = cast5_cbc_enc_tv_template,
2549 .count = CAST5_CBC_ENC_TEST_VECTORS
2552 .vecs = cast5_cbc_dec_tv_template,
2553 .count = CAST5_CBC_DEC_TEST_VECTORS
2558 .alg = "cbc(cast6)",
2559 .test = alg_test_skcipher,
2563 .vecs = cast6_cbc_enc_tv_template,
2564 .count = CAST6_CBC_ENC_TEST_VECTORS
2567 .vecs = cast6_cbc_dec_tv_template,
2568 .count = CAST6_CBC_DEC_TEST_VECTORS
2574 .test = alg_test_skcipher,
2578 .vecs = des_cbc_enc_tv_template,
2579 .count = DES_CBC_ENC_TEST_VECTORS
2582 .vecs = des_cbc_dec_tv_template,
2583 .count = DES_CBC_DEC_TEST_VECTORS
2588 .alg = "cbc(des3_ede)",
2589 .test = alg_test_skcipher,
2594 .vecs = des3_ede_cbc_enc_tv_template,
2595 .count = DES3_EDE_CBC_ENC_TEST_VECTORS
2598 .vecs = des3_ede_cbc_dec_tv_template,
2599 .count = DES3_EDE_CBC_DEC_TEST_VECTORS
2604 .alg = "cbc(serpent)",
2605 .test = alg_test_skcipher,
2609 .vecs = serpent_cbc_enc_tv_template,
2610 .count = SERPENT_CBC_ENC_TEST_VECTORS
2613 .vecs = serpent_cbc_dec_tv_template,
2614 .count = SERPENT_CBC_DEC_TEST_VECTORS
2619 .alg = "cbc(twofish)",
2620 .test = alg_test_skcipher,
2624 .vecs = tf_cbc_enc_tv_template,
2625 .count = TF_CBC_ENC_TEST_VECTORS
2628 .vecs = tf_cbc_dec_tv_template,
2629 .count = TF_CBC_DEC_TEST_VECTORS
2635 .test = alg_test_aead,
2640 .vecs = aes_ccm_enc_tv_template,
2641 .count = AES_CCM_ENC_TEST_VECTORS
2644 .vecs = aes_ccm_dec_tv_template,
2645 .count = AES_CCM_DEC_TEST_VECTORS
2651 .test = alg_test_skcipher,
2655 .vecs = chacha20_enc_tv_template,
2656 .count = CHACHA20_ENC_TEST_VECTORS
2659 .vecs = chacha20_enc_tv_template,
2660 .count = CHACHA20_ENC_TEST_VECTORS
2667 .test = alg_test_hash,
2670 .vecs = aes_cmac128_tv_template,
2671 .count = CMAC_AES_TEST_VECTORS
2675 .alg = "cmac(des3_ede)",
2677 .test = alg_test_hash,
2680 .vecs = des3_ede_cmac64_tv_template,
2681 .count = CMAC_DES3_EDE_TEST_VECTORS
2685 .alg = "compress_null",
2686 .test = alg_test_null,
2689 .test = alg_test_hash,
2692 .vecs = crc32_tv_template,
2693 .count = CRC32_TEST_VECTORS
2698 .test = alg_test_crc32c,
2702 .vecs = crc32c_tv_template,
2703 .count = CRC32C_TEST_VECTORS
2708 .test = alg_test_hash,
2712 .vecs = crct10dif_tv_template,
2713 .count = CRCT10DIF_TEST_VECTORS
2718 .test = alg_test_skcipher,
2723 .vecs = aes_ctr_enc_tv_template,
2724 .count = AES_CTR_ENC_TEST_VECTORS
2727 .vecs = aes_ctr_dec_tv_template,
2728 .count = AES_CTR_DEC_TEST_VECTORS
2733 .alg = "ctr(blowfish)",
2734 .test = alg_test_skcipher,
2738 .vecs = bf_ctr_enc_tv_template,
2739 .count = BF_CTR_ENC_TEST_VECTORS
2742 .vecs = bf_ctr_dec_tv_template,
2743 .count = BF_CTR_DEC_TEST_VECTORS
2748 .alg = "ctr(camellia)",
2749 .test = alg_test_skcipher,
2753 .vecs = camellia_ctr_enc_tv_template,
2754 .count = CAMELLIA_CTR_ENC_TEST_VECTORS
2757 .vecs = camellia_ctr_dec_tv_template,
2758 .count = CAMELLIA_CTR_DEC_TEST_VECTORS
2763 .alg = "ctr(cast5)",
2764 .test = alg_test_skcipher,
2768 .vecs = cast5_ctr_enc_tv_template,
2769 .count = CAST5_CTR_ENC_TEST_VECTORS
2772 .vecs = cast5_ctr_dec_tv_template,
2773 .count = CAST5_CTR_DEC_TEST_VECTORS
2778 .alg = "ctr(cast6)",
2779 .test = alg_test_skcipher,
2783 .vecs = cast6_ctr_enc_tv_template,
2784 .count = CAST6_CTR_ENC_TEST_VECTORS
2787 .vecs = cast6_ctr_dec_tv_template,
2788 .count = CAST6_CTR_DEC_TEST_VECTORS
2794 .test = alg_test_skcipher,
2798 .vecs = des_ctr_enc_tv_template,
2799 .count = DES_CTR_ENC_TEST_VECTORS
2802 .vecs = des_ctr_dec_tv_template,
2803 .count = DES_CTR_DEC_TEST_VECTORS
2808 .alg = "ctr(des3_ede)",
2809 .test = alg_test_skcipher,
2813 .vecs = des3_ede_ctr_enc_tv_template,
2814 .count = DES3_EDE_CTR_ENC_TEST_VECTORS
2817 .vecs = des3_ede_ctr_dec_tv_template,
2818 .count = DES3_EDE_CTR_DEC_TEST_VECTORS
2823 .alg = "ctr(serpent)",
2824 .test = alg_test_skcipher,
2828 .vecs = serpent_ctr_enc_tv_template,
2829 .count = SERPENT_CTR_ENC_TEST_VECTORS
2832 .vecs = serpent_ctr_dec_tv_template,
2833 .count = SERPENT_CTR_DEC_TEST_VECTORS
2838 .alg = "ctr(twofish)",
2839 .test = alg_test_skcipher,
2843 .vecs = tf_ctr_enc_tv_template,
2844 .count = TF_CTR_ENC_TEST_VECTORS
2847 .vecs = tf_ctr_dec_tv_template,
2848 .count = TF_CTR_DEC_TEST_VECTORS
2853 .alg = "cts(cbc(aes))",
2854 .test = alg_test_skcipher,
2858 .vecs = cts_mode_enc_tv_template,
2859 .count = CTS_MODE_ENC_TEST_VECTORS
2862 .vecs = cts_mode_dec_tv_template,
2863 .count = CTS_MODE_DEC_TEST_VECTORS
2869 .test = alg_test_comp,
2874 .vecs = deflate_comp_tv_template,
2875 .count = DEFLATE_COMP_TEST_VECTORS
2878 .vecs = deflate_decomp_tv_template,
2879 .count = DEFLATE_DECOMP_TEST_VECTORS
2885 .test = alg_test_kpp,
2889 .vecs = dh_tv_template,
2890 .count = DH_TEST_VECTORS
2894 .alg = "digest_null",
2895 .test = alg_test_null,
2897 .alg = "drbg_nopr_ctr_aes128",
2898 .test = alg_test_drbg,
2902 .vecs = drbg_nopr_ctr_aes128_tv_template,
2903 .count = ARRAY_SIZE(drbg_nopr_ctr_aes128_tv_template)
2907 .alg = "drbg_nopr_ctr_aes192",
2908 .test = alg_test_drbg,
2912 .vecs = drbg_nopr_ctr_aes192_tv_template,
2913 .count = ARRAY_SIZE(drbg_nopr_ctr_aes192_tv_template)
2917 .alg = "drbg_nopr_ctr_aes256",
2918 .test = alg_test_drbg,
2922 .vecs = drbg_nopr_ctr_aes256_tv_template,
2923 .count = ARRAY_SIZE(drbg_nopr_ctr_aes256_tv_template)
2928 * There is no need to specifically test the DRBG with every
2929 * backend cipher -- covered by drbg_nopr_hmac_sha256 test
2931 .alg = "drbg_nopr_hmac_sha1",
2933 .test = alg_test_null,
2935 .alg = "drbg_nopr_hmac_sha256",
2936 .test = alg_test_drbg,
2940 .vecs = drbg_nopr_hmac_sha256_tv_template,
2942 ARRAY_SIZE(drbg_nopr_hmac_sha256_tv_template)
2946 /* covered by drbg_nopr_hmac_sha256 test */
2947 .alg = "drbg_nopr_hmac_sha384",
2949 .test = alg_test_null,
2951 .alg = "drbg_nopr_hmac_sha512",
2952 .test = alg_test_null,
2955 .alg = "drbg_nopr_sha1",
2957 .test = alg_test_null,
2959 .alg = "drbg_nopr_sha256",
2960 .test = alg_test_drbg,
2964 .vecs = drbg_nopr_sha256_tv_template,
2965 .count = ARRAY_SIZE(drbg_nopr_sha256_tv_template)
2969 /* covered by drbg_nopr_sha256 test */
2970 .alg = "drbg_nopr_sha384",
2972 .test = alg_test_null,
2974 .alg = "drbg_nopr_sha512",
2976 .test = alg_test_null,
2978 .alg = "drbg_pr_ctr_aes128",
2979 .test = alg_test_drbg,
2983 .vecs = drbg_pr_ctr_aes128_tv_template,
2984 .count = ARRAY_SIZE(drbg_pr_ctr_aes128_tv_template)
2988 /* covered by drbg_pr_ctr_aes128 test */
2989 .alg = "drbg_pr_ctr_aes192",
2991 .test = alg_test_null,
2993 .alg = "drbg_pr_ctr_aes256",
2995 .test = alg_test_null,
2997 .alg = "drbg_pr_hmac_sha1",
2999 .test = alg_test_null,
3001 .alg = "drbg_pr_hmac_sha256",
3002 .test = alg_test_drbg,
3006 .vecs = drbg_pr_hmac_sha256_tv_template,
3007 .count = ARRAY_SIZE(drbg_pr_hmac_sha256_tv_template)
3011 /* covered by drbg_pr_hmac_sha256 test */
3012 .alg = "drbg_pr_hmac_sha384",
3014 .test = alg_test_null,
3016 .alg = "drbg_pr_hmac_sha512",
3017 .test = alg_test_null,
3020 .alg = "drbg_pr_sha1",
3022 .test = alg_test_null,
3024 .alg = "drbg_pr_sha256",
3025 .test = alg_test_drbg,
3029 .vecs = drbg_pr_sha256_tv_template,
3030 .count = ARRAY_SIZE(drbg_pr_sha256_tv_template)
3034 /* covered by drbg_pr_sha256 test */
3035 .alg = "drbg_pr_sha384",
3037 .test = alg_test_null,
3039 .alg = "drbg_pr_sha512",
3041 .test = alg_test_null,
3044 .test = alg_test_skcipher,
3049 .vecs = aes_enc_tv_template,
3050 .count = AES_ENC_TEST_VECTORS
3053 .vecs = aes_dec_tv_template,
3054 .count = AES_DEC_TEST_VECTORS
3059 .alg = "ecb(anubis)",
3060 .test = alg_test_skcipher,
3064 .vecs = anubis_enc_tv_template,
3065 .count = ANUBIS_ENC_TEST_VECTORS
3068 .vecs = anubis_dec_tv_template,
3069 .count = ANUBIS_DEC_TEST_VECTORS
3075 .test = alg_test_skcipher,
3079 .vecs = arc4_enc_tv_template,
3080 .count = ARC4_ENC_TEST_VECTORS
3083 .vecs = arc4_dec_tv_template,
3084 .count = ARC4_DEC_TEST_VECTORS
3089 .alg = "ecb(blowfish)",
3090 .test = alg_test_skcipher,
3094 .vecs = bf_enc_tv_template,
3095 .count = BF_ENC_TEST_VECTORS
3098 .vecs = bf_dec_tv_template,
3099 .count = BF_DEC_TEST_VECTORS
3104 .alg = "ecb(camellia)",
3105 .test = alg_test_skcipher,
3109 .vecs = camellia_enc_tv_template,
3110 .count = CAMELLIA_ENC_TEST_VECTORS
3113 .vecs = camellia_dec_tv_template,
3114 .count = CAMELLIA_DEC_TEST_VECTORS
3119 .alg = "ecb(cast5)",
3120 .test = alg_test_skcipher,
3124 .vecs = cast5_enc_tv_template,
3125 .count = CAST5_ENC_TEST_VECTORS
3128 .vecs = cast5_dec_tv_template,
3129 .count = CAST5_DEC_TEST_VECTORS
3134 .alg = "ecb(cast6)",
3135 .test = alg_test_skcipher,
3139 .vecs = cast6_enc_tv_template,
3140 .count = CAST6_ENC_TEST_VECTORS
3143 .vecs = cast6_dec_tv_template,
3144 .count = CAST6_DEC_TEST_VECTORS
3149 .alg = "ecb(cipher_null)",
3150 .test = alg_test_null,
3153 .test = alg_test_skcipher,
3157 .vecs = des_enc_tv_template,
3158 .count = DES_ENC_TEST_VECTORS
3161 .vecs = des_dec_tv_template,
3162 .count = DES_DEC_TEST_VECTORS
3167 .alg = "ecb(des3_ede)",
3168 .test = alg_test_skcipher,
3173 .vecs = des3_ede_enc_tv_template,
3174 .count = DES3_EDE_ENC_TEST_VECTORS
3177 .vecs = des3_ede_dec_tv_template,
3178 .count = DES3_EDE_DEC_TEST_VECTORS
3183 .alg = "ecb(fcrypt)",
3184 .test = alg_test_skcipher,
3188 .vecs = fcrypt_pcbc_enc_tv_template,
3192 .vecs = fcrypt_pcbc_dec_tv_template,
3198 .alg = "ecb(khazad)",
3199 .test = alg_test_skcipher,
3203 .vecs = khazad_enc_tv_template,
3204 .count = KHAZAD_ENC_TEST_VECTORS
3207 .vecs = khazad_dec_tv_template,
3208 .count = KHAZAD_DEC_TEST_VECTORS
3214 .test = alg_test_skcipher,
3218 .vecs = seed_enc_tv_template,
3219 .count = SEED_ENC_TEST_VECTORS
3222 .vecs = seed_dec_tv_template,
3223 .count = SEED_DEC_TEST_VECTORS
3228 .alg = "ecb(serpent)",
3229 .test = alg_test_skcipher,
3233 .vecs = serpent_enc_tv_template,
3234 .count = SERPENT_ENC_TEST_VECTORS
3237 .vecs = serpent_dec_tv_template,
3238 .count = SERPENT_DEC_TEST_VECTORS
3244 .test = alg_test_skcipher,
3248 .vecs = tea_enc_tv_template,
3249 .count = TEA_ENC_TEST_VECTORS
3252 .vecs = tea_dec_tv_template,
3253 .count = TEA_DEC_TEST_VECTORS
3258 .alg = "ecb(tnepres)",
3259 .test = alg_test_skcipher,
3263 .vecs = tnepres_enc_tv_template,
3264 .count = TNEPRES_ENC_TEST_VECTORS
3267 .vecs = tnepres_dec_tv_template,
3268 .count = TNEPRES_DEC_TEST_VECTORS
3273 .alg = "ecb(twofish)",
3274 .test = alg_test_skcipher,
3278 .vecs = tf_enc_tv_template,
3279 .count = TF_ENC_TEST_VECTORS
3282 .vecs = tf_dec_tv_template,
3283 .count = TF_DEC_TEST_VECTORS
3289 .test = alg_test_skcipher,
3293 .vecs = xeta_enc_tv_template,
3294 .count = XETA_ENC_TEST_VECTORS
3297 .vecs = xeta_dec_tv_template,
3298 .count = XETA_DEC_TEST_VECTORS
3304 .test = alg_test_skcipher,
3308 .vecs = xtea_enc_tv_template,
3309 .count = XTEA_ENC_TEST_VECTORS
3312 .vecs = xtea_dec_tv_template,
3313 .count = XTEA_DEC_TEST_VECTORS
3319 .test = alg_test_kpp,
3323 .vecs = ecdh_tv_template,
3324 .count = ECDH_TEST_VECTORS
3329 .test = alg_test_aead,
3334 .vecs = aes_gcm_enc_tv_template,
3335 .count = AES_GCM_ENC_TEST_VECTORS
3338 .vecs = aes_gcm_dec_tv_template,
3339 .count = AES_GCM_DEC_TEST_VECTORS
3345 .test = alg_test_hash,
3349 .vecs = ghash_tv_template,
3350 .count = GHASH_TEST_VECTORS
3354 .alg = "hmac(crc32)",
3355 .test = alg_test_hash,
3358 .vecs = bfin_crc_tv_template,
3359 .count = BFIN_CRC_TEST_VECTORS
3364 .test = alg_test_hash,
3367 .vecs = hmac_md5_tv_template,
3368 .count = HMAC_MD5_TEST_VECTORS
3372 .alg = "hmac(rmd128)",
3373 .test = alg_test_hash,
3376 .vecs = hmac_rmd128_tv_template,
3377 .count = HMAC_RMD128_TEST_VECTORS
3381 .alg = "hmac(rmd160)",
3382 .test = alg_test_hash,
3385 .vecs = hmac_rmd160_tv_template,
3386 .count = HMAC_RMD160_TEST_VECTORS
3390 .alg = "hmac(sha1)",
3391 .test = alg_test_hash,
3395 .vecs = hmac_sha1_tv_template,
3396 .count = HMAC_SHA1_TEST_VECTORS
3400 .alg = "hmac(sha224)",
3401 .test = alg_test_hash,
3405 .vecs = hmac_sha224_tv_template,
3406 .count = HMAC_SHA224_TEST_VECTORS
3410 .alg = "hmac(sha256)",
3411 .test = alg_test_hash,
3415 .vecs = hmac_sha256_tv_template,
3416 .count = HMAC_SHA256_TEST_VECTORS
3420 .alg = "hmac(sha3-224)",
3421 .test = alg_test_hash,
3425 .vecs = hmac_sha3_224_tv_template,
3426 .count = HMAC_SHA3_224_TEST_VECTORS
3430 .alg = "hmac(sha3-256)",
3431 .test = alg_test_hash,
3435 .vecs = hmac_sha3_256_tv_template,
3436 .count = HMAC_SHA3_256_TEST_VECTORS
3440 .alg = "hmac(sha3-384)",
3441 .test = alg_test_hash,
3445 .vecs = hmac_sha3_384_tv_template,
3446 .count = HMAC_SHA3_384_TEST_VECTORS
3450 .alg = "hmac(sha3-512)",
3451 .test = alg_test_hash,
3455 .vecs = hmac_sha3_512_tv_template,
3456 .count = HMAC_SHA3_512_TEST_VECTORS
3460 .alg = "hmac(sha384)",
3461 .test = alg_test_hash,
3465 .vecs = hmac_sha384_tv_template,
3466 .count = HMAC_SHA384_TEST_VECTORS
3470 .alg = "hmac(sha512)",
3471 .test = alg_test_hash,
3475 .vecs = hmac_sha512_tv_template,
3476 .count = HMAC_SHA512_TEST_VECTORS
3480 .alg = "jitterentropy_rng",
3482 .test = alg_test_null,
3485 .test = alg_test_skcipher,
3490 .vecs = aes_kw_enc_tv_template,
3491 .count = ARRAY_SIZE(aes_kw_enc_tv_template)
3494 .vecs = aes_kw_dec_tv_template,
3495 .count = ARRAY_SIZE(aes_kw_dec_tv_template)
3501 .test = alg_test_skcipher,
3505 .vecs = aes_lrw_enc_tv_template,
3506 .count = AES_LRW_ENC_TEST_VECTORS
3509 .vecs = aes_lrw_dec_tv_template,
3510 .count = AES_LRW_DEC_TEST_VECTORS
3515 .alg = "lrw(camellia)",
3516 .test = alg_test_skcipher,
3520 .vecs = camellia_lrw_enc_tv_template,
3521 .count = CAMELLIA_LRW_ENC_TEST_VECTORS
3524 .vecs = camellia_lrw_dec_tv_template,
3525 .count = CAMELLIA_LRW_DEC_TEST_VECTORS
3530 .alg = "lrw(cast6)",
3531 .test = alg_test_skcipher,
3535 .vecs = cast6_lrw_enc_tv_template,
3536 .count = CAST6_LRW_ENC_TEST_VECTORS
3539 .vecs = cast6_lrw_dec_tv_template,
3540 .count = CAST6_LRW_DEC_TEST_VECTORS
3545 .alg = "lrw(serpent)",
3546 .test = alg_test_skcipher,
3550 .vecs = serpent_lrw_enc_tv_template,
3551 .count = SERPENT_LRW_ENC_TEST_VECTORS
3554 .vecs = serpent_lrw_dec_tv_template,
3555 .count = SERPENT_LRW_DEC_TEST_VECTORS
3560 .alg = "lrw(twofish)",
3561 .test = alg_test_skcipher,
3565 .vecs = tf_lrw_enc_tv_template,
3566 .count = TF_LRW_ENC_TEST_VECTORS
3569 .vecs = tf_lrw_dec_tv_template,
3570 .count = TF_LRW_DEC_TEST_VECTORS
3576 .test = alg_test_comp,
3581 .vecs = lz4_comp_tv_template,
3582 .count = LZ4_COMP_TEST_VECTORS
3585 .vecs = lz4_decomp_tv_template,
3586 .count = LZ4_DECOMP_TEST_VECTORS
3592 .test = alg_test_comp,
3597 .vecs = lz4hc_comp_tv_template,
3598 .count = LZ4HC_COMP_TEST_VECTORS
3601 .vecs = lz4hc_decomp_tv_template,
3602 .count = LZ4HC_DECOMP_TEST_VECTORS
3608 .test = alg_test_comp,
3613 .vecs = lzo_comp_tv_template,
3614 .count = LZO_COMP_TEST_VECTORS
3617 .vecs = lzo_decomp_tv_template,
3618 .count = LZO_DECOMP_TEST_VECTORS
3624 .test = alg_test_hash,
3627 .vecs = md4_tv_template,
3628 .count = MD4_TEST_VECTORS
3633 .test = alg_test_hash,
3636 .vecs = md5_tv_template,
3637 .count = MD5_TEST_VECTORS
3641 .alg = "michael_mic",
3642 .test = alg_test_hash,
3645 .vecs = michael_mic_tv_template,
3646 .count = MICHAEL_MIC_TEST_VECTORS
3651 .test = alg_test_skcipher,
3656 .vecs = aes_ofb_enc_tv_template,
3657 .count = AES_OFB_ENC_TEST_VECTORS
3660 .vecs = aes_ofb_dec_tv_template,
3661 .count = AES_OFB_DEC_TEST_VECTORS
3666 .alg = "pcbc(fcrypt)",
3667 .test = alg_test_skcipher,
3671 .vecs = fcrypt_pcbc_enc_tv_template,
3672 .count = FCRYPT_ENC_TEST_VECTORS
3675 .vecs = fcrypt_pcbc_dec_tv_template,
3676 .count = FCRYPT_DEC_TEST_VECTORS
3682 .test = alg_test_hash,
3685 .vecs = poly1305_tv_template,
3686 .count = POLY1305_TEST_VECTORS
3690 .alg = "rfc3686(ctr(aes))",
3691 .test = alg_test_skcipher,
3696 .vecs = aes_ctr_rfc3686_enc_tv_template,
3697 .count = AES_CTR_3686_ENC_TEST_VECTORS
3700 .vecs = aes_ctr_rfc3686_dec_tv_template,
3701 .count = AES_CTR_3686_DEC_TEST_VECTORS
3706 .alg = "rfc4106(gcm(aes))",
3707 .test = alg_test_aead,
3712 .vecs = aes_gcm_rfc4106_enc_tv_template,
3713 .count = AES_GCM_4106_ENC_TEST_VECTORS
3716 .vecs = aes_gcm_rfc4106_dec_tv_template,
3717 .count = AES_GCM_4106_DEC_TEST_VECTORS
3722 .alg = "rfc4309(ccm(aes))",
3723 .test = alg_test_aead,
3728 .vecs = aes_ccm_rfc4309_enc_tv_template,
3729 .count = AES_CCM_4309_ENC_TEST_VECTORS
3732 .vecs = aes_ccm_rfc4309_dec_tv_template,
3733 .count = AES_CCM_4309_DEC_TEST_VECTORS
3738 .alg = "rfc4543(gcm(aes))",
3739 .test = alg_test_aead,
3743 .vecs = aes_gcm_rfc4543_enc_tv_template,
3744 .count = AES_GCM_4543_ENC_TEST_VECTORS
3747 .vecs = aes_gcm_rfc4543_dec_tv_template,
3748 .count = AES_GCM_4543_DEC_TEST_VECTORS
3753 .alg = "rfc7539(chacha20,poly1305)",
3754 .test = alg_test_aead,
3758 .vecs = rfc7539_enc_tv_template,
3759 .count = RFC7539_ENC_TEST_VECTORS
3762 .vecs = rfc7539_dec_tv_template,
3763 .count = RFC7539_DEC_TEST_VECTORS
3768 .alg = "rfc7539esp(chacha20,poly1305)",
3769 .test = alg_test_aead,
3773 .vecs = rfc7539esp_enc_tv_template,
3774 .count = RFC7539ESP_ENC_TEST_VECTORS
3777 .vecs = rfc7539esp_dec_tv_template,
3778 .count = RFC7539ESP_DEC_TEST_VECTORS
3784 .test = alg_test_hash,
3787 .vecs = rmd128_tv_template,
3788 .count = RMD128_TEST_VECTORS
3793 .test = alg_test_hash,
3796 .vecs = rmd160_tv_template,
3797 .count = RMD160_TEST_VECTORS
3802 .test = alg_test_hash,
3805 .vecs = rmd256_tv_template,
3806 .count = RMD256_TEST_VECTORS
3811 .test = alg_test_hash,
3814 .vecs = rmd320_tv_template,
3815 .count = RMD320_TEST_VECTORS
3820 .test = alg_test_akcipher,
3824 .vecs = rsa_tv_template,
3825 .count = RSA_TEST_VECTORS
3830 .test = alg_test_skcipher,
3834 .vecs = salsa20_stream_enc_tv_template,
3835 .count = SALSA20_STREAM_ENC_TEST_VECTORS
3841 .test = alg_test_hash,
3845 .vecs = sha1_tv_template,
3846 .count = SHA1_TEST_VECTORS
3851 .test = alg_test_hash,
3855 .vecs = sha224_tv_template,
3856 .count = SHA224_TEST_VECTORS
3861 .test = alg_test_hash,
3865 .vecs = sha256_tv_template,
3866 .count = SHA256_TEST_VECTORS
3871 .test = alg_test_hash,
3875 .vecs = sha3_224_tv_template,
3876 .count = SHA3_224_TEST_VECTORS
3881 .test = alg_test_hash,
3885 .vecs = sha3_256_tv_template,
3886 .count = SHA3_256_TEST_VECTORS
3891 .test = alg_test_hash,
3895 .vecs = sha3_384_tv_template,
3896 .count = SHA3_384_TEST_VECTORS
3901 .test = alg_test_hash,
3905 .vecs = sha3_512_tv_template,
3906 .count = SHA3_512_TEST_VECTORS
3911 .test = alg_test_hash,
3915 .vecs = sha384_tv_template,
3916 .count = SHA384_TEST_VECTORS
3921 .test = alg_test_hash,
3925 .vecs = sha512_tv_template,
3926 .count = SHA512_TEST_VECTORS
3931 .test = alg_test_hash,
3934 .vecs = tgr128_tv_template,
3935 .count = TGR128_TEST_VECTORS
3940 .test = alg_test_hash,
3943 .vecs = tgr160_tv_template,
3944 .count = TGR160_TEST_VECTORS
3949 .test = alg_test_hash,
3952 .vecs = tgr192_tv_template,
3953 .count = TGR192_TEST_VECTORS
3958 .test = alg_test_hash,
3961 .vecs = aes_vmac128_tv_template,
3962 .count = VMAC_AES_TEST_VECTORS
3967 .test = alg_test_hash,
3970 .vecs = wp256_tv_template,
3971 .count = WP256_TEST_VECTORS
3976 .test = alg_test_hash,
3979 .vecs = wp384_tv_template,
3980 .count = WP384_TEST_VECTORS
3985 .test = alg_test_hash,
3988 .vecs = wp512_tv_template,
3989 .count = WP512_TEST_VECTORS
3994 .test = alg_test_hash,
3997 .vecs = aes_xcbc128_tv_template,
3998 .count = XCBC_AES_TEST_VECTORS
4003 .test = alg_test_skcipher,
4008 .vecs = aes_xts_enc_tv_template,
4009 .count = AES_XTS_ENC_TEST_VECTORS
4012 .vecs = aes_xts_dec_tv_template,
4013 .count = AES_XTS_DEC_TEST_VECTORS
4018 .alg = "xts(camellia)",
4019 .test = alg_test_skcipher,
4023 .vecs = camellia_xts_enc_tv_template,
4024 .count = CAMELLIA_XTS_ENC_TEST_VECTORS
4027 .vecs = camellia_xts_dec_tv_template,
4028 .count = CAMELLIA_XTS_DEC_TEST_VECTORS
4033 .alg = "xts(cast6)",
4034 .test = alg_test_skcipher,
4038 .vecs = cast6_xts_enc_tv_template,
4039 .count = CAST6_XTS_ENC_TEST_VECTORS
4042 .vecs = cast6_xts_dec_tv_template,
4043 .count = CAST6_XTS_DEC_TEST_VECTORS
4048 .alg = "xts(serpent)",
4049 .test = alg_test_skcipher,
4053 .vecs = serpent_xts_enc_tv_template,
4054 .count = SERPENT_XTS_ENC_TEST_VECTORS
4057 .vecs = serpent_xts_dec_tv_template,
4058 .count = SERPENT_XTS_DEC_TEST_VECTORS
4063 .alg = "xts(twofish)",
4064 .test = alg_test_skcipher,
4068 .vecs = tf_xts_enc_tv_template,
4069 .count = TF_XTS_ENC_TEST_VECTORS
4072 .vecs = tf_xts_dec_tv_template,
4073 .count = TF_XTS_DEC_TEST_VECTORS
4080 static bool alg_test_descs_checked;
4082 static void alg_test_descs_check_order(void)
4086 /* only check once */
4087 if (alg_test_descs_checked)
4090 alg_test_descs_checked = true;
4092 for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
4093 int diff = strcmp(alg_test_descs[i - 1].alg,
4094 alg_test_descs[i].alg);
4096 if (WARN_ON(diff > 0)) {
4097 pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
4098 alg_test_descs[i - 1].alg,
4099 alg_test_descs[i].alg);
4102 if (WARN_ON(diff == 0)) {
4103 pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
4104 alg_test_descs[i].alg);
4109 static int alg_find_test(const char *alg)
4112 int end = ARRAY_SIZE(alg_test_descs);
4114 while (start < end) {
4115 int i = (start + end) / 2;
4116 int diff = strcmp(alg_test_descs[i].alg, alg);
4134 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
4140 if (!fips_enabled && notests) {
4141 printk_once(KERN_INFO "alg: self-tests disabled\n");
4145 alg_test_descs_check_order();
4147 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
4148 char nalg[CRYPTO_MAX_ALG_NAME];
4150 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
4152 return -ENAMETOOLONG;
4154 i = alg_find_test(nalg);
4158 if (fips_enabled && !alg_test_descs[i].fips_allowed)
4161 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
4165 i = alg_find_test(alg);
4166 j = alg_find_test(driver);
4170 if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
4171 (j >= 0 && !alg_test_descs[j].fips_allowed)))
4176 rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
4178 if (j >= 0 && j != i)
4179 rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
4183 if (fips_enabled && rc)
4184 panic("%s: %s alg self test failed in fips mode!\n", driver, alg);
4186 if (fips_enabled && !rc)
4187 pr_info("alg: self-tests for %s (%s) passed\n", driver, alg);
4192 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
4198 #endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
4200 EXPORT_SYMBOL_GPL(alg_test);