2 * Algorithm testing framework and tests.
6 * Copyright (c) 2007 Nokia Siemens Networks
9 * Updated RFC4106 AES-GCM testing.
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>
40 module_param(notests, bool, 0644);
41 MODULE_PARM_DESC(notests, "disable crypto self-tests");
43 #ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
46 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
56 * Need slab memory for testing (size in number of pages).
61 * Indexes into the xbuf to simulate cross-page access.
73 * Used by test_cipher()
78 struct tcrypt_result {
79 struct completion completion;
83 struct aead_test_suite {
85 struct aead_testvec *vecs;
90 struct cipher_test_suite {
92 struct cipher_testvec *vecs;
97 struct comp_test_suite {
99 struct comp_testvec *vecs;
104 struct hash_test_suite {
105 struct hash_testvec *vecs;
109 struct cprng_test_suite {
110 struct cprng_testvec *vecs;
114 struct drbg_test_suite {
115 struct drbg_testvec *vecs;
119 struct akcipher_test_suite {
120 struct akcipher_testvec *vecs;
124 struct kpp_test_suite {
125 struct kpp_testvec *vecs;
129 struct alg_test_desc {
131 int (*test)(const struct alg_test_desc *desc, const char *driver,
133 int fips_allowed; /* set if alg is allowed in fips mode */
136 struct aead_test_suite aead;
137 struct cipher_test_suite cipher;
138 struct comp_test_suite comp;
139 struct hash_test_suite hash;
140 struct cprng_test_suite cprng;
141 struct drbg_test_suite drbg;
142 struct akcipher_test_suite akcipher;
143 struct kpp_test_suite kpp;
147 static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
149 static void hexdump(unsigned char *buf, unsigned int len)
151 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
156 static void tcrypt_complete(struct crypto_async_request *req, int err)
158 struct tcrypt_result *res = req->data;
160 if (err == -EINPROGRESS)
164 complete(&res->completion);
167 static int testmgr_alloc_buf(char *buf[XBUFSIZE])
171 for (i = 0; i < XBUFSIZE; i++) {
172 buf[i] = (void *)__get_free_page(GFP_KERNEL);
181 free_page((unsigned long)buf[i]);
186 static void testmgr_free_buf(char *buf[XBUFSIZE])
190 for (i = 0; i < XBUFSIZE; i++)
191 free_page((unsigned long)buf[i]);
194 static int wait_async_op(struct tcrypt_result *tr, int ret)
196 if (ret == -EINPROGRESS || ret == -EBUSY) {
197 wait_for_completion(&tr->completion);
198 reinit_completion(&tr->completion);
204 static int ahash_partial_update(struct ahash_request **preq,
205 struct crypto_ahash *tfm, struct hash_testvec *template,
206 void *hash_buff, int k, int temp, struct scatterlist *sg,
207 const char *algo, char *result, struct tcrypt_result *tresult)
210 struct ahash_request *req;
211 int statesize, ret = -EINVAL;
212 const char guard[] = { 0x00, 0xba, 0xad, 0x00 };
215 statesize = crypto_ahash_statesize(
216 crypto_ahash_reqtfm(req));
217 state = kmalloc(statesize + sizeof(guard), GFP_KERNEL);
219 pr_err("alt: hash: Failed to alloc state for %s\n", algo);
222 memcpy(state + statesize, guard, sizeof(guard));
223 ret = crypto_ahash_export(req, state);
224 WARN_ON(memcmp(state + statesize, guard, sizeof(guard)));
226 pr_err("alt: hash: Failed to export() for %s\n", algo);
229 ahash_request_free(req);
230 req = ahash_request_alloc(tfm, GFP_KERNEL);
232 pr_err("alg: hash: Failed to alloc request for %s\n", algo);
235 ahash_request_set_callback(req,
236 CRYPTO_TFM_REQ_MAY_BACKLOG,
237 tcrypt_complete, tresult);
239 memcpy(hash_buff, template->plaintext + temp,
241 sg_init_one(&sg[0], hash_buff, template->tap[k]);
242 ahash_request_set_crypt(req, sg, result, template->tap[k]);
243 ret = crypto_ahash_import(req, state);
245 pr_err("alg: hash: Failed to import() for %s\n", algo);
248 ret = wait_async_op(tresult, crypto_ahash_update(req));
255 ahash_request_free(req);
262 static int __test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
263 unsigned int tcount, bool use_digest,
264 const int align_offset)
266 const char *algo = crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
267 unsigned int i, j, k, temp;
268 struct scatterlist sg[8];
271 struct ahash_request *req;
272 struct tcrypt_result tresult;
274 char *xbuf[XBUFSIZE];
277 result = kmalloc(MAX_DIGEST_SIZE, GFP_KERNEL);
280 key = kmalloc(MAX_KEYLEN, GFP_KERNEL);
283 if (testmgr_alloc_buf(xbuf))
286 init_completion(&tresult.completion);
288 req = ahash_request_alloc(tfm, GFP_KERNEL);
290 printk(KERN_ERR "alg: hash: Failed to allocate request for "
294 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
295 tcrypt_complete, &tresult);
298 for (i = 0; i < tcount; i++) {
303 if (WARN_ON(align_offset + template[i].psize > PAGE_SIZE))
307 memset(result, 0, MAX_DIGEST_SIZE);
310 hash_buff += align_offset;
312 memcpy(hash_buff, template[i].plaintext, template[i].psize);
313 sg_init_one(&sg[0], hash_buff, template[i].psize);
315 if (template[i].ksize) {
316 crypto_ahash_clear_flags(tfm, ~0);
317 if (template[i].ksize > MAX_KEYLEN) {
318 pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
319 j, algo, template[i].ksize, MAX_KEYLEN);
323 memcpy(key, template[i].key, template[i].ksize);
324 ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
326 printk(KERN_ERR "alg: hash: setkey failed on "
327 "test %d for %s: ret=%d\n", j, algo,
333 ahash_request_set_crypt(req, sg, result, template[i].psize);
335 ret = wait_async_op(&tresult, crypto_ahash_digest(req));
337 pr_err("alg: hash: digest failed on test %d "
338 "for %s: ret=%d\n", j, algo, -ret);
342 ret = wait_async_op(&tresult, crypto_ahash_init(req));
344 pr_err("alt: hash: init failed on test %d "
345 "for %s: ret=%d\n", j, algo, -ret);
348 ret = wait_async_op(&tresult, crypto_ahash_update(req));
350 pr_err("alt: hash: update failed on test %d "
351 "for %s: ret=%d\n", j, algo, -ret);
354 ret = wait_async_op(&tresult, crypto_ahash_final(req));
356 pr_err("alt: hash: final failed on test %d "
357 "for %s: ret=%d\n", j, algo, -ret);
362 if (memcmp(result, template[i].digest,
363 crypto_ahash_digestsize(tfm))) {
364 printk(KERN_ERR "alg: hash: Test %d failed for %s\n",
366 hexdump(result, crypto_ahash_digestsize(tfm));
373 for (i = 0; i < tcount; i++) {
374 /* alignment tests are only done with continuous buffers */
375 if (align_offset != 0)
382 memset(result, 0, MAX_DIGEST_SIZE);
385 sg_init_table(sg, template[i].np);
387 for (k = 0; k < template[i].np; k++) {
388 if (WARN_ON(offset_in_page(IDX[k]) +
389 template[i].tap[k] > PAGE_SIZE))
392 memcpy(xbuf[IDX[k] >> PAGE_SHIFT] +
393 offset_in_page(IDX[k]),
394 template[i].plaintext + temp,
397 temp += template[i].tap[k];
400 if (template[i].ksize) {
401 if (template[i].ksize > MAX_KEYLEN) {
402 pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
403 j, algo, template[i].ksize, MAX_KEYLEN);
407 crypto_ahash_clear_flags(tfm, ~0);
408 memcpy(key, template[i].key, template[i].ksize);
409 ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
412 printk(KERN_ERR "alg: hash: setkey "
413 "failed on chunking test %d "
414 "for %s: ret=%d\n", j, algo, -ret);
419 ahash_request_set_crypt(req, sg, result, template[i].psize);
420 ret = crypto_ahash_digest(req);
426 wait_for_completion(&tresult.completion);
427 reinit_completion(&tresult.completion);
433 printk(KERN_ERR "alg: hash: digest failed "
434 "on chunking test %d for %s: "
435 "ret=%d\n", j, algo, -ret);
439 if (memcmp(result, template[i].digest,
440 crypto_ahash_digestsize(tfm))) {
441 printk(KERN_ERR "alg: hash: Chunking test %d "
442 "failed for %s\n", j, algo);
443 hexdump(result, crypto_ahash_digestsize(tfm));
449 /* partial update exercise */
451 for (i = 0; i < tcount; i++) {
452 /* alignment tests are only done with continuous buffers */
453 if (align_offset != 0)
456 if (template[i].np < 2)
460 memset(result, 0, MAX_DIGEST_SIZE);
464 memcpy(hash_buff, template[i].plaintext,
466 sg_init_one(&sg[0], hash_buff, template[i].tap[0]);
468 if (template[i].ksize) {
469 crypto_ahash_clear_flags(tfm, ~0);
470 if (template[i].ksize > MAX_KEYLEN) {
471 pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
472 j, algo, template[i].ksize, MAX_KEYLEN);
476 memcpy(key, template[i].key, template[i].ksize);
477 ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
479 pr_err("alg: hash: setkey failed on test %d for %s: ret=%d\n",
485 ahash_request_set_crypt(req, sg, result, template[i].tap[0]);
486 ret = wait_async_op(&tresult, crypto_ahash_init(req));
488 pr_err("alt: hash: init failed on test %d for %s: ret=%d\n",
492 ret = wait_async_op(&tresult, crypto_ahash_update(req));
494 pr_err("alt: hash: update failed on test %d for %s: ret=%d\n",
499 temp = template[i].tap[0];
500 for (k = 1; k < template[i].np; k++) {
501 ret = ahash_partial_update(&req, tfm, &template[i],
502 hash_buff, k, temp, &sg[0], algo, result,
505 pr_err("hash: partial update failed on test %d for %s: ret=%d\n",
509 temp += template[i].tap[k];
511 ret = wait_async_op(&tresult, crypto_ahash_final(req));
513 pr_err("alt: hash: final failed on test %d for %s: ret=%d\n",
517 if (memcmp(result, template[i].digest,
518 crypto_ahash_digestsize(tfm))) {
519 pr_err("alg: hash: Partial Test %d failed for %s\n",
521 hexdump(result, crypto_ahash_digestsize(tfm));
530 ahash_request_free(req);
532 testmgr_free_buf(xbuf);
539 static int test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
540 unsigned int tcount, bool use_digest)
542 unsigned int alignmask;
545 ret = __test_hash(tfm, template, tcount, use_digest, 0);
549 /* test unaligned buffers, check with one byte offset */
550 ret = __test_hash(tfm, template, tcount, use_digest, 1);
554 alignmask = crypto_tfm_alg_alignmask(&tfm->base);
556 /* Check if alignment mask for tfm is correctly set. */
557 ret = __test_hash(tfm, template, tcount, use_digest,
566 static int __test_aead(struct crypto_aead *tfm, int enc,
567 struct aead_testvec *template, unsigned int tcount,
568 const bool diff_dst, const int align_offset)
570 const char *algo = crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm));
571 unsigned int i, j, k, n, temp;
575 struct aead_request *req;
576 struct scatterlist *sg;
577 struct scatterlist *sgout;
579 struct tcrypt_result result;
580 unsigned int authsize, iv_len;
585 char *xbuf[XBUFSIZE];
586 char *xoutbuf[XBUFSIZE];
587 char *axbuf[XBUFSIZE];
589 iv = kzalloc(MAX_IVLEN, GFP_KERNEL);
592 key = kmalloc(MAX_KEYLEN, GFP_KERNEL);
595 if (testmgr_alloc_buf(xbuf))
597 if (testmgr_alloc_buf(axbuf))
599 if (diff_dst && testmgr_alloc_buf(xoutbuf))
602 /* avoid "the frame size is larger than 1024 bytes" compiler warning */
603 sg = kmalloc(sizeof(*sg) * 8 * (diff_dst ? 4 : 2), GFP_KERNEL);
618 init_completion(&result.completion);
620 req = aead_request_alloc(tfm, GFP_KERNEL);
622 pr_err("alg: aead%s: Failed to allocate request for %s\n",
627 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
628 tcrypt_complete, &result);
630 iv_len = crypto_aead_ivsize(tfm);
632 for (i = 0, j = 0; i < tcount; i++) {
638 /* some templates have no input data but they will
642 input += align_offset;
646 if (WARN_ON(align_offset + template[i].ilen >
647 PAGE_SIZE || template[i].alen > PAGE_SIZE))
650 memcpy(input, template[i].input, template[i].ilen);
651 memcpy(assoc, template[i].assoc, template[i].alen);
653 memcpy(iv, template[i].iv, iv_len);
655 memset(iv, 0, iv_len);
657 crypto_aead_clear_flags(tfm, ~0);
659 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
661 if (template[i].klen > MAX_KEYLEN) {
662 pr_err("alg: aead%s: setkey failed on test %d for %s: key size %d > %d\n",
663 d, j, algo, template[i].klen,
668 memcpy(key, template[i].key, template[i].klen);
670 ret = crypto_aead_setkey(tfm, key, template[i].klen);
671 if (template[i].fail == !ret) {
672 pr_err("alg: aead%s: setkey failed on test %d for %s: flags=%x\n",
673 d, j, algo, crypto_aead_get_flags(tfm));
678 authsize = abs(template[i].rlen - template[i].ilen);
679 ret = crypto_aead_setauthsize(tfm, authsize);
681 pr_err("alg: aead%s: Failed to set authsize to %u on test %d for %s\n",
682 d, authsize, j, algo);
686 k = !!template[i].alen;
687 sg_init_table(sg, k + 1);
688 sg_set_buf(&sg[0], assoc, template[i].alen);
689 sg_set_buf(&sg[k], input,
690 template[i].ilen + (enc ? authsize : 0));
694 sg_init_table(sgout, k + 1);
695 sg_set_buf(&sgout[0], assoc, template[i].alen);
698 output += align_offset;
699 sg_set_buf(&sgout[k], output,
700 template[i].rlen + (enc ? 0 : authsize));
703 aead_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
704 template[i].ilen, iv);
706 aead_request_set_ad(req, template[i].alen);
708 ret = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
712 if (template[i].novrfy) {
713 /* verification was supposed to fail */
714 pr_err("alg: aead%s: %s failed on test %d for %s: ret was 0, expected -EBADMSG\n",
716 /* so really, we got a bad message */
723 wait_for_completion(&result.completion);
724 reinit_completion(&result.completion);
729 if (template[i].novrfy)
730 /* verification failure was expected */
734 pr_err("alg: aead%s: %s failed on test %d for %s: ret=%d\n",
735 d, e, j, algo, -ret);
740 if (memcmp(q, template[i].result, template[i].rlen)) {
741 pr_err("alg: aead%s: Test %d failed on %s for %s\n",
743 hexdump(q, template[i].rlen);
749 for (i = 0, j = 0; i < tcount; i++) {
750 /* alignment tests are only done with continuous buffers */
751 if (align_offset != 0)
760 memcpy(iv, template[i].iv, iv_len);
762 memset(iv, 0, MAX_IVLEN);
764 crypto_aead_clear_flags(tfm, ~0);
766 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
767 if (template[i].klen > MAX_KEYLEN) {
768 pr_err("alg: aead%s: setkey failed on test %d for %s: key size %d > %d\n",
769 d, j, algo, template[i].klen, MAX_KEYLEN);
773 memcpy(key, template[i].key, template[i].klen);
775 ret = crypto_aead_setkey(tfm, key, template[i].klen);
776 if (template[i].fail == !ret) {
777 pr_err("alg: aead%s: setkey failed on chunk test %d for %s: flags=%x\n",
778 d, j, algo, crypto_aead_get_flags(tfm));
783 authsize = abs(template[i].rlen - template[i].ilen);
786 sg_init_table(sg, template[i].anp + template[i].np);
788 sg_init_table(sgout, template[i].anp + template[i].np);
791 for (k = 0, temp = 0; k < template[i].anp; k++) {
792 if (WARN_ON(offset_in_page(IDX[k]) +
793 template[i].atap[k] > PAGE_SIZE))
796 memcpy(axbuf[IDX[k] >> PAGE_SHIFT] +
797 offset_in_page(IDX[k]),
798 template[i].assoc + temp,
799 template[i].atap[k]),
800 template[i].atap[k]);
802 sg_set_buf(&sgout[k],
803 axbuf[IDX[k] >> PAGE_SHIFT] +
804 offset_in_page(IDX[k]),
805 template[i].atap[k]);
806 temp += template[i].atap[k];
809 for (k = 0, temp = 0; k < template[i].np; k++) {
810 if (WARN_ON(offset_in_page(IDX[k]) +
811 template[i].tap[k] > PAGE_SIZE))
814 q = xbuf[IDX[k] >> PAGE_SHIFT] + offset_in_page(IDX[k]);
815 memcpy(q, template[i].input + temp, template[i].tap[k]);
816 sg_set_buf(&sg[template[i].anp + k],
817 q, template[i].tap[k]);
820 q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
821 offset_in_page(IDX[k]);
823 memset(q, 0, template[i].tap[k]);
825 sg_set_buf(&sgout[template[i].anp + k],
826 q, template[i].tap[k]);
829 n = template[i].tap[k];
830 if (k == template[i].np - 1 && enc)
832 if (offset_in_page(q) + n < PAGE_SIZE)
835 temp += template[i].tap[k];
838 ret = crypto_aead_setauthsize(tfm, authsize);
840 pr_err("alg: aead%s: Failed to set authsize to %u on chunk test %d for %s\n",
841 d, authsize, j, algo);
846 if (WARN_ON(sg[template[i].anp + k - 1].offset +
847 sg[template[i].anp + k - 1].length +
848 authsize > PAGE_SIZE)) {
854 sgout[template[i].anp + k - 1].length +=
856 sg[template[i].anp + k - 1].length += authsize;
859 aead_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
863 aead_request_set_ad(req, template[i].alen);
865 ret = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
869 if (template[i].novrfy) {
870 /* verification was supposed to fail */
871 pr_err("alg: aead%s: %s failed on chunk test %d for %s: ret was 0, expected -EBADMSG\n",
873 /* so really, we got a bad message */
880 wait_for_completion(&result.completion);
881 reinit_completion(&result.completion);
886 if (template[i].novrfy)
887 /* verification failure was expected */
891 pr_err("alg: aead%s: %s failed on chunk test %d for %s: ret=%d\n",
892 d, e, j, algo, -ret);
897 for (k = 0, temp = 0; k < template[i].np; k++) {
899 q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
900 offset_in_page(IDX[k]);
902 q = xbuf[IDX[k] >> PAGE_SHIFT] +
903 offset_in_page(IDX[k]);
905 n = template[i].tap[k];
906 if (k == template[i].np - 1)
907 n += enc ? authsize : -authsize;
909 if (memcmp(q, template[i].result + temp, n)) {
910 pr_err("alg: aead%s: Chunk test %d failed on %s at page %u for %s\n",
917 if (k == template[i].np - 1 && !enc) {
919 memcmp(q, template[i].input +
925 for (n = 0; offset_in_page(q + n) && q[n]; n++)
929 pr_err("alg: aead%s: Result buffer corruption in chunk test %d on %s at page %u for %s: %u bytes:\n",
930 d, j, e, k, algo, n);
935 temp += template[i].tap[k];
942 aead_request_free(req);
946 testmgr_free_buf(xoutbuf);
948 testmgr_free_buf(axbuf);
950 testmgr_free_buf(xbuf);
957 static int test_aead(struct crypto_aead *tfm, int enc,
958 struct aead_testvec *template, unsigned int tcount)
960 unsigned int alignmask;
963 /* test 'dst == src' case */
964 ret = __test_aead(tfm, enc, template, tcount, false, 0);
968 /* test 'dst != src' case */
969 ret = __test_aead(tfm, enc, template, tcount, true, 0);
973 /* test unaligned buffers, check with one byte offset */
974 ret = __test_aead(tfm, enc, template, tcount, true, 1);
978 alignmask = crypto_tfm_alg_alignmask(&tfm->base);
980 /* Check if alignment mask for tfm is correctly set. */
981 ret = __test_aead(tfm, enc, template, tcount, true,
990 static int test_cipher(struct crypto_cipher *tfm, int enc,
991 struct cipher_testvec *template, unsigned int tcount)
993 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
994 unsigned int i, j, k;
998 char *xbuf[XBUFSIZE];
1001 if (testmgr_alloc_buf(xbuf))
1010 for (i = 0; i < tcount; i++) {
1014 if (fips_enabled && template[i].fips_skip)
1020 if (WARN_ON(template[i].ilen > PAGE_SIZE))
1024 memcpy(data, template[i].input, template[i].ilen);
1026 crypto_cipher_clear_flags(tfm, ~0);
1028 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
1030 ret = crypto_cipher_setkey(tfm, template[i].key,
1032 if (template[i].fail == !ret) {
1033 printk(KERN_ERR "alg: cipher: setkey failed "
1034 "on test %d for %s: flags=%x\n", j,
1035 algo, crypto_cipher_get_flags(tfm));
1040 for (k = 0; k < template[i].ilen;
1041 k += crypto_cipher_blocksize(tfm)) {
1043 crypto_cipher_encrypt_one(tfm, data + k,
1046 crypto_cipher_decrypt_one(tfm, data + k,
1051 if (memcmp(q, template[i].result, template[i].rlen)) {
1052 printk(KERN_ERR "alg: cipher: Test %d failed "
1053 "on %s for %s\n", j, e, algo);
1054 hexdump(q, template[i].rlen);
1063 testmgr_free_buf(xbuf);
1068 static int __test_skcipher(struct crypto_skcipher *tfm, int enc,
1069 struct cipher_testvec *template, unsigned int tcount,
1070 const bool diff_dst, const int align_offset)
1073 crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm));
1074 unsigned int i, j, k, n, temp;
1076 struct skcipher_request *req;
1077 struct scatterlist sg[8];
1078 struct scatterlist sgout[8];
1080 struct tcrypt_result result;
1083 char *xbuf[XBUFSIZE];
1084 char *xoutbuf[XBUFSIZE];
1086 unsigned int ivsize = crypto_skcipher_ivsize(tfm);
1088 if (testmgr_alloc_buf(xbuf))
1091 if (diff_dst && testmgr_alloc_buf(xoutbuf))
1104 init_completion(&result.completion);
1106 req = skcipher_request_alloc(tfm, GFP_KERNEL);
1108 pr_err("alg: skcipher%s: Failed to allocate request for %s\n",
1113 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1114 tcrypt_complete, &result);
1117 for (i = 0; i < tcount; i++) {
1118 if (template[i].np && !template[i].also_non_np)
1121 if (fips_enabled && template[i].fips_skip)
1125 memcpy(iv, template[i].iv, ivsize);
1127 memset(iv, 0, MAX_IVLEN);
1131 if (WARN_ON(align_offset + template[i].ilen > PAGE_SIZE))
1135 data += align_offset;
1136 memcpy(data, template[i].input, template[i].ilen);
1138 crypto_skcipher_clear_flags(tfm, ~0);
1140 crypto_skcipher_set_flags(tfm,
1141 CRYPTO_TFM_REQ_WEAK_KEY);
1143 ret = crypto_skcipher_setkey(tfm, template[i].key,
1145 if (template[i].fail == !ret) {
1146 pr_err("alg: skcipher%s: setkey failed on test %d for %s: flags=%x\n",
1147 d, j, algo, crypto_skcipher_get_flags(tfm));
1152 sg_init_one(&sg[0], data, template[i].ilen);
1155 data += align_offset;
1156 sg_init_one(&sgout[0], data, template[i].ilen);
1159 skcipher_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
1160 template[i].ilen, iv);
1161 ret = enc ? crypto_skcipher_encrypt(req) :
1162 crypto_skcipher_decrypt(req);
1169 wait_for_completion(&result.completion);
1170 reinit_completion(&result.completion);
1176 pr_err("alg: skcipher%s: %s failed on test %d for %s: ret=%d\n",
1177 d, e, j, algo, -ret);
1182 if (memcmp(q, template[i].result, template[i].rlen)) {
1183 pr_err("alg: skcipher%s: Test %d failed (invalid result) on %s for %s\n",
1185 hexdump(q, template[i].rlen);
1190 if (template[i].iv_out &&
1191 memcmp(iv, template[i].iv_out,
1192 crypto_skcipher_ivsize(tfm))) {
1193 pr_err("alg: skcipher%s: Test %d failed (invalid output IV) on %s for %s\n",
1195 hexdump(iv, crypto_skcipher_ivsize(tfm));
1202 for (i = 0; i < tcount; i++) {
1203 /* alignment tests are only done with continuous buffers */
1204 if (align_offset != 0)
1207 if (!template[i].np)
1210 if (fips_enabled && template[i].fips_skip)
1214 memcpy(iv, template[i].iv, ivsize);
1216 memset(iv, 0, MAX_IVLEN);
1219 crypto_skcipher_clear_flags(tfm, ~0);
1221 crypto_skcipher_set_flags(tfm,
1222 CRYPTO_TFM_REQ_WEAK_KEY);
1224 ret = crypto_skcipher_setkey(tfm, template[i].key,
1226 if (template[i].fail == !ret) {
1227 pr_err("alg: skcipher%s: setkey failed on chunk test %d for %s: flags=%x\n",
1228 d, j, algo, crypto_skcipher_get_flags(tfm));
1235 sg_init_table(sg, template[i].np);
1237 sg_init_table(sgout, template[i].np);
1238 for (k = 0; k < template[i].np; k++) {
1239 if (WARN_ON(offset_in_page(IDX[k]) +
1240 template[i].tap[k] > PAGE_SIZE))
1243 q = xbuf[IDX[k] >> PAGE_SHIFT] + offset_in_page(IDX[k]);
1245 memcpy(q, template[i].input + temp, template[i].tap[k]);
1247 if (offset_in_page(q) + template[i].tap[k] < PAGE_SIZE)
1248 q[template[i].tap[k]] = 0;
1250 sg_set_buf(&sg[k], q, template[i].tap[k]);
1252 q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
1253 offset_in_page(IDX[k]);
1255 sg_set_buf(&sgout[k], q, template[i].tap[k]);
1257 memset(q, 0, template[i].tap[k]);
1258 if (offset_in_page(q) +
1259 template[i].tap[k] < PAGE_SIZE)
1260 q[template[i].tap[k]] = 0;
1263 temp += template[i].tap[k];
1266 skcipher_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
1267 template[i].ilen, iv);
1269 ret = enc ? crypto_skcipher_encrypt(req) :
1270 crypto_skcipher_decrypt(req);
1277 wait_for_completion(&result.completion);
1278 reinit_completion(&result.completion);
1284 pr_err("alg: skcipher%s: %s failed on chunk test %d for %s: ret=%d\n",
1285 d, e, j, algo, -ret);
1291 for (k = 0; k < template[i].np; k++) {
1293 q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
1294 offset_in_page(IDX[k]);
1296 q = xbuf[IDX[k] >> PAGE_SHIFT] +
1297 offset_in_page(IDX[k]);
1299 if (memcmp(q, template[i].result + temp,
1300 template[i].tap[k])) {
1301 pr_err("alg: skcipher%s: Chunk test %d failed on %s at page %u for %s\n",
1303 hexdump(q, template[i].tap[k]);
1307 q += template[i].tap[k];
1308 for (n = 0; offset_in_page(q + n) && q[n]; n++)
1311 pr_err("alg: skcipher%s: Result buffer corruption in chunk test %d on %s at page %u for %s: %u bytes:\n",
1312 d, j, e, k, algo, n);
1316 temp += template[i].tap[k];
1323 skcipher_request_free(req);
1325 testmgr_free_buf(xoutbuf);
1327 testmgr_free_buf(xbuf);
1332 static int test_skcipher(struct crypto_skcipher *tfm, int enc,
1333 struct cipher_testvec *template, unsigned int tcount)
1335 unsigned int alignmask;
1338 /* test 'dst == src' case */
1339 ret = __test_skcipher(tfm, enc, template, tcount, false, 0);
1343 /* test 'dst != src' case */
1344 ret = __test_skcipher(tfm, enc, template, tcount, true, 0);
1348 /* test unaligned buffers, check with one byte offset */
1349 ret = __test_skcipher(tfm, enc, template, tcount, true, 1);
1353 alignmask = crypto_tfm_alg_alignmask(&tfm->base);
1355 /* Check if alignment mask for tfm is correctly set. */
1356 ret = __test_skcipher(tfm, enc, template, tcount, true,
1365 static int test_comp(struct crypto_comp *tfm, struct comp_testvec *ctemplate,
1366 struct comp_testvec *dtemplate, int ctcount, int dtcount)
1368 const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
1370 char result[COMP_BUF_SIZE];
1373 for (i = 0; i < ctcount; i++) {
1375 unsigned int dlen = COMP_BUF_SIZE;
1377 memset(result, 0, sizeof (result));
1379 ilen = ctemplate[i].inlen;
1380 ret = crypto_comp_compress(tfm, ctemplate[i].input,
1381 ilen, result, &dlen);
1383 printk(KERN_ERR "alg: comp: compression failed "
1384 "on test %d for %s: ret=%d\n", i + 1, algo,
1389 if (dlen != ctemplate[i].outlen) {
1390 printk(KERN_ERR "alg: comp: Compression test %d "
1391 "failed for %s: output len = %d\n", i + 1, algo,
1397 if (memcmp(result, ctemplate[i].output, dlen)) {
1398 printk(KERN_ERR "alg: comp: Compression test %d "
1399 "failed for %s\n", i + 1, algo);
1400 hexdump(result, dlen);
1406 for (i = 0; i < dtcount; i++) {
1408 unsigned int dlen = COMP_BUF_SIZE;
1410 memset(result, 0, sizeof (result));
1412 ilen = dtemplate[i].inlen;
1413 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
1414 ilen, result, &dlen);
1416 printk(KERN_ERR "alg: comp: decompression failed "
1417 "on test %d for %s: ret=%d\n", i + 1, algo,
1422 if (dlen != dtemplate[i].outlen) {
1423 printk(KERN_ERR "alg: comp: Decompression test %d "
1424 "failed for %s: output len = %d\n", i + 1, algo,
1430 if (memcmp(result, dtemplate[i].output, dlen)) {
1431 printk(KERN_ERR "alg: comp: Decompression test %d "
1432 "failed for %s\n", i + 1, algo);
1433 hexdump(result, dlen);
1445 static int test_cprng(struct crypto_rng *tfm, struct cprng_testvec *template,
1446 unsigned int tcount)
1448 const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
1449 int err = 0, i, j, seedsize;
1453 seedsize = crypto_rng_seedsize(tfm);
1455 seed = kmalloc(seedsize, GFP_KERNEL);
1457 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
1462 for (i = 0; i < tcount; i++) {
1463 memset(result, 0, 32);
1465 memcpy(seed, template[i].v, template[i].vlen);
1466 memcpy(seed + template[i].vlen, template[i].key,
1468 memcpy(seed + template[i].vlen + template[i].klen,
1469 template[i].dt, template[i].dtlen);
1471 err = crypto_rng_reset(tfm, seed, seedsize);
1473 printk(KERN_ERR "alg: cprng: Failed to reset rng "
1478 for (j = 0; j < template[i].loops; j++) {
1479 err = crypto_rng_get_bytes(tfm, result,
1482 printk(KERN_ERR "alg: cprng: Failed to obtain "
1483 "the correct amount of random data for "
1484 "%s (requested %d)\n", algo,
1490 err = memcmp(result, template[i].result,
1493 printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
1495 hexdump(result, template[i].rlen);
1506 static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
1509 struct crypto_aead *tfm;
1512 tfm = crypto_alloc_aead(driver, type | CRYPTO_ALG_INTERNAL, mask);
1514 printk(KERN_ERR "alg: aead: Failed to load transform for %s: "
1515 "%ld\n", driver, PTR_ERR(tfm));
1516 return PTR_ERR(tfm);
1519 if (desc->suite.aead.enc.vecs) {
1520 err = test_aead(tfm, ENCRYPT, desc->suite.aead.enc.vecs,
1521 desc->suite.aead.enc.count);
1526 if (!err && desc->suite.aead.dec.vecs)
1527 err = test_aead(tfm, DECRYPT, desc->suite.aead.dec.vecs,
1528 desc->suite.aead.dec.count);
1531 crypto_free_aead(tfm);
1535 static int alg_test_cipher(const struct alg_test_desc *desc,
1536 const char *driver, u32 type, u32 mask)
1538 struct crypto_cipher *tfm;
1541 tfm = crypto_alloc_cipher(driver, type | CRYPTO_ALG_INTERNAL, mask);
1543 printk(KERN_ERR "alg: cipher: Failed to load transform for "
1544 "%s: %ld\n", driver, PTR_ERR(tfm));
1545 return PTR_ERR(tfm);
1548 if (desc->suite.cipher.enc.vecs) {
1549 err = test_cipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
1550 desc->suite.cipher.enc.count);
1555 if (desc->suite.cipher.dec.vecs)
1556 err = test_cipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
1557 desc->suite.cipher.dec.count);
1560 crypto_free_cipher(tfm);
1564 static int alg_test_skcipher(const struct alg_test_desc *desc,
1565 const char *driver, u32 type, u32 mask)
1567 struct crypto_skcipher *tfm;
1570 tfm = crypto_alloc_skcipher(driver, type | CRYPTO_ALG_INTERNAL, mask);
1572 printk(KERN_ERR "alg: skcipher: Failed to load transform for "
1573 "%s: %ld\n", driver, PTR_ERR(tfm));
1574 return PTR_ERR(tfm);
1577 if (desc->suite.cipher.enc.vecs) {
1578 err = test_skcipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
1579 desc->suite.cipher.enc.count);
1584 if (desc->suite.cipher.dec.vecs)
1585 err = test_skcipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
1586 desc->suite.cipher.dec.count);
1589 crypto_free_skcipher(tfm);
1593 static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
1596 struct crypto_comp *tfm;
1599 tfm = crypto_alloc_comp(driver, type, mask);
1601 printk(KERN_ERR "alg: comp: Failed to load transform for %s: "
1602 "%ld\n", driver, PTR_ERR(tfm));
1603 return PTR_ERR(tfm);
1606 err = test_comp(tfm, desc->suite.comp.comp.vecs,
1607 desc->suite.comp.decomp.vecs,
1608 desc->suite.comp.comp.count,
1609 desc->suite.comp.decomp.count);
1611 crypto_free_comp(tfm);
1615 static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1618 struct crypto_ahash *tfm;
1621 tfm = crypto_alloc_ahash(driver, type | CRYPTO_ALG_INTERNAL, mask);
1623 printk(KERN_ERR "alg: hash: Failed to load transform for %s: "
1624 "%ld\n", driver, PTR_ERR(tfm));
1625 return PTR_ERR(tfm);
1628 err = test_hash(tfm, desc->suite.hash.vecs,
1629 desc->suite.hash.count, true);
1631 err = test_hash(tfm, desc->suite.hash.vecs,
1632 desc->suite.hash.count, false);
1634 crypto_free_ahash(tfm);
1638 static int alg_test_crc32c(const struct alg_test_desc *desc,
1639 const char *driver, u32 type, u32 mask)
1641 struct crypto_shash *tfm;
1645 err = alg_test_hash(desc, driver, type, mask);
1649 tfm = crypto_alloc_shash(driver, type | CRYPTO_ALG_INTERNAL, mask);
1651 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
1652 "%ld\n", driver, PTR_ERR(tfm));
1658 SHASH_DESC_ON_STACK(shash, tfm);
1659 u32 *ctx = (u32 *)shash_desc_ctx(shash);
1664 *ctx = le32_to_cpu(420553207);
1665 err = crypto_shash_final(shash, (u8 *)&val);
1667 printk(KERN_ERR "alg: crc32c: Operation failed for "
1668 "%s: %d\n", driver, err);
1672 if (val != ~420553207) {
1673 printk(KERN_ERR "alg: crc32c: Test failed for %s: "
1674 "%d\n", driver, val);
1679 crypto_free_shash(tfm);
1685 static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
1688 struct crypto_rng *rng;
1691 rng = crypto_alloc_rng(driver, type | CRYPTO_ALG_INTERNAL, mask);
1693 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
1694 "%ld\n", driver, PTR_ERR(rng));
1695 return PTR_ERR(rng);
1698 err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
1700 crypto_free_rng(rng);
1706 static int drbg_cavs_test(struct drbg_testvec *test, int pr,
1707 const char *driver, u32 type, u32 mask)
1710 struct crypto_rng *drng;
1711 struct drbg_test_data test_data;
1712 struct drbg_string addtl, pers, testentropy;
1713 unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
1718 drng = crypto_alloc_rng(driver, type | CRYPTO_ALG_INTERNAL, mask);
1720 printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
1726 test_data.testentropy = &testentropy;
1727 drbg_string_fill(&testentropy, test->entropy, test->entropylen);
1728 drbg_string_fill(&pers, test->pers, test->perslen);
1729 ret = crypto_drbg_reset_test(drng, &pers, &test_data);
1731 printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
1735 drbg_string_fill(&addtl, test->addtla, test->addtllen);
1737 drbg_string_fill(&testentropy, test->entpra, test->entprlen);
1738 ret = crypto_drbg_get_bytes_addtl_test(drng,
1739 buf, test->expectedlen, &addtl, &test_data);
1741 ret = crypto_drbg_get_bytes_addtl(drng,
1742 buf, test->expectedlen, &addtl);
1745 printk(KERN_ERR "alg: drbg: could not obtain random data for "
1746 "driver %s\n", driver);
1750 drbg_string_fill(&addtl, test->addtlb, test->addtllen);
1752 drbg_string_fill(&testentropy, test->entprb, test->entprlen);
1753 ret = crypto_drbg_get_bytes_addtl_test(drng,
1754 buf, test->expectedlen, &addtl, &test_data);
1756 ret = crypto_drbg_get_bytes_addtl(drng,
1757 buf, test->expectedlen, &addtl);
1760 printk(KERN_ERR "alg: drbg: could not obtain random data for "
1761 "driver %s\n", driver);
1765 ret = memcmp(test->expected, buf, test->expectedlen);
1768 crypto_free_rng(drng);
1774 static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
1780 struct drbg_testvec *template = desc->suite.drbg.vecs;
1781 unsigned int tcount = desc->suite.drbg.count;
1783 if (0 == memcmp(driver, "drbg_pr_", 8))
1786 for (i = 0; i < tcount; i++) {
1787 err = drbg_cavs_test(&template[i], pr, driver, type, mask);
1789 printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
1799 static int do_test_kpp(struct crypto_kpp *tfm, struct kpp_testvec *vec,
1802 struct kpp_request *req;
1803 void *input_buf = NULL;
1804 void *output_buf = NULL;
1805 struct tcrypt_result result;
1806 unsigned int out_len_max;
1808 struct scatterlist src, dst;
1810 req = kpp_request_alloc(tfm, GFP_KERNEL);
1814 init_completion(&result.completion);
1816 err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
1820 out_len_max = crypto_kpp_maxsize(tfm);
1821 output_buf = kzalloc(out_len_max, GFP_KERNEL);
1827 /* Use appropriate parameter as base */
1828 kpp_request_set_input(req, NULL, 0);
1829 sg_init_one(&dst, output_buf, out_len_max);
1830 kpp_request_set_output(req, &dst, out_len_max);
1831 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1832 tcrypt_complete, &result);
1834 /* Compute public key */
1835 err = wait_async_op(&result, crypto_kpp_generate_public_key(req));
1837 pr_err("alg: %s: generate public key test failed. err %d\n",
1841 /* Verify calculated public key */
1842 if (memcmp(vec->expected_a_public, sg_virt(req->dst),
1843 vec->expected_a_public_size)) {
1844 pr_err("alg: %s: generate public key test failed. Invalid output\n",
1850 /* Calculate shared secret key by using counter part (b) public key. */
1851 input_buf = kzalloc(vec->b_public_size, GFP_KERNEL);
1857 memcpy(input_buf, vec->b_public, vec->b_public_size);
1858 sg_init_one(&src, input_buf, vec->b_public_size);
1859 sg_init_one(&dst, output_buf, out_len_max);
1860 kpp_request_set_input(req, &src, vec->b_public_size);
1861 kpp_request_set_output(req, &dst, out_len_max);
1862 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1863 tcrypt_complete, &result);
1864 err = wait_async_op(&result, crypto_kpp_compute_shared_secret(req));
1866 pr_err("alg: %s: compute shard secret test failed. err %d\n",
1871 * verify shared secret from which the user will derive
1872 * secret key by executing whatever hash it has chosen
1874 if (memcmp(vec->expected_ss, sg_virt(req->dst),
1875 vec->expected_ss_size)) {
1876 pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
1886 kpp_request_free(req);
1890 static int test_kpp(struct crypto_kpp *tfm, const char *alg,
1891 struct kpp_testvec *vecs, unsigned int tcount)
1895 for (i = 0; i < tcount; i++) {
1896 ret = do_test_kpp(tfm, vecs++, alg);
1898 pr_err("alg: %s: test failed on vector %d, err=%d\n",
1906 static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
1909 struct crypto_kpp *tfm;
1912 tfm = crypto_alloc_kpp(driver, type | CRYPTO_ALG_INTERNAL, mask);
1914 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
1915 driver, PTR_ERR(tfm));
1916 return PTR_ERR(tfm);
1918 if (desc->suite.kpp.vecs)
1919 err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
1920 desc->suite.kpp.count);
1922 crypto_free_kpp(tfm);
1926 static int test_akcipher_one(struct crypto_akcipher *tfm,
1927 struct akcipher_testvec *vecs)
1929 char *xbuf[XBUFSIZE];
1930 struct akcipher_request *req;
1931 void *outbuf_enc = NULL;
1932 void *outbuf_dec = NULL;
1933 struct tcrypt_result result;
1934 unsigned int out_len_max, out_len = 0;
1936 struct scatterlist src, dst, src_tab[2];
1938 if (testmgr_alloc_buf(xbuf))
1941 req = akcipher_request_alloc(tfm, GFP_KERNEL);
1945 init_completion(&result.completion);
1947 if (vecs->public_key_vec)
1948 err = crypto_akcipher_set_pub_key(tfm, vecs->key,
1951 err = crypto_akcipher_set_priv_key(tfm, vecs->key,
1957 out_len_max = crypto_akcipher_maxsize(tfm);
1958 outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
1962 if (WARN_ON(vecs->m_size > PAGE_SIZE))
1965 memcpy(xbuf[0], vecs->m, vecs->m_size);
1967 sg_init_table(src_tab, 2);
1968 sg_set_buf(&src_tab[0], xbuf[0], 8);
1969 sg_set_buf(&src_tab[1], xbuf[0] + 8, vecs->m_size - 8);
1970 sg_init_one(&dst, outbuf_enc, out_len_max);
1971 akcipher_request_set_crypt(req, src_tab, &dst, vecs->m_size,
1973 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1974 tcrypt_complete, &result);
1976 /* Run RSA encrypt - c = m^e mod n;*/
1977 err = wait_async_op(&result, crypto_akcipher_encrypt(req));
1979 pr_err("alg: akcipher: encrypt test failed. err %d\n", err);
1982 if (req->dst_len != vecs->c_size) {
1983 pr_err("alg: akcipher: encrypt test failed. Invalid output len\n");
1987 /* verify that encrypted message is equal to expected */
1988 if (memcmp(vecs->c, outbuf_enc, vecs->c_size)) {
1989 pr_err("alg: akcipher: encrypt test failed. Invalid output\n");
1990 hexdump(outbuf_enc, vecs->c_size);
1994 /* Don't invoke decrypt for vectors with public key */
1995 if (vecs->public_key_vec) {
1999 outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
2005 if (WARN_ON(vecs->c_size > PAGE_SIZE))
2008 memcpy(xbuf[0], vecs->c, vecs->c_size);
2010 sg_init_one(&src, xbuf[0], vecs->c_size);
2011 sg_init_one(&dst, outbuf_dec, out_len_max);
2012 init_completion(&result.completion);
2013 akcipher_request_set_crypt(req, &src, &dst, vecs->c_size, out_len_max);
2015 /* Run RSA decrypt - m = c^d mod n;*/
2016 err = wait_async_op(&result, crypto_akcipher_decrypt(req));
2018 pr_err("alg: akcipher: decrypt test failed. err %d\n", err);
2021 out_len = req->dst_len;
2022 if (out_len < vecs->m_size) {
2023 pr_err("alg: akcipher: decrypt test failed. "
2024 "Invalid output len %u\n", out_len);
2028 /* verify that decrypted message is equal to the original msg */
2029 if (memchr_inv(outbuf_dec, 0, out_len - vecs->m_size) ||
2030 memcmp(vecs->m, outbuf_dec + out_len - vecs->m_size,
2032 pr_err("alg: akcipher: decrypt test failed. Invalid output\n");
2033 hexdump(outbuf_dec, out_len);
2040 akcipher_request_free(req);
2042 testmgr_free_buf(xbuf);
2046 static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
2047 struct akcipher_testvec *vecs, unsigned int tcount)
2050 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
2053 for (i = 0; i < tcount; i++) {
2054 ret = test_akcipher_one(tfm, vecs++);
2058 pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
2065 static int alg_test_akcipher(const struct alg_test_desc *desc,
2066 const char *driver, u32 type, u32 mask)
2068 struct crypto_akcipher *tfm;
2071 tfm = crypto_alloc_akcipher(driver, type | CRYPTO_ALG_INTERNAL, mask);
2073 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
2074 driver, PTR_ERR(tfm));
2075 return PTR_ERR(tfm);
2077 if (desc->suite.akcipher.vecs)
2078 err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
2079 desc->suite.akcipher.count);
2081 crypto_free_akcipher(tfm);
2085 static int alg_test_null(const struct alg_test_desc *desc,
2086 const char *driver, u32 type, u32 mask)
2091 /* Please keep this list sorted by algorithm name. */
2092 static const struct alg_test_desc alg_test_descs[] = {
2094 .alg = "__cbc-cast5-avx",
2095 .test = alg_test_null,
2097 .alg = "__cbc-cast6-avx",
2098 .test = alg_test_null,
2100 .alg = "__cbc-serpent-avx",
2101 .test = alg_test_null,
2103 .alg = "__cbc-serpent-avx2",
2104 .test = alg_test_null,
2106 .alg = "__cbc-serpent-sse2",
2107 .test = alg_test_null,
2109 .alg = "__cbc-twofish-avx",
2110 .test = alg_test_null,
2112 .alg = "__driver-cbc-aes-aesni",
2113 .test = alg_test_null,
2116 .alg = "__driver-cbc-camellia-aesni",
2117 .test = alg_test_null,
2119 .alg = "__driver-cbc-camellia-aesni-avx2",
2120 .test = alg_test_null,
2122 .alg = "__driver-cbc-cast5-avx",
2123 .test = alg_test_null,
2125 .alg = "__driver-cbc-cast6-avx",
2126 .test = alg_test_null,
2128 .alg = "__driver-cbc-serpent-avx",
2129 .test = alg_test_null,
2131 .alg = "__driver-cbc-serpent-avx2",
2132 .test = alg_test_null,
2134 .alg = "__driver-cbc-serpent-sse2",
2135 .test = alg_test_null,
2137 .alg = "__driver-cbc-twofish-avx",
2138 .test = alg_test_null,
2140 .alg = "__driver-ecb-aes-aesni",
2141 .test = alg_test_null,
2144 .alg = "__driver-ecb-camellia-aesni",
2145 .test = alg_test_null,
2147 .alg = "__driver-ecb-camellia-aesni-avx2",
2148 .test = alg_test_null,
2150 .alg = "__driver-ecb-cast5-avx",
2151 .test = alg_test_null,
2153 .alg = "__driver-ecb-cast6-avx",
2154 .test = alg_test_null,
2156 .alg = "__driver-ecb-serpent-avx",
2157 .test = alg_test_null,
2159 .alg = "__driver-ecb-serpent-avx2",
2160 .test = alg_test_null,
2162 .alg = "__driver-ecb-serpent-sse2",
2163 .test = alg_test_null,
2165 .alg = "__driver-ecb-twofish-avx",
2166 .test = alg_test_null,
2168 .alg = "__driver-gcm-aes-aesni",
2169 .test = alg_test_null,
2172 .alg = "__ghash-pclmulqdqni",
2173 .test = alg_test_null,
2176 .alg = "ansi_cprng",
2177 .test = alg_test_cprng,
2180 .vecs = ansi_cprng_aes_tv_template,
2181 .count = ANSI_CPRNG_AES_TEST_VECTORS
2185 .alg = "authenc(hmac(md5),ecb(cipher_null))",
2186 .test = alg_test_aead,
2190 .vecs = hmac_md5_ecb_cipher_null_enc_tv_template,
2191 .count = HMAC_MD5_ECB_CIPHER_NULL_ENC_TEST_VECTORS
2194 .vecs = hmac_md5_ecb_cipher_null_dec_tv_template,
2195 .count = HMAC_MD5_ECB_CIPHER_NULL_DEC_TEST_VECTORS
2200 .alg = "authenc(hmac(sha1),cbc(aes))",
2201 .test = alg_test_aead,
2206 hmac_sha1_aes_cbc_enc_tv_temp,
2208 HMAC_SHA1_AES_CBC_ENC_TEST_VEC
2213 .alg = "authenc(hmac(sha1),cbc(des))",
2214 .test = alg_test_aead,
2219 hmac_sha1_des_cbc_enc_tv_temp,
2221 HMAC_SHA1_DES_CBC_ENC_TEST_VEC
2226 .alg = "authenc(hmac(sha1),cbc(des3_ede))",
2227 .test = alg_test_aead,
2233 hmac_sha1_des3_ede_cbc_enc_tv_temp,
2235 HMAC_SHA1_DES3_EDE_CBC_ENC_TEST_VEC
2240 .alg = "authenc(hmac(sha1),ctr(aes))",
2241 .test = alg_test_null,
2244 .alg = "authenc(hmac(sha1),ecb(cipher_null))",
2245 .test = alg_test_aead,
2250 hmac_sha1_ecb_cipher_null_enc_tv_temp,
2252 HMAC_SHA1_ECB_CIPHER_NULL_ENC_TEST_VEC
2256 hmac_sha1_ecb_cipher_null_dec_tv_temp,
2258 HMAC_SHA1_ECB_CIPHER_NULL_DEC_TEST_VEC
2263 .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
2264 .test = alg_test_null,
2267 .alg = "authenc(hmac(sha224),cbc(des))",
2268 .test = alg_test_aead,
2273 hmac_sha224_des_cbc_enc_tv_temp,
2275 HMAC_SHA224_DES_CBC_ENC_TEST_VEC
2280 .alg = "authenc(hmac(sha224),cbc(des3_ede))",
2281 .test = alg_test_aead,
2287 hmac_sha224_des3_ede_cbc_enc_tv_temp,
2289 HMAC_SHA224_DES3_EDE_CBC_ENC_TEST_VEC
2294 .alg = "authenc(hmac(sha256),cbc(aes))",
2295 .test = alg_test_aead,
2301 hmac_sha256_aes_cbc_enc_tv_temp,
2303 HMAC_SHA256_AES_CBC_ENC_TEST_VEC
2308 .alg = "authenc(hmac(sha256),cbc(des))",
2309 .test = alg_test_aead,
2314 hmac_sha256_des_cbc_enc_tv_temp,
2316 HMAC_SHA256_DES_CBC_ENC_TEST_VEC
2321 .alg = "authenc(hmac(sha256),cbc(des3_ede))",
2322 .test = alg_test_aead,
2328 hmac_sha256_des3_ede_cbc_enc_tv_temp,
2330 HMAC_SHA256_DES3_EDE_CBC_ENC_TEST_VEC
2335 .alg = "authenc(hmac(sha256),ctr(aes))",
2336 .test = alg_test_null,
2339 .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
2340 .test = alg_test_null,
2343 .alg = "authenc(hmac(sha384),cbc(des))",
2344 .test = alg_test_aead,
2349 hmac_sha384_des_cbc_enc_tv_temp,
2351 HMAC_SHA384_DES_CBC_ENC_TEST_VEC
2356 .alg = "authenc(hmac(sha384),cbc(des3_ede))",
2357 .test = alg_test_aead,
2363 hmac_sha384_des3_ede_cbc_enc_tv_temp,
2365 HMAC_SHA384_DES3_EDE_CBC_ENC_TEST_VEC
2370 .alg = "authenc(hmac(sha384),ctr(aes))",
2371 .test = alg_test_null,
2374 .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
2375 .test = alg_test_null,
2378 .alg = "authenc(hmac(sha512),cbc(aes))",
2380 .test = alg_test_aead,
2385 hmac_sha512_aes_cbc_enc_tv_temp,
2387 HMAC_SHA512_AES_CBC_ENC_TEST_VEC
2392 .alg = "authenc(hmac(sha512),cbc(des))",
2393 .test = alg_test_aead,
2398 hmac_sha512_des_cbc_enc_tv_temp,
2400 HMAC_SHA512_DES_CBC_ENC_TEST_VEC
2405 .alg = "authenc(hmac(sha512),cbc(des3_ede))",
2406 .test = alg_test_aead,
2412 hmac_sha512_des3_ede_cbc_enc_tv_temp,
2414 HMAC_SHA512_DES3_EDE_CBC_ENC_TEST_VEC
2419 .alg = "authenc(hmac(sha512),ctr(aes))",
2420 .test = alg_test_null,
2423 .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
2424 .test = alg_test_null,
2428 .test = alg_test_skcipher,
2433 .vecs = aes_cbc_enc_tv_template,
2434 .count = AES_CBC_ENC_TEST_VECTORS
2437 .vecs = aes_cbc_dec_tv_template,
2438 .count = AES_CBC_DEC_TEST_VECTORS
2443 .alg = "cbc(anubis)",
2444 .test = alg_test_skcipher,
2448 .vecs = anubis_cbc_enc_tv_template,
2449 .count = ANUBIS_CBC_ENC_TEST_VECTORS
2452 .vecs = anubis_cbc_dec_tv_template,
2453 .count = ANUBIS_CBC_DEC_TEST_VECTORS
2458 .alg = "cbc(blowfish)",
2459 .test = alg_test_skcipher,
2463 .vecs = bf_cbc_enc_tv_template,
2464 .count = BF_CBC_ENC_TEST_VECTORS
2467 .vecs = bf_cbc_dec_tv_template,
2468 .count = BF_CBC_DEC_TEST_VECTORS
2473 .alg = "cbc(camellia)",
2474 .test = alg_test_skcipher,
2478 .vecs = camellia_cbc_enc_tv_template,
2479 .count = CAMELLIA_CBC_ENC_TEST_VECTORS
2482 .vecs = camellia_cbc_dec_tv_template,
2483 .count = CAMELLIA_CBC_DEC_TEST_VECTORS
2488 .alg = "cbc(cast5)",
2489 .test = alg_test_skcipher,
2493 .vecs = cast5_cbc_enc_tv_template,
2494 .count = CAST5_CBC_ENC_TEST_VECTORS
2497 .vecs = cast5_cbc_dec_tv_template,
2498 .count = CAST5_CBC_DEC_TEST_VECTORS
2503 .alg = "cbc(cast6)",
2504 .test = alg_test_skcipher,
2508 .vecs = cast6_cbc_enc_tv_template,
2509 .count = CAST6_CBC_ENC_TEST_VECTORS
2512 .vecs = cast6_cbc_dec_tv_template,
2513 .count = CAST6_CBC_DEC_TEST_VECTORS
2519 .test = alg_test_skcipher,
2523 .vecs = des_cbc_enc_tv_template,
2524 .count = DES_CBC_ENC_TEST_VECTORS
2527 .vecs = des_cbc_dec_tv_template,
2528 .count = DES_CBC_DEC_TEST_VECTORS
2533 .alg = "cbc(des3_ede)",
2534 .test = alg_test_skcipher,
2539 .vecs = des3_ede_cbc_enc_tv_template,
2540 .count = DES3_EDE_CBC_ENC_TEST_VECTORS
2543 .vecs = des3_ede_cbc_dec_tv_template,
2544 .count = DES3_EDE_CBC_DEC_TEST_VECTORS
2549 .alg = "cbc(serpent)",
2550 .test = alg_test_skcipher,
2554 .vecs = serpent_cbc_enc_tv_template,
2555 .count = SERPENT_CBC_ENC_TEST_VECTORS
2558 .vecs = serpent_cbc_dec_tv_template,
2559 .count = SERPENT_CBC_DEC_TEST_VECTORS
2564 .alg = "cbc(twofish)",
2565 .test = alg_test_skcipher,
2569 .vecs = tf_cbc_enc_tv_template,
2570 .count = TF_CBC_ENC_TEST_VECTORS
2573 .vecs = tf_cbc_dec_tv_template,
2574 .count = TF_CBC_DEC_TEST_VECTORS
2580 .test = alg_test_aead,
2585 .vecs = aes_ccm_enc_tv_template,
2586 .count = AES_CCM_ENC_TEST_VECTORS
2589 .vecs = aes_ccm_dec_tv_template,
2590 .count = AES_CCM_DEC_TEST_VECTORS
2596 .test = alg_test_skcipher,
2600 .vecs = chacha20_enc_tv_template,
2601 .count = CHACHA20_ENC_TEST_VECTORS
2604 .vecs = chacha20_enc_tv_template,
2605 .count = CHACHA20_ENC_TEST_VECTORS
2612 .test = alg_test_hash,
2615 .vecs = aes_cmac128_tv_template,
2616 .count = CMAC_AES_TEST_VECTORS
2620 .alg = "cmac(des3_ede)",
2622 .test = alg_test_hash,
2625 .vecs = des3_ede_cmac64_tv_template,
2626 .count = CMAC_DES3_EDE_TEST_VECTORS
2630 .alg = "compress_null",
2631 .test = alg_test_null,
2634 .test = alg_test_hash,
2637 .vecs = crc32_tv_template,
2638 .count = CRC32_TEST_VECTORS
2643 .test = alg_test_crc32c,
2647 .vecs = crc32c_tv_template,
2648 .count = CRC32C_TEST_VECTORS
2653 .test = alg_test_hash,
2657 .vecs = crct10dif_tv_template,
2658 .count = CRCT10DIF_TEST_VECTORS
2662 .alg = "cryptd(__driver-cbc-aes-aesni)",
2663 .test = alg_test_null,
2666 .alg = "cryptd(__driver-cbc-camellia-aesni)",
2667 .test = alg_test_null,
2669 .alg = "cryptd(__driver-cbc-camellia-aesni-avx2)",
2670 .test = alg_test_null,
2672 .alg = "cryptd(__driver-cbc-serpent-avx2)",
2673 .test = alg_test_null,
2675 .alg = "cryptd(__driver-ecb-aes-aesni)",
2676 .test = alg_test_null,
2679 .alg = "cryptd(__driver-ecb-camellia-aesni)",
2680 .test = alg_test_null,
2682 .alg = "cryptd(__driver-ecb-camellia-aesni-avx2)",
2683 .test = alg_test_null,
2685 .alg = "cryptd(__driver-ecb-cast5-avx)",
2686 .test = alg_test_null,
2688 .alg = "cryptd(__driver-ecb-cast6-avx)",
2689 .test = alg_test_null,
2691 .alg = "cryptd(__driver-ecb-serpent-avx)",
2692 .test = alg_test_null,
2694 .alg = "cryptd(__driver-ecb-serpent-avx2)",
2695 .test = alg_test_null,
2697 .alg = "cryptd(__driver-ecb-serpent-sse2)",
2698 .test = alg_test_null,
2700 .alg = "cryptd(__driver-ecb-twofish-avx)",
2701 .test = alg_test_null,
2703 .alg = "cryptd(__driver-gcm-aes-aesni)",
2704 .test = alg_test_null,
2707 .alg = "cryptd(__ghash-pclmulqdqni)",
2708 .test = alg_test_null,
2712 .test = alg_test_skcipher,
2717 .vecs = aes_ctr_enc_tv_template,
2718 .count = AES_CTR_ENC_TEST_VECTORS
2721 .vecs = aes_ctr_dec_tv_template,
2722 .count = AES_CTR_DEC_TEST_VECTORS
2727 .alg = "ctr(blowfish)",
2728 .test = alg_test_skcipher,
2732 .vecs = bf_ctr_enc_tv_template,
2733 .count = BF_CTR_ENC_TEST_VECTORS
2736 .vecs = bf_ctr_dec_tv_template,
2737 .count = BF_CTR_DEC_TEST_VECTORS
2742 .alg = "ctr(camellia)",
2743 .test = alg_test_skcipher,
2747 .vecs = camellia_ctr_enc_tv_template,
2748 .count = CAMELLIA_CTR_ENC_TEST_VECTORS
2751 .vecs = camellia_ctr_dec_tv_template,
2752 .count = CAMELLIA_CTR_DEC_TEST_VECTORS
2757 .alg = "ctr(cast5)",
2758 .test = alg_test_skcipher,
2762 .vecs = cast5_ctr_enc_tv_template,
2763 .count = CAST5_CTR_ENC_TEST_VECTORS
2766 .vecs = cast5_ctr_dec_tv_template,
2767 .count = CAST5_CTR_DEC_TEST_VECTORS
2772 .alg = "ctr(cast6)",
2773 .test = alg_test_skcipher,
2777 .vecs = cast6_ctr_enc_tv_template,
2778 .count = CAST6_CTR_ENC_TEST_VECTORS
2781 .vecs = cast6_ctr_dec_tv_template,
2782 .count = CAST6_CTR_DEC_TEST_VECTORS
2788 .test = alg_test_skcipher,
2792 .vecs = des_ctr_enc_tv_template,
2793 .count = DES_CTR_ENC_TEST_VECTORS
2796 .vecs = des_ctr_dec_tv_template,
2797 .count = DES_CTR_DEC_TEST_VECTORS
2802 .alg = "ctr(des3_ede)",
2803 .test = alg_test_skcipher,
2807 .vecs = des3_ede_ctr_enc_tv_template,
2808 .count = DES3_EDE_CTR_ENC_TEST_VECTORS
2811 .vecs = des3_ede_ctr_dec_tv_template,
2812 .count = DES3_EDE_CTR_DEC_TEST_VECTORS
2817 .alg = "ctr(serpent)",
2818 .test = alg_test_skcipher,
2822 .vecs = serpent_ctr_enc_tv_template,
2823 .count = SERPENT_CTR_ENC_TEST_VECTORS
2826 .vecs = serpent_ctr_dec_tv_template,
2827 .count = SERPENT_CTR_DEC_TEST_VECTORS
2832 .alg = "ctr(twofish)",
2833 .test = alg_test_skcipher,
2837 .vecs = tf_ctr_enc_tv_template,
2838 .count = TF_CTR_ENC_TEST_VECTORS
2841 .vecs = tf_ctr_dec_tv_template,
2842 .count = TF_CTR_DEC_TEST_VECTORS
2847 .alg = "cts(cbc(aes))",
2848 .test = alg_test_skcipher,
2852 .vecs = cts_mode_enc_tv_template,
2853 .count = CTS_MODE_ENC_TEST_VECTORS
2856 .vecs = cts_mode_dec_tv_template,
2857 .count = CTS_MODE_DEC_TEST_VECTORS
2863 .test = alg_test_comp,
2868 .vecs = deflate_comp_tv_template,
2869 .count = DEFLATE_COMP_TEST_VECTORS
2872 .vecs = deflate_decomp_tv_template,
2873 .count = DEFLATE_DECOMP_TEST_VECTORS
2879 .test = alg_test_kpp,
2883 .vecs = dh_tv_template,
2884 .count = DH_TEST_VECTORS
2888 .alg = "digest_null",
2889 .test = alg_test_null,
2891 .alg = "drbg_nopr_ctr_aes128",
2892 .test = alg_test_drbg,
2896 .vecs = drbg_nopr_ctr_aes128_tv_template,
2897 .count = ARRAY_SIZE(drbg_nopr_ctr_aes128_tv_template)
2901 .alg = "drbg_nopr_ctr_aes192",
2902 .test = alg_test_drbg,
2906 .vecs = drbg_nopr_ctr_aes192_tv_template,
2907 .count = ARRAY_SIZE(drbg_nopr_ctr_aes192_tv_template)
2911 .alg = "drbg_nopr_ctr_aes256",
2912 .test = alg_test_drbg,
2916 .vecs = drbg_nopr_ctr_aes256_tv_template,
2917 .count = ARRAY_SIZE(drbg_nopr_ctr_aes256_tv_template)
2922 * There is no need to specifically test the DRBG with every
2923 * backend cipher -- covered by drbg_nopr_hmac_sha256 test
2925 .alg = "drbg_nopr_hmac_sha1",
2927 .test = alg_test_null,
2929 .alg = "drbg_nopr_hmac_sha256",
2930 .test = alg_test_drbg,
2934 .vecs = drbg_nopr_hmac_sha256_tv_template,
2936 ARRAY_SIZE(drbg_nopr_hmac_sha256_tv_template)
2940 /* covered by drbg_nopr_hmac_sha256 test */
2941 .alg = "drbg_nopr_hmac_sha384",
2943 .test = alg_test_null,
2945 .alg = "drbg_nopr_hmac_sha512",
2946 .test = alg_test_null,
2949 .alg = "drbg_nopr_sha1",
2951 .test = alg_test_null,
2953 .alg = "drbg_nopr_sha256",
2954 .test = alg_test_drbg,
2958 .vecs = drbg_nopr_sha256_tv_template,
2959 .count = ARRAY_SIZE(drbg_nopr_sha256_tv_template)
2963 /* covered by drbg_nopr_sha256 test */
2964 .alg = "drbg_nopr_sha384",
2966 .test = alg_test_null,
2968 .alg = "drbg_nopr_sha512",
2970 .test = alg_test_null,
2972 .alg = "drbg_pr_ctr_aes128",
2973 .test = alg_test_drbg,
2977 .vecs = drbg_pr_ctr_aes128_tv_template,
2978 .count = ARRAY_SIZE(drbg_pr_ctr_aes128_tv_template)
2982 /* covered by drbg_pr_ctr_aes128 test */
2983 .alg = "drbg_pr_ctr_aes192",
2985 .test = alg_test_null,
2987 .alg = "drbg_pr_ctr_aes256",
2989 .test = alg_test_null,
2991 .alg = "drbg_pr_hmac_sha1",
2993 .test = alg_test_null,
2995 .alg = "drbg_pr_hmac_sha256",
2996 .test = alg_test_drbg,
3000 .vecs = drbg_pr_hmac_sha256_tv_template,
3001 .count = ARRAY_SIZE(drbg_pr_hmac_sha256_tv_template)
3005 /* covered by drbg_pr_hmac_sha256 test */
3006 .alg = "drbg_pr_hmac_sha384",
3008 .test = alg_test_null,
3010 .alg = "drbg_pr_hmac_sha512",
3011 .test = alg_test_null,
3014 .alg = "drbg_pr_sha1",
3016 .test = alg_test_null,
3018 .alg = "drbg_pr_sha256",
3019 .test = alg_test_drbg,
3023 .vecs = drbg_pr_sha256_tv_template,
3024 .count = ARRAY_SIZE(drbg_pr_sha256_tv_template)
3028 /* covered by drbg_pr_sha256 test */
3029 .alg = "drbg_pr_sha384",
3031 .test = alg_test_null,
3033 .alg = "drbg_pr_sha512",
3035 .test = alg_test_null,
3037 .alg = "ecb(__aes-aesni)",
3038 .test = alg_test_null,
3042 .test = alg_test_skcipher,
3047 .vecs = aes_enc_tv_template,
3048 .count = AES_ENC_TEST_VECTORS
3051 .vecs = aes_dec_tv_template,
3052 .count = AES_DEC_TEST_VECTORS
3057 .alg = "ecb(anubis)",
3058 .test = alg_test_skcipher,
3062 .vecs = anubis_enc_tv_template,
3063 .count = ANUBIS_ENC_TEST_VECTORS
3066 .vecs = anubis_dec_tv_template,
3067 .count = ANUBIS_DEC_TEST_VECTORS
3073 .test = alg_test_skcipher,
3077 .vecs = arc4_enc_tv_template,
3078 .count = ARC4_ENC_TEST_VECTORS
3081 .vecs = arc4_dec_tv_template,
3082 .count = ARC4_DEC_TEST_VECTORS
3087 .alg = "ecb(blowfish)",
3088 .test = alg_test_skcipher,
3092 .vecs = bf_enc_tv_template,
3093 .count = BF_ENC_TEST_VECTORS
3096 .vecs = bf_dec_tv_template,
3097 .count = BF_DEC_TEST_VECTORS
3102 .alg = "ecb(camellia)",
3103 .test = alg_test_skcipher,
3107 .vecs = camellia_enc_tv_template,
3108 .count = CAMELLIA_ENC_TEST_VECTORS
3111 .vecs = camellia_dec_tv_template,
3112 .count = CAMELLIA_DEC_TEST_VECTORS
3117 .alg = "ecb(cast5)",
3118 .test = alg_test_skcipher,
3122 .vecs = cast5_enc_tv_template,
3123 .count = CAST5_ENC_TEST_VECTORS
3126 .vecs = cast5_dec_tv_template,
3127 .count = CAST5_DEC_TEST_VECTORS
3132 .alg = "ecb(cast6)",
3133 .test = alg_test_skcipher,
3137 .vecs = cast6_enc_tv_template,
3138 .count = CAST6_ENC_TEST_VECTORS
3141 .vecs = cast6_dec_tv_template,
3142 .count = CAST6_DEC_TEST_VECTORS
3147 .alg = "ecb(cipher_null)",
3148 .test = alg_test_null,
3151 .test = alg_test_skcipher,
3155 .vecs = des_enc_tv_template,
3156 .count = DES_ENC_TEST_VECTORS
3159 .vecs = des_dec_tv_template,
3160 .count = DES_DEC_TEST_VECTORS
3165 .alg = "ecb(des3_ede)",
3166 .test = alg_test_skcipher,
3171 .vecs = des3_ede_enc_tv_template,
3172 .count = DES3_EDE_ENC_TEST_VECTORS
3175 .vecs = des3_ede_dec_tv_template,
3176 .count = DES3_EDE_DEC_TEST_VECTORS
3181 .alg = "ecb(fcrypt)",
3182 .test = alg_test_skcipher,
3186 .vecs = fcrypt_pcbc_enc_tv_template,
3190 .vecs = fcrypt_pcbc_dec_tv_template,
3196 .alg = "ecb(khazad)",
3197 .test = alg_test_skcipher,
3201 .vecs = khazad_enc_tv_template,
3202 .count = KHAZAD_ENC_TEST_VECTORS
3205 .vecs = khazad_dec_tv_template,
3206 .count = KHAZAD_DEC_TEST_VECTORS
3212 .test = alg_test_skcipher,
3216 .vecs = seed_enc_tv_template,
3217 .count = SEED_ENC_TEST_VECTORS
3220 .vecs = seed_dec_tv_template,
3221 .count = SEED_DEC_TEST_VECTORS
3226 .alg = "ecb(serpent)",
3227 .test = alg_test_skcipher,
3231 .vecs = serpent_enc_tv_template,
3232 .count = SERPENT_ENC_TEST_VECTORS
3235 .vecs = serpent_dec_tv_template,
3236 .count = SERPENT_DEC_TEST_VECTORS
3242 .test = alg_test_skcipher,
3246 .vecs = tea_enc_tv_template,
3247 .count = TEA_ENC_TEST_VECTORS
3250 .vecs = tea_dec_tv_template,
3251 .count = TEA_DEC_TEST_VECTORS
3256 .alg = "ecb(tnepres)",
3257 .test = alg_test_skcipher,
3261 .vecs = tnepres_enc_tv_template,
3262 .count = TNEPRES_ENC_TEST_VECTORS
3265 .vecs = tnepres_dec_tv_template,
3266 .count = TNEPRES_DEC_TEST_VECTORS
3271 .alg = "ecb(twofish)",
3272 .test = alg_test_skcipher,
3276 .vecs = tf_enc_tv_template,
3277 .count = TF_ENC_TEST_VECTORS
3280 .vecs = tf_dec_tv_template,
3281 .count = TF_DEC_TEST_VECTORS
3287 .test = alg_test_skcipher,
3291 .vecs = xeta_enc_tv_template,
3292 .count = XETA_ENC_TEST_VECTORS
3295 .vecs = xeta_dec_tv_template,
3296 .count = XETA_DEC_TEST_VECTORS
3302 .test = alg_test_skcipher,
3306 .vecs = xtea_enc_tv_template,
3307 .count = XTEA_ENC_TEST_VECTORS
3310 .vecs = xtea_dec_tv_template,
3311 .count = XTEA_DEC_TEST_VECTORS
3317 .test = alg_test_kpp,
3321 .vecs = ecdh_tv_template,
3322 .count = ECDH_TEST_VECTORS
3327 .test = alg_test_aead,
3332 .vecs = aes_gcm_enc_tv_template,
3333 .count = AES_GCM_ENC_TEST_VECTORS
3336 .vecs = aes_gcm_dec_tv_template,
3337 .count = AES_GCM_DEC_TEST_VECTORS
3343 .test = alg_test_hash,
3347 .vecs = ghash_tv_template,
3348 .count = GHASH_TEST_VECTORS
3352 .alg = "hmac(crc32)",
3353 .test = alg_test_hash,
3356 .vecs = bfin_crc_tv_template,
3357 .count = BFIN_CRC_TEST_VECTORS
3362 .test = alg_test_hash,
3365 .vecs = hmac_md5_tv_template,
3366 .count = HMAC_MD5_TEST_VECTORS
3370 .alg = "hmac(rmd128)",
3371 .test = alg_test_hash,
3374 .vecs = hmac_rmd128_tv_template,
3375 .count = HMAC_RMD128_TEST_VECTORS
3379 .alg = "hmac(rmd160)",
3380 .test = alg_test_hash,
3383 .vecs = hmac_rmd160_tv_template,
3384 .count = HMAC_RMD160_TEST_VECTORS
3388 .alg = "hmac(sha1)",
3389 .test = alg_test_hash,
3393 .vecs = hmac_sha1_tv_template,
3394 .count = HMAC_SHA1_TEST_VECTORS
3398 .alg = "hmac(sha224)",
3399 .test = alg_test_hash,
3403 .vecs = hmac_sha224_tv_template,
3404 .count = HMAC_SHA224_TEST_VECTORS
3408 .alg = "hmac(sha256)",
3409 .test = alg_test_hash,
3413 .vecs = hmac_sha256_tv_template,
3414 .count = HMAC_SHA256_TEST_VECTORS
3418 .alg = "hmac(sha3-224)",
3419 .test = alg_test_hash,
3423 .vecs = hmac_sha3_224_tv_template,
3424 .count = HMAC_SHA3_224_TEST_VECTORS
3428 .alg = "hmac(sha3-256)",
3429 .test = alg_test_hash,
3433 .vecs = hmac_sha3_256_tv_template,
3434 .count = HMAC_SHA3_256_TEST_VECTORS
3438 .alg = "hmac(sha3-384)",
3439 .test = alg_test_hash,
3443 .vecs = hmac_sha3_384_tv_template,
3444 .count = HMAC_SHA3_384_TEST_VECTORS
3448 .alg = "hmac(sha3-512)",
3449 .test = alg_test_hash,
3453 .vecs = hmac_sha3_512_tv_template,
3454 .count = HMAC_SHA3_512_TEST_VECTORS
3458 .alg = "hmac(sha384)",
3459 .test = alg_test_hash,
3463 .vecs = hmac_sha384_tv_template,
3464 .count = HMAC_SHA384_TEST_VECTORS
3468 .alg = "hmac(sha512)",
3469 .test = alg_test_hash,
3473 .vecs = hmac_sha512_tv_template,
3474 .count = HMAC_SHA512_TEST_VECTORS
3478 .alg = "jitterentropy_rng",
3480 .test = alg_test_null,
3483 .test = alg_test_skcipher,
3488 .vecs = aes_kw_enc_tv_template,
3489 .count = ARRAY_SIZE(aes_kw_enc_tv_template)
3492 .vecs = aes_kw_dec_tv_template,
3493 .count = ARRAY_SIZE(aes_kw_dec_tv_template)
3499 .test = alg_test_skcipher,
3503 .vecs = aes_lrw_enc_tv_template,
3504 .count = AES_LRW_ENC_TEST_VECTORS
3507 .vecs = aes_lrw_dec_tv_template,
3508 .count = AES_LRW_DEC_TEST_VECTORS
3513 .alg = "lrw(camellia)",
3514 .test = alg_test_skcipher,
3518 .vecs = camellia_lrw_enc_tv_template,
3519 .count = CAMELLIA_LRW_ENC_TEST_VECTORS
3522 .vecs = camellia_lrw_dec_tv_template,
3523 .count = CAMELLIA_LRW_DEC_TEST_VECTORS
3528 .alg = "lrw(cast6)",
3529 .test = alg_test_skcipher,
3533 .vecs = cast6_lrw_enc_tv_template,
3534 .count = CAST6_LRW_ENC_TEST_VECTORS
3537 .vecs = cast6_lrw_dec_tv_template,
3538 .count = CAST6_LRW_DEC_TEST_VECTORS
3543 .alg = "lrw(serpent)",
3544 .test = alg_test_skcipher,
3548 .vecs = serpent_lrw_enc_tv_template,
3549 .count = SERPENT_LRW_ENC_TEST_VECTORS
3552 .vecs = serpent_lrw_dec_tv_template,
3553 .count = SERPENT_LRW_DEC_TEST_VECTORS
3558 .alg = "lrw(twofish)",
3559 .test = alg_test_skcipher,
3563 .vecs = tf_lrw_enc_tv_template,
3564 .count = TF_LRW_ENC_TEST_VECTORS
3567 .vecs = tf_lrw_dec_tv_template,
3568 .count = TF_LRW_DEC_TEST_VECTORS
3574 .test = alg_test_comp,
3579 .vecs = lz4_comp_tv_template,
3580 .count = LZ4_COMP_TEST_VECTORS
3583 .vecs = lz4_decomp_tv_template,
3584 .count = LZ4_DECOMP_TEST_VECTORS
3590 .test = alg_test_comp,
3595 .vecs = lz4hc_comp_tv_template,
3596 .count = LZ4HC_COMP_TEST_VECTORS
3599 .vecs = lz4hc_decomp_tv_template,
3600 .count = LZ4HC_DECOMP_TEST_VECTORS
3606 .test = alg_test_comp,
3611 .vecs = lzo_comp_tv_template,
3612 .count = LZO_COMP_TEST_VECTORS
3615 .vecs = lzo_decomp_tv_template,
3616 .count = LZO_DECOMP_TEST_VECTORS
3622 .test = alg_test_hash,
3625 .vecs = md4_tv_template,
3626 .count = MD4_TEST_VECTORS
3631 .test = alg_test_hash,
3634 .vecs = md5_tv_template,
3635 .count = MD5_TEST_VECTORS
3639 .alg = "michael_mic",
3640 .test = alg_test_hash,
3643 .vecs = michael_mic_tv_template,
3644 .count = MICHAEL_MIC_TEST_VECTORS
3649 .test = alg_test_skcipher,
3654 .vecs = aes_ofb_enc_tv_template,
3655 .count = AES_OFB_ENC_TEST_VECTORS
3658 .vecs = aes_ofb_dec_tv_template,
3659 .count = AES_OFB_DEC_TEST_VECTORS
3664 .alg = "pcbc(fcrypt)",
3665 .test = alg_test_skcipher,
3669 .vecs = fcrypt_pcbc_enc_tv_template,
3670 .count = FCRYPT_ENC_TEST_VECTORS
3673 .vecs = fcrypt_pcbc_dec_tv_template,
3674 .count = FCRYPT_DEC_TEST_VECTORS
3680 .test = alg_test_hash,
3683 .vecs = poly1305_tv_template,
3684 .count = POLY1305_TEST_VECTORS
3688 .alg = "rfc3686(ctr(aes))",
3689 .test = alg_test_skcipher,
3694 .vecs = aes_ctr_rfc3686_enc_tv_template,
3695 .count = AES_CTR_3686_ENC_TEST_VECTORS
3698 .vecs = aes_ctr_rfc3686_dec_tv_template,
3699 .count = AES_CTR_3686_DEC_TEST_VECTORS
3704 .alg = "rfc4106(gcm(aes))",
3705 .test = alg_test_aead,
3710 .vecs = aes_gcm_rfc4106_enc_tv_template,
3711 .count = AES_GCM_4106_ENC_TEST_VECTORS
3714 .vecs = aes_gcm_rfc4106_dec_tv_template,
3715 .count = AES_GCM_4106_DEC_TEST_VECTORS
3720 .alg = "rfc4309(ccm(aes))",
3721 .test = alg_test_aead,
3726 .vecs = aes_ccm_rfc4309_enc_tv_template,
3727 .count = AES_CCM_4309_ENC_TEST_VECTORS
3730 .vecs = aes_ccm_rfc4309_dec_tv_template,
3731 .count = AES_CCM_4309_DEC_TEST_VECTORS
3736 .alg = "rfc4543(gcm(aes))",
3737 .test = alg_test_aead,
3741 .vecs = aes_gcm_rfc4543_enc_tv_template,
3742 .count = AES_GCM_4543_ENC_TEST_VECTORS
3745 .vecs = aes_gcm_rfc4543_dec_tv_template,
3746 .count = AES_GCM_4543_DEC_TEST_VECTORS
3751 .alg = "rfc7539(chacha20,poly1305)",
3752 .test = alg_test_aead,
3756 .vecs = rfc7539_enc_tv_template,
3757 .count = RFC7539_ENC_TEST_VECTORS
3760 .vecs = rfc7539_dec_tv_template,
3761 .count = RFC7539_DEC_TEST_VECTORS
3766 .alg = "rfc7539esp(chacha20,poly1305)",
3767 .test = alg_test_aead,
3771 .vecs = rfc7539esp_enc_tv_template,
3772 .count = RFC7539ESP_ENC_TEST_VECTORS
3775 .vecs = rfc7539esp_dec_tv_template,
3776 .count = RFC7539ESP_DEC_TEST_VECTORS
3782 .test = alg_test_hash,
3785 .vecs = rmd128_tv_template,
3786 .count = RMD128_TEST_VECTORS
3791 .test = alg_test_hash,
3794 .vecs = rmd160_tv_template,
3795 .count = RMD160_TEST_VECTORS
3800 .test = alg_test_hash,
3803 .vecs = rmd256_tv_template,
3804 .count = RMD256_TEST_VECTORS
3809 .test = alg_test_hash,
3812 .vecs = rmd320_tv_template,
3813 .count = RMD320_TEST_VECTORS
3818 .test = alg_test_akcipher,
3822 .vecs = rsa_tv_template,
3823 .count = RSA_TEST_VECTORS
3828 .test = alg_test_skcipher,
3832 .vecs = salsa20_stream_enc_tv_template,
3833 .count = SALSA20_STREAM_ENC_TEST_VECTORS
3839 .test = alg_test_hash,
3843 .vecs = sha1_tv_template,
3844 .count = SHA1_TEST_VECTORS
3849 .test = alg_test_hash,
3853 .vecs = sha224_tv_template,
3854 .count = SHA224_TEST_VECTORS
3859 .test = alg_test_hash,
3863 .vecs = sha256_tv_template,
3864 .count = SHA256_TEST_VECTORS
3869 .test = alg_test_hash,
3873 .vecs = sha3_224_tv_template,
3874 .count = SHA3_224_TEST_VECTORS
3879 .test = alg_test_hash,
3883 .vecs = sha3_256_tv_template,
3884 .count = SHA3_256_TEST_VECTORS
3889 .test = alg_test_hash,
3893 .vecs = sha3_384_tv_template,
3894 .count = SHA3_384_TEST_VECTORS
3899 .test = alg_test_hash,
3903 .vecs = sha3_512_tv_template,
3904 .count = SHA3_512_TEST_VECTORS
3909 .test = alg_test_hash,
3913 .vecs = sha384_tv_template,
3914 .count = SHA384_TEST_VECTORS
3919 .test = alg_test_hash,
3923 .vecs = sha512_tv_template,
3924 .count = SHA512_TEST_VECTORS
3929 .test = alg_test_hash,
3932 .vecs = tgr128_tv_template,
3933 .count = TGR128_TEST_VECTORS
3938 .test = alg_test_hash,
3941 .vecs = tgr160_tv_template,
3942 .count = TGR160_TEST_VECTORS
3947 .test = alg_test_hash,
3950 .vecs = tgr192_tv_template,
3951 .count = TGR192_TEST_VECTORS
3956 .test = alg_test_hash,
3959 .vecs = aes_vmac128_tv_template,
3960 .count = VMAC_AES_TEST_VECTORS
3965 .test = alg_test_hash,
3968 .vecs = wp256_tv_template,
3969 .count = WP256_TEST_VECTORS
3974 .test = alg_test_hash,
3977 .vecs = wp384_tv_template,
3978 .count = WP384_TEST_VECTORS
3983 .test = alg_test_hash,
3986 .vecs = wp512_tv_template,
3987 .count = WP512_TEST_VECTORS
3992 .test = alg_test_hash,
3995 .vecs = aes_xcbc128_tv_template,
3996 .count = XCBC_AES_TEST_VECTORS
4001 .test = alg_test_skcipher,
4006 .vecs = aes_xts_enc_tv_template,
4007 .count = AES_XTS_ENC_TEST_VECTORS
4010 .vecs = aes_xts_dec_tv_template,
4011 .count = AES_XTS_DEC_TEST_VECTORS
4016 .alg = "xts(camellia)",
4017 .test = alg_test_skcipher,
4021 .vecs = camellia_xts_enc_tv_template,
4022 .count = CAMELLIA_XTS_ENC_TEST_VECTORS
4025 .vecs = camellia_xts_dec_tv_template,
4026 .count = CAMELLIA_XTS_DEC_TEST_VECTORS
4031 .alg = "xts(cast6)",
4032 .test = alg_test_skcipher,
4036 .vecs = cast6_xts_enc_tv_template,
4037 .count = CAST6_XTS_ENC_TEST_VECTORS
4040 .vecs = cast6_xts_dec_tv_template,
4041 .count = CAST6_XTS_DEC_TEST_VECTORS
4046 .alg = "xts(serpent)",
4047 .test = alg_test_skcipher,
4051 .vecs = serpent_xts_enc_tv_template,
4052 .count = SERPENT_XTS_ENC_TEST_VECTORS
4055 .vecs = serpent_xts_dec_tv_template,
4056 .count = SERPENT_XTS_DEC_TEST_VECTORS
4061 .alg = "xts(twofish)",
4062 .test = alg_test_skcipher,
4066 .vecs = tf_xts_enc_tv_template,
4067 .count = TF_XTS_ENC_TEST_VECTORS
4070 .vecs = tf_xts_dec_tv_template,
4071 .count = TF_XTS_DEC_TEST_VECTORS
4078 static bool alg_test_descs_checked;
4080 static void alg_test_descs_check_order(void)
4084 /* only check once */
4085 if (alg_test_descs_checked)
4088 alg_test_descs_checked = true;
4090 for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
4091 int diff = strcmp(alg_test_descs[i - 1].alg,
4092 alg_test_descs[i].alg);
4094 if (WARN_ON(diff > 0)) {
4095 pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
4096 alg_test_descs[i - 1].alg,
4097 alg_test_descs[i].alg);
4100 if (WARN_ON(diff == 0)) {
4101 pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
4102 alg_test_descs[i].alg);
4107 static int alg_find_test(const char *alg)
4110 int end = ARRAY_SIZE(alg_test_descs);
4112 while (start < end) {
4113 int i = (start + end) / 2;
4114 int diff = strcmp(alg_test_descs[i].alg, alg);
4132 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
4138 if (!fips_enabled && notests) {
4139 printk_once(KERN_INFO "alg: self-tests disabled\n");
4143 alg_test_descs_check_order();
4145 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
4146 char nalg[CRYPTO_MAX_ALG_NAME];
4148 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
4150 return -ENAMETOOLONG;
4152 i = alg_find_test(nalg);
4156 if (fips_enabled && !alg_test_descs[i].fips_allowed)
4159 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
4163 i = alg_find_test(alg);
4164 j = alg_find_test(driver);
4168 if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
4169 (j >= 0 && !alg_test_descs[j].fips_allowed)))
4174 rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
4176 if (j >= 0 && j != i)
4177 rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
4181 if (fips_enabled && rc)
4182 panic("%s: %s alg self test failed in fips mode!\n", driver, alg);
4184 if (fips_enabled && !rc)
4185 pr_info("alg: self-tests for %s (%s) passed\n", driver, alg);
4190 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
4196 #endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
4198 EXPORT_SYMBOL_GPL(alg_test);