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
863 /* Generate a random length in range [0, max_len], but prefer smaller values */
864 static unsigned int generate_random_length(unsigned int max_len)
866 unsigned int len = get_random_u32_below(max_len + 1);
868 switch (get_random_u32_below(4)) {
880 /* Flip a random bit in the given nonempty data buffer */
881 static void flip_random_bit(u8 *buf, size_t size)
885 bitpos = get_random_u32_below(size * 8);
886 buf[bitpos / 8] ^= 1 << (bitpos % 8);
889 /* Flip a random byte in the given nonempty data buffer */
890 static void flip_random_byte(u8 *buf, size_t size)
892 buf[get_random_u32_below(size)] ^= 0xff;
895 /* Sometimes make some random changes to the given nonempty data buffer */
896 static void mutate_buffer(u8 *buf, size_t size)
901 /* Sometimes flip some bits */
902 if (get_random_u32_below(4) == 0) {
903 num_flips = min_t(size_t, 1 << get_random_u32_below(8), size * 8);
904 for (i = 0; i < num_flips; i++)
905 flip_random_bit(buf, size);
908 /* Sometimes flip some bytes */
909 if (get_random_u32_below(4) == 0) {
910 num_flips = min_t(size_t, 1 << get_random_u32_below(8), size);
911 for (i = 0; i < num_flips; i++)
912 flip_random_byte(buf, size);
916 /* Randomly generate 'count' bytes, but sometimes make them "interesting" */
917 static void generate_random_bytes(u8 *buf, size_t count)
926 switch (get_random_u32_below(8)) { /* Choose a generation strategy */
929 /* All the same byte, plus optional mutations */
930 switch (get_random_u32_below(4)) {
941 memset(buf, b, count);
942 mutate_buffer(buf, count);
945 /* Ascending or descending bytes, plus optional mutations */
946 increment = get_random_u8();
948 for (i = 0; i < count; i++, b += increment)
950 mutate_buffer(buf, count);
953 /* Fully random bytes */
954 for (i = 0; i < count; i++)
955 buf[i] = get_random_u8();
959 static char *generate_random_sgl_divisions(struct test_sg_division *divs,
960 size_t max_divs, char *p, char *end,
961 bool gen_flushes, u32 req_flags)
963 struct test_sg_division *div = divs;
964 unsigned int remaining = TEST_SG_TOTAL;
967 unsigned int this_len;
968 const char *flushtype_str;
970 if (div == &divs[max_divs - 1] || get_random_u32_below(2) == 0)
971 this_len = remaining;
973 this_len = get_random_u32_inclusive(1, remaining);
974 div->proportion_of_total = this_len;
976 if (get_random_u32_below(4) == 0)
977 div->offset = get_random_u32_inclusive(PAGE_SIZE - 128, PAGE_SIZE - 1);
978 else if (get_random_u32_below(2) == 0)
979 div->offset = get_random_u32_below(32);
981 div->offset = get_random_u32_below(PAGE_SIZE);
982 if (get_random_u32_below(8) == 0)
983 div->offset_relative_to_alignmask = true;
985 div->flush_type = FLUSH_TYPE_NONE;
987 switch (get_random_u32_below(4)) {
989 div->flush_type = FLUSH_TYPE_REIMPORT;
992 div->flush_type = FLUSH_TYPE_FLUSH;
997 if (div->flush_type != FLUSH_TYPE_NONE &&
998 !(req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) &&
999 get_random_u32_below(2) == 0)
1002 switch (div->flush_type) {
1003 case FLUSH_TYPE_FLUSH:
1005 flushtype_str = "<flush,nosimd>";
1007 flushtype_str = "<flush>";
1009 case FLUSH_TYPE_REIMPORT:
1011 flushtype_str = "<reimport,nosimd>";
1013 flushtype_str = "<reimport>";
1020 BUILD_BUG_ON(TEST_SG_TOTAL != 10000); /* for "%u.%u%%" */
1021 p += scnprintf(p, end - p, "%s%u.%u%%@%s+%u%s", flushtype_str,
1022 this_len / 100, this_len % 100,
1023 div->offset_relative_to_alignmask ?
1025 div->offset, this_len == remaining ? "" : ", ");
1026 remaining -= this_len;
1028 } while (remaining);
1033 /* Generate a random testvec_config for fuzz testing */
1034 static void generate_random_testvec_config(struct testvec_config *cfg,
1035 char *name, size_t max_namelen)
1038 char * const end = name + max_namelen;
1040 memset(cfg, 0, sizeof(*cfg));
1044 p += scnprintf(p, end - p, "random:");
1046 switch (get_random_u32_below(4)) {
1049 cfg->inplace_mode = OUT_OF_PLACE;
1052 cfg->inplace_mode = INPLACE_ONE_SGLIST;
1053 p += scnprintf(p, end - p, " inplace_one_sglist");
1056 cfg->inplace_mode = INPLACE_TWO_SGLISTS;
1057 p += scnprintf(p, end - p, " inplace_two_sglists");
1061 if (get_random_u32_below(2) == 0) {
1062 cfg->req_flags |= CRYPTO_TFM_REQ_MAY_SLEEP;
1063 p += scnprintf(p, end - p, " may_sleep");
1066 switch (get_random_u32_below(4)) {
1068 cfg->finalization_type = FINALIZATION_TYPE_FINAL;
1069 p += scnprintf(p, end - p, " use_final");
1072 cfg->finalization_type = FINALIZATION_TYPE_FINUP;
1073 p += scnprintf(p, end - p, " use_finup");
1076 cfg->finalization_type = FINALIZATION_TYPE_DIGEST;
1077 p += scnprintf(p, end - p, " use_digest");
1081 if (!(cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) &&
1082 get_random_u32_below(2) == 0) {
1084 p += scnprintf(p, end - p, " nosimd");
1087 p += scnprintf(p, end - p, " src_divs=[");
1088 p = generate_random_sgl_divisions(cfg->src_divs,
1089 ARRAY_SIZE(cfg->src_divs), p, end,
1090 (cfg->finalization_type !=
1091 FINALIZATION_TYPE_DIGEST),
1093 p += scnprintf(p, end - p, "]");
1095 if (cfg->inplace_mode == OUT_OF_PLACE && get_random_u32_below(2) == 0) {
1096 p += scnprintf(p, end - p, " dst_divs=[");
1097 p = generate_random_sgl_divisions(cfg->dst_divs,
1098 ARRAY_SIZE(cfg->dst_divs),
1101 p += scnprintf(p, end - p, "]");
1104 if (get_random_u32_below(2) == 0) {
1105 cfg->iv_offset = get_random_u32_inclusive(1, MAX_ALGAPI_ALIGNMASK);
1106 p += scnprintf(p, end - p, " iv_offset=%u", cfg->iv_offset);
1109 if (get_random_u32_below(2) == 0) {
1110 cfg->key_offset = get_random_u32_inclusive(1, MAX_ALGAPI_ALIGNMASK);
1111 p += scnprintf(p, end - p, " key_offset=%u", cfg->key_offset);
1114 WARN_ON_ONCE(!valid_testvec_config(cfg));
1117 static void crypto_disable_simd_for_test(void)
1120 __this_cpu_write(crypto_simd_disabled_for_test, true);
1123 static void crypto_reenable_simd_for_test(void)
1125 __this_cpu_write(crypto_simd_disabled_for_test, false);
1130 * Given an algorithm name, build the name of the generic implementation of that
1131 * algorithm, assuming the usual naming convention. Specifically, this appends
1132 * "-generic" to every part of the name that is not a template name. Examples:
1134 * aes => aes-generic
1135 * cbc(aes) => cbc(aes-generic)
1136 * cts(cbc(aes)) => cts(cbc(aes-generic))
1137 * rfc7539(chacha20,poly1305) => rfc7539(chacha20-generic,poly1305-generic)
1139 * Return: 0 on success, or -ENAMETOOLONG if the generic name would be too long
1141 static int build_generic_driver_name(const char *algname,
1142 char driver_name[CRYPTO_MAX_ALG_NAME])
1144 const char *in = algname;
1145 char *out = driver_name;
1146 size_t len = strlen(algname);
1148 if (len >= CRYPTO_MAX_ALG_NAME)
1151 const char *in_saved = in;
1153 while (*in && *in != '(' && *in != ')' && *in != ',')
1155 if (*in != '(' && in > in_saved) {
1157 if (len >= CRYPTO_MAX_ALG_NAME)
1159 memcpy(out, "-generic", 8);
1162 } while ((*out++ = *in++) != '\0');
1166 pr_err("alg: generic driver name for \"%s\" would be too long\n",
1168 return -ENAMETOOLONG;
1170 #else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1171 static void crypto_disable_simd_for_test(void)
1175 static void crypto_reenable_simd_for_test(void)
1178 #endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1180 static int build_hash_sglist(struct test_sglist *tsgl,
1181 const struct hash_testvec *vec,
1182 const struct testvec_config *cfg,
1183 unsigned int alignmask,
1184 const struct test_sg_division *divs[XBUFSIZE])
1187 struct iov_iter input;
1189 kv.iov_base = (void *)vec->plaintext;
1190 kv.iov_len = vec->psize;
1191 iov_iter_kvec(&input, ITER_SOURCE, &kv, 1, vec->psize);
1192 return build_test_sglist(tsgl, cfg->src_divs, alignmask, vec->psize,
1196 static int check_hash_result(const char *type,
1197 const u8 *result, unsigned int digestsize,
1198 const struct hash_testvec *vec,
1199 const char *vec_name,
1201 const struct testvec_config *cfg)
1203 if (memcmp(result, vec->digest, digestsize) != 0) {
1204 pr_err("alg: %s: %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
1205 type, driver, vec_name, cfg->name);
1208 if (!testmgr_is_poison(&result[digestsize], TESTMGR_POISON_LEN)) {
1209 pr_err("alg: %s: %s overran result buffer on test vector %s, cfg=\"%s\"\n",
1210 type, driver, vec_name, cfg->name);
1216 static inline int check_shash_op(const char *op, int err,
1217 const char *driver, const char *vec_name,
1218 const struct testvec_config *cfg)
1221 pr_err("alg: shash: %s %s() failed with err %d on test vector %s, cfg=\"%s\"\n",
1222 driver, op, err, vec_name, cfg->name);
1226 /* Test one hash test vector in one configuration, using the shash API */
1227 static int test_shash_vec_cfg(const struct hash_testvec *vec,
1228 const char *vec_name,
1229 const struct testvec_config *cfg,
1230 struct shash_desc *desc,
1231 struct test_sglist *tsgl,
1234 struct crypto_shash *tfm = desc->tfm;
1235 const unsigned int alignmask = crypto_shash_alignmask(tfm);
1236 const unsigned int digestsize = crypto_shash_digestsize(tfm);
1237 const unsigned int statesize = crypto_shash_statesize(tfm);
1238 const char *driver = crypto_shash_driver_name(tfm);
1239 const struct test_sg_division *divs[XBUFSIZE];
1241 u8 result[HASH_MAX_DIGESTSIZE + TESTMGR_POISON_LEN];
1244 /* Set the key, if specified */
1246 err = do_setkey(crypto_shash_setkey, tfm, vec->key, vec->ksize,
1249 if (err == vec->setkey_error)
1251 pr_err("alg: shash: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1252 driver, vec_name, vec->setkey_error, err,
1253 crypto_shash_get_flags(tfm));
1256 if (vec->setkey_error) {
1257 pr_err("alg: shash: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1258 driver, vec_name, vec->setkey_error);
1263 /* Build the scatterlist for the source data */
1264 err = build_hash_sglist(tsgl, vec, cfg, alignmask, divs);
1266 pr_err("alg: shash: %s: error preparing scatterlist for test vector %s, cfg=\"%s\"\n",
1267 driver, vec_name, cfg->name);
1271 /* Do the actual hashing */
1273 testmgr_poison(desc->__ctx, crypto_shash_descsize(tfm));
1274 testmgr_poison(result, digestsize + TESTMGR_POISON_LEN);
1276 if (cfg->finalization_type == FINALIZATION_TYPE_DIGEST ||
1277 vec->digest_error) {
1278 /* Just using digest() */
1279 if (tsgl->nents != 1)
1282 crypto_disable_simd_for_test();
1283 err = crypto_shash_digest(desc, sg_virt(&tsgl->sgl[0]),
1284 tsgl->sgl[0].length, result);
1286 crypto_reenable_simd_for_test();
1288 if (err == vec->digest_error)
1290 pr_err("alg: shash: %s digest() failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
1291 driver, vec_name, vec->digest_error, err,
1295 if (vec->digest_error) {
1296 pr_err("alg: shash: %s digest() unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
1297 driver, vec_name, vec->digest_error, cfg->name);
1303 /* Using init(), zero or more update(), then final() or finup() */
1306 crypto_disable_simd_for_test();
1307 err = crypto_shash_init(desc);
1309 crypto_reenable_simd_for_test();
1310 err = check_shash_op("init", err, driver, vec_name, cfg);
1314 for (i = 0; i < tsgl->nents; i++) {
1315 if (i + 1 == tsgl->nents &&
1316 cfg->finalization_type == FINALIZATION_TYPE_FINUP) {
1317 if (divs[i]->nosimd)
1318 crypto_disable_simd_for_test();
1319 err = crypto_shash_finup(desc, sg_virt(&tsgl->sgl[i]),
1320 tsgl->sgl[i].length, result);
1321 if (divs[i]->nosimd)
1322 crypto_reenable_simd_for_test();
1323 err = check_shash_op("finup", err, driver, vec_name,
1329 if (divs[i]->nosimd)
1330 crypto_disable_simd_for_test();
1331 err = crypto_shash_update(desc, sg_virt(&tsgl->sgl[i]),
1332 tsgl->sgl[i].length);
1333 if (divs[i]->nosimd)
1334 crypto_reenable_simd_for_test();
1335 err = check_shash_op("update", err, driver, vec_name, cfg);
1338 if (divs[i]->flush_type == FLUSH_TYPE_REIMPORT) {
1339 /* Test ->export() and ->import() */
1340 testmgr_poison(hashstate + statesize,
1341 TESTMGR_POISON_LEN);
1342 err = crypto_shash_export(desc, hashstate);
1343 err = check_shash_op("export", err, driver, vec_name,
1347 if (!testmgr_is_poison(hashstate + statesize,
1348 TESTMGR_POISON_LEN)) {
1349 pr_err("alg: shash: %s export() overran state buffer on test vector %s, cfg=\"%s\"\n",
1350 driver, vec_name, cfg->name);
1353 testmgr_poison(desc->__ctx, crypto_shash_descsize(tfm));
1354 err = crypto_shash_import(desc, hashstate);
1355 err = check_shash_op("import", err, driver, vec_name,
1363 crypto_disable_simd_for_test();
1364 err = crypto_shash_final(desc, result);
1366 crypto_reenable_simd_for_test();
1367 err = check_shash_op("final", err, driver, vec_name, cfg);
1371 return check_hash_result("shash", result, digestsize, vec, vec_name,
1375 static int do_ahash_op(int (*op)(struct ahash_request *req),
1376 struct ahash_request *req,
1377 struct crypto_wait *wait, bool nosimd)
1382 crypto_disable_simd_for_test();
1387 crypto_reenable_simd_for_test();
1389 return crypto_wait_req(err, wait);
1392 static int check_nonfinal_ahash_op(const char *op, int err,
1393 u8 *result, unsigned int digestsize,
1394 const char *driver, const char *vec_name,
1395 const struct testvec_config *cfg)
1398 pr_err("alg: ahash: %s %s() failed with err %d on test vector %s, cfg=\"%s\"\n",
1399 driver, op, err, vec_name, cfg->name);
1402 if (!testmgr_is_poison(result, digestsize)) {
1403 pr_err("alg: ahash: %s %s() used result buffer on test vector %s, cfg=\"%s\"\n",
1404 driver, op, vec_name, cfg->name);
1410 /* Test one hash test vector in one configuration, using the ahash API */
1411 static int test_ahash_vec_cfg(const struct hash_testvec *vec,
1412 const char *vec_name,
1413 const struct testvec_config *cfg,
1414 struct ahash_request *req,
1415 struct test_sglist *tsgl,
1418 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1419 const unsigned int alignmask = crypto_ahash_alignmask(tfm);
1420 const unsigned int digestsize = crypto_ahash_digestsize(tfm);
1421 const unsigned int statesize = crypto_ahash_statesize(tfm);
1422 const char *driver = crypto_ahash_driver_name(tfm);
1423 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1424 const struct test_sg_division *divs[XBUFSIZE];
1425 DECLARE_CRYPTO_WAIT(wait);
1427 struct scatterlist *pending_sgl;
1428 unsigned int pending_len;
1429 u8 result[HASH_MAX_DIGESTSIZE + TESTMGR_POISON_LEN];
1432 /* Set the key, if specified */
1434 err = do_setkey(crypto_ahash_setkey, tfm, vec->key, vec->ksize,
1437 if (err == vec->setkey_error)
1439 pr_err("alg: ahash: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1440 driver, vec_name, vec->setkey_error, err,
1441 crypto_ahash_get_flags(tfm));
1444 if (vec->setkey_error) {
1445 pr_err("alg: ahash: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1446 driver, vec_name, vec->setkey_error);
1451 /* Build the scatterlist for the source data */
1452 err = build_hash_sglist(tsgl, vec, cfg, alignmask, divs);
1454 pr_err("alg: ahash: %s: error preparing scatterlist for test vector %s, cfg=\"%s\"\n",
1455 driver, vec_name, cfg->name);
1459 /* Do the actual hashing */
1461 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
1462 testmgr_poison(result, digestsize + TESTMGR_POISON_LEN);
1464 if (cfg->finalization_type == FINALIZATION_TYPE_DIGEST ||
1465 vec->digest_error) {
1466 /* Just using digest() */
1467 ahash_request_set_callback(req, req_flags, crypto_req_done,
1469 ahash_request_set_crypt(req, tsgl->sgl, result, vec->psize);
1470 err = do_ahash_op(crypto_ahash_digest, req, &wait, cfg->nosimd);
1472 if (err == vec->digest_error)
1474 pr_err("alg: ahash: %s digest() failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
1475 driver, vec_name, vec->digest_error, err,
1479 if (vec->digest_error) {
1480 pr_err("alg: ahash: %s digest() unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
1481 driver, vec_name, vec->digest_error, cfg->name);
1487 /* Using init(), zero or more update(), then final() or finup() */
1489 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
1490 ahash_request_set_crypt(req, NULL, result, 0);
1491 err = do_ahash_op(crypto_ahash_init, req, &wait, cfg->nosimd);
1492 err = check_nonfinal_ahash_op("init", err, result, digestsize,
1493 driver, vec_name, cfg);
1499 for (i = 0; i < tsgl->nents; i++) {
1500 if (divs[i]->flush_type != FLUSH_TYPE_NONE &&
1501 pending_sgl != NULL) {
1502 /* update() with the pending data */
1503 ahash_request_set_callback(req, req_flags,
1504 crypto_req_done, &wait);
1505 ahash_request_set_crypt(req, pending_sgl, result,
1507 err = do_ahash_op(crypto_ahash_update, req, &wait,
1509 err = check_nonfinal_ahash_op("update", err,
1511 driver, vec_name, cfg);
1517 if (divs[i]->flush_type == FLUSH_TYPE_REIMPORT) {
1518 /* Test ->export() and ->import() */
1519 testmgr_poison(hashstate + statesize,
1520 TESTMGR_POISON_LEN);
1521 err = crypto_ahash_export(req, hashstate);
1522 err = check_nonfinal_ahash_op("export", err,
1524 driver, vec_name, cfg);
1527 if (!testmgr_is_poison(hashstate + statesize,
1528 TESTMGR_POISON_LEN)) {
1529 pr_err("alg: ahash: %s export() overran state buffer on test vector %s, cfg=\"%s\"\n",
1530 driver, vec_name, cfg->name);
1534 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
1535 err = crypto_ahash_import(req, hashstate);
1536 err = check_nonfinal_ahash_op("import", err,
1538 driver, vec_name, cfg);
1542 if (pending_sgl == NULL)
1543 pending_sgl = &tsgl->sgl[i];
1544 pending_len += tsgl->sgl[i].length;
1547 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
1548 ahash_request_set_crypt(req, pending_sgl, result, pending_len);
1549 if (cfg->finalization_type == FINALIZATION_TYPE_FINAL) {
1550 /* finish with update() and final() */
1551 err = do_ahash_op(crypto_ahash_update, req, &wait, cfg->nosimd);
1552 err = check_nonfinal_ahash_op("update", err, result, digestsize,
1553 driver, vec_name, cfg);
1556 err = do_ahash_op(crypto_ahash_final, req, &wait, cfg->nosimd);
1558 pr_err("alg: ahash: %s final() failed with err %d on test vector %s, cfg=\"%s\"\n",
1559 driver, err, vec_name, cfg->name);
1563 /* finish with finup() */
1564 err = do_ahash_op(crypto_ahash_finup, req, &wait, cfg->nosimd);
1566 pr_err("alg: ahash: %s finup() failed with err %d on test vector %s, cfg=\"%s\"\n",
1567 driver, err, vec_name, cfg->name);
1573 return check_hash_result("ahash", result, digestsize, vec, vec_name,
1577 static int test_hash_vec_cfg(const struct hash_testvec *vec,
1578 const char *vec_name,
1579 const struct testvec_config *cfg,
1580 struct ahash_request *req,
1581 struct shash_desc *desc,
1582 struct test_sglist *tsgl,
1588 * For algorithms implemented as "shash", most bugs will be detected by
1589 * both the shash and ahash tests. Test the shash API first so that the
1590 * failures involve less indirection, so are easier to debug.
1594 err = test_shash_vec_cfg(vec, vec_name, cfg, desc, tsgl,
1600 return test_ahash_vec_cfg(vec, vec_name, cfg, req, tsgl, hashstate);
1603 static int test_hash_vec(const struct hash_testvec *vec, unsigned int vec_num,
1604 struct ahash_request *req, struct shash_desc *desc,
1605 struct test_sglist *tsgl, u8 *hashstate)
1611 sprintf(vec_name, "%u", vec_num);
1613 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++) {
1614 err = test_hash_vec_cfg(vec, vec_name,
1615 &default_hash_testvec_configs[i],
1616 req, desc, tsgl, hashstate);
1621 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1622 if (!noextratests) {
1623 struct testvec_config cfg;
1624 char cfgname[TESTVEC_CONFIG_NAMELEN];
1626 for (i = 0; i < fuzz_iterations; i++) {
1627 generate_random_testvec_config(&cfg, cfgname,
1629 err = test_hash_vec_cfg(vec, vec_name, &cfg,
1630 req, desc, tsgl, hashstate);
1640 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1642 * Generate a hash test vector from the given implementation.
1643 * Assumes the buffers in 'vec' were already allocated.
1645 static void generate_random_hash_testvec(struct shash_desc *desc,
1646 struct hash_testvec *vec,
1647 unsigned int maxkeysize,
1648 unsigned int maxdatasize,
1649 char *name, size_t max_namelen)
1652 vec->psize = generate_random_length(maxdatasize);
1653 generate_random_bytes((u8 *)vec->plaintext, vec->psize);
1656 * Key: length in range [1, maxkeysize], but usually choose maxkeysize.
1657 * If algorithm is unkeyed, then maxkeysize == 0 and set ksize = 0.
1659 vec->setkey_error = 0;
1662 vec->ksize = maxkeysize;
1663 if (get_random_u32_below(4) == 0)
1664 vec->ksize = get_random_u32_inclusive(1, maxkeysize);
1665 generate_random_bytes((u8 *)vec->key, vec->ksize);
1667 vec->setkey_error = crypto_shash_setkey(desc->tfm, vec->key,
1669 /* If the key couldn't be set, no need to continue to digest. */
1670 if (vec->setkey_error)
1675 vec->digest_error = crypto_shash_digest(desc, vec->plaintext,
1676 vec->psize, (u8 *)vec->digest);
1678 snprintf(name, max_namelen, "\"random: psize=%u ksize=%u\"",
1679 vec->psize, vec->ksize);
1683 * Test the hash algorithm represented by @req against the corresponding generic
1684 * implementation, if one is available.
1686 static int test_hash_vs_generic_impl(const char *generic_driver,
1687 unsigned int maxkeysize,
1688 struct ahash_request *req,
1689 struct shash_desc *desc,
1690 struct test_sglist *tsgl,
1693 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1694 const unsigned int digestsize = crypto_ahash_digestsize(tfm);
1695 const unsigned int blocksize = crypto_ahash_blocksize(tfm);
1696 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
1697 const char *algname = crypto_hash_alg_common(tfm)->base.cra_name;
1698 const char *driver = crypto_ahash_driver_name(tfm);
1699 char _generic_driver[CRYPTO_MAX_ALG_NAME];
1700 struct crypto_shash *generic_tfm = NULL;
1701 struct shash_desc *generic_desc = NULL;
1703 struct hash_testvec vec = { 0 };
1705 struct testvec_config *cfg;
1706 char cfgname[TESTVEC_CONFIG_NAMELEN];
1712 if (!generic_driver) { /* Use default naming convention? */
1713 err = build_generic_driver_name(algname, _generic_driver);
1716 generic_driver = _generic_driver;
1719 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
1722 generic_tfm = crypto_alloc_shash(generic_driver, 0, 0);
1723 if (IS_ERR(generic_tfm)) {
1724 err = PTR_ERR(generic_tfm);
1725 if (err == -ENOENT) {
1726 pr_warn("alg: hash: skipping comparison tests for %s because %s is unavailable\n",
1727 driver, generic_driver);
1730 pr_err("alg: hash: error allocating %s (generic impl of %s): %d\n",
1731 generic_driver, algname, err);
1735 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1741 generic_desc = kzalloc(sizeof(*desc) +
1742 crypto_shash_descsize(generic_tfm), GFP_KERNEL);
1743 if (!generic_desc) {
1747 generic_desc->tfm = generic_tfm;
1749 /* Check the algorithm properties for consistency. */
1751 if (digestsize != crypto_shash_digestsize(generic_tfm)) {
1752 pr_err("alg: hash: digestsize for %s (%u) doesn't match generic impl (%u)\n",
1754 crypto_shash_digestsize(generic_tfm));
1759 if (blocksize != crypto_shash_blocksize(generic_tfm)) {
1760 pr_err("alg: hash: blocksize for %s (%u) doesn't match generic impl (%u)\n",
1761 driver, blocksize, crypto_shash_blocksize(generic_tfm));
1767 * Now generate test vectors using the generic implementation, and test
1768 * the other implementation against them.
1771 vec.key = kmalloc(maxkeysize, GFP_KERNEL);
1772 vec.plaintext = kmalloc(maxdatasize, GFP_KERNEL);
1773 vec.digest = kmalloc(digestsize, GFP_KERNEL);
1774 if (!vec.key || !vec.plaintext || !vec.digest) {
1779 for (i = 0; i < fuzz_iterations * 8; i++) {
1780 generate_random_hash_testvec(generic_desc, &vec,
1781 maxkeysize, maxdatasize,
1782 vec_name, sizeof(vec_name));
1783 generate_random_testvec_config(cfg, cfgname, sizeof(cfgname));
1785 err = test_hash_vec_cfg(&vec, vec_name, cfg,
1786 req, desc, tsgl, hashstate);
1795 kfree(vec.plaintext);
1797 crypto_free_shash(generic_tfm);
1798 kfree_sensitive(generic_desc);
1801 #else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1802 static int test_hash_vs_generic_impl(const char *generic_driver,
1803 unsigned int maxkeysize,
1804 struct ahash_request *req,
1805 struct shash_desc *desc,
1806 struct test_sglist *tsgl,
1811 #endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1813 static int alloc_shash(const char *driver, u32 type, u32 mask,
1814 struct crypto_shash **tfm_ret,
1815 struct shash_desc **desc_ret)
1817 struct crypto_shash *tfm;
1818 struct shash_desc *desc;
1820 tfm = crypto_alloc_shash(driver, type, mask);
1822 if (PTR_ERR(tfm) == -ENOENT) {
1824 * This algorithm is only available through the ahash
1825 * API, not the shash API, so skip the shash tests.
1829 pr_err("alg: hash: failed to allocate shash transform for %s: %ld\n",
1830 driver, PTR_ERR(tfm));
1831 return PTR_ERR(tfm);
1834 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL);
1836 crypto_free_shash(tfm);
1846 static int __alg_test_hash(const struct hash_testvec *vecs,
1847 unsigned int num_vecs, const char *driver,
1849 const char *generic_driver, unsigned int maxkeysize)
1851 struct crypto_ahash *atfm = NULL;
1852 struct ahash_request *req = NULL;
1853 struct crypto_shash *stfm = NULL;
1854 struct shash_desc *desc = NULL;
1855 struct test_sglist *tsgl = NULL;
1856 u8 *hashstate = NULL;
1857 unsigned int statesize;
1862 * Always test the ahash API. This works regardless of whether the
1863 * algorithm is implemented as ahash or shash.
1866 atfm = crypto_alloc_ahash(driver, type, mask);
1868 pr_err("alg: hash: failed to allocate transform for %s: %ld\n",
1869 driver, PTR_ERR(atfm));
1870 return PTR_ERR(atfm);
1872 driver = crypto_ahash_driver_name(atfm);
1874 req = ahash_request_alloc(atfm, GFP_KERNEL);
1876 pr_err("alg: hash: failed to allocate request for %s\n",
1883 * If available also test the shash API, to cover corner cases that may
1884 * be missed by testing the ahash API only.
1886 err = alloc_shash(driver, type, mask, &stfm, &desc);
1890 tsgl = kmalloc(sizeof(*tsgl), GFP_KERNEL);
1891 if (!tsgl || init_test_sglist(tsgl) != 0) {
1892 pr_err("alg: hash: failed to allocate test buffers for %s\n",
1900 statesize = crypto_ahash_statesize(atfm);
1902 statesize = max(statesize, crypto_shash_statesize(stfm));
1903 hashstate = kmalloc(statesize + TESTMGR_POISON_LEN, GFP_KERNEL);
1905 pr_err("alg: hash: failed to allocate hash state buffer for %s\n",
1911 for (i = 0; i < num_vecs; i++) {
1912 if (fips_enabled && vecs[i].fips_skip)
1915 err = test_hash_vec(&vecs[i], i, req, desc, tsgl, hashstate);
1920 err = test_hash_vs_generic_impl(generic_driver, maxkeysize, req,
1921 desc, tsgl, hashstate);
1925 destroy_test_sglist(tsgl);
1929 crypto_free_shash(stfm);
1930 ahash_request_free(req);
1931 crypto_free_ahash(atfm);
1935 static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1938 const struct hash_testvec *template = desc->suite.hash.vecs;
1939 unsigned int tcount = desc->suite.hash.count;
1940 unsigned int nr_unkeyed, nr_keyed;
1941 unsigned int maxkeysize = 0;
1945 * For OPTIONAL_KEY algorithms, we have to do all the unkeyed tests
1946 * first, before setting a key on the tfm. To make this easier, we
1947 * require that the unkeyed test vectors (if any) are listed first.
1950 for (nr_unkeyed = 0; nr_unkeyed < tcount; nr_unkeyed++) {
1951 if (template[nr_unkeyed].ksize)
1954 for (nr_keyed = 0; nr_unkeyed + nr_keyed < tcount; nr_keyed++) {
1955 if (!template[nr_unkeyed + nr_keyed].ksize) {
1956 pr_err("alg: hash: test vectors for %s out of order, "
1957 "unkeyed ones must come first\n", desc->alg);
1960 maxkeysize = max_t(unsigned int, maxkeysize,
1961 template[nr_unkeyed + nr_keyed].ksize);
1966 err = __alg_test_hash(template, nr_unkeyed, driver, type, mask,
1967 desc->generic_driver, maxkeysize);
1968 template += nr_unkeyed;
1971 if (!err && nr_keyed)
1972 err = __alg_test_hash(template, nr_keyed, driver, type, mask,
1973 desc->generic_driver, maxkeysize);
1978 static int test_aead_vec_cfg(int enc, const struct aead_testvec *vec,
1979 const char *vec_name,
1980 const struct testvec_config *cfg,
1981 struct aead_request *req,
1982 struct cipher_test_sglists *tsgls)
1984 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1985 const unsigned int alignmask = crypto_aead_alignmask(tfm);
1986 const unsigned int ivsize = crypto_aead_ivsize(tfm);
1987 const unsigned int authsize = vec->clen - vec->plen;
1988 const char *driver = crypto_aead_driver_name(tfm);
1989 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1990 const char *op = enc ? "encryption" : "decryption";
1991 DECLARE_CRYPTO_WAIT(wait);
1992 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
1993 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
1995 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
1996 struct kvec input[2];
2001 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
2003 crypto_aead_clear_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
2005 err = do_setkey(crypto_aead_setkey, tfm, vec->key, vec->klen,
2007 if (err && err != vec->setkey_error) {
2008 pr_err("alg: aead: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
2009 driver, vec_name, vec->setkey_error, err,
2010 crypto_aead_get_flags(tfm));
2013 if (!err && vec->setkey_error) {
2014 pr_err("alg: aead: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
2015 driver, vec_name, vec->setkey_error);
2019 /* Set the authentication tag size */
2020 err = crypto_aead_setauthsize(tfm, authsize);
2021 if (err && err != vec->setauthsize_error) {
2022 pr_err("alg: aead: %s setauthsize failed on test vector %s; expected_error=%d, actual_error=%d\n",
2023 driver, vec_name, vec->setauthsize_error, err);
2026 if (!err && vec->setauthsize_error) {
2027 pr_err("alg: aead: %s setauthsize unexpectedly succeeded on test vector %s; expected_error=%d\n",
2028 driver, vec_name, vec->setauthsize_error);
2032 if (vec->setkey_error || vec->setauthsize_error)
2035 /* The IV must be copied to a buffer, as the algorithm may modify it */
2036 if (WARN_ON(ivsize > MAX_IVLEN))
2039 memcpy(iv, vec->iv, ivsize);
2041 memset(iv, 0, ivsize);
2043 /* Build the src/dst scatterlists */
2044 input[0].iov_base = (void *)vec->assoc;
2045 input[0].iov_len = vec->alen;
2046 input[1].iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
2047 input[1].iov_len = enc ? vec->plen : vec->clen;
2048 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
2049 vec->alen + (enc ? vec->plen :
2051 vec->alen + (enc ? vec->clen :
2055 pr_err("alg: aead: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
2056 driver, op, vec_name, cfg->name);
2060 /* Do the actual encryption or decryption */
2061 testmgr_poison(req->__ctx, crypto_aead_reqsize(tfm));
2062 aead_request_set_callback(req, req_flags, crypto_req_done, &wait);
2063 aead_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
2064 enc ? vec->plen : vec->clen, iv);
2065 aead_request_set_ad(req, vec->alen);
2067 crypto_disable_simd_for_test();
2068 err = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
2070 crypto_reenable_simd_for_test();
2071 err = crypto_wait_req(err, &wait);
2073 /* Check that the algorithm didn't overwrite things it shouldn't have */
2074 if (req->cryptlen != (enc ? vec->plen : vec->clen) ||
2075 req->assoclen != vec->alen ||
2077 req->src != tsgls->src.sgl_ptr ||
2078 req->dst != tsgls->dst.sgl_ptr ||
2079 crypto_aead_reqtfm(req) != tfm ||
2080 req->base.complete != crypto_req_done ||
2081 req->base.flags != req_flags ||
2082 req->base.data != &wait) {
2083 pr_err("alg: aead: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
2084 driver, op, vec_name, cfg->name);
2085 if (req->cryptlen != (enc ? vec->plen : vec->clen))
2086 pr_err("alg: aead: changed 'req->cryptlen'\n");
2087 if (req->assoclen != vec->alen)
2088 pr_err("alg: aead: changed 'req->assoclen'\n");
2090 pr_err("alg: aead: changed 'req->iv'\n");
2091 if (req->src != tsgls->src.sgl_ptr)
2092 pr_err("alg: aead: changed 'req->src'\n");
2093 if (req->dst != tsgls->dst.sgl_ptr)
2094 pr_err("alg: aead: changed 'req->dst'\n");
2095 if (crypto_aead_reqtfm(req) != tfm)
2096 pr_err("alg: aead: changed 'req->base.tfm'\n");
2097 if (req->base.complete != crypto_req_done)
2098 pr_err("alg: aead: changed 'req->base.complete'\n");
2099 if (req->base.flags != req_flags)
2100 pr_err("alg: aead: changed 'req->base.flags'\n");
2101 if (req->base.data != &wait)
2102 pr_err("alg: aead: changed 'req->base.data'\n");
2105 if (is_test_sglist_corrupted(&tsgls->src)) {
2106 pr_err("alg: aead: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
2107 driver, op, vec_name, cfg->name);
2110 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
2111 is_test_sglist_corrupted(&tsgls->dst)) {
2112 pr_err("alg: aead: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
2113 driver, op, vec_name, cfg->name);
2117 /* Check for unexpected success or failure, or wrong error code */
2118 if ((err == 0 && vec->novrfy) ||
2119 (err != vec->crypt_error && !(err == -EBADMSG && vec->novrfy))) {
2120 char expected_error[32];
2123 vec->crypt_error != 0 && vec->crypt_error != -EBADMSG)
2124 sprintf(expected_error, "-EBADMSG or %d",
2126 else if (vec->novrfy)
2127 sprintf(expected_error, "-EBADMSG");
2129 sprintf(expected_error, "%d", vec->crypt_error);
2131 pr_err("alg: aead: %s %s failed on test vector %s; expected_error=%s, actual_error=%d, cfg=\"%s\"\n",
2132 driver, op, vec_name, expected_error, err,
2136 pr_err("alg: aead: %s %s unexpectedly succeeded on test vector %s; expected_error=%s, cfg=\"%s\"\n",
2137 driver, op, vec_name, expected_error, cfg->name);
2140 if (err) /* Expectedly failed. */
2143 /* Check for the correct output (ciphertext or plaintext) */
2144 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
2145 enc ? vec->clen : vec->plen,
2147 enc || cfg->inplace_mode == OUT_OF_PLACE);
2148 if (err == -EOVERFLOW) {
2149 pr_err("alg: aead: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
2150 driver, op, vec_name, cfg->name);
2154 pr_err("alg: aead: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
2155 driver, op, vec_name, cfg->name);
2162 static int test_aead_vec(int enc, const struct aead_testvec *vec,
2163 unsigned int vec_num, struct aead_request *req,
2164 struct cipher_test_sglists *tsgls)
2170 if (enc && vec->novrfy)
2173 sprintf(vec_name, "%u", vec_num);
2175 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
2176 err = test_aead_vec_cfg(enc, vec, vec_name,
2177 &default_cipher_testvec_configs[i],
2183 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2184 if (!noextratests) {
2185 struct testvec_config cfg;
2186 char cfgname[TESTVEC_CONFIG_NAMELEN];
2188 for (i = 0; i < fuzz_iterations; i++) {
2189 generate_random_testvec_config(&cfg, cfgname,
2191 err = test_aead_vec_cfg(enc, vec, vec_name,
2202 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2204 struct aead_extra_tests_ctx {
2205 struct aead_request *req;
2206 struct crypto_aead *tfm;
2207 const struct alg_test_desc *test_desc;
2208 struct cipher_test_sglists *tsgls;
2209 unsigned int maxdatasize;
2210 unsigned int maxkeysize;
2212 struct aead_testvec vec;
2214 char cfgname[TESTVEC_CONFIG_NAMELEN];
2215 struct testvec_config cfg;
2219 * Make at least one random change to a (ciphertext, AAD) pair. "Ciphertext"
2220 * here means the full ciphertext including the authentication tag. The
2221 * authentication tag (and hence also the ciphertext) is assumed to be nonempty.
2223 static void mutate_aead_message(struct aead_testvec *vec, bool aad_iv,
2224 unsigned int ivsize)
2226 const unsigned int aad_tail_size = aad_iv ? ivsize : 0;
2227 const unsigned int authsize = vec->clen - vec->plen;
2229 if (get_random_u32_below(2) == 0 && vec->alen > aad_tail_size) {
2230 /* Mutate the AAD */
2231 flip_random_bit((u8 *)vec->assoc, vec->alen - aad_tail_size);
2232 if (get_random_u32_below(2) == 0)
2235 if (get_random_u32_below(2) == 0) {
2236 /* Mutate auth tag (assuming it's at the end of ciphertext) */
2237 flip_random_bit((u8 *)vec->ctext + vec->plen, authsize);
2239 /* Mutate any part of the ciphertext */
2240 flip_random_bit((u8 *)vec->ctext, vec->clen);
2245 * Minimum authentication tag size in bytes at which we assume that we can
2246 * reliably generate inauthentic messages, i.e. not generate an authentic
2247 * message by chance.
2249 #define MIN_COLLISION_FREE_AUTHSIZE 8
2251 static void generate_aead_message(struct aead_request *req,
2252 const struct aead_test_suite *suite,
2253 struct aead_testvec *vec,
2254 bool prefer_inauthentic)
2256 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2257 const unsigned int ivsize = crypto_aead_ivsize(tfm);
2258 const unsigned int authsize = vec->clen - vec->plen;
2259 const bool inauthentic = (authsize >= MIN_COLLISION_FREE_AUTHSIZE) &&
2260 (prefer_inauthentic || get_random_u32_below(4) == 0);
2262 /* Generate the AAD. */
2263 generate_random_bytes((u8 *)vec->assoc, vec->alen);
2264 if (suite->aad_iv && vec->alen >= ivsize)
2265 /* Avoid implementation-defined behavior. */
2266 memcpy((u8 *)vec->assoc + vec->alen - ivsize, vec->iv, ivsize);
2268 if (inauthentic && get_random_u32_below(2) == 0) {
2269 /* Generate a random ciphertext. */
2270 generate_random_bytes((u8 *)vec->ctext, vec->clen);
2273 struct scatterlist src[2], dst;
2275 DECLARE_CRYPTO_WAIT(wait);
2277 /* Generate a random plaintext and encrypt it. */
2278 sg_init_table(src, 2);
2280 sg_set_buf(&src[i++], vec->assoc, vec->alen);
2282 generate_random_bytes((u8 *)vec->ptext, vec->plen);
2283 sg_set_buf(&src[i++], vec->ptext, vec->plen);
2285 sg_init_one(&dst, vec->ctext, vec->alen + vec->clen);
2286 memcpy(iv, vec->iv, ivsize);
2287 aead_request_set_callback(req, 0, crypto_req_done, &wait);
2288 aead_request_set_crypt(req, src, &dst, vec->plen, iv);
2289 aead_request_set_ad(req, vec->alen);
2290 vec->crypt_error = crypto_wait_req(crypto_aead_encrypt(req),
2292 /* If encryption failed, we're done. */
2293 if (vec->crypt_error != 0)
2295 memmove((u8 *)vec->ctext, vec->ctext + vec->alen, vec->clen);
2299 * Mutate the authentic (ciphertext, AAD) pair to get an
2302 mutate_aead_message(vec, suite->aad_iv, ivsize);
2305 if (suite->einval_allowed)
2306 vec->crypt_error = -EINVAL;
2310 * Generate an AEAD test vector 'vec' using the implementation specified by
2311 * 'req'. The buffers in 'vec' must already be allocated.
2313 * If 'prefer_inauthentic' is true, then this function will generate inauthentic
2314 * test vectors (i.e. vectors with 'vec->novrfy=1') more often.
2316 static void generate_random_aead_testvec(struct aead_request *req,
2317 struct aead_testvec *vec,
2318 const struct aead_test_suite *suite,
2319 unsigned int maxkeysize,
2320 unsigned int maxdatasize,
2321 char *name, size_t max_namelen,
2322 bool prefer_inauthentic)
2324 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2325 const unsigned int ivsize = crypto_aead_ivsize(tfm);
2326 const unsigned int maxauthsize = crypto_aead_maxauthsize(tfm);
2327 unsigned int authsize;
2328 unsigned int total_len;
2330 /* Key: length in [0, maxkeysize], but usually choose maxkeysize */
2331 vec->klen = maxkeysize;
2332 if (get_random_u32_below(4) == 0)
2333 vec->klen = get_random_u32_below(maxkeysize + 1);
2334 generate_random_bytes((u8 *)vec->key, vec->klen);
2335 vec->setkey_error = crypto_aead_setkey(tfm, vec->key, vec->klen);
2338 generate_random_bytes((u8 *)vec->iv, ivsize);
2340 /* Tag length: in [0, maxauthsize], but usually choose maxauthsize */
2341 authsize = maxauthsize;
2342 if (get_random_u32_below(4) == 0)
2343 authsize = get_random_u32_below(maxauthsize + 1);
2344 if (prefer_inauthentic && authsize < MIN_COLLISION_FREE_AUTHSIZE)
2345 authsize = MIN_COLLISION_FREE_AUTHSIZE;
2346 if (WARN_ON(authsize > maxdatasize))
2347 authsize = maxdatasize;
2348 maxdatasize -= authsize;
2349 vec->setauthsize_error = crypto_aead_setauthsize(tfm, authsize);
2351 /* AAD, plaintext, and ciphertext lengths */
2352 total_len = generate_random_length(maxdatasize);
2353 if (get_random_u32_below(4) == 0)
2356 vec->alen = generate_random_length(total_len);
2357 vec->plen = total_len - vec->alen;
2358 vec->clen = vec->plen + authsize;
2361 * Generate the AAD, plaintext, and ciphertext. Not applicable if the
2362 * key or the authentication tag size couldn't be set.
2365 vec->crypt_error = 0;
2366 if (vec->setkey_error == 0 && vec->setauthsize_error == 0)
2367 generate_aead_message(req, suite, vec, prefer_inauthentic);
2368 snprintf(name, max_namelen,
2369 "\"random: alen=%u plen=%u authsize=%u klen=%u novrfy=%d\"",
2370 vec->alen, vec->plen, authsize, vec->klen, vec->novrfy);
2373 static void try_to_generate_inauthentic_testvec(
2374 struct aead_extra_tests_ctx *ctx)
2378 for (i = 0; i < 10; i++) {
2379 generate_random_aead_testvec(ctx->req, &ctx->vec,
2380 &ctx->test_desc->suite.aead,
2381 ctx->maxkeysize, ctx->maxdatasize,
2383 sizeof(ctx->vec_name), true);
2384 if (ctx->vec.novrfy)
2390 * Generate inauthentic test vectors (i.e. ciphertext, AAD pairs that aren't the
2391 * result of an encryption with the key) and verify that decryption fails.
2393 static int test_aead_inauthentic_inputs(struct aead_extra_tests_ctx *ctx)
2398 for (i = 0; i < fuzz_iterations * 8; i++) {
2400 * Since this part of the tests isn't comparing the
2401 * implementation to another, there's no point in testing any
2402 * test vectors other than inauthentic ones (vec.novrfy=1) here.
2404 * If we're having trouble generating such a test vector, e.g.
2405 * if the algorithm keeps rejecting the generated keys, don't
2406 * retry forever; just continue on.
2408 try_to_generate_inauthentic_testvec(ctx);
2409 if (ctx->vec.novrfy) {
2410 generate_random_testvec_config(&ctx->cfg, ctx->cfgname,
2411 sizeof(ctx->cfgname));
2412 err = test_aead_vec_cfg(DECRYPT, &ctx->vec,
2413 ctx->vec_name, &ctx->cfg,
2414 ctx->req, ctx->tsgls);
2424 * Test the AEAD algorithm against the corresponding generic implementation, if
2427 static int test_aead_vs_generic_impl(struct aead_extra_tests_ctx *ctx)
2429 struct crypto_aead *tfm = ctx->tfm;
2430 const char *algname = crypto_aead_alg(tfm)->base.cra_name;
2431 const char *driver = crypto_aead_driver_name(tfm);
2432 const char *generic_driver = ctx->test_desc->generic_driver;
2433 char _generic_driver[CRYPTO_MAX_ALG_NAME];
2434 struct crypto_aead *generic_tfm = NULL;
2435 struct aead_request *generic_req = NULL;
2439 if (!generic_driver) { /* Use default naming convention? */
2440 err = build_generic_driver_name(algname, _generic_driver);
2443 generic_driver = _generic_driver;
2446 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
2449 generic_tfm = crypto_alloc_aead(generic_driver, 0, 0);
2450 if (IS_ERR(generic_tfm)) {
2451 err = PTR_ERR(generic_tfm);
2452 if (err == -ENOENT) {
2453 pr_warn("alg: aead: skipping comparison tests for %s because %s is unavailable\n",
2454 driver, generic_driver);
2457 pr_err("alg: aead: error allocating %s (generic impl of %s): %d\n",
2458 generic_driver, algname, err);
2462 generic_req = aead_request_alloc(generic_tfm, GFP_KERNEL);
2468 /* Check the algorithm properties for consistency. */
2470 if (crypto_aead_maxauthsize(tfm) !=
2471 crypto_aead_maxauthsize(generic_tfm)) {
2472 pr_err("alg: aead: maxauthsize for %s (%u) doesn't match generic impl (%u)\n",
2473 driver, crypto_aead_maxauthsize(tfm),
2474 crypto_aead_maxauthsize(generic_tfm));
2479 if (crypto_aead_ivsize(tfm) != crypto_aead_ivsize(generic_tfm)) {
2480 pr_err("alg: aead: ivsize for %s (%u) doesn't match generic impl (%u)\n",
2481 driver, crypto_aead_ivsize(tfm),
2482 crypto_aead_ivsize(generic_tfm));
2487 if (crypto_aead_blocksize(tfm) != crypto_aead_blocksize(generic_tfm)) {
2488 pr_err("alg: aead: blocksize for %s (%u) doesn't match generic impl (%u)\n",
2489 driver, crypto_aead_blocksize(tfm),
2490 crypto_aead_blocksize(generic_tfm));
2496 * Now generate test vectors using the generic implementation, and test
2497 * the other implementation against them.
2499 for (i = 0; i < fuzz_iterations * 8; i++) {
2500 generate_random_aead_testvec(generic_req, &ctx->vec,
2501 &ctx->test_desc->suite.aead,
2502 ctx->maxkeysize, ctx->maxdatasize,
2504 sizeof(ctx->vec_name), false);
2505 generate_random_testvec_config(&ctx->cfg, ctx->cfgname,
2506 sizeof(ctx->cfgname));
2507 if (!ctx->vec.novrfy) {
2508 err = test_aead_vec_cfg(ENCRYPT, &ctx->vec,
2509 ctx->vec_name, &ctx->cfg,
2510 ctx->req, ctx->tsgls);
2514 if (ctx->vec.crypt_error == 0 || ctx->vec.novrfy) {
2515 err = test_aead_vec_cfg(DECRYPT, &ctx->vec,
2516 ctx->vec_name, &ctx->cfg,
2517 ctx->req, ctx->tsgls);
2525 crypto_free_aead(generic_tfm);
2526 aead_request_free(generic_req);
2530 static int test_aead_extra(const struct alg_test_desc *test_desc,
2531 struct aead_request *req,
2532 struct cipher_test_sglists *tsgls)
2534 struct aead_extra_tests_ctx *ctx;
2541 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2545 ctx->tfm = crypto_aead_reqtfm(req);
2546 ctx->test_desc = test_desc;
2548 ctx->maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
2549 ctx->maxkeysize = 0;
2550 for (i = 0; i < test_desc->suite.aead.count; i++)
2551 ctx->maxkeysize = max_t(unsigned int, ctx->maxkeysize,
2552 test_desc->suite.aead.vecs[i].klen);
2554 ctx->vec.key = kmalloc(ctx->maxkeysize, GFP_KERNEL);
2555 ctx->vec.iv = kmalloc(crypto_aead_ivsize(ctx->tfm), GFP_KERNEL);
2556 ctx->vec.assoc = kmalloc(ctx->maxdatasize, GFP_KERNEL);
2557 ctx->vec.ptext = kmalloc(ctx->maxdatasize, GFP_KERNEL);
2558 ctx->vec.ctext = kmalloc(ctx->maxdatasize, GFP_KERNEL);
2559 if (!ctx->vec.key || !ctx->vec.iv || !ctx->vec.assoc ||
2560 !ctx->vec.ptext || !ctx->vec.ctext) {
2565 err = test_aead_vs_generic_impl(ctx);
2569 err = test_aead_inauthentic_inputs(ctx);
2571 kfree(ctx->vec.key);
2573 kfree(ctx->vec.assoc);
2574 kfree(ctx->vec.ptext);
2575 kfree(ctx->vec.ctext);
2579 #else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
2580 static int test_aead_extra(const struct alg_test_desc *test_desc,
2581 struct aead_request *req,
2582 struct cipher_test_sglists *tsgls)
2586 #endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
2588 static int test_aead(int enc, const struct aead_test_suite *suite,
2589 struct aead_request *req,
2590 struct cipher_test_sglists *tsgls)
2595 for (i = 0; i < suite->count; i++) {
2596 err = test_aead_vec(enc, &suite->vecs[i], i, req, tsgls);
2604 static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
2607 const struct aead_test_suite *suite = &desc->suite.aead;
2608 struct crypto_aead *tfm;
2609 struct aead_request *req = NULL;
2610 struct cipher_test_sglists *tsgls = NULL;
2613 if (suite->count <= 0) {
2614 pr_err("alg: aead: empty test suite for %s\n", driver);
2618 tfm = crypto_alloc_aead(driver, type, mask);
2620 pr_err("alg: aead: failed to allocate transform for %s: %ld\n",
2621 driver, PTR_ERR(tfm));
2622 return PTR_ERR(tfm);
2624 driver = crypto_aead_driver_name(tfm);
2626 req = aead_request_alloc(tfm, GFP_KERNEL);
2628 pr_err("alg: aead: failed to allocate request for %s\n",
2634 tsgls = alloc_cipher_test_sglists();
2636 pr_err("alg: aead: failed to allocate test buffers for %s\n",
2642 err = test_aead(ENCRYPT, suite, req, tsgls);
2646 err = test_aead(DECRYPT, suite, req, tsgls);
2650 err = test_aead_extra(desc, req, tsgls);
2652 free_cipher_test_sglists(tsgls);
2653 aead_request_free(req);
2654 crypto_free_aead(tfm);
2658 static int test_cipher(struct crypto_cipher *tfm, int enc,
2659 const struct cipher_testvec *template,
2660 unsigned int tcount)
2662 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
2663 unsigned int i, j, k;
2666 const char *input, *result;
2668 char *xbuf[XBUFSIZE];
2671 if (testmgr_alloc_buf(xbuf))
2680 for (i = 0; i < tcount; i++) {
2682 if (fips_enabled && template[i].fips_skip)
2685 input = enc ? template[i].ptext : template[i].ctext;
2686 result = enc ? template[i].ctext : template[i].ptext;
2690 if (WARN_ON(template[i].len > PAGE_SIZE))
2694 memcpy(data, input, template[i].len);
2696 crypto_cipher_clear_flags(tfm, ~0);
2698 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
2700 ret = crypto_cipher_setkey(tfm, template[i].key,
2703 if (ret == template[i].setkey_error)
2705 pr_err("alg: cipher: %s setkey failed on test vector %u; expected_error=%d, actual_error=%d, flags=%#x\n",
2706 algo, j, template[i].setkey_error, ret,
2707 crypto_cipher_get_flags(tfm));
2710 if (template[i].setkey_error) {
2711 pr_err("alg: cipher: %s setkey unexpectedly succeeded on test vector %u; expected_error=%d\n",
2712 algo, j, template[i].setkey_error);
2717 for (k = 0; k < template[i].len;
2718 k += crypto_cipher_blocksize(tfm)) {
2720 crypto_cipher_encrypt_one(tfm, data + k,
2723 crypto_cipher_decrypt_one(tfm, data + k,
2728 if (memcmp(q, result, template[i].len)) {
2729 printk(KERN_ERR "alg: cipher: Test %d failed "
2730 "on %s for %s\n", j, e, algo);
2731 hexdump(q, template[i].len);
2740 testmgr_free_buf(xbuf);
2745 static int test_skcipher_vec_cfg(int enc, const struct cipher_testvec *vec,
2746 const char *vec_name,
2747 const struct testvec_config *cfg,
2748 struct skcipher_request *req,
2749 struct cipher_test_sglists *tsgls)
2751 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2752 const unsigned int alignmask = crypto_skcipher_alignmask(tfm);
2753 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2754 const char *driver = crypto_skcipher_driver_name(tfm);
2755 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
2756 const char *op = enc ? "encryption" : "decryption";
2757 DECLARE_CRYPTO_WAIT(wait);
2758 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
2759 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
2761 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
2767 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
2769 crypto_skcipher_clear_flags(tfm,
2770 CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
2771 err = do_setkey(crypto_skcipher_setkey, tfm, vec->key, vec->klen,
2774 if (err == vec->setkey_error)
2776 pr_err("alg: skcipher: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
2777 driver, vec_name, vec->setkey_error, err,
2778 crypto_skcipher_get_flags(tfm));
2781 if (vec->setkey_error) {
2782 pr_err("alg: skcipher: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
2783 driver, vec_name, vec->setkey_error);
2787 /* The IV must be copied to a buffer, as the algorithm may modify it */
2789 if (WARN_ON(ivsize > MAX_IVLEN))
2791 if (vec->generates_iv && !enc)
2792 memcpy(iv, vec->iv_out, ivsize);
2794 memcpy(iv, vec->iv, ivsize);
2796 memset(iv, 0, ivsize);
2798 if (vec->generates_iv) {
2799 pr_err("alg: skcipher: %s has ivsize=0 but test vector %s generates IV!\n",
2806 /* Build the src/dst scatterlists */
2807 input.iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
2808 input.iov_len = vec->len;
2809 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
2810 vec->len, vec->len, &input, 1);
2812 pr_err("alg: skcipher: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
2813 driver, op, vec_name, cfg->name);
2817 /* Do the actual encryption or decryption */
2818 testmgr_poison(req->__ctx, crypto_skcipher_reqsize(tfm));
2819 skcipher_request_set_callback(req, req_flags, crypto_req_done, &wait);
2820 skcipher_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
2823 crypto_disable_simd_for_test();
2824 err = enc ? crypto_skcipher_encrypt(req) : crypto_skcipher_decrypt(req);
2826 crypto_reenable_simd_for_test();
2827 err = crypto_wait_req(err, &wait);
2829 /* Check that the algorithm didn't overwrite things it shouldn't have */
2830 if (req->cryptlen != vec->len ||
2832 req->src != tsgls->src.sgl_ptr ||
2833 req->dst != tsgls->dst.sgl_ptr ||
2834 crypto_skcipher_reqtfm(req) != tfm ||
2835 req->base.complete != crypto_req_done ||
2836 req->base.flags != req_flags ||
2837 req->base.data != &wait) {
2838 pr_err("alg: skcipher: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
2839 driver, op, vec_name, cfg->name);
2840 if (req->cryptlen != vec->len)
2841 pr_err("alg: skcipher: changed 'req->cryptlen'\n");
2843 pr_err("alg: skcipher: changed 'req->iv'\n");
2844 if (req->src != tsgls->src.sgl_ptr)
2845 pr_err("alg: skcipher: changed 'req->src'\n");
2846 if (req->dst != tsgls->dst.sgl_ptr)
2847 pr_err("alg: skcipher: changed 'req->dst'\n");
2848 if (crypto_skcipher_reqtfm(req) != tfm)
2849 pr_err("alg: skcipher: changed 'req->base.tfm'\n");
2850 if (req->base.complete != crypto_req_done)
2851 pr_err("alg: skcipher: changed 'req->base.complete'\n");
2852 if (req->base.flags != req_flags)
2853 pr_err("alg: skcipher: changed 'req->base.flags'\n");
2854 if (req->base.data != &wait)
2855 pr_err("alg: skcipher: changed 'req->base.data'\n");
2858 if (is_test_sglist_corrupted(&tsgls->src)) {
2859 pr_err("alg: skcipher: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
2860 driver, op, vec_name, cfg->name);
2863 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
2864 is_test_sglist_corrupted(&tsgls->dst)) {
2865 pr_err("alg: skcipher: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
2866 driver, op, vec_name, cfg->name);
2870 /* Check for success or failure */
2872 if (err == vec->crypt_error)
2874 pr_err("alg: skcipher: %s %s failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
2875 driver, op, vec_name, vec->crypt_error, err, cfg->name);
2878 if (vec->crypt_error) {
2879 pr_err("alg: skcipher: %s %s unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
2880 driver, op, vec_name, vec->crypt_error, cfg->name);
2884 /* Check for the correct output (ciphertext or plaintext) */
2885 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
2887 if (err == -EOVERFLOW) {
2888 pr_err("alg: skcipher: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
2889 driver, op, vec_name, cfg->name);
2893 pr_err("alg: skcipher: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
2894 driver, op, vec_name, cfg->name);
2898 /* If applicable, check that the algorithm generated the correct IV */
2899 if (vec->iv_out && memcmp(iv, vec->iv_out, ivsize) != 0) {
2900 pr_err("alg: skcipher: %s %s test failed (wrong output IV) on test vector %s, cfg=\"%s\"\n",
2901 driver, op, vec_name, cfg->name);
2902 hexdump(iv, ivsize);
2909 static int test_skcipher_vec(int enc, const struct cipher_testvec *vec,
2910 unsigned int vec_num,
2911 struct skcipher_request *req,
2912 struct cipher_test_sglists *tsgls)
2918 if (fips_enabled && vec->fips_skip)
2921 sprintf(vec_name, "%u", vec_num);
2923 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
2924 err = test_skcipher_vec_cfg(enc, vec, vec_name,
2925 &default_cipher_testvec_configs[i],
2931 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2932 if (!noextratests) {
2933 struct testvec_config cfg;
2934 char cfgname[TESTVEC_CONFIG_NAMELEN];
2936 for (i = 0; i < fuzz_iterations; i++) {
2937 generate_random_testvec_config(&cfg, cfgname,
2939 err = test_skcipher_vec_cfg(enc, vec, vec_name,
2950 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2952 * Generate a symmetric cipher test vector from the given implementation.
2953 * Assumes the buffers in 'vec' were already allocated.
2955 static void generate_random_cipher_testvec(struct skcipher_request *req,
2956 struct cipher_testvec *vec,
2957 unsigned int maxdatasize,
2958 char *name, size_t max_namelen)
2960 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2961 const unsigned int maxkeysize = crypto_skcipher_max_keysize(tfm);
2962 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2963 struct scatterlist src, dst;
2965 DECLARE_CRYPTO_WAIT(wait);
2967 /* Key: length in [0, maxkeysize], but usually choose maxkeysize */
2968 vec->klen = maxkeysize;
2969 if (get_random_u32_below(4) == 0)
2970 vec->klen = get_random_u32_below(maxkeysize + 1);
2971 generate_random_bytes((u8 *)vec->key, vec->klen);
2972 vec->setkey_error = crypto_skcipher_setkey(tfm, vec->key, vec->klen);
2975 generate_random_bytes((u8 *)vec->iv, ivsize);
2978 vec->len = generate_random_length(maxdatasize);
2979 generate_random_bytes((u8 *)vec->ptext, vec->len);
2981 /* If the key couldn't be set, no need to continue to encrypt. */
2982 if (vec->setkey_error)
2986 sg_init_one(&src, vec->ptext, vec->len);
2987 sg_init_one(&dst, vec->ctext, vec->len);
2988 memcpy(iv, vec->iv, ivsize);
2989 skcipher_request_set_callback(req, 0, crypto_req_done, &wait);
2990 skcipher_request_set_crypt(req, &src, &dst, vec->len, iv);
2991 vec->crypt_error = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
2992 if (vec->crypt_error != 0) {
2994 * The only acceptable error here is for an invalid length, so
2995 * skcipher decryption should fail with the same error too.
2996 * We'll test for this. But to keep the API usage well-defined,
2997 * explicitly initialize the ciphertext buffer too.
2999 memset((u8 *)vec->ctext, 0, vec->len);
3002 snprintf(name, max_namelen, "\"random: len=%u klen=%u\"",
3003 vec->len, vec->klen);
3007 * Test the skcipher algorithm represented by @req against the corresponding
3008 * generic implementation, if one is available.
3010 static int test_skcipher_vs_generic_impl(const char *generic_driver,
3011 struct skcipher_request *req,
3012 struct cipher_test_sglists *tsgls)
3014 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
3015 const unsigned int maxkeysize = crypto_skcipher_max_keysize(tfm);
3016 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
3017 const unsigned int blocksize = crypto_skcipher_blocksize(tfm);
3018 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
3019 const char *algname = crypto_skcipher_alg(tfm)->base.cra_name;
3020 const char *driver = crypto_skcipher_driver_name(tfm);
3021 char _generic_driver[CRYPTO_MAX_ALG_NAME];
3022 struct crypto_skcipher *generic_tfm = NULL;
3023 struct skcipher_request *generic_req = NULL;
3025 struct cipher_testvec vec = { 0 };
3027 struct testvec_config *cfg;
3028 char cfgname[TESTVEC_CONFIG_NAMELEN];
3034 /* Keywrap isn't supported here yet as it handles its IV differently. */
3035 if (strncmp(algname, "kw(", 3) == 0)
3038 if (!generic_driver) { /* Use default naming convention? */
3039 err = build_generic_driver_name(algname, _generic_driver);
3042 generic_driver = _generic_driver;
3045 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
3048 generic_tfm = crypto_alloc_skcipher(generic_driver, 0, 0);
3049 if (IS_ERR(generic_tfm)) {
3050 err = PTR_ERR(generic_tfm);
3051 if (err == -ENOENT) {
3052 pr_warn("alg: skcipher: skipping comparison tests for %s because %s is unavailable\n",
3053 driver, generic_driver);
3056 pr_err("alg: skcipher: error allocating %s (generic impl of %s): %d\n",
3057 generic_driver, algname, err);
3061 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
3067 generic_req = skcipher_request_alloc(generic_tfm, GFP_KERNEL);
3073 /* Check the algorithm properties for consistency. */
3075 if (crypto_skcipher_min_keysize(tfm) !=
3076 crypto_skcipher_min_keysize(generic_tfm)) {
3077 pr_err("alg: skcipher: min keysize for %s (%u) doesn't match generic impl (%u)\n",
3078 driver, crypto_skcipher_min_keysize(tfm),
3079 crypto_skcipher_min_keysize(generic_tfm));
3084 if (maxkeysize != crypto_skcipher_max_keysize(generic_tfm)) {
3085 pr_err("alg: skcipher: max keysize for %s (%u) doesn't match generic impl (%u)\n",
3087 crypto_skcipher_max_keysize(generic_tfm));
3092 if (ivsize != crypto_skcipher_ivsize(generic_tfm)) {
3093 pr_err("alg: skcipher: ivsize for %s (%u) doesn't match generic impl (%u)\n",
3094 driver, ivsize, crypto_skcipher_ivsize(generic_tfm));
3099 if (blocksize != crypto_skcipher_blocksize(generic_tfm)) {
3100 pr_err("alg: skcipher: blocksize for %s (%u) doesn't match generic impl (%u)\n",
3102 crypto_skcipher_blocksize(generic_tfm));
3108 * Now generate test vectors using the generic implementation, and test
3109 * the other implementation against them.
3112 vec.key = kmalloc(maxkeysize, GFP_KERNEL);
3113 vec.iv = kmalloc(ivsize, GFP_KERNEL);
3114 vec.ptext = kmalloc(maxdatasize, GFP_KERNEL);
3115 vec.ctext = kmalloc(maxdatasize, GFP_KERNEL);
3116 if (!vec.key || !vec.iv || !vec.ptext || !vec.ctext) {
3121 for (i = 0; i < fuzz_iterations * 8; i++) {
3122 generate_random_cipher_testvec(generic_req, &vec, maxdatasize,
3123 vec_name, sizeof(vec_name));
3124 generate_random_testvec_config(cfg, cfgname, sizeof(cfgname));
3126 err = test_skcipher_vec_cfg(ENCRYPT, &vec, vec_name,
3130 err = test_skcipher_vec_cfg(DECRYPT, &vec, vec_name,
3143 crypto_free_skcipher(generic_tfm);
3144 skcipher_request_free(generic_req);
3147 #else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
3148 static int test_skcipher_vs_generic_impl(const char *generic_driver,
3149 struct skcipher_request *req,
3150 struct cipher_test_sglists *tsgls)
3154 #endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
3156 static int test_skcipher(int enc, const struct cipher_test_suite *suite,
3157 struct skcipher_request *req,
3158 struct cipher_test_sglists *tsgls)
3163 for (i = 0; i < suite->count; i++) {
3164 err = test_skcipher_vec(enc, &suite->vecs[i], i, req, tsgls);
3172 static int alg_test_skcipher(const struct alg_test_desc *desc,
3173 const char *driver, u32 type, u32 mask)
3175 const struct cipher_test_suite *suite = &desc->suite.cipher;
3176 struct crypto_skcipher *tfm;
3177 struct skcipher_request *req = NULL;
3178 struct cipher_test_sglists *tsgls = NULL;
3181 if (suite->count <= 0) {
3182 pr_err("alg: skcipher: empty test suite for %s\n", driver);
3186 tfm = crypto_alloc_skcipher(driver, type, mask);
3188 pr_err("alg: skcipher: failed to allocate transform for %s: %ld\n",
3189 driver, PTR_ERR(tfm));
3190 return PTR_ERR(tfm);
3192 driver = crypto_skcipher_driver_name(tfm);
3194 req = skcipher_request_alloc(tfm, GFP_KERNEL);
3196 pr_err("alg: skcipher: failed to allocate request for %s\n",
3202 tsgls = alloc_cipher_test_sglists();
3204 pr_err("alg: skcipher: failed to allocate test buffers for %s\n",
3210 err = test_skcipher(ENCRYPT, suite, req, tsgls);
3214 err = test_skcipher(DECRYPT, suite, req, tsgls);
3218 err = test_skcipher_vs_generic_impl(desc->generic_driver, req, tsgls);
3220 free_cipher_test_sglists(tsgls);
3221 skcipher_request_free(req);
3222 crypto_free_skcipher(tfm);
3226 static int test_comp(struct crypto_comp *tfm,
3227 const struct comp_testvec *ctemplate,
3228 const struct comp_testvec *dtemplate,
3229 int ctcount, int dtcount)
3231 const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
3232 char *output, *decomp_output;
3236 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3240 decomp_output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3241 if (!decomp_output) {
3246 for (i = 0; i < ctcount; i++) {
3248 unsigned int dlen = COMP_BUF_SIZE;
3250 memset(output, 0, COMP_BUF_SIZE);
3251 memset(decomp_output, 0, COMP_BUF_SIZE);
3253 ilen = ctemplate[i].inlen;
3254 ret = crypto_comp_compress(tfm, ctemplate[i].input,
3255 ilen, output, &dlen);
3257 printk(KERN_ERR "alg: comp: compression failed "
3258 "on test %d for %s: ret=%d\n", i + 1, algo,
3264 dlen = COMP_BUF_SIZE;
3265 ret = crypto_comp_decompress(tfm, output,
3266 ilen, decomp_output, &dlen);
3268 pr_err("alg: comp: compression failed: decompress: on test %d for %s failed: ret=%d\n",
3273 if (dlen != ctemplate[i].inlen) {
3274 printk(KERN_ERR "alg: comp: Compression test %d "
3275 "failed for %s: output len = %d\n", i + 1, algo,
3281 if (memcmp(decomp_output, ctemplate[i].input,
3282 ctemplate[i].inlen)) {
3283 pr_err("alg: comp: compression failed: output differs: on test %d for %s\n",
3285 hexdump(decomp_output, dlen);
3291 for (i = 0; i < dtcount; i++) {
3293 unsigned int dlen = COMP_BUF_SIZE;
3295 memset(decomp_output, 0, COMP_BUF_SIZE);
3297 ilen = dtemplate[i].inlen;
3298 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
3299 ilen, decomp_output, &dlen);
3301 printk(KERN_ERR "alg: comp: decompression failed "
3302 "on test %d for %s: ret=%d\n", i + 1, algo,
3307 if (dlen != dtemplate[i].outlen) {
3308 printk(KERN_ERR "alg: comp: Decompression test %d "
3309 "failed for %s: output len = %d\n", i + 1, algo,
3315 if (memcmp(decomp_output, dtemplate[i].output, dlen)) {
3316 printk(KERN_ERR "alg: comp: Decompression test %d "
3317 "failed for %s\n", i + 1, algo);
3318 hexdump(decomp_output, dlen);
3327 kfree(decomp_output);
3332 static int test_acomp(struct crypto_acomp *tfm,
3333 const struct comp_testvec *ctemplate,
3334 const struct comp_testvec *dtemplate,
3335 int ctcount, int dtcount)
3337 const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
3339 char *output, *decomp_out;
3341 struct scatterlist src, dst;
3342 struct acomp_req *req;
3343 struct crypto_wait wait;
3345 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3349 decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3355 for (i = 0; i < ctcount; i++) {
3356 unsigned int dlen = COMP_BUF_SIZE;
3357 int ilen = ctemplate[i].inlen;
3360 input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL);
3366 memset(output, 0, dlen);
3367 crypto_init_wait(&wait);
3368 sg_init_one(&src, input_vec, ilen);
3369 sg_init_one(&dst, output, dlen);
3371 req = acomp_request_alloc(tfm);
3373 pr_err("alg: acomp: request alloc failed for %s\n",
3380 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3381 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3382 crypto_req_done, &wait);
3384 ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
3386 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
3389 acomp_request_free(req);
3394 dlen = COMP_BUF_SIZE;
3395 sg_init_one(&src, output, ilen);
3396 sg_init_one(&dst, decomp_out, dlen);
3397 crypto_init_wait(&wait);
3398 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3400 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
3402 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
3405 acomp_request_free(req);
3409 if (req->dlen != ctemplate[i].inlen) {
3410 pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
3411 i + 1, algo, req->dlen);
3414 acomp_request_free(req);
3418 if (memcmp(input_vec, decomp_out, req->dlen)) {
3419 pr_err("alg: acomp: Compression test %d failed for %s\n",
3421 hexdump(output, req->dlen);
3424 acomp_request_free(req);
3428 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
3429 crypto_init_wait(&wait);
3430 sg_init_one(&src, input_vec, ilen);
3431 acomp_request_set_params(req, &src, NULL, ilen, 0);
3433 ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
3435 pr_err("alg: acomp: compression failed on NULL dst buffer test %d for %s: ret=%d\n",
3438 acomp_request_free(req);
3444 acomp_request_free(req);
3447 for (i = 0; i < dtcount; i++) {
3448 unsigned int dlen = COMP_BUF_SIZE;
3449 int ilen = dtemplate[i].inlen;
3452 input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL);
3458 memset(output, 0, dlen);
3459 crypto_init_wait(&wait);
3460 sg_init_one(&src, input_vec, ilen);
3461 sg_init_one(&dst, output, dlen);
3463 req = acomp_request_alloc(tfm);
3465 pr_err("alg: acomp: request alloc failed for %s\n",
3472 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3473 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3474 crypto_req_done, &wait);
3476 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
3478 pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
3481 acomp_request_free(req);
3485 if (req->dlen != dtemplate[i].outlen) {
3486 pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
3487 i + 1, algo, req->dlen);
3490 acomp_request_free(req);
3494 if (memcmp(output, dtemplate[i].output, req->dlen)) {
3495 pr_err("alg: acomp: Decompression test %d failed for %s\n",
3497 hexdump(output, req->dlen);
3500 acomp_request_free(req);
3504 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
3505 crypto_init_wait(&wait);
3506 acomp_request_set_params(req, &src, NULL, ilen, 0);
3508 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
3510 pr_err("alg: acomp: decompression failed on NULL dst buffer test %d for %s: ret=%d\n",
3513 acomp_request_free(req);
3519 acomp_request_free(req);
3530 static int test_cprng(struct crypto_rng *tfm,
3531 const struct cprng_testvec *template,
3532 unsigned int tcount)
3534 const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
3535 int err = 0, i, j, seedsize;
3539 seedsize = crypto_rng_seedsize(tfm);
3541 seed = kmalloc(seedsize, GFP_KERNEL);
3543 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
3548 for (i = 0; i < tcount; i++) {
3549 memset(result, 0, 32);
3551 memcpy(seed, template[i].v, template[i].vlen);
3552 memcpy(seed + template[i].vlen, template[i].key,
3554 memcpy(seed + template[i].vlen + template[i].klen,
3555 template[i].dt, template[i].dtlen);
3557 err = crypto_rng_reset(tfm, seed, seedsize);
3559 printk(KERN_ERR "alg: cprng: Failed to reset rng "
3564 for (j = 0; j < template[i].loops; j++) {
3565 err = crypto_rng_get_bytes(tfm, result,
3568 printk(KERN_ERR "alg: cprng: Failed to obtain "
3569 "the correct amount of random data for "
3570 "%s (requested %d)\n", algo,
3576 err = memcmp(result, template[i].result,
3579 printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
3581 hexdump(result, template[i].rlen);
3592 static int alg_test_cipher(const struct alg_test_desc *desc,
3593 const char *driver, u32 type, u32 mask)
3595 const struct cipher_test_suite *suite = &desc->suite.cipher;
3596 struct crypto_cipher *tfm;
3599 tfm = crypto_alloc_cipher(driver, type, mask);
3601 printk(KERN_ERR "alg: cipher: Failed to load transform for "
3602 "%s: %ld\n", driver, PTR_ERR(tfm));
3603 return PTR_ERR(tfm);
3606 err = test_cipher(tfm, ENCRYPT, suite->vecs, suite->count);
3608 err = test_cipher(tfm, DECRYPT, suite->vecs, suite->count);
3610 crypto_free_cipher(tfm);
3614 static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
3617 struct crypto_comp *comp;
3618 struct crypto_acomp *acomp;
3620 u32 algo_type = type & CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
3622 if (algo_type == CRYPTO_ALG_TYPE_ACOMPRESS) {
3623 acomp = crypto_alloc_acomp(driver, type, mask);
3624 if (IS_ERR(acomp)) {
3625 pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
3626 driver, PTR_ERR(acomp));
3627 return PTR_ERR(acomp);
3629 err = test_acomp(acomp, desc->suite.comp.comp.vecs,
3630 desc->suite.comp.decomp.vecs,
3631 desc->suite.comp.comp.count,
3632 desc->suite.comp.decomp.count);
3633 crypto_free_acomp(acomp);
3635 comp = crypto_alloc_comp(driver, type, mask);
3637 pr_err("alg: comp: Failed to load transform for %s: %ld\n",
3638 driver, PTR_ERR(comp));
3639 return PTR_ERR(comp);
3642 err = test_comp(comp, desc->suite.comp.comp.vecs,
3643 desc->suite.comp.decomp.vecs,
3644 desc->suite.comp.comp.count,
3645 desc->suite.comp.decomp.count);
3647 crypto_free_comp(comp);
3652 static int alg_test_crc32c(const struct alg_test_desc *desc,
3653 const char *driver, u32 type, u32 mask)
3655 struct crypto_shash *tfm;
3659 err = alg_test_hash(desc, driver, type, mask);
3663 tfm = crypto_alloc_shash(driver, type, mask);
3665 if (PTR_ERR(tfm) == -ENOENT) {
3667 * This crc32c implementation is only available through
3668 * ahash API, not the shash API, so the remaining part
3669 * of the test is not applicable to it.
3673 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
3674 "%ld\n", driver, PTR_ERR(tfm));
3675 return PTR_ERR(tfm);
3677 driver = crypto_shash_driver_name(tfm);
3680 SHASH_DESC_ON_STACK(shash, tfm);
3681 u32 *ctx = (u32 *)shash_desc_ctx(shash);
3686 err = crypto_shash_final(shash, (u8 *)&val);
3688 printk(KERN_ERR "alg: crc32c: Operation failed for "
3689 "%s: %d\n", driver, err);
3693 if (val != cpu_to_le32(~420553207)) {
3694 pr_err("alg: crc32c: Test failed for %s: %u\n",
3695 driver, le32_to_cpu(val));
3700 crypto_free_shash(tfm);
3705 static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
3708 struct crypto_rng *rng;
3711 rng = crypto_alloc_rng(driver, type, mask);
3713 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
3714 "%ld\n", driver, PTR_ERR(rng));
3715 return PTR_ERR(rng);
3718 err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
3720 crypto_free_rng(rng);
3726 static int drbg_cavs_test(const struct drbg_testvec *test, int pr,
3727 const char *driver, u32 type, u32 mask)
3730 struct crypto_rng *drng;
3731 struct drbg_test_data test_data;
3732 struct drbg_string addtl, pers, testentropy;
3733 unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
3738 drng = crypto_alloc_rng(driver, type, mask);
3740 printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
3742 kfree_sensitive(buf);
3746 test_data.testentropy = &testentropy;
3747 drbg_string_fill(&testentropy, test->entropy, test->entropylen);
3748 drbg_string_fill(&pers, test->pers, test->perslen);
3749 ret = crypto_drbg_reset_test(drng, &pers, &test_data);
3751 printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
3755 drbg_string_fill(&addtl, test->addtla, test->addtllen);
3757 drbg_string_fill(&testentropy, test->entpra, test->entprlen);
3758 ret = crypto_drbg_get_bytes_addtl_test(drng,
3759 buf, test->expectedlen, &addtl, &test_data);
3761 ret = crypto_drbg_get_bytes_addtl(drng,
3762 buf, test->expectedlen, &addtl);
3765 printk(KERN_ERR "alg: drbg: could not obtain random data for "
3766 "driver %s\n", driver);
3770 drbg_string_fill(&addtl, test->addtlb, test->addtllen);
3772 drbg_string_fill(&testentropy, test->entprb, test->entprlen);
3773 ret = crypto_drbg_get_bytes_addtl_test(drng,
3774 buf, test->expectedlen, &addtl, &test_data);
3776 ret = crypto_drbg_get_bytes_addtl(drng,
3777 buf, test->expectedlen, &addtl);
3780 printk(KERN_ERR "alg: drbg: could not obtain random data for "
3781 "driver %s\n", driver);
3785 ret = memcmp(test->expected, buf, test->expectedlen);
3788 crypto_free_rng(drng);
3789 kfree_sensitive(buf);
3794 static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
3800 const struct drbg_testvec *template = desc->suite.drbg.vecs;
3801 unsigned int tcount = desc->suite.drbg.count;
3803 if (0 == memcmp(driver, "drbg_pr_", 8))
3806 for (i = 0; i < tcount; i++) {
3807 err = drbg_cavs_test(&template[i], pr, driver, type, mask);
3809 printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
3819 static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
3822 struct kpp_request *req;
3823 void *input_buf = NULL;
3824 void *output_buf = NULL;
3825 void *a_public = NULL;
3827 void *shared_secret = NULL;
3828 struct crypto_wait wait;
3829 unsigned int out_len_max;
3831 struct scatterlist src, dst;
3833 req = kpp_request_alloc(tfm, GFP_KERNEL);
3837 crypto_init_wait(&wait);
3839 err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
3843 out_len_max = crypto_kpp_maxsize(tfm);
3844 output_buf = kzalloc(out_len_max, GFP_KERNEL);
3850 /* Use appropriate parameter as base */
3851 kpp_request_set_input(req, NULL, 0);
3852 sg_init_one(&dst, output_buf, out_len_max);
3853 kpp_request_set_output(req, &dst, out_len_max);
3854 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3855 crypto_req_done, &wait);
3857 /* Compute party A's public key */
3858 err = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
3860 pr_err("alg: %s: Party A: generate public key test failed. err %d\n",
3866 /* Save party A's public key */
3867 a_public = kmemdup(sg_virt(req->dst), out_len_max, GFP_KERNEL);
3873 /* Verify calculated public key */
3874 if (memcmp(vec->expected_a_public, sg_virt(req->dst),
3875 vec->expected_a_public_size)) {
3876 pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n",
3883 /* Calculate shared secret key by using counter part (b) public key. */
3884 input_buf = kmemdup(vec->b_public, vec->b_public_size, GFP_KERNEL);
3890 sg_init_one(&src, input_buf, vec->b_public_size);
3891 sg_init_one(&dst, output_buf, out_len_max);
3892 kpp_request_set_input(req, &src, vec->b_public_size);
3893 kpp_request_set_output(req, &dst, out_len_max);
3894 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3895 crypto_req_done, &wait);
3896 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait);
3898 pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n",
3904 /* Save the shared secret obtained by party A */
3905 a_ss = kmemdup(sg_virt(req->dst), vec->expected_ss_size, GFP_KERNEL);
3912 * Calculate party B's shared secret by using party A's
3915 err = crypto_kpp_set_secret(tfm, vec->b_secret,
3916 vec->b_secret_size);
3920 sg_init_one(&src, a_public, vec->expected_a_public_size);
3921 sg_init_one(&dst, output_buf, out_len_max);
3922 kpp_request_set_input(req, &src, vec->expected_a_public_size);
3923 kpp_request_set_output(req, &dst, out_len_max);
3924 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3925 crypto_req_done, &wait);
3926 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req),
3929 pr_err("alg: %s: Party B: compute shared secret failed. err %d\n",
3934 shared_secret = a_ss;
3936 shared_secret = (void *)vec->expected_ss;
3940 * verify shared secret from which the user will derive
3941 * secret key by executing whatever hash it has chosen
3943 if (memcmp(shared_secret, sg_virt(req->dst),
3944 vec->expected_ss_size)) {
3945 pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
3957 kpp_request_free(req);
3961 static int test_kpp(struct crypto_kpp *tfm, const char *alg,
3962 const struct kpp_testvec *vecs, unsigned int tcount)
3966 for (i = 0; i < tcount; i++) {
3967 ret = do_test_kpp(tfm, vecs++, alg);
3969 pr_err("alg: %s: test failed on vector %d, err=%d\n",
3977 static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
3980 struct crypto_kpp *tfm;
3983 tfm = crypto_alloc_kpp(driver, type, mask);
3985 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
3986 driver, PTR_ERR(tfm));
3987 return PTR_ERR(tfm);
3989 if (desc->suite.kpp.vecs)
3990 err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
3991 desc->suite.kpp.count);
3993 crypto_free_kpp(tfm);
3997 static u8 *test_pack_u32(u8 *dst, u32 val)
3999 memcpy(dst, &val, sizeof(val));
4000 return dst + sizeof(val);
4003 static int test_akcipher_one(struct crypto_akcipher *tfm,
4004 const struct akcipher_testvec *vecs)
4006 char *xbuf[XBUFSIZE];
4007 struct akcipher_request *req;
4008 void *outbuf_enc = NULL;
4009 void *outbuf_dec = NULL;
4010 struct crypto_wait wait;
4011 unsigned int out_len_max, out_len = 0;
4013 struct scatterlist src, dst, src_tab[3];
4015 unsigned int m_size, c_size;
4019 if (testmgr_alloc_buf(xbuf))
4022 req = akcipher_request_alloc(tfm, GFP_KERNEL);
4026 crypto_init_wait(&wait);
4028 key = kmalloc(vecs->key_len + sizeof(u32) * 2 + vecs->param_len,
4032 memcpy(key, vecs->key, vecs->key_len);
4033 ptr = key + vecs->key_len;
4034 ptr = test_pack_u32(ptr, vecs->algo);
4035 ptr = test_pack_u32(ptr, vecs->param_len);
4036 memcpy(ptr, vecs->params, vecs->param_len);
4038 if (vecs->public_key_vec)
4039 err = crypto_akcipher_set_pub_key(tfm, key, vecs->key_len);
4041 err = crypto_akcipher_set_priv_key(tfm, key, vecs->key_len);
4046 * First run test which do not require a private key, such as
4047 * encrypt or verify.
4050 out_len_max = crypto_akcipher_maxsize(tfm);
4051 outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
4055 if (!vecs->siggen_sigver_test) {
4057 m_size = vecs->m_size;
4059 c_size = vecs->c_size;
4062 /* Swap args so we could keep plaintext (digest)
4063 * in vecs->m, and cooked signature in vecs->c.
4065 m = vecs->c; /* signature */
4066 m_size = vecs->c_size;
4067 c = vecs->m; /* digest */
4068 c_size = vecs->m_size;
4073 if (WARN_ON(m_size > PAGE_SIZE))
4075 memcpy(xbuf[0], m, m_size);
4077 sg_init_table(src_tab, 3);
4078 sg_set_buf(&src_tab[0], xbuf[0], 8);
4079 sg_set_buf(&src_tab[1], xbuf[0] + 8, m_size - 8);
4080 if (vecs->siggen_sigver_test) {
4081 if (WARN_ON(c_size > PAGE_SIZE))
4083 memcpy(xbuf[1], c, c_size);
4084 sg_set_buf(&src_tab[2], xbuf[1], c_size);
4085 akcipher_request_set_crypt(req, src_tab, NULL, m_size, c_size);
4087 sg_init_one(&dst, outbuf_enc, out_len_max);
4088 akcipher_request_set_crypt(req, src_tab, &dst, m_size,
4091 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
4092 crypto_req_done, &wait);
4094 err = crypto_wait_req(vecs->siggen_sigver_test ?
4095 /* Run asymmetric signature verification */
4096 crypto_akcipher_verify(req) :
4097 /* Run asymmetric encrypt */
4098 crypto_akcipher_encrypt(req), &wait);
4100 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
4103 if (!vecs->siggen_sigver_test && c) {
4104 if (req->dst_len != c_size) {
4105 pr_err("alg: akcipher: %s test failed. Invalid output len\n",
4110 /* verify that encrypted message is equal to expected */
4111 if (memcmp(c, outbuf_enc, c_size) != 0) {
4112 pr_err("alg: akcipher: %s test failed. Invalid output\n",
4114 hexdump(outbuf_enc, c_size);
4121 * Don't invoke (decrypt or sign) test which require a private key
4122 * for vectors with only a public key.
4124 if (vecs->public_key_vec) {
4128 outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
4134 if (!vecs->siggen_sigver_test && !c) {
4136 c_size = req->dst_len;
4140 op = vecs->siggen_sigver_test ? "sign" : "decrypt";
4141 if (WARN_ON(c_size > PAGE_SIZE))
4143 memcpy(xbuf[0], c, c_size);
4145 sg_init_one(&src, xbuf[0], c_size);
4146 sg_init_one(&dst, outbuf_dec, out_len_max);
4147 crypto_init_wait(&wait);
4148 akcipher_request_set_crypt(req, &src, &dst, c_size, out_len_max);
4150 err = crypto_wait_req(vecs->siggen_sigver_test ?
4151 /* Run asymmetric signature generation */
4152 crypto_akcipher_sign(req) :
4153 /* Run asymmetric decrypt */
4154 crypto_akcipher_decrypt(req), &wait);
4156 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
4159 out_len = req->dst_len;
4160 if (out_len < m_size) {
4161 pr_err("alg: akcipher: %s test failed. Invalid output len %u\n",
4166 /* verify that decrypted message is equal to the original msg */
4167 if (memchr_inv(outbuf_dec, 0, out_len - m_size) ||
4168 memcmp(m, outbuf_dec + out_len - m_size, m_size)) {
4169 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
4170 hexdump(outbuf_dec, out_len);
4179 akcipher_request_free(req);
4181 testmgr_free_buf(xbuf);
4185 static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
4186 const struct akcipher_testvec *vecs,
4187 unsigned int tcount)
4190 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
4193 for (i = 0; i < tcount; i++) {
4194 ret = test_akcipher_one(tfm, vecs++);
4198 pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
4205 static int alg_test_akcipher(const struct alg_test_desc *desc,
4206 const char *driver, u32 type, u32 mask)
4208 struct crypto_akcipher *tfm;
4211 tfm = crypto_alloc_akcipher(driver, type, mask);
4213 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
4214 driver, PTR_ERR(tfm));
4215 return PTR_ERR(tfm);
4217 if (desc->suite.akcipher.vecs)
4218 err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
4219 desc->suite.akcipher.count);
4221 crypto_free_akcipher(tfm);
4225 static int alg_test_null(const struct alg_test_desc *desc,
4226 const char *driver, u32 type, u32 mask)
4231 #define ____VECS(tv) .vecs = tv, .count = ARRAY_SIZE(tv)
4232 #define __VECS(tv) { ____VECS(tv) }
4234 /* Please keep this list sorted by algorithm name. */
4235 static const struct alg_test_desc alg_test_descs[] = {
4237 .alg = "adiantum(xchacha12,aes)",
4238 .generic_driver = "adiantum(xchacha12-generic,aes-generic,nhpoly1305-generic)",
4239 .test = alg_test_skcipher,
4241 .cipher = __VECS(adiantum_xchacha12_aes_tv_template)
4244 .alg = "adiantum(xchacha20,aes)",
4245 .generic_driver = "adiantum(xchacha20-generic,aes-generic,nhpoly1305-generic)",
4246 .test = alg_test_skcipher,
4248 .cipher = __VECS(adiantum_xchacha20_aes_tv_template)
4252 .test = alg_test_aead,
4254 .aead = __VECS(aegis128_tv_template)
4257 .alg = "ansi_cprng",
4258 .test = alg_test_cprng,
4260 .cprng = __VECS(ansi_cprng_aes_tv_template)
4263 .alg = "authenc(hmac(md5),ecb(cipher_null))",
4264 .test = alg_test_aead,
4266 .aead = __VECS(hmac_md5_ecb_cipher_null_tv_template)
4269 .alg = "authenc(hmac(sha1),cbc(aes))",
4270 .test = alg_test_aead,
4273 .aead = __VECS(hmac_sha1_aes_cbc_tv_temp)
4276 .alg = "authenc(hmac(sha1),cbc(des))",
4277 .test = alg_test_aead,
4279 .aead = __VECS(hmac_sha1_des_cbc_tv_temp)
4282 .alg = "authenc(hmac(sha1),cbc(des3_ede))",
4283 .test = alg_test_aead,
4285 .aead = __VECS(hmac_sha1_des3_ede_cbc_tv_temp)
4288 .alg = "authenc(hmac(sha1),ctr(aes))",
4289 .test = alg_test_null,
4292 .alg = "authenc(hmac(sha1),ecb(cipher_null))",
4293 .test = alg_test_aead,
4295 .aead = __VECS(hmac_sha1_ecb_cipher_null_tv_temp)
4298 .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
4299 .test = alg_test_null,
4302 .alg = "authenc(hmac(sha224),cbc(des))",
4303 .test = alg_test_aead,
4305 .aead = __VECS(hmac_sha224_des_cbc_tv_temp)
4308 .alg = "authenc(hmac(sha224),cbc(des3_ede))",
4309 .test = alg_test_aead,
4311 .aead = __VECS(hmac_sha224_des3_ede_cbc_tv_temp)
4314 .alg = "authenc(hmac(sha256),cbc(aes))",
4315 .test = alg_test_aead,
4318 .aead = __VECS(hmac_sha256_aes_cbc_tv_temp)
4321 .alg = "authenc(hmac(sha256),cbc(des))",
4322 .test = alg_test_aead,
4324 .aead = __VECS(hmac_sha256_des_cbc_tv_temp)
4327 .alg = "authenc(hmac(sha256),cbc(des3_ede))",
4328 .test = alg_test_aead,
4330 .aead = __VECS(hmac_sha256_des3_ede_cbc_tv_temp)
4333 .alg = "authenc(hmac(sha256),ctr(aes))",
4334 .test = alg_test_null,
4337 .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
4338 .test = alg_test_null,
4341 .alg = "authenc(hmac(sha384),cbc(des))",
4342 .test = alg_test_aead,
4344 .aead = __VECS(hmac_sha384_des_cbc_tv_temp)
4347 .alg = "authenc(hmac(sha384),cbc(des3_ede))",
4348 .test = alg_test_aead,
4350 .aead = __VECS(hmac_sha384_des3_ede_cbc_tv_temp)
4353 .alg = "authenc(hmac(sha384),ctr(aes))",
4354 .test = alg_test_null,
4357 .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
4358 .test = alg_test_null,
4361 .alg = "authenc(hmac(sha512),cbc(aes))",
4363 .test = alg_test_aead,
4365 .aead = __VECS(hmac_sha512_aes_cbc_tv_temp)
4368 .alg = "authenc(hmac(sha512),cbc(des))",
4369 .test = alg_test_aead,
4371 .aead = __VECS(hmac_sha512_des_cbc_tv_temp)
4374 .alg = "authenc(hmac(sha512),cbc(des3_ede))",
4375 .test = alg_test_aead,
4377 .aead = __VECS(hmac_sha512_des3_ede_cbc_tv_temp)
4380 .alg = "authenc(hmac(sha512),ctr(aes))",
4381 .test = alg_test_null,
4384 .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
4385 .test = alg_test_null,
4388 .alg = "blake2b-160",
4389 .test = alg_test_hash,
4392 .hash = __VECS(blake2b_160_tv_template)
4395 .alg = "blake2b-256",
4396 .test = alg_test_hash,
4399 .hash = __VECS(blake2b_256_tv_template)
4402 .alg = "blake2b-384",
4403 .test = alg_test_hash,
4406 .hash = __VECS(blake2b_384_tv_template)
4409 .alg = "blake2b-512",
4410 .test = alg_test_hash,
4413 .hash = __VECS(blake2b_512_tv_template)
4417 .test = alg_test_skcipher,
4420 .cipher = __VECS(aes_cbc_tv_template)
4423 .alg = "cbc(anubis)",
4424 .test = alg_test_skcipher,
4426 .cipher = __VECS(anubis_cbc_tv_template)
4430 .test = alg_test_skcipher,
4432 .cipher = __VECS(aria_cbc_tv_template)
4435 .alg = "cbc(blowfish)",
4436 .test = alg_test_skcipher,
4438 .cipher = __VECS(bf_cbc_tv_template)
4441 .alg = "cbc(camellia)",
4442 .test = alg_test_skcipher,
4444 .cipher = __VECS(camellia_cbc_tv_template)
4447 .alg = "cbc(cast5)",
4448 .test = alg_test_skcipher,
4450 .cipher = __VECS(cast5_cbc_tv_template)
4453 .alg = "cbc(cast6)",
4454 .test = alg_test_skcipher,
4456 .cipher = __VECS(cast6_cbc_tv_template)
4460 .test = alg_test_skcipher,
4462 .cipher = __VECS(des_cbc_tv_template)
4465 .alg = "cbc(des3_ede)",
4466 .test = alg_test_skcipher,
4468 .cipher = __VECS(des3_ede_cbc_tv_template)
4471 /* Same as cbc(aes) except the key is stored in
4472 * hardware secure memory which we reference by index
4475 .test = alg_test_null,
4478 /* Same as cbc(sm4) except the key is stored in
4479 * hardware secure memory which we reference by index
4482 .test = alg_test_null,
4484 .alg = "cbc(serpent)",
4485 .test = alg_test_skcipher,
4487 .cipher = __VECS(serpent_cbc_tv_template)
4491 .test = alg_test_skcipher,
4493 .cipher = __VECS(sm4_cbc_tv_template)
4496 .alg = "cbc(twofish)",
4497 .test = alg_test_skcipher,
4499 .cipher = __VECS(tf_cbc_tv_template)
4502 #if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
4503 .alg = "cbc-paes-s390",
4505 .test = alg_test_skcipher,
4507 .cipher = __VECS(aes_cbc_tv_template)
4511 .alg = "cbcmac(aes)",
4512 .test = alg_test_hash,
4514 .hash = __VECS(aes_cbcmac_tv_template)
4517 .alg = "cbcmac(sm4)",
4518 .test = alg_test_hash,
4520 .hash = __VECS(sm4_cbcmac_tv_template)
4524 .generic_driver = "ccm_base(ctr(aes-generic),cbcmac(aes-generic))",
4525 .test = alg_test_aead,
4529 ____VECS(aes_ccm_tv_template),
4530 .einval_allowed = 1,
4535 .generic_driver = "ccm_base(ctr(sm4-generic),cbcmac(sm4-generic))",
4536 .test = alg_test_aead,
4539 ____VECS(sm4_ccm_tv_template),
4540 .einval_allowed = 1,
4545 .test = alg_test_skcipher,
4548 .cipher = __VECS(aes_cfb_tv_template)
4552 .test = alg_test_skcipher,
4554 .cipher = __VECS(aria_cfb_tv_template)
4558 .test = alg_test_skcipher,
4560 .cipher = __VECS(sm4_cfb_tv_template)
4564 .test = alg_test_skcipher,
4566 .cipher = __VECS(chacha20_tv_template)
4571 .test = alg_test_hash,
4573 .hash = __VECS(aes_cmac128_tv_template)
4576 .alg = "cmac(des3_ede)",
4577 .test = alg_test_hash,
4579 .hash = __VECS(des3_ede_cmac64_tv_template)
4583 .test = alg_test_hash,
4585 .hash = __VECS(sm4_cmac128_tv_template)
4588 .alg = "compress_null",
4589 .test = alg_test_null,
4592 .test = alg_test_hash,
4595 .hash = __VECS(crc32_tv_template)
4599 .test = alg_test_crc32c,
4602 .hash = __VECS(crc32c_tv_template)
4605 .alg = "crc64-rocksoft",
4606 .test = alg_test_hash,
4609 .hash = __VECS(crc64_rocksoft_tv_template)
4613 .test = alg_test_hash,
4616 .hash = __VECS(crct10dif_tv_template)
4620 .test = alg_test_skcipher,
4623 .cipher = __VECS(aes_ctr_tv_template)
4627 .test = alg_test_skcipher,
4629 .cipher = __VECS(aria_ctr_tv_template)
4632 .alg = "ctr(blowfish)",
4633 .test = alg_test_skcipher,
4635 .cipher = __VECS(bf_ctr_tv_template)
4638 .alg = "ctr(camellia)",
4639 .test = alg_test_skcipher,
4641 .cipher = __VECS(camellia_ctr_tv_template)
4644 .alg = "ctr(cast5)",
4645 .test = alg_test_skcipher,
4647 .cipher = __VECS(cast5_ctr_tv_template)
4650 .alg = "ctr(cast6)",
4651 .test = alg_test_skcipher,
4653 .cipher = __VECS(cast6_ctr_tv_template)
4657 .test = alg_test_skcipher,
4659 .cipher = __VECS(des_ctr_tv_template)
4662 .alg = "ctr(des3_ede)",
4663 .test = alg_test_skcipher,
4665 .cipher = __VECS(des3_ede_ctr_tv_template)
4668 /* Same as ctr(aes) except the key is stored in
4669 * hardware secure memory which we reference by index
4672 .test = alg_test_null,
4676 /* Same as ctr(sm4) except the key is stored in
4677 * hardware secure memory which we reference by index
4680 .test = alg_test_null,
4682 .alg = "ctr(serpent)",
4683 .test = alg_test_skcipher,
4685 .cipher = __VECS(serpent_ctr_tv_template)
4689 .test = alg_test_skcipher,
4691 .cipher = __VECS(sm4_ctr_tv_template)
4694 .alg = "ctr(twofish)",
4695 .test = alg_test_skcipher,
4697 .cipher = __VECS(tf_ctr_tv_template)
4700 #if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
4701 .alg = "ctr-paes-s390",
4703 .test = alg_test_skcipher,
4705 .cipher = __VECS(aes_ctr_tv_template)
4709 .alg = "cts(cbc(aes))",
4710 .test = alg_test_skcipher,
4713 .cipher = __VECS(cts_mode_tv_template)
4716 /* Same as cts(cbc((aes)) except the key is stored in
4717 * hardware secure memory which we reference by index
4719 .alg = "cts(cbc(paes))",
4720 .test = alg_test_null,
4723 .alg = "cts(cbc(sm4))",
4724 .test = alg_test_skcipher,
4726 .cipher = __VECS(sm4_cts_tv_template)
4729 .alg = "curve25519",
4730 .test = alg_test_kpp,
4732 .kpp = __VECS(curve25519_tv_template)
4736 .test = alg_test_comp,
4740 .comp = __VECS(deflate_comp_tv_template),
4741 .decomp = __VECS(deflate_decomp_tv_template)
4746 .test = alg_test_kpp,
4748 .kpp = __VECS(dh_tv_template)
4751 .alg = "digest_null",
4752 .test = alg_test_null,
4754 .alg = "drbg_nopr_ctr_aes128",
4755 .test = alg_test_drbg,
4758 .drbg = __VECS(drbg_nopr_ctr_aes128_tv_template)
4761 .alg = "drbg_nopr_ctr_aes192",
4762 .test = alg_test_drbg,
4765 .drbg = __VECS(drbg_nopr_ctr_aes192_tv_template)
4768 .alg = "drbg_nopr_ctr_aes256",
4769 .test = alg_test_drbg,
4772 .drbg = __VECS(drbg_nopr_ctr_aes256_tv_template)
4776 * There is no need to specifically test the DRBG with every
4777 * backend cipher -- covered by drbg_nopr_hmac_sha256 test
4779 .alg = "drbg_nopr_hmac_sha1",
4781 .test = alg_test_null,
4783 .alg = "drbg_nopr_hmac_sha256",
4784 .test = alg_test_drbg,
4787 .drbg = __VECS(drbg_nopr_hmac_sha256_tv_template)
4790 /* covered by drbg_nopr_hmac_sha256 test */
4791 .alg = "drbg_nopr_hmac_sha384",
4792 .test = alg_test_null,
4794 .alg = "drbg_nopr_hmac_sha512",
4795 .test = alg_test_drbg,
4798 .drbg = __VECS(drbg_nopr_hmac_sha512_tv_template)
4801 .alg = "drbg_nopr_sha1",
4803 .test = alg_test_null,
4805 .alg = "drbg_nopr_sha256",
4806 .test = alg_test_drbg,
4809 .drbg = __VECS(drbg_nopr_sha256_tv_template)
4812 /* covered by drbg_nopr_sha256 test */
4813 .alg = "drbg_nopr_sha384",
4814 .test = alg_test_null,
4816 .alg = "drbg_nopr_sha512",
4818 .test = alg_test_null,
4820 .alg = "drbg_pr_ctr_aes128",
4821 .test = alg_test_drbg,
4824 .drbg = __VECS(drbg_pr_ctr_aes128_tv_template)
4827 /* covered by drbg_pr_ctr_aes128 test */
4828 .alg = "drbg_pr_ctr_aes192",
4830 .test = alg_test_null,
4832 .alg = "drbg_pr_ctr_aes256",
4834 .test = alg_test_null,
4836 .alg = "drbg_pr_hmac_sha1",
4838 .test = alg_test_null,
4840 .alg = "drbg_pr_hmac_sha256",
4841 .test = alg_test_drbg,
4844 .drbg = __VECS(drbg_pr_hmac_sha256_tv_template)
4847 /* covered by drbg_pr_hmac_sha256 test */
4848 .alg = "drbg_pr_hmac_sha384",
4849 .test = alg_test_null,
4851 .alg = "drbg_pr_hmac_sha512",
4852 .test = alg_test_null,
4855 .alg = "drbg_pr_sha1",
4857 .test = alg_test_null,
4859 .alg = "drbg_pr_sha256",
4860 .test = alg_test_drbg,
4863 .drbg = __VECS(drbg_pr_sha256_tv_template)
4866 /* covered by drbg_pr_sha256 test */
4867 .alg = "drbg_pr_sha384",
4868 .test = alg_test_null,
4870 .alg = "drbg_pr_sha512",
4872 .test = alg_test_null,
4875 .test = alg_test_skcipher,
4878 .cipher = __VECS(aes_tv_template)
4881 .alg = "ecb(anubis)",
4882 .test = alg_test_skcipher,
4884 .cipher = __VECS(anubis_tv_template)
4888 .generic_driver = "ecb(arc4)-generic",
4889 .test = alg_test_skcipher,
4891 .cipher = __VECS(arc4_tv_template)
4895 .test = alg_test_skcipher,
4897 .cipher = __VECS(aria_tv_template)
4900 .alg = "ecb(blowfish)",
4901 .test = alg_test_skcipher,
4903 .cipher = __VECS(bf_tv_template)
4906 .alg = "ecb(camellia)",
4907 .test = alg_test_skcipher,
4909 .cipher = __VECS(camellia_tv_template)
4912 .alg = "ecb(cast5)",
4913 .test = alg_test_skcipher,
4915 .cipher = __VECS(cast5_tv_template)
4918 .alg = "ecb(cast6)",
4919 .test = alg_test_skcipher,
4921 .cipher = __VECS(cast6_tv_template)
4924 .alg = "ecb(cipher_null)",
4925 .test = alg_test_null,
4929 .test = alg_test_skcipher,
4931 .cipher = __VECS(des_tv_template)
4934 .alg = "ecb(des3_ede)",
4935 .test = alg_test_skcipher,
4937 .cipher = __VECS(des3_ede_tv_template)
4940 .alg = "ecb(fcrypt)",
4941 .test = alg_test_skcipher,
4944 .vecs = fcrypt_pcbc_tv_template,
4949 .alg = "ecb(khazad)",
4950 .test = alg_test_skcipher,
4952 .cipher = __VECS(khazad_tv_template)
4955 /* Same as ecb(aes) except the key is stored in
4956 * hardware secure memory which we reference by index
4959 .test = alg_test_null,
4963 .test = alg_test_skcipher,
4965 .cipher = __VECS(seed_tv_template)
4968 .alg = "ecb(serpent)",
4969 .test = alg_test_skcipher,
4971 .cipher = __VECS(serpent_tv_template)
4975 .test = alg_test_skcipher,
4977 .cipher = __VECS(sm4_tv_template)
4981 .test = alg_test_skcipher,
4983 .cipher = __VECS(tea_tv_template)
4986 .alg = "ecb(twofish)",
4987 .test = alg_test_skcipher,
4989 .cipher = __VECS(tf_tv_template)
4993 .test = alg_test_skcipher,
4995 .cipher = __VECS(xeta_tv_template)
4999 .test = alg_test_skcipher,
5001 .cipher = __VECS(xtea_tv_template)
5004 #if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
5005 .alg = "ecb-paes-s390",
5007 .test = alg_test_skcipher,
5009 .cipher = __VECS(aes_tv_template)
5013 .alg = "ecdh-nist-p192",
5014 .test = alg_test_kpp,
5016 .kpp = __VECS(ecdh_p192_tv_template)
5019 .alg = "ecdh-nist-p256",
5020 .test = alg_test_kpp,
5023 .kpp = __VECS(ecdh_p256_tv_template)
5026 .alg = "ecdh-nist-p384",
5027 .test = alg_test_kpp,
5030 .kpp = __VECS(ecdh_p384_tv_template)
5033 .alg = "ecdsa-nist-p192",
5034 .test = alg_test_akcipher,
5036 .akcipher = __VECS(ecdsa_nist_p192_tv_template)
5039 .alg = "ecdsa-nist-p256",
5040 .test = alg_test_akcipher,
5043 .akcipher = __VECS(ecdsa_nist_p256_tv_template)
5046 .alg = "ecdsa-nist-p384",
5047 .test = alg_test_akcipher,
5050 .akcipher = __VECS(ecdsa_nist_p384_tv_template)
5054 .test = alg_test_akcipher,
5056 .akcipher = __VECS(ecrdsa_tv_template)
5059 .alg = "essiv(authenc(hmac(sha256),cbc(aes)),sha256)",
5060 .test = alg_test_aead,
5063 .aead = __VECS(essiv_hmac_sha256_aes_cbc_tv_temp)
5066 .alg = "essiv(cbc(aes),sha256)",
5067 .test = alg_test_skcipher,
5070 .cipher = __VECS(essiv_aes_cbc_tv_template)
5073 #if IS_ENABLED(CONFIG_CRYPTO_DH_RFC7919_GROUPS)
5074 .alg = "ffdhe2048(dh)",
5075 .test = alg_test_kpp,
5078 .kpp = __VECS(ffdhe2048_dh_tv_template)
5081 .alg = "ffdhe3072(dh)",
5082 .test = alg_test_kpp,
5085 .kpp = __VECS(ffdhe3072_dh_tv_template)
5088 .alg = "ffdhe4096(dh)",
5089 .test = alg_test_kpp,
5092 .kpp = __VECS(ffdhe4096_dh_tv_template)
5095 .alg = "ffdhe6144(dh)",
5096 .test = alg_test_kpp,
5099 .kpp = __VECS(ffdhe6144_dh_tv_template)
5102 .alg = "ffdhe8192(dh)",
5103 .test = alg_test_kpp,
5106 .kpp = __VECS(ffdhe8192_dh_tv_template)
5109 #endif /* CONFIG_CRYPTO_DH_RFC7919_GROUPS */
5111 .generic_driver = "gcm_base(ctr(aes-generic),ghash-generic)",
5112 .test = alg_test_aead,
5115 .aead = __VECS(aes_gcm_tv_template)
5119 .generic_driver = "gcm_base(ctr(aria-generic),ghash-generic)",
5120 .test = alg_test_aead,
5122 .aead = __VECS(aria_gcm_tv_template)
5126 .generic_driver = "gcm_base(ctr(sm4-generic),ghash-generic)",
5127 .test = alg_test_aead,
5129 .aead = __VECS(sm4_gcm_tv_template)
5133 .test = alg_test_hash,
5135 .hash = __VECS(ghash_tv_template)
5138 .alg = "hctr2(aes)",
5140 "hctr2_base(xctr(aes-generic),polyval-generic)",
5141 .test = alg_test_skcipher,
5143 .cipher = __VECS(aes_hctr2_tv_template)
5147 .test = alg_test_hash,
5149 .hash = __VECS(hmac_md5_tv_template)
5152 .alg = "hmac(rmd160)",
5153 .test = alg_test_hash,
5155 .hash = __VECS(hmac_rmd160_tv_template)
5158 .alg = "hmac(sha1)",
5159 .test = alg_test_hash,
5162 .hash = __VECS(hmac_sha1_tv_template)
5165 .alg = "hmac(sha224)",
5166 .test = alg_test_hash,
5169 .hash = __VECS(hmac_sha224_tv_template)
5172 .alg = "hmac(sha256)",
5173 .test = alg_test_hash,
5176 .hash = __VECS(hmac_sha256_tv_template)
5179 .alg = "hmac(sha3-224)",
5180 .test = alg_test_hash,
5183 .hash = __VECS(hmac_sha3_224_tv_template)
5186 .alg = "hmac(sha3-256)",
5187 .test = alg_test_hash,
5190 .hash = __VECS(hmac_sha3_256_tv_template)
5193 .alg = "hmac(sha3-384)",
5194 .test = alg_test_hash,
5197 .hash = __VECS(hmac_sha3_384_tv_template)
5200 .alg = "hmac(sha3-512)",
5201 .test = alg_test_hash,
5204 .hash = __VECS(hmac_sha3_512_tv_template)
5207 .alg = "hmac(sha384)",
5208 .test = alg_test_hash,
5211 .hash = __VECS(hmac_sha384_tv_template)
5214 .alg = "hmac(sha512)",
5215 .test = alg_test_hash,
5218 .hash = __VECS(hmac_sha512_tv_template)
5222 .test = alg_test_hash,
5224 .hash = __VECS(hmac_sm3_tv_template)
5227 .alg = "hmac(streebog256)",
5228 .test = alg_test_hash,
5230 .hash = __VECS(hmac_streebog256_tv_template)
5233 .alg = "hmac(streebog512)",
5234 .test = alg_test_hash,
5236 .hash = __VECS(hmac_streebog512_tv_template)
5239 .alg = "jitterentropy_rng",
5241 .test = alg_test_null,
5244 .test = alg_test_skcipher,
5247 .cipher = __VECS(aes_kw_tv_template)
5251 .generic_driver = "lrw(ecb(aes-generic))",
5252 .test = alg_test_skcipher,
5254 .cipher = __VECS(aes_lrw_tv_template)
5257 .alg = "lrw(camellia)",
5258 .generic_driver = "lrw(ecb(camellia-generic))",
5259 .test = alg_test_skcipher,
5261 .cipher = __VECS(camellia_lrw_tv_template)
5264 .alg = "lrw(cast6)",
5265 .generic_driver = "lrw(ecb(cast6-generic))",
5266 .test = alg_test_skcipher,
5268 .cipher = __VECS(cast6_lrw_tv_template)
5271 .alg = "lrw(serpent)",
5272 .generic_driver = "lrw(ecb(serpent-generic))",
5273 .test = alg_test_skcipher,
5275 .cipher = __VECS(serpent_lrw_tv_template)
5278 .alg = "lrw(twofish)",
5279 .generic_driver = "lrw(ecb(twofish-generic))",
5280 .test = alg_test_skcipher,
5282 .cipher = __VECS(tf_lrw_tv_template)
5286 .test = alg_test_comp,
5290 .comp = __VECS(lz4_comp_tv_template),
5291 .decomp = __VECS(lz4_decomp_tv_template)
5296 .test = alg_test_comp,
5300 .comp = __VECS(lz4hc_comp_tv_template),
5301 .decomp = __VECS(lz4hc_decomp_tv_template)
5306 .test = alg_test_comp,
5310 .comp = __VECS(lzo_comp_tv_template),
5311 .decomp = __VECS(lzo_decomp_tv_template)
5316 .test = alg_test_comp,
5320 .comp = __VECS(lzorle_comp_tv_template),
5321 .decomp = __VECS(lzorle_decomp_tv_template)
5326 .test = alg_test_hash,
5328 .hash = __VECS(md4_tv_template)
5332 .test = alg_test_hash,
5334 .hash = __VECS(md5_tv_template)
5337 .alg = "michael_mic",
5338 .test = alg_test_hash,
5340 .hash = __VECS(michael_mic_tv_template)
5343 .alg = "nhpoly1305",
5344 .test = alg_test_hash,
5346 .hash = __VECS(nhpoly1305_tv_template)
5350 .test = alg_test_skcipher,
5353 .cipher = __VECS(aes_ofb_tv_template)
5356 /* Same as ofb(aes) except the key is stored in
5357 * hardware secure memory which we reference by index
5360 .test = alg_test_null,
5364 .test = alg_test_skcipher,
5366 .cipher = __VECS(sm4_ofb_tv_template)
5369 .alg = "pcbc(fcrypt)",
5370 .test = alg_test_skcipher,
5372 .cipher = __VECS(fcrypt_pcbc_tv_template)
5375 .alg = "pkcs1pad(rsa,sha224)",
5376 .test = alg_test_null,
5379 .alg = "pkcs1pad(rsa,sha256)",
5380 .test = alg_test_akcipher,
5383 .akcipher = __VECS(pkcs1pad_rsa_tv_template)
5386 .alg = "pkcs1pad(rsa,sha384)",
5387 .test = alg_test_null,
5390 .alg = "pkcs1pad(rsa,sha512)",
5391 .test = alg_test_null,
5395 .test = alg_test_hash,
5397 .hash = __VECS(poly1305_tv_template)
5401 .test = alg_test_hash,
5403 .hash = __VECS(polyval_tv_template)
5406 .alg = "rfc3686(ctr(aes))",
5407 .test = alg_test_skcipher,
5410 .cipher = __VECS(aes_ctr_rfc3686_tv_template)
5413 .alg = "rfc3686(ctr(sm4))",
5414 .test = alg_test_skcipher,
5416 .cipher = __VECS(sm4_ctr_rfc3686_tv_template)
5419 .alg = "rfc4106(gcm(aes))",
5420 .generic_driver = "rfc4106(gcm_base(ctr(aes-generic),ghash-generic))",
5421 .test = alg_test_aead,
5425 ____VECS(aes_gcm_rfc4106_tv_template),
5426 .einval_allowed = 1,
5431 .alg = "rfc4309(ccm(aes))",
5432 .generic_driver = "rfc4309(ccm_base(ctr(aes-generic),cbcmac(aes-generic)))",
5433 .test = alg_test_aead,
5437 ____VECS(aes_ccm_rfc4309_tv_template),
5438 .einval_allowed = 1,
5443 .alg = "rfc4543(gcm(aes))",
5444 .generic_driver = "rfc4543(gcm_base(ctr(aes-generic),ghash-generic))",
5445 .test = alg_test_aead,
5448 ____VECS(aes_gcm_rfc4543_tv_template),
5449 .einval_allowed = 1,
5454 .alg = "rfc7539(chacha20,poly1305)",
5455 .test = alg_test_aead,
5457 .aead = __VECS(rfc7539_tv_template)
5460 .alg = "rfc7539esp(chacha20,poly1305)",
5461 .test = alg_test_aead,
5464 ____VECS(rfc7539esp_tv_template),
5465 .einval_allowed = 1,
5471 .test = alg_test_hash,
5473 .hash = __VECS(rmd160_tv_template)
5477 .test = alg_test_akcipher,
5480 .akcipher = __VECS(rsa_tv_template)
5484 .test = alg_test_hash,
5487 .hash = __VECS(sha1_tv_template)
5491 .test = alg_test_hash,
5494 .hash = __VECS(sha224_tv_template)
5498 .test = alg_test_hash,
5501 .hash = __VECS(sha256_tv_template)
5505 .test = alg_test_hash,
5508 .hash = __VECS(sha3_224_tv_template)
5512 .test = alg_test_hash,
5515 .hash = __VECS(sha3_256_tv_template)
5519 .test = alg_test_hash,
5522 .hash = __VECS(sha3_384_tv_template)
5526 .test = alg_test_hash,
5529 .hash = __VECS(sha3_512_tv_template)
5533 .test = alg_test_hash,
5536 .hash = __VECS(sha384_tv_template)
5540 .test = alg_test_hash,
5543 .hash = __VECS(sha512_tv_template)
5547 .test = alg_test_akcipher,
5549 .akcipher = __VECS(sm2_tv_template)
5553 .test = alg_test_hash,
5555 .hash = __VECS(sm3_tv_template)
5558 .alg = "streebog256",
5559 .test = alg_test_hash,
5561 .hash = __VECS(streebog256_tv_template)
5564 .alg = "streebog512",
5565 .test = alg_test_hash,
5567 .hash = __VECS(streebog512_tv_template)
5570 .alg = "vmac64(aes)",
5571 .test = alg_test_hash,
5573 .hash = __VECS(vmac64_aes_tv_template)
5577 .test = alg_test_hash,
5579 .hash = __VECS(wp256_tv_template)
5583 .test = alg_test_hash,
5585 .hash = __VECS(wp384_tv_template)
5589 .test = alg_test_hash,
5591 .hash = __VECS(wp512_tv_template)
5595 .test = alg_test_hash,
5597 .hash = __VECS(aes_xcbc128_tv_template)
5601 .test = alg_test_hash,
5603 .hash = __VECS(sm4_xcbc128_tv_template)
5607 .test = alg_test_skcipher,
5609 .cipher = __VECS(xchacha12_tv_template)
5613 .test = alg_test_skcipher,
5615 .cipher = __VECS(xchacha20_tv_template)
5619 .test = alg_test_skcipher,
5621 .cipher = __VECS(aes_xctr_tv_template)
5625 .generic_driver = "xts(ecb(aes-generic))",
5626 .test = alg_test_skcipher,
5629 .cipher = __VECS(aes_xts_tv_template)
5632 .alg = "xts(camellia)",
5633 .generic_driver = "xts(ecb(camellia-generic))",
5634 .test = alg_test_skcipher,
5636 .cipher = __VECS(camellia_xts_tv_template)
5639 .alg = "xts(cast6)",
5640 .generic_driver = "xts(ecb(cast6-generic))",
5641 .test = alg_test_skcipher,
5643 .cipher = __VECS(cast6_xts_tv_template)
5646 /* Same as xts(aes) except the key is stored in
5647 * hardware secure memory which we reference by index
5650 .test = alg_test_null,
5653 .alg = "xts(serpent)",
5654 .generic_driver = "xts(ecb(serpent-generic))",
5655 .test = alg_test_skcipher,
5657 .cipher = __VECS(serpent_xts_tv_template)
5661 .generic_driver = "xts(ecb(sm4-generic))",
5662 .test = alg_test_skcipher,
5664 .cipher = __VECS(sm4_xts_tv_template)
5667 .alg = "xts(twofish)",
5668 .generic_driver = "xts(ecb(twofish-generic))",
5669 .test = alg_test_skcipher,
5671 .cipher = __VECS(tf_xts_tv_template)
5674 #if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
5675 .alg = "xts-paes-s390",
5677 .test = alg_test_skcipher,
5679 .cipher = __VECS(aes_xts_tv_template)
5683 .alg = "xts4096(paes)",
5684 .test = alg_test_null,
5687 .alg = "xts512(paes)",
5688 .test = alg_test_null,
5692 .test = alg_test_hash,
5695 .hash = __VECS(xxhash64_tv_template)
5698 .alg = "zlib-deflate",
5699 .test = alg_test_comp,
5703 .comp = __VECS(zlib_deflate_comp_tv_template),
5704 .decomp = __VECS(zlib_deflate_decomp_tv_template)
5709 .test = alg_test_comp,
5713 .comp = __VECS(zstd_comp_tv_template),
5714 .decomp = __VECS(zstd_decomp_tv_template)
5720 static void alg_check_test_descs_order(void)
5724 for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
5725 int diff = strcmp(alg_test_descs[i - 1].alg,
5726 alg_test_descs[i].alg);
5728 if (WARN_ON(diff > 0)) {
5729 pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
5730 alg_test_descs[i - 1].alg,
5731 alg_test_descs[i].alg);
5734 if (WARN_ON(diff == 0)) {
5735 pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
5736 alg_test_descs[i].alg);
5741 static void alg_check_testvec_configs(void)
5745 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++)
5746 WARN_ON(!valid_testvec_config(
5747 &default_cipher_testvec_configs[i]));
5749 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++)
5750 WARN_ON(!valid_testvec_config(
5751 &default_hash_testvec_configs[i]));
5754 static void testmgr_onetime_init(void)
5756 alg_check_test_descs_order();
5757 alg_check_testvec_configs();
5759 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
5760 pr_warn("alg: extra crypto tests enabled. This is intended for developer use only.\n");
5764 static int alg_find_test(const char *alg)
5767 int end = ARRAY_SIZE(alg_test_descs);
5769 while (start < end) {
5770 int i = (start + end) / 2;
5771 int diff = strcmp(alg_test_descs[i].alg, alg);
5789 static int alg_fips_disabled(const char *driver, const char *alg)
5791 pr_info("alg: %s (%s) is disabled due to FIPS\n", alg, driver);
5796 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
5802 if (!fips_enabled && notests) {
5803 printk_once(KERN_INFO "alg: self-tests disabled\n");
5807 DO_ONCE(testmgr_onetime_init);
5809 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
5810 char nalg[CRYPTO_MAX_ALG_NAME];
5812 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
5814 return -ENAMETOOLONG;
5816 i = alg_find_test(nalg);
5820 if (fips_enabled && !alg_test_descs[i].fips_allowed)
5823 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
5827 i = alg_find_test(alg);
5828 j = alg_find_test(driver);
5833 if (j >= 0 && !alg_test_descs[j].fips_allowed)
5836 if (i >= 0 && !alg_test_descs[i].fips_allowed)
5842 rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
5844 if (j >= 0 && j != i)
5845 rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
5850 if (fips_enabled || panic_on_fail) {
5852 panic("alg: self-tests for %s (%s) failed in %s mode!\n",
5854 fips_enabled ? "fips" : "panic_on_fail");
5856 pr_warn("alg: self-tests for %s using %s failed (rc=%d)",
5859 "alg: self-tests for %s using %s failed (rc=%d)",
5863 pr_info("alg: self-tests for %s (%s) passed\n",
5870 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
5872 if (type & CRYPTO_ALG_FIPS_INTERNAL)
5873 return alg_fips_disabled(driver, alg);
5877 return alg_fips_disabled(driver, alg);
5880 #endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
5882 EXPORT_SYMBOL_GPL(alg_test);