1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Algorithm testing framework and tests.
5 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
6 * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
7 * Copyright (c) 2007 Nokia Siemens Networks
8 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
9 * Copyright (c) 2019 Google LLC
11 * Updated RFC4106 AES-GCM testing.
12 * Authors: Aidan O'Mahony (aidan.o.mahony@intel.com)
13 * Adrian Hoban <adrian.hoban@intel.com>
14 * Gabriele Paoloni <gabriele.paoloni@intel.com>
15 * Tadeusz Struk (tadeusz.struk@intel.com)
16 * Copyright (c) 2010, Intel Corporation.
19 #include <crypto/aead.h>
20 #include <crypto/hash.h>
21 #include <crypto/skcipher.h>
22 #include <linux/err.h>
23 #include <linux/fips.h>
24 #include <linux/module.h>
25 #include <linux/once.h>
26 #include <linux/random.h>
27 #include <linux/scatterlist.h>
28 #include <linux/slab.h>
29 #include <linux/string.h>
30 #include <linux/uio.h>
31 #include <crypto/rng.h>
32 #include <crypto/drbg.h>
33 #include <crypto/akcipher.h>
34 #include <crypto/kpp.h>
35 #include <crypto/acompress.h>
36 #include <crypto/internal/cipher.h>
37 #include <crypto/internal/simd.h>
41 MODULE_IMPORT_NS(CRYPTO_INTERNAL);
44 module_param(notests, bool, 0644);
45 MODULE_PARM_DESC(notests, "disable crypto self-tests");
47 static bool panic_on_fail;
48 module_param(panic_on_fail, bool, 0444);
50 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
51 static bool noextratests;
52 module_param(noextratests, bool, 0644);
53 MODULE_PARM_DESC(noextratests, "disable expensive crypto self-tests");
55 static unsigned int fuzz_iterations = 100;
56 module_param(fuzz_iterations, uint, 0644);
57 MODULE_PARM_DESC(fuzz_iterations, "number of fuzz test iterations");
60 #ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
63 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
73 * Need slab memory for testing (size in number of pages).
78 * Used by test_cipher()
83 struct aead_test_suite {
84 const struct aead_testvec *vecs;
88 * Set if trying to decrypt an inauthentic ciphertext with this
89 * algorithm might result in EINVAL rather than EBADMSG, due to other
90 * validation the algorithm does on the inputs such as length checks.
92 unsigned int einval_allowed : 1;
95 * Set if this algorithm requires that the IV be located at the end of
96 * the AAD buffer, in addition to being given in the normal way. The
97 * behavior when the two IV copies differ is implementation-defined.
99 unsigned int aad_iv : 1;
102 struct cipher_test_suite {
103 const struct cipher_testvec *vecs;
107 struct comp_test_suite {
109 const struct comp_testvec *vecs;
114 struct hash_test_suite {
115 const struct hash_testvec *vecs;
119 struct cprng_test_suite {
120 const struct cprng_testvec *vecs;
124 struct drbg_test_suite {
125 const struct drbg_testvec *vecs;
129 struct akcipher_test_suite {
130 const struct akcipher_testvec *vecs;
134 struct kpp_test_suite {
135 const struct kpp_testvec *vecs;
139 struct alg_test_desc {
141 const char *generic_driver;
142 int (*test)(const struct alg_test_desc *desc, const char *driver,
144 int fips_allowed; /* set if alg is allowed in fips mode */
147 struct aead_test_suite aead;
148 struct cipher_test_suite cipher;
149 struct comp_test_suite comp;
150 struct hash_test_suite hash;
151 struct cprng_test_suite cprng;
152 struct drbg_test_suite drbg;
153 struct akcipher_test_suite akcipher;
154 struct kpp_test_suite kpp;
158 static void hexdump(unsigned char *buf, unsigned int len)
160 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
165 static int __testmgr_alloc_buf(char *buf[XBUFSIZE], int order)
169 for (i = 0; i < XBUFSIZE; i++) {
170 buf[i] = (char *)__get_free_pages(GFP_KERNEL, order);
179 free_pages((unsigned long)buf[i], order);
184 static int testmgr_alloc_buf(char *buf[XBUFSIZE])
186 return __testmgr_alloc_buf(buf, 0);
189 static void __testmgr_free_buf(char *buf[XBUFSIZE], int order)
193 for (i = 0; i < XBUFSIZE; i++)
194 free_pages((unsigned long)buf[i], order);
197 static void testmgr_free_buf(char *buf[XBUFSIZE])
199 __testmgr_free_buf(buf, 0);
202 #define TESTMGR_POISON_BYTE 0xfe
203 #define TESTMGR_POISON_LEN 16
205 static inline void testmgr_poison(void *addr, size_t len)
207 memset(addr, TESTMGR_POISON_BYTE, len);
210 /* Is the memory region still fully poisoned? */
211 static inline bool testmgr_is_poison(const void *addr, size_t len)
213 return memchr_inv(addr, TESTMGR_POISON_BYTE, len) == NULL;
216 /* flush type for hash algorithms */
218 /* merge with update of previous buffer(s) */
221 /* update with previous buffer(s) before doing this one */
224 /* likewise, but also export and re-import the intermediate state */
228 /* finalization function for hash algorithms */
229 enum finalization_type {
230 FINALIZATION_TYPE_FINAL, /* use final() */
231 FINALIZATION_TYPE_FINUP, /* use finup() */
232 FINALIZATION_TYPE_DIGEST, /* use digest() */
236 * Whether the crypto operation will occur in-place, and if so whether the
237 * source and destination scatterlist pointers will coincide (req->src ==
238 * req->dst), or whether they'll merely point to two separate scatterlists
239 * (req->src != req->dst) that reference the same underlying memory.
241 * This is only relevant for algorithm types that support in-place operation.
249 #define TEST_SG_TOTAL 10000
252 * struct test_sg_division - description of a scatterlist entry
254 * This struct describes one entry of a scatterlist being constructed to check a
255 * crypto test vector.
257 * @proportion_of_total: length of this chunk relative to the total length,
258 * given as a proportion out of TEST_SG_TOTAL so that it
259 * scales to fit any test vector
260 * @offset: byte offset into a 2-page buffer at which this chunk will start
261 * @offset_relative_to_alignmask: if true, add the algorithm's alignmask to the
263 * @flush_type: for hashes, whether an update() should be done now vs.
264 * continuing to accumulate data
265 * @nosimd: if doing the pending update(), do it with SIMD disabled?
267 struct test_sg_division {
268 unsigned int proportion_of_total;
270 bool offset_relative_to_alignmask;
271 enum flush_type flush_type;
276 * struct testvec_config - configuration for testing a crypto test vector
278 * This struct describes the data layout and other parameters with which each
279 * crypto test vector can be tested.
281 * @name: name of this config, logged for debugging purposes if a test fails
282 * @inplace_mode: whether and how to operate on the data in-place, if applicable
283 * @req_flags: extra request_flags, e.g. CRYPTO_TFM_REQ_MAY_SLEEP
284 * @src_divs: description of how to arrange the source scatterlist
285 * @dst_divs: description of how to arrange the dst scatterlist, if applicable
286 * for the algorithm type. Defaults to @src_divs if unset.
287 * @iv_offset: misalignment of the IV in the range [0..MAX_ALGAPI_ALIGNMASK+1],
288 * where 0 is aligned to a 2*(MAX_ALGAPI_ALIGNMASK+1) byte boundary
289 * @iv_offset_relative_to_alignmask: if true, add the algorithm's alignmask to
291 * @key_offset: misalignment of the key, where 0 is default alignment
292 * @key_offset_relative_to_alignmask: if true, add the algorithm's alignmask to
294 * @finalization_type: what finalization function to use for hashes
295 * @nosimd: execute with SIMD disabled? Requires !CRYPTO_TFM_REQ_MAY_SLEEP.
297 struct testvec_config {
299 enum inplace_mode inplace_mode;
301 struct test_sg_division src_divs[XBUFSIZE];
302 struct test_sg_division dst_divs[XBUFSIZE];
303 unsigned int iv_offset;
304 unsigned int key_offset;
305 bool iv_offset_relative_to_alignmask;
306 bool key_offset_relative_to_alignmask;
307 enum finalization_type finalization_type;
311 #define TESTVEC_CONFIG_NAMELEN 192
314 * The following are the lists of testvec_configs to test for each algorithm
315 * type when the basic crypto self-tests are enabled, i.e. when
316 * CONFIG_CRYPTO_MANAGER_DISABLE_TESTS is unset. They aim to provide good test
317 * coverage, while keeping the test time much shorter than the full fuzz tests
318 * so that the basic tests can be enabled in a wider range of circumstances.
321 /* Configs for skciphers and aeads */
322 static const struct testvec_config default_cipher_testvec_configs[] = {
324 .name = "in-place (one sglist)",
325 .inplace_mode = INPLACE_ONE_SGLIST,
326 .src_divs = { { .proportion_of_total = 10000 } },
328 .name = "in-place (two sglists)",
329 .inplace_mode = INPLACE_TWO_SGLISTS,
330 .src_divs = { { .proportion_of_total = 10000 } },
332 .name = "out-of-place",
333 .inplace_mode = OUT_OF_PLACE,
334 .src_divs = { { .proportion_of_total = 10000 } },
336 .name = "unaligned buffer, offset=1",
337 .src_divs = { { .proportion_of_total = 10000, .offset = 1 } },
341 .name = "buffer aligned only to alignmask",
344 .proportion_of_total = 10000,
346 .offset_relative_to_alignmask = true,
350 .iv_offset_relative_to_alignmask = true,
352 .key_offset_relative_to_alignmask = true,
354 .name = "two even aligned splits",
356 { .proportion_of_total = 5000 },
357 { .proportion_of_total = 5000 },
360 .name = "one src, two even splits dst",
361 .inplace_mode = OUT_OF_PLACE,
362 .src_divs = { { .proportion_of_total = 10000 } },
364 { .proportion_of_total = 5000 },
365 { .proportion_of_total = 5000 },
368 .name = "uneven misaligned splits, may sleep",
369 .req_flags = CRYPTO_TFM_REQ_MAY_SLEEP,
371 { .proportion_of_total = 1900, .offset = 33 },
372 { .proportion_of_total = 3300, .offset = 7 },
373 { .proportion_of_total = 4800, .offset = 18 },
378 .name = "misaligned splits crossing pages, inplace",
379 .inplace_mode = INPLACE_ONE_SGLIST,
382 .proportion_of_total = 7500,
383 .offset = PAGE_SIZE - 32
385 .proportion_of_total = 2500,
386 .offset = PAGE_SIZE - 7
392 static const struct testvec_config default_hash_testvec_configs[] = {
394 .name = "init+update+final aligned buffer",
395 .src_divs = { { .proportion_of_total = 10000 } },
396 .finalization_type = FINALIZATION_TYPE_FINAL,
398 .name = "init+finup aligned buffer",
399 .src_divs = { { .proportion_of_total = 10000 } },
400 .finalization_type = FINALIZATION_TYPE_FINUP,
402 .name = "digest aligned buffer",
403 .src_divs = { { .proportion_of_total = 10000 } },
404 .finalization_type = FINALIZATION_TYPE_DIGEST,
406 .name = "init+update+final misaligned buffer",
407 .src_divs = { { .proportion_of_total = 10000, .offset = 1 } },
408 .finalization_type = FINALIZATION_TYPE_FINAL,
411 .name = "digest buffer aligned only to alignmask",
414 .proportion_of_total = 10000,
416 .offset_relative_to_alignmask = true,
419 .finalization_type = FINALIZATION_TYPE_DIGEST,
421 .key_offset_relative_to_alignmask = true,
423 .name = "init+update+update+final two even splits",
425 { .proportion_of_total = 5000 },
427 .proportion_of_total = 5000,
428 .flush_type = FLUSH_TYPE_FLUSH,
431 .finalization_type = FINALIZATION_TYPE_FINAL,
433 .name = "digest uneven misaligned splits, may sleep",
434 .req_flags = CRYPTO_TFM_REQ_MAY_SLEEP,
436 { .proportion_of_total = 1900, .offset = 33 },
437 { .proportion_of_total = 3300, .offset = 7 },
438 { .proportion_of_total = 4800, .offset = 18 },
440 .finalization_type = FINALIZATION_TYPE_DIGEST,
442 .name = "digest misaligned splits crossing pages",
445 .proportion_of_total = 7500,
446 .offset = PAGE_SIZE - 32,
448 .proportion_of_total = 2500,
449 .offset = PAGE_SIZE - 7,
452 .finalization_type = FINALIZATION_TYPE_DIGEST,
454 .name = "import/export",
457 .proportion_of_total = 6500,
458 .flush_type = FLUSH_TYPE_REIMPORT,
460 .proportion_of_total = 3500,
461 .flush_type = FLUSH_TYPE_REIMPORT,
464 .finalization_type = FINALIZATION_TYPE_FINAL,
468 static unsigned int count_test_sg_divisions(const struct test_sg_division *divs)
470 unsigned int remaining = TEST_SG_TOTAL;
471 unsigned int ndivs = 0;
474 remaining -= divs[ndivs++].proportion_of_total;
480 #define SGDIVS_HAVE_FLUSHES BIT(0)
481 #define SGDIVS_HAVE_NOSIMD BIT(1)
483 static bool valid_sg_divisions(const struct test_sg_division *divs,
484 unsigned int count, int *flags_ret)
486 unsigned int total = 0;
489 for (i = 0; i < count && total != TEST_SG_TOTAL; i++) {
490 if (divs[i].proportion_of_total <= 0 ||
491 divs[i].proportion_of_total > TEST_SG_TOTAL - total)
493 total += divs[i].proportion_of_total;
494 if (divs[i].flush_type != FLUSH_TYPE_NONE)
495 *flags_ret |= SGDIVS_HAVE_FLUSHES;
497 *flags_ret |= SGDIVS_HAVE_NOSIMD;
499 return total == TEST_SG_TOTAL &&
500 memchr_inv(&divs[i], 0, (count - i) * sizeof(divs[0])) == NULL;
504 * Check whether the given testvec_config is valid. This isn't strictly needed
505 * since every testvec_config should be valid, but check anyway so that people
506 * don't unknowingly add broken configs that don't do what they wanted.
508 static bool valid_testvec_config(const struct testvec_config *cfg)
512 if (cfg->name == NULL)
515 if (!valid_sg_divisions(cfg->src_divs, ARRAY_SIZE(cfg->src_divs),
519 if (cfg->dst_divs[0].proportion_of_total) {
520 if (!valid_sg_divisions(cfg->dst_divs,
521 ARRAY_SIZE(cfg->dst_divs), &flags))
524 if (memchr_inv(cfg->dst_divs, 0, sizeof(cfg->dst_divs)))
526 /* defaults to dst_divs=src_divs */
530 (cfg->iv_offset_relative_to_alignmask ? MAX_ALGAPI_ALIGNMASK : 0) >
531 MAX_ALGAPI_ALIGNMASK + 1)
534 if ((flags & (SGDIVS_HAVE_FLUSHES | SGDIVS_HAVE_NOSIMD)) &&
535 cfg->finalization_type == FINALIZATION_TYPE_DIGEST)
538 if ((cfg->nosimd || (flags & SGDIVS_HAVE_NOSIMD)) &&
539 (cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP))
546 char *bufs[XBUFSIZE];
547 struct scatterlist sgl[XBUFSIZE];
548 struct scatterlist sgl_saved[XBUFSIZE];
549 struct scatterlist *sgl_ptr;
553 static int init_test_sglist(struct test_sglist *tsgl)
555 return __testmgr_alloc_buf(tsgl->bufs, 1 /* two pages per buffer */);
558 static void destroy_test_sglist(struct test_sglist *tsgl)
560 return __testmgr_free_buf(tsgl->bufs, 1 /* two pages per buffer */);
564 * build_test_sglist() - build a scatterlist for a crypto test
566 * @tsgl: the scatterlist to build. @tsgl->bufs[] contains an array of 2-page
567 * buffers which the scatterlist @tsgl->sgl[] will be made to point into.
568 * @divs: the layout specification on which the scatterlist will be based
569 * @alignmask: the algorithm's alignmask
570 * @total_len: the total length of the scatterlist to build in bytes
571 * @data: if non-NULL, the buffers will be filled with this data until it ends.
572 * Otherwise the buffers will be poisoned. In both cases, some bytes
573 * past the end of each buffer will be poisoned to help detect overruns.
574 * @out_divs: if non-NULL, the test_sg_division to which each scatterlist entry
575 * corresponds will be returned here. This will match @divs except
576 * that divisions resolving to a length of 0 are omitted as they are
577 * not included in the scatterlist.
579 * Return: 0 or a -errno value
581 static int build_test_sglist(struct test_sglist *tsgl,
582 const struct test_sg_division *divs,
583 const unsigned int alignmask,
584 const unsigned int total_len,
585 struct iov_iter *data,
586 const struct test_sg_division *out_divs[XBUFSIZE])
589 const struct test_sg_division *div;
591 } partitions[XBUFSIZE];
592 const unsigned int ndivs = count_test_sg_divisions(divs);
593 unsigned int len_remaining = total_len;
596 BUILD_BUG_ON(ARRAY_SIZE(partitions) != ARRAY_SIZE(tsgl->sgl));
597 if (WARN_ON(ndivs > ARRAY_SIZE(partitions)))
600 /* Calculate the (div, length) pairs */
602 for (i = 0; i < ndivs; i++) {
603 unsigned int len_this_sg =
605 (total_len * divs[i].proportion_of_total +
606 TEST_SG_TOTAL / 2) / TEST_SG_TOTAL);
608 if (len_this_sg != 0) {
609 partitions[tsgl->nents].div = &divs[i];
610 partitions[tsgl->nents].length = len_this_sg;
612 len_remaining -= len_this_sg;
615 if (tsgl->nents == 0) {
616 partitions[tsgl->nents].div = &divs[0];
617 partitions[tsgl->nents].length = 0;
620 partitions[tsgl->nents - 1].length += len_remaining;
622 /* Set up the sgl entries and fill the data or poison */
623 sg_init_table(tsgl->sgl, tsgl->nents);
624 for (i = 0; i < tsgl->nents; i++) {
625 unsigned int offset = partitions[i].div->offset;
628 if (partitions[i].div->offset_relative_to_alignmask)
631 while (offset + partitions[i].length + TESTMGR_POISON_LEN >
633 if (WARN_ON(offset <= 0))
638 addr = &tsgl->bufs[i][offset];
639 sg_set_buf(&tsgl->sgl[i], addr, partitions[i].length);
642 out_divs[i] = partitions[i].div;
645 size_t copy_len, copied;
647 copy_len = min(partitions[i].length, data->count);
648 copied = copy_from_iter(addr, copy_len, data);
649 if (WARN_ON(copied != copy_len))
651 testmgr_poison(addr + copy_len, partitions[i].length +
652 TESTMGR_POISON_LEN - copy_len);
654 testmgr_poison(addr, partitions[i].length +
659 sg_mark_end(&tsgl->sgl[tsgl->nents - 1]);
660 tsgl->sgl_ptr = tsgl->sgl;
661 memcpy(tsgl->sgl_saved, tsgl->sgl, tsgl->nents * sizeof(tsgl->sgl[0]));
666 * Verify that a scatterlist crypto operation produced the correct output.
668 * @tsgl: scatterlist containing the actual output
669 * @expected_output: buffer containing the expected output
670 * @len_to_check: length of @expected_output in bytes
671 * @unchecked_prefix_len: number of ignored bytes in @tsgl prior to real result
672 * @check_poison: verify that the poison bytes after each chunk are intact?
674 * Return: 0 if correct, -EINVAL if incorrect, -EOVERFLOW if buffer overrun.
676 static int verify_correct_output(const struct test_sglist *tsgl,
677 const char *expected_output,
678 unsigned int len_to_check,
679 unsigned int unchecked_prefix_len,
684 for (i = 0; i < tsgl->nents; i++) {
685 struct scatterlist *sg = &tsgl->sgl_ptr[i];
686 unsigned int len = sg->length;
687 unsigned int offset = sg->offset;
688 const char *actual_output;
690 if (unchecked_prefix_len) {
691 if (unchecked_prefix_len >= len) {
692 unchecked_prefix_len -= len;
695 offset += unchecked_prefix_len;
696 len -= unchecked_prefix_len;
697 unchecked_prefix_len = 0;
699 len = min(len, len_to_check);
700 actual_output = page_address(sg_page(sg)) + offset;
701 if (memcmp(expected_output, actual_output, len) != 0)
704 !testmgr_is_poison(actual_output + len, TESTMGR_POISON_LEN))
707 expected_output += len;
709 if (WARN_ON(len_to_check != 0))
714 static bool is_test_sglist_corrupted(const struct test_sglist *tsgl)
718 for (i = 0; i < tsgl->nents; i++) {
719 if (tsgl->sgl[i].page_link != tsgl->sgl_saved[i].page_link)
721 if (tsgl->sgl[i].offset != tsgl->sgl_saved[i].offset)
723 if (tsgl->sgl[i].length != tsgl->sgl_saved[i].length)
729 struct cipher_test_sglists {
730 struct test_sglist src;
731 struct test_sglist dst;
734 static struct cipher_test_sglists *alloc_cipher_test_sglists(void)
736 struct cipher_test_sglists *tsgls;
738 tsgls = kmalloc(sizeof(*tsgls), GFP_KERNEL);
742 if (init_test_sglist(&tsgls->src) != 0)
744 if (init_test_sglist(&tsgls->dst) != 0)
745 goto fail_destroy_src;
750 destroy_test_sglist(&tsgls->src);
756 static void free_cipher_test_sglists(struct cipher_test_sglists *tsgls)
759 destroy_test_sglist(&tsgls->src);
760 destroy_test_sglist(&tsgls->dst);
765 /* Build the src and dst scatterlists for an skcipher or AEAD test */
766 static int build_cipher_test_sglists(struct cipher_test_sglists *tsgls,
767 const struct testvec_config *cfg,
768 unsigned int alignmask,
769 unsigned int src_total_len,
770 unsigned int dst_total_len,
771 const struct kvec *inputs,
772 unsigned int nr_inputs)
774 struct iov_iter input;
777 iov_iter_kvec(&input, ITER_SOURCE, inputs, nr_inputs, src_total_len);
778 err = build_test_sglist(&tsgls->src, cfg->src_divs, alignmask,
779 cfg->inplace_mode != OUT_OF_PLACE ?
780 max(dst_total_len, src_total_len) :
787 * In-place crypto operations can use the same scatterlist for both the
788 * source and destination (req->src == req->dst), or can use separate
789 * scatterlists (req->src != req->dst) which point to the same
790 * underlying memory. Make sure to test both cases.
792 if (cfg->inplace_mode == INPLACE_ONE_SGLIST) {
793 tsgls->dst.sgl_ptr = tsgls->src.sgl;
794 tsgls->dst.nents = tsgls->src.nents;
797 if (cfg->inplace_mode == INPLACE_TWO_SGLISTS) {
799 * For now we keep it simple and only test the case where the
800 * two scatterlists have identical entries, rather than
801 * different entries that split up the same memory differently.
803 memcpy(tsgls->dst.sgl, tsgls->src.sgl,
804 tsgls->src.nents * sizeof(tsgls->src.sgl[0]));
805 memcpy(tsgls->dst.sgl_saved, tsgls->src.sgl,
806 tsgls->src.nents * sizeof(tsgls->src.sgl[0]));
807 tsgls->dst.sgl_ptr = tsgls->dst.sgl;
808 tsgls->dst.nents = tsgls->src.nents;
812 return build_test_sglist(&tsgls->dst,
813 cfg->dst_divs[0].proportion_of_total ?
814 cfg->dst_divs : cfg->src_divs,
815 alignmask, dst_total_len, NULL, NULL);
819 * Support for testing passing a misaligned key to setkey():
821 * If cfg->key_offset is set, copy the key into a new buffer at that offset,
822 * optionally adding alignmask. Else, just use the key directly.
824 static int prepare_keybuf(const u8 *key, unsigned int ksize,
825 const struct testvec_config *cfg,
826 unsigned int alignmask,
827 const u8 **keybuf_ret, const u8 **keyptr_ret)
829 unsigned int key_offset = cfg->key_offset;
830 u8 *keybuf = NULL, *keyptr = (u8 *)key;
832 if (key_offset != 0) {
833 if (cfg->key_offset_relative_to_alignmask)
834 key_offset += alignmask;
835 keybuf = kmalloc(key_offset + ksize, GFP_KERNEL);
838 keyptr = keybuf + key_offset;
839 memcpy(keyptr, key, ksize);
841 *keybuf_ret = keybuf;
842 *keyptr_ret = keyptr;
846 /* Like setkey_f(tfm, key, ksize), but sometimes misalign the key */
847 #define do_setkey(setkey_f, tfm, key, ksize, cfg, alignmask) \
849 const u8 *keybuf, *keyptr; \
852 err = prepare_keybuf((key), (ksize), (cfg), (alignmask), \
855 err = setkey_f((tfm), keyptr, (ksize)); \
861 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
864 * The fuzz tests use prandom instead of the normal Linux RNG since they don't
865 * need cryptographically secure random numbers. This greatly improves the
866 * performance of these tests, especially if they are run before the Linux RNG
867 * has been initialized or if they are run on a lockdep-enabled kernel.
870 static inline void init_rnd_state(struct rnd_state *rng)
872 prandom_seed_state(rng, get_random_u64());
875 static inline u8 prandom_u8(struct rnd_state *rng)
877 return prandom_u32_state(rng);
880 static inline u32 prandom_u32_below(struct rnd_state *rng, u32 ceil)
883 * This is slightly biased for non-power-of-2 values of 'ceil', but this
884 * isn't important here.
886 return prandom_u32_state(rng) % ceil;
889 static inline bool prandom_bool(struct rnd_state *rng)
891 return prandom_u32_below(rng, 2);
894 static inline u32 prandom_u32_inclusive(struct rnd_state *rng,
897 return floor + prandom_u32_below(rng, ceil - floor + 1);
900 /* Generate a random length in range [0, max_len], but prefer smaller values */
901 static unsigned int generate_random_length(struct rnd_state *rng,
902 unsigned int max_len)
904 unsigned int len = prandom_u32_below(rng, max_len + 1);
906 switch (prandom_u32_below(rng, 4)) {
918 /* Flip a random bit in the given nonempty data buffer */
919 static void flip_random_bit(struct rnd_state *rng, u8 *buf, size_t size)
923 bitpos = prandom_u32_below(rng, size * 8);
924 buf[bitpos / 8] ^= 1 << (bitpos % 8);
927 /* Flip a random byte in the given nonempty data buffer */
928 static void flip_random_byte(struct rnd_state *rng, u8 *buf, size_t size)
930 buf[prandom_u32_below(rng, size)] ^= 0xff;
933 /* Sometimes make some random changes to the given nonempty data buffer */
934 static void mutate_buffer(struct rnd_state *rng, u8 *buf, size_t size)
939 /* Sometimes flip some bits */
940 if (prandom_u32_below(rng, 4) == 0) {
941 num_flips = min_t(size_t, 1 << prandom_u32_below(rng, 8),
943 for (i = 0; i < num_flips; i++)
944 flip_random_bit(rng, buf, size);
947 /* Sometimes flip some bytes */
948 if (prandom_u32_below(rng, 4) == 0) {
949 num_flips = min_t(size_t, 1 << prandom_u32_below(rng, 8), size);
950 for (i = 0; i < num_flips; i++)
951 flip_random_byte(rng, buf, size);
955 /* Randomly generate 'count' bytes, but sometimes make them "interesting" */
956 static void generate_random_bytes(struct rnd_state *rng, u8 *buf, size_t count)
965 switch (prandom_u32_below(rng, 8)) { /* Choose a generation strategy */
968 /* All the same byte, plus optional mutations */
969 switch (prandom_u32_below(rng, 4)) {
980 memset(buf, b, count);
981 mutate_buffer(rng, buf, count);
984 /* Ascending or descending bytes, plus optional mutations */
985 increment = prandom_u8(rng);
987 for (i = 0; i < count; i++, b += increment)
989 mutate_buffer(rng, buf, count);
992 /* Fully random bytes */
993 prandom_bytes_state(rng, buf, count);
997 static char *generate_random_sgl_divisions(struct rnd_state *rng,
998 struct test_sg_division *divs,
999 size_t max_divs, char *p, char *end,
1000 bool gen_flushes, u32 req_flags)
1002 struct test_sg_division *div = divs;
1003 unsigned int remaining = TEST_SG_TOTAL;
1006 unsigned int this_len;
1007 const char *flushtype_str;
1009 if (div == &divs[max_divs - 1] || prandom_bool(rng))
1010 this_len = remaining;
1012 this_len = prandom_u32_inclusive(rng, 1, remaining);
1013 div->proportion_of_total = this_len;
1015 if (prandom_u32_below(rng, 4) == 0)
1016 div->offset = prandom_u32_inclusive(rng,
1019 else if (prandom_bool(rng))
1020 div->offset = prandom_u32_below(rng, 32);
1022 div->offset = prandom_u32_below(rng, PAGE_SIZE);
1023 if (prandom_u32_below(rng, 8) == 0)
1024 div->offset_relative_to_alignmask = true;
1026 div->flush_type = FLUSH_TYPE_NONE;
1028 switch (prandom_u32_below(rng, 4)) {
1030 div->flush_type = FLUSH_TYPE_REIMPORT;
1033 div->flush_type = FLUSH_TYPE_FLUSH;
1038 if (div->flush_type != FLUSH_TYPE_NONE &&
1039 !(req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) &&
1043 switch (div->flush_type) {
1044 case FLUSH_TYPE_FLUSH:
1046 flushtype_str = "<flush,nosimd>";
1048 flushtype_str = "<flush>";
1050 case FLUSH_TYPE_REIMPORT:
1052 flushtype_str = "<reimport,nosimd>";
1054 flushtype_str = "<reimport>";
1061 BUILD_BUG_ON(TEST_SG_TOTAL != 10000); /* for "%u.%u%%" */
1062 p += scnprintf(p, end - p, "%s%u.%u%%@%s+%u%s", flushtype_str,
1063 this_len / 100, this_len % 100,
1064 div->offset_relative_to_alignmask ?
1066 div->offset, this_len == remaining ? "" : ", ");
1067 remaining -= this_len;
1069 } while (remaining);
1074 /* Generate a random testvec_config for fuzz testing */
1075 static void generate_random_testvec_config(struct rnd_state *rng,
1076 struct testvec_config *cfg,
1077 char *name, size_t max_namelen)
1080 char * const end = name + max_namelen;
1082 memset(cfg, 0, sizeof(*cfg));
1086 p += scnprintf(p, end - p, "random:");
1088 switch (prandom_u32_below(rng, 4)) {
1091 cfg->inplace_mode = OUT_OF_PLACE;
1094 cfg->inplace_mode = INPLACE_ONE_SGLIST;
1095 p += scnprintf(p, end - p, " inplace_one_sglist");
1098 cfg->inplace_mode = INPLACE_TWO_SGLISTS;
1099 p += scnprintf(p, end - p, " inplace_two_sglists");
1103 if (prandom_bool(rng)) {
1104 cfg->req_flags |= CRYPTO_TFM_REQ_MAY_SLEEP;
1105 p += scnprintf(p, end - p, " may_sleep");
1108 switch (prandom_u32_below(rng, 4)) {
1110 cfg->finalization_type = FINALIZATION_TYPE_FINAL;
1111 p += scnprintf(p, end - p, " use_final");
1114 cfg->finalization_type = FINALIZATION_TYPE_FINUP;
1115 p += scnprintf(p, end - p, " use_finup");
1118 cfg->finalization_type = FINALIZATION_TYPE_DIGEST;
1119 p += scnprintf(p, end - p, " use_digest");
1123 if (!(cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) && prandom_bool(rng)) {
1125 p += scnprintf(p, end - p, " nosimd");
1128 p += scnprintf(p, end - p, " src_divs=[");
1129 p = generate_random_sgl_divisions(rng, cfg->src_divs,
1130 ARRAY_SIZE(cfg->src_divs), p, end,
1131 (cfg->finalization_type !=
1132 FINALIZATION_TYPE_DIGEST),
1134 p += scnprintf(p, end - p, "]");
1136 if (cfg->inplace_mode == OUT_OF_PLACE && prandom_bool(rng)) {
1137 p += scnprintf(p, end - p, " dst_divs=[");
1138 p = generate_random_sgl_divisions(rng, cfg->dst_divs,
1139 ARRAY_SIZE(cfg->dst_divs),
1142 p += scnprintf(p, end - p, "]");
1145 if (prandom_bool(rng)) {
1146 cfg->iv_offset = prandom_u32_inclusive(rng, 1,
1147 MAX_ALGAPI_ALIGNMASK);
1148 p += scnprintf(p, end - p, " iv_offset=%u", cfg->iv_offset);
1151 if (prandom_bool(rng)) {
1152 cfg->key_offset = prandom_u32_inclusive(rng, 1,
1153 MAX_ALGAPI_ALIGNMASK);
1154 p += scnprintf(p, end - p, " key_offset=%u", cfg->key_offset);
1157 WARN_ON_ONCE(!valid_testvec_config(cfg));
1160 static void crypto_disable_simd_for_test(void)
1163 __this_cpu_write(crypto_simd_disabled_for_test, true);
1166 static void crypto_reenable_simd_for_test(void)
1168 __this_cpu_write(crypto_simd_disabled_for_test, false);
1173 * Given an algorithm name, build the name of the generic implementation of that
1174 * algorithm, assuming the usual naming convention. Specifically, this appends
1175 * "-generic" to every part of the name that is not a template name. Examples:
1177 * aes => aes-generic
1178 * cbc(aes) => cbc(aes-generic)
1179 * cts(cbc(aes)) => cts(cbc(aes-generic))
1180 * rfc7539(chacha20,poly1305) => rfc7539(chacha20-generic,poly1305-generic)
1182 * Return: 0 on success, or -ENAMETOOLONG if the generic name would be too long
1184 static int build_generic_driver_name(const char *algname,
1185 char driver_name[CRYPTO_MAX_ALG_NAME])
1187 const char *in = algname;
1188 char *out = driver_name;
1189 size_t len = strlen(algname);
1191 if (len >= CRYPTO_MAX_ALG_NAME)
1194 const char *in_saved = in;
1196 while (*in && *in != '(' && *in != ')' && *in != ',')
1198 if (*in != '(' && in > in_saved) {
1200 if (len >= CRYPTO_MAX_ALG_NAME)
1202 memcpy(out, "-generic", 8);
1205 } while ((*out++ = *in++) != '\0');
1209 pr_err("alg: generic driver name for \"%s\" would be too long\n",
1211 return -ENAMETOOLONG;
1213 #else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1214 static void crypto_disable_simd_for_test(void)
1218 static void crypto_reenable_simd_for_test(void)
1221 #endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1223 static int build_hash_sglist(struct test_sglist *tsgl,
1224 const struct hash_testvec *vec,
1225 const struct testvec_config *cfg,
1226 unsigned int alignmask,
1227 const struct test_sg_division *divs[XBUFSIZE])
1230 struct iov_iter input;
1232 kv.iov_base = (void *)vec->plaintext;
1233 kv.iov_len = vec->psize;
1234 iov_iter_kvec(&input, ITER_SOURCE, &kv, 1, vec->psize);
1235 return build_test_sglist(tsgl, cfg->src_divs, alignmask, vec->psize,
1239 static int check_hash_result(const char *type,
1240 const u8 *result, unsigned int digestsize,
1241 const struct hash_testvec *vec,
1242 const char *vec_name,
1244 const struct testvec_config *cfg)
1246 if (memcmp(result, vec->digest, digestsize) != 0) {
1247 pr_err("alg: %s: %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
1248 type, driver, vec_name, cfg->name);
1251 if (!testmgr_is_poison(&result[digestsize], TESTMGR_POISON_LEN)) {
1252 pr_err("alg: %s: %s overran result buffer on test vector %s, cfg=\"%s\"\n",
1253 type, driver, vec_name, cfg->name);
1259 static inline int check_shash_op(const char *op, int err,
1260 const char *driver, const char *vec_name,
1261 const struct testvec_config *cfg)
1264 pr_err("alg: shash: %s %s() failed with err %d on test vector %s, cfg=\"%s\"\n",
1265 driver, op, err, vec_name, cfg->name);
1269 /* Test one hash test vector in one configuration, using the shash API */
1270 static int test_shash_vec_cfg(const struct hash_testvec *vec,
1271 const char *vec_name,
1272 const struct testvec_config *cfg,
1273 struct shash_desc *desc,
1274 struct test_sglist *tsgl,
1277 struct crypto_shash *tfm = desc->tfm;
1278 const unsigned int digestsize = crypto_shash_digestsize(tfm);
1279 const unsigned int statesize = crypto_shash_statesize(tfm);
1280 const char *driver = crypto_shash_driver_name(tfm);
1281 const struct test_sg_division *divs[XBUFSIZE];
1283 u8 result[HASH_MAX_DIGESTSIZE + TESTMGR_POISON_LEN];
1286 /* Set the key, if specified */
1288 err = do_setkey(crypto_shash_setkey, tfm, vec->key, vec->ksize,
1291 if (err == vec->setkey_error)
1293 pr_err("alg: shash: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1294 driver, vec_name, vec->setkey_error, err,
1295 crypto_shash_get_flags(tfm));
1298 if (vec->setkey_error) {
1299 pr_err("alg: shash: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1300 driver, vec_name, vec->setkey_error);
1305 /* Build the scatterlist for the source data */
1306 err = build_hash_sglist(tsgl, vec, cfg, 0, divs);
1308 pr_err("alg: shash: %s: error preparing scatterlist for test vector %s, cfg=\"%s\"\n",
1309 driver, vec_name, cfg->name);
1313 /* Do the actual hashing */
1315 testmgr_poison(desc->__ctx, crypto_shash_descsize(tfm));
1316 testmgr_poison(result, digestsize + TESTMGR_POISON_LEN);
1318 if (cfg->finalization_type == FINALIZATION_TYPE_DIGEST ||
1319 vec->digest_error) {
1320 /* Just using digest() */
1321 if (tsgl->nents != 1)
1324 crypto_disable_simd_for_test();
1325 err = crypto_shash_digest(desc, sg_virt(&tsgl->sgl[0]),
1326 tsgl->sgl[0].length, result);
1328 crypto_reenable_simd_for_test();
1330 if (err == vec->digest_error)
1332 pr_err("alg: shash: %s digest() failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
1333 driver, vec_name, vec->digest_error, err,
1337 if (vec->digest_error) {
1338 pr_err("alg: shash: %s digest() unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
1339 driver, vec_name, vec->digest_error, cfg->name);
1345 /* Using init(), zero or more update(), then final() or finup() */
1348 crypto_disable_simd_for_test();
1349 err = crypto_shash_init(desc);
1351 crypto_reenable_simd_for_test();
1352 err = check_shash_op("init", err, driver, vec_name, cfg);
1356 for (i = 0; i < tsgl->nents; i++) {
1357 if (i + 1 == tsgl->nents &&
1358 cfg->finalization_type == FINALIZATION_TYPE_FINUP) {
1359 if (divs[i]->nosimd)
1360 crypto_disable_simd_for_test();
1361 err = crypto_shash_finup(desc, sg_virt(&tsgl->sgl[i]),
1362 tsgl->sgl[i].length, result);
1363 if (divs[i]->nosimd)
1364 crypto_reenable_simd_for_test();
1365 err = check_shash_op("finup", err, driver, vec_name,
1371 if (divs[i]->nosimd)
1372 crypto_disable_simd_for_test();
1373 err = crypto_shash_update(desc, sg_virt(&tsgl->sgl[i]),
1374 tsgl->sgl[i].length);
1375 if (divs[i]->nosimd)
1376 crypto_reenable_simd_for_test();
1377 err = check_shash_op("update", err, driver, vec_name, cfg);
1380 if (divs[i]->flush_type == FLUSH_TYPE_REIMPORT) {
1381 /* Test ->export() and ->import() */
1382 testmgr_poison(hashstate + statesize,
1383 TESTMGR_POISON_LEN);
1384 err = crypto_shash_export(desc, hashstate);
1385 err = check_shash_op("export", err, driver, vec_name,
1389 if (!testmgr_is_poison(hashstate + statesize,
1390 TESTMGR_POISON_LEN)) {
1391 pr_err("alg: shash: %s export() overran state buffer on test vector %s, cfg=\"%s\"\n",
1392 driver, vec_name, cfg->name);
1395 testmgr_poison(desc->__ctx, crypto_shash_descsize(tfm));
1396 err = crypto_shash_import(desc, hashstate);
1397 err = check_shash_op("import", err, driver, vec_name,
1405 crypto_disable_simd_for_test();
1406 err = crypto_shash_final(desc, result);
1408 crypto_reenable_simd_for_test();
1409 err = check_shash_op("final", err, driver, vec_name, cfg);
1413 return check_hash_result("shash", result, digestsize, vec, vec_name,
1417 static int do_ahash_op(int (*op)(struct ahash_request *req),
1418 struct ahash_request *req,
1419 struct crypto_wait *wait, bool nosimd)
1424 crypto_disable_simd_for_test();
1429 crypto_reenable_simd_for_test();
1431 return crypto_wait_req(err, wait);
1434 static int check_nonfinal_ahash_op(const char *op, int err,
1435 u8 *result, unsigned int digestsize,
1436 const char *driver, const char *vec_name,
1437 const struct testvec_config *cfg)
1440 pr_err("alg: ahash: %s %s() failed with err %d on test vector %s, cfg=\"%s\"\n",
1441 driver, op, err, vec_name, cfg->name);
1444 if (!testmgr_is_poison(result, digestsize)) {
1445 pr_err("alg: ahash: %s %s() used result buffer on test vector %s, cfg=\"%s\"\n",
1446 driver, op, vec_name, cfg->name);
1452 /* Test one hash test vector in one configuration, using the ahash API */
1453 static int test_ahash_vec_cfg(const struct hash_testvec *vec,
1454 const char *vec_name,
1455 const struct testvec_config *cfg,
1456 struct ahash_request *req,
1457 struct test_sglist *tsgl,
1460 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1461 const unsigned int alignmask = crypto_ahash_alignmask(tfm);
1462 const unsigned int digestsize = crypto_ahash_digestsize(tfm);
1463 const unsigned int statesize = crypto_ahash_statesize(tfm);
1464 const char *driver = crypto_ahash_driver_name(tfm);
1465 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1466 const struct test_sg_division *divs[XBUFSIZE];
1467 DECLARE_CRYPTO_WAIT(wait);
1469 struct scatterlist *pending_sgl;
1470 unsigned int pending_len;
1471 u8 result[HASH_MAX_DIGESTSIZE + TESTMGR_POISON_LEN];
1474 /* Set the key, if specified */
1476 err = do_setkey(crypto_ahash_setkey, tfm, vec->key, vec->ksize,
1479 if (err == vec->setkey_error)
1481 pr_err("alg: ahash: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1482 driver, vec_name, vec->setkey_error, err,
1483 crypto_ahash_get_flags(tfm));
1486 if (vec->setkey_error) {
1487 pr_err("alg: ahash: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1488 driver, vec_name, vec->setkey_error);
1493 /* Build the scatterlist for the source data */
1494 err = build_hash_sglist(tsgl, vec, cfg, alignmask, divs);
1496 pr_err("alg: ahash: %s: error preparing scatterlist for test vector %s, cfg=\"%s\"\n",
1497 driver, vec_name, cfg->name);
1501 /* Do the actual hashing */
1503 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
1504 testmgr_poison(result, digestsize + TESTMGR_POISON_LEN);
1506 if (cfg->finalization_type == FINALIZATION_TYPE_DIGEST ||
1507 vec->digest_error) {
1508 /* Just using digest() */
1509 ahash_request_set_callback(req, req_flags, crypto_req_done,
1511 ahash_request_set_crypt(req, tsgl->sgl, result, vec->psize);
1512 err = do_ahash_op(crypto_ahash_digest, req, &wait, cfg->nosimd);
1514 if (err == vec->digest_error)
1516 pr_err("alg: ahash: %s digest() failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
1517 driver, vec_name, vec->digest_error, err,
1521 if (vec->digest_error) {
1522 pr_err("alg: ahash: %s digest() unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
1523 driver, vec_name, vec->digest_error, cfg->name);
1529 /* Using init(), zero or more update(), then final() or finup() */
1531 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
1532 ahash_request_set_crypt(req, NULL, result, 0);
1533 err = do_ahash_op(crypto_ahash_init, req, &wait, cfg->nosimd);
1534 err = check_nonfinal_ahash_op("init", err, result, digestsize,
1535 driver, vec_name, cfg);
1541 for (i = 0; i < tsgl->nents; i++) {
1542 if (divs[i]->flush_type != FLUSH_TYPE_NONE &&
1543 pending_sgl != NULL) {
1544 /* update() with the pending data */
1545 ahash_request_set_callback(req, req_flags,
1546 crypto_req_done, &wait);
1547 ahash_request_set_crypt(req, pending_sgl, result,
1549 err = do_ahash_op(crypto_ahash_update, req, &wait,
1551 err = check_nonfinal_ahash_op("update", err,
1553 driver, vec_name, cfg);
1559 if (divs[i]->flush_type == FLUSH_TYPE_REIMPORT) {
1560 /* Test ->export() and ->import() */
1561 testmgr_poison(hashstate + statesize,
1562 TESTMGR_POISON_LEN);
1563 err = crypto_ahash_export(req, hashstate);
1564 err = check_nonfinal_ahash_op("export", err,
1566 driver, vec_name, cfg);
1569 if (!testmgr_is_poison(hashstate + statesize,
1570 TESTMGR_POISON_LEN)) {
1571 pr_err("alg: ahash: %s export() overran state buffer on test vector %s, cfg=\"%s\"\n",
1572 driver, vec_name, cfg->name);
1576 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
1577 err = crypto_ahash_import(req, hashstate);
1578 err = check_nonfinal_ahash_op("import", err,
1580 driver, vec_name, cfg);
1584 if (pending_sgl == NULL)
1585 pending_sgl = &tsgl->sgl[i];
1586 pending_len += tsgl->sgl[i].length;
1589 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
1590 ahash_request_set_crypt(req, pending_sgl, result, pending_len);
1591 if (cfg->finalization_type == FINALIZATION_TYPE_FINAL) {
1592 /* finish with update() and final() */
1593 err = do_ahash_op(crypto_ahash_update, req, &wait, cfg->nosimd);
1594 err = check_nonfinal_ahash_op("update", err, result, digestsize,
1595 driver, vec_name, cfg);
1598 err = do_ahash_op(crypto_ahash_final, req, &wait, cfg->nosimd);
1600 pr_err("alg: ahash: %s final() failed with err %d on test vector %s, cfg=\"%s\"\n",
1601 driver, err, vec_name, cfg->name);
1605 /* finish with finup() */
1606 err = do_ahash_op(crypto_ahash_finup, req, &wait, cfg->nosimd);
1608 pr_err("alg: ahash: %s finup() failed with err %d on test vector %s, cfg=\"%s\"\n",
1609 driver, err, vec_name, cfg->name);
1615 return check_hash_result("ahash", result, digestsize, vec, vec_name,
1619 static int test_hash_vec_cfg(const struct hash_testvec *vec,
1620 const char *vec_name,
1621 const struct testvec_config *cfg,
1622 struct ahash_request *req,
1623 struct shash_desc *desc,
1624 struct test_sglist *tsgl,
1630 * For algorithms implemented as "shash", most bugs will be detected by
1631 * both the shash and ahash tests. Test the shash API first so that the
1632 * failures involve less indirection, so are easier to debug.
1636 err = test_shash_vec_cfg(vec, vec_name, cfg, desc, tsgl,
1642 return test_ahash_vec_cfg(vec, vec_name, cfg, req, tsgl, hashstate);
1645 static int test_hash_vec(const struct hash_testvec *vec, unsigned int vec_num,
1646 struct ahash_request *req, struct shash_desc *desc,
1647 struct test_sglist *tsgl, u8 *hashstate)
1653 sprintf(vec_name, "%u", vec_num);
1655 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++) {
1656 err = test_hash_vec_cfg(vec, vec_name,
1657 &default_hash_testvec_configs[i],
1658 req, desc, tsgl, hashstate);
1663 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1664 if (!noextratests) {
1665 struct rnd_state rng;
1666 struct testvec_config cfg;
1667 char cfgname[TESTVEC_CONFIG_NAMELEN];
1669 init_rnd_state(&rng);
1671 for (i = 0; i < fuzz_iterations; i++) {
1672 generate_random_testvec_config(&rng, &cfg, cfgname,
1674 err = test_hash_vec_cfg(vec, vec_name, &cfg,
1675 req, desc, tsgl, hashstate);
1685 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1687 * Generate a hash test vector from the given implementation.
1688 * Assumes the buffers in 'vec' were already allocated.
1690 static void generate_random_hash_testvec(struct rnd_state *rng,
1691 struct shash_desc *desc,
1692 struct hash_testvec *vec,
1693 unsigned int maxkeysize,
1694 unsigned int maxdatasize,
1695 char *name, size_t max_namelen)
1698 vec->psize = generate_random_length(rng, maxdatasize);
1699 generate_random_bytes(rng, (u8 *)vec->plaintext, vec->psize);
1702 * Key: length in range [1, maxkeysize], but usually choose maxkeysize.
1703 * If algorithm is unkeyed, then maxkeysize == 0 and set ksize = 0.
1705 vec->setkey_error = 0;
1708 vec->ksize = maxkeysize;
1709 if (prandom_u32_below(rng, 4) == 0)
1710 vec->ksize = prandom_u32_inclusive(rng, 1, maxkeysize);
1711 generate_random_bytes(rng, (u8 *)vec->key, vec->ksize);
1713 vec->setkey_error = crypto_shash_setkey(desc->tfm, vec->key,
1715 /* If the key couldn't be set, no need to continue to digest. */
1716 if (vec->setkey_error)
1721 vec->digest_error = crypto_shash_digest(desc, vec->plaintext,
1722 vec->psize, (u8 *)vec->digest);
1724 snprintf(name, max_namelen, "\"random: psize=%u ksize=%u\"",
1725 vec->psize, vec->ksize);
1729 * Test the hash algorithm represented by @req against the corresponding generic
1730 * implementation, if one is available.
1732 static int test_hash_vs_generic_impl(const char *generic_driver,
1733 unsigned int maxkeysize,
1734 struct ahash_request *req,
1735 struct shash_desc *desc,
1736 struct test_sglist *tsgl,
1739 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1740 const unsigned int digestsize = crypto_ahash_digestsize(tfm);
1741 const unsigned int blocksize = crypto_ahash_blocksize(tfm);
1742 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
1743 const char *algname = crypto_hash_alg_common(tfm)->base.cra_name;
1744 const char *driver = crypto_ahash_driver_name(tfm);
1745 struct rnd_state rng;
1746 char _generic_driver[CRYPTO_MAX_ALG_NAME];
1747 struct crypto_shash *generic_tfm = NULL;
1748 struct shash_desc *generic_desc = NULL;
1750 struct hash_testvec vec = { 0 };
1752 struct testvec_config *cfg;
1753 char cfgname[TESTVEC_CONFIG_NAMELEN];
1759 init_rnd_state(&rng);
1761 if (!generic_driver) { /* Use default naming convention? */
1762 err = build_generic_driver_name(algname, _generic_driver);
1765 generic_driver = _generic_driver;
1768 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
1771 generic_tfm = crypto_alloc_shash(generic_driver, 0, 0);
1772 if (IS_ERR(generic_tfm)) {
1773 err = PTR_ERR(generic_tfm);
1774 if (err == -ENOENT) {
1775 pr_warn("alg: hash: skipping comparison tests for %s because %s is unavailable\n",
1776 driver, generic_driver);
1779 pr_err("alg: hash: error allocating %s (generic impl of %s): %d\n",
1780 generic_driver, algname, err);
1784 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1790 generic_desc = kzalloc(sizeof(*desc) +
1791 crypto_shash_descsize(generic_tfm), GFP_KERNEL);
1792 if (!generic_desc) {
1796 generic_desc->tfm = generic_tfm;
1798 /* Check the algorithm properties for consistency. */
1800 if (digestsize != crypto_shash_digestsize(generic_tfm)) {
1801 pr_err("alg: hash: digestsize for %s (%u) doesn't match generic impl (%u)\n",
1803 crypto_shash_digestsize(generic_tfm));
1808 if (blocksize != crypto_shash_blocksize(generic_tfm)) {
1809 pr_err("alg: hash: blocksize for %s (%u) doesn't match generic impl (%u)\n",
1810 driver, blocksize, crypto_shash_blocksize(generic_tfm));
1816 * Now generate test vectors using the generic implementation, and test
1817 * the other implementation against them.
1820 vec.key = kmalloc(maxkeysize, GFP_KERNEL);
1821 vec.plaintext = kmalloc(maxdatasize, GFP_KERNEL);
1822 vec.digest = kmalloc(digestsize, GFP_KERNEL);
1823 if (!vec.key || !vec.plaintext || !vec.digest) {
1828 for (i = 0; i < fuzz_iterations * 8; i++) {
1829 generate_random_hash_testvec(&rng, generic_desc, &vec,
1830 maxkeysize, maxdatasize,
1831 vec_name, sizeof(vec_name));
1832 generate_random_testvec_config(&rng, cfg, cfgname,
1835 err = test_hash_vec_cfg(&vec, vec_name, cfg,
1836 req, desc, tsgl, hashstate);
1845 kfree(vec.plaintext);
1847 crypto_free_shash(generic_tfm);
1848 kfree_sensitive(generic_desc);
1851 #else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1852 static int test_hash_vs_generic_impl(const char *generic_driver,
1853 unsigned int maxkeysize,
1854 struct ahash_request *req,
1855 struct shash_desc *desc,
1856 struct test_sglist *tsgl,
1861 #endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1863 static int alloc_shash(const char *driver, u32 type, u32 mask,
1864 struct crypto_shash **tfm_ret,
1865 struct shash_desc **desc_ret)
1867 struct crypto_shash *tfm;
1868 struct shash_desc *desc;
1870 tfm = crypto_alloc_shash(driver, type, mask);
1872 if (PTR_ERR(tfm) == -ENOENT) {
1874 * This algorithm is only available through the ahash
1875 * API, not the shash API, so skip the shash tests.
1879 pr_err("alg: hash: failed to allocate shash transform for %s: %ld\n",
1880 driver, PTR_ERR(tfm));
1881 return PTR_ERR(tfm);
1884 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL);
1886 crypto_free_shash(tfm);
1896 static int __alg_test_hash(const struct hash_testvec *vecs,
1897 unsigned int num_vecs, const char *driver,
1899 const char *generic_driver, unsigned int maxkeysize)
1901 struct crypto_ahash *atfm = NULL;
1902 struct ahash_request *req = NULL;
1903 struct crypto_shash *stfm = NULL;
1904 struct shash_desc *desc = NULL;
1905 struct test_sglist *tsgl = NULL;
1906 u8 *hashstate = NULL;
1907 unsigned int statesize;
1912 * Always test the ahash API. This works regardless of whether the
1913 * algorithm is implemented as ahash or shash.
1916 atfm = crypto_alloc_ahash(driver, type, mask);
1918 pr_err("alg: hash: failed to allocate transform for %s: %ld\n",
1919 driver, PTR_ERR(atfm));
1920 return PTR_ERR(atfm);
1922 driver = crypto_ahash_driver_name(atfm);
1924 req = ahash_request_alloc(atfm, GFP_KERNEL);
1926 pr_err("alg: hash: failed to allocate request for %s\n",
1933 * If available also test the shash API, to cover corner cases that may
1934 * be missed by testing the ahash API only.
1936 err = alloc_shash(driver, type, mask, &stfm, &desc);
1940 tsgl = kmalloc(sizeof(*tsgl), GFP_KERNEL);
1941 if (!tsgl || init_test_sglist(tsgl) != 0) {
1942 pr_err("alg: hash: failed to allocate test buffers for %s\n",
1950 statesize = crypto_ahash_statesize(atfm);
1952 statesize = max(statesize, crypto_shash_statesize(stfm));
1953 hashstate = kmalloc(statesize + TESTMGR_POISON_LEN, GFP_KERNEL);
1955 pr_err("alg: hash: failed to allocate hash state buffer for %s\n",
1961 for (i = 0; i < num_vecs; i++) {
1962 if (fips_enabled && vecs[i].fips_skip)
1965 err = test_hash_vec(&vecs[i], i, req, desc, tsgl, hashstate);
1970 err = test_hash_vs_generic_impl(generic_driver, maxkeysize, req,
1971 desc, tsgl, hashstate);
1975 destroy_test_sglist(tsgl);
1979 crypto_free_shash(stfm);
1980 ahash_request_free(req);
1981 crypto_free_ahash(atfm);
1985 static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1988 const struct hash_testvec *template = desc->suite.hash.vecs;
1989 unsigned int tcount = desc->suite.hash.count;
1990 unsigned int nr_unkeyed, nr_keyed;
1991 unsigned int maxkeysize = 0;
1995 * For OPTIONAL_KEY algorithms, we have to do all the unkeyed tests
1996 * first, before setting a key on the tfm. To make this easier, we
1997 * require that the unkeyed test vectors (if any) are listed first.
2000 for (nr_unkeyed = 0; nr_unkeyed < tcount; nr_unkeyed++) {
2001 if (template[nr_unkeyed].ksize)
2004 for (nr_keyed = 0; nr_unkeyed + nr_keyed < tcount; nr_keyed++) {
2005 if (!template[nr_unkeyed + nr_keyed].ksize) {
2006 pr_err("alg: hash: test vectors for %s out of order, "
2007 "unkeyed ones must come first\n", desc->alg);
2010 maxkeysize = max_t(unsigned int, maxkeysize,
2011 template[nr_unkeyed + nr_keyed].ksize);
2016 err = __alg_test_hash(template, nr_unkeyed, driver, type, mask,
2017 desc->generic_driver, maxkeysize);
2018 template += nr_unkeyed;
2021 if (!err && nr_keyed)
2022 err = __alg_test_hash(template, nr_keyed, driver, type, mask,
2023 desc->generic_driver, maxkeysize);
2028 static int test_aead_vec_cfg(int enc, const struct aead_testvec *vec,
2029 const char *vec_name,
2030 const struct testvec_config *cfg,
2031 struct aead_request *req,
2032 struct cipher_test_sglists *tsgls)
2034 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2035 const unsigned int alignmask = crypto_aead_alignmask(tfm);
2036 const unsigned int ivsize = crypto_aead_ivsize(tfm);
2037 const unsigned int authsize = vec->clen - vec->plen;
2038 const char *driver = crypto_aead_driver_name(tfm);
2039 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
2040 const char *op = enc ? "encryption" : "decryption";
2041 DECLARE_CRYPTO_WAIT(wait);
2042 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
2043 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
2045 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
2046 struct kvec input[2];
2051 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
2053 crypto_aead_clear_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
2055 err = do_setkey(crypto_aead_setkey, tfm, vec->key, vec->klen,
2057 if (err && err != vec->setkey_error) {
2058 pr_err("alg: aead: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
2059 driver, vec_name, vec->setkey_error, err,
2060 crypto_aead_get_flags(tfm));
2063 if (!err && vec->setkey_error) {
2064 pr_err("alg: aead: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
2065 driver, vec_name, vec->setkey_error);
2069 /* Set the authentication tag size */
2070 err = crypto_aead_setauthsize(tfm, authsize);
2071 if (err && err != vec->setauthsize_error) {
2072 pr_err("alg: aead: %s setauthsize failed on test vector %s; expected_error=%d, actual_error=%d\n",
2073 driver, vec_name, vec->setauthsize_error, err);
2076 if (!err && vec->setauthsize_error) {
2077 pr_err("alg: aead: %s setauthsize unexpectedly succeeded on test vector %s; expected_error=%d\n",
2078 driver, vec_name, vec->setauthsize_error);
2082 if (vec->setkey_error || vec->setauthsize_error)
2085 /* The IV must be copied to a buffer, as the algorithm may modify it */
2086 if (WARN_ON(ivsize > MAX_IVLEN))
2089 memcpy(iv, vec->iv, ivsize);
2091 memset(iv, 0, ivsize);
2093 /* Build the src/dst scatterlists */
2094 input[0].iov_base = (void *)vec->assoc;
2095 input[0].iov_len = vec->alen;
2096 input[1].iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
2097 input[1].iov_len = enc ? vec->plen : vec->clen;
2098 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
2099 vec->alen + (enc ? vec->plen :
2101 vec->alen + (enc ? vec->clen :
2105 pr_err("alg: aead: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
2106 driver, op, vec_name, cfg->name);
2110 /* Do the actual encryption or decryption */
2111 testmgr_poison(req->__ctx, crypto_aead_reqsize(tfm));
2112 aead_request_set_callback(req, req_flags, crypto_req_done, &wait);
2113 aead_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
2114 enc ? vec->plen : vec->clen, iv);
2115 aead_request_set_ad(req, vec->alen);
2117 crypto_disable_simd_for_test();
2118 err = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
2120 crypto_reenable_simd_for_test();
2121 err = crypto_wait_req(err, &wait);
2123 /* Check that the algorithm didn't overwrite things it shouldn't have */
2124 if (req->cryptlen != (enc ? vec->plen : vec->clen) ||
2125 req->assoclen != vec->alen ||
2127 req->src != tsgls->src.sgl_ptr ||
2128 req->dst != tsgls->dst.sgl_ptr ||
2129 crypto_aead_reqtfm(req) != tfm ||
2130 req->base.complete != crypto_req_done ||
2131 req->base.flags != req_flags ||
2132 req->base.data != &wait) {
2133 pr_err("alg: aead: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
2134 driver, op, vec_name, cfg->name);
2135 if (req->cryptlen != (enc ? vec->plen : vec->clen))
2136 pr_err("alg: aead: changed 'req->cryptlen'\n");
2137 if (req->assoclen != vec->alen)
2138 pr_err("alg: aead: changed 'req->assoclen'\n");
2140 pr_err("alg: aead: changed 'req->iv'\n");
2141 if (req->src != tsgls->src.sgl_ptr)
2142 pr_err("alg: aead: changed 'req->src'\n");
2143 if (req->dst != tsgls->dst.sgl_ptr)
2144 pr_err("alg: aead: changed 'req->dst'\n");
2145 if (crypto_aead_reqtfm(req) != tfm)
2146 pr_err("alg: aead: changed 'req->base.tfm'\n");
2147 if (req->base.complete != crypto_req_done)
2148 pr_err("alg: aead: changed 'req->base.complete'\n");
2149 if (req->base.flags != req_flags)
2150 pr_err("alg: aead: changed 'req->base.flags'\n");
2151 if (req->base.data != &wait)
2152 pr_err("alg: aead: changed 'req->base.data'\n");
2155 if (is_test_sglist_corrupted(&tsgls->src)) {
2156 pr_err("alg: aead: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
2157 driver, op, vec_name, cfg->name);
2160 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
2161 is_test_sglist_corrupted(&tsgls->dst)) {
2162 pr_err("alg: aead: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
2163 driver, op, vec_name, cfg->name);
2167 /* Check for unexpected success or failure, or wrong error code */
2168 if ((err == 0 && vec->novrfy) ||
2169 (err != vec->crypt_error && !(err == -EBADMSG && vec->novrfy))) {
2170 char expected_error[32];
2173 vec->crypt_error != 0 && vec->crypt_error != -EBADMSG)
2174 sprintf(expected_error, "-EBADMSG or %d",
2176 else if (vec->novrfy)
2177 sprintf(expected_error, "-EBADMSG");
2179 sprintf(expected_error, "%d", vec->crypt_error);
2181 pr_err("alg: aead: %s %s failed on test vector %s; expected_error=%s, actual_error=%d, cfg=\"%s\"\n",
2182 driver, op, vec_name, expected_error, err,
2186 pr_err("alg: aead: %s %s unexpectedly succeeded on test vector %s; expected_error=%s, cfg=\"%s\"\n",
2187 driver, op, vec_name, expected_error, cfg->name);
2190 if (err) /* Expectedly failed. */
2193 /* Check for the correct output (ciphertext or plaintext) */
2194 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
2195 enc ? vec->clen : vec->plen,
2197 enc || cfg->inplace_mode == OUT_OF_PLACE);
2198 if (err == -EOVERFLOW) {
2199 pr_err("alg: aead: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
2200 driver, op, vec_name, cfg->name);
2204 pr_err("alg: aead: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
2205 driver, op, vec_name, cfg->name);
2212 static int test_aead_vec(int enc, const struct aead_testvec *vec,
2213 unsigned int vec_num, struct aead_request *req,
2214 struct cipher_test_sglists *tsgls)
2220 if (enc && vec->novrfy)
2223 sprintf(vec_name, "%u", vec_num);
2225 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
2226 err = test_aead_vec_cfg(enc, vec, vec_name,
2227 &default_cipher_testvec_configs[i],
2233 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2234 if (!noextratests) {
2235 struct rnd_state rng;
2236 struct testvec_config cfg;
2237 char cfgname[TESTVEC_CONFIG_NAMELEN];
2239 init_rnd_state(&rng);
2241 for (i = 0; i < fuzz_iterations; i++) {
2242 generate_random_testvec_config(&rng, &cfg, cfgname,
2244 err = test_aead_vec_cfg(enc, vec, vec_name,
2255 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2257 struct aead_extra_tests_ctx {
2258 struct rnd_state rng;
2259 struct aead_request *req;
2260 struct crypto_aead *tfm;
2261 const struct alg_test_desc *test_desc;
2262 struct cipher_test_sglists *tsgls;
2263 unsigned int maxdatasize;
2264 unsigned int maxkeysize;
2266 struct aead_testvec vec;
2268 char cfgname[TESTVEC_CONFIG_NAMELEN];
2269 struct testvec_config cfg;
2273 * Make at least one random change to a (ciphertext, AAD) pair. "Ciphertext"
2274 * here means the full ciphertext including the authentication tag. The
2275 * authentication tag (and hence also the ciphertext) is assumed to be nonempty.
2277 static void mutate_aead_message(struct rnd_state *rng,
2278 struct aead_testvec *vec, bool aad_iv,
2279 unsigned int ivsize)
2281 const unsigned int aad_tail_size = aad_iv ? ivsize : 0;
2282 const unsigned int authsize = vec->clen - vec->plen;
2284 if (prandom_bool(rng) && vec->alen > aad_tail_size) {
2285 /* Mutate the AAD */
2286 flip_random_bit(rng, (u8 *)vec->assoc,
2287 vec->alen - aad_tail_size);
2288 if (prandom_bool(rng))
2291 if (prandom_bool(rng)) {
2292 /* Mutate auth tag (assuming it's at the end of ciphertext) */
2293 flip_random_bit(rng, (u8 *)vec->ctext + vec->plen, authsize);
2295 /* Mutate any part of the ciphertext */
2296 flip_random_bit(rng, (u8 *)vec->ctext, vec->clen);
2301 * Minimum authentication tag size in bytes at which we assume that we can
2302 * reliably generate inauthentic messages, i.e. not generate an authentic
2303 * message by chance.
2305 #define MIN_COLLISION_FREE_AUTHSIZE 8
2307 static void generate_aead_message(struct rnd_state *rng,
2308 struct aead_request *req,
2309 const struct aead_test_suite *suite,
2310 struct aead_testvec *vec,
2311 bool prefer_inauthentic)
2313 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2314 const unsigned int ivsize = crypto_aead_ivsize(tfm);
2315 const unsigned int authsize = vec->clen - vec->plen;
2316 const bool inauthentic = (authsize >= MIN_COLLISION_FREE_AUTHSIZE) &&
2317 (prefer_inauthentic ||
2318 prandom_u32_below(rng, 4) == 0);
2320 /* Generate the AAD. */
2321 generate_random_bytes(rng, (u8 *)vec->assoc, vec->alen);
2322 if (suite->aad_iv && vec->alen >= ivsize)
2323 /* Avoid implementation-defined behavior. */
2324 memcpy((u8 *)vec->assoc + vec->alen - ivsize, vec->iv, ivsize);
2326 if (inauthentic && prandom_bool(rng)) {
2327 /* Generate a random ciphertext. */
2328 generate_random_bytes(rng, (u8 *)vec->ctext, vec->clen);
2331 struct scatterlist src[2], dst;
2333 DECLARE_CRYPTO_WAIT(wait);
2335 /* Generate a random plaintext and encrypt it. */
2336 sg_init_table(src, 2);
2338 sg_set_buf(&src[i++], vec->assoc, vec->alen);
2340 generate_random_bytes(rng, (u8 *)vec->ptext, vec->plen);
2341 sg_set_buf(&src[i++], vec->ptext, vec->plen);
2343 sg_init_one(&dst, vec->ctext, vec->alen + vec->clen);
2344 memcpy(iv, vec->iv, ivsize);
2345 aead_request_set_callback(req, 0, crypto_req_done, &wait);
2346 aead_request_set_crypt(req, src, &dst, vec->plen, iv);
2347 aead_request_set_ad(req, vec->alen);
2348 vec->crypt_error = crypto_wait_req(crypto_aead_encrypt(req),
2350 /* If encryption failed, we're done. */
2351 if (vec->crypt_error != 0)
2353 memmove((u8 *)vec->ctext, vec->ctext + vec->alen, vec->clen);
2357 * Mutate the authentic (ciphertext, AAD) pair to get an
2360 mutate_aead_message(rng, vec, suite->aad_iv, ivsize);
2363 if (suite->einval_allowed)
2364 vec->crypt_error = -EINVAL;
2368 * Generate an AEAD test vector 'vec' using the implementation specified by
2369 * 'req'. The buffers in 'vec' must already be allocated.
2371 * If 'prefer_inauthentic' is true, then this function will generate inauthentic
2372 * test vectors (i.e. vectors with 'vec->novrfy=1') more often.
2374 static void generate_random_aead_testvec(struct rnd_state *rng,
2375 struct aead_request *req,
2376 struct aead_testvec *vec,
2377 const struct aead_test_suite *suite,
2378 unsigned int maxkeysize,
2379 unsigned int maxdatasize,
2380 char *name, size_t max_namelen,
2381 bool prefer_inauthentic)
2383 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2384 const unsigned int ivsize = crypto_aead_ivsize(tfm);
2385 const unsigned int maxauthsize = crypto_aead_maxauthsize(tfm);
2386 unsigned int authsize;
2387 unsigned int total_len;
2389 /* Key: length in [0, maxkeysize], but usually choose maxkeysize */
2390 vec->klen = maxkeysize;
2391 if (prandom_u32_below(rng, 4) == 0)
2392 vec->klen = prandom_u32_below(rng, maxkeysize + 1);
2393 generate_random_bytes(rng, (u8 *)vec->key, vec->klen);
2394 vec->setkey_error = crypto_aead_setkey(tfm, vec->key, vec->klen);
2397 generate_random_bytes(rng, (u8 *)vec->iv, ivsize);
2399 /* Tag length: in [0, maxauthsize], but usually choose maxauthsize */
2400 authsize = maxauthsize;
2401 if (prandom_u32_below(rng, 4) == 0)
2402 authsize = prandom_u32_below(rng, maxauthsize + 1);
2403 if (prefer_inauthentic && authsize < MIN_COLLISION_FREE_AUTHSIZE)
2404 authsize = MIN_COLLISION_FREE_AUTHSIZE;
2405 if (WARN_ON(authsize > maxdatasize))
2406 authsize = maxdatasize;
2407 maxdatasize -= authsize;
2408 vec->setauthsize_error = crypto_aead_setauthsize(tfm, authsize);
2410 /* AAD, plaintext, and ciphertext lengths */
2411 total_len = generate_random_length(rng, maxdatasize);
2412 if (prandom_u32_below(rng, 4) == 0)
2415 vec->alen = generate_random_length(rng, total_len);
2416 vec->plen = total_len - vec->alen;
2417 vec->clen = vec->plen + authsize;
2420 * Generate the AAD, plaintext, and ciphertext. Not applicable if the
2421 * key or the authentication tag size couldn't be set.
2424 vec->crypt_error = 0;
2425 if (vec->setkey_error == 0 && vec->setauthsize_error == 0)
2426 generate_aead_message(rng, req, suite, vec, prefer_inauthentic);
2427 snprintf(name, max_namelen,
2428 "\"random: alen=%u plen=%u authsize=%u klen=%u novrfy=%d\"",
2429 vec->alen, vec->plen, authsize, vec->klen, vec->novrfy);
2432 static void try_to_generate_inauthentic_testvec(
2433 struct aead_extra_tests_ctx *ctx)
2437 for (i = 0; i < 10; i++) {
2438 generate_random_aead_testvec(&ctx->rng, ctx->req, &ctx->vec,
2439 &ctx->test_desc->suite.aead,
2440 ctx->maxkeysize, ctx->maxdatasize,
2442 sizeof(ctx->vec_name), true);
2443 if (ctx->vec.novrfy)
2449 * Generate inauthentic test vectors (i.e. ciphertext, AAD pairs that aren't the
2450 * result of an encryption with the key) and verify that decryption fails.
2452 static int test_aead_inauthentic_inputs(struct aead_extra_tests_ctx *ctx)
2457 for (i = 0; i < fuzz_iterations * 8; i++) {
2459 * Since this part of the tests isn't comparing the
2460 * implementation to another, there's no point in testing any
2461 * test vectors other than inauthentic ones (vec.novrfy=1) here.
2463 * If we're having trouble generating such a test vector, e.g.
2464 * if the algorithm keeps rejecting the generated keys, don't
2465 * retry forever; just continue on.
2467 try_to_generate_inauthentic_testvec(ctx);
2468 if (ctx->vec.novrfy) {
2469 generate_random_testvec_config(&ctx->rng, &ctx->cfg,
2471 sizeof(ctx->cfgname));
2472 err = test_aead_vec_cfg(DECRYPT, &ctx->vec,
2473 ctx->vec_name, &ctx->cfg,
2474 ctx->req, ctx->tsgls);
2484 * Test the AEAD algorithm against the corresponding generic implementation, if
2487 static int test_aead_vs_generic_impl(struct aead_extra_tests_ctx *ctx)
2489 struct crypto_aead *tfm = ctx->tfm;
2490 const char *algname = crypto_aead_alg(tfm)->base.cra_name;
2491 const char *driver = crypto_aead_driver_name(tfm);
2492 const char *generic_driver = ctx->test_desc->generic_driver;
2493 char _generic_driver[CRYPTO_MAX_ALG_NAME];
2494 struct crypto_aead *generic_tfm = NULL;
2495 struct aead_request *generic_req = NULL;
2499 if (!generic_driver) { /* Use default naming convention? */
2500 err = build_generic_driver_name(algname, _generic_driver);
2503 generic_driver = _generic_driver;
2506 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
2509 generic_tfm = crypto_alloc_aead(generic_driver, 0, 0);
2510 if (IS_ERR(generic_tfm)) {
2511 err = PTR_ERR(generic_tfm);
2512 if (err == -ENOENT) {
2513 pr_warn("alg: aead: skipping comparison tests for %s because %s is unavailable\n",
2514 driver, generic_driver);
2517 pr_err("alg: aead: error allocating %s (generic impl of %s): %d\n",
2518 generic_driver, algname, err);
2522 generic_req = aead_request_alloc(generic_tfm, GFP_KERNEL);
2528 /* Check the algorithm properties for consistency. */
2530 if (crypto_aead_maxauthsize(tfm) !=
2531 crypto_aead_maxauthsize(generic_tfm)) {
2532 pr_err("alg: aead: maxauthsize for %s (%u) doesn't match generic impl (%u)\n",
2533 driver, crypto_aead_maxauthsize(tfm),
2534 crypto_aead_maxauthsize(generic_tfm));
2539 if (crypto_aead_ivsize(tfm) != crypto_aead_ivsize(generic_tfm)) {
2540 pr_err("alg: aead: ivsize for %s (%u) doesn't match generic impl (%u)\n",
2541 driver, crypto_aead_ivsize(tfm),
2542 crypto_aead_ivsize(generic_tfm));
2547 if (crypto_aead_blocksize(tfm) != crypto_aead_blocksize(generic_tfm)) {
2548 pr_err("alg: aead: blocksize for %s (%u) doesn't match generic impl (%u)\n",
2549 driver, crypto_aead_blocksize(tfm),
2550 crypto_aead_blocksize(generic_tfm));
2556 * Now generate test vectors using the generic implementation, and test
2557 * the other implementation against them.
2559 for (i = 0; i < fuzz_iterations * 8; i++) {
2560 generate_random_aead_testvec(&ctx->rng, generic_req, &ctx->vec,
2561 &ctx->test_desc->suite.aead,
2562 ctx->maxkeysize, ctx->maxdatasize,
2564 sizeof(ctx->vec_name), false);
2565 generate_random_testvec_config(&ctx->rng, &ctx->cfg,
2567 sizeof(ctx->cfgname));
2568 if (!ctx->vec.novrfy) {
2569 err = test_aead_vec_cfg(ENCRYPT, &ctx->vec,
2570 ctx->vec_name, &ctx->cfg,
2571 ctx->req, ctx->tsgls);
2575 if (ctx->vec.crypt_error == 0 || ctx->vec.novrfy) {
2576 err = test_aead_vec_cfg(DECRYPT, &ctx->vec,
2577 ctx->vec_name, &ctx->cfg,
2578 ctx->req, ctx->tsgls);
2586 crypto_free_aead(generic_tfm);
2587 aead_request_free(generic_req);
2591 static int test_aead_extra(const struct alg_test_desc *test_desc,
2592 struct aead_request *req,
2593 struct cipher_test_sglists *tsgls)
2595 struct aead_extra_tests_ctx *ctx;
2602 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2605 init_rnd_state(&ctx->rng);
2607 ctx->tfm = crypto_aead_reqtfm(req);
2608 ctx->test_desc = test_desc;
2610 ctx->maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
2611 ctx->maxkeysize = 0;
2612 for (i = 0; i < test_desc->suite.aead.count; i++)
2613 ctx->maxkeysize = max_t(unsigned int, ctx->maxkeysize,
2614 test_desc->suite.aead.vecs[i].klen);
2616 ctx->vec.key = kmalloc(ctx->maxkeysize, GFP_KERNEL);
2617 ctx->vec.iv = kmalloc(crypto_aead_ivsize(ctx->tfm), GFP_KERNEL);
2618 ctx->vec.assoc = kmalloc(ctx->maxdatasize, GFP_KERNEL);
2619 ctx->vec.ptext = kmalloc(ctx->maxdatasize, GFP_KERNEL);
2620 ctx->vec.ctext = kmalloc(ctx->maxdatasize, GFP_KERNEL);
2621 if (!ctx->vec.key || !ctx->vec.iv || !ctx->vec.assoc ||
2622 !ctx->vec.ptext || !ctx->vec.ctext) {
2627 err = test_aead_vs_generic_impl(ctx);
2631 err = test_aead_inauthentic_inputs(ctx);
2633 kfree(ctx->vec.key);
2635 kfree(ctx->vec.assoc);
2636 kfree(ctx->vec.ptext);
2637 kfree(ctx->vec.ctext);
2641 #else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
2642 static int test_aead_extra(const struct alg_test_desc *test_desc,
2643 struct aead_request *req,
2644 struct cipher_test_sglists *tsgls)
2648 #endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
2650 static int test_aead(int enc, const struct aead_test_suite *suite,
2651 struct aead_request *req,
2652 struct cipher_test_sglists *tsgls)
2657 for (i = 0; i < suite->count; i++) {
2658 err = test_aead_vec(enc, &suite->vecs[i], i, req, tsgls);
2666 static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
2669 const struct aead_test_suite *suite = &desc->suite.aead;
2670 struct crypto_aead *tfm;
2671 struct aead_request *req = NULL;
2672 struct cipher_test_sglists *tsgls = NULL;
2675 if (suite->count <= 0) {
2676 pr_err("alg: aead: empty test suite for %s\n", driver);
2680 tfm = crypto_alloc_aead(driver, type, mask);
2682 pr_err("alg: aead: failed to allocate transform for %s: %ld\n",
2683 driver, PTR_ERR(tfm));
2684 return PTR_ERR(tfm);
2686 driver = crypto_aead_driver_name(tfm);
2688 req = aead_request_alloc(tfm, GFP_KERNEL);
2690 pr_err("alg: aead: failed to allocate request for %s\n",
2696 tsgls = alloc_cipher_test_sglists();
2698 pr_err("alg: aead: failed to allocate test buffers for %s\n",
2704 err = test_aead(ENCRYPT, suite, req, tsgls);
2708 err = test_aead(DECRYPT, suite, req, tsgls);
2712 err = test_aead_extra(desc, req, tsgls);
2714 free_cipher_test_sglists(tsgls);
2715 aead_request_free(req);
2716 crypto_free_aead(tfm);
2720 static int test_cipher(struct crypto_cipher *tfm, int enc,
2721 const struct cipher_testvec *template,
2722 unsigned int tcount)
2724 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
2725 unsigned int i, j, k;
2728 const char *input, *result;
2730 char *xbuf[XBUFSIZE];
2733 if (testmgr_alloc_buf(xbuf))
2742 for (i = 0; i < tcount; i++) {
2744 if (fips_enabled && template[i].fips_skip)
2747 input = enc ? template[i].ptext : template[i].ctext;
2748 result = enc ? template[i].ctext : template[i].ptext;
2752 if (WARN_ON(template[i].len > PAGE_SIZE))
2756 memcpy(data, input, template[i].len);
2758 crypto_cipher_clear_flags(tfm, ~0);
2760 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
2762 ret = crypto_cipher_setkey(tfm, template[i].key,
2765 if (ret == template[i].setkey_error)
2767 pr_err("alg: cipher: %s setkey failed on test vector %u; expected_error=%d, actual_error=%d, flags=%#x\n",
2768 algo, j, template[i].setkey_error, ret,
2769 crypto_cipher_get_flags(tfm));
2772 if (template[i].setkey_error) {
2773 pr_err("alg: cipher: %s setkey unexpectedly succeeded on test vector %u; expected_error=%d\n",
2774 algo, j, template[i].setkey_error);
2779 for (k = 0; k < template[i].len;
2780 k += crypto_cipher_blocksize(tfm)) {
2782 crypto_cipher_encrypt_one(tfm, data + k,
2785 crypto_cipher_decrypt_one(tfm, data + k,
2790 if (memcmp(q, result, template[i].len)) {
2791 printk(KERN_ERR "alg: cipher: Test %d failed "
2792 "on %s for %s\n", j, e, algo);
2793 hexdump(q, template[i].len);
2802 testmgr_free_buf(xbuf);
2807 static int test_skcipher_vec_cfg(int enc, const struct cipher_testvec *vec,
2808 const char *vec_name,
2809 const struct testvec_config *cfg,
2810 struct skcipher_request *req,
2811 struct cipher_test_sglists *tsgls)
2813 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2814 const unsigned int alignmask = crypto_skcipher_alignmask(tfm);
2815 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2816 const char *driver = crypto_skcipher_driver_name(tfm);
2817 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
2818 const char *op = enc ? "encryption" : "decryption";
2819 DECLARE_CRYPTO_WAIT(wait);
2820 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
2821 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
2823 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
2829 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
2831 crypto_skcipher_clear_flags(tfm,
2832 CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
2833 err = do_setkey(crypto_skcipher_setkey, tfm, vec->key, vec->klen,
2836 if (err == vec->setkey_error)
2838 pr_err("alg: skcipher: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
2839 driver, vec_name, vec->setkey_error, err,
2840 crypto_skcipher_get_flags(tfm));
2843 if (vec->setkey_error) {
2844 pr_err("alg: skcipher: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
2845 driver, vec_name, vec->setkey_error);
2849 /* The IV must be copied to a buffer, as the algorithm may modify it */
2851 if (WARN_ON(ivsize > MAX_IVLEN))
2853 if (vec->generates_iv && !enc)
2854 memcpy(iv, vec->iv_out, ivsize);
2856 memcpy(iv, vec->iv, ivsize);
2858 memset(iv, 0, ivsize);
2860 if (vec->generates_iv) {
2861 pr_err("alg: skcipher: %s has ivsize=0 but test vector %s generates IV!\n",
2868 /* Build the src/dst scatterlists */
2869 input.iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
2870 input.iov_len = vec->len;
2871 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
2872 vec->len, vec->len, &input, 1);
2874 pr_err("alg: skcipher: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
2875 driver, op, vec_name, cfg->name);
2879 /* Do the actual encryption or decryption */
2880 testmgr_poison(req->__ctx, crypto_skcipher_reqsize(tfm));
2881 skcipher_request_set_callback(req, req_flags, crypto_req_done, &wait);
2882 skcipher_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
2885 crypto_disable_simd_for_test();
2886 err = enc ? crypto_skcipher_encrypt(req) : crypto_skcipher_decrypt(req);
2888 crypto_reenable_simd_for_test();
2889 err = crypto_wait_req(err, &wait);
2891 /* Check that the algorithm didn't overwrite things it shouldn't have */
2892 if (req->cryptlen != vec->len ||
2894 req->src != tsgls->src.sgl_ptr ||
2895 req->dst != tsgls->dst.sgl_ptr ||
2896 crypto_skcipher_reqtfm(req) != tfm ||
2897 req->base.complete != crypto_req_done ||
2898 req->base.flags != req_flags ||
2899 req->base.data != &wait) {
2900 pr_err("alg: skcipher: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
2901 driver, op, vec_name, cfg->name);
2902 if (req->cryptlen != vec->len)
2903 pr_err("alg: skcipher: changed 'req->cryptlen'\n");
2905 pr_err("alg: skcipher: changed 'req->iv'\n");
2906 if (req->src != tsgls->src.sgl_ptr)
2907 pr_err("alg: skcipher: changed 'req->src'\n");
2908 if (req->dst != tsgls->dst.sgl_ptr)
2909 pr_err("alg: skcipher: changed 'req->dst'\n");
2910 if (crypto_skcipher_reqtfm(req) != tfm)
2911 pr_err("alg: skcipher: changed 'req->base.tfm'\n");
2912 if (req->base.complete != crypto_req_done)
2913 pr_err("alg: skcipher: changed 'req->base.complete'\n");
2914 if (req->base.flags != req_flags)
2915 pr_err("alg: skcipher: changed 'req->base.flags'\n");
2916 if (req->base.data != &wait)
2917 pr_err("alg: skcipher: changed 'req->base.data'\n");
2920 if (is_test_sglist_corrupted(&tsgls->src)) {
2921 pr_err("alg: skcipher: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
2922 driver, op, vec_name, cfg->name);
2925 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
2926 is_test_sglist_corrupted(&tsgls->dst)) {
2927 pr_err("alg: skcipher: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
2928 driver, op, vec_name, cfg->name);
2932 /* Check for success or failure */
2934 if (err == vec->crypt_error)
2936 pr_err("alg: skcipher: %s %s failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
2937 driver, op, vec_name, vec->crypt_error, err, cfg->name);
2940 if (vec->crypt_error) {
2941 pr_err("alg: skcipher: %s %s unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
2942 driver, op, vec_name, vec->crypt_error, cfg->name);
2946 /* Check for the correct output (ciphertext or plaintext) */
2947 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
2949 if (err == -EOVERFLOW) {
2950 pr_err("alg: skcipher: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
2951 driver, op, vec_name, cfg->name);
2955 pr_err("alg: skcipher: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
2956 driver, op, vec_name, cfg->name);
2960 /* If applicable, check that the algorithm generated the correct IV */
2961 if (vec->iv_out && memcmp(iv, vec->iv_out, ivsize) != 0) {
2962 pr_err("alg: skcipher: %s %s test failed (wrong output IV) on test vector %s, cfg=\"%s\"\n",
2963 driver, op, vec_name, cfg->name);
2964 hexdump(iv, ivsize);
2971 static int test_skcipher_vec(int enc, const struct cipher_testvec *vec,
2972 unsigned int vec_num,
2973 struct skcipher_request *req,
2974 struct cipher_test_sglists *tsgls)
2980 if (fips_enabled && vec->fips_skip)
2983 sprintf(vec_name, "%u", vec_num);
2985 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
2986 err = test_skcipher_vec_cfg(enc, vec, vec_name,
2987 &default_cipher_testvec_configs[i],
2993 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2994 if (!noextratests) {
2995 struct rnd_state rng;
2996 struct testvec_config cfg;
2997 char cfgname[TESTVEC_CONFIG_NAMELEN];
2999 init_rnd_state(&rng);
3001 for (i = 0; i < fuzz_iterations; i++) {
3002 generate_random_testvec_config(&rng, &cfg, cfgname,
3004 err = test_skcipher_vec_cfg(enc, vec, vec_name,
3015 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
3017 * Generate a symmetric cipher test vector from the given implementation.
3018 * Assumes the buffers in 'vec' were already allocated.
3020 static void generate_random_cipher_testvec(struct rnd_state *rng,
3021 struct skcipher_request *req,
3022 struct cipher_testvec *vec,
3023 unsigned int maxdatasize,
3024 char *name, size_t max_namelen)
3026 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
3027 const unsigned int maxkeysize = crypto_skcipher_max_keysize(tfm);
3028 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
3029 struct scatterlist src, dst;
3031 DECLARE_CRYPTO_WAIT(wait);
3033 /* Key: length in [0, maxkeysize], but usually choose maxkeysize */
3034 vec->klen = maxkeysize;
3035 if (prandom_u32_below(rng, 4) == 0)
3036 vec->klen = prandom_u32_below(rng, maxkeysize + 1);
3037 generate_random_bytes(rng, (u8 *)vec->key, vec->klen);
3038 vec->setkey_error = crypto_skcipher_setkey(tfm, vec->key, vec->klen);
3041 generate_random_bytes(rng, (u8 *)vec->iv, ivsize);
3044 vec->len = generate_random_length(rng, maxdatasize);
3045 generate_random_bytes(rng, (u8 *)vec->ptext, vec->len);
3047 /* If the key couldn't be set, no need to continue to encrypt. */
3048 if (vec->setkey_error)
3052 sg_init_one(&src, vec->ptext, vec->len);
3053 sg_init_one(&dst, vec->ctext, vec->len);
3054 memcpy(iv, vec->iv, ivsize);
3055 skcipher_request_set_callback(req, 0, crypto_req_done, &wait);
3056 skcipher_request_set_crypt(req, &src, &dst, vec->len, iv);
3057 vec->crypt_error = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
3058 if (vec->crypt_error != 0) {
3060 * The only acceptable error here is for an invalid length, so
3061 * skcipher decryption should fail with the same error too.
3062 * We'll test for this. But to keep the API usage well-defined,
3063 * explicitly initialize the ciphertext buffer too.
3065 memset((u8 *)vec->ctext, 0, vec->len);
3068 snprintf(name, max_namelen, "\"random: len=%u klen=%u\"",
3069 vec->len, vec->klen);
3073 * Test the skcipher algorithm represented by @req against the corresponding
3074 * generic implementation, if one is available.
3076 static int test_skcipher_vs_generic_impl(const char *generic_driver,
3077 struct skcipher_request *req,
3078 struct cipher_test_sglists *tsgls)
3080 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
3081 const unsigned int maxkeysize = crypto_skcipher_max_keysize(tfm);
3082 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
3083 const unsigned int blocksize = crypto_skcipher_blocksize(tfm);
3084 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
3085 const char *algname = crypto_skcipher_alg(tfm)->base.cra_name;
3086 const char *driver = crypto_skcipher_driver_name(tfm);
3087 struct rnd_state rng;
3088 char _generic_driver[CRYPTO_MAX_ALG_NAME];
3089 struct crypto_skcipher *generic_tfm = NULL;
3090 struct skcipher_request *generic_req = NULL;
3092 struct cipher_testvec vec = { 0 };
3094 struct testvec_config *cfg;
3095 char cfgname[TESTVEC_CONFIG_NAMELEN];
3101 /* Keywrap isn't supported here yet as it handles its IV differently. */
3102 if (strncmp(algname, "kw(", 3) == 0)
3105 init_rnd_state(&rng);
3107 if (!generic_driver) { /* Use default naming convention? */
3108 err = build_generic_driver_name(algname, _generic_driver);
3111 generic_driver = _generic_driver;
3114 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
3117 generic_tfm = crypto_alloc_skcipher(generic_driver, 0, 0);
3118 if (IS_ERR(generic_tfm)) {
3119 err = PTR_ERR(generic_tfm);
3120 if (err == -ENOENT) {
3121 pr_warn("alg: skcipher: skipping comparison tests for %s because %s is unavailable\n",
3122 driver, generic_driver);
3125 pr_err("alg: skcipher: error allocating %s (generic impl of %s): %d\n",
3126 generic_driver, algname, err);
3130 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
3136 generic_req = skcipher_request_alloc(generic_tfm, GFP_KERNEL);
3142 /* Check the algorithm properties for consistency. */
3144 if (crypto_skcipher_min_keysize(tfm) !=
3145 crypto_skcipher_min_keysize(generic_tfm)) {
3146 pr_err("alg: skcipher: min keysize for %s (%u) doesn't match generic impl (%u)\n",
3147 driver, crypto_skcipher_min_keysize(tfm),
3148 crypto_skcipher_min_keysize(generic_tfm));
3153 if (maxkeysize != crypto_skcipher_max_keysize(generic_tfm)) {
3154 pr_err("alg: skcipher: max keysize for %s (%u) doesn't match generic impl (%u)\n",
3156 crypto_skcipher_max_keysize(generic_tfm));
3161 if (ivsize != crypto_skcipher_ivsize(generic_tfm)) {
3162 pr_err("alg: skcipher: ivsize for %s (%u) doesn't match generic impl (%u)\n",
3163 driver, ivsize, crypto_skcipher_ivsize(generic_tfm));
3168 if (blocksize != crypto_skcipher_blocksize(generic_tfm)) {
3169 pr_err("alg: skcipher: blocksize for %s (%u) doesn't match generic impl (%u)\n",
3171 crypto_skcipher_blocksize(generic_tfm));
3177 * Now generate test vectors using the generic implementation, and test
3178 * the other implementation against them.
3181 vec.key = kmalloc(maxkeysize, GFP_KERNEL);
3182 vec.iv = kmalloc(ivsize, GFP_KERNEL);
3183 vec.ptext = kmalloc(maxdatasize, GFP_KERNEL);
3184 vec.ctext = kmalloc(maxdatasize, GFP_KERNEL);
3185 if (!vec.key || !vec.iv || !vec.ptext || !vec.ctext) {
3190 for (i = 0; i < fuzz_iterations * 8; i++) {
3191 generate_random_cipher_testvec(&rng, generic_req, &vec,
3193 vec_name, sizeof(vec_name));
3194 generate_random_testvec_config(&rng, cfg, cfgname,
3197 err = test_skcipher_vec_cfg(ENCRYPT, &vec, vec_name,
3201 err = test_skcipher_vec_cfg(DECRYPT, &vec, vec_name,
3214 crypto_free_skcipher(generic_tfm);
3215 skcipher_request_free(generic_req);
3218 #else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
3219 static int test_skcipher_vs_generic_impl(const char *generic_driver,
3220 struct skcipher_request *req,
3221 struct cipher_test_sglists *tsgls)
3225 #endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
3227 static int test_skcipher(int enc, const struct cipher_test_suite *suite,
3228 struct skcipher_request *req,
3229 struct cipher_test_sglists *tsgls)
3234 for (i = 0; i < suite->count; i++) {
3235 err = test_skcipher_vec(enc, &suite->vecs[i], i, req, tsgls);
3243 static int alg_test_skcipher(const struct alg_test_desc *desc,
3244 const char *driver, u32 type, u32 mask)
3246 const struct cipher_test_suite *suite = &desc->suite.cipher;
3247 struct crypto_skcipher *tfm;
3248 struct skcipher_request *req = NULL;
3249 struct cipher_test_sglists *tsgls = NULL;
3252 if (suite->count <= 0) {
3253 pr_err("alg: skcipher: empty test suite for %s\n", driver);
3257 tfm = crypto_alloc_skcipher(driver, type, mask);
3259 pr_err("alg: skcipher: failed to allocate transform for %s: %ld\n",
3260 driver, PTR_ERR(tfm));
3261 return PTR_ERR(tfm);
3263 driver = crypto_skcipher_driver_name(tfm);
3265 req = skcipher_request_alloc(tfm, GFP_KERNEL);
3267 pr_err("alg: skcipher: failed to allocate request for %s\n",
3273 tsgls = alloc_cipher_test_sglists();
3275 pr_err("alg: skcipher: failed to allocate test buffers for %s\n",
3281 err = test_skcipher(ENCRYPT, suite, req, tsgls);
3285 err = test_skcipher(DECRYPT, suite, req, tsgls);
3289 err = test_skcipher_vs_generic_impl(desc->generic_driver, req, tsgls);
3291 free_cipher_test_sglists(tsgls);
3292 skcipher_request_free(req);
3293 crypto_free_skcipher(tfm);
3297 static int test_comp(struct crypto_comp *tfm,
3298 const struct comp_testvec *ctemplate,
3299 const struct comp_testvec *dtemplate,
3300 int ctcount, int dtcount)
3302 const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
3303 char *output, *decomp_output;
3307 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3311 decomp_output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3312 if (!decomp_output) {
3317 for (i = 0; i < ctcount; i++) {
3319 unsigned int dlen = COMP_BUF_SIZE;
3321 memset(output, 0, COMP_BUF_SIZE);
3322 memset(decomp_output, 0, COMP_BUF_SIZE);
3324 ilen = ctemplate[i].inlen;
3325 ret = crypto_comp_compress(tfm, ctemplate[i].input,
3326 ilen, output, &dlen);
3328 printk(KERN_ERR "alg: comp: compression failed "
3329 "on test %d for %s: ret=%d\n", i + 1, algo,
3335 dlen = COMP_BUF_SIZE;
3336 ret = crypto_comp_decompress(tfm, output,
3337 ilen, decomp_output, &dlen);
3339 pr_err("alg: comp: compression failed: decompress: on test %d for %s failed: ret=%d\n",
3344 if (dlen != ctemplate[i].inlen) {
3345 printk(KERN_ERR "alg: comp: Compression test %d "
3346 "failed for %s: output len = %d\n", i + 1, algo,
3352 if (memcmp(decomp_output, ctemplate[i].input,
3353 ctemplate[i].inlen)) {
3354 pr_err("alg: comp: compression failed: output differs: on test %d for %s\n",
3356 hexdump(decomp_output, dlen);
3362 for (i = 0; i < dtcount; i++) {
3364 unsigned int dlen = COMP_BUF_SIZE;
3366 memset(decomp_output, 0, COMP_BUF_SIZE);
3368 ilen = dtemplate[i].inlen;
3369 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
3370 ilen, decomp_output, &dlen);
3372 printk(KERN_ERR "alg: comp: decompression failed "
3373 "on test %d for %s: ret=%d\n", i + 1, algo,
3378 if (dlen != dtemplate[i].outlen) {
3379 printk(KERN_ERR "alg: comp: Decompression test %d "
3380 "failed for %s: output len = %d\n", i + 1, algo,
3386 if (memcmp(decomp_output, dtemplate[i].output, dlen)) {
3387 printk(KERN_ERR "alg: comp: Decompression test %d "
3388 "failed for %s\n", i + 1, algo);
3389 hexdump(decomp_output, dlen);
3398 kfree(decomp_output);
3403 static int test_acomp(struct crypto_acomp *tfm,
3404 const struct comp_testvec *ctemplate,
3405 const struct comp_testvec *dtemplate,
3406 int ctcount, int dtcount)
3408 const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
3410 char *output, *decomp_out;
3412 struct scatterlist src, dst;
3413 struct acomp_req *req;
3414 struct crypto_wait wait;
3416 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3420 decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3426 for (i = 0; i < ctcount; i++) {
3427 unsigned int dlen = COMP_BUF_SIZE;
3428 int ilen = ctemplate[i].inlen;
3431 input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL);
3437 memset(output, 0, dlen);
3438 crypto_init_wait(&wait);
3439 sg_init_one(&src, input_vec, ilen);
3440 sg_init_one(&dst, output, dlen);
3442 req = acomp_request_alloc(tfm);
3444 pr_err("alg: acomp: request alloc failed for %s\n",
3451 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3452 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3453 crypto_req_done, &wait);
3455 ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
3457 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
3460 acomp_request_free(req);
3465 dlen = COMP_BUF_SIZE;
3466 sg_init_one(&src, output, ilen);
3467 sg_init_one(&dst, decomp_out, dlen);
3468 crypto_init_wait(&wait);
3469 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3471 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
3473 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
3476 acomp_request_free(req);
3480 if (req->dlen != ctemplate[i].inlen) {
3481 pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
3482 i + 1, algo, req->dlen);
3485 acomp_request_free(req);
3489 if (memcmp(input_vec, decomp_out, req->dlen)) {
3490 pr_err("alg: acomp: Compression test %d failed for %s\n",
3492 hexdump(output, req->dlen);
3495 acomp_request_free(req);
3499 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
3500 crypto_init_wait(&wait);
3501 sg_init_one(&src, input_vec, ilen);
3502 acomp_request_set_params(req, &src, NULL, ilen, 0);
3504 ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
3506 pr_err("alg: acomp: compression failed on NULL dst buffer test %d for %s: ret=%d\n",
3509 acomp_request_free(req);
3515 acomp_request_free(req);
3518 for (i = 0; i < dtcount; i++) {
3519 unsigned int dlen = COMP_BUF_SIZE;
3520 int ilen = dtemplate[i].inlen;
3523 input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL);
3529 memset(output, 0, dlen);
3530 crypto_init_wait(&wait);
3531 sg_init_one(&src, input_vec, ilen);
3532 sg_init_one(&dst, output, dlen);
3534 req = acomp_request_alloc(tfm);
3536 pr_err("alg: acomp: request alloc failed for %s\n",
3543 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3544 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3545 crypto_req_done, &wait);
3547 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
3549 pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
3552 acomp_request_free(req);
3556 if (req->dlen != dtemplate[i].outlen) {
3557 pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
3558 i + 1, algo, req->dlen);
3561 acomp_request_free(req);
3565 if (memcmp(output, dtemplate[i].output, req->dlen)) {
3566 pr_err("alg: acomp: Decompression test %d failed for %s\n",
3568 hexdump(output, req->dlen);
3571 acomp_request_free(req);
3575 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
3576 crypto_init_wait(&wait);
3577 acomp_request_set_params(req, &src, NULL, ilen, 0);
3579 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
3581 pr_err("alg: acomp: decompression failed on NULL dst buffer test %d for %s: ret=%d\n",
3584 acomp_request_free(req);
3590 acomp_request_free(req);
3601 static int test_cprng(struct crypto_rng *tfm,
3602 const struct cprng_testvec *template,
3603 unsigned int tcount)
3605 const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
3606 int err = 0, i, j, seedsize;
3610 seedsize = crypto_rng_seedsize(tfm);
3612 seed = kmalloc(seedsize, GFP_KERNEL);
3614 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
3619 for (i = 0; i < tcount; i++) {
3620 memset(result, 0, 32);
3622 memcpy(seed, template[i].v, template[i].vlen);
3623 memcpy(seed + template[i].vlen, template[i].key,
3625 memcpy(seed + template[i].vlen + template[i].klen,
3626 template[i].dt, template[i].dtlen);
3628 err = crypto_rng_reset(tfm, seed, seedsize);
3630 printk(KERN_ERR "alg: cprng: Failed to reset rng "
3635 for (j = 0; j < template[i].loops; j++) {
3636 err = crypto_rng_get_bytes(tfm, result,
3639 printk(KERN_ERR "alg: cprng: Failed to obtain "
3640 "the correct amount of random data for "
3641 "%s (requested %d)\n", algo,
3647 err = memcmp(result, template[i].result,
3650 printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
3652 hexdump(result, template[i].rlen);
3663 static int alg_test_cipher(const struct alg_test_desc *desc,
3664 const char *driver, u32 type, u32 mask)
3666 const struct cipher_test_suite *suite = &desc->suite.cipher;
3667 struct crypto_cipher *tfm;
3670 tfm = crypto_alloc_cipher(driver, type, mask);
3672 printk(KERN_ERR "alg: cipher: Failed to load transform for "
3673 "%s: %ld\n", driver, PTR_ERR(tfm));
3674 return PTR_ERR(tfm);
3677 err = test_cipher(tfm, ENCRYPT, suite->vecs, suite->count);
3679 err = test_cipher(tfm, DECRYPT, suite->vecs, suite->count);
3681 crypto_free_cipher(tfm);
3685 static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
3688 struct crypto_comp *comp;
3689 struct crypto_acomp *acomp;
3691 u32 algo_type = type & CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
3693 if (algo_type == CRYPTO_ALG_TYPE_ACOMPRESS) {
3694 acomp = crypto_alloc_acomp(driver, type, mask);
3695 if (IS_ERR(acomp)) {
3696 pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
3697 driver, PTR_ERR(acomp));
3698 return PTR_ERR(acomp);
3700 err = test_acomp(acomp, desc->suite.comp.comp.vecs,
3701 desc->suite.comp.decomp.vecs,
3702 desc->suite.comp.comp.count,
3703 desc->suite.comp.decomp.count);
3704 crypto_free_acomp(acomp);
3706 comp = crypto_alloc_comp(driver, type, mask);
3708 pr_err("alg: comp: Failed to load transform for %s: %ld\n",
3709 driver, PTR_ERR(comp));
3710 return PTR_ERR(comp);
3713 err = test_comp(comp, desc->suite.comp.comp.vecs,
3714 desc->suite.comp.decomp.vecs,
3715 desc->suite.comp.comp.count,
3716 desc->suite.comp.decomp.count);
3718 crypto_free_comp(comp);
3723 static int alg_test_crc32c(const struct alg_test_desc *desc,
3724 const char *driver, u32 type, u32 mask)
3726 struct crypto_shash *tfm;
3730 err = alg_test_hash(desc, driver, type, mask);
3734 tfm = crypto_alloc_shash(driver, type, mask);
3736 if (PTR_ERR(tfm) == -ENOENT) {
3738 * This crc32c implementation is only available through
3739 * ahash API, not the shash API, so the remaining part
3740 * of the test is not applicable to it.
3744 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
3745 "%ld\n", driver, PTR_ERR(tfm));
3746 return PTR_ERR(tfm);
3748 driver = crypto_shash_driver_name(tfm);
3751 SHASH_DESC_ON_STACK(shash, tfm);
3752 u32 *ctx = (u32 *)shash_desc_ctx(shash);
3757 err = crypto_shash_final(shash, (u8 *)&val);
3759 printk(KERN_ERR "alg: crc32c: Operation failed for "
3760 "%s: %d\n", driver, err);
3764 if (val != cpu_to_le32(~420553207)) {
3765 pr_err("alg: crc32c: Test failed for %s: %u\n",
3766 driver, le32_to_cpu(val));
3771 crypto_free_shash(tfm);
3776 static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
3779 struct crypto_rng *rng;
3782 rng = crypto_alloc_rng(driver, type, mask);
3784 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
3785 "%ld\n", driver, PTR_ERR(rng));
3786 return PTR_ERR(rng);
3789 err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
3791 crypto_free_rng(rng);
3797 static int drbg_cavs_test(const struct drbg_testvec *test, int pr,
3798 const char *driver, u32 type, u32 mask)
3801 struct crypto_rng *drng;
3802 struct drbg_test_data test_data;
3803 struct drbg_string addtl, pers, testentropy;
3804 unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
3809 drng = crypto_alloc_rng(driver, type, mask);
3811 printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
3813 kfree_sensitive(buf);
3817 test_data.testentropy = &testentropy;
3818 drbg_string_fill(&testentropy, test->entropy, test->entropylen);
3819 drbg_string_fill(&pers, test->pers, test->perslen);
3820 ret = crypto_drbg_reset_test(drng, &pers, &test_data);
3822 printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
3826 drbg_string_fill(&addtl, test->addtla, test->addtllen);
3828 drbg_string_fill(&testentropy, test->entpra, test->entprlen);
3829 ret = crypto_drbg_get_bytes_addtl_test(drng,
3830 buf, test->expectedlen, &addtl, &test_data);
3832 ret = crypto_drbg_get_bytes_addtl(drng,
3833 buf, test->expectedlen, &addtl);
3836 printk(KERN_ERR "alg: drbg: could not obtain random data for "
3837 "driver %s\n", driver);
3841 drbg_string_fill(&addtl, test->addtlb, test->addtllen);
3843 drbg_string_fill(&testentropy, test->entprb, test->entprlen);
3844 ret = crypto_drbg_get_bytes_addtl_test(drng,
3845 buf, test->expectedlen, &addtl, &test_data);
3847 ret = crypto_drbg_get_bytes_addtl(drng,
3848 buf, test->expectedlen, &addtl);
3851 printk(KERN_ERR "alg: drbg: could not obtain random data for "
3852 "driver %s\n", driver);
3856 ret = memcmp(test->expected, buf, test->expectedlen);
3859 crypto_free_rng(drng);
3860 kfree_sensitive(buf);
3865 static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
3871 const struct drbg_testvec *template = desc->suite.drbg.vecs;
3872 unsigned int tcount = desc->suite.drbg.count;
3874 if (0 == memcmp(driver, "drbg_pr_", 8))
3877 for (i = 0; i < tcount; i++) {
3878 err = drbg_cavs_test(&template[i], pr, driver, type, mask);
3880 printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
3890 static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
3893 struct kpp_request *req;
3894 void *input_buf = NULL;
3895 void *output_buf = NULL;
3896 void *a_public = NULL;
3898 void *shared_secret = NULL;
3899 struct crypto_wait wait;
3900 unsigned int out_len_max;
3902 struct scatterlist src, dst;
3904 req = kpp_request_alloc(tfm, GFP_KERNEL);
3908 crypto_init_wait(&wait);
3910 err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
3914 out_len_max = crypto_kpp_maxsize(tfm);
3915 output_buf = kzalloc(out_len_max, GFP_KERNEL);
3921 /* Use appropriate parameter as base */
3922 kpp_request_set_input(req, NULL, 0);
3923 sg_init_one(&dst, output_buf, out_len_max);
3924 kpp_request_set_output(req, &dst, out_len_max);
3925 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3926 crypto_req_done, &wait);
3928 /* Compute party A's public key */
3929 err = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
3931 pr_err("alg: %s: Party A: generate public key test failed. err %d\n",
3937 /* Save party A's public key */
3938 a_public = kmemdup(sg_virt(req->dst), out_len_max, GFP_KERNEL);
3944 /* Verify calculated public key */
3945 if (memcmp(vec->expected_a_public, sg_virt(req->dst),
3946 vec->expected_a_public_size)) {
3947 pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n",
3954 /* Calculate shared secret key by using counter part (b) public key. */
3955 input_buf = kmemdup(vec->b_public, vec->b_public_size, GFP_KERNEL);
3961 sg_init_one(&src, input_buf, vec->b_public_size);
3962 sg_init_one(&dst, output_buf, out_len_max);
3963 kpp_request_set_input(req, &src, vec->b_public_size);
3964 kpp_request_set_output(req, &dst, out_len_max);
3965 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3966 crypto_req_done, &wait);
3967 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait);
3969 pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n",
3975 /* Save the shared secret obtained by party A */
3976 a_ss = kmemdup(sg_virt(req->dst), vec->expected_ss_size, GFP_KERNEL);
3983 * Calculate party B's shared secret by using party A's
3986 err = crypto_kpp_set_secret(tfm, vec->b_secret,
3987 vec->b_secret_size);
3991 sg_init_one(&src, a_public, vec->expected_a_public_size);
3992 sg_init_one(&dst, output_buf, out_len_max);
3993 kpp_request_set_input(req, &src, vec->expected_a_public_size);
3994 kpp_request_set_output(req, &dst, out_len_max);
3995 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3996 crypto_req_done, &wait);
3997 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req),
4000 pr_err("alg: %s: Party B: compute shared secret failed. err %d\n",
4005 shared_secret = a_ss;
4007 shared_secret = (void *)vec->expected_ss;
4011 * verify shared secret from which the user will derive
4012 * secret key by executing whatever hash it has chosen
4014 if (memcmp(shared_secret, sg_virt(req->dst),
4015 vec->expected_ss_size)) {
4016 pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
4028 kpp_request_free(req);
4032 static int test_kpp(struct crypto_kpp *tfm, const char *alg,
4033 const struct kpp_testvec *vecs, unsigned int tcount)
4037 for (i = 0; i < tcount; i++) {
4038 ret = do_test_kpp(tfm, vecs++, alg);
4040 pr_err("alg: %s: test failed on vector %d, err=%d\n",
4048 static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
4051 struct crypto_kpp *tfm;
4054 tfm = crypto_alloc_kpp(driver, type, mask);
4056 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
4057 driver, PTR_ERR(tfm));
4058 return PTR_ERR(tfm);
4060 if (desc->suite.kpp.vecs)
4061 err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
4062 desc->suite.kpp.count);
4064 crypto_free_kpp(tfm);
4068 static u8 *test_pack_u32(u8 *dst, u32 val)
4070 memcpy(dst, &val, sizeof(val));
4071 return dst + sizeof(val);
4074 static int test_akcipher_one(struct crypto_akcipher *tfm,
4075 const struct akcipher_testvec *vecs)
4077 char *xbuf[XBUFSIZE];
4078 struct akcipher_request *req;
4079 void *outbuf_enc = NULL;
4080 void *outbuf_dec = NULL;
4081 struct crypto_wait wait;
4082 unsigned int out_len_max, out_len = 0;
4084 struct scatterlist src, dst, src_tab[3];
4086 unsigned int m_size, c_size;
4090 if (testmgr_alloc_buf(xbuf))
4093 req = akcipher_request_alloc(tfm, GFP_KERNEL);
4097 crypto_init_wait(&wait);
4099 key = kmalloc(vecs->key_len + sizeof(u32) * 2 + vecs->param_len,
4103 memcpy(key, vecs->key, vecs->key_len);
4104 ptr = key + vecs->key_len;
4105 ptr = test_pack_u32(ptr, vecs->algo);
4106 ptr = test_pack_u32(ptr, vecs->param_len);
4107 memcpy(ptr, vecs->params, vecs->param_len);
4109 if (vecs->public_key_vec)
4110 err = crypto_akcipher_set_pub_key(tfm, key, vecs->key_len);
4112 err = crypto_akcipher_set_priv_key(tfm, key, vecs->key_len);
4117 * First run test which do not require a private key, such as
4118 * encrypt or verify.
4121 out_len_max = crypto_akcipher_maxsize(tfm);
4122 outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
4126 if (!vecs->siggen_sigver_test) {
4128 m_size = vecs->m_size;
4130 c_size = vecs->c_size;
4133 /* Swap args so we could keep plaintext (digest)
4134 * in vecs->m, and cooked signature in vecs->c.
4136 m = vecs->c; /* signature */
4137 m_size = vecs->c_size;
4138 c = vecs->m; /* digest */
4139 c_size = vecs->m_size;
4144 if (WARN_ON(m_size > PAGE_SIZE))
4146 memcpy(xbuf[0], m, m_size);
4148 sg_init_table(src_tab, 3);
4149 sg_set_buf(&src_tab[0], xbuf[0], 8);
4150 sg_set_buf(&src_tab[1], xbuf[0] + 8, m_size - 8);
4151 if (vecs->siggen_sigver_test) {
4152 if (WARN_ON(c_size > PAGE_SIZE))
4154 memcpy(xbuf[1], c, c_size);
4155 sg_set_buf(&src_tab[2], xbuf[1], c_size);
4156 akcipher_request_set_crypt(req, src_tab, NULL, m_size, c_size);
4158 sg_init_one(&dst, outbuf_enc, out_len_max);
4159 akcipher_request_set_crypt(req, src_tab, &dst, m_size,
4162 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
4163 crypto_req_done, &wait);
4165 err = crypto_wait_req(vecs->siggen_sigver_test ?
4166 /* Run asymmetric signature verification */
4167 crypto_akcipher_verify(req) :
4168 /* Run asymmetric encrypt */
4169 crypto_akcipher_encrypt(req), &wait);
4171 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
4174 if (!vecs->siggen_sigver_test && c) {
4175 if (req->dst_len != c_size) {
4176 pr_err("alg: akcipher: %s test failed. Invalid output len\n",
4181 /* verify that encrypted message is equal to expected */
4182 if (memcmp(c, outbuf_enc, c_size) != 0) {
4183 pr_err("alg: akcipher: %s test failed. Invalid output\n",
4185 hexdump(outbuf_enc, c_size);
4192 * Don't invoke (decrypt or sign) test which require a private key
4193 * for vectors with only a public key.
4195 if (vecs->public_key_vec) {
4199 outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
4205 if (!vecs->siggen_sigver_test && !c) {
4207 c_size = req->dst_len;
4211 op = vecs->siggen_sigver_test ? "sign" : "decrypt";
4212 if (WARN_ON(c_size > PAGE_SIZE))
4214 memcpy(xbuf[0], c, c_size);
4216 sg_init_one(&src, xbuf[0], c_size);
4217 sg_init_one(&dst, outbuf_dec, out_len_max);
4218 crypto_init_wait(&wait);
4219 akcipher_request_set_crypt(req, &src, &dst, c_size, out_len_max);
4221 err = crypto_wait_req(vecs->siggen_sigver_test ?
4222 /* Run asymmetric signature generation */
4223 crypto_akcipher_sign(req) :
4224 /* Run asymmetric decrypt */
4225 crypto_akcipher_decrypt(req), &wait);
4227 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
4230 out_len = req->dst_len;
4231 if (out_len < m_size) {
4232 pr_err("alg: akcipher: %s test failed. Invalid output len %u\n",
4237 /* verify that decrypted message is equal to the original msg */
4238 if (memchr_inv(outbuf_dec, 0, out_len - m_size) ||
4239 memcmp(m, outbuf_dec + out_len - m_size, m_size)) {
4240 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
4241 hexdump(outbuf_dec, out_len);
4250 akcipher_request_free(req);
4252 testmgr_free_buf(xbuf);
4256 static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
4257 const struct akcipher_testvec *vecs,
4258 unsigned int tcount)
4261 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
4264 for (i = 0; i < tcount; i++) {
4265 ret = test_akcipher_one(tfm, vecs++);
4269 pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
4276 static int alg_test_akcipher(const struct alg_test_desc *desc,
4277 const char *driver, u32 type, u32 mask)
4279 struct crypto_akcipher *tfm;
4282 tfm = crypto_alloc_akcipher(driver, type, mask);
4284 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
4285 driver, PTR_ERR(tfm));
4286 return PTR_ERR(tfm);
4288 if (desc->suite.akcipher.vecs)
4289 err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
4290 desc->suite.akcipher.count);
4292 crypto_free_akcipher(tfm);
4296 static int alg_test_null(const struct alg_test_desc *desc,
4297 const char *driver, u32 type, u32 mask)
4302 #define ____VECS(tv) .vecs = tv, .count = ARRAY_SIZE(tv)
4303 #define __VECS(tv) { ____VECS(tv) }
4305 /* Please keep this list sorted by algorithm name. */
4306 static const struct alg_test_desc alg_test_descs[] = {
4308 .alg = "adiantum(xchacha12,aes)",
4309 .generic_driver = "adiantum(xchacha12-generic,aes-generic,nhpoly1305-generic)",
4310 .test = alg_test_skcipher,
4312 .cipher = __VECS(adiantum_xchacha12_aes_tv_template)
4315 .alg = "adiantum(xchacha20,aes)",
4316 .generic_driver = "adiantum(xchacha20-generic,aes-generic,nhpoly1305-generic)",
4317 .test = alg_test_skcipher,
4319 .cipher = __VECS(adiantum_xchacha20_aes_tv_template)
4323 .test = alg_test_aead,
4325 .aead = __VECS(aegis128_tv_template)
4328 .alg = "ansi_cprng",
4329 .test = alg_test_cprng,
4331 .cprng = __VECS(ansi_cprng_aes_tv_template)
4334 .alg = "authenc(hmac(md5),ecb(cipher_null))",
4335 .test = alg_test_aead,
4337 .aead = __VECS(hmac_md5_ecb_cipher_null_tv_template)
4340 .alg = "authenc(hmac(sha1),cbc(aes))",
4341 .test = alg_test_aead,
4344 .aead = __VECS(hmac_sha1_aes_cbc_tv_temp)
4347 .alg = "authenc(hmac(sha1),cbc(des))",
4348 .test = alg_test_aead,
4350 .aead = __VECS(hmac_sha1_des_cbc_tv_temp)
4353 .alg = "authenc(hmac(sha1),cbc(des3_ede))",
4354 .test = alg_test_aead,
4356 .aead = __VECS(hmac_sha1_des3_ede_cbc_tv_temp)
4359 .alg = "authenc(hmac(sha1),ctr(aes))",
4360 .test = alg_test_null,
4363 .alg = "authenc(hmac(sha1),ecb(cipher_null))",
4364 .test = alg_test_aead,
4366 .aead = __VECS(hmac_sha1_ecb_cipher_null_tv_temp)
4369 .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
4370 .test = alg_test_null,
4373 .alg = "authenc(hmac(sha224),cbc(des))",
4374 .test = alg_test_aead,
4376 .aead = __VECS(hmac_sha224_des_cbc_tv_temp)
4379 .alg = "authenc(hmac(sha224),cbc(des3_ede))",
4380 .test = alg_test_aead,
4382 .aead = __VECS(hmac_sha224_des3_ede_cbc_tv_temp)
4385 .alg = "authenc(hmac(sha256),cbc(aes))",
4386 .test = alg_test_aead,
4389 .aead = __VECS(hmac_sha256_aes_cbc_tv_temp)
4392 .alg = "authenc(hmac(sha256),cbc(des))",
4393 .test = alg_test_aead,
4395 .aead = __VECS(hmac_sha256_des_cbc_tv_temp)
4398 .alg = "authenc(hmac(sha256),cbc(des3_ede))",
4399 .test = alg_test_aead,
4401 .aead = __VECS(hmac_sha256_des3_ede_cbc_tv_temp)
4404 .alg = "authenc(hmac(sha256),ctr(aes))",
4405 .test = alg_test_null,
4408 .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
4409 .test = alg_test_null,
4412 .alg = "authenc(hmac(sha384),cbc(des))",
4413 .test = alg_test_aead,
4415 .aead = __VECS(hmac_sha384_des_cbc_tv_temp)
4418 .alg = "authenc(hmac(sha384),cbc(des3_ede))",
4419 .test = alg_test_aead,
4421 .aead = __VECS(hmac_sha384_des3_ede_cbc_tv_temp)
4424 .alg = "authenc(hmac(sha384),ctr(aes))",
4425 .test = alg_test_null,
4428 .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
4429 .test = alg_test_null,
4432 .alg = "authenc(hmac(sha512),cbc(aes))",
4434 .test = alg_test_aead,
4436 .aead = __VECS(hmac_sha512_aes_cbc_tv_temp)
4439 .alg = "authenc(hmac(sha512),cbc(des))",
4440 .test = alg_test_aead,
4442 .aead = __VECS(hmac_sha512_des_cbc_tv_temp)
4445 .alg = "authenc(hmac(sha512),cbc(des3_ede))",
4446 .test = alg_test_aead,
4448 .aead = __VECS(hmac_sha512_des3_ede_cbc_tv_temp)
4451 .alg = "authenc(hmac(sha512),ctr(aes))",
4452 .test = alg_test_null,
4455 .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
4456 .test = alg_test_null,
4459 .alg = "blake2b-160",
4460 .test = alg_test_hash,
4463 .hash = __VECS(blake2b_160_tv_template)
4466 .alg = "blake2b-256",
4467 .test = alg_test_hash,
4470 .hash = __VECS(blake2b_256_tv_template)
4473 .alg = "blake2b-384",
4474 .test = alg_test_hash,
4477 .hash = __VECS(blake2b_384_tv_template)
4480 .alg = "blake2b-512",
4481 .test = alg_test_hash,
4484 .hash = __VECS(blake2b_512_tv_template)
4488 .test = alg_test_skcipher,
4491 .cipher = __VECS(aes_cbc_tv_template)
4494 .alg = "cbc(anubis)",
4495 .test = alg_test_skcipher,
4497 .cipher = __VECS(anubis_cbc_tv_template)
4501 .test = alg_test_skcipher,
4503 .cipher = __VECS(aria_cbc_tv_template)
4506 .alg = "cbc(blowfish)",
4507 .test = alg_test_skcipher,
4509 .cipher = __VECS(bf_cbc_tv_template)
4512 .alg = "cbc(camellia)",
4513 .test = alg_test_skcipher,
4515 .cipher = __VECS(camellia_cbc_tv_template)
4518 .alg = "cbc(cast5)",
4519 .test = alg_test_skcipher,
4521 .cipher = __VECS(cast5_cbc_tv_template)
4524 .alg = "cbc(cast6)",
4525 .test = alg_test_skcipher,
4527 .cipher = __VECS(cast6_cbc_tv_template)
4531 .test = alg_test_skcipher,
4533 .cipher = __VECS(des_cbc_tv_template)
4536 .alg = "cbc(des3_ede)",
4537 .test = alg_test_skcipher,
4539 .cipher = __VECS(des3_ede_cbc_tv_template)
4542 /* Same as cbc(aes) except the key is stored in
4543 * hardware secure memory which we reference by index
4546 .test = alg_test_null,
4549 /* Same as cbc(sm4) except the key is stored in
4550 * hardware secure memory which we reference by index
4553 .test = alg_test_null,
4555 .alg = "cbc(serpent)",
4556 .test = alg_test_skcipher,
4558 .cipher = __VECS(serpent_cbc_tv_template)
4562 .test = alg_test_skcipher,
4564 .cipher = __VECS(sm4_cbc_tv_template)
4567 .alg = "cbc(twofish)",
4568 .test = alg_test_skcipher,
4570 .cipher = __VECS(tf_cbc_tv_template)
4573 #if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
4574 .alg = "cbc-paes-s390",
4576 .test = alg_test_skcipher,
4578 .cipher = __VECS(aes_cbc_tv_template)
4582 .alg = "cbcmac(aes)",
4583 .test = alg_test_hash,
4585 .hash = __VECS(aes_cbcmac_tv_template)
4588 .alg = "cbcmac(sm4)",
4589 .test = alg_test_hash,
4591 .hash = __VECS(sm4_cbcmac_tv_template)
4595 .generic_driver = "ccm_base(ctr(aes-generic),cbcmac(aes-generic))",
4596 .test = alg_test_aead,
4600 ____VECS(aes_ccm_tv_template),
4601 .einval_allowed = 1,
4606 .generic_driver = "ccm_base(ctr(sm4-generic),cbcmac(sm4-generic))",
4607 .test = alg_test_aead,
4610 ____VECS(sm4_ccm_tv_template),
4611 .einval_allowed = 1,
4616 .test = alg_test_skcipher,
4619 .cipher = __VECS(aes_cfb_tv_template)
4623 .test = alg_test_skcipher,
4625 .cipher = __VECS(aria_cfb_tv_template)
4629 .test = alg_test_skcipher,
4631 .cipher = __VECS(sm4_cfb_tv_template)
4635 .test = alg_test_skcipher,
4637 .cipher = __VECS(chacha20_tv_template)
4642 .test = alg_test_hash,
4644 .hash = __VECS(aes_cmac128_tv_template)
4647 .alg = "cmac(camellia)",
4648 .test = alg_test_hash,
4650 .hash = __VECS(camellia_cmac128_tv_template)
4653 .alg = "cmac(des3_ede)",
4654 .test = alg_test_hash,
4656 .hash = __VECS(des3_ede_cmac64_tv_template)
4660 .test = alg_test_hash,
4662 .hash = __VECS(sm4_cmac128_tv_template)
4665 .alg = "compress_null",
4666 .test = alg_test_null,
4669 .test = alg_test_hash,
4672 .hash = __VECS(crc32_tv_template)
4676 .test = alg_test_crc32c,
4679 .hash = __VECS(crc32c_tv_template)
4682 .alg = "crc64-rocksoft",
4683 .test = alg_test_hash,
4686 .hash = __VECS(crc64_rocksoft_tv_template)
4690 .test = alg_test_hash,
4693 .hash = __VECS(crct10dif_tv_template)
4697 .test = alg_test_skcipher,
4700 .cipher = __VECS(aes_ctr_tv_template)
4704 .test = alg_test_skcipher,
4706 .cipher = __VECS(aria_ctr_tv_template)
4709 .alg = "ctr(blowfish)",
4710 .test = alg_test_skcipher,
4712 .cipher = __VECS(bf_ctr_tv_template)
4715 .alg = "ctr(camellia)",
4716 .test = alg_test_skcipher,
4718 .cipher = __VECS(camellia_ctr_tv_template)
4721 .alg = "ctr(cast5)",
4722 .test = alg_test_skcipher,
4724 .cipher = __VECS(cast5_ctr_tv_template)
4727 .alg = "ctr(cast6)",
4728 .test = alg_test_skcipher,
4730 .cipher = __VECS(cast6_ctr_tv_template)
4734 .test = alg_test_skcipher,
4736 .cipher = __VECS(des_ctr_tv_template)
4739 .alg = "ctr(des3_ede)",
4740 .test = alg_test_skcipher,
4742 .cipher = __VECS(des3_ede_ctr_tv_template)
4745 /* Same as ctr(aes) except the key is stored in
4746 * hardware secure memory which we reference by index
4749 .test = alg_test_null,
4753 /* Same as ctr(sm4) except the key is stored in
4754 * hardware secure memory which we reference by index
4757 .test = alg_test_null,
4759 .alg = "ctr(serpent)",
4760 .test = alg_test_skcipher,
4762 .cipher = __VECS(serpent_ctr_tv_template)
4766 .test = alg_test_skcipher,
4768 .cipher = __VECS(sm4_ctr_tv_template)
4771 .alg = "ctr(twofish)",
4772 .test = alg_test_skcipher,
4774 .cipher = __VECS(tf_ctr_tv_template)
4777 #if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
4778 .alg = "ctr-paes-s390",
4780 .test = alg_test_skcipher,
4782 .cipher = __VECS(aes_ctr_tv_template)
4786 .alg = "cts(cbc(aes))",
4787 .test = alg_test_skcipher,
4790 .cipher = __VECS(cts_mode_tv_template)
4793 /* Same as cts(cbc((aes)) except the key is stored in
4794 * hardware secure memory which we reference by index
4796 .alg = "cts(cbc(paes))",
4797 .test = alg_test_null,
4800 .alg = "cts(cbc(sm4))",
4801 .test = alg_test_skcipher,
4803 .cipher = __VECS(sm4_cts_tv_template)
4806 .alg = "curve25519",
4807 .test = alg_test_kpp,
4809 .kpp = __VECS(curve25519_tv_template)
4813 .test = alg_test_comp,
4817 .comp = __VECS(deflate_comp_tv_template),
4818 .decomp = __VECS(deflate_decomp_tv_template)
4823 .test = alg_test_kpp,
4825 .kpp = __VECS(dh_tv_template)
4828 .alg = "digest_null",
4829 .test = alg_test_null,
4831 .alg = "drbg_nopr_ctr_aes128",
4832 .test = alg_test_drbg,
4835 .drbg = __VECS(drbg_nopr_ctr_aes128_tv_template)
4838 .alg = "drbg_nopr_ctr_aes192",
4839 .test = alg_test_drbg,
4842 .drbg = __VECS(drbg_nopr_ctr_aes192_tv_template)
4845 .alg = "drbg_nopr_ctr_aes256",
4846 .test = alg_test_drbg,
4849 .drbg = __VECS(drbg_nopr_ctr_aes256_tv_template)
4853 * There is no need to specifically test the DRBG with every
4854 * backend cipher -- covered by drbg_nopr_hmac_sha256 test
4856 .alg = "drbg_nopr_hmac_sha1",
4858 .test = alg_test_null,
4860 .alg = "drbg_nopr_hmac_sha256",
4861 .test = alg_test_drbg,
4864 .drbg = __VECS(drbg_nopr_hmac_sha256_tv_template)
4867 /* covered by drbg_nopr_hmac_sha256 test */
4868 .alg = "drbg_nopr_hmac_sha384",
4869 .test = alg_test_null,
4871 .alg = "drbg_nopr_hmac_sha512",
4872 .test = alg_test_drbg,
4875 .drbg = __VECS(drbg_nopr_hmac_sha512_tv_template)
4878 .alg = "drbg_nopr_sha1",
4880 .test = alg_test_null,
4882 .alg = "drbg_nopr_sha256",
4883 .test = alg_test_drbg,
4886 .drbg = __VECS(drbg_nopr_sha256_tv_template)
4889 /* covered by drbg_nopr_sha256 test */
4890 .alg = "drbg_nopr_sha384",
4891 .test = alg_test_null,
4893 .alg = "drbg_nopr_sha512",
4895 .test = alg_test_null,
4897 .alg = "drbg_pr_ctr_aes128",
4898 .test = alg_test_drbg,
4901 .drbg = __VECS(drbg_pr_ctr_aes128_tv_template)
4904 /* covered by drbg_pr_ctr_aes128 test */
4905 .alg = "drbg_pr_ctr_aes192",
4907 .test = alg_test_null,
4909 .alg = "drbg_pr_ctr_aes256",
4911 .test = alg_test_null,
4913 .alg = "drbg_pr_hmac_sha1",
4915 .test = alg_test_null,
4917 .alg = "drbg_pr_hmac_sha256",
4918 .test = alg_test_drbg,
4921 .drbg = __VECS(drbg_pr_hmac_sha256_tv_template)
4924 /* covered by drbg_pr_hmac_sha256 test */
4925 .alg = "drbg_pr_hmac_sha384",
4926 .test = alg_test_null,
4928 .alg = "drbg_pr_hmac_sha512",
4929 .test = alg_test_null,
4932 .alg = "drbg_pr_sha1",
4934 .test = alg_test_null,
4936 .alg = "drbg_pr_sha256",
4937 .test = alg_test_drbg,
4940 .drbg = __VECS(drbg_pr_sha256_tv_template)
4943 /* covered by drbg_pr_sha256 test */
4944 .alg = "drbg_pr_sha384",
4945 .test = alg_test_null,
4947 .alg = "drbg_pr_sha512",
4949 .test = alg_test_null,
4952 .test = alg_test_skcipher,
4955 .cipher = __VECS(aes_tv_template)
4958 .alg = "ecb(anubis)",
4959 .test = alg_test_skcipher,
4961 .cipher = __VECS(anubis_tv_template)
4965 .generic_driver = "arc4-generic",
4966 .test = alg_test_skcipher,
4968 .cipher = __VECS(arc4_tv_template)
4972 .test = alg_test_skcipher,
4974 .cipher = __VECS(aria_tv_template)
4977 .alg = "ecb(blowfish)",
4978 .test = alg_test_skcipher,
4980 .cipher = __VECS(bf_tv_template)
4983 .alg = "ecb(camellia)",
4984 .test = alg_test_skcipher,
4986 .cipher = __VECS(camellia_tv_template)
4989 .alg = "ecb(cast5)",
4990 .test = alg_test_skcipher,
4992 .cipher = __VECS(cast5_tv_template)
4995 .alg = "ecb(cast6)",
4996 .test = alg_test_skcipher,
4998 .cipher = __VECS(cast6_tv_template)
5001 .alg = "ecb(cipher_null)",
5002 .test = alg_test_null,
5006 .test = alg_test_skcipher,
5008 .cipher = __VECS(des_tv_template)
5011 .alg = "ecb(des3_ede)",
5012 .test = alg_test_skcipher,
5014 .cipher = __VECS(des3_ede_tv_template)
5017 .alg = "ecb(fcrypt)",
5018 .test = alg_test_skcipher,
5021 .vecs = fcrypt_pcbc_tv_template,
5026 .alg = "ecb(khazad)",
5027 .test = alg_test_skcipher,
5029 .cipher = __VECS(khazad_tv_template)
5032 /* Same as ecb(aes) except the key is stored in
5033 * hardware secure memory which we reference by index
5036 .test = alg_test_null,
5040 .test = alg_test_skcipher,
5042 .cipher = __VECS(seed_tv_template)
5045 .alg = "ecb(serpent)",
5046 .test = alg_test_skcipher,
5048 .cipher = __VECS(serpent_tv_template)
5052 .test = alg_test_skcipher,
5054 .cipher = __VECS(sm4_tv_template)
5058 .test = alg_test_skcipher,
5060 .cipher = __VECS(tea_tv_template)
5063 .alg = "ecb(twofish)",
5064 .test = alg_test_skcipher,
5066 .cipher = __VECS(tf_tv_template)
5070 .test = alg_test_skcipher,
5072 .cipher = __VECS(xeta_tv_template)
5076 .test = alg_test_skcipher,
5078 .cipher = __VECS(xtea_tv_template)
5081 #if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
5082 .alg = "ecb-paes-s390",
5084 .test = alg_test_skcipher,
5086 .cipher = __VECS(aes_tv_template)
5090 .alg = "ecdh-nist-p192",
5091 .test = alg_test_kpp,
5093 .kpp = __VECS(ecdh_p192_tv_template)
5096 .alg = "ecdh-nist-p256",
5097 .test = alg_test_kpp,
5100 .kpp = __VECS(ecdh_p256_tv_template)
5103 .alg = "ecdh-nist-p384",
5104 .test = alg_test_kpp,
5107 .kpp = __VECS(ecdh_p384_tv_template)
5110 .alg = "ecdsa-nist-p192",
5111 .test = alg_test_akcipher,
5113 .akcipher = __VECS(ecdsa_nist_p192_tv_template)
5116 .alg = "ecdsa-nist-p256",
5117 .test = alg_test_akcipher,
5120 .akcipher = __VECS(ecdsa_nist_p256_tv_template)
5123 .alg = "ecdsa-nist-p384",
5124 .test = alg_test_akcipher,
5127 .akcipher = __VECS(ecdsa_nist_p384_tv_template)
5131 .test = alg_test_akcipher,
5133 .akcipher = __VECS(ecrdsa_tv_template)
5136 .alg = "essiv(authenc(hmac(sha256),cbc(aes)),sha256)",
5137 .test = alg_test_aead,
5140 .aead = __VECS(essiv_hmac_sha256_aes_cbc_tv_temp)
5143 .alg = "essiv(cbc(aes),sha256)",
5144 .test = alg_test_skcipher,
5147 .cipher = __VECS(essiv_aes_cbc_tv_template)
5150 #if IS_ENABLED(CONFIG_CRYPTO_DH_RFC7919_GROUPS)
5151 .alg = "ffdhe2048(dh)",
5152 .test = alg_test_kpp,
5155 .kpp = __VECS(ffdhe2048_dh_tv_template)
5158 .alg = "ffdhe3072(dh)",
5159 .test = alg_test_kpp,
5162 .kpp = __VECS(ffdhe3072_dh_tv_template)
5165 .alg = "ffdhe4096(dh)",
5166 .test = alg_test_kpp,
5169 .kpp = __VECS(ffdhe4096_dh_tv_template)
5172 .alg = "ffdhe6144(dh)",
5173 .test = alg_test_kpp,
5176 .kpp = __VECS(ffdhe6144_dh_tv_template)
5179 .alg = "ffdhe8192(dh)",
5180 .test = alg_test_kpp,
5183 .kpp = __VECS(ffdhe8192_dh_tv_template)
5186 #endif /* CONFIG_CRYPTO_DH_RFC7919_GROUPS */
5188 .generic_driver = "gcm_base(ctr(aes-generic),ghash-generic)",
5189 .test = alg_test_aead,
5192 .aead = __VECS(aes_gcm_tv_template)
5196 .generic_driver = "gcm_base(ctr(aria-generic),ghash-generic)",
5197 .test = alg_test_aead,
5199 .aead = __VECS(aria_gcm_tv_template)
5203 .generic_driver = "gcm_base(ctr(sm4-generic),ghash-generic)",
5204 .test = alg_test_aead,
5206 .aead = __VECS(sm4_gcm_tv_template)
5210 .test = alg_test_hash,
5212 .hash = __VECS(ghash_tv_template)
5215 .alg = "hctr2(aes)",
5217 "hctr2_base(xctr(aes-generic),polyval-generic)",
5218 .test = alg_test_skcipher,
5220 .cipher = __VECS(aes_hctr2_tv_template)
5224 .test = alg_test_hash,
5226 .hash = __VECS(hmac_md5_tv_template)
5229 .alg = "hmac(rmd160)",
5230 .test = alg_test_hash,
5232 .hash = __VECS(hmac_rmd160_tv_template)
5235 .alg = "hmac(sha1)",
5236 .test = alg_test_hash,
5239 .hash = __VECS(hmac_sha1_tv_template)
5242 .alg = "hmac(sha224)",
5243 .test = alg_test_hash,
5246 .hash = __VECS(hmac_sha224_tv_template)
5249 .alg = "hmac(sha256)",
5250 .test = alg_test_hash,
5253 .hash = __VECS(hmac_sha256_tv_template)
5256 .alg = "hmac(sha3-224)",
5257 .test = alg_test_hash,
5260 .hash = __VECS(hmac_sha3_224_tv_template)
5263 .alg = "hmac(sha3-256)",
5264 .test = alg_test_hash,
5267 .hash = __VECS(hmac_sha3_256_tv_template)
5270 .alg = "hmac(sha3-384)",
5271 .test = alg_test_hash,
5274 .hash = __VECS(hmac_sha3_384_tv_template)
5277 .alg = "hmac(sha3-512)",
5278 .test = alg_test_hash,
5281 .hash = __VECS(hmac_sha3_512_tv_template)
5284 .alg = "hmac(sha384)",
5285 .test = alg_test_hash,
5288 .hash = __VECS(hmac_sha384_tv_template)
5291 .alg = "hmac(sha512)",
5292 .test = alg_test_hash,
5295 .hash = __VECS(hmac_sha512_tv_template)
5299 .test = alg_test_hash,
5301 .hash = __VECS(hmac_sm3_tv_template)
5304 .alg = "hmac(streebog256)",
5305 .test = alg_test_hash,
5307 .hash = __VECS(hmac_streebog256_tv_template)
5310 .alg = "hmac(streebog512)",
5311 .test = alg_test_hash,
5313 .hash = __VECS(hmac_streebog512_tv_template)
5316 .alg = "jitterentropy_rng",
5318 .test = alg_test_null,
5321 .test = alg_test_skcipher,
5324 .cipher = __VECS(aes_kw_tv_template)
5328 .generic_driver = "lrw(ecb(aes-generic))",
5329 .test = alg_test_skcipher,
5331 .cipher = __VECS(aes_lrw_tv_template)
5334 .alg = "lrw(camellia)",
5335 .generic_driver = "lrw(ecb(camellia-generic))",
5336 .test = alg_test_skcipher,
5338 .cipher = __VECS(camellia_lrw_tv_template)
5341 .alg = "lrw(cast6)",
5342 .generic_driver = "lrw(ecb(cast6-generic))",
5343 .test = alg_test_skcipher,
5345 .cipher = __VECS(cast6_lrw_tv_template)
5348 .alg = "lrw(serpent)",
5349 .generic_driver = "lrw(ecb(serpent-generic))",
5350 .test = alg_test_skcipher,
5352 .cipher = __VECS(serpent_lrw_tv_template)
5355 .alg = "lrw(twofish)",
5356 .generic_driver = "lrw(ecb(twofish-generic))",
5357 .test = alg_test_skcipher,
5359 .cipher = __VECS(tf_lrw_tv_template)
5363 .test = alg_test_comp,
5367 .comp = __VECS(lz4_comp_tv_template),
5368 .decomp = __VECS(lz4_decomp_tv_template)
5373 .test = alg_test_comp,
5377 .comp = __VECS(lz4hc_comp_tv_template),
5378 .decomp = __VECS(lz4hc_decomp_tv_template)
5383 .test = alg_test_comp,
5387 .comp = __VECS(lzo_comp_tv_template),
5388 .decomp = __VECS(lzo_decomp_tv_template)
5393 .test = alg_test_comp,
5397 .comp = __VECS(lzorle_comp_tv_template),
5398 .decomp = __VECS(lzorle_decomp_tv_template)
5403 .test = alg_test_hash,
5405 .hash = __VECS(md4_tv_template)
5409 .test = alg_test_hash,
5411 .hash = __VECS(md5_tv_template)
5414 .alg = "michael_mic",
5415 .test = alg_test_hash,
5417 .hash = __VECS(michael_mic_tv_template)
5420 .alg = "nhpoly1305",
5421 .test = alg_test_hash,
5423 .hash = __VECS(nhpoly1305_tv_template)
5427 .test = alg_test_skcipher,
5430 .cipher = __VECS(aes_ofb_tv_template)
5433 /* Same as ofb(aes) except the key is stored in
5434 * hardware secure memory which we reference by index
5437 .test = alg_test_null,
5441 .test = alg_test_skcipher,
5443 .cipher = __VECS(sm4_ofb_tv_template)
5446 .alg = "pcbc(fcrypt)",
5447 .test = alg_test_skcipher,
5449 .cipher = __VECS(fcrypt_pcbc_tv_template)
5452 .alg = "pkcs1pad(rsa,sha224)",
5453 .test = alg_test_null,
5456 .alg = "pkcs1pad(rsa,sha256)",
5457 .test = alg_test_akcipher,
5460 .akcipher = __VECS(pkcs1pad_rsa_tv_template)
5463 .alg = "pkcs1pad(rsa,sha384)",
5464 .test = alg_test_null,
5467 .alg = "pkcs1pad(rsa,sha512)",
5468 .test = alg_test_null,
5472 .test = alg_test_hash,
5474 .hash = __VECS(poly1305_tv_template)
5478 .test = alg_test_hash,
5480 .hash = __VECS(polyval_tv_template)
5483 .alg = "rfc3686(ctr(aes))",
5484 .test = alg_test_skcipher,
5487 .cipher = __VECS(aes_ctr_rfc3686_tv_template)
5490 .alg = "rfc3686(ctr(sm4))",
5491 .test = alg_test_skcipher,
5493 .cipher = __VECS(sm4_ctr_rfc3686_tv_template)
5496 .alg = "rfc4106(gcm(aes))",
5497 .generic_driver = "rfc4106(gcm_base(ctr(aes-generic),ghash-generic))",
5498 .test = alg_test_aead,
5502 ____VECS(aes_gcm_rfc4106_tv_template),
5503 .einval_allowed = 1,
5508 .alg = "rfc4309(ccm(aes))",
5509 .generic_driver = "rfc4309(ccm_base(ctr(aes-generic),cbcmac(aes-generic)))",
5510 .test = alg_test_aead,
5514 ____VECS(aes_ccm_rfc4309_tv_template),
5515 .einval_allowed = 1,
5520 .alg = "rfc4543(gcm(aes))",
5521 .generic_driver = "rfc4543(gcm_base(ctr(aes-generic),ghash-generic))",
5522 .test = alg_test_aead,
5525 ____VECS(aes_gcm_rfc4543_tv_template),
5526 .einval_allowed = 1,
5531 .alg = "rfc7539(chacha20,poly1305)",
5532 .test = alg_test_aead,
5534 .aead = __VECS(rfc7539_tv_template)
5537 .alg = "rfc7539esp(chacha20,poly1305)",
5538 .test = alg_test_aead,
5541 ____VECS(rfc7539esp_tv_template),
5542 .einval_allowed = 1,
5548 .test = alg_test_hash,
5550 .hash = __VECS(rmd160_tv_template)
5554 .test = alg_test_akcipher,
5557 .akcipher = __VECS(rsa_tv_template)
5561 .test = alg_test_hash,
5564 .hash = __VECS(sha1_tv_template)
5568 .test = alg_test_hash,
5571 .hash = __VECS(sha224_tv_template)
5575 .test = alg_test_hash,
5578 .hash = __VECS(sha256_tv_template)
5582 .test = alg_test_hash,
5585 .hash = __VECS(sha3_224_tv_template)
5589 .test = alg_test_hash,
5592 .hash = __VECS(sha3_256_tv_template)
5596 .test = alg_test_hash,
5599 .hash = __VECS(sha3_384_tv_template)
5603 .test = alg_test_hash,
5606 .hash = __VECS(sha3_512_tv_template)
5610 .test = alg_test_hash,
5613 .hash = __VECS(sha384_tv_template)
5617 .test = alg_test_hash,
5620 .hash = __VECS(sha512_tv_template)
5624 .test = alg_test_akcipher,
5626 .akcipher = __VECS(sm2_tv_template)
5630 .test = alg_test_hash,
5632 .hash = __VECS(sm3_tv_template)
5635 .alg = "streebog256",
5636 .test = alg_test_hash,
5638 .hash = __VECS(streebog256_tv_template)
5641 .alg = "streebog512",
5642 .test = alg_test_hash,
5644 .hash = __VECS(streebog512_tv_template)
5647 .alg = "vmac64(aes)",
5648 .test = alg_test_hash,
5650 .hash = __VECS(vmac64_aes_tv_template)
5654 .test = alg_test_hash,
5656 .hash = __VECS(wp256_tv_template)
5660 .test = alg_test_hash,
5662 .hash = __VECS(wp384_tv_template)
5666 .test = alg_test_hash,
5668 .hash = __VECS(wp512_tv_template)
5672 .test = alg_test_hash,
5674 .hash = __VECS(aes_xcbc128_tv_template)
5678 .test = alg_test_hash,
5680 .hash = __VECS(sm4_xcbc128_tv_template)
5684 .test = alg_test_skcipher,
5686 .cipher = __VECS(xchacha12_tv_template)
5690 .test = alg_test_skcipher,
5692 .cipher = __VECS(xchacha20_tv_template)
5696 .test = alg_test_skcipher,
5698 .cipher = __VECS(aes_xctr_tv_template)
5702 .generic_driver = "xts(ecb(aes-generic))",
5703 .test = alg_test_skcipher,
5706 .cipher = __VECS(aes_xts_tv_template)
5709 .alg = "xts(camellia)",
5710 .generic_driver = "xts(ecb(camellia-generic))",
5711 .test = alg_test_skcipher,
5713 .cipher = __VECS(camellia_xts_tv_template)
5716 .alg = "xts(cast6)",
5717 .generic_driver = "xts(ecb(cast6-generic))",
5718 .test = alg_test_skcipher,
5720 .cipher = __VECS(cast6_xts_tv_template)
5723 /* Same as xts(aes) except the key is stored in
5724 * hardware secure memory which we reference by index
5727 .test = alg_test_null,
5730 .alg = "xts(serpent)",
5731 .generic_driver = "xts(ecb(serpent-generic))",
5732 .test = alg_test_skcipher,
5734 .cipher = __VECS(serpent_xts_tv_template)
5738 .generic_driver = "xts(ecb(sm4-generic))",
5739 .test = alg_test_skcipher,
5741 .cipher = __VECS(sm4_xts_tv_template)
5744 .alg = "xts(twofish)",
5745 .generic_driver = "xts(ecb(twofish-generic))",
5746 .test = alg_test_skcipher,
5748 .cipher = __VECS(tf_xts_tv_template)
5751 #if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
5752 .alg = "xts-paes-s390",
5754 .test = alg_test_skcipher,
5756 .cipher = __VECS(aes_xts_tv_template)
5760 .alg = "xts4096(paes)",
5761 .test = alg_test_null,
5764 .alg = "xts512(paes)",
5765 .test = alg_test_null,
5769 .test = alg_test_hash,
5772 .hash = __VECS(xxhash64_tv_template)
5776 .test = alg_test_comp,
5780 .comp = __VECS(zstd_comp_tv_template),
5781 .decomp = __VECS(zstd_decomp_tv_template)
5787 static void alg_check_test_descs_order(void)
5791 for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
5792 int diff = strcmp(alg_test_descs[i - 1].alg,
5793 alg_test_descs[i].alg);
5795 if (WARN_ON(diff > 0)) {
5796 pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
5797 alg_test_descs[i - 1].alg,
5798 alg_test_descs[i].alg);
5801 if (WARN_ON(diff == 0)) {
5802 pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
5803 alg_test_descs[i].alg);
5808 static void alg_check_testvec_configs(void)
5812 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++)
5813 WARN_ON(!valid_testvec_config(
5814 &default_cipher_testvec_configs[i]));
5816 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++)
5817 WARN_ON(!valid_testvec_config(
5818 &default_hash_testvec_configs[i]));
5821 static void testmgr_onetime_init(void)
5823 alg_check_test_descs_order();
5824 alg_check_testvec_configs();
5826 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
5827 pr_warn("alg: extra crypto tests enabled. This is intended for developer use only.\n");
5831 static int alg_find_test(const char *alg)
5834 int end = ARRAY_SIZE(alg_test_descs);
5836 while (start < end) {
5837 int i = (start + end) / 2;
5838 int diff = strcmp(alg_test_descs[i].alg, alg);
5856 static int alg_fips_disabled(const char *driver, const char *alg)
5858 pr_info("alg: %s (%s) is disabled due to FIPS\n", alg, driver);
5863 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
5869 if (!fips_enabled && notests) {
5870 printk_once(KERN_INFO "alg: self-tests disabled\n");
5874 DO_ONCE(testmgr_onetime_init);
5876 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
5877 char nalg[CRYPTO_MAX_ALG_NAME];
5879 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
5881 return -ENAMETOOLONG;
5883 i = alg_find_test(nalg);
5887 if (fips_enabled && !alg_test_descs[i].fips_allowed)
5890 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
5894 i = alg_find_test(alg);
5895 j = alg_find_test(driver);
5900 if (j >= 0 && !alg_test_descs[j].fips_allowed)
5903 if (i >= 0 && !alg_test_descs[i].fips_allowed)
5909 rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
5911 if (j >= 0 && j != i)
5912 rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
5917 if (fips_enabled || panic_on_fail) {
5919 panic("alg: self-tests for %s (%s) failed in %s mode!\n",
5921 fips_enabled ? "fips" : "panic_on_fail");
5923 pr_warn("alg: self-tests for %s using %s failed (rc=%d)",
5926 "alg: self-tests for %s using %s failed (rc=%d)",
5930 pr_info("alg: self-tests for %s (%s) passed\n",
5937 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_LSKCIPHER) {
5938 char nalg[CRYPTO_MAX_ALG_NAME];
5940 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
5944 i = alg_find_test(nalg);
5948 if (fips_enabled && !alg_test_descs[i].fips_allowed)
5951 rc = alg_test_skcipher(alg_test_descs + i, driver, type, mask);
5956 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
5958 if (type & CRYPTO_ALG_FIPS_INTERNAL)
5959 return alg_fips_disabled(driver, alg);
5963 return alg_fips_disabled(driver, alg);
5966 #endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
5968 EXPORT_SYMBOL_GPL(alg_test);