1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Algorithm testing framework and tests.
5 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
6 * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
7 * Copyright (c) 2007 Nokia Siemens Networks
8 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
9 * Copyright (c) 2019 Google LLC
11 * Updated RFC4106 AES-GCM testing.
12 * Authors: Aidan O'Mahony (aidan.o.mahony@intel.com)
13 * Adrian Hoban <adrian.hoban@intel.com>
14 * Gabriele Paoloni <gabriele.paoloni@intel.com>
15 * Tadeusz Struk (tadeusz.struk@intel.com)
16 * Copyright (c) 2010, Intel Corporation.
19 #include <crypto/aead.h>
20 #include <crypto/hash.h>
21 #include <crypto/skcipher.h>
22 #include <linux/err.h>
23 #include <linux/fips.h>
24 #include <linux/module.h>
25 #include <linux/once.h>
26 #include <linux/random.h>
27 #include <linux/scatterlist.h>
28 #include <linux/slab.h>
29 #include <linux/string.h>
30 #include <linux/uio.h>
31 #include <crypto/rng.h>
32 #include <crypto/drbg.h>
33 #include <crypto/akcipher.h>
34 #include <crypto/kpp.h>
35 #include <crypto/acompress.h>
36 #include <crypto/internal/cipher.h>
37 #include <crypto/internal/simd.h>
41 MODULE_IMPORT_NS(CRYPTO_INTERNAL);
44 module_param(notests, bool, 0644);
45 MODULE_PARM_DESC(notests, "disable crypto self-tests");
47 static bool panic_on_fail;
48 module_param(panic_on_fail, bool, 0444);
50 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
51 static bool noextratests;
52 module_param(noextratests, bool, 0644);
53 MODULE_PARM_DESC(noextratests, "disable expensive crypto self-tests");
55 static unsigned int fuzz_iterations = 100;
56 module_param(fuzz_iterations, uint, 0644);
57 MODULE_PARM_DESC(fuzz_iterations, "number of fuzz test iterations");
59 DEFINE_PER_CPU(bool, crypto_simd_disabled_for_test);
60 EXPORT_PER_CPU_SYMBOL_GPL(crypto_simd_disabled_for_test);
63 #ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
66 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
76 * Need slab memory for testing (size in number of pages).
81 * Used by test_cipher()
86 struct aead_test_suite {
87 const struct aead_testvec *vecs;
91 * Set if trying to decrypt an inauthentic ciphertext with this
92 * algorithm might result in EINVAL rather than EBADMSG, due to other
93 * validation the algorithm does on the inputs such as length checks.
95 unsigned int einval_allowed : 1;
98 * Set if this algorithm requires that the IV be located at the end of
99 * the AAD buffer, in addition to being given in the normal way. The
100 * behavior when the two IV copies differ is implementation-defined.
102 unsigned int aad_iv : 1;
105 struct cipher_test_suite {
106 const struct cipher_testvec *vecs;
110 struct comp_test_suite {
112 const struct comp_testvec *vecs;
117 struct hash_test_suite {
118 const struct hash_testvec *vecs;
122 struct cprng_test_suite {
123 const struct cprng_testvec *vecs;
127 struct drbg_test_suite {
128 const struct drbg_testvec *vecs;
132 struct akcipher_test_suite {
133 const struct akcipher_testvec *vecs;
137 struct kpp_test_suite {
138 const struct kpp_testvec *vecs;
142 struct alg_test_desc {
144 const char *generic_driver;
145 int (*test)(const struct alg_test_desc *desc, const char *driver,
147 int fips_allowed; /* set if alg is allowed in fips mode */
150 struct aead_test_suite aead;
151 struct cipher_test_suite cipher;
152 struct comp_test_suite comp;
153 struct hash_test_suite hash;
154 struct cprng_test_suite cprng;
155 struct drbg_test_suite drbg;
156 struct akcipher_test_suite akcipher;
157 struct kpp_test_suite kpp;
161 static void hexdump(unsigned char *buf, unsigned int len)
163 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
168 static int __testmgr_alloc_buf(char *buf[XBUFSIZE], int order)
172 for (i = 0; i < XBUFSIZE; i++) {
173 buf[i] = (char *)__get_free_pages(GFP_KERNEL, order);
182 free_pages((unsigned long)buf[i], order);
187 static int testmgr_alloc_buf(char *buf[XBUFSIZE])
189 return __testmgr_alloc_buf(buf, 0);
192 static void __testmgr_free_buf(char *buf[XBUFSIZE], int order)
196 for (i = 0; i < XBUFSIZE; i++)
197 free_pages((unsigned long)buf[i], order);
200 static void testmgr_free_buf(char *buf[XBUFSIZE])
202 __testmgr_free_buf(buf, 0);
205 #define TESTMGR_POISON_BYTE 0xfe
206 #define TESTMGR_POISON_LEN 16
208 static inline void testmgr_poison(void *addr, size_t len)
210 memset(addr, TESTMGR_POISON_BYTE, len);
213 /* Is the memory region still fully poisoned? */
214 static inline bool testmgr_is_poison(const void *addr, size_t len)
216 return memchr_inv(addr, TESTMGR_POISON_BYTE, len) == NULL;
219 /* flush type for hash algorithms */
221 /* merge with update of previous buffer(s) */
224 /* update with previous buffer(s) before doing this one */
227 /* likewise, but also export and re-import the intermediate state */
231 /* finalization function for hash algorithms */
232 enum finalization_type {
233 FINALIZATION_TYPE_FINAL, /* use final() */
234 FINALIZATION_TYPE_FINUP, /* use finup() */
235 FINALIZATION_TYPE_DIGEST, /* use digest() */
238 #define TEST_SG_TOTAL 10000
241 * struct test_sg_division - description of a scatterlist entry
243 * This struct describes one entry of a scatterlist being constructed to check a
244 * crypto test vector.
246 * @proportion_of_total: length of this chunk relative to the total length,
247 * given as a proportion out of TEST_SG_TOTAL so that it
248 * scales to fit any test vector
249 * @offset: byte offset into a 2-page buffer at which this chunk will start
250 * @offset_relative_to_alignmask: if true, add the algorithm's alignmask to the
252 * @flush_type: for hashes, whether an update() should be done now vs.
253 * continuing to accumulate data
254 * @nosimd: if doing the pending update(), do it with SIMD disabled?
256 struct test_sg_division {
257 unsigned int proportion_of_total;
259 bool offset_relative_to_alignmask;
260 enum flush_type flush_type;
265 * struct testvec_config - configuration for testing a crypto test vector
267 * This struct describes the data layout and other parameters with which each
268 * crypto test vector can be tested.
270 * @name: name of this config, logged for debugging purposes if a test fails
271 * @inplace: operate on the data in-place, if applicable for the algorithm type?
272 * @req_flags: extra request_flags, e.g. CRYPTO_TFM_REQ_MAY_SLEEP
273 * @src_divs: description of how to arrange the source scatterlist
274 * @dst_divs: description of how to arrange the dst scatterlist, if applicable
275 * for the algorithm type. Defaults to @src_divs if unset.
276 * @iv_offset: misalignment of the IV in the range [0..MAX_ALGAPI_ALIGNMASK+1],
277 * where 0 is aligned to a 2*(MAX_ALGAPI_ALIGNMASK+1) byte boundary
278 * @iv_offset_relative_to_alignmask: if true, add the algorithm's alignmask to
280 * @key_offset: misalignment of the key, where 0 is default alignment
281 * @key_offset_relative_to_alignmask: if true, add the algorithm's alignmask to
283 * @finalization_type: what finalization function to use for hashes
284 * @nosimd: execute with SIMD disabled? Requires !CRYPTO_TFM_REQ_MAY_SLEEP.
286 struct testvec_config {
290 struct test_sg_division src_divs[XBUFSIZE];
291 struct test_sg_division dst_divs[XBUFSIZE];
292 unsigned int iv_offset;
293 unsigned int key_offset;
294 bool iv_offset_relative_to_alignmask;
295 bool key_offset_relative_to_alignmask;
296 enum finalization_type finalization_type;
300 #define TESTVEC_CONFIG_NAMELEN 192
303 * The following are the lists of testvec_configs to test for each algorithm
304 * type when the basic crypto self-tests are enabled, i.e. when
305 * CONFIG_CRYPTO_MANAGER_DISABLE_TESTS is unset. They aim to provide good test
306 * coverage, while keeping the test time much shorter than the full fuzz tests
307 * so that the basic tests can be enabled in a wider range of circumstances.
310 /* Configs for skciphers and aeads */
311 static const struct testvec_config default_cipher_testvec_configs[] = {
315 .src_divs = { { .proportion_of_total = 10000 } },
317 .name = "out-of-place",
318 .src_divs = { { .proportion_of_total = 10000 } },
320 .name = "unaligned buffer, offset=1",
321 .src_divs = { { .proportion_of_total = 10000, .offset = 1 } },
325 .name = "buffer aligned only to alignmask",
328 .proportion_of_total = 10000,
330 .offset_relative_to_alignmask = true,
334 .iv_offset_relative_to_alignmask = true,
336 .key_offset_relative_to_alignmask = true,
338 .name = "two even aligned splits",
340 { .proportion_of_total = 5000 },
341 { .proportion_of_total = 5000 },
344 .name = "uneven misaligned splits, may sleep",
345 .req_flags = CRYPTO_TFM_REQ_MAY_SLEEP,
347 { .proportion_of_total = 1900, .offset = 33 },
348 { .proportion_of_total = 3300, .offset = 7 },
349 { .proportion_of_total = 4800, .offset = 18 },
354 .name = "misaligned splits crossing pages, inplace",
358 .proportion_of_total = 7500,
359 .offset = PAGE_SIZE - 32
361 .proportion_of_total = 2500,
362 .offset = PAGE_SIZE - 7
368 static const struct testvec_config default_hash_testvec_configs[] = {
370 .name = "init+update+final aligned buffer",
371 .src_divs = { { .proportion_of_total = 10000 } },
372 .finalization_type = FINALIZATION_TYPE_FINAL,
374 .name = "init+finup aligned buffer",
375 .src_divs = { { .proportion_of_total = 10000 } },
376 .finalization_type = FINALIZATION_TYPE_FINUP,
378 .name = "digest aligned buffer",
379 .src_divs = { { .proportion_of_total = 10000 } },
380 .finalization_type = FINALIZATION_TYPE_DIGEST,
382 .name = "init+update+final misaligned buffer",
383 .src_divs = { { .proportion_of_total = 10000, .offset = 1 } },
384 .finalization_type = FINALIZATION_TYPE_FINAL,
387 .name = "digest buffer aligned only to alignmask",
390 .proportion_of_total = 10000,
392 .offset_relative_to_alignmask = true,
395 .finalization_type = FINALIZATION_TYPE_DIGEST,
397 .key_offset_relative_to_alignmask = true,
399 .name = "init+update+update+final two even splits",
401 { .proportion_of_total = 5000 },
403 .proportion_of_total = 5000,
404 .flush_type = FLUSH_TYPE_FLUSH,
407 .finalization_type = FINALIZATION_TYPE_FINAL,
409 .name = "digest uneven misaligned splits, may sleep",
410 .req_flags = CRYPTO_TFM_REQ_MAY_SLEEP,
412 { .proportion_of_total = 1900, .offset = 33 },
413 { .proportion_of_total = 3300, .offset = 7 },
414 { .proportion_of_total = 4800, .offset = 18 },
416 .finalization_type = FINALIZATION_TYPE_DIGEST,
418 .name = "digest misaligned splits crossing pages",
421 .proportion_of_total = 7500,
422 .offset = PAGE_SIZE - 32,
424 .proportion_of_total = 2500,
425 .offset = PAGE_SIZE - 7,
428 .finalization_type = FINALIZATION_TYPE_DIGEST,
430 .name = "import/export",
433 .proportion_of_total = 6500,
434 .flush_type = FLUSH_TYPE_REIMPORT,
436 .proportion_of_total = 3500,
437 .flush_type = FLUSH_TYPE_REIMPORT,
440 .finalization_type = FINALIZATION_TYPE_FINAL,
444 static unsigned int count_test_sg_divisions(const struct test_sg_division *divs)
446 unsigned int remaining = TEST_SG_TOTAL;
447 unsigned int ndivs = 0;
450 remaining -= divs[ndivs++].proportion_of_total;
456 #define SGDIVS_HAVE_FLUSHES BIT(0)
457 #define SGDIVS_HAVE_NOSIMD BIT(1)
459 static bool valid_sg_divisions(const struct test_sg_division *divs,
460 unsigned int count, int *flags_ret)
462 unsigned int total = 0;
465 for (i = 0; i < count && total != TEST_SG_TOTAL; i++) {
466 if (divs[i].proportion_of_total <= 0 ||
467 divs[i].proportion_of_total > TEST_SG_TOTAL - total)
469 total += divs[i].proportion_of_total;
470 if (divs[i].flush_type != FLUSH_TYPE_NONE)
471 *flags_ret |= SGDIVS_HAVE_FLUSHES;
473 *flags_ret |= SGDIVS_HAVE_NOSIMD;
475 return total == TEST_SG_TOTAL &&
476 memchr_inv(&divs[i], 0, (count - i) * sizeof(divs[0])) == NULL;
480 * Check whether the given testvec_config is valid. This isn't strictly needed
481 * since every testvec_config should be valid, but check anyway so that people
482 * don't unknowingly add broken configs that don't do what they wanted.
484 static bool valid_testvec_config(const struct testvec_config *cfg)
488 if (cfg->name == NULL)
491 if (!valid_sg_divisions(cfg->src_divs, ARRAY_SIZE(cfg->src_divs),
495 if (cfg->dst_divs[0].proportion_of_total) {
496 if (!valid_sg_divisions(cfg->dst_divs,
497 ARRAY_SIZE(cfg->dst_divs), &flags))
500 if (memchr_inv(cfg->dst_divs, 0, sizeof(cfg->dst_divs)))
502 /* defaults to dst_divs=src_divs */
506 (cfg->iv_offset_relative_to_alignmask ? MAX_ALGAPI_ALIGNMASK : 0) >
507 MAX_ALGAPI_ALIGNMASK + 1)
510 if ((flags & (SGDIVS_HAVE_FLUSHES | SGDIVS_HAVE_NOSIMD)) &&
511 cfg->finalization_type == FINALIZATION_TYPE_DIGEST)
514 if ((cfg->nosimd || (flags & SGDIVS_HAVE_NOSIMD)) &&
515 (cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP))
522 char *bufs[XBUFSIZE];
523 struct scatterlist sgl[XBUFSIZE];
524 struct scatterlist sgl_saved[XBUFSIZE];
525 struct scatterlist *sgl_ptr;
529 static int init_test_sglist(struct test_sglist *tsgl)
531 return __testmgr_alloc_buf(tsgl->bufs, 1 /* two pages per buffer */);
534 static void destroy_test_sglist(struct test_sglist *tsgl)
536 return __testmgr_free_buf(tsgl->bufs, 1 /* two pages per buffer */);
540 * build_test_sglist() - build a scatterlist for a crypto test
542 * @tsgl: the scatterlist to build. @tsgl->bufs[] contains an array of 2-page
543 * buffers which the scatterlist @tsgl->sgl[] will be made to point into.
544 * @divs: the layout specification on which the scatterlist will be based
545 * @alignmask: the algorithm's alignmask
546 * @total_len: the total length of the scatterlist to build in bytes
547 * @data: if non-NULL, the buffers will be filled with this data until it ends.
548 * Otherwise the buffers will be poisoned. In both cases, some bytes
549 * past the end of each buffer will be poisoned to help detect overruns.
550 * @out_divs: if non-NULL, the test_sg_division to which each scatterlist entry
551 * corresponds will be returned here. This will match @divs except
552 * that divisions resolving to a length of 0 are omitted as they are
553 * not included in the scatterlist.
555 * Return: 0 or a -errno value
557 static int build_test_sglist(struct test_sglist *tsgl,
558 const struct test_sg_division *divs,
559 const unsigned int alignmask,
560 const unsigned int total_len,
561 struct iov_iter *data,
562 const struct test_sg_division *out_divs[XBUFSIZE])
565 const struct test_sg_division *div;
567 } partitions[XBUFSIZE];
568 const unsigned int ndivs = count_test_sg_divisions(divs);
569 unsigned int len_remaining = total_len;
572 BUILD_BUG_ON(ARRAY_SIZE(partitions) != ARRAY_SIZE(tsgl->sgl));
573 if (WARN_ON(ndivs > ARRAY_SIZE(partitions)))
576 /* Calculate the (div, length) pairs */
578 for (i = 0; i < ndivs; i++) {
579 unsigned int len_this_sg =
581 (total_len * divs[i].proportion_of_total +
582 TEST_SG_TOTAL / 2) / TEST_SG_TOTAL);
584 if (len_this_sg != 0) {
585 partitions[tsgl->nents].div = &divs[i];
586 partitions[tsgl->nents].length = len_this_sg;
588 len_remaining -= len_this_sg;
591 if (tsgl->nents == 0) {
592 partitions[tsgl->nents].div = &divs[0];
593 partitions[tsgl->nents].length = 0;
596 partitions[tsgl->nents - 1].length += len_remaining;
598 /* Set up the sgl entries and fill the data or poison */
599 sg_init_table(tsgl->sgl, tsgl->nents);
600 for (i = 0; i < tsgl->nents; i++) {
601 unsigned int offset = partitions[i].div->offset;
604 if (partitions[i].div->offset_relative_to_alignmask)
607 while (offset + partitions[i].length + TESTMGR_POISON_LEN >
609 if (WARN_ON(offset <= 0))
614 addr = &tsgl->bufs[i][offset];
615 sg_set_buf(&tsgl->sgl[i], addr, partitions[i].length);
618 out_divs[i] = partitions[i].div;
621 size_t copy_len, copied;
623 copy_len = min(partitions[i].length, data->count);
624 copied = copy_from_iter(addr, copy_len, data);
625 if (WARN_ON(copied != copy_len))
627 testmgr_poison(addr + copy_len, partitions[i].length +
628 TESTMGR_POISON_LEN - copy_len);
630 testmgr_poison(addr, partitions[i].length +
635 sg_mark_end(&tsgl->sgl[tsgl->nents - 1]);
636 tsgl->sgl_ptr = tsgl->sgl;
637 memcpy(tsgl->sgl_saved, tsgl->sgl, tsgl->nents * sizeof(tsgl->sgl[0]));
642 * Verify that a scatterlist crypto operation produced the correct output.
644 * @tsgl: scatterlist containing the actual output
645 * @expected_output: buffer containing the expected output
646 * @len_to_check: length of @expected_output in bytes
647 * @unchecked_prefix_len: number of ignored bytes in @tsgl prior to real result
648 * @check_poison: verify that the poison bytes after each chunk are intact?
650 * Return: 0 if correct, -EINVAL if incorrect, -EOVERFLOW if buffer overrun.
652 static int verify_correct_output(const struct test_sglist *tsgl,
653 const char *expected_output,
654 unsigned int len_to_check,
655 unsigned int unchecked_prefix_len,
660 for (i = 0; i < tsgl->nents; i++) {
661 struct scatterlist *sg = &tsgl->sgl_ptr[i];
662 unsigned int len = sg->length;
663 unsigned int offset = sg->offset;
664 const char *actual_output;
666 if (unchecked_prefix_len) {
667 if (unchecked_prefix_len >= len) {
668 unchecked_prefix_len -= len;
671 offset += unchecked_prefix_len;
672 len -= unchecked_prefix_len;
673 unchecked_prefix_len = 0;
675 len = min(len, len_to_check);
676 actual_output = page_address(sg_page(sg)) + offset;
677 if (memcmp(expected_output, actual_output, len) != 0)
680 !testmgr_is_poison(actual_output + len, TESTMGR_POISON_LEN))
683 expected_output += len;
685 if (WARN_ON(len_to_check != 0))
690 static bool is_test_sglist_corrupted(const struct test_sglist *tsgl)
694 for (i = 0; i < tsgl->nents; i++) {
695 if (tsgl->sgl[i].page_link != tsgl->sgl_saved[i].page_link)
697 if (tsgl->sgl[i].offset != tsgl->sgl_saved[i].offset)
699 if (tsgl->sgl[i].length != tsgl->sgl_saved[i].length)
705 struct cipher_test_sglists {
706 struct test_sglist src;
707 struct test_sglist dst;
710 static struct cipher_test_sglists *alloc_cipher_test_sglists(void)
712 struct cipher_test_sglists *tsgls;
714 tsgls = kmalloc(sizeof(*tsgls), GFP_KERNEL);
718 if (init_test_sglist(&tsgls->src) != 0)
720 if (init_test_sglist(&tsgls->dst) != 0)
721 goto fail_destroy_src;
726 destroy_test_sglist(&tsgls->src);
732 static void free_cipher_test_sglists(struct cipher_test_sglists *tsgls)
735 destroy_test_sglist(&tsgls->src);
736 destroy_test_sglist(&tsgls->dst);
741 /* Build the src and dst scatterlists for an skcipher or AEAD test */
742 static int build_cipher_test_sglists(struct cipher_test_sglists *tsgls,
743 const struct testvec_config *cfg,
744 unsigned int alignmask,
745 unsigned int src_total_len,
746 unsigned int dst_total_len,
747 const struct kvec *inputs,
748 unsigned int nr_inputs)
750 struct iov_iter input;
753 iov_iter_kvec(&input, WRITE, inputs, nr_inputs, src_total_len);
754 err = build_test_sglist(&tsgls->src, cfg->src_divs, alignmask,
756 max(dst_total_len, src_total_len) :
763 tsgls->dst.sgl_ptr = tsgls->src.sgl;
764 tsgls->dst.nents = tsgls->src.nents;
767 return build_test_sglist(&tsgls->dst,
768 cfg->dst_divs[0].proportion_of_total ?
769 cfg->dst_divs : cfg->src_divs,
770 alignmask, dst_total_len, NULL, NULL);
774 * Support for testing passing a misaligned key to setkey():
776 * If cfg->key_offset is set, copy the key into a new buffer at that offset,
777 * optionally adding alignmask. Else, just use the key directly.
779 static int prepare_keybuf(const u8 *key, unsigned int ksize,
780 const struct testvec_config *cfg,
781 unsigned int alignmask,
782 const u8 **keybuf_ret, const u8 **keyptr_ret)
784 unsigned int key_offset = cfg->key_offset;
785 u8 *keybuf = NULL, *keyptr = (u8 *)key;
787 if (key_offset != 0) {
788 if (cfg->key_offset_relative_to_alignmask)
789 key_offset += alignmask;
790 keybuf = kmalloc(key_offset + ksize, GFP_KERNEL);
793 keyptr = keybuf + key_offset;
794 memcpy(keyptr, key, ksize);
796 *keybuf_ret = keybuf;
797 *keyptr_ret = keyptr;
801 /* Like setkey_f(tfm, key, ksize), but sometimes misalign the key */
802 #define do_setkey(setkey_f, tfm, key, ksize, cfg, alignmask) \
804 const u8 *keybuf, *keyptr; \
807 err = prepare_keybuf((key), (ksize), (cfg), (alignmask), \
810 err = setkey_f((tfm), keyptr, (ksize)); \
816 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
818 /* Generate a random length in range [0, max_len], but prefer smaller values */
819 static unsigned int generate_random_length(unsigned int max_len)
821 unsigned int len = prandom_u32() % (max_len + 1);
823 switch (prandom_u32() % 4) {
835 /* Flip a random bit in the given nonempty data buffer */
836 static void flip_random_bit(u8 *buf, size_t size)
840 bitpos = prandom_u32() % (size * 8);
841 buf[bitpos / 8] ^= 1 << (bitpos % 8);
844 /* Flip a random byte in the given nonempty data buffer */
845 static void flip_random_byte(u8 *buf, size_t size)
847 buf[prandom_u32() % size] ^= 0xff;
850 /* Sometimes make some random changes to the given nonempty data buffer */
851 static void mutate_buffer(u8 *buf, size_t size)
856 /* Sometimes flip some bits */
857 if (prandom_u32() % 4 == 0) {
858 num_flips = min_t(size_t, 1 << (prandom_u32() % 8), size * 8);
859 for (i = 0; i < num_flips; i++)
860 flip_random_bit(buf, size);
863 /* Sometimes flip some bytes */
864 if (prandom_u32() % 4 == 0) {
865 num_flips = min_t(size_t, 1 << (prandom_u32() % 8), size);
866 for (i = 0; i < num_flips; i++)
867 flip_random_byte(buf, size);
871 /* Randomly generate 'count' bytes, but sometimes make them "interesting" */
872 static void generate_random_bytes(u8 *buf, size_t count)
881 switch (prandom_u32() % 8) { /* Choose a generation strategy */
884 /* All the same byte, plus optional mutations */
885 switch (prandom_u32() % 4) {
893 b = (u8)prandom_u32();
896 memset(buf, b, count);
897 mutate_buffer(buf, count);
900 /* Ascending or descending bytes, plus optional mutations */
901 increment = (u8)prandom_u32();
902 b = (u8)prandom_u32();
903 for (i = 0; i < count; i++, b += increment)
905 mutate_buffer(buf, count);
908 /* Fully random bytes */
909 for (i = 0; i < count; i++)
910 buf[i] = (u8)prandom_u32();
914 static char *generate_random_sgl_divisions(struct test_sg_division *divs,
915 size_t max_divs, char *p, char *end,
916 bool gen_flushes, u32 req_flags)
918 struct test_sg_division *div = divs;
919 unsigned int remaining = TEST_SG_TOTAL;
922 unsigned int this_len;
923 const char *flushtype_str;
925 if (div == &divs[max_divs - 1] || prandom_u32() % 2 == 0)
926 this_len = remaining;
928 this_len = 1 + (prandom_u32() % remaining);
929 div->proportion_of_total = this_len;
931 if (prandom_u32() % 4 == 0)
932 div->offset = (PAGE_SIZE - 128) + (prandom_u32() % 128);
933 else if (prandom_u32() % 2 == 0)
934 div->offset = prandom_u32() % 32;
936 div->offset = prandom_u32() % PAGE_SIZE;
937 if (prandom_u32() % 8 == 0)
938 div->offset_relative_to_alignmask = true;
940 div->flush_type = FLUSH_TYPE_NONE;
942 switch (prandom_u32() % 4) {
944 div->flush_type = FLUSH_TYPE_REIMPORT;
947 div->flush_type = FLUSH_TYPE_FLUSH;
952 if (div->flush_type != FLUSH_TYPE_NONE &&
953 !(req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) &&
954 prandom_u32() % 2 == 0)
957 switch (div->flush_type) {
958 case FLUSH_TYPE_FLUSH:
960 flushtype_str = "<flush,nosimd>";
962 flushtype_str = "<flush>";
964 case FLUSH_TYPE_REIMPORT:
966 flushtype_str = "<reimport,nosimd>";
968 flushtype_str = "<reimport>";
975 BUILD_BUG_ON(TEST_SG_TOTAL != 10000); /* for "%u.%u%%" */
976 p += scnprintf(p, end - p, "%s%u.%u%%@%s+%u%s", flushtype_str,
977 this_len / 100, this_len % 100,
978 div->offset_relative_to_alignmask ?
980 div->offset, this_len == remaining ? "" : ", ");
981 remaining -= this_len;
988 /* Generate a random testvec_config for fuzz testing */
989 static void generate_random_testvec_config(struct testvec_config *cfg,
990 char *name, size_t max_namelen)
993 char * const end = name + max_namelen;
995 memset(cfg, 0, sizeof(*cfg));
999 p += scnprintf(p, end - p, "random:");
1001 if (prandom_u32() % 2 == 0) {
1002 cfg->inplace = true;
1003 p += scnprintf(p, end - p, " inplace");
1006 if (prandom_u32() % 2 == 0) {
1007 cfg->req_flags |= CRYPTO_TFM_REQ_MAY_SLEEP;
1008 p += scnprintf(p, end - p, " may_sleep");
1011 switch (prandom_u32() % 4) {
1013 cfg->finalization_type = FINALIZATION_TYPE_FINAL;
1014 p += scnprintf(p, end - p, " use_final");
1017 cfg->finalization_type = FINALIZATION_TYPE_FINUP;
1018 p += scnprintf(p, end - p, " use_finup");
1021 cfg->finalization_type = FINALIZATION_TYPE_DIGEST;
1022 p += scnprintf(p, end - p, " use_digest");
1026 if (!(cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) &&
1027 prandom_u32() % 2 == 0) {
1029 p += scnprintf(p, end - p, " nosimd");
1032 p += scnprintf(p, end - p, " src_divs=[");
1033 p = generate_random_sgl_divisions(cfg->src_divs,
1034 ARRAY_SIZE(cfg->src_divs), p, end,
1035 (cfg->finalization_type !=
1036 FINALIZATION_TYPE_DIGEST),
1038 p += scnprintf(p, end - p, "]");
1040 if (!cfg->inplace && prandom_u32() % 2 == 0) {
1041 p += scnprintf(p, end - p, " dst_divs=[");
1042 p = generate_random_sgl_divisions(cfg->dst_divs,
1043 ARRAY_SIZE(cfg->dst_divs),
1046 p += scnprintf(p, end - p, "]");
1049 if (prandom_u32() % 2 == 0) {
1050 cfg->iv_offset = 1 + (prandom_u32() % MAX_ALGAPI_ALIGNMASK);
1051 p += scnprintf(p, end - p, " iv_offset=%u", cfg->iv_offset);
1054 if (prandom_u32() % 2 == 0) {
1055 cfg->key_offset = 1 + (prandom_u32() % MAX_ALGAPI_ALIGNMASK);
1056 p += scnprintf(p, end - p, " key_offset=%u", cfg->key_offset);
1059 WARN_ON_ONCE(!valid_testvec_config(cfg));
1062 static void crypto_disable_simd_for_test(void)
1065 __this_cpu_write(crypto_simd_disabled_for_test, true);
1068 static void crypto_reenable_simd_for_test(void)
1070 __this_cpu_write(crypto_simd_disabled_for_test, false);
1075 * Given an algorithm name, build the name of the generic implementation of that
1076 * algorithm, assuming the usual naming convention. Specifically, this appends
1077 * "-generic" to every part of the name that is not a template name. Examples:
1079 * aes => aes-generic
1080 * cbc(aes) => cbc(aes-generic)
1081 * cts(cbc(aes)) => cts(cbc(aes-generic))
1082 * rfc7539(chacha20,poly1305) => rfc7539(chacha20-generic,poly1305-generic)
1084 * Return: 0 on success, or -ENAMETOOLONG if the generic name would be too long
1086 static int build_generic_driver_name(const char *algname,
1087 char driver_name[CRYPTO_MAX_ALG_NAME])
1089 const char *in = algname;
1090 char *out = driver_name;
1091 size_t len = strlen(algname);
1093 if (len >= CRYPTO_MAX_ALG_NAME)
1096 const char *in_saved = in;
1098 while (*in && *in != '(' && *in != ')' && *in != ',')
1100 if (*in != '(' && in > in_saved) {
1102 if (len >= CRYPTO_MAX_ALG_NAME)
1104 memcpy(out, "-generic", 8);
1107 } while ((*out++ = *in++) != '\0');
1111 pr_err("alg: generic driver name for \"%s\" would be too long\n",
1113 return -ENAMETOOLONG;
1115 #else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1116 static void crypto_disable_simd_for_test(void)
1120 static void crypto_reenable_simd_for_test(void)
1123 #endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1125 static int build_hash_sglist(struct test_sglist *tsgl,
1126 const struct hash_testvec *vec,
1127 const struct testvec_config *cfg,
1128 unsigned int alignmask,
1129 const struct test_sg_division *divs[XBUFSIZE])
1132 struct iov_iter input;
1134 kv.iov_base = (void *)vec->plaintext;
1135 kv.iov_len = vec->psize;
1136 iov_iter_kvec(&input, WRITE, &kv, 1, vec->psize);
1137 return build_test_sglist(tsgl, cfg->src_divs, alignmask, vec->psize,
1141 static int check_hash_result(const char *type,
1142 const u8 *result, unsigned int digestsize,
1143 const struct hash_testvec *vec,
1144 const char *vec_name,
1146 const struct testvec_config *cfg)
1148 if (memcmp(result, vec->digest, digestsize) != 0) {
1149 pr_err("alg: %s: %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
1150 type, driver, vec_name, cfg->name);
1153 if (!testmgr_is_poison(&result[digestsize], TESTMGR_POISON_LEN)) {
1154 pr_err("alg: %s: %s overran result buffer on test vector %s, cfg=\"%s\"\n",
1155 type, driver, vec_name, cfg->name);
1161 static inline int check_shash_op(const char *op, int err,
1162 const char *driver, const char *vec_name,
1163 const struct testvec_config *cfg)
1166 pr_err("alg: shash: %s %s() failed with err %d on test vector %s, cfg=\"%s\"\n",
1167 driver, op, err, vec_name, cfg->name);
1171 static inline const void *sg_data(struct scatterlist *sg)
1173 return page_address(sg_page(sg)) + sg->offset;
1176 /* Test one hash test vector in one configuration, using the shash API */
1177 static int test_shash_vec_cfg(const struct hash_testvec *vec,
1178 const char *vec_name,
1179 const struct testvec_config *cfg,
1180 struct shash_desc *desc,
1181 struct test_sglist *tsgl,
1184 struct crypto_shash *tfm = desc->tfm;
1185 const unsigned int alignmask = crypto_shash_alignmask(tfm);
1186 const unsigned int digestsize = crypto_shash_digestsize(tfm);
1187 const unsigned int statesize = crypto_shash_statesize(tfm);
1188 const char *driver = crypto_shash_driver_name(tfm);
1189 const struct test_sg_division *divs[XBUFSIZE];
1191 u8 result[HASH_MAX_DIGESTSIZE + TESTMGR_POISON_LEN];
1194 /* Set the key, if specified */
1196 err = do_setkey(crypto_shash_setkey, tfm, vec->key, vec->ksize,
1199 if (err == vec->setkey_error)
1201 pr_err("alg: shash: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1202 driver, vec_name, vec->setkey_error, err,
1203 crypto_shash_get_flags(tfm));
1206 if (vec->setkey_error) {
1207 pr_err("alg: shash: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1208 driver, vec_name, vec->setkey_error);
1213 /* Build the scatterlist for the source data */
1214 err = build_hash_sglist(tsgl, vec, cfg, alignmask, divs);
1216 pr_err("alg: shash: %s: error preparing scatterlist for test vector %s, cfg=\"%s\"\n",
1217 driver, vec_name, cfg->name);
1221 /* Do the actual hashing */
1223 testmgr_poison(desc->__ctx, crypto_shash_descsize(tfm));
1224 testmgr_poison(result, digestsize + TESTMGR_POISON_LEN);
1226 if (cfg->finalization_type == FINALIZATION_TYPE_DIGEST ||
1227 vec->digest_error) {
1228 /* Just using digest() */
1229 if (tsgl->nents != 1)
1232 crypto_disable_simd_for_test();
1233 err = crypto_shash_digest(desc, sg_data(&tsgl->sgl[0]),
1234 tsgl->sgl[0].length, result);
1236 crypto_reenable_simd_for_test();
1238 if (err == vec->digest_error)
1240 pr_err("alg: shash: %s digest() failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
1241 driver, vec_name, vec->digest_error, err,
1245 if (vec->digest_error) {
1246 pr_err("alg: shash: %s digest() unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
1247 driver, vec_name, vec->digest_error, cfg->name);
1253 /* Using init(), zero or more update(), then final() or finup() */
1256 crypto_disable_simd_for_test();
1257 err = crypto_shash_init(desc);
1259 crypto_reenable_simd_for_test();
1260 err = check_shash_op("init", err, driver, vec_name, cfg);
1264 for (i = 0; i < tsgl->nents; i++) {
1265 if (i + 1 == tsgl->nents &&
1266 cfg->finalization_type == FINALIZATION_TYPE_FINUP) {
1267 if (divs[i]->nosimd)
1268 crypto_disable_simd_for_test();
1269 err = crypto_shash_finup(desc, sg_data(&tsgl->sgl[i]),
1270 tsgl->sgl[i].length, result);
1271 if (divs[i]->nosimd)
1272 crypto_reenable_simd_for_test();
1273 err = check_shash_op("finup", err, driver, vec_name,
1279 if (divs[i]->nosimd)
1280 crypto_disable_simd_for_test();
1281 err = crypto_shash_update(desc, sg_data(&tsgl->sgl[i]),
1282 tsgl->sgl[i].length);
1283 if (divs[i]->nosimd)
1284 crypto_reenable_simd_for_test();
1285 err = check_shash_op("update", err, driver, vec_name, cfg);
1288 if (divs[i]->flush_type == FLUSH_TYPE_REIMPORT) {
1289 /* Test ->export() and ->import() */
1290 testmgr_poison(hashstate + statesize,
1291 TESTMGR_POISON_LEN);
1292 err = crypto_shash_export(desc, hashstate);
1293 err = check_shash_op("export", err, driver, vec_name,
1297 if (!testmgr_is_poison(hashstate + statesize,
1298 TESTMGR_POISON_LEN)) {
1299 pr_err("alg: shash: %s export() overran state buffer on test vector %s, cfg=\"%s\"\n",
1300 driver, vec_name, cfg->name);
1303 testmgr_poison(desc->__ctx, crypto_shash_descsize(tfm));
1304 err = crypto_shash_import(desc, hashstate);
1305 err = check_shash_op("import", err, driver, vec_name,
1313 crypto_disable_simd_for_test();
1314 err = crypto_shash_final(desc, result);
1316 crypto_reenable_simd_for_test();
1317 err = check_shash_op("final", err, driver, vec_name, cfg);
1321 return check_hash_result("shash", result, digestsize, vec, vec_name,
1325 static int do_ahash_op(int (*op)(struct ahash_request *req),
1326 struct ahash_request *req,
1327 struct crypto_wait *wait, bool nosimd)
1332 crypto_disable_simd_for_test();
1337 crypto_reenable_simd_for_test();
1339 return crypto_wait_req(err, wait);
1342 static int check_nonfinal_ahash_op(const char *op, int err,
1343 u8 *result, unsigned int digestsize,
1344 const char *driver, const char *vec_name,
1345 const struct testvec_config *cfg)
1348 pr_err("alg: ahash: %s %s() failed with err %d on test vector %s, cfg=\"%s\"\n",
1349 driver, op, err, vec_name, cfg->name);
1352 if (!testmgr_is_poison(result, digestsize)) {
1353 pr_err("alg: ahash: %s %s() used result buffer on test vector %s, cfg=\"%s\"\n",
1354 driver, op, vec_name, cfg->name);
1360 /* Test one hash test vector in one configuration, using the ahash API */
1361 static int test_ahash_vec_cfg(const struct hash_testvec *vec,
1362 const char *vec_name,
1363 const struct testvec_config *cfg,
1364 struct ahash_request *req,
1365 struct test_sglist *tsgl,
1368 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1369 const unsigned int alignmask = crypto_ahash_alignmask(tfm);
1370 const unsigned int digestsize = crypto_ahash_digestsize(tfm);
1371 const unsigned int statesize = crypto_ahash_statesize(tfm);
1372 const char *driver = crypto_ahash_driver_name(tfm);
1373 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1374 const struct test_sg_division *divs[XBUFSIZE];
1375 DECLARE_CRYPTO_WAIT(wait);
1377 struct scatterlist *pending_sgl;
1378 unsigned int pending_len;
1379 u8 result[HASH_MAX_DIGESTSIZE + TESTMGR_POISON_LEN];
1382 /* Set the key, if specified */
1384 err = do_setkey(crypto_ahash_setkey, tfm, vec->key, vec->ksize,
1387 if (err == vec->setkey_error)
1389 pr_err("alg: ahash: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1390 driver, vec_name, vec->setkey_error, err,
1391 crypto_ahash_get_flags(tfm));
1394 if (vec->setkey_error) {
1395 pr_err("alg: ahash: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1396 driver, vec_name, vec->setkey_error);
1401 /* Build the scatterlist for the source data */
1402 err = build_hash_sglist(tsgl, vec, cfg, alignmask, divs);
1404 pr_err("alg: ahash: %s: error preparing scatterlist for test vector %s, cfg=\"%s\"\n",
1405 driver, vec_name, cfg->name);
1409 /* Do the actual hashing */
1411 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
1412 testmgr_poison(result, digestsize + TESTMGR_POISON_LEN);
1414 if (cfg->finalization_type == FINALIZATION_TYPE_DIGEST ||
1415 vec->digest_error) {
1416 /* Just using digest() */
1417 ahash_request_set_callback(req, req_flags, crypto_req_done,
1419 ahash_request_set_crypt(req, tsgl->sgl, result, vec->psize);
1420 err = do_ahash_op(crypto_ahash_digest, req, &wait, cfg->nosimd);
1422 if (err == vec->digest_error)
1424 pr_err("alg: ahash: %s digest() failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
1425 driver, vec_name, vec->digest_error, err,
1429 if (vec->digest_error) {
1430 pr_err("alg: ahash: %s digest() unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
1431 driver, vec_name, vec->digest_error, cfg->name);
1437 /* Using init(), zero or more update(), then final() or finup() */
1439 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
1440 ahash_request_set_crypt(req, NULL, result, 0);
1441 err = do_ahash_op(crypto_ahash_init, req, &wait, cfg->nosimd);
1442 err = check_nonfinal_ahash_op("init", err, result, digestsize,
1443 driver, vec_name, cfg);
1449 for (i = 0; i < tsgl->nents; i++) {
1450 if (divs[i]->flush_type != FLUSH_TYPE_NONE &&
1451 pending_sgl != NULL) {
1452 /* update() with the pending data */
1453 ahash_request_set_callback(req, req_flags,
1454 crypto_req_done, &wait);
1455 ahash_request_set_crypt(req, pending_sgl, result,
1457 err = do_ahash_op(crypto_ahash_update, req, &wait,
1459 err = check_nonfinal_ahash_op("update", err,
1461 driver, vec_name, cfg);
1467 if (divs[i]->flush_type == FLUSH_TYPE_REIMPORT) {
1468 /* Test ->export() and ->import() */
1469 testmgr_poison(hashstate + statesize,
1470 TESTMGR_POISON_LEN);
1471 err = crypto_ahash_export(req, hashstate);
1472 err = check_nonfinal_ahash_op("export", err,
1474 driver, vec_name, cfg);
1477 if (!testmgr_is_poison(hashstate + statesize,
1478 TESTMGR_POISON_LEN)) {
1479 pr_err("alg: ahash: %s export() overran state buffer on test vector %s, cfg=\"%s\"\n",
1480 driver, vec_name, cfg->name);
1484 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
1485 err = crypto_ahash_import(req, hashstate);
1486 err = check_nonfinal_ahash_op("import", err,
1488 driver, vec_name, cfg);
1492 if (pending_sgl == NULL)
1493 pending_sgl = &tsgl->sgl[i];
1494 pending_len += tsgl->sgl[i].length;
1497 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
1498 ahash_request_set_crypt(req, pending_sgl, result, pending_len);
1499 if (cfg->finalization_type == FINALIZATION_TYPE_FINAL) {
1500 /* finish with update() and final() */
1501 err = do_ahash_op(crypto_ahash_update, req, &wait, cfg->nosimd);
1502 err = check_nonfinal_ahash_op("update", err, result, digestsize,
1503 driver, vec_name, cfg);
1506 err = do_ahash_op(crypto_ahash_final, req, &wait, cfg->nosimd);
1508 pr_err("alg: ahash: %s final() failed with err %d on test vector %s, cfg=\"%s\"\n",
1509 driver, err, vec_name, cfg->name);
1513 /* finish with finup() */
1514 err = do_ahash_op(crypto_ahash_finup, req, &wait, cfg->nosimd);
1516 pr_err("alg: ahash: %s finup() failed with err %d on test vector %s, cfg=\"%s\"\n",
1517 driver, err, vec_name, cfg->name);
1523 return check_hash_result("ahash", result, digestsize, vec, vec_name,
1527 static int test_hash_vec_cfg(const struct hash_testvec *vec,
1528 const char *vec_name,
1529 const struct testvec_config *cfg,
1530 struct ahash_request *req,
1531 struct shash_desc *desc,
1532 struct test_sglist *tsgl,
1538 * For algorithms implemented as "shash", most bugs will be detected by
1539 * both the shash and ahash tests. Test the shash API first so that the
1540 * failures involve less indirection, so are easier to debug.
1544 err = test_shash_vec_cfg(vec, vec_name, cfg, desc, tsgl,
1550 return test_ahash_vec_cfg(vec, vec_name, cfg, req, tsgl, hashstate);
1553 static int test_hash_vec(const struct hash_testvec *vec, unsigned int vec_num,
1554 struct ahash_request *req, struct shash_desc *desc,
1555 struct test_sglist *tsgl, u8 *hashstate)
1561 sprintf(vec_name, "%u", vec_num);
1563 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++) {
1564 err = test_hash_vec_cfg(vec, vec_name,
1565 &default_hash_testvec_configs[i],
1566 req, desc, tsgl, hashstate);
1571 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1572 if (!noextratests) {
1573 struct testvec_config cfg;
1574 char cfgname[TESTVEC_CONFIG_NAMELEN];
1576 for (i = 0; i < fuzz_iterations; i++) {
1577 generate_random_testvec_config(&cfg, cfgname,
1579 err = test_hash_vec_cfg(vec, vec_name, &cfg,
1580 req, desc, tsgl, hashstate);
1590 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1592 * Generate a hash test vector from the given implementation.
1593 * Assumes the buffers in 'vec' were already allocated.
1595 static void generate_random_hash_testvec(struct shash_desc *desc,
1596 struct hash_testvec *vec,
1597 unsigned int maxkeysize,
1598 unsigned int maxdatasize,
1599 char *name, size_t max_namelen)
1602 vec->psize = generate_random_length(maxdatasize);
1603 generate_random_bytes((u8 *)vec->plaintext, vec->psize);
1606 * Key: length in range [1, maxkeysize], but usually choose maxkeysize.
1607 * If algorithm is unkeyed, then maxkeysize == 0 and set ksize = 0.
1609 vec->setkey_error = 0;
1612 vec->ksize = maxkeysize;
1613 if (prandom_u32() % 4 == 0)
1614 vec->ksize = 1 + (prandom_u32() % maxkeysize);
1615 generate_random_bytes((u8 *)vec->key, vec->ksize);
1617 vec->setkey_error = crypto_shash_setkey(desc->tfm, vec->key,
1619 /* If the key couldn't be set, no need to continue to digest. */
1620 if (vec->setkey_error)
1625 vec->digest_error = crypto_shash_digest(desc, vec->plaintext,
1626 vec->psize, (u8 *)vec->digest);
1628 snprintf(name, max_namelen, "\"random: psize=%u ksize=%u\"",
1629 vec->psize, vec->ksize);
1633 * Test the hash algorithm represented by @req against the corresponding generic
1634 * implementation, if one is available.
1636 static int test_hash_vs_generic_impl(const char *generic_driver,
1637 unsigned int maxkeysize,
1638 struct ahash_request *req,
1639 struct shash_desc *desc,
1640 struct test_sglist *tsgl,
1643 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1644 const unsigned int digestsize = crypto_ahash_digestsize(tfm);
1645 const unsigned int blocksize = crypto_ahash_blocksize(tfm);
1646 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
1647 const char *algname = crypto_hash_alg_common(tfm)->base.cra_name;
1648 const char *driver = crypto_ahash_driver_name(tfm);
1649 char _generic_driver[CRYPTO_MAX_ALG_NAME];
1650 struct crypto_shash *generic_tfm = NULL;
1651 struct shash_desc *generic_desc = NULL;
1653 struct hash_testvec vec = { 0 };
1655 struct testvec_config *cfg;
1656 char cfgname[TESTVEC_CONFIG_NAMELEN];
1662 if (!generic_driver) { /* Use default naming convention? */
1663 err = build_generic_driver_name(algname, _generic_driver);
1666 generic_driver = _generic_driver;
1669 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
1672 generic_tfm = crypto_alloc_shash(generic_driver, 0, 0);
1673 if (IS_ERR(generic_tfm)) {
1674 err = PTR_ERR(generic_tfm);
1675 if (err == -ENOENT) {
1676 pr_warn("alg: hash: skipping comparison tests for %s because %s is unavailable\n",
1677 driver, generic_driver);
1680 pr_err("alg: hash: error allocating %s (generic impl of %s): %d\n",
1681 generic_driver, algname, err);
1685 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1691 generic_desc = kzalloc(sizeof(*desc) +
1692 crypto_shash_descsize(generic_tfm), GFP_KERNEL);
1693 if (!generic_desc) {
1697 generic_desc->tfm = generic_tfm;
1699 /* Check the algorithm properties for consistency. */
1701 if (digestsize != crypto_shash_digestsize(generic_tfm)) {
1702 pr_err("alg: hash: digestsize for %s (%u) doesn't match generic impl (%u)\n",
1704 crypto_shash_digestsize(generic_tfm));
1709 if (blocksize != crypto_shash_blocksize(generic_tfm)) {
1710 pr_err("alg: hash: blocksize for %s (%u) doesn't match generic impl (%u)\n",
1711 driver, blocksize, crypto_shash_blocksize(generic_tfm));
1717 * Now generate test vectors using the generic implementation, and test
1718 * the other implementation against them.
1721 vec.key = kmalloc(maxkeysize, GFP_KERNEL);
1722 vec.plaintext = kmalloc(maxdatasize, GFP_KERNEL);
1723 vec.digest = kmalloc(digestsize, GFP_KERNEL);
1724 if (!vec.key || !vec.plaintext || !vec.digest) {
1729 for (i = 0; i < fuzz_iterations * 8; i++) {
1730 generate_random_hash_testvec(generic_desc, &vec,
1731 maxkeysize, maxdatasize,
1732 vec_name, sizeof(vec_name));
1733 generate_random_testvec_config(cfg, cfgname, sizeof(cfgname));
1735 err = test_hash_vec_cfg(&vec, vec_name, cfg,
1736 req, desc, tsgl, hashstate);
1745 kfree(vec.plaintext);
1747 crypto_free_shash(generic_tfm);
1748 kfree_sensitive(generic_desc);
1751 #else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1752 static int test_hash_vs_generic_impl(const char *generic_driver,
1753 unsigned int maxkeysize,
1754 struct ahash_request *req,
1755 struct shash_desc *desc,
1756 struct test_sglist *tsgl,
1761 #endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1763 static int alloc_shash(const char *driver, u32 type, u32 mask,
1764 struct crypto_shash **tfm_ret,
1765 struct shash_desc **desc_ret)
1767 struct crypto_shash *tfm;
1768 struct shash_desc *desc;
1770 tfm = crypto_alloc_shash(driver, type, mask);
1772 if (PTR_ERR(tfm) == -ENOENT) {
1774 * This algorithm is only available through the ahash
1775 * API, not the shash API, so skip the shash tests.
1779 pr_err("alg: hash: failed to allocate shash transform for %s: %ld\n",
1780 driver, PTR_ERR(tfm));
1781 return PTR_ERR(tfm);
1784 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL);
1786 crypto_free_shash(tfm);
1796 static int __alg_test_hash(const struct hash_testvec *vecs,
1797 unsigned int num_vecs, const char *driver,
1799 const char *generic_driver, unsigned int maxkeysize)
1801 struct crypto_ahash *atfm = NULL;
1802 struct ahash_request *req = NULL;
1803 struct crypto_shash *stfm = NULL;
1804 struct shash_desc *desc = NULL;
1805 struct test_sglist *tsgl = NULL;
1806 u8 *hashstate = NULL;
1807 unsigned int statesize;
1812 * Always test the ahash API. This works regardless of whether the
1813 * algorithm is implemented as ahash or shash.
1816 atfm = crypto_alloc_ahash(driver, type, mask);
1818 pr_err("alg: hash: failed to allocate transform for %s: %ld\n",
1819 driver, PTR_ERR(atfm));
1820 return PTR_ERR(atfm);
1822 driver = crypto_ahash_driver_name(atfm);
1824 req = ahash_request_alloc(atfm, GFP_KERNEL);
1826 pr_err("alg: hash: failed to allocate request for %s\n",
1833 * If available also test the shash API, to cover corner cases that may
1834 * be missed by testing the ahash API only.
1836 err = alloc_shash(driver, type, mask, &stfm, &desc);
1840 tsgl = kmalloc(sizeof(*tsgl), GFP_KERNEL);
1841 if (!tsgl || init_test_sglist(tsgl) != 0) {
1842 pr_err("alg: hash: failed to allocate test buffers for %s\n",
1850 statesize = crypto_ahash_statesize(atfm);
1852 statesize = max(statesize, crypto_shash_statesize(stfm));
1853 hashstate = kmalloc(statesize + TESTMGR_POISON_LEN, GFP_KERNEL);
1855 pr_err("alg: hash: failed to allocate hash state buffer for %s\n",
1861 for (i = 0; i < num_vecs; i++) {
1862 err = test_hash_vec(&vecs[i], i, req, desc, tsgl, hashstate);
1867 err = test_hash_vs_generic_impl(generic_driver, maxkeysize, req,
1868 desc, tsgl, hashstate);
1872 destroy_test_sglist(tsgl);
1876 crypto_free_shash(stfm);
1877 ahash_request_free(req);
1878 crypto_free_ahash(atfm);
1882 static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1885 const struct hash_testvec *template = desc->suite.hash.vecs;
1886 unsigned int tcount = desc->suite.hash.count;
1887 unsigned int nr_unkeyed, nr_keyed;
1888 unsigned int maxkeysize = 0;
1892 * For OPTIONAL_KEY algorithms, we have to do all the unkeyed tests
1893 * first, before setting a key on the tfm. To make this easier, we
1894 * require that the unkeyed test vectors (if any) are listed first.
1897 for (nr_unkeyed = 0; nr_unkeyed < tcount; nr_unkeyed++) {
1898 if (template[nr_unkeyed].ksize)
1901 for (nr_keyed = 0; nr_unkeyed + nr_keyed < tcount; nr_keyed++) {
1902 if (!template[nr_unkeyed + nr_keyed].ksize) {
1903 pr_err("alg: hash: test vectors for %s out of order, "
1904 "unkeyed ones must come first\n", desc->alg);
1907 maxkeysize = max_t(unsigned int, maxkeysize,
1908 template[nr_unkeyed + nr_keyed].ksize);
1913 err = __alg_test_hash(template, nr_unkeyed, driver, type, mask,
1914 desc->generic_driver, maxkeysize);
1915 template += nr_unkeyed;
1918 if (!err && nr_keyed)
1919 err = __alg_test_hash(template, nr_keyed, driver, type, mask,
1920 desc->generic_driver, maxkeysize);
1925 static int test_aead_vec_cfg(int enc, const struct aead_testvec *vec,
1926 const char *vec_name,
1927 const struct testvec_config *cfg,
1928 struct aead_request *req,
1929 struct cipher_test_sglists *tsgls)
1931 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1932 const unsigned int alignmask = crypto_aead_alignmask(tfm);
1933 const unsigned int ivsize = crypto_aead_ivsize(tfm);
1934 const unsigned int authsize = vec->clen - vec->plen;
1935 const char *driver = crypto_aead_driver_name(tfm);
1936 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1937 const char *op = enc ? "encryption" : "decryption";
1938 DECLARE_CRYPTO_WAIT(wait);
1939 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
1940 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
1942 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
1943 struct kvec input[2];
1948 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1950 crypto_aead_clear_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1952 err = do_setkey(crypto_aead_setkey, tfm, vec->key, vec->klen,
1954 if (err && err != vec->setkey_error) {
1955 pr_err("alg: aead: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1956 driver, vec_name, vec->setkey_error, err,
1957 crypto_aead_get_flags(tfm));
1960 if (!err && vec->setkey_error) {
1961 pr_err("alg: aead: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1962 driver, vec_name, vec->setkey_error);
1966 /* Set the authentication tag size */
1967 err = crypto_aead_setauthsize(tfm, authsize);
1968 if (err && err != vec->setauthsize_error) {
1969 pr_err("alg: aead: %s setauthsize failed on test vector %s; expected_error=%d, actual_error=%d\n",
1970 driver, vec_name, vec->setauthsize_error, err);
1973 if (!err && vec->setauthsize_error) {
1974 pr_err("alg: aead: %s setauthsize unexpectedly succeeded on test vector %s; expected_error=%d\n",
1975 driver, vec_name, vec->setauthsize_error);
1979 if (vec->setkey_error || vec->setauthsize_error)
1982 /* The IV must be copied to a buffer, as the algorithm may modify it */
1983 if (WARN_ON(ivsize > MAX_IVLEN))
1986 memcpy(iv, vec->iv, ivsize);
1988 memset(iv, 0, ivsize);
1990 /* Build the src/dst scatterlists */
1991 input[0].iov_base = (void *)vec->assoc;
1992 input[0].iov_len = vec->alen;
1993 input[1].iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
1994 input[1].iov_len = enc ? vec->plen : vec->clen;
1995 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
1996 vec->alen + (enc ? vec->plen :
1998 vec->alen + (enc ? vec->clen :
2002 pr_err("alg: aead: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
2003 driver, op, vec_name, cfg->name);
2007 /* Do the actual encryption or decryption */
2008 testmgr_poison(req->__ctx, crypto_aead_reqsize(tfm));
2009 aead_request_set_callback(req, req_flags, crypto_req_done, &wait);
2010 aead_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
2011 enc ? vec->plen : vec->clen, iv);
2012 aead_request_set_ad(req, vec->alen);
2014 crypto_disable_simd_for_test();
2015 err = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
2017 crypto_reenable_simd_for_test();
2018 err = crypto_wait_req(err, &wait);
2020 /* Check that the algorithm didn't overwrite things it shouldn't have */
2021 if (req->cryptlen != (enc ? vec->plen : vec->clen) ||
2022 req->assoclen != vec->alen ||
2024 req->src != tsgls->src.sgl_ptr ||
2025 req->dst != tsgls->dst.sgl_ptr ||
2026 crypto_aead_reqtfm(req) != tfm ||
2027 req->base.complete != crypto_req_done ||
2028 req->base.flags != req_flags ||
2029 req->base.data != &wait) {
2030 pr_err("alg: aead: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
2031 driver, op, vec_name, cfg->name);
2032 if (req->cryptlen != (enc ? vec->plen : vec->clen))
2033 pr_err("alg: aead: changed 'req->cryptlen'\n");
2034 if (req->assoclen != vec->alen)
2035 pr_err("alg: aead: changed 'req->assoclen'\n");
2037 pr_err("alg: aead: changed 'req->iv'\n");
2038 if (req->src != tsgls->src.sgl_ptr)
2039 pr_err("alg: aead: changed 'req->src'\n");
2040 if (req->dst != tsgls->dst.sgl_ptr)
2041 pr_err("alg: aead: changed 'req->dst'\n");
2042 if (crypto_aead_reqtfm(req) != tfm)
2043 pr_err("alg: aead: changed 'req->base.tfm'\n");
2044 if (req->base.complete != crypto_req_done)
2045 pr_err("alg: aead: changed 'req->base.complete'\n");
2046 if (req->base.flags != req_flags)
2047 pr_err("alg: aead: changed 'req->base.flags'\n");
2048 if (req->base.data != &wait)
2049 pr_err("alg: aead: changed 'req->base.data'\n");
2052 if (is_test_sglist_corrupted(&tsgls->src)) {
2053 pr_err("alg: aead: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
2054 driver, op, vec_name, cfg->name);
2057 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
2058 is_test_sglist_corrupted(&tsgls->dst)) {
2059 pr_err("alg: aead: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
2060 driver, op, vec_name, cfg->name);
2064 /* Check for unexpected success or failure, or wrong error code */
2065 if ((err == 0 && vec->novrfy) ||
2066 (err != vec->crypt_error && !(err == -EBADMSG && vec->novrfy))) {
2067 char expected_error[32];
2070 vec->crypt_error != 0 && vec->crypt_error != -EBADMSG)
2071 sprintf(expected_error, "-EBADMSG or %d",
2073 else if (vec->novrfy)
2074 sprintf(expected_error, "-EBADMSG");
2076 sprintf(expected_error, "%d", vec->crypt_error);
2078 pr_err("alg: aead: %s %s failed on test vector %s; expected_error=%s, actual_error=%d, cfg=\"%s\"\n",
2079 driver, op, vec_name, expected_error, err,
2083 pr_err("alg: aead: %s %s unexpectedly succeeded on test vector %s; expected_error=%s, cfg=\"%s\"\n",
2084 driver, op, vec_name, expected_error, cfg->name);
2087 if (err) /* Expectedly failed. */
2090 /* Check for the correct output (ciphertext or plaintext) */
2091 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
2092 enc ? vec->clen : vec->plen,
2093 vec->alen, enc || !cfg->inplace);
2094 if (err == -EOVERFLOW) {
2095 pr_err("alg: aead: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
2096 driver, op, vec_name, cfg->name);
2100 pr_err("alg: aead: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
2101 driver, op, vec_name, cfg->name);
2108 static int test_aead_vec(int enc, const struct aead_testvec *vec,
2109 unsigned int vec_num, struct aead_request *req,
2110 struct cipher_test_sglists *tsgls)
2116 if (enc && vec->novrfy)
2119 sprintf(vec_name, "%u", vec_num);
2121 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
2122 err = test_aead_vec_cfg(enc, vec, vec_name,
2123 &default_cipher_testvec_configs[i],
2129 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2130 if (!noextratests) {
2131 struct testvec_config cfg;
2132 char cfgname[TESTVEC_CONFIG_NAMELEN];
2134 for (i = 0; i < fuzz_iterations; i++) {
2135 generate_random_testvec_config(&cfg, cfgname,
2137 err = test_aead_vec_cfg(enc, vec, vec_name,
2148 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2150 struct aead_extra_tests_ctx {
2151 struct aead_request *req;
2152 struct crypto_aead *tfm;
2153 const struct alg_test_desc *test_desc;
2154 struct cipher_test_sglists *tsgls;
2155 unsigned int maxdatasize;
2156 unsigned int maxkeysize;
2158 struct aead_testvec vec;
2160 char cfgname[TESTVEC_CONFIG_NAMELEN];
2161 struct testvec_config cfg;
2165 * Make at least one random change to a (ciphertext, AAD) pair. "Ciphertext"
2166 * here means the full ciphertext including the authentication tag. The
2167 * authentication tag (and hence also the ciphertext) is assumed to be nonempty.
2169 static void mutate_aead_message(struct aead_testvec *vec, bool aad_iv,
2170 unsigned int ivsize)
2172 const unsigned int aad_tail_size = aad_iv ? ivsize : 0;
2173 const unsigned int authsize = vec->clen - vec->plen;
2175 if (prandom_u32() % 2 == 0 && vec->alen > aad_tail_size) {
2176 /* Mutate the AAD */
2177 flip_random_bit((u8 *)vec->assoc, vec->alen - aad_tail_size);
2178 if (prandom_u32() % 2 == 0)
2181 if (prandom_u32() % 2 == 0) {
2182 /* Mutate auth tag (assuming it's at the end of ciphertext) */
2183 flip_random_bit((u8 *)vec->ctext + vec->plen, authsize);
2185 /* Mutate any part of the ciphertext */
2186 flip_random_bit((u8 *)vec->ctext, vec->clen);
2191 * Minimum authentication tag size in bytes at which we assume that we can
2192 * reliably generate inauthentic messages, i.e. not generate an authentic
2193 * message by chance.
2195 #define MIN_COLLISION_FREE_AUTHSIZE 8
2197 static void generate_aead_message(struct aead_request *req,
2198 const struct aead_test_suite *suite,
2199 struct aead_testvec *vec,
2200 bool prefer_inauthentic)
2202 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2203 const unsigned int ivsize = crypto_aead_ivsize(tfm);
2204 const unsigned int authsize = vec->clen - vec->plen;
2205 const bool inauthentic = (authsize >= MIN_COLLISION_FREE_AUTHSIZE) &&
2206 (prefer_inauthentic || prandom_u32() % 4 == 0);
2208 /* Generate the AAD. */
2209 generate_random_bytes((u8 *)vec->assoc, vec->alen);
2210 if (suite->aad_iv && vec->alen >= ivsize)
2211 /* Avoid implementation-defined behavior. */
2212 memcpy((u8 *)vec->assoc + vec->alen - ivsize, vec->iv, ivsize);
2214 if (inauthentic && prandom_u32() % 2 == 0) {
2215 /* Generate a random ciphertext. */
2216 generate_random_bytes((u8 *)vec->ctext, vec->clen);
2219 struct scatterlist src[2], dst;
2221 DECLARE_CRYPTO_WAIT(wait);
2223 /* Generate a random plaintext and encrypt it. */
2224 sg_init_table(src, 2);
2226 sg_set_buf(&src[i++], vec->assoc, vec->alen);
2228 generate_random_bytes((u8 *)vec->ptext, vec->plen);
2229 sg_set_buf(&src[i++], vec->ptext, vec->plen);
2231 sg_init_one(&dst, vec->ctext, vec->alen + vec->clen);
2232 memcpy(iv, vec->iv, ivsize);
2233 aead_request_set_callback(req, 0, crypto_req_done, &wait);
2234 aead_request_set_crypt(req, src, &dst, vec->plen, iv);
2235 aead_request_set_ad(req, vec->alen);
2236 vec->crypt_error = crypto_wait_req(crypto_aead_encrypt(req),
2238 /* If encryption failed, we're done. */
2239 if (vec->crypt_error != 0)
2241 memmove((u8 *)vec->ctext, vec->ctext + vec->alen, vec->clen);
2245 * Mutate the authentic (ciphertext, AAD) pair to get an
2248 mutate_aead_message(vec, suite->aad_iv, ivsize);
2251 if (suite->einval_allowed)
2252 vec->crypt_error = -EINVAL;
2256 * Generate an AEAD test vector 'vec' using the implementation specified by
2257 * 'req'. The buffers in 'vec' must already be allocated.
2259 * If 'prefer_inauthentic' is true, then this function will generate inauthentic
2260 * test vectors (i.e. vectors with 'vec->novrfy=1') more often.
2262 static void generate_random_aead_testvec(struct aead_request *req,
2263 struct aead_testvec *vec,
2264 const struct aead_test_suite *suite,
2265 unsigned int maxkeysize,
2266 unsigned int maxdatasize,
2267 char *name, size_t max_namelen,
2268 bool prefer_inauthentic)
2270 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2271 const unsigned int ivsize = crypto_aead_ivsize(tfm);
2272 const unsigned int maxauthsize = crypto_aead_maxauthsize(tfm);
2273 unsigned int authsize;
2274 unsigned int total_len;
2276 /* Key: length in [0, maxkeysize], but usually choose maxkeysize */
2277 vec->klen = maxkeysize;
2278 if (prandom_u32() % 4 == 0)
2279 vec->klen = prandom_u32() % (maxkeysize + 1);
2280 generate_random_bytes((u8 *)vec->key, vec->klen);
2281 vec->setkey_error = crypto_aead_setkey(tfm, vec->key, vec->klen);
2284 generate_random_bytes((u8 *)vec->iv, ivsize);
2286 /* Tag length: in [0, maxauthsize], but usually choose maxauthsize */
2287 authsize = maxauthsize;
2288 if (prandom_u32() % 4 == 0)
2289 authsize = prandom_u32() % (maxauthsize + 1);
2290 if (prefer_inauthentic && authsize < MIN_COLLISION_FREE_AUTHSIZE)
2291 authsize = MIN_COLLISION_FREE_AUTHSIZE;
2292 if (WARN_ON(authsize > maxdatasize))
2293 authsize = maxdatasize;
2294 maxdatasize -= authsize;
2295 vec->setauthsize_error = crypto_aead_setauthsize(tfm, authsize);
2297 /* AAD, plaintext, and ciphertext lengths */
2298 total_len = generate_random_length(maxdatasize);
2299 if (prandom_u32() % 4 == 0)
2302 vec->alen = generate_random_length(total_len);
2303 vec->plen = total_len - vec->alen;
2304 vec->clen = vec->plen + authsize;
2307 * Generate the AAD, plaintext, and ciphertext. Not applicable if the
2308 * key or the authentication tag size couldn't be set.
2311 vec->crypt_error = 0;
2312 if (vec->setkey_error == 0 && vec->setauthsize_error == 0)
2313 generate_aead_message(req, suite, vec, prefer_inauthentic);
2314 snprintf(name, max_namelen,
2315 "\"random: alen=%u plen=%u authsize=%u klen=%u novrfy=%d\"",
2316 vec->alen, vec->plen, authsize, vec->klen, vec->novrfy);
2319 static void try_to_generate_inauthentic_testvec(
2320 struct aead_extra_tests_ctx *ctx)
2324 for (i = 0; i < 10; i++) {
2325 generate_random_aead_testvec(ctx->req, &ctx->vec,
2326 &ctx->test_desc->suite.aead,
2327 ctx->maxkeysize, ctx->maxdatasize,
2329 sizeof(ctx->vec_name), true);
2330 if (ctx->vec.novrfy)
2336 * Generate inauthentic test vectors (i.e. ciphertext, AAD pairs that aren't the
2337 * result of an encryption with the key) and verify that decryption fails.
2339 static int test_aead_inauthentic_inputs(struct aead_extra_tests_ctx *ctx)
2344 for (i = 0; i < fuzz_iterations * 8; i++) {
2346 * Since this part of the tests isn't comparing the
2347 * implementation to another, there's no point in testing any
2348 * test vectors other than inauthentic ones (vec.novrfy=1) here.
2350 * If we're having trouble generating such a test vector, e.g.
2351 * if the algorithm keeps rejecting the generated keys, don't
2352 * retry forever; just continue on.
2354 try_to_generate_inauthentic_testvec(ctx);
2355 if (ctx->vec.novrfy) {
2356 generate_random_testvec_config(&ctx->cfg, ctx->cfgname,
2357 sizeof(ctx->cfgname));
2358 err = test_aead_vec_cfg(DECRYPT, &ctx->vec,
2359 ctx->vec_name, &ctx->cfg,
2360 ctx->req, ctx->tsgls);
2370 * Test the AEAD algorithm against the corresponding generic implementation, if
2373 static int test_aead_vs_generic_impl(struct aead_extra_tests_ctx *ctx)
2375 struct crypto_aead *tfm = ctx->tfm;
2376 const char *algname = crypto_aead_alg(tfm)->base.cra_name;
2377 const char *driver = crypto_aead_driver_name(tfm);
2378 const char *generic_driver = ctx->test_desc->generic_driver;
2379 char _generic_driver[CRYPTO_MAX_ALG_NAME];
2380 struct crypto_aead *generic_tfm = NULL;
2381 struct aead_request *generic_req = NULL;
2385 if (!generic_driver) { /* Use default naming convention? */
2386 err = build_generic_driver_name(algname, _generic_driver);
2389 generic_driver = _generic_driver;
2392 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
2395 generic_tfm = crypto_alloc_aead(generic_driver, 0, 0);
2396 if (IS_ERR(generic_tfm)) {
2397 err = PTR_ERR(generic_tfm);
2398 if (err == -ENOENT) {
2399 pr_warn("alg: aead: skipping comparison tests for %s because %s is unavailable\n",
2400 driver, generic_driver);
2403 pr_err("alg: aead: error allocating %s (generic impl of %s): %d\n",
2404 generic_driver, algname, err);
2408 generic_req = aead_request_alloc(generic_tfm, GFP_KERNEL);
2414 /* Check the algorithm properties for consistency. */
2416 if (crypto_aead_maxauthsize(tfm) !=
2417 crypto_aead_maxauthsize(generic_tfm)) {
2418 pr_err("alg: aead: maxauthsize for %s (%u) doesn't match generic impl (%u)\n",
2419 driver, crypto_aead_maxauthsize(tfm),
2420 crypto_aead_maxauthsize(generic_tfm));
2425 if (crypto_aead_ivsize(tfm) != crypto_aead_ivsize(generic_tfm)) {
2426 pr_err("alg: aead: ivsize for %s (%u) doesn't match generic impl (%u)\n",
2427 driver, crypto_aead_ivsize(tfm),
2428 crypto_aead_ivsize(generic_tfm));
2433 if (crypto_aead_blocksize(tfm) != crypto_aead_blocksize(generic_tfm)) {
2434 pr_err("alg: aead: blocksize for %s (%u) doesn't match generic impl (%u)\n",
2435 driver, crypto_aead_blocksize(tfm),
2436 crypto_aead_blocksize(generic_tfm));
2442 * Now generate test vectors using the generic implementation, and test
2443 * the other implementation against them.
2445 for (i = 0; i < fuzz_iterations * 8; i++) {
2446 generate_random_aead_testvec(generic_req, &ctx->vec,
2447 &ctx->test_desc->suite.aead,
2448 ctx->maxkeysize, ctx->maxdatasize,
2450 sizeof(ctx->vec_name), false);
2451 generate_random_testvec_config(&ctx->cfg, ctx->cfgname,
2452 sizeof(ctx->cfgname));
2453 if (!ctx->vec.novrfy) {
2454 err = test_aead_vec_cfg(ENCRYPT, &ctx->vec,
2455 ctx->vec_name, &ctx->cfg,
2456 ctx->req, ctx->tsgls);
2460 if (ctx->vec.crypt_error == 0 || ctx->vec.novrfy) {
2461 err = test_aead_vec_cfg(DECRYPT, &ctx->vec,
2462 ctx->vec_name, &ctx->cfg,
2463 ctx->req, ctx->tsgls);
2471 crypto_free_aead(generic_tfm);
2472 aead_request_free(generic_req);
2476 static int test_aead_extra(const struct alg_test_desc *test_desc,
2477 struct aead_request *req,
2478 struct cipher_test_sglists *tsgls)
2480 struct aead_extra_tests_ctx *ctx;
2487 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2491 ctx->tfm = crypto_aead_reqtfm(req);
2492 ctx->test_desc = test_desc;
2494 ctx->maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
2495 ctx->maxkeysize = 0;
2496 for (i = 0; i < test_desc->suite.aead.count; i++)
2497 ctx->maxkeysize = max_t(unsigned int, ctx->maxkeysize,
2498 test_desc->suite.aead.vecs[i].klen);
2500 ctx->vec.key = kmalloc(ctx->maxkeysize, GFP_KERNEL);
2501 ctx->vec.iv = kmalloc(crypto_aead_ivsize(ctx->tfm), GFP_KERNEL);
2502 ctx->vec.assoc = kmalloc(ctx->maxdatasize, GFP_KERNEL);
2503 ctx->vec.ptext = kmalloc(ctx->maxdatasize, GFP_KERNEL);
2504 ctx->vec.ctext = kmalloc(ctx->maxdatasize, GFP_KERNEL);
2505 if (!ctx->vec.key || !ctx->vec.iv || !ctx->vec.assoc ||
2506 !ctx->vec.ptext || !ctx->vec.ctext) {
2511 err = test_aead_vs_generic_impl(ctx);
2515 err = test_aead_inauthentic_inputs(ctx);
2517 kfree(ctx->vec.key);
2519 kfree(ctx->vec.assoc);
2520 kfree(ctx->vec.ptext);
2521 kfree(ctx->vec.ctext);
2525 #else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
2526 static int test_aead_extra(const struct alg_test_desc *test_desc,
2527 struct aead_request *req,
2528 struct cipher_test_sglists *tsgls)
2532 #endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
2534 static int test_aead(int enc, const struct aead_test_suite *suite,
2535 struct aead_request *req,
2536 struct cipher_test_sglists *tsgls)
2541 for (i = 0; i < suite->count; i++) {
2542 err = test_aead_vec(enc, &suite->vecs[i], i, req, tsgls);
2550 static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
2553 const struct aead_test_suite *suite = &desc->suite.aead;
2554 struct crypto_aead *tfm;
2555 struct aead_request *req = NULL;
2556 struct cipher_test_sglists *tsgls = NULL;
2559 if (suite->count <= 0) {
2560 pr_err("alg: aead: empty test suite for %s\n", driver);
2564 tfm = crypto_alloc_aead(driver, type, mask);
2566 pr_err("alg: aead: failed to allocate transform for %s: %ld\n",
2567 driver, PTR_ERR(tfm));
2568 return PTR_ERR(tfm);
2570 driver = crypto_aead_driver_name(tfm);
2572 req = aead_request_alloc(tfm, GFP_KERNEL);
2574 pr_err("alg: aead: failed to allocate request for %s\n",
2580 tsgls = alloc_cipher_test_sglists();
2582 pr_err("alg: aead: failed to allocate test buffers for %s\n",
2588 err = test_aead(ENCRYPT, suite, req, tsgls);
2592 err = test_aead(DECRYPT, suite, req, tsgls);
2596 err = test_aead_extra(desc, req, tsgls);
2598 free_cipher_test_sglists(tsgls);
2599 aead_request_free(req);
2600 crypto_free_aead(tfm);
2604 static int test_cipher(struct crypto_cipher *tfm, int enc,
2605 const struct cipher_testvec *template,
2606 unsigned int tcount)
2608 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
2609 unsigned int i, j, k;
2612 const char *input, *result;
2614 char *xbuf[XBUFSIZE];
2617 if (testmgr_alloc_buf(xbuf))
2626 for (i = 0; i < tcount; i++) {
2628 if (fips_enabled && template[i].fips_skip)
2631 input = enc ? template[i].ptext : template[i].ctext;
2632 result = enc ? template[i].ctext : template[i].ptext;
2636 if (WARN_ON(template[i].len > PAGE_SIZE))
2640 memcpy(data, input, template[i].len);
2642 crypto_cipher_clear_flags(tfm, ~0);
2644 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
2646 ret = crypto_cipher_setkey(tfm, template[i].key,
2649 if (ret == template[i].setkey_error)
2651 pr_err("alg: cipher: %s setkey failed on test vector %u; expected_error=%d, actual_error=%d, flags=%#x\n",
2652 algo, j, template[i].setkey_error, ret,
2653 crypto_cipher_get_flags(tfm));
2656 if (template[i].setkey_error) {
2657 pr_err("alg: cipher: %s setkey unexpectedly succeeded on test vector %u; expected_error=%d\n",
2658 algo, j, template[i].setkey_error);
2663 for (k = 0; k < template[i].len;
2664 k += crypto_cipher_blocksize(tfm)) {
2666 crypto_cipher_encrypt_one(tfm, data + k,
2669 crypto_cipher_decrypt_one(tfm, data + k,
2674 if (memcmp(q, result, template[i].len)) {
2675 printk(KERN_ERR "alg: cipher: Test %d failed "
2676 "on %s for %s\n", j, e, algo);
2677 hexdump(q, template[i].len);
2686 testmgr_free_buf(xbuf);
2691 static int test_skcipher_vec_cfg(int enc, const struct cipher_testvec *vec,
2692 const char *vec_name,
2693 const struct testvec_config *cfg,
2694 struct skcipher_request *req,
2695 struct cipher_test_sglists *tsgls)
2697 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2698 const unsigned int alignmask = crypto_skcipher_alignmask(tfm);
2699 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2700 const char *driver = crypto_skcipher_driver_name(tfm);
2701 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
2702 const char *op = enc ? "encryption" : "decryption";
2703 DECLARE_CRYPTO_WAIT(wait);
2704 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
2705 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
2707 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
2713 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
2715 crypto_skcipher_clear_flags(tfm,
2716 CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
2717 err = do_setkey(crypto_skcipher_setkey, tfm, vec->key, vec->klen,
2720 if (err == vec->setkey_error)
2722 pr_err("alg: skcipher: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
2723 driver, vec_name, vec->setkey_error, err,
2724 crypto_skcipher_get_flags(tfm));
2727 if (vec->setkey_error) {
2728 pr_err("alg: skcipher: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
2729 driver, vec_name, vec->setkey_error);
2733 /* The IV must be copied to a buffer, as the algorithm may modify it */
2735 if (WARN_ON(ivsize > MAX_IVLEN))
2737 if (vec->generates_iv && !enc)
2738 memcpy(iv, vec->iv_out, ivsize);
2740 memcpy(iv, vec->iv, ivsize);
2742 memset(iv, 0, ivsize);
2744 if (vec->generates_iv) {
2745 pr_err("alg: skcipher: %s has ivsize=0 but test vector %s generates IV!\n",
2752 /* Build the src/dst scatterlists */
2753 input.iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
2754 input.iov_len = vec->len;
2755 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
2756 vec->len, vec->len, &input, 1);
2758 pr_err("alg: skcipher: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
2759 driver, op, vec_name, cfg->name);
2763 /* Do the actual encryption or decryption */
2764 testmgr_poison(req->__ctx, crypto_skcipher_reqsize(tfm));
2765 skcipher_request_set_callback(req, req_flags, crypto_req_done, &wait);
2766 skcipher_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
2769 crypto_disable_simd_for_test();
2770 err = enc ? crypto_skcipher_encrypt(req) : crypto_skcipher_decrypt(req);
2772 crypto_reenable_simd_for_test();
2773 err = crypto_wait_req(err, &wait);
2775 /* Check that the algorithm didn't overwrite things it shouldn't have */
2776 if (req->cryptlen != vec->len ||
2778 req->src != tsgls->src.sgl_ptr ||
2779 req->dst != tsgls->dst.sgl_ptr ||
2780 crypto_skcipher_reqtfm(req) != tfm ||
2781 req->base.complete != crypto_req_done ||
2782 req->base.flags != req_flags ||
2783 req->base.data != &wait) {
2784 pr_err("alg: skcipher: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
2785 driver, op, vec_name, cfg->name);
2786 if (req->cryptlen != vec->len)
2787 pr_err("alg: skcipher: changed 'req->cryptlen'\n");
2789 pr_err("alg: skcipher: changed 'req->iv'\n");
2790 if (req->src != tsgls->src.sgl_ptr)
2791 pr_err("alg: skcipher: changed 'req->src'\n");
2792 if (req->dst != tsgls->dst.sgl_ptr)
2793 pr_err("alg: skcipher: changed 'req->dst'\n");
2794 if (crypto_skcipher_reqtfm(req) != tfm)
2795 pr_err("alg: skcipher: changed 'req->base.tfm'\n");
2796 if (req->base.complete != crypto_req_done)
2797 pr_err("alg: skcipher: changed 'req->base.complete'\n");
2798 if (req->base.flags != req_flags)
2799 pr_err("alg: skcipher: changed 'req->base.flags'\n");
2800 if (req->base.data != &wait)
2801 pr_err("alg: skcipher: changed 'req->base.data'\n");
2804 if (is_test_sglist_corrupted(&tsgls->src)) {
2805 pr_err("alg: skcipher: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
2806 driver, op, vec_name, cfg->name);
2809 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
2810 is_test_sglist_corrupted(&tsgls->dst)) {
2811 pr_err("alg: skcipher: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
2812 driver, op, vec_name, cfg->name);
2816 /* Check for success or failure */
2818 if (err == vec->crypt_error)
2820 pr_err("alg: skcipher: %s %s failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
2821 driver, op, vec_name, vec->crypt_error, err, cfg->name);
2824 if (vec->crypt_error) {
2825 pr_err("alg: skcipher: %s %s unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
2826 driver, op, vec_name, vec->crypt_error, cfg->name);
2830 /* Check for the correct output (ciphertext or plaintext) */
2831 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
2833 if (err == -EOVERFLOW) {
2834 pr_err("alg: skcipher: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
2835 driver, op, vec_name, cfg->name);
2839 pr_err("alg: skcipher: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
2840 driver, op, vec_name, cfg->name);
2844 /* If applicable, check that the algorithm generated the correct IV */
2845 if (vec->iv_out && memcmp(iv, vec->iv_out, ivsize) != 0) {
2846 pr_err("alg: skcipher: %s %s test failed (wrong output IV) on test vector %s, cfg=\"%s\"\n",
2847 driver, op, vec_name, cfg->name);
2848 hexdump(iv, ivsize);
2855 static int test_skcipher_vec(int enc, const struct cipher_testvec *vec,
2856 unsigned int vec_num,
2857 struct skcipher_request *req,
2858 struct cipher_test_sglists *tsgls)
2864 if (fips_enabled && vec->fips_skip)
2867 sprintf(vec_name, "%u", vec_num);
2869 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
2870 err = test_skcipher_vec_cfg(enc, vec, vec_name,
2871 &default_cipher_testvec_configs[i],
2877 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2878 if (!noextratests) {
2879 struct testvec_config cfg;
2880 char cfgname[TESTVEC_CONFIG_NAMELEN];
2882 for (i = 0; i < fuzz_iterations; i++) {
2883 generate_random_testvec_config(&cfg, cfgname,
2885 err = test_skcipher_vec_cfg(enc, vec, vec_name,
2896 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2898 * Generate a symmetric cipher test vector from the given implementation.
2899 * Assumes the buffers in 'vec' were already allocated.
2901 static void generate_random_cipher_testvec(struct skcipher_request *req,
2902 struct cipher_testvec *vec,
2903 unsigned int maxdatasize,
2904 char *name, size_t max_namelen)
2906 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2907 const unsigned int maxkeysize = crypto_skcipher_max_keysize(tfm);
2908 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2909 struct scatterlist src, dst;
2911 DECLARE_CRYPTO_WAIT(wait);
2913 /* Key: length in [0, maxkeysize], but usually choose maxkeysize */
2914 vec->klen = maxkeysize;
2915 if (prandom_u32() % 4 == 0)
2916 vec->klen = prandom_u32() % (maxkeysize + 1);
2917 generate_random_bytes((u8 *)vec->key, vec->klen);
2918 vec->setkey_error = crypto_skcipher_setkey(tfm, vec->key, vec->klen);
2921 generate_random_bytes((u8 *)vec->iv, ivsize);
2924 vec->len = generate_random_length(maxdatasize);
2925 generate_random_bytes((u8 *)vec->ptext, vec->len);
2927 /* If the key couldn't be set, no need to continue to encrypt. */
2928 if (vec->setkey_error)
2932 sg_init_one(&src, vec->ptext, vec->len);
2933 sg_init_one(&dst, vec->ctext, vec->len);
2934 memcpy(iv, vec->iv, ivsize);
2935 skcipher_request_set_callback(req, 0, crypto_req_done, &wait);
2936 skcipher_request_set_crypt(req, &src, &dst, vec->len, iv);
2937 vec->crypt_error = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
2938 if (vec->crypt_error != 0) {
2940 * The only acceptable error here is for an invalid length, so
2941 * skcipher decryption should fail with the same error too.
2942 * We'll test for this. But to keep the API usage well-defined,
2943 * explicitly initialize the ciphertext buffer too.
2945 memset((u8 *)vec->ctext, 0, vec->len);
2948 snprintf(name, max_namelen, "\"random: len=%u klen=%u\"",
2949 vec->len, vec->klen);
2953 * Test the skcipher algorithm represented by @req against the corresponding
2954 * generic implementation, if one is available.
2956 static int test_skcipher_vs_generic_impl(const char *generic_driver,
2957 struct skcipher_request *req,
2958 struct cipher_test_sglists *tsgls)
2960 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2961 const unsigned int maxkeysize = crypto_skcipher_max_keysize(tfm);
2962 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2963 const unsigned int blocksize = crypto_skcipher_blocksize(tfm);
2964 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
2965 const char *algname = crypto_skcipher_alg(tfm)->base.cra_name;
2966 const char *driver = crypto_skcipher_driver_name(tfm);
2967 char _generic_driver[CRYPTO_MAX_ALG_NAME];
2968 struct crypto_skcipher *generic_tfm = NULL;
2969 struct skcipher_request *generic_req = NULL;
2971 struct cipher_testvec vec = { 0 };
2973 struct testvec_config *cfg;
2974 char cfgname[TESTVEC_CONFIG_NAMELEN];
2980 /* Keywrap isn't supported here yet as it handles its IV differently. */
2981 if (strncmp(algname, "kw(", 3) == 0)
2984 if (!generic_driver) { /* Use default naming convention? */
2985 err = build_generic_driver_name(algname, _generic_driver);
2988 generic_driver = _generic_driver;
2991 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
2994 generic_tfm = crypto_alloc_skcipher(generic_driver, 0, 0);
2995 if (IS_ERR(generic_tfm)) {
2996 err = PTR_ERR(generic_tfm);
2997 if (err == -ENOENT) {
2998 pr_warn("alg: skcipher: skipping comparison tests for %s because %s is unavailable\n",
2999 driver, generic_driver);
3002 pr_err("alg: skcipher: error allocating %s (generic impl of %s): %d\n",
3003 generic_driver, algname, err);
3007 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
3013 generic_req = skcipher_request_alloc(generic_tfm, GFP_KERNEL);
3019 /* Check the algorithm properties for consistency. */
3021 if (crypto_skcipher_min_keysize(tfm) !=
3022 crypto_skcipher_min_keysize(generic_tfm)) {
3023 pr_err("alg: skcipher: min keysize for %s (%u) doesn't match generic impl (%u)\n",
3024 driver, crypto_skcipher_min_keysize(tfm),
3025 crypto_skcipher_min_keysize(generic_tfm));
3030 if (maxkeysize != crypto_skcipher_max_keysize(generic_tfm)) {
3031 pr_err("alg: skcipher: max keysize for %s (%u) doesn't match generic impl (%u)\n",
3033 crypto_skcipher_max_keysize(generic_tfm));
3038 if (ivsize != crypto_skcipher_ivsize(generic_tfm)) {
3039 pr_err("alg: skcipher: ivsize for %s (%u) doesn't match generic impl (%u)\n",
3040 driver, ivsize, crypto_skcipher_ivsize(generic_tfm));
3045 if (blocksize != crypto_skcipher_blocksize(generic_tfm)) {
3046 pr_err("alg: skcipher: blocksize for %s (%u) doesn't match generic impl (%u)\n",
3048 crypto_skcipher_blocksize(generic_tfm));
3054 * Now generate test vectors using the generic implementation, and test
3055 * the other implementation against them.
3058 vec.key = kmalloc(maxkeysize, GFP_KERNEL);
3059 vec.iv = kmalloc(ivsize, GFP_KERNEL);
3060 vec.ptext = kmalloc(maxdatasize, GFP_KERNEL);
3061 vec.ctext = kmalloc(maxdatasize, GFP_KERNEL);
3062 if (!vec.key || !vec.iv || !vec.ptext || !vec.ctext) {
3067 for (i = 0; i < fuzz_iterations * 8; i++) {
3068 generate_random_cipher_testvec(generic_req, &vec, maxdatasize,
3069 vec_name, sizeof(vec_name));
3070 generate_random_testvec_config(cfg, cfgname, sizeof(cfgname));
3072 err = test_skcipher_vec_cfg(ENCRYPT, &vec, vec_name,
3076 err = test_skcipher_vec_cfg(DECRYPT, &vec, vec_name,
3089 crypto_free_skcipher(generic_tfm);
3090 skcipher_request_free(generic_req);
3093 #else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
3094 static int test_skcipher_vs_generic_impl(const char *generic_driver,
3095 struct skcipher_request *req,
3096 struct cipher_test_sglists *tsgls)
3100 #endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
3102 static int test_skcipher(int enc, const struct cipher_test_suite *suite,
3103 struct skcipher_request *req,
3104 struct cipher_test_sglists *tsgls)
3109 for (i = 0; i < suite->count; i++) {
3110 err = test_skcipher_vec(enc, &suite->vecs[i], i, req, tsgls);
3118 static int alg_test_skcipher(const struct alg_test_desc *desc,
3119 const char *driver, u32 type, u32 mask)
3121 const struct cipher_test_suite *suite = &desc->suite.cipher;
3122 struct crypto_skcipher *tfm;
3123 struct skcipher_request *req = NULL;
3124 struct cipher_test_sglists *tsgls = NULL;
3127 if (suite->count <= 0) {
3128 pr_err("alg: skcipher: empty test suite for %s\n", driver);
3132 tfm = crypto_alloc_skcipher(driver, type, mask);
3134 pr_err("alg: skcipher: failed to allocate transform for %s: %ld\n",
3135 driver, PTR_ERR(tfm));
3136 return PTR_ERR(tfm);
3138 driver = crypto_skcipher_driver_name(tfm);
3140 req = skcipher_request_alloc(tfm, GFP_KERNEL);
3142 pr_err("alg: skcipher: failed to allocate request for %s\n",
3148 tsgls = alloc_cipher_test_sglists();
3150 pr_err("alg: skcipher: failed to allocate test buffers for %s\n",
3156 err = test_skcipher(ENCRYPT, suite, req, tsgls);
3160 err = test_skcipher(DECRYPT, suite, req, tsgls);
3164 err = test_skcipher_vs_generic_impl(desc->generic_driver, req, tsgls);
3166 free_cipher_test_sglists(tsgls);
3167 skcipher_request_free(req);
3168 crypto_free_skcipher(tfm);
3172 static int test_comp(struct crypto_comp *tfm,
3173 const struct comp_testvec *ctemplate,
3174 const struct comp_testvec *dtemplate,
3175 int ctcount, int dtcount)
3177 const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
3178 char *output, *decomp_output;
3182 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3186 decomp_output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3187 if (!decomp_output) {
3192 for (i = 0; i < ctcount; i++) {
3194 unsigned int dlen = COMP_BUF_SIZE;
3196 memset(output, 0, COMP_BUF_SIZE);
3197 memset(decomp_output, 0, COMP_BUF_SIZE);
3199 ilen = ctemplate[i].inlen;
3200 ret = crypto_comp_compress(tfm, ctemplate[i].input,
3201 ilen, output, &dlen);
3203 printk(KERN_ERR "alg: comp: compression failed "
3204 "on test %d for %s: ret=%d\n", i + 1, algo,
3210 dlen = COMP_BUF_SIZE;
3211 ret = crypto_comp_decompress(tfm, output,
3212 ilen, decomp_output, &dlen);
3214 pr_err("alg: comp: compression failed: decompress: on test %d for %s failed: ret=%d\n",
3219 if (dlen != ctemplate[i].inlen) {
3220 printk(KERN_ERR "alg: comp: Compression test %d "
3221 "failed for %s: output len = %d\n", i + 1, algo,
3227 if (memcmp(decomp_output, ctemplate[i].input,
3228 ctemplate[i].inlen)) {
3229 pr_err("alg: comp: compression failed: output differs: on test %d for %s\n",
3231 hexdump(decomp_output, dlen);
3237 for (i = 0; i < dtcount; i++) {
3239 unsigned int dlen = COMP_BUF_SIZE;
3241 memset(decomp_output, 0, COMP_BUF_SIZE);
3243 ilen = dtemplate[i].inlen;
3244 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
3245 ilen, decomp_output, &dlen);
3247 printk(KERN_ERR "alg: comp: decompression failed "
3248 "on test %d for %s: ret=%d\n", i + 1, algo,
3253 if (dlen != dtemplate[i].outlen) {
3254 printk(KERN_ERR "alg: comp: Decompression test %d "
3255 "failed for %s: output len = %d\n", i + 1, algo,
3261 if (memcmp(decomp_output, dtemplate[i].output, dlen)) {
3262 printk(KERN_ERR "alg: comp: Decompression test %d "
3263 "failed for %s\n", i + 1, algo);
3264 hexdump(decomp_output, dlen);
3273 kfree(decomp_output);
3278 static int test_acomp(struct crypto_acomp *tfm,
3279 const struct comp_testvec *ctemplate,
3280 const struct comp_testvec *dtemplate,
3281 int ctcount, int dtcount)
3283 const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
3285 char *output, *decomp_out;
3287 struct scatterlist src, dst;
3288 struct acomp_req *req;
3289 struct crypto_wait wait;
3291 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3295 decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3301 for (i = 0; i < ctcount; i++) {
3302 unsigned int dlen = COMP_BUF_SIZE;
3303 int ilen = ctemplate[i].inlen;
3306 input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL);
3312 memset(output, 0, dlen);
3313 crypto_init_wait(&wait);
3314 sg_init_one(&src, input_vec, ilen);
3315 sg_init_one(&dst, output, dlen);
3317 req = acomp_request_alloc(tfm);
3319 pr_err("alg: acomp: request alloc failed for %s\n",
3326 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3327 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3328 crypto_req_done, &wait);
3330 ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
3332 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
3335 acomp_request_free(req);
3340 dlen = COMP_BUF_SIZE;
3341 sg_init_one(&src, output, ilen);
3342 sg_init_one(&dst, decomp_out, dlen);
3343 crypto_init_wait(&wait);
3344 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3346 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
3348 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
3351 acomp_request_free(req);
3355 if (req->dlen != ctemplate[i].inlen) {
3356 pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
3357 i + 1, algo, req->dlen);
3360 acomp_request_free(req);
3364 if (memcmp(input_vec, decomp_out, req->dlen)) {
3365 pr_err("alg: acomp: Compression test %d failed for %s\n",
3367 hexdump(output, req->dlen);
3370 acomp_request_free(req);
3375 acomp_request_free(req);
3378 for (i = 0; i < dtcount; i++) {
3379 unsigned int dlen = COMP_BUF_SIZE;
3380 int ilen = dtemplate[i].inlen;
3383 input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL);
3389 memset(output, 0, dlen);
3390 crypto_init_wait(&wait);
3391 sg_init_one(&src, input_vec, ilen);
3392 sg_init_one(&dst, output, dlen);
3394 req = acomp_request_alloc(tfm);
3396 pr_err("alg: acomp: request alloc failed for %s\n",
3403 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3404 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3405 crypto_req_done, &wait);
3407 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
3409 pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
3412 acomp_request_free(req);
3416 if (req->dlen != dtemplate[i].outlen) {
3417 pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
3418 i + 1, algo, req->dlen);
3421 acomp_request_free(req);
3425 if (memcmp(output, dtemplate[i].output, req->dlen)) {
3426 pr_err("alg: acomp: Decompression test %d failed for %s\n",
3428 hexdump(output, req->dlen);
3431 acomp_request_free(req);
3436 acomp_request_free(req);
3447 static int test_cprng(struct crypto_rng *tfm,
3448 const struct cprng_testvec *template,
3449 unsigned int tcount)
3451 const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
3452 int err = 0, i, j, seedsize;
3456 seedsize = crypto_rng_seedsize(tfm);
3458 seed = kmalloc(seedsize, GFP_KERNEL);
3460 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
3465 for (i = 0; i < tcount; i++) {
3466 memset(result, 0, 32);
3468 memcpy(seed, template[i].v, template[i].vlen);
3469 memcpy(seed + template[i].vlen, template[i].key,
3471 memcpy(seed + template[i].vlen + template[i].klen,
3472 template[i].dt, template[i].dtlen);
3474 err = crypto_rng_reset(tfm, seed, seedsize);
3476 printk(KERN_ERR "alg: cprng: Failed to reset rng "
3481 for (j = 0; j < template[i].loops; j++) {
3482 err = crypto_rng_get_bytes(tfm, result,
3485 printk(KERN_ERR "alg: cprng: Failed to obtain "
3486 "the correct amount of random data for "
3487 "%s (requested %d)\n", algo,
3493 err = memcmp(result, template[i].result,
3496 printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
3498 hexdump(result, template[i].rlen);
3509 static int alg_test_cipher(const struct alg_test_desc *desc,
3510 const char *driver, u32 type, u32 mask)
3512 const struct cipher_test_suite *suite = &desc->suite.cipher;
3513 struct crypto_cipher *tfm;
3516 tfm = crypto_alloc_cipher(driver, type, mask);
3518 printk(KERN_ERR "alg: cipher: Failed to load transform for "
3519 "%s: %ld\n", driver, PTR_ERR(tfm));
3520 return PTR_ERR(tfm);
3523 err = test_cipher(tfm, ENCRYPT, suite->vecs, suite->count);
3525 err = test_cipher(tfm, DECRYPT, suite->vecs, suite->count);
3527 crypto_free_cipher(tfm);
3531 static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
3534 struct crypto_comp *comp;
3535 struct crypto_acomp *acomp;
3537 u32 algo_type = type & CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
3539 if (algo_type == CRYPTO_ALG_TYPE_ACOMPRESS) {
3540 acomp = crypto_alloc_acomp(driver, type, mask);
3541 if (IS_ERR(acomp)) {
3542 pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
3543 driver, PTR_ERR(acomp));
3544 return PTR_ERR(acomp);
3546 err = test_acomp(acomp, desc->suite.comp.comp.vecs,
3547 desc->suite.comp.decomp.vecs,
3548 desc->suite.comp.comp.count,
3549 desc->suite.comp.decomp.count);
3550 crypto_free_acomp(acomp);
3552 comp = crypto_alloc_comp(driver, type, mask);
3554 pr_err("alg: comp: Failed to load transform for %s: %ld\n",
3555 driver, PTR_ERR(comp));
3556 return PTR_ERR(comp);
3559 err = test_comp(comp, desc->suite.comp.comp.vecs,
3560 desc->suite.comp.decomp.vecs,
3561 desc->suite.comp.comp.count,
3562 desc->suite.comp.decomp.count);
3564 crypto_free_comp(comp);
3569 static int alg_test_crc32c(const struct alg_test_desc *desc,
3570 const char *driver, u32 type, u32 mask)
3572 struct crypto_shash *tfm;
3576 err = alg_test_hash(desc, driver, type, mask);
3580 tfm = crypto_alloc_shash(driver, type, mask);
3582 if (PTR_ERR(tfm) == -ENOENT) {
3584 * This crc32c implementation is only available through
3585 * ahash API, not the shash API, so the remaining part
3586 * of the test is not applicable to it.
3590 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
3591 "%ld\n", driver, PTR_ERR(tfm));
3592 return PTR_ERR(tfm);
3594 driver = crypto_shash_driver_name(tfm);
3597 SHASH_DESC_ON_STACK(shash, tfm);
3598 u32 *ctx = (u32 *)shash_desc_ctx(shash);
3603 err = crypto_shash_final(shash, (u8 *)&val);
3605 printk(KERN_ERR "alg: crc32c: Operation failed for "
3606 "%s: %d\n", driver, err);
3610 if (val != cpu_to_le32(~420553207)) {
3611 pr_err("alg: crc32c: Test failed for %s: %u\n",
3612 driver, le32_to_cpu(val));
3617 crypto_free_shash(tfm);
3622 static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
3625 struct crypto_rng *rng;
3628 rng = crypto_alloc_rng(driver, type, mask);
3630 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
3631 "%ld\n", driver, PTR_ERR(rng));
3632 return PTR_ERR(rng);
3635 err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
3637 crypto_free_rng(rng);
3643 static int drbg_cavs_test(const struct drbg_testvec *test, int pr,
3644 const char *driver, u32 type, u32 mask)
3647 struct crypto_rng *drng;
3648 struct drbg_test_data test_data;
3649 struct drbg_string addtl, pers, testentropy;
3650 unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
3655 drng = crypto_alloc_rng(driver, type, mask);
3657 printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
3659 kfree_sensitive(buf);
3663 test_data.testentropy = &testentropy;
3664 drbg_string_fill(&testentropy, test->entropy, test->entropylen);
3665 drbg_string_fill(&pers, test->pers, test->perslen);
3666 ret = crypto_drbg_reset_test(drng, &pers, &test_data);
3668 printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
3672 drbg_string_fill(&addtl, test->addtla, test->addtllen);
3674 drbg_string_fill(&testentropy, test->entpra, test->entprlen);
3675 ret = crypto_drbg_get_bytes_addtl_test(drng,
3676 buf, test->expectedlen, &addtl, &test_data);
3678 ret = crypto_drbg_get_bytes_addtl(drng,
3679 buf, test->expectedlen, &addtl);
3682 printk(KERN_ERR "alg: drbg: could not obtain random data for "
3683 "driver %s\n", driver);
3687 drbg_string_fill(&addtl, test->addtlb, test->addtllen);
3689 drbg_string_fill(&testentropy, test->entprb, test->entprlen);
3690 ret = crypto_drbg_get_bytes_addtl_test(drng,
3691 buf, test->expectedlen, &addtl, &test_data);
3693 ret = crypto_drbg_get_bytes_addtl(drng,
3694 buf, test->expectedlen, &addtl);
3697 printk(KERN_ERR "alg: drbg: could not obtain random data for "
3698 "driver %s\n", driver);
3702 ret = memcmp(test->expected, buf, test->expectedlen);
3705 crypto_free_rng(drng);
3706 kfree_sensitive(buf);
3711 static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
3717 const struct drbg_testvec *template = desc->suite.drbg.vecs;
3718 unsigned int tcount = desc->suite.drbg.count;
3720 if (0 == memcmp(driver, "drbg_pr_", 8))
3723 for (i = 0; i < tcount; i++) {
3724 err = drbg_cavs_test(&template[i], pr, driver, type, mask);
3726 printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
3736 static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
3739 struct kpp_request *req;
3740 void *input_buf = NULL;
3741 void *output_buf = NULL;
3742 void *a_public = NULL;
3744 void *shared_secret = NULL;
3745 struct crypto_wait wait;
3746 unsigned int out_len_max;
3748 struct scatterlist src, dst;
3750 req = kpp_request_alloc(tfm, GFP_KERNEL);
3754 crypto_init_wait(&wait);
3756 err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
3760 out_len_max = crypto_kpp_maxsize(tfm);
3761 output_buf = kzalloc(out_len_max, GFP_KERNEL);
3767 /* Use appropriate parameter as base */
3768 kpp_request_set_input(req, NULL, 0);
3769 sg_init_one(&dst, output_buf, out_len_max);
3770 kpp_request_set_output(req, &dst, out_len_max);
3771 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3772 crypto_req_done, &wait);
3774 /* Compute party A's public key */
3775 err = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
3777 pr_err("alg: %s: Party A: generate public key test failed. err %d\n",
3783 /* Save party A's public key */
3784 a_public = kmemdup(sg_virt(req->dst), out_len_max, GFP_KERNEL);
3790 /* Verify calculated public key */
3791 if (memcmp(vec->expected_a_public, sg_virt(req->dst),
3792 vec->expected_a_public_size)) {
3793 pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n",
3800 /* Calculate shared secret key by using counter part (b) public key. */
3801 input_buf = kmemdup(vec->b_public, vec->b_public_size, GFP_KERNEL);
3807 sg_init_one(&src, input_buf, vec->b_public_size);
3808 sg_init_one(&dst, output_buf, out_len_max);
3809 kpp_request_set_input(req, &src, vec->b_public_size);
3810 kpp_request_set_output(req, &dst, out_len_max);
3811 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3812 crypto_req_done, &wait);
3813 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait);
3815 pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n",
3821 /* Save the shared secret obtained by party A */
3822 a_ss = kmemdup(sg_virt(req->dst), vec->expected_ss_size, GFP_KERNEL);
3829 * Calculate party B's shared secret by using party A's
3832 err = crypto_kpp_set_secret(tfm, vec->b_secret,
3833 vec->b_secret_size);
3837 sg_init_one(&src, a_public, vec->expected_a_public_size);
3838 sg_init_one(&dst, output_buf, out_len_max);
3839 kpp_request_set_input(req, &src, vec->expected_a_public_size);
3840 kpp_request_set_output(req, &dst, out_len_max);
3841 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3842 crypto_req_done, &wait);
3843 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req),
3846 pr_err("alg: %s: Party B: compute shared secret failed. err %d\n",
3851 shared_secret = a_ss;
3853 shared_secret = (void *)vec->expected_ss;
3857 * verify shared secret from which the user will derive
3858 * secret key by executing whatever hash it has chosen
3860 if (memcmp(shared_secret, sg_virt(req->dst),
3861 vec->expected_ss_size)) {
3862 pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
3874 kpp_request_free(req);
3878 static int test_kpp(struct crypto_kpp *tfm, const char *alg,
3879 const struct kpp_testvec *vecs, unsigned int tcount)
3883 for (i = 0; i < tcount; i++) {
3884 ret = do_test_kpp(tfm, vecs++, alg);
3886 pr_err("alg: %s: test failed on vector %d, err=%d\n",
3894 static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
3897 struct crypto_kpp *tfm;
3900 tfm = crypto_alloc_kpp(driver, type, mask);
3902 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
3903 driver, PTR_ERR(tfm));
3904 return PTR_ERR(tfm);
3906 if (desc->suite.kpp.vecs)
3907 err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
3908 desc->suite.kpp.count);
3910 crypto_free_kpp(tfm);
3914 static u8 *test_pack_u32(u8 *dst, u32 val)
3916 memcpy(dst, &val, sizeof(val));
3917 return dst + sizeof(val);
3920 static int test_akcipher_one(struct crypto_akcipher *tfm,
3921 const struct akcipher_testvec *vecs)
3923 char *xbuf[XBUFSIZE];
3924 struct akcipher_request *req;
3925 void *outbuf_enc = NULL;
3926 void *outbuf_dec = NULL;
3927 struct crypto_wait wait;
3928 unsigned int out_len_max, out_len = 0;
3930 struct scatterlist src, dst, src_tab[3];
3932 unsigned int m_size, c_size;
3936 if (testmgr_alloc_buf(xbuf))
3939 req = akcipher_request_alloc(tfm, GFP_KERNEL);
3943 crypto_init_wait(&wait);
3945 key = kmalloc(vecs->key_len + sizeof(u32) * 2 + vecs->param_len,
3949 memcpy(key, vecs->key, vecs->key_len);
3950 ptr = key + vecs->key_len;
3951 ptr = test_pack_u32(ptr, vecs->algo);
3952 ptr = test_pack_u32(ptr, vecs->param_len);
3953 memcpy(ptr, vecs->params, vecs->param_len);
3955 if (vecs->public_key_vec)
3956 err = crypto_akcipher_set_pub_key(tfm, key, vecs->key_len);
3958 err = crypto_akcipher_set_priv_key(tfm, key, vecs->key_len);
3963 * First run test which do not require a private key, such as
3964 * encrypt or verify.
3967 out_len_max = crypto_akcipher_maxsize(tfm);
3968 outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
3972 if (!vecs->siggen_sigver_test) {
3974 m_size = vecs->m_size;
3976 c_size = vecs->c_size;
3979 /* Swap args so we could keep plaintext (digest)
3980 * in vecs->m, and cooked signature in vecs->c.
3982 m = vecs->c; /* signature */
3983 m_size = vecs->c_size;
3984 c = vecs->m; /* digest */
3985 c_size = vecs->m_size;
3990 if (WARN_ON(m_size > PAGE_SIZE))
3992 memcpy(xbuf[0], m, m_size);
3994 sg_init_table(src_tab, 3);
3995 sg_set_buf(&src_tab[0], xbuf[0], 8);
3996 sg_set_buf(&src_tab[1], xbuf[0] + 8, m_size - 8);
3997 if (vecs->siggen_sigver_test) {
3998 if (WARN_ON(c_size > PAGE_SIZE))
4000 memcpy(xbuf[1], c, c_size);
4001 sg_set_buf(&src_tab[2], xbuf[1], c_size);
4002 akcipher_request_set_crypt(req, src_tab, NULL, m_size, c_size);
4004 sg_init_one(&dst, outbuf_enc, out_len_max);
4005 akcipher_request_set_crypt(req, src_tab, &dst, m_size,
4008 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
4009 crypto_req_done, &wait);
4011 err = crypto_wait_req(vecs->siggen_sigver_test ?
4012 /* Run asymmetric signature verification */
4013 crypto_akcipher_verify(req) :
4014 /* Run asymmetric encrypt */
4015 crypto_akcipher_encrypt(req), &wait);
4017 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
4020 if (!vecs->siggen_sigver_test && c) {
4021 if (req->dst_len != c_size) {
4022 pr_err("alg: akcipher: %s test failed. Invalid output len\n",
4027 /* verify that encrypted message is equal to expected */
4028 if (memcmp(c, outbuf_enc, c_size) != 0) {
4029 pr_err("alg: akcipher: %s test failed. Invalid output\n",
4031 hexdump(outbuf_enc, c_size);
4038 * Don't invoke (decrypt or sign) test which require a private key
4039 * for vectors with only a public key.
4041 if (vecs->public_key_vec) {
4045 outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
4051 if (!vecs->siggen_sigver_test && !c) {
4053 c_size = req->dst_len;
4057 op = vecs->siggen_sigver_test ? "sign" : "decrypt";
4058 if (WARN_ON(c_size > PAGE_SIZE))
4060 memcpy(xbuf[0], c, c_size);
4062 sg_init_one(&src, xbuf[0], c_size);
4063 sg_init_one(&dst, outbuf_dec, out_len_max);
4064 crypto_init_wait(&wait);
4065 akcipher_request_set_crypt(req, &src, &dst, c_size, out_len_max);
4067 err = crypto_wait_req(vecs->siggen_sigver_test ?
4068 /* Run asymmetric signature generation */
4069 crypto_akcipher_sign(req) :
4070 /* Run asymmetric decrypt */
4071 crypto_akcipher_decrypt(req), &wait);
4073 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
4076 out_len = req->dst_len;
4077 if (out_len < m_size) {
4078 pr_err("alg: akcipher: %s test failed. Invalid output len %u\n",
4083 /* verify that decrypted message is equal to the original msg */
4084 if (memchr_inv(outbuf_dec, 0, out_len - m_size) ||
4085 memcmp(m, outbuf_dec + out_len - m_size, m_size)) {
4086 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
4087 hexdump(outbuf_dec, out_len);
4096 akcipher_request_free(req);
4098 testmgr_free_buf(xbuf);
4102 static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
4103 const struct akcipher_testvec *vecs,
4104 unsigned int tcount)
4107 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
4110 for (i = 0; i < tcount; i++) {
4111 ret = test_akcipher_one(tfm, vecs++);
4115 pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
4122 static int alg_test_akcipher(const struct alg_test_desc *desc,
4123 const char *driver, u32 type, u32 mask)
4125 struct crypto_akcipher *tfm;
4128 tfm = crypto_alloc_akcipher(driver, type, mask);
4130 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
4131 driver, PTR_ERR(tfm));
4132 return PTR_ERR(tfm);
4134 if (desc->suite.akcipher.vecs)
4135 err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
4136 desc->suite.akcipher.count);
4138 crypto_free_akcipher(tfm);
4142 static int alg_test_null(const struct alg_test_desc *desc,
4143 const char *driver, u32 type, u32 mask)
4148 #define ____VECS(tv) .vecs = tv, .count = ARRAY_SIZE(tv)
4149 #define __VECS(tv) { ____VECS(tv) }
4151 /* Please keep this list sorted by algorithm name. */
4152 static const struct alg_test_desc alg_test_descs[] = {
4154 .alg = "adiantum(xchacha12,aes)",
4155 .generic_driver = "adiantum(xchacha12-generic,aes-generic,nhpoly1305-generic)",
4156 .test = alg_test_skcipher,
4158 .cipher = __VECS(adiantum_xchacha12_aes_tv_template)
4161 .alg = "adiantum(xchacha20,aes)",
4162 .generic_driver = "adiantum(xchacha20-generic,aes-generic,nhpoly1305-generic)",
4163 .test = alg_test_skcipher,
4165 .cipher = __VECS(adiantum_xchacha20_aes_tv_template)
4169 .test = alg_test_aead,
4171 .aead = __VECS(aegis128_tv_template)
4174 .alg = "ansi_cprng",
4175 .test = alg_test_cprng,
4177 .cprng = __VECS(ansi_cprng_aes_tv_template)
4180 .alg = "authenc(hmac(md5),ecb(cipher_null))",
4181 .test = alg_test_aead,
4183 .aead = __VECS(hmac_md5_ecb_cipher_null_tv_template)
4186 .alg = "authenc(hmac(sha1),cbc(aes))",
4187 .test = alg_test_aead,
4190 .aead = __VECS(hmac_sha1_aes_cbc_tv_temp)
4193 .alg = "authenc(hmac(sha1),cbc(des))",
4194 .test = alg_test_aead,
4196 .aead = __VECS(hmac_sha1_des_cbc_tv_temp)
4199 .alg = "authenc(hmac(sha1),cbc(des3_ede))",
4200 .test = alg_test_aead,
4203 .aead = __VECS(hmac_sha1_des3_ede_cbc_tv_temp)
4206 .alg = "authenc(hmac(sha1),ctr(aes))",
4207 .test = alg_test_null,
4210 .alg = "authenc(hmac(sha1),ecb(cipher_null))",
4211 .test = alg_test_aead,
4213 .aead = __VECS(hmac_sha1_ecb_cipher_null_tv_temp)
4216 .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
4217 .test = alg_test_null,
4220 .alg = "authenc(hmac(sha224),cbc(des))",
4221 .test = alg_test_aead,
4223 .aead = __VECS(hmac_sha224_des_cbc_tv_temp)
4226 .alg = "authenc(hmac(sha224),cbc(des3_ede))",
4227 .test = alg_test_aead,
4230 .aead = __VECS(hmac_sha224_des3_ede_cbc_tv_temp)
4233 .alg = "authenc(hmac(sha256),cbc(aes))",
4234 .test = alg_test_aead,
4237 .aead = __VECS(hmac_sha256_aes_cbc_tv_temp)
4240 .alg = "authenc(hmac(sha256),cbc(des))",
4241 .test = alg_test_aead,
4243 .aead = __VECS(hmac_sha256_des_cbc_tv_temp)
4246 .alg = "authenc(hmac(sha256),cbc(des3_ede))",
4247 .test = alg_test_aead,
4250 .aead = __VECS(hmac_sha256_des3_ede_cbc_tv_temp)
4253 .alg = "authenc(hmac(sha256),ctr(aes))",
4254 .test = alg_test_null,
4257 .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
4258 .test = alg_test_null,
4261 .alg = "authenc(hmac(sha384),cbc(des))",
4262 .test = alg_test_aead,
4264 .aead = __VECS(hmac_sha384_des_cbc_tv_temp)
4267 .alg = "authenc(hmac(sha384),cbc(des3_ede))",
4268 .test = alg_test_aead,
4271 .aead = __VECS(hmac_sha384_des3_ede_cbc_tv_temp)
4274 .alg = "authenc(hmac(sha384),ctr(aes))",
4275 .test = alg_test_null,
4278 .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
4279 .test = alg_test_null,
4282 .alg = "authenc(hmac(sha512),cbc(aes))",
4284 .test = alg_test_aead,
4286 .aead = __VECS(hmac_sha512_aes_cbc_tv_temp)
4289 .alg = "authenc(hmac(sha512),cbc(des))",
4290 .test = alg_test_aead,
4292 .aead = __VECS(hmac_sha512_des_cbc_tv_temp)
4295 .alg = "authenc(hmac(sha512),cbc(des3_ede))",
4296 .test = alg_test_aead,
4299 .aead = __VECS(hmac_sha512_des3_ede_cbc_tv_temp)
4302 .alg = "authenc(hmac(sha512),ctr(aes))",
4303 .test = alg_test_null,
4306 .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
4307 .test = alg_test_null,
4310 .alg = "blake2b-160",
4311 .test = alg_test_hash,
4314 .hash = __VECS(blake2b_160_tv_template)
4317 .alg = "blake2b-256",
4318 .test = alg_test_hash,
4321 .hash = __VECS(blake2b_256_tv_template)
4324 .alg = "blake2b-384",
4325 .test = alg_test_hash,
4328 .hash = __VECS(blake2b_384_tv_template)
4331 .alg = "blake2b-512",
4332 .test = alg_test_hash,
4335 .hash = __VECS(blake2b_512_tv_template)
4338 .alg = "blake2s-128",
4339 .test = alg_test_hash,
4341 .hash = __VECS(blakes2s_128_tv_template)
4344 .alg = "blake2s-160",
4345 .test = alg_test_hash,
4347 .hash = __VECS(blakes2s_160_tv_template)
4350 .alg = "blake2s-224",
4351 .test = alg_test_hash,
4353 .hash = __VECS(blakes2s_224_tv_template)
4356 .alg = "blake2s-256",
4357 .test = alg_test_hash,
4359 .hash = __VECS(blakes2s_256_tv_template)
4363 .test = alg_test_skcipher,
4366 .cipher = __VECS(aes_cbc_tv_template)
4369 .alg = "cbc(anubis)",
4370 .test = alg_test_skcipher,
4372 .cipher = __VECS(anubis_cbc_tv_template)
4375 .alg = "cbc(blowfish)",
4376 .test = alg_test_skcipher,
4378 .cipher = __VECS(bf_cbc_tv_template)
4381 .alg = "cbc(camellia)",
4382 .test = alg_test_skcipher,
4384 .cipher = __VECS(camellia_cbc_tv_template)
4387 .alg = "cbc(cast5)",
4388 .test = alg_test_skcipher,
4390 .cipher = __VECS(cast5_cbc_tv_template)
4393 .alg = "cbc(cast6)",
4394 .test = alg_test_skcipher,
4396 .cipher = __VECS(cast6_cbc_tv_template)
4400 .test = alg_test_skcipher,
4402 .cipher = __VECS(des_cbc_tv_template)
4405 .alg = "cbc(des3_ede)",
4406 .test = alg_test_skcipher,
4409 .cipher = __VECS(des3_ede_cbc_tv_template)
4412 /* Same as cbc(aes) except the key is stored in
4413 * hardware secure memory which we reference by index
4416 .test = alg_test_null,
4419 /* Same as cbc(sm4) except the key is stored in
4420 * hardware secure memory which we reference by index
4423 .test = alg_test_null,
4425 .alg = "cbc(serpent)",
4426 .test = alg_test_skcipher,
4428 .cipher = __VECS(serpent_cbc_tv_template)
4432 .test = alg_test_skcipher,
4434 .cipher = __VECS(sm4_cbc_tv_template)
4437 .alg = "cbc(twofish)",
4438 .test = alg_test_skcipher,
4440 .cipher = __VECS(tf_cbc_tv_template)
4443 #if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
4444 .alg = "cbc-paes-s390",
4446 .test = alg_test_skcipher,
4448 .cipher = __VECS(aes_cbc_tv_template)
4452 .alg = "cbcmac(aes)",
4454 .test = alg_test_hash,
4456 .hash = __VECS(aes_cbcmac_tv_template)
4460 .generic_driver = "ccm_base(ctr(aes-generic),cbcmac(aes-generic))",
4461 .test = alg_test_aead,
4465 ____VECS(aes_ccm_tv_template),
4466 .einval_allowed = 1,
4471 .test = alg_test_skcipher,
4474 .cipher = __VECS(aes_cfb_tv_template)
4478 .test = alg_test_skcipher,
4480 .cipher = __VECS(sm4_cfb_tv_template)
4484 .test = alg_test_skcipher,
4486 .cipher = __VECS(chacha20_tv_template)
4491 .test = alg_test_hash,
4493 .hash = __VECS(aes_cmac128_tv_template)
4496 .alg = "cmac(des3_ede)",
4498 .test = alg_test_hash,
4500 .hash = __VECS(des3_ede_cmac64_tv_template)
4503 .alg = "compress_null",
4504 .test = alg_test_null,
4507 .test = alg_test_hash,
4510 .hash = __VECS(crc32_tv_template)
4514 .test = alg_test_crc32c,
4517 .hash = __VECS(crc32c_tv_template)
4521 .test = alg_test_hash,
4524 .hash = __VECS(crct10dif_tv_template)
4528 .test = alg_test_skcipher,
4531 .cipher = __VECS(aes_ctr_tv_template)
4534 .alg = "ctr(blowfish)",
4535 .test = alg_test_skcipher,
4537 .cipher = __VECS(bf_ctr_tv_template)
4540 .alg = "ctr(camellia)",
4541 .test = alg_test_skcipher,
4543 .cipher = __VECS(camellia_ctr_tv_template)
4546 .alg = "ctr(cast5)",
4547 .test = alg_test_skcipher,
4549 .cipher = __VECS(cast5_ctr_tv_template)
4552 .alg = "ctr(cast6)",
4553 .test = alg_test_skcipher,
4555 .cipher = __VECS(cast6_ctr_tv_template)
4559 .test = alg_test_skcipher,
4561 .cipher = __VECS(des_ctr_tv_template)
4564 .alg = "ctr(des3_ede)",
4565 .test = alg_test_skcipher,
4568 .cipher = __VECS(des3_ede_ctr_tv_template)
4571 /* Same as ctr(aes) except the key is stored in
4572 * hardware secure memory which we reference by index
4575 .test = alg_test_null,
4579 /* Same as ctr(sm4) except the key is stored in
4580 * hardware secure memory which we reference by index
4583 .test = alg_test_null,
4585 .alg = "ctr(serpent)",
4586 .test = alg_test_skcipher,
4588 .cipher = __VECS(serpent_ctr_tv_template)
4592 .test = alg_test_skcipher,
4594 .cipher = __VECS(sm4_ctr_tv_template)
4597 .alg = "ctr(twofish)",
4598 .test = alg_test_skcipher,
4600 .cipher = __VECS(tf_ctr_tv_template)
4603 #if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
4604 .alg = "ctr-paes-s390",
4606 .test = alg_test_skcipher,
4608 .cipher = __VECS(aes_ctr_tv_template)
4612 .alg = "cts(cbc(aes))",
4613 .test = alg_test_skcipher,
4616 .cipher = __VECS(cts_mode_tv_template)
4619 /* Same as cts(cbc((aes)) except the key is stored in
4620 * hardware secure memory which we reference by index
4622 .alg = "cts(cbc(paes))",
4623 .test = alg_test_null,
4626 .alg = "curve25519",
4627 .test = alg_test_kpp,
4629 .kpp = __VECS(curve25519_tv_template)
4633 .test = alg_test_comp,
4637 .comp = __VECS(deflate_comp_tv_template),
4638 .decomp = __VECS(deflate_decomp_tv_template)
4643 .test = alg_test_kpp,
4646 .kpp = __VECS(dh_tv_template)
4649 .alg = "digest_null",
4650 .test = alg_test_null,
4652 .alg = "drbg_nopr_ctr_aes128",
4653 .test = alg_test_drbg,
4656 .drbg = __VECS(drbg_nopr_ctr_aes128_tv_template)
4659 .alg = "drbg_nopr_ctr_aes192",
4660 .test = alg_test_drbg,
4663 .drbg = __VECS(drbg_nopr_ctr_aes192_tv_template)
4666 .alg = "drbg_nopr_ctr_aes256",
4667 .test = alg_test_drbg,
4670 .drbg = __VECS(drbg_nopr_ctr_aes256_tv_template)
4674 * There is no need to specifically test the DRBG with every
4675 * backend cipher -- covered by drbg_nopr_hmac_sha256 test
4677 .alg = "drbg_nopr_hmac_sha1",
4679 .test = alg_test_null,
4681 .alg = "drbg_nopr_hmac_sha256",
4682 .test = alg_test_drbg,
4685 .drbg = __VECS(drbg_nopr_hmac_sha256_tv_template)
4688 /* covered by drbg_nopr_hmac_sha256 test */
4689 .alg = "drbg_nopr_hmac_sha384",
4691 .test = alg_test_null,
4693 .alg = "drbg_nopr_hmac_sha512",
4694 .test = alg_test_null,
4697 .alg = "drbg_nopr_sha1",
4699 .test = alg_test_null,
4701 .alg = "drbg_nopr_sha256",
4702 .test = alg_test_drbg,
4705 .drbg = __VECS(drbg_nopr_sha256_tv_template)
4708 /* covered by drbg_nopr_sha256 test */
4709 .alg = "drbg_nopr_sha384",
4711 .test = alg_test_null,
4713 .alg = "drbg_nopr_sha512",
4715 .test = alg_test_null,
4717 .alg = "drbg_pr_ctr_aes128",
4718 .test = alg_test_drbg,
4721 .drbg = __VECS(drbg_pr_ctr_aes128_tv_template)
4724 /* covered by drbg_pr_ctr_aes128 test */
4725 .alg = "drbg_pr_ctr_aes192",
4727 .test = alg_test_null,
4729 .alg = "drbg_pr_ctr_aes256",
4731 .test = alg_test_null,
4733 .alg = "drbg_pr_hmac_sha1",
4735 .test = alg_test_null,
4737 .alg = "drbg_pr_hmac_sha256",
4738 .test = alg_test_drbg,
4741 .drbg = __VECS(drbg_pr_hmac_sha256_tv_template)
4744 /* covered by drbg_pr_hmac_sha256 test */
4745 .alg = "drbg_pr_hmac_sha384",
4747 .test = alg_test_null,
4749 .alg = "drbg_pr_hmac_sha512",
4750 .test = alg_test_null,
4753 .alg = "drbg_pr_sha1",
4755 .test = alg_test_null,
4757 .alg = "drbg_pr_sha256",
4758 .test = alg_test_drbg,
4761 .drbg = __VECS(drbg_pr_sha256_tv_template)
4764 /* covered by drbg_pr_sha256 test */
4765 .alg = "drbg_pr_sha384",
4767 .test = alg_test_null,
4769 .alg = "drbg_pr_sha512",
4771 .test = alg_test_null,
4774 .test = alg_test_skcipher,
4777 .cipher = __VECS(aes_tv_template)
4780 .alg = "ecb(anubis)",
4781 .test = alg_test_skcipher,
4783 .cipher = __VECS(anubis_tv_template)
4787 .generic_driver = "ecb(arc4)-generic",
4788 .test = alg_test_skcipher,
4790 .cipher = __VECS(arc4_tv_template)
4793 .alg = "ecb(blowfish)",
4794 .test = alg_test_skcipher,
4796 .cipher = __VECS(bf_tv_template)
4799 .alg = "ecb(camellia)",
4800 .test = alg_test_skcipher,
4802 .cipher = __VECS(camellia_tv_template)
4805 .alg = "ecb(cast5)",
4806 .test = alg_test_skcipher,
4808 .cipher = __VECS(cast5_tv_template)
4811 .alg = "ecb(cast6)",
4812 .test = alg_test_skcipher,
4814 .cipher = __VECS(cast6_tv_template)
4817 .alg = "ecb(cipher_null)",
4818 .test = alg_test_null,
4822 .test = alg_test_skcipher,
4824 .cipher = __VECS(des_tv_template)
4827 .alg = "ecb(des3_ede)",
4828 .test = alg_test_skcipher,
4831 .cipher = __VECS(des3_ede_tv_template)
4834 .alg = "ecb(fcrypt)",
4835 .test = alg_test_skcipher,
4838 .vecs = fcrypt_pcbc_tv_template,
4843 .alg = "ecb(khazad)",
4844 .test = alg_test_skcipher,
4846 .cipher = __VECS(khazad_tv_template)
4849 /* Same as ecb(aes) except the key is stored in
4850 * hardware secure memory which we reference by index
4853 .test = alg_test_null,
4857 .test = alg_test_skcipher,
4859 .cipher = __VECS(seed_tv_template)
4862 .alg = "ecb(serpent)",
4863 .test = alg_test_skcipher,
4865 .cipher = __VECS(serpent_tv_template)
4869 .test = alg_test_skcipher,
4871 .cipher = __VECS(sm4_tv_template)
4875 .test = alg_test_skcipher,
4877 .cipher = __VECS(tea_tv_template)
4880 .alg = "ecb(tnepres)",
4881 .test = alg_test_skcipher,
4883 .cipher = __VECS(tnepres_tv_template)
4886 .alg = "ecb(twofish)",
4887 .test = alg_test_skcipher,
4889 .cipher = __VECS(tf_tv_template)
4893 .test = alg_test_skcipher,
4895 .cipher = __VECS(xeta_tv_template)
4899 .test = alg_test_skcipher,
4901 .cipher = __VECS(xtea_tv_template)
4904 #if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
4905 .alg = "ecb-paes-s390",
4907 .test = alg_test_skcipher,
4909 .cipher = __VECS(aes_tv_template)
4914 .test = alg_test_kpp,
4917 .kpp = __VECS(ecdh_tv_template)
4921 .test = alg_test_akcipher,
4923 .akcipher = __VECS(ecrdsa_tv_template)
4926 .alg = "essiv(authenc(hmac(sha256),cbc(aes)),sha256)",
4927 .test = alg_test_aead,
4930 .aead = __VECS(essiv_hmac_sha256_aes_cbc_tv_temp)
4933 .alg = "essiv(cbc(aes),sha256)",
4934 .test = alg_test_skcipher,
4937 .cipher = __VECS(essiv_aes_cbc_tv_template)
4941 .generic_driver = "gcm_base(ctr(aes-generic),ghash-generic)",
4942 .test = alg_test_aead,
4945 .aead = __VECS(aes_gcm_tv_template)
4949 .test = alg_test_hash,
4952 .hash = __VECS(ghash_tv_template)
4956 .test = alg_test_hash,
4958 .hash = __VECS(hmac_md5_tv_template)
4961 .alg = "hmac(rmd128)",
4962 .test = alg_test_hash,
4964 .hash = __VECS(hmac_rmd128_tv_template)
4967 .alg = "hmac(rmd160)",
4968 .test = alg_test_hash,
4970 .hash = __VECS(hmac_rmd160_tv_template)
4973 .alg = "hmac(sha1)",
4974 .test = alg_test_hash,
4977 .hash = __VECS(hmac_sha1_tv_template)
4980 .alg = "hmac(sha224)",
4981 .test = alg_test_hash,
4984 .hash = __VECS(hmac_sha224_tv_template)
4987 .alg = "hmac(sha256)",
4988 .test = alg_test_hash,
4991 .hash = __VECS(hmac_sha256_tv_template)
4994 .alg = "hmac(sha3-224)",
4995 .test = alg_test_hash,
4998 .hash = __VECS(hmac_sha3_224_tv_template)
5001 .alg = "hmac(sha3-256)",
5002 .test = alg_test_hash,
5005 .hash = __VECS(hmac_sha3_256_tv_template)
5008 .alg = "hmac(sha3-384)",
5009 .test = alg_test_hash,
5012 .hash = __VECS(hmac_sha3_384_tv_template)
5015 .alg = "hmac(sha3-512)",
5016 .test = alg_test_hash,
5019 .hash = __VECS(hmac_sha3_512_tv_template)
5022 .alg = "hmac(sha384)",
5023 .test = alg_test_hash,
5026 .hash = __VECS(hmac_sha384_tv_template)
5029 .alg = "hmac(sha512)",
5030 .test = alg_test_hash,
5033 .hash = __VECS(hmac_sha512_tv_template)
5037 .test = alg_test_hash,
5039 .hash = __VECS(hmac_sm3_tv_template)
5042 .alg = "hmac(streebog256)",
5043 .test = alg_test_hash,
5045 .hash = __VECS(hmac_streebog256_tv_template)
5048 .alg = "hmac(streebog512)",
5049 .test = alg_test_hash,
5051 .hash = __VECS(hmac_streebog512_tv_template)
5054 .alg = "jitterentropy_rng",
5056 .test = alg_test_null,
5059 .test = alg_test_skcipher,
5062 .cipher = __VECS(aes_kw_tv_template)
5066 .generic_driver = "lrw(ecb(aes-generic))",
5067 .test = alg_test_skcipher,
5069 .cipher = __VECS(aes_lrw_tv_template)
5072 .alg = "lrw(camellia)",
5073 .generic_driver = "lrw(ecb(camellia-generic))",
5074 .test = alg_test_skcipher,
5076 .cipher = __VECS(camellia_lrw_tv_template)
5079 .alg = "lrw(cast6)",
5080 .generic_driver = "lrw(ecb(cast6-generic))",
5081 .test = alg_test_skcipher,
5083 .cipher = __VECS(cast6_lrw_tv_template)
5086 .alg = "lrw(serpent)",
5087 .generic_driver = "lrw(ecb(serpent-generic))",
5088 .test = alg_test_skcipher,
5090 .cipher = __VECS(serpent_lrw_tv_template)
5093 .alg = "lrw(twofish)",
5094 .generic_driver = "lrw(ecb(twofish-generic))",
5095 .test = alg_test_skcipher,
5097 .cipher = __VECS(tf_lrw_tv_template)
5101 .test = alg_test_comp,
5105 .comp = __VECS(lz4_comp_tv_template),
5106 .decomp = __VECS(lz4_decomp_tv_template)
5111 .test = alg_test_comp,
5115 .comp = __VECS(lz4hc_comp_tv_template),
5116 .decomp = __VECS(lz4hc_decomp_tv_template)
5121 .test = alg_test_comp,
5125 .comp = __VECS(lzo_comp_tv_template),
5126 .decomp = __VECS(lzo_decomp_tv_template)
5131 .test = alg_test_comp,
5135 .comp = __VECS(lzorle_comp_tv_template),
5136 .decomp = __VECS(lzorle_decomp_tv_template)
5141 .test = alg_test_hash,
5143 .hash = __VECS(md4_tv_template)
5147 .test = alg_test_hash,
5149 .hash = __VECS(md5_tv_template)
5152 .alg = "michael_mic",
5153 .test = alg_test_hash,
5155 .hash = __VECS(michael_mic_tv_template)
5158 .alg = "nhpoly1305",
5159 .test = alg_test_hash,
5161 .hash = __VECS(nhpoly1305_tv_template)
5165 .test = alg_test_skcipher,
5168 .cipher = __VECS(aes_ofb_tv_template)
5171 /* Same as ofb(aes) except the key is stored in
5172 * hardware secure memory which we reference by index
5175 .test = alg_test_null,
5179 .test = alg_test_skcipher,
5181 .cipher = __VECS(sm4_ofb_tv_template)
5184 .alg = "pcbc(fcrypt)",
5185 .test = alg_test_skcipher,
5187 .cipher = __VECS(fcrypt_pcbc_tv_template)
5190 .alg = "pkcs1pad(rsa,sha224)",
5191 .test = alg_test_null,
5194 .alg = "pkcs1pad(rsa,sha256)",
5195 .test = alg_test_akcipher,
5198 .akcipher = __VECS(pkcs1pad_rsa_tv_template)
5201 .alg = "pkcs1pad(rsa,sha384)",
5202 .test = alg_test_null,
5205 .alg = "pkcs1pad(rsa,sha512)",
5206 .test = alg_test_null,
5210 .test = alg_test_hash,
5212 .hash = __VECS(poly1305_tv_template)
5215 .alg = "rfc3686(ctr(aes))",
5216 .test = alg_test_skcipher,
5219 .cipher = __VECS(aes_ctr_rfc3686_tv_template)
5222 .alg = "rfc3686(ctr(sm4))",
5223 .test = alg_test_skcipher,
5225 .cipher = __VECS(sm4_ctr_rfc3686_tv_template)
5228 .alg = "rfc4106(gcm(aes))",
5229 .generic_driver = "rfc4106(gcm_base(ctr(aes-generic),ghash-generic))",
5230 .test = alg_test_aead,
5234 ____VECS(aes_gcm_rfc4106_tv_template),
5235 .einval_allowed = 1,
5240 .alg = "rfc4309(ccm(aes))",
5241 .generic_driver = "rfc4309(ccm_base(ctr(aes-generic),cbcmac(aes-generic)))",
5242 .test = alg_test_aead,
5246 ____VECS(aes_ccm_rfc4309_tv_template),
5247 .einval_allowed = 1,
5252 .alg = "rfc4543(gcm(aes))",
5253 .generic_driver = "rfc4543(gcm_base(ctr(aes-generic),ghash-generic))",
5254 .test = alg_test_aead,
5257 ____VECS(aes_gcm_rfc4543_tv_template),
5258 .einval_allowed = 1,
5263 .alg = "rfc7539(chacha20,poly1305)",
5264 .test = alg_test_aead,
5266 .aead = __VECS(rfc7539_tv_template)
5269 .alg = "rfc7539esp(chacha20,poly1305)",
5270 .test = alg_test_aead,
5273 ____VECS(rfc7539esp_tv_template),
5274 .einval_allowed = 1,
5280 .test = alg_test_hash,
5282 .hash = __VECS(rmd128_tv_template)
5286 .test = alg_test_hash,
5288 .hash = __VECS(rmd160_tv_template)
5292 .test = alg_test_hash,
5294 .hash = __VECS(rmd256_tv_template)
5298 .test = alg_test_hash,
5300 .hash = __VECS(rmd320_tv_template)
5304 .test = alg_test_akcipher,
5307 .akcipher = __VECS(rsa_tv_template)
5311 .test = alg_test_skcipher,
5313 .cipher = __VECS(salsa20_stream_tv_template)
5317 .test = alg_test_hash,
5320 .hash = __VECS(sha1_tv_template)
5324 .test = alg_test_hash,
5327 .hash = __VECS(sha224_tv_template)
5331 .test = alg_test_hash,
5334 .hash = __VECS(sha256_tv_template)
5338 .test = alg_test_hash,
5341 .hash = __VECS(sha3_224_tv_template)
5345 .test = alg_test_hash,
5348 .hash = __VECS(sha3_256_tv_template)
5352 .test = alg_test_hash,
5355 .hash = __VECS(sha3_384_tv_template)
5359 .test = alg_test_hash,
5362 .hash = __VECS(sha3_512_tv_template)
5366 .test = alg_test_hash,
5369 .hash = __VECS(sha384_tv_template)
5373 .test = alg_test_hash,
5376 .hash = __VECS(sha512_tv_template)
5380 .test = alg_test_akcipher,
5382 .akcipher = __VECS(sm2_tv_template)
5386 .test = alg_test_hash,
5388 .hash = __VECS(sm3_tv_template)
5391 .alg = "streebog256",
5392 .test = alg_test_hash,
5394 .hash = __VECS(streebog256_tv_template)
5397 .alg = "streebog512",
5398 .test = alg_test_hash,
5400 .hash = __VECS(streebog512_tv_template)
5404 .test = alg_test_hash,
5406 .hash = __VECS(tgr128_tv_template)
5410 .test = alg_test_hash,
5412 .hash = __VECS(tgr160_tv_template)
5416 .test = alg_test_hash,
5418 .hash = __VECS(tgr192_tv_template)
5421 .alg = "vmac64(aes)",
5422 .test = alg_test_hash,
5424 .hash = __VECS(vmac64_aes_tv_template)
5428 .test = alg_test_hash,
5430 .hash = __VECS(wp256_tv_template)
5434 .test = alg_test_hash,
5436 .hash = __VECS(wp384_tv_template)
5440 .test = alg_test_hash,
5442 .hash = __VECS(wp512_tv_template)
5446 .test = alg_test_hash,
5448 .hash = __VECS(aes_xcbc128_tv_template)
5452 .test = alg_test_skcipher,
5454 .cipher = __VECS(xchacha12_tv_template)
5458 .test = alg_test_skcipher,
5460 .cipher = __VECS(xchacha20_tv_template)
5464 .generic_driver = "xts(ecb(aes-generic))",
5465 .test = alg_test_skcipher,
5468 .cipher = __VECS(aes_xts_tv_template)
5471 .alg = "xts(camellia)",
5472 .generic_driver = "xts(ecb(camellia-generic))",
5473 .test = alg_test_skcipher,
5475 .cipher = __VECS(camellia_xts_tv_template)
5478 .alg = "xts(cast6)",
5479 .generic_driver = "xts(ecb(cast6-generic))",
5480 .test = alg_test_skcipher,
5482 .cipher = __VECS(cast6_xts_tv_template)
5485 /* Same as xts(aes) except the key is stored in
5486 * hardware secure memory which we reference by index
5489 .test = alg_test_null,
5492 .alg = "xts(serpent)",
5493 .generic_driver = "xts(ecb(serpent-generic))",
5494 .test = alg_test_skcipher,
5496 .cipher = __VECS(serpent_xts_tv_template)
5499 .alg = "xts(twofish)",
5500 .generic_driver = "xts(ecb(twofish-generic))",
5501 .test = alg_test_skcipher,
5503 .cipher = __VECS(tf_xts_tv_template)
5506 #if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
5507 .alg = "xts-paes-s390",
5509 .test = alg_test_skcipher,
5511 .cipher = __VECS(aes_xts_tv_template)
5515 .alg = "xts4096(paes)",
5516 .test = alg_test_null,
5519 .alg = "xts512(paes)",
5520 .test = alg_test_null,
5524 .test = alg_test_hash,
5527 .hash = __VECS(xxhash64_tv_template)
5530 .alg = "zlib-deflate",
5531 .test = alg_test_comp,
5535 .comp = __VECS(zlib_deflate_comp_tv_template),
5536 .decomp = __VECS(zlib_deflate_decomp_tv_template)
5541 .test = alg_test_comp,
5545 .comp = __VECS(zstd_comp_tv_template),
5546 .decomp = __VECS(zstd_decomp_tv_template)
5552 static void alg_check_test_descs_order(void)
5556 for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
5557 int diff = strcmp(alg_test_descs[i - 1].alg,
5558 alg_test_descs[i].alg);
5560 if (WARN_ON(diff > 0)) {
5561 pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
5562 alg_test_descs[i - 1].alg,
5563 alg_test_descs[i].alg);
5566 if (WARN_ON(diff == 0)) {
5567 pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
5568 alg_test_descs[i].alg);
5573 static void alg_check_testvec_configs(void)
5577 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++)
5578 WARN_ON(!valid_testvec_config(
5579 &default_cipher_testvec_configs[i]));
5581 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++)
5582 WARN_ON(!valid_testvec_config(
5583 &default_hash_testvec_configs[i]));
5586 static void testmgr_onetime_init(void)
5588 alg_check_test_descs_order();
5589 alg_check_testvec_configs();
5591 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
5592 pr_warn("alg: extra crypto tests enabled. This is intended for developer use only.\n");
5596 static int alg_find_test(const char *alg)
5599 int end = ARRAY_SIZE(alg_test_descs);
5601 while (start < end) {
5602 int i = (start + end) / 2;
5603 int diff = strcmp(alg_test_descs[i].alg, alg);
5621 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
5627 if (!fips_enabled && notests) {
5628 printk_once(KERN_INFO "alg: self-tests disabled\n");
5632 DO_ONCE(testmgr_onetime_init);
5634 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
5635 char nalg[CRYPTO_MAX_ALG_NAME];
5637 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
5639 return -ENAMETOOLONG;
5641 i = alg_find_test(nalg);
5645 if (fips_enabled && !alg_test_descs[i].fips_allowed)
5648 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
5652 i = alg_find_test(alg);
5653 j = alg_find_test(driver);
5657 if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
5658 (j >= 0 && !alg_test_descs[j].fips_allowed)))
5663 rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
5665 if (j >= 0 && j != i)
5666 rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
5671 if (fips_enabled || panic_on_fail) {
5673 panic("alg: self-tests for %s (%s) failed in %s mode!\n",
5675 fips_enabled ? "fips" : "panic_on_fail");
5677 WARN(1, "alg: self-tests for %s (%s) failed (rc=%d)",
5681 pr_info("alg: self-tests for %s (%s) passed\n",
5688 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
5694 #endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
5696 EXPORT_SYMBOL_GPL(alg_test);