2 * Algorithm testing framework and tests.
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
6 * Copyright (c) 2007 Nokia Siemens Networks
7 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
9 * Updated RFC4106 AES-GCM testing.
10 * Authors: Aidan O'Mahony (aidan.o.mahony@intel.com)
11 * Adrian Hoban <adrian.hoban@intel.com>
12 * Gabriele Paoloni <gabriele.paoloni@intel.com>
13 * Tadeusz Struk (tadeusz.struk@intel.com)
14 * Copyright (c) 2010, Intel Corporation.
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the Free
18 * Software Foundation; either version 2 of the License, or (at your option)
23 #include <crypto/aead.h>
24 #include <crypto/hash.h>
25 #include <crypto/skcipher.h>
26 #include <linux/err.h>
27 #include <linux/fips.h>
28 #include <linux/module.h>
29 #include <linux/scatterlist.h>
30 #include <linux/slab.h>
31 #include <linux/string.h>
32 #include <crypto/rng.h>
33 #include <crypto/drbg.h>
34 #include <crypto/akcipher.h>
35 #include <crypto/kpp.h>
36 #include <crypto/acompress.h>
41 module_param(notests
, bool, 0644);
42 MODULE_PARM_DESC(notests
, "disable crypto self-tests");
44 #ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
47 int alg_test(const char *driver
, const char *alg
, u32 type
, u32 mask
)
57 * Need slab memory for testing (size in number of pages).
62 * Indexes into the xbuf to simulate cross-page access.
74 * Used by test_cipher()
79 struct tcrypt_result
{
80 struct completion completion
;
84 struct aead_test_suite
{
86 const struct aead_testvec
*vecs
;
91 struct cipher_test_suite
{
93 const struct cipher_testvec
*vecs
;
98 struct comp_test_suite
{
100 const struct comp_testvec
*vecs
;
105 struct hash_test_suite
{
106 const struct hash_testvec
*vecs
;
110 struct cprng_test_suite
{
111 const struct cprng_testvec
*vecs
;
115 struct drbg_test_suite
{
116 const struct drbg_testvec
*vecs
;
120 struct akcipher_test_suite
{
121 const struct akcipher_testvec
*vecs
;
125 struct kpp_test_suite
{
126 const struct kpp_testvec
*vecs
;
130 struct alg_test_desc
{
132 int (*test
)(const struct alg_test_desc
*desc
, const char *driver
,
134 int fips_allowed
; /* set if alg is allowed in fips mode */
137 struct aead_test_suite aead
;
138 struct cipher_test_suite cipher
;
139 struct comp_test_suite comp
;
140 struct hash_test_suite hash
;
141 struct cprng_test_suite cprng
;
142 struct drbg_test_suite drbg
;
143 struct akcipher_test_suite akcipher
;
144 struct kpp_test_suite kpp
;
148 static const unsigned int IDX
[8] = {
149 IDX1
, IDX2
, IDX3
, IDX4
, IDX5
, IDX6
, IDX7
, IDX8
};
151 static void hexdump(unsigned char *buf
, unsigned int len
)
153 print_hex_dump(KERN_CONT
, "", DUMP_PREFIX_OFFSET
,
158 static void tcrypt_complete(struct crypto_async_request
*req
, int err
)
160 struct tcrypt_result
*res
= req
->data
;
162 if (err
== -EINPROGRESS
)
166 complete(&res
->completion
);
169 static int testmgr_alloc_buf(char *buf
[XBUFSIZE
])
173 for (i
= 0; i
< XBUFSIZE
; i
++) {
174 buf
[i
] = (void *)__get_free_page(GFP_KERNEL
);
183 free_page((unsigned long)buf
[i
]);
188 static void testmgr_free_buf(char *buf
[XBUFSIZE
])
192 for (i
= 0; i
< XBUFSIZE
; i
++)
193 free_page((unsigned long)buf
[i
]);
196 static int wait_async_op(struct tcrypt_result
*tr
, int ret
)
198 if (ret
== -EINPROGRESS
|| ret
== -EBUSY
) {
199 wait_for_completion(&tr
->completion
);
200 reinit_completion(&tr
->completion
);
206 static int ahash_partial_update(struct ahash_request
**preq
,
207 struct crypto_ahash
*tfm
, const struct hash_testvec
*template,
208 void *hash_buff
, int k
, int temp
, struct scatterlist
*sg
,
209 const char *algo
, char *result
, struct tcrypt_result
*tresult
)
212 struct ahash_request
*req
;
213 int statesize
, ret
= -EINVAL
;
214 const char guard
[] = { 0x00, 0xba, 0xad, 0x00 };
217 statesize
= crypto_ahash_statesize(
218 crypto_ahash_reqtfm(req
));
219 state
= kmalloc(statesize
+ sizeof(guard
), GFP_KERNEL
);
221 pr_err("alt: hash: Failed to alloc state for %s\n", algo
);
224 memcpy(state
+ statesize
, guard
, sizeof(guard
));
225 ret
= crypto_ahash_export(req
, state
);
226 WARN_ON(memcmp(state
+ statesize
, guard
, sizeof(guard
)));
228 pr_err("alt: hash: Failed to export() for %s\n", algo
);
231 ahash_request_free(req
);
232 req
= ahash_request_alloc(tfm
, GFP_KERNEL
);
234 pr_err("alg: hash: Failed to alloc request for %s\n", algo
);
237 ahash_request_set_callback(req
,
238 CRYPTO_TFM_REQ_MAY_BACKLOG
,
239 tcrypt_complete
, tresult
);
241 memcpy(hash_buff
, template->plaintext
+ temp
,
243 sg_init_one(&sg
[0], hash_buff
, template->tap
[k
]);
244 ahash_request_set_crypt(req
, sg
, result
, template->tap
[k
]);
245 ret
= crypto_ahash_import(req
, state
);
247 pr_err("alg: hash: Failed to import() for %s\n", algo
);
250 ret
= wait_async_op(tresult
, crypto_ahash_update(req
));
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 tcrypt_result tresult
;
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 init_completion(&tresult
.completion
);
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 tcrypt_complete
, &tresult
);
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
= wait_async_op(&tresult
, crypto_ahash_digest(req
));
340 pr_err("alg: hash: digest failed on test %d "
341 "for %s: ret=%d\n", j
, algo
, -ret
);
345 ret
= wait_async_op(&tresult
, crypto_ahash_init(req
));
347 pr_err("alt: hash: init failed on test %d "
348 "for %s: ret=%d\n", j
, algo
, -ret
);
351 ret
= wait_async_op(&tresult
, crypto_ahash_update(req
));
353 pr_err("alt: hash: update failed on test %d "
354 "for %s: ret=%d\n", j
, algo
, -ret
);
357 ret
= wait_async_op(&tresult
, crypto_ahash_final(req
));
359 pr_err("alt: hash: final failed on test %d "
360 "for %s: ret=%d\n", j
, algo
, -ret
);
365 if (memcmp(result
, template[i
].digest
,
366 crypto_ahash_digestsize(tfm
))) {
367 printk(KERN_ERR
"alg: hash: Test %d failed for %s\n",
369 hexdump(result
, crypto_ahash_digestsize(tfm
));
376 for (i
= 0; i
< tcount
; i
++) {
377 /* alignment tests are only done with continuous buffers */
378 if (align_offset
!= 0)
385 memset(result
, 0, digest_size
);
388 sg_init_table(sg
, template[i
].np
);
390 for (k
= 0; k
< template[i
].np
; k
++) {
391 if (WARN_ON(offset_in_page(IDX
[k
]) +
392 template[i
].tap
[k
] > PAGE_SIZE
))
395 memcpy(xbuf
[IDX
[k
] >> PAGE_SHIFT
] +
396 offset_in_page(IDX
[k
]),
397 template[i
].plaintext
+ temp
,
400 temp
+= template[i
].tap
[k
];
403 if (template[i
].ksize
) {
404 if (template[i
].ksize
> MAX_KEYLEN
) {
405 pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
406 j
, algo
, template[i
].ksize
, MAX_KEYLEN
);
410 crypto_ahash_clear_flags(tfm
, ~0);
411 memcpy(key
, template[i
].key
, template[i
].ksize
);
412 ret
= crypto_ahash_setkey(tfm
, key
, template[i
].ksize
);
415 printk(KERN_ERR
"alg: hash: setkey "
416 "failed on chunking test %d "
417 "for %s: ret=%d\n", j
, algo
, -ret
);
422 ahash_request_set_crypt(req
, sg
, result
, template[i
].psize
);
423 ret
= crypto_ahash_digest(req
);
429 wait_for_completion(&tresult
.completion
);
430 reinit_completion(&tresult
.completion
);
436 printk(KERN_ERR
"alg: hash: digest failed "
437 "on chunking test %d for %s: "
438 "ret=%d\n", j
, algo
, -ret
);
442 if (memcmp(result
, template[i
].digest
,
443 crypto_ahash_digestsize(tfm
))) {
444 printk(KERN_ERR
"alg: hash: Chunking test %d "
445 "failed for %s\n", j
, algo
);
446 hexdump(result
, crypto_ahash_digestsize(tfm
));
452 /* partial update exercise */
454 for (i
= 0; i
< tcount
; i
++) {
455 /* alignment tests are only done with continuous buffers */
456 if (align_offset
!= 0)
459 if (template[i
].np
< 2)
463 memset(result
, 0, digest_size
);
467 memcpy(hash_buff
, template[i
].plaintext
,
469 sg_init_one(&sg
[0], hash_buff
, template[i
].tap
[0]);
471 if (template[i
].ksize
) {
472 crypto_ahash_clear_flags(tfm
, ~0);
473 if (template[i
].ksize
> MAX_KEYLEN
) {
474 pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
475 j
, algo
, template[i
].ksize
, MAX_KEYLEN
);
479 memcpy(key
, template[i
].key
, template[i
].ksize
);
480 ret
= crypto_ahash_setkey(tfm
, key
, template[i
].ksize
);
482 pr_err("alg: hash: setkey failed on test %d for %s: ret=%d\n",
488 ahash_request_set_crypt(req
, sg
, result
, template[i
].tap
[0]);
489 ret
= wait_async_op(&tresult
, crypto_ahash_init(req
));
491 pr_err("alt: hash: init failed on test %d for %s: ret=%d\n",
495 ret
= wait_async_op(&tresult
, crypto_ahash_update(req
));
497 pr_err("alt: hash: update failed on test %d for %s: ret=%d\n",
502 temp
= template[i
].tap
[0];
503 for (k
= 1; k
< template[i
].np
; k
++) {
504 ret
= ahash_partial_update(&req
, tfm
, &template[i
],
505 hash_buff
, k
, temp
, &sg
[0], algo
, result
,
508 pr_err("hash: partial update failed on test %d for %s: ret=%d\n",
512 temp
+= template[i
].tap
[k
];
514 ret
= wait_async_op(&tresult
, crypto_ahash_final(req
));
516 pr_err("alt: hash: final failed on test %d for %s: ret=%d\n",
520 if (memcmp(result
, template[i
].digest
,
521 crypto_ahash_digestsize(tfm
))) {
522 pr_err("alg: hash: Partial Test %d failed for %s\n",
524 hexdump(result
, crypto_ahash_digestsize(tfm
));
533 ahash_request_free(req
);
535 testmgr_free_buf(xbuf
);
542 static int test_hash(struct crypto_ahash
*tfm
,
543 const struct hash_testvec
*template,
544 unsigned int tcount
, bool use_digest
)
546 unsigned int alignmask
;
549 ret
= __test_hash(tfm
, template, tcount
, use_digest
, 0);
553 /* test unaligned buffers, check with one byte offset */
554 ret
= __test_hash(tfm
, template, tcount
, use_digest
, 1);
558 alignmask
= crypto_tfm_alg_alignmask(&tfm
->base
);
560 /* Check if alignment mask for tfm is correctly set. */
561 ret
= __test_hash(tfm
, template, tcount
, use_digest
,
570 static int __test_aead(struct crypto_aead
*tfm
, int enc
,
571 const struct aead_testvec
*template, unsigned int tcount
,
572 const bool diff_dst
, const int align_offset
)
574 const char *algo
= crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm
));
575 unsigned int i
, j
, k
, n
, temp
;
579 struct aead_request
*req
;
580 struct scatterlist
*sg
;
581 struct scatterlist
*sgout
;
583 struct tcrypt_result result
;
584 unsigned int authsize
, iv_len
;
589 char *xbuf
[XBUFSIZE
];
590 char *xoutbuf
[XBUFSIZE
];
591 char *axbuf
[XBUFSIZE
];
593 iv
= kzalloc(MAX_IVLEN
, GFP_KERNEL
);
596 key
= kmalloc(MAX_KEYLEN
, GFP_KERNEL
);
599 if (testmgr_alloc_buf(xbuf
))
601 if (testmgr_alloc_buf(axbuf
))
603 if (diff_dst
&& testmgr_alloc_buf(xoutbuf
))
606 /* avoid "the frame size is larger than 1024 bytes" compiler warning */
607 sg
= kmalloc(sizeof(*sg
) * 8 * (diff_dst
? 4 : 2), GFP_KERNEL
);
622 init_completion(&result
.completion
);
624 req
= aead_request_alloc(tfm
, GFP_KERNEL
);
626 pr_err("alg: aead%s: Failed to allocate request for %s\n",
631 aead_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
632 tcrypt_complete
, &result
);
634 iv_len
= crypto_aead_ivsize(tfm
);
636 for (i
= 0, j
= 0; i
< tcount
; i
++) {
642 /* some templates have no input data but they will
646 input
+= align_offset
;
650 if (WARN_ON(align_offset
+ template[i
].ilen
>
651 PAGE_SIZE
|| template[i
].alen
> PAGE_SIZE
))
654 memcpy(input
, template[i
].input
, template[i
].ilen
);
655 memcpy(assoc
, template[i
].assoc
, template[i
].alen
);
657 memcpy(iv
, template[i
].iv
, iv_len
);
659 memset(iv
, 0, iv_len
);
661 crypto_aead_clear_flags(tfm
, ~0);
663 crypto_aead_set_flags(tfm
, CRYPTO_TFM_REQ_WEAK_KEY
);
665 if (template[i
].klen
> MAX_KEYLEN
) {
666 pr_err("alg: aead%s: setkey failed on test %d for %s: key size %d > %d\n",
667 d
, j
, algo
, template[i
].klen
,
672 memcpy(key
, template[i
].key
, template[i
].klen
);
674 ret
= crypto_aead_setkey(tfm
, key
, template[i
].klen
);
675 if (template[i
].fail
== !ret
) {
676 pr_err("alg: aead%s: setkey failed on test %d for %s: flags=%x\n",
677 d
, j
, algo
, crypto_aead_get_flags(tfm
));
682 authsize
= abs(template[i
].rlen
- template[i
].ilen
);
683 ret
= crypto_aead_setauthsize(tfm
, authsize
);
685 pr_err("alg: aead%s: Failed to set authsize to %u on test %d for %s\n",
686 d
, authsize
, j
, algo
);
690 k
= !!template[i
].alen
;
691 sg_init_table(sg
, k
+ 1);
692 sg_set_buf(&sg
[0], assoc
, template[i
].alen
);
693 sg_set_buf(&sg
[k
], input
,
694 template[i
].ilen
+ (enc
? authsize
: 0));
698 sg_init_table(sgout
, k
+ 1);
699 sg_set_buf(&sgout
[0], assoc
, template[i
].alen
);
702 output
+= align_offset
;
703 sg_set_buf(&sgout
[k
], output
,
704 template[i
].rlen
+ (enc
? 0 : authsize
));
707 aead_request_set_crypt(req
, sg
, (diff_dst
) ? sgout
: sg
,
708 template[i
].ilen
, iv
);
710 aead_request_set_ad(req
, template[i
].alen
);
712 ret
= enc
? crypto_aead_encrypt(req
) : crypto_aead_decrypt(req
);
716 if (template[i
].novrfy
) {
717 /* verification was supposed to fail */
718 pr_err("alg: aead%s: %s failed on test %d for %s: ret was 0, expected -EBADMSG\n",
720 /* so really, we got a bad message */
727 wait_for_completion(&result
.completion
);
728 reinit_completion(&result
.completion
);
733 if (template[i
].novrfy
)
734 /* verification failure was expected */
738 pr_err("alg: aead%s: %s failed on test %d for %s: ret=%d\n",
739 d
, e
, j
, algo
, -ret
);
744 if (memcmp(q
, template[i
].result
, template[i
].rlen
)) {
745 pr_err("alg: aead%s: Test %d failed on %s for %s\n",
747 hexdump(q
, template[i
].rlen
);
753 for (i
= 0, j
= 0; i
< tcount
; i
++) {
754 /* alignment tests are only done with continuous buffers */
755 if (align_offset
!= 0)
764 memcpy(iv
, template[i
].iv
, iv_len
);
766 memset(iv
, 0, MAX_IVLEN
);
768 crypto_aead_clear_flags(tfm
, ~0);
770 crypto_aead_set_flags(tfm
, CRYPTO_TFM_REQ_WEAK_KEY
);
771 if (template[i
].klen
> MAX_KEYLEN
) {
772 pr_err("alg: aead%s: setkey failed on test %d for %s: key size %d > %d\n",
773 d
, j
, algo
, template[i
].klen
, MAX_KEYLEN
);
777 memcpy(key
, template[i
].key
, template[i
].klen
);
779 ret
= crypto_aead_setkey(tfm
, key
, template[i
].klen
);
780 if (template[i
].fail
== !ret
) {
781 pr_err("alg: aead%s: setkey failed on chunk test %d for %s: flags=%x\n",
782 d
, j
, algo
, crypto_aead_get_flags(tfm
));
787 authsize
= abs(template[i
].rlen
- template[i
].ilen
);
790 sg_init_table(sg
, template[i
].anp
+ template[i
].np
);
792 sg_init_table(sgout
, template[i
].anp
+ template[i
].np
);
795 for (k
= 0, temp
= 0; k
< template[i
].anp
; k
++) {
796 if (WARN_ON(offset_in_page(IDX
[k
]) +
797 template[i
].atap
[k
] > PAGE_SIZE
))
800 memcpy(axbuf
[IDX
[k
] >> PAGE_SHIFT
] +
801 offset_in_page(IDX
[k
]),
802 template[i
].assoc
+ temp
,
803 template[i
].atap
[k
]),
804 template[i
].atap
[k
]);
806 sg_set_buf(&sgout
[k
],
807 axbuf
[IDX
[k
] >> PAGE_SHIFT
] +
808 offset_in_page(IDX
[k
]),
809 template[i
].atap
[k
]);
810 temp
+= template[i
].atap
[k
];
813 for (k
= 0, temp
= 0; k
< template[i
].np
; k
++) {
814 if (WARN_ON(offset_in_page(IDX
[k
]) +
815 template[i
].tap
[k
] > PAGE_SIZE
))
818 q
= xbuf
[IDX
[k
] >> PAGE_SHIFT
] + offset_in_page(IDX
[k
]);
819 memcpy(q
, template[i
].input
+ temp
, template[i
].tap
[k
]);
820 sg_set_buf(&sg
[template[i
].anp
+ k
],
821 q
, template[i
].tap
[k
]);
824 q
= xoutbuf
[IDX
[k
] >> PAGE_SHIFT
] +
825 offset_in_page(IDX
[k
]);
827 memset(q
, 0, template[i
].tap
[k
]);
829 sg_set_buf(&sgout
[template[i
].anp
+ k
],
830 q
, template[i
].tap
[k
]);
833 n
= template[i
].tap
[k
];
834 if (k
== template[i
].np
- 1 && enc
)
836 if (offset_in_page(q
) + n
< PAGE_SIZE
)
839 temp
+= template[i
].tap
[k
];
842 ret
= crypto_aead_setauthsize(tfm
, authsize
);
844 pr_err("alg: aead%s: Failed to set authsize to %u on chunk test %d for %s\n",
845 d
, authsize
, j
, algo
);
850 if (WARN_ON(sg
[template[i
].anp
+ k
- 1].offset
+
851 sg
[template[i
].anp
+ k
- 1].length
+
852 authsize
> PAGE_SIZE
)) {
858 sgout
[template[i
].anp
+ k
- 1].length
+=
860 sg
[template[i
].anp
+ k
- 1].length
+= authsize
;
863 aead_request_set_crypt(req
, sg
, (diff_dst
) ? sgout
: sg
,
867 aead_request_set_ad(req
, template[i
].alen
);
869 ret
= enc
? crypto_aead_encrypt(req
) : crypto_aead_decrypt(req
);
873 if (template[i
].novrfy
) {
874 /* verification was supposed to fail */
875 pr_err("alg: aead%s: %s failed on chunk test %d for %s: ret was 0, expected -EBADMSG\n",
877 /* so really, we got a bad message */
884 wait_for_completion(&result
.completion
);
885 reinit_completion(&result
.completion
);
890 if (template[i
].novrfy
)
891 /* verification failure was expected */
895 pr_err("alg: aead%s: %s failed on chunk test %d for %s: ret=%d\n",
896 d
, e
, j
, algo
, -ret
);
901 for (k
= 0, temp
= 0; k
< template[i
].np
; k
++) {
903 q
= xoutbuf
[IDX
[k
] >> PAGE_SHIFT
] +
904 offset_in_page(IDX
[k
]);
906 q
= xbuf
[IDX
[k
] >> PAGE_SHIFT
] +
907 offset_in_page(IDX
[k
]);
909 n
= template[i
].tap
[k
];
910 if (k
== template[i
].np
- 1)
911 n
+= enc
? authsize
: -authsize
;
913 if (memcmp(q
, template[i
].result
+ temp
, n
)) {
914 pr_err("alg: aead%s: Chunk test %d failed on %s at page %u for %s\n",
921 if (k
== template[i
].np
- 1 && !enc
) {
923 memcmp(q
, template[i
].input
+
929 for (n
= 0; offset_in_page(q
+ n
) && q
[n
]; n
++)
933 pr_err("alg: aead%s: Result buffer corruption in chunk test %d on %s at page %u for %s: %u bytes:\n",
934 d
, j
, e
, k
, algo
, n
);
939 temp
+= template[i
].tap
[k
];
946 aead_request_free(req
);
950 testmgr_free_buf(xoutbuf
);
952 testmgr_free_buf(axbuf
);
954 testmgr_free_buf(xbuf
);
961 static int test_aead(struct crypto_aead
*tfm
, int enc
,
962 const struct aead_testvec
*template, unsigned int tcount
)
964 unsigned int alignmask
;
967 /* test 'dst == src' case */
968 ret
= __test_aead(tfm
, enc
, template, tcount
, false, 0);
972 /* test 'dst != src' case */
973 ret
= __test_aead(tfm
, enc
, template, tcount
, true, 0);
977 /* test unaligned buffers, check with one byte offset */
978 ret
= __test_aead(tfm
, enc
, template, tcount
, true, 1);
982 alignmask
= crypto_tfm_alg_alignmask(&tfm
->base
);
984 /* Check if alignment mask for tfm is correctly set. */
985 ret
= __test_aead(tfm
, enc
, template, tcount
, true,
994 static int test_cipher(struct crypto_cipher
*tfm
, int enc
,
995 const struct cipher_testvec
*template,
998 const char *algo
= crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm
));
999 unsigned int i
, j
, k
;
1003 char *xbuf
[XBUFSIZE
];
1006 if (testmgr_alloc_buf(xbuf
))
1015 for (i
= 0; i
< tcount
; i
++) {
1019 if (fips_enabled
&& template[i
].fips_skip
)
1025 if (WARN_ON(template[i
].ilen
> PAGE_SIZE
))
1029 memcpy(data
, template[i
].input
, template[i
].ilen
);
1031 crypto_cipher_clear_flags(tfm
, ~0);
1033 crypto_cipher_set_flags(tfm
, CRYPTO_TFM_REQ_WEAK_KEY
);
1035 ret
= crypto_cipher_setkey(tfm
, template[i
].key
,
1037 if (template[i
].fail
== !ret
) {
1038 printk(KERN_ERR
"alg: cipher: setkey failed "
1039 "on test %d for %s: flags=%x\n", j
,
1040 algo
, crypto_cipher_get_flags(tfm
));
1045 for (k
= 0; k
< template[i
].ilen
;
1046 k
+= crypto_cipher_blocksize(tfm
)) {
1048 crypto_cipher_encrypt_one(tfm
, data
+ k
,
1051 crypto_cipher_decrypt_one(tfm
, data
+ k
,
1056 if (memcmp(q
, template[i
].result
, template[i
].rlen
)) {
1057 printk(KERN_ERR
"alg: cipher: Test %d failed "
1058 "on %s for %s\n", j
, e
, algo
);
1059 hexdump(q
, template[i
].rlen
);
1068 testmgr_free_buf(xbuf
);
1073 static int __test_skcipher(struct crypto_skcipher
*tfm
, int enc
,
1074 const struct cipher_testvec
*template,
1075 unsigned int tcount
,
1076 const bool diff_dst
, const int align_offset
)
1079 crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm
));
1080 unsigned int i
, j
, k
, n
, temp
;
1082 struct skcipher_request
*req
;
1083 struct scatterlist sg
[8];
1084 struct scatterlist sgout
[8];
1086 struct tcrypt_result result
;
1089 char *xbuf
[XBUFSIZE
];
1090 char *xoutbuf
[XBUFSIZE
];
1092 unsigned int ivsize
= crypto_skcipher_ivsize(tfm
);
1094 if (testmgr_alloc_buf(xbuf
))
1097 if (diff_dst
&& testmgr_alloc_buf(xoutbuf
))
1110 init_completion(&result
.completion
);
1112 req
= skcipher_request_alloc(tfm
, GFP_KERNEL
);
1114 pr_err("alg: skcipher%s: Failed to allocate request for %s\n",
1119 skcipher_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
1120 tcrypt_complete
, &result
);
1123 for (i
= 0; i
< tcount
; i
++) {
1124 if (template[i
].np
&& !template[i
].also_non_np
)
1127 if (fips_enabled
&& template[i
].fips_skip
)
1131 memcpy(iv
, template[i
].iv
, ivsize
);
1133 memset(iv
, 0, MAX_IVLEN
);
1137 if (WARN_ON(align_offset
+ template[i
].ilen
> PAGE_SIZE
))
1141 data
+= align_offset
;
1142 memcpy(data
, template[i
].input
, template[i
].ilen
);
1144 crypto_skcipher_clear_flags(tfm
, ~0);
1146 crypto_skcipher_set_flags(tfm
,
1147 CRYPTO_TFM_REQ_WEAK_KEY
);
1149 ret
= crypto_skcipher_setkey(tfm
, template[i
].key
,
1151 if (template[i
].fail
== !ret
) {
1152 pr_err("alg: skcipher%s: setkey failed on test %d for %s: flags=%x\n",
1153 d
, j
, algo
, crypto_skcipher_get_flags(tfm
));
1158 sg_init_one(&sg
[0], data
, template[i
].ilen
);
1161 data
+= align_offset
;
1162 sg_init_one(&sgout
[0], data
, template[i
].ilen
);
1165 skcipher_request_set_crypt(req
, sg
, (diff_dst
) ? sgout
: sg
,
1166 template[i
].ilen
, iv
);
1167 ret
= enc
? crypto_skcipher_encrypt(req
) :
1168 crypto_skcipher_decrypt(req
);
1175 wait_for_completion(&result
.completion
);
1176 reinit_completion(&result
.completion
);
1182 pr_err("alg: skcipher%s: %s failed on test %d for %s: ret=%d\n",
1183 d
, e
, j
, algo
, -ret
);
1188 if (memcmp(q
, template[i
].result
, template[i
].rlen
)) {
1189 pr_err("alg: skcipher%s: Test %d failed (invalid result) on %s for %s\n",
1191 hexdump(q
, template[i
].rlen
);
1196 if (template[i
].iv_out
&&
1197 memcmp(iv
, template[i
].iv_out
,
1198 crypto_skcipher_ivsize(tfm
))) {
1199 pr_err("alg: skcipher%s: Test %d failed (invalid output IV) on %s for %s\n",
1201 hexdump(iv
, crypto_skcipher_ivsize(tfm
));
1208 for (i
= 0; i
< tcount
; i
++) {
1209 /* alignment tests are only done with continuous buffers */
1210 if (align_offset
!= 0)
1213 if (!template[i
].np
)
1216 if (fips_enabled
&& template[i
].fips_skip
)
1220 memcpy(iv
, template[i
].iv
, ivsize
);
1222 memset(iv
, 0, MAX_IVLEN
);
1225 crypto_skcipher_clear_flags(tfm
, ~0);
1227 crypto_skcipher_set_flags(tfm
,
1228 CRYPTO_TFM_REQ_WEAK_KEY
);
1230 ret
= crypto_skcipher_setkey(tfm
, template[i
].key
,
1232 if (template[i
].fail
== !ret
) {
1233 pr_err("alg: skcipher%s: setkey failed on chunk test %d for %s: flags=%x\n",
1234 d
, j
, algo
, crypto_skcipher_get_flags(tfm
));
1241 sg_init_table(sg
, template[i
].np
);
1243 sg_init_table(sgout
, template[i
].np
);
1244 for (k
= 0; k
< template[i
].np
; k
++) {
1245 if (WARN_ON(offset_in_page(IDX
[k
]) +
1246 template[i
].tap
[k
] > PAGE_SIZE
))
1249 q
= xbuf
[IDX
[k
] >> PAGE_SHIFT
] + offset_in_page(IDX
[k
]);
1251 memcpy(q
, template[i
].input
+ temp
, template[i
].tap
[k
]);
1253 if (offset_in_page(q
) + template[i
].tap
[k
] < PAGE_SIZE
)
1254 q
[template[i
].tap
[k
]] = 0;
1256 sg_set_buf(&sg
[k
], q
, template[i
].tap
[k
]);
1258 q
= xoutbuf
[IDX
[k
] >> PAGE_SHIFT
] +
1259 offset_in_page(IDX
[k
]);
1261 sg_set_buf(&sgout
[k
], q
, template[i
].tap
[k
]);
1263 memset(q
, 0, template[i
].tap
[k
]);
1264 if (offset_in_page(q
) +
1265 template[i
].tap
[k
] < PAGE_SIZE
)
1266 q
[template[i
].tap
[k
]] = 0;
1269 temp
+= template[i
].tap
[k
];
1272 skcipher_request_set_crypt(req
, sg
, (diff_dst
) ? sgout
: sg
,
1273 template[i
].ilen
, iv
);
1275 ret
= enc
? crypto_skcipher_encrypt(req
) :
1276 crypto_skcipher_decrypt(req
);
1283 wait_for_completion(&result
.completion
);
1284 reinit_completion(&result
.completion
);
1290 pr_err("alg: skcipher%s: %s failed on chunk test %d for %s: ret=%d\n",
1291 d
, e
, j
, algo
, -ret
);
1297 for (k
= 0; k
< template[i
].np
; k
++) {
1299 q
= xoutbuf
[IDX
[k
] >> PAGE_SHIFT
] +
1300 offset_in_page(IDX
[k
]);
1302 q
= xbuf
[IDX
[k
] >> PAGE_SHIFT
] +
1303 offset_in_page(IDX
[k
]);
1305 if (memcmp(q
, template[i
].result
+ temp
,
1306 template[i
].tap
[k
])) {
1307 pr_err("alg: skcipher%s: Chunk test %d failed on %s at page %u for %s\n",
1309 hexdump(q
, template[i
].tap
[k
]);
1313 q
+= template[i
].tap
[k
];
1314 for (n
= 0; offset_in_page(q
+ n
) && q
[n
]; n
++)
1317 pr_err("alg: skcipher%s: Result buffer corruption in chunk test %d on %s at page %u for %s: %u bytes:\n",
1318 d
, j
, e
, k
, algo
, n
);
1322 temp
+= template[i
].tap
[k
];
1329 skcipher_request_free(req
);
1331 testmgr_free_buf(xoutbuf
);
1333 testmgr_free_buf(xbuf
);
1338 static int test_skcipher(struct crypto_skcipher
*tfm
, int enc
,
1339 const struct cipher_testvec
*template,
1340 unsigned int tcount
)
1342 unsigned int alignmask
;
1345 /* test 'dst == src' case */
1346 ret
= __test_skcipher(tfm
, enc
, template, tcount
, false, 0);
1350 /* test 'dst != src' case */
1351 ret
= __test_skcipher(tfm
, enc
, template, tcount
, true, 0);
1355 /* test unaligned buffers, check with one byte offset */
1356 ret
= __test_skcipher(tfm
, enc
, template, tcount
, true, 1);
1360 alignmask
= crypto_tfm_alg_alignmask(&tfm
->base
);
1362 /* Check if alignment mask for tfm is correctly set. */
1363 ret
= __test_skcipher(tfm
, enc
, template, tcount
, true,
1372 static int test_comp(struct crypto_comp
*tfm
,
1373 const struct comp_testvec
*ctemplate
,
1374 const struct comp_testvec
*dtemplate
,
1375 int ctcount
, int dtcount
)
1377 const char *algo
= crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm
));
1379 char result
[COMP_BUF_SIZE
];
1382 for (i
= 0; i
< ctcount
; i
++) {
1384 unsigned int dlen
= COMP_BUF_SIZE
;
1386 memset(result
, 0, sizeof (result
));
1388 ilen
= ctemplate
[i
].inlen
;
1389 ret
= crypto_comp_compress(tfm
, ctemplate
[i
].input
,
1390 ilen
, result
, &dlen
);
1392 printk(KERN_ERR
"alg: comp: compression failed "
1393 "on test %d for %s: ret=%d\n", i
+ 1, algo
,
1398 if (dlen
!= ctemplate
[i
].outlen
) {
1399 printk(KERN_ERR
"alg: comp: Compression test %d "
1400 "failed for %s: output len = %d\n", i
+ 1, algo
,
1406 if (memcmp(result
, ctemplate
[i
].output
, dlen
)) {
1407 printk(KERN_ERR
"alg: comp: Compression test %d "
1408 "failed for %s\n", i
+ 1, algo
);
1409 hexdump(result
, dlen
);
1415 for (i
= 0; i
< dtcount
; i
++) {
1417 unsigned int dlen
= COMP_BUF_SIZE
;
1419 memset(result
, 0, sizeof (result
));
1421 ilen
= dtemplate
[i
].inlen
;
1422 ret
= crypto_comp_decompress(tfm
, dtemplate
[i
].input
,
1423 ilen
, result
, &dlen
);
1425 printk(KERN_ERR
"alg: comp: decompression failed "
1426 "on test %d for %s: ret=%d\n", i
+ 1, algo
,
1431 if (dlen
!= dtemplate
[i
].outlen
) {
1432 printk(KERN_ERR
"alg: comp: Decompression test %d "
1433 "failed for %s: output len = %d\n", i
+ 1, algo
,
1439 if (memcmp(result
, dtemplate
[i
].output
, dlen
)) {
1440 printk(KERN_ERR
"alg: comp: Decompression test %d "
1441 "failed for %s\n", i
+ 1, algo
);
1442 hexdump(result
, dlen
);
1454 static int test_acomp(struct crypto_acomp
*tfm
,
1455 const struct comp_testvec
*ctemplate
,
1456 const struct comp_testvec
*dtemplate
,
1457 int ctcount
, int dtcount
)
1459 const char *algo
= crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm
));
1463 struct scatterlist src
, dst
;
1464 struct acomp_req
*req
;
1465 struct tcrypt_result result
;
1467 output
= kmalloc(COMP_BUF_SIZE
, GFP_KERNEL
);
1471 for (i
= 0; i
< ctcount
; i
++) {
1472 unsigned int dlen
= COMP_BUF_SIZE
;
1473 int ilen
= ctemplate
[i
].inlen
;
1476 input_vec
= kmemdup(ctemplate
[i
].input
, ilen
, GFP_KERNEL
);
1482 memset(output
, 0, dlen
);
1483 init_completion(&result
.completion
);
1484 sg_init_one(&src
, input_vec
, ilen
);
1485 sg_init_one(&dst
, output
, dlen
);
1487 req
= acomp_request_alloc(tfm
);
1489 pr_err("alg: acomp: request alloc failed for %s\n",
1496 acomp_request_set_params(req
, &src
, &dst
, ilen
, dlen
);
1497 acomp_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
1498 tcrypt_complete
, &result
);
1500 ret
= wait_async_op(&result
, crypto_acomp_compress(req
));
1502 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
1505 acomp_request_free(req
);
1509 if (req
->dlen
!= ctemplate
[i
].outlen
) {
1510 pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
1511 i
+ 1, algo
, req
->dlen
);
1514 acomp_request_free(req
);
1518 if (memcmp(output
, ctemplate
[i
].output
, req
->dlen
)) {
1519 pr_err("alg: acomp: Compression test %d failed for %s\n",
1521 hexdump(output
, req
->dlen
);
1524 acomp_request_free(req
);
1529 acomp_request_free(req
);
1532 for (i
= 0; i
< dtcount
; i
++) {
1533 unsigned int dlen
= COMP_BUF_SIZE
;
1534 int ilen
= dtemplate
[i
].inlen
;
1537 input_vec
= kmemdup(dtemplate
[i
].input
, ilen
, GFP_KERNEL
);
1543 memset(output
, 0, dlen
);
1544 init_completion(&result
.completion
);
1545 sg_init_one(&src
, input_vec
, ilen
);
1546 sg_init_one(&dst
, output
, dlen
);
1548 req
= acomp_request_alloc(tfm
);
1550 pr_err("alg: acomp: request alloc failed for %s\n",
1557 acomp_request_set_params(req
, &src
, &dst
, ilen
, dlen
);
1558 acomp_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
1559 tcrypt_complete
, &result
);
1561 ret
= wait_async_op(&result
, crypto_acomp_decompress(req
));
1563 pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
1566 acomp_request_free(req
);
1570 if (req
->dlen
!= dtemplate
[i
].outlen
) {
1571 pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
1572 i
+ 1, algo
, req
->dlen
);
1575 acomp_request_free(req
);
1579 if (memcmp(output
, dtemplate
[i
].output
, req
->dlen
)) {
1580 pr_err("alg: acomp: Decompression test %d failed for %s\n",
1582 hexdump(output
, req
->dlen
);
1585 acomp_request_free(req
);
1590 acomp_request_free(req
);
1600 static int test_cprng(struct crypto_rng
*tfm
,
1601 const struct cprng_testvec
*template,
1602 unsigned int tcount
)
1604 const char *algo
= crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm
));
1605 int err
= 0, i
, j
, seedsize
;
1609 seedsize
= crypto_rng_seedsize(tfm
);
1611 seed
= kmalloc(seedsize
, GFP_KERNEL
);
1613 printk(KERN_ERR
"alg: cprng: Failed to allocate seed space "
1618 for (i
= 0; i
< tcount
; i
++) {
1619 memset(result
, 0, 32);
1621 memcpy(seed
, template[i
].v
, template[i
].vlen
);
1622 memcpy(seed
+ template[i
].vlen
, template[i
].key
,
1624 memcpy(seed
+ template[i
].vlen
+ template[i
].klen
,
1625 template[i
].dt
, template[i
].dtlen
);
1627 err
= crypto_rng_reset(tfm
, seed
, seedsize
);
1629 printk(KERN_ERR
"alg: cprng: Failed to reset rng "
1634 for (j
= 0; j
< template[i
].loops
; j
++) {
1635 err
= crypto_rng_get_bytes(tfm
, result
,
1638 printk(KERN_ERR
"alg: cprng: Failed to obtain "
1639 "the correct amount of random data for "
1640 "%s (requested %d)\n", algo
,
1646 err
= memcmp(result
, template[i
].result
,
1649 printk(KERN_ERR
"alg: cprng: Test %d failed for %s\n",
1651 hexdump(result
, template[i
].rlen
);
1662 static int alg_test_aead(const struct alg_test_desc
*desc
, const char *driver
,
1665 struct crypto_aead
*tfm
;
1668 tfm
= crypto_alloc_aead(driver
, type
, mask
);
1670 printk(KERN_ERR
"alg: aead: Failed to load transform for %s: "
1671 "%ld\n", driver
, PTR_ERR(tfm
));
1672 return PTR_ERR(tfm
);
1675 if (desc
->suite
.aead
.enc
.vecs
) {
1676 err
= test_aead(tfm
, ENCRYPT
, desc
->suite
.aead
.enc
.vecs
,
1677 desc
->suite
.aead
.enc
.count
);
1682 if (!err
&& desc
->suite
.aead
.dec
.vecs
)
1683 err
= test_aead(tfm
, DECRYPT
, desc
->suite
.aead
.dec
.vecs
,
1684 desc
->suite
.aead
.dec
.count
);
1687 crypto_free_aead(tfm
);
1691 static int alg_test_cipher(const struct alg_test_desc
*desc
,
1692 const char *driver
, u32 type
, u32 mask
)
1694 struct crypto_cipher
*tfm
;
1697 tfm
= crypto_alloc_cipher(driver
, type
, mask
);
1699 printk(KERN_ERR
"alg: cipher: Failed to load transform for "
1700 "%s: %ld\n", driver
, PTR_ERR(tfm
));
1701 return PTR_ERR(tfm
);
1704 if (desc
->suite
.cipher
.enc
.vecs
) {
1705 err
= test_cipher(tfm
, ENCRYPT
, desc
->suite
.cipher
.enc
.vecs
,
1706 desc
->suite
.cipher
.enc
.count
);
1711 if (desc
->suite
.cipher
.dec
.vecs
)
1712 err
= test_cipher(tfm
, DECRYPT
, desc
->suite
.cipher
.dec
.vecs
,
1713 desc
->suite
.cipher
.dec
.count
);
1716 crypto_free_cipher(tfm
);
1720 static int alg_test_skcipher(const struct alg_test_desc
*desc
,
1721 const char *driver
, u32 type
, u32 mask
)
1723 struct crypto_skcipher
*tfm
;
1726 tfm
= crypto_alloc_skcipher(driver
, type
, mask
);
1728 printk(KERN_ERR
"alg: skcipher: Failed to load transform for "
1729 "%s: %ld\n", driver
, PTR_ERR(tfm
));
1730 return PTR_ERR(tfm
);
1733 if (desc
->suite
.cipher
.enc
.vecs
) {
1734 err
= test_skcipher(tfm
, ENCRYPT
, desc
->suite
.cipher
.enc
.vecs
,
1735 desc
->suite
.cipher
.enc
.count
);
1740 if (desc
->suite
.cipher
.dec
.vecs
)
1741 err
= test_skcipher(tfm
, DECRYPT
, desc
->suite
.cipher
.dec
.vecs
,
1742 desc
->suite
.cipher
.dec
.count
);
1745 crypto_free_skcipher(tfm
);
1749 static int alg_test_comp(const struct alg_test_desc
*desc
, const char *driver
,
1752 struct crypto_comp
*comp
;
1753 struct crypto_acomp
*acomp
;
1755 u32 algo_type
= type
& CRYPTO_ALG_TYPE_ACOMPRESS_MASK
;
1757 if (algo_type
== CRYPTO_ALG_TYPE_ACOMPRESS
) {
1758 acomp
= crypto_alloc_acomp(driver
, type
, mask
);
1759 if (IS_ERR(acomp
)) {
1760 pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
1761 driver
, PTR_ERR(acomp
));
1762 return PTR_ERR(acomp
);
1764 err
= test_acomp(acomp
, desc
->suite
.comp
.comp
.vecs
,
1765 desc
->suite
.comp
.decomp
.vecs
,
1766 desc
->suite
.comp
.comp
.count
,
1767 desc
->suite
.comp
.decomp
.count
);
1768 crypto_free_acomp(acomp
);
1770 comp
= crypto_alloc_comp(driver
, type
, mask
);
1772 pr_err("alg: comp: Failed to load transform for %s: %ld\n",
1773 driver
, PTR_ERR(comp
));
1774 return PTR_ERR(comp
);
1777 err
= test_comp(comp
, desc
->suite
.comp
.comp
.vecs
,
1778 desc
->suite
.comp
.decomp
.vecs
,
1779 desc
->suite
.comp
.comp
.count
,
1780 desc
->suite
.comp
.decomp
.count
);
1782 crypto_free_comp(comp
);
1787 static int alg_test_hash(const struct alg_test_desc
*desc
, const char *driver
,
1790 struct crypto_ahash
*tfm
;
1793 tfm
= crypto_alloc_ahash(driver
, type
, mask
);
1795 printk(KERN_ERR
"alg: hash: Failed to load transform for %s: "
1796 "%ld\n", driver
, PTR_ERR(tfm
));
1797 return PTR_ERR(tfm
);
1800 err
= test_hash(tfm
, desc
->suite
.hash
.vecs
,
1801 desc
->suite
.hash
.count
, true);
1803 err
= test_hash(tfm
, desc
->suite
.hash
.vecs
,
1804 desc
->suite
.hash
.count
, false);
1806 crypto_free_ahash(tfm
);
1810 static int alg_test_crc32c(const struct alg_test_desc
*desc
,
1811 const char *driver
, u32 type
, u32 mask
)
1813 struct crypto_shash
*tfm
;
1817 err
= alg_test_hash(desc
, driver
, type
, mask
);
1821 tfm
= crypto_alloc_shash(driver
, type
, mask
);
1823 printk(KERN_ERR
"alg: crc32c: Failed to load transform for %s: "
1824 "%ld\n", driver
, PTR_ERR(tfm
));
1830 SHASH_DESC_ON_STACK(shash
, tfm
);
1831 u32
*ctx
= (u32
*)shash_desc_ctx(shash
);
1836 *ctx
= le32_to_cpu(420553207);
1837 err
= crypto_shash_final(shash
, (u8
*)&val
);
1839 printk(KERN_ERR
"alg: crc32c: Operation failed for "
1840 "%s: %d\n", driver
, err
);
1844 if (val
!= ~420553207) {
1845 printk(KERN_ERR
"alg: crc32c: Test failed for %s: "
1846 "%d\n", driver
, val
);
1851 crypto_free_shash(tfm
);
1857 static int alg_test_cprng(const struct alg_test_desc
*desc
, const char *driver
,
1860 struct crypto_rng
*rng
;
1863 rng
= crypto_alloc_rng(driver
, type
, mask
);
1865 printk(KERN_ERR
"alg: cprng: Failed to load transform for %s: "
1866 "%ld\n", driver
, PTR_ERR(rng
));
1867 return PTR_ERR(rng
);
1870 err
= test_cprng(rng
, desc
->suite
.cprng
.vecs
, desc
->suite
.cprng
.count
);
1872 crypto_free_rng(rng
);
1878 static int drbg_cavs_test(const struct drbg_testvec
*test
, int pr
,
1879 const char *driver
, u32 type
, u32 mask
)
1882 struct crypto_rng
*drng
;
1883 struct drbg_test_data test_data
;
1884 struct drbg_string addtl
, pers
, testentropy
;
1885 unsigned char *buf
= kzalloc(test
->expectedlen
, GFP_KERNEL
);
1890 drng
= crypto_alloc_rng(driver
, type
, mask
);
1892 printk(KERN_ERR
"alg: drbg: could not allocate DRNG handle for "
1898 test_data
.testentropy
= &testentropy
;
1899 drbg_string_fill(&testentropy
, test
->entropy
, test
->entropylen
);
1900 drbg_string_fill(&pers
, test
->pers
, test
->perslen
);
1901 ret
= crypto_drbg_reset_test(drng
, &pers
, &test_data
);
1903 printk(KERN_ERR
"alg: drbg: Failed to reset rng\n");
1907 drbg_string_fill(&addtl
, test
->addtla
, test
->addtllen
);
1909 drbg_string_fill(&testentropy
, test
->entpra
, test
->entprlen
);
1910 ret
= crypto_drbg_get_bytes_addtl_test(drng
,
1911 buf
, test
->expectedlen
, &addtl
, &test_data
);
1913 ret
= crypto_drbg_get_bytes_addtl(drng
,
1914 buf
, test
->expectedlen
, &addtl
);
1917 printk(KERN_ERR
"alg: drbg: could not obtain random data for "
1918 "driver %s\n", driver
);
1922 drbg_string_fill(&addtl
, test
->addtlb
, test
->addtllen
);
1924 drbg_string_fill(&testentropy
, test
->entprb
, test
->entprlen
);
1925 ret
= crypto_drbg_get_bytes_addtl_test(drng
,
1926 buf
, test
->expectedlen
, &addtl
, &test_data
);
1928 ret
= crypto_drbg_get_bytes_addtl(drng
,
1929 buf
, test
->expectedlen
, &addtl
);
1932 printk(KERN_ERR
"alg: drbg: could not obtain random data for "
1933 "driver %s\n", driver
);
1937 ret
= memcmp(test
->expected
, buf
, test
->expectedlen
);
1940 crypto_free_rng(drng
);
1946 static int alg_test_drbg(const struct alg_test_desc
*desc
, const char *driver
,
1952 const struct drbg_testvec
*template = desc
->suite
.drbg
.vecs
;
1953 unsigned int tcount
= desc
->suite
.drbg
.count
;
1955 if (0 == memcmp(driver
, "drbg_pr_", 8))
1958 for (i
= 0; i
< tcount
; i
++) {
1959 err
= drbg_cavs_test(&template[i
], pr
, driver
, type
, mask
);
1961 printk(KERN_ERR
"alg: drbg: Test %d failed for %s\n",
1971 static int do_test_kpp(struct crypto_kpp
*tfm
, const struct kpp_testvec
*vec
,
1974 struct kpp_request
*req
;
1975 void *input_buf
= NULL
;
1976 void *output_buf
= NULL
;
1977 struct tcrypt_result result
;
1978 unsigned int out_len_max
;
1980 struct scatterlist src
, dst
;
1982 req
= kpp_request_alloc(tfm
, GFP_KERNEL
);
1986 init_completion(&result
.completion
);
1988 err
= crypto_kpp_set_secret(tfm
, vec
->secret
, vec
->secret_size
);
1992 out_len_max
= crypto_kpp_maxsize(tfm
);
1993 output_buf
= kzalloc(out_len_max
, GFP_KERNEL
);
1999 /* Use appropriate parameter as base */
2000 kpp_request_set_input(req
, NULL
, 0);
2001 sg_init_one(&dst
, output_buf
, out_len_max
);
2002 kpp_request_set_output(req
, &dst
, out_len_max
);
2003 kpp_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
2004 tcrypt_complete
, &result
);
2006 /* Compute public key */
2007 err
= wait_async_op(&result
, crypto_kpp_generate_public_key(req
));
2009 pr_err("alg: %s: generate public key test failed. err %d\n",
2013 /* Verify calculated public key */
2014 if (memcmp(vec
->expected_a_public
, sg_virt(req
->dst
),
2015 vec
->expected_a_public_size
)) {
2016 pr_err("alg: %s: generate public key test failed. Invalid output\n",
2022 /* Calculate shared secret key by using counter part (b) public key. */
2023 input_buf
= kzalloc(vec
->b_public_size
, GFP_KERNEL
);
2029 memcpy(input_buf
, vec
->b_public
, vec
->b_public_size
);
2030 sg_init_one(&src
, input_buf
, vec
->b_public_size
);
2031 sg_init_one(&dst
, output_buf
, out_len_max
);
2032 kpp_request_set_input(req
, &src
, vec
->b_public_size
);
2033 kpp_request_set_output(req
, &dst
, out_len_max
);
2034 kpp_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
2035 tcrypt_complete
, &result
);
2036 err
= wait_async_op(&result
, crypto_kpp_compute_shared_secret(req
));
2038 pr_err("alg: %s: compute shard secret test failed. err %d\n",
2043 * verify shared secret from which the user will derive
2044 * secret key by executing whatever hash it has chosen
2046 if (memcmp(vec
->expected_ss
, sg_virt(req
->dst
),
2047 vec
->expected_ss_size
)) {
2048 pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
2058 kpp_request_free(req
);
2062 static int test_kpp(struct crypto_kpp
*tfm
, const char *alg
,
2063 const struct kpp_testvec
*vecs
, unsigned int tcount
)
2067 for (i
= 0; i
< tcount
; i
++) {
2068 ret
= do_test_kpp(tfm
, vecs
++, alg
);
2070 pr_err("alg: %s: test failed on vector %d, err=%d\n",
2078 static int alg_test_kpp(const struct alg_test_desc
*desc
, const char *driver
,
2081 struct crypto_kpp
*tfm
;
2084 tfm
= crypto_alloc_kpp(driver
, type
, mask
);
2086 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
2087 driver
, PTR_ERR(tfm
));
2088 return PTR_ERR(tfm
);
2090 if (desc
->suite
.kpp
.vecs
)
2091 err
= test_kpp(tfm
, desc
->alg
, desc
->suite
.kpp
.vecs
,
2092 desc
->suite
.kpp
.count
);
2094 crypto_free_kpp(tfm
);
2098 static int test_akcipher_one(struct crypto_akcipher
*tfm
,
2099 const struct akcipher_testvec
*vecs
)
2101 char *xbuf
[XBUFSIZE
];
2102 struct akcipher_request
*req
;
2103 void *outbuf_enc
= NULL
;
2104 void *outbuf_dec
= NULL
;
2105 struct tcrypt_result result
;
2106 unsigned int out_len_max
, out_len
= 0;
2108 struct scatterlist src
, dst
, src_tab
[2];
2110 if (testmgr_alloc_buf(xbuf
))
2113 req
= akcipher_request_alloc(tfm
, GFP_KERNEL
);
2117 init_completion(&result
.completion
);
2119 if (vecs
->public_key_vec
)
2120 err
= crypto_akcipher_set_pub_key(tfm
, vecs
->key
,
2123 err
= crypto_akcipher_set_priv_key(tfm
, vecs
->key
,
2129 out_len_max
= crypto_akcipher_maxsize(tfm
);
2130 outbuf_enc
= kzalloc(out_len_max
, GFP_KERNEL
);
2134 if (WARN_ON(vecs
->m_size
> PAGE_SIZE
))
2137 memcpy(xbuf
[0], vecs
->m
, vecs
->m_size
);
2139 sg_init_table(src_tab
, 2);
2140 sg_set_buf(&src_tab
[0], xbuf
[0], 8);
2141 sg_set_buf(&src_tab
[1], xbuf
[0] + 8, vecs
->m_size
- 8);
2142 sg_init_one(&dst
, outbuf_enc
, out_len_max
);
2143 akcipher_request_set_crypt(req
, src_tab
, &dst
, vecs
->m_size
,
2145 akcipher_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
2146 tcrypt_complete
, &result
);
2148 /* Run RSA encrypt - c = m^e mod n;*/
2149 err
= wait_async_op(&result
, crypto_akcipher_encrypt(req
));
2151 pr_err("alg: akcipher: encrypt test failed. err %d\n", err
);
2154 if (req
->dst_len
!= vecs
->c_size
) {
2155 pr_err("alg: akcipher: encrypt test failed. Invalid output len\n");
2159 /* verify that encrypted message is equal to expected */
2160 if (memcmp(vecs
->c
, outbuf_enc
, vecs
->c_size
)) {
2161 pr_err("alg: akcipher: encrypt test failed. Invalid output\n");
2162 hexdump(outbuf_enc
, vecs
->c_size
);
2166 /* Don't invoke decrypt for vectors with public key */
2167 if (vecs
->public_key_vec
) {
2171 outbuf_dec
= kzalloc(out_len_max
, GFP_KERNEL
);
2177 if (WARN_ON(vecs
->c_size
> PAGE_SIZE
))
2180 memcpy(xbuf
[0], vecs
->c
, vecs
->c_size
);
2182 sg_init_one(&src
, xbuf
[0], vecs
->c_size
);
2183 sg_init_one(&dst
, outbuf_dec
, out_len_max
);
2184 init_completion(&result
.completion
);
2185 akcipher_request_set_crypt(req
, &src
, &dst
, vecs
->c_size
, out_len_max
);
2187 /* Run RSA decrypt - m = c^d mod n;*/
2188 err
= wait_async_op(&result
, crypto_akcipher_decrypt(req
));
2190 pr_err("alg: akcipher: decrypt test failed. err %d\n", err
);
2193 out_len
= req
->dst_len
;
2194 if (out_len
< vecs
->m_size
) {
2195 pr_err("alg: akcipher: decrypt test failed. "
2196 "Invalid output len %u\n", out_len
);
2200 /* verify that decrypted message is equal to the original msg */
2201 if (memchr_inv(outbuf_dec
, 0, out_len
- vecs
->m_size
) ||
2202 memcmp(vecs
->m
, outbuf_dec
+ out_len
- vecs
->m_size
,
2204 pr_err("alg: akcipher: decrypt test failed. Invalid output\n");
2205 hexdump(outbuf_dec
, out_len
);
2212 akcipher_request_free(req
);
2214 testmgr_free_buf(xbuf
);
2218 static int test_akcipher(struct crypto_akcipher
*tfm
, const char *alg
,
2219 const struct akcipher_testvec
*vecs
,
2220 unsigned int tcount
)
2223 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm
));
2226 for (i
= 0; i
< tcount
; i
++) {
2227 ret
= test_akcipher_one(tfm
, vecs
++);
2231 pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
2238 static int alg_test_akcipher(const struct alg_test_desc
*desc
,
2239 const char *driver
, u32 type
, u32 mask
)
2241 struct crypto_akcipher
*tfm
;
2244 tfm
= crypto_alloc_akcipher(driver
, type
, mask
);
2246 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
2247 driver
, PTR_ERR(tfm
));
2248 return PTR_ERR(tfm
);
2250 if (desc
->suite
.akcipher
.vecs
)
2251 err
= test_akcipher(tfm
, desc
->alg
, desc
->suite
.akcipher
.vecs
,
2252 desc
->suite
.akcipher
.count
);
2254 crypto_free_akcipher(tfm
);
2258 static int alg_test_null(const struct alg_test_desc
*desc
,
2259 const char *driver
, u32 type
, u32 mask
)
2264 #define __VECS(tv) { .vecs = tv, .count = ARRAY_SIZE(tv) }
2266 /* Please keep this list sorted by algorithm name. */
2267 static const struct alg_test_desc alg_test_descs
[] = {
2269 .alg
= "ansi_cprng",
2270 .test
= alg_test_cprng
,
2272 .cprng
= __VECS(ansi_cprng_aes_tv_template
)
2275 .alg
= "authenc(hmac(md5),ecb(cipher_null))",
2276 .test
= alg_test_aead
,
2279 .enc
= __VECS(hmac_md5_ecb_cipher_null_enc_tv_template
),
2280 .dec
= __VECS(hmac_md5_ecb_cipher_null_dec_tv_template
)
2284 .alg
= "authenc(hmac(sha1),cbc(aes))",
2285 .test
= alg_test_aead
,
2288 .enc
= __VECS(hmac_sha1_aes_cbc_enc_tv_temp
)
2292 .alg
= "authenc(hmac(sha1),cbc(des))",
2293 .test
= alg_test_aead
,
2296 .enc
= __VECS(hmac_sha1_des_cbc_enc_tv_temp
)
2300 .alg
= "authenc(hmac(sha1),cbc(des3_ede))",
2301 .test
= alg_test_aead
,
2305 .enc
= __VECS(hmac_sha1_des3_ede_cbc_enc_tv_temp
)
2309 .alg
= "authenc(hmac(sha1),ctr(aes))",
2310 .test
= alg_test_null
,
2313 .alg
= "authenc(hmac(sha1),ecb(cipher_null))",
2314 .test
= alg_test_aead
,
2317 .enc
= __VECS(hmac_sha1_ecb_cipher_null_enc_tv_temp
),
2318 .dec
= __VECS(hmac_sha1_ecb_cipher_null_dec_tv_temp
)
2322 .alg
= "authenc(hmac(sha1),rfc3686(ctr(aes)))",
2323 .test
= alg_test_null
,
2326 .alg
= "authenc(hmac(sha224),cbc(des))",
2327 .test
= alg_test_aead
,
2330 .enc
= __VECS(hmac_sha224_des_cbc_enc_tv_temp
)
2334 .alg
= "authenc(hmac(sha224),cbc(des3_ede))",
2335 .test
= alg_test_aead
,
2339 .enc
= __VECS(hmac_sha224_des3_ede_cbc_enc_tv_temp
)
2343 .alg
= "authenc(hmac(sha256),cbc(aes))",
2344 .test
= alg_test_aead
,
2348 .enc
= __VECS(hmac_sha256_aes_cbc_enc_tv_temp
)
2352 .alg
= "authenc(hmac(sha256),cbc(des))",
2353 .test
= alg_test_aead
,
2356 .enc
= __VECS(hmac_sha256_des_cbc_enc_tv_temp
)
2360 .alg
= "authenc(hmac(sha256),cbc(des3_ede))",
2361 .test
= alg_test_aead
,
2365 .enc
= __VECS(hmac_sha256_des3_ede_cbc_enc_tv_temp
)
2369 .alg
= "authenc(hmac(sha256),ctr(aes))",
2370 .test
= alg_test_null
,
2373 .alg
= "authenc(hmac(sha256),rfc3686(ctr(aes)))",
2374 .test
= alg_test_null
,
2377 .alg
= "authenc(hmac(sha384),cbc(des))",
2378 .test
= alg_test_aead
,
2381 .enc
= __VECS(hmac_sha384_des_cbc_enc_tv_temp
)
2385 .alg
= "authenc(hmac(sha384),cbc(des3_ede))",
2386 .test
= alg_test_aead
,
2390 .enc
= __VECS(hmac_sha384_des3_ede_cbc_enc_tv_temp
)
2394 .alg
= "authenc(hmac(sha384),ctr(aes))",
2395 .test
= alg_test_null
,
2398 .alg
= "authenc(hmac(sha384),rfc3686(ctr(aes)))",
2399 .test
= alg_test_null
,
2402 .alg
= "authenc(hmac(sha512),cbc(aes))",
2404 .test
= alg_test_aead
,
2407 .enc
= __VECS(hmac_sha512_aes_cbc_enc_tv_temp
)
2411 .alg
= "authenc(hmac(sha512),cbc(des))",
2412 .test
= alg_test_aead
,
2415 .enc
= __VECS(hmac_sha512_des_cbc_enc_tv_temp
)
2419 .alg
= "authenc(hmac(sha512),cbc(des3_ede))",
2420 .test
= alg_test_aead
,
2424 .enc
= __VECS(hmac_sha512_des3_ede_cbc_enc_tv_temp
)
2428 .alg
= "authenc(hmac(sha512),ctr(aes))",
2429 .test
= alg_test_null
,
2432 .alg
= "authenc(hmac(sha512),rfc3686(ctr(aes)))",
2433 .test
= alg_test_null
,
2437 .test
= alg_test_skcipher
,
2441 .enc
= __VECS(aes_cbc_enc_tv_template
),
2442 .dec
= __VECS(aes_cbc_dec_tv_template
)
2446 .alg
= "cbc(anubis)",
2447 .test
= alg_test_skcipher
,
2450 .enc
= __VECS(anubis_cbc_enc_tv_template
),
2451 .dec
= __VECS(anubis_cbc_dec_tv_template
)
2455 .alg
= "cbc(blowfish)",
2456 .test
= alg_test_skcipher
,
2459 .enc
= __VECS(bf_cbc_enc_tv_template
),
2460 .dec
= __VECS(bf_cbc_dec_tv_template
)
2464 .alg
= "cbc(camellia)",
2465 .test
= alg_test_skcipher
,
2468 .enc
= __VECS(camellia_cbc_enc_tv_template
),
2469 .dec
= __VECS(camellia_cbc_dec_tv_template
)
2473 .alg
= "cbc(cast5)",
2474 .test
= alg_test_skcipher
,
2477 .enc
= __VECS(cast5_cbc_enc_tv_template
),
2478 .dec
= __VECS(cast5_cbc_dec_tv_template
)
2482 .alg
= "cbc(cast6)",
2483 .test
= alg_test_skcipher
,
2486 .enc
= __VECS(cast6_cbc_enc_tv_template
),
2487 .dec
= __VECS(cast6_cbc_dec_tv_template
)
2492 .test
= alg_test_skcipher
,
2495 .enc
= __VECS(des_cbc_enc_tv_template
),
2496 .dec
= __VECS(des_cbc_dec_tv_template
)
2500 .alg
= "cbc(des3_ede)",
2501 .test
= alg_test_skcipher
,
2505 .enc
= __VECS(des3_ede_cbc_enc_tv_template
),
2506 .dec
= __VECS(des3_ede_cbc_dec_tv_template
)
2510 .alg
= "cbc(serpent)",
2511 .test
= alg_test_skcipher
,
2514 .enc
= __VECS(serpent_cbc_enc_tv_template
),
2515 .dec
= __VECS(serpent_cbc_dec_tv_template
)
2519 .alg
= "cbc(twofish)",
2520 .test
= alg_test_skcipher
,
2523 .enc
= __VECS(tf_cbc_enc_tv_template
),
2524 .dec
= __VECS(tf_cbc_dec_tv_template
)
2528 .alg
= "cbcmac(aes)",
2530 .test
= alg_test_hash
,
2532 .hash
= __VECS(aes_cbcmac_tv_template
)
2536 .test
= alg_test_aead
,
2540 .enc
= __VECS(aes_ccm_enc_tv_template
),
2541 .dec
= __VECS(aes_ccm_dec_tv_template
)
2546 .test
= alg_test_skcipher
,
2549 .enc
= __VECS(chacha20_enc_tv_template
),
2550 .dec
= __VECS(chacha20_enc_tv_template
),
2556 .test
= alg_test_hash
,
2558 .hash
= __VECS(aes_cmac128_tv_template
)
2561 .alg
= "cmac(des3_ede)",
2563 .test
= alg_test_hash
,
2565 .hash
= __VECS(des3_ede_cmac64_tv_template
)
2568 .alg
= "compress_null",
2569 .test
= alg_test_null
,
2572 .test
= alg_test_hash
,
2574 .hash
= __VECS(crc32_tv_template
)
2578 .test
= alg_test_crc32c
,
2581 .hash
= __VECS(crc32c_tv_template
)
2585 .test
= alg_test_hash
,
2588 .hash
= __VECS(crct10dif_tv_template
)
2592 .test
= alg_test_skcipher
,
2596 .enc
= __VECS(aes_ctr_enc_tv_template
),
2597 .dec
= __VECS(aes_ctr_dec_tv_template
)
2601 .alg
= "ctr(blowfish)",
2602 .test
= alg_test_skcipher
,
2605 .enc
= __VECS(bf_ctr_enc_tv_template
),
2606 .dec
= __VECS(bf_ctr_dec_tv_template
)
2610 .alg
= "ctr(camellia)",
2611 .test
= alg_test_skcipher
,
2614 .enc
= __VECS(camellia_ctr_enc_tv_template
),
2615 .dec
= __VECS(camellia_ctr_dec_tv_template
)
2619 .alg
= "ctr(cast5)",
2620 .test
= alg_test_skcipher
,
2623 .enc
= __VECS(cast5_ctr_enc_tv_template
),
2624 .dec
= __VECS(cast5_ctr_dec_tv_template
)
2628 .alg
= "ctr(cast6)",
2629 .test
= alg_test_skcipher
,
2632 .enc
= __VECS(cast6_ctr_enc_tv_template
),
2633 .dec
= __VECS(cast6_ctr_dec_tv_template
)
2638 .test
= alg_test_skcipher
,
2641 .enc
= __VECS(des_ctr_enc_tv_template
),
2642 .dec
= __VECS(des_ctr_dec_tv_template
)
2646 .alg
= "ctr(des3_ede)",
2647 .test
= alg_test_skcipher
,
2650 .enc
= __VECS(des3_ede_ctr_enc_tv_template
),
2651 .dec
= __VECS(des3_ede_ctr_dec_tv_template
)
2655 .alg
= "ctr(serpent)",
2656 .test
= alg_test_skcipher
,
2659 .enc
= __VECS(serpent_ctr_enc_tv_template
),
2660 .dec
= __VECS(serpent_ctr_dec_tv_template
)
2664 .alg
= "ctr(twofish)",
2665 .test
= alg_test_skcipher
,
2668 .enc
= __VECS(tf_ctr_enc_tv_template
),
2669 .dec
= __VECS(tf_ctr_dec_tv_template
)
2673 .alg
= "cts(cbc(aes))",
2674 .test
= alg_test_skcipher
,
2677 .enc
= __VECS(cts_mode_enc_tv_template
),
2678 .dec
= __VECS(cts_mode_dec_tv_template
)
2683 .test
= alg_test_comp
,
2687 .comp
= __VECS(deflate_comp_tv_template
),
2688 .decomp
= __VECS(deflate_decomp_tv_template
)
2693 .test
= alg_test_kpp
,
2696 .kpp
= __VECS(dh_tv_template
)
2699 .alg
= "digest_null",
2700 .test
= alg_test_null
,
2702 .alg
= "drbg_nopr_ctr_aes128",
2703 .test
= alg_test_drbg
,
2706 .drbg
= __VECS(drbg_nopr_ctr_aes128_tv_template
)
2709 .alg
= "drbg_nopr_ctr_aes192",
2710 .test
= alg_test_drbg
,
2713 .drbg
= __VECS(drbg_nopr_ctr_aes192_tv_template
)
2716 .alg
= "drbg_nopr_ctr_aes256",
2717 .test
= alg_test_drbg
,
2720 .drbg
= __VECS(drbg_nopr_ctr_aes256_tv_template
)
2724 * There is no need to specifically test the DRBG with every
2725 * backend cipher -- covered by drbg_nopr_hmac_sha256 test
2727 .alg
= "drbg_nopr_hmac_sha1",
2729 .test
= alg_test_null
,
2731 .alg
= "drbg_nopr_hmac_sha256",
2732 .test
= alg_test_drbg
,
2735 .drbg
= __VECS(drbg_nopr_hmac_sha256_tv_template
)
2738 /* covered by drbg_nopr_hmac_sha256 test */
2739 .alg
= "drbg_nopr_hmac_sha384",
2741 .test
= alg_test_null
,
2743 .alg
= "drbg_nopr_hmac_sha512",
2744 .test
= alg_test_null
,
2747 .alg
= "drbg_nopr_sha1",
2749 .test
= alg_test_null
,
2751 .alg
= "drbg_nopr_sha256",
2752 .test
= alg_test_drbg
,
2755 .drbg
= __VECS(drbg_nopr_sha256_tv_template
)
2758 /* covered by drbg_nopr_sha256 test */
2759 .alg
= "drbg_nopr_sha384",
2761 .test
= alg_test_null
,
2763 .alg
= "drbg_nopr_sha512",
2765 .test
= alg_test_null
,
2767 .alg
= "drbg_pr_ctr_aes128",
2768 .test
= alg_test_drbg
,
2771 .drbg
= __VECS(drbg_pr_ctr_aes128_tv_template
)
2774 /* covered by drbg_pr_ctr_aes128 test */
2775 .alg
= "drbg_pr_ctr_aes192",
2777 .test
= alg_test_null
,
2779 .alg
= "drbg_pr_ctr_aes256",
2781 .test
= alg_test_null
,
2783 .alg
= "drbg_pr_hmac_sha1",
2785 .test
= alg_test_null
,
2787 .alg
= "drbg_pr_hmac_sha256",
2788 .test
= alg_test_drbg
,
2791 .drbg
= __VECS(drbg_pr_hmac_sha256_tv_template
)
2794 /* covered by drbg_pr_hmac_sha256 test */
2795 .alg
= "drbg_pr_hmac_sha384",
2797 .test
= alg_test_null
,
2799 .alg
= "drbg_pr_hmac_sha512",
2800 .test
= alg_test_null
,
2803 .alg
= "drbg_pr_sha1",
2805 .test
= alg_test_null
,
2807 .alg
= "drbg_pr_sha256",
2808 .test
= alg_test_drbg
,
2811 .drbg
= __VECS(drbg_pr_sha256_tv_template
)
2814 /* covered by drbg_pr_sha256 test */
2815 .alg
= "drbg_pr_sha384",
2817 .test
= alg_test_null
,
2819 .alg
= "drbg_pr_sha512",
2821 .test
= alg_test_null
,
2824 .test
= alg_test_skcipher
,
2828 .enc
= __VECS(aes_enc_tv_template
),
2829 .dec
= __VECS(aes_dec_tv_template
)
2833 .alg
= "ecb(anubis)",
2834 .test
= alg_test_skcipher
,
2837 .enc
= __VECS(anubis_enc_tv_template
),
2838 .dec
= __VECS(anubis_dec_tv_template
)
2843 .test
= alg_test_skcipher
,
2846 .enc
= __VECS(arc4_enc_tv_template
),
2847 .dec
= __VECS(arc4_dec_tv_template
)
2851 .alg
= "ecb(blowfish)",
2852 .test
= alg_test_skcipher
,
2855 .enc
= __VECS(bf_enc_tv_template
),
2856 .dec
= __VECS(bf_dec_tv_template
)
2860 .alg
= "ecb(camellia)",
2861 .test
= alg_test_skcipher
,
2864 .enc
= __VECS(camellia_enc_tv_template
),
2865 .dec
= __VECS(camellia_dec_tv_template
)
2869 .alg
= "ecb(cast5)",
2870 .test
= alg_test_skcipher
,
2873 .enc
= __VECS(cast5_enc_tv_template
),
2874 .dec
= __VECS(cast5_dec_tv_template
)
2878 .alg
= "ecb(cast6)",
2879 .test
= alg_test_skcipher
,
2882 .enc
= __VECS(cast6_enc_tv_template
),
2883 .dec
= __VECS(cast6_dec_tv_template
)
2887 .alg
= "ecb(cipher_null)",
2888 .test
= alg_test_null
,
2891 .test
= alg_test_skcipher
,
2894 .enc
= __VECS(des_enc_tv_template
),
2895 .dec
= __VECS(des_dec_tv_template
)
2899 .alg
= "ecb(des3_ede)",
2900 .test
= alg_test_skcipher
,
2904 .enc
= __VECS(des3_ede_enc_tv_template
),
2905 .dec
= __VECS(des3_ede_dec_tv_template
)
2909 .alg
= "ecb(fcrypt)",
2910 .test
= alg_test_skcipher
,
2914 .vecs
= fcrypt_pcbc_enc_tv_template
,
2918 .vecs
= fcrypt_pcbc_dec_tv_template
,
2924 .alg
= "ecb(khazad)",
2925 .test
= alg_test_skcipher
,
2928 .enc
= __VECS(khazad_enc_tv_template
),
2929 .dec
= __VECS(khazad_dec_tv_template
)
2934 .test
= alg_test_skcipher
,
2937 .enc
= __VECS(seed_enc_tv_template
),
2938 .dec
= __VECS(seed_dec_tv_template
)
2942 .alg
= "ecb(serpent)",
2943 .test
= alg_test_skcipher
,
2946 .enc
= __VECS(serpent_enc_tv_template
),
2947 .dec
= __VECS(serpent_dec_tv_template
)
2952 .test
= alg_test_skcipher
,
2955 .enc
= __VECS(tea_enc_tv_template
),
2956 .dec
= __VECS(tea_dec_tv_template
)
2960 .alg
= "ecb(tnepres)",
2961 .test
= alg_test_skcipher
,
2964 .enc
= __VECS(tnepres_enc_tv_template
),
2965 .dec
= __VECS(tnepres_dec_tv_template
)
2969 .alg
= "ecb(twofish)",
2970 .test
= alg_test_skcipher
,
2973 .enc
= __VECS(tf_enc_tv_template
),
2974 .dec
= __VECS(tf_dec_tv_template
)
2979 .test
= alg_test_skcipher
,
2982 .enc
= __VECS(xeta_enc_tv_template
),
2983 .dec
= __VECS(xeta_dec_tv_template
)
2988 .test
= alg_test_skcipher
,
2991 .enc
= __VECS(xtea_enc_tv_template
),
2992 .dec
= __VECS(xtea_dec_tv_template
)
2997 .test
= alg_test_kpp
,
3000 .kpp
= __VECS(ecdh_tv_template
)
3004 .test
= alg_test_aead
,
3008 .enc
= __VECS(aes_gcm_enc_tv_template
),
3009 .dec
= __VECS(aes_gcm_dec_tv_template
)
3014 .test
= alg_test_hash
,
3017 .hash
= __VECS(ghash_tv_template
)
3020 .alg
= "hmac(crc32)",
3021 .test
= alg_test_hash
,
3023 .hash
= __VECS(bfin_crc_tv_template
)
3027 .test
= alg_test_hash
,
3029 .hash
= __VECS(hmac_md5_tv_template
)
3032 .alg
= "hmac(rmd128)",
3033 .test
= alg_test_hash
,
3035 .hash
= __VECS(hmac_rmd128_tv_template
)
3038 .alg
= "hmac(rmd160)",
3039 .test
= alg_test_hash
,
3041 .hash
= __VECS(hmac_rmd160_tv_template
)
3044 .alg
= "hmac(sha1)",
3045 .test
= alg_test_hash
,
3048 .hash
= __VECS(hmac_sha1_tv_template
)
3051 .alg
= "hmac(sha224)",
3052 .test
= alg_test_hash
,
3055 .hash
= __VECS(hmac_sha224_tv_template
)
3058 .alg
= "hmac(sha256)",
3059 .test
= alg_test_hash
,
3062 .hash
= __VECS(hmac_sha256_tv_template
)
3065 .alg
= "hmac(sha3-224)",
3066 .test
= alg_test_hash
,
3069 .hash
= __VECS(hmac_sha3_224_tv_template
)
3072 .alg
= "hmac(sha3-256)",
3073 .test
= alg_test_hash
,
3076 .hash
= __VECS(hmac_sha3_256_tv_template
)
3079 .alg
= "hmac(sha3-384)",
3080 .test
= alg_test_hash
,
3083 .hash
= __VECS(hmac_sha3_384_tv_template
)
3086 .alg
= "hmac(sha3-512)",
3087 .test
= alg_test_hash
,
3090 .hash
= __VECS(hmac_sha3_512_tv_template
)
3093 .alg
= "hmac(sha384)",
3094 .test
= alg_test_hash
,
3097 .hash
= __VECS(hmac_sha384_tv_template
)
3100 .alg
= "hmac(sha512)",
3101 .test
= alg_test_hash
,
3104 .hash
= __VECS(hmac_sha512_tv_template
)
3107 .alg
= "jitterentropy_rng",
3109 .test
= alg_test_null
,
3112 .test
= alg_test_skcipher
,
3116 .enc
= __VECS(aes_kw_enc_tv_template
),
3117 .dec
= __VECS(aes_kw_dec_tv_template
)
3122 .test
= alg_test_skcipher
,
3125 .enc
= __VECS(aes_lrw_enc_tv_template
),
3126 .dec
= __VECS(aes_lrw_dec_tv_template
)
3130 .alg
= "lrw(camellia)",
3131 .test
= alg_test_skcipher
,
3134 .enc
= __VECS(camellia_lrw_enc_tv_template
),
3135 .dec
= __VECS(camellia_lrw_dec_tv_template
)
3139 .alg
= "lrw(cast6)",
3140 .test
= alg_test_skcipher
,
3143 .enc
= __VECS(cast6_lrw_enc_tv_template
),
3144 .dec
= __VECS(cast6_lrw_dec_tv_template
)
3148 .alg
= "lrw(serpent)",
3149 .test
= alg_test_skcipher
,
3152 .enc
= __VECS(serpent_lrw_enc_tv_template
),
3153 .dec
= __VECS(serpent_lrw_dec_tv_template
)
3157 .alg
= "lrw(twofish)",
3158 .test
= alg_test_skcipher
,
3161 .enc
= __VECS(tf_lrw_enc_tv_template
),
3162 .dec
= __VECS(tf_lrw_dec_tv_template
)
3167 .test
= alg_test_comp
,
3171 .comp
= __VECS(lz4_comp_tv_template
),
3172 .decomp
= __VECS(lz4_decomp_tv_template
)
3177 .test
= alg_test_comp
,
3181 .comp
= __VECS(lz4hc_comp_tv_template
),
3182 .decomp
= __VECS(lz4hc_decomp_tv_template
)
3187 .test
= alg_test_comp
,
3191 .comp
= __VECS(lzo_comp_tv_template
),
3192 .decomp
= __VECS(lzo_decomp_tv_template
)
3197 .test
= alg_test_hash
,
3199 .hash
= __VECS(md4_tv_template
)
3203 .test
= alg_test_hash
,
3205 .hash
= __VECS(md5_tv_template
)
3208 .alg
= "michael_mic",
3209 .test
= alg_test_hash
,
3211 .hash
= __VECS(michael_mic_tv_template
)
3215 .test
= alg_test_skcipher
,
3219 .enc
= __VECS(aes_ofb_enc_tv_template
),
3220 .dec
= __VECS(aes_ofb_dec_tv_template
)
3224 .alg
= "pcbc(fcrypt)",
3225 .test
= alg_test_skcipher
,
3228 .enc
= __VECS(fcrypt_pcbc_enc_tv_template
),
3229 .dec
= __VECS(fcrypt_pcbc_dec_tv_template
)
3234 .test
= alg_test_hash
,
3236 .hash
= __VECS(poly1305_tv_template
)
3239 .alg
= "rfc3686(ctr(aes))",
3240 .test
= alg_test_skcipher
,
3244 .enc
= __VECS(aes_ctr_rfc3686_enc_tv_template
),
3245 .dec
= __VECS(aes_ctr_rfc3686_dec_tv_template
)
3249 .alg
= "rfc4106(gcm(aes))",
3250 .test
= alg_test_aead
,
3254 .enc
= __VECS(aes_gcm_rfc4106_enc_tv_template
),
3255 .dec
= __VECS(aes_gcm_rfc4106_dec_tv_template
)
3259 .alg
= "rfc4309(ccm(aes))",
3260 .test
= alg_test_aead
,
3264 .enc
= __VECS(aes_ccm_rfc4309_enc_tv_template
),
3265 .dec
= __VECS(aes_ccm_rfc4309_dec_tv_template
)
3269 .alg
= "rfc4543(gcm(aes))",
3270 .test
= alg_test_aead
,
3273 .enc
= __VECS(aes_gcm_rfc4543_enc_tv_template
),
3274 .dec
= __VECS(aes_gcm_rfc4543_dec_tv_template
),
3278 .alg
= "rfc7539(chacha20,poly1305)",
3279 .test
= alg_test_aead
,
3282 .enc
= __VECS(rfc7539_enc_tv_template
),
3283 .dec
= __VECS(rfc7539_dec_tv_template
),
3287 .alg
= "rfc7539esp(chacha20,poly1305)",
3288 .test
= alg_test_aead
,
3291 .enc
= __VECS(rfc7539esp_enc_tv_template
),
3292 .dec
= __VECS(rfc7539esp_dec_tv_template
),
3297 .test
= alg_test_hash
,
3299 .hash
= __VECS(rmd128_tv_template
)
3303 .test
= alg_test_hash
,
3305 .hash
= __VECS(rmd160_tv_template
)
3309 .test
= alg_test_hash
,
3311 .hash
= __VECS(rmd256_tv_template
)
3315 .test
= alg_test_hash
,
3317 .hash
= __VECS(rmd320_tv_template
)
3321 .test
= alg_test_akcipher
,
3324 .akcipher
= __VECS(rsa_tv_template
)
3328 .test
= alg_test_skcipher
,
3331 .enc
= __VECS(salsa20_stream_enc_tv_template
)
3336 .test
= alg_test_hash
,
3339 .hash
= __VECS(sha1_tv_template
)
3343 .test
= alg_test_hash
,
3346 .hash
= __VECS(sha224_tv_template
)
3350 .test
= alg_test_hash
,
3353 .hash
= __VECS(sha256_tv_template
)
3357 .test
= alg_test_hash
,
3360 .hash
= __VECS(sha3_224_tv_template
)
3364 .test
= alg_test_hash
,
3367 .hash
= __VECS(sha3_256_tv_template
)
3371 .test
= alg_test_hash
,
3374 .hash
= __VECS(sha3_384_tv_template
)
3378 .test
= alg_test_hash
,
3381 .hash
= __VECS(sha3_512_tv_template
)
3385 .test
= alg_test_hash
,
3388 .hash
= __VECS(sha384_tv_template
)
3392 .test
= alg_test_hash
,
3395 .hash
= __VECS(sha512_tv_template
)
3399 .test
= alg_test_hash
,
3401 .hash
= __VECS(tgr128_tv_template
)
3405 .test
= alg_test_hash
,
3407 .hash
= __VECS(tgr160_tv_template
)
3411 .test
= alg_test_hash
,
3413 .hash
= __VECS(tgr192_tv_template
)
3417 .test
= alg_test_hash
,
3419 .hash
= __VECS(aes_vmac128_tv_template
)
3423 .test
= alg_test_hash
,
3425 .hash
= __VECS(wp256_tv_template
)
3429 .test
= alg_test_hash
,
3431 .hash
= __VECS(wp384_tv_template
)
3435 .test
= alg_test_hash
,
3437 .hash
= __VECS(wp512_tv_template
)
3441 .test
= alg_test_hash
,
3443 .hash
= __VECS(aes_xcbc128_tv_template
)
3447 .test
= alg_test_skcipher
,
3451 .enc
= __VECS(aes_xts_enc_tv_template
),
3452 .dec
= __VECS(aes_xts_dec_tv_template
)
3456 .alg
= "xts(camellia)",
3457 .test
= alg_test_skcipher
,
3460 .enc
= __VECS(camellia_xts_enc_tv_template
),
3461 .dec
= __VECS(camellia_xts_dec_tv_template
)
3465 .alg
= "xts(cast6)",
3466 .test
= alg_test_skcipher
,
3469 .enc
= __VECS(cast6_xts_enc_tv_template
),
3470 .dec
= __VECS(cast6_xts_dec_tv_template
)
3474 .alg
= "xts(serpent)",
3475 .test
= alg_test_skcipher
,
3478 .enc
= __VECS(serpent_xts_enc_tv_template
),
3479 .dec
= __VECS(serpent_xts_dec_tv_template
)
3483 .alg
= "xts(twofish)",
3484 .test
= alg_test_skcipher
,
3487 .enc
= __VECS(tf_xts_enc_tv_template
),
3488 .dec
= __VECS(tf_xts_dec_tv_template
)
3494 static bool alg_test_descs_checked
;
3496 static void alg_test_descs_check_order(void)
3500 /* only check once */
3501 if (alg_test_descs_checked
)
3504 alg_test_descs_checked
= true;
3506 for (i
= 1; i
< ARRAY_SIZE(alg_test_descs
); i
++) {
3507 int diff
= strcmp(alg_test_descs
[i
- 1].alg
,
3508 alg_test_descs
[i
].alg
);
3510 if (WARN_ON(diff
> 0)) {
3511 pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
3512 alg_test_descs
[i
- 1].alg
,
3513 alg_test_descs
[i
].alg
);
3516 if (WARN_ON(diff
== 0)) {
3517 pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
3518 alg_test_descs
[i
].alg
);
3523 static int alg_find_test(const char *alg
)
3526 int end
= ARRAY_SIZE(alg_test_descs
);
3528 while (start
< end
) {
3529 int i
= (start
+ end
) / 2;
3530 int diff
= strcmp(alg_test_descs
[i
].alg
, alg
);
3548 int alg_test(const char *driver
, const char *alg
, u32 type
, u32 mask
)
3554 if (!fips_enabled
&& notests
) {
3555 printk_once(KERN_INFO
"alg: self-tests disabled\n");
3559 alg_test_descs_check_order();
3561 if ((type
& CRYPTO_ALG_TYPE_MASK
) == CRYPTO_ALG_TYPE_CIPHER
) {
3562 char nalg
[CRYPTO_MAX_ALG_NAME
];
3564 if (snprintf(nalg
, sizeof(nalg
), "ecb(%s)", alg
) >=
3566 return -ENAMETOOLONG
;
3568 i
= alg_find_test(nalg
);
3572 if (fips_enabled
&& !alg_test_descs
[i
].fips_allowed
)
3575 rc
= alg_test_cipher(alg_test_descs
+ i
, driver
, type
, mask
);
3579 i
= alg_find_test(alg
);
3580 j
= alg_find_test(driver
);
3584 if (fips_enabled
&& ((i
>= 0 && !alg_test_descs
[i
].fips_allowed
) ||
3585 (j
>= 0 && !alg_test_descs
[j
].fips_allowed
)))
3590 rc
|= alg_test_descs
[i
].test(alg_test_descs
+ i
, driver
,
3592 if (j
>= 0 && j
!= i
)
3593 rc
|= alg_test_descs
[j
].test(alg_test_descs
+ j
, driver
,
3597 if (fips_enabled
&& rc
)
3598 panic("%s: %s alg self test failed in fips mode!\n", driver
, alg
);
3600 if (fips_enabled
&& !rc
)
3601 pr_info("alg: self-tests for %s (%s) passed\n", driver
, alg
);
3606 printk(KERN_INFO
"alg: No test for %s (%s)\n", alg
, driver
);
3612 #endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
3614 EXPORT_SYMBOL_GPL(alg_test
);