xref: /openbmc/linux/crypto/testmgr.c (revision c7381b01)
1da7f033dSHerbert Xu /*
2da7f033dSHerbert Xu  * Algorithm testing framework and tests.
3da7f033dSHerbert Xu  *
4da7f033dSHerbert Xu  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5da7f033dSHerbert Xu  * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
6da7f033dSHerbert Xu  * Copyright (c) 2007 Nokia Siemens Networks
7da7f033dSHerbert Xu  * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
83f47a03dSEric Biggers  * Copyright (c) 2019 Google LLC
9da7f033dSHerbert Xu  *
1069435b94SAdrian Hoban  * Updated RFC4106 AES-GCM testing.
1169435b94SAdrian Hoban  *    Authors: Aidan O'Mahony (aidan.o.mahony@intel.com)
1269435b94SAdrian Hoban  *             Adrian Hoban <adrian.hoban@intel.com>
1369435b94SAdrian Hoban  *             Gabriele Paoloni <gabriele.paoloni@intel.com>
1469435b94SAdrian Hoban  *             Tadeusz Struk (tadeusz.struk@intel.com)
1569435b94SAdrian Hoban  *    Copyright (c) 2010, Intel Corporation.
1669435b94SAdrian Hoban  *
17da7f033dSHerbert Xu  * This program is free software; you can redistribute it and/or modify it
18da7f033dSHerbert Xu  * under the terms of the GNU General Public License as published by the Free
19da7f033dSHerbert Xu  * Software Foundation; either version 2 of the License, or (at your option)
20da7f033dSHerbert Xu  * any later version.
21da7f033dSHerbert Xu  *
22da7f033dSHerbert Xu  */
23da7f033dSHerbert Xu 
241ce33115SHerbert Xu #include <crypto/aead.h>
25da7f033dSHerbert Xu #include <crypto/hash.h>
2612773d93SHerbert Xu #include <crypto/skcipher.h>
27da7f033dSHerbert Xu #include <linux/err.h>
281c41b882SHerbert Xu #include <linux/fips.h>
29da7f033dSHerbert Xu #include <linux/module.h>
303f47a03dSEric Biggers #include <linux/once.h>
3125f9dddbSEric Biggers #include <linux/random.h>
32da7f033dSHerbert Xu #include <linux/scatterlist.h>
33da7f033dSHerbert Xu #include <linux/slab.h>
34da7f033dSHerbert Xu #include <linux/string.h>
357647d6ceSJarod Wilson #include <crypto/rng.h>
3664d1cdfbSStephan Mueller #include <crypto/drbg.h>
37946cc463STadeusz Struk #include <crypto/akcipher.h>
38802c7f1cSSalvatore Benedetto #include <crypto/kpp.h>
39d7db7a88SGiovanni Cabiddu #include <crypto/acompress.h>
40b55e1a39SEric Biggers #include <crypto/internal/simd.h>
41da7f033dSHerbert Xu 
42da7f033dSHerbert Xu #include "internal.h"
430b767f96SAlexander Shishkin 
449e5c9fe4SRichard W.M. Jones static bool notests;
459e5c9fe4SRichard W.M. Jones module_param(notests, bool, 0644);
469e5c9fe4SRichard W.M. Jones MODULE_PARM_DESC(notests, "disable crypto self-tests");
479e5c9fe4SRichard W.M. Jones 
48eda69b0cSEric Biggers static bool panic_on_fail;
49eda69b0cSEric Biggers module_param(panic_on_fail, bool, 0444);
50eda69b0cSEric Biggers 
515b2706a4SEric Biggers #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
525b2706a4SEric Biggers static bool noextratests;
535b2706a4SEric Biggers module_param(noextratests, bool, 0644);
545b2706a4SEric Biggers MODULE_PARM_DESC(noextratests, "disable expensive crypto self-tests");
555b2706a4SEric Biggers 
565b2706a4SEric Biggers static unsigned int fuzz_iterations = 100;
575b2706a4SEric Biggers module_param(fuzz_iterations, uint, 0644);
585b2706a4SEric Biggers MODULE_PARM_DESC(fuzz_iterations, "number of fuzz test iterations");
59b55e1a39SEric Biggers 
60b55e1a39SEric Biggers DEFINE_PER_CPU(bool, crypto_simd_disabled_for_test);
61b55e1a39SEric Biggers EXPORT_PER_CPU_SYMBOL_GPL(crypto_simd_disabled_for_test);
625b2706a4SEric Biggers #endif
635b2706a4SEric Biggers 
64326a6346SHerbert Xu #ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
650b767f96SAlexander Shishkin 
660b767f96SAlexander Shishkin /* a perfect nop */
670b767f96SAlexander Shishkin int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
680b767f96SAlexander Shishkin {
690b767f96SAlexander Shishkin 	return 0;
700b767f96SAlexander Shishkin }
710b767f96SAlexander Shishkin 
720b767f96SAlexander Shishkin #else
730b767f96SAlexander Shishkin 
74da7f033dSHerbert Xu #include "testmgr.h"
75da7f033dSHerbert Xu 
76da7f033dSHerbert Xu /*
77da7f033dSHerbert Xu  * Need slab memory for testing (size in number of pages).
78da7f033dSHerbert Xu  */
79da7f033dSHerbert Xu #define XBUFSIZE	8
80da7f033dSHerbert Xu 
81da7f033dSHerbert Xu /*
82da7f033dSHerbert Xu * Used by test_cipher()
83da7f033dSHerbert Xu */
84da7f033dSHerbert Xu #define ENCRYPT 1
85da7f033dSHerbert Xu #define DECRYPT 0
86da7f033dSHerbert Xu 
87da7f033dSHerbert Xu struct aead_test_suite {
88b13b1e0cSEric Biggers 	const struct aead_testvec *vecs;
89da7f033dSHerbert Xu 	unsigned int count;
90da7f033dSHerbert Xu };
91da7f033dSHerbert Xu 
92da7f033dSHerbert Xu struct cipher_test_suite {
93b13b1e0cSEric Biggers 	const struct cipher_testvec *vecs;
94da7f033dSHerbert Xu 	unsigned int count;
95da7f033dSHerbert Xu };
96da7f033dSHerbert Xu 
97da7f033dSHerbert Xu struct comp_test_suite {
98da7f033dSHerbert Xu 	struct {
99b13b1e0cSEric Biggers 		const struct comp_testvec *vecs;
100da7f033dSHerbert Xu 		unsigned int count;
101da7f033dSHerbert Xu 	} comp, decomp;
102da7f033dSHerbert Xu };
103da7f033dSHerbert Xu 
104da7f033dSHerbert Xu struct hash_test_suite {
105b13b1e0cSEric Biggers 	const struct hash_testvec *vecs;
106da7f033dSHerbert Xu 	unsigned int count;
107da7f033dSHerbert Xu };
108da7f033dSHerbert Xu 
1097647d6ceSJarod Wilson struct cprng_test_suite {
110b13b1e0cSEric Biggers 	const struct cprng_testvec *vecs;
1117647d6ceSJarod Wilson 	unsigned int count;
1127647d6ceSJarod Wilson };
1137647d6ceSJarod Wilson 
11464d1cdfbSStephan Mueller struct drbg_test_suite {
115b13b1e0cSEric Biggers 	const struct drbg_testvec *vecs;
11664d1cdfbSStephan Mueller 	unsigned int count;
11764d1cdfbSStephan Mueller };
11864d1cdfbSStephan Mueller 
119946cc463STadeusz Struk struct akcipher_test_suite {
120b13b1e0cSEric Biggers 	const struct akcipher_testvec *vecs;
121946cc463STadeusz Struk 	unsigned int count;
122946cc463STadeusz Struk };
123946cc463STadeusz Struk 
124802c7f1cSSalvatore Benedetto struct kpp_test_suite {
125b13b1e0cSEric Biggers 	const struct kpp_testvec *vecs;
126802c7f1cSSalvatore Benedetto 	unsigned int count;
127802c7f1cSSalvatore Benedetto };
128802c7f1cSSalvatore Benedetto 
129da7f033dSHerbert Xu struct alg_test_desc {
130da7f033dSHerbert Xu 	const char *alg;
131da7f033dSHerbert Xu 	int (*test)(const struct alg_test_desc *desc, const char *driver,
132da7f033dSHerbert Xu 		    u32 type, u32 mask);
133a1915d51SJarod Wilson 	int fips_allowed;	/* set if alg is allowed in fips mode */
134da7f033dSHerbert Xu 
135da7f033dSHerbert Xu 	union {
136da7f033dSHerbert Xu 		struct aead_test_suite aead;
137da7f033dSHerbert Xu 		struct cipher_test_suite cipher;
138da7f033dSHerbert Xu 		struct comp_test_suite comp;
139da7f033dSHerbert Xu 		struct hash_test_suite hash;
1407647d6ceSJarod Wilson 		struct cprng_test_suite cprng;
14164d1cdfbSStephan Mueller 		struct drbg_test_suite drbg;
142946cc463STadeusz Struk 		struct akcipher_test_suite akcipher;
143802c7f1cSSalvatore Benedetto 		struct kpp_test_suite kpp;
144da7f033dSHerbert Xu 	} suite;
145da7f033dSHerbert Xu };
146da7f033dSHerbert Xu 
147da7f033dSHerbert Xu static void hexdump(unsigned char *buf, unsigned int len)
148da7f033dSHerbert Xu {
149da7f033dSHerbert Xu 	print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
150da7f033dSHerbert Xu 			16, 1,
151da7f033dSHerbert Xu 			buf, len, false);
152da7f033dSHerbert Xu }
153da7f033dSHerbert Xu 
1543f47a03dSEric Biggers static int __testmgr_alloc_buf(char *buf[XBUFSIZE], int order)
155f8b0d4d0SHerbert Xu {
156f8b0d4d0SHerbert Xu 	int i;
157f8b0d4d0SHerbert Xu 
158f8b0d4d0SHerbert Xu 	for (i = 0; i < XBUFSIZE; i++) {
1593f47a03dSEric Biggers 		buf[i] = (char *)__get_free_pages(GFP_KERNEL, order);
160f8b0d4d0SHerbert Xu 		if (!buf[i])
161f8b0d4d0SHerbert Xu 			goto err_free_buf;
162f8b0d4d0SHerbert Xu 	}
163f8b0d4d0SHerbert Xu 
164f8b0d4d0SHerbert Xu 	return 0;
165f8b0d4d0SHerbert Xu 
166f8b0d4d0SHerbert Xu err_free_buf:
167f8b0d4d0SHerbert Xu 	while (i-- > 0)
1683f47a03dSEric Biggers 		free_pages((unsigned long)buf[i], order);
169f8b0d4d0SHerbert Xu 
170f8b0d4d0SHerbert Xu 	return -ENOMEM;
171f8b0d4d0SHerbert Xu }
172f8b0d4d0SHerbert Xu 
1733f47a03dSEric Biggers static int testmgr_alloc_buf(char *buf[XBUFSIZE])
1743f47a03dSEric Biggers {
1753f47a03dSEric Biggers 	return __testmgr_alloc_buf(buf, 0);
1763f47a03dSEric Biggers }
1773f47a03dSEric Biggers 
1783f47a03dSEric Biggers static void __testmgr_free_buf(char *buf[XBUFSIZE], int order)
179f8b0d4d0SHerbert Xu {
180f8b0d4d0SHerbert Xu 	int i;
181f8b0d4d0SHerbert Xu 
182f8b0d4d0SHerbert Xu 	for (i = 0; i < XBUFSIZE; i++)
1833f47a03dSEric Biggers 		free_pages((unsigned long)buf[i], order);
1843f47a03dSEric Biggers }
1853f47a03dSEric Biggers 
1863f47a03dSEric Biggers static void testmgr_free_buf(char *buf[XBUFSIZE])
1873f47a03dSEric Biggers {
1883f47a03dSEric Biggers 	__testmgr_free_buf(buf, 0);
1893f47a03dSEric Biggers }
1903f47a03dSEric Biggers 
1913f47a03dSEric Biggers #define TESTMGR_POISON_BYTE	0xfe
1923f47a03dSEric Biggers #define TESTMGR_POISON_LEN	16
1933f47a03dSEric Biggers 
1943f47a03dSEric Biggers static inline void testmgr_poison(void *addr, size_t len)
1953f47a03dSEric Biggers {
1963f47a03dSEric Biggers 	memset(addr, TESTMGR_POISON_BYTE, len);
1973f47a03dSEric Biggers }
1983f47a03dSEric Biggers 
1993f47a03dSEric Biggers /* Is the memory region still fully poisoned? */
2003f47a03dSEric Biggers static inline bool testmgr_is_poison(const void *addr, size_t len)
2013f47a03dSEric Biggers {
2023f47a03dSEric Biggers 	return memchr_inv(addr, TESTMGR_POISON_BYTE, len) == NULL;
2033f47a03dSEric Biggers }
2043f47a03dSEric Biggers 
2053f47a03dSEric Biggers /* flush type for hash algorithms */
2063f47a03dSEric Biggers enum flush_type {
2073f47a03dSEric Biggers 	/* merge with update of previous buffer(s) */
2083f47a03dSEric Biggers 	FLUSH_TYPE_NONE = 0,
2093f47a03dSEric Biggers 
2103f47a03dSEric Biggers 	/* update with previous buffer(s) before doing this one */
2113f47a03dSEric Biggers 	FLUSH_TYPE_FLUSH,
2123f47a03dSEric Biggers 
2133f47a03dSEric Biggers 	/* likewise, but also export and re-import the intermediate state */
2143f47a03dSEric Biggers 	FLUSH_TYPE_REIMPORT,
2153f47a03dSEric Biggers };
2163f47a03dSEric Biggers 
2173f47a03dSEric Biggers /* finalization function for hash algorithms */
2183f47a03dSEric Biggers enum finalization_type {
2193f47a03dSEric Biggers 	FINALIZATION_TYPE_FINAL,	/* use final() */
2203f47a03dSEric Biggers 	FINALIZATION_TYPE_FINUP,	/* use finup() */
2213f47a03dSEric Biggers 	FINALIZATION_TYPE_DIGEST,	/* use digest() */
2223f47a03dSEric Biggers };
2233f47a03dSEric Biggers 
2243f47a03dSEric Biggers #define TEST_SG_TOTAL	10000
2253f47a03dSEric Biggers 
2263f47a03dSEric Biggers /**
2273f47a03dSEric Biggers  * struct test_sg_division - description of a scatterlist entry
2283f47a03dSEric Biggers  *
2293f47a03dSEric Biggers  * This struct describes one entry of a scatterlist being constructed to check a
2303f47a03dSEric Biggers  * crypto test vector.
2313f47a03dSEric Biggers  *
2323f47a03dSEric Biggers  * @proportion_of_total: length of this chunk relative to the total length,
2333f47a03dSEric Biggers  *			 given as a proportion out of TEST_SG_TOTAL so that it
2343f47a03dSEric Biggers  *			 scales to fit any test vector
2353f47a03dSEric Biggers  * @offset: byte offset into a 2-page buffer at which this chunk will start
2363f47a03dSEric Biggers  * @offset_relative_to_alignmask: if true, add the algorithm's alignmask to the
2373f47a03dSEric Biggers  *				  @offset
2383f47a03dSEric Biggers  * @flush_type: for hashes, whether an update() should be done now vs.
2393f47a03dSEric Biggers  *		continuing to accumulate data
2406570737cSEric Biggers  * @nosimd: if doing the pending update(), do it with SIMD disabled?
2413f47a03dSEric Biggers  */
2423f47a03dSEric Biggers struct test_sg_division {
2433f47a03dSEric Biggers 	unsigned int proportion_of_total;
2443f47a03dSEric Biggers 	unsigned int offset;
2453f47a03dSEric Biggers 	bool offset_relative_to_alignmask;
2463f47a03dSEric Biggers 	enum flush_type flush_type;
2476570737cSEric Biggers 	bool nosimd;
2483f47a03dSEric Biggers };
2493f47a03dSEric Biggers 
2503f47a03dSEric Biggers /**
2513f47a03dSEric Biggers  * struct testvec_config - configuration for testing a crypto test vector
2523f47a03dSEric Biggers  *
2533f47a03dSEric Biggers  * This struct describes the data layout and other parameters with which each
2543f47a03dSEric Biggers  * crypto test vector can be tested.
2553f47a03dSEric Biggers  *
2563f47a03dSEric Biggers  * @name: name of this config, logged for debugging purposes if a test fails
2573f47a03dSEric Biggers  * @inplace: operate on the data in-place, if applicable for the algorithm type?
2583f47a03dSEric Biggers  * @req_flags: extra request_flags, e.g. CRYPTO_TFM_REQ_MAY_SLEEP
2593f47a03dSEric Biggers  * @src_divs: description of how to arrange the source scatterlist
2603f47a03dSEric Biggers  * @dst_divs: description of how to arrange the dst scatterlist, if applicable
2613f47a03dSEric Biggers  *	      for the algorithm type.  Defaults to @src_divs if unset.
2623f47a03dSEric Biggers  * @iv_offset: misalignment of the IV in the range [0..MAX_ALGAPI_ALIGNMASK+1],
2633f47a03dSEric Biggers  *	       where 0 is aligned to a 2*(MAX_ALGAPI_ALIGNMASK+1) byte boundary
2643f47a03dSEric Biggers  * @iv_offset_relative_to_alignmask: if true, add the algorithm's alignmask to
2653f47a03dSEric Biggers  *				     the @iv_offset
2663f47a03dSEric Biggers  * @finalization_type: what finalization function to use for hashes
2676570737cSEric Biggers  * @nosimd: execute with SIMD disabled?  Requires !CRYPTO_TFM_REQ_MAY_SLEEP.
2683f47a03dSEric Biggers  */
2693f47a03dSEric Biggers struct testvec_config {
2703f47a03dSEric Biggers 	const char *name;
2713f47a03dSEric Biggers 	bool inplace;
2723f47a03dSEric Biggers 	u32 req_flags;
2733f47a03dSEric Biggers 	struct test_sg_division src_divs[XBUFSIZE];
2743f47a03dSEric Biggers 	struct test_sg_division dst_divs[XBUFSIZE];
2753f47a03dSEric Biggers 	unsigned int iv_offset;
2763f47a03dSEric Biggers 	bool iv_offset_relative_to_alignmask;
2773f47a03dSEric Biggers 	enum finalization_type finalization_type;
2786570737cSEric Biggers 	bool nosimd;
2793f47a03dSEric Biggers };
2803f47a03dSEric Biggers 
2813f47a03dSEric Biggers #define TESTVEC_CONFIG_NAMELEN	192
2823f47a03dSEric Biggers 
2834e7babbaSEric Biggers /*
2844e7babbaSEric Biggers  * The following are the lists of testvec_configs to test for each algorithm
2854e7babbaSEric Biggers  * type when the basic crypto self-tests are enabled, i.e. when
2864e7babbaSEric Biggers  * CONFIG_CRYPTO_MANAGER_DISABLE_TESTS is unset.  They aim to provide good test
2874e7babbaSEric Biggers  * coverage, while keeping the test time much shorter than the full fuzz tests
2884e7babbaSEric Biggers  * so that the basic tests can be enabled in a wider range of circumstances.
2894e7babbaSEric Biggers  */
2904e7babbaSEric Biggers 
2914e7babbaSEric Biggers /* Configs for skciphers and aeads */
2924e7babbaSEric Biggers static const struct testvec_config default_cipher_testvec_configs[] = {
2934e7babbaSEric Biggers 	{
2944e7babbaSEric Biggers 		.name = "in-place",
2954e7babbaSEric Biggers 		.inplace = true,
2964e7babbaSEric Biggers 		.src_divs = { { .proportion_of_total = 10000 } },
2974e7babbaSEric Biggers 	}, {
2984e7babbaSEric Biggers 		.name = "out-of-place",
2994e7babbaSEric Biggers 		.src_divs = { { .proportion_of_total = 10000 } },
3004e7babbaSEric Biggers 	}, {
3014e7babbaSEric Biggers 		.name = "unaligned buffer, offset=1",
3024e7babbaSEric Biggers 		.src_divs = { { .proportion_of_total = 10000, .offset = 1 } },
3034e7babbaSEric Biggers 		.iv_offset = 1,
3044e7babbaSEric Biggers 	}, {
3054e7babbaSEric Biggers 		.name = "buffer aligned only to alignmask",
3064e7babbaSEric Biggers 		.src_divs = {
3074e7babbaSEric Biggers 			{
3084e7babbaSEric Biggers 				.proportion_of_total = 10000,
3094e7babbaSEric Biggers 				.offset = 1,
3104e7babbaSEric Biggers 				.offset_relative_to_alignmask = true,
3114e7babbaSEric Biggers 			},
3124e7babbaSEric Biggers 		},
3134e7babbaSEric Biggers 		.iv_offset = 1,
3144e7babbaSEric Biggers 		.iv_offset_relative_to_alignmask = true,
3154e7babbaSEric Biggers 	}, {
3164e7babbaSEric Biggers 		.name = "two even aligned splits",
3174e7babbaSEric Biggers 		.src_divs = {
3184e7babbaSEric Biggers 			{ .proportion_of_total = 5000 },
3194e7babbaSEric Biggers 			{ .proportion_of_total = 5000 },
3204e7babbaSEric Biggers 		},
3214e7babbaSEric Biggers 	}, {
3224e7babbaSEric Biggers 		.name = "uneven misaligned splits, may sleep",
3234e7babbaSEric Biggers 		.req_flags = CRYPTO_TFM_REQ_MAY_SLEEP,
3244e7babbaSEric Biggers 		.src_divs = {
3254e7babbaSEric Biggers 			{ .proportion_of_total = 1900, .offset = 33 },
3264e7babbaSEric Biggers 			{ .proportion_of_total = 3300, .offset = 7  },
3274e7babbaSEric Biggers 			{ .proportion_of_total = 4800, .offset = 18 },
3284e7babbaSEric Biggers 		},
3294e7babbaSEric Biggers 		.iv_offset = 3,
3304e7babbaSEric Biggers 	}, {
3314e7babbaSEric Biggers 		.name = "misaligned splits crossing pages, inplace",
3324e7babbaSEric Biggers 		.inplace = true,
3334e7babbaSEric Biggers 		.src_divs = {
3344e7babbaSEric Biggers 			{
3354e7babbaSEric Biggers 				.proportion_of_total = 7500,
3364e7babbaSEric Biggers 				.offset = PAGE_SIZE - 32
3374e7babbaSEric Biggers 			}, {
3384e7babbaSEric Biggers 				.proportion_of_total = 2500,
3394e7babbaSEric Biggers 				.offset = PAGE_SIZE - 7
3404e7babbaSEric Biggers 			},
3414e7babbaSEric Biggers 		},
3424e7babbaSEric Biggers 	}
3434e7babbaSEric Biggers };
3444e7babbaSEric Biggers 
3454cc2dcf9SEric Biggers static const struct testvec_config default_hash_testvec_configs[] = {
3464cc2dcf9SEric Biggers 	{
3474cc2dcf9SEric Biggers 		.name = "init+update+final aligned buffer",
3484cc2dcf9SEric Biggers 		.src_divs = { { .proportion_of_total = 10000 } },
3494cc2dcf9SEric Biggers 		.finalization_type = FINALIZATION_TYPE_FINAL,
3504cc2dcf9SEric Biggers 	}, {
3514cc2dcf9SEric Biggers 		.name = "init+finup aligned buffer",
3524cc2dcf9SEric Biggers 		.src_divs = { { .proportion_of_total = 10000 } },
3534cc2dcf9SEric Biggers 		.finalization_type = FINALIZATION_TYPE_FINUP,
3544cc2dcf9SEric Biggers 	}, {
3554cc2dcf9SEric Biggers 		.name = "digest aligned buffer",
3564cc2dcf9SEric Biggers 		.src_divs = { { .proportion_of_total = 10000 } },
3574cc2dcf9SEric Biggers 		.finalization_type = FINALIZATION_TYPE_DIGEST,
3584cc2dcf9SEric Biggers 	}, {
3594cc2dcf9SEric Biggers 		.name = "init+update+final misaligned buffer",
3604cc2dcf9SEric Biggers 		.src_divs = { { .proportion_of_total = 10000, .offset = 1 } },
3614cc2dcf9SEric Biggers 		.finalization_type = FINALIZATION_TYPE_FINAL,
3624cc2dcf9SEric Biggers 	}, {
3634cc2dcf9SEric Biggers 		.name = "digest buffer aligned only to alignmask",
3644cc2dcf9SEric Biggers 		.src_divs = {
3654cc2dcf9SEric Biggers 			{
3664cc2dcf9SEric Biggers 				.proportion_of_total = 10000,
3674cc2dcf9SEric Biggers 				.offset = 1,
3684cc2dcf9SEric Biggers 				.offset_relative_to_alignmask = true,
3694cc2dcf9SEric Biggers 			},
3704cc2dcf9SEric Biggers 		},
3714cc2dcf9SEric Biggers 		.finalization_type = FINALIZATION_TYPE_DIGEST,
3724cc2dcf9SEric Biggers 	}, {
3734cc2dcf9SEric Biggers 		.name = "init+update+update+final two even splits",
3744cc2dcf9SEric Biggers 		.src_divs = {
3754cc2dcf9SEric Biggers 			{ .proportion_of_total = 5000 },
3764cc2dcf9SEric Biggers 			{
3774cc2dcf9SEric Biggers 				.proportion_of_total = 5000,
3784cc2dcf9SEric Biggers 				.flush_type = FLUSH_TYPE_FLUSH,
3794cc2dcf9SEric Biggers 			},
3804cc2dcf9SEric Biggers 		},
3814cc2dcf9SEric Biggers 		.finalization_type = FINALIZATION_TYPE_FINAL,
3824cc2dcf9SEric Biggers 	}, {
3834cc2dcf9SEric Biggers 		.name = "digest uneven misaligned splits, may sleep",
3844cc2dcf9SEric Biggers 		.req_flags = CRYPTO_TFM_REQ_MAY_SLEEP,
3854cc2dcf9SEric Biggers 		.src_divs = {
3864cc2dcf9SEric Biggers 			{ .proportion_of_total = 1900, .offset = 33 },
3874cc2dcf9SEric Biggers 			{ .proportion_of_total = 3300, .offset = 7  },
3884cc2dcf9SEric Biggers 			{ .proportion_of_total = 4800, .offset = 18 },
3894cc2dcf9SEric Biggers 		},
3904cc2dcf9SEric Biggers 		.finalization_type = FINALIZATION_TYPE_DIGEST,
3914cc2dcf9SEric Biggers 	}, {
3924cc2dcf9SEric Biggers 		.name = "digest misaligned splits crossing pages",
3934cc2dcf9SEric Biggers 		.src_divs = {
3944cc2dcf9SEric Biggers 			{
3954cc2dcf9SEric Biggers 				.proportion_of_total = 7500,
3964cc2dcf9SEric Biggers 				.offset = PAGE_SIZE - 32,
3974cc2dcf9SEric Biggers 			}, {
3984cc2dcf9SEric Biggers 				.proportion_of_total = 2500,
3994cc2dcf9SEric Biggers 				.offset = PAGE_SIZE - 7,
4004cc2dcf9SEric Biggers 			},
4014cc2dcf9SEric Biggers 		},
4024cc2dcf9SEric Biggers 		.finalization_type = FINALIZATION_TYPE_DIGEST,
4034cc2dcf9SEric Biggers 	}, {
4044cc2dcf9SEric Biggers 		.name = "import/export",
4054cc2dcf9SEric Biggers 		.src_divs = {
4064cc2dcf9SEric Biggers 			{
4074cc2dcf9SEric Biggers 				.proportion_of_total = 6500,
4084cc2dcf9SEric Biggers 				.flush_type = FLUSH_TYPE_REIMPORT,
4094cc2dcf9SEric Biggers 			}, {
4104cc2dcf9SEric Biggers 				.proportion_of_total = 3500,
4114cc2dcf9SEric Biggers 				.flush_type = FLUSH_TYPE_REIMPORT,
4124cc2dcf9SEric Biggers 			},
4134cc2dcf9SEric Biggers 		},
4144cc2dcf9SEric Biggers 		.finalization_type = FINALIZATION_TYPE_FINAL,
4154cc2dcf9SEric Biggers 	}
4164cc2dcf9SEric Biggers };
4174cc2dcf9SEric Biggers 
4183f47a03dSEric Biggers static unsigned int count_test_sg_divisions(const struct test_sg_division *divs)
4193f47a03dSEric Biggers {
4203f47a03dSEric Biggers 	unsigned int remaining = TEST_SG_TOTAL;
4213f47a03dSEric Biggers 	unsigned int ndivs = 0;
4223f47a03dSEric Biggers 
4233f47a03dSEric Biggers 	do {
4243f47a03dSEric Biggers 		remaining -= divs[ndivs++].proportion_of_total;
4253f47a03dSEric Biggers 	} while (remaining);
4263f47a03dSEric Biggers 
4273f47a03dSEric Biggers 	return ndivs;
4283f47a03dSEric Biggers }
4293f47a03dSEric Biggers 
4306570737cSEric Biggers #define SGDIVS_HAVE_FLUSHES	BIT(0)
4316570737cSEric Biggers #define SGDIVS_HAVE_NOSIMD	BIT(1)
4326570737cSEric Biggers 
4333f47a03dSEric Biggers static bool valid_sg_divisions(const struct test_sg_division *divs,
4346570737cSEric Biggers 			       unsigned int count, int *flags_ret)
4353f47a03dSEric Biggers {
4363f47a03dSEric Biggers 	unsigned int total = 0;
4373f47a03dSEric Biggers 	unsigned int i;
4383f47a03dSEric Biggers 
4393f47a03dSEric Biggers 	for (i = 0; i < count && total != TEST_SG_TOTAL; i++) {
4403f47a03dSEric Biggers 		if (divs[i].proportion_of_total <= 0 ||
4413f47a03dSEric Biggers 		    divs[i].proportion_of_total > TEST_SG_TOTAL - total)
4423f47a03dSEric Biggers 			return false;
4433f47a03dSEric Biggers 		total += divs[i].proportion_of_total;
4443f47a03dSEric Biggers 		if (divs[i].flush_type != FLUSH_TYPE_NONE)
4456570737cSEric Biggers 			*flags_ret |= SGDIVS_HAVE_FLUSHES;
4466570737cSEric Biggers 		if (divs[i].nosimd)
4476570737cSEric Biggers 			*flags_ret |= SGDIVS_HAVE_NOSIMD;
4483f47a03dSEric Biggers 	}
4493f47a03dSEric Biggers 	return total == TEST_SG_TOTAL &&
4503f47a03dSEric Biggers 		memchr_inv(&divs[i], 0, (count - i) * sizeof(divs[0])) == NULL;
4513f47a03dSEric Biggers }
4523f47a03dSEric Biggers 
4533f47a03dSEric Biggers /*
4543f47a03dSEric Biggers  * Check whether the given testvec_config is valid.  This isn't strictly needed
4553f47a03dSEric Biggers  * since every testvec_config should be valid, but check anyway so that people
4563f47a03dSEric Biggers  * don't unknowingly add broken configs that don't do what they wanted.
4573f47a03dSEric Biggers  */
4583f47a03dSEric Biggers static bool valid_testvec_config(const struct testvec_config *cfg)
4593f47a03dSEric Biggers {
4606570737cSEric Biggers 	int flags = 0;
4613f47a03dSEric Biggers 
4623f47a03dSEric Biggers 	if (cfg->name == NULL)
4633f47a03dSEric Biggers 		return false;
4643f47a03dSEric Biggers 
4653f47a03dSEric Biggers 	if (!valid_sg_divisions(cfg->src_divs, ARRAY_SIZE(cfg->src_divs),
4666570737cSEric Biggers 				&flags))
4673f47a03dSEric Biggers 		return false;
4683f47a03dSEric Biggers 
4693f47a03dSEric Biggers 	if (cfg->dst_divs[0].proportion_of_total) {
4703f47a03dSEric Biggers 		if (!valid_sg_divisions(cfg->dst_divs,
4716570737cSEric Biggers 					ARRAY_SIZE(cfg->dst_divs), &flags))
4723f47a03dSEric Biggers 			return false;
4733f47a03dSEric Biggers 	} else {
4743f47a03dSEric Biggers 		if (memchr_inv(cfg->dst_divs, 0, sizeof(cfg->dst_divs)))
4753f47a03dSEric Biggers 			return false;
4763f47a03dSEric Biggers 		/* defaults to dst_divs=src_divs */
4773f47a03dSEric Biggers 	}
4783f47a03dSEric Biggers 
4793f47a03dSEric Biggers 	if (cfg->iv_offset +
4803f47a03dSEric Biggers 	    (cfg->iv_offset_relative_to_alignmask ? MAX_ALGAPI_ALIGNMASK : 0) >
4813f47a03dSEric Biggers 	    MAX_ALGAPI_ALIGNMASK + 1)
4823f47a03dSEric Biggers 		return false;
4833f47a03dSEric Biggers 
4846570737cSEric Biggers 	if ((flags & (SGDIVS_HAVE_FLUSHES | SGDIVS_HAVE_NOSIMD)) &&
4856570737cSEric Biggers 	    cfg->finalization_type == FINALIZATION_TYPE_DIGEST)
4866570737cSEric Biggers 		return false;
4876570737cSEric Biggers 
4886570737cSEric Biggers 	if ((cfg->nosimd || (flags & SGDIVS_HAVE_NOSIMD)) &&
4896570737cSEric Biggers 	    (cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP))
4903f47a03dSEric Biggers 		return false;
4913f47a03dSEric Biggers 
4923f47a03dSEric Biggers 	return true;
4933f47a03dSEric Biggers }
4943f47a03dSEric Biggers 
4953f47a03dSEric Biggers struct test_sglist {
4963f47a03dSEric Biggers 	char *bufs[XBUFSIZE];
4973f47a03dSEric Biggers 	struct scatterlist sgl[XBUFSIZE];
4983f47a03dSEric Biggers 	struct scatterlist sgl_saved[XBUFSIZE];
4993f47a03dSEric Biggers 	struct scatterlist *sgl_ptr;
5003f47a03dSEric Biggers 	unsigned int nents;
5013f47a03dSEric Biggers };
5023f47a03dSEric Biggers 
5033f47a03dSEric Biggers static int init_test_sglist(struct test_sglist *tsgl)
5043f47a03dSEric Biggers {
5053f47a03dSEric Biggers 	return __testmgr_alloc_buf(tsgl->bufs, 1 /* two pages per buffer */);
5063f47a03dSEric Biggers }
5073f47a03dSEric Biggers 
5083f47a03dSEric Biggers static void destroy_test_sglist(struct test_sglist *tsgl)
5093f47a03dSEric Biggers {
5103f47a03dSEric Biggers 	return __testmgr_free_buf(tsgl->bufs, 1 /* two pages per buffer */);
5113f47a03dSEric Biggers }
5123f47a03dSEric Biggers 
5133f47a03dSEric Biggers /**
5143f47a03dSEric Biggers  * build_test_sglist() - build a scatterlist for a crypto test
5153f47a03dSEric Biggers  *
5163f47a03dSEric Biggers  * @tsgl: the scatterlist to build.  @tsgl->bufs[] contains an array of 2-page
5173f47a03dSEric Biggers  *	  buffers which the scatterlist @tsgl->sgl[] will be made to point into.
5183f47a03dSEric Biggers  * @divs: the layout specification on which the scatterlist will be based
5193f47a03dSEric Biggers  * @alignmask: the algorithm's alignmask
5203f47a03dSEric Biggers  * @total_len: the total length of the scatterlist to build in bytes
5213f47a03dSEric Biggers  * @data: if non-NULL, the buffers will be filled with this data until it ends.
5223f47a03dSEric Biggers  *	  Otherwise the buffers will be poisoned.  In both cases, some bytes
5233f47a03dSEric Biggers  *	  past the end of each buffer will be poisoned to help detect overruns.
5243f47a03dSEric Biggers  * @out_divs: if non-NULL, the test_sg_division to which each scatterlist entry
5253f47a03dSEric Biggers  *	      corresponds will be returned here.  This will match @divs except
5263f47a03dSEric Biggers  *	      that divisions resolving to a length of 0 are omitted as they are
5273f47a03dSEric Biggers  *	      not included in the scatterlist.
5283f47a03dSEric Biggers  *
5293f47a03dSEric Biggers  * Return: 0 or a -errno value
5303f47a03dSEric Biggers  */
5313f47a03dSEric Biggers static int build_test_sglist(struct test_sglist *tsgl,
5323f47a03dSEric Biggers 			     const struct test_sg_division *divs,
5333f47a03dSEric Biggers 			     const unsigned int alignmask,
5343f47a03dSEric Biggers 			     const unsigned int total_len,
5353f47a03dSEric Biggers 			     struct iov_iter *data,
5363f47a03dSEric Biggers 			     const struct test_sg_division *out_divs[XBUFSIZE])
5373f47a03dSEric Biggers {
5383f47a03dSEric Biggers 	struct {
5393f47a03dSEric Biggers 		const struct test_sg_division *div;
5403f47a03dSEric Biggers 		size_t length;
5413f47a03dSEric Biggers 	} partitions[XBUFSIZE];
5423f47a03dSEric Biggers 	const unsigned int ndivs = count_test_sg_divisions(divs);
5433f47a03dSEric Biggers 	unsigned int len_remaining = total_len;
5443f47a03dSEric Biggers 	unsigned int i;
5453f47a03dSEric Biggers 
5463f47a03dSEric Biggers 	BUILD_BUG_ON(ARRAY_SIZE(partitions) != ARRAY_SIZE(tsgl->sgl));
5473f47a03dSEric Biggers 	if (WARN_ON(ndivs > ARRAY_SIZE(partitions)))
5483f47a03dSEric Biggers 		return -EINVAL;
5493f47a03dSEric Biggers 
5503f47a03dSEric Biggers 	/* Calculate the (div, length) pairs */
5513f47a03dSEric Biggers 	tsgl->nents = 0;
5523f47a03dSEric Biggers 	for (i = 0; i < ndivs; i++) {
5533f47a03dSEric Biggers 		unsigned int len_this_sg =
5543f47a03dSEric Biggers 			min(len_remaining,
5553f47a03dSEric Biggers 			    (total_len * divs[i].proportion_of_total +
5563f47a03dSEric Biggers 			     TEST_SG_TOTAL / 2) / TEST_SG_TOTAL);
5573f47a03dSEric Biggers 
5583f47a03dSEric Biggers 		if (len_this_sg != 0) {
5593f47a03dSEric Biggers 			partitions[tsgl->nents].div = &divs[i];
5603f47a03dSEric Biggers 			partitions[tsgl->nents].length = len_this_sg;
5613f47a03dSEric Biggers 			tsgl->nents++;
5623f47a03dSEric Biggers 			len_remaining -= len_this_sg;
5633f47a03dSEric Biggers 		}
5643f47a03dSEric Biggers 	}
5653f47a03dSEric Biggers 	if (tsgl->nents == 0) {
5663f47a03dSEric Biggers 		partitions[tsgl->nents].div = &divs[0];
5673f47a03dSEric Biggers 		partitions[tsgl->nents].length = 0;
5683f47a03dSEric Biggers 		tsgl->nents++;
5693f47a03dSEric Biggers 	}
5703f47a03dSEric Biggers 	partitions[tsgl->nents - 1].length += len_remaining;
5713f47a03dSEric Biggers 
5723f47a03dSEric Biggers 	/* Set up the sgl entries and fill the data or poison */
5733f47a03dSEric Biggers 	sg_init_table(tsgl->sgl, tsgl->nents);
5743f47a03dSEric Biggers 	for (i = 0; i < tsgl->nents; i++) {
5753f47a03dSEric Biggers 		unsigned int offset = partitions[i].div->offset;
5763f47a03dSEric Biggers 		void *addr;
5773f47a03dSEric Biggers 
5783f47a03dSEric Biggers 		if (partitions[i].div->offset_relative_to_alignmask)
5793f47a03dSEric Biggers 			offset += alignmask;
5803f47a03dSEric Biggers 
5813f47a03dSEric Biggers 		while (offset + partitions[i].length + TESTMGR_POISON_LEN >
5823f47a03dSEric Biggers 		       2 * PAGE_SIZE) {
5833f47a03dSEric Biggers 			if (WARN_ON(offset <= 0))
5843f47a03dSEric Biggers 				return -EINVAL;
5853f47a03dSEric Biggers 			offset /= 2;
5863f47a03dSEric Biggers 		}
5873f47a03dSEric Biggers 
5883f47a03dSEric Biggers 		addr = &tsgl->bufs[i][offset];
5893f47a03dSEric Biggers 		sg_set_buf(&tsgl->sgl[i], addr, partitions[i].length);
5903f47a03dSEric Biggers 
5913f47a03dSEric Biggers 		if (out_divs)
5923f47a03dSEric Biggers 			out_divs[i] = partitions[i].div;
5933f47a03dSEric Biggers 
5943f47a03dSEric Biggers 		if (data) {
5953f47a03dSEric Biggers 			size_t copy_len, copied;
5963f47a03dSEric Biggers 
5973f47a03dSEric Biggers 			copy_len = min(partitions[i].length, data->count);
5983f47a03dSEric Biggers 			copied = copy_from_iter(addr, copy_len, data);
5993f47a03dSEric Biggers 			if (WARN_ON(copied != copy_len))
6003f47a03dSEric Biggers 				return -EINVAL;
6013f47a03dSEric Biggers 			testmgr_poison(addr + copy_len, partitions[i].length +
6023f47a03dSEric Biggers 				       TESTMGR_POISON_LEN - copy_len);
6033f47a03dSEric Biggers 		} else {
6043f47a03dSEric Biggers 			testmgr_poison(addr, partitions[i].length +
6053f47a03dSEric Biggers 				       TESTMGR_POISON_LEN);
6063f47a03dSEric Biggers 		}
6073f47a03dSEric Biggers 	}
6083f47a03dSEric Biggers 
6093f47a03dSEric Biggers 	sg_mark_end(&tsgl->sgl[tsgl->nents - 1]);
6103f47a03dSEric Biggers 	tsgl->sgl_ptr = tsgl->sgl;
6113f47a03dSEric Biggers 	memcpy(tsgl->sgl_saved, tsgl->sgl, tsgl->nents * sizeof(tsgl->sgl[0]));
6123f47a03dSEric Biggers 	return 0;
6133f47a03dSEric Biggers }
6143f47a03dSEric Biggers 
6153f47a03dSEric Biggers /*
6163f47a03dSEric Biggers  * Verify that a scatterlist crypto operation produced the correct output.
6173f47a03dSEric Biggers  *
6183f47a03dSEric Biggers  * @tsgl: scatterlist containing the actual output
6193f47a03dSEric Biggers  * @expected_output: buffer containing the expected output
6203f47a03dSEric Biggers  * @len_to_check: length of @expected_output in bytes
6213f47a03dSEric Biggers  * @unchecked_prefix_len: number of ignored bytes in @tsgl prior to real result
6223f47a03dSEric Biggers  * @check_poison: verify that the poison bytes after each chunk are intact?
6233f47a03dSEric Biggers  *
6243f47a03dSEric Biggers  * Return: 0 if correct, -EINVAL if incorrect, -EOVERFLOW if buffer overrun.
6253f47a03dSEric Biggers  */
6263f47a03dSEric Biggers static int verify_correct_output(const struct test_sglist *tsgl,
6273f47a03dSEric Biggers 				 const char *expected_output,
6283f47a03dSEric Biggers 				 unsigned int len_to_check,
6293f47a03dSEric Biggers 				 unsigned int unchecked_prefix_len,
6303f47a03dSEric Biggers 				 bool check_poison)
6313f47a03dSEric Biggers {
6323f47a03dSEric Biggers 	unsigned int i;
6333f47a03dSEric Biggers 
6343f47a03dSEric Biggers 	for (i = 0; i < tsgl->nents; i++) {
6353f47a03dSEric Biggers 		struct scatterlist *sg = &tsgl->sgl_ptr[i];
6363f47a03dSEric Biggers 		unsigned int len = sg->length;
6373f47a03dSEric Biggers 		unsigned int offset = sg->offset;
6383f47a03dSEric Biggers 		const char *actual_output;
6393f47a03dSEric Biggers 
6403f47a03dSEric Biggers 		if (unchecked_prefix_len) {
6413f47a03dSEric Biggers 			if (unchecked_prefix_len >= len) {
6423f47a03dSEric Biggers 				unchecked_prefix_len -= len;
6433f47a03dSEric Biggers 				continue;
6443f47a03dSEric Biggers 			}
6453f47a03dSEric Biggers 			offset += unchecked_prefix_len;
6463f47a03dSEric Biggers 			len -= unchecked_prefix_len;
6473f47a03dSEric Biggers 			unchecked_prefix_len = 0;
6483f47a03dSEric Biggers 		}
6493f47a03dSEric Biggers 		len = min(len, len_to_check);
6503f47a03dSEric Biggers 		actual_output = page_address(sg_page(sg)) + offset;
6513f47a03dSEric Biggers 		if (memcmp(expected_output, actual_output, len) != 0)
6523f47a03dSEric Biggers 			return -EINVAL;
6533f47a03dSEric Biggers 		if (check_poison &&
6543f47a03dSEric Biggers 		    !testmgr_is_poison(actual_output + len, TESTMGR_POISON_LEN))
6553f47a03dSEric Biggers 			return -EOVERFLOW;
6563f47a03dSEric Biggers 		len_to_check -= len;
6573f47a03dSEric Biggers 		expected_output += len;
6583f47a03dSEric Biggers 	}
6593f47a03dSEric Biggers 	if (WARN_ON(len_to_check != 0))
6603f47a03dSEric Biggers 		return -EINVAL;
6613f47a03dSEric Biggers 	return 0;
6623f47a03dSEric Biggers }
6633f47a03dSEric Biggers 
6643f47a03dSEric Biggers static bool is_test_sglist_corrupted(const struct test_sglist *tsgl)
6653f47a03dSEric Biggers {
6663f47a03dSEric Biggers 	unsigned int i;
6673f47a03dSEric Biggers 
6683f47a03dSEric Biggers 	for (i = 0; i < tsgl->nents; i++) {
6693f47a03dSEric Biggers 		if (tsgl->sgl[i].page_link != tsgl->sgl_saved[i].page_link)
6703f47a03dSEric Biggers 			return true;
6713f47a03dSEric Biggers 		if (tsgl->sgl[i].offset != tsgl->sgl_saved[i].offset)
6723f47a03dSEric Biggers 			return true;
6733f47a03dSEric Biggers 		if (tsgl->sgl[i].length != tsgl->sgl_saved[i].length)
6743f47a03dSEric Biggers 			return true;
6753f47a03dSEric Biggers 	}
6763f47a03dSEric Biggers 	return false;
6773f47a03dSEric Biggers }
6783f47a03dSEric Biggers 
6793f47a03dSEric Biggers struct cipher_test_sglists {
6803f47a03dSEric Biggers 	struct test_sglist src;
6813f47a03dSEric Biggers 	struct test_sglist dst;
6823f47a03dSEric Biggers };
6833f47a03dSEric Biggers 
6843f47a03dSEric Biggers static struct cipher_test_sglists *alloc_cipher_test_sglists(void)
6853f47a03dSEric Biggers {
6863f47a03dSEric Biggers 	struct cipher_test_sglists *tsgls;
6873f47a03dSEric Biggers 
6883f47a03dSEric Biggers 	tsgls = kmalloc(sizeof(*tsgls), GFP_KERNEL);
6893f47a03dSEric Biggers 	if (!tsgls)
6903f47a03dSEric Biggers 		return NULL;
6913f47a03dSEric Biggers 
6923f47a03dSEric Biggers 	if (init_test_sglist(&tsgls->src) != 0)
6933f47a03dSEric Biggers 		goto fail_kfree;
6943f47a03dSEric Biggers 	if (init_test_sglist(&tsgls->dst) != 0)
6953f47a03dSEric Biggers 		goto fail_destroy_src;
6963f47a03dSEric Biggers 
6973f47a03dSEric Biggers 	return tsgls;
6983f47a03dSEric Biggers 
6993f47a03dSEric Biggers fail_destroy_src:
7003f47a03dSEric Biggers 	destroy_test_sglist(&tsgls->src);
7013f47a03dSEric Biggers fail_kfree:
7023f47a03dSEric Biggers 	kfree(tsgls);
7033f47a03dSEric Biggers 	return NULL;
7043f47a03dSEric Biggers }
7053f47a03dSEric Biggers 
7063f47a03dSEric Biggers static void free_cipher_test_sglists(struct cipher_test_sglists *tsgls)
7073f47a03dSEric Biggers {
7083f47a03dSEric Biggers 	if (tsgls) {
7093f47a03dSEric Biggers 		destroy_test_sglist(&tsgls->src);
7103f47a03dSEric Biggers 		destroy_test_sglist(&tsgls->dst);
7113f47a03dSEric Biggers 		kfree(tsgls);
7123f47a03dSEric Biggers 	}
7133f47a03dSEric Biggers }
7143f47a03dSEric Biggers 
7153f47a03dSEric Biggers /* Build the src and dst scatterlists for an skcipher or AEAD test */
7163f47a03dSEric Biggers static int build_cipher_test_sglists(struct cipher_test_sglists *tsgls,
7173f47a03dSEric Biggers 				     const struct testvec_config *cfg,
7183f47a03dSEric Biggers 				     unsigned int alignmask,
7193f47a03dSEric Biggers 				     unsigned int src_total_len,
7203f47a03dSEric Biggers 				     unsigned int dst_total_len,
7213f47a03dSEric Biggers 				     const struct kvec *inputs,
7223f47a03dSEric Biggers 				     unsigned int nr_inputs)
7233f47a03dSEric Biggers {
7243f47a03dSEric Biggers 	struct iov_iter input;
7253f47a03dSEric Biggers 	int err;
7263f47a03dSEric Biggers 
7273f47a03dSEric Biggers 	iov_iter_kvec(&input, WRITE, inputs, nr_inputs, src_total_len);
7283f47a03dSEric Biggers 	err = build_test_sglist(&tsgls->src, cfg->src_divs, alignmask,
7293f47a03dSEric Biggers 				cfg->inplace ?
7303f47a03dSEric Biggers 					max(dst_total_len, src_total_len) :
7313f47a03dSEric Biggers 					src_total_len,
7323f47a03dSEric Biggers 				&input, NULL);
7333f47a03dSEric Biggers 	if (err)
7343f47a03dSEric Biggers 		return err;
7353f47a03dSEric Biggers 
7363f47a03dSEric Biggers 	if (cfg->inplace) {
7373f47a03dSEric Biggers 		tsgls->dst.sgl_ptr = tsgls->src.sgl;
7383f47a03dSEric Biggers 		tsgls->dst.nents = tsgls->src.nents;
7393f47a03dSEric Biggers 		return 0;
7403f47a03dSEric Biggers 	}
7413f47a03dSEric Biggers 	return build_test_sglist(&tsgls->dst,
7423f47a03dSEric Biggers 				 cfg->dst_divs[0].proportion_of_total ?
7433f47a03dSEric Biggers 					cfg->dst_divs : cfg->src_divs,
7443f47a03dSEric Biggers 				 alignmask, dst_total_len, NULL, NULL);
745f8b0d4d0SHerbert Xu }
746f8b0d4d0SHerbert Xu 
74725f9dddbSEric Biggers #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
74825f9dddbSEric Biggers static char *generate_random_sgl_divisions(struct test_sg_division *divs,
74925f9dddbSEric Biggers 					   size_t max_divs, char *p, char *end,
7506570737cSEric Biggers 					   bool gen_flushes, u32 req_flags)
75125f9dddbSEric Biggers {
75225f9dddbSEric Biggers 	struct test_sg_division *div = divs;
75325f9dddbSEric Biggers 	unsigned int remaining = TEST_SG_TOTAL;
75425f9dddbSEric Biggers 
75525f9dddbSEric Biggers 	do {
75625f9dddbSEric Biggers 		unsigned int this_len;
7576570737cSEric Biggers 		const char *flushtype_str;
75825f9dddbSEric Biggers 
75925f9dddbSEric Biggers 		if (div == &divs[max_divs - 1] || prandom_u32() % 2 == 0)
76025f9dddbSEric Biggers 			this_len = remaining;
76125f9dddbSEric Biggers 		else
76225f9dddbSEric Biggers 			this_len = 1 + (prandom_u32() % remaining);
76325f9dddbSEric Biggers 		div->proportion_of_total = this_len;
76425f9dddbSEric Biggers 
76525f9dddbSEric Biggers 		if (prandom_u32() % 4 == 0)
76625f9dddbSEric Biggers 			div->offset = (PAGE_SIZE - 128) + (prandom_u32() % 128);
76725f9dddbSEric Biggers 		else if (prandom_u32() % 2 == 0)
76825f9dddbSEric Biggers 			div->offset = prandom_u32() % 32;
76925f9dddbSEric Biggers 		else
77025f9dddbSEric Biggers 			div->offset = prandom_u32() % PAGE_SIZE;
77125f9dddbSEric Biggers 		if (prandom_u32() % 8 == 0)
77225f9dddbSEric Biggers 			div->offset_relative_to_alignmask = true;
77325f9dddbSEric Biggers 
77425f9dddbSEric Biggers 		div->flush_type = FLUSH_TYPE_NONE;
77525f9dddbSEric Biggers 		if (gen_flushes) {
77625f9dddbSEric Biggers 			switch (prandom_u32() % 4) {
77725f9dddbSEric Biggers 			case 0:
77825f9dddbSEric Biggers 				div->flush_type = FLUSH_TYPE_REIMPORT;
77925f9dddbSEric Biggers 				break;
78025f9dddbSEric Biggers 			case 1:
78125f9dddbSEric Biggers 				div->flush_type = FLUSH_TYPE_FLUSH;
78225f9dddbSEric Biggers 				break;
78325f9dddbSEric Biggers 			}
78425f9dddbSEric Biggers 		}
78525f9dddbSEric Biggers 
7866570737cSEric Biggers 		if (div->flush_type != FLUSH_TYPE_NONE &&
7876570737cSEric Biggers 		    !(req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) &&
7886570737cSEric Biggers 		    prandom_u32() % 2 == 0)
7896570737cSEric Biggers 			div->nosimd = true;
7906570737cSEric Biggers 
7916570737cSEric Biggers 		switch (div->flush_type) {
7926570737cSEric Biggers 		case FLUSH_TYPE_FLUSH:
7936570737cSEric Biggers 			if (div->nosimd)
7946570737cSEric Biggers 				flushtype_str = "<flush,nosimd>";
7956570737cSEric Biggers 			else
7966570737cSEric Biggers 				flushtype_str = "<flush>";
7976570737cSEric Biggers 			break;
7986570737cSEric Biggers 		case FLUSH_TYPE_REIMPORT:
7996570737cSEric Biggers 			if (div->nosimd)
8006570737cSEric Biggers 				flushtype_str = "<reimport,nosimd>";
8016570737cSEric Biggers 			else
8026570737cSEric Biggers 				flushtype_str = "<reimport>";
8036570737cSEric Biggers 			break;
8046570737cSEric Biggers 		default:
8056570737cSEric Biggers 			flushtype_str = "";
8066570737cSEric Biggers 			break;
8076570737cSEric Biggers 		}
8086570737cSEric Biggers 
80925f9dddbSEric Biggers 		BUILD_BUG_ON(TEST_SG_TOTAL != 10000); /* for "%u.%u%%" */
8106570737cSEric Biggers 		p += scnprintf(p, end - p, "%s%u.%u%%@%s+%u%s", flushtype_str,
81125f9dddbSEric Biggers 			       this_len / 100, this_len % 100,
81225f9dddbSEric Biggers 			       div->offset_relative_to_alignmask ?
81325f9dddbSEric Biggers 					"alignmask" : "",
81425f9dddbSEric Biggers 			       div->offset, this_len == remaining ? "" : ", ");
81525f9dddbSEric Biggers 		remaining -= this_len;
81625f9dddbSEric Biggers 		div++;
81725f9dddbSEric Biggers 	} while (remaining);
81825f9dddbSEric Biggers 
81925f9dddbSEric Biggers 	return p;
82025f9dddbSEric Biggers }
82125f9dddbSEric Biggers 
82225f9dddbSEric Biggers /* Generate a random testvec_config for fuzz testing */
82325f9dddbSEric Biggers static void generate_random_testvec_config(struct testvec_config *cfg,
82425f9dddbSEric Biggers 					   char *name, size_t max_namelen)
82525f9dddbSEric Biggers {
82625f9dddbSEric Biggers 	char *p = name;
82725f9dddbSEric Biggers 	char * const end = name + max_namelen;
82825f9dddbSEric Biggers 
82925f9dddbSEric Biggers 	memset(cfg, 0, sizeof(*cfg));
83025f9dddbSEric Biggers 
83125f9dddbSEric Biggers 	cfg->name = name;
83225f9dddbSEric Biggers 
83325f9dddbSEric Biggers 	p += scnprintf(p, end - p, "random:");
83425f9dddbSEric Biggers 
83525f9dddbSEric Biggers 	if (prandom_u32() % 2 == 0) {
83625f9dddbSEric Biggers 		cfg->inplace = true;
83725f9dddbSEric Biggers 		p += scnprintf(p, end - p, " inplace");
83825f9dddbSEric Biggers 	}
83925f9dddbSEric Biggers 
84025f9dddbSEric Biggers 	if (prandom_u32() % 2 == 0) {
84125f9dddbSEric Biggers 		cfg->req_flags |= CRYPTO_TFM_REQ_MAY_SLEEP;
84225f9dddbSEric Biggers 		p += scnprintf(p, end - p, " may_sleep");
84325f9dddbSEric Biggers 	}
84425f9dddbSEric Biggers 
84525f9dddbSEric Biggers 	switch (prandom_u32() % 4) {
84625f9dddbSEric Biggers 	case 0:
84725f9dddbSEric Biggers 		cfg->finalization_type = FINALIZATION_TYPE_FINAL;
84825f9dddbSEric Biggers 		p += scnprintf(p, end - p, " use_final");
84925f9dddbSEric Biggers 		break;
85025f9dddbSEric Biggers 	case 1:
85125f9dddbSEric Biggers 		cfg->finalization_type = FINALIZATION_TYPE_FINUP;
85225f9dddbSEric Biggers 		p += scnprintf(p, end - p, " use_finup");
85325f9dddbSEric Biggers 		break;
85425f9dddbSEric Biggers 	default:
85525f9dddbSEric Biggers 		cfg->finalization_type = FINALIZATION_TYPE_DIGEST;
85625f9dddbSEric Biggers 		p += scnprintf(p, end - p, " use_digest");
85725f9dddbSEric Biggers 		break;
85825f9dddbSEric Biggers 	}
85925f9dddbSEric Biggers 
8606570737cSEric Biggers 	if (!(cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) &&
8616570737cSEric Biggers 	    prandom_u32() % 2 == 0) {
8626570737cSEric Biggers 		cfg->nosimd = true;
8636570737cSEric Biggers 		p += scnprintf(p, end - p, " nosimd");
8646570737cSEric Biggers 	}
8656570737cSEric Biggers 
86625f9dddbSEric Biggers 	p += scnprintf(p, end - p, " src_divs=[");
86725f9dddbSEric Biggers 	p = generate_random_sgl_divisions(cfg->src_divs,
86825f9dddbSEric Biggers 					  ARRAY_SIZE(cfg->src_divs), p, end,
86925f9dddbSEric Biggers 					  (cfg->finalization_type !=
8706570737cSEric Biggers 					   FINALIZATION_TYPE_DIGEST),
8716570737cSEric Biggers 					  cfg->req_flags);
87225f9dddbSEric Biggers 	p += scnprintf(p, end - p, "]");
87325f9dddbSEric Biggers 
87425f9dddbSEric Biggers 	if (!cfg->inplace && prandom_u32() % 2 == 0) {
87525f9dddbSEric Biggers 		p += scnprintf(p, end - p, " dst_divs=[");
87625f9dddbSEric Biggers 		p = generate_random_sgl_divisions(cfg->dst_divs,
87725f9dddbSEric Biggers 						  ARRAY_SIZE(cfg->dst_divs),
8786570737cSEric Biggers 						  p, end, false,
8796570737cSEric Biggers 						  cfg->req_flags);
88025f9dddbSEric Biggers 		p += scnprintf(p, end - p, "]");
88125f9dddbSEric Biggers 	}
88225f9dddbSEric Biggers 
88325f9dddbSEric Biggers 	if (prandom_u32() % 2 == 0) {
88425f9dddbSEric Biggers 		cfg->iv_offset = 1 + (prandom_u32() % MAX_ALGAPI_ALIGNMASK);
88525f9dddbSEric Biggers 		p += scnprintf(p, end - p, " iv_offset=%u", cfg->iv_offset);
88625f9dddbSEric Biggers 	}
88725f9dddbSEric Biggers 
88825f9dddbSEric Biggers 	WARN_ON_ONCE(!valid_testvec_config(cfg));
88925f9dddbSEric Biggers }
890b55e1a39SEric Biggers 
891b55e1a39SEric Biggers static void crypto_disable_simd_for_test(void)
892b55e1a39SEric Biggers {
893b55e1a39SEric Biggers 	preempt_disable();
894b55e1a39SEric Biggers 	__this_cpu_write(crypto_simd_disabled_for_test, true);
895b55e1a39SEric Biggers }
896b55e1a39SEric Biggers 
897b55e1a39SEric Biggers static void crypto_reenable_simd_for_test(void)
898b55e1a39SEric Biggers {
899b55e1a39SEric Biggers 	__this_cpu_write(crypto_simd_disabled_for_test, false);
900b55e1a39SEric Biggers 	preempt_enable();
901b55e1a39SEric Biggers }
902b55e1a39SEric Biggers #else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
903b55e1a39SEric Biggers static void crypto_disable_simd_for_test(void)
904b55e1a39SEric Biggers {
905b55e1a39SEric Biggers }
906b55e1a39SEric Biggers 
907b55e1a39SEric Biggers static void crypto_reenable_simd_for_test(void)
908b55e1a39SEric Biggers {
909b55e1a39SEric Biggers }
910b55e1a39SEric Biggers #endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
91125f9dddbSEric Biggers 
9126570737cSEric Biggers static int do_ahash_op(int (*op)(struct ahash_request *req),
9136570737cSEric Biggers 		       struct ahash_request *req,
9146570737cSEric Biggers 		       struct crypto_wait *wait, bool nosimd)
9156570737cSEric Biggers {
9166570737cSEric Biggers 	int err;
9176570737cSEric Biggers 
9186570737cSEric Biggers 	if (nosimd)
9196570737cSEric Biggers 		crypto_disable_simd_for_test();
9206570737cSEric Biggers 
9216570737cSEric Biggers 	err = op(req);
9226570737cSEric Biggers 
9236570737cSEric Biggers 	if (nosimd)
9246570737cSEric Biggers 		crypto_reenable_simd_for_test();
9256570737cSEric Biggers 
9266570737cSEric Biggers 	return crypto_wait_req(err, wait);
9276570737cSEric Biggers }
9286570737cSEric Biggers 
9294cc2dcf9SEric Biggers static int check_nonfinal_hash_op(const char *op, int err,
9304cc2dcf9SEric Biggers 				  u8 *result, unsigned int digestsize,
9314cc2dcf9SEric Biggers 				  const char *driver, unsigned int vec_num,
9324cc2dcf9SEric Biggers 				  const struct testvec_config *cfg)
933466d7b9fSKamil Konieczny {
9344cc2dcf9SEric Biggers 	if (err) {
9354cc2dcf9SEric Biggers 		pr_err("alg: hash: %s %s() failed with err %d on test vector %u, cfg=\"%s\"\n",
9364cc2dcf9SEric Biggers 		       driver, op, err, vec_num, cfg->name);
9374cc2dcf9SEric Biggers 		return err;
9384cc2dcf9SEric Biggers 	}
9394cc2dcf9SEric Biggers 	if (!testmgr_is_poison(result, digestsize)) {
9404cc2dcf9SEric Biggers 		pr_err("alg: hash: %s %s() used result buffer on test vector %u, cfg=\"%s\"\n",
9414cc2dcf9SEric Biggers 		       driver, op, vec_num, cfg->name);
942466d7b9fSKamil Konieczny 		return -EINVAL;
943466d7b9fSKamil Konieczny 	}
944466d7b9fSKamil Konieczny 	return 0;
945466d7b9fSKamil Konieczny }
946466d7b9fSKamil Konieczny 
9474cc2dcf9SEric Biggers static int test_hash_vec_cfg(const char *driver,
9484cc2dcf9SEric Biggers 			     const struct hash_testvec *vec,
9494cc2dcf9SEric Biggers 			     unsigned int vec_num,
9504cc2dcf9SEric Biggers 			     const struct testvec_config *cfg,
9514cc2dcf9SEric Biggers 			     struct ahash_request *req,
9524cc2dcf9SEric Biggers 			     struct test_sglist *tsgl,
9534cc2dcf9SEric Biggers 			     u8 *hashstate)
954018ba95cSWang, Rui Y {
9554cc2dcf9SEric Biggers 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
9564cc2dcf9SEric Biggers 	const unsigned int alignmask = crypto_ahash_alignmask(tfm);
9574cc2dcf9SEric Biggers 	const unsigned int digestsize = crypto_ahash_digestsize(tfm);
9584cc2dcf9SEric Biggers 	const unsigned int statesize = crypto_ahash_statesize(tfm);
9594cc2dcf9SEric Biggers 	const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
9604cc2dcf9SEric Biggers 	const struct test_sg_division *divs[XBUFSIZE];
9614cc2dcf9SEric Biggers 	DECLARE_CRYPTO_WAIT(wait);
9624cc2dcf9SEric Biggers 	struct kvec _input;
9634cc2dcf9SEric Biggers 	struct iov_iter input;
9644cc2dcf9SEric Biggers 	unsigned int i;
9654cc2dcf9SEric Biggers 	struct scatterlist *pending_sgl;
9664cc2dcf9SEric Biggers 	unsigned int pending_len;
9674cc2dcf9SEric Biggers 	u8 result[HASH_MAX_DIGESTSIZE + TESTMGR_POISON_LEN];
9684cc2dcf9SEric Biggers 	int err;
969018ba95cSWang, Rui Y 
9704cc2dcf9SEric Biggers 	/* Set the key, if specified */
9714cc2dcf9SEric Biggers 	if (vec->ksize) {
9724cc2dcf9SEric Biggers 		err = crypto_ahash_setkey(tfm, vec->key, vec->ksize);
9734cc2dcf9SEric Biggers 		if (err) {
9744cc2dcf9SEric Biggers 			pr_err("alg: hash: %s setkey failed with err %d on test vector %u; flags=%#x\n",
9754cc2dcf9SEric Biggers 			       driver, err, vec_num,
9764cc2dcf9SEric Biggers 			       crypto_ahash_get_flags(tfm));
9774cc2dcf9SEric Biggers 			return err;
978da7f033dSHerbert Xu 		}
979da7f033dSHerbert Xu 	}
980da7f033dSHerbert Xu 
9814cc2dcf9SEric Biggers 	/* Build the scatterlist for the source data */
9824cc2dcf9SEric Biggers 	_input.iov_base = (void *)vec->plaintext;
9834cc2dcf9SEric Biggers 	_input.iov_len = vec->psize;
9844cc2dcf9SEric Biggers 	iov_iter_kvec(&input, WRITE, &_input, 1, vec->psize);
9854cc2dcf9SEric Biggers 	err = build_test_sglist(tsgl, cfg->src_divs, alignmask, vec->psize,
9864cc2dcf9SEric Biggers 				&input, divs);
9874cc2dcf9SEric Biggers 	if (err) {
9884cc2dcf9SEric Biggers 		pr_err("alg: hash: %s: error preparing scatterlist for test vector %u, cfg=\"%s\"\n",
9894cc2dcf9SEric Biggers 		       driver, vec_num, cfg->name);
9904cc2dcf9SEric Biggers 		return err;
991a8f1a052SDavid S. Miller 	}
992da7f033dSHerbert Xu 
9934cc2dcf9SEric Biggers 	/* Do the actual hashing */
994da7f033dSHerbert Xu 
9954cc2dcf9SEric Biggers 	testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
9964cc2dcf9SEric Biggers 	testmgr_poison(result, digestsize + TESTMGR_POISON_LEN);
99776715095SGilad Ben-Yossef 
9984cc2dcf9SEric Biggers 	if (cfg->finalization_type == FINALIZATION_TYPE_DIGEST) {
9994cc2dcf9SEric Biggers 		/* Just using digest() */
10004cc2dcf9SEric Biggers 		ahash_request_set_callback(req, req_flags, crypto_req_done,
10017f397136SGilad Ben-Yossef 					   &wait);
10024cc2dcf9SEric Biggers 		ahash_request_set_crypt(req, tsgl->sgl, result, vec->psize);
10036570737cSEric Biggers 		err = do_ahash_op(crypto_ahash_digest, req, &wait, cfg->nosimd);
10044cc2dcf9SEric Biggers 		if (err) {
10054cc2dcf9SEric Biggers 			pr_err("alg: hash: %s digest() failed with err %d on test vector %u, cfg=\"%s\"\n",
10064cc2dcf9SEric Biggers 			       driver, err, vec_num, cfg->name);
10074cc2dcf9SEric Biggers 			return err;
1008018ba95cSWang, Rui Y 		}
10094cc2dcf9SEric Biggers 		goto result_ready;
1010018ba95cSWang, Rui Y 	}
10114cc2dcf9SEric Biggers 
10124cc2dcf9SEric Biggers 	/* Using init(), zero or more update(), then final() or finup() */
10134cc2dcf9SEric Biggers 
10144cc2dcf9SEric Biggers 	ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
10154cc2dcf9SEric Biggers 	ahash_request_set_crypt(req, NULL, result, 0);
10166570737cSEric Biggers 	err = do_ahash_op(crypto_ahash_init, req, &wait, cfg->nosimd);
10174cc2dcf9SEric Biggers 	err = check_nonfinal_hash_op("init", err, result, digestsize,
10184cc2dcf9SEric Biggers 				     driver, vec_num, cfg);
10194cc2dcf9SEric Biggers 	if (err)
10204cc2dcf9SEric Biggers 		return err;
10214cc2dcf9SEric Biggers 
10224cc2dcf9SEric Biggers 	pending_sgl = NULL;
10234cc2dcf9SEric Biggers 	pending_len = 0;
10244cc2dcf9SEric Biggers 	for (i = 0; i < tsgl->nents; i++) {
10254cc2dcf9SEric Biggers 		if (divs[i]->flush_type != FLUSH_TYPE_NONE &&
10264cc2dcf9SEric Biggers 		    pending_sgl != NULL) {
10274cc2dcf9SEric Biggers 			/* update() with the pending data */
10284cc2dcf9SEric Biggers 			ahash_request_set_callback(req, req_flags,
10294cc2dcf9SEric Biggers 						   crypto_req_done, &wait);
10304cc2dcf9SEric Biggers 			ahash_request_set_crypt(req, pending_sgl, result,
10314cc2dcf9SEric Biggers 						pending_len);
10326570737cSEric Biggers 			err = do_ahash_op(crypto_ahash_update, req, &wait,
10336570737cSEric Biggers 					  divs[i]->nosimd);
10344cc2dcf9SEric Biggers 			err = check_nonfinal_hash_op("update", err,
10354cc2dcf9SEric Biggers 						     result, digestsize,
10364cc2dcf9SEric Biggers 						     driver, vec_num, cfg);
10374cc2dcf9SEric Biggers 			if (err)
10384cc2dcf9SEric Biggers 				return err;
10394cc2dcf9SEric Biggers 			pending_sgl = NULL;
10404cc2dcf9SEric Biggers 			pending_len = 0;
1041018ba95cSWang, Rui Y 		}
10424cc2dcf9SEric Biggers 		if (divs[i]->flush_type == FLUSH_TYPE_REIMPORT) {
10434cc2dcf9SEric Biggers 			/* Test ->export() and ->import() */
10444cc2dcf9SEric Biggers 			testmgr_poison(hashstate + statesize,
10454cc2dcf9SEric Biggers 				       TESTMGR_POISON_LEN);
10464cc2dcf9SEric Biggers 			err = crypto_ahash_export(req, hashstate);
10474cc2dcf9SEric Biggers 			err = check_nonfinal_hash_op("export", err,
10484cc2dcf9SEric Biggers 						     result, digestsize,
10494cc2dcf9SEric Biggers 						     driver, vec_num, cfg);
10504cc2dcf9SEric Biggers 			if (err)
10514cc2dcf9SEric Biggers 				return err;
10524cc2dcf9SEric Biggers 			if (!testmgr_is_poison(hashstate + statesize,
10534cc2dcf9SEric Biggers 					       TESTMGR_POISON_LEN)) {
10544cc2dcf9SEric Biggers 				pr_err("alg: hash: %s export() overran state buffer on test vector %u, cfg=\"%s\"\n",
10554cc2dcf9SEric Biggers 				       driver, vec_num, cfg->name);
10564cc2dcf9SEric Biggers 				return -EOVERFLOW;
10574cc2dcf9SEric Biggers 			}
10584cc2dcf9SEric Biggers 
10594cc2dcf9SEric Biggers 			testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
10604cc2dcf9SEric Biggers 			err = crypto_ahash_import(req, hashstate);
10614cc2dcf9SEric Biggers 			err = check_nonfinal_hash_op("import", err,
10624cc2dcf9SEric Biggers 						     result, digestsize,
10634cc2dcf9SEric Biggers 						     driver, vec_num, cfg);
10644cc2dcf9SEric Biggers 			if (err)
10654cc2dcf9SEric Biggers 				return err;
10664cc2dcf9SEric Biggers 		}
10674cc2dcf9SEric Biggers 		if (pending_sgl == NULL)
10684cc2dcf9SEric Biggers 			pending_sgl = &tsgl->sgl[i];
10694cc2dcf9SEric Biggers 		pending_len += tsgl->sgl[i].length;
10704cc2dcf9SEric Biggers 	}
10714cc2dcf9SEric Biggers 
10724cc2dcf9SEric Biggers 	ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
10734cc2dcf9SEric Biggers 	ahash_request_set_crypt(req, pending_sgl, result, pending_len);
10744cc2dcf9SEric Biggers 	if (cfg->finalization_type == FINALIZATION_TYPE_FINAL) {
10754cc2dcf9SEric Biggers 		/* finish with update() and final() */
10766570737cSEric Biggers 		err = do_ahash_op(crypto_ahash_update, req, &wait, cfg->nosimd);
10774cc2dcf9SEric Biggers 		err = check_nonfinal_hash_op("update", err, result, digestsize,
10784cc2dcf9SEric Biggers 					     driver, vec_num, cfg);
10794cc2dcf9SEric Biggers 		if (err)
10804cc2dcf9SEric Biggers 			return err;
10816570737cSEric Biggers 		err = do_ahash_op(crypto_ahash_final, req, &wait, cfg->nosimd);
10824cc2dcf9SEric Biggers 		if (err) {
10834cc2dcf9SEric Biggers 			pr_err("alg: hash: %s final() failed with err %d on test vector %u, cfg=\"%s\"\n",
10844cc2dcf9SEric Biggers 			       driver, err, vec_num, cfg->name);
10854cc2dcf9SEric Biggers 			return err;
10864cc2dcf9SEric Biggers 		}
10874cc2dcf9SEric Biggers 	} else {
10884cc2dcf9SEric Biggers 		/* finish with finup() */
10896570737cSEric Biggers 		err = do_ahash_op(crypto_ahash_finup, req, &wait, cfg->nosimd);
10904cc2dcf9SEric Biggers 		if (err) {
10914cc2dcf9SEric Biggers 			pr_err("alg: hash: %s finup() failed with err %d on test vector %u, cfg=\"%s\"\n",
10924cc2dcf9SEric Biggers 			       driver, err, vec_num, cfg->name);
10934cc2dcf9SEric Biggers 			return err;
1094018ba95cSWang, Rui Y 		}
1095018ba95cSWang, Rui Y 	}
1096018ba95cSWang, Rui Y 
10974cc2dcf9SEric Biggers result_ready:
10984cc2dcf9SEric Biggers 	/* Check that the algorithm produced the correct digest */
10994cc2dcf9SEric Biggers 	if (memcmp(result, vec->digest, digestsize) != 0) {
11004cc2dcf9SEric Biggers 		pr_err("alg: hash: %s test failed (wrong result) on test vector %u, cfg=\"%s\"\n",
11014cc2dcf9SEric Biggers 		       driver, vec_num, cfg->name);
11024cc2dcf9SEric Biggers 		return -EINVAL;
1103da7f033dSHerbert Xu 	}
11044cc2dcf9SEric Biggers 	if (!testmgr_is_poison(&result[digestsize], TESTMGR_POISON_LEN)) {
11054cc2dcf9SEric Biggers 		pr_err("alg: hash: %s overran result buffer on test vector %u, cfg=\"%s\"\n",
11064cc2dcf9SEric Biggers 		       driver, vec_num, cfg->name);
11074cc2dcf9SEric Biggers 		return -EOVERFLOW;
1108da5ffe11SJussi Kivilinna 	}
1109da5ffe11SJussi Kivilinna 
1110da5ffe11SJussi Kivilinna 	return 0;
1111da5ffe11SJussi Kivilinna }
1112da5ffe11SJussi Kivilinna 
11134cc2dcf9SEric Biggers static int test_hash_vec(const char *driver, const struct hash_testvec *vec,
11144cc2dcf9SEric Biggers 			 unsigned int vec_num, struct ahash_request *req,
11154cc2dcf9SEric Biggers 			 struct test_sglist *tsgl, u8 *hashstate)
11164cc2dcf9SEric Biggers {
11174cc2dcf9SEric Biggers 	unsigned int i;
11184cc2dcf9SEric Biggers 	int err;
11194cc2dcf9SEric Biggers 
11204cc2dcf9SEric Biggers 	for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++) {
11214cc2dcf9SEric Biggers 		err = test_hash_vec_cfg(driver, vec, vec_num,
11224cc2dcf9SEric Biggers 					&default_hash_testvec_configs[i],
11234cc2dcf9SEric Biggers 					req, tsgl, hashstate);
11244cc2dcf9SEric Biggers 		if (err)
11254cc2dcf9SEric Biggers 			return err;
11264cc2dcf9SEric Biggers 	}
11274cc2dcf9SEric Biggers 
11284cc2dcf9SEric Biggers #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
11294cc2dcf9SEric Biggers 	if (!noextratests) {
11304cc2dcf9SEric Biggers 		struct testvec_config cfg;
11314cc2dcf9SEric Biggers 		char cfgname[TESTVEC_CONFIG_NAMELEN];
11324cc2dcf9SEric Biggers 
11334cc2dcf9SEric Biggers 		for (i = 0; i < fuzz_iterations; i++) {
11344cc2dcf9SEric Biggers 			generate_random_testvec_config(&cfg, cfgname,
11354cc2dcf9SEric Biggers 						       sizeof(cfgname));
11364cc2dcf9SEric Biggers 			err = test_hash_vec_cfg(driver, vec, vec_num, &cfg,
11374cc2dcf9SEric Biggers 						req, tsgl, hashstate);
11384cc2dcf9SEric Biggers 			if (err)
11394cc2dcf9SEric Biggers 				return err;
11404cc2dcf9SEric Biggers 		}
11414cc2dcf9SEric Biggers 	}
11424cc2dcf9SEric Biggers #endif
11434cc2dcf9SEric Biggers 	return 0;
11444cc2dcf9SEric Biggers }
11454cc2dcf9SEric Biggers 
11464cc2dcf9SEric Biggers static int __alg_test_hash(const struct hash_testvec *vecs,
11474cc2dcf9SEric Biggers 			   unsigned int num_vecs, const char *driver,
11484cc2dcf9SEric Biggers 			   u32 type, u32 mask)
11494cc2dcf9SEric Biggers {
11504cc2dcf9SEric Biggers 	struct crypto_ahash *tfm;
11514cc2dcf9SEric Biggers 	struct ahash_request *req = NULL;
11524cc2dcf9SEric Biggers 	struct test_sglist *tsgl = NULL;
11534cc2dcf9SEric Biggers 	u8 *hashstate = NULL;
11544cc2dcf9SEric Biggers 	unsigned int i;
11554cc2dcf9SEric Biggers 	int err;
11564cc2dcf9SEric Biggers 
11574cc2dcf9SEric Biggers 	tfm = crypto_alloc_ahash(driver, type, mask);
11584cc2dcf9SEric Biggers 	if (IS_ERR(tfm)) {
11594cc2dcf9SEric Biggers 		pr_err("alg: hash: failed to allocate transform for %s: %ld\n",
11604cc2dcf9SEric Biggers 		       driver, PTR_ERR(tfm));
11614cc2dcf9SEric Biggers 		return PTR_ERR(tfm);
11624cc2dcf9SEric Biggers 	}
11634cc2dcf9SEric Biggers 
11644cc2dcf9SEric Biggers 	req = ahash_request_alloc(tfm, GFP_KERNEL);
11654cc2dcf9SEric Biggers 	if (!req) {
11664cc2dcf9SEric Biggers 		pr_err("alg: hash: failed to allocate request for %s\n",
11674cc2dcf9SEric Biggers 		       driver);
11684cc2dcf9SEric Biggers 		err = -ENOMEM;
11694cc2dcf9SEric Biggers 		goto out;
11704cc2dcf9SEric Biggers 	}
11714cc2dcf9SEric Biggers 
11724cc2dcf9SEric Biggers 	tsgl = kmalloc(sizeof(*tsgl), GFP_KERNEL);
11734cc2dcf9SEric Biggers 	if (!tsgl || init_test_sglist(tsgl) != 0) {
11744cc2dcf9SEric Biggers 		pr_err("alg: hash: failed to allocate test buffers for %s\n",
11754cc2dcf9SEric Biggers 		       driver);
11764cc2dcf9SEric Biggers 		kfree(tsgl);
11774cc2dcf9SEric Biggers 		tsgl = NULL;
11784cc2dcf9SEric Biggers 		err = -ENOMEM;
11794cc2dcf9SEric Biggers 		goto out;
11804cc2dcf9SEric Biggers 	}
11814cc2dcf9SEric Biggers 
11824cc2dcf9SEric Biggers 	hashstate = kmalloc(crypto_ahash_statesize(tfm) + TESTMGR_POISON_LEN,
11834cc2dcf9SEric Biggers 			    GFP_KERNEL);
11844cc2dcf9SEric Biggers 	if (!hashstate) {
11854cc2dcf9SEric Biggers 		pr_err("alg: hash: failed to allocate hash state buffer for %s\n",
11864cc2dcf9SEric Biggers 		       driver);
11874cc2dcf9SEric Biggers 		err = -ENOMEM;
11884cc2dcf9SEric Biggers 		goto out;
11894cc2dcf9SEric Biggers 	}
11904cc2dcf9SEric Biggers 
11914cc2dcf9SEric Biggers 	for (i = 0; i < num_vecs; i++) {
11924cc2dcf9SEric Biggers 		err = test_hash_vec(driver, &vecs[i], i, req, tsgl, hashstate);
11934cc2dcf9SEric Biggers 		if (err)
11944cc2dcf9SEric Biggers 			goto out;
11954cc2dcf9SEric Biggers 	}
11964cc2dcf9SEric Biggers 	err = 0;
11974cc2dcf9SEric Biggers out:
11984cc2dcf9SEric Biggers 	kfree(hashstate);
11994cc2dcf9SEric Biggers 	if (tsgl) {
12004cc2dcf9SEric Biggers 		destroy_test_sglist(tsgl);
12014cc2dcf9SEric Biggers 		kfree(tsgl);
12024cc2dcf9SEric Biggers 	}
12034cc2dcf9SEric Biggers 	ahash_request_free(req);
12044cc2dcf9SEric Biggers 	crypto_free_ahash(tfm);
12054cc2dcf9SEric Biggers 	return err;
12064cc2dcf9SEric Biggers }
12074cc2dcf9SEric Biggers 
12084cc2dcf9SEric Biggers static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
12094cc2dcf9SEric Biggers 			 u32 type, u32 mask)
12104cc2dcf9SEric Biggers {
12114cc2dcf9SEric Biggers 	const struct hash_testvec *template = desc->suite.hash.vecs;
12124cc2dcf9SEric Biggers 	unsigned int tcount = desc->suite.hash.count;
12134cc2dcf9SEric Biggers 	unsigned int nr_unkeyed, nr_keyed;
12144cc2dcf9SEric Biggers 	int err;
12154cc2dcf9SEric Biggers 
12164cc2dcf9SEric Biggers 	/*
12174cc2dcf9SEric Biggers 	 * For OPTIONAL_KEY algorithms, we have to do all the unkeyed tests
12184cc2dcf9SEric Biggers 	 * first, before setting a key on the tfm.  To make this easier, we
12194cc2dcf9SEric Biggers 	 * require that the unkeyed test vectors (if any) are listed first.
12204cc2dcf9SEric Biggers 	 */
12214cc2dcf9SEric Biggers 
12224cc2dcf9SEric Biggers 	for (nr_unkeyed = 0; nr_unkeyed < tcount; nr_unkeyed++) {
12234cc2dcf9SEric Biggers 		if (template[nr_unkeyed].ksize)
12244cc2dcf9SEric Biggers 			break;
12254cc2dcf9SEric Biggers 	}
12264cc2dcf9SEric Biggers 	for (nr_keyed = 0; nr_unkeyed + nr_keyed < tcount; nr_keyed++) {
12274cc2dcf9SEric Biggers 		if (!template[nr_unkeyed + nr_keyed].ksize) {
12284cc2dcf9SEric Biggers 			pr_err("alg: hash: test vectors for %s out of order, "
12294cc2dcf9SEric Biggers 			       "unkeyed ones must come first\n", desc->alg);
12304cc2dcf9SEric Biggers 			return -EINVAL;
12314cc2dcf9SEric Biggers 		}
12324cc2dcf9SEric Biggers 	}
12334cc2dcf9SEric Biggers 
12344cc2dcf9SEric Biggers 	err = 0;
12354cc2dcf9SEric Biggers 	if (nr_unkeyed) {
12364cc2dcf9SEric Biggers 		err = __alg_test_hash(template, nr_unkeyed, driver, type, mask);
12374cc2dcf9SEric Biggers 		template += nr_unkeyed;
12384cc2dcf9SEric Biggers 	}
12394cc2dcf9SEric Biggers 
12404cc2dcf9SEric Biggers 	if (!err && nr_keyed)
12414cc2dcf9SEric Biggers 		err = __alg_test_hash(template, nr_keyed, driver, type, mask);
12424cc2dcf9SEric Biggers 
12434cc2dcf9SEric Biggers 	return err;
12444cc2dcf9SEric Biggers }
12454cc2dcf9SEric Biggers 
1246ed96804fSEric Biggers static int test_aead_vec_cfg(const char *driver, int enc,
1247ed96804fSEric Biggers 			     const struct aead_testvec *vec,
1248ed96804fSEric Biggers 			     unsigned int vec_num,
1249ed96804fSEric Biggers 			     const struct testvec_config *cfg,
1250ed96804fSEric Biggers 			     struct aead_request *req,
1251ed96804fSEric Biggers 			     struct cipher_test_sglists *tsgls)
1252da7f033dSHerbert Xu {
1253ed96804fSEric Biggers 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1254ed96804fSEric Biggers 	const unsigned int alignmask = crypto_aead_alignmask(tfm);
1255ed96804fSEric Biggers 	const unsigned int ivsize = crypto_aead_ivsize(tfm);
1256ed96804fSEric Biggers 	const unsigned int authsize = vec->clen - vec->plen;
1257ed96804fSEric Biggers 	const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1258ed96804fSEric Biggers 	const char *op = enc ? "encryption" : "decryption";
1259ed96804fSEric Biggers 	DECLARE_CRYPTO_WAIT(wait);
1260ed96804fSEric Biggers 	u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
1261ed96804fSEric Biggers 	u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
1262ed96804fSEric Biggers 		 cfg->iv_offset +
1263ed96804fSEric Biggers 		 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
1264ed96804fSEric Biggers 	struct kvec input[2];
1265ed96804fSEric Biggers 	int err;
1266f8b0d4d0SHerbert Xu 
1267ed96804fSEric Biggers 	/* Set the key */
1268ed96804fSEric Biggers 	if (vec->wk)
1269ed96804fSEric Biggers 		crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1270d8a32ac2SJussi Kivilinna 	else
1271ed96804fSEric Biggers 		crypto_aead_clear_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1272ed96804fSEric Biggers 	err = crypto_aead_setkey(tfm, vec->key, vec->klen);
1273ed96804fSEric Biggers 	if (err) {
1274ed96804fSEric Biggers 		if (vec->fail) /* expectedly failed to set key? */
1275ed96804fSEric Biggers 			return 0;
1276ed96804fSEric Biggers 		pr_err("alg: aead: %s setkey failed with err %d on test vector %u; flags=%#x\n",
1277ed96804fSEric Biggers 		       driver, err, vec_num, crypto_aead_get_flags(tfm));
1278ed96804fSEric Biggers 		return err;
1279ed96804fSEric Biggers 	}
1280ed96804fSEric Biggers 	if (vec->fail) {
1281ed96804fSEric Biggers 		pr_err("alg: aead: %s setkey unexpectedly succeeded on test vector %u\n",
1282ed96804fSEric Biggers 		       driver, vec_num);
1283ed96804fSEric Biggers 		return -EINVAL;
1284ed96804fSEric Biggers 	}
1285d8a32ac2SJussi Kivilinna 
1286ed96804fSEric Biggers 	/* Set the authentication tag size */
1287ed96804fSEric Biggers 	err = crypto_aead_setauthsize(tfm, authsize);
1288ed96804fSEric Biggers 	if (err) {
1289ed96804fSEric Biggers 		pr_err("alg: aead: %s setauthsize failed with err %d on test vector %u\n",
1290ed96804fSEric Biggers 		       driver, err, vec_num);
1291ed96804fSEric Biggers 		return err;
1292ed96804fSEric Biggers 	}
1293ed96804fSEric Biggers 
1294ed96804fSEric Biggers 	/* The IV must be copied to a buffer, as the algorithm may modify it */
1295ed96804fSEric Biggers 	if (WARN_ON(ivsize > MAX_IVLEN))
1296ed96804fSEric Biggers 		return -EINVAL;
1297ed96804fSEric Biggers 	if (vec->iv)
1298ed96804fSEric Biggers 		memcpy(iv, vec->iv, ivsize);
1299da7f033dSHerbert Xu 	else
1300ed96804fSEric Biggers 		memset(iv, 0, ivsize);
1301da7f033dSHerbert Xu 
1302ed96804fSEric Biggers 	/* Build the src/dst scatterlists */
1303ed96804fSEric Biggers 	input[0].iov_base = (void *)vec->assoc;
1304ed96804fSEric Biggers 	input[0].iov_len = vec->alen;
1305ed96804fSEric Biggers 	input[1].iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
1306ed96804fSEric Biggers 	input[1].iov_len = enc ? vec->plen : vec->clen;
1307ed96804fSEric Biggers 	err = build_cipher_test_sglists(tsgls, cfg, alignmask,
1308ed96804fSEric Biggers 					vec->alen + (enc ? vec->plen :
1309ed96804fSEric Biggers 						     vec->clen),
1310ed96804fSEric Biggers 					vec->alen + (enc ? vec->clen :
1311ed96804fSEric Biggers 						     vec->plen),
1312ed96804fSEric Biggers 					input, 2);
1313ed96804fSEric Biggers 	if (err) {
1314ed96804fSEric Biggers 		pr_err("alg: aead: %s %s: error preparing scatterlists for test vector %u, cfg=\"%s\"\n",
1315ed96804fSEric Biggers 		       driver, op, vec_num, cfg->name);
1316ed96804fSEric Biggers 		return err;
1317da7f033dSHerbert Xu 	}
1318da7f033dSHerbert Xu 
1319ed96804fSEric Biggers 	/* Do the actual encryption or decryption */
1320ed96804fSEric Biggers 	testmgr_poison(req->__ctx, crypto_aead_reqsize(tfm));
1321ed96804fSEric Biggers 	aead_request_set_callback(req, req_flags, crypto_req_done, &wait);
1322ed96804fSEric Biggers 	aead_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
1323ed96804fSEric Biggers 			       enc ? vec->plen : vec->clen, iv);
1324ed96804fSEric Biggers 	aead_request_set_ad(req, vec->alen);
13256570737cSEric Biggers 	if (cfg->nosimd)
13266570737cSEric Biggers 		crypto_disable_simd_for_test();
13276570737cSEric Biggers 	err = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
13286570737cSEric Biggers 	if (cfg->nosimd)
13296570737cSEric Biggers 		crypto_reenable_simd_for_test();
13306570737cSEric Biggers 	err = crypto_wait_req(err, &wait);
1331ed96804fSEric Biggers 	if (err) {
1332ed96804fSEric Biggers 		if (err == -EBADMSG && vec->novrfy)
1333ed96804fSEric Biggers 			return 0;
1334ed96804fSEric Biggers 		pr_err("alg: aead: %s %s failed with err %d on test vector %u, cfg=\"%s\"\n",
1335ed96804fSEric Biggers 		       driver, op, err, vec_num, cfg->name);
1336ed96804fSEric Biggers 		return err;
1337ed96804fSEric Biggers 	}
1338ed96804fSEric Biggers 	if (vec->novrfy) {
1339ed96804fSEric Biggers 		pr_err("alg: aead: %s %s unexpectedly succeeded on test vector %u, cfg=\"%s\"\n",
1340ed96804fSEric Biggers 		       driver, op, vec_num, cfg->name);
1341ed96804fSEric Biggers 		return -EINVAL;
1342a0d608eeSEric Biggers 	}
134305b1d338SCristian Stoica 
1344a6e5ef9bSEric Biggers 	/* Check that the algorithm didn't overwrite things it shouldn't have */
1345a6e5ef9bSEric Biggers 	if (req->cryptlen != (enc ? vec->plen : vec->clen) ||
1346a6e5ef9bSEric Biggers 	    req->assoclen != vec->alen ||
1347a6e5ef9bSEric Biggers 	    req->iv != iv ||
1348a6e5ef9bSEric Biggers 	    req->src != tsgls->src.sgl_ptr ||
1349a6e5ef9bSEric Biggers 	    req->dst != tsgls->dst.sgl_ptr ||
1350a6e5ef9bSEric Biggers 	    crypto_aead_reqtfm(req) != tfm ||
1351a6e5ef9bSEric Biggers 	    req->base.complete != crypto_req_done ||
1352a6e5ef9bSEric Biggers 	    req->base.flags != req_flags ||
1353a6e5ef9bSEric Biggers 	    req->base.data != &wait) {
1354a6e5ef9bSEric Biggers 		pr_err("alg: aead: %s %s corrupted request struct on test vector %u, cfg=\"%s\"\n",
1355a6e5ef9bSEric Biggers 		       driver, op, vec_num, cfg->name);
1356a6e5ef9bSEric Biggers 		if (req->cryptlen != (enc ? vec->plen : vec->clen))
1357a6e5ef9bSEric Biggers 			pr_err("alg: aead: changed 'req->cryptlen'\n");
1358a6e5ef9bSEric Biggers 		if (req->assoclen != vec->alen)
1359a6e5ef9bSEric Biggers 			pr_err("alg: aead: changed 'req->assoclen'\n");
1360a6e5ef9bSEric Biggers 		if (req->iv != iv)
1361a6e5ef9bSEric Biggers 			pr_err("alg: aead: changed 'req->iv'\n");
1362a6e5ef9bSEric Biggers 		if (req->src != tsgls->src.sgl_ptr)
1363a6e5ef9bSEric Biggers 			pr_err("alg: aead: changed 'req->src'\n");
1364a6e5ef9bSEric Biggers 		if (req->dst != tsgls->dst.sgl_ptr)
1365a6e5ef9bSEric Biggers 			pr_err("alg: aead: changed 'req->dst'\n");
1366a6e5ef9bSEric Biggers 		if (crypto_aead_reqtfm(req) != tfm)
1367a6e5ef9bSEric Biggers 			pr_err("alg: aead: changed 'req->base.tfm'\n");
1368a6e5ef9bSEric Biggers 		if (req->base.complete != crypto_req_done)
1369a6e5ef9bSEric Biggers 			pr_err("alg: aead: changed 'req->base.complete'\n");
1370a6e5ef9bSEric Biggers 		if (req->base.flags != req_flags)
1371a6e5ef9bSEric Biggers 			pr_err("alg: aead: changed 'req->base.flags'\n");
1372a6e5ef9bSEric Biggers 		if (req->base.data != &wait)
1373a6e5ef9bSEric Biggers 			pr_err("alg: aead: changed 'req->base.data'\n");
1374a6e5ef9bSEric Biggers 		return -EINVAL;
1375a6e5ef9bSEric Biggers 	}
1376a6e5ef9bSEric Biggers 	if (is_test_sglist_corrupted(&tsgls->src)) {
1377a6e5ef9bSEric Biggers 		pr_err("alg: aead: %s %s corrupted src sgl on test vector %u, cfg=\"%s\"\n",
1378a6e5ef9bSEric Biggers 		       driver, op, vec_num, cfg->name);
1379a6e5ef9bSEric Biggers 		return -EINVAL;
1380a6e5ef9bSEric Biggers 	}
1381a6e5ef9bSEric Biggers 	if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
1382a6e5ef9bSEric Biggers 	    is_test_sglist_corrupted(&tsgls->dst)) {
1383a6e5ef9bSEric Biggers 		pr_err("alg: aead: %s %s corrupted dst sgl on test vector %u, cfg=\"%s\"\n",
1384a6e5ef9bSEric Biggers 		       driver, op, vec_num, cfg->name);
1385a6e5ef9bSEric Biggers 		return -EINVAL;
1386a6e5ef9bSEric Biggers 	}
1387a6e5ef9bSEric Biggers 
1388ed96804fSEric Biggers 	/* Check for the correct output (ciphertext or plaintext) */
1389ed96804fSEric Biggers 	err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
1390ed96804fSEric Biggers 				    enc ? vec->clen : vec->plen,
1391ed96804fSEric Biggers 				    vec->alen, enc || !cfg->inplace);
1392ed96804fSEric Biggers 	if (err == -EOVERFLOW) {
1393ed96804fSEric Biggers 		pr_err("alg: aead: %s %s overran dst buffer on test vector %u, cfg=\"%s\"\n",
1394ed96804fSEric Biggers 		       driver, op, vec_num, cfg->name);
1395ed96804fSEric Biggers 		return err;
139629b77e5dSHoria Geanta 	}
1397ed96804fSEric Biggers 	if (err) {
1398ed96804fSEric Biggers 		pr_err("alg: aead: %s %s test failed (wrong result) on test vector %u, cfg=\"%s\"\n",
1399ed96804fSEric Biggers 		       driver, op, vec_num, cfg->name);
1400ed96804fSEric Biggers 		return err;
140158dcf548SJussi Kivilinna 	}
140258dcf548SJussi Kivilinna 
140358dcf548SJussi Kivilinna 	return 0;
1404d8a32ac2SJussi Kivilinna }
1405d8a32ac2SJussi Kivilinna 
1406ed96804fSEric Biggers static int test_aead_vec(const char *driver, int enc,
1407ed96804fSEric Biggers 			 const struct aead_testvec *vec, unsigned int vec_num,
1408ed96804fSEric Biggers 			 struct aead_request *req,
1409ed96804fSEric Biggers 			 struct cipher_test_sglists *tsgls)
1410ed96804fSEric Biggers {
1411ed96804fSEric Biggers 	unsigned int i;
1412ed96804fSEric Biggers 	int err;
1413ed96804fSEric Biggers 
1414ed96804fSEric Biggers 	if (enc && vec->novrfy)
1415ed96804fSEric Biggers 		return 0;
1416ed96804fSEric Biggers 
1417ed96804fSEric Biggers 	for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
1418ed96804fSEric Biggers 		err = test_aead_vec_cfg(driver, enc, vec, vec_num,
1419ed96804fSEric Biggers 					&default_cipher_testvec_configs[i],
1420ed96804fSEric Biggers 					req, tsgls);
1421ed96804fSEric Biggers 		if (err)
1422ed96804fSEric Biggers 			return err;
1423ed96804fSEric Biggers 	}
1424ed96804fSEric Biggers 
1425ed96804fSEric Biggers #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1426ed96804fSEric Biggers 	if (!noextratests) {
1427ed96804fSEric Biggers 		struct testvec_config cfg;
1428ed96804fSEric Biggers 		char cfgname[TESTVEC_CONFIG_NAMELEN];
1429ed96804fSEric Biggers 
1430ed96804fSEric Biggers 		for (i = 0; i < fuzz_iterations; i++) {
1431ed96804fSEric Biggers 			generate_random_testvec_config(&cfg, cfgname,
1432ed96804fSEric Biggers 						       sizeof(cfgname));
1433ed96804fSEric Biggers 			err = test_aead_vec_cfg(driver, enc, vec, vec_num,
1434ed96804fSEric Biggers 						&cfg, req, tsgls);
1435ed96804fSEric Biggers 			if (err)
1436ed96804fSEric Biggers 				return err;
1437ed96804fSEric Biggers 		}
1438ed96804fSEric Biggers 	}
1439ed96804fSEric Biggers #endif
1440ed96804fSEric Biggers 	return 0;
1441ed96804fSEric Biggers }
1442ed96804fSEric Biggers 
1443ed96804fSEric Biggers static int test_aead(const char *driver, int enc,
1444ed96804fSEric Biggers 		     const struct aead_test_suite *suite,
1445ed96804fSEric Biggers 		     struct aead_request *req,
1446ed96804fSEric Biggers 		     struct cipher_test_sglists *tsgls)
1447ed96804fSEric Biggers {
1448ed96804fSEric Biggers 	unsigned int i;
1449ed96804fSEric Biggers 	int err;
1450ed96804fSEric Biggers 
1451ed96804fSEric Biggers 	for (i = 0; i < suite->count; i++) {
1452ed96804fSEric Biggers 		err = test_aead_vec(driver, enc, &suite->vecs[i], i, req,
1453ed96804fSEric Biggers 				    tsgls);
1454ed96804fSEric Biggers 		if (err)
1455ed96804fSEric Biggers 			return err;
1456ed96804fSEric Biggers 	}
1457ed96804fSEric Biggers 	return 0;
1458ed96804fSEric Biggers }
1459ed96804fSEric Biggers 
1460ed96804fSEric Biggers static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
1461ed96804fSEric Biggers 			 u32 type, u32 mask)
1462ed96804fSEric Biggers {
1463ed96804fSEric Biggers 	const struct aead_test_suite *suite = &desc->suite.aead;
1464ed96804fSEric Biggers 	struct crypto_aead *tfm;
1465ed96804fSEric Biggers 	struct aead_request *req = NULL;
1466ed96804fSEric Biggers 	struct cipher_test_sglists *tsgls = NULL;
1467ed96804fSEric Biggers 	int err;
1468ed96804fSEric Biggers 
1469ed96804fSEric Biggers 	if (suite->count <= 0) {
1470ed96804fSEric Biggers 		pr_err("alg: aead: empty test suite for %s\n", driver);
1471ed96804fSEric Biggers 		return -EINVAL;
1472ed96804fSEric Biggers 	}
1473ed96804fSEric Biggers 
1474ed96804fSEric Biggers 	tfm = crypto_alloc_aead(driver, type, mask);
1475ed96804fSEric Biggers 	if (IS_ERR(tfm)) {
1476ed96804fSEric Biggers 		pr_err("alg: aead: failed to allocate transform for %s: %ld\n",
1477ed96804fSEric Biggers 		       driver, PTR_ERR(tfm));
1478ed96804fSEric Biggers 		return PTR_ERR(tfm);
1479ed96804fSEric Biggers 	}
1480ed96804fSEric Biggers 
1481ed96804fSEric Biggers 	req = aead_request_alloc(tfm, GFP_KERNEL);
1482ed96804fSEric Biggers 	if (!req) {
1483ed96804fSEric Biggers 		pr_err("alg: aead: failed to allocate request for %s\n",
1484ed96804fSEric Biggers 		       driver);
1485ed96804fSEric Biggers 		err = -ENOMEM;
1486ed96804fSEric Biggers 		goto out;
1487ed96804fSEric Biggers 	}
1488ed96804fSEric Biggers 
1489ed96804fSEric Biggers 	tsgls = alloc_cipher_test_sglists();
1490ed96804fSEric Biggers 	if (!tsgls) {
1491ed96804fSEric Biggers 		pr_err("alg: aead: failed to allocate test buffers for %s\n",
1492ed96804fSEric Biggers 		       driver);
1493ed96804fSEric Biggers 		err = -ENOMEM;
1494ed96804fSEric Biggers 		goto out;
1495ed96804fSEric Biggers 	}
1496ed96804fSEric Biggers 
1497ed96804fSEric Biggers 	err = test_aead(driver, ENCRYPT, suite, req, tsgls);
1498ed96804fSEric Biggers 	if (err)
1499ed96804fSEric Biggers 		goto out;
1500ed96804fSEric Biggers 
1501ed96804fSEric Biggers 	err = test_aead(driver, DECRYPT, suite, req, tsgls);
1502ed96804fSEric Biggers out:
1503ed96804fSEric Biggers 	free_cipher_test_sglists(tsgls);
1504ed96804fSEric Biggers 	aead_request_free(req);
1505ed96804fSEric Biggers 	crypto_free_aead(tfm);
1506ed96804fSEric Biggers 	return err;
1507ed96804fSEric Biggers }
1508ed96804fSEric Biggers 
15091aa4ecd9SHerbert Xu static int test_cipher(struct crypto_cipher *tfm, int enc,
1510b13b1e0cSEric Biggers 		       const struct cipher_testvec *template,
1511b13b1e0cSEric Biggers 		       unsigned int tcount)
15121aa4ecd9SHerbert Xu {
15131aa4ecd9SHerbert Xu 	const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
15141aa4ecd9SHerbert Xu 	unsigned int i, j, k;
15151aa4ecd9SHerbert Xu 	char *q;
15161aa4ecd9SHerbert Xu 	const char *e;
151792a4c9feSEric Biggers 	const char *input, *result;
15181aa4ecd9SHerbert Xu 	void *data;
1519f8b0d4d0SHerbert Xu 	char *xbuf[XBUFSIZE];
1520f8b0d4d0SHerbert Xu 	int ret = -ENOMEM;
1521f8b0d4d0SHerbert Xu 
1522f8b0d4d0SHerbert Xu 	if (testmgr_alloc_buf(xbuf))
1523f8b0d4d0SHerbert Xu 		goto out_nobuf;
15241aa4ecd9SHerbert Xu 
15251aa4ecd9SHerbert Xu 	if (enc == ENCRYPT)
15261aa4ecd9SHerbert Xu 	        e = "encryption";
15271aa4ecd9SHerbert Xu 	else
15281aa4ecd9SHerbert Xu 		e = "decryption";
15291aa4ecd9SHerbert Xu 
15301aa4ecd9SHerbert Xu 	j = 0;
15311aa4ecd9SHerbert Xu 	for (i = 0; i < tcount; i++) {
15321aa4ecd9SHerbert Xu 
153310faa8c0SStephan Mueller 		if (fips_enabled && template[i].fips_skip)
153410faa8c0SStephan Mueller 			continue;
153510faa8c0SStephan Mueller 
153692a4c9feSEric Biggers 		input  = enc ? template[i].ptext : template[i].ctext;
153792a4c9feSEric Biggers 		result = enc ? template[i].ctext : template[i].ptext;
15381aa4ecd9SHerbert Xu 		j++;
15391aa4ecd9SHerbert Xu 
1540fd57f22aSHerbert Xu 		ret = -EINVAL;
154192a4c9feSEric Biggers 		if (WARN_ON(template[i].len > PAGE_SIZE))
1542fd57f22aSHerbert Xu 			goto out;
1543fd57f22aSHerbert Xu 
15441aa4ecd9SHerbert Xu 		data = xbuf[0];
154592a4c9feSEric Biggers 		memcpy(data, input, template[i].len);
15461aa4ecd9SHerbert Xu 
15471aa4ecd9SHerbert Xu 		crypto_cipher_clear_flags(tfm, ~0);
15481aa4ecd9SHerbert Xu 		if (template[i].wk)
1549231baecdSEric Biggers 			crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
15501aa4ecd9SHerbert Xu 
15511aa4ecd9SHerbert Xu 		ret = crypto_cipher_setkey(tfm, template[i].key,
15521aa4ecd9SHerbert Xu 					   template[i].klen);
15530fae0c1eSYanjiang Jin 		if (template[i].fail == !ret) {
15541aa4ecd9SHerbert Xu 			printk(KERN_ERR "alg: cipher: setkey failed "
15551aa4ecd9SHerbert Xu 			       "on test %d for %s: flags=%x\n", j,
15561aa4ecd9SHerbert Xu 			       algo, crypto_cipher_get_flags(tfm));
15571aa4ecd9SHerbert Xu 			goto out;
15581aa4ecd9SHerbert Xu 		} else if (ret)
15591aa4ecd9SHerbert Xu 			continue;
15601aa4ecd9SHerbert Xu 
156192a4c9feSEric Biggers 		for (k = 0; k < template[i].len;
15621aa4ecd9SHerbert Xu 		     k += crypto_cipher_blocksize(tfm)) {
15631aa4ecd9SHerbert Xu 			if (enc)
15641aa4ecd9SHerbert Xu 				crypto_cipher_encrypt_one(tfm, data + k,
15651aa4ecd9SHerbert Xu 							  data + k);
15661aa4ecd9SHerbert Xu 			else
15671aa4ecd9SHerbert Xu 				crypto_cipher_decrypt_one(tfm, data + k,
15681aa4ecd9SHerbert Xu 							  data + k);
15691aa4ecd9SHerbert Xu 		}
15701aa4ecd9SHerbert Xu 
15711aa4ecd9SHerbert Xu 		q = data;
157292a4c9feSEric Biggers 		if (memcmp(q, result, template[i].len)) {
15731aa4ecd9SHerbert Xu 			printk(KERN_ERR "alg: cipher: Test %d failed "
15741aa4ecd9SHerbert Xu 			       "on %s for %s\n", j, e, algo);
157592a4c9feSEric Biggers 			hexdump(q, template[i].len);
15761aa4ecd9SHerbert Xu 			ret = -EINVAL;
15771aa4ecd9SHerbert Xu 			goto out;
15781aa4ecd9SHerbert Xu 		}
15791aa4ecd9SHerbert Xu 	}
15801aa4ecd9SHerbert Xu 
15811aa4ecd9SHerbert Xu 	ret = 0;
15821aa4ecd9SHerbert Xu 
15831aa4ecd9SHerbert Xu out:
1584f8b0d4d0SHerbert Xu 	testmgr_free_buf(xbuf);
1585f8b0d4d0SHerbert Xu out_nobuf:
15861aa4ecd9SHerbert Xu 	return ret;
15871aa4ecd9SHerbert Xu }
15881aa4ecd9SHerbert Xu 
15894e7babbaSEric Biggers static int test_skcipher_vec_cfg(const char *driver, int enc,
15904e7babbaSEric Biggers 				 const struct cipher_testvec *vec,
15914e7babbaSEric Biggers 				 unsigned int vec_num,
15924e7babbaSEric Biggers 				 const struct testvec_config *cfg,
15934e7babbaSEric Biggers 				 struct skcipher_request *req,
15944e7babbaSEric Biggers 				 struct cipher_test_sglists *tsgls)
1595da7f033dSHerbert Xu {
15964e7babbaSEric Biggers 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
15974e7babbaSEric Biggers 	const unsigned int alignmask = crypto_skcipher_alignmask(tfm);
15984e7babbaSEric Biggers 	const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
15994e7babbaSEric Biggers 	const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
16004e7babbaSEric Biggers 	const char *op = enc ? "encryption" : "decryption";
16014e7babbaSEric Biggers 	DECLARE_CRYPTO_WAIT(wait);
16024e7babbaSEric Biggers 	u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
16034e7babbaSEric Biggers 	u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
16044e7babbaSEric Biggers 		 cfg->iv_offset +
16054e7babbaSEric Biggers 		 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
16064e7babbaSEric Biggers 	struct kvec input;
16074e7babbaSEric Biggers 	int err;
1608f8b0d4d0SHerbert Xu 
16094e7babbaSEric Biggers 	/* Set the key */
16104e7babbaSEric Biggers 	if (vec->wk)
1611231baecdSEric Biggers 		crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1612da7f033dSHerbert Xu 	else
16134e7babbaSEric Biggers 		crypto_skcipher_clear_flags(tfm,
16144e7babbaSEric Biggers 					    CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
16154e7babbaSEric Biggers 	err = crypto_skcipher_setkey(tfm, vec->key, vec->klen);
16164e7babbaSEric Biggers 	if (err) {
16174e7babbaSEric Biggers 		if (vec->fail) /* expectedly failed to set key? */
16184e7babbaSEric Biggers 			return 0;
16194e7babbaSEric Biggers 		pr_err("alg: skcipher: %s setkey failed with err %d on test vector %u; flags=%#x\n",
16204e7babbaSEric Biggers 		       driver, err, vec_num, crypto_skcipher_get_flags(tfm));
16214e7babbaSEric Biggers 		return err;
16224e7babbaSEric Biggers 	}
16234e7babbaSEric Biggers 	if (vec->fail) {
16244e7babbaSEric Biggers 		pr_err("alg: skcipher: %s setkey unexpectedly succeeded on test vector %u\n",
16254e7babbaSEric Biggers 		       driver, vec_num);
16264e7babbaSEric Biggers 		return -EINVAL;
162708d6af8cSJussi Kivilinna 	}
1628da7f033dSHerbert Xu 
16294e7babbaSEric Biggers 	/* The IV must be copied to a buffer, as the algorithm may modify it */
16304e7babbaSEric Biggers 	if (ivsize) {
16314e7babbaSEric Biggers 		if (WARN_ON(ivsize > MAX_IVLEN))
16324e7babbaSEric Biggers 			return -EINVAL;
16338efd972eSEric Biggers 		if (vec->generates_iv && !enc)
16348efd972eSEric Biggers 			memcpy(iv, vec->iv_out, ivsize);
16358efd972eSEric Biggers 		else if (vec->iv)
16364e7babbaSEric Biggers 			memcpy(iv, vec->iv, ivsize);
163708d6af8cSJussi Kivilinna 		else
16384e7babbaSEric Biggers 			memset(iv, 0, ivsize);
16394e7babbaSEric Biggers 	} else {
16404e7babbaSEric Biggers 		if (vec->generates_iv) {
16414e7babbaSEric Biggers 			pr_err("alg: skcipher: %s has ivsize=0 but test vector %u generates IV!\n",
16424e7babbaSEric Biggers 			       driver, vec_num);
16434e7babbaSEric Biggers 			return -EINVAL;
16444e7babbaSEric Biggers 		}
16454e7babbaSEric Biggers 		iv = NULL;
1646da7f033dSHerbert Xu 	}
1647da7f033dSHerbert Xu 
16484e7babbaSEric Biggers 	/* Build the src/dst scatterlists */
16494e7babbaSEric Biggers 	input.iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
16504e7babbaSEric Biggers 	input.iov_len = vec->len;
16514e7babbaSEric Biggers 	err = build_cipher_test_sglists(tsgls, cfg, alignmask,
16524e7babbaSEric Biggers 					vec->len, vec->len, &input, 1);
16534e7babbaSEric Biggers 	if (err) {
16544e7babbaSEric Biggers 		pr_err("alg: skcipher: %s %s: error preparing scatterlists for test vector %u, cfg=\"%s\"\n",
16554e7babbaSEric Biggers 		       driver, op, vec_num, cfg->name);
16564e7babbaSEric Biggers 		return err;
1657da7f033dSHerbert Xu 	}
1658da7f033dSHerbert Xu 
16594e7babbaSEric Biggers 	/* Do the actual encryption or decryption */
16604e7babbaSEric Biggers 	testmgr_poison(req->__ctx, crypto_skcipher_reqsize(tfm));
16614e7babbaSEric Biggers 	skcipher_request_set_callback(req, req_flags, crypto_req_done, &wait);
16624e7babbaSEric Biggers 	skcipher_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
16634e7babbaSEric Biggers 				   vec->len, iv);
16646570737cSEric Biggers 	if (cfg->nosimd)
16656570737cSEric Biggers 		crypto_disable_simd_for_test();
16666570737cSEric Biggers 	err = enc ? crypto_skcipher_encrypt(req) : crypto_skcipher_decrypt(req);
16676570737cSEric Biggers 	if (cfg->nosimd)
16686570737cSEric Biggers 		crypto_reenable_simd_for_test();
16696570737cSEric Biggers 	err = crypto_wait_req(err, &wait);
16704e7babbaSEric Biggers 	if (err) {
16714e7babbaSEric Biggers 		pr_err("alg: skcipher: %s %s failed with err %d on test vector %u, cfg=\"%s\"\n",
16724e7babbaSEric Biggers 		       driver, op, err, vec_num, cfg->name);
16734e7babbaSEric Biggers 		return err;
1674da7f033dSHerbert Xu 	}
1675da7f033dSHerbert Xu 
1676fa353c99SEric Biggers 	/* Check that the algorithm didn't overwrite things it shouldn't have */
1677fa353c99SEric Biggers 	if (req->cryptlen != vec->len ||
1678fa353c99SEric Biggers 	    req->iv != iv ||
1679fa353c99SEric Biggers 	    req->src != tsgls->src.sgl_ptr ||
1680fa353c99SEric Biggers 	    req->dst != tsgls->dst.sgl_ptr ||
1681fa353c99SEric Biggers 	    crypto_skcipher_reqtfm(req) != tfm ||
1682fa353c99SEric Biggers 	    req->base.complete != crypto_req_done ||
1683fa353c99SEric Biggers 	    req->base.flags != req_flags ||
1684fa353c99SEric Biggers 	    req->base.data != &wait) {
1685fa353c99SEric Biggers 		pr_err("alg: skcipher: %s %s corrupted request struct on test vector %u, cfg=\"%s\"\n",
1686fa353c99SEric Biggers 		       driver, op, vec_num, cfg->name);
1687fa353c99SEric Biggers 		if (req->cryptlen != vec->len)
1688fa353c99SEric Biggers 			pr_err("alg: skcipher: changed 'req->cryptlen'\n");
1689fa353c99SEric Biggers 		if (req->iv != iv)
1690fa353c99SEric Biggers 			pr_err("alg: skcipher: changed 'req->iv'\n");
1691fa353c99SEric Biggers 		if (req->src != tsgls->src.sgl_ptr)
1692fa353c99SEric Biggers 			pr_err("alg: skcipher: changed 'req->src'\n");
1693fa353c99SEric Biggers 		if (req->dst != tsgls->dst.sgl_ptr)
1694fa353c99SEric Biggers 			pr_err("alg: skcipher: changed 'req->dst'\n");
1695fa353c99SEric Biggers 		if (crypto_skcipher_reqtfm(req) != tfm)
1696fa353c99SEric Biggers 			pr_err("alg: skcipher: changed 'req->base.tfm'\n");
1697fa353c99SEric Biggers 		if (req->base.complete != crypto_req_done)
1698fa353c99SEric Biggers 			pr_err("alg: skcipher: changed 'req->base.complete'\n");
1699fa353c99SEric Biggers 		if (req->base.flags != req_flags)
1700fa353c99SEric Biggers 			pr_err("alg: skcipher: changed 'req->base.flags'\n");
1701fa353c99SEric Biggers 		if (req->base.data != &wait)
1702fa353c99SEric Biggers 			pr_err("alg: skcipher: changed 'req->base.data'\n");
1703fa353c99SEric Biggers 		return -EINVAL;
1704fa353c99SEric Biggers 	}
1705fa353c99SEric Biggers 	if (is_test_sglist_corrupted(&tsgls->src)) {
1706fa353c99SEric Biggers 		pr_err("alg: skcipher: %s %s corrupted src sgl on test vector %u, cfg=\"%s\"\n",
1707fa353c99SEric Biggers 		       driver, op, vec_num, cfg->name);
1708fa353c99SEric Biggers 		return -EINVAL;
1709fa353c99SEric Biggers 	}
1710fa353c99SEric Biggers 	if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
1711fa353c99SEric Biggers 	    is_test_sglist_corrupted(&tsgls->dst)) {
1712fa353c99SEric Biggers 		pr_err("alg: skcipher: %s %s corrupted dst sgl on test vector %u, cfg=\"%s\"\n",
1713fa353c99SEric Biggers 		       driver, op, vec_num, cfg->name);
1714fa353c99SEric Biggers 		return -EINVAL;
1715fa353c99SEric Biggers 	}
1716fa353c99SEric Biggers 
17174e7babbaSEric Biggers 	/* Check for the correct output (ciphertext or plaintext) */
17184e7babbaSEric Biggers 	err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
17194e7babbaSEric Biggers 				    vec->len, 0, true);
17204e7babbaSEric Biggers 	if (err == -EOVERFLOW) {
17214e7babbaSEric Biggers 		pr_err("alg: skcipher: %s %s overran dst buffer on test vector %u, cfg=\"%s\"\n",
17224e7babbaSEric Biggers 		       driver, op, vec_num, cfg->name);
17234e7babbaSEric Biggers 		return err;
17244e7babbaSEric Biggers 	}
17254e7babbaSEric Biggers 	if (err) {
17264e7babbaSEric Biggers 		pr_err("alg: skcipher: %s %s test failed (wrong result) on test vector %u, cfg=\"%s\"\n",
17274e7babbaSEric Biggers 		       driver, op, vec_num, cfg->name);
17284e7babbaSEric Biggers 		return err;
17294e7babbaSEric Biggers 	}
173008d6af8cSJussi Kivilinna 
17314e7babbaSEric Biggers 	/* If applicable, check that the algorithm generated the correct IV */
17328efd972eSEric Biggers 	if (vec->iv_out && memcmp(iv, vec->iv_out, ivsize) != 0) {
17334e7babbaSEric Biggers 		pr_err("alg: skcipher: %s %s test failed (wrong output IV) on test vector %u, cfg=\"%s\"\n",
17344e7babbaSEric Biggers 		       driver, op, vec_num, cfg->name);
17354e7babbaSEric Biggers 		hexdump(iv, ivsize);
17364e7babbaSEric Biggers 		return -EINVAL;
17373a338f20SJussi Kivilinna 	}
17383a338f20SJussi Kivilinna 
17393a338f20SJussi Kivilinna 	return 0;
174008d6af8cSJussi Kivilinna }
174108d6af8cSJussi Kivilinna 
17424e7babbaSEric Biggers static int test_skcipher_vec(const char *driver, int enc,
17434e7babbaSEric Biggers 			     const struct cipher_testvec *vec,
17444e7babbaSEric Biggers 			     unsigned int vec_num,
17454e7babbaSEric Biggers 			     struct skcipher_request *req,
17464e7babbaSEric Biggers 			     struct cipher_test_sglists *tsgls)
17474e7babbaSEric Biggers {
17484e7babbaSEric Biggers 	unsigned int i;
17494e7babbaSEric Biggers 	int err;
17504e7babbaSEric Biggers 
17514e7babbaSEric Biggers 	if (fips_enabled && vec->fips_skip)
17524e7babbaSEric Biggers 		return 0;
17534e7babbaSEric Biggers 
17544e7babbaSEric Biggers 	for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
17554e7babbaSEric Biggers 		err = test_skcipher_vec_cfg(driver, enc, vec, vec_num,
17564e7babbaSEric Biggers 					    &default_cipher_testvec_configs[i],
17574e7babbaSEric Biggers 					    req, tsgls);
17584e7babbaSEric Biggers 		if (err)
17594e7babbaSEric Biggers 			return err;
17604e7babbaSEric Biggers 	}
17614e7babbaSEric Biggers 
17624e7babbaSEric Biggers #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
17634e7babbaSEric Biggers 	if (!noextratests) {
17644e7babbaSEric Biggers 		struct testvec_config cfg;
17654e7babbaSEric Biggers 		char cfgname[TESTVEC_CONFIG_NAMELEN];
17664e7babbaSEric Biggers 
17674e7babbaSEric Biggers 		for (i = 0; i < fuzz_iterations; i++) {
17684e7babbaSEric Biggers 			generate_random_testvec_config(&cfg, cfgname,
17694e7babbaSEric Biggers 						       sizeof(cfgname));
17704e7babbaSEric Biggers 			err = test_skcipher_vec_cfg(driver, enc, vec, vec_num,
17714e7babbaSEric Biggers 						    &cfg, req, tsgls);
17724e7babbaSEric Biggers 			if (err)
17734e7babbaSEric Biggers 				return err;
17744e7babbaSEric Biggers 		}
17754e7babbaSEric Biggers 	}
17764e7babbaSEric Biggers #endif
17774e7babbaSEric Biggers 	return 0;
17784e7babbaSEric Biggers }
17794e7babbaSEric Biggers 
17804e7babbaSEric Biggers static int test_skcipher(const char *driver, int enc,
17814e7babbaSEric Biggers 			 const struct cipher_test_suite *suite,
17824e7babbaSEric Biggers 			 struct skcipher_request *req,
17834e7babbaSEric Biggers 			 struct cipher_test_sglists *tsgls)
17844e7babbaSEric Biggers {
17854e7babbaSEric Biggers 	unsigned int i;
17864e7babbaSEric Biggers 	int err;
17874e7babbaSEric Biggers 
17884e7babbaSEric Biggers 	for (i = 0; i < suite->count; i++) {
17894e7babbaSEric Biggers 		err = test_skcipher_vec(driver, enc, &suite->vecs[i], i, req,
17904e7babbaSEric Biggers 					tsgls);
17914e7babbaSEric Biggers 		if (err)
17924e7babbaSEric Biggers 			return err;
17934e7babbaSEric Biggers 	}
17944e7babbaSEric Biggers 	return 0;
17954e7babbaSEric Biggers }
17964e7babbaSEric Biggers 
17974e7babbaSEric Biggers static int alg_test_skcipher(const struct alg_test_desc *desc,
17984e7babbaSEric Biggers 			     const char *driver, u32 type, u32 mask)
17994e7babbaSEric Biggers {
18004e7babbaSEric Biggers 	const struct cipher_test_suite *suite = &desc->suite.cipher;
18014e7babbaSEric Biggers 	struct crypto_skcipher *tfm;
18024e7babbaSEric Biggers 	struct skcipher_request *req = NULL;
18034e7babbaSEric Biggers 	struct cipher_test_sglists *tsgls = NULL;
18044e7babbaSEric Biggers 	int err;
18054e7babbaSEric Biggers 
18064e7babbaSEric Biggers 	if (suite->count <= 0) {
18074e7babbaSEric Biggers 		pr_err("alg: skcipher: empty test suite for %s\n", driver);
18084e7babbaSEric Biggers 		return -EINVAL;
18094e7babbaSEric Biggers 	}
18104e7babbaSEric Biggers 
18114e7babbaSEric Biggers 	tfm = crypto_alloc_skcipher(driver, type, mask);
18124e7babbaSEric Biggers 	if (IS_ERR(tfm)) {
18134e7babbaSEric Biggers 		pr_err("alg: skcipher: failed to allocate transform for %s: %ld\n",
18144e7babbaSEric Biggers 		       driver, PTR_ERR(tfm));
18154e7babbaSEric Biggers 		return PTR_ERR(tfm);
18164e7babbaSEric Biggers 	}
18174e7babbaSEric Biggers 
18184e7babbaSEric Biggers 	req = skcipher_request_alloc(tfm, GFP_KERNEL);
18194e7babbaSEric Biggers 	if (!req) {
18204e7babbaSEric Biggers 		pr_err("alg: skcipher: failed to allocate request for %s\n",
18214e7babbaSEric Biggers 		       driver);
18224e7babbaSEric Biggers 		err = -ENOMEM;
18234e7babbaSEric Biggers 		goto out;
18244e7babbaSEric Biggers 	}
18254e7babbaSEric Biggers 
18264e7babbaSEric Biggers 	tsgls = alloc_cipher_test_sglists();
18274e7babbaSEric Biggers 	if (!tsgls) {
18284e7babbaSEric Biggers 		pr_err("alg: skcipher: failed to allocate test buffers for %s\n",
18294e7babbaSEric Biggers 		       driver);
18304e7babbaSEric Biggers 		err = -ENOMEM;
18314e7babbaSEric Biggers 		goto out;
18324e7babbaSEric Biggers 	}
18334e7babbaSEric Biggers 
18344e7babbaSEric Biggers 	err = test_skcipher(driver, ENCRYPT, suite, req, tsgls);
18354e7babbaSEric Biggers 	if (err)
18364e7babbaSEric Biggers 		goto out;
18374e7babbaSEric Biggers 
18384e7babbaSEric Biggers 	err = test_skcipher(driver, DECRYPT, suite, req, tsgls);
18394e7babbaSEric Biggers out:
18404e7babbaSEric Biggers 	free_cipher_test_sglists(tsgls);
18414e7babbaSEric Biggers 	skcipher_request_free(req);
18424e7babbaSEric Biggers 	crypto_free_skcipher(tfm);
18434e7babbaSEric Biggers 	return err;
18444e7babbaSEric Biggers }
18454e7babbaSEric Biggers 
1846b13b1e0cSEric Biggers static int test_comp(struct crypto_comp *tfm,
1847b13b1e0cSEric Biggers 		     const struct comp_testvec *ctemplate,
1848b13b1e0cSEric Biggers 		     const struct comp_testvec *dtemplate,
1849b13b1e0cSEric Biggers 		     int ctcount, int dtcount)
1850da7f033dSHerbert Xu {
1851da7f033dSHerbert Xu 	const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
185233607384SMahipal Challa 	char *output, *decomp_output;
1853da7f033dSHerbert Xu 	unsigned int i;
1854da7f033dSHerbert Xu 	int ret;
1855da7f033dSHerbert Xu 
185633607384SMahipal Challa 	output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
185733607384SMahipal Challa 	if (!output)
185833607384SMahipal Challa 		return -ENOMEM;
185933607384SMahipal Challa 
186033607384SMahipal Challa 	decomp_output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
186133607384SMahipal Challa 	if (!decomp_output) {
186233607384SMahipal Challa 		kfree(output);
186333607384SMahipal Challa 		return -ENOMEM;
186433607384SMahipal Challa 	}
186533607384SMahipal Challa 
1866da7f033dSHerbert Xu 	for (i = 0; i < ctcount; i++) {
1867c79cf910SGeert Uytterhoeven 		int ilen;
1868c79cf910SGeert Uytterhoeven 		unsigned int dlen = COMP_BUF_SIZE;
1869da7f033dSHerbert Xu 
187022a8118dSMichael Schupikov 		memset(output, 0, COMP_BUF_SIZE);
187122a8118dSMichael Schupikov 		memset(decomp_output, 0, COMP_BUF_SIZE);
1872da7f033dSHerbert Xu 
1873da7f033dSHerbert Xu 		ilen = ctemplate[i].inlen;
1874da7f033dSHerbert Xu 		ret = crypto_comp_compress(tfm, ctemplate[i].input,
187533607384SMahipal Challa 					   ilen, output, &dlen);
1876da7f033dSHerbert Xu 		if (ret) {
1877da7f033dSHerbert Xu 			printk(KERN_ERR "alg: comp: compression failed "
1878da7f033dSHerbert Xu 			       "on test %d for %s: ret=%d\n", i + 1, algo,
1879da7f033dSHerbert Xu 			       -ret);
1880da7f033dSHerbert Xu 			goto out;
1881da7f033dSHerbert Xu 		}
1882da7f033dSHerbert Xu 
188333607384SMahipal Challa 		ilen = dlen;
188433607384SMahipal Challa 		dlen = COMP_BUF_SIZE;
188533607384SMahipal Challa 		ret = crypto_comp_decompress(tfm, output,
188633607384SMahipal Challa 					     ilen, decomp_output, &dlen);
188733607384SMahipal Challa 		if (ret) {
188833607384SMahipal Challa 			pr_err("alg: comp: compression failed: decompress: on test %d for %s failed: ret=%d\n",
188933607384SMahipal Challa 			       i + 1, algo, -ret);
189033607384SMahipal Challa 			goto out;
189133607384SMahipal Challa 		}
189233607384SMahipal Challa 
189333607384SMahipal Challa 		if (dlen != ctemplate[i].inlen) {
1894b812eb00SGeert Uytterhoeven 			printk(KERN_ERR "alg: comp: Compression test %d "
1895b812eb00SGeert Uytterhoeven 			       "failed for %s: output len = %d\n", i + 1, algo,
1896b812eb00SGeert Uytterhoeven 			       dlen);
1897b812eb00SGeert Uytterhoeven 			ret = -EINVAL;
1898b812eb00SGeert Uytterhoeven 			goto out;
1899b812eb00SGeert Uytterhoeven 		}
1900b812eb00SGeert Uytterhoeven 
190133607384SMahipal Challa 		if (memcmp(decomp_output, ctemplate[i].input,
190233607384SMahipal Challa 			   ctemplate[i].inlen)) {
190333607384SMahipal Challa 			pr_err("alg: comp: compression failed: output differs: on test %d for %s\n",
190433607384SMahipal Challa 			       i + 1, algo);
190533607384SMahipal Challa 			hexdump(decomp_output, dlen);
1906da7f033dSHerbert Xu 			ret = -EINVAL;
1907da7f033dSHerbert Xu 			goto out;
1908da7f033dSHerbert Xu 		}
1909da7f033dSHerbert Xu 	}
1910da7f033dSHerbert Xu 
1911da7f033dSHerbert Xu 	for (i = 0; i < dtcount; i++) {
1912c79cf910SGeert Uytterhoeven 		int ilen;
1913c79cf910SGeert Uytterhoeven 		unsigned int dlen = COMP_BUF_SIZE;
1914da7f033dSHerbert Xu 
191522a8118dSMichael Schupikov 		memset(decomp_output, 0, COMP_BUF_SIZE);
1916da7f033dSHerbert Xu 
1917da7f033dSHerbert Xu 		ilen = dtemplate[i].inlen;
1918da7f033dSHerbert Xu 		ret = crypto_comp_decompress(tfm, dtemplate[i].input,
191933607384SMahipal Challa 					     ilen, decomp_output, &dlen);
1920da7f033dSHerbert Xu 		if (ret) {
1921da7f033dSHerbert Xu 			printk(KERN_ERR "alg: comp: decompression failed "
1922da7f033dSHerbert Xu 			       "on test %d for %s: ret=%d\n", i + 1, algo,
1923da7f033dSHerbert Xu 			       -ret);
1924da7f033dSHerbert Xu 			goto out;
1925da7f033dSHerbert Xu 		}
1926da7f033dSHerbert Xu 
1927b812eb00SGeert Uytterhoeven 		if (dlen != dtemplate[i].outlen) {
1928b812eb00SGeert Uytterhoeven 			printk(KERN_ERR "alg: comp: Decompression test %d "
1929b812eb00SGeert Uytterhoeven 			       "failed for %s: output len = %d\n", i + 1, algo,
1930b812eb00SGeert Uytterhoeven 			       dlen);
1931b812eb00SGeert Uytterhoeven 			ret = -EINVAL;
1932b812eb00SGeert Uytterhoeven 			goto out;
1933b812eb00SGeert Uytterhoeven 		}
1934b812eb00SGeert Uytterhoeven 
193533607384SMahipal Challa 		if (memcmp(decomp_output, dtemplate[i].output, dlen)) {
1936da7f033dSHerbert Xu 			printk(KERN_ERR "alg: comp: Decompression test %d "
1937da7f033dSHerbert Xu 			       "failed for %s\n", i + 1, algo);
193833607384SMahipal Challa 			hexdump(decomp_output, dlen);
1939da7f033dSHerbert Xu 			ret = -EINVAL;
1940da7f033dSHerbert Xu 			goto out;
1941da7f033dSHerbert Xu 		}
1942da7f033dSHerbert Xu 	}
1943da7f033dSHerbert Xu 
1944da7f033dSHerbert Xu 	ret = 0;
1945da7f033dSHerbert Xu 
1946da7f033dSHerbert Xu out:
194733607384SMahipal Challa 	kfree(decomp_output);
194833607384SMahipal Challa 	kfree(output);
1949da7f033dSHerbert Xu 	return ret;
1950da7f033dSHerbert Xu }
1951da7f033dSHerbert Xu 
1952b13b1e0cSEric Biggers static int test_acomp(struct crypto_acomp *tfm,
1953b13b1e0cSEric Biggers 			      const struct comp_testvec *ctemplate,
1954b13b1e0cSEric Biggers 		      const struct comp_testvec *dtemplate,
1955b13b1e0cSEric Biggers 		      int ctcount, int dtcount)
1956d7db7a88SGiovanni Cabiddu {
1957d7db7a88SGiovanni Cabiddu 	const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
1958d7db7a88SGiovanni Cabiddu 	unsigned int i;
1959a9943a0aSGiovanni Cabiddu 	char *output, *decomp_out;
1960d7db7a88SGiovanni Cabiddu 	int ret;
1961d7db7a88SGiovanni Cabiddu 	struct scatterlist src, dst;
1962d7db7a88SGiovanni Cabiddu 	struct acomp_req *req;
19637f397136SGilad Ben-Yossef 	struct crypto_wait wait;
1964d7db7a88SGiovanni Cabiddu 
1965eb095593SEric Biggers 	output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1966eb095593SEric Biggers 	if (!output)
1967eb095593SEric Biggers 		return -ENOMEM;
1968eb095593SEric Biggers 
1969a9943a0aSGiovanni Cabiddu 	decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1970a9943a0aSGiovanni Cabiddu 	if (!decomp_out) {
1971a9943a0aSGiovanni Cabiddu 		kfree(output);
1972a9943a0aSGiovanni Cabiddu 		return -ENOMEM;
1973a9943a0aSGiovanni Cabiddu 	}
1974a9943a0aSGiovanni Cabiddu 
1975d7db7a88SGiovanni Cabiddu 	for (i = 0; i < ctcount; i++) {
1976d7db7a88SGiovanni Cabiddu 		unsigned int dlen = COMP_BUF_SIZE;
1977d7db7a88SGiovanni Cabiddu 		int ilen = ctemplate[i].inlen;
197802608e02SLaura Abbott 		void *input_vec;
1979d7db7a88SGiovanni Cabiddu 
1980d2110224SEric Biggers 		input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL);
198102608e02SLaura Abbott 		if (!input_vec) {
198202608e02SLaura Abbott 			ret = -ENOMEM;
198302608e02SLaura Abbott 			goto out;
198402608e02SLaura Abbott 		}
198502608e02SLaura Abbott 
1986eb095593SEric Biggers 		memset(output, 0, dlen);
19877f397136SGilad Ben-Yossef 		crypto_init_wait(&wait);
198802608e02SLaura Abbott 		sg_init_one(&src, input_vec, ilen);
1989d7db7a88SGiovanni Cabiddu 		sg_init_one(&dst, output, dlen);
1990d7db7a88SGiovanni Cabiddu 
1991d7db7a88SGiovanni Cabiddu 		req = acomp_request_alloc(tfm);
1992d7db7a88SGiovanni Cabiddu 		if (!req) {
1993d7db7a88SGiovanni Cabiddu 			pr_err("alg: acomp: request alloc failed for %s\n",
1994d7db7a88SGiovanni Cabiddu 			       algo);
199502608e02SLaura Abbott 			kfree(input_vec);
1996d7db7a88SGiovanni Cabiddu 			ret = -ENOMEM;
1997d7db7a88SGiovanni Cabiddu 			goto out;
1998d7db7a88SGiovanni Cabiddu 		}
1999d7db7a88SGiovanni Cabiddu 
2000d7db7a88SGiovanni Cabiddu 		acomp_request_set_params(req, &src, &dst, ilen, dlen);
2001d7db7a88SGiovanni Cabiddu 		acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
20027f397136SGilad Ben-Yossef 					   crypto_req_done, &wait);
2003d7db7a88SGiovanni Cabiddu 
20047f397136SGilad Ben-Yossef 		ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
2005d7db7a88SGiovanni Cabiddu 		if (ret) {
2006d7db7a88SGiovanni Cabiddu 			pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
2007d7db7a88SGiovanni Cabiddu 			       i + 1, algo, -ret);
200802608e02SLaura Abbott 			kfree(input_vec);
2009d7db7a88SGiovanni Cabiddu 			acomp_request_free(req);
2010d7db7a88SGiovanni Cabiddu 			goto out;
2011d7db7a88SGiovanni Cabiddu 		}
2012d7db7a88SGiovanni Cabiddu 
2013a9943a0aSGiovanni Cabiddu 		ilen = req->dlen;
2014a9943a0aSGiovanni Cabiddu 		dlen = COMP_BUF_SIZE;
2015a9943a0aSGiovanni Cabiddu 		sg_init_one(&src, output, ilen);
2016a9943a0aSGiovanni Cabiddu 		sg_init_one(&dst, decomp_out, dlen);
20177f397136SGilad Ben-Yossef 		crypto_init_wait(&wait);
2018a9943a0aSGiovanni Cabiddu 		acomp_request_set_params(req, &src, &dst, ilen, dlen);
2019a9943a0aSGiovanni Cabiddu 
20207f397136SGilad Ben-Yossef 		ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
2021a9943a0aSGiovanni Cabiddu 		if (ret) {
2022a9943a0aSGiovanni Cabiddu 			pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
2023a9943a0aSGiovanni Cabiddu 			       i + 1, algo, -ret);
2024a9943a0aSGiovanni Cabiddu 			kfree(input_vec);
2025a9943a0aSGiovanni Cabiddu 			acomp_request_free(req);
2026a9943a0aSGiovanni Cabiddu 			goto out;
2027a9943a0aSGiovanni Cabiddu 		}
2028a9943a0aSGiovanni Cabiddu 
2029a9943a0aSGiovanni Cabiddu 		if (req->dlen != ctemplate[i].inlen) {
2030d7db7a88SGiovanni Cabiddu 			pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
2031d7db7a88SGiovanni Cabiddu 			       i + 1, algo, req->dlen);
2032d7db7a88SGiovanni Cabiddu 			ret = -EINVAL;
203302608e02SLaura Abbott 			kfree(input_vec);
2034d7db7a88SGiovanni Cabiddu 			acomp_request_free(req);
2035d7db7a88SGiovanni Cabiddu 			goto out;
2036d7db7a88SGiovanni Cabiddu 		}
2037d7db7a88SGiovanni Cabiddu 
2038a9943a0aSGiovanni Cabiddu 		if (memcmp(input_vec, decomp_out, req->dlen)) {
2039d7db7a88SGiovanni Cabiddu 			pr_err("alg: acomp: Compression test %d failed for %s\n",
2040d7db7a88SGiovanni Cabiddu 			       i + 1, algo);
2041d7db7a88SGiovanni Cabiddu 			hexdump(output, req->dlen);
2042d7db7a88SGiovanni Cabiddu 			ret = -EINVAL;
204302608e02SLaura Abbott 			kfree(input_vec);
2044d7db7a88SGiovanni Cabiddu 			acomp_request_free(req);
2045d7db7a88SGiovanni Cabiddu 			goto out;
2046d7db7a88SGiovanni Cabiddu 		}
2047d7db7a88SGiovanni Cabiddu 
204802608e02SLaura Abbott 		kfree(input_vec);
2049d7db7a88SGiovanni Cabiddu 		acomp_request_free(req);
2050d7db7a88SGiovanni Cabiddu 	}
2051d7db7a88SGiovanni Cabiddu 
2052d7db7a88SGiovanni Cabiddu 	for (i = 0; i < dtcount; i++) {
2053d7db7a88SGiovanni Cabiddu 		unsigned int dlen = COMP_BUF_SIZE;
2054d7db7a88SGiovanni Cabiddu 		int ilen = dtemplate[i].inlen;
205502608e02SLaura Abbott 		void *input_vec;
2056d7db7a88SGiovanni Cabiddu 
2057d2110224SEric Biggers 		input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL);
205802608e02SLaura Abbott 		if (!input_vec) {
205902608e02SLaura Abbott 			ret = -ENOMEM;
206002608e02SLaura Abbott 			goto out;
206102608e02SLaura Abbott 		}
206202608e02SLaura Abbott 
2063eb095593SEric Biggers 		memset(output, 0, dlen);
20647f397136SGilad Ben-Yossef 		crypto_init_wait(&wait);
206502608e02SLaura Abbott 		sg_init_one(&src, input_vec, ilen);
2066d7db7a88SGiovanni Cabiddu 		sg_init_one(&dst, output, dlen);
2067d7db7a88SGiovanni Cabiddu 
2068d7db7a88SGiovanni Cabiddu 		req = acomp_request_alloc(tfm);
2069d7db7a88SGiovanni Cabiddu 		if (!req) {
2070d7db7a88SGiovanni Cabiddu 			pr_err("alg: acomp: request alloc failed for %s\n",
2071d7db7a88SGiovanni Cabiddu 			       algo);
207202608e02SLaura Abbott 			kfree(input_vec);
2073d7db7a88SGiovanni Cabiddu 			ret = -ENOMEM;
2074d7db7a88SGiovanni Cabiddu 			goto out;
2075d7db7a88SGiovanni Cabiddu 		}
2076d7db7a88SGiovanni Cabiddu 
2077d7db7a88SGiovanni Cabiddu 		acomp_request_set_params(req, &src, &dst, ilen, dlen);
2078d7db7a88SGiovanni Cabiddu 		acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
20797f397136SGilad Ben-Yossef 					   crypto_req_done, &wait);
2080d7db7a88SGiovanni Cabiddu 
20817f397136SGilad Ben-Yossef 		ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
2082d7db7a88SGiovanni Cabiddu 		if (ret) {
2083d7db7a88SGiovanni Cabiddu 			pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
2084d7db7a88SGiovanni Cabiddu 			       i + 1, algo, -ret);
208502608e02SLaura Abbott 			kfree(input_vec);
2086d7db7a88SGiovanni Cabiddu 			acomp_request_free(req);
2087d7db7a88SGiovanni Cabiddu 			goto out;
2088d7db7a88SGiovanni Cabiddu 		}
2089d7db7a88SGiovanni Cabiddu 
2090d7db7a88SGiovanni Cabiddu 		if (req->dlen != dtemplate[i].outlen) {
2091d7db7a88SGiovanni Cabiddu 			pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
2092d7db7a88SGiovanni Cabiddu 			       i + 1, algo, req->dlen);
2093d7db7a88SGiovanni Cabiddu 			ret = -EINVAL;
209402608e02SLaura Abbott 			kfree(input_vec);
2095d7db7a88SGiovanni Cabiddu 			acomp_request_free(req);
2096d7db7a88SGiovanni Cabiddu 			goto out;
2097d7db7a88SGiovanni Cabiddu 		}
2098d7db7a88SGiovanni Cabiddu 
2099d7db7a88SGiovanni Cabiddu 		if (memcmp(output, dtemplate[i].output, req->dlen)) {
2100d7db7a88SGiovanni Cabiddu 			pr_err("alg: acomp: Decompression test %d failed for %s\n",
2101d7db7a88SGiovanni Cabiddu 			       i + 1, algo);
2102d7db7a88SGiovanni Cabiddu 			hexdump(output, req->dlen);
2103d7db7a88SGiovanni Cabiddu 			ret = -EINVAL;
210402608e02SLaura Abbott 			kfree(input_vec);
2105d7db7a88SGiovanni Cabiddu 			acomp_request_free(req);
2106d7db7a88SGiovanni Cabiddu 			goto out;
2107d7db7a88SGiovanni Cabiddu 		}
2108d7db7a88SGiovanni Cabiddu 
210902608e02SLaura Abbott 		kfree(input_vec);
2110d7db7a88SGiovanni Cabiddu 		acomp_request_free(req);
2111d7db7a88SGiovanni Cabiddu 	}
2112d7db7a88SGiovanni Cabiddu 
2113d7db7a88SGiovanni Cabiddu 	ret = 0;
2114d7db7a88SGiovanni Cabiddu 
2115d7db7a88SGiovanni Cabiddu out:
2116a9943a0aSGiovanni Cabiddu 	kfree(decomp_out);
2117eb095593SEric Biggers 	kfree(output);
2118d7db7a88SGiovanni Cabiddu 	return ret;
2119d7db7a88SGiovanni Cabiddu }
2120d7db7a88SGiovanni Cabiddu 
2121b13b1e0cSEric Biggers static int test_cprng(struct crypto_rng *tfm,
2122b13b1e0cSEric Biggers 		      const struct cprng_testvec *template,
21237647d6ceSJarod Wilson 		      unsigned int tcount)
21247647d6ceSJarod Wilson {
21257647d6ceSJarod Wilson 	const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
2126fa4ef8a6SFelipe Contreras 	int err = 0, i, j, seedsize;
21277647d6ceSJarod Wilson 	u8 *seed;
21287647d6ceSJarod Wilson 	char result[32];
21297647d6ceSJarod Wilson 
21307647d6ceSJarod Wilson 	seedsize = crypto_rng_seedsize(tfm);
21317647d6ceSJarod Wilson 
21327647d6ceSJarod Wilson 	seed = kmalloc(seedsize, GFP_KERNEL);
21337647d6ceSJarod Wilson 	if (!seed) {
21347647d6ceSJarod Wilson 		printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
21357647d6ceSJarod Wilson 		       "for %s\n", algo);
21367647d6ceSJarod Wilson 		return -ENOMEM;
21377647d6ceSJarod Wilson 	}
21387647d6ceSJarod Wilson 
21397647d6ceSJarod Wilson 	for (i = 0; i < tcount; i++) {
21407647d6ceSJarod Wilson 		memset(result, 0, 32);
21417647d6ceSJarod Wilson 
21427647d6ceSJarod Wilson 		memcpy(seed, template[i].v, template[i].vlen);
21437647d6ceSJarod Wilson 		memcpy(seed + template[i].vlen, template[i].key,
21447647d6ceSJarod Wilson 		       template[i].klen);
21457647d6ceSJarod Wilson 		memcpy(seed + template[i].vlen + template[i].klen,
21467647d6ceSJarod Wilson 		       template[i].dt, template[i].dtlen);
21477647d6ceSJarod Wilson 
21487647d6ceSJarod Wilson 		err = crypto_rng_reset(tfm, seed, seedsize);
21497647d6ceSJarod Wilson 		if (err) {
21507647d6ceSJarod Wilson 			printk(KERN_ERR "alg: cprng: Failed to reset rng "
21517647d6ceSJarod Wilson 			       "for %s\n", algo);
21527647d6ceSJarod Wilson 			goto out;
21537647d6ceSJarod Wilson 		}
21547647d6ceSJarod Wilson 
21557647d6ceSJarod Wilson 		for (j = 0; j < template[i].loops; j++) {
21567647d6ceSJarod Wilson 			err = crypto_rng_get_bytes(tfm, result,
21577647d6ceSJarod Wilson 						   template[i].rlen);
215819e60e13SStephan Mueller 			if (err < 0) {
21597647d6ceSJarod Wilson 				printk(KERN_ERR "alg: cprng: Failed to obtain "
21607647d6ceSJarod Wilson 				       "the correct amount of random data for "
216119e60e13SStephan Mueller 				       "%s (requested %d)\n", algo,
216219e60e13SStephan Mueller 				       template[i].rlen);
21637647d6ceSJarod Wilson 				goto out;
21647647d6ceSJarod Wilson 			}
21657647d6ceSJarod Wilson 		}
21667647d6ceSJarod Wilson 
21677647d6ceSJarod Wilson 		err = memcmp(result, template[i].result,
21687647d6ceSJarod Wilson 			     template[i].rlen);
21697647d6ceSJarod Wilson 		if (err) {
21707647d6ceSJarod Wilson 			printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
21717647d6ceSJarod Wilson 			       i, algo);
21727647d6ceSJarod Wilson 			hexdump(result, template[i].rlen);
21737647d6ceSJarod Wilson 			err = -EINVAL;
21747647d6ceSJarod Wilson 			goto out;
21757647d6ceSJarod Wilson 		}
21767647d6ceSJarod Wilson 	}
21777647d6ceSJarod Wilson 
21787647d6ceSJarod Wilson out:
21797647d6ceSJarod Wilson 	kfree(seed);
21807647d6ceSJarod Wilson 	return err;
21817647d6ceSJarod Wilson }
21827647d6ceSJarod Wilson 
2183da7f033dSHerbert Xu static int alg_test_cipher(const struct alg_test_desc *desc,
2184da7f033dSHerbert Xu 			   const char *driver, u32 type, u32 mask)
2185da7f033dSHerbert Xu {
218692a4c9feSEric Biggers 	const struct cipher_test_suite *suite = &desc->suite.cipher;
21871aa4ecd9SHerbert Xu 	struct crypto_cipher *tfm;
218892a4c9feSEric Biggers 	int err;
2189da7f033dSHerbert Xu 
2190eed93e0cSHerbert Xu 	tfm = crypto_alloc_cipher(driver, type, mask);
2191da7f033dSHerbert Xu 	if (IS_ERR(tfm)) {
2192da7f033dSHerbert Xu 		printk(KERN_ERR "alg: cipher: Failed to load transform for "
2193da7f033dSHerbert Xu 		       "%s: %ld\n", driver, PTR_ERR(tfm));
2194da7f033dSHerbert Xu 		return PTR_ERR(tfm);
2195da7f033dSHerbert Xu 	}
2196da7f033dSHerbert Xu 
219792a4c9feSEric Biggers 	err = test_cipher(tfm, ENCRYPT, suite->vecs, suite->count);
219892a4c9feSEric Biggers 	if (!err)
219992a4c9feSEric Biggers 		err = test_cipher(tfm, DECRYPT, suite->vecs, suite->count);
2200da7f033dSHerbert Xu 
22011aa4ecd9SHerbert Xu 	crypto_free_cipher(tfm);
22021aa4ecd9SHerbert Xu 	return err;
22031aa4ecd9SHerbert Xu }
22041aa4ecd9SHerbert Xu 
2205da7f033dSHerbert Xu static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
2206da7f033dSHerbert Xu 			 u32 type, u32 mask)
2207da7f033dSHerbert Xu {
2208d7db7a88SGiovanni Cabiddu 	struct crypto_comp *comp;
2209d7db7a88SGiovanni Cabiddu 	struct crypto_acomp *acomp;
2210da7f033dSHerbert Xu 	int err;
2211d7db7a88SGiovanni Cabiddu 	u32 algo_type = type & CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
2212da7f033dSHerbert Xu 
2213d7db7a88SGiovanni Cabiddu 	if (algo_type == CRYPTO_ALG_TYPE_ACOMPRESS) {
2214d7db7a88SGiovanni Cabiddu 		acomp = crypto_alloc_acomp(driver, type, mask);
2215d7db7a88SGiovanni Cabiddu 		if (IS_ERR(acomp)) {
2216d7db7a88SGiovanni Cabiddu 			pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
2217d7db7a88SGiovanni Cabiddu 			       driver, PTR_ERR(acomp));
2218d7db7a88SGiovanni Cabiddu 			return PTR_ERR(acomp);
2219d7db7a88SGiovanni Cabiddu 		}
2220d7db7a88SGiovanni Cabiddu 		err = test_acomp(acomp, desc->suite.comp.comp.vecs,
2221d7db7a88SGiovanni Cabiddu 				 desc->suite.comp.decomp.vecs,
2222d7db7a88SGiovanni Cabiddu 				 desc->suite.comp.comp.count,
2223d7db7a88SGiovanni Cabiddu 				 desc->suite.comp.decomp.count);
2224d7db7a88SGiovanni Cabiddu 		crypto_free_acomp(acomp);
2225d7db7a88SGiovanni Cabiddu 	} else {
2226d7db7a88SGiovanni Cabiddu 		comp = crypto_alloc_comp(driver, type, mask);
2227d7db7a88SGiovanni Cabiddu 		if (IS_ERR(comp)) {
2228d7db7a88SGiovanni Cabiddu 			pr_err("alg: comp: Failed to load transform for %s: %ld\n",
2229d7db7a88SGiovanni Cabiddu 			       driver, PTR_ERR(comp));
2230d7db7a88SGiovanni Cabiddu 			return PTR_ERR(comp);
2231da7f033dSHerbert Xu 		}
2232da7f033dSHerbert Xu 
2233d7db7a88SGiovanni Cabiddu 		err = test_comp(comp, desc->suite.comp.comp.vecs,
2234da7f033dSHerbert Xu 				desc->suite.comp.decomp.vecs,
2235da7f033dSHerbert Xu 				desc->suite.comp.comp.count,
2236da7f033dSHerbert Xu 				desc->suite.comp.decomp.count);
2237da7f033dSHerbert Xu 
2238d7db7a88SGiovanni Cabiddu 		crypto_free_comp(comp);
2239d7db7a88SGiovanni Cabiddu 	}
2240da7f033dSHerbert Xu 	return err;
2241da7f033dSHerbert Xu }
2242da7f033dSHerbert Xu 
22438e3ee85eSHerbert Xu static int alg_test_crc32c(const struct alg_test_desc *desc,
22448e3ee85eSHerbert Xu 			   const char *driver, u32 type, u32 mask)
22458e3ee85eSHerbert Xu {
22468e3ee85eSHerbert Xu 	struct crypto_shash *tfm;
2247cb9dde88SEric Biggers 	__le32 val;
22488e3ee85eSHerbert Xu 	int err;
22498e3ee85eSHerbert Xu 
22508e3ee85eSHerbert Xu 	err = alg_test_hash(desc, driver, type, mask);
22518e3ee85eSHerbert Xu 	if (err)
2252eb5e6730SEric Biggers 		return err;
22538e3ee85eSHerbert Xu 
2254eed93e0cSHerbert Xu 	tfm = crypto_alloc_shash(driver, type, mask);
22558e3ee85eSHerbert Xu 	if (IS_ERR(tfm)) {
2256eb5e6730SEric Biggers 		if (PTR_ERR(tfm) == -ENOENT) {
2257eb5e6730SEric Biggers 			/*
2258eb5e6730SEric Biggers 			 * This crc32c implementation is only available through
2259eb5e6730SEric Biggers 			 * ahash API, not the shash API, so the remaining part
2260eb5e6730SEric Biggers 			 * of the test is not applicable to it.
2261eb5e6730SEric Biggers 			 */
2262eb5e6730SEric Biggers 			return 0;
2263eb5e6730SEric Biggers 		}
22648e3ee85eSHerbert Xu 		printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
22658e3ee85eSHerbert Xu 		       "%ld\n", driver, PTR_ERR(tfm));
2266eb5e6730SEric Biggers 		return PTR_ERR(tfm);
22678e3ee85eSHerbert Xu 	}
22688e3ee85eSHerbert Xu 
22698e3ee85eSHerbert Xu 	do {
22704c5c3024SJan-Simon Möller 		SHASH_DESC_ON_STACK(shash, tfm);
22714c5c3024SJan-Simon Möller 		u32 *ctx = (u32 *)shash_desc_ctx(shash);
22728e3ee85eSHerbert Xu 
22734c5c3024SJan-Simon Möller 		shash->tfm = tfm;
22744c5c3024SJan-Simon Möller 		shash->flags = 0;
22758e3ee85eSHerbert Xu 
2276cb9dde88SEric Biggers 		*ctx = 420553207;
22774c5c3024SJan-Simon Möller 		err = crypto_shash_final(shash, (u8 *)&val);
22788e3ee85eSHerbert Xu 		if (err) {
22798e3ee85eSHerbert Xu 			printk(KERN_ERR "alg: crc32c: Operation failed for "
22808e3ee85eSHerbert Xu 			       "%s: %d\n", driver, err);
22818e3ee85eSHerbert Xu 			break;
22828e3ee85eSHerbert Xu 		}
22838e3ee85eSHerbert Xu 
2284cb9dde88SEric Biggers 		if (val != cpu_to_le32(~420553207)) {
2285cb9dde88SEric Biggers 			pr_err("alg: crc32c: Test failed for %s: %u\n",
2286cb9dde88SEric Biggers 			       driver, le32_to_cpu(val));
22878e3ee85eSHerbert Xu 			err = -EINVAL;
22888e3ee85eSHerbert Xu 		}
22898e3ee85eSHerbert Xu 	} while (0);
22908e3ee85eSHerbert Xu 
22918e3ee85eSHerbert Xu 	crypto_free_shash(tfm);
22928e3ee85eSHerbert Xu 
22938e3ee85eSHerbert Xu 	return err;
22948e3ee85eSHerbert Xu }
22958e3ee85eSHerbert Xu 
22967647d6ceSJarod Wilson static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
22977647d6ceSJarod Wilson 			  u32 type, u32 mask)
22987647d6ceSJarod Wilson {
22997647d6ceSJarod Wilson 	struct crypto_rng *rng;
23007647d6ceSJarod Wilson 	int err;
23017647d6ceSJarod Wilson 
2302eed93e0cSHerbert Xu 	rng = crypto_alloc_rng(driver, type, mask);
23037647d6ceSJarod Wilson 	if (IS_ERR(rng)) {
23047647d6ceSJarod Wilson 		printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
23057647d6ceSJarod Wilson 		       "%ld\n", driver, PTR_ERR(rng));
23067647d6ceSJarod Wilson 		return PTR_ERR(rng);
23077647d6ceSJarod Wilson 	}
23087647d6ceSJarod Wilson 
23097647d6ceSJarod Wilson 	err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
23107647d6ceSJarod Wilson 
23117647d6ceSJarod Wilson 	crypto_free_rng(rng);
23127647d6ceSJarod Wilson 
23137647d6ceSJarod Wilson 	return err;
23147647d6ceSJarod Wilson }
23157647d6ceSJarod Wilson 
231664d1cdfbSStephan Mueller 
2317b13b1e0cSEric Biggers static int drbg_cavs_test(const struct drbg_testvec *test, int pr,
231864d1cdfbSStephan Mueller 			  const char *driver, u32 type, u32 mask)
231964d1cdfbSStephan Mueller {
232064d1cdfbSStephan Mueller 	int ret = -EAGAIN;
232164d1cdfbSStephan Mueller 	struct crypto_rng *drng;
232264d1cdfbSStephan Mueller 	struct drbg_test_data test_data;
232364d1cdfbSStephan Mueller 	struct drbg_string addtl, pers, testentropy;
232464d1cdfbSStephan Mueller 	unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
232564d1cdfbSStephan Mueller 
232664d1cdfbSStephan Mueller 	if (!buf)
232764d1cdfbSStephan Mueller 		return -ENOMEM;
232864d1cdfbSStephan Mueller 
2329eed93e0cSHerbert Xu 	drng = crypto_alloc_rng(driver, type, mask);
233064d1cdfbSStephan Mueller 	if (IS_ERR(drng)) {
233164d1cdfbSStephan Mueller 		printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
233264d1cdfbSStephan Mueller 		       "%s\n", driver);
233364d1cdfbSStephan Mueller 		kzfree(buf);
233464d1cdfbSStephan Mueller 		return -ENOMEM;
233564d1cdfbSStephan Mueller 	}
233664d1cdfbSStephan Mueller 
233764d1cdfbSStephan Mueller 	test_data.testentropy = &testentropy;
233864d1cdfbSStephan Mueller 	drbg_string_fill(&testentropy, test->entropy, test->entropylen);
233964d1cdfbSStephan Mueller 	drbg_string_fill(&pers, test->pers, test->perslen);
234064d1cdfbSStephan Mueller 	ret = crypto_drbg_reset_test(drng, &pers, &test_data);
234164d1cdfbSStephan Mueller 	if (ret) {
234264d1cdfbSStephan Mueller 		printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
234364d1cdfbSStephan Mueller 		goto outbuf;
234464d1cdfbSStephan Mueller 	}
234564d1cdfbSStephan Mueller 
234664d1cdfbSStephan Mueller 	drbg_string_fill(&addtl, test->addtla, test->addtllen);
234764d1cdfbSStephan Mueller 	if (pr) {
234864d1cdfbSStephan Mueller 		drbg_string_fill(&testentropy, test->entpra, test->entprlen);
234964d1cdfbSStephan Mueller 		ret = crypto_drbg_get_bytes_addtl_test(drng,
235064d1cdfbSStephan Mueller 			buf, test->expectedlen, &addtl,	&test_data);
235164d1cdfbSStephan Mueller 	} else {
235264d1cdfbSStephan Mueller 		ret = crypto_drbg_get_bytes_addtl(drng,
235364d1cdfbSStephan Mueller 			buf, test->expectedlen, &addtl);
235464d1cdfbSStephan Mueller 	}
235519e60e13SStephan Mueller 	if (ret < 0) {
235664d1cdfbSStephan Mueller 		printk(KERN_ERR "alg: drbg: could not obtain random data for "
235764d1cdfbSStephan Mueller 		       "driver %s\n", driver);
235864d1cdfbSStephan Mueller 		goto outbuf;
235964d1cdfbSStephan Mueller 	}
236064d1cdfbSStephan Mueller 
236164d1cdfbSStephan Mueller 	drbg_string_fill(&addtl, test->addtlb, test->addtllen);
236264d1cdfbSStephan Mueller 	if (pr) {
236364d1cdfbSStephan Mueller 		drbg_string_fill(&testentropy, test->entprb, test->entprlen);
236464d1cdfbSStephan Mueller 		ret = crypto_drbg_get_bytes_addtl_test(drng,
236564d1cdfbSStephan Mueller 			buf, test->expectedlen, &addtl, &test_data);
236664d1cdfbSStephan Mueller 	} else {
236764d1cdfbSStephan Mueller 		ret = crypto_drbg_get_bytes_addtl(drng,
236864d1cdfbSStephan Mueller 			buf, test->expectedlen, &addtl);
236964d1cdfbSStephan Mueller 	}
237019e60e13SStephan Mueller 	if (ret < 0) {
237164d1cdfbSStephan Mueller 		printk(KERN_ERR "alg: drbg: could not obtain random data for "
237264d1cdfbSStephan Mueller 		       "driver %s\n", driver);
237364d1cdfbSStephan Mueller 		goto outbuf;
237464d1cdfbSStephan Mueller 	}
237564d1cdfbSStephan Mueller 
237664d1cdfbSStephan Mueller 	ret = memcmp(test->expected, buf, test->expectedlen);
237764d1cdfbSStephan Mueller 
237864d1cdfbSStephan Mueller outbuf:
237964d1cdfbSStephan Mueller 	crypto_free_rng(drng);
238064d1cdfbSStephan Mueller 	kzfree(buf);
238164d1cdfbSStephan Mueller 	return ret;
238264d1cdfbSStephan Mueller }
238364d1cdfbSStephan Mueller 
238464d1cdfbSStephan Mueller 
238564d1cdfbSStephan Mueller static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
238664d1cdfbSStephan Mueller 			 u32 type, u32 mask)
238764d1cdfbSStephan Mueller {
238864d1cdfbSStephan Mueller 	int err = 0;
238964d1cdfbSStephan Mueller 	int pr = 0;
239064d1cdfbSStephan Mueller 	int i = 0;
2391b13b1e0cSEric Biggers 	const struct drbg_testvec *template = desc->suite.drbg.vecs;
239264d1cdfbSStephan Mueller 	unsigned int tcount = desc->suite.drbg.count;
239364d1cdfbSStephan Mueller 
239464d1cdfbSStephan Mueller 	if (0 == memcmp(driver, "drbg_pr_", 8))
239564d1cdfbSStephan Mueller 		pr = 1;
239664d1cdfbSStephan Mueller 
239764d1cdfbSStephan Mueller 	for (i = 0; i < tcount; i++) {
239864d1cdfbSStephan Mueller 		err = drbg_cavs_test(&template[i], pr, driver, type, mask);
239964d1cdfbSStephan Mueller 		if (err) {
240064d1cdfbSStephan Mueller 			printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
240164d1cdfbSStephan Mueller 			       i, driver);
240264d1cdfbSStephan Mueller 			err = -EINVAL;
240364d1cdfbSStephan Mueller 			break;
240464d1cdfbSStephan Mueller 		}
240564d1cdfbSStephan Mueller 	}
240664d1cdfbSStephan Mueller 	return err;
240764d1cdfbSStephan Mueller 
240864d1cdfbSStephan Mueller }
240964d1cdfbSStephan Mueller 
2410b13b1e0cSEric Biggers static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
2411802c7f1cSSalvatore Benedetto 		       const char *alg)
2412802c7f1cSSalvatore Benedetto {
2413802c7f1cSSalvatore Benedetto 	struct kpp_request *req;
2414802c7f1cSSalvatore Benedetto 	void *input_buf = NULL;
2415802c7f1cSSalvatore Benedetto 	void *output_buf = NULL;
241647d3fd39STudor-Dan Ambarus 	void *a_public = NULL;
241747d3fd39STudor-Dan Ambarus 	void *a_ss = NULL;
241847d3fd39STudor-Dan Ambarus 	void *shared_secret = NULL;
24197f397136SGilad Ben-Yossef 	struct crypto_wait wait;
2420802c7f1cSSalvatore Benedetto 	unsigned int out_len_max;
2421802c7f1cSSalvatore Benedetto 	int err = -ENOMEM;
2422802c7f1cSSalvatore Benedetto 	struct scatterlist src, dst;
2423802c7f1cSSalvatore Benedetto 
2424802c7f1cSSalvatore Benedetto 	req = kpp_request_alloc(tfm, GFP_KERNEL);
2425802c7f1cSSalvatore Benedetto 	if (!req)
2426802c7f1cSSalvatore Benedetto 		return err;
2427802c7f1cSSalvatore Benedetto 
24287f397136SGilad Ben-Yossef 	crypto_init_wait(&wait);
2429802c7f1cSSalvatore Benedetto 
2430802c7f1cSSalvatore Benedetto 	err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
2431802c7f1cSSalvatore Benedetto 	if (err < 0)
2432802c7f1cSSalvatore Benedetto 		goto free_req;
2433802c7f1cSSalvatore Benedetto 
2434802c7f1cSSalvatore Benedetto 	out_len_max = crypto_kpp_maxsize(tfm);
2435802c7f1cSSalvatore Benedetto 	output_buf = kzalloc(out_len_max, GFP_KERNEL);
2436802c7f1cSSalvatore Benedetto 	if (!output_buf) {
2437802c7f1cSSalvatore Benedetto 		err = -ENOMEM;
2438802c7f1cSSalvatore Benedetto 		goto free_req;
2439802c7f1cSSalvatore Benedetto 	}
2440802c7f1cSSalvatore Benedetto 
2441802c7f1cSSalvatore Benedetto 	/* Use appropriate parameter as base */
2442802c7f1cSSalvatore Benedetto 	kpp_request_set_input(req, NULL, 0);
2443802c7f1cSSalvatore Benedetto 	sg_init_one(&dst, output_buf, out_len_max);
2444802c7f1cSSalvatore Benedetto 	kpp_request_set_output(req, &dst, out_len_max);
2445802c7f1cSSalvatore Benedetto 	kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
24467f397136SGilad Ben-Yossef 				 crypto_req_done, &wait);
2447802c7f1cSSalvatore Benedetto 
244847d3fd39STudor-Dan Ambarus 	/* Compute party A's public key */
24497f397136SGilad Ben-Yossef 	err = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
2450802c7f1cSSalvatore Benedetto 	if (err) {
245147d3fd39STudor-Dan Ambarus 		pr_err("alg: %s: Party A: generate public key test failed. err %d\n",
2452802c7f1cSSalvatore Benedetto 		       alg, err);
2453802c7f1cSSalvatore Benedetto 		goto free_output;
2454802c7f1cSSalvatore Benedetto 	}
245547d3fd39STudor-Dan Ambarus 
245647d3fd39STudor-Dan Ambarus 	if (vec->genkey) {
245747d3fd39STudor-Dan Ambarus 		/* Save party A's public key */
2458e3d90e52SChristopher Diaz Riveros 		a_public = kmemdup(sg_virt(req->dst), out_len_max, GFP_KERNEL);
245947d3fd39STudor-Dan Ambarus 		if (!a_public) {
246047d3fd39STudor-Dan Ambarus 			err = -ENOMEM;
246147d3fd39STudor-Dan Ambarus 			goto free_output;
246247d3fd39STudor-Dan Ambarus 		}
246347d3fd39STudor-Dan Ambarus 	} else {
2464802c7f1cSSalvatore Benedetto 		/* Verify calculated public key */
2465802c7f1cSSalvatore Benedetto 		if (memcmp(vec->expected_a_public, sg_virt(req->dst),
2466802c7f1cSSalvatore Benedetto 			   vec->expected_a_public_size)) {
246747d3fd39STudor-Dan Ambarus 			pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n",
2468802c7f1cSSalvatore Benedetto 			       alg);
2469802c7f1cSSalvatore Benedetto 			err = -EINVAL;
2470802c7f1cSSalvatore Benedetto 			goto free_output;
2471802c7f1cSSalvatore Benedetto 		}
247247d3fd39STudor-Dan Ambarus 	}
2473802c7f1cSSalvatore Benedetto 
2474802c7f1cSSalvatore Benedetto 	/* Calculate shared secret key by using counter part (b) public key. */
2475e3d90e52SChristopher Diaz Riveros 	input_buf = kmemdup(vec->b_public, vec->b_public_size, GFP_KERNEL);
2476802c7f1cSSalvatore Benedetto 	if (!input_buf) {
2477802c7f1cSSalvatore Benedetto 		err = -ENOMEM;
2478802c7f1cSSalvatore Benedetto 		goto free_output;
2479802c7f1cSSalvatore Benedetto 	}
2480802c7f1cSSalvatore Benedetto 
2481802c7f1cSSalvatore Benedetto 	sg_init_one(&src, input_buf, vec->b_public_size);
2482802c7f1cSSalvatore Benedetto 	sg_init_one(&dst, output_buf, out_len_max);
2483802c7f1cSSalvatore Benedetto 	kpp_request_set_input(req, &src, vec->b_public_size);
2484802c7f1cSSalvatore Benedetto 	kpp_request_set_output(req, &dst, out_len_max);
2485802c7f1cSSalvatore Benedetto 	kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
24867f397136SGilad Ben-Yossef 				 crypto_req_done, &wait);
24877f397136SGilad Ben-Yossef 	err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait);
2488802c7f1cSSalvatore Benedetto 	if (err) {
248947d3fd39STudor-Dan Ambarus 		pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n",
2490802c7f1cSSalvatore Benedetto 		       alg, err);
2491802c7f1cSSalvatore Benedetto 		goto free_all;
2492802c7f1cSSalvatore Benedetto 	}
249347d3fd39STudor-Dan Ambarus 
249447d3fd39STudor-Dan Ambarus 	if (vec->genkey) {
249547d3fd39STudor-Dan Ambarus 		/* Save the shared secret obtained by party A */
2496e3d90e52SChristopher Diaz Riveros 		a_ss = kmemdup(sg_virt(req->dst), vec->expected_ss_size, GFP_KERNEL);
249747d3fd39STudor-Dan Ambarus 		if (!a_ss) {
249847d3fd39STudor-Dan Ambarus 			err = -ENOMEM;
249947d3fd39STudor-Dan Ambarus 			goto free_all;
250047d3fd39STudor-Dan Ambarus 		}
250147d3fd39STudor-Dan Ambarus 
250247d3fd39STudor-Dan Ambarus 		/*
250347d3fd39STudor-Dan Ambarus 		 * Calculate party B's shared secret by using party A's
250447d3fd39STudor-Dan Ambarus 		 * public key.
250547d3fd39STudor-Dan Ambarus 		 */
250647d3fd39STudor-Dan Ambarus 		err = crypto_kpp_set_secret(tfm, vec->b_secret,
250747d3fd39STudor-Dan Ambarus 					    vec->b_secret_size);
250847d3fd39STudor-Dan Ambarus 		if (err < 0)
250947d3fd39STudor-Dan Ambarus 			goto free_all;
251047d3fd39STudor-Dan Ambarus 
251147d3fd39STudor-Dan Ambarus 		sg_init_one(&src, a_public, vec->expected_a_public_size);
251247d3fd39STudor-Dan Ambarus 		sg_init_one(&dst, output_buf, out_len_max);
251347d3fd39STudor-Dan Ambarus 		kpp_request_set_input(req, &src, vec->expected_a_public_size);
251447d3fd39STudor-Dan Ambarus 		kpp_request_set_output(req, &dst, out_len_max);
251547d3fd39STudor-Dan Ambarus 		kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
25167f397136SGilad Ben-Yossef 					 crypto_req_done, &wait);
25177f397136SGilad Ben-Yossef 		err = crypto_wait_req(crypto_kpp_compute_shared_secret(req),
25187f397136SGilad Ben-Yossef 				      &wait);
251947d3fd39STudor-Dan Ambarus 		if (err) {
252047d3fd39STudor-Dan Ambarus 			pr_err("alg: %s: Party B: compute shared secret failed. err %d\n",
252147d3fd39STudor-Dan Ambarus 			       alg, err);
252247d3fd39STudor-Dan Ambarus 			goto free_all;
252347d3fd39STudor-Dan Ambarus 		}
252447d3fd39STudor-Dan Ambarus 
252547d3fd39STudor-Dan Ambarus 		shared_secret = a_ss;
252647d3fd39STudor-Dan Ambarus 	} else {
252747d3fd39STudor-Dan Ambarus 		shared_secret = (void *)vec->expected_ss;
252847d3fd39STudor-Dan Ambarus 	}
252947d3fd39STudor-Dan Ambarus 
2530802c7f1cSSalvatore Benedetto 	/*
2531802c7f1cSSalvatore Benedetto 	 * verify shared secret from which the user will derive
2532802c7f1cSSalvatore Benedetto 	 * secret key by executing whatever hash it has chosen
2533802c7f1cSSalvatore Benedetto 	 */
253447d3fd39STudor-Dan Ambarus 	if (memcmp(shared_secret, sg_virt(req->dst),
2535802c7f1cSSalvatore Benedetto 		   vec->expected_ss_size)) {
2536802c7f1cSSalvatore Benedetto 		pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
2537802c7f1cSSalvatore Benedetto 		       alg);
2538802c7f1cSSalvatore Benedetto 		err = -EINVAL;
2539802c7f1cSSalvatore Benedetto 	}
2540802c7f1cSSalvatore Benedetto 
2541802c7f1cSSalvatore Benedetto free_all:
254247d3fd39STudor-Dan Ambarus 	kfree(a_ss);
2543802c7f1cSSalvatore Benedetto 	kfree(input_buf);
2544802c7f1cSSalvatore Benedetto free_output:
254547d3fd39STudor-Dan Ambarus 	kfree(a_public);
2546802c7f1cSSalvatore Benedetto 	kfree(output_buf);
2547802c7f1cSSalvatore Benedetto free_req:
2548802c7f1cSSalvatore Benedetto 	kpp_request_free(req);
2549802c7f1cSSalvatore Benedetto 	return err;
2550802c7f1cSSalvatore Benedetto }
2551802c7f1cSSalvatore Benedetto 
2552802c7f1cSSalvatore Benedetto static int test_kpp(struct crypto_kpp *tfm, const char *alg,
2553b13b1e0cSEric Biggers 		    const struct kpp_testvec *vecs, unsigned int tcount)
2554802c7f1cSSalvatore Benedetto {
2555802c7f1cSSalvatore Benedetto 	int ret, i;
2556802c7f1cSSalvatore Benedetto 
2557802c7f1cSSalvatore Benedetto 	for (i = 0; i < tcount; i++) {
2558802c7f1cSSalvatore Benedetto 		ret = do_test_kpp(tfm, vecs++, alg);
2559802c7f1cSSalvatore Benedetto 		if (ret) {
2560802c7f1cSSalvatore Benedetto 			pr_err("alg: %s: test failed on vector %d, err=%d\n",
2561802c7f1cSSalvatore Benedetto 			       alg, i + 1, ret);
2562802c7f1cSSalvatore Benedetto 			return ret;
2563802c7f1cSSalvatore Benedetto 		}
2564802c7f1cSSalvatore Benedetto 	}
2565802c7f1cSSalvatore Benedetto 	return 0;
2566802c7f1cSSalvatore Benedetto }
2567802c7f1cSSalvatore Benedetto 
2568802c7f1cSSalvatore Benedetto static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
2569802c7f1cSSalvatore Benedetto 			u32 type, u32 mask)
2570802c7f1cSSalvatore Benedetto {
2571802c7f1cSSalvatore Benedetto 	struct crypto_kpp *tfm;
2572802c7f1cSSalvatore Benedetto 	int err = 0;
2573802c7f1cSSalvatore Benedetto 
2574eed93e0cSHerbert Xu 	tfm = crypto_alloc_kpp(driver, type, mask);
2575802c7f1cSSalvatore Benedetto 	if (IS_ERR(tfm)) {
2576802c7f1cSSalvatore Benedetto 		pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
2577802c7f1cSSalvatore Benedetto 		       driver, PTR_ERR(tfm));
2578802c7f1cSSalvatore Benedetto 		return PTR_ERR(tfm);
2579802c7f1cSSalvatore Benedetto 	}
2580802c7f1cSSalvatore Benedetto 	if (desc->suite.kpp.vecs)
2581802c7f1cSSalvatore Benedetto 		err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
2582802c7f1cSSalvatore Benedetto 			       desc->suite.kpp.count);
2583802c7f1cSSalvatore Benedetto 
2584802c7f1cSSalvatore Benedetto 	crypto_free_kpp(tfm);
2585802c7f1cSSalvatore Benedetto 	return err;
2586802c7f1cSSalvatore Benedetto }
2587802c7f1cSSalvatore Benedetto 
258850d2b643SHerbert Xu static int test_akcipher_one(struct crypto_akcipher *tfm,
2589b13b1e0cSEric Biggers 			     const struct akcipher_testvec *vecs)
2590946cc463STadeusz Struk {
2591df27b26fSHerbert Xu 	char *xbuf[XBUFSIZE];
2592946cc463STadeusz Struk 	struct akcipher_request *req;
2593946cc463STadeusz Struk 	void *outbuf_enc = NULL;
2594946cc463STadeusz Struk 	void *outbuf_dec = NULL;
25957f397136SGilad Ben-Yossef 	struct crypto_wait wait;
2596946cc463STadeusz Struk 	unsigned int out_len_max, out_len = 0;
2597946cc463STadeusz Struk 	int err = -ENOMEM;
2598c7381b01SVitaly Chikunov 	struct scatterlist src, dst, src_tab[3];
25990507de94SVitaly Chikunov 	const char *m, *c;
26000507de94SVitaly Chikunov 	unsigned int m_size, c_size;
26010507de94SVitaly Chikunov 	const char *op;
2602946cc463STadeusz Struk 
2603df27b26fSHerbert Xu 	if (testmgr_alloc_buf(xbuf))
2604df27b26fSHerbert Xu 		return err;
2605df27b26fSHerbert Xu 
2606946cc463STadeusz Struk 	req = akcipher_request_alloc(tfm, GFP_KERNEL);
2607946cc463STadeusz Struk 	if (!req)
2608df27b26fSHerbert Xu 		goto free_xbuf;
2609946cc463STadeusz Struk 
26107f397136SGilad Ben-Yossef 	crypto_init_wait(&wait);
261122287b0bSTadeusz Struk 
261222287b0bSTadeusz Struk 	if (vecs->public_key_vec)
261322287b0bSTadeusz Struk 		err = crypto_akcipher_set_pub_key(tfm, vecs->key,
261422287b0bSTadeusz Struk 						  vecs->key_len);
261522287b0bSTadeusz Struk 	else
261622287b0bSTadeusz Struk 		err = crypto_akcipher_set_priv_key(tfm, vecs->key,
261722287b0bSTadeusz Struk 						   vecs->key_len);
2618946cc463STadeusz Struk 	if (err)
2619946cc463STadeusz Struk 		goto free_req;
2620946cc463STadeusz Struk 
26210507de94SVitaly Chikunov 	/*
26220507de94SVitaly Chikunov 	 * First run test which do not require a private key, such as
26230507de94SVitaly Chikunov 	 * encrypt or verify.
26240507de94SVitaly Chikunov 	 */
2625c7381b01SVitaly Chikunov 	err = -ENOMEM;
2626c7381b01SVitaly Chikunov 	out_len_max = crypto_akcipher_maxsize(tfm);
2627946cc463STadeusz Struk 	outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
2628946cc463STadeusz Struk 	if (!outbuf_enc)
2629946cc463STadeusz Struk 		goto free_req;
2630946cc463STadeusz Struk 
26310507de94SVitaly Chikunov 	if (!vecs->siggen_sigver_test) {
26320507de94SVitaly Chikunov 		m = vecs->m;
26330507de94SVitaly Chikunov 		m_size = vecs->m_size;
26340507de94SVitaly Chikunov 		c = vecs->c;
26350507de94SVitaly Chikunov 		c_size = vecs->c_size;
26360507de94SVitaly Chikunov 		op = "encrypt";
26370507de94SVitaly Chikunov 	} else {
26380507de94SVitaly Chikunov 		/* Swap args so we could keep plaintext (digest)
26390507de94SVitaly Chikunov 		 * in vecs->m, and cooked signature in vecs->c.
26400507de94SVitaly Chikunov 		 */
26410507de94SVitaly Chikunov 		m = vecs->c; /* signature */
26420507de94SVitaly Chikunov 		m_size = vecs->c_size;
26430507de94SVitaly Chikunov 		c = vecs->m; /* digest */
26440507de94SVitaly Chikunov 		c_size = vecs->m_size;
26450507de94SVitaly Chikunov 		op = "verify";
26460507de94SVitaly Chikunov 	}
2647df27b26fSHerbert Xu 
26480507de94SVitaly Chikunov 	if (WARN_ON(m_size > PAGE_SIZE))
26490507de94SVitaly Chikunov 		goto free_all;
26500507de94SVitaly Chikunov 	memcpy(xbuf[0], m, m_size);
2651df27b26fSHerbert Xu 
2652c7381b01SVitaly Chikunov 	sg_init_table(src_tab, 3);
2653df27b26fSHerbert Xu 	sg_set_buf(&src_tab[0], xbuf[0], 8);
26540507de94SVitaly Chikunov 	sg_set_buf(&src_tab[1], xbuf[0] + 8, m_size - 8);
2655c7381b01SVitaly Chikunov 	if (vecs->siggen_sigver_test) {
2656c7381b01SVitaly Chikunov 		if (WARN_ON(c_size > PAGE_SIZE))
2657c7381b01SVitaly Chikunov 			goto free_all;
2658c7381b01SVitaly Chikunov 		memcpy(xbuf[1], c, c_size);
2659c7381b01SVitaly Chikunov 		sg_set_buf(&src_tab[2], xbuf[1], c_size);
2660c7381b01SVitaly Chikunov 		akcipher_request_set_crypt(req, src_tab, NULL, m_size, c_size);
2661c7381b01SVitaly Chikunov 	} else {
266222287b0bSTadeusz Struk 		sg_init_one(&dst, outbuf_enc, out_len_max);
26630507de94SVitaly Chikunov 		akcipher_request_set_crypt(req, src_tab, &dst, m_size,
266422287b0bSTadeusz Struk 					   out_len_max);
2665c7381b01SVitaly Chikunov 	}
2666946cc463STadeusz Struk 	akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
26677f397136SGilad Ben-Yossef 				      crypto_req_done, &wait);
2668946cc463STadeusz Struk 
26697f397136SGilad Ben-Yossef 	err = crypto_wait_req(vecs->siggen_sigver_test ?
26700507de94SVitaly Chikunov 			      /* Run asymmetric signature verification */
26710507de94SVitaly Chikunov 			      crypto_akcipher_verify(req) :
26721207107cSStephan Mueller 			      /* Run asymmetric encrypt */
26737f397136SGilad Ben-Yossef 			      crypto_akcipher_encrypt(req), &wait);
2674946cc463STadeusz Struk 	if (err) {
26750507de94SVitaly Chikunov 		pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
2676946cc463STadeusz Struk 		goto free_all;
2677946cc463STadeusz Struk 	}
2678c7381b01SVitaly Chikunov 	if (!vecs->siggen_sigver_test) {
26790507de94SVitaly Chikunov 		if (req->dst_len != c_size) {
26800507de94SVitaly Chikunov 			pr_err("alg: akcipher: %s test failed. Invalid output len\n",
26810507de94SVitaly Chikunov 			       op);
2682946cc463STadeusz Struk 			err = -EINVAL;
2683946cc463STadeusz Struk 			goto free_all;
2684946cc463STadeusz Struk 		}
2685946cc463STadeusz Struk 		/* verify that encrypted message is equal to expected */
2686c7381b01SVitaly Chikunov 		if (memcmp(c, outbuf_enc, c_size) != 0) {
2687c7381b01SVitaly Chikunov 			pr_err("alg: akcipher: %s test failed. Invalid output\n",
2688c7381b01SVitaly Chikunov 			       op);
26890507de94SVitaly Chikunov 			hexdump(outbuf_enc, c_size);
2690946cc463STadeusz Struk 			err = -EINVAL;
2691946cc463STadeusz Struk 			goto free_all;
2692946cc463STadeusz Struk 		}
2693c7381b01SVitaly Chikunov 	}
26940507de94SVitaly Chikunov 
26950507de94SVitaly Chikunov 	/*
26960507de94SVitaly Chikunov 	 * Don't invoke (decrypt or sign) test which require a private key
26970507de94SVitaly Chikunov 	 * for vectors with only a public key.
26980507de94SVitaly Chikunov 	 */
2699946cc463STadeusz Struk 	if (vecs->public_key_vec) {
2700946cc463STadeusz Struk 		err = 0;
2701946cc463STadeusz Struk 		goto free_all;
2702946cc463STadeusz Struk 	}
2703946cc463STadeusz Struk 	outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
2704946cc463STadeusz Struk 	if (!outbuf_dec) {
2705946cc463STadeusz Struk 		err = -ENOMEM;
2706946cc463STadeusz Struk 		goto free_all;
2707946cc463STadeusz Struk 	}
2708df27b26fSHerbert Xu 
27090507de94SVitaly Chikunov 	op = vecs->siggen_sigver_test ? "sign" : "decrypt";
27100507de94SVitaly Chikunov 	if (WARN_ON(c_size > PAGE_SIZE))
2711df27b26fSHerbert Xu 		goto free_all;
27120507de94SVitaly Chikunov 	memcpy(xbuf[0], c, c_size);
2713df27b26fSHerbert Xu 
27140507de94SVitaly Chikunov 	sg_init_one(&src, xbuf[0], c_size);
271522287b0bSTadeusz Struk 	sg_init_one(&dst, outbuf_dec, out_len_max);
27167f397136SGilad Ben-Yossef 	crypto_init_wait(&wait);
27170507de94SVitaly Chikunov 	akcipher_request_set_crypt(req, &src, &dst, c_size, out_len_max);
2718946cc463STadeusz Struk 
27197f397136SGilad Ben-Yossef 	err = crypto_wait_req(vecs->siggen_sigver_test ?
27200507de94SVitaly Chikunov 			      /* Run asymmetric signature generation */
27210507de94SVitaly Chikunov 			      crypto_akcipher_sign(req) :
27221207107cSStephan Mueller 			      /* Run asymmetric decrypt */
27237f397136SGilad Ben-Yossef 			      crypto_akcipher_decrypt(req), &wait);
2724946cc463STadeusz Struk 	if (err) {
27250507de94SVitaly Chikunov 		pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
2726946cc463STadeusz Struk 		goto free_all;
2727946cc463STadeusz Struk 	}
2728946cc463STadeusz Struk 	out_len = req->dst_len;
27290507de94SVitaly Chikunov 	if (out_len < m_size) {
27300507de94SVitaly Chikunov 		pr_err("alg: akcipher: %s test failed. Invalid output len %u\n",
27310507de94SVitaly Chikunov 		       op, out_len);
2732946cc463STadeusz Struk 		err = -EINVAL;
2733946cc463STadeusz Struk 		goto free_all;
2734946cc463STadeusz Struk 	}
2735946cc463STadeusz Struk 	/* verify that decrypted message is equal to the original msg */
27360507de94SVitaly Chikunov 	if (memchr_inv(outbuf_dec, 0, out_len - m_size) ||
27370507de94SVitaly Chikunov 	    memcmp(m, outbuf_dec + out_len - m_size, m_size)) {
27380507de94SVitaly Chikunov 		pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
273950d2b643SHerbert Xu 		hexdump(outbuf_dec, out_len);
2740946cc463STadeusz Struk 		err = -EINVAL;
2741946cc463STadeusz Struk 	}
2742946cc463STadeusz Struk free_all:
2743946cc463STadeusz Struk 	kfree(outbuf_dec);
2744946cc463STadeusz Struk 	kfree(outbuf_enc);
2745946cc463STadeusz Struk free_req:
2746946cc463STadeusz Struk 	akcipher_request_free(req);
2747df27b26fSHerbert Xu free_xbuf:
2748df27b26fSHerbert Xu 	testmgr_free_buf(xbuf);
2749946cc463STadeusz Struk 	return err;
2750946cc463STadeusz Struk }
2751946cc463STadeusz Struk 
275250d2b643SHerbert Xu static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
2753b13b1e0cSEric Biggers 			 const struct akcipher_testvec *vecs,
2754b13b1e0cSEric Biggers 			 unsigned int tcount)
2755946cc463STadeusz Struk {
275615226e48SHerbert Xu 	const char *algo =
275715226e48SHerbert Xu 		crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
2758946cc463STadeusz Struk 	int ret, i;
2759946cc463STadeusz Struk 
2760946cc463STadeusz Struk 	for (i = 0; i < tcount; i++) {
276150d2b643SHerbert Xu 		ret = test_akcipher_one(tfm, vecs++);
276250d2b643SHerbert Xu 		if (!ret)
276350d2b643SHerbert Xu 			continue;
276450d2b643SHerbert Xu 
276515226e48SHerbert Xu 		pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
276615226e48SHerbert Xu 		       i + 1, algo, ret);
2767946cc463STadeusz Struk 		return ret;
2768946cc463STadeusz Struk 	}
2769946cc463STadeusz Struk 	return 0;
2770946cc463STadeusz Struk }
2771946cc463STadeusz Struk 
2772946cc463STadeusz Struk static int alg_test_akcipher(const struct alg_test_desc *desc,
2773946cc463STadeusz Struk 			     const char *driver, u32 type, u32 mask)
2774946cc463STadeusz Struk {
2775946cc463STadeusz Struk 	struct crypto_akcipher *tfm;
2776946cc463STadeusz Struk 	int err = 0;
2777946cc463STadeusz Struk 
2778eed93e0cSHerbert Xu 	tfm = crypto_alloc_akcipher(driver, type, mask);
2779946cc463STadeusz Struk 	if (IS_ERR(tfm)) {
2780946cc463STadeusz Struk 		pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
2781946cc463STadeusz Struk 		       driver, PTR_ERR(tfm));
2782946cc463STadeusz Struk 		return PTR_ERR(tfm);
2783946cc463STadeusz Struk 	}
2784946cc463STadeusz Struk 	if (desc->suite.akcipher.vecs)
2785946cc463STadeusz Struk 		err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
2786946cc463STadeusz Struk 				    desc->suite.akcipher.count);
2787946cc463STadeusz Struk 
2788946cc463STadeusz Struk 	crypto_free_akcipher(tfm);
2789946cc463STadeusz Struk 	return err;
2790946cc463STadeusz Struk }
2791946cc463STadeusz Struk 
2792863b557aSYouquan, Song static int alg_test_null(const struct alg_test_desc *desc,
2793863b557aSYouquan, Song 			     const char *driver, u32 type, u32 mask)
2794863b557aSYouquan, Song {
2795863b557aSYouquan, Song 	return 0;
2796863b557aSYouquan, Song }
2797863b557aSYouquan, Song 
279821c8e720SArd Biesheuvel #define __VECS(tv)	{ .vecs = tv, .count = ARRAY_SIZE(tv) }
279921c8e720SArd Biesheuvel 
2800da7f033dSHerbert Xu /* Please keep this list sorted by algorithm name. */
2801da7f033dSHerbert Xu static const struct alg_test_desc alg_test_descs[] = {
2802da7f033dSHerbert Xu 	{
2803059c2a4dSEric Biggers 		.alg = "adiantum(xchacha12,aes)",
2804059c2a4dSEric Biggers 		.test = alg_test_skcipher,
2805059c2a4dSEric Biggers 		.suite = {
2806059c2a4dSEric Biggers 			.cipher = __VECS(adiantum_xchacha12_aes_tv_template)
2807059c2a4dSEric Biggers 		},
2808059c2a4dSEric Biggers 	}, {
2809059c2a4dSEric Biggers 		.alg = "adiantum(xchacha20,aes)",
2810059c2a4dSEric Biggers 		.test = alg_test_skcipher,
2811059c2a4dSEric Biggers 		.suite = {
2812059c2a4dSEric Biggers 			.cipher = __VECS(adiantum_xchacha20_aes_tv_template)
2813059c2a4dSEric Biggers 		},
2814059c2a4dSEric Biggers 	}, {
2815b87dc203SOndrej Mosnacek 		.alg = "aegis128",
2816b87dc203SOndrej Mosnacek 		.test = alg_test_aead,
2817b87dc203SOndrej Mosnacek 		.suite = {
2818a0d608eeSEric Biggers 			.aead = __VECS(aegis128_tv_template)
2819b87dc203SOndrej Mosnacek 		}
2820b87dc203SOndrej Mosnacek 	}, {
2821b87dc203SOndrej Mosnacek 		.alg = "aegis128l",
2822b87dc203SOndrej Mosnacek 		.test = alg_test_aead,
2823b87dc203SOndrej Mosnacek 		.suite = {
2824a0d608eeSEric Biggers 			.aead = __VECS(aegis128l_tv_template)
2825b87dc203SOndrej Mosnacek 		}
2826b87dc203SOndrej Mosnacek 	}, {
2827b87dc203SOndrej Mosnacek 		.alg = "aegis256",
2828b87dc203SOndrej Mosnacek 		.test = alg_test_aead,
2829b87dc203SOndrej Mosnacek 		.suite = {
2830a0d608eeSEric Biggers 			.aead = __VECS(aegis256_tv_template)
2831b87dc203SOndrej Mosnacek 		}
2832b87dc203SOndrej Mosnacek 	}, {
2833e08ca2daSJarod Wilson 		.alg = "ansi_cprng",
2834e08ca2daSJarod Wilson 		.test = alg_test_cprng,
2835e08ca2daSJarod Wilson 		.suite = {
283621c8e720SArd Biesheuvel 			.cprng = __VECS(ansi_cprng_aes_tv_template)
2837e08ca2daSJarod Wilson 		}
2838e08ca2daSJarod Wilson 	}, {
2839bca4feb0SHoria Geanta 		.alg = "authenc(hmac(md5),ecb(cipher_null))",
2840bca4feb0SHoria Geanta 		.test = alg_test_aead,
2841bca4feb0SHoria Geanta 		.suite = {
2842a0d608eeSEric Biggers 			.aead = __VECS(hmac_md5_ecb_cipher_null_tv_template)
2843bca4feb0SHoria Geanta 		}
2844bca4feb0SHoria Geanta 	}, {
2845a4198fd4SHerbert Xu 		.alg = "authenc(hmac(sha1),cbc(aes))",
2846e46e9a46SHoria Geanta 		.test = alg_test_aead,
2847bcf741cbSHerbert Xu 		.fips_allowed = 1,
2848e46e9a46SHoria Geanta 		.suite = {
2849a0d608eeSEric Biggers 			.aead = __VECS(hmac_sha1_aes_cbc_tv_temp)
28505208ed2cSNitesh Lal 		}
28515208ed2cSNitesh Lal 	}, {
2852a4198fd4SHerbert Xu 		.alg = "authenc(hmac(sha1),cbc(des))",
28535208ed2cSNitesh Lal 		.test = alg_test_aead,
28545208ed2cSNitesh Lal 		.suite = {
2855a0d608eeSEric Biggers 			.aead = __VECS(hmac_sha1_des_cbc_tv_temp)
28565208ed2cSNitesh Lal 		}
28575208ed2cSNitesh Lal 	}, {
2858a4198fd4SHerbert Xu 		.alg = "authenc(hmac(sha1),cbc(des3_ede))",
28595208ed2cSNitesh Lal 		.test = alg_test_aead,
2860ed1afac9SMarcus Meissner 		.fips_allowed = 1,
28615208ed2cSNitesh Lal 		.suite = {
2862a0d608eeSEric Biggers 			.aead = __VECS(hmac_sha1_des3_ede_cbc_tv_temp)
2863e46e9a46SHoria Geanta 		}
2864e46e9a46SHoria Geanta 	}, {
2865fb16abc2SMarcus Meissner 		.alg = "authenc(hmac(sha1),ctr(aes))",
2866fb16abc2SMarcus Meissner 		.test = alg_test_null,
2867fb16abc2SMarcus Meissner 		.fips_allowed = 1,
2868fb16abc2SMarcus Meissner 	}, {
2869bca4feb0SHoria Geanta 		.alg = "authenc(hmac(sha1),ecb(cipher_null))",
2870bca4feb0SHoria Geanta 		.test = alg_test_aead,
2871bca4feb0SHoria Geanta 		.suite = {
2872a0d608eeSEric Biggers 			.aead = __VECS(hmac_sha1_ecb_cipher_null_tv_temp)
28735208ed2cSNitesh Lal 		}
28745208ed2cSNitesh Lal 	}, {
28758888690eSMarcus Meissner 		.alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
28768888690eSMarcus Meissner 		.test = alg_test_null,
28778888690eSMarcus Meissner 		.fips_allowed = 1,
28788888690eSMarcus Meissner 	}, {
2879a4198fd4SHerbert Xu 		.alg = "authenc(hmac(sha224),cbc(des))",
28805208ed2cSNitesh Lal 		.test = alg_test_aead,
28815208ed2cSNitesh Lal 		.suite = {
2882a0d608eeSEric Biggers 			.aead = __VECS(hmac_sha224_des_cbc_tv_temp)
28835208ed2cSNitesh Lal 		}
28845208ed2cSNitesh Lal 	}, {
2885a4198fd4SHerbert Xu 		.alg = "authenc(hmac(sha224),cbc(des3_ede))",
28865208ed2cSNitesh Lal 		.test = alg_test_aead,
2887ed1afac9SMarcus Meissner 		.fips_allowed = 1,
28885208ed2cSNitesh Lal 		.suite = {
2889a0d608eeSEric Biggers 			.aead = __VECS(hmac_sha224_des3_ede_cbc_tv_temp)
2890bca4feb0SHoria Geanta 		}
2891bca4feb0SHoria Geanta 	}, {
2892a4198fd4SHerbert Xu 		.alg = "authenc(hmac(sha256),cbc(aes))",
2893e46e9a46SHoria Geanta 		.test = alg_test_aead,
2894ed1afac9SMarcus Meissner 		.fips_allowed = 1,
2895e46e9a46SHoria Geanta 		.suite = {
2896a0d608eeSEric Biggers 			.aead = __VECS(hmac_sha256_aes_cbc_tv_temp)
28975208ed2cSNitesh Lal 		}
28985208ed2cSNitesh Lal 	}, {
2899a4198fd4SHerbert Xu 		.alg = "authenc(hmac(sha256),cbc(des))",
29005208ed2cSNitesh Lal 		.test = alg_test_aead,
29015208ed2cSNitesh Lal 		.suite = {
2902a0d608eeSEric Biggers 			.aead = __VECS(hmac_sha256_des_cbc_tv_temp)
29035208ed2cSNitesh Lal 		}
29045208ed2cSNitesh Lal 	}, {
2905a4198fd4SHerbert Xu 		.alg = "authenc(hmac(sha256),cbc(des3_ede))",
29065208ed2cSNitesh Lal 		.test = alg_test_aead,
2907ed1afac9SMarcus Meissner 		.fips_allowed = 1,
29085208ed2cSNitesh Lal 		.suite = {
2909a0d608eeSEric Biggers 			.aead = __VECS(hmac_sha256_des3_ede_cbc_tv_temp)
29105208ed2cSNitesh Lal 		}
29115208ed2cSNitesh Lal 	}, {
2912fb16abc2SMarcus Meissner 		.alg = "authenc(hmac(sha256),ctr(aes))",
2913fb16abc2SMarcus Meissner 		.test = alg_test_null,
2914fb16abc2SMarcus Meissner 		.fips_allowed = 1,
2915fb16abc2SMarcus Meissner 	}, {
29168888690eSMarcus Meissner 		.alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
29178888690eSMarcus Meissner 		.test = alg_test_null,
29188888690eSMarcus Meissner 		.fips_allowed = 1,
29198888690eSMarcus Meissner 	}, {
2920a4198fd4SHerbert Xu 		.alg = "authenc(hmac(sha384),cbc(des))",
29215208ed2cSNitesh Lal 		.test = alg_test_aead,
29225208ed2cSNitesh Lal 		.suite = {
2923a0d608eeSEric Biggers 			.aead = __VECS(hmac_sha384_des_cbc_tv_temp)
29245208ed2cSNitesh Lal 		}
29255208ed2cSNitesh Lal 	}, {
2926a4198fd4SHerbert Xu 		.alg = "authenc(hmac(sha384),cbc(des3_ede))",
29275208ed2cSNitesh Lal 		.test = alg_test_aead,
2928ed1afac9SMarcus Meissner 		.fips_allowed = 1,
29295208ed2cSNitesh Lal 		.suite = {
2930a0d608eeSEric Biggers 			.aead = __VECS(hmac_sha384_des3_ede_cbc_tv_temp)
2931e46e9a46SHoria Geanta 		}
2932e46e9a46SHoria Geanta 	}, {
2933fb16abc2SMarcus Meissner 		.alg = "authenc(hmac(sha384),ctr(aes))",
2934fb16abc2SMarcus Meissner 		.test = alg_test_null,
2935fb16abc2SMarcus Meissner 		.fips_allowed = 1,
2936fb16abc2SMarcus Meissner 	}, {
29378888690eSMarcus Meissner 		.alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
29388888690eSMarcus Meissner 		.test = alg_test_null,
29398888690eSMarcus Meissner 		.fips_allowed = 1,
29408888690eSMarcus Meissner 	}, {
2941a4198fd4SHerbert Xu 		.alg = "authenc(hmac(sha512),cbc(aes))",
2942ed1afac9SMarcus Meissner 		.fips_allowed = 1,
2943e46e9a46SHoria Geanta 		.test = alg_test_aead,
2944e46e9a46SHoria Geanta 		.suite = {
2945a0d608eeSEric Biggers 			.aead = __VECS(hmac_sha512_aes_cbc_tv_temp)
29465208ed2cSNitesh Lal 		}
29475208ed2cSNitesh Lal 	}, {
2948a4198fd4SHerbert Xu 		.alg = "authenc(hmac(sha512),cbc(des))",
29495208ed2cSNitesh Lal 		.test = alg_test_aead,
29505208ed2cSNitesh Lal 		.suite = {
2951a0d608eeSEric Biggers 			.aead = __VECS(hmac_sha512_des_cbc_tv_temp)
29525208ed2cSNitesh Lal 		}
29535208ed2cSNitesh Lal 	}, {
2954a4198fd4SHerbert Xu 		.alg = "authenc(hmac(sha512),cbc(des3_ede))",
29555208ed2cSNitesh Lal 		.test = alg_test_aead,
2956ed1afac9SMarcus Meissner 		.fips_allowed = 1,
29575208ed2cSNitesh Lal 		.suite = {
2958a0d608eeSEric Biggers 			.aead = __VECS(hmac_sha512_des3_ede_cbc_tv_temp)
2959e46e9a46SHoria Geanta 		}
2960e46e9a46SHoria Geanta 	}, {
2961fb16abc2SMarcus Meissner 		.alg = "authenc(hmac(sha512),ctr(aes))",
2962fb16abc2SMarcus Meissner 		.test = alg_test_null,
2963fb16abc2SMarcus Meissner 		.fips_allowed = 1,
2964fb16abc2SMarcus Meissner 	}, {
29658888690eSMarcus Meissner 		.alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
29668888690eSMarcus Meissner 		.test = alg_test_null,
29678888690eSMarcus Meissner 		.fips_allowed = 1,
29688888690eSMarcus Meissner 	}, {
2969da7f033dSHerbert Xu 		.alg = "cbc(aes)",
29701aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
2971a1915d51SJarod Wilson 		.fips_allowed = 1,
2972da7f033dSHerbert Xu 		.suite = {
297392a4c9feSEric Biggers 			.cipher = __VECS(aes_cbc_tv_template)
297492a4c9feSEric Biggers 		},
2975da7f033dSHerbert Xu 	}, {
2976da7f033dSHerbert Xu 		.alg = "cbc(anubis)",
29771aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
2978da7f033dSHerbert Xu 		.suite = {
297992a4c9feSEric Biggers 			.cipher = __VECS(anubis_cbc_tv_template)
298092a4c9feSEric Biggers 		},
2981da7f033dSHerbert Xu 	}, {
2982da7f033dSHerbert Xu 		.alg = "cbc(blowfish)",
29831aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
2984da7f033dSHerbert Xu 		.suite = {
298592a4c9feSEric Biggers 			.cipher = __VECS(bf_cbc_tv_template)
298692a4c9feSEric Biggers 		},
2987da7f033dSHerbert Xu 	}, {
2988da7f033dSHerbert Xu 		.alg = "cbc(camellia)",
29891aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
2990da7f033dSHerbert Xu 		.suite = {
299192a4c9feSEric Biggers 			.cipher = __VECS(camellia_cbc_tv_template)
299292a4c9feSEric Biggers 		},
2993da7f033dSHerbert Xu 	}, {
2994a2c58260SJohannes Goetzfried 		.alg = "cbc(cast5)",
2995a2c58260SJohannes Goetzfried 		.test = alg_test_skcipher,
2996a2c58260SJohannes Goetzfried 		.suite = {
299792a4c9feSEric Biggers 			.cipher = __VECS(cast5_cbc_tv_template)
299892a4c9feSEric Biggers 		},
2999a2c58260SJohannes Goetzfried 	}, {
30009b8b0405SJohannes Goetzfried 		.alg = "cbc(cast6)",
30019b8b0405SJohannes Goetzfried 		.test = alg_test_skcipher,
30029b8b0405SJohannes Goetzfried 		.suite = {
300392a4c9feSEric Biggers 			.cipher = __VECS(cast6_cbc_tv_template)
300492a4c9feSEric Biggers 		},
30059b8b0405SJohannes Goetzfried 	}, {
3006da7f033dSHerbert Xu 		.alg = "cbc(des)",
30071aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
3008da7f033dSHerbert Xu 		.suite = {
300992a4c9feSEric Biggers 			.cipher = __VECS(des_cbc_tv_template)
301092a4c9feSEric Biggers 		},
3011da7f033dSHerbert Xu 	}, {
3012da7f033dSHerbert Xu 		.alg = "cbc(des3_ede)",
30131aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
3014a1915d51SJarod Wilson 		.fips_allowed = 1,
3015da7f033dSHerbert Xu 		.suite = {
301692a4c9feSEric Biggers 			.cipher = __VECS(des3_ede_cbc_tv_template)
301792a4c9feSEric Biggers 		},
3018da7f033dSHerbert Xu 	}, {
3019a794d8d8SGilad Ben-Yossef 		/* Same as cbc(aes) except the key is stored in
3020a794d8d8SGilad Ben-Yossef 		 * hardware secure memory which we reference by index
3021a794d8d8SGilad Ben-Yossef 		 */
3022a794d8d8SGilad Ben-Yossef 		.alg = "cbc(paes)",
3023a794d8d8SGilad Ben-Yossef 		.test = alg_test_null,
3024a794d8d8SGilad Ben-Yossef 		.fips_allowed = 1,
3025a794d8d8SGilad Ben-Yossef 	}, {
30269d25917dSJussi Kivilinna 		.alg = "cbc(serpent)",
30279d25917dSJussi Kivilinna 		.test = alg_test_skcipher,
30289d25917dSJussi Kivilinna 		.suite = {
302992a4c9feSEric Biggers 			.cipher = __VECS(serpent_cbc_tv_template)
303092a4c9feSEric Biggers 		},
30319d25917dSJussi Kivilinna 	}, {
303295ba5973SGilad Ben-Yossef 		.alg = "cbc(sm4)",
303395ba5973SGilad Ben-Yossef 		.test = alg_test_skcipher,
303495ba5973SGilad Ben-Yossef 		.suite = {
303595ba5973SGilad Ben-Yossef 			.cipher = __VECS(sm4_cbc_tv_template)
303695ba5973SGilad Ben-Yossef 		}
303795ba5973SGilad Ben-Yossef 	}, {
3038da7f033dSHerbert Xu 		.alg = "cbc(twofish)",
30391aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
3040da7f033dSHerbert Xu 		.suite = {
304192a4c9feSEric Biggers 			.cipher = __VECS(tf_cbc_tv_template)
304292a4c9feSEric Biggers 		},
3043da7f033dSHerbert Xu 	}, {
3044092acf06SArd Biesheuvel 		.alg = "cbcmac(aes)",
3045092acf06SArd Biesheuvel 		.fips_allowed = 1,
3046092acf06SArd Biesheuvel 		.test = alg_test_hash,
3047092acf06SArd Biesheuvel 		.suite = {
3048092acf06SArd Biesheuvel 			.hash = __VECS(aes_cbcmac_tv_template)
3049092acf06SArd Biesheuvel 		}
3050092acf06SArd Biesheuvel 	}, {
3051da7f033dSHerbert Xu 		.alg = "ccm(aes)",
3052da7f033dSHerbert Xu 		.test = alg_test_aead,
3053a1915d51SJarod Wilson 		.fips_allowed = 1,
3054da7f033dSHerbert Xu 		.suite = {
3055a0d608eeSEric Biggers 			.aead = __VECS(aes_ccm_tv_template)
3056da7f033dSHerbert Xu 		}
3057da7f033dSHerbert Xu 	}, {
30587da66670SDmitry Eremin-Solenikov 		.alg = "cfb(aes)",
30597da66670SDmitry Eremin-Solenikov 		.test = alg_test_skcipher,
30607da66670SDmitry Eremin-Solenikov 		.fips_allowed = 1,
30617da66670SDmitry Eremin-Solenikov 		.suite = {
30627da66670SDmitry Eremin-Solenikov 			.cipher = __VECS(aes_cfb_tv_template)
30637da66670SDmitry Eremin-Solenikov 		},
30647da66670SDmitry Eremin-Solenikov 	}, {
30653590ebf2SMartin Willi 		.alg = "chacha20",
30663590ebf2SMartin Willi 		.test = alg_test_skcipher,
30673590ebf2SMartin Willi 		.suite = {
306892a4c9feSEric Biggers 			.cipher = __VECS(chacha20_tv_template)
306992a4c9feSEric Biggers 		},
30703590ebf2SMartin Willi 	}, {
307193b5e86aSJussi Kivilinna 		.alg = "cmac(aes)",
30728f183751SStephan Mueller 		.fips_allowed = 1,
307393b5e86aSJussi Kivilinna 		.test = alg_test_hash,
307493b5e86aSJussi Kivilinna 		.suite = {
307521c8e720SArd Biesheuvel 			.hash = __VECS(aes_cmac128_tv_template)
307693b5e86aSJussi Kivilinna 		}
307793b5e86aSJussi Kivilinna 	}, {
307893b5e86aSJussi Kivilinna 		.alg = "cmac(des3_ede)",
30798f183751SStephan Mueller 		.fips_allowed = 1,
308093b5e86aSJussi Kivilinna 		.test = alg_test_hash,
308193b5e86aSJussi Kivilinna 		.suite = {
308221c8e720SArd Biesheuvel 			.hash = __VECS(des3_ede_cmac64_tv_template)
308393b5e86aSJussi Kivilinna 		}
308493b5e86aSJussi Kivilinna 	}, {
3085e448370dSJussi Kivilinna 		.alg = "compress_null",
3086e448370dSJussi Kivilinna 		.test = alg_test_null,
3087e448370dSJussi Kivilinna 	}, {
3088ebb3472fSArd Biesheuvel 		.alg = "crc32",
3089ebb3472fSArd Biesheuvel 		.test = alg_test_hash,
3090a8a34416SMilan Broz 		.fips_allowed = 1,
3091ebb3472fSArd Biesheuvel 		.suite = {
309221c8e720SArd Biesheuvel 			.hash = __VECS(crc32_tv_template)
3093ebb3472fSArd Biesheuvel 		}
3094ebb3472fSArd Biesheuvel 	}, {
3095da7f033dSHerbert Xu 		.alg = "crc32c",
30968e3ee85eSHerbert Xu 		.test = alg_test_crc32c,
3097a1915d51SJarod Wilson 		.fips_allowed = 1,
3098da7f033dSHerbert Xu 		.suite = {
309921c8e720SArd Biesheuvel 			.hash = __VECS(crc32c_tv_template)
3100da7f033dSHerbert Xu 		}
3101da7f033dSHerbert Xu 	}, {
310268411521SHerbert Xu 		.alg = "crct10dif",
310368411521SHerbert Xu 		.test = alg_test_hash,
310468411521SHerbert Xu 		.fips_allowed = 1,
310568411521SHerbert Xu 		.suite = {
310621c8e720SArd Biesheuvel 			.hash = __VECS(crct10dif_tv_template)
310768411521SHerbert Xu 		}
310868411521SHerbert Xu 	}, {
3109f7cb80f2SJarod Wilson 		.alg = "ctr(aes)",
3110f7cb80f2SJarod Wilson 		.test = alg_test_skcipher,
3111a1915d51SJarod Wilson 		.fips_allowed = 1,
3112f7cb80f2SJarod Wilson 		.suite = {
311392a4c9feSEric Biggers 			.cipher = __VECS(aes_ctr_tv_template)
3114f7cb80f2SJarod Wilson 		}
3115f7cb80f2SJarod Wilson 	}, {
311685b63e34SJussi Kivilinna 		.alg = "ctr(blowfish)",
311785b63e34SJussi Kivilinna 		.test = alg_test_skcipher,
311885b63e34SJussi Kivilinna 		.suite = {
311992a4c9feSEric Biggers 			.cipher = __VECS(bf_ctr_tv_template)
312085b63e34SJussi Kivilinna 		}
312185b63e34SJussi Kivilinna 	}, {
31220840605eSJussi Kivilinna 		.alg = "ctr(camellia)",
31230840605eSJussi Kivilinna 		.test = alg_test_skcipher,
31240840605eSJussi Kivilinna 		.suite = {
312592a4c9feSEric Biggers 			.cipher = __VECS(camellia_ctr_tv_template)
31260840605eSJussi Kivilinna 		}
31270840605eSJussi Kivilinna 	}, {
3128a2c58260SJohannes Goetzfried 		.alg = "ctr(cast5)",
3129a2c58260SJohannes Goetzfried 		.test = alg_test_skcipher,
3130a2c58260SJohannes Goetzfried 		.suite = {
313192a4c9feSEric Biggers 			.cipher = __VECS(cast5_ctr_tv_template)
3132a2c58260SJohannes Goetzfried 		}
3133a2c58260SJohannes Goetzfried 	}, {
31349b8b0405SJohannes Goetzfried 		.alg = "ctr(cast6)",
31359b8b0405SJohannes Goetzfried 		.test = alg_test_skcipher,
31369b8b0405SJohannes Goetzfried 		.suite = {
313792a4c9feSEric Biggers 			.cipher = __VECS(cast6_ctr_tv_template)
31389b8b0405SJohannes Goetzfried 		}
31399b8b0405SJohannes Goetzfried 	}, {
31408163fc30SJussi Kivilinna 		.alg = "ctr(des)",
31418163fc30SJussi Kivilinna 		.test = alg_test_skcipher,
31428163fc30SJussi Kivilinna 		.suite = {
314392a4c9feSEric Biggers 			.cipher = __VECS(des_ctr_tv_template)
31448163fc30SJussi Kivilinna 		}
31458163fc30SJussi Kivilinna 	}, {
3146e080b17aSJussi Kivilinna 		.alg = "ctr(des3_ede)",
3147e080b17aSJussi Kivilinna 		.test = alg_test_skcipher,
31480d8da104SMarcelo Cerri 		.fips_allowed = 1,
3149e080b17aSJussi Kivilinna 		.suite = {
315092a4c9feSEric Biggers 			.cipher = __VECS(des3_ede_ctr_tv_template)
3151e080b17aSJussi Kivilinna 		}
3152e080b17aSJussi Kivilinna 	}, {
3153a794d8d8SGilad Ben-Yossef 		/* Same as ctr(aes) except the key is stored in
3154a794d8d8SGilad Ben-Yossef 		 * hardware secure memory which we reference by index
3155a794d8d8SGilad Ben-Yossef 		 */
3156a794d8d8SGilad Ben-Yossef 		.alg = "ctr(paes)",
3157a794d8d8SGilad Ben-Yossef 		.test = alg_test_null,
3158a794d8d8SGilad Ben-Yossef 		.fips_allowed = 1,
3159a794d8d8SGilad Ben-Yossef 	}, {
31609d25917dSJussi Kivilinna 		.alg = "ctr(serpent)",
31619d25917dSJussi Kivilinna 		.test = alg_test_skcipher,
31629d25917dSJussi Kivilinna 		.suite = {
316392a4c9feSEric Biggers 			.cipher = __VECS(serpent_ctr_tv_template)
31649d25917dSJussi Kivilinna 		}
31659d25917dSJussi Kivilinna 	}, {
316695ba5973SGilad Ben-Yossef 		.alg = "ctr(sm4)",
316795ba5973SGilad Ben-Yossef 		.test = alg_test_skcipher,
316895ba5973SGilad Ben-Yossef 		.suite = {
316995ba5973SGilad Ben-Yossef 			.cipher = __VECS(sm4_ctr_tv_template)
317095ba5973SGilad Ben-Yossef 		}
317195ba5973SGilad Ben-Yossef 	}, {
3172573da620SJussi Kivilinna 		.alg = "ctr(twofish)",
3173573da620SJussi Kivilinna 		.test = alg_test_skcipher,
3174573da620SJussi Kivilinna 		.suite = {
317592a4c9feSEric Biggers 			.cipher = __VECS(tf_ctr_tv_template)
3176573da620SJussi Kivilinna 		}
3177573da620SJussi Kivilinna 	}, {
3178da7f033dSHerbert Xu 		.alg = "cts(cbc(aes))",
31791aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
3180196ad604SGilad Ben-Yossef 		.fips_allowed = 1,
3181da7f033dSHerbert Xu 		.suite = {
318292a4c9feSEric Biggers 			.cipher = __VECS(cts_mode_tv_template)
3183da7f033dSHerbert Xu 		}
3184da7f033dSHerbert Xu 	}, {
3185da7f033dSHerbert Xu 		.alg = "deflate",
3186da7f033dSHerbert Xu 		.test = alg_test_comp,
31870818904dSMilan Broz 		.fips_allowed = 1,
3188da7f033dSHerbert Xu 		.suite = {
3189da7f033dSHerbert Xu 			.comp = {
319021c8e720SArd Biesheuvel 				.comp = __VECS(deflate_comp_tv_template),
319121c8e720SArd Biesheuvel 				.decomp = __VECS(deflate_decomp_tv_template)
3192da7f033dSHerbert Xu 			}
3193da7f033dSHerbert Xu 		}
3194da7f033dSHerbert Xu 	}, {
3195802c7f1cSSalvatore Benedetto 		.alg = "dh",
3196802c7f1cSSalvatore Benedetto 		.test = alg_test_kpp,
3197802c7f1cSSalvatore Benedetto 		.fips_allowed = 1,
3198802c7f1cSSalvatore Benedetto 		.suite = {
319921c8e720SArd Biesheuvel 			.kpp = __VECS(dh_tv_template)
3200802c7f1cSSalvatore Benedetto 		}
3201802c7f1cSSalvatore Benedetto 	}, {
3202e448370dSJussi Kivilinna 		.alg = "digest_null",
3203e448370dSJussi Kivilinna 		.test = alg_test_null,
3204e448370dSJussi Kivilinna 	}, {
320564d1cdfbSStephan Mueller 		.alg = "drbg_nopr_ctr_aes128",
320664d1cdfbSStephan Mueller 		.test = alg_test_drbg,
320764d1cdfbSStephan Mueller 		.fips_allowed = 1,
320864d1cdfbSStephan Mueller 		.suite = {
320921c8e720SArd Biesheuvel 			.drbg = __VECS(drbg_nopr_ctr_aes128_tv_template)
321064d1cdfbSStephan Mueller 		}
321164d1cdfbSStephan Mueller 	}, {
321264d1cdfbSStephan Mueller 		.alg = "drbg_nopr_ctr_aes192",
321364d1cdfbSStephan Mueller 		.test = alg_test_drbg,
321464d1cdfbSStephan Mueller 		.fips_allowed = 1,
321564d1cdfbSStephan Mueller 		.suite = {
321621c8e720SArd Biesheuvel 			.drbg = __VECS(drbg_nopr_ctr_aes192_tv_template)
321764d1cdfbSStephan Mueller 		}
321864d1cdfbSStephan Mueller 	}, {
321964d1cdfbSStephan Mueller 		.alg = "drbg_nopr_ctr_aes256",
322064d1cdfbSStephan Mueller 		.test = alg_test_drbg,
322164d1cdfbSStephan Mueller 		.fips_allowed = 1,
322264d1cdfbSStephan Mueller 		.suite = {
322321c8e720SArd Biesheuvel 			.drbg = __VECS(drbg_nopr_ctr_aes256_tv_template)
322464d1cdfbSStephan Mueller 		}
322564d1cdfbSStephan Mueller 	}, {
322664d1cdfbSStephan Mueller 		/*
322764d1cdfbSStephan Mueller 		 * There is no need to specifically test the DRBG with every
322864d1cdfbSStephan Mueller 		 * backend cipher -- covered by drbg_nopr_hmac_sha256 test
322964d1cdfbSStephan Mueller 		 */
323064d1cdfbSStephan Mueller 		.alg = "drbg_nopr_hmac_sha1",
323164d1cdfbSStephan Mueller 		.fips_allowed = 1,
323264d1cdfbSStephan Mueller 		.test = alg_test_null,
323364d1cdfbSStephan Mueller 	}, {
323464d1cdfbSStephan Mueller 		.alg = "drbg_nopr_hmac_sha256",
323564d1cdfbSStephan Mueller 		.test = alg_test_drbg,
323664d1cdfbSStephan Mueller 		.fips_allowed = 1,
323764d1cdfbSStephan Mueller 		.suite = {
323821c8e720SArd Biesheuvel 			.drbg = __VECS(drbg_nopr_hmac_sha256_tv_template)
323964d1cdfbSStephan Mueller 		}
324064d1cdfbSStephan Mueller 	}, {
324164d1cdfbSStephan Mueller 		/* covered by drbg_nopr_hmac_sha256 test */
324264d1cdfbSStephan Mueller 		.alg = "drbg_nopr_hmac_sha384",
324364d1cdfbSStephan Mueller 		.fips_allowed = 1,
324464d1cdfbSStephan Mueller 		.test = alg_test_null,
324564d1cdfbSStephan Mueller 	}, {
324664d1cdfbSStephan Mueller 		.alg = "drbg_nopr_hmac_sha512",
324764d1cdfbSStephan Mueller 		.test = alg_test_null,
324864d1cdfbSStephan Mueller 		.fips_allowed = 1,
324964d1cdfbSStephan Mueller 	}, {
325064d1cdfbSStephan Mueller 		.alg = "drbg_nopr_sha1",
325164d1cdfbSStephan Mueller 		.fips_allowed = 1,
325264d1cdfbSStephan Mueller 		.test = alg_test_null,
325364d1cdfbSStephan Mueller 	}, {
325464d1cdfbSStephan Mueller 		.alg = "drbg_nopr_sha256",
325564d1cdfbSStephan Mueller 		.test = alg_test_drbg,
325664d1cdfbSStephan Mueller 		.fips_allowed = 1,
325764d1cdfbSStephan Mueller 		.suite = {
325821c8e720SArd Biesheuvel 			.drbg = __VECS(drbg_nopr_sha256_tv_template)
325964d1cdfbSStephan Mueller 		}
326064d1cdfbSStephan Mueller 	}, {
326164d1cdfbSStephan Mueller 		/* covered by drbg_nopr_sha256 test */
326264d1cdfbSStephan Mueller 		.alg = "drbg_nopr_sha384",
326364d1cdfbSStephan Mueller 		.fips_allowed = 1,
326464d1cdfbSStephan Mueller 		.test = alg_test_null,
326564d1cdfbSStephan Mueller 	}, {
326664d1cdfbSStephan Mueller 		.alg = "drbg_nopr_sha512",
326764d1cdfbSStephan Mueller 		.fips_allowed = 1,
326864d1cdfbSStephan Mueller 		.test = alg_test_null,
326964d1cdfbSStephan Mueller 	}, {
327064d1cdfbSStephan Mueller 		.alg = "drbg_pr_ctr_aes128",
327164d1cdfbSStephan Mueller 		.test = alg_test_drbg,
327264d1cdfbSStephan Mueller 		.fips_allowed = 1,
327364d1cdfbSStephan Mueller 		.suite = {
327421c8e720SArd Biesheuvel 			.drbg = __VECS(drbg_pr_ctr_aes128_tv_template)
327564d1cdfbSStephan Mueller 		}
327664d1cdfbSStephan Mueller 	}, {
327764d1cdfbSStephan Mueller 		/* covered by drbg_pr_ctr_aes128 test */
327864d1cdfbSStephan Mueller 		.alg = "drbg_pr_ctr_aes192",
327964d1cdfbSStephan Mueller 		.fips_allowed = 1,
328064d1cdfbSStephan Mueller 		.test = alg_test_null,
328164d1cdfbSStephan Mueller 	}, {
328264d1cdfbSStephan Mueller 		.alg = "drbg_pr_ctr_aes256",
328364d1cdfbSStephan Mueller 		.fips_allowed = 1,
328464d1cdfbSStephan Mueller 		.test = alg_test_null,
328564d1cdfbSStephan Mueller 	}, {
328664d1cdfbSStephan Mueller 		.alg = "drbg_pr_hmac_sha1",
328764d1cdfbSStephan Mueller 		.fips_allowed = 1,
328864d1cdfbSStephan Mueller 		.test = alg_test_null,
328964d1cdfbSStephan Mueller 	}, {
329064d1cdfbSStephan Mueller 		.alg = "drbg_pr_hmac_sha256",
329164d1cdfbSStephan Mueller 		.test = alg_test_drbg,
329264d1cdfbSStephan Mueller 		.fips_allowed = 1,
329364d1cdfbSStephan Mueller 		.suite = {
329421c8e720SArd Biesheuvel 			.drbg = __VECS(drbg_pr_hmac_sha256_tv_template)
329564d1cdfbSStephan Mueller 		}
329664d1cdfbSStephan Mueller 	}, {
329764d1cdfbSStephan Mueller 		/* covered by drbg_pr_hmac_sha256 test */
329864d1cdfbSStephan Mueller 		.alg = "drbg_pr_hmac_sha384",
329964d1cdfbSStephan Mueller 		.fips_allowed = 1,
330064d1cdfbSStephan Mueller 		.test = alg_test_null,
330164d1cdfbSStephan Mueller 	}, {
330264d1cdfbSStephan Mueller 		.alg = "drbg_pr_hmac_sha512",
330364d1cdfbSStephan Mueller 		.test = alg_test_null,
330464d1cdfbSStephan Mueller 		.fips_allowed = 1,
330564d1cdfbSStephan Mueller 	}, {
330664d1cdfbSStephan Mueller 		.alg = "drbg_pr_sha1",
330764d1cdfbSStephan Mueller 		.fips_allowed = 1,
330864d1cdfbSStephan Mueller 		.test = alg_test_null,
330964d1cdfbSStephan Mueller 	}, {
331064d1cdfbSStephan Mueller 		.alg = "drbg_pr_sha256",
331164d1cdfbSStephan Mueller 		.test = alg_test_drbg,
331264d1cdfbSStephan Mueller 		.fips_allowed = 1,
331364d1cdfbSStephan Mueller 		.suite = {
331421c8e720SArd Biesheuvel 			.drbg = __VECS(drbg_pr_sha256_tv_template)
331564d1cdfbSStephan Mueller 		}
331664d1cdfbSStephan Mueller 	}, {
331764d1cdfbSStephan Mueller 		/* covered by drbg_pr_sha256 test */
331864d1cdfbSStephan Mueller 		.alg = "drbg_pr_sha384",
331964d1cdfbSStephan Mueller 		.fips_allowed = 1,
332064d1cdfbSStephan Mueller 		.test = alg_test_null,
332164d1cdfbSStephan Mueller 	}, {
332264d1cdfbSStephan Mueller 		.alg = "drbg_pr_sha512",
332364d1cdfbSStephan Mueller 		.fips_allowed = 1,
332464d1cdfbSStephan Mueller 		.test = alg_test_null,
332564d1cdfbSStephan Mueller 	}, {
3326da7f033dSHerbert Xu 		.alg = "ecb(aes)",
33271aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
3328a1915d51SJarod Wilson 		.fips_allowed = 1,
3329da7f033dSHerbert Xu 		.suite = {
333092a4c9feSEric Biggers 			.cipher = __VECS(aes_tv_template)
3331da7f033dSHerbert Xu 		}
3332da7f033dSHerbert Xu 	}, {
3333da7f033dSHerbert Xu 		.alg = "ecb(anubis)",
33341aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
3335da7f033dSHerbert Xu 		.suite = {
333692a4c9feSEric Biggers 			.cipher = __VECS(anubis_tv_template)
3337da7f033dSHerbert Xu 		}
3338da7f033dSHerbert Xu 	}, {
3339da7f033dSHerbert Xu 		.alg = "ecb(arc4)",
33401aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
3341da7f033dSHerbert Xu 		.suite = {
334292a4c9feSEric Biggers 			.cipher = __VECS(arc4_tv_template)
3343da7f033dSHerbert Xu 		}
3344da7f033dSHerbert Xu 	}, {
3345da7f033dSHerbert Xu 		.alg = "ecb(blowfish)",
33461aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
3347da7f033dSHerbert Xu 		.suite = {
334892a4c9feSEric Biggers 			.cipher = __VECS(bf_tv_template)
3349da7f033dSHerbert Xu 		}
3350da7f033dSHerbert Xu 	}, {
3351da7f033dSHerbert Xu 		.alg = "ecb(camellia)",
33521aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
3353da7f033dSHerbert Xu 		.suite = {
335492a4c9feSEric Biggers 			.cipher = __VECS(camellia_tv_template)
3355da7f033dSHerbert Xu 		}
3356da7f033dSHerbert Xu 	}, {
3357da7f033dSHerbert Xu 		.alg = "ecb(cast5)",
33581aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
3359da7f033dSHerbert Xu 		.suite = {
336092a4c9feSEric Biggers 			.cipher = __VECS(cast5_tv_template)
3361da7f033dSHerbert Xu 		}
3362da7f033dSHerbert Xu 	}, {
3363da7f033dSHerbert Xu 		.alg = "ecb(cast6)",
33641aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
3365da7f033dSHerbert Xu 		.suite = {
336692a4c9feSEric Biggers 			.cipher = __VECS(cast6_tv_template)
3367da7f033dSHerbert Xu 		}
3368da7f033dSHerbert Xu 	}, {
3369e448370dSJussi Kivilinna 		.alg = "ecb(cipher_null)",
3370e448370dSJussi Kivilinna 		.test = alg_test_null,
33716175ca2bSMilan Broz 		.fips_allowed = 1,
3372e448370dSJussi Kivilinna 	}, {
3373da7f033dSHerbert Xu 		.alg = "ecb(des)",
33741aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
3375da7f033dSHerbert Xu 		.suite = {
337692a4c9feSEric Biggers 			.cipher = __VECS(des_tv_template)
3377da7f033dSHerbert Xu 		}
3378da7f033dSHerbert Xu 	}, {
3379da7f033dSHerbert Xu 		.alg = "ecb(des3_ede)",
33801aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
3381a1915d51SJarod Wilson 		.fips_allowed = 1,
3382da7f033dSHerbert Xu 		.suite = {
338392a4c9feSEric Biggers 			.cipher = __VECS(des3_ede_tv_template)
3384da7f033dSHerbert Xu 		}
3385da7f033dSHerbert Xu 	}, {
338666e5bd00SJussi Kivilinna 		.alg = "ecb(fcrypt)",
338766e5bd00SJussi Kivilinna 		.test = alg_test_skcipher,
338866e5bd00SJussi Kivilinna 		.suite = {
338966e5bd00SJussi Kivilinna 			.cipher = {
339092a4c9feSEric Biggers 				.vecs = fcrypt_pcbc_tv_template,
339166e5bd00SJussi Kivilinna 				.count = 1
339266e5bd00SJussi Kivilinna 			}
339366e5bd00SJussi Kivilinna 		}
339466e5bd00SJussi Kivilinna 	}, {
3395da7f033dSHerbert Xu 		.alg = "ecb(khazad)",
33961aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
3397da7f033dSHerbert Xu 		.suite = {
339892a4c9feSEric Biggers 			.cipher = __VECS(khazad_tv_template)
3399da7f033dSHerbert Xu 		}
3400da7f033dSHerbert Xu 	}, {
340115f47ce5SGilad Ben-Yossef 		/* Same as ecb(aes) except the key is stored in
340215f47ce5SGilad Ben-Yossef 		 * hardware secure memory which we reference by index
340315f47ce5SGilad Ben-Yossef 		 */
340415f47ce5SGilad Ben-Yossef 		.alg = "ecb(paes)",
340515f47ce5SGilad Ben-Yossef 		.test = alg_test_null,
340615f47ce5SGilad Ben-Yossef 		.fips_allowed = 1,
340715f47ce5SGilad Ben-Yossef 	}, {
3408da7f033dSHerbert Xu 		.alg = "ecb(seed)",
34091aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
3410da7f033dSHerbert Xu 		.suite = {
341192a4c9feSEric Biggers 			.cipher = __VECS(seed_tv_template)
3412da7f033dSHerbert Xu 		}
3413da7f033dSHerbert Xu 	}, {
3414da7f033dSHerbert Xu 		.alg = "ecb(serpent)",
34151aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
3416da7f033dSHerbert Xu 		.suite = {
341792a4c9feSEric Biggers 			.cipher = __VECS(serpent_tv_template)
3418da7f033dSHerbert Xu 		}
3419da7f033dSHerbert Xu 	}, {
3420cd83a8a7SGilad Ben-Yossef 		.alg = "ecb(sm4)",
3421cd83a8a7SGilad Ben-Yossef 		.test = alg_test_skcipher,
3422cd83a8a7SGilad Ben-Yossef 		.suite = {
342392a4c9feSEric Biggers 			.cipher = __VECS(sm4_tv_template)
3424cd83a8a7SGilad Ben-Yossef 		}
3425cd83a8a7SGilad Ben-Yossef 	}, {
3426da7f033dSHerbert Xu 		.alg = "ecb(tea)",
34271aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
3428da7f033dSHerbert Xu 		.suite = {
342992a4c9feSEric Biggers 			.cipher = __VECS(tea_tv_template)
3430da7f033dSHerbert Xu 		}
3431da7f033dSHerbert Xu 	}, {
3432da7f033dSHerbert Xu 		.alg = "ecb(tnepres)",
34331aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
3434da7f033dSHerbert Xu 		.suite = {
343592a4c9feSEric Biggers 			.cipher = __VECS(tnepres_tv_template)
3436da7f033dSHerbert Xu 		}
3437da7f033dSHerbert Xu 	}, {
3438da7f033dSHerbert Xu 		.alg = "ecb(twofish)",
34391aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
3440da7f033dSHerbert Xu 		.suite = {
344192a4c9feSEric Biggers 			.cipher = __VECS(tf_tv_template)
3442da7f033dSHerbert Xu 		}
3443da7f033dSHerbert Xu 	}, {
3444da7f033dSHerbert Xu 		.alg = "ecb(xeta)",
34451aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
3446da7f033dSHerbert Xu 		.suite = {
344792a4c9feSEric Biggers 			.cipher = __VECS(xeta_tv_template)
3448da7f033dSHerbert Xu 		}
3449da7f033dSHerbert Xu 	}, {
3450da7f033dSHerbert Xu 		.alg = "ecb(xtea)",
34511aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
3452da7f033dSHerbert Xu 		.suite = {
345392a4c9feSEric Biggers 			.cipher = __VECS(xtea_tv_template)
3454da7f033dSHerbert Xu 		}
3455da7f033dSHerbert Xu 	}, {
34563c4b2390SSalvatore Benedetto 		.alg = "ecdh",
34573c4b2390SSalvatore Benedetto 		.test = alg_test_kpp,
34583c4b2390SSalvatore Benedetto 		.fips_allowed = 1,
34593c4b2390SSalvatore Benedetto 		.suite = {
346021c8e720SArd Biesheuvel 			.kpp = __VECS(ecdh_tv_template)
34613c4b2390SSalvatore Benedetto 		}
34623c4b2390SSalvatore Benedetto 	}, {
3463da7f033dSHerbert Xu 		.alg = "gcm(aes)",
3464da7f033dSHerbert Xu 		.test = alg_test_aead,
3465a1915d51SJarod Wilson 		.fips_allowed = 1,
3466da7f033dSHerbert Xu 		.suite = {
3467a0d608eeSEric Biggers 			.aead = __VECS(aes_gcm_tv_template)
3468da7f033dSHerbert Xu 		}
3469da7f033dSHerbert Xu 	}, {
3470507069c9SYouquan, Song 		.alg = "ghash",
3471507069c9SYouquan, Song 		.test = alg_test_hash,
347218c0ebd2SJarod Wilson 		.fips_allowed = 1,
3473507069c9SYouquan, Song 		.suite = {
347421c8e720SArd Biesheuvel 			.hash = __VECS(ghash_tv_template)
3475507069c9SYouquan, Song 		}
3476507069c9SYouquan, Song 	}, {
3477da7f033dSHerbert Xu 		.alg = "hmac(md5)",
3478da7f033dSHerbert Xu 		.test = alg_test_hash,
3479da7f033dSHerbert Xu 		.suite = {
348021c8e720SArd Biesheuvel 			.hash = __VECS(hmac_md5_tv_template)
3481da7f033dSHerbert Xu 		}
3482da7f033dSHerbert Xu 	}, {
3483da7f033dSHerbert Xu 		.alg = "hmac(rmd128)",
3484da7f033dSHerbert Xu 		.test = alg_test_hash,
3485da7f033dSHerbert Xu 		.suite = {
348621c8e720SArd Biesheuvel 			.hash = __VECS(hmac_rmd128_tv_template)
3487da7f033dSHerbert Xu 		}
3488da7f033dSHerbert Xu 	}, {
3489da7f033dSHerbert Xu 		.alg = "hmac(rmd160)",
3490da7f033dSHerbert Xu 		.test = alg_test_hash,
3491da7f033dSHerbert Xu 		.suite = {
349221c8e720SArd Biesheuvel 			.hash = __VECS(hmac_rmd160_tv_template)
3493da7f033dSHerbert Xu 		}
3494da7f033dSHerbert Xu 	}, {
3495da7f033dSHerbert Xu 		.alg = "hmac(sha1)",
3496da7f033dSHerbert Xu 		.test = alg_test_hash,
3497a1915d51SJarod Wilson 		.fips_allowed = 1,
3498da7f033dSHerbert Xu 		.suite = {
349921c8e720SArd Biesheuvel 			.hash = __VECS(hmac_sha1_tv_template)
3500da7f033dSHerbert Xu 		}
3501da7f033dSHerbert Xu 	}, {
3502da7f033dSHerbert Xu 		.alg = "hmac(sha224)",
3503da7f033dSHerbert Xu 		.test = alg_test_hash,
3504a1915d51SJarod Wilson 		.fips_allowed = 1,
3505da7f033dSHerbert Xu 		.suite = {
350621c8e720SArd Biesheuvel 			.hash = __VECS(hmac_sha224_tv_template)
3507da7f033dSHerbert Xu 		}
3508da7f033dSHerbert Xu 	}, {
3509da7f033dSHerbert Xu 		.alg = "hmac(sha256)",
3510da7f033dSHerbert Xu 		.test = alg_test_hash,
3511a1915d51SJarod Wilson 		.fips_allowed = 1,
3512da7f033dSHerbert Xu 		.suite = {
351321c8e720SArd Biesheuvel 			.hash = __VECS(hmac_sha256_tv_template)
3514da7f033dSHerbert Xu 		}
3515da7f033dSHerbert Xu 	}, {
351698eca72fSraveendra padasalagi 		.alg = "hmac(sha3-224)",
351798eca72fSraveendra padasalagi 		.test = alg_test_hash,
351898eca72fSraveendra padasalagi 		.fips_allowed = 1,
351998eca72fSraveendra padasalagi 		.suite = {
352021c8e720SArd Biesheuvel 			.hash = __VECS(hmac_sha3_224_tv_template)
352198eca72fSraveendra padasalagi 		}
352298eca72fSraveendra padasalagi 	}, {
352398eca72fSraveendra padasalagi 		.alg = "hmac(sha3-256)",
352498eca72fSraveendra padasalagi 		.test = alg_test_hash,
352598eca72fSraveendra padasalagi 		.fips_allowed = 1,
352698eca72fSraveendra padasalagi 		.suite = {
352721c8e720SArd Biesheuvel 			.hash = __VECS(hmac_sha3_256_tv_template)
352898eca72fSraveendra padasalagi 		}
352998eca72fSraveendra padasalagi 	}, {
353098eca72fSraveendra padasalagi 		.alg = "hmac(sha3-384)",
353198eca72fSraveendra padasalagi 		.test = alg_test_hash,
353298eca72fSraveendra padasalagi 		.fips_allowed = 1,
353398eca72fSraveendra padasalagi 		.suite = {
353421c8e720SArd Biesheuvel 			.hash = __VECS(hmac_sha3_384_tv_template)
353598eca72fSraveendra padasalagi 		}
353698eca72fSraveendra padasalagi 	}, {
353798eca72fSraveendra padasalagi 		.alg = "hmac(sha3-512)",
353898eca72fSraveendra padasalagi 		.test = alg_test_hash,
353998eca72fSraveendra padasalagi 		.fips_allowed = 1,
354098eca72fSraveendra padasalagi 		.suite = {
354121c8e720SArd Biesheuvel 			.hash = __VECS(hmac_sha3_512_tv_template)
354298eca72fSraveendra padasalagi 		}
354398eca72fSraveendra padasalagi 	}, {
3544da7f033dSHerbert Xu 		.alg = "hmac(sha384)",
3545da7f033dSHerbert Xu 		.test = alg_test_hash,
3546a1915d51SJarod Wilson 		.fips_allowed = 1,
3547da7f033dSHerbert Xu 		.suite = {
354821c8e720SArd Biesheuvel 			.hash = __VECS(hmac_sha384_tv_template)
3549da7f033dSHerbert Xu 		}
3550da7f033dSHerbert Xu 	}, {
3551da7f033dSHerbert Xu 		.alg = "hmac(sha512)",
3552da7f033dSHerbert Xu 		.test = alg_test_hash,
3553a1915d51SJarod Wilson 		.fips_allowed = 1,
3554da7f033dSHerbert Xu 		.suite = {
355521c8e720SArd Biesheuvel 			.hash = __VECS(hmac_sha512_tv_template)
3556da7f033dSHerbert Xu 		}
3557da7f033dSHerbert Xu 	}, {
355825a0b9d4SVitaly Chikunov 		.alg = "hmac(streebog256)",
355925a0b9d4SVitaly Chikunov 		.test = alg_test_hash,
356025a0b9d4SVitaly Chikunov 		.suite = {
356125a0b9d4SVitaly Chikunov 			.hash = __VECS(hmac_streebog256_tv_template)
356225a0b9d4SVitaly Chikunov 		}
356325a0b9d4SVitaly Chikunov 	}, {
356425a0b9d4SVitaly Chikunov 		.alg = "hmac(streebog512)",
356525a0b9d4SVitaly Chikunov 		.test = alg_test_hash,
356625a0b9d4SVitaly Chikunov 		.suite = {
356725a0b9d4SVitaly Chikunov 			.hash = __VECS(hmac_streebog512_tv_template)
356825a0b9d4SVitaly Chikunov 		}
356925a0b9d4SVitaly Chikunov 	}, {
3570bb5530e4SStephan Mueller 		.alg = "jitterentropy_rng",
3571bb5530e4SStephan Mueller 		.fips_allowed = 1,
3572bb5530e4SStephan Mueller 		.test = alg_test_null,
3573bb5530e4SStephan Mueller 	}, {
357435351988SStephan Mueller 		.alg = "kw(aes)",
357535351988SStephan Mueller 		.test = alg_test_skcipher,
357635351988SStephan Mueller 		.fips_allowed = 1,
357735351988SStephan Mueller 		.suite = {
357892a4c9feSEric Biggers 			.cipher = __VECS(aes_kw_tv_template)
357935351988SStephan Mueller 		}
358035351988SStephan Mueller 	}, {
3581da7f033dSHerbert Xu 		.alg = "lrw(aes)",
35821aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
3583da7f033dSHerbert Xu 		.suite = {
358492a4c9feSEric Biggers 			.cipher = __VECS(aes_lrw_tv_template)
3585da7f033dSHerbert Xu 		}
3586da7f033dSHerbert Xu 	}, {
35870840605eSJussi Kivilinna 		.alg = "lrw(camellia)",
35880840605eSJussi Kivilinna 		.test = alg_test_skcipher,
35890840605eSJussi Kivilinna 		.suite = {
359092a4c9feSEric Biggers 			.cipher = __VECS(camellia_lrw_tv_template)
35910840605eSJussi Kivilinna 		}
35920840605eSJussi Kivilinna 	}, {
35939b8b0405SJohannes Goetzfried 		.alg = "lrw(cast6)",
35949b8b0405SJohannes Goetzfried 		.test = alg_test_skcipher,
35959b8b0405SJohannes Goetzfried 		.suite = {
359692a4c9feSEric Biggers 			.cipher = __VECS(cast6_lrw_tv_template)
35979b8b0405SJohannes Goetzfried 		}
35989b8b0405SJohannes Goetzfried 	}, {
3599d7bfc0faSJussi Kivilinna 		.alg = "lrw(serpent)",
3600d7bfc0faSJussi Kivilinna 		.test = alg_test_skcipher,
3601d7bfc0faSJussi Kivilinna 		.suite = {
360292a4c9feSEric Biggers 			.cipher = __VECS(serpent_lrw_tv_template)
3603d7bfc0faSJussi Kivilinna 		}
3604d7bfc0faSJussi Kivilinna 	}, {
36050b2a1551SJussi Kivilinna 		.alg = "lrw(twofish)",
36060b2a1551SJussi Kivilinna 		.test = alg_test_skcipher,
36070b2a1551SJussi Kivilinna 		.suite = {
360892a4c9feSEric Biggers 			.cipher = __VECS(tf_lrw_tv_template)
36090b2a1551SJussi Kivilinna 		}
36100b2a1551SJussi Kivilinna 	}, {
36111443cc9bSKOVACS Krisztian 		.alg = "lz4",
36121443cc9bSKOVACS Krisztian 		.test = alg_test_comp,
36131443cc9bSKOVACS Krisztian 		.fips_allowed = 1,
36141443cc9bSKOVACS Krisztian 		.suite = {
36151443cc9bSKOVACS Krisztian 			.comp = {
361621c8e720SArd Biesheuvel 				.comp = __VECS(lz4_comp_tv_template),
361721c8e720SArd Biesheuvel 				.decomp = __VECS(lz4_decomp_tv_template)
36181443cc9bSKOVACS Krisztian 			}
36191443cc9bSKOVACS Krisztian 		}
36201443cc9bSKOVACS Krisztian 	}, {
36211443cc9bSKOVACS Krisztian 		.alg = "lz4hc",
36221443cc9bSKOVACS Krisztian 		.test = alg_test_comp,
36231443cc9bSKOVACS Krisztian 		.fips_allowed = 1,
36241443cc9bSKOVACS Krisztian 		.suite = {
36251443cc9bSKOVACS Krisztian 			.comp = {
362621c8e720SArd Biesheuvel 				.comp = __VECS(lz4hc_comp_tv_template),
362721c8e720SArd Biesheuvel 				.decomp = __VECS(lz4hc_decomp_tv_template)
36281443cc9bSKOVACS Krisztian 			}
36291443cc9bSKOVACS Krisztian 		}
36301443cc9bSKOVACS Krisztian 	}, {
3631da7f033dSHerbert Xu 		.alg = "lzo",
3632da7f033dSHerbert Xu 		.test = alg_test_comp,
36330818904dSMilan Broz 		.fips_allowed = 1,
3634da7f033dSHerbert Xu 		.suite = {
3635da7f033dSHerbert Xu 			.comp = {
363621c8e720SArd Biesheuvel 				.comp = __VECS(lzo_comp_tv_template),
363721c8e720SArd Biesheuvel 				.decomp = __VECS(lzo_decomp_tv_template)
3638da7f033dSHerbert Xu 			}
3639da7f033dSHerbert Xu 		}
3640da7f033dSHerbert Xu 	}, {
3641da7f033dSHerbert Xu 		.alg = "md4",
3642da7f033dSHerbert Xu 		.test = alg_test_hash,
3643da7f033dSHerbert Xu 		.suite = {
364421c8e720SArd Biesheuvel 			.hash = __VECS(md4_tv_template)
3645da7f033dSHerbert Xu 		}
3646da7f033dSHerbert Xu 	}, {
3647da7f033dSHerbert Xu 		.alg = "md5",
3648da7f033dSHerbert Xu 		.test = alg_test_hash,
3649da7f033dSHerbert Xu 		.suite = {
365021c8e720SArd Biesheuvel 			.hash = __VECS(md5_tv_template)
3651da7f033dSHerbert Xu 		}
3652da7f033dSHerbert Xu 	}, {
3653da7f033dSHerbert Xu 		.alg = "michael_mic",
3654da7f033dSHerbert Xu 		.test = alg_test_hash,
3655da7f033dSHerbert Xu 		.suite = {
365621c8e720SArd Biesheuvel 			.hash = __VECS(michael_mic_tv_template)
3657da7f033dSHerbert Xu 		}
3658da7f033dSHerbert Xu 	}, {
36594feb4c59SOndrej Mosnacek 		.alg = "morus1280",
36604feb4c59SOndrej Mosnacek 		.test = alg_test_aead,
36614feb4c59SOndrej Mosnacek 		.suite = {
3662a0d608eeSEric Biggers 			.aead = __VECS(morus1280_tv_template)
36634feb4c59SOndrej Mosnacek 		}
36644feb4c59SOndrej Mosnacek 	}, {
36654feb4c59SOndrej Mosnacek 		.alg = "morus640",
36664feb4c59SOndrej Mosnacek 		.test = alg_test_aead,
36674feb4c59SOndrej Mosnacek 		.suite = {
3668a0d608eeSEric Biggers 			.aead = __VECS(morus640_tv_template)
36694feb4c59SOndrej Mosnacek 		}
36704feb4c59SOndrej Mosnacek 	}, {
367126609a21SEric Biggers 		.alg = "nhpoly1305",
367226609a21SEric Biggers 		.test = alg_test_hash,
367326609a21SEric Biggers 		.suite = {
367426609a21SEric Biggers 			.hash = __VECS(nhpoly1305_tv_template)
367526609a21SEric Biggers 		}
367626609a21SEric Biggers 	}, {
3677ba0e14acSPuneet Saxena 		.alg = "ofb(aes)",
3678ba0e14acSPuneet Saxena 		.test = alg_test_skcipher,
3679ba0e14acSPuneet Saxena 		.fips_allowed = 1,
3680ba0e14acSPuneet Saxena 		.suite = {
368192a4c9feSEric Biggers 			.cipher = __VECS(aes_ofb_tv_template)
3682ba0e14acSPuneet Saxena 		}
3683ba0e14acSPuneet Saxena 	}, {
3684a794d8d8SGilad Ben-Yossef 		/* Same as ofb(aes) except the key is stored in
3685a794d8d8SGilad Ben-Yossef 		 * hardware secure memory which we reference by index
3686a794d8d8SGilad Ben-Yossef 		 */
3687a794d8d8SGilad Ben-Yossef 		.alg = "ofb(paes)",
3688a794d8d8SGilad Ben-Yossef 		.test = alg_test_null,
3689a794d8d8SGilad Ben-Yossef 		.fips_allowed = 1,
3690a794d8d8SGilad Ben-Yossef 	}, {
3691da7f033dSHerbert Xu 		.alg = "pcbc(fcrypt)",
36921aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
3693da7f033dSHerbert Xu 		.suite = {
369492a4c9feSEric Biggers 			.cipher = __VECS(fcrypt_pcbc_tv_template)
3695da7f033dSHerbert Xu 		}
3696da7f033dSHerbert Xu 	}, {
36971207107cSStephan Mueller 		.alg = "pkcs1pad(rsa,sha224)",
36981207107cSStephan Mueller 		.test = alg_test_null,
36991207107cSStephan Mueller 		.fips_allowed = 1,
37001207107cSStephan Mueller 	}, {
37011207107cSStephan Mueller 		.alg = "pkcs1pad(rsa,sha256)",
37021207107cSStephan Mueller 		.test = alg_test_akcipher,
37031207107cSStephan Mueller 		.fips_allowed = 1,
37041207107cSStephan Mueller 		.suite = {
37051207107cSStephan Mueller 			.akcipher = __VECS(pkcs1pad_rsa_tv_template)
37061207107cSStephan Mueller 		}
37071207107cSStephan Mueller 	}, {
37081207107cSStephan Mueller 		.alg = "pkcs1pad(rsa,sha384)",
37091207107cSStephan Mueller 		.test = alg_test_null,
37101207107cSStephan Mueller 		.fips_allowed = 1,
37111207107cSStephan Mueller 	}, {
37121207107cSStephan Mueller 		.alg = "pkcs1pad(rsa,sha512)",
37131207107cSStephan Mueller 		.test = alg_test_null,
37141207107cSStephan Mueller 		.fips_allowed = 1,
37151207107cSStephan Mueller 	}, {
3716eee9dc61SMartin Willi 		.alg = "poly1305",
3717eee9dc61SMartin Willi 		.test = alg_test_hash,
3718eee9dc61SMartin Willi 		.suite = {
371921c8e720SArd Biesheuvel 			.hash = __VECS(poly1305_tv_template)
3720eee9dc61SMartin Willi 		}
3721eee9dc61SMartin Willi 	}, {
3722da7f033dSHerbert Xu 		.alg = "rfc3686(ctr(aes))",
37231aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
3724a1915d51SJarod Wilson 		.fips_allowed = 1,
3725da7f033dSHerbert Xu 		.suite = {
372692a4c9feSEric Biggers 			.cipher = __VECS(aes_ctr_rfc3686_tv_template)
3727da7f033dSHerbert Xu 		}
3728da7f033dSHerbert Xu 	}, {
37293f31a740SHerbert Xu 		.alg = "rfc4106(gcm(aes))",
373069435b94SAdrian Hoban 		.test = alg_test_aead,
3731db71f29aSJarod Wilson 		.fips_allowed = 1,
373269435b94SAdrian Hoban 		.suite = {
3733a0d608eeSEric Biggers 			.aead = __VECS(aes_gcm_rfc4106_tv_template)
373469435b94SAdrian Hoban 		}
373569435b94SAdrian Hoban 	}, {
3736544c436aSHerbert Xu 		.alg = "rfc4309(ccm(aes))",
37375d667322SJarod Wilson 		.test = alg_test_aead,
3738a1915d51SJarod Wilson 		.fips_allowed = 1,
37395d667322SJarod Wilson 		.suite = {
3740a0d608eeSEric Biggers 			.aead = __VECS(aes_ccm_rfc4309_tv_template)
37415d667322SJarod Wilson 		}
37425d667322SJarod Wilson 	}, {
3743bb68745eSHerbert Xu 		.alg = "rfc4543(gcm(aes))",
3744e9b7441aSJussi Kivilinna 		.test = alg_test_aead,
3745e9b7441aSJussi Kivilinna 		.suite = {
3746a0d608eeSEric Biggers 			.aead = __VECS(aes_gcm_rfc4543_tv_template)
3747e9b7441aSJussi Kivilinna 		}
3748e9b7441aSJussi Kivilinna 	}, {
3749af2b76b5SMartin Willi 		.alg = "rfc7539(chacha20,poly1305)",
3750af2b76b5SMartin Willi 		.test = alg_test_aead,
3751af2b76b5SMartin Willi 		.suite = {
3752a0d608eeSEric Biggers 			.aead = __VECS(rfc7539_tv_template)
3753af2b76b5SMartin Willi 		}
3754af2b76b5SMartin Willi 	}, {
37555900758dSMartin Willi 		.alg = "rfc7539esp(chacha20,poly1305)",
37565900758dSMartin Willi 		.test = alg_test_aead,
37575900758dSMartin Willi 		.suite = {
3758a0d608eeSEric Biggers 			.aead = __VECS(rfc7539esp_tv_template)
37595900758dSMartin Willi 		}
37605900758dSMartin Willi 	}, {
3761da7f033dSHerbert Xu 		.alg = "rmd128",
3762da7f033dSHerbert Xu 		.test = alg_test_hash,
3763da7f033dSHerbert Xu 		.suite = {
376421c8e720SArd Biesheuvel 			.hash = __VECS(rmd128_tv_template)
3765da7f033dSHerbert Xu 		}
3766da7f033dSHerbert Xu 	}, {
3767da7f033dSHerbert Xu 		.alg = "rmd160",
3768da7f033dSHerbert Xu 		.test = alg_test_hash,
3769da7f033dSHerbert Xu 		.suite = {
377021c8e720SArd Biesheuvel 			.hash = __VECS(rmd160_tv_template)
3771da7f033dSHerbert Xu 		}
3772da7f033dSHerbert Xu 	}, {
3773da7f033dSHerbert Xu 		.alg = "rmd256",
3774da7f033dSHerbert Xu 		.test = alg_test_hash,
3775da7f033dSHerbert Xu 		.suite = {
377621c8e720SArd Biesheuvel 			.hash = __VECS(rmd256_tv_template)
3777da7f033dSHerbert Xu 		}
3778da7f033dSHerbert Xu 	}, {
3779da7f033dSHerbert Xu 		.alg = "rmd320",
3780da7f033dSHerbert Xu 		.test = alg_test_hash,
3781da7f033dSHerbert Xu 		.suite = {
378221c8e720SArd Biesheuvel 			.hash = __VECS(rmd320_tv_template)
3783da7f033dSHerbert Xu 		}
3784da7f033dSHerbert Xu 	}, {
3785946cc463STadeusz Struk 		.alg = "rsa",
3786946cc463STadeusz Struk 		.test = alg_test_akcipher,
3787946cc463STadeusz Struk 		.fips_allowed = 1,
3788946cc463STadeusz Struk 		.suite = {
378921c8e720SArd Biesheuvel 			.akcipher = __VECS(rsa_tv_template)
3790946cc463STadeusz Struk 		}
3791946cc463STadeusz Struk 	}, {
3792da7f033dSHerbert Xu 		.alg = "salsa20",
37931aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
3794da7f033dSHerbert Xu 		.suite = {
379592a4c9feSEric Biggers 			.cipher = __VECS(salsa20_stream_tv_template)
3796da7f033dSHerbert Xu 		}
3797da7f033dSHerbert Xu 	}, {
3798da7f033dSHerbert Xu 		.alg = "sha1",
3799da7f033dSHerbert Xu 		.test = alg_test_hash,
3800a1915d51SJarod Wilson 		.fips_allowed = 1,
3801da7f033dSHerbert Xu 		.suite = {
380221c8e720SArd Biesheuvel 			.hash = __VECS(sha1_tv_template)
3803da7f033dSHerbert Xu 		}
3804da7f033dSHerbert Xu 	}, {
3805da7f033dSHerbert Xu 		.alg = "sha224",
3806da7f033dSHerbert Xu 		.test = alg_test_hash,
3807a1915d51SJarod Wilson 		.fips_allowed = 1,
3808da7f033dSHerbert Xu 		.suite = {
380921c8e720SArd Biesheuvel 			.hash = __VECS(sha224_tv_template)
3810da7f033dSHerbert Xu 		}
3811da7f033dSHerbert Xu 	}, {
3812da7f033dSHerbert Xu 		.alg = "sha256",
3813da7f033dSHerbert Xu 		.test = alg_test_hash,
3814a1915d51SJarod Wilson 		.fips_allowed = 1,
3815da7f033dSHerbert Xu 		.suite = {
381621c8e720SArd Biesheuvel 			.hash = __VECS(sha256_tv_template)
3817da7f033dSHerbert Xu 		}
3818da7f033dSHerbert Xu 	}, {
381979cc6ab8Sraveendra padasalagi 		.alg = "sha3-224",
382079cc6ab8Sraveendra padasalagi 		.test = alg_test_hash,
382179cc6ab8Sraveendra padasalagi 		.fips_allowed = 1,
382279cc6ab8Sraveendra padasalagi 		.suite = {
382321c8e720SArd Biesheuvel 			.hash = __VECS(sha3_224_tv_template)
382479cc6ab8Sraveendra padasalagi 		}
382579cc6ab8Sraveendra padasalagi 	}, {
382679cc6ab8Sraveendra padasalagi 		.alg = "sha3-256",
382779cc6ab8Sraveendra padasalagi 		.test = alg_test_hash,
382879cc6ab8Sraveendra padasalagi 		.fips_allowed = 1,
382979cc6ab8Sraveendra padasalagi 		.suite = {
383021c8e720SArd Biesheuvel 			.hash = __VECS(sha3_256_tv_template)
383179cc6ab8Sraveendra padasalagi 		}
383279cc6ab8Sraveendra padasalagi 	}, {
383379cc6ab8Sraveendra padasalagi 		.alg = "sha3-384",
383479cc6ab8Sraveendra padasalagi 		.test = alg_test_hash,
383579cc6ab8Sraveendra padasalagi 		.fips_allowed = 1,
383679cc6ab8Sraveendra padasalagi 		.suite = {
383721c8e720SArd Biesheuvel 			.hash = __VECS(sha3_384_tv_template)
383879cc6ab8Sraveendra padasalagi 		}
383979cc6ab8Sraveendra padasalagi 	}, {
384079cc6ab8Sraveendra padasalagi 		.alg = "sha3-512",
384179cc6ab8Sraveendra padasalagi 		.test = alg_test_hash,
384279cc6ab8Sraveendra padasalagi 		.fips_allowed = 1,
384379cc6ab8Sraveendra padasalagi 		.suite = {
384421c8e720SArd Biesheuvel 			.hash = __VECS(sha3_512_tv_template)
384579cc6ab8Sraveendra padasalagi 		}
384679cc6ab8Sraveendra padasalagi 	}, {
3847da7f033dSHerbert Xu 		.alg = "sha384",
3848da7f033dSHerbert Xu 		.test = alg_test_hash,
3849a1915d51SJarod Wilson 		.fips_allowed = 1,
3850da7f033dSHerbert Xu 		.suite = {
385121c8e720SArd Biesheuvel 			.hash = __VECS(sha384_tv_template)
3852da7f033dSHerbert Xu 		}
3853da7f033dSHerbert Xu 	}, {
3854da7f033dSHerbert Xu 		.alg = "sha512",
3855da7f033dSHerbert Xu 		.test = alg_test_hash,
3856a1915d51SJarod Wilson 		.fips_allowed = 1,
3857da7f033dSHerbert Xu 		.suite = {
385821c8e720SArd Biesheuvel 			.hash = __VECS(sha512_tv_template)
3859da7f033dSHerbert Xu 		}
3860da7f033dSHerbert Xu 	}, {
3861b7e27530SGilad Ben-Yossef 		.alg = "sm3",
3862b7e27530SGilad Ben-Yossef 		.test = alg_test_hash,
3863b7e27530SGilad Ben-Yossef 		.suite = {
3864b7e27530SGilad Ben-Yossef 			.hash = __VECS(sm3_tv_template)
3865b7e27530SGilad Ben-Yossef 		}
3866b7e27530SGilad Ben-Yossef 	}, {
386725a0b9d4SVitaly Chikunov 		.alg = "streebog256",
386825a0b9d4SVitaly Chikunov 		.test = alg_test_hash,
386925a0b9d4SVitaly Chikunov 		.suite = {
387025a0b9d4SVitaly Chikunov 			.hash = __VECS(streebog256_tv_template)
387125a0b9d4SVitaly Chikunov 		}
387225a0b9d4SVitaly Chikunov 	}, {
387325a0b9d4SVitaly Chikunov 		.alg = "streebog512",
387425a0b9d4SVitaly Chikunov 		.test = alg_test_hash,
387525a0b9d4SVitaly Chikunov 		.suite = {
387625a0b9d4SVitaly Chikunov 			.hash = __VECS(streebog512_tv_template)
387725a0b9d4SVitaly Chikunov 		}
387825a0b9d4SVitaly Chikunov 	}, {
3879da7f033dSHerbert Xu 		.alg = "tgr128",
3880da7f033dSHerbert Xu 		.test = alg_test_hash,
3881da7f033dSHerbert Xu 		.suite = {
388221c8e720SArd Biesheuvel 			.hash = __VECS(tgr128_tv_template)
3883da7f033dSHerbert Xu 		}
3884da7f033dSHerbert Xu 	}, {
3885da7f033dSHerbert Xu 		.alg = "tgr160",
3886da7f033dSHerbert Xu 		.test = alg_test_hash,
3887da7f033dSHerbert Xu 		.suite = {
388821c8e720SArd Biesheuvel 			.hash = __VECS(tgr160_tv_template)
3889da7f033dSHerbert Xu 		}
3890da7f033dSHerbert Xu 	}, {
3891da7f033dSHerbert Xu 		.alg = "tgr192",
3892da7f033dSHerbert Xu 		.test = alg_test_hash,
3893da7f033dSHerbert Xu 		.suite = {
389421c8e720SArd Biesheuvel 			.hash = __VECS(tgr192_tv_template)
3895da7f033dSHerbert Xu 		}
3896da7f033dSHerbert Xu 	}, {
3897ed331adaSEric Biggers 		.alg = "vmac64(aes)",
3898ed331adaSEric Biggers 		.test = alg_test_hash,
3899ed331adaSEric Biggers 		.suite = {
3900ed331adaSEric Biggers 			.hash = __VECS(vmac64_aes_tv_template)
3901ed331adaSEric Biggers 		}
3902ed331adaSEric Biggers 	}, {
3903da7f033dSHerbert Xu 		.alg = "wp256",
3904da7f033dSHerbert Xu 		.test = alg_test_hash,
3905da7f033dSHerbert Xu 		.suite = {
390621c8e720SArd Biesheuvel 			.hash = __VECS(wp256_tv_template)
3907da7f033dSHerbert Xu 		}
3908da7f033dSHerbert Xu 	}, {
3909da7f033dSHerbert Xu 		.alg = "wp384",
3910da7f033dSHerbert Xu 		.test = alg_test_hash,
3911da7f033dSHerbert Xu 		.suite = {
391221c8e720SArd Biesheuvel 			.hash = __VECS(wp384_tv_template)
3913da7f033dSHerbert Xu 		}
3914da7f033dSHerbert Xu 	}, {
3915da7f033dSHerbert Xu 		.alg = "wp512",
3916da7f033dSHerbert Xu 		.test = alg_test_hash,
3917da7f033dSHerbert Xu 		.suite = {
391821c8e720SArd Biesheuvel 			.hash = __VECS(wp512_tv_template)
3919da7f033dSHerbert Xu 		}
3920da7f033dSHerbert Xu 	}, {
3921da7f033dSHerbert Xu 		.alg = "xcbc(aes)",
3922da7f033dSHerbert Xu 		.test = alg_test_hash,
3923da7f033dSHerbert Xu 		.suite = {
392421c8e720SArd Biesheuvel 			.hash = __VECS(aes_xcbc128_tv_template)
3925da7f033dSHerbert Xu 		}
3926da7f033dSHerbert Xu 	}, {
3927aa762409SEric Biggers 		.alg = "xchacha12",
3928aa762409SEric Biggers 		.test = alg_test_skcipher,
3929aa762409SEric Biggers 		.suite = {
3930aa762409SEric Biggers 			.cipher = __VECS(xchacha12_tv_template)
3931aa762409SEric Biggers 		},
3932aa762409SEric Biggers 	}, {
3933de61d7aeSEric Biggers 		.alg = "xchacha20",
3934de61d7aeSEric Biggers 		.test = alg_test_skcipher,
3935de61d7aeSEric Biggers 		.suite = {
3936de61d7aeSEric Biggers 			.cipher = __VECS(xchacha20_tv_template)
3937de61d7aeSEric Biggers 		},
3938de61d7aeSEric Biggers 	}, {
3939da7f033dSHerbert Xu 		.alg = "xts(aes)",
39401aa4ecd9SHerbert Xu 		.test = alg_test_skcipher,
39412918aa8dSJarod Wilson 		.fips_allowed = 1,
3942da7f033dSHerbert Xu 		.suite = {
394392a4c9feSEric Biggers 			.cipher = __VECS(aes_xts_tv_template)
3944da7f033dSHerbert Xu 		}
39450c01aed5SGeert Uytterhoeven 	}, {
39460840605eSJussi Kivilinna 		.alg = "xts(camellia)",
39470840605eSJussi Kivilinna 		.test = alg_test_skcipher,
39480840605eSJussi Kivilinna 		.suite = {
394992a4c9feSEric Biggers 			.cipher = __VECS(camellia_xts_tv_template)
39500840605eSJussi Kivilinna 		}
39510840605eSJussi Kivilinna 	}, {
39529b8b0405SJohannes Goetzfried 		.alg = "xts(cast6)",
39539b8b0405SJohannes Goetzfried 		.test = alg_test_skcipher,
39549b8b0405SJohannes Goetzfried 		.suite = {
395592a4c9feSEric Biggers 			.cipher = __VECS(cast6_xts_tv_template)
39569b8b0405SJohannes Goetzfried 		}
39579b8b0405SJohannes Goetzfried 	}, {
395815f47ce5SGilad Ben-Yossef 		/* Same as xts(aes) except the key is stored in
395915f47ce5SGilad Ben-Yossef 		 * hardware secure memory which we reference by index
396015f47ce5SGilad Ben-Yossef 		 */
396115f47ce5SGilad Ben-Yossef 		.alg = "xts(paes)",
396215f47ce5SGilad Ben-Yossef 		.test = alg_test_null,
396315f47ce5SGilad Ben-Yossef 		.fips_allowed = 1,
396415f47ce5SGilad Ben-Yossef 	}, {
396518be20b9SJussi Kivilinna 		.alg = "xts(serpent)",
396618be20b9SJussi Kivilinna 		.test = alg_test_skcipher,
396718be20b9SJussi Kivilinna 		.suite = {
396892a4c9feSEric Biggers 			.cipher = __VECS(serpent_xts_tv_template)
396918be20b9SJussi Kivilinna 		}
397018be20b9SJussi Kivilinna 	}, {
3971aed265b9SJussi Kivilinna 		.alg = "xts(twofish)",
3972aed265b9SJussi Kivilinna 		.test = alg_test_skcipher,
3973aed265b9SJussi Kivilinna 		.suite = {
397492a4c9feSEric Biggers 			.cipher = __VECS(tf_xts_tv_template)
3975aed265b9SJussi Kivilinna 		}
3976a368f43dSGiovanni Cabiddu 	}, {
397715f47ce5SGilad Ben-Yossef 		.alg = "xts4096(paes)",
397815f47ce5SGilad Ben-Yossef 		.test = alg_test_null,
397915f47ce5SGilad Ben-Yossef 		.fips_allowed = 1,
398015f47ce5SGilad Ben-Yossef 	}, {
398115f47ce5SGilad Ben-Yossef 		.alg = "xts512(paes)",
398215f47ce5SGilad Ben-Yossef 		.test = alg_test_null,
398315f47ce5SGilad Ben-Yossef 		.fips_allowed = 1,
398415f47ce5SGilad Ben-Yossef 	}, {
3985a368f43dSGiovanni Cabiddu 		.alg = "zlib-deflate",
3986a368f43dSGiovanni Cabiddu 		.test = alg_test_comp,
3987a368f43dSGiovanni Cabiddu 		.fips_allowed = 1,
3988a368f43dSGiovanni Cabiddu 		.suite = {
3989a368f43dSGiovanni Cabiddu 			.comp = {
3990a368f43dSGiovanni Cabiddu 				.comp = __VECS(zlib_deflate_comp_tv_template),
3991a368f43dSGiovanni Cabiddu 				.decomp = __VECS(zlib_deflate_decomp_tv_template)
3992a368f43dSGiovanni Cabiddu 			}
3993a368f43dSGiovanni Cabiddu 		}
3994d28fc3dbSNick Terrell 	}, {
3995d28fc3dbSNick Terrell 		.alg = "zstd",
3996d28fc3dbSNick Terrell 		.test = alg_test_comp,
3997d28fc3dbSNick Terrell 		.fips_allowed = 1,
3998d28fc3dbSNick Terrell 		.suite = {
3999d28fc3dbSNick Terrell 			.comp = {
4000d28fc3dbSNick Terrell 				.comp = __VECS(zstd_comp_tv_template),
4001d28fc3dbSNick Terrell 				.decomp = __VECS(zstd_decomp_tv_template)
4002d28fc3dbSNick Terrell 			}
4003d28fc3dbSNick Terrell 		}
4004da7f033dSHerbert Xu 	}
4005da7f033dSHerbert Xu };
4006da7f033dSHerbert Xu 
40073f47a03dSEric Biggers static void alg_check_test_descs_order(void)
40085714758bSJussi Kivilinna {
40095714758bSJussi Kivilinna 	int i;
40105714758bSJussi Kivilinna 
40115714758bSJussi Kivilinna 	for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
40125714758bSJussi Kivilinna 		int diff = strcmp(alg_test_descs[i - 1].alg,
40135714758bSJussi Kivilinna 				  alg_test_descs[i].alg);
40145714758bSJussi Kivilinna 
40155714758bSJussi Kivilinna 		if (WARN_ON(diff > 0)) {
40165714758bSJussi Kivilinna 			pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
40175714758bSJussi Kivilinna 				alg_test_descs[i - 1].alg,
40185714758bSJussi Kivilinna 				alg_test_descs[i].alg);
40195714758bSJussi Kivilinna 		}
40205714758bSJussi Kivilinna 
40215714758bSJussi Kivilinna 		if (WARN_ON(diff == 0)) {
40225714758bSJussi Kivilinna 			pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
40235714758bSJussi Kivilinna 				alg_test_descs[i].alg);
40245714758bSJussi Kivilinna 		}
40255714758bSJussi Kivilinna 	}
40265714758bSJussi Kivilinna }
40275714758bSJussi Kivilinna 
40283f47a03dSEric Biggers static void alg_check_testvec_configs(void)
40293f47a03dSEric Biggers {
40304e7babbaSEric Biggers 	int i;
40314e7babbaSEric Biggers 
40324e7babbaSEric Biggers 	for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++)
40334e7babbaSEric Biggers 		WARN_ON(!valid_testvec_config(
40344e7babbaSEric Biggers 				&default_cipher_testvec_configs[i]));
40354cc2dcf9SEric Biggers 
40364cc2dcf9SEric Biggers 	for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++)
40374cc2dcf9SEric Biggers 		WARN_ON(!valid_testvec_config(
40384cc2dcf9SEric Biggers 				&default_hash_testvec_configs[i]));
40393f47a03dSEric Biggers }
40403f47a03dSEric Biggers 
40413f47a03dSEric Biggers static void testmgr_onetime_init(void)
40423f47a03dSEric Biggers {
40433f47a03dSEric Biggers 	alg_check_test_descs_order();
40443f47a03dSEric Biggers 	alg_check_testvec_configs();
40455b2706a4SEric Biggers 
40465b2706a4SEric Biggers #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
40475b2706a4SEric Biggers 	pr_warn("alg: extra crypto tests enabled.  This is intended for developer use only.\n");
40485b2706a4SEric Biggers #endif
40493f47a03dSEric Biggers }
40503f47a03dSEric Biggers 
40511aa4ecd9SHerbert Xu static int alg_find_test(const char *alg)
4052da7f033dSHerbert Xu {
4053da7f033dSHerbert Xu 	int start = 0;
4054da7f033dSHerbert Xu 	int end = ARRAY_SIZE(alg_test_descs);
4055da7f033dSHerbert Xu 
4056da7f033dSHerbert Xu 	while (start < end) {
4057da7f033dSHerbert Xu 		int i = (start + end) / 2;
4058da7f033dSHerbert Xu 		int diff = strcmp(alg_test_descs[i].alg, alg);
4059da7f033dSHerbert Xu 
4060da7f033dSHerbert Xu 		if (diff > 0) {
4061da7f033dSHerbert Xu 			end = i;
4062da7f033dSHerbert Xu 			continue;
4063da7f033dSHerbert Xu 		}
4064da7f033dSHerbert Xu 
4065da7f033dSHerbert Xu 		if (diff < 0) {
4066da7f033dSHerbert Xu 			start = i + 1;
4067da7f033dSHerbert Xu 			continue;
4068da7f033dSHerbert Xu 		}
4069da7f033dSHerbert Xu 
40701aa4ecd9SHerbert Xu 		return i;
4071da7f033dSHerbert Xu 	}
4072da7f033dSHerbert Xu 
40731aa4ecd9SHerbert Xu 	return -1;
40741aa4ecd9SHerbert Xu }
40751aa4ecd9SHerbert Xu 
40761aa4ecd9SHerbert Xu int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
40771aa4ecd9SHerbert Xu {
40781aa4ecd9SHerbert Xu 	int i;
4079a68f6610SHerbert Xu 	int j;
4080d12d6b6dSNeil Horman 	int rc;
40811aa4ecd9SHerbert Xu 
40829e5c9fe4SRichard W.M. Jones 	if (!fips_enabled && notests) {
40839e5c9fe4SRichard W.M. Jones 		printk_once(KERN_INFO "alg: self-tests disabled\n");
40849e5c9fe4SRichard W.M. Jones 		return 0;
40859e5c9fe4SRichard W.M. Jones 	}
40869e5c9fe4SRichard W.M. Jones 
40873f47a03dSEric Biggers 	DO_ONCE(testmgr_onetime_init);
40885714758bSJussi Kivilinna 
40891aa4ecd9SHerbert Xu 	if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
40901aa4ecd9SHerbert Xu 		char nalg[CRYPTO_MAX_ALG_NAME];
40911aa4ecd9SHerbert Xu 
40921aa4ecd9SHerbert Xu 		if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
40931aa4ecd9SHerbert Xu 		    sizeof(nalg))
40941aa4ecd9SHerbert Xu 			return -ENAMETOOLONG;
40951aa4ecd9SHerbert Xu 
40961aa4ecd9SHerbert Xu 		i = alg_find_test(nalg);
40971aa4ecd9SHerbert Xu 		if (i < 0)
40981aa4ecd9SHerbert Xu 			goto notest;
40991aa4ecd9SHerbert Xu 
4100a3bef3a3SJarod Wilson 		if (fips_enabled && !alg_test_descs[i].fips_allowed)
4101a3bef3a3SJarod Wilson 			goto non_fips_alg;
4102a3bef3a3SJarod Wilson 
4103941fb328SJarod Wilson 		rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
4104941fb328SJarod Wilson 		goto test_done;
41051aa4ecd9SHerbert Xu 	}
41061aa4ecd9SHerbert Xu 
41071aa4ecd9SHerbert Xu 	i = alg_find_test(alg);
4108a68f6610SHerbert Xu 	j = alg_find_test(driver);
4109a68f6610SHerbert Xu 	if (i < 0 && j < 0)
41101aa4ecd9SHerbert Xu 		goto notest;
41111aa4ecd9SHerbert Xu 
4112a68f6610SHerbert Xu 	if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
4113a68f6610SHerbert Xu 			     (j >= 0 && !alg_test_descs[j].fips_allowed)))
4114a3bef3a3SJarod Wilson 		goto non_fips_alg;
4115a3bef3a3SJarod Wilson 
4116a68f6610SHerbert Xu 	rc = 0;
4117a68f6610SHerbert Xu 	if (i >= 0)
4118a68f6610SHerbert Xu 		rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
41191aa4ecd9SHerbert Xu 					     type, mask);
4120032c8cacSCristian Stoica 	if (j >= 0 && j != i)
4121a68f6610SHerbert Xu 		rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
4122a68f6610SHerbert Xu 					     type, mask);
4123a68f6610SHerbert Xu 
4124941fb328SJarod Wilson test_done:
4125eda69b0cSEric Biggers 	if (rc && (fips_enabled || panic_on_fail))
4126eda69b0cSEric Biggers 		panic("alg: self-tests for %s (%s) failed in %s mode!\n",
4127eda69b0cSEric Biggers 		      driver, alg, fips_enabled ? "fips" : "panic_on_fail");
4128d12d6b6dSNeil Horman 
412929ecd4abSJarod Wilson 	if (fips_enabled && !rc)
41303e8cffd4SMasanari Iida 		pr_info("alg: self-tests for %s (%s) passed\n", driver, alg);
413129ecd4abSJarod Wilson 
4132d12d6b6dSNeil Horman 	return rc;
41331aa4ecd9SHerbert Xu 
41341aa4ecd9SHerbert Xu notest:
4135da7f033dSHerbert Xu 	printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
4136da7f033dSHerbert Xu 	return 0;
4137a3bef3a3SJarod Wilson non_fips_alg:
4138a3bef3a3SJarod Wilson 	return -EINVAL;
4139da7f033dSHerbert Xu }
41400b767f96SAlexander Shishkin 
4141326a6346SHerbert Xu #endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
41420b767f96SAlexander Shishkin 
4143da7f033dSHerbert Xu EXPORT_SYMBOL_GPL(alg_test);
4144