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 aead_test_suite {
81 const struct aead_testvec *vecs;
86 struct cipher_test_suite {
88 const struct cipher_testvec *vecs;
93 struct comp_test_suite {
95 const struct comp_testvec *vecs;
100 struct hash_test_suite {
101 const struct hash_testvec *vecs;
105 struct cprng_test_suite {
106 const struct cprng_testvec *vecs;
110 struct drbg_test_suite {
111 const struct drbg_testvec *vecs;
115 struct akcipher_test_suite {
116 const struct akcipher_testvec *vecs;
120 struct kpp_test_suite {
121 const struct kpp_testvec *vecs;
125 struct alg_test_desc {
127 int (*test)(const struct alg_test_desc *desc, const char *driver,
129 int fips_allowed; /* set if alg is allowed in fips mode */
132 struct aead_test_suite aead;
133 struct cipher_test_suite cipher;
134 struct comp_test_suite comp;
135 struct hash_test_suite hash;
136 struct cprng_test_suite cprng;
137 struct drbg_test_suite drbg;
138 struct akcipher_test_suite akcipher;
139 struct kpp_test_suite kpp;
143 static const unsigned int IDX[8] = {
144 IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
146 static void hexdump(unsigned char *buf, unsigned int len)
148 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
153 static int testmgr_alloc_buf(char *buf[XBUFSIZE])
157 for (i = 0; i < XBUFSIZE; i++) {
158 buf[i] = (void *)__get_free_page(GFP_KERNEL);
167 free_page((unsigned long)buf[i]);
172 static void testmgr_free_buf(char *buf[XBUFSIZE])
176 for (i = 0; i < XBUFSIZE; i++)
177 free_page((unsigned long)buf[i]);
180 static int ahash_guard_result(char *result, char c, int size)
184 for (i = 0; i < size; i++) {
192 static int ahash_partial_update(struct ahash_request **preq,
193 struct crypto_ahash *tfm, const struct hash_testvec *template,
194 void *hash_buff, int k, int temp, struct scatterlist *sg,
195 const char *algo, char *result, struct crypto_wait *wait)
198 struct ahash_request *req;
199 int statesize, ret = -EINVAL;
200 static const unsigned char guard[] = { 0x00, 0xba, 0xad, 0x00 };
201 int digestsize = crypto_ahash_digestsize(tfm);
204 statesize = crypto_ahash_statesize(
205 crypto_ahash_reqtfm(req));
206 state = kmalloc(statesize + sizeof(guard), GFP_KERNEL);
208 pr_err("alg: hash: Failed to alloc state for %s\n", algo);
211 memcpy(state + statesize, guard, sizeof(guard));
212 memset(result, 1, digestsize);
213 ret = crypto_ahash_export(req, state);
214 WARN_ON(memcmp(state + statesize, guard, sizeof(guard)));
216 pr_err("alg: hash: Failed to export() for %s\n", algo);
219 ret = ahash_guard_result(result, 1, digestsize);
221 pr_err("alg: hash: Failed, export used req->result for %s\n",
225 ahash_request_free(req);
226 req = ahash_request_alloc(tfm, GFP_KERNEL);
228 pr_err("alg: hash: Failed to alloc request for %s\n", algo);
231 ahash_request_set_callback(req,
232 CRYPTO_TFM_REQ_MAY_BACKLOG,
233 crypto_req_done, wait);
235 memcpy(hash_buff, template->plaintext + temp,
237 sg_init_one(&sg[0], hash_buff, template->tap[k]);
238 ahash_request_set_crypt(req, sg, result, template->tap[k]);
239 ret = crypto_ahash_import(req, state);
241 pr_err("alg: hash: Failed to import() for %s\n", algo);
244 ret = ahash_guard_result(result, 1, digestsize);
246 pr_err("alg: hash: Failed, import used req->result for %s\n",
250 ret = crypto_wait_req(crypto_ahash_update(req), wait);
257 ahash_request_free(req);
264 static int __test_hash(struct crypto_ahash *tfm,
265 const struct hash_testvec *template, unsigned int tcount,
266 bool use_digest, const int align_offset)
268 const char *algo = crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
269 size_t digest_size = crypto_ahash_digestsize(tfm);
270 unsigned int i, j, k, temp;
271 struct scatterlist sg[8];
274 struct ahash_request *req;
275 struct crypto_wait wait;
277 char *xbuf[XBUFSIZE];
280 result = kmalloc(digest_size, GFP_KERNEL);
283 key = kmalloc(MAX_KEYLEN, GFP_KERNEL);
286 if (testmgr_alloc_buf(xbuf))
289 crypto_init_wait(&wait);
291 req = ahash_request_alloc(tfm, GFP_KERNEL);
293 printk(KERN_ERR "alg: hash: Failed to allocate request for "
297 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
298 crypto_req_done, &wait);
301 for (i = 0; i < tcount; i++) {
306 if (WARN_ON(align_offset + template[i].psize > PAGE_SIZE))
310 memset(result, 0, digest_size);
313 hash_buff += align_offset;
315 memcpy(hash_buff, template[i].plaintext, template[i].psize);
316 sg_init_one(&sg[0], hash_buff, template[i].psize);
318 if (template[i].ksize) {
319 crypto_ahash_clear_flags(tfm, ~0);
320 if (template[i].ksize > MAX_KEYLEN) {
321 pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
322 j, algo, template[i].ksize, MAX_KEYLEN);
326 memcpy(key, template[i].key, template[i].ksize);
327 ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
329 printk(KERN_ERR "alg: hash: setkey failed on "
330 "test %d for %s: ret=%d\n", j, algo,
336 ahash_request_set_crypt(req, sg, result, template[i].psize);
338 ret = crypto_wait_req(crypto_ahash_digest(req), &wait);
340 pr_err("alg: hash: digest failed on test %d "
341 "for %s: ret=%d\n", j, algo, -ret);
345 memset(result, 1, digest_size);
346 ret = crypto_wait_req(crypto_ahash_init(req), &wait);
348 pr_err("alg: hash: init failed on test %d "
349 "for %s: ret=%d\n", j, algo, -ret);
352 ret = ahash_guard_result(result, 1, digest_size);
354 pr_err("alg: hash: init failed on test %d "
355 "for %s: used req->result\n", j, algo);
358 ret = crypto_wait_req(crypto_ahash_update(req), &wait);
360 pr_err("alg: hash: update failed on test %d "
361 "for %s: ret=%d\n", j, algo, -ret);
364 ret = ahash_guard_result(result, 1, digest_size);
366 pr_err("alg: hash: update failed on test %d "
367 "for %s: used req->result\n", j, algo);
370 ret = crypto_wait_req(crypto_ahash_final(req), &wait);
372 pr_err("alg: hash: final failed on test %d "
373 "for %s: ret=%d\n", j, algo, -ret);
378 if (memcmp(result, template[i].digest,
379 crypto_ahash_digestsize(tfm))) {
380 printk(KERN_ERR "alg: hash: Test %d failed for %s\n",
382 hexdump(result, crypto_ahash_digestsize(tfm));
389 for (i = 0; i < tcount; i++) {
390 /* alignment tests are only done with continuous buffers */
391 if (align_offset != 0)
398 memset(result, 0, digest_size);
401 sg_init_table(sg, template[i].np);
403 for (k = 0; k < template[i].np; k++) {
404 if (WARN_ON(offset_in_page(IDX[k]) +
405 template[i].tap[k] > PAGE_SIZE))
408 memcpy(xbuf[IDX[k] >> PAGE_SHIFT] +
409 offset_in_page(IDX[k]),
410 template[i].plaintext + temp,
413 temp += template[i].tap[k];
416 if (template[i].ksize) {
417 if (template[i].ksize > MAX_KEYLEN) {
418 pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
419 j, algo, template[i].ksize, MAX_KEYLEN);
423 crypto_ahash_clear_flags(tfm, ~0);
424 memcpy(key, template[i].key, template[i].ksize);
425 ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
428 printk(KERN_ERR "alg: hash: setkey "
429 "failed on chunking test %d "
430 "for %s: ret=%d\n", j, algo, -ret);
435 ahash_request_set_crypt(req, sg, result, template[i].psize);
436 ret = crypto_wait_req(crypto_ahash_digest(req), &wait);
438 pr_err("alg: hash: digest failed on chunking test %d for %s: ret=%d\n",
443 if (memcmp(result, template[i].digest,
444 crypto_ahash_digestsize(tfm))) {
445 printk(KERN_ERR "alg: hash: Chunking test %d "
446 "failed for %s\n", j, algo);
447 hexdump(result, crypto_ahash_digestsize(tfm));
453 /* partial update exercise */
455 for (i = 0; i < tcount; i++) {
456 /* alignment tests are only done with continuous buffers */
457 if (align_offset != 0)
460 if (template[i].np < 2)
464 memset(result, 0, digest_size);
468 memcpy(hash_buff, template[i].plaintext,
470 sg_init_one(&sg[0], hash_buff, template[i].tap[0]);
472 if (template[i].ksize) {
473 crypto_ahash_clear_flags(tfm, ~0);
474 if (template[i].ksize > MAX_KEYLEN) {
475 pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
476 j, algo, template[i].ksize, MAX_KEYLEN);
480 memcpy(key, template[i].key, template[i].ksize);
481 ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
483 pr_err("alg: hash: setkey failed on test %d for %s: ret=%d\n",
489 ahash_request_set_crypt(req, sg, result, template[i].tap[0]);
490 ret = crypto_wait_req(crypto_ahash_init(req), &wait);
492 pr_err("alg: hash: init failed on test %d for %s: ret=%d\n",
496 ret = crypto_wait_req(crypto_ahash_update(req), &wait);
498 pr_err("alg: hash: update failed on test %d for %s: ret=%d\n",
503 temp = template[i].tap[0];
504 for (k = 1; k < template[i].np; k++) {
505 ret = ahash_partial_update(&req, tfm, &template[i],
506 hash_buff, k, temp, &sg[0], algo, result,
509 pr_err("alg: hash: partial update failed on test %d for %s: ret=%d\n",
513 temp += template[i].tap[k];
515 ret = crypto_wait_req(crypto_ahash_final(req), &wait);
517 pr_err("alg: hash: final failed on test %d for %s: ret=%d\n",
521 if (memcmp(result, template[i].digest,
522 crypto_ahash_digestsize(tfm))) {
523 pr_err("alg: hash: Partial Test %d failed for %s\n",
525 hexdump(result, crypto_ahash_digestsize(tfm));
534 ahash_request_free(req);
536 testmgr_free_buf(xbuf);
543 static int test_hash(struct crypto_ahash *tfm,
544 const struct hash_testvec *template,
545 unsigned int tcount, bool use_digest)
547 unsigned int alignmask;
550 ret = __test_hash(tfm, template, tcount, use_digest, 0);
554 /* test unaligned buffers, check with one byte offset */
555 ret = __test_hash(tfm, template, tcount, use_digest, 1);
559 alignmask = crypto_tfm_alg_alignmask(&tfm->base);
561 /* Check if alignment mask for tfm is correctly set. */
562 ret = __test_hash(tfm, template, tcount, use_digest,
571 static int __test_aead(struct crypto_aead *tfm, int enc,
572 const struct aead_testvec *template, unsigned int tcount,
573 const bool diff_dst, const int align_offset)
575 const char *algo = crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm));
576 unsigned int i, j, k, n, temp;
580 struct aead_request *req;
581 struct scatterlist *sg;
582 struct scatterlist *sgout;
584 struct crypto_wait wait;
585 unsigned int authsize, iv_len;
590 char *xbuf[XBUFSIZE];
591 char *xoutbuf[XBUFSIZE];
592 char *axbuf[XBUFSIZE];
594 iv = kzalloc(MAX_IVLEN, GFP_KERNEL);
597 key = kmalloc(MAX_KEYLEN, GFP_KERNEL);
600 if (testmgr_alloc_buf(xbuf))
602 if (testmgr_alloc_buf(axbuf))
604 if (diff_dst && testmgr_alloc_buf(xoutbuf))
607 /* avoid "the frame size is larger than 1024 bytes" compiler warning */
608 sg = kmalloc(sizeof(*sg) * 8 * (diff_dst ? 4 : 2), GFP_KERNEL);
623 crypto_init_wait(&wait);
625 req = aead_request_alloc(tfm, GFP_KERNEL);
627 pr_err("alg: aead%s: Failed to allocate request for %s\n",
632 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
633 crypto_req_done, &wait);
635 iv_len = crypto_aead_ivsize(tfm);
637 for (i = 0, j = 0; i < tcount; i++) {
643 /* some templates have no input data but they will
647 input += align_offset;
651 if (WARN_ON(align_offset + template[i].ilen >
652 PAGE_SIZE || template[i].alen > PAGE_SIZE))
655 memcpy(input, template[i].input, template[i].ilen);
656 memcpy(assoc, template[i].assoc, template[i].alen);
658 memcpy(iv, template[i].iv, iv_len);
660 memset(iv, 0, iv_len);
662 crypto_aead_clear_flags(tfm, ~0);
664 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
666 if (template[i].klen > MAX_KEYLEN) {
667 pr_err("alg: aead%s: setkey failed on test %d for %s: key size %d > %d\n",
668 d, j, algo, template[i].klen,
673 memcpy(key, template[i].key, template[i].klen);
675 ret = crypto_aead_setkey(tfm, key, template[i].klen);
676 if (template[i].fail == !ret) {
677 pr_err("alg: aead%s: setkey failed on test %d for %s: flags=%x\n",
678 d, j, algo, crypto_aead_get_flags(tfm));
683 authsize = abs(template[i].rlen - template[i].ilen);
684 ret = crypto_aead_setauthsize(tfm, authsize);
686 pr_err("alg: aead%s: Failed to set authsize to %u on test %d for %s\n",
687 d, authsize, j, algo);
691 k = !!template[i].alen;
692 sg_init_table(sg, k + 1);
693 sg_set_buf(&sg[0], assoc, template[i].alen);
694 sg_set_buf(&sg[k], input,
695 template[i].ilen + (enc ? authsize : 0));
699 sg_init_table(sgout, k + 1);
700 sg_set_buf(&sgout[0], assoc, template[i].alen);
703 output += align_offset;
704 sg_set_buf(&sgout[k], output,
705 template[i].rlen + (enc ? 0 : authsize));
708 aead_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
709 template[i].ilen, iv);
711 aead_request_set_ad(req, template[i].alen);
713 ret = crypto_wait_req(enc ? crypto_aead_encrypt(req)
714 : crypto_aead_decrypt(req), &wait);
718 if (template[i].novrfy) {
719 /* verification was supposed to fail */
720 pr_err("alg: aead%s: %s failed on test %d for %s: ret was 0, expected -EBADMSG\n",
722 /* so really, we got a bad message */
728 if (template[i].novrfy)
729 /* verification failure was expected */
733 pr_err("alg: aead%s: %s failed on test %d for %s: ret=%d\n",
734 d, e, j, algo, -ret);
739 if (memcmp(q, template[i].result, template[i].rlen)) {
740 pr_err("alg: aead%s: Test %d failed on %s for %s\n",
742 hexdump(q, template[i].rlen);
748 for (i = 0, j = 0; i < tcount; i++) {
749 /* alignment tests are only done with continuous buffers */
750 if (align_offset != 0)
759 memcpy(iv, template[i].iv, iv_len);
761 memset(iv, 0, MAX_IVLEN);
763 crypto_aead_clear_flags(tfm, ~0);
765 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
766 if (template[i].klen > MAX_KEYLEN) {
767 pr_err("alg: aead%s: setkey failed on test %d for %s: key size %d > %d\n",
768 d, j, algo, template[i].klen, MAX_KEYLEN);
772 memcpy(key, template[i].key, template[i].klen);
774 ret = crypto_aead_setkey(tfm, key, template[i].klen);
775 if (template[i].fail == !ret) {
776 pr_err("alg: aead%s: setkey failed on chunk test %d for %s: flags=%x\n",
777 d, j, algo, crypto_aead_get_flags(tfm));
782 authsize = abs(template[i].rlen - template[i].ilen);
785 sg_init_table(sg, template[i].anp + template[i].np);
787 sg_init_table(sgout, template[i].anp + template[i].np);
790 for (k = 0, temp = 0; k < template[i].anp; k++) {
791 if (WARN_ON(offset_in_page(IDX[k]) +
792 template[i].atap[k] > PAGE_SIZE))
795 memcpy(axbuf[IDX[k] >> PAGE_SHIFT] +
796 offset_in_page(IDX[k]),
797 template[i].assoc + temp,
798 template[i].atap[k]),
799 template[i].atap[k]);
801 sg_set_buf(&sgout[k],
802 axbuf[IDX[k] >> PAGE_SHIFT] +
803 offset_in_page(IDX[k]),
804 template[i].atap[k]);
805 temp += template[i].atap[k];
808 for (k = 0, temp = 0; k < template[i].np; k++) {
809 if (WARN_ON(offset_in_page(IDX[k]) +
810 template[i].tap[k] > PAGE_SIZE))
813 q = xbuf[IDX[k] >> PAGE_SHIFT] + offset_in_page(IDX[k]);
814 memcpy(q, template[i].input + temp, template[i].tap[k]);
815 sg_set_buf(&sg[template[i].anp + k],
816 q, template[i].tap[k]);
819 q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
820 offset_in_page(IDX[k]);
822 memset(q, 0, template[i].tap[k]);
824 sg_set_buf(&sgout[template[i].anp + k],
825 q, template[i].tap[k]);
828 n = template[i].tap[k];
829 if (k == template[i].np - 1 && enc)
831 if (offset_in_page(q) + n < PAGE_SIZE)
834 temp += template[i].tap[k];
837 ret = crypto_aead_setauthsize(tfm, authsize);
839 pr_err("alg: aead%s: Failed to set authsize to %u on chunk test %d for %s\n",
840 d, authsize, j, algo);
845 if (WARN_ON(sg[template[i].anp + k - 1].offset +
846 sg[template[i].anp + k - 1].length +
847 authsize > PAGE_SIZE)) {
853 sgout[template[i].anp + k - 1].length +=
855 sg[template[i].anp + k - 1].length += authsize;
858 aead_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
862 aead_request_set_ad(req, template[i].alen);
864 ret = crypto_wait_req(enc ? crypto_aead_encrypt(req)
865 : crypto_aead_decrypt(req), &wait);
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 */
879 if (template[i].novrfy)
880 /* verification failure was expected */
884 pr_err("alg: aead%s: %s failed on chunk test %d for %s: ret=%d\n",
885 d, e, j, algo, -ret);
890 for (k = 0, temp = 0; k < template[i].np; k++) {
892 q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
893 offset_in_page(IDX[k]);
895 q = xbuf[IDX[k] >> PAGE_SHIFT] +
896 offset_in_page(IDX[k]);
898 n = template[i].tap[k];
899 if (k == template[i].np - 1)
900 n += enc ? authsize : -authsize;
902 if (memcmp(q, template[i].result + temp, n)) {
903 pr_err("alg: aead%s: Chunk test %d failed on %s at page %u for %s\n",
910 if (k == template[i].np - 1 && !enc) {
912 memcmp(q, template[i].input +
918 for (n = 0; offset_in_page(q + n) && q[n]; n++)
922 pr_err("alg: aead%s: Result buffer corruption in chunk test %d on %s at page %u for %s: %u bytes:\n",
923 d, j, e, k, algo, n);
928 temp += template[i].tap[k];
935 aead_request_free(req);
939 testmgr_free_buf(xoutbuf);
941 testmgr_free_buf(axbuf);
943 testmgr_free_buf(xbuf);
950 static int test_aead(struct crypto_aead *tfm, int enc,
951 const struct aead_testvec *template, unsigned int tcount)
953 unsigned int alignmask;
956 /* test 'dst == src' case */
957 ret = __test_aead(tfm, enc, template, tcount, false, 0);
961 /* test 'dst != src' case */
962 ret = __test_aead(tfm, enc, template, tcount, true, 0);
966 /* test unaligned buffers, check with one byte offset */
967 ret = __test_aead(tfm, enc, template, tcount, true, 1);
971 alignmask = crypto_tfm_alg_alignmask(&tfm->base);
973 /* Check if alignment mask for tfm is correctly set. */
974 ret = __test_aead(tfm, enc, template, tcount, true,
983 static int test_cipher(struct crypto_cipher *tfm, int enc,
984 const struct cipher_testvec *template,
987 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
988 unsigned int i, j, k;
992 char *xbuf[XBUFSIZE];
995 if (testmgr_alloc_buf(xbuf))
1004 for (i = 0; i < tcount; i++) {
1008 if (fips_enabled && template[i].fips_skip)
1014 if (WARN_ON(template[i].ilen > PAGE_SIZE))
1018 memcpy(data, template[i].input, template[i].ilen);
1020 crypto_cipher_clear_flags(tfm, ~0);
1022 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
1024 ret = crypto_cipher_setkey(tfm, template[i].key,
1026 if (template[i].fail == !ret) {
1027 printk(KERN_ERR "alg: cipher: setkey failed "
1028 "on test %d for %s: flags=%x\n", j,
1029 algo, crypto_cipher_get_flags(tfm));
1034 for (k = 0; k < template[i].ilen;
1035 k += crypto_cipher_blocksize(tfm)) {
1037 crypto_cipher_encrypt_one(tfm, data + k,
1040 crypto_cipher_decrypt_one(tfm, data + k,
1045 if (memcmp(q, template[i].result, template[i].rlen)) {
1046 printk(KERN_ERR "alg: cipher: Test %d failed "
1047 "on %s for %s\n", j, e, algo);
1048 hexdump(q, template[i].rlen);
1057 testmgr_free_buf(xbuf);
1062 static int __test_skcipher(struct crypto_skcipher *tfm, int enc,
1063 const struct cipher_testvec *template,
1064 unsigned int tcount,
1065 const bool diff_dst, const int align_offset)
1068 crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm));
1069 unsigned int i, j, k, n, temp;
1071 struct skcipher_request *req;
1072 struct scatterlist sg[8];
1073 struct scatterlist sgout[8];
1075 struct crypto_wait wait;
1078 char *xbuf[XBUFSIZE];
1079 char *xoutbuf[XBUFSIZE];
1081 unsigned int ivsize = crypto_skcipher_ivsize(tfm);
1083 if (testmgr_alloc_buf(xbuf))
1086 if (diff_dst && testmgr_alloc_buf(xoutbuf))
1099 crypto_init_wait(&wait);
1101 req = skcipher_request_alloc(tfm, GFP_KERNEL);
1103 pr_err("alg: skcipher%s: Failed to allocate request for %s\n",
1108 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1109 crypto_req_done, &wait);
1112 for (i = 0; i < tcount; i++) {
1113 if (template[i].np && !template[i].also_non_np)
1116 if (fips_enabled && template[i].fips_skip)
1120 memcpy(iv, template[i].iv, ivsize);
1122 memset(iv, 0, MAX_IVLEN);
1126 if (WARN_ON(align_offset + template[i].ilen > PAGE_SIZE))
1130 data += align_offset;
1131 memcpy(data, template[i].input, template[i].ilen);
1133 crypto_skcipher_clear_flags(tfm, ~0);
1135 crypto_skcipher_set_flags(tfm,
1136 CRYPTO_TFM_REQ_WEAK_KEY);
1138 ret = crypto_skcipher_setkey(tfm, template[i].key,
1140 if (template[i].fail == !ret) {
1141 pr_err("alg: skcipher%s: setkey failed on test %d for %s: flags=%x\n",
1142 d, j, algo, crypto_skcipher_get_flags(tfm));
1147 sg_init_one(&sg[0], data, template[i].ilen);
1150 data += align_offset;
1151 sg_init_one(&sgout[0], data, template[i].ilen);
1154 skcipher_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
1155 template[i].ilen, iv);
1156 ret = crypto_wait_req(enc ? crypto_skcipher_encrypt(req) :
1157 crypto_skcipher_decrypt(req), &wait);
1160 pr_err("alg: skcipher%s: %s failed on test %d for %s: ret=%d\n",
1161 d, e, j, algo, -ret);
1166 if (memcmp(q, template[i].result, template[i].rlen)) {
1167 pr_err("alg: skcipher%s: Test %d failed (invalid result) on %s for %s\n",
1169 hexdump(q, template[i].rlen);
1174 if (template[i].iv_out &&
1175 memcmp(iv, template[i].iv_out,
1176 crypto_skcipher_ivsize(tfm))) {
1177 pr_err("alg: skcipher%s: Test %d failed (invalid output IV) on %s for %s\n",
1179 hexdump(iv, crypto_skcipher_ivsize(tfm));
1186 for (i = 0; i < tcount; i++) {
1187 /* alignment tests are only done with continuous buffers */
1188 if (align_offset != 0)
1191 if (!template[i].np)
1194 if (fips_enabled && template[i].fips_skip)
1198 memcpy(iv, template[i].iv, ivsize);
1200 memset(iv, 0, MAX_IVLEN);
1203 crypto_skcipher_clear_flags(tfm, ~0);
1205 crypto_skcipher_set_flags(tfm,
1206 CRYPTO_TFM_REQ_WEAK_KEY);
1208 ret = crypto_skcipher_setkey(tfm, template[i].key,
1210 if (template[i].fail == !ret) {
1211 pr_err("alg: skcipher%s: setkey failed on chunk test %d for %s: flags=%x\n",
1212 d, j, algo, crypto_skcipher_get_flags(tfm));
1219 sg_init_table(sg, template[i].np);
1221 sg_init_table(sgout, template[i].np);
1222 for (k = 0; k < template[i].np; k++) {
1223 if (WARN_ON(offset_in_page(IDX[k]) +
1224 template[i].tap[k] > PAGE_SIZE))
1227 q = xbuf[IDX[k] >> PAGE_SHIFT] + offset_in_page(IDX[k]);
1229 memcpy(q, template[i].input + temp, template[i].tap[k]);
1231 if (offset_in_page(q) + template[i].tap[k] < PAGE_SIZE)
1232 q[template[i].tap[k]] = 0;
1234 sg_set_buf(&sg[k], q, template[i].tap[k]);
1236 q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
1237 offset_in_page(IDX[k]);
1239 sg_set_buf(&sgout[k], q, template[i].tap[k]);
1241 memset(q, 0, template[i].tap[k]);
1242 if (offset_in_page(q) +
1243 template[i].tap[k] < PAGE_SIZE)
1244 q[template[i].tap[k]] = 0;
1247 temp += template[i].tap[k];
1250 skcipher_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
1251 template[i].ilen, iv);
1253 ret = crypto_wait_req(enc ? crypto_skcipher_encrypt(req) :
1254 crypto_skcipher_decrypt(req), &wait);
1257 pr_err("alg: skcipher%s: %s failed on chunk test %d for %s: ret=%d\n",
1258 d, e, j, algo, -ret);
1264 for (k = 0; k < template[i].np; k++) {
1266 q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
1267 offset_in_page(IDX[k]);
1269 q = xbuf[IDX[k] >> PAGE_SHIFT] +
1270 offset_in_page(IDX[k]);
1272 if (memcmp(q, template[i].result + temp,
1273 template[i].tap[k])) {
1274 pr_err("alg: skcipher%s: Chunk test %d failed on %s at page %u for %s\n",
1276 hexdump(q, template[i].tap[k]);
1280 q += template[i].tap[k];
1281 for (n = 0; offset_in_page(q + n) && q[n]; n++)
1284 pr_err("alg: skcipher%s: Result buffer corruption in chunk test %d on %s at page %u for %s: %u bytes:\n",
1285 d, j, e, k, algo, n);
1289 temp += template[i].tap[k];
1296 skcipher_request_free(req);
1298 testmgr_free_buf(xoutbuf);
1300 testmgr_free_buf(xbuf);
1305 static int test_skcipher(struct crypto_skcipher *tfm, int enc,
1306 const struct cipher_testvec *template,
1307 unsigned int tcount)
1309 unsigned int alignmask;
1312 /* test 'dst == src' case */
1313 ret = __test_skcipher(tfm, enc, template, tcount, false, 0);
1317 /* test 'dst != src' case */
1318 ret = __test_skcipher(tfm, enc, template, tcount, true, 0);
1322 /* test unaligned buffers, check with one byte offset */
1323 ret = __test_skcipher(tfm, enc, template, tcount, true, 1);
1327 alignmask = crypto_tfm_alg_alignmask(&tfm->base);
1329 /* Check if alignment mask for tfm is correctly set. */
1330 ret = __test_skcipher(tfm, enc, template, tcount, true,
1339 static int test_comp(struct crypto_comp *tfm,
1340 const struct comp_testvec *ctemplate,
1341 const struct comp_testvec *dtemplate,
1342 int ctcount, int dtcount)
1344 const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
1345 char *output, *decomp_output;
1349 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1353 decomp_output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1354 if (!decomp_output) {
1359 for (i = 0; i < ctcount; i++) {
1361 unsigned int dlen = COMP_BUF_SIZE;
1363 memset(output, 0, sizeof(COMP_BUF_SIZE));
1364 memset(decomp_output, 0, sizeof(COMP_BUF_SIZE));
1366 ilen = ctemplate[i].inlen;
1367 ret = crypto_comp_compress(tfm, ctemplate[i].input,
1368 ilen, output, &dlen);
1370 printk(KERN_ERR "alg: comp: compression failed "
1371 "on test %d for %s: ret=%d\n", i + 1, algo,
1377 dlen = COMP_BUF_SIZE;
1378 ret = crypto_comp_decompress(tfm, output,
1379 ilen, decomp_output, &dlen);
1381 pr_err("alg: comp: compression failed: decompress: on test %d for %s failed: ret=%d\n",
1386 if (dlen != ctemplate[i].inlen) {
1387 printk(KERN_ERR "alg: comp: Compression test %d "
1388 "failed for %s: output len = %d\n", i + 1, algo,
1394 if (memcmp(decomp_output, ctemplate[i].input,
1395 ctemplate[i].inlen)) {
1396 pr_err("alg: comp: compression failed: output differs: on test %d for %s\n",
1398 hexdump(decomp_output, dlen);
1404 for (i = 0; i < dtcount; i++) {
1406 unsigned int dlen = COMP_BUF_SIZE;
1408 memset(decomp_output, 0, sizeof(COMP_BUF_SIZE));
1410 ilen = dtemplate[i].inlen;
1411 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
1412 ilen, decomp_output, &dlen);
1414 printk(KERN_ERR "alg: comp: decompression failed "
1415 "on test %d for %s: ret=%d\n", i + 1, algo,
1420 if (dlen != dtemplate[i].outlen) {
1421 printk(KERN_ERR "alg: comp: Decompression test %d "
1422 "failed for %s: output len = %d\n", i + 1, algo,
1428 if (memcmp(decomp_output, dtemplate[i].output, dlen)) {
1429 printk(KERN_ERR "alg: comp: Decompression test %d "
1430 "failed for %s\n", i + 1, algo);
1431 hexdump(decomp_output, dlen);
1440 kfree(decomp_output);
1445 static int test_acomp(struct crypto_acomp *tfm,
1446 const struct comp_testvec *ctemplate,
1447 const struct comp_testvec *dtemplate,
1448 int ctcount, int dtcount)
1450 const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
1452 char *output, *decomp_out;
1454 struct scatterlist src, dst;
1455 struct acomp_req *req;
1456 struct crypto_wait wait;
1458 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1462 decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1468 for (i = 0; i < ctcount; i++) {
1469 unsigned int dlen = COMP_BUF_SIZE;
1470 int ilen = ctemplate[i].inlen;
1473 input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL);
1479 memset(output, 0, dlen);
1480 crypto_init_wait(&wait);
1481 sg_init_one(&src, input_vec, ilen);
1482 sg_init_one(&dst, output, dlen);
1484 req = acomp_request_alloc(tfm);
1486 pr_err("alg: acomp: request alloc failed for %s\n",
1493 acomp_request_set_params(req, &src, &dst, ilen, dlen);
1494 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1495 crypto_req_done, &wait);
1497 ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
1499 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
1502 acomp_request_free(req);
1507 dlen = COMP_BUF_SIZE;
1508 sg_init_one(&src, output, ilen);
1509 sg_init_one(&dst, decomp_out, dlen);
1510 crypto_init_wait(&wait);
1511 acomp_request_set_params(req, &src, &dst, ilen, dlen);
1513 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
1515 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
1518 acomp_request_free(req);
1522 if (req->dlen != ctemplate[i].inlen) {
1523 pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
1524 i + 1, algo, req->dlen);
1527 acomp_request_free(req);
1531 if (memcmp(input_vec, decomp_out, req->dlen)) {
1532 pr_err("alg: acomp: Compression test %d failed for %s\n",
1534 hexdump(output, req->dlen);
1537 acomp_request_free(req);
1542 acomp_request_free(req);
1545 for (i = 0; i < dtcount; i++) {
1546 unsigned int dlen = COMP_BUF_SIZE;
1547 int ilen = dtemplate[i].inlen;
1550 input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL);
1556 memset(output, 0, dlen);
1557 crypto_init_wait(&wait);
1558 sg_init_one(&src, input_vec, ilen);
1559 sg_init_one(&dst, output, dlen);
1561 req = acomp_request_alloc(tfm);
1563 pr_err("alg: acomp: request alloc failed for %s\n",
1570 acomp_request_set_params(req, &src, &dst, ilen, dlen);
1571 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1572 crypto_req_done, &wait);
1574 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
1576 pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
1579 acomp_request_free(req);
1583 if (req->dlen != dtemplate[i].outlen) {
1584 pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
1585 i + 1, algo, req->dlen);
1588 acomp_request_free(req);
1592 if (memcmp(output, dtemplate[i].output, req->dlen)) {
1593 pr_err("alg: acomp: Decompression test %d failed for %s\n",
1595 hexdump(output, req->dlen);
1598 acomp_request_free(req);
1603 acomp_request_free(req);
1614 static int test_cprng(struct crypto_rng *tfm,
1615 const struct cprng_testvec *template,
1616 unsigned int tcount)
1618 const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
1619 int err = 0, i, j, seedsize;
1623 seedsize = crypto_rng_seedsize(tfm);
1625 seed = kmalloc(seedsize, GFP_KERNEL);
1627 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
1632 for (i = 0; i < tcount; i++) {
1633 memset(result, 0, 32);
1635 memcpy(seed, template[i].v, template[i].vlen);
1636 memcpy(seed + template[i].vlen, template[i].key,
1638 memcpy(seed + template[i].vlen + template[i].klen,
1639 template[i].dt, template[i].dtlen);
1641 err = crypto_rng_reset(tfm, seed, seedsize);
1643 printk(KERN_ERR "alg: cprng: Failed to reset rng "
1648 for (j = 0; j < template[i].loops; j++) {
1649 err = crypto_rng_get_bytes(tfm, result,
1652 printk(KERN_ERR "alg: cprng: Failed to obtain "
1653 "the correct amount of random data for "
1654 "%s (requested %d)\n", algo,
1660 err = memcmp(result, template[i].result,
1663 printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
1665 hexdump(result, template[i].rlen);
1676 static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
1679 struct crypto_aead *tfm;
1682 tfm = crypto_alloc_aead(driver, type, mask);
1684 printk(KERN_ERR "alg: aead: Failed to load transform for %s: "
1685 "%ld\n", driver, PTR_ERR(tfm));
1686 return PTR_ERR(tfm);
1689 if (desc->suite.aead.enc.vecs) {
1690 err = test_aead(tfm, ENCRYPT, desc->suite.aead.enc.vecs,
1691 desc->suite.aead.enc.count);
1696 if (!err && desc->suite.aead.dec.vecs)
1697 err = test_aead(tfm, DECRYPT, desc->suite.aead.dec.vecs,
1698 desc->suite.aead.dec.count);
1701 crypto_free_aead(tfm);
1705 static int alg_test_cipher(const struct alg_test_desc *desc,
1706 const char *driver, u32 type, u32 mask)
1708 struct crypto_cipher *tfm;
1711 tfm = crypto_alloc_cipher(driver, type, mask);
1713 printk(KERN_ERR "alg: cipher: Failed to load transform for "
1714 "%s: %ld\n", driver, PTR_ERR(tfm));
1715 return PTR_ERR(tfm);
1718 if (desc->suite.cipher.enc.vecs) {
1719 err = test_cipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
1720 desc->suite.cipher.enc.count);
1725 if (desc->suite.cipher.dec.vecs)
1726 err = test_cipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
1727 desc->suite.cipher.dec.count);
1730 crypto_free_cipher(tfm);
1734 static int alg_test_skcipher(const struct alg_test_desc *desc,
1735 const char *driver, u32 type, u32 mask)
1737 struct crypto_skcipher *tfm;
1740 tfm = crypto_alloc_skcipher(driver, type, mask);
1742 printk(KERN_ERR "alg: skcipher: Failed to load transform for "
1743 "%s: %ld\n", driver, PTR_ERR(tfm));
1744 return PTR_ERR(tfm);
1747 if (desc->suite.cipher.enc.vecs) {
1748 err = test_skcipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
1749 desc->suite.cipher.enc.count);
1754 if (desc->suite.cipher.dec.vecs)
1755 err = test_skcipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
1756 desc->suite.cipher.dec.count);
1759 crypto_free_skcipher(tfm);
1763 static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
1766 struct crypto_comp *comp;
1767 struct crypto_acomp *acomp;
1769 u32 algo_type = type & CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
1771 if (algo_type == CRYPTO_ALG_TYPE_ACOMPRESS) {
1772 acomp = crypto_alloc_acomp(driver, type, mask);
1773 if (IS_ERR(acomp)) {
1774 pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
1775 driver, PTR_ERR(acomp));
1776 return PTR_ERR(acomp);
1778 err = test_acomp(acomp, desc->suite.comp.comp.vecs,
1779 desc->suite.comp.decomp.vecs,
1780 desc->suite.comp.comp.count,
1781 desc->suite.comp.decomp.count);
1782 crypto_free_acomp(acomp);
1784 comp = crypto_alloc_comp(driver, type, mask);
1786 pr_err("alg: comp: Failed to load transform for %s: %ld\n",
1787 driver, PTR_ERR(comp));
1788 return PTR_ERR(comp);
1791 err = test_comp(comp, desc->suite.comp.comp.vecs,
1792 desc->suite.comp.decomp.vecs,
1793 desc->suite.comp.comp.count,
1794 desc->suite.comp.decomp.count);
1796 crypto_free_comp(comp);
1801 static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1804 struct crypto_ahash *tfm;
1807 tfm = crypto_alloc_ahash(driver, type, mask);
1809 printk(KERN_ERR "alg: hash: Failed to load transform for %s: "
1810 "%ld\n", driver, PTR_ERR(tfm));
1811 return PTR_ERR(tfm);
1814 err = test_hash(tfm, desc->suite.hash.vecs,
1815 desc->suite.hash.count, true);
1817 err = test_hash(tfm, desc->suite.hash.vecs,
1818 desc->suite.hash.count, false);
1820 crypto_free_ahash(tfm);
1824 static int alg_test_crc32c(const struct alg_test_desc *desc,
1825 const char *driver, u32 type, u32 mask)
1827 struct crypto_shash *tfm;
1831 err = alg_test_hash(desc, driver, type, mask);
1835 tfm = crypto_alloc_shash(driver, type, mask);
1837 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
1838 "%ld\n", driver, PTR_ERR(tfm));
1844 SHASH_DESC_ON_STACK(shash, tfm);
1845 u32 *ctx = (u32 *)shash_desc_ctx(shash);
1850 *ctx = le32_to_cpu(420553207);
1851 err = crypto_shash_final(shash, (u8 *)&val);
1853 printk(KERN_ERR "alg: crc32c: Operation failed for "
1854 "%s: %d\n", driver, err);
1858 if (val != ~420553207) {
1859 printk(KERN_ERR "alg: crc32c: Test failed for %s: "
1860 "%d\n", driver, val);
1865 crypto_free_shash(tfm);
1871 static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
1874 struct crypto_rng *rng;
1877 rng = crypto_alloc_rng(driver, type, mask);
1879 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
1880 "%ld\n", driver, PTR_ERR(rng));
1881 return PTR_ERR(rng);
1884 err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
1886 crypto_free_rng(rng);
1892 static int drbg_cavs_test(const struct drbg_testvec *test, int pr,
1893 const char *driver, u32 type, u32 mask)
1896 struct crypto_rng *drng;
1897 struct drbg_test_data test_data;
1898 struct drbg_string addtl, pers, testentropy;
1899 unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
1904 drng = crypto_alloc_rng(driver, type, mask);
1906 printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
1912 test_data.testentropy = &testentropy;
1913 drbg_string_fill(&testentropy, test->entropy, test->entropylen);
1914 drbg_string_fill(&pers, test->pers, test->perslen);
1915 ret = crypto_drbg_reset_test(drng, &pers, &test_data);
1917 printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
1921 drbg_string_fill(&addtl, test->addtla, test->addtllen);
1923 drbg_string_fill(&testentropy, test->entpra, test->entprlen);
1924 ret = crypto_drbg_get_bytes_addtl_test(drng,
1925 buf, test->expectedlen, &addtl, &test_data);
1927 ret = crypto_drbg_get_bytes_addtl(drng,
1928 buf, test->expectedlen, &addtl);
1931 printk(KERN_ERR "alg: drbg: could not obtain random data for "
1932 "driver %s\n", driver);
1936 drbg_string_fill(&addtl, test->addtlb, test->addtllen);
1938 drbg_string_fill(&testentropy, test->entprb, test->entprlen);
1939 ret = crypto_drbg_get_bytes_addtl_test(drng,
1940 buf, test->expectedlen, &addtl, &test_data);
1942 ret = crypto_drbg_get_bytes_addtl(drng,
1943 buf, test->expectedlen, &addtl);
1946 printk(KERN_ERR "alg: drbg: could not obtain random data for "
1947 "driver %s\n", driver);
1951 ret = memcmp(test->expected, buf, test->expectedlen);
1954 crypto_free_rng(drng);
1960 static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
1966 const struct drbg_testvec *template = desc->suite.drbg.vecs;
1967 unsigned int tcount = desc->suite.drbg.count;
1969 if (0 == memcmp(driver, "drbg_pr_", 8))
1972 for (i = 0; i < tcount; i++) {
1973 err = drbg_cavs_test(&template[i], pr, driver, type, mask);
1975 printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
1985 static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
1988 struct kpp_request *req;
1989 void *input_buf = NULL;
1990 void *output_buf = NULL;
1991 void *a_public = NULL;
1993 void *shared_secret = NULL;
1994 struct crypto_wait wait;
1995 unsigned int out_len_max;
1997 struct scatterlist src, dst;
1999 req = kpp_request_alloc(tfm, GFP_KERNEL);
2003 crypto_init_wait(&wait);
2005 err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
2009 out_len_max = crypto_kpp_maxsize(tfm);
2010 output_buf = kzalloc(out_len_max, GFP_KERNEL);
2016 /* Use appropriate parameter as base */
2017 kpp_request_set_input(req, NULL, 0);
2018 sg_init_one(&dst, output_buf, out_len_max);
2019 kpp_request_set_output(req, &dst, out_len_max);
2020 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2021 crypto_req_done, &wait);
2023 /* Compute party A's public key */
2024 err = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
2026 pr_err("alg: %s: Party A: generate public key test failed. err %d\n",
2032 /* Save party A's public key */
2033 a_public = kzalloc(out_len_max, GFP_KERNEL);
2038 memcpy(a_public, sg_virt(req->dst), out_len_max);
2040 /* Verify calculated public key */
2041 if (memcmp(vec->expected_a_public, sg_virt(req->dst),
2042 vec->expected_a_public_size)) {
2043 pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n",
2050 /* Calculate shared secret key by using counter part (b) public key. */
2051 input_buf = kzalloc(vec->b_public_size, GFP_KERNEL);
2057 memcpy(input_buf, vec->b_public, vec->b_public_size);
2058 sg_init_one(&src, input_buf, vec->b_public_size);
2059 sg_init_one(&dst, output_buf, out_len_max);
2060 kpp_request_set_input(req, &src, vec->b_public_size);
2061 kpp_request_set_output(req, &dst, out_len_max);
2062 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2063 crypto_req_done, &wait);
2064 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait);
2066 pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n",
2072 /* Save the shared secret obtained by party A */
2073 a_ss = kzalloc(vec->expected_ss_size, GFP_KERNEL);
2078 memcpy(a_ss, sg_virt(req->dst), vec->expected_ss_size);
2081 * Calculate party B's shared secret by using party A's
2084 err = crypto_kpp_set_secret(tfm, vec->b_secret,
2085 vec->b_secret_size);
2089 sg_init_one(&src, a_public, vec->expected_a_public_size);
2090 sg_init_one(&dst, output_buf, out_len_max);
2091 kpp_request_set_input(req, &src, vec->expected_a_public_size);
2092 kpp_request_set_output(req, &dst, out_len_max);
2093 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2094 crypto_req_done, &wait);
2095 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req),
2098 pr_err("alg: %s: Party B: compute shared secret failed. err %d\n",
2103 shared_secret = a_ss;
2105 shared_secret = (void *)vec->expected_ss;
2109 * verify shared secret from which the user will derive
2110 * secret key by executing whatever hash it has chosen
2112 if (memcmp(shared_secret, sg_virt(req->dst),
2113 vec->expected_ss_size)) {
2114 pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
2126 kpp_request_free(req);
2130 static int test_kpp(struct crypto_kpp *tfm, const char *alg,
2131 const struct kpp_testvec *vecs, unsigned int tcount)
2135 for (i = 0; i < tcount; i++) {
2136 ret = do_test_kpp(tfm, vecs++, alg);
2138 pr_err("alg: %s: test failed on vector %d, err=%d\n",
2146 static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
2149 struct crypto_kpp *tfm;
2152 tfm = crypto_alloc_kpp(driver, type, mask);
2154 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
2155 driver, PTR_ERR(tfm));
2156 return PTR_ERR(tfm);
2158 if (desc->suite.kpp.vecs)
2159 err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
2160 desc->suite.kpp.count);
2162 crypto_free_kpp(tfm);
2166 static int test_akcipher_one(struct crypto_akcipher *tfm,
2167 const struct akcipher_testvec *vecs)
2169 char *xbuf[XBUFSIZE];
2170 struct akcipher_request *req;
2171 void *outbuf_enc = NULL;
2172 void *outbuf_dec = NULL;
2173 struct crypto_wait wait;
2174 unsigned int out_len_max, out_len = 0;
2176 struct scatterlist src, dst, src_tab[2];
2178 if (testmgr_alloc_buf(xbuf))
2181 req = akcipher_request_alloc(tfm, GFP_KERNEL);
2185 crypto_init_wait(&wait);
2187 if (vecs->public_key_vec)
2188 err = crypto_akcipher_set_pub_key(tfm, vecs->key,
2191 err = crypto_akcipher_set_priv_key(tfm, vecs->key,
2197 out_len_max = crypto_akcipher_maxsize(tfm);
2198 outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
2202 if (WARN_ON(vecs->m_size > PAGE_SIZE))
2205 memcpy(xbuf[0], vecs->m, vecs->m_size);
2207 sg_init_table(src_tab, 2);
2208 sg_set_buf(&src_tab[0], xbuf[0], 8);
2209 sg_set_buf(&src_tab[1], xbuf[0] + 8, vecs->m_size - 8);
2210 sg_init_one(&dst, outbuf_enc, out_len_max);
2211 akcipher_request_set_crypt(req, src_tab, &dst, vecs->m_size,
2213 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2214 crypto_req_done, &wait);
2216 err = crypto_wait_req(vecs->siggen_sigver_test ?
2217 /* Run asymmetric signature generation */
2218 crypto_akcipher_sign(req) :
2219 /* Run asymmetric encrypt */
2220 crypto_akcipher_encrypt(req), &wait);
2222 pr_err("alg: akcipher: encrypt test failed. err %d\n", err);
2225 if (req->dst_len != vecs->c_size) {
2226 pr_err("alg: akcipher: encrypt test failed. Invalid output len\n");
2230 /* verify that encrypted message is equal to expected */
2231 if (memcmp(vecs->c, outbuf_enc, vecs->c_size)) {
2232 pr_err("alg: akcipher: encrypt test failed. Invalid output\n");
2233 hexdump(outbuf_enc, vecs->c_size);
2237 /* Don't invoke decrypt for vectors with public key */
2238 if (vecs->public_key_vec) {
2242 outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
2248 if (WARN_ON(vecs->c_size > PAGE_SIZE))
2251 memcpy(xbuf[0], vecs->c, vecs->c_size);
2253 sg_init_one(&src, xbuf[0], vecs->c_size);
2254 sg_init_one(&dst, outbuf_dec, out_len_max);
2255 crypto_init_wait(&wait);
2256 akcipher_request_set_crypt(req, &src, &dst, vecs->c_size, out_len_max);
2258 err = crypto_wait_req(vecs->siggen_sigver_test ?
2259 /* Run asymmetric signature verification */
2260 crypto_akcipher_verify(req) :
2261 /* Run asymmetric decrypt */
2262 crypto_akcipher_decrypt(req), &wait);
2264 pr_err("alg: akcipher: decrypt test failed. err %d\n", err);
2267 out_len = req->dst_len;
2268 if (out_len < vecs->m_size) {
2269 pr_err("alg: akcipher: decrypt test failed. "
2270 "Invalid output len %u\n", out_len);
2274 /* verify that decrypted message is equal to the original msg */
2275 if (memchr_inv(outbuf_dec, 0, out_len - vecs->m_size) ||
2276 memcmp(vecs->m, outbuf_dec + out_len - vecs->m_size,
2278 pr_err("alg: akcipher: decrypt test failed. Invalid output\n");
2279 hexdump(outbuf_dec, out_len);
2286 akcipher_request_free(req);
2288 testmgr_free_buf(xbuf);
2292 static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
2293 const struct akcipher_testvec *vecs,
2294 unsigned int tcount)
2297 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
2300 for (i = 0; i < tcount; i++) {
2301 ret = test_akcipher_one(tfm, vecs++);
2305 pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
2312 static int alg_test_akcipher(const struct alg_test_desc *desc,
2313 const char *driver, u32 type, u32 mask)
2315 struct crypto_akcipher *tfm;
2318 tfm = crypto_alloc_akcipher(driver, type, mask);
2320 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
2321 driver, PTR_ERR(tfm));
2322 return PTR_ERR(tfm);
2324 if (desc->suite.akcipher.vecs)
2325 err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
2326 desc->suite.akcipher.count);
2328 crypto_free_akcipher(tfm);
2332 static int alg_test_null(const struct alg_test_desc *desc,
2333 const char *driver, u32 type, u32 mask)
2338 #define __VECS(tv) { .vecs = tv, .count = ARRAY_SIZE(tv) }
2340 /* Please keep this list sorted by algorithm name. */
2341 static const struct alg_test_desc alg_test_descs[] = {
2343 .alg = "ansi_cprng",
2344 .test = alg_test_cprng,
2346 .cprng = __VECS(ansi_cprng_aes_tv_template)
2349 .alg = "authenc(hmac(md5),ecb(cipher_null))",
2350 .test = alg_test_aead,
2353 .enc = __VECS(hmac_md5_ecb_cipher_null_enc_tv_template),
2354 .dec = __VECS(hmac_md5_ecb_cipher_null_dec_tv_template)
2358 .alg = "authenc(hmac(sha1),cbc(aes))",
2359 .test = alg_test_aead,
2363 .enc = __VECS(hmac_sha1_aes_cbc_enc_tv_temp)
2367 .alg = "authenc(hmac(sha1),cbc(des))",
2368 .test = alg_test_aead,
2371 .enc = __VECS(hmac_sha1_des_cbc_enc_tv_temp)
2375 .alg = "authenc(hmac(sha1),cbc(des3_ede))",
2376 .test = alg_test_aead,
2380 .enc = __VECS(hmac_sha1_des3_ede_cbc_enc_tv_temp)
2384 .alg = "authenc(hmac(sha1),ctr(aes))",
2385 .test = alg_test_null,
2388 .alg = "authenc(hmac(sha1),ecb(cipher_null))",
2389 .test = alg_test_aead,
2392 .enc = __VECS(hmac_sha1_ecb_cipher_null_enc_tv_temp),
2393 .dec = __VECS(hmac_sha1_ecb_cipher_null_dec_tv_temp)
2397 .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
2398 .test = alg_test_null,
2401 .alg = "authenc(hmac(sha224),cbc(des))",
2402 .test = alg_test_aead,
2405 .enc = __VECS(hmac_sha224_des_cbc_enc_tv_temp)
2409 .alg = "authenc(hmac(sha224),cbc(des3_ede))",
2410 .test = alg_test_aead,
2414 .enc = __VECS(hmac_sha224_des3_ede_cbc_enc_tv_temp)
2418 .alg = "authenc(hmac(sha256),cbc(aes))",
2419 .test = alg_test_aead,
2423 .enc = __VECS(hmac_sha256_aes_cbc_enc_tv_temp)
2427 .alg = "authenc(hmac(sha256),cbc(des))",
2428 .test = alg_test_aead,
2431 .enc = __VECS(hmac_sha256_des_cbc_enc_tv_temp)
2435 .alg = "authenc(hmac(sha256),cbc(des3_ede))",
2436 .test = alg_test_aead,
2440 .enc = __VECS(hmac_sha256_des3_ede_cbc_enc_tv_temp)
2444 .alg = "authenc(hmac(sha256),ctr(aes))",
2445 .test = alg_test_null,
2448 .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
2449 .test = alg_test_null,
2452 .alg = "authenc(hmac(sha384),cbc(des))",
2453 .test = alg_test_aead,
2456 .enc = __VECS(hmac_sha384_des_cbc_enc_tv_temp)
2460 .alg = "authenc(hmac(sha384),cbc(des3_ede))",
2461 .test = alg_test_aead,
2465 .enc = __VECS(hmac_sha384_des3_ede_cbc_enc_tv_temp)
2469 .alg = "authenc(hmac(sha384),ctr(aes))",
2470 .test = alg_test_null,
2473 .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
2474 .test = alg_test_null,
2477 .alg = "authenc(hmac(sha512),cbc(aes))",
2479 .test = alg_test_aead,
2482 .enc = __VECS(hmac_sha512_aes_cbc_enc_tv_temp)
2486 .alg = "authenc(hmac(sha512),cbc(des))",
2487 .test = alg_test_aead,
2490 .enc = __VECS(hmac_sha512_des_cbc_enc_tv_temp)
2494 .alg = "authenc(hmac(sha512),cbc(des3_ede))",
2495 .test = alg_test_aead,
2499 .enc = __VECS(hmac_sha512_des3_ede_cbc_enc_tv_temp)
2503 .alg = "authenc(hmac(sha512),ctr(aes))",
2504 .test = alg_test_null,
2507 .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
2508 .test = alg_test_null,
2512 .test = alg_test_skcipher,
2516 .enc = __VECS(aes_cbc_enc_tv_template),
2517 .dec = __VECS(aes_cbc_dec_tv_template)
2521 .alg = "cbc(anubis)",
2522 .test = alg_test_skcipher,
2525 .enc = __VECS(anubis_cbc_enc_tv_template),
2526 .dec = __VECS(anubis_cbc_dec_tv_template)
2530 .alg = "cbc(blowfish)",
2531 .test = alg_test_skcipher,
2534 .enc = __VECS(bf_cbc_enc_tv_template),
2535 .dec = __VECS(bf_cbc_dec_tv_template)
2539 .alg = "cbc(camellia)",
2540 .test = alg_test_skcipher,
2543 .enc = __VECS(camellia_cbc_enc_tv_template),
2544 .dec = __VECS(camellia_cbc_dec_tv_template)
2548 .alg = "cbc(cast5)",
2549 .test = alg_test_skcipher,
2552 .enc = __VECS(cast5_cbc_enc_tv_template),
2553 .dec = __VECS(cast5_cbc_dec_tv_template)
2557 .alg = "cbc(cast6)",
2558 .test = alg_test_skcipher,
2561 .enc = __VECS(cast6_cbc_enc_tv_template),
2562 .dec = __VECS(cast6_cbc_dec_tv_template)
2567 .test = alg_test_skcipher,
2570 .enc = __VECS(des_cbc_enc_tv_template),
2571 .dec = __VECS(des_cbc_dec_tv_template)
2575 .alg = "cbc(des3_ede)",
2576 .test = alg_test_skcipher,
2580 .enc = __VECS(des3_ede_cbc_enc_tv_template),
2581 .dec = __VECS(des3_ede_cbc_dec_tv_template)
2585 /* Same as cbc(aes) except the key is stored in
2586 * hardware secure memory which we reference by index
2589 .test = alg_test_null,
2592 .alg = "cbc(serpent)",
2593 .test = alg_test_skcipher,
2596 .enc = __VECS(serpent_cbc_enc_tv_template),
2597 .dec = __VECS(serpent_cbc_dec_tv_template)
2601 .alg = "cbc(twofish)",
2602 .test = alg_test_skcipher,
2605 .enc = __VECS(tf_cbc_enc_tv_template),
2606 .dec = __VECS(tf_cbc_dec_tv_template)
2610 .alg = "cbcmac(aes)",
2612 .test = alg_test_hash,
2614 .hash = __VECS(aes_cbcmac_tv_template)
2618 .test = alg_test_aead,
2622 .enc = __VECS(aes_ccm_enc_tv_template),
2623 .dec = __VECS(aes_ccm_dec_tv_template)
2628 .test = alg_test_skcipher,
2631 .enc = __VECS(chacha20_enc_tv_template),
2632 .dec = __VECS(chacha20_enc_tv_template),
2638 .test = alg_test_hash,
2640 .hash = __VECS(aes_cmac128_tv_template)
2643 .alg = "cmac(des3_ede)",
2645 .test = alg_test_hash,
2647 .hash = __VECS(des3_ede_cmac64_tv_template)
2650 .alg = "compress_null",
2651 .test = alg_test_null,
2654 .test = alg_test_hash,
2656 .hash = __VECS(crc32_tv_template)
2660 .test = alg_test_crc32c,
2663 .hash = __VECS(crc32c_tv_template)
2667 .test = alg_test_hash,
2670 .hash = __VECS(crct10dif_tv_template)
2674 .test = alg_test_skcipher,
2678 .enc = __VECS(aes_ctr_enc_tv_template),
2679 .dec = __VECS(aes_ctr_dec_tv_template)
2683 .alg = "ctr(blowfish)",
2684 .test = alg_test_skcipher,
2687 .enc = __VECS(bf_ctr_enc_tv_template),
2688 .dec = __VECS(bf_ctr_dec_tv_template)
2692 .alg = "ctr(camellia)",
2693 .test = alg_test_skcipher,
2696 .enc = __VECS(camellia_ctr_enc_tv_template),
2697 .dec = __VECS(camellia_ctr_dec_tv_template)
2701 .alg = "ctr(cast5)",
2702 .test = alg_test_skcipher,
2705 .enc = __VECS(cast5_ctr_enc_tv_template),
2706 .dec = __VECS(cast5_ctr_dec_tv_template)
2710 .alg = "ctr(cast6)",
2711 .test = alg_test_skcipher,
2714 .enc = __VECS(cast6_ctr_enc_tv_template),
2715 .dec = __VECS(cast6_ctr_dec_tv_template)
2720 .test = alg_test_skcipher,
2723 .enc = __VECS(des_ctr_enc_tv_template),
2724 .dec = __VECS(des_ctr_dec_tv_template)
2728 .alg = "ctr(des3_ede)",
2729 .test = alg_test_skcipher,
2733 .enc = __VECS(des3_ede_ctr_enc_tv_template),
2734 .dec = __VECS(des3_ede_ctr_dec_tv_template)
2738 /* Same as ctr(aes) except the key is stored in
2739 * hardware secure memory which we reference by index
2742 .test = alg_test_null,
2745 .alg = "ctr(serpent)",
2746 .test = alg_test_skcipher,
2749 .enc = __VECS(serpent_ctr_enc_tv_template),
2750 .dec = __VECS(serpent_ctr_dec_tv_template)
2754 .alg = "ctr(twofish)",
2755 .test = alg_test_skcipher,
2758 .enc = __VECS(tf_ctr_enc_tv_template),
2759 .dec = __VECS(tf_ctr_dec_tv_template)
2763 .alg = "cts(cbc(aes))",
2764 .test = alg_test_skcipher,
2767 .enc = __VECS(cts_mode_enc_tv_template),
2768 .dec = __VECS(cts_mode_dec_tv_template)
2773 .test = alg_test_comp,
2777 .comp = __VECS(deflate_comp_tv_template),
2778 .decomp = __VECS(deflate_decomp_tv_template)
2783 .test = alg_test_kpp,
2786 .kpp = __VECS(dh_tv_template)
2789 .alg = "digest_null",
2790 .test = alg_test_null,
2792 .alg = "drbg_nopr_ctr_aes128",
2793 .test = alg_test_drbg,
2796 .drbg = __VECS(drbg_nopr_ctr_aes128_tv_template)
2799 .alg = "drbg_nopr_ctr_aes192",
2800 .test = alg_test_drbg,
2803 .drbg = __VECS(drbg_nopr_ctr_aes192_tv_template)
2806 .alg = "drbg_nopr_ctr_aes256",
2807 .test = alg_test_drbg,
2810 .drbg = __VECS(drbg_nopr_ctr_aes256_tv_template)
2814 * There is no need to specifically test the DRBG with every
2815 * backend cipher -- covered by drbg_nopr_hmac_sha256 test
2817 .alg = "drbg_nopr_hmac_sha1",
2819 .test = alg_test_null,
2821 .alg = "drbg_nopr_hmac_sha256",
2822 .test = alg_test_drbg,
2825 .drbg = __VECS(drbg_nopr_hmac_sha256_tv_template)
2828 /* covered by drbg_nopr_hmac_sha256 test */
2829 .alg = "drbg_nopr_hmac_sha384",
2831 .test = alg_test_null,
2833 .alg = "drbg_nopr_hmac_sha512",
2834 .test = alg_test_null,
2837 .alg = "drbg_nopr_sha1",
2839 .test = alg_test_null,
2841 .alg = "drbg_nopr_sha256",
2842 .test = alg_test_drbg,
2845 .drbg = __VECS(drbg_nopr_sha256_tv_template)
2848 /* covered by drbg_nopr_sha256 test */
2849 .alg = "drbg_nopr_sha384",
2851 .test = alg_test_null,
2853 .alg = "drbg_nopr_sha512",
2855 .test = alg_test_null,
2857 .alg = "drbg_pr_ctr_aes128",
2858 .test = alg_test_drbg,
2861 .drbg = __VECS(drbg_pr_ctr_aes128_tv_template)
2864 /* covered by drbg_pr_ctr_aes128 test */
2865 .alg = "drbg_pr_ctr_aes192",
2867 .test = alg_test_null,
2869 .alg = "drbg_pr_ctr_aes256",
2871 .test = alg_test_null,
2873 .alg = "drbg_pr_hmac_sha1",
2875 .test = alg_test_null,
2877 .alg = "drbg_pr_hmac_sha256",
2878 .test = alg_test_drbg,
2881 .drbg = __VECS(drbg_pr_hmac_sha256_tv_template)
2884 /* covered by drbg_pr_hmac_sha256 test */
2885 .alg = "drbg_pr_hmac_sha384",
2887 .test = alg_test_null,
2889 .alg = "drbg_pr_hmac_sha512",
2890 .test = alg_test_null,
2893 .alg = "drbg_pr_sha1",
2895 .test = alg_test_null,
2897 .alg = "drbg_pr_sha256",
2898 .test = alg_test_drbg,
2901 .drbg = __VECS(drbg_pr_sha256_tv_template)
2904 /* covered by drbg_pr_sha256 test */
2905 .alg = "drbg_pr_sha384",
2907 .test = alg_test_null,
2909 .alg = "drbg_pr_sha512",
2911 .test = alg_test_null,
2914 .test = alg_test_skcipher,
2918 .enc = __VECS(aes_enc_tv_template),
2919 .dec = __VECS(aes_dec_tv_template)
2923 .alg = "ecb(anubis)",
2924 .test = alg_test_skcipher,
2927 .enc = __VECS(anubis_enc_tv_template),
2928 .dec = __VECS(anubis_dec_tv_template)
2933 .test = alg_test_skcipher,
2936 .enc = __VECS(arc4_enc_tv_template),
2937 .dec = __VECS(arc4_dec_tv_template)
2941 .alg = "ecb(blowfish)",
2942 .test = alg_test_skcipher,
2945 .enc = __VECS(bf_enc_tv_template),
2946 .dec = __VECS(bf_dec_tv_template)
2950 .alg = "ecb(camellia)",
2951 .test = alg_test_skcipher,
2954 .enc = __VECS(camellia_enc_tv_template),
2955 .dec = __VECS(camellia_dec_tv_template)
2959 .alg = "ecb(cast5)",
2960 .test = alg_test_skcipher,
2963 .enc = __VECS(cast5_enc_tv_template),
2964 .dec = __VECS(cast5_dec_tv_template)
2968 .alg = "ecb(cast6)",
2969 .test = alg_test_skcipher,
2972 .enc = __VECS(cast6_enc_tv_template),
2973 .dec = __VECS(cast6_dec_tv_template)
2977 .alg = "ecb(cipher_null)",
2978 .test = alg_test_null,
2982 .test = alg_test_skcipher,
2985 .enc = __VECS(des_enc_tv_template),
2986 .dec = __VECS(des_dec_tv_template)
2990 .alg = "ecb(des3_ede)",
2991 .test = alg_test_skcipher,
2995 .enc = __VECS(des3_ede_enc_tv_template),
2996 .dec = __VECS(des3_ede_dec_tv_template)
3000 .alg = "ecb(fcrypt)",
3001 .test = alg_test_skcipher,
3005 .vecs = fcrypt_pcbc_enc_tv_template,
3009 .vecs = fcrypt_pcbc_dec_tv_template,
3015 .alg = "ecb(khazad)",
3016 .test = alg_test_skcipher,
3019 .enc = __VECS(khazad_enc_tv_template),
3020 .dec = __VECS(khazad_dec_tv_template)
3024 /* Same as ecb(aes) except the key is stored in
3025 * hardware secure memory which we reference by index
3028 .test = alg_test_null,
3032 .test = alg_test_skcipher,
3035 .enc = __VECS(seed_enc_tv_template),
3036 .dec = __VECS(seed_dec_tv_template)
3040 .alg = "ecb(serpent)",
3041 .test = alg_test_skcipher,
3044 .enc = __VECS(serpent_enc_tv_template),
3045 .dec = __VECS(serpent_dec_tv_template)
3050 .test = alg_test_skcipher,
3053 .enc = __VECS(sm4_enc_tv_template),
3054 .dec = __VECS(sm4_dec_tv_template)
3058 .alg = "ecb(speck128)",
3059 .test = alg_test_skcipher,
3062 .enc = __VECS(speck128_enc_tv_template),
3063 .dec = __VECS(speck128_dec_tv_template)
3067 .alg = "ecb(speck64)",
3068 .test = alg_test_skcipher,
3071 .enc = __VECS(speck64_enc_tv_template),
3072 .dec = __VECS(speck64_dec_tv_template)
3077 .test = alg_test_skcipher,
3080 .enc = __VECS(tea_enc_tv_template),
3081 .dec = __VECS(tea_dec_tv_template)
3085 .alg = "ecb(tnepres)",
3086 .test = alg_test_skcipher,
3089 .enc = __VECS(tnepres_enc_tv_template),
3090 .dec = __VECS(tnepres_dec_tv_template)
3094 .alg = "ecb(twofish)",
3095 .test = alg_test_skcipher,
3098 .enc = __VECS(tf_enc_tv_template),
3099 .dec = __VECS(tf_dec_tv_template)
3104 .test = alg_test_skcipher,
3107 .enc = __VECS(xeta_enc_tv_template),
3108 .dec = __VECS(xeta_dec_tv_template)
3113 .test = alg_test_skcipher,
3116 .enc = __VECS(xtea_enc_tv_template),
3117 .dec = __VECS(xtea_dec_tv_template)
3122 .test = alg_test_kpp,
3125 .kpp = __VECS(ecdh_tv_template)
3129 .test = alg_test_aead,
3133 .enc = __VECS(aes_gcm_enc_tv_template),
3134 .dec = __VECS(aes_gcm_dec_tv_template)
3139 .test = alg_test_hash,
3142 .hash = __VECS(ghash_tv_template)
3145 .alg = "hmac(crc32)",
3146 .test = alg_test_hash,
3148 .hash = __VECS(bfin_crc_tv_template)
3152 .test = alg_test_hash,
3154 .hash = __VECS(hmac_md5_tv_template)
3157 .alg = "hmac(rmd128)",
3158 .test = alg_test_hash,
3160 .hash = __VECS(hmac_rmd128_tv_template)
3163 .alg = "hmac(rmd160)",
3164 .test = alg_test_hash,
3166 .hash = __VECS(hmac_rmd160_tv_template)
3169 .alg = "hmac(sha1)",
3170 .test = alg_test_hash,
3173 .hash = __VECS(hmac_sha1_tv_template)
3176 .alg = "hmac(sha224)",
3177 .test = alg_test_hash,
3180 .hash = __VECS(hmac_sha224_tv_template)
3183 .alg = "hmac(sha256)",
3184 .test = alg_test_hash,
3187 .hash = __VECS(hmac_sha256_tv_template)
3190 .alg = "hmac(sha3-224)",
3191 .test = alg_test_hash,
3194 .hash = __VECS(hmac_sha3_224_tv_template)
3197 .alg = "hmac(sha3-256)",
3198 .test = alg_test_hash,
3201 .hash = __VECS(hmac_sha3_256_tv_template)
3204 .alg = "hmac(sha3-384)",
3205 .test = alg_test_hash,
3208 .hash = __VECS(hmac_sha3_384_tv_template)
3211 .alg = "hmac(sha3-512)",
3212 .test = alg_test_hash,
3215 .hash = __VECS(hmac_sha3_512_tv_template)
3218 .alg = "hmac(sha384)",
3219 .test = alg_test_hash,
3222 .hash = __VECS(hmac_sha384_tv_template)
3225 .alg = "hmac(sha512)",
3226 .test = alg_test_hash,
3229 .hash = __VECS(hmac_sha512_tv_template)
3232 .alg = "jitterentropy_rng",
3234 .test = alg_test_null,
3237 .test = alg_test_skcipher,
3241 .enc = __VECS(aes_kw_enc_tv_template),
3242 .dec = __VECS(aes_kw_dec_tv_template)
3247 .test = alg_test_skcipher,
3250 .enc = __VECS(aes_lrw_enc_tv_template),
3251 .dec = __VECS(aes_lrw_dec_tv_template)
3255 .alg = "lrw(camellia)",
3256 .test = alg_test_skcipher,
3259 .enc = __VECS(camellia_lrw_enc_tv_template),
3260 .dec = __VECS(camellia_lrw_dec_tv_template)
3264 .alg = "lrw(cast6)",
3265 .test = alg_test_skcipher,
3268 .enc = __VECS(cast6_lrw_enc_tv_template),
3269 .dec = __VECS(cast6_lrw_dec_tv_template)
3273 .alg = "lrw(serpent)",
3274 .test = alg_test_skcipher,
3277 .enc = __VECS(serpent_lrw_enc_tv_template),
3278 .dec = __VECS(serpent_lrw_dec_tv_template)
3282 .alg = "lrw(twofish)",
3283 .test = alg_test_skcipher,
3286 .enc = __VECS(tf_lrw_enc_tv_template),
3287 .dec = __VECS(tf_lrw_dec_tv_template)
3292 .test = alg_test_comp,
3296 .comp = __VECS(lz4_comp_tv_template),
3297 .decomp = __VECS(lz4_decomp_tv_template)
3302 .test = alg_test_comp,
3306 .comp = __VECS(lz4hc_comp_tv_template),
3307 .decomp = __VECS(lz4hc_decomp_tv_template)
3312 .test = alg_test_comp,
3316 .comp = __VECS(lzo_comp_tv_template),
3317 .decomp = __VECS(lzo_decomp_tv_template)
3322 .test = alg_test_hash,
3324 .hash = __VECS(md4_tv_template)
3328 .test = alg_test_hash,
3330 .hash = __VECS(md5_tv_template)
3333 .alg = "michael_mic",
3334 .test = alg_test_hash,
3336 .hash = __VECS(michael_mic_tv_template)
3340 .test = alg_test_skcipher,
3344 .enc = __VECS(aes_ofb_enc_tv_template),
3345 .dec = __VECS(aes_ofb_dec_tv_template)
3349 /* Same as ofb(aes) except the key is stored in
3350 * hardware secure memory which we reference by index
3353 .test = alg_test_null,
3356 .alg = "pcbc(fcrypt)",
3357 .test = alg_test_skcipher,
3360 .enc = __VECS(fcrypt_pcbc_enc_tv_template),
3361 .dec = __VECS(fcrypt_pcbc_dec_tv_template)
3365 .alg = "pkcs1pad(rsa,sha224)",
3366 .test = alg_test_null,
3369 .alg = "pkcs1pad(rsa,sha256)",
3370 .test = alg_test_akcipher,
3373 .akcipher = __VECS(pkcs1pad_rsa_tv_template)
3376 .alg = "pkcs1pad(rsa,sha384)",
3377 .test = alg_test_null,
3380 .alg = "pkcs1pad(rsa,sha512)",
3381 .test = alg_test_null,
3385 .test = alg_test_hash,
3387 .hash = __VECS(poly1305_tv_template)
3390 .alg = "rfc3686(ctr(aes))",
3391 .test = alg_test_skcipher,
3395 .enc = __VECS(aes_ctr_rfc3686_enc_tv_template),
3396 .dec = __VECS(aes_ctr_rfc3686_dec_tv_template)
3400 .alg = "rfc4106(gcm(aes))",
3401 .test = alg_test_aead,
3405 .enc = __VECS(aes_gcm_rfc4106_enc_tv_template),
3406 .dec = __VECS(aes_gcm_rfc4106_dec_tv_template)
3410 .alg = "rfc4309(ccm(aes))",
3411 .test = alg_test_aead,
3415 .enc = __VECS(aes_ccm_rfc4309_enc_tv_template),
3416 .dec = __VECS(aes_ccm_rfc4309_dec_tv_template)
3420 .alg = "rfc4543(gcm(aes))",
3421 .test = alg_test_aead,
3424 .enc = __VECS(aes_gcm_rfc4543_enc_tv_template),
3425 .dec = __VECS(aes_gcm_rfc4543_dec_tv_template),
3429 .alg = "rfc7539(chacha20,poly1305)",
3430 .test = alg_test_aead,
3433 .enc = __VECS(rfc7539_enc_tv_template),
3434 .dec = __VECS(rfc7539_dec_tv_template),
3438 .alg = "rfc7539esp(chacha20,poly1305)",
3439 .test = alg_test_aead,
3442 .enc = __VECS(rfc7539esp_enc_tv_template),
3443 .dec = __VECS(rfc7539esp_dec_tv_template),
3448 .test = alg_test_hash,
3450 .hash = __VECS(rmd128_tv_template)
3454 .test = alg_test_hash,
3456 .hash = __VECS(rmd160_tv_template)
3460 .test = alg_test_hash,
3462 .hash = __VECS(rmd256_tv_template)
3466 .test = alg_test_hash,
3468 .hash = __VECS(rmd320_tv_template)
3472 .test = alg_test_akcipher,
3475 .akcipher = __VECS(rsa_tv_template)
3479 .test = alg_test_skcipher,
3482 .enc = __VECS(salsa20_stream_enc_tv_template)
3487 .test = alg_test_hash,
3490 .hash = __VECS(sha1_tv_template)
3494 .test = alg_test_hash,
3497 .hash = __VECS(sha224_tv_template)
3501 .test = alg_test_hash,
3504 .hash = __VECS(sha256_tv_template)
3508 .test = alg_test_hash,
3511 .hash = __VECS(sha3_224_tv_template)
3515 .test = alg_test_hash,
3518 .hash = __VECS(sha3_256_tv_template)
3522 .test = alg_test_hash,
3525 .hash = __VECS(sha3_384_tv_template)
3529 .test = alg_test_hash,
3532 .hash = __VECS(sha3_512_tv_template)
3536 .test = alg_test_hash,
3539 .hash = __VECS(sha384_tv_template)
3543 .test = alg_test_hash,
3546 .hash = __VECS(sha512_tv_template)
3550 .test = alg_test_hash,
3552 .hash = __VECS(sm3_tv_template)
3556 .test = alg_test_hash,
3558 .hash = __VECS(tgr128_tv_template)
3562 .test = alg_test_hash,
3564 .hash = __VECS(tgr160_tv_template)
3568 .test = alg_test_hash,
3570 .hash = __VECS(tgr192_tv_template)
3574 .test = alg_test_hash,
3576 .hash = __VECS(aes_vmac128_tv_template)
3580 .test = alg_test_hash,
3582 .hash = __VECS(wp256_tv_template)
3586 .test = alg_test_hash,
3588 .hash = __VECS(wp384_tv_template)
3592 .test = alg_test_hash,
3594 .hash = __VECS(wp512_tv_template)
3598 .test = alg_test_hash,
3600 .hash = __VECS(aes_xcbc128_tv_template)
3604 .test = alg_test_skcipher,
3608 .enc = __VECS(aes_xts_enc_tv_template),
3609 .dec = __VECS(aes_xts_dec_tv_template)
3613 .alg = "xts(camellia)",
3614 .test = alg_test_skcipher,
3617 .enc = __VECS(camellia_xts_enc_tv_template),
3618 .dec = __VECS(camellia_xts_dec_tv_template)
3622 .alg = "xts(cast6)",
3623 .test = alg_test_skcipher,
3626 .enc = __VECS(cast6_xts_enc_tv_template),
3627 .dec = __VECS(cast6_xts_dec_tv_template)
3631 /* Same as xts(aes) except the key is stored in
3632 * hardware secure memory which we reference by index
3635 .test = alg_test_null,
3638 .alg = "xts(serpent)",
3639 .test = alg_test_skcipher,
3642 .enc = __VECS(serpent_xts_enc_tv_template),
3643 .dec = __VECS(serpent_xts_dec_tv_template)
3647 .alg = "xts(speck128)",
3648 .test = alg_test_skcipher,
3651 .enc = __VECS(speck128_xts_enc_tv_template),
3652 .dec = __VECS(speck128_xts_dec_tv_template)
3656 .alg = "xts(speck64)",
3657 .test = alg_test_skcipher,
3660 .enc = __VECS(speck64_xts_enc_tv_template),
3661 .dec = __VECS(speck64_xts_dec_tv_template)
3665 .alg = "xts(twofish)",
3666 .test = alg_test_skcipher,
3669 .enc = __VECS(tf_xts_enc_tv_template),
3670 .dec = __VECS(tf_xts_dec_tv_template)
3674 .alg = "xts4096(paes)",
3675 .test = alg_test_null,
3678 .alg = "xts512(paes)",
3679 .test = alg_test_null,
3682 .alg = "zlib-deflate",
3683 .test = alg_test_comp,
3687 .comp = __VECS(zlib_deflate_comp_tv_template),
3688 .decomp = __VECS(zlib_deflate_decomp_tv_template)
3693 .test = alg_test_comp,
3697 .comp = __VECS(zstd_comp_tv_template),
3698 .decomp = __VECS(zstd_decomp_tv_template)
3704 static bool alg_test_descs_checked;
3706 static void alg_test_descs_check_order(void)
3710 /* only check once */
3711 if (alg_test_descs_checked)
3714 alg_test_descs_checked = true;
3716 for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
3717 int diff = strcmp(alg_test_descs[i - 1].alg,
3718 alg_test_descs[i].alg);
3720 if (WARN_ON(diff > 0)) {
3721 pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
3722 alg_test_descs[i - 1].alg,
3723 alg_test_descs[i].alg);
3726 if (WARN_ON(diff == 0)) {
3727 pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
3728 alg_test_descs[i].alg);
3733 static int alg_find_test(const char *alg)
3736 int end = ARRAY_SIZE(alg_test_descs);
3738 while (start < end) {
3739 int i = (start + end) / 2;
3740 int diff = strcmp(alg_test_descs[i].alg, alg);
3758 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
3764 if (!fips_enabled && notests) {
3765 printk_once(KERN_INFO "alg: self-tests disabled\n");
3769 alg_test_descs_check_order();
3771 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
3772 char nalg[CRYPTO_MAX_ALG_NAME];
3774 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
3776 return -ENAMETOOLONG;
3778 i = alg_find_test(nalg);
3782 if (fips_enabled && !alg_test_descs[i].fips_allowed)
3785 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
3789 i = alg_find_test(alg);
3790 j = alg_find_test(driver);
3794 if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
3795 (j >= 0 && !alg_test_descs[j].fips_allowed)))
3800 rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
3802 if (j >= 0 && j != i)
3803 rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
3807 if (fips_enabled && rc)
3808 panic("%s: %s alg self test failed in fips mode!\n", driver, alg);
3810 if (fips_enabled && !rc)
3811 pr_info("alg: self-tests for %s (%s) passed\n", driver, alg);
3816 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
3822 #endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
3824 EXPORT_SYMBOL_GPL(alg_test);