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>
8 * Copyright (c) 2019 Google LLC
10 * Updated RFC4106 AES-GCM testing.
11 * Authors: Aidan O'Mahony (aidan.o.mahony@intel.com)
12 * Adrian Hoban <adrian.hoban@intel.com>
13 * Gabriele Paoloni <gabriele.paoloni@intel.com>
14 * Tadeusz Struk (tadeusz.struk@intel.com)
15 * Copyright (c) 2010, Intel Corporation.
17 * This program is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the Free
19 * Software Foundation; either version 2 of the License, or (at your option)
24 #include <crypto/aead.h>
25 #include <crypto/hash.h>
26 #include <crypto/skcipher.h>
27 #include <linux/err.h>
28 #include <linux/fips.h>
29 #include <linux/module.h>
30 #include <linux/once.h>
31 #include <linux/random.h>
32 #include <linux/scatterlist.h>
33 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <crypto/rng.h>
36 #include <crypto/drbg.h>
37 #include <crypto/akcipher.h>
38 #include <crypto/kpp.h>
39 #include <crypto/acompress.h>
44 module_param(notests, bool, 0644);
45 MODULE_PARM_DESC(notests, "disable crypto self-tests");
47 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
48 static bool noextratests;
49 module_param(noextratests, bool, 0644);
50 MODULE_PARM_DESC(noextratests, "disable expensive crypto self-tests");
52 static unsigned int fuzz_iterations = 100;
53 module_param(fuzz_iterations, uint, 0644);
54 MODULE_PARM_DESC(fuzz_iterations, "number of fuzz test iterations");
57 #ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
60 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
70 * Need slab memory for testing (size in number of pages).
75 * Indexes into the xbuf to simulate cross-page access.
87 * Used by test_cipher()
92 struct aead_test_suite {
93 const struct aead_testvec *vecs;
97 struct cipher_test_suite {
98 const struct cipher_testvec *vecs;
102 struct comp_test_suite {
104 const struct comp_testvec *vecs;
109 struct hash_test_suite {
110 const struct hash_testvec *vecs;
114 struct cprng_test_suite {
115 const struct cprng_testvec *vecs;
119 struct drbg_test_suite {
120 const struct drbg_testvec *vecs;
124 struct akcipher_test_suite {
125 const struct akcipher_testvec *vecs;
129 struct kpp_test_suite {
130 const struct kpp_testvec *vecs;
134 struct alg_test_desc {
136 int (*test)(const struct alg_test_desc *desc, const char *driver,
138 int fips_allowed; /* set if alg is allowed in fips mode */
141 struct aead_test_suite aead;
142 struct cipher_test_suite cipher;
143 struct comp_test_suite comp;
144 struct hash_test_suite hash;
145 struct cprng_test_suite cprng;
146 struct drbg_test_suite drbg;
147 struct akcipher_test_suite akcipher;
148 struct kpp_test_suite kpp;
152 static const unsigned int IDX[8] = {
153 IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
155 static void hexdump(unsigned char *buf, unsigned int len)
157 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
162 static int __testmgr_alloc_buf(char *buf[XBUFSIZE], int order)
166 for (i = 0; i < XBUFSIZE; i++) {
167 buf[i] = (char *)__get_free_pages(GFP_KERNEL, order);
176 free_pages((unsigned long)buf[i], order);
181 static int testmgr_alloc_buf(char *buf[XBUFSIZE])
183 return __testmgr_alloc_buf(buf, 0);
186 static void __testmgr_free_buf(char *buf[XBUFSIZE], int order)
190 for (i = 0; i < XBUFSIZE; i++)
191 free_pages((unsigned long)buf[i], order);
194 static void testmgr_free_buf(char *buf[XBUFSIZE])
196 __testmgr_free_buf(buf, 0);
199 #define TESTMGR_POISON_BYTE 0xfe
200 #define TESTMGR_POISON_LEN 16
202 static inline void testmgr_poison(void *addr, size_t len)
204 memset(addr, TESTMGR_POISON_BYTE, len);
207 /* Is the memory region still fully poisoned? */
208 static inline bool testmgr_is_poison(const void *addr, size_t len)
210 return memchr_inv(addr, TESTMGR_POISON_BYTE, len) == NULL;
213 /* flush type for hash algorithms */
215 /* merge with update of previous buffer(s) */
218 /* update with previous buffer(s) before doing this one */
221 /* likewise, but also export and re-import the intermediate state */
225 /* finalization function for hash algorithms */
226 enum finalization_type {
227 FINALIZATION_TYPE_FINAL, /* use final() */
228 FINALIZATION_TYPE_FINUP, /* use finup() */
229 FINALIZATION_TYPE_DIGEST, /* use digest() */
232 #define TEST_SG_TOTAL 10000
235 * struct test_sg_division - description of a scatterlist entry
237 * This struct describes one entry of a scatterlist being constructed to check a
238 * crypto test vector.
240 * @proportion_of_total: length of this chunk relative to the total length,
241 * given as a proportion out of TEST_SG_TOTAL so that it
242 * scales to fit any test vector
243 * @offset: byte offset into a 2-page buffer at which this chunk will start
244 * @offset_relative_to_alignmask: if true, add the algorithm's alignmask to the
246 * @flush_type: for hashes, whether an update() should be done now vs.
247 * continuing to accumulate data
249 struct test_sg_division {
250 unsigned int proportion_of_total;
252 bool offset_relative_to_alignmask;
253 enum flush_type flush_type;
257 * struct testvec_config - configuration for testing a crypto test vector
259 * This struct describes the data layout and other parameters with which each
260 * crypto test vector can be tested.
262 * @name: name of this config, logged for debugging purposes if a test fails
263 * @inplace: operate on the data in-place, if applicable for the algorithm type?
264 * @req_flags: extra request_flags, e.g. CRYPTO_TFM_REQ_MAY_SLEEP
265 * @src_divs: description of how to arrange the source scatterlist
266 * @dst_divs: description of how to arrange the dst scatterlist, if applicable
267 * for the algorithm type. Defaults to @src_divs if unset.
268 * @iv_offset: misalignment of the IV in the range [0..MAX_ALGAPI_ALIGNMASK+1],
269 * where 0 is aligned to a 2*(MAX_ALGAPI_ALIGNMASK+1) byte boundary
270 * @iv_offset_relative_to_alignmask: if true, add the algorithm's alignmask to
272 * @finalization_type: what finalization function to use for hashes
274 struct testvec_config {
278 struct test_sg_division src_divs[XBUFSIZE];
279 struct test_sg_division dst_divs[XBUFSIZE];
280 unsigned int iv_offset;
281 bool iv_offset_relative_to_alignmask;
282 enum finalization_type finalization_type;
285 #define TESTVEC_CONFIG_NAMELEN 192
288 * The following are the lists of testvec_configs to test for each algorithm
289 * type when the basic crypto self-tests are enabled, i.e. when
290 * CONFIG_CRYPTO_MANAGER_DISABLE_TESTS is unset. They aim to provide good test
291 * coverage, while keeping the test time much shorter than the full fuzz tests
292 * so that the basic tests can be enabled in a wider range of circumstances.
295 /* Configs for skciphers and aeads */
296 static const struct testvec_config default_cipher_testvec_configs[] = {
300 .src_divs = { { .proportion_of_total = 10000 } },
302 .name = "out-of-place",
303 .src_divs = { { .proportion_of_total = 10000 } },
305 .name = "unaligned buffer, offset=1",
306 .src_divs = { { .proportion_of_total = 10000, .offset = 1 } },
309 .name = "buffer aligned only to alignmask",
312 .proportion_of_total = 10000,
314 .offset_relative_to_alignmask = true,
318 .iv_offset_relative_to_alignmask = true,
320 .name = "two even aligned splits",
322 { .proportion_of_total = 5000 },
323 { .proportion_of_total = 5000 },
326 .name = "uneven misaligned splits, may sleep",
327 .req_flags = CRYPTO_TFM_REQ_MAY_SLEEP,
329 { .proportion_of_total = 1900, .offset = 33 },
330 { .proportion_of_total = 3300, .offset = 7 },
331 { .proportion_of_total = 4800, .offset = 18 },
335 .name = "misaligned splits crossing pages, inplace",
339 .proportion_of_total = 7500,
340 .offset = PAGE_SIZE - 32
342 .proportion_of_total = 2500,
343 .offset = PAGE_SIZE - 7
349 static unsigned int count_test_sg_divisions(const struct test_sg_division *divs)
351 unsigned int remaining = TEST_SG_TOTAL;
352 unsigned int ndivs = 0;
355 remaining -= divs[ndivs++].proportion_of_total;
361 static bool valid_sg_divisions(const struct test_sg_division *divs,
362 unsigned int count, bool *any_flushes_ret)
364 unsigned int total = 0;
367 for (i = 0; i < count && total != TEST_SG_TOTAL; i++) {
368 if (divs[i].proportion_of_total <= 0 ||
369 divs[i].proportion_of_total > TEST_SG_TOTAL - total)
371 total += divs[i].proportion_of_total;
372 if (divs[i].flush_type != FLUSH_TYPE_NONE)
373 *any_flushes_ret = true;
375 return total == TEST_SG_TOTAL &&
376 memchr_inv(&divs[i], 0, (count - i) * sizeof(divs[0])) == NULL;
380 * Check whether the given testvec_config is valid. This isn't strictly needed
381 * since every testvec_config should be valid, but check anyway so that people
382 * don't unknowingly add broken configs that don't do what they wanted.
384 static bool valid_testvec_config(const struct testvec_config *cfg)
386 bool any_flushes = false;
388 if (cfg->name == NULL)
391 if (!valid_sg_divisions(cfg->src_divs, ARRAY_SIZE(cfg->src_divs),
395 if (cfg->dst_divs[0].proportion_of_total) {
396 if (!valid_sg_divisions(cfg->dst_divs,
397 ARRAY_SIZE(cfg->dst_divs),
401 if (memchr_inv(cfg->dst_divs, 0, sizeof(cfg->dst_divs)))
403 /* defaults to dst_divs=src_divs */
407 (cfg->iv_offset_relative_to_alignmask ? MAX_ALGAPI_ALIGNMASK : 0) >
408 MAX_ALGAPI_ALIGNMASK + 1)
411 if (any_flushes && cfg->finalization_type == FINALIZATION_TYPE_DIGEST)
418 char *bufs[XBUFSIZE];
419 struct scatterlist sgl[XBUFSIZE];
420 struct scatterlist sgl_saved[XBUFSIZE];
421 struct scatterlist *sgl_ptr;
425 static int init_test_sglist(struct test_sglist *tsgl)
427 return __testmgr_alloc_buf(tsgl->bufs, 1 /* two pages per buffer */);
430 static void destroy_test_sglist(struct test_sglist *tsgl)
432 return __testmgr_free_buf(tsgl->bufs, 1 /* two pages per buffer */);
436 * build_test_sglist() - build a scatterlist for a crypto test
438 * @tsgl: the scatterlist to build. @tsgl->bufs[] contains an array of 2-page
439 * buffers which the scatterlist @tsgl->sgl[] will be made to point into.
440 * @divs: the layout specification on which the scatterlist will be based
441 * @alignmask: the algorithm's alignmask
442 * @total_len: the total length of the scatterlist to build in bytes
443 * @data: if non-NULL, the buffers will be filled with this data until it ends.
444 * Otherwise the buffers will be poisoned. In both cases, some bytes
445 * past the end of each buffer will be poisoned to help detect overruns.
446 * @out_divs: if non-NULL, the test_sg_division to which each scatterlist entry
447 * corresponds will be returned here. This will match @divs except
448 * that divisions resolving to a length of 0 are omitted as they are
449 * not included in the scatterlist.
451 * Return: 0 or a -errno value
453 static int build_test_sglist(struct test_sglist *tsgl,
454 const struct test_sg_division *divs,
455 const unsigned int alignmask,
456 const unsigned int total_len,
457 struct iov_iter *data,
458 const struct test_sg_division *out_divs[XBUFSIZE])
461 const struct test_sg_division *div;
463 } partitions[XBUFSIZE];
464 const unsigned int ndivs = count_test_sg_divisions(divs);
465 unsigned int len_remaining = total_len;
468 BUILD_BUG_ON(ARRAY_SIZE(partitions) != ARRAY_SIZE(tsgl->sgl));
469 if (WARN_ON(ndivs > ARRAY_SIZE(partitions)))
472 /* Calculate the (div, length) pairs */
474 for (i = 0; i < ndivs; i++) {
475 unsigned int len_this_sg =
477 (total_len * divs[i].proportion_of_total +
478 TEST_SG_TOTAL / 2) / TEST_SG_TOTAL);
480 if (len_this_sg != 0) {
481 partitions[tsgl->nents].div = &divs[i];
482 partitions[tsgl->nents].length = len_this_sg;
484 len_remaining -= len_this_sg;
487 if (tsgl->nents == 0) {
488 partitions[tsgl->nents].div = &divs[0];
489 partitions[tsgl->nents].length = 0;
492 partitions[tsgl->nents - 1].length += len_remaining;
494 /* Set up the sgl entries and fill the data or poison */
495 sg_init_table(tsgl->sgl, tsgl->nents);
496 for (i = 0; i < tsgl->nents; i++) {
497 unsigned int offset = partitions[i].div->offset;
500 if (partitions[i].div->offset_relative_to_alignmask)
503 while (offset + partitions[i].length + TESTMGR_POISON_LEN >
505 if (WARN_ON(offset <= 0))
510 addr = &tsgl->bufs[i][offset];
511 sg_set_buf(&tsgl->sgl[i], addr, partitions[i].length);
514 out_divs[i] = partitions[i].div;
517 size_t copy_len, copied;
519 copy_len = min(partitions[i].length, data->count);
520 copied = copy_from_iter(addr, copy_len, data);
521 if (WARN_ON(copied != copy_len))
523 testmgr_poison(addr + copy_len, partitions[i].length +
524 TESTMGR_POISON_LEN - copy_len);
526 testmgr_poison(addr, partitions[i].length +
531 sg_mark_end(&tsgl->sgl[tsgl->nents - 1]);
532 tsgl->sgl_ptr = tsgl->sgl;
533 memcpy(tsgl->sgl_saved, tsgl->sgl, tsgl->nents * sizeof(tsgl->sgl[0]));
538 * Verify that a scatterlist crypto operation produced the correct output.
540 * @tsgl: scatterlist containing the actual output
541 * @expected_output: buffer containing the expected output
542 * @len_to_check: length of @expected_output in bytes
543 * @unchecked_prefix_len: number of ignored bytes in @tsgl prior to real result
544 * @check_poison: verify that the poison bytes after each chunk are intact?
546 * Return: 0 if correct, -EINVAL if incorrect, -EOVERFLOW if buffer overrun.
548 static int verify_correct_output(const struct test_sglist *tsgl,
549 const char *expected_output,
550 unsigned int len_to_check,
551 unsigned int unchecked_prefix_len,
556 for (i = 0; i < tsgl->nents; i++) {
557 struct scatterlist *sg = &tsgl->sgl_ptr[i];
558 unsigned int len = sg->length;
559 unsigned int offset = sg->offset;
560 const char *actual_output;
562 if (unchecked_prefix_len) {
563 if (unchecked_prefix_len >= len) {
564 unchecked_prefix_len -= len;
567 offset += unchecked_prefix_len;
568 len -= unchecked_prefix_len;
569 unchecked_prefix_len = 0;
571 len = min(len, len_to_check);
572 actual_output = page_address(sg_page(sg)) + offset;
573 if (memcmp(expected_output, actual_output, len) != 0)
576 !testmgr_is_poison(actual_output + len, TESTMGR_POISON_LEN))
579 expected_output += len;
581 if (WARN_ON(len_to_check != 0))
586 static bool is_test_sglist_corrupted(const struct test_sglist *tsgl)
590 for (i = 0; i < tsgl->nents; i++) {
591 if (tsgl->sgl[i].page_link != tsgl->sgl_saved[i].page_link)
593 if (tsgl->sgl[i].offset != tsgl->sgl_saved[i].offset)
595 if (tsgl->sgl[i].length != tsgl->sgl_saved[i].length)
601 struct cipher_test_sglists {
602 struct test_sglist src;
603 struct test_sglist dst;
606 static struct cipher_test_sglists *alloc_cipher_test_sglists(void)
608 struct cipher_test_sglists *tsgls;
610 tsgls = kmalloc(sizeof(*tsgls), GFP_KERNEL);
614 if (init_test_sglist(&tsgls->src) != 0)
616 if (init_test_sglist(&tsgls->dst) != 0)
617 goto fail_destroy_src;
622 destroy_test_sglist(&tsgls->src);
628 static void free_cipher_test_sglists(struct cipher_test_sglists *tsgls)
631 destroy_test_sglist(&tsgls->src);
632 destroy_test_sglist(&tsgls->dst);
637 /* Build the src and dst scatterlists for an skcipher or AEAD test */
638 static int build_cipher_test_sglists(struct cipher_test_sglists *tsgls,
639 const struct testvec_config *cfg,
640 unsigned int alignmask,
641 unsigned int src_total_len,
642 unsigned int dst_total_len,
643 const struct kvec *inputs,
644 unsigned int nr_inputs)
646 struct iov_iter input;
649 iov_iter_kvec(&input, WRITE, inputs, nr_inputs, src_total_len);
650 err = build_test_sglist(&tsgls->src, cfg->src_divs, alignmask,
652 max(dst_total_len, src_total_len) :
659 tsgls->dst.sgl_ptr = tsgls->src.sgl;
660 tsgls->dst.nents = tsgls->src.nents;
663 return build_test_sglist(&tsgls->dst,
664 cfg->dst_divs[0].proportion_of_total ?
665 cfg->dst_divs : cfg->src_divs,
666 alignmask, dst_total_len, NULL, NULL);
669 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
670 static char *generate_random_sgl_divisions(struct test_sg_division *divs,
671 size_t max_divs, char *p, char *end,
674 struct test_sg_division *div = divs;
675 unsigned int remaining = TEST_SG_TOTAL;
678 unsigned int this_len;
680 if (div == &divs[max_divs - 1] || prandom_u32() % 2 == 0)
681 this_len = remaining;
683 this_len = 1 + (prandom_u32() % remaining);
684 div->proportion_of_total = this_len;
686 if (prandom_u32() % 4 == 0)
687 div->offset = (PAGE_SIZE - 128) + (prandom_u32() % 128);
688 else if (prandom_u32() % 2 == 0)
689 div->offset = prandom_u32() % 32;
691 div->offset = prandom_u32() % PAGE_SIZE;
692 if (prandom_u32() % 8 == 0)
693 div->offset_relative_to_alignmask = true;
695 div->flush_type = FLUSH_TYPE_NONE;
697 switch (prandom_u32() % 4) {
699 div->flush_type = FLUSH_TYPE_REIMPORT;
702 div->flush_type = FLUSH_TYPE_FLUSH;
707 BUILD_BUG_ON(TEST_SG_TOTAL != 10000); /* for "%u.%u%%" */
708 p += scnprintf(p, end - p, "%s%u.%u%%@%s+%u%s",
709 div->flush_type == FLUSH_TYPE_NONE ? "" :
710 div->flush_type == FLUSH_TYPE_FLUSH ?
711 "<flush> " : "<reimport> ",
712 this_len / 100, this_len % 100,
713 div->offset_relative_to_alignmask ?
715 div->offset, this_len == remaining ? "" : ", ");
716 remaining -= this_len;
723 /* Generate a random testvec_config for fuzz testing */
724 static void generate_random_testvec_config(struct testvec_config *cfg,
725 char *name, size_t max_namelen)
728 char * const end = name + max_namelen;
730 memset(cfg, 0, sizeof(*cfg));
734 p += scnprintf(p, end - p, "random:");
736 if (prandom_u32() % 2 == 0) {
738 p += scnprintf(p, end - p, " inplace");
741 if (prandom_u32() % 2 == 0) {
742 cfg->req_flags |= CRYPTO_TFM_REQ_MAY_SLEEP;
743 p += scnprintf(p, end - p, " may_sleep");
746 switch (prandom_u32() % 4) {
748 cfg->finalization_type = FINALIZATION_TYPE_FINAL;
749 p += scnprintf(p, end - p, " use_final");
752 cfg->finalization_type = FINALIZATION_TYPE_FINUP;
753 p += scnprintf(p, end - p, " use_finup");
756 cfg->finalization_type = FINALIZATION_TYPE_DIGEST;
757 p += scnprintf(p, end - p, " use_digest");
761 p += scnprintf(p, end - p, " src_divs=[");
762 p = generate_random_sgl_divisions(cfg->src_divs,
763 ARRAY_SIZE(cfg->src_divs), p, end,
764 (cfg->finalization_type !=
765 FINALIZATION_TYPE_DIGEST));
766 p += scnprintf(p, end - p, "]");
768 if (!cfg->inplace && prandom_u32() % 2 == 0) {
769 p += scnprintf(p, end - p, " dst_divs=[");
770 p = generate_random_sgl_divisions(cfg->dst_divs,
771 ARRAY_SIZE(cfg->dst_divs),
773 p += scnprintf(p, end - p, "]");
776 if (prandom_u32() % 2 == 0) {
777 cfg->iv_offset = 1 + (prandom_u32() % MAX_ALGAPI_ALIGNMASK);
778 p += scnprintf(p, end - p, " iv_offset=%u", cfg->iv_offset);
781 WARN_ON_ONCE(!valid_testvec_config(cfg));
783 #endif /* CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
785 static int ahash_guard_result(char *result, char c, int size)
789 for (i = 0; i < size; i++) {
797 static int ahash_partial_update(struct ahash_request **preq,
798 struct crypto_ahash *tfm, const struct hash_testvec *template,
799 void *hash_buff, int k, int temp, struct scatterlist *sg,
800 const char *algo, char *result, struct crypto_wait *wait)
803 struct ahash_request *req;
804 int statesize, ret = -EINVAL;
805 static const unsigned char guard[] = { 0x00, 0xba, 0xad, 0x00 };
806 int digestsize = crypto_ahash_digestsize(tfm);
809 statesize = crypto_ahash_statesize(
810 crypto_ahash_reqtfm(req));
811 state = kmalloc(statesize + sizeof(guard), GFP_KERNEL);
813 pr_err("alg: hash: Failed to alloc state for %s\n", algo);
816 memcpy(state + statesize, guard, sizeof(guard));
817 memset(result, 1, digestsize);
818 ret = crypto_ahash_export(req, state);
819 WARN_ON(memcmp(state + statesize, guard, sizeof(guard)));
821 pr_err("alg: hash: Failed to export() for %s\n", algo);
824 ret = ahash_guard_result(result, 1, digestsize);
826 pr_err("alg: hash: Failed, export used req->result for %s\n",
830 ahash_request_free(req);
831 req = ahash_request_alloc(tfm, GFP_KERNEL);
833 pr_err("alg: hash: Failed to alloc request for %s\n", algo);
836 ahash_request_set_callback(req,
837 CRYPTO_TFM_REQ_MAY_BACKLOG,
838 crypto_req_done, wait);
840 memcpy(hash_buff, template->plaintext + temp,
842 sg_init_one(&sg[0], hash_buff, template->tap[k]);
843 ahash_request_set_crypt(req, sg, result, template->tap[k]);
844 ret = crypto_ahash_import(req, state);
846 pr_err("alg: hash: Failed to import() for %s\n", algo);
849 ret = ahash_guard_result(result, 1, digestsize);
851 pr_err("alg: hash: Failed, import used req->result for %s\n",
855 ret = crypto_wait_req(crypto_ahash_update(req), wait);
862 ahash_request_free(req);
875 static int __test_hash(struct crypto_ahash *tfm,
876 const struct hash_testvec *template, unsigned int tcount,
877 enum hash_test test_type, const int align_offset)
879 const char *algo = crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
880 size_t digest_size = crypto_ahash_digestsize(tfm);
881 unsigned int i, j, k, temp;
882 struct scatterlist sg[8];
885 struct ahash_request *req;
886 struct crypto_wait wait;
888 char *xbuf[XBUFSIZE];
891 result = kmalloc(digest_size, GFP_KERNEL);
894 key = kmalloc(MAX_KEYLEN, GFP_KERNEL);
897 if (testmgr_alloc_buf(xbuf))
900 crypto_init_wait(&wait);
902 req = ahash_request_alloc(tfm, GFP_KERNEL);
904 printk(KERN_ERR "alg: hash: Failed to allocate request for "
908 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
909 crypto_req_done, &wait);
912 for (i = 0; i < tcount; i++) {
917 if (WARN_ON(align_offset + template[i].psize > PAGE_SIZE))
921 memset(result, 0, digest_size);
924 hash_buff += align_offset;
926 memcpy(hash_buff, template[i].plaintext, template[i].psize);
927 sg_init_one(&sg[0], hash_buff, template[i].psize);
929 if (template[i].ksize) {
930 crypto_ahash_clear_flags(tfm, ~0);
931 if (template[i].ksize > MAX_KEYLEN) {
932 pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
933 j, algo, template[i].ksize, MAX_KEYLEN);
937 memcpy(key, template[i].key, template[i].ksize);
938 ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
940 printk(KERN_ERR "alg: hash: setkey failed on "
941 "test %d for %s: ret=%d\n", j, algo,
947 ahash_request_set_crypt(req, sg, result, template[i].psize);
949 case HASH_TEST_DIGEST:
950 ret = crypto_wait_req(crypto_ahash_digest(req), &wait);
952 pr_err("alg: hash: digest failed on test %d "
953 "for %s: ret=%d\n", j, algo, -ret);
958 case HASH_TEST_FINAL:
959 memset(result, 1, digest_size);
960 ret = crypto_wait_req(crypto_ahash_init(req), &wait);
962 pr_err("alg: hash: init failed on test %d "
963 "for %s: ret=%d\n", j, algo, -ret);
966 ret = ahash_guard_result(result, 1, digest_size);
968 pr_err("alg: hash: init failed on test %d "
969 "for %s: used req->result\n", j, algo);
972 ret = crypto_wait_req(crypto_ahash_update(req), &wait);
974 pr_err("alg: hash: update failed on test %d "
975 "for %s: ret=%d\n", j, algo, -ret);
978 ret = ahash_guard_result(result, 1, digest_size);
980 pr_err("alg: hash: update failed on test %d "
981 "for %s: used req->result\n", j, algo);
984 ret = crypto_wait_req(crypto_ahash_final(req), &wait);
986 pr_err("alg: hash: final failed on test %d "
987 "for %s: ret=%d\n", j, algo, -ret);
992 case HASH_TEST_FINUP:
993 memset(result, 1, digest_size);
994 ret = crypto_wait_req(crypto_ahash_init(req), &wait);
996 pr_err("alg: hash: init failed on test %d "
997 "for %s: ret=%d\n", j, algo, -ret);
1000 ret = ahash_guard_result(result, 1, digest_size);
1002 pr_err("alg: hash: init failed on test %d "
1003 "for %s: used req->result\n", j, algo);
1006 ret = crypto_wait_req(crypto_ahash_finup(req), &wait);
1008 pr_err("alg: hash: final failed on test %d "
1009 "for %s: ret=%d\n", j, algo, -ret);
1015 if (memcmp(result, template[i].digest,
1016 crypto_ahash_digestsize(tfm))) {
1017 printk(KERN_ERR "alg: hash: Test %d failed for %s\n",
1019 hexdump(result, crypto_ahash_digestsize(tfm));
1029 for (i = 0; i < tcount; i++) {
1030 /* alignment tests are only done with continuous buffers */
1031 if (align_offset != 0)
1034 if (!template[i].np)
1038 memset(result, 0, digest_size);
1041 sg_init_table(sg, template[i].np);
1043 for (k = 0; k < template[i].np; k++) {
1044 if (WARN_ON(offset_in_page(IDX[k]) +
1045 template[i].tap[k] > PAGE_SIZE))
1048 memcpy(xbuf[IDX[k] >> PAGE_SHIFT] +
1049 offset_in_page(IDX[k]),
1050 template[i].plaintext + temp,
1051 template[i].tap[k]),
1052 template[i].tap[k]);
1053 temp += template[i].tap[k];
1056 if (template[i].ksize) {
1057 if (template[i].ksize > MAX_KEYLEN) {
1058 pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
1059 j, algo, template[i].ksize, MAX_KEYLEN);
1063 crypto_ahash_clear_flags(tfm, ~0);
1064 memcpy(key, template[i].key, template[i].ksize);
1065 ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
1068 printk(KERN_ERR "alg: hash: setkey "
1069 "failed on chunking test %d "
1070 "for %s: ret=%d\n", j, algo, -ret);
1075 ahash_request_set_crypt(req, sg, result, template[i].psize);
1076 ret = crypto_wait_req(crypto_ahash_digest(req), &wait);
1078 pr_err("alg: hash: digest failed on chunking test %d for %s: ret=%d\n",
1083 if (memcmp(result, template[i].digest,
1084 crypto_ahash_digestsize(tfm))) {
1085 printk(KERN_ERR "alg: hash: Chunking test %d "
1086 "failed for %s\n", j, algo);
1087 hexdump(result, crypto_ahash_digestsize(tfm));
1093 /* partial update exercise */
1095 for (i = 0; i < tcount; i++) {
1096 /* alignment tests are only done with continuous buffers */
1097 if (align_offset != 0)
1100 if (template[i].np < 2)
1104 memset(result, 0, digest_size);
1107 hash_buff = xbuf[0];
1108 memcpy(hash_buff, template[i].plaintext,
1109 template[i].tap[0]);
1110 sg_init_one(&sg[0], hash_buff, template[i].tap[0]);
1112 if (template[i].ksize) {
1113 crypto_ahash_clear_flags(tfm, ~0);
1114 if (template[i].ksize > MAX_KEYLEN) {
1115 pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
1116 j, algo, template[i].ksize, MAX_KEYLEN);
1120 memcpy(key, template[i].key, template[i].ksize);
1121 ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
1123 pr_err("alg: hash: setkey failed on test %d for %s: ret=%d\n",
1129 ahash_request_set_crypt(req, sg, result, template[i].tap[0]);
1130 ret = crypto_wait_req(crypto_ahash_init(req), &wait);
1132 pr_err("alg: hash: init failed on test %d for %s: ret=%d\n",
1136 ret = crypto_wait_req(crypto_ahash_update(req), &wait);
1138 pr_err("alg: hash: update failed on test %d for %s: ret=%d\n",
1143 temp = template[i].tap[0];
1144 for (k = 1; k < template[i].np; k++) {
1145 ret = ahash_partial_update(&req, tfm, &template[i],
1146 hash_buff, k, temp, &sg[0], algo, result,
1149 pr_err("alg: hash: partial update failed on test %d for %s: ret=%d\n",
1153 temp += template[i].tap[k];
1155 ret = crypto_wait_req(crypto_ahash_final(req), &wait);
1157 pr_err("alg: hash: final failed on test %d for %s: ret=%d\n",
1161 if (memcmp(result, template[i].digest,
1162 crypto_ahash_digestsize(tfm))) {
1163 pr_err("alg: hash: Partial Test %d failed for %s\n",
1165 hexdump(result, crypto_ahash_digestsize(tfm));
1174 ahash_request_free(req);
1176 testmgr_free_buf(xbuf);
1183 static int test_hash(struct crypto_ahash *tfm,
1184 const struct hash_testvec *template,
1185 unsigned int tcount, enum hash_test test_type)
1187 unsigned int alignmask;
1190 ret = __test_hash(tfm, template, tcount, test_type, 0);
1194 /* test unaligned buffers, check with one byte offset */
1195 ret = __test_hash(tfm, template, tcount, test_type, 1);
1199 alignmask = crypto_tfm_alg_alignmask(&tfm->base);
1201 /* Check if alignment mask for tfm is correctly set. */
1202 ret = __test_hash(tfm, template, tcount, test_type,
1211 static int test_aead_vec_cfg(const char *driver, int enc,
1212 const struct aead_testvec *vec,
1213 unsigned int vec_num,
1214 const struct testvec_config *cfg,
1215 struct aead_request *req,
1216 struct cipher_test_sglists *tsgls)
1218 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1219 const unsigned int alignmask = crypto_aead_alignmask(tfm);
1220 const unsigned int ivsize = crypto_aead_ivsize(tfm);
1221 const unsigned int authsize = vec->clen - vec->plen;
1222 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1223 const char *op = enc ? "encryption" : "decryption";
1224 DECLARE_CRYPTO_WAIT(wait);
1225 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
1226 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
1228 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
1229 struct kvec input[2];
1234 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1236 crypto_aead_clear_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1237 err = crypto_aead_setkey(tfm, vec->key, vec->klen);
1239 if (vec->fail) /* expectedly failed to set key? */
1241 pr_err("alg: aead: %s setkey failed with err %d on test vector %u; flags=%#x\n",
1242 driver, err, vec_num, crypto_aead_get_flags(tfm));
1246 pr_err("alg: aead: %s setkey unexpectedly succeeded on test vector %u\n",
1251 /* Set the authentication tag size */
1252 err = crypto_aead_setauthsize(tfm, authsize);
1254 pr_err("alg: aead: %s setauthsize failed with err %d on test vector %u\n",
1255 driver, err, vec_num);
1259 /* The IV must be copied to a buffer, as the algorithm may modify it */
1260 if (WARN_ON(ivsize > MAX_IVLEN))
1263 memcpy(iv, vec->iv, ivsize);
1265 memset(iv, 0, ivsize);
1267 /* Build the src/dst scatterlists */
1268 input[0].iov_base = (void *)vec->assoc;
1269 input[0].iov_len = vec->alen;
1270 input[1].iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
1271 input[1].iov_len = enc ? vec->plen : vec->clen;
1272 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
1273 vec->alen + (enc ? vec->plen :
1275 vec->alen + (enc ? vec->clen :
1279 pr_err("alg: aead: %s %s: error preparing scatterlists for test vector %u, cfg=\"%s\"\n",
1280 driver, op, vec_num, cfg->name);
1284 /* Do the actual encryption or decryption */
1285 testmgr_poison(req->__ctx, crypto_aead_reqsize(tfm));
1286 aead_request_set_callback(req, req_flags, crypto_req_done, &wait);
1287 aead_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
1288 enc ? vec->plen : vec->clen, iv);
1289 aead_request_set_ad(req, vec->alen);
1290 err = crypto_wait_req(enc ? crypto_aead_encrypt(req) :
1291 crypto_aead_decrypt(req), &wait);
1293 aead_request_set_tfm(req, tfm); /* TODO: get rid of this */
1296 if (err == -EBADMSG && vec->novrfy)
1298 pr_err("alg: aead: %s %s failed with err %d on test vector %u, cfg=\"%s\"\n",
1299 driver, op, err, vec_num, cfg->name);
1303 pr_err("alg: aead: %s %s unexpectedly succeeded on test vector %u, cfg=\"%s\"\n",
1304 driver, op, vec_num, cfg->name);
1308 /* Check for the correct output (ciphertext or plaintext) */
1309 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
1310 enc ? vec->clen : vec->plen,
1311 vec->alen, enc || !cfg->inplace);
1312 if (err == -EOVERFLOW) {
1313 pr_err("alg: aead: %s %s overran dst buffer on test vector %u, cfg=\"%s\"\n",
1314 driver, op, vec_num, cfg->name);
1318 pr_err("alg: aead: %s %s test failed (wrong result) on test vector %u, cfg=\"%s\"\n",
1319 driver, op, vec_num, cfg->name);
1326 static int test_aead_vec(const char *driver, int enc,
1327 const struct aead_testvec *vec, unsigned int vec_num,
1328 struct aead_request *req,
1329 struct cipher_test_sglists *tsgls)
1334 if (enc && vec->novrfy)
1337 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
1338 err = test_aead_vec_cfg(driver, enc, vec, vec_num,
1339 &default_cipher_testvec_configs[i],
1345 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1346 if (!noextratests) {
1347 struct testvec_config cfg;
1348 char cfgname[TESTVEC_CONFIG_NAMELEN];
1350 for (i = 0; i < fuzz_iterations; i++) {
1351 generate_random_testvec_config(&cfg, cfgname,
1353 err = test_aead_vec_cfg(driver, enc, vec, vec_num,
1363 static int test_aead(const char *driver, int enc,
1364 const struct aead_test_suite *suite,
1365 struct aead_request *req,
1366 struct cipher_test_sglists *tsgls)
1371 for (i = 0; i < suite->count; i++) {
1372 err = test_aead_vec(driver, enc, &suite->vecs[i], i, req,
1380 static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
1383 const struct aead_test_suite *suite = &desc->suite.aead;
1384 struct crypto_aead *tfm;
1385 struct aead_request *req = NULL;
1386 struct cipher_test_sglists *tsgls = NULL;
1389 if (suite->count <= 0) {
1390 pr_err("alg: aead: empty test suite for %s\n", driver);
1394 tfm = crypto_alloc_aead(driver, type, mask);
1396 pr_err("alg: aead: failed to allocate transform for %s: %ld\n",
1397 driver, PTR_ERR(tfm));
1398 return PTR_ERR(tfm);
1401 req = aead_request_alloc(tfm, GFP_KERNEL);
1403 pr_err("alg: aead: failed to allocate request for %s\n",
1409 tsgls = alloc_cipher_test_sglists();
1411 pr_err("alg: aead: failed to allocate test buffers for %s\n",
1417 err = test_aead(driver, ENCRYPT, suite, req, tsgls);
1421 err = test_aead(driver, DECRYPT, suite, req, tsgls);
1423 free_cipher_test_sglists(tsgls);
1424 aead_request_free(req);
1425 crypto_free_aead(tfm);
1429 static int test_cipher(struct crypto_cipher *tfm, int enc,
1430 const struct cipher_testvec *template,
1431 unsigned int tcount)
1433 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
1434 unsigned int i, j, k;
1437 const char *input, *result;
1439 char *xbuf[XBUFSIZE];
1442 if (testmgr_alloc_buf(xbuf))
1451 for (i = 0; i < tcount; i++) {
1453 if (fips_enabled && template[i].fips_skip)
1456 input = enc ? template[i].ptext : template[i].ctext;
1457 result = enc ? template[i].ctext : template[i].ptext;
1461 if (WARN_ON(template[i].len > PAGE_SIZE))
1465 memcpy(data, input, template[i].len);
1467 crypto_cipher_clear_flags(tfm, ~0);
1469 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1471 ret = crypto_cipher_setkey(tfm, template[i].key,
1473 if (template[i].fail == !ret) {
1474 printk(KERN_ERR "alg: cipher: setkey failed "
1475 "on test %d for %s: flags=%x\n", j,
1476 algo, crypto_cipher_get_flags(tfm));
1481 for (k = 0; k < template[i].len;
1482 k += crypto_cipher_blocksize(tfm)) {
1484 crypto_cipher_encrypt_one(tfm, data + k,
1487 crypto_cipher_decrypt_one(tfm, data + k,
1492 if (memcmp(q, result, template[i].len)) {
1493 printk(KERN_ERR "alg: cipher: Test %d failed "
1494 "on %s for %s\n", j, e, algo);
1495 hexdump(q, template[i].len);
1504 testmgr_free_buf(xbuf);
1509 static int test_skcipher_vec_cfg(const char *driver, int enc,
1510 const struct cipher_testvec *vec,
1511 unsigned int vec_num,
1512 const struct testvec_config *cfg,
1513 struct skcipher_request *req,
1514 struct cipher_test_sglists *tsgls)
1516 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1517 const unsigned int alignmask = crypto_skcipher_alignmask(tfm);
1518 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
1519 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1520 const char *op = enc ? "encryption" : "decryption";
1521 DECLARE_CRYPTO_WAIT(wait);
1522 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
1523 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
1525 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
1531 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1533 crypto_skcipher_clear_flags(tfm,
1534 CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1535 err = crypto_skcipher_setkey(tfm, vec->key, vec->klen);
1537 if (vec->fail) /* expectedly failed to set key? */
1539 pr_err("alg: skcipher: %s setkey failed with err %d on test vector %u; flags=%#x\n",
1540 driver, err, vec_num, crypto_skcipher_get_flags(tfm));
1544 pr_err("alg: skcipher: %s setkey unexpectedly succeeded on test vector %u\n",
1549 /* The IV must be copied to a buffer, as the algorithm may modify it */
1551 if (WARN_ON(ivsize > MAX_IVLEN))
1553 if (vec->iv && !(vec->generates_iv && enc))
1554 memcpy(iv, vec->iv, ivsize);
1556 memset(iv, 0, ivsize);
1558 if (vec->generates_iv) {
1559 pr_err("alg: skcipher: %s has ivsize=0 but test vector %u generates IV!\n",
1566 /* Build the src/dst scatterlists */
1567 input.iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
1568 input.iov_len = vec->len;
1569 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
1570 vec->len, vec->len, &input, 1);
1572 pr_err("alg: skcipher: %s %s: error preparing scatterlists for test vector %u, cfg=\"%s\"\n",
1573 driver, op, vec_num, cfg->name);
1577 /* Do the actual encryption or decryption */
1578 testmgr_poison(req->__ctx, crypto_skcipher_reqsize(tfm));
1579 skcipher_request_set_callback(req, req_flags, crypto_req_done, &wait);
1580 skcipher_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
1582 err = crypto_wait_req(enc ? crypto_skcipher_encrypt(req) :
1583 crypto_skcipher_decrypt(req), &wait);
1585 pr_err("alg: skcipher: %s %s failed with err %d on test vector %u, cfg=\"%s\"\n",
1586 driver, op, err, vec_num, cfg->name);
1590 /* Check for the correct output (ciphertext or plaintext) */
1591 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
1593 if (err == -EOVERFLOW) {
1594 pr_err("alg: skcipher: %s %s overran dst buffer on test vector %u, cfg=\"%s\"\n",
1595 driver, op, vec_num, cfg->name);
1599 pr_err("alg: skcipher: %s %s test failed (wrong result) on test vector %u, cfg=\"%s\"\n",
1600 driver, op, vec_num, cfg->name);
1604 /* If applicable, check that the algorithm generated the correct IV */
1605 if (vec->generates_iv && enc && memcmp(iv, vec->iv, ivsize) != 0) {
1606 pr_err("alg: skcipher: %s %s test failed (wrong output IV) on test vector %u, cfg=\"%s\"\n",
1607 driver, op, vec_num, cfg->name);
1608 hexdump(iv, ivsize);
1615 static int test_skcipher_vec(const char *driver, int enc,
1616 const struct cipher_testvec *vec,
1617 unsigned int vec_num,
1618 struct skcipher_request *req,
1619 struct cipher_test_sglists *tsgls)
1624 if (fips_enabled && vec->fips_skip)
1627 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
1628 err = test_skcipher_vec_cfg(driver, enc, vec, vec_num,
1629 &default_cipher_testvec_configs[i],
1635 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1636 if (!noextratests) {
1637 struct testvec_config cfg;
1638 char cfgname[TESTVEC_CONFIG_NAMELEN];
1640 for (i = 0; i < fuzz_iterations; i++) {
1641 generate_random_testvec_config(&cfg, cfgname,
1643 err = test_skcipher_vec_cfg(driver, enc, vec, vec_num,
1653 static int test_skcipher(const char *driver, int enc,
1654 const struct cipher_test_suite *suite,
1655 struct skcipher_request *req,
1656 struct cipher_test_sglists *tsgls)
1661 for (i = 0; i < suite->count; i++) {
1662 err = test_skcipher_vec(driver, enc, &suite->vecs[i], i, req,
1670 static int alg_test_skcipher(const struct alg_test_desc *desc,
1671 const char *driver, u32 type, u32 mask)
1673 const struct cipher_test_suite *suite = &desc->suite.cipher;
1674 struct crypto_skcipher *tfm;
1675 struct skcipher_request *req = NULL;
1676 struct cipher_test_sglists *tsgls = NULL;
1679 if (suite->count <= 0) {
1680 pr_err("alg: skcipher: empty test suite for %s\n", driver);
1684 tfm = crypto_alloc_skcipher(driver, type, mask);
1686 pr_err("alg: skcipher: failed to allocate transform for %s: %ld\n",
1687 driver, PTR_ERR(tfm));
1688 return PTR_ERR(tfm);
1691 req = skcipher_request_alloc(tfm, GFP_KERNEL);
1693 pr_err("alg: skcipher: failed to allocate request for %s\n",
1699 tsgls = alloc_cipher_test_sglists();
1701 pr_err("alg: skcipher: failed to allocate test buffers for %s\n",
1707 err = test_skcipher(driver, ENCRYPT, suite, req, tsgls);
1711 err = test_skcipher(driver, DECRYPT, suite, req, tsgls);
1713 free_cipher_test_sglists(tsgls);
1714 skcipher_request_free(req);
1715 crypto_free_skcipher(tfm);
1719 static int test_comp(struct crypto_comp *tfm,
1720 const struct comp_testvec *ctemplate,
1721 const struct comp_testvec *dtemplate,
1722 int ctcount, int dtcount)
1724 const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
1725 char *output, *decomp_output;
1729 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1733 decomp_output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1734 if (!decomp_output) {
1739 for (i = 0; i < ctcount; i++) {
1741 unsigned int dlen = COMP_BUF_SIZE;
1743 memset(output, 0, COMP_BUF_SIZE);
1744 memset(decomp_output, 0, COMP_BUF_SIZE);
1746 ilen = ctemplate[i].inlen;
1747 ret = crypto_comp_compress(tfm, ctemplate[i].input,
1748 ilen, output, &dlen);
1750 printk(KERN_ERR "alg: comp: compression failed "
1751 "on test %d for %s: ret=%d\n", i + 1, algo,
1757 dlen = COMP_BUF_SIZE;
1758 ret = crypto_comp_decompress(tfm, output,
1759 ilen, decomp_output, &dlen);
1761 pr_err("alg: comp: compression failed: decompress: on test %d for %s failed: ret=%d\n",
1766 if (dlen != ctemplate[i].inlen) {
1767 printk(KERN_ERR "alg: comp: Compression test %d "
1768 "failed for %s: output len = %d\n", i + 1, algo,
1774 if (memcmp(decomp_output, ctemplate[i].input,
1775 ctemplate[i].inlen)) {
1776 pr_err("alg: comp: compression failed: output differs: on test %d for %s\n",
1778 hexdump(decomp_output, dlen);
1784 for (i = 0; i < dtcount; i++) {
1786 unsigned int dlen = COMP_BUF_SIZE;
1788 memset(decomp_output, 0, COMP_BUF_SIZE);
1790 ilen = dtemplate[i].inlen;
1791 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
1792 ilen, decomp_output, &dlen);
1794 printk(KERN_ERR "alg: comp: decompression failed "
1795 "on test %d for %s: ret=%d\n", i + 1, algo,
1800 if (dlen != dtemplate[i].outlen) {
1801 printk(KERN_ERR "alg: comp: Decompression test %d "
1802 "failed for %s: output len = %d\n", i + 1, algo,
1808 if (memcmp(decomp_output, dtemplate[i].output, dlen)) {
1809 printk(KERN_ERR "alg: comp: Decompression test %d "
1810 "failed for %s\n", i + 1, algo);
1811 hexdump(decomp_output, dlen);
1820 kfree(decomp_output);
1825 static int test_acomp(struct crypto_acomp *tfm,
1826 const struct comp_testvec *ctemplate,
1827 const struct comp_testvec *dtemplate,
1828 int ctcount, int dtcount)
1830 const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
1832 char *output, *decomp_out;
1834 struct scatterlist src, dst;
1835 struct acomp_req *req;
1836 struct crypto_wait wait;
1838 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1842 decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1848 for (i = 0; i < ctcount; i++) {
1849 unsigned int dlen = COMP_BUF_SIZE;
1850 int ilen = ctemplate[i].inlen;
1853 input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL);
1859 memset(output, 0, dlen);
1860 crypto_init_wait(&wait);
1861 sg_init_one(&src, input_vec, ilen);
1862 sg_init_one(&dst, output, dlen);
1864 req = acomp_request_alloc(tfm);
1866 pr_err("alg: acomp: request alloc failed for %s\n",
1873 acomp_request_set_params(req, &src, &dst, ilen, dlen);
1874 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1875 crypto_req_done, &wait);
1877 ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
1879 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
1882 acomp_request_free(req);
1887 dlen = COMP_BUF_SIZE;
1888 sg_init_one(&src, output, ilen);
1889 sg_init_one(&dst, decomp_out, dlen);
1890 crypto_init_wait(&wait);
1891 acomp_request_set_params(req, &src, &dst, ilen, dlen);
1893 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
1895 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
1898 acomp_request_free(req);
1902 if (req->dlen != ctemplate[i].inlen) {
1903 pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
1904 i + 1, algo, req->dlen);
1907 acomp_request_free(req);
1911 if (memcmp(input_vec, decomp_out, req->dlen)) {
1912 pr_err("alg: acomp: Compression test %d failed for %s\n",
1914 hexdump(output, req->dlen);
1917 acomp_request_free(req);
1922 acomp_request_free(req);
1925 for (i = 0; i < dtcount; i++) {
1926 unsigned int dlen = COMP_BUF_SIZE;
1927 int ilen = dtemplate[i].inlen;
1930 input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL);
1936 memset(output, 0, dlen);
1937 crypto_init_wait(&wait);
1938 sg_init_one(&src, input_vec, ilen);
1939 sg_init_one(&dst, output, dlen);
1941 req = acomp_request_alloc(tfm);
1943 pr_err("alg: acomp: request alloc failed for %s\n",
1950 acomp_request_set_params(req, &src, &dst, ilen, dlen);
1951 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1952 crypto_req_done, &wait);
1954 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
1956 pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
1959 acomp_request_free(req);
1963 if (req->dlen != dtemplate[i].outlen) {
1964 pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
1965 i + 1, algo, req->dlen);
1968 acomp_request_free(req);
1972 if (memcmp(output, dtemplate[i].output, req->dlen)) {
1973 pr_err("alg: acomp: Decompression test %d failed for %s\n",
1975 hexdump(output, req->dlen);
1978 acomp_request_free(req);
1983 acomp_request_free(req);
1994 static int test_cprng(struct crypto_rng *tfm,
1995 const struct cprng_testvec *template,
1996 unsigned int tcount)
1998 const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
1999 int err = 0, i, j, seedsize;
2003 seedsize = crypto_rng_seedsize(tfm);
2005 seed = kmalloc(seedsize, GFP_KERNEL);
2007 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
2012 for (i = 0; i < tcount; i++) {
2013 memset(result, 0, 32);
2015 memcpy(seed, template[i].v, template[i].vlen);
2016 memcpy(seed + template[i].vlen, template[i].key,
2018 memcpy(seed + template[i].vlen + template[i].klen,
2019 template[i].dt, template[i].dtlen);
2021 err = crypto_rng_reset(tfm, seed, seedsize);
2023 printk(KERN_ERR "alg: cprng: Failed to reset rng "
2028 for (j = 0; j < template[i].loops; j++) {
2029 err = crypto_rng_get_bytes(tfm, result,
2032 printk(KERN_ERR "alg: cprng: Failed to obtain "
2033 "the correct amount of random data for "
2034 "%s (requested %d)\n", algo,
2040 err = memcmp(result, template[i].result,
2043 printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
2045 hexdump(result, template[i].rlen);
2056 static int alg_test_cipher(const struct alg_test_desc *desc,
2057 const char *driver, u32 type, u32 mask)
2059 const struct cipher_test_suite *suite = &desc->suite.cipher;
2060 struct crypto_cipher *tfm;
2063 tfm = crypto_alloc_cipher(driver, type, mask);
2065 printk(KERN_ERR "alg: cipher: Failed to load transform for "
2066 "%s: %ld\n", driver, PTR_ERR(tfm));
2067 return PTR_ERR(tfm);
2070 err = test_cipher(tfm, ENCRYPT, suite->vecs, suite->count);
2072 err = test_cipher(tfm, DECRYPT, suite->vecs, suite->count);
2074 crypto_free_cipher(tfm);
2078 static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
2081 struct crypto_comp *comp;
2082 struct crypto_acomp *acomp;
2084 u32 algo_type = type & CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
2086 if (algo_type == CRYPTO_ALG_TYPE_ACOMPRESS) {
2087 acomp = crypto_alloc_acomp(driver, type, mask);
2088 if (IS_ERR(acomp)) {
2089 pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
2090 driver, PTR_ERR(acomp));
2091 return PTR_ERR(acomp);
2093 err = test_acomp(acomp, desc->suite.comp.comp.vecs,
2094 desc->suite.comp.decomp.vecs,
2095 desc->suite.comp.comp.count,
2096 desc->suite.comp.decomp.count);
2097 crypto_free_acomp(acomp);
2099 comp = crypto_alloc_comp(driver, type, mask);
2101 pr_err("alg: comp: Failed to load transform for %s: %ld\n",
2102 driver, PTR_ERR(comp));
2103 return PTR_ERR(comp);
2106 err = test_comp(comp, desc->suite.comp.comp.vecs,
2107 desc->suite.comp.decomp.vecs,
2108 desc->suite.comp.comp.count,
2109 desc->suite.comp.decomp.count);
2111 crypto_free_comp(comp);
2116 static int __alg_test_hash(const struct hash_testvec *template,
2117 unsigned int tcount, const char *driver,
2120 struct crypto_ahash *tfm;
2123 tfm = crypto_alloc_ahash(driver, type, mask);
2125 printk(KERN_ERR "alg: hash: Failed to load transform for %s: "
2126 "%ld\n", driver, PTR_ERR(tfm));
2127 return PTR_ERR(tfm);
2130 err = test_hash(tfm, template, tcount, HASH_TEST_DIGEST);
2132 err = test_hash(tfm, template, tcount, HASH_TEST_FINAL);
2134 err = test_hash(tfm, template, tcount, HASH_TEST_FINUP);
2135 crypto_free_ahash(tfm);
2139 static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
2142 const struct hash_testvec *template = desc->suite.hash.vecs;
2143 unsigned int tcount = desc->suite.hash.count;
2144 unsigned int nr_unkeyed, nr_keyed;
2148 * For OPTIONAL_KEY algorithms, we have to do all the unkeyed tests
2149 * first, before setting a key on the tfm. To make this easier, we
2150 * require that the unkeyed test vectors (if any) are listed first.
2153 for (nr_unkeyed = 0; nr_unkeyed < tcount; nr_unkeyed++) {
2154 if (template[nr_unkeyed].ksize)
2157 for (nr_keyed = 0; nr_unkeyed + nr_keyed < tcount; nr_keyed++) {
2158 if (!template[nr_unkeyed + nr_keyed].ksize) {
2159 pr_err("alg: hash: test vectors for %s out of order, "
2160 "unkeyed ones must come first\n", desc->alg);
2167 err = __alg_test_hash(template, nr_unkeyed, driver, type, mask);
2168 template += nr_unkeyed;
2171 if (!err && nr_keyed)
2172 err = __alg_test_hash(template, nr_keyed, driver, type, mask);
2177 static int alg_test_crc32c(const struct alg_test_desc *desc,
2178 const char *driver, u32 type, u32 mask)
2180 struct crypto_shash *tfm;
2184 err = alg_test_hash(desc, driver, type, mask);
2188 tfm = crypto_alloc_shash(driver, type, mask);
2190 if (PTR_ERR(tfm) == -ENOENT) {
2192 * This crc32c implementation is only available through
2193 * ahash API, not the shash API, so the remaining part
2194 * of the test is not applicable to it.
2198 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
2199 "%ld\n", driver, PTR_ERR(tfm));
2200 return PTR_ERR(tfm);
2204 SHASH_DESC_ON_STACK(shash, tfm);
2205 u32 *ctx = (u32 *)shash_desc_ctx(shash);
2211 err = crypto_shash_final(shash, (u8 *)&val);
2213 printk(KERN_ERR "alg: crc32c: Operation failed for "
2214 "%s: %d\n", driver, err);
2218 if (val != cpu_to_le32(~420553207)) {
2219 pr_err("alg: crc32c: Test failed for %s: %u\n",
2220 driver, le32_to_cpu(val));
2225 crypto_free_shash(tfm);
2230 static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
2233 struct crypto_rng *rng;
2236 rng = crypto_alloc_rng(driver, type, mask);
2238 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
2239 "%ld\n", driver, PTR_ERR(rng));
2240 return PTR_ERR(rng);
2243 err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
2245 crypto_free_rng(rng);
2251 static int drbg_cavs_test(const struct drbg_testvec *test, int pr,
2252 const char *driver, u32 type, u32 mask)
2255 struct crypto_rng *drng;
2256 struct drbg_test_data test_data;
2257 struct drbg_string addtl, pers, testentropy;
2258 unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
2263 drng = crypto_alloc_rng(driver, type, mask);
2265 printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
2271 test_data.testentropy = &testentropy;
2272 drbg_string_fill(&testentropy, test->entropy, test->entropylen);
2273 drbg_string_fill(&pers, test->pers, test->perslen);
2274 ret = crypto_drbg_reset_test(drng, &pers, &test_data);
2276 printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
2280 drbg_string_fill(&addtl, test->addtla, test->addtllen);
2282 drbg_string_fill(&testentropy, test->entpra, test->entprlen);
2283 ret = crypto_drbg_get_bytes_addtl_test(drng,
2284 buf, test->expectedlen, &addtl, &test_data);
2286 ret = crypto_drbg_get_bytes_addtl(drng,
2287 buf, test->expectedlen, &addtl);
2290 printk(KERN_ERR "alg: drbg: could not obtain random data for "
2291 "driver %s\n", driver);
2295 drbg_string_fill(&addtl, test->addtlb, test->addtllen);
2297 drbg_string_fill(&testentropy, test->entprb, test->entprlen);
2298 ret = crypto_drbg_get_bytes_addtl_test(drng,
2299 buf, test->expectedlen, &addtl, &test_data);
2301 ret = crypto_drbg_get_bytes_addtl(drng,
2302 buf, test->expectedlen, &addtl);
2305 printk(KERN_ERR "alg: drbg: could not obtain random data for "
2306 "driver %s\n", driver);
2310 ret = memcmp(test->expected, buf, test->expectedlen);
2313 crypto_free_rng(drng);
2319 static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
2325 const struct drbg_testvec *template = desc->suite.drbg.vecs;
2326 unsigned int tcount = desc->suite.drbg.count;
2328 if (0 == memcmp(driver, "drbg_pr_", 8))
2331 for (i = 0; i < tcount; i++) {
2332 err = drbg_cavs_test(&template[i], pr, driver, type, mask);
2334 printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
2344 static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
2347 struct kpp_request *req;
2348 void *input_buf = NULL;
2349 void *output_buf = NULL;
2350 void *a_public = NULL;
2352 void *shared_secret = NULL;
2353 struct crypto_wait wait;
2354 unsigned int out_len_max;
2356 struct scatterlist src, dst;
2358 req = kpp_request_alloc(tfm, GFP_KERNEL);
2362 crypto_init_wait(&wait);
2364 err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
2368 out_len_max = crypto_kpp_maxsize(tfm);
2369 output_buf = kzalloc(out_len_max, GFP_KERNEL);
2375 /* Use appropriate parameter as base */
2376 kpp_request_set_input(req, NULL, 0);
2377 sg_init_one(&dst, output_buf, out_len_max);
2378 kpp_request_set_output(req, &dst, out_len_max);
2379 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2380 crypto_req_done, &wait);
2382 /* Compute party A's public key */
2383 err = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
2385 pr_err("alg: %s: Party A: generate public key test failed. err %d\n",
2391 /* Save party A's public key */
2392 a_public = kmemdup(sg_virt(req->dst), out_len_max, GFP_KERNEL);
2398 /* Verify calculated public key */
2399 if (memcmp(vec->expected_a_public, sg_virt(req->dst),
2400 vec->expected_a_public_size)) {
2401 pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n",
2408 /* Calculate shared secret key by using counter part (b) public key. */
2409 input_buf = kmemdup(vec->b_public, vec->b_public_size, GFP_KERNEL);
2415 sg_init_one(&src, input_buf, vec->b_public_size);
2416 sg_init_one(&dst, output_buf, out_len_max);
2417 kpp_request_set_input(req, &src, vec->b_public_size);
2418 kpp_request_set_output(req, &dst, out_len_max);
2419 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2420 crypto_req_done, &wait);
2421 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait);
2423 pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n",
2429 /* Save the shared secret obtained by party A */
2430 a_ss = kmemdup(sg_virt(req->dst), vec->expected_ss_size, GFP_KERNEL);
2437 * Calculate party B's shared secret by using party A's
2440 err = crypto_kpp_set_secret(tfm, vec->b_secret,
2441 vec->b_secret_size);
2445 sg_init_one(&src, a_public, vec->expected_a_public_size);
2446 sg_init_one(&dst, output_buf, out_len_max);
2447 kpp_request_set_input(req, &src, vec->expected_a_public_size);
2448 kpp_request_set_output(req, &dst, out_len_max);
2449 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2450 crypto_req_done, &wait);
2451 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req),
2454 pr_err("alg: %s: Party B: compute shared secret failed. err %d\n",
2459 shared_secret = a_ss;
2461 shared_secret = (void *)vec->expected_ss;
2465 * verify shared secret from which the user will derive
2466 * secret key by executing whatever hash it has chosen
2468 if (memcmp(shared_secret, sg_virt(req->dst),
2469 vec->expected_ss_size)) {
2470 pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
2482 kpp_request_free(req);
2486 static int test_kpp(struct crypto_kpp *tfm, const char *alg,
2487 const struct kpp_testvec *vecs, unsigned int tcount)
2491 for (i = 0; i < tcount; i++) {
2492 ret = do_test_kpp(tfm, vecs++, alg);
2494 pr_err("alg: %s: test failed on vector %d, err=%d\n",
2502 static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
2505 struct crypto_kpp *tfm;
2508 tfm = crypto_alloc_kpp(driver, type, mask);
2510 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
2511 driver, PTR_ERR(tfm));
2512 return PTR_ERR(tfm);
2514 if (desc->suite.kpp.vecs)
2515 err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
2516 desc->suite.kpp.count);
2518 crypto_free_kpp(tfm);
2522 static int test_akcipher_one(struct crypto_akcipher *tfm,
2523 const struct akcipher_testvec *vecs)
2525 char *xbuf[XBUFSIZE];
2526 struct akcipher_request *req;
2527 void *outbuf_enc = NULL;
2528 void *outbuf_dec = NULL;
2529 struct crypto_wait wait;
2530 unsigned int out_len_max, out_len = 0;
2532 struct scatterlist src, dst, src_tab[2];
2534 unsigned int m_size, c_size;
2537 if (testmgr_alloc_buf(xbuf))
2540 req = akcipher_request_alloc(tfm, GFP_KERNEL);
2544 crypto_init_wait(&wait);
2546 if (vecs->public_key_vec)
2547 err = crypto_akcipher_set_pub_key(tfm, vecs->key,
2550 err = crypto_akcipher_set_priv_key(tfm, vecs->key,
2556 out_len_max = crypto_akcipher_maxsize(tfm);
2559 * First run test which do not require a private key, such as
2560 * encrypt or verify.
2562 outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
2566 if (!vecs->siggen_sigver_test) {
2568 m_size = vecs->m_size;
2570 c_size = vecs->c_size;
2573 /* Swap args so we could keep plaintext (digest)
2574 * in vecs->m, and cooked signature in vecs->c.
2576 m = vecs->c; /* signature */
2577 m_size = vecs->c_size;
2578 c = vecs->m; /* digest */
2579 c_size = vecs->m_size;
2583 if (WARN_ON(m_size > PAGE_SIZE))
2585 memcpy(xbuf[0], m, m_size);
2587 sg_init_table(src_tab, 2);
2588 sg_set_buf(&src_tab[0], xbuf[0], 8);
2589 sg_set_buf(&src_tab[1], xbuf[0] + 8, m_size - 8);
2590 sg_init_one(&dst, outbuf_enc, out_len_max);
2591 akcipher_request_set_crypt(req, src_tab, &dst, m_size,
2593 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2594 crypto_req_done, &wait);
2596 err = crypto_wait_req(vecs->siggen_sigver_test ?
2597 /* Run asymmetric signature verification */
2598 crypto_akcipher_verify(req) :
2599 /* Run asymmetric encrypt */
2600 crypto_akcipher_encrypt(req), &wait);
2602 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
2605 if (req->dst_len != c_size) {
2606 pr_err("alg: akcipher: %s test failed. Invalid output len\n",
2611 /* verify that encrypted message is equal to expected */
2612 if (memcmp(c, outbuf_enc, c_size)) {
2613 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
2614 hexdump(outbuf_enc, c_size);
2620 * Don't invoke (decrypt or sign) test which require a private key
2621 * for vectors with only a public key.
2623 if (vecs->public_key_vec) {
2627 outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
2633 op = vecs->siggen_sigver_test ? "sign" : "decrypt";
2634 if (WARN_ON(c_size > PAGE_SIZE))
2636 memcpy(xbuf[0], c, c_size);
2638 sg_init_one(&src, xbuf[0], c_size);
2639 sg_init_one(&dst, outbuf_dec, out_len_max);
2640 crypto_init_wait(&wait);
2641 akcipher_request_set_crypt(req, &src, &dst, c_size, out_len_max);
2643 err = crypto_wait_req(vecs->siggen_sigver_test ?
2644 /* Run asymmetric signature generation */
2645 crypto_akcipher_sign(req) :
2646 /* Run asymmetric decrypt */
2647 crypto_akcipher_decrypt(req), &wait);
2649 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
2652 out_len = req->dst_len;
2653 if (out_len < m_size) {
2654 pr_err("alg: akcipher: %s test failed. Invalid output len %u\n",
2659 /* verify that decrypted message is equal to the original msg */
2660 if (memchr_inv(outbuf_dec, 0, out_len - m_size) ||
2661 memcmp(m, outbuf_dec + out_len - m_size, m_size)) {
2662 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
2663 hexdump(outbuf_dec, out_len);
2670 akcipher_request_free(req);
2672 testmgr_free_buf(xbuf);
2676 static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
2677 const struct akcipher_testvec *vecs,
2678 unsigned int tcount)
2681 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
2684 for (i = 0; i < tcount; i++) {
2685 ret = test_akcipher_one(tfm, vecs++);
2689 pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
2696 static int alg_test_akcipher(const struct alg_test_desc *desc,
2697 const char *driver, u32 type, u32 mask)
2699 struct crypto_akcipher *tfm;
2702 tfm = crypto_alloc_akcipher(driver, type, mask);
2704 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
2705 driver, PTR_ERR(tfm));
2706 return PTR_ERR(tfm);
2708 if (desc->suite.akcipher.vecs)
2709 err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
2710 desc->suite.akcipher.count);
2712 crypto_free_akcipher(tfm);
2716 static int alg_test_null(const struct alg_test_desc *desc,
2717 const char *driver, u32 type, u32 mask)
2722 #define __VECS(tv) { .vecs = tv, .count = ARRAY_SIZE(tv) }
2724 /* Please keep this list sorted by algorithm name. */
2725 static const struct alg_test_desc alg_test_descs[] = {
2727 .alg = "adiantum(xchacha12,aes)",
2728 .test = alg_test_skcipher,
2730 .cipher = __VECS(adiantum_xchacha12_aes_tv_template)
2733 .alg = "adiantum(xchacha20,aes)",
2734 .test = alg_test_skcipher,
2736 .cipher = __VECS(adiantum_xchacha20_aes_tv_template)
2740 .test = alg_test_aead,
2742 .aead = __VECS(aegis128_tv_template)
2746 .test = alg_test_aead,
2748 .aead = __VECS(aegis128l_tv_template)
2752 .test = alg_test_aead,
2754 .aead = __VECS(aegis256_tv_template)
2757 .alg = "ansi_cprng",
2758 .test = alg_test_cprng,
2760 .cprng = __VECS(ansi_cprng_aes_tv_template)
2763 .alg = "authenc(hmac(md5),ecb(cipher_null))",
2764 .test = alg_test_aead,
2766 .aead = __VECS(hmac_md5_ecb_cipher_null_tv_template)
2769 .alg = "authenc(hmac(sha1),cbc(aes))",
2770 .test = alg_test_aead,
2773 .aead = __VECS(hmac_sha1_aes_cbc_tv_temp)
2776 .alg = "authenc(hmac(sha1),cbc(des))",
2777 .test = alg_test_aead,
2779 .aead = __VECS(hmac_sha1_des_cbc_tv_temp)
2782 .alg = "authenc(hmac(sha1),cbc(des3_ede))",
2783 .test = alg_test_aead,
2786 .aead = __VECS(hmac_sha1_des3_ede_cbc_tv_temp)
2789 .alg = "authenc(hmac(sha1),ctr(aes))",
2790 .test = alg_test_null,
2793 .alg = "authenc(hmac(sha1),ecb(cipher_null))",
2794 .test = alg_test_aead,
2796 .aead = __VECS(hmac_sha1_ecb_cipher_null_tv_temp)
2799 .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
2800 .test = alg_test_null,
2803 .alg = "authenc(hmac(sha224),cbc(des))",
2804 .test = alg_test_aead,
2806 .aead = __VECS(hmac_sha224_des_cbc_tv_temp)
2809 .alg = "authenc(hmac(sha224),cbc(des3_ede))",
2810 .test = alg_test_aead,
2813 .aead = __VECS(hmac_sha224_des3_ede_cbc_tv_temp)
2816 .alg = "authenc(hmac(sha256),cbc(aes))",
2817 .test = alg_test_aead,
2820 .aead = __VECS(hmac_sha256_aes_cbc_tv_temp)
2823 .alg = "authenc(hmac(sha256),cbc(des))",
2824 .test = alg_test_aead,
2826 .aead = __VECS(hmac_sha256_des_cbc_tv_temp)
2829 .alg = "authenc(hmac(sha256),cbc(des3_ede))",
2830 .test = alg_test_aead,
2833 .aead = __VECS(hmac_sha256_des3_ede_cbc_tv_temp)
2836 .alg = "authenc(hmac(sha256),ctr(aes))",
2837 .test = alg_test_null,
2840 .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
2841 .test = alg_test_null,
2844 .alg = "authenc(hmac(sha384),cbc(des))",
2845 .test = alg_test_aead,
2847 .aead = __VECS(hmac_sha384_des_cbc_tv_temp)
2850 .alg = "authenc(hmac(sha384),cbc(des3_ede))",
2851 .test = alg_test_aead,
2854 .aead = __VECS(hmac_sha384_des3_ede_cbc_tv_temp)
2857 .alg = "authenc(hmac(sha384),ctr(aes))",
2858 .test = alg_test_null,
2861 .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
2862 .test = alg_test_null,
2865 .alg = "authenc(hmac(sha512),cbc(aes))",
2867 .test = alg_test_aead,
2869 .aead = __VECS(hmac_sha512_aes_cbc_tv_temp)
2872 .alg = "authenc(hmac(sha512),cbc(des))",
2873 .test = alg_test_aead,
2875 .aead = __VECS(hmac_sha512_des_cbc_tv_temp)
2878 .alg = "authenc(hmac(sha512),cbc(des3_ede))",
2879 .test = alg_test_aead,
2882 .aead = __VECS(hmac_sha512_des3_ede_cbc_tv_temp)
2885 .alg = "authenc(hmac(sha512),ctr(aes))",
2886 .test = alg_test_null,
2889 .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
2890 .test = alg_test_null,
2894 .test = alg_test_skcipher,
2897 .cipher = __VECS(aes_cbc_tv_template)
2900 .alg = "cbc(anubis)",
2901 .test = alg_test_skcipher,
2903 .cipher = __VECS(anubis_cbc_tv_template)
2906 .alg = "cbc(blowfish)",
2907 .test = alg_test_skcipher,
2909 .cipher = __VECS(bf_cbc_tv_template)
2912 .alg = "cbc(camellia)",
2913 .test = alg_test_skcipher,
2915 .cipher = __VECS(camellia_cbc_tv_template)
2918 .alg = "cbc(cast5)",
2919 .test = alg_test_skcipher,
2921 .cipher = __VECS(cast5_cbc_tv_template)
2924 .alg = "cbc(cast6)",
2925 .test = alg_test_skcipher,
2927 .cipher = __VECS(cast6_cbc_tv_template)
2931 .test = alg_test_skcipher,
2933 .cipher = __VECS(des_cbc_tv_template)
2936 .alg = "cbc(des3_ede)",
2937 .test = alg_test_skcipher,
2940 .cipher = __VECS(des3_ede_cbc_tv_template)
2943 /* Same as cbc(aes) except the key is stored in
2944 * hardware secure memory which we reference by index
2947 .test = alg_test_null,
2950 .alg = "cbc(serpent)",
2951 .test = alg_test_skcipher,
2953 .cipher = __VECS(serpent_cbc_tv_template)
2957 .test = alg_test_skcipher,
2959 .cipher = __VECS(sm4_cbc_tv_template)
2962 .alg = "cbc(twofish)",
2963 .test = alg_test_skcipher,
2965 .cipher = __VECS(tf_cbc_tv_template)
2968 .alg = "cbcmac(aes)",
2970 .test = alg_test_hash,
2972 .hash = __VECS(aes_cbcmac_tv_template)
2976 .test = alg_test_aead,
2979 .aead = __VECS(aes_ccm_tv_template)
2983 .test = alg_test_skcipher,
2986 .cipher = __VECS(aes_cfb_tv_template)
2990 .test = alg_test_skcipher,
2992 .cipher = __VECS(chacha20_tv_template)
2997 .test = alg_test_hash,
2999 .hash = __VECS(aes_cmac128_tv_template)
3002 .alg = "cmac(des3_ede)",
3004 .test = alg_test_hash,
3006 .hash = __VECS(des3_ede_cmac64_tv_template)
3009 .alg = "compress_null",
3010 .test = alg_test_null,
3013 .test = alg_test_hash,
3016 .hash = __VECS(crc32_tv_template)
3020 .test = alg_test_crc32c,
3023 .hash = __VECS(crc32c_tv_template)
3027 .test = alg_test_hash,
3030 .hash = __VECS(crct10dif_tv_template)
3034 .test = alg_test_skcipher,
3037 .cipher = __VECS(aes_ctr_tv_template)
3040 .alg = "ctr(blowfish)",
3041 .test = alg_test_skcipher,
3043 .cipher = __VECS(bf_ctr_tv_template)
3046 .alg = "ctr(camellia)",
3047 .test = alg_test_skcipher,
3049 .cipher = __VECS(camellia_ctr_tv_template)
3052 .alg = "ctr(cast5)",
3053 .test = alg_test_skcipher,
3055 .cipher = __VECS(cast5_ctr_tv_template)
3058 .alg = "ctr(cast6)",
3059 .test = alg_test_skcipher,
3061 .cipher = __VECS(cast6_ctr_tv_template)
3065 .test = alg_test_skcipher,
3067 .cipher = __VECS(des_ctr_tv_template)
3070 .alg = "ctr(des3_ede)",
3071 .test = alg_test_skcipher,
3074 .cipher = __VECS(des3_ede_ctr_tv_template)
3077 /* Same as ctr(aes) except the key is stored in
3078 * hardware secure memory which we reference by index
3081 .test = alg_test_null,
3084 .alg = "ctr(serpent)",
3085 .test = alg_test_skcipher,
3087 .cipher = __VECS(serpent_ctr_tv_template)
3091 .test = alg_test_skcipher,
3093 .cipher = __VECS(sm4_ctr_tv_template)
3096 .alg = "ctr(twofish)",
3097 .test = alg_test_skcipher,
3099 .cipher = __VECS(tf_ctr_tv_template)
3102 .alg = "cts(cbc(aes))",
3103 .test = alg_test_skcipher,
3106 .cipher = __VECS(cts_mode_tv_template)
3110 .test = alg_test_comp,
3114 .comp = __VECS(deflate_comp_tv_template),
3115 .decomp = __VECS(deflate_decomp_tv_template)
3120 .test = alg_test_kpp,
3123 .kpp = __VECS(dh_tv_template)
3126 .alg = "digest_null",
3127 .test = alg_test_null,
3129 .alg = "drbg_nopr_ctr_aes128",
3130 .test = alg_test_drbg,
3133 .drbg = __VECS(drbg_nopr_ctr_aes128_tv_template)
3136 .alg = "drbg_nopr_ctr_aes192",
3137 .test = alg_test_drbg,
3140 .drbg = __VECS(drbg_nopr_ctr_aes192_tv_template)
3143 .alg = "drbg_nopr_ctr_aes256",
3144 .test = alg_test_drbg,
3147 .drbg = __VECS(drbg_nopr_ctr_aes256_tv_template)
3151 * There is no need to specifically test the DRBG with every
3152 * backend cipher -- covered by drbg_nopr_hmac_sha256 test
3154 .alg = "drbg_nopr_hmac_sha1",
3156 .test = alg_test_null,
3158 .alg = "drbg_nopr_hmac_sha256",
3159 .test = alg_test_drbg,
3162 .drbg = __VECS(drbg_nopr_hmac_sha256_tv_template)
3165 /* covered by drbg_nopr_hmac_sha256 test */
3166 .alg = "drbg_nopr_hmac_sha384",
3168 .test = alg_test_null,
3170 .alg = "drbg_nopr_hmac_sha512",
3171 .test = alg_test_null,
3174 .alg = "drbg_nopr_sha1",
3176 .test = alg_test_null,
3178 .alg = "drbg_nopr_sha256",
3179 .test = alg_test_drbg,
3182 .drbg = __VECS(drbg_nopr_sha256_tv_template)
3185 /* covered by drbg_nopr_sha256 test */
3186 .alg = "drbg_nopr_sha384",
3188 .test = alg_test_null,
3190 .alg = "drbg_nopr_sha512",
3192 .test = alg_test_null,
3194 .alg = "drbg_pr_ctr_aes128",
3195 .test = alg_test_drbg,
3198 .drbg = __VECS(drbg_pr_ctr_aes128_tv_template)
3201 /* covered by drbg_pr_ctr_aes128 test */
3202 .alg = "drbg_pr_ctr_aes192",
3204 .test = alg_test_null,
3206 .alg = "drbg_pr_ctr_aes256",
3208 .test = alg_test_null,
3210 .alg = "drbg_pr_hmac_sha1",
3212 .test = alg_test_null,
3214 .alg = "drbg_pr_hmac_sha256",
3215 .test = alg_test_drbg,
3218 .drbg = __VECS(drbg_pr_hmac_sha256_tv_template)
3221 /* covered by drbg_pr_hmac_sha256 test */
3222 .alg = "drbg_pr_hmac_sha384",
3224 .test = alg_test_null,
3226 .alg = "drbg_pr_hmac_sha512",
3227 .test = alg_test_null,
3230 .alg = "drbg_pr_sha1",
3232 .test = alg_test_null,
3234 .alg = "drbg_pr_sha256",
3235 .test = alg_test_drbg,
3238 .drbg = __VECS(drbg_pr_sha256_tv_template)
3241 /* covered by drbg_pr_sha256 test */
3242 .alg = "drbg_pr_sha384",
3244 .test = alg_test_null,
3246 .alg = "drbg_pr_sha512",
3248 .test = alg_test_null,
3251 .test = alg_test_skcipher,
3254 .cipher = __VECS(aes_tv_template)
3257 .alg = "ecb(anubis)",
3258 .test = alg_test_skcipher,
3260 .cipher = __VECS(anubis_tv_template)
3264 .test = alg_test_skcipher,
3266 .cipher = __VECS(arc4_tv_template)
3269 .alg = "ecb(blowfish)",
3270 .test = alg_test_skcipher,
3272 .cipher = __VECS(bf_tv_template)
3275 .alg = "ecb(camellia)",
3276 .test = alg_test_skcipher,
3278 .cipher = __VECS(camellia_tv_template)
3281 .alg = "ecb(cast5)",
3282 .test = alg_test_skcipher,
3284 .cipher = __VECS(cast5_tv_template)
3287 .alg = "ecb(cast6)",
3288 .test = alg_test_skcipher,
3290 .cipher = __VECS(cast6_tv_template)
3293 .alg = "ecb(cipher_null)",
3294 .test = alg_test_null,
3298 .test = alg_test_skcipher,
3300 .cipher = __VECS(des_tv_template)
3303 .alg = "ecb(des3_ede)",
3304 .test = alg_test_skcipher,
3307 .cipher = __VECS(des3_ede_tv_template)
3310 .alg = "ecb(fcrypt)",
3311 .test = alg_test_skcipher,
3314 .vecs = fcrypt_pcbc_tv_template,
3319 .alg = "ecb(khazad)",
3320 .test = alg_test_skcipher,
3322 .cipher = __VECS(khazad_tv_template)
3325 /* Same as ecb(aes) except the key is stored in
3326 * hardware secure memory which we reference by index
3329 .test = alg_test_null,
3333 .test = alg_test_skcipher,
3335 .cipher = __VECS(seed_tv_template)
3338 .alg = "ecb(serpent)",
3339 .test = alg_test_skcipher,
3341 .cipher = __VECS(serpent_tv_template)
3345 .test = alg_test_skcipher,
3347 .cipher = __VECS(sm4_tv_template)
3351 .test = alg_test_skcipher,
3353 .cipher = __VECS(tea_tv_template)
3356 .alg = "ecb(tnepres)",
3357 .test = alg_test_skcipher,
3359 .cipher = __VECS(tnepres_tv_template)
3362 .alg = "ecb(twofish)",
3363 .test = alg_test_skcipher,
3365 .cipher = __VECS(tf_tv_template)
3369 .test = alg_test_skcipher,
3371 .cipher = __VECS(xeta_tv_template)
3375 .test = alg_test_skcipher,
3377 .cipher = __VECS(xtea_tv_template)
3381 .test = alg_test_kpp,
3384 .kpp = __VECS(ecdh_tv_template)
3388 .test = alg_test_aead,
3391 .aead = __VECS(aes_gcm_tv_template)
3395 .test = alg_test_hash,
3398 .hash = __VECS(ghash_tv_template)
3402 .test = alg_test_hash,
3404 .hash = __VECS(hmac_md5_tv_template)
3407 .alg = "hmac(rmd128)",
3408 .test = alg_test_hash,
3410 .hash = __VECS(hmac_rmd128_tv_template)
3413 .alg = "hmac(rmd160)",
3414 .test = alg_test_hash,
3416 .hash = __VECS(hmac_rmd160_tv_template)
3419 .alg = "hmac(sha1)",
3420 .test = alg_test_hash,
3423 .hash = __VECS(hmac_sha1_tv_template)
3426 .alg = "hmac(sha224)",
3427 .test = alg_test_hash,
3430 .hash = __VECS(hmac_sha224_tv_template)
3433 .alg = "hmac(sha256)",
3434 .test = alg_test_hash,
3437 .hash = __VECS(hmac_sha256_tv_template)
3440 .alg = "hmac(sha3-224)",
3441 .test = alg_test_hash,
3444 .hash = __VECS(hmac_sha3_224_tv_template)
3447 .alg = "hmac(sha3-256)",
3448 .test = alg_test_hash,
3451 .hash = __VECS(hmac_sha3_256_tv_template)
3454 .alg = "hmac(sha3-384)",
3455 .test = alg_test_hash,
3458 .hash = __VECS(hmac_sha3_384_tv_template)
3461 .alg = "hmac(sha3-512)",
3462 .test = alg_test_hash,
3465 .hash = __VECS(hmac_sha3_512_tv_template)
3468 .alg = "hmac(sha384)",
3469 .test = alg_test_hash,
3472 .hash = __VECS(hmac_sha384_tv_template)
3475 .alg = "hmac(sha512)",
3476 .test = alg_test_hash,
3479 .hash = __VECS(hmac_sha512_tv_template)
3482 .alg = "hmac(streebog256)",
3483 .test = alg_test_hash,
3485 .hash = __VECS(hmac_streebog256_tv_template)
3488 .alg = "hmac(streebog512)",
3489 .test = alg_test_hash,
3491 .hash = __VECS(hmac_streebog512_tv_template)
3494 .alg = "jitterentropy_rng",
3496 .test = alg_test_null,
3499 .test = alg_test_skcipher,
3502 .cipher = __VECS(aes_kw_tv_template)
3506 .test = alg_test_skcipher,
3508 .cipher = __VECS(aes_lrw_tv_template)
3511 .alg = "lrw(camellia)",
3512 .test = alg_test_skcipher,
3514 .cipher = __VECS(camellia_lrw_tv_template)
3517 .alg = "lrw(cast6)",
3518 .test = alg_test_skcipher,
3520 .cipher = __VECS(cast6_lrw_tv_template)
3523 .alg = "lrw(serpent)",
3524 .test = alg_test_skcipher,
3526 .cipher = __VECS(serpent_lrw_tv_template)
3529 .alg = "lrw(twofish)",
3530 .test = alg_test_skcipher,
3532 .cipher = __VECS(tf_lrw_tv_template)
3536 .test = alg_test_comp,
3540 .comp = __VECS(lz4_comp_tv_template),
3541 .decomp = __VECS(lz4_decomp_tv_template)
3546 .test = alg_test_comp,
3550 .comp = __VECS(lz4hc_comp_tv_template),
3551 .decomp = __VECS(lz4hc_decomp_tv_template)
3556 .test = alg_test_comp,
3560 .comp = __VECS(lzo_comp_tv_template),
3561 .decomp = __VECS(lzo_decomp_tv_template)
3566 .test = alg_test_hash,
3568 .hash = __VECS(md4_tv_template)
3572 .test = alg_test_hash,
3574 .hash = __VECS(md5_tv_template)
3577 .alg = "michael_mic",
3578 .test = alg_test_hash,
3580 .hash = __VECS(michael_mic_tv_template)
3584 .test = alg_test_aead,
3586 .aead = __VECS(morus1280_tv_template)
3590 .test = alg_test_aead,
3592 .aead = __VECS(morus640_tv_template)
3595 .alg = "nhpoly1305",
3596 .test = alg_test_hash,
3598 .hash = __VECS(nhpoly1305_tv_template)
3602 .test = alg_test_skcipher,
3605 .cipher = __VECS(aes_ofb_tv_template)
3608 /* Same as ofb(aes) except the key is stored in
3609 * hardware secure memory which we reference by index
3612 .test = alg_test_null,
3615 .alg = "pcbc(fcrypt)",
3616 .test = alg_test_skcipher,
3618 .cipher = __VECS(fcrypt_pcbc_tv_template)
3621 .alg = "pkcs1pad(rsa,sha224)",
3622 .test = alg_test_null,
3625 .alg = "pkcs1pad(rsa,sha256)",
3626 .test = alg_test_akcipher,
3629 .akcipher = __VECS(pkcs1pad_rsa_tv_template)
3632 .alg = "pkcs1pad(rsa,sha384)",
3633 .test = alg_test_null,
3636 .alg = "pkcs1pad(rsa,sha512)",
3637 .test = alg_test_null,
3641 .test = alg_test_hash,
3643 .hash = __VECS(poly1305_tv_template)
3646 .alg = "rfc3686(ctr(aes))",
3647 .test = alg_test_skcipher,
3650 .cipher = __VECS(aes_ctr_rfc3686_tv_template)
3653 .alg = "rfc4106(gcm(aes))",
3654 .test = alg_test_aead,
3657 .aead = __VECS(aes_gcm_rfc4106_tv_template)
3660 .alg = "rfc4309(ccm(aes))",
3661 .test = alg_test_aead,
3664 .aead = __VECS(aes_ccm_rfc4309_tv_template)
3667 .alg = "rfc4543(gcm(aes))",
3668 .test = alg_test_aead,
3670 .aead = __VECS(aes_gcm_rfc4543_tv_template)
3673 .alg = "rfc7539(chacha20,poly1305)",
3674 .test = alg_test_aead,
3676 .aead = __VECS(rfc7539_tv_template)
3679 .alg = "rfc7539esp(chacha20,poly1305)",
3680 .test = alg_test_aead,
3682 .aead = __VECS(rfc7539esp_tv_template)
3686 .test = alg_test_hash,
3688 .hash = __VECS(rmd128_tv_template)
3692 .test = alg_test_hash,
3694 .hash = __VECS(rmd160_tv_template)
3698 .test = alg_test_hash,
3700 .hash = __VECS(rmd256_tv_template)
3704 .test = alg_test_hash,
3706 .hash = __VECS(rmd320_tv_template)
3710 .test = alg_test_akcipher,
3713 .akcipher = __VECS(rsa_tv_template)
3717 .test = alg_test_skcipher,
3719 .cipher = __VECS(salsa20_stream_tv_template)
3723 .test = alg_test_hash,
3726 .hash = __VECS(sha1_tv_template)
3730 .test = alg_test_hash,
3733 .hash = __VECS(sha224_tv_template)
3737 .test = alg_test_hash,
3740 .hash = __VECS(sha256_tv_template)
3744 .test = alg_test_hash,
3747 .hash = __VECS(sha3_224_tv_template)
3751 .test = alg_test_hash,
3754 .hash = __VECS(sha3_256_tv_template)
3758 .test = alg_test_hash,
3761 .hash = __VECS(sha3_384_tv_template)
3765 .test = alg_test_hash,
3768 .hash = __VECS(sha3_512_tv_template)
3772 .test = alg_test_hash,
3775 .hash = __VECS(sha384_tv_template)
3779 .test = alg_test_hash,
3782 .hash = __VECS(sha512_tv_template)
3786 .test = alg_test_hash,
3788 .hash = __VECS(sm3_tv_template)
3791 .alg = "streebog256",
3792 .test = alg_test_hash,
3794 .hash = __VECS(streebog256_tv_template)
3797 .alg = "streebog512",
3798 .test = alg_test_hash,
3800 .hash = __VECS(streebog512_tv_template)
3804 .test = alg_test_hash,
3806 .hash = __VECS(tgr128_tv_template)
3810 .test = alg_test_hash,
3812 .hash = __VECS(tgr160_tv_template)
3816 .test = alg_test_hash,
3818 .hash = __VECS(tgr192_tv_template)
3821 .alg = "vmac64(aes)",
3822 .test = alg_test_hash,
3824 .hash = __VECS(vmac64_aes_tv_template)
3828 .test = alg_test_hash,
3830 .hash = __VECS(wp256_tv_template)
3834 .test = alg_test_hash,
3836 .hash = __VECS(wp384_tv_template)
3840 .test = alg_test_hash,
3842 .hash = __VECS(wp512_tv_template)
3846 .test = alg_test_hash,
3848 .hash = __VECS(aes_xcbc128_tv_template)
3852 .test = alg_test_skcipher,
3854 .cipher = __VECS(xchacha12_tv_template)
3858 .test = alg_test_skcipher,
3860 .cipher = __VECS(xchacha20_tv_template)
3864 .test = alg_test_skcipher,
3867 .cipher = __VECS(aes_xts_tv_template)
3870 .alg = "xts(camellia)",
3871 .test = alg_test_skcipher,
3873 .cipher = __VECS(camellia_xts_tv_template)
3876 .alg = "xts(cast6)",
3877 .test = alg_test_skcipher,
3879 .cipher = __VECS(cast6_xts_tv_template)
3882 /* Same as xts(aes) except the key is stored in
3883 * hardware secure memory which we reference by index
3886 .test = alg_test_null,
3889 .alg = "xts(serpent)",
3890 .test = alg_test_skcipher,
3892 .cipher = __VECS(serpent_xts_tv_template)
3895 .alg = "xts(twofish)",
3896 .test = alg_test_skcipher,
3898 .cipher = __VECS(tf_xts_tv_template)
3901 .alg = "xts4096(paes)",
3902 .test = alg_test_null,
3905 .alg = "xts512(paes)",
3906 .test = alg_test_null,
3909 .alg = "zlib-deflate",
3910 .test = alg_test_comp,
3914 .comp = __VECS(zlib_deflate_comp_tv_template),
3915 .decomp = __VECS(zlib_deflate_decomp_tv_template)
3920 .test = alg_test_comp,
3924 .comp = __VECS(zstd_comp_tv_template),
3925 .decomp = __VECS(zstd_decomp_tv_template)
3931 static void alg_check_test_descs_order(void)
3935 for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
3936 int diff = strcmp(alg_test_descs[i - 1].alg,
3937 alg_test_descs[i].alg);
3939 if (WARN_ON(diff > 0)) {
3940 pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
3941 alg_test_descs[i - 1].alg,
3942 alg_test_descs[i].alg);
3945 if (WARN_ON(diff == 0)) {
3946 pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
3947 alg_test_descs[i].alg);
3952 static void alg_check_testvec_configs(void)
3956 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++)
3957 WARN_ON(!valid_testvec_config(
3958 &default_cipher_testvec_configs[i]));
3961 static void testmgr_onetime_init(void)
3963 alg_check_test_descs_order();
3964 alg_check_testvec_configs();
3966 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
3967 pr_warn("alg: extra crypto tests enabled. This is intended for developer use only.\n");
3971 static int alg_find_test(const char *alg)
3974 int end = ARRAY_SIZE(alg_test_descs);
3976 while (start < end) {
3977 int i = (start + end) / 2;
3978 int diff = strcmp(alg_test_descs[i].alg, alg);
3996 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
4002 if (!fips_enabled && notests) {
4003 printk_once(KERN_INFO "alg: self-tests disabled\n");
4007 DO_ONCE(testmgr_onetime_init);
4009 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
4010 char nalg[CRYPTO_MAX_ALG_NAME];
4012 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
4014 return -ENAMETOOLONG;
4016 i = alg_find_test(nalg);
4020 if (fips_enabled && !alg_test_descs[i].fips_allowed)
4023 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
4027 i = alg_find_test(alg);
4028 j = alg_find_test(driver);
4032 if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
4033 (j >= 0 && !alg_test_descs[j].fips_allowed)))
4038 rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
4040 if (j >= 0 && j != i)
4041 rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
4045 if (fips_enabled && rc)
4046 panic("%s: %s alg self test failed in fips mode!\n", driver, alg);
4048 if (fips_enabled && !rc)
4049 pr_info("alg: self-tests for %s (%s) passed\n", driver, alg);
4054 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
4060 #endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
4062 EXPORT_SYMBOL_GPL(alg_test);