2 * Algorithm testing framework and tests.
6 * Copyright (c) 2007 Nokia Siemens Networks
8 * Copyright (c) 2019 Google LLC
10 * Updated RFC4106 AES-GCM testing.
15 * Copyright (c) 2010, Intel Corporation.
17 * This program is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the Free
19 * Software Foundation; either version 2 of the License, or (at your option)
24 #include <crypto/aead.h>
25 #include <crypto/hash.h>
26 #include <crypto/skcipher.h>
27 #include <linux/err.h>
28 #include <linux/fips.h>
29 #include <linux/module.h>
30 #include <linux/once.h>
31 #include <linux/random.h>
32 #include <linux/scatterlist.h>
33 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <crypto/rng.h>
36 #include <crypto/drbg.h>
37 #include <crypto/akcipher.h>
38 #include <crypto/kpp.h>
39 #include <crypto/acompress.h>
40 #include <crypto/internal/simd.h>
45 module_param(notests, bool, 0644);
46 MODULE_PARM_DESC(notests, "disable crypto self-tests");
48 static bool panic_on_fail;
49 module_param(panic_on_fail, bool, 0444);
51 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
52 static bool noextratests;
53 module_param(noextratests, bool, 0644);
54 MODULE_PARM_DESC(noextratests, "disable expensive crypto self-tests");
56 static unsigned int fuzz_iterations = 100;
57 module_param(fuzz_iterations, uint, 0644);
58 MODULE_PARM_DESC(fuzz_iterations, "number of fuzz test iterations");
60 DEFINE_PER_CPU(bool, crypto_simd_disabled_for_test);
61 EXPORT_PER_CPU_SYMBOL_GPL(crypto_simd_disabled_for_test);
64 #ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
67 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
77 * Need slab memory for testing (size in number of pages).
82 * Used by test_cipher()
87 struct aead_test_suite {
88 const struct aead_testvec *vecs;
92 struct cipher_test_suite {
93 const struct cipher_testvec *vecs;
97 struct comp_test_suite {
99 const struct comp_testvec *vecs;
104 struct hash_test_suite {
105 const struct hash_testvec *vecs;
109 struct cprng_test_suite {
110 const struct cprng_testvec *vecs;
114 struct drbg_test_suite {
115 const struct drbg_testvec *vecs;
119 struct akcipher_test_suite {
120 const struct akcipher_testvec *vecs;
124 struct kpp_test_suite {
125 const struct kpp_testvec *vecs;
129 struct alg_test_desc {
131 const char *generic_driver;
132 int (*test)(const struct alg_test_desc *desc, const char *driver,
134 int fips_allowed; /* set if alg is allowed in fips mode */
137 struct aead_test_suite aead;
138 struct cipher_test_suite cipher;
139 struct comp_test_suite comp;
140 struct hash_test_suite hash;
141 struct cprng_test_suite cprng;
142 struct drbg_test_suite drbg;
143 struct akcipher_test_suite akcipher;
144 struct kpp_test_suite kpp;
148 static void hexdump(unsigned char *buf, unsigned int len)
150 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
155 static int __testmgr_alloc_buf(char *buf[XBUFSIZE], int order)
159 for (i = 0; i < XBUFSIZE; i++) {
160 buf[i] = (char *)__get_free_pages(GFP_KERNEL, order);
169 free_pages((unsigned long)buf[i], order);
174 static int testmgr_alloc_buf(char *buf[XBUFSIZE])
176 return __testmgr_alloc_buf(buf, 0);
179 static void __testmgr_free_buf(char *buf[XBUFSIZE], int order)
183 for (i = 0; i < XBUFSIZE; i++)
184 free_pages((unsigned long)buf[i], order);
187 static void testmgr_free_buf(char *buf[XBUFSIZE])
189 __testmgr_free_buf(buf, 0);
192 #define TESTMGR_POISON_BYTE 0xfe
193 #define TESTMGR_POISON_LEN 16
195 static inline void testmgr_poison(void *addr, size_t len)
197 memset(addr, TESTMGR_POISON_BYTE, len);
200 /* Is the memory region still fully poisoned? */
201 static inline bool testmgr_is_poison(const void *addr, size_t len)
203 return memchr_inv(addr, TESTMGR_POISON_BYTE, len) == NULL;
206 /* flush type for hash algorithms */
208 /* merge with update of previous buffer(s) */
211 /* update with previous buffer(s) before doing this one */
214 /* likewise, but also export and re-import the intermediate state */
218 /* finalization function for hash algorithms */
219 enum finalization_type {
220 FINALIZATION_TYPE_FINAL, /* use final() */
221 FINALIZATION_TYPE_FINUP, /* use finup() */
222 FINALIZATION_TYPE_DIGEST, /* use digest() */
225 #define TEST_SG_TOTAL 10000
228 * struct test_sg_division - description of a scatterlist entry
230 * This struct describes one entry of a scatterlist being constructed to check a
231 * crypto test vector.
233 * @proportion_of_total: length of this chunk relative to the total length,
234 * given as a proportion out of TEST_SG_TOTAL so that it
235 * scales to fit any test vector
236 * @offset: byte offset into a 2-page buffer at which this chunk will start
237 * @offset_relative_to_alignmask: if true, add the algorithm's alignmask to the
239 * @flush_type: for hashes, whether an update() should be done now vs.
240 * continuing to accumulate data
241 * @nosimd: if doing the pending update(), do it with SIMD disabled?
243 struct test_sg_division {
244 unsigned int proportion_of_total;
246 bool offset_relative_to_alignmask;
247 enum flush_type flush_type;
252 * struct testvec_config - configuration for testing a crypto test vector
254 * This struct describes the data layout and other parameters with which each
255 * crypto test vector can be tested.
257 * @name: name of this config, logged for debugging purposes if a test fails
258 * @inplace: operate on the data in-place, if applicable for the algorithm type?
259 * @req_flags: extra request_flags, e.g. CRYPTO_TFM_REQ_MAY_SLEEP
260 * @src_divs: description of how to arrange the source scatterlist
261 * @dst_divs: description of how to arrange the dst scatterlist, if applicable
262 * for the algorithm type. Defaults to @src_divs if unset.
263 * @iv_offset: misalignment of the IV in the range [0..MAX_ALGAPI_ALIGNMASK+1],
264 * where 0 is aligned to a 2*(MAX_ALGAPI_ALIGNMASK+1) byte boundary
265 * @iv_offset_relative_to_alignmask: if true, add the algorithm's alignmask to
267 * @finalization_type: what finalization function to use for hashes
268 * @nosimd: execute with SIMD disabled? Requires !CRYPTO_TFM_REQ_MAY_SLEEP.
270 struct testvec_config {
274 struct test_sg_division src_divs[XBUFSIZE];
275 struct test_sg_division dst_divs[XBUFSIZE];
276 unsigned int iv_offset;
277 bool iv_offset_relative_to_alignmask;
278 enum finalization_type finalization_type;
282 #define TESTVEC_CONFIG_NAMELEN 192
285 * The following are the lists of testvec_configs to test for each algorithm
286 * type when the basic crypto self-tests are enabled, i.e. when
287 * CONFIG_CRYPTO_MANAGER_DISABLE_TESTS is unset. They aim to provide good test
288 * coverage, while keeping the test time much shorter than the full fuzz tests
289 * so that the basic tests can be enabled in a wider range of circumstances.
292 /* Configs for skciphers and aeads */
293 static const struct testvec_config default_cipher_testvec_configs[] = {
297 .src_divs = { { .proportion_of_total = 10000 } },
299 .name = "out-of-place",
300 .src_divs = { { .proportion_of_total = 10000 } },
302 .name = "unaligned buffer, offset=1",
303 .src_divs = { { .proportion_of_total = 10000, .offset = 1 } },
306 .name = "buffer aligned only to alignmask",
309 .proportion_of_total = 10000,
311 .offset_relative_to_alignmask = true,
315 .iv_offset_relative_to_alignmask = true,
317 .name = "two even aligned splits",
319 { .proportion_of_total = 5000 },
320 { .proportion_of_total = 5000 },
323 .name = "uneven misaligned splits, may sleep",
324 .req_flags = CRYPTO_TFM_REQ_MAY_SLEEP,
326 { .proportion_of_total = 1900, .offset = 33 },
327 { .proportion_of_total = 3300, .offset = 7 },
328 { .proportion_of_total = 4800, .offset = 18 },
332 .name = "misaligned splits crossing pages, inplace",
336 .proportion_of_total = 7500,
337 .offset = PAGE_SIZE - 32
339 .proportion_of_total = 2500,
340 .offset = PAGE_SIZE - 7
346 static const struct testvec_config default_hash_testvec_configs[] = {
348 .name = "init+update+final aligned buffer",
349 .src_divs = { { .proportion_of_total = 10000 } },
350 .finalization_type = FINALIZATION_TYPE_FINAL,
352 .name = "init+finup aligned buffer",
353 .src_divs = { { .proportion_of_total = 10000 } },
354 .finalization_type = FINALIZATION_TYPE_FINUP,
356 .name = "digest aligned buffer",
357 .src_divs = { { .proportion_of_total = 10000 } },
358 .finalization_type = FINALIZATION_TYPE_DIGEST,
360 .name = "init+update+final misaligned buffer",
361 .src_divs = { { .proportion_of_total = 10000, .offset = 1 } },
362 .finalization_type = FINALIZATION_TYPE_FINAL,
364 .name = "digest buffer aligned only to alignmask",
367 .proportion_of_total = 10000,
369 .offset_relative_to_alignmask = true,
372 .finalization_type = FINALIZATION_TYPE_DIGEST,
374 .name = "init+update+update+final two even splits",
376 { .proportion_of_total = 5000 },
378 .proportion_of_total = 5000,
379 .flush_type = FLUSH_TYPE_FLUSH,
382 .finalization_type = FINALIZATION_TYPE_FINAL,
384 .name = "digest uneven misaligned splits, may sleep",
385 .req_flags = CRYPTO_TFM_REQ_MAY_SLEEP,
387 { .proportion_of_total = 1900, .offset = 33 },
388 { .proportion_of_total = 3300, .offset = 7 },
389 { .proportion_of_total = 4800, .offset = 18 },
391 .finalization_type = FINALIZATION_TYPE_DIGEST,
393 .name = "digest misaligned splits crossing pages",
396 .proportion_of_total = 7500,
397 .offset = PAGE_SIZE - 32,
399 .proportion_of_total = 2500,
400 .offset = PAGE_SIZE - 7,
403 .finalization_type = FINALIZATION_TYPE_DIGEST,
405 .name = "import/export",
408 .proportion_of_total = 6500,
409 .flush_type = FLUSH_TYPE_REIMPORT,
411 .proportion_of_total = 3500,
412 .flush_type = FLUSH_TYPE_REIMPORT,
415 .finalization_type = FINALIZATION_TYPE_FINAL,
419 static unsigned int count_test_sg_divisions(const struct test_sg_division *divs)
421 unsigned int remaining = TEST_SG_TOTAL;
422 unsigned int ndivs = 0;
425 remaining -= divs[ndivs++].proportion_of_total;
431 #define SGDIVS_HAVE_FLUSHES BIT(0)
432 #define SGDIVS_HAVE_NOSIMD BIT(1)
434 static bool valid_sg_divisions(const struct test_sg_division *divs,
435 unsigned int count, int *flags_ret)
437 unsigned int total = 0;
440 for (i = 0; i < count && total != TEST_SG_TOTAL; i++) {
441 if (divs[i].proportion_of_total <= 0 ||
442 divs[i].proportion_of_total > TEST_SG_TOTAL - total)
444 total += divs[i].proportion_of_total;
445 if (divs[i].flush_type != FLUSH_TYPE_NONE)
446 *flags_ret |= SGDIVS_HAVE_FLUSHES;
448 *flags_ret |= SGDIVS_HAVE_NOSIMD;
450 return total == TEST_SG_TOTAL &&
451 memchr_inv(&divs[i], 0, (count - i) * sizeof(divs[0])) == NULL;
455 * Check whether the given testvec_config is valid. This isn't strictly needed
456 * since every testvec_config should be valid, but check anyway so that people
457 * don't unknowingly add broken configs that don't do what they wanted.
459 static bool valid_testvec_config(const struct testvec_config *cfg)
463 if (cfg->name == NULL)
466 if (!valid_sg_divisions(cfg->src_divs, ARRAY_SIZE(cfg->src_divs),
470 if (cfg->dst_divs[0].proportion_of_total) {
471 if (!valid_sg_divisions(cfg->dst_divs,
472 ARRAY_SIZE(cfg->dst_divs), &flags))
475 if (memchr_inv(cfg->dst_divs, 0, sizeof(cfg->dst_divs)))
477 /* defaults to dst_divs=src_divs */
481 (cfg->iv_offset_relative_to_alignmask ? MAX_ALGAPI_ALIGNMASK : 0) >
482 MAX_ALGAPI_ALIGNMASK + 1)
485 if ((flags & (SGDIVS_HAVE_FLUSHES | SGDIVS_HAVE_NOSIMD)) &&
486 cfg->finalization_type == FINALIZATION_TYPE_DIGEST)
489 if ((cfg->nosimd || (flags & SGDIVS_HAVE_NOSIMD)) &&
490 (cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP))
497 char *bufs[XBUFSIZE];
498 struct scatterlist sgl[XBUFSIZE];
499 struct scatterlist sgl_saved[XBUFSIZE];
500 struct scatterlist *sgl_ptr;
504 static int init_test_sglist(struct test_sglist *tsgl)
506 return __testmgr_alloc_buf(tsgl->bufs, 1 /* two pages per buffer */);
509 static void destroy_test_sglist(struct test_sglist *tsgl)
511 return __testmgr_free_buf(tsgl->bufs, 1 /* two pages per buffer */);
515 * build_test_sglist() - build a scatterlist for a crypto test
517 * @tsgl: the scatterlist to build. @tsgl->bufs[] contains an array of 2-page
518 * buffers which the scatterlist @tsgl->sgl[] will be made to point into.
519 * @divs: the layout specification on which the scatterlist will be based
520 * @alignmask: the algorithm's alignmask
521 * @total_len: the total length of the scatterlist to build in bytes
522 * @data: if non-NULL, the buffers will be filled with this data until it ends.
523 * Otherwise the buffers will be poisoned. In both cases, some bytes
524 * past the end of each buffer will be poisoned to help detect overruns.
525 * @out_divs: if non-NULL, the test_sg_division to which each scatterlist entry
526 * corresponds will be returned here. This will match @divs except
527 * that divisions resolving to a length of 0 are omitted as they are
528 * not included in the scatterlist.
530 * Return: 0 or a -errno value
532 static int build_test_sglist(struct test_sglist *tsgl,
533 const struct test_sg_division *divs,
534 const unsigned int alignmask,
535 const unsigned int total_len,
536 struct iov_iter *data,
537 const struct test_sg_division *out_divs[XBUFSIZE])
540 const struct test_sg_division *div;
542 } partitions[XBUFSIZE];
543 const unsigned int ndivs = count_test_sg_divisions(divs);
544 unsigned int len_remaining = total_len;
547 BUILD_BUG_ON(ARRAY_SIZE(partitions) != ARRAY_SIZE(tsgl->sgl));
548 if (WARN_ON(ndivs > ARRAY_SIZE(partitions)))
551 /* Calculate the (div, length) pairs */
553 for (i = 0; i < ndivs; i++) {
554 unsigned int len_this_sg =
556 (total_len * divs[i].proportion_of_total +
557 TEST_SG_TOTAL / 2) / TEST_SG_TOTAL);
559 if (len_this_sg != 0) {
560 partitions[tsgl->nents].div = &divs[i];
561 partitions[tsgl->nents].length = len_this_sg;
563 len_remaining -= len_this_sg;
566 if (tsgl->nents == 0) {
567 partitions[tsgl->nents].div = &divs[0];
568 partitions[tsgl->nents].length = 0;
571 partitions[tsgl->nents - 1].length += len_remaining;
573 /* Set up the sgl entries and fill the data or poison */
574 sg_init_table(tsgl->sgl, tsgl->nents);
575 for (i = 0; i < tsgl->nents; i++) {
576 unsigned int offset = partitions[i].div->offset;
579 if (partitions[i].div->offset_relative_to_alignmask)
582 while (offset + partitions[i].length + TESTMGR_POISON_LEN >
584 if (WARN_ON(offset <= 0))
589 addr = &tsgl->bufs[i][offset];
590 sg_set_buf(&tsgl->sgl[i], addr, partitions[i].length);
593 out_divs[i] = partitions[i].div;
596 size_t copy_len, copied;
598 copy_len = min(partitions[i].length, data->count);
599 copied = copy_from_iter(addr, copy_len, data);
600 if (WARN_ON(copied != copy_len))
602 testmgr_poison(addr + copy_len, partitions[i].length +
603 TESTMGR_POISON_LEN - copy_len);
605 testmgr_poison(addr, partitions[i].length +
610 sg_mark_end(&tsgl->sgl[tsgl->nents - 1]);
611 tsgl->sgl_ptr = tsgl->sgl;
612 memcpy(tsgl->sgl_saved, tsgl->sgl, tsgl->nents * sizeof(tsgl->sgl[0]));
617 * Verify that a scatterlist crypto operation produced the correct output.
619 * @tsgl: scatterlist containing the actual output
620 * @expected_output: buffer containing the expected output
621 * @len_to_check: length of @expected_output in bytes
622 * @unchecked_prefix_len: number of ignored bytes in @tsgl prior to real result
623 * @check_poison: verify that the poison bytes after each chunk are intact?
625 * Return: 0 if correct, -EINVAL if incorrect, -EOVERFLOW if buffer overrun.
627 static int verify_correct_output(const struct test_sglist *tsgl,
628 const char *expected_output,
629 unsigned int len_to_check,
630 unsigned int unchecked_prefix_len,
635 for (i = 0; i < tsgl->nents; i++) {
636 struct scatterlist *sg = &tsgl->sgl_ptr[i];
637 unsigned int len = sg->length;
638 unsigned int offset = sg->offset;
639 const char *actual_output;
641 if (unchecked_prefix_len) {
642 if (unchecked_prefix_len >= len) {
643 unchecked_prefix_len -= len;
646 offset += unchecked_prefix_len;
647 len -= unchecked_prefix_len;
648 unchecked_prefix_len = 0;
650 len = min(len, len_to_check);
651 actual_output = page_address(sg_page(sg)) + offset;
652 if (memcmp(expected_output, actual_output, len) != 0)
655 !testmgr_is_poison(actual_output + len, TESTMGR_POISON_LEN))
658 expected_output += len;
660 if (WARN_ON(len_to_check != 0))
665 static bool is_test_sglist_corrupted(const struct test_sglist *tsgl)
669 for (i = 0; i < tsgl->nents; i++) {
670 if (tsgl->sgl[i].page_link != tsgl->sgl_saved[i].page_link)
672 if (tsgl->sgl[i].offset != tsgl->sgl_saved[i].offset)
674 if (tsgl->sgl[i].length != tsgl->sgl_saved[i].length)
680 struct cipher_test_sglists {
681 struct test_sglist src;
682 struct test_sglist dst;
685 static struct cipher_test_sglists *alloc_cipher_test_sglists(void)
687 struct cipher_test_sglists *tsgls;
689 tsgls = kmalloc(sizeof(*tsgls), GFP_KERNEL);
693 if (init_test_sglist(&tsgls->src) != 0)
695 if (init_test_sglist(&tsgls->dst) != 0)
696 goto fail_destroy_src;
701 destroy_test_sglist(&tsgls->src);
707 static void free_cipher_test_sglists(struct cipher_test_sglists *tsgls)
710 destroy_test_sglist(&tsgls->src);
711 destroy_test_sglist(&tsgls->dst);
716 /* Build the src and dst scatterlists for an skcipher or AEAD test */
717 static int build_cipher_test_sglists(struct cipher_test_sglists *tsgls,
718 const struct testvec_config *cfg,
719 unsigned int alignmask,
720 unsigned int src_total_len,
721 unsigned int dst_total_len,
722 const struct kvec *inputs,
723 unsigned int nr_inputs)
725 struct iov_iter input;
728 iov_iter_kvec(&input, WRITE, inputs, nr_inputs, src_total_len);
729 err = build_test_sglist(&tsgls->src, cfg->src_divs, alignmask,
731 max(dst_total_len, src_total_len) :
738 tsgls->dst.sgl_ptr = tsgls->src.sgl;
739 tsgls->dst.nents = tsgls->src.nents;
742 return build_test_sglist(&tsgls->dst,
743 cfg->dst_divs[0].proportion_of_total ?
744 cfg->dst_divs : cfg->src_divs,
745 alignmask, dst_total_len, NULL, NULL);
748 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
750 /* Generate a random length in range [0, max_len], but prefer smaller values */
751 static unsigned int generate_random_length(unsigned int max_len)
753 unsigned int len = prandom_u32() % (max_len + 1);
755 switch (prandom_u32() % 4) {
767 /* Sometimes make some random changes to the given data buffer */
768 static void mutate_buffer(u8 *buf, size_t count)
774 /* Sometimes flip some bits */
775 if (prandom_u32() % 4 == 0) {
776 num_flips = min_t(size_t, 1 << (prandom_u32() % 8), count * 8);
777 for (i = 0; i < num_flips; i++) {
778 pos = prandom_u32() % (count * 8);
779 buf[pos / 8] ^= 1 << (pos % 8);
783 /* Sometimes flip some bytes */
784 if (prandom_u32() % 4 == 0) {
785 num_flips = min_t(size_t, 1 << (prandom_u32() % 8), count);
786 for (i = 0; i < num_flips; i++)
787 buf[prandom_u32() % count] ^= 0xff;
791 /* Randomly generate 'count' bytes, but sometimes make them "interesting" */
792 static void generate_random_bytes(u8 *buf, size_t count)
801 switch (prandom_u32() % 8) { /* Choose a generation strategy */
804 /* All the same byte, plus optional mutations */
805 switch (prandom_u32() % 4) {
813 b = (u8)prandom_u32();
816 memset(buf, b, count);
817 mutate_buffer(buf, count);
820 /* Ascending or descending bytes, plus optional mutations */
821 increment = (u8)prandom_u32();
822 b = (u8)prandom_u32();
823 for (i = 0; i < count; i++, b += increment)
825 mutate_buffer(buf, count);
828 /* Fully random bytes */
829 for (i = 0; i < count; i++)
830 buf[i] = (u8)prandom_u32();
834 static char *generate_random_sgl_divisions(struct test_sg_division *divs,
835 size_t max_divs, char *p, char *end,
836 bool gen_flushes, u32 req_flags)
838 struct test_sg_division *div = divs;
839 unsigned int remaining = TEST_SG_TOTAL;
842 unsigned int this_len;
843 const char *flushtype_str;
845 if (div == &divs[max_divs - 1] || prandom_u32() % 2 == 0)
846 this_len = remaining;
848 this_len = 1 + (prandom_u32() % remaining);
849 div->proportion_of_total = this_len;
851 if (prandom_u32() % 4 == 0)
852 div->offset = (PAGE_SIZE - 128) + (prandom_u32() % 128);
853 else if (prandom_u32() % 2 == 0)
854 div->offset = prandom_u32() % 32;
856 div->offset = prandom_u32() % PAGE_SIZE;
857 if (prandom_u32() % 8 == 0)
858 div->offset_relative_to_alignmask = true;
860 div->flush_type = FLUSH_TYPE_NONE;
862 switch (prandom_u32() % 4) {
864 div->flush_type = FLUSH_TYPE_REIMPORT;
867 div->flush_type = FLUSH_TYPE_FLUSH;
872 if (div->flush_type != FLUSH_TYPE_NONE &&
873 !(req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) &&
874 prandom_u32() % 2 == 0)
877 switch (div->flush_type) {
878 case FLUSH_TYPE_FLUSH:
880 flushtype_str = "<flush,nosimd>";
882 flushtype_str = "<flush>";
884 case FLUSH_TYPE_REIMPORT:
886 flushtype_str = "<reimport,nosimd>";
888 flushtype_str = "<reimport>";
895 BUILD_BUG_ON(TEST_SG_TOTAL != 10000); /* for "%u.%u%%" */
896 p += scnprintf(p, end - p, "%s%u.%u%%@%s+%u%s", flushtype_str,
897 this_len / 100, this_len % 100,
898 div->offset_relative_to_alignmask ?
900 div->offset, this_len == remaining ? "" : ", ");
901 remaining -= this_len;
908 /* Generate a random testvec_config for fuzz testing */
909 static void generate_random_testvec_config(struct testvec_config *cfg,
910 char *name, size_t max_namelen)
913 char * const end = name + max_namelen;
915 memset(cfg, 0, sizeof(*cfg));
919 p += scnprintf(p, end - p, "random:");
921 if (prandom_u32() % 2 == 0) {
923 p += scnprintf(p, end - p, " inplace");
926 if (prandom_u32() % 2 == 0) {
927 cfg->req_flags |= CRYPTO_TFM_REQ_MAY_SLEEP;
928 p += scnprintf(p, end - p, " may_sleep");
931 switch (prandom_u32() % 4) {
933 cfg->finalization_type = FINALIZATION_TYPE_FINAL;
934 p += scnprintf(p, end - p, " use_final");
937 cfg->finalization_type = FINALIZATION_TYPE_FINUP;
938 p += scnprintf(p, end - p, " use_finup");
941 cfg->finalization_type = FINALIZATION_TYPE_DIGEST;
942 p += scnprintf(p, end - p, " use_digest");
946 if (!(cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) &&
947 prandom_u32() % 2 == 0) {
949 p += scnprintf(p, end - p, " nosimd");
952 p += scnprintf(p, end - p, " src_divs=[");
953 p = generate_random_sgl_divisions(cfg->src_divs,
954 ARRAY_SIZE(cfg->src_divs), p, end,
955 (cfg->finalization_type !=
956 FINALIZATION_TYPE_DIGEST),
958 p += scnprintf(p, end - p, "]");
960 if (!cfg->inplace && prandom_u32() % 2 == 0) {
961 p += scnprintf(p, end - p, " dst_divs=[");
962 p = generate_random_sgl_divisions(cfg->dst_divs,
963 ARRAY_SIZE(cfg->dst_divs),
966 p += scnprintf(p, end - p, "]");
969 if (prandom_u32() % 2 == 0) {
970 cfg->iv_offset = 1 + (prandom_u32() % MAX_ALGAPI_ALIGNMASK);
971 p += scnprintf(p, end - p, " iv_offset=%u", cfg->iv_offset);
974 WARN_ON_ONCE(!valid_testvec_config(cfg));
977 static void crypto_disable_simd_for_test(void)
980 __this_cpu_write(crypto_simd_disabled_for_test, true);
983 static void crypto_reenable_simd_for_test(void)
985 __this_cpu_write(crypto_simd_disabled_for_test, false);
990 * Given an algorithm name, build the name of the generic implementation of that
991 * algorithm, assuming the usual naming convention. Specifically, this appends
992 * "-generic" to every part of the name that is not a template name. Examples:
995 * cbc(aes) => cbc(aes-generic)
996 * cts(cbc(aes)) => cts(cbc(aes-generic))
997 * rfc7539(chacha20,poly1305) => rfc7539(chacha20-generic,poly1305-generic)
999 * Return: 0 on success, or -ENAMETOOLONG if the generic name would be too long
1001 static int build_generic_driver_name(const char *algname,
1002 char driver_name[CRYPTO_MAX_ALG_NAME])
1004 const char *in = algname;
1005 char *out = driver_name;
1006 size_t len = strlen(algname);
1008 if (len >= CRYPTO_MAX_ALG_NAME)
1011 const char *in_saved = in;
1013 while (*in && *in != '(' && *in != ')' && *in != ',')
1015 if (*in != '(' && in > in_saved) {
1017 if (len >= CRYPTO_MAX_ALG_NAME)
1019 memcpy(out, "-generic", 8);
1022 } while ((*out++ = *in++) != '\0');
1026 pr_err("alg: generic driver name for \"%s\" would be too long\n",
1028 return -ENAMETOOLONG;
1030 #else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1031 static void crypto_disable_simd_for_test(void)
1035 static void crypto_reenable_simd_for_test(void)
1038 #endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1040 static int do_ahash_op(int (*op)(struct ahash_request *req),
1041 struct ahash_request *req,
1042 struct crypto_wait *wait, bool nosimd)
1047 crypto_disable_simd_for_test();
1052 crypto_reenable_simd_for_test();
1054 return crypto_wait_req(err, wait);
1057 static int check_nonfinal_hash_op(const char *op, int err,
1058 u8 *result, unsigned int digestsize,
1059 const char *driver, const char *vec_name,
1060 const struct testvec_config *cfg)
1063 pr_err("alg: hash: %s %s() failed with err %d on test vector %s, cfg=\"%s\"\n",
1064 driver, op, err, vec_name, cfg->name);
1067 if (!testmgr_is_poison(result, digestsize)) {
1068 pr_err("alg: hash: %s %s() used result buffer on test vector %s, cfg=\"%s\"\n",
1069 driver, op, vec_name, cfg->name);
1075 static int test_hash_vec_cfg(const char *driver,
1076 const struct hash_testvec *vec,
1077 const char *vec_name,
1078 const struct testvec_config *cfg,
1079 struct ahash_request *req,
1080 struct test_sglist *tsgl,
1083 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1084 const unsigned int alignmask = crypto_ahash_alignmask(tfm);
1085 const unsigned int digestsize = crypto_ahash_digestsize(tfm);
1086 const unsigned int statesize = crypto_ahash_statesize(tfm);
1087 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1088 const struct test_sg_division *divs[XBUFSIZE];
1089 DECLARE_CRYPTO_WAIT(wait);
1091 struct iov_iter input;
1093 struct scatterlist *pending_sgl;
1094 unsigned int pending_len;
1095 u8 result[HASH_MAX_DIGESTSIZE + TESTMGR_POISON_LEN];
1098 /* Set the key, if specified */
1100 err = crypto_ahash_setkey(tfm, vec->key, vec->ksize);
1102 if (err == vec->setkey_error)
1104 pr_err("alg: hash: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1105 driver, vec_name, vec->setkey_error, err,
1106 crypto_ahash_get_flags(tfm));
1109 if (vec->setkey_error) {
1110 pr_err("alg: hash: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1111 driver, vec_name, vec->setkey_error);
1116 /* Build the scatterlist for the source data */
1117 _input.iov_base = (void *)vec->plaintext;
1118 _input.iov_len = vec->psize;
1119 iov_iter_kvec(&input, WRITE, &_input, 1, vec->psize);
1120 err = build_test_sglist(tsgl, cfg->src_divs, alignmask, vec->psize,
1123 pr_err("alg: hash: %s: error preparing scatterlist for test vector %s, cfg=\"%s\"\n",
1124 driver, vec_name, cfg->name);
1128 /* Do the actual hashing */
1130 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
1131 testmgr_poison(result, digestsize + TESTMGR_POISON_LEN);
1133 if (cfg->finalization_type == FINALIZATION_TYPE_DIGEST ||
1134 vec->digest_error) {
1135 /* Just using digest() */
1136 ahash_request_set_callback(req, req_flags, crypto_req_done,
1138 ahash_request_set_crypt(req, tsgl->sgl, result, vec->psize);
1139 err = do_ahash_op(crypto_ahash_digest, req, &wait, cfg->nosimd);
1141 if (err == vec->digest_error)
1143 pr_err("alg: hash: %s digest() failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
1144 driver, vec_name, vec->digest_error, err,
1148 if (vec->digest_error) {
1149 pr_err("alg: hash: %s digest() unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
1150 driver, vec_name, vec->digest_error, cfg->name);
1156 /* Using init(), zero or more update(), then final() or finup() */
1158 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
1159 ahash_request_set_crypt(req, NULL, result, 0);
1160 err = do_ahash_op(crypto_ahash_init, req, &wait, cfg->nosimd);
1161 err = check_nonfinal_hash_op("init", err, result, digestsize,
1162 driver, vec_name, cfg);
1168 for (i = 0; i < tsgl->nents; i++) {
1169 if (divs[i]->flush_type != FLUSH_TYPE_NONE &&
1170 pending_sgl != NULL) {
1171 /* update() with the pending data */
1172 ahash_request_set_callback(req, req_flags,
1173 crypto_req_done, &wait);
1174 ahash_request_set_crypt(req, pending_sgl, result,
1176 err = do_ahash_op(crypto_ahash_update, req, &wait,
1178 err = check_nonfinal_hash_op("update", err,
1180 driver, vec_name, cfg);
1186 if (divs[i]->flush_type == FLUSH_TYPE_REIMPORT) {
1187 /* Test ->export() and ->import() */
1188 testmgr_poison(hashstate + statesize,
1189 TESTMGR_POISON_LEN);
1190 err = crypto_ahash_export(req, hashstate);
1191 err = check_nonfinal_hash_op("export", err,
1193 driver, vec_name, cfg);
1196 if (!testmgr_is_poison(hashstate + statesize,
1197 TESTMGR_POISON_LEN)) {
1198 pr_err("alg: hash: %s export() overran state buffer on test vector %s, cfg=\"%s\"\n",
1199 driver, vec_name, cfg->name);
1203 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
1204 err = crypto_ahash_import(req, hashstate);
1205 err = check_nonfinal_hash_op("import", err,
1207 driver, vec_name, cfg);
1211 if (pending_sgl == NULL)
1212 pending_sgl = &tsgl->sgl[i];
1213 pending_len += tsgl->sgl[i].length;
1216 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
1217 ahash_request_set_crypt(req, pending_sgl, result, pending_len);
1218 if (cfg->finalization_type == FINALIZATION_TYPE_FINAL) {
1219 /* finish with update() and final() */
1220 err = do_ahash_op(crypto_ahash_update, req, &wait, cfg->nosimd);
1221 err = check_nonfinal_hash_op("update", err, result, digestsize,
1222 driver, vec_name, cfg);
1225 err = do_ahash_op(crypto_ahash_final, req, &wait, cfg->nosimd);
1227 pr_err("alg: hash: %s final() failed with err %d on test vector %s, cfg=\"%s\"\n",
1228 driver, err, vec_name, cfg->name);
1232 /* finish with finup() */
1233 err = do_ahash_op(crypto_ahash_finup, req, &wait, cfg->nosimd);
1235 pr_err("alg: hash: %s finup() failed with err %d on test vector %s, cfg=\"%s\"\n",
1236 driver, err, vec_name, cfg->name);
1242 /* Check that the algorithm produced the correct digest */
1243 if (memcmp(result, vec->digest, digestsize) != 0) {
1244 pr_err("alg: hash: %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
1245 driver, vec_name, cfg->name);
1248 if (!testmgr_is_poison(&result[digestsize], TESTMGR_POISON_LEN)) {
1249 pr_err("alg: hash: %s overran result buffer on test vector %s, cfg=\"%s\"\n",
1250 driver, vec_name, cfg->name);
1257 static int test_hash_vec(const char *driver, const struct hash_testvec *vec,
1258 unsigned int vec_num, struct ahash_request *req,
1259 struct test_sglist *tsgl, u8 *hashstate)
1265 sprintf(vec_name, "%u", vec_num);
1267 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++) {
1268 err = test_hash_vec_cfg(driver, vec, vec_name,
1269 &default_hash_testvec_configs[i],
1270 req, tsgl, hashstate);
1275 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1276 if (!noextratests) {
1277 struct testvec_config cfg;
1278 char cfgname[TESTVEC_CONFIG_NAMELEN];
1280 for (i = 0; i < fuzz_iterations; i++) {
1281 generate_random_testvec_config(&cfg, cfgname,
1283 err = test_hash_vec_cfg(driver, vec, vec_name, &cfg,
1284 req, tsgl, hashstate);
1293 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1295 * Generate a hash test vector from the given implementation.
1296 * Assumes the buffers in 'vec' were already allocated.
1298 static void generate_random_hash_testvec(struct crypto_shash *tfm,
1299 struct hash_testvec *vec,
1300 unsigned int maxkeysize,
1301 unsigned int maxdatasize,
1302 char *name, size_t max_namelen)
1304 SHASH_DESC_ON_STACK(desc, tfm);
1307 vec->psize = generate_random_length(maxdatasize);
1308 generate_random_bytes((u8 *)vec->plaintext, vec->psize);
1311 * Key: length in range [1, maxkeysize], but usually choose maxkeysize.
1312 * If algorithm is unkeyed, then maxkeysize == 0 and set ksize = 0.
1314 vec->setkey_error = 0;
1317 vec->ksize = maxkeysize;
1318 if (prandom_u32() % 4 == 0)
1319 vec->ksize = 1 + (prandom_u32() % maxkeysize);
1320 generate_random_bytes((u8 *)vec->key, vec->ksize);
1322 vec->setkey_error = crypto_shash_setkey(tfm, vec->key,
1324 /* If the key couldn't be set, no need to continue to digest. */
1325 if (vec->setkey_error)
1331 vec->digest_error = crypto_shash_digest(desc, vec->plaintext,
1332 vec->psize, (u8 *)vec->digest);
1334 snprintf(name, max_namelen, "\"random: psize=%u ksize=%u\"",
1335 vec->psize, vec->ksize);
1339 * Test the hash algorithm represented by @req against the corresponding generic
1340 * implementation, if one is available.
1342 static int test_hash_vs_generic_impl(const char *driver,
1343 const char *generic_driver,
1344 unsigned int maxkeysize,
1345 struct ahash_request *req,
1346 struct test_sglist *tsgl,
1349 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1350 const unsigned int digestsize = crypto_ahash_digestsize(tfm);
1351 const unsigned int blocksize = crypto_ahash_blocksize(tfm);
1352 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
1353 const char *algname = crypto_hash_alg_common(tfm)->base.cra_name;
1354 char _generic_driver[CRYPTO_MAX_ALG_NAME];
1355 struct crypto_shash *generic_tfm = NULL;
1357 struct hash_testvec vec = { 0 };
1359 struct testvec_config cfg;
1360 char cfgname[TESTVEC_CONFIG_NAMELEN];
1366 if (!generic_driver) { /* Use default naming convention? */
1367 err = build_generic_driver_name(algname, _generic_driver);
1370 generic_driver = _generic_driver;
1373 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
1376 generic_tfm = crypto_alloc_shash(generic_driver, 0, 0);
1377 if (IS_ERR(generic_tfm)) {
1378 err = PTR_ERR(generic_tfm);
1379 if (err == -ENOENT) {
1380 pr_warn("alg: hash: skipping comparison tests for %s because %s is unavailable\n",
1381 driver, generic_driver);
1384 pr_err("alg: hash: error allocating %s (generic impl of %s): %d\n",
1385 generic_driver, algname, err);
1389 /* Check the algorithm properties for consistency. */
1391 if (digestsize != crypto_shash_digestsize(generic_tfm)) {
1392 pr_err("alg: hash: digestsize for %s (%u) doesn't match generic impl (%u)\n",
1394 crypto_shash_digestsize(generic_tfm));
1399 if (blocksize != crypto_shash_blocksize(generic_tfm)) {
1400 pr_err("alg: hash: blocksize for %s (%u) doesn't match generic impl (%u)\n",
1401 driver, blocksize, crypto_shash_blocksize(generic_tfm));
1407 * Now generate test vectors using the generic implementation, and test
1408 * the other implementation against them.
1411 vec.key = kmalloc(maxkeysize, GFP_KERNEL);
1412 vec.plaintext = kmalloc(maxdatasize, GFP_KERNEL);
1413 vec.digest = kmalloc(digestsize, GFP_KERNEL);
1414 if (!vec.key || !vec.plaintext || !vec.digest) {
1419 for (i = 0; i < fuzz_iterations * 8; i++) {
1420 generate_random_hash_testvec(generic_tfm, &vec,
1421 maxkeysize, maxdatasize,
1422 vec_name, sizeof(vec_name));
1423 generate_random_testvec_config(&cfg, cfgname, sizeof(cfgname));
1425 err = test_hash_vec_cfg(driver, &vec, vec_name, &cfg,
1426 req, tsgl, hashstate);
1434 kfree(vec.plaintext);
1436 crypto_free_shash(generic_tfm);
1439 #else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1440 static int test_hash_vs_generic_impl(const char *driver,
1441 const char *generic_driver,
1442 unsigned int maxkeysize,
1443 struct ahash_request *req,
1444 struct test_sglist *tsgl,
1449 #endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1451 static int __alg_test_hash(const struct hash_testvec *vecs,
1452 unsigned int num_vecs, const char *driver,
1454 const char *generic_driver, unsigned int maxkeysize)
1456 struct crypto_ahash *tfm;
1457 struct ahash_request *req = NULL;
1458 struct test_sglist *tsgl = NULL;
1459 u8 *hashstate = NULL;
1463 tfm = crypto_alloc_ahash(driver, type, mask);
1465 pr_err("alg: hash: failed to allocate transform for %s: %ld\n",
1466 driver, PTR_ERR(tfm));
1467 return PTR_ERR(tfm);
1470 req = ahash_request_alloc(tfm, GFP_KERNEL);
1472 pr_err("alg: hash: failed to allocate request for %s\n",
1478 tsgl = kmalloc(sizeof(*tsgl), GFP_KERNEL);
1479 if (!tsgl || init_test_sglist(tsgl) != 0) {
1480 pr_err("alg: hash: failed to allocate test buffers for %s\n",
1488 hashstate = kmalloc(crypto_ahash_statesize(tfm) + TESTMGR_POISON_LEN,
1491 pr_err("alg: hash: failed to allocate hash state buffer for %s\n",
1497 for (i = 0; i < num_vecs; i++) {
1498 err = test_hash_vec(driver, &vecs[i], i, req, tsgl, hashstate);
1502 err = test_hash_vs_generic_impl(driver, generic_driver, maxkeysize, req,
1507 destroy_test_sglist(tsgl);
1510 ahash_request_free(req);
1511 crypto_free_ahash(tfm);
1515 static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1518 const struct hash_testvec *template = desc->suite.hash.vecs;
1519 unsigned int tcount = desc->suite.hash.count;
1520 unsigned int nr_unkeyed, nr_keyed;
1521 unsigned int maxkeysize = 0;
1525 * For OPTIONAL_KEY algorithms, we have to do all the unkeyed tests
1526 * first, before setting a key on the tfm. To make this easier, we
1527 * require that the unkeyed test vectors (if any) are listed first.
1530 for (nr_unkeyed = 0; nr_unkeyed < tcount; nr_unkeyed++) {
1531 if (template[nr_unkeyed].ksize)
1534 for (nr_keyed = 0; nr_unkeyed + nr_keyed < tcount; nr_keyed++) {
1535 if (!template[nr_unkeyed + nr_keyed].ksize) {
1536 pr_err("alg: hash: test vectors for %s out of order, "
1537 "unkeyed ones must come first\n", desc->alg);
1540 maxkeysize = max_t(unsigned int, maxkeysize,
1541 template[nr_unkeyed + nr_keyed].ksize);
1546 err = __alg_test_hash(template, nr_unkeyed, driver, type, mask,
1547 desc->generic_driver, maxkeysize);
1548 template += nr_unkeyed;
1551 if (!err && nr_keyed)
1552 err = __alg_test_hash(template, nr_keyed, driver, type, mask,
1553 desc->generic_driver, maxkeysize);
1558 static int test_aead_vec_cfg(const char *driver, int enc,
1559 const struct aead_testvec *vec,
1560 const char *vec_name,
1561 const struct testvec_config *cfg,
1562 struct aead_request *req,
1563 struct cipher_test_sglists *tsgls)
1565 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1566 const unsigned int alignmask = crypto_aead_alignmask(tfm);
1567 const unsigned int ivsize = crypto_aead_ivsize(tfm);
1568 const unsigned int authsize = vec->clen - vec->plen;
1569 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1570 const char *op = enc ? "encryption" : "decryption";
1571 DECLARE_CRYPTO_WAIT(wait);
1572 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
1573 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
1575 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
1576 struct kvec input[2];
1582 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1584 crypto_aead_clear_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1585 err = crypto_aead_setkey(tfm, vec->key, vec->klen);
1586 if (err && err != vec->setkey_error) {
1587 pr_err("alg: aead: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1588 driver, vec_name, vec->setkey_error, err,
1589 crypto_aead_get_flags(tfm));
1592 if (!err && vec->setkey_error) {
1593 pr_err("alg: aead: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1594 driver, vec_name, vec->setkey_error);
1598 /* Set the authentication tag size */
1599 err = crypto_aead_setauthsize(tfm, authsize);
1600 if (err && err != vec->setauthsize_error) {
1601 pr_err("alg: aead: %s setauthsize failed on test vector %s; expected_error=%d, actual_error=%d\n",
1602 driver, vec_name, vec->setauthsize_error, err);
1605 if (!err && vec->setauthsize_error) {
1606 pr_err("alg: aead: %s setauthsize unexpectedly succeeded on test vector %s; expected_error=%d\n",
1607 driver, vec_name, vec->setauthsize_error);
1611 if (vec->setkey_error || vec->setauthsize_error)
1614 /* The IV must be copied to a buffer, as the algorithm may modify it */
1615 if (WARN_ON(ivsize > MAX_IVLEN))
1618 memcpy(iv, vec->iv, ivsize);
1620 memset(iv, 0, ivsize);
1622 /* Build the src/dst scatterlists */
1623 input[0].iov_base = (void *)vec->assoc;
1624 input[0].iov_len = vec->alen;
1625 input[1].iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
1626 input[1].iov_len = enc ? vec->plen : vec->clen;
1627 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
1628 vec->alen + (enc ? vec->plen :
1630 vec->alen + (enc ? vec->clen :
1634 pr_err("alg: aead: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
1635 driver, op, vec_name, cfg->name);
1639 /* Do the actual encryption or decryption */
1640 testmgr_poison(req->__ctx, crypto_aead_reqsize(tfm));
1641 aead_request_set_callback(req, req_flags, crypto_req_done, &wait);
1642 aead_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
1643 enc ? vec->plen : vec->clen, iv);
1644 aead_request_set_ad(req, vec->alen);
1646 crypto_disable_simd_for_test();
1647 err = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
1649 crypto_reenable_simd_for_test();
1650 err = crypto_wait_req(err, &wait);
1652 /* Check that the algorithm didn't overwrite things it shouldn't have */
1653 if (req->cryptlen != (enc ? vec->plen : vec->clen) ||
1654 req->assoclen != vec->alen ||
1656 req->src != tsgls->src.sgl_ptr ||
1657 req->dst != tsgls->dst.sgl_ptr ||
1658 crypto_aead_reqtfm(req) != tfm ||
1659 req->base.complete != crypto_req_done ||
1660 req->base.flags != req_flags ||
1661 req->base.data != &wait) {
1662 pr_err("alg: aead: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
1663 driver, op, vec_name, cfg->name);
1664 if (req->cryptlen != (enc ? vec->plen : vec->clen))
1665 pr_err("alg: aead: changed 'req->cryptlen'\n");
1666 if (req->assoclen != vec->alen)
1667 pr_err("alg: aead: changed 'req->assoclen'\n");
1669 pr_err("alg: aead: changed 'req->iv'\n");
1670 if (req->src != tsgls->src.sgl_ptr)
1671 pr_err("alg: aead: changed 'req->src'\n");
1672 if (req->dst != tsgls->dst.sgl_ptr)
1673 pr_err("alg: aead: changed 'req->dst'\n");
1674 if (crypto_aead_reqtfm(req) != tfm)
1675 pr_err("alg: aead: changed 'req->base.tfm'\n");
1676 if (req->base.complete != crypto_req_done)
1677 pr_err("alg: aead: changed 'req->base.complete'\n");
1678 if (req->base.flags != req_flags)
1679 pr_err("alg: aead: changed 'req->base.flags'\n");
1680 if (req->base.data != &wait)
1681 pr_err("alg: aead: changed 'req->base.data'\n");
1684 if (is_test_sglist_corrupted(&tsgls->src)) {
1685 pr_err("alg: aead: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
1686 driver, op, vec_name, cfg->name);
1689 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
1690 is_test_sglist_corrupted(&tsgls->dst)) {
1691 pr_err("alg: aead: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
1692 driver, op, vec_name, cfg->name);
1696 /* Check for success or failure */
1697 expected_error = vec->novrfy ? -EBADMSG : vec->crypt_error;
1699 if (err == expected_error)
1701 pr_err("alg: aead: %s %s failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
1702 driver, op, vec_name, expected_error, err, cfg->name);
1705 if (expected_error) {
1706 pr_err("alg: aead: %s %s unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
1707 driver, op, vec_name, expected_error, cfg->name);
1711 /* Check for the correct output (ciphertext or plaintext) */
1712 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
1713 enc ? vec->clen : vec->plen,
1714 vec->alen, enc || !cfg->inplace);
1715 if (err == -EOVERFLOW) {
1716 pr_err("alg: aead: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
1717 driver, op, vec_name, cfg->name);
1721 pr_err("alg: aead: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
1722 driver, op, vec_name, cfg->name);
1729 static int test_aead_vec(const char *driver, int enc,
1730 const struct aead_testvec *vec, unsigned int vec_num,
1731 struct aead_request *req,
1732 struct cipher_test_sglists *tsgls)
1738 if (enc && vec->novrfy)
1741 sprintf(vec_name, "%u", vec_num);
1743 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
1744 err = test_aead_vec_cfg(driver, enc, vec, vec_name,
1745 &default_cipher_testvec_configs[i],
1751 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1752 if (!noextratests) {
1753 struct testvec_config cfg;
1754 char cfgname[TESTVEC_CONFIG_NAMELEN];
1756 for (i = 0; i < fuzz_iterations; i++) {
1757 generate_random_testvec_config(&cfg, cfgname,
1759 err = test_aead_vec_cfg(driver, enc, vec, vec_name,
1769 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1771 * Generate an AEAD test vector from the given implementation.
1772 * Assumes the buffers in 'vec' were already allocated.
1774 static void generate_random_aead_testvec(struct aead_request *req,
1775 struct aead_testvec *vec,
1776 unsigned int maxkeysize,
1777 unsigned int maxdatasize,
1778 char *name, size_t max_namelen)
1780 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1781 const unsigned int ivsize = crypto_aead_ivsize(tfm);
1782 unsigned int maxauthsize = crypto_aead_alg(tfm)->maxauthsize;
1783 unsigned int authsize;
1784 unsigned int total_len;
1786 struct scatterlist src[2], dst;
1788 DECLARE_CRYPTO_WAIT(wait);
1790 /* Key: length in [0, maxkeysize], but usually choose maxkeysize */
1791 vec->klen = maxkeysize;
1792 if (prandom_u32() % 4 == 0)
1793 vec->klen = prandom_u32() % (maxkeysize + 1);
1794 generate_random_bytes((u8 *)vec->key, vec->klen);
1795 vec->setkey_error = crypto_aead_setkey(tfm, vec->key, vec->klen);
1798 generate_random_bytes((u8 *)vec->iv, ivsize);
1800 /* Tag length: in [0, maxauthsize], but usually choose maxauthsize */
1801 authsize = maxauthsize;
1802 if (prandom_u32() % 4 == 0)
1803 authsize = prandom_u32() % (maxauthsize + 1);
1804 if (WARN_ON(authsize > maxdatasize))
1805 authsize = maxdatasize;
1806 maxdatasize -= authsize;
1807 vec->setauthsize_error = crypto_aead_setauthsize(tfm, authsize);
1809 /* Plaintext and associated data */
1810 total_len = generate_random_length(maxdatasize);
1811 if (prandom_u32() % 4 == 0)
1814 vec->alen = generate_random_length(total_len);
1815 vec->plen = total_len - vec->alen;
1816 generate_random_bytes((u8 *)vec->assoc, vec->alen);
1817 generate_random_bytes((u8 *)vec->ptext, vec->plen);
1819 vec->clen = vec->plen + authsize;
1822 * If the key or authentication tag size couldn't be set, no need to
1823 * continue to encrypt.
1825 if (vec->setkey_error || vec->setauthsize_error)
1829 sg_init_table(src, 2);
1832 sg_set_buf(&src[i++], vec->assoc, vec->alen);
1834 sg_set_buf(&src[i++], vec->ptext, vec->plen);
1835 sg_init_one(&dst, vec->ctext, vec->alen + vec->clen);
1836 memcpy(iv, vec->iv, ivsize);
1837 aead_request_set_callback(req, 0, crypto_req_done, &wait);
1838 aead_request_set_crypt(req, src, &dst, vec->plen, iv);
1839 aead_request_set_ad(req, vec->alen);
1840 vec->crypt_error = crypto_wait_req(crypto_aead_encrypt(req), &wait);
1841 if (vec->crypt_error == 0)
1842 memmove((u8 *)vec->ctext, vec->ctext + vec->alen, vec->clen);
1844 snprintf(name, max_namelen,
1845 "\"random: alen=%u plen=%u authsize=%u klen=%u\"",
1846 vec->alen, vec->plen, authsize, vec->klen);
1850 * Test the AEAD algorithm represented by @req against the corresponding generic
1851 * implementation, if one is available.
1853 static int test_aead_vs_generic_impl(const char *driver,
1854 const struct alg_test_desc *test_desc,
1855 struct aead_request *req,
1856 struct cipher_test_sglists *tsgls)
1858 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1859 const unsigned int ivsize = crypto_aead_ivsize(tfm);
1860 const unsigned int maxauthsize = crypto_aead_alg(tfm)->maxauthsize;
1861 const unsigned int blocksize = crypto_aead_blocksize(tfm);
1862 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
1863 const char *algname = crypto_aead_alg(tfm)->base.cra_name;
1864 const char *generic_driver = test_desc->generic_driver;
1865 char _generic_driver[CRYPTO_MAX_ALG_NAME];
1866 struct crypto_aead *generic_tfm = NULL;
1867 struct aead_request *generic_req = NULL;
1868 unsigned int maxkeysize;
1870 struct aead_testvec vec = { 0 };
1872 struct testvec_config cfg;
1873 char cfgname[TESTVEC_CONFIG_NAMELEN];
1879 if (!generic_driver) { /* Use default naming convention? */
1880 err = build_generic_driver_name(algname, _generic_driver);
1883 generic_driver = _generic_driver;
1886 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
1889 generic_tfm = crypto_alloc_aead(generic_driver, 0, 0);
1890 if (IS_ERR(generic_tfm)) {
1891 err = PTR_ERR(generic_tfm);
1892 if (err == -ENOENT) {
1893 pr_warn("alg: aead: skipping comparison tests for %s because %s is unavailable\n",
1894 driver, generic_driver);
1897 pr_err("alg: aead: error allocating %s (generic impl of %s): %d\n",
1898 generic_driver, algname, err);
1902 generic_req = aead_request_alloc(generic_tfm, GFP_KERNEL);
1908 /* Check the algorithm properties for consistency. */
1910 if (maxauthsize != crypto_aead_alg(generic_tfm)->maxauthsize) {
1911 pr_err("alg: aead: maxauthsize for %s (%u) doesn't match generic impl (%u)\n",
1912 driver, maxauthsize,
1913 crypto_aead_alg(generic_tfm)->maxauthsize);
1918 if (ivsize != crypto_aead_ivsize(generic_tfm)) {
1919 pr_err("alg: aead: ivsize for %s (%u) doesn't match generic impl (%u)\n",
1920 driver, ivsize, crypto_aead_ivsize(generic_tfm));
1925 if (blocksize != crypto_aead_blocksize(generic_tfm)) {
1926 pr_err("alg: aead: blocksize for %s (%u) doesn't match generic impl (%u)\n",
1927 driver, blocksize, crypto_aead_blocksize(generic_tfm));
1933 * Now generate test vectors using the generic implementation, and test
1934 * the other implementation against them.
1938 for (i = 0; i < test_desc->suite.aead.count; i++)
1939 maxkeysize = max_t(unsigned int, maxkeysize,
1940 test_desc->suite.aead.vecs[i].klen);
1942 vec.key = kmalloc(maxkeysize, GFP_KERNEL);
1943 vec.iv = kmalloc(ivsize, GFP_KERNEL);
1944 vec.assoc = kmalloc(maxdatasize, GFP_KERNEL);
1945 vec.ptext = kmalloc(maxdatasize, GFP_KERNEL);
1946 vec.ctext = kmalloc(maxdatasize, GFP_KERNEL);
1947 if (!vec.key || !vec.iv || !vec.assoc || !vec.ptext || !vec.ctext) {
1952 for (i = 0; i < fuzz_iterations * 8; i++) {
1953 generate_random_aead_testvec(generic_req, &vec,
1954 maxkeysize, maxdatasize,
1955 vec_name, sizeof(vec_name));
1956 generate_random_testvec_config(&cfg, cfgname, sizeof(cfgname));
1958 err = test_aead_vec_cfg(driver, ENCRYPT, &vec, vec_name, &cfg,
1962 err = test_aead_vec_cfg(driver, DECRYPT, &vec, vec_name, &cfg,
1975 crypto_free_aead(generic_tfm);
1976 aead_request_free(generic_req);
1979 #else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1980 static int test_aead_vs_generic_impl(const char *driver,
1981 const struct alg_test_desc *test_desc,
1982 struct aead_request *req,
1983 struct cipher_test_sglists *tsgls)
1987 #endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1989 static int test_aead(const char *driver, int enc,
1990 const struct aead_test_suite *suite,
1991 struct aead_request *req,
1992 struct cipher_test_sglists *tsgls)
1997 for (i = 0; i < suite->count; i++) {
1998 err = test_aead_vec(driver, enc, &suite->vecs[i], i, req,
2006 static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
2009 const struct aead_test_suite *suite = &desc->suite.aead;
2010 struct crypto_aead *tfm;
2011 struct aead_request *req = NULL;
2012 struct cipher_test_sglists *tsgls = NULL;
2015 if (suite->count <= 0) {
2016 pr_err("alg: aead: empty test suite for %s\n", driver);
2020 tfm = crypto_alloc_aead(driver, type, mask);
2022 pr_err("alg: aead: failed to allocate transform for %s: %ld\n",
2023 driver, PTR_ERR(tfm));
2024 return PTR_ERR(tfm);
2027 req = aead_request_alloc(tfm, GFP_KERNEL);
2029 pr_err("alg: aead: failed to allocate request for %s\n",
2035 tsgls = alloc_cipher_test_sglists();
2037 pr_err("alg: aead: failed to allocate test buffers for %s\n",
2043 err = test_aead(driver, ENCRYPT, suite, req, tsgls);
2047 err = test_aead(driver, DECRYPT, suite, req, tsgls);
2051 err = test_aead_vs_generic_impl(driver, desc, req, tsgls);
2053 free_cipher_test_sglists(tsgls);
2054 aead_request_free(req);
2055 crypto_free_aead(tfm);
2059 static int test_cipher(struct crypto_cipher *tfm, int enc,
2060 const struct cipher_testvec *template,
2061 unsigned int tcount)
2063 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
2064 unsigned int i, j, k;
2067 const char *input, *result;
2069 char *xbuf[XBUFSIZE];
2072 if (testmgr_alloc_buf(xbuf))
2081 for (i = 0; i < tcount; i++) {
2083 if (fips_enabled && template[i].fips_skip)
2086 input = enc ? template[i].ptext : template[i].ctext;
2087 result = enc ? template[i].ctext : template[i].ptext;
2091 if (WARN_ON(template[i].len > PAGE_SIZE))
2095 memcpy(data, input, template[i].len);
2097 crypto_cipher_clear_flags(tfm, ~0);
2099 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
2101 ret = crypto_cipher_setkey(tfm, template[i].key,
2104 if (ret == template[i].setkey_error)
2106 pr_err("alg: cipher: %s setkey failed on test vector %u; expected_error=%d, actual_error=%d, flags=%#x\n",
2107 algo, j, template[i].setkey_error, ret,
2108 crypto_cipher_get_flags(tfm));
2111 if (template[i].setkey_error) {
2112 pr_err("alg: cipher: %s setkey unexpectedly succeeded on test vector %u; expected_error=%d\n",
2113 algo, j, template[i].setkey_error);
2118 for (k = 0; k < template[i].len;
2119 k += crypto_cipher_blocksize(tfm)) {
2121 crypto_cipher_encrypt_one(tfm, data + k,
2124 crypto_cipher_decrypt_one(tfm, data + k,
2129 if (memcmp(q, result, template[i].len)) {
2130 printk(KERN_ERR "alg: cipher: Test %d failed "
2131 "on %s for %s\n", j, e, algo);
2132 hexdump(q, template[i].len);
2141 testmgr_free_buf(xbuf);
2146 static int test_skcipher_vec_cfg(const char *driver, int enc,
2147 const struct cipher_testvec *vec,
2148 const char *vec_name,
2149 const struct testvec_config *cfg,
2150 struct skcipher_request *req,
2151 struct cipher_test_sglists *tsgls)
2153 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2154 const unsigned int alignmask = crypto_skcipher_alignmask(tfm);
2155 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2156 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
2157 const char *op = enc ? "encryption" : "decryption";
2158 DECLARE_CRYPTO_WAIT(wait);
2159 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
2160 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
2162 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
2168 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
2170 crypto_skcipher_clear_flags(tfm,
2171 CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
2172 err = crypto_skcipher_setkey(tfm, vec->key, vec->klen);
2174 if (err == vec->setkey_error)
2176 pr_err("alg: skcipher: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
2177 driver, vec_name, vec->setkey_error, err,
2178 crypto_skcipher_get_flags(tfm));
2181 if (vec->setkey_error) {
2182 pr_err("alg: skcipher: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
2183 driver, vec_name, vec->setkey_error);
2187 /* The IV must be copied to a buffer, as the algorithm may modify it */
2189 if (WARN_ON(ivsize > MAX_IVLEN))
2191 if (vec->generates_iv && !enc)
2192 memcpy(iv, vec->iv_out, ivsize);
2194 memcpy(iv, vec->iv, ivsize);
2196 memset(iv, 0, ivsize);
2198 if (vec->generates_iv) {
2199 pr_err("alg: skcipher: %s has ivsize=0 but test vector %s generates IV!\n",
2206 /* Build the src/dst scatterlists */
2207 input.iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
2208 input.iov_len = vec->len;
2209 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
2210 vec->len, vec->len, &input, 1);
2212 pr_err("alg: skcipher: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
2213 driver, op, vec_name, cfg->name);
2217 /* Do the actual encryption or decryption */
2218 testmgr_poison(req->__ctx, crypto_skcipher_reqsize(tfm));
2219 skcipher_request_set_callback(req, req_flags, crypto_req_done, &wait);
2220 skcipher_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
2223 crypto_disable_simd_for_test();
2224 err = enc ? crypto_skcipher_encrypt(req) : crypto_skcipher_decrypt(req);
2226 crypto_reenable_simd_for_test();
2227 err = crypto_wait_req(err, &wait);
2229 /* Check that the algorithm didn't overwrite things it shouldn't have */
2230 if (req->cryptlen != vec->len ||
2232 req->src != tsgls->src.sgl_ptr ||
2233 req->dst != tsgls->dst.sgl_ptr ||
2234 crypto_skcipher_reqtfm(req) != tfm ||
2235 req->base.complete != crypto_req_done ||
2236 req->base.flags != req_flags ||
2237 req->base.data != &wait) {
2238 pr_err("alg: skcipher: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
2239 driver, op, vec_name, cfg->name);
2240 if (req->cryptlen != vec->len)
2241 pr_err("alg: skcipher: changed 'req->cryptlen'\n");
2243 pr_err("alg: skcipher: changed 'req->iv'\n");
2244 if (req->src != tsgls->src.sgl_ptr)
2245 pr_err("alg: skcipher: changed 'req->src'\n");
2246 if (req->dst != tsgls->dst.sgl_ptr)
2247 pr_err("alg: skcipher: changed 'req->dst'\n");
2248 if (crypto_skcipher_reqtfm(req) != tfm)
2249 pr_err("alg: skcipher: changed 'req->base.tfm'\n");
2250 if (req->base.complete != crypto_req_done)
2251 pr_err("alg: skcipher: changed 'req->base.complete'\n");
2252 if (req->base.flags != req_flags)
2253 pr_err("alg: skcipher: changed 'req->base.flags'\n");
2254 if (req->base.data != &wait)
2255 pr_err("alg: skcipher: changed 'req->base.data'\n");
2258 if (is_test_sglist_corrupted(&tsgls->src)) {
2259 pr_err("alg: skcipher: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
2260 driver, op, vec_name, cfg->name);
2263 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
2264 is_test_sglist_corrupted(&tsgls->dst)) {
2265 pr_err("alg: skcipher: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
2266 driver, op, vec_name, cfg->name);
2270 /* Check for success or failure */
2272 if (err == vec->crypt_error)
2274 pr_err("alg: skcipher: %s %s failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
2275 driver, op, vec_name, vec->crypt_error, err, cfg->name);
2278 if (vec->crypt_error) {
2279 pr_err("alg: skcipher: %s %s unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
2280 driver, op, vec_name, vec->crypt_error, cfg->name);
2284 /* Check for the correct output (ciphertext or plaintext) */
2285 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
2287 if (err == -EOVERFLOW) {
2288 pr_err("alg: skcipher: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
2289 driver, op, vec_name, cfg->name);
2293 pr_err("alg: skcipher: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
2294 driver, op, vec_name, cfg->name);
2298 /* If applicable, check that the algorithm generated the correct IV */
2299 if (vec->iv_out && memcmp(iv, vec->iv_out, ivsize) != 0) {
2300 pr_err("alg: skcipher: %s %s test failed (wrong output IV) on test vector %s, cfg=\"%s\"\n",
2301 driver, op, vec_name, cfg->name);
2302 hexdump(iv, ivsize);
2309 static int test_skcipher_vec(const char *driver, int enc,
2310 const struct cipher_testvec *vec,
2311 unsigned int vec_num,
2312 struct skcipher_request *req,
2313 struct cipher_test_sglists *tsgls)
2319 if (fips_enabled && vec->fips_skip)
2322 sprintf(vec_name, "%u", vec_num);
2324 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
2325 err = test_skcipher_vec_cfg(driver, enc, vec, vec_name,
2326 &default_cipher_testvec_configs[i],
2332 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2333 if (!noextratests) {
2334 struct testvec_config cfg;
2335 char cfgname[TESTVEC_CONFIG_NAMELEN];
2337 for (i = 0; i < fuzz_iterations; i++) {
2338 generate_random_testvec_config(&cfg, cfgname,
2340 err = test_skcipher_vec_cfg(driver, enc, vec, vec_name,
2350 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2352 * Generate a symmetric cipher test vector from the given implementation.
2353 * Assumes the buffers in 'vec' were already allocated.
2355 static void generate_random_cipher_testvec(struct skcipher_request *req,
2356 struct cipher_testvec *vec,
2357 unsigned int maxdatasize,
2358 char *name, size_t max_namelen)
2360 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2361 const unsigned int maxkeysize = tfm->keysize;
2362 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2363 struct scatterlist src, dst;
2365 DECLARE_CRYPTO_WAIT(wait);
2367 /* Key: length in [0, maxkeysize], but usually choose maxkeysize */
2368 vec->klen = maxkeysize;
2369 if (prandom_u32() % 4 == 0)
2370 vec->klen = prandom_u32() % (maxkeysize + 1);
2371 generate_random_bytes((u8 *)vec->key, vec->klen);
2372 vec->setkey_error = crypto_skcipher_setkey(tfm, vec->key, vec->klen);
2375 generate_random_bytes((u8 *)vec->iv, ivsize);
2378 vec->len = generate_random_length(maxdatasize);
2379 generate_random_bytes((u8 *)vec->ptext, vec->len);
2381 /* If the key couldn't be set, no need to continue to encrypt. */
2382 if (vec->setkey_error)
2386 sg_init_one(&src, vec->ptext, vec->len);
2387 sg_init_one(&dst, vec->ctext, vec->len);
2388 memcpy(iv, vec->iv, ivsize);
2389 skcipher_request_set_callback(req, 0, crypto_req_done, &wait);
2390 skcipher_request_set_crypt(req, &src, &dst, vec->len, iv);
2391 vec->crypt_error = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
2393 snprintf(name, max_namelen, "\"random: len=%u klen=%u\"",
2394 vec->len, vec->klen);
2398 * Test the skcipher algorithm represented by @req against the corresponding
2399 * generic implementation, if one is available.
2401 static int test_skcipher_vs_generic_impl(const char *driver,
2402 const char *generic_driver,
2403 struct skcipher_request *req,
2404 struct cipher_test_sglists *tsgls)
2406 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2407 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2408 const unsigned int blocksize = crypto_skcipher_blocksize(tfm);
2409 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
2410 const char *algname = crypto_skcipher_alg(tfm)->base.cra_name;
2411 char _generic_driver[CRYPTO_MAX_ALG_NAME];
2412 struct crypto_skcipher *generic_tfm = NULL;
2413 struct skcipher_request *generic_req = NULL;
2415 struct cipher_testvec vec = { 0 };
2417 struct testvec_config cfg;
2418 char cfgname[TESTVEC_CONFIG_NAMELEN];
2424 /* Keywrap isn't supported here yet as it handles its IV differently. */
2425 if (strncmp(algname, "kw(", 3) == 0)
2428 if (!generic_driver) { /* Use default naming convention? */
2429 err = build_generic_driver_name(algname, _generic_driver);
2432 generic_driver = _generic_driver;
2435 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
2438 generic_tfm = crypto_alloc_skcipher(generic_driver, 0, 0);
2439 if (IS_ERR(generic_tfm)) {
2440 err = PTR_ERR(generic_tfm);
2441 if (err == -ENOENT) {
2442 pr_warn("alg: skcipher: skipping comparison tests for %s because %s is unavailable\n",
2443 driver, generic_driver);
2446 pr_err("alg: skcipher: error allocating %s (generic impl of %s): %d\n",
2447 generic_driver, algname, err);
2451 generic_req = skcipher_request_alloc(generic_tfm, GFP_KERNEL);
2457 /* Check the algorithm properties for consistency. */
2459 if (tfm->keysize != generic_tfm->keysize) {
2460 pr_err("alg: skcipher: max keysize for %s (%u) doesn't match generic impl (%u)\n",
2461 driver, tfm->keysize, generic_tfm->keysize);
2466 if (ivsize != crypto_skcipher_ivsize(generic_tfm)) {
2467 pr_err("alg: skcipher: ivsize for %s (%u) doesn't match generic impl (%u)\n",
2468 driver, ivsize, crypto_skcipher_ivsize(generic_tfm));
2473 if (blocksize != crypto_skcipher_blocksize(generic_tfm)) {
2474 pr_err("alg: skcipher: blocksize for %s (%u) doesn't match generic impl (%u)\n",
2476 crypto_skcipher_blocksize(generic_tfm));
2482 * Now generate test vectors using the generic implementation, and test
2483 * the other implementation against them.
2486 vec.key = kmalloc(tfm->keysize, GFP_KERNEL);
2487 vec.iv = kmalloc(ivsize, GFP_KERNEL);
2488 vec.ptext = kmalloc(maxdatasize, GFP_KERNEL);
2489 vec.ctext = kmalloc(maxdatasize, GFP_KERNEL);
2490 if (!vec.key || !vec.iv || !vec.ptext || !vec.ctext) {
2495 for (i = 0; i < fuzz_iterations * 8; i++) {
2496 generate_random_cipher_testvec(generic_req, &vec, maxdatasize,
2497 vec_name, sizeof(vec_name));
2498 generate_random_testvec_config(&cfg, cfgname, sizeof(cfgname));
2500 err = test_skcipher_vec_cfg(driver, ENCRYPT, &vec, vec_name,
2504 err = test_skcipher_vec_cfg(driver, DECRYPT, &vec, vec_name,
2516 crypto_free_skcipher(generic_tfm);
2517 skcipher_request_free(generic_req);
2520 #else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
2521 static int test_skcipher_vs_generic_impl(const char *driver,
2522 const char *generic_driver,
2523 struct skcipher_request *req,
2524 struct cipher_test_sglists *tsgls)
2528 #endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
2530 static int test_skcipher(const char *driver, int enc,
2531 const struct cipher_test_suite *suite,
2532 struct skcipher_request *req,
2533 struct cipher_test_sglists *tsgls)
2538 for (i = 0; i < suite->count; i++) {
2539 err = test_skcipher_vec(driver, enc, &suite->vecs[i], i, req,
2547 static int alg_test_skcipher(const struct alg_test_desc *desc,
2548 const char *driver, u32 type, u32 mask)
2550 const struct cipher_test_suite *suite = &desc->suite.cipher;
2551 struct crypto_skcipher *tfm;
2552 struct skcipher_request *req = NULL;
2553 struct cipher_test_sglists *tsgls = NULL;
2556 if (suite->count <= 0) {
2557 pr_err("alg: skcipher: empty test suite for %s\n", driver);
2561 tfm = crypto_alloc_skcipher(driver, type, mask);
2563 pr_err("alg: skcipher: failed to allocate transform for %s: %ld\n",
2564 driver, PTR_ERR(tfm));
2565 return PTR_ERR(tfm);
2568 req = skcipher_request_alloc(tfm, GFP_KERNEL);
2570 pr_err("alg: skcipher: failed to allocate request for %s\n",
2576 tsgls = alloc_cipher_test_sglists();
2578 pr_err("alg: skcipher: failed to allocate test buffers for %s\n",
2584 err = test_skcipher(driver, ENCRYPT, suite, req, tsgls);
2588 err = test_skcipher(driver, DECRYPT, suite, req, tsgls);
2592 err = test_skcipher_vs_generic_impl(driver, desc->generic_driver, req,
2595 free_cipher_test_sglists(tsgls);
2596 skcipher_request_free(req);
2597 crypto_free_skcipher(tfm);
2601 static int test_comp(struct crypto_comp *tfm,
2602 const struct comp_testvec *ctemplate,
2603 const struct comp_testvec *dtemplate,
2604 int ctcount, int dtcount)
2606 const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
2607 char *output, *decomp_output;
2611 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
2615 decomp_output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
2616 if (!decomp_output) {
2621 for (i = 0; i < ctcount; i++) {
2623 unsigned int dlen = COMP_BUF_SIZE;
2625 memset(output, 0, COMP_BUF_SIZE);
2626 memset(decomp_output, 0, COMP_BUF_SIZE);
2628 ilen = ctemplate[i].inlen;
2629 ret = crypto_comp_compress(tfm, ctemplate[i].input,
2630 ilen, output, &dlen);
2632 printk(KERN_ERR "alg: comp: compression failed "
2633 "on test %d for %s: ret=%d\n", i + 1, algo,
2639 dlen = COMP_BUF_SIZE;
2640 ret = crypto_comp_decompress(tfm, output,
2641 ilen, decomp_output, &dlen);
2643 pr_err("alg: comp: compression failed: decompress: on test %d for %s failed: ret=%d\n",
2648 if (dlen != ctemplate[i].inlen) {
2649 printk(KERN_ERR "alg: comp: Compression test %d "
2650 "failed for %s: output len = %d\n", i + 1, algo,
2656 if (memcmp(decomp_output, ctemplate[i].input,
2657 ctemplate[i].inlen)) {
2658 pr_err("alg: comp: compression failed: output differs: on test %d for %s\n",
2660 hexdump(decomp_output, dlen);
2666 for (i = 0; i < dtcount; i++) {
2668 unsigned int dlen = COMP_BUF_SIZE;
2670 memset(decomp_output, 0, COMP_BUF_SIZE);
2672 ilen = dtemplate[i].inlen;
2673 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
2674 ilen, decomp_output, &dlen);
2676 printk(KERN_ERR "alg: comp: decompression failed "
2677 "on test %d for %s: ret=%d\n", i + 1, algo,
2682 if (dlen != dtemplate[i].outlen) {
2683 printk(KERN_ERR "alg: comp: Decompression test %d "
2684 "failed for %s: output len = %d\n", i + 1, algo,
2690 if (memcmp(decomp_output, dtemplate[i].output, dlen)) {
2691 printk(KERN_ERR "alg: comp: Decompression test %d "
2692 "failed for %s\n", i + 1, algo);
2693 hexdump(decomp_output, dlen);
2702 kfree(decomp_output);
2707 static int test_acomp(struct crypto_acomp *tfm,
2708 const struct comp_testvec *ctemplate,
2709 const struct comp_testvec *dtemplate,
2710 int ctcount, int dtcount)
2712 const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
2714 char *output, *decomp_out;
2716 struct scatterlist src, dst;
2717 struct acomp_req *req;
2718 struct crypto_wait wait;
2720 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
2724 decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
2730 for (i = 0; i < ctcount; i++) {
2731 unsigned int dlen = COMP_BUF_SIZE;
2732 int ilen = ctemplate[i].inlen;
2735 input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL);
2741 memset(output, 0, dlen);
2742 crypto_init_wait(&wait);
2743 sg_init_one(&src, input_vec, ilen);
2744 sg_init_one(&dst, output, dlen);
2746 req = acomp_request_alloc(tfm);
2748 pr_err("alg: acomp: request alloc failed for %s\n",
2755 acomp_request_set_params(req, &src, &dst, ilen, dlen);
2756 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2757 crypto_req_done, &wait);
2759 ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
2761 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
2764 acomp_request_free(req);
2769 dlen = COMP_BUF_SIZE;
2770 sg_init_one(&src, output, ilen);
2771 sg_init_one(&dst, decomp_out, dlen);
2772 crypto_init_wait(&wait);
2773 acomp_request_set_params(req, &src, &dst, ilen, dlen);
2775 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
2777 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
2780 acomp_request_free(req);
2784 if (req->dlen != ctemplate[i].inlen) {
2785 pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
2786 i + 1, algo, req->dlen);
2789 acomp_request_free(req);
2793 if (memcmp(input_vec, decomp_out, req->dlen)) {
2794 pr_err("alg: acomp: Compression test %d failed for %s\n",
2796 hexdump(output, req->dlen);
2799 acomp_request_free(req);
2804 acomp_request_free(req);
2807 for (i = 0; i < dtcount; i++) {
2808 unsigned int dlen = COMP_BUF_SIZE;
2809 int ilen = dtemplate[i].inlen;
2812 input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL);
2818 memset(output, 0, dlen);
2819 crypto_init_wait(&wait);
2820 sg_init_one(&src, input_vec, ilen);
2821 sg_init_one(&dst, output, dlen);
2823 req = acomp_request_alloc(tfm);
2825 pr_err("alg: acomp: request alloc failed for %s\n",
2832 acomp_request_set_params(req, &src, &dst, ilen, dlen);
2833 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2834 crypto_req_done, &wait);
2836 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
2838 pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
2841 acomp_request_free(req);
2845 if (req->dlen != dtemplate[i].outlen) {
2846 pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
2847 i + 1, algo, req->dlen);
2850 acomp_request_free(req);
2854 if (memcmp(output, dtemplate[i].output, req->dlen)) {
2855 pr_err("alg: acomp: Decompression test %d failed for %s\n",
2857 hexdump(output, req->dlen);
2860 acomp_request_free(req);
2865 acomp_request_free(req);
2876 static int test_cprng(struct crypto_rng *tfm,
2877 const struct cprng_testvec *template,
2878 unsigned int tcount)
2880 const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
2881 int err = 0, i, j, seedsize;
2885 seedsize = crypto_rng_seedsize(tfm);
2887 seed = kmalloc(seedsize, GFP_KERNEL);
2889 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
2894 for (i = 0; i < tcount; i++) {
2895 memset(result, 0, 32);
2897 memcpy(seed, template[i].v, template[i].vlen);
2898 memcpy(seed + template[i].vlen, template[i].key,
2900 memcpy(seed + template[i].vlen + template[i].klen,
2901 template[i].dt, template[i].dtlen);
2903 err = crypto_rng_reset(tfm, seed, seedsize);
2905 printk(KERN_ERR "alg: cprng: Failed to reset rng "
2910 for (j = 0; j < template[i].loops; j++) {
2911 err = crypto_rng_get_bytes(tfm, result,
2914 printk(KERN_ERR "alg: cprng: Failed to obtain "
2915 "the correct amount of random data for "
2916 "%s (requested %d)\n", algo,
2922 err = memcmp(result, template[i].result,
2925 printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
2927 hexdump(result, template[i].rlen);
2938 static int alg_test_cipher(const struct alg_test_desc *desc,
2939 const char *driver, u32 type, u32 mask)
2941 const struct cipher_test_suite *suite = &desc->suite.cipher;
2942 struct crypto_cipher *tfm;
2945 tfm = crypto_alloc_cipher(driver, type, mask);
2947 printk(KERN_ERR "alg: cipher: Failed to load transform for "
2948 "%s: %ld\n", driver, PTR_ERR(tfm));
2949 return PTR_ERR(tfm);
2952 err = test_cipher(tfm, ENCRYPT, suite->vecs, suite->count);
2954 err = test_cipher(tfm, DECRYPT, suite->vecs, suite->count);
2956 crypto_free_cipher(tfm);
2960 static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
2963 struct crypto_comp *comp;
2964 struct crypto_acomp *acomp;
2966 u32 algo_type = type & CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
2968 if (algo_type == CRYPTO_ALG_TYPE_ACOMPRESS) {
2969 acomp = crypto_alloc_acomp(driver, type, mask);
2970 if (IS_ERR(acomp)) {
2971 pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
2972 driver, PTR_ERR(acomp));
2973 return PTR_ERR(acomp);
2975 err = test_acomp(acomp, desc->suite.comp.comp.vecs,
2976 desc->suite.comp.decomp.vecs,
2977 desc->suite.comp.comp.count,
2978 desc->suite.comp.decomp.count);
2979 crypto_free_acomp(acomp);
2981 comp = crypto_alloc_comp(driver, type, mask);
2983 pr_err("alg: comp: Failed to load transform for %s: %ld\n",
2984 driver, PTR_ERR(comp));
2985 return PTR_ERR(comp);
2988 err = test_comp(comp, desc->suite.comp.comp.vecs,
2989 desc->suite.comp.decomp.vecs,
2990 desc->suite.comp.comp.count,
2991 desc->suite.comp.decomp.count);
2993 crypto_free_comp(comp);
2998 static int alg_test_crc32c(const struct alg_test_desc *desc,
2999 const char *driver, u32 type, u32 mask)
3001 struct crypto_shash *tfm;
3005 err = alg_test_hash(desc, driver, type, mask);
3009 tfm = crypto_alloc_shash(driver, type, mask);
3011 if (PTR_ERR(tfm) == -ENOENT) {
3013 * This crc32c implementation is only available through
3014 * ahash API, not the shash API, so the remaining part
3015 * of the test is not applicable to it.
3019 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
3020 "%ld\n", driver, PTR_ERR(tfm));
3021 return PTR_ERR(tfm);
3025 SHASH_DESC_ON_STACK(shash, tfm);
3026 u32 *ctx = (u32 *)shash_desc_ctx(shash);
3031 err = crypto_shash_final(shash, (u8 *)&val);
3033 printk(KERN_ERR "alg: crc32c: Operation failed for "
3034 "%s: %d\n", driver, err);
3038 if (val != cpu_to_le32(~420553207)) {
3039 pr_err("alg: crc32c: Test failed for %s: %u\n",
3040 driver, le32_to_cpu(val));
3045 crypto_free_shash(tfm);
3050 static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
3053 struct crypto_rng *rng;
3056 rng = crypto_alloc_rng(driver, type, mask);
3058 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
3059 "%ld\n", driver, PTR_ERR(rng));
3060 return PTR_ERR(rng);
3063 err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
3065 crypto_free_rng(rng);
3071 static int drbg_cavs_test(const struct drbg_testvec *test, int pr,
3072 const char *driver, u32 type, u32 mask)
3075 struct crypto_rng *drng;
3076 struct drbg_test_data test_data;
3077 struct drbg_string addtl, pers, testentropy;
3078 unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
3083 drng = crypto_alloc_rng(driver, type, mask);
3085 printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
3091 test_data.testentropy = &testentropy;
3092 drbg_string_fill(&testentropy, test->entropy, test->entropylen);
3093 drbg_string_fill(&pers, test->pers, test->perslen);
3094 ret = crypto_drbg_reset_test(drng, &pers, &test_data);
3096 printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
3100 drbg_string_fill(&addtl, test->addtla, test->addtllen);
3102 drbg_string_fill(&testentropy, test->entpra, test->entprlen);
3103 ret = crypto_drbg_get_bytes_addtl_test(drng,
3104 buf, test->expectedlen, &addtl, &test_data);
3106 ret = crypto_drbg_get_bytes_addtl(drng,
3107 buf, test->expectedlen, &addtl);
3110 printk(KERN_ERR "alg: drbg: could not obtain random data for "
3111 "driver %s\n", driver);
3115 drbg_string_fill(&addtl, test->addtlb, test->addtllen);
3117 drbg_string_fill(&testentropy, test->entprb, test->entprlen);
3118 ret = crypto_drbg_get_bytes_addtl_test(drng,
3119 buf, test->expectedlen, &addtl, &test_data);
3121 ret = crypto_drbg_get_bytes_addtl(drng,
3122 buf, test->expectedlen, &addtl);
3125 printk(KERN_ERR "alg: drbg: could not obtain random data for "
3126 "driver %s\n", driver);
3130 ret = memcmp(test->expected, buf, test->expectedlen);
3133 crypto_free_rng(drng);
3139 static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
3145 const struct drbg_testvec *template = desc->suite.drbg.vecs;
3146 unsigned int tcount = desc->suite.drbg.count;
3148 if (0 == memcmp(driver, "drbg_pr_", 8))
3151 for (i = 0; i < tcount; i++) {
3152 err = drbg_cavs_test(&template[i], pr, driver, type, mask);
3154 printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
3164 static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
3167 struct kpp_request *req;
3168 void *input_buf = NULL;
3169 void *output_buf = NULL;
3170 void *a_public = NULL;
3172 void *shared_secret = NULL;
3173 struct crypto_wait wait;
3174 unsigned int out_len_max;
3176 struct scatterlist src, dst;
3178 req = kpp_request_alloc(tfm, GFP_KERNEL);
3182 crypto_init_wait(&wait);
3184 err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
3188 out_len_max = crypto_kpp_maxsize(tfm);
3189 output_buf = kzalloc(out_len_max, GFP_KERNEL);
3195 /* Use appropriate parameter as base */
3196 kpp_request_set_input(req, NULL, 0);
3197 sg_init_one(&dst, output_buf, out_len_max);
3198 kpp_request_set_output(req, &dst, out_len_max);
3199 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3200 crypto_req_done, &wait);
3202 /* Compute party A's public key */
3203 err = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
3205 pr_err("alg: %s: Party A: generate public key test failed. err %d\n",
3211 /* Save party A's public key */
3212 a_public = kmemdup(sg_virt(req->dst), out_len_max, GFP_KERNEL);
3218 /* Verify calculated public key */
3219 if (memcmp(vec->expected_a_public, sg_virt(req->dst),
3220 vec->expected_a_public_size)) {
3221 pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n",
3228 /* Calculate shared secret key by using counter part (b) public key. */
3229 input_buf = kmemdup(vec->b_public, vec->b_public_size, GFP_KERNEL);
3235 sg_init_one(&src, input_buf, vec->b_public_size);
3236 sg_init_one(&dst, output_buf, out_len_max);
3237 kpp_request_set_input(req, &src, vec->b_public_size);
3238 kpp_request_set_output(req, &dst, out_len_max);
3239 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3240 crypto_req_done, &wait);
3241 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait);
3243 pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n",
3249 /* Save the shared secret obtained by party A */
3250 a_ss = kmemdup(sg_virt(req->dst), vec->expected_ss_size, GFP_KERNEL);
3257 * Calculate party B's shared secret by using party A's
3260 err = crypto_kpp_set_secret(tfm, vec->b_secret,
3261 vec->b_secret_size);
3265 sg_init_one(&src, a_public, vec->expected_a_public_size);
3266 sg_init_one(&dst, output_buf, out_len_max);
3267 kpp_request_set_input(req, &src, vec->expected_a_public_size);
3268 kpp_request_set_output(req, &dst, out_len_max);
3269 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3270 crypto_req_done, &wait);
3271 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req),
3274 pr_err("alg: %s: Party B: compute shared secret failed. err %d\n",
3279 shared_secret = a_ss;
3281 shared_secret = (void *)vec->expected_ss;
3285 * verify shared secret from which the user will derive
3286 * secret key by executing whatever hash it has chosen
3288 if (memcmp(shared_secret, sg_virt(req->dst),
3289 vec->expected_ss_size)) {
3290 pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
3302 kpp_request_free(req);
3306 static int test_kpp(struct crypto_kpp *tfm, const char *alg,
3307 const struct kpp_testvec *vecs, unsigned int tcount)
3311 for (i = 0; i < tcount; i++) {
3312 ret = do_test_kpp(tfm, vecs++, alg);
3314 pr_err("alg: %s: test failed on vector %d, err=%d\n",
3322 static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
3325 struct crypto_kpp *tfm;
3328 tfm = crypto_alloc_kpp(driver, type, mask);
3330 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
3331 driver, PTR_ERR(tfm));
3332 return PTR_ERR(tfm);
3334 if (desc->suite.kpp.vecs)
3335 err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
3336 desc->suite.kpp.count);
3338 crypto_free_kpp(tfm);
3342 static u8 *test_pack_u32(u8 *dst, u32 val)
3344 memcpy(dst, &val, sizeof(val));
3345 return dst + sizeof(val);
3348 static int test_akcipher_one(struct crypto_akcipher *tfm,
3349 const struct akcipher_testvec *vecs)
3351 char *xbuf[XBUFSIZE];
3352 struct akcipher_request *req;
3353 void *outbuf_enc = NULL;
3354 void *outbuf_dec = NULL;
3355 struct crypto_wait wait;
3356 unsigned int out_len_max, out_len = 0;
3358 struct scatterlist src, dst, src_tab[3];
3360 unsigned int m_size, c_size;
3364 if (testmgr_alloc_buf(xbuf))
3367 req = akcipher_request_alloc(tfm, GFP_KERNEL);
3371 crypto_init_wait(&wait);
3373 key = kmalloc(vecs->key_len + sizeof(u32) * 2 + vecs->param_len,
3377 memcpy(key, vecs->key, vecs->key_len);
3378 ptr = key + vecs->key_len;
3379 ptr = test_pack_u32(ptr, vecs->algo);
3380 ptr = test_pack_u32(ptr, vecs->param_len);
3381 memcpy(ptr, vecs->params, vecs->param_len);
3383 if (vecs->public_key_vec)
3384 err = crypto_akcipher_set_pub_key(tfm, key, vecs->key_len);
3386 err = crypto_akcipher_set_priv_key(tfm, key, vecs->key_len);
3391 * First run test which do not require a private key, such as
3392 * encrypt or verify.
3395 out_len_max = crypto_akcipher_maxsize(tfm);
3396 outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
3400 if (!vecs->siggen_sigver_test) {
3402 m_size = vecs->m_size;
3404 c_size = vecs->c_size;
3407 /* Swap args so we could keep plaintext (digest)
3408 * in vecs->m, and cooked signature in vecs->c.
3410 m = vecs->c; /* signature */
3411 m_size = vecs->c_size;
3412 c = vecs->m; /* digest */
3413 c_size = vecs->m_size;
3417 if (WARN_ON(m_size > PAGE_SIZE))
3419 memcpy(xbuf[0], m, m_size);
3421 sg_init_table(src_tab, 3);
3422 sg_set_buf(&src_tab[0], xbuf[0], 8);
3423 sg_set_buf(&src_tab[1], xbuf[0] + 8, m_size - 8);
3424 if (vecs->siggen_sigver_test) {
3425 if (WARN_ON(c_size > PAGE_SIZE))
3427 memcpy(xbuf[1], c, c_size);
3428 sg_set_buf(&src_tab[2], xbuf[1], c_size);
3429 akcipher_request_set_crypt(req, src_tab, NULL, m_size, c_size);
3431 sg_init_one(&dst, outbuf_enc, out_len_max);
3432 akcipher_request_set_crypt(req, src_tab, &dst, m_size,
3435 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3436 crypto_req_done, &wait);
3438 err = crypto_wait_req(vecs->siggen_sigver_test ?
3439 /* Run asymmetric signature verification */
3440 crypto_akcipher_verify(req) :
3441 /* Run asymmetric encrypt */
3442 crypto_akcipher_encrypt(req), &wait);
3444 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
3447 if (!vecs->siggen_sigver_test) {
3448 if (req->dst_len != c_size) {
3449 pr_err("alg: akcipher: %s test failed. Invalid output len\n",
3454 /* verify that encrypted message is equal to expected */
3455 if (memcmp(c, outbuf_enc, c_size) != 0) {
3456 pr_err("alg: akcipher: %s test failed. Invalid output\n",
3458 hexdump(outbuf_enc, c_size);
3465 * Don't invoke (decrypt or sign) test which require a private key
3466 * for vectors with only a public key.
3468 if (vecs->public_key_vec) {
3472 outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
3478 op = vecs->siggen_sigver_test ? "sign" : "decrypt";
3479 if (WARN_ON(c_size > PAGE_SIZE))
3481 memcpy(xbuf[0], c, c_size);
3483 sg_init_one(&src, xbuf[0], c_size);
3484 sg_init_one(&dst, outbuf_dec, out_len_max);
3485 crypto_init_wait(&wait);
3486 akcipher_request_set_crypt(req, &src, &dst, c_size, out_len_max);
3488 err = crypto_wait_req(vecs->siggen_sigver_test ?
3489 /* Run asymmetric signature generation */
3490 crypto_akcipher_sign(req) :
3491 /* Run asymmetric decrypt */
3492 crypto_akcipher_decrypt(req), &wait);
3494 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
3497 out_len = req->dst_len;
3498 if (out_len < m_size) {
3499 pr_err("alg: akcipher: %s test failed. Invalid output len %u\n",
3504 /* verify that decrypted message is equal to the original msg */
3505 if (memchr_inv(outbuf_dec, 0, out_len - m_size) ||
3506 memcmp(m, outbuf_dec + out_len - m_size, m_size)) {
3507 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
3508 hexdump(outbuf_dec, out_len);
3515 akcipher_request_free(req);
3518 testmgr_free_buf(xbuf);
3522 static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
3523 const struct akcipher_testvec *vecs,
3524 unsigned int tcount)
3527 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
3530 for (i = 0; i < tcount; i++) {
3531 ret = test_akcipher_one(tfm, vecs++);
3535 pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
3542 static int alg_test_akcipher(const struct alg_test_desc *desc,
3543 const char *driver, u32 type, u32 mask)
3545 struct crypto_akcipher *tfm;
3548 tfm = crypto_alloc_akcipher(driver, type, mask);
3550 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
3551 driver, PTR_ERR(tfm));
3552 return PTR_ERR(tfm);
3554 if (desc->suite.akcipher.vecs)
3555 err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
3556 desc->suite.akcipher.count);
3558 crypto_free_akcipher(tfm);
3562 static int alg_test_null(const struct alg_test_desc *desc,
3563 const char *driver, u32 type, u32 mask)
3568 #define __VECS(tv) { .vecs = tv, .count = ARRAY_SIZE(tv) }
3570 /* Please keep this list sorted by algorithm name. */
3571 static const struct alg_test_desc alg_test_descs[] = {
3573 .alg = "adiantum(xchacha12,aes)",
3574 .generic_driver = "adiantum(xchacha12-generic,aes-generic,nhpoly1305-generic)",
3575 .test = alg_test_skcipher,
3577 .cipher = __VECS(adiantum_xchacha12_aes_tv_template)
3580 .alg = "adiantum(xchacha20,aes)",
3581 .generic_driver = "adiantum(xchacha20-generic,aes-generic,nhpoly1305-generic)",
3582 .test = alg_test_skcipher,
3584 .cipher = __VECS(adiantum_xchacha20_aes_tv_template)
3588 .test = alg_test_aead,
3590 .aead = __VECS(aegis128_tv_template)
3594 .test = alg_test_aead,
3596 .aead = __VECS(aegis128l_tv_template)
3600 .test = alg_test_aead,
3602 .aead = __VECS(aegis256_tv_template)
3605 .alg = "ansi_cprng",
3606 .test = alg_test_cprng,
3608 .cprng = __VECS(ansi_cprng_aes_tv_template)
3611 .alg = "authenc(hmac(md5),ecb(cipher_null))",
3612 .test = alg_test_aead,
3614 .aead = __VECS(hmac_md5_ecb_cipher_null_tv_template)
3617 .alg = "authenc(hmac(sha1),cbc(aes))",
3618 .test = alg_test_aead,
3621 .aead = __VECS(hmac_sha1_aes_cbc_tv_temp)
3624 .alg = "authenc(hmac(sha1),cbc(des))",
3625 .test = alg_test_aead,
3627 .aead = __VECS(hmac_sha1_des_cbc_tv_temp)
3630 .alg = "authenc(hmac(sha1),cbc(des3_ede))",
3631 .test = alg_test_aead,
3634 .aead = __VECS(hmac_sha1_des3_ede_cbc_tv_temp)
3637 .alg = "authenc(hmac(sha1),ctr(aes))",
3638 .test = alg_test_null,
3641 .alg = "authenc(hmac(sha1),ecb(cipher_null))",
3642 .test = alg_test_aead,
3644 .aead = __VECS(hmac_sha1_ecb_cipher_null_tv_temp)
3647 .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
3648 .test = alg_test_null,
3651 .alg = "authenc(hmac(sha224),cbc(des))",
3652 .test = alg_test_aead,
3654 .aead = __VECS(hmac_sha224_des_cbc_tv_temp)
3657 .alg = "authenc(hmac(sha224),cbc(des3_ede))",
3658 .test = alg_test_aead,
3661 .aead = __VECS(hmac_sha224_des3_ede_cbc_tv_temp)
3664 .alg = "authenc(hmac(sha256),cbc(aes))",
3665 .test = alg_test_aead,
3668 .aead = __VECS(hmac_sha256_aes_cbc_tv_temp)
3671 .alg = "authenc(hmac(sha256),cbc(des))",
3672 .test = alg_test_aead,
3674 .aead = __VECS(hmac_sha256_des_cbc_tv_temp)
3677 .alg = "authenc(hmac(sha256),cbc(des3_ede))",
3678 .test = alg_test_aead,
3681 .aead = __VECS(hmac_sha256_des3_ede_cbc_tv_temp)
3684 .alg = "authenc(hmac(sha256),ctr(aes))",
3685 .test = alg_test_null,
3688 .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
3689 .test = alg_test_null,
3692 .alg = "authenc(hmac(sha384),cbc(des))",
3693 .test = alg_test_aead,
3695 .aead = __VECS(hmac_sha384_des_cbc_tv_temp)
3698 .alg = "authenc(hmac(sha384),cbc(des3_ede))",
3699 .test = alg_test_aead,
3702 .aead = __VECS(hmac_sha384_des3_ede_cbc_tv_temp)
3705 .alg = "authenc(hmac(sha384),ctr(aes))",
3706 .test = alg_test_null,
3709 .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
3710 .test = alg_test_null,
3713 .alg = "authenc(hmac(sha512),cbc(aes))",
3715 .test = alg_test_aead,
3717 .aead = __VECS(hmac_sha512_aes_cbc_tv_temp)
3720 .alg = "authenc(hmac(sha512),cbc(des))",
3721 .test = alg_test_aead,
3723 .aead = __VECS(hmac_sha512_des_cbc_tv_temp)
3726 .alg = "authenc(hmac(sha512),cbc(des3_ede))",
3727 .test = alg_test_aead,
3730 .aead = __VECS(hmac_sha512_des3_ede_cbc_tv_temp)
3733 .alg = "authenc(hmac(sha512),ctr(aes))",
3734 .test = alg_test_null,
3737 .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
3738 .test = alg_test_null,
3742 .test = alg_test_skcipher,
3745 .cipher = __VECS(aes_cbc_tv_template)
3748 .alg = "cbc(anubis)",
3749 .test = alg_test_skcipher,
3751 .cipher = __VECS(anubis_cbc_tv_template)
3754 .alg = "cbc(blowfish)",
3755 .test = alg_test_skcipher,
3757 .cipher = __VECS(bf_cbc_tv_template)
3760 .alg = "cbc(camellia)",
3761 .test = alg_test_skcipher,
3763 .cipher = __VECS(camellia_cbc_tv_template)
3766 .alg = "cbc(cast5)",
3767 .test = alg_test_skcipher,
3769 .cipher = __VECS(cast5_cbc_tv_template)
3772 .alg = "cbc(cast6)",
3773 .test = alg_test_skcipher,
3775 .cipher = __VECS(cast6_cbc_tv_template)
3779 .test = alg_test_skcipher,
3781 .cipher = __VECS(des_cbc_tv_template)
3784 .alg = "cbc(des3_ede)",
3785 .test = alg_test_skcipher,
3788 .cipher = __VECS(des3_ede_cbc_tv_template)
3791 /* Same as cbc(aes) except the key is stored in
3792 * hardware secure memory which we reference by index
3795 .test = alg_test_null,
3798 /* Same as cbc(sm4) except the key is stored in
3799 * hardware secure memory which we reference by index
3802 .test = alg_test_null,
3804 .alg = "cbc(serpent)",
3805 .test = alg_test_skcipher,
3807 .cipher = __VECS(serpent_cbc_tv_template)
3811 .test = alg_test_skcipher,
3813 .cipher = __VECS(sm4_cbc_tv_template)
3816 .alg = "cbc(twofish)",
3817 .test = alg_test_skcipher,
3819 .cipher = __VECS(tf_cbc_tv_template)
3822 .alg = "cbcmac(aes)",
3824 .test = alg_test_hash,
3826 .hash = __VECS(aes_cbcmac_tv_template)
3830 .generic_driver = "ccm_base(ctr(aes-generic),cbcmac(aes-generic))",
3831 .test = alg_test_aead,
3834 .aead = __VECS(aes_ccm_tv_template)
3838 .test = alg_test_skcipher,
3841 .cipher = __VECS(aes_cfb_tv_template)
3845 .test = alg_test_skcipher,
3847 .cipher = __VECS(chacha20_tv_template)
3852 .test = alg_test_hash,
3854 .hash = __VECS(aes_cmac128_tv_template)
3857 .alg = "cmac(des3_ede)",
3859 .test = alg_test_hash,
3861 .hash = __VECS(des3_ede_cmac64_tv_template)
3864 .alg = "compress_null",
3865 .test = alg_test_null,
3868 .test = alg_test_hash,
3871 .hash = __VECS(crc32_tv_template)
3875 .test = alg_test_crc32c,
3878 .hash = __VECS(crc32c_tv_template)
3882 .test = alg_test_hash,
3885 .hash = __VECS(crct10dif_tv_template)
3889 .test = alg_test_skcipher,
3892 .cipher = __VECS(aes_ctr_tv_template)
3895 .alg = "ctr(blowfish)",
3896 .test = alg_test_skcipher,
3898 .cipher = __VECS(bf_ctr_tv_template)
3901 .alg = "ctr(camellia)",
3902 .test = alg_test_skcipher,
3904 .cipher = __VECS(camellia_ctr_tv_template)
3907 .alg = "ctr(cast5)",
3908 .test = alg_test_skcipher,
3910 .cipher = __VECS(cast5_ctr_tv_template)
3913 .alg = "ctr(cast6)",
3914 .test = alg_test_skcipher,
3916 .cipher = __VECS(cast6_ctr_tv_template)
3920 .test = alg_test_skcipher,
3922 .cipher = __VECS(des_ctr_tv_template)
3925 .alg = "ctr(des3_ede)",
3926 .test = alg_test_skcipher,
3929 .cipher = __VECS(des3_ede_ctr_tv_template)
3932 /* Same as ctr(aes) except the key is stored in
3933 * hardware secure memory which we reference by index
3936 .test = alg_test_null,
3940 /* Same as ctr(sm4) except the key is stored in
3941 * hardware secure memory which we reference by index
3944 .test = alg_test_null,
3946 .alg = "ctr(serpent)",
3947 .test = alg_test_skcipher,
3949 .cipher = __VECS(serpent_ctr_tv_template)
3953 .test = alg_test_skcipher,
3955 .cipher = __VECS(sm4_ctr_tv_template)
3958 .alg = "ctr(twofish)",
3959 .test = alg_test_skcipher,
3961 .cipher = __VECS(tf_ctr_tv_template)
3964 .alg = "cts(cbc(aes))",
3965 .test = alg_test_skcipher,
3968 .cipher = __VECS(cts_mode_tv_template)
3971 /* Same as cts(cbc((aes)) except the key is stored in
3972 * hardware secure memory which we reference by index
3974 .alg = "cts(cbc(paes))",
3975 .test = alg_test_null,
3979 .test = alg_test_comp,
3983 .comp = __VECS(deflate_comp_tv_template),
3984 .decomp = __VECS(deflate_decomp_tv_template)
3989 .test = alg_test_kpp,
3992 .kpp = __VECS(dh_tv_template)
3995 .alg = "digest_null",
3996 .test = alg_test_null,
3998 .alg = "drbg_nopr_ctr_aes128",
3999 .test = alg_test_drbg,
4002 .drbg = __VECS(drbg_nopr_ctr_aes128_tv_template)
4005 .alg = "drbg_nopr_ctr_aes192",
4006 .test = alg_test_drbg,
4009 .drbg = __VECS(drbg_nopr_ctr_aes192_tv_template)
4012 .alg = "drbg_nopr_ctr_aes256",
4013 .test = alg_test_drbg,
4016 .drbg = __VECS(drbg_nopr_ctr_aes256_tv_template)
4020 * There is no need to specifically test the DRBG with every
4021 * backend cipher -- covered by drbg_nopr_hmac_sha256 test
4023 .alg = "drbg_nopr_hmac_sha1",
4025 .test = alg_test_null,
4027 .alg = "drbg_nopr_hmac_sha256",
4028 .test = alg_test_drbg,
4031 .drbg = __VECS(drbg_nopr_hmac_sha256_tv_template)
4034 /* covered by drbg_nopr_hmac_sha256 test */
4035 .alg = "drbg_nopr_hmac_sha384",
4037 .test = alg_test_null,
4039 .alg = "drbg_nopr_hmac_sha512",
4040 .test = alg_test_null,
4043 .alg = "drbg_nopr_sha1",
4045 .test = alg_test_null,
4047 .alg = "drbg_nopr_sha256",
4048 .test = alg_test_drbg,
4051 .drbg = __VECS(drbg_nopr_sha256_tv_template)
4054 /* covered by drbg_nopr_sha256 test */
4055 .alg = "drbg_nopr_sha384",
4057 .test = alg_test_null,
4059 .alg = "drbg_nopr_sha512",
4061 .test = alg_test_null,
4063 .alg = "drbg_pr_ctr_aes128",
4064 .test = alg_test_drbg,
4067 .drbg = __VECS(drbg_pr_ctr_aes128_tv_template)
4070 /* covered by drbg_pr_ctr_aes128 test */
4071 .alg = "drbg_pr_ctr_aes192",
4073 .test = alg_test_null,
4075 .alg = "drbg_pr_ctr_aes256",
4077 .test = alg_test_null,
4079 .alg = "drbg_pr_hmac_sha1",
4081 .test = alg_test_null,
4083 .alg = "drbg_pr_hmac_sha256",
4084 .test = alg_test_drbg,
4087 .drbg = __VECS(drbg_pr_hmac_sha256_tv_template)
4090 /* covered by drbg_pr_hmac_sha256 test */
4091 .alg = "drbg_pr_hmac_sha384",
4093 .test = alg_test_null,
4095 .alg = "drbg_pr_hmac_sha512",
4096 .test = alg_test_null,
4099 .alg = "drbg_pr_sha1",
4101 .test = alg_test_null,
4103 .alg = "drbg_pr_sha256",
4104 .test = alg_test_drbg,
4107 .drbg = __VECS(drbg_pr_sha256_tv_template)
4110 /* covered by drbg_pr_sha256 test */
4111 .alg = "drbg_pr_sha384",
4113 .test = alg_test_null,
4115 .alg = "drbg_pr_sha512",
4117 .test = alg_test_null,
4120 .test = alg_test_skcipher,
4123 .cipher = __VECS(aes_tv_template)
4126 .alg = "ecb(anubis)",
4127 .test = alg_test_skcipher,
4129 .cipher = __VECS(anubis_tv_template)
4133 .test = alg_test_skcipher,
4135 .cipher = __VECS(arc4_tv_template)
4138 .alg = "ecb(blowfish)",
4139 .test = alg_test_skcipher,
4141 .cipher = __VECS(bf_tv_template)
4144 .alg = "ecb(camellia)",
4145 .test = alg_test_skcipher,
4147 .cipher = __VECS(camellia_tv_template)
4150 .alg = "ecb(cast5)",
4151 .test = alg_test_skcipher,
4153 .cipher = __VECS(cast5_tv_template)
4156 .alg = "ecb(cast6)",
4157 .test = alg_test_skcipher,
4159 .cipher = __VECS(cast6_tv_template)
4162 .alg = "ecb(cipher_null)",
4163 .test = alg_test_null,
4167 .test = alg_test_skcipher,
4169 .cipher = __VECS(des_tv_template)
4172 .alg = "ecb(des3_ede)",
4173 .test = alg_test_skcipher,
4176 .cipher = __VECS(des3_ede_tv_template)
4179 .alg = "ecb(fcrypt)",
4180 .test = alg_test_skcipher,
4183 .vecs = fcrypt_pcbc_tv_template,
4188 .alg = "ecb(khazad)",
4189 .test = alg_test_skcipher,
4191 .cipher = __VECS(khazad_tv_template)
4194 /* Same as ecb(aes) except the key is stored in
4195 * hardware secure memory which we reference by index
4198 .test = alg_test_null,
4202 .test = alg_test_skcipher,
4204 .cipher = __VECS(seed_tv_template)
4207 .alg = "ecb(serpent)",
4208 .test = alg_test_skcipher,
4210 .cipher = __VECS(serpent_tv_template)
4214 .test = alg_test_skcipher,
4216 .cipher = __VECS(sm4_tv_template)
4220 .test = alg_test_skcipher,
4222 .cipher = __VECS(tea_tv_template)
4225 .alg = "ecb(tnepres)",
4226 .test = alg_test_skcipher,
4228 .cipher = __VECS(tnepres_tv_template)
4231 .alg = "ecb(twofish)",
4232 .test = alg_test_skcipher,
4234 .cipher = __VECS(tf_tv_template)
4238 .test = alg_test_skcipher,
4240 .cipher = __VECS(xeta_tv_template)
4244 .test = alg_test_skcipher,
4246 .cipher = __VECS(xtea_tv_template)
4250 .test = alg_test_kpp,
4253 .kpp = __VECS(ecdh_tv_template)
4257 .test = alg_test_akcipher,
4259 .akcipher = __VECS(ecrdsa_tv_template)
4263 .generic_driver = "gcm_base(ctr(aes-generic),ghash-generic)",
4264 .test = alg_test_aead,
4267 .aead = __VECS(aes_gcm_tv_template)
4271 .test = alg_test_hash,
4274 .hash = __VECS(ghash_tv_template)
4278 .test = alg_test_hash,
4280 .hash = __VECS(hmac_md5_tv_template)
4283 .alg = "hmac(rmd128)",
4284 .test = alg_test_hash,
4286 .hash = __VECS(hmac_rmd128_tv_template)
4289 .alg = "hmac(rmd160)",
4290 .test = alg_test_hash,
4292 .hash = __VECS(hmac_rmd160_tv_template)
4295 .alg = "hmac(sha1)",
4296 .test = alg_test_hash,
4299 .hash = __VECS(hmac_sha1_tv_template)
4302 .alg = "hmac(sha224)",
4303 .test = alg_test_hash,
4306 .hash = __VECS(hmac_sha224_tv_template)
4309 .alg = "hmac(sha256)",
4310 .test = alg_test_hash,
4313 .hash = __VECS(hmac_sha256_tv_template)
4316 .alg = "hmac(sha3-224)",
4317 .test = alg_test_hash,
4320 .hash = __VECS(hmac_sha3_224_tv_template)
4323 .alg = "hmac(sha3-256)",
4324 .test = alg_test_hash,
4327 .hash = __VECS(hmac_sha3_256_tv_template)
4330 .alg = "hmac(sha3-384)",
4331 .test = alg_test_hash,
4334 .hash = __VECS(hmac_sha3_384_tv_template)
4337 .alg = "hmac(sha3-512)",
4338 .test = alg_test_hash,
4341 .hash = __VECS(hmac_sha3_512_tv_template)
4344 .alg = "hmac(sha384)",
4345 .test = alg_test_hash,
4348 .hash = __VECS(hmac_sha384_tv_template)
4351 .alg = "hmac(sha512)",
4352 .test = alg_test_hash,
4355 .hash = __VECS(hmac_sha512_tv_template)
4358 .alg = "hmac(streebog256)",
4359 .test = alg_test_hash,
4361 .hash = __VECS(hmac_streebog256_tv_template)
4364 .alg = "hmac(streebog512)",
4365 .test = alg_test_hash,
4367 .hash = __VECS(hmac_streebog512_tv_template)
4370 .alg = "jitterentropy_rng",
4372 .test = alg_test_null,
4375 .test = alg_test_skcipher,
4378 .cipher = __VECS(aes_kw_tv_template)
4382 .generic_driver = "lrw(ecb(aes-generic))",
4383 .test = alg_test_skcipher,
4385 .cipher = __VECS(aes_lrw_tv_template)
4388 .alg = "lrw(camellia)",
4389 .generic_driver = "lrw(ecb(camellia-generic))",
4390 .test = alg_test_skcipher,
4392 .cipher = __VECS(camellia_lrw_tv_template)
4395 .alg = "lrw(cast6)",
4396 .generic_driver = "lrw(ecb(cast6-generic))",
4397 .test = alg_test_skcipher,
4399 .cipher = __VECS(cast6_lrw_tv_template)
4402 .alg = "lrw(serpent)",
4403 .generic_driver = "lrw(ecb(serpent-generic))",
4404 .test = alg_test_skcipher,
4406 .cipher = __VECS(serpent_lrw_tv_template)
4409 .alg = "lrw(twofish)",
4410 .generic_driver = "lrw(ecb(twofish-generic))",
4411 .test = alg_test_skcipher,
4413 .cipher = __VECS(tf_lrw_tv_template)
4417 .test = alg_test_comp,
4421 .comp = __VECS(lz4_comp_tv_template),
4422 .decomp = __VECS(lz4_decomp_tv_template)
4427 .test = alg_test_comp,
4431 .comp = __VECS(lz4hc_comp_tv_template),
4432 .decomp = __VECS(lz4hc_decomp_tv_template)
4437 .test = alg_test_comp,
4441 .comp = __VECS(lzo_comp_tv_template),
4442 .decomp = __VECS(lzo_decomp_tv_template)
4447 .test = alg_test_hash,
4449 .hash = __VECS(md4_tv_template)
4453 .test = alg_test_hash,
4455 .hash = __VECS(md5_tv_template)
4458 .alg = "michael_mic",
4459 .test = alg_test_hash,
4461 .hash = __VECS(michael_mic_tv_template)
4465 .test = alg_test_aead,
4467 .aead = __VECS(morus1280_tv_template)
4471 .test = alg_test_aead,
4473 .aead = __VECS(morus640_tv_template)
4476 .alg = "nhpoly1305",
4477 .test = alg_test_hash,
4479 .hash = __VECS(nhpoly1305_tv_template)
4483 .test = alg_test_skcipher,
4486 .cipher = __VECS(aes_ofb_tv_template)
4489 /* Same as ofb(aes) except the key is stored in
4490 * hardware secure memory which we reference by index
4493 .test = alg_test_null,
4496 .alg = "pcbc(fcrypt)",
4497 .test = alg_test_skcipher,
4499 .cipher = __VECS(fcrypt_pcbc_tv_template)
4502 .alg = "pkcs1pad(rsa,sha224)",
4503 .test = alg_test_null,
4506 .alg = "pkcs1pad(rsa,sha256)",
4507 .test = alg_test_akcipher,
4510 .akcipher = __VECS(pkcs1pad_rsa_tv_template)
4513 .alg = "pkcs1pad(rsa,sha384)",
4514 .test = alg_test_null,
4517 .alg = "pkcs1pad(rsa,sha512)",
4518 .test = alg_test_null,
4522 .test = alg_test_hash,
4524 .hash = __VECS(poly1305_tv_template)
4527 .alg = "rfc3686(ctr(aes))",
4528 .test = alg_test_skcipher,
4531 .cipher = __VECS(aes_ctr_rfc3686_tv_template)
4534 .alg = "rfc4106(gcm(aes))",
4535 .generic_driver = "rfc4106(gcm_base(ctr(aes-generic),ghash-generic))",
4536 .test = alg_test_aead,
4539 .aead = __VECS(aes_gcm_rfc4106_tv_template)
4542 .alg = "rfc4309(ccm(aes))",
4543 .generic_driver = "rfc4309(ccm_base(ctr(aes-generic),cbcmac(aes-generic)))",
4544 .test = alg_test_aead,
4547 .aead = __VECS(aes_ccm_rfc4309_tv_template)
4550 .alg = "rfc4543(gcm(aes))",
4551 .generic_driver = "rfc4543(gcm_base(ctr(aes-generic),ghash-generic))",
4552 .test = alg_test_aead,
4554 .aead = __VECS(aes_gcm_rfc4543_tv_template)
4557 .alg = "rfc7539(chacha20,poly1305)",
4558 .test = alg_test_aead,
4560 .aead = __VECS(rfc7539_tv_template)
4563 .alg = "rfc7539esp(chacha20,poly1305)",
4564 .test = alg_test_aead,
4566 .aead = __VECS(rfc7539esp_tv_template)
4570 .test = alg_test_hash,
4572 .hash = __VECS(rmd128_tv_template)
4576 .test = alg_test_hash,
4578 .hash = __VECS(rmd160_tv_template)
4582 .test = alg_test_hash,
4584 .hash = __VECS(rmd256_tv_template)
4588 .test = alg_test_hash,
4590 .hash = __VECS(rmd320_tv_template)
4594 .test = alg_test_akcipher,
4597 .akcipher = __VECS(rsa_tv_template)
4601 .test = alg_test_skcipher,
4603 .cipher = __VECS(salsa20_stream_tv_template)
4607 .test = alg_test_hash,
4610 .hash = __VECS(sha1_tv_template)
4614 .test = alg_test_hash,
4617 .hash = __VECS(sha224_tv_template)
4621 .test = alg_test_hash,
4624 .hash = __VECS(sha256_tv_template)
4628 .test = alg_test_hash,
4631 .hash = __VECS(sha3_224_tv_template)
4635 .test = alg_test_hash,
4638 .hash = __VECS(sha3_256_tv_template)
4642 .test = alg_test_hash,
4645 .hash = __VECS(sha3_384_tv_template)
4649 .test = alg_test_hash,
4652 .hash = __VECS(sha3_512_tv_template)
4656 .test = alg_test_hash,
4659 .hash = __VECS(sha384_tv_template)
4663 .test = alg_test_hash,
4666 .hash = __VECS(sha512_tv_template)
4670 .test = alg_test_hash,
4672 .hash = __VECS(sm3_tv_template)
4675 .alg = "streebog256",
4676 .test = alg_test_hash,
4678 .hash = __VECS(streebog256_tv_template)
4681 .alg = "streebog512",
4682 .test = alg_test_hash,
4684 .hash = __VECS(streebog512_tv_template)
4688 .test = alg_test_hash,
4690 .hash = __VECS(tgr128_tv_template)
4694 .test = alg_test_hash,
4696 .hash = __VECS(tgr160_tv_template)
4700 .test = alg_test_hash,
4702 .hash = __VECS(tgr192_tv_template)
4705 .alg = "vmac64(aes)",
4706 .test = alg_test_hash,
4708 .hash = __VECS(vmac64_aes_tv_template)
4712 .test = alg_test_hash,
4714 .hash = __VECS(wp256_tv_template)
4718 .test = alg_test_hash,
4720 .hash = __VECS(wp384_tv_template)
4724 .test = alg_test_hash,
4726 .hash = __VECS(wp512_tv_template)
4730 .test = alg_test_hash,
4732 .hash = __VECS(aes_xcbc128_tv_template)
4736 .test = alg_test_skcipher,
4738 .cipher = __VECS(xchacha12_tv_template)
4742 .test = alg_test_skcipher,
4744 .cipher = __VECS(xchacha20_tv_template)
4748 .generic_driver = "xts(ecb(aes-generic))",
4749 .test = alg_test_skcipher,
4752 .cipher = __VECS(aes_xts_tv_template)
4755 .alg = "xts(camellia)",
4756 .generic_driver = "xts(ecb(camellia-generic))",
4757 .test = alg_test_skcipher,
4759 .cipher = __VECS(camellia_xts_tv_template)
4762 .alg = "xts(cast6)",
4763 .generic_driver = "xts(ecb(cast6-generic))",
4764 .test = alg_test_skcipher,
4766 .cipher = __VECS(cast6_xts_tv_template)
4769 /* Same as xts(aes) except the key is stored in
4770 * hardware secure memory which we reference by index
4773 .test = alg_test_null,
4776 .alg = "xts(serpent)",
4777 .generic_driver = "xts(ecb(serpent-generic))",
4778 .test = alg_test_skcipher,
4780 .cipher = __VECS(serpent_xts_tv_template)
4783 .alg = "xts(twofish)",
4784 .generic_driver = "xts(ecb(twofish-generic))",
4785 .test = alg_test_skcipher,
4787 .cipher = __VECS(tf_xts_tv_template)
4790 .alg = "xts4096(paes)",
4791 .test = alg_test_null,
4794 .alg = "xts512(paes)",
4795 .test = alg_test_null,
4798 .alg = "zlib-deflate",
4799 .test = alg_test_comp,
4803 .comp = __VECS(zlib_deflate_comp_tv_template),
4804 .decomp = __VECS(zlib_deflate_decomp_tv_template)
4809 .test = alg_test_comp,
4813 .comp = __VECS(zstd_comp_tv_template),
4814 .decomp = __VECS(zstd_decomp_tv_template)
4820 static void alg_check_test_descs_order(void)
4824 for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
4825 int diff = strcmp(alg_test_descs[i - 1].alg,
4826 alg_test_descs[i].alg);
4828 if (WARN_ON(diff > 0)) {
4829 pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
4830 alg_test_descs[i - 1].alg,
4831 alg_test_descs[i].alg);
4834 if (WARN_ON(diff == 0)) {
4835 pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
4836 alg_test_descs[i].alg);
4841 static void alg_check_testvec_configs(void)
4845 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++)
4846 WARN_ON(!valid_testvec_config(
4847 &default_cipher_testvec_configs[i]));
4849 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++)
4850 WARN_ON(!valid_testvec_config(
4851 &default_hash_testvec_configs[i]));
4854 static void testmgr_onetime_init(void)
4856 alg_check_test_descs_order();
4857 alg_check_testvec_configs();
4859 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
4860 pr_warn("alg: extra crypto tests enabled. This is intended for developer use only.\n");
4864 static int alg_find_test(const char *alg)
4867 int end = ARRAY_SIZE(alg_test_descs);
4869 while (start < end) {
4870 int i = (start + end) / 2;
4871 int diff = strcmp(alg_test_descs[i].alg, alg);
4889 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
4895 if (!fips_enabled && notests) {
4896 printk_once(KERN_INFO "alg: self-tests disabled\n");
4900 DO_ONCE(testmgr_onetime_init);
4902 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
4903 char nalg[CRYPTO_MAX_ALG_NAME];
4905 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
4907 return -ENAMETOOLONG;
4909 i = alg_find_test(nalg);
4913 if (fips_enabled && !alg_test_descs[i].fips_allowed)
4916 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
4920 i = alg_find_test(alg);
4921 j = alg_find_test(driver);
4925 if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
4926 (j >= 0 && !alg_test_descs[j].fips_allowed)))
4931 rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
4933 if (j >= 0 && j != i)
4934 rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
4938 if (rc && (fips_enabled || panic_on_fail))
4939 panic("alg: self-tests for %s (%s) failed in %s mode!\n",
4940 driver, alg, fips_enabled ? "fips" : "panic_on_fail");
4942 if (fips_enabled && !rc)
4943 pr_info("alg: self-tests for %s (%s) passed\n", driver, alg);
4948 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
4954 #endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
4956 EXPORT_SYMBOL_GPL(alg_test);