blob: facc167f3d16af727d855488165f31b1aa683e32 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Herbert Xuda7f0332008-07-31 17:08:25 +08002/*
3 * Algorithm testing framework and tests.
4 *
5 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
6 * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
7 * Copyright (c) 2007 Nokia Siemens Networks
8 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
Eric Biggers3f47a032019-01-31 23:51:43 -08009 * Copyright (c) 2019 Google LLC
Herbert Xuda7f0332008-07-31 17:08:25 +080010 *
Adrian Hoban69435b92010-11-04 15:02:04 -040011 * Updated RFC4106 AES-GCM testing.
12 * Authors: Aidan O'Mahony (aidan.o.mahony@intel.com)
13 * Adrian Hoban <adrian.hoban@intel.com>
14 * Gabriele Paoloni <gabriele.paoloni@intel.com>
15 * Tadeusz Struk (tadeusz.struk@intel.com)
16 * Copyright (c) 2010, Intel Corporation.
Herbert Xuda7f0332008-07-31 17:08:25 +080017 */
18
Herbert Xu1ce33112015-04-22 15:06:31 +080019#include <crypto/aead.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080020#include <crypto/hash.h>
Herbert Xu12773d92015-08-20 15:21:46 +080021#include <crypto/skcipher.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080022#include <linux/err.h>
Herbert Xu1c41b882015-04-22 13:25:58 +080023#include <linux/fips.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080024#include <linux/module.h>
Eric Biggers3f47a032019-01-31 23:51:43 -080025#include <linux/once.h>
Eric Biggers25f9ddd2019-01-31 23:51:45 -080026#include <linux/random.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080027#include <linux/scatterlist.h>
28#include <linux/slab.h>
29#include <linux/string.h>
Herbert Xu0c3dc782020-08-19 21:58:20 +100030#include <linux/uio.h>
Jarod Wilson7647d6c2009-05-04 19:44:50 +080031#include <crypto/rng.h>
Stephan Mueller64d1cdf2014-05-31 17:25:36 +020032#include <crypto/drbg.h>
Tadeusz Struk946cc462015-06-16 10:31:06 -070033#include <crypto/akcipher.h>
Salvatore Benedetto802c7f12016-06-22 17:49:14 +010034#include <crypto/kpp.h>
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +010035#include <crypto/acompress.h>
Ard Biesheuvel665fe012020-12-11 13:27:15 +010036#include <crypto/internal/cipher.h>
Eric Biggersb55e1a32019-03-12 22:12:47 -070037#include <crypto/internal/simd.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080038
39#include "internal.h"
Alexander Shishkin0b767f92010-06-03 20:53:43 +100040
Ard Biesheuvel665fe012020-12-11 13:27:15 +010041MODULE_IMPORT_NS(CRYPTO_INTERNAL);
42
Richard W.M. Jones9e5c9fe2016-05-03 10:00:17 +010043static bool notests;
44module_param(notests, bool, 0644);
45MODULE_PARM_DESC(notests, "disable crypto self-tests");
46
Eric Biggerseda69b02019-03-31 13:09:14 -070047static bool panic_on_fail;
48module_param(panic_on_fail, bool, 0444);
49
Eric Biggers5b2706a2019-01-31 23:51:44 -080050#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
51static bool noextratests;
52module_param(noextratests, bool, 0644);
53MODULE_PARM_DESC(noextratests, "disable expensive crypto self-tests");
54
55static unsigned int fuzz_iterations = 100;
56module_param(fuzz_iterations, uint, 0644);
57MODULE_PARM_DESC(fuzz_iterations, "number of fuzz test iterations");
Eric Biggersb55e1a32019-03-12 22:12:47 -070058
59DEFINE_PER_CPU(bool, crypto_simd_disabled_for_test);
60EXPORT_PER_CPU_SYMBOL_GPL(crypto_simd_disabled_for_test);
Eric Biggers5b2706a2019-01-31 23:51:44 -080061#endif
62
Herbert Xu326a6342010-08-06 09:40:28 +080063#ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
Alexander Shishkin0b767f92010-06-03 20:53:43 +100064
65/* a perfect nop */
66int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
67{
68 return 0;
69}
70
71#else
72
Herbert Xuda7f0332008-07-31 17:08:25 +080073#include "testmgr.h"
74
75/*
76 * Need slab memory for testing (size in number of pages).
77 */
78#define XBUFSIZE 8
79
80/*
Herbert Xuda7f0332008-07-31 17:08:25 +080081* Used by test_cipher()
82*/
83#define ENCRYPT 1
84#define DECRYPT 0
85
Herbert Xuda7f0332008-07-31 17:08:25 +080086struct aead_test_suite {
Eric Biggersa0d608ee2019-01-13 15:32:28 -080087 const struct aead_testvec *vecs;
88 unsigned int count;
Eric Biggers49763fc2019-12-01 13:53:30 -080089
90 /*
91 * Set if trying to decrypt an inauthentic ciphertext with this
92 * algorithm might result in EINVAL rather than EBADMSG, due to other
93 * validation the algorithm does on the inputs such as length checks.
94 */
95 unsigned int einval_allowed : 1;
96
97 /*
Eric Biggers6f3a06d2020-03-04 14:44:03 -080098 * Set if this algorithm requires that the IV be located at the end of
99 * the AAD buffer, in addition to being given in the normal way. The
100 * behavior when the two IV copies differ is implementation-defined.
Eric Biggers49763fc2019-12-01 13:53:30 -0800101 */
Eric Biggers6f3a06d2020-03-04 14:44:03 -0800102 unsigned int aad_iv : 1;
Herbert Xuda7f0332008-07-31 17:08:25 +0800103};
104
105struct cipher_test_suite {
Eric Biggers92a4c9f2018-05-20 22:50:29 -0700106 const struct cipher_testvec *vecs;
107 unsigned int count;
Herbert Xuda7f0332008-07-31 17:08:25 +0800108};
109
110struct comp_test_suite {
111 struct {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800112 const struct comp_testvec *vecs;
Herbert Xuda7f0332008-07-31 17:08:25 +0800113 unsigned int count;
114 } comp, decomp;
115};
116
117struct hash_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800118 const struct hash_testvec *vecs;
Herbert Xuda7f0332008-07-31 17:08:25 +0800119 unsigned int count;
120};
121
Jarod Wilson7647d6c2009-05-04 19:44:50 +0800122struct cprng_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800123 const struct cprng_testvec *vecs;
Jarod Wilson7647d6c2009-05-04 19:44:50 +0800124 unsigned int count;
125};
126
Stephan Mueller64d1cdf2014-05-31 17:25:36 +0200127struct drbg_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800128 const struct drbg_testvec *vecs;
Stephan Mueller64d1cdf2014-05-31 17:25:36 +0200129 unsigned int count;
130};
131
Tadeusz Struk946cc462015-06-16 10:31:06 -0700132struct akcipher_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800133 const struct akcipher_testvec *vecs;
Tadeusz Struk946cc462015-06-16 10:31:06 -0700134 unsigned int count;
135};
136
Salvatore Benedetto802c7f12016-06-22 17:49:14 +0100137struct kpp_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800138 const struct kpp_testvec *vecs;
Salvatore Benedetto802c7f12016-06-22 17:49:14 +0100139 unsigned int count;
140};
141
Herbert Xuda7f0332008-07-31 17:08:25 +0800142struct alg_test_desc {
143 const char *alg;
Eric Biggersf2bb770a2019-04-11 21:57:38 -0700144 const char *generic_driver;
Herbert Xuda7f0332008-07-31 17:08:25 +0800145 int (*test)(const struct alg_test_desc *desc, const char *driver,
146 u32 type, u32 mask);
Jarod Wilsona1915d52009-05-15 15:16:03 +1000147 int fips_allowed; /* set if alg is allowed in fips mode */
Herbert Xuda7f0332008-07-31 17:08:25 +0800148
149 union {
150 struct aead_test_suite aead;
151 struct cipher_test_suite cipher;
152 struct comp_test_suite comp;
153 struct hash_test_suite hash;
Jarod Wilson7647d6c2009-05-04 19:44:50 +0800154 struct cprng_test_suite cprng;
Stephan Mueller64d1cdf2014-05-31 17:25:36 +0200155 struct drbg_test_suite drbg;
Tadeusz Struk946cc462015-06-16 10:31:06 -0700156 struct akcipher_test_suite akcipher;
Salvatore Benedetto802c7f12016-06-22 17:49:14 +0100157 struct kpp_test_suite kpp;
Herbert Xuda7f0332008-07-31 17:08:25 +0800158 } suite;
159};
160
Herbert Xuda7f0332008-07-31 17:08:25 +0800161static void hexdump(unsigned char *buf, unsigned int len)
162{
163 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
164 16, 1,
165 buf, len, false);
166}
167
Eric Biggers3f47a032019-01-31 23:51:43 -0800168static int __testmgr_alloc_buf(char *buf[XBUFSIZE], int order)
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800169{
170 int i;
171
172 for (i = 0; i < XBUFSIZE; i++) {
Eric Biggers3f47a032019-01-31 23:51:43 -0800173 buf[i] = (char *)__get_free_pages(GFP_KERNEL, order);
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800174 if (!buf[i])
175 goto err_free_buf;
176 }
177
178 return 0;
179
180err_free_buf:
181 while (i-- > 0)
Eric Biggers3f47a032019-01-31 23:51:43 -0800182 free_pages((unsigned long)buf[i], order);
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800183
184 return -ENOMEM;
185}
186
Eric Biggers3f47a032019-01-31 23:51:43 -0800187static int testmgr_alloc_buf(char *buf[XBUFSIZE])
188{
189 return __testmgr_alloc_buf(buf, 0);
190}
191
192static void __testmgr_free_buf(char *buf[XBUFSIZE], int order)
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800193{
194 int i;
195
196 for (i = 0; i < XBUFSIZE; i++)
Eric Biggers3f47a032019-01-31 23:51:43 -0800197 free_pages((unsigned long)buf[i], order);
198}
199
200static void testmgr_free_buf(char *buf[XBUFSIZE])
201{
202 __testmgr_free_buf(buf, 0);
203}
204
205#define TESTMGR_POISON_BYTE 0xfe
206#define TESTMGR_POISON_LEN 16
207
208static inline void testmgr_poison(void *addr, size_t len)
209{
210 memset(addr, TESTMGR_POISON_BYTE, len);
211}
212
213/* Is the memory region still fully poisoned? */
214static inline bool testmgr_is_poison(const void *addr, size_t len)
215{
216 return memchr_inv(addr, TESTMGR_POISON_BYTE, len) == NULL;
217}
218
219/* flush type for hash algorithms */
220enum flush_type {
221 /* merge with update of previous buffer(s) */
222 FLUSH_TYPE_NONE = 0,
223
224 /* update with previous buffer(s) before doing this one */
225 FLUSH_TYPE_FLUSH,
226
227 /* likewise, but also export and re-import the intermediate state */
228 FLUSH_TYPE_REIMPORT,
229};
230
231/* finalization function for hash algorithms */
232enum finalization_type {
233 FINALIZATION_TYPE_FINAL, /* use final() */
234 FINALIZATION_TYPE_FINUP, /* use finup() */
235 FINALIZATION_TYPE_DIGEST, /* use digest() */
236};
237
238#define TEST_SG_TOTAL 10000
239
240/**
241 * struct test_sg_division - description of a scatterlist entry
242 *
243 * This struct describes one entry of a scatterlist being constructed to check a
244 * crypto test vector.
245 *
246 * @proportion_of_total: length of this chunk relative to the total length,
247 * given as a proportion out of TEST_SG_TOTAL so that it
248 * scales to fit any test vector
249 * @offset: byte offset into a 2-page buffer at which this chunk will start
250 * @offset_relative_to_alignmask: if true, add the algorithm's alignmask to the
251 * @offset
252 * @flush_type: for hashes, whether an update() should be done now vs.
253 * continuing to accumulate data
Eric Biggers65707372019-03-12 22:12:52 -0700254 * @nosimd: if doing the pending update(), do it with SIMD disabled?
Eric Biggers3f47a032019-01-31 23:51:43 -0800255 */
256struct test_sg_division {
257 unsigned int proportion_of_total;
258 unsigned int offset;
259 bool offset_relative_to_alignmask;
260 enum flush_type flush_type;
Eric Biggers65707372019-03-12 22:12:52 -0700261 bool nosimd;
Eric Biggers3f47a032019-01-31 23:51:43 -0800262};
263
264/**
265 * struct testvec_config - configuration for testing a crypto test vector
266 *
267 * This struct describes the data layout and other parameters with which each
268 * crypto test vector can be tested.
269 *
270 * @name: name of this config, logged for debugging purposes if a test fails
271 * @inplace: operate on the data in-place, if applicable for the algorithm type?
272 * @req_flags: extra request_flags, e.g. CRYPTO_TFM_REQ_MAY_SLEEP
273 * @src_divs: description of how to arrange the source scatterlist
274 * @dst_divs: description of how to arrange the dst scatterlist, if applicable
275 * for the algorithm type. Defaults to @src_divs if unset.
276 * @iv_offset: misalignment of the IV in the range [0..MAX_ALGAPI_ALIGNMASK+1],
277 * where 0 is aligned to a 2*(MAX_ALGAPI_ALIGNMASK+1) byte boundary
278 * @iv_offset_relative_to_alignmask: if true, add the algorithm's alignmask to
279 * the @iv_offset
Eric Biggersfd8c37c2019-12-01 13:53:28 -0800280 * @key_offset: misalignment of the key, where 0 is default alignment
281 * @key_offset_relative_to_alignmask: if true, add the algorithm's alignmask to
282 * the @key_offset
Eric Biggers3f47a032019-01-31 23:51:43 -0800283 * @finalization_type: what finalization function to use for hashes
Eric Biggers65707372019-03-12 22:12:52 -0700284 * @nosimd: execute with SIMD disabled? Requires !CRYPTO_TFM_REQ_MAY_SLEEP.
Eric Biggers3f47a032019-01-31 23:51:43 -0800285 */
286struct testvec_config {
287 const char *name;
288 bool inplace;
289 u32 req_flags;
290 struct test_sg_division src_divs[XBUFSIZE];
291 struct test_sg_division dst_divs[XBUFSIZE];
292 unsigned int iv_offset;
Eric Biggersfd8c37c2019-12-01 13:53:28 -0800293 unsigned int key_offset;
Eric Biggers3f47a032019-01-31 23:51:43 -0800294 bool iv_offset_relative_to_alignmask;
Eric Biggersfd8c37c2019-12-01 13:53:28 -0800295 bool key_offset_relative_to_alignmask;
Eric Biggers3f47a032019-01-31 23:51:43 -0800296 enum finalization_type finalization_type;
Eric Biggers65707372019-03-12 22:12:52 -0700297 bool nosimd;
Eric Biggers3f47a032019-01-31 23:51:43 -0800298};
299
300#define TESTVEC_CONFIG_NAMELEN 192
301
Eric Biggers4e7babba2019-01-31 23:51:46 -0800302/*
303 * The following are the lists of testvec_configs to test for each algorithm
304 * type when the basic crypto self-tests are enabled, i.e. when
305 * CONFIG_CRYPTO_MANAGER_DISABLE_TESTS is unset. They aim to provide good test
306 * coverage, while keeping the test time much shorter than the full fuzz tests
307 * so that the basic tests can be enabled in a wider range of circumstances.
308 */
309
310/* Configs for skciphers and aeads */
311static const struct testvec_config default_cipher_testvec_configs[] = {
312 {
313 .name = "in-place",
314 .inplace = true,
315 .src_divs = { { .proportion_of_total = 10000 } },
316 }, {
317 .name = "out-of-place",
318 .src_divs = { { .proportion_of_total = 10000 } },
319 }, {
320 .name = "unaligned buffer, offset=1",
321 .src_divs = { { .proportion_of_total = 10000, .offset = 1 } },
322 .iv_offset = 1,
Eric Biggersfd8c37c2019-12-01 13:53:28 -0800323 .key_offset = 1,
Eric Biggers4e7babba2019-01-31 23:51:46 -0800324 }, {
325 .name = "buffer aligned only to alignmask",
326 .src_divs = {
327 {
328 .proportion_of_total = 10000,
329 .offset = 1,
330 .offset_relative_to_alignmask = true,
331 },
332 },
333 .iv_offset = 1,
334 .iv_offset_relative_to_alignmask = true,
Eric Biggersfd8c37c2019-12-01 13:53:28 -0800335 .key_offset = 1,
336 .key_offset_relative_to_alignmask = true,
Eric Biggers4e7babba2019-01-31 23:51:46 -0800337 }, {
338 .name = "two even aligned splits",
339 .src_divs = {
340 { .proportion_of_total = 5000 },
341 { .proportion_of_total = 5000 },
342 },
343 }, {
344 .name = "uneven misaligned splits, may sleep",
345 .req_flags = CRYPTO_TFM_REQ_MAY_SLEEP,
346 .src_divs = {
347 { .proportion_of_total = 1900, .offset = 33 },
348 { .proportion_of_total = 3300, .offset = 7 },
349 { .proportion_of_total = 4800, .offset = 18 },
350 },
351 .iv_offset = 3,
Eric Biggersfd8c37c2019-12-01 13:53:28 -0800352 .key_offset = 3,
Eric Biggers4e7babba2019-01-31 23:51:46 -0800353 }, {
354 .name = "misaligned splits crossing pages, inplace",
355 .inplace = true,
356 .src_divs = {
357 {
358 .proportion_of_total = 7500,
359 .offset = PAGE_SIZE - 32
360 }, {
361 .proportion_of_total = 2500,
362 .offset = PAGE_SIZE - 7
363 },
364 },
365 }
366};
367
Eric Biggers4cc2dcf2019-01-31 23:51:48 -0800368static const struct testvec_config default_hash_testvec_configs[] = {
369 {
370 .name = "init+update+final aligned buffer",
371 .src_divs = { { .proportion_of_total = 10000 } },
372 .finalization_type = FINALIZATION_TYPE_FINAL,
373 }, {
374 .name = "init+finup aligned buffer",
375 .src_divs = { { .proportion_of_total = 10000 } },
376 .finalization_type = FINALIZATION_TYPE_FINUP,
377 }, {
378 .name = "digest aligned buffer",
379 .src_divs = { { .proportion_of_total = 10000 } },
380 .finalization_type = FINALIZATION_TYPE_DIGEST,
381 }, {
382 .name = "init+update+final misaligned buffer",
383 .src_divs = { { .proportion_of_total = 10000, .offset = 1 } },
384 .finalization_type = FINALIZATION_TYPE_FINAL,
Eric Biggersfd8c37c2019-12-01 13:53:28 -0800385 .key_offset = 1,
Eric Biggers4cc2dcf2019-01-31 23:51:48 -0800386 }, {
387 .name = "digest buffer aligned only to alignmask",
388 .src_divs = {
389 {
390 .proportion_of_total = 10000,
391 .offset = 1,
392 .offset_relative_to_alignmask = true,
393 },
394 },
395 .finalization_type = FINALIZATION_TYPE_DIGEST,
Eric Biggersfd8c37c2019-12-01 13:53:28 -0800396 .key_offset = 1,
397 .key_offset_relative_to_alignmask = true,
Eric Biggers4cc2dcf2019-01-31 23:51:48 -0800398 }, {
399 .name = "init+update+update+final two even splits",
400 .src_divs = {
401 { .proportion_of_total = 5000 },
402 {
403 .proportion_of_total = 5000,
404 .flush_type = FLUSH_TYPE_FLUSH,
405 },
406 },
407 .finalization_type = FINALIZATION_TYPE_FINAL,
408 }, {
409 .name = "digest uneven misaligned splits, may sleep",
410 .req_flags = CRYPTO_TFM_REQ_MAY_SLEEP,
411 .src_divs = {
412 { .proportion_of_total = 1900, .offset = 33 },
413 { .proportion_of_total = 3300, .offset = 7 },
414 { .proportion_of_total = 4800, .offset = 18 },
415 },
416 .finalization_type = FINALIZATION_TYPE_DIGEST,
417 }, {
418 .name = "digest misaligned splits crossing pages",
419 .src_divs = {
420 {
421 .proportion_of_total = 7500,
422 .offset = PAGE_SIZE - 32,
423 }, {
424 .proportion_of_total = 2500,
425 .offset = PAGE_SIZE - 7,
426 },
427 },
428 .finalization_type = FINALIZATION_TYPE_DIGEST,
429 }, {
430 .name = "import/export",
431 .src_divs = {
432 {
433 .proportion_of_total = 6500,
434 .flush_type = FLUSH_TYPE_REIMPORT,
435 }, {
436 .proportion_of_total = 3500,
437 .flush_type = FLUSH_TYPE_REIMPORT,
438 },
439 },
440 .finalization_type = FINALIZATION_TYPE_FINAL,
441 }
442};
443
Eric Biggers3f47a032019-01-31 23:51:43 -0800444static unsigned int count_test_sg_divisions(const struct test_sg_division *divs)
445{
446 unsigned int remaining = TEST_SG_TOTAL;
447 unsigned int ndivs = 0;
448
449 do {
450 remaining -= divs[ndivs++].proportion_of_total;
451 } while (remaining);
452
453 return ndivs;
454}
455
Eric Biggers65707372019-03-12 22:12:52 -0700456#define SGDIVS_HAVE_FLUSHES BIT(0)
457#define SGDIVS_HAVE_NOSIMD BIT(1)
458
Eric Biggers3f47a032019-01-31 23:51:43 -0800459static bool valid_sg_divisions(const struct test_sg_division *divs,
Eric Biggers65707372019-03-12 22:12:52 -0700460 unsigned int count, int *flags_ret)
Eric Biggers3f47a032019-01-31 23:51:43 -0800461{
462 unsigned int total = 0;
463 unsigned int i;
464
465 for (i = 0; i < count && total != TEST_SG_TOTAL; i++) {
466 if (divs[i].proportion_of_total <= 0 ||
467 divs[i].proportion_of_total > TEST_SG_TOTAL - total)
468 return false;
469 total += divs[i].proportion_of_total;
470 if (divs[i].flush_type != FLUSH_TYPE_NONE)
Eric Biggers65707372019-03-12 22:12:52 -0700471 *flags_ret |= SGDIVS_HAVE_FLUSHES;
472 if (divs[i].nosimd)
473 *flags_ret |= SGDIVS_HAVE_NOSIMD;
Eric Biggers3f47a032019-01-31 23:51:43 -0800474 }
475 return total == TEST_SG_TOTAL &&
476 memchr_inv(&divs[i], 0, (count - i) * sizeof(divs[0])) == NULL;
477}
478
479/*
480 * Check whether the given testvec_config is valid. This isn't strictly needed
481 * since every testvec_config should be valid, but check anyway so that people
482 * don't unknowingly add broken configs that don't do what they wanted.
483 */
484static bool valid_testvec_config(const struct testvec_config *cfg)
485{
Eric Biggers65707372019-03-12 22:12:52 -0700486 int flags = 0;
Eric Biggers3f47a032019-01-31 23:51:43 -0800487
488 if (cfg->name == NULL)
489 return false;
490
491 if (!valid_sg_divisions(cfg->src_divs, ARRAY_SIZE(cfg->src_divs),
Eric Biggers65707372019-03-12 22:12:52 -0700492 &flags))
Eric Biggers3f47a032019-01-31 23:51:43 -0800493 return false;
494
495 if (cfg->dst_divs[0].proportion_of_total) {
496 if (!valid_sg_divisions(cfg->dst_divs,
Eric Biggers65707372019-03-12 22:12:52 -0700497 ARRAY_SIZE(cfg->dst_divs), &flags))
Eric Biggers3f47a032019-01-31 23:51:43 -0800498 return false;
499 } else {
500 if (memchr_inv(cfg->dst_divs, 0, sizeof(cfg->dst_divs)))
501 return false;
502 /* defaults to dst_divs=src_divs */
503 }
504
505 if (cfg->iv_offset +
506 (cfg->iv_offset_relative_to_alignmask ? MAX_ALGAPI_ALIGNMASK : 0) >
507 MAX_ALGAPI_ALIGNMASK + 1)
508 return false;
509
Eric Biggers65707372019-03-12 22:12:52 -0700510 if ((flags & (SGDIVS_HAVE_FLUSHES | SGDIVS_HAVE_NOSIMD)) &&
511 cfg->finalization_type == FINALIZATION_TYPE_DIGEST)
512 return false;
513
514 if ((cfg->nosimd || (flags & SGDIVS_HAVE_NOSIMD)) &&
515 (cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP))
Eric Biggers3f47a032019-01-31 23:51:43 -0800516 return false;
517
518 return true;
519}
520
521struct test_sglist {
522 char *bufs[XBUFSIZE];
523 struct scatterlist sgl[XBUFSIZE];
524 struct scatterlist sgl_saved[XBUFSIZE];
525 struct scatterlist *sgl_ptr;
526 unsigned int nents;
527};
528
529static int init_test_sglist(struct test_sglist *tsgl)
530{
531 return __testmgr_alloc_buf(tsgl->bufs, 1 /* two pages per buffer */);
532}
533
534static void destroy_test_sglist(struct test_sglist *tsgl)
535{
536 return __testmgr_free_buf(tsgl->bufs, 1 /* two pages per buffer */);
537}
538
539/**
540 * build_test_sglist() - build a scatterlist for a crypto test
541 *
542 * @tsgl: the scatterlist to build. @tsgl->bufs[] contains an array of 2-page
543 * buffers which the scatterlist @tsgl->sgl[] will be made to point into.
544 * @divs: the layout specification on which the scatterlist will be based
545 * @alignmask: the algorithm's alignmask
546 * @total_len: the total length of the scatterlist to build in bytes
547 * @data: if non-NULL, the buffers will be filled with this data until it ends.
548 * Otherwise the buffers will be poisoned. In both cases, some bytes
549 * past the end of each buffer will be poisoned to help detect overruns.
550 * @out_divs: if non-NULL, the test_sg_division to which each scatterlist entry
551 * corresponds will be returned here. This will match @divs except
552 * that divisions resolving to a length of 0 are omitted as they are
553 * not included in the scatterlist.
554 *
555 * Return: 0 or a -errno value
556 */
557static int build_test_sglist(struct test_sglist *tsgl,
558 const struct test_sg_division *divs,
559 const unsigned int alignmask,
560 const unsigned int total_len,
561 struct iov_iter *data,
562 const struct test_sg_division *out_divs[XBUFSIZE])
563{
564 struct {
565 const struct test_sg_division *div;
566 size_t length;
567 } partitions[XBUFSIZE];
568 const unsigned int ndivs = count_test_sg_divisions(divs);
569 unsigned int len_remaining = total_len;
570 unsigned int i;
571
572 BUILD_BUG_ON(ARRAY_SIZE(partitions) != ARRAY_SIZE(tsgl->sgl));
573 if (WARN_ON(ndivs > ARRAY_SIZE(partitions)))
574 return -EINVAL;
575
576 /* Calculate the (div, length) pairs */
577 tsgl->nents = 0;
578 for (i = 0; i < ndivs; i++) {
579 unsigned int len_this_sg =
580 min(len_remaining,
581 (total_len * divs[i].proportion_of_total +
582 TEST_SG_TOTAL / 2) / TEST_SG_TOTAL);
583
584 if (len_this_sg != 0) {
585 partitions[tsgl->nents].div = &divs[i];
586 partitions[tsgl->nents].length = len_this_sg;
587 tsgl->nents++;
588 len_remaining -= len_this_sg;
589 }
590 }
591 if (tsgl->nents == 0) {
592 partitions[tsgl->nents].div = &divs[0];
593 partitions[tsgl->nents].length = 0;
594 tsgl->nents++;
595 }
596 partitions[tsgl->nents - 1].length += len_remaining;
597
598 /* Set up the sgl entries and fill the data or poison */
599 sg_init_table(tsgl->sgl, tsgl->nents);
600 for (i = 0; i < tsgl->nents; i++) {
601 unsigned int offset = partitions[i].div->offset;
602 void *addr;
603
604 if (partitions[i].div->offset_relative_to_alignmask)
605 offset += alignmask;
606
607 while (offset + partitions[i].length + TESTMGR_POISON_LEN >
608 2 * PAGE_SIZE) {
609 if (WARN_ON(offset <= 0))
610 return -EINVAL;
611 offset /= 2;
612 }
613
614 addr = &tsgl->bufs[i][offset];
615 sg_set_buf(&tsgl->sgl[i], addr, partitions[i].length);
616
617 if (out_divs)
618 out_divs[i] = partitions[i].div;
619
620 if (data) {
621 size_t copy_len, copied;
622
623 copy_len = min(partitions[i].length, data->count);
624 copied = copy_from_iter(addr, copy_len, data);
625 if (WARN_ON(copied != copy_len))
626 return -EINVAL;
627 testmgr_poison(addr + copy_len, partitions[i].length +
628 TESTMGR_POISON_LEN - copy_len);
629 } else {
630 testmgr_poison(addr, partitions[i].length +
631 TESTMGR_POISON_LEN);
632 }
633 }
634
635 sg_mark_end(&tsgl->sgl[tsgl->nents - 1]);
636 tsgl->sgl_ptr = tsgl->sgl;
637 memcpy(tsgl->sgl_saved, tsgl->sgl, tsgl->nents * sizeof(tsgl->sgl[0]));
638 return 0;
639}
640
641/*
642 * Verify that a scatterlist crypto operation produced the correct output.
643 *
644 * @tsgl: scatterlist containing the actual output
645 * @expected_output: buffer containing the expected output
646 * @len_to_check: length of @expected_output in bytes
647 * @unchecked_prefix_len: number of ignored bytes in @tsgl prior to real result
648 * @check_poison: verify that the poison bytes after each chunk are intact?
649 *
650 * Return: 0 if correct, -EINVAL if incorrect, -EOVERFLOW if buffer overrun.
651 */
652static int verify_correct_output(const struct test_sglist *tsgl,
653 const char *expected_output,
654 unsigned int len_to_check,
655 unsigned int unchecked_prefix_len,
656 bool check_poison)
657{
658 unsigned int i;
659
660 for (i = 0; i < tsgl->nents; i++) {
661 struct scatterlist *sg = &tsgl->sgl_ptr[i];
662 unsigned int len = sg->length;
663 unsigned int offset = sg->offset;
664 const char *actual_output;
665
666 if (unchecked_prefix_len) {
667 if (unchecked_prefix_len >= len) {
668 unchecked_prefix_len -= len;
669 continue;
670 }
671 offset += unchecked_prefix_len;
672 len -= unchecked_prefix_len;
673 unchecked_prefix_len = 0;
674 }
675 len = min(len, len_to_check);
676 actual_output = page_address(sg_page(sg)) + offset;
677 if (memcmp(expected_output, actual_output, len) != 0)
678 return -EINVAL;
679 if (check_poison &&
680 !testmgr_is_poison(actual_output + len, TESTMGR_POISON_LEN))
681 return -EOVERFLOW;
682 len_to_check -= len;
683 expected_output += len;
684 }
685 if (WARN_ON(len_to_check != 0))
686 return -EINVAL;
687 return 0;
688}
689
690static bool is_test_sglist_corrupted(const struct test_sglist *tsgl)
691{
692 unsigned int i;
693
694 for (i = 0; i < tsgl->nents; i++) {
695 if (tsgl->sgl[i].page_link != tsgl->sgl_saved[i].page_link)
696 return true;
697 if (tsgl->sgl[i].offset != tsgl->sgl_saved[i].offset)
698 return true;
699 if (tsgl->sgl[i].length != tsgl->sgl_saved[i].length)
700 return true;
701 }
702 return false;
703}
704
705struct cipher_test_sglists {
706 struct test_sglist src;
707 struct test_sglist dst;
708};
709
710static struct cipher_test_sglists *alloc_cipher_test_sglists(void)
711{
712 struct cipher_test_sglists *tsgls;
713
714 tsgls = kmalloc(sizeof(*tsgls), GFP_KERNEL);
715 if (!tsgls)
716 return NULL;
717
718 if (init_test_sglist(&tsgls->src) != 0)
719 goto fail_kfree;
720 if (init_test_sglist(&tsgls->dst) != 0)
721 goto fail_destroy_src;
722
723 return tsgls;
724
725fail_destroy_src:
726 destroy_test_sglist(&tsgls->src);
727fail_kfree:
728 kfree(tsgls);
729 return NULL;
730}
731
732static void free_cipher_test_sglists(struct cipher_test_sglists *tsgls)
733{
734 if (tsgls) {
735 destroy_test_sglist(&tsgls->src);
736 destroy_test_sglist(&tsgls->dst);
737 kfree(tsgls);
738 }
739}
740
741/* Build the src and dst scatterlists for an skcipher or AEAD test */
742static int build_cipher_test_sglists(struct cipher_test_sglists *tsgls,
743 const struct testvec_config *cfg,
744 unsigned int alignmask,
745 unsigned int src_total_len,
746 unsigned int dst_total_len,
747 const struct kvec *inputs,
748 unsigned int nr_inputs)
749{
750 struct iov_iter input;
751 int err;
752
753 iov_iter_kvec(&input, WRITE, inputs, nr_inputs, src_total_len);
754 err = build_test_sglist(&tsgls->src, cfg->src_divs, alignmask,
755 cfg->inplace ?
756 max(dst_total_len, src_total_len) :
757 src_total_len,
758 &input, NULL);
759 if (err)
760 return err;
761
762 if (cfg->inplace) {
763 tsgls->dst.sgl_ptr = tsgls->src.sgl;
764 tsgls->dst.nents = tsgls->src.nents;
765 return 0;
766 }
767 return build_test_sglist(&tsgls->dst,
768 cfg->dst_divs[0].proportion_of_total ?
769 cfg->dst_divs : cfg->src_divs,
770 alignmask, dst_total_len, NULL, NULL);
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800771}
772
Eric Biggersfd8c37c2019-12-01 13:53:28 -0800773/*
774 * Support for testing passing a misaligned key to setkey():
775 *
776 * If cfg->key_offset is set, copy the key into a new buffer at that offset,
777 * optionally adding alignmask. Else, just use the key directly.
778 */
779static int prepare_keybuf(const u8 *key, unsigned int ksize,
780 const struct testvec_config *cfg,
781 unsigned int alignmask,
782 const u8 **keybuf_ret, const u8 **keyptr_ret)
783{
784 unsigned int key_offset = cfg->key_offset;
785 u8 *keybuf = NULL, *keyptr = (u8 *)key;
786
787 if (key_offset != 0) {
788 if (cfg->key_offset_relative_to_alignmask)
789 key_offset += alignmask;
790 keybuf = kmalloc(key_offset + ksize, GFP_KERNEL);
791 if (!keybuf)
792 return -ENOMEM;
793 keyptr = keybuf + key_offset;
794 memcpy(keyptr, key, ksize);
795 }
796 *keybuf_ret = keybuf;
797 *keyptr_ret = keyptr;
798 return 0;
799}
800
801/* Like setkey_f(tfm, key, ksize), but sometimes misalign the key */
802#define do_setkey(setkey_f, tfm, key, ksize, cfg, alignmask) \
803({ \
804 const u8 *keybuf, *keyptr; \
805 int err; \
806 \
807 err = prepare_keybuf((key), (ksize), (cfg), (alignmask), \
808 &keybuf, &keyptr); \
809 if (err == 0) { \
810 err = setkey_f((tfm), keyptr, (ksize)); \
811 kfree(keybuf); \
812 } \
813 err; \
814})
815
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800816#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
Eric Biggersf2bb770a2019-04-11 21:57:38 -0700817
818/* Generate a random length in range [0, max_len], but prefer smaller values */
819static unsigned int generate_random_length(unsigned int max_len)
820{
821 unsigned int len = prandom_u32() % (max_len + 1);
822
823 switch (prandom_u32() % 4) {
824 case 0:
825 return len % 64;
826 case 1:
827 return len % 256;
828 case 2:
829 return len % 1024;
830 default:
831 return len;
832 }
833}
834
Eric Biggers49763fc2019-12-01 13:53:30 -0800835/* Flip a random bit in the given nonempty data buffer */
836static void flip_random_bit(u8 *buf, size_t size)
837{
838 size_t bitpos;
839
840 bitpos = prandom_u32() % (size * 8);
841 buf[bitpos / 8] ^= 1 << (bitpos % 8);
842}
843
844/* Flip a random byte in the given nonempty data buffer */
845static void flip_random_byte(u8 *buf, size_t size)
846{
847 buf[prandom_u32() % size] ^= 0xff;
848}
849
850/* Sometimes make some random changes to the given nonempty data buffer */
851static void mutate_buffer(u8 *buf, size_t size)
Eric Biggersf2bb770a2019-04-11 21:57:38 -0700852{
853 size_t num_flips;
854 size_t i;
Eric Biggersf2bb770a2019-04-11 21:57:38 -0700855
856 /* Sometimes flip some bits */
857 if (prandom_u32() % 4 == 0) {
Eric Biggers49763fc2019-12-01 13:53:30 -0800858 num_flips = min_t(size_t, 1 << (prandom_u32() % 8), size * 8);
859 for (i = 0; i < num_flips; i++)
860 flip_random_bit(buf, size);
Eric Biggersf2bb770a2019-04-11 21:57:38 -0700861 }
862
863 /* Sometimes flip some bytes */
864 if (prandom_u32() % 4 == 0) {
Eric Biggers49763fc2019-12-01 13:53:30 -0800865 num_flips = min_t(size_t, 1 << (prandom_u32() % 8), size);
Eric Biggersf2bb770a2019-04-11 21:57:38 -0700866 for (i = 0; i < num_flips; i++)
Eric Biggers49763fc2019-12-01 13:53:30 -0800867 flip_random_byte(buf, size);
Eric Biggersf2bb770a2019-04-11 21:57:38 -0700868 }
869}
870
871/* Randomly generate 'count' bytes, but sometimes make them "interesting" */
872static void generate_random_bytes(u8 *buf, size_t count)
873{
874 u8 b;
875 u8 increment;
876 size_t i;
877
878 if (count == 0)
879 return;
880
881 switch (prandom_u32() % 8) { /* Choose a generation strategy */
882 case 0:
883 case 1:
884 /* All the same byte, plus optional mutations */
885 switch (prandom_u32() % 4) {
886 case 0:
887 b = 0x00;
888 break;
889 case 1:
890 b = 0xff;
891 break;
892 default:
893 b = (u8)prandom_u32();
894 break;
895 }
896 memset(buf, b, count);
897 mutate_buffer(buf, count);
898 break;
899 case 2:
900 /* Ascending or descending bytes, plus optional mutations */
901 increment = (u8)prandom_u32();
902 b = (u8)prandom_u32();
903 for (i = 0; i < count; i++, b += increment)
904 buf[i] = b;
905 mutate_buffer(buf, count);
906 break;
907 default:
908 /* Fully random bytes */
909 for (i = 0; i < count; i++)
910 buf[i] = (u8)prandom_u32();
911 }
912}
913
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800914static char *generate_random_sgl_divisions(struct test_sg_division *divs,
915 size_t max_divs, char *p, char *end,
Eric Biggers65707372019-03-12 22:12:52 -0700916 bool gen_flushes, u32 req_flags)
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800917{
918 struct test_sg_division *div = divs;
919 unsigned int remaining = TEST_SG_TOTAL;
920
921 do {
922 unsigned int this_len;
Eric Biggers65707372019-03-12 22:12:52 -0700923 const char *flushtype_str;
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800924
925 if (div == &divs[max_divs - 1] || prandom_u32() % 2 == 0)
926 this_len = remaining;
927 else
928 this_len = 1 + (prandom_u32() % remaining);
929 div->proportion_of_total = this_len;
930
931 if (prandom_u32() % 4 == 0)
932 div->offset = (PAGE_SIZE - 128) + (prandom_u32() % 128);
933 else if (prandom_u32() % 2 == 0)
934 div->offset = prandom_u32() % 32;
935 else
936 div->offset = prandom_u32() % PAGE_SIZE;
937 if (prandom_u32() % 8 == 0)
938 div->offset_relative_to_alignmask = true;
939
940 div->flush_type = FLUSH_TYPE_NONE;
941 if (gen_flushes) {
942 switch (prandom_u32() % 4) {
943 case 0:
944 div->flush_type = FLUSH_TYPE_REIMPORT;
945 break;
946 case 1:
947 div->flush_type = FLUSH_TYPE_FLUSH;
948 break;
949 }
950 }
951
Eric Biggers65707372019-03-12 22:12:52 -0700952 if (div->flush_type != FLUSH_TYPE_NONE &&
953 !(req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) &&
954 prandom_u32() % 2 == 0)
955 div->nosimd = true;
956
957 switch (div->flush_type) {
958 case FLUSH_TYPE_FLUSH:
959 if (div->nosimd)
960 flushtype_str = "<flush,nosimd>";
961 else
962 flushtype_str = "<flush>";
963 break;
964 case FLUSH_TYPE_REIMPORT:
965 if (div->nosimd)
966 flushtype_str = "<reimport,nosimd>";
967 else
968 flushtype_str = "<reimport>";
969 break;
970 default:
971 flushtype_str = "";
972 break;
973 }
974
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800975 BUILD_BUG_ON(TEST_SG_TOTAL != 10000); /* for "%u.%u%%" */
Eric Biggers65707372019-03-12 22:12:52 -0700976 p += scnprintf(p, end - p, "%s%u.%u%%@%s+%u%s", flushtype_str,
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800977 this_len / 100, this_len % 100,
978 div->offset_relative_to_alignmask ?
979 "alignmask" : "",
980 div->offset, this_len == remaining ? "" : ", ");
981 remaining -= this_len;
982 div++;
983 } while (remaining);
984
985 return p;
986}
987
988/* Generate a random testvec_config for fuzz testing */
989static void generate_random_testvec_config(struct testvec_config *cfg,
990 char *name, size_t max_namelen)
991{
992 char *p = name;
993 char * const end = name + max_namelen;
994
995 memset(cfg, 0, sizeof(*cfg));
996
997 cfg->name = name;
998
999 p += scnprintf(p, end - p, "random:");
1000
1001 if (prandom_u32() % 2 == 0) {
1002 cfg->inplace = true;
1003 p += scnprintf(p, end - p, " inplace");
1004 }
1005
1006 if (prandom_u32() % 2 == 0) {
1007 cfg->req_flags |= CRYPTO_TFM_REQ_MAY_SLEEP;
1008 p += scnprintf(p, end - p, " may_sleep");
1009 }
1010
1011 switch (prandom_u32() % 4) {
1012 case 0:
1013 cfg->finalization_type = FINALIZATION_TYPE_FINAL;
1014 p += scnprintf(p, end - p, " use_final");
1015 break;
1016 case 1:
1017 cfg->finalization_type = FINALIZATION_TYPE_FINUP;
1018 p += scnprintf(p, end - p, " use_finup");
1019 break;
1020 default:
1021 cfg->finalization_type = FINALIZATION_TYPE_DIGEST;
1022 p += scnprintf(p, end - p, " use_digest");
1023 break;
1024 }
1025
Eric Biggers65707372019-03-12 22:12:52 -07001026 if (!(cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) &&
1027 prandom_u32() % 2 == 0) {
1028 cfg->nosimd = true;
1029 p += scnprintf(p, end - p, " nosimd");
1030 }
1031
Eric Biggers25f9ddd2019-01-31 23:51:45 -08001032 p += scnprintf(p, end - p, " src_divs=[");
1033 p = generate_random_sgl_divisions(cfg->src_divs,
1034 ARRAY_SIZE(cfg->src_divs), p, end,
1035 (cfg->finalization_type !=
Eric Biggers65707372019-03-12 22:12:52 -07001036 FINALIZATION_TYPE_DIGEST),
1037 cfg->req_flags);
Eric Biggers25f9ddd2019-01-31 23:51:45 -08001038 p += scnprintf(p, end - p, "]");
1039
1040 if (!cfg->inplace && prandom_u32() % 2 == 0) {
1041 p += scnprintf(p, end - p, " dst_divs=[");
1042 p = generate_random_sgl_divisions(cfg->dst_divs,
1043 ARRAY_SIZE(cfg->dst_divs),
Eric Biggers65707372019-03-12 22:12:52 -07001044 p, end, false,
1045 cfg->req_flags);
Eric Biggers25f9ddd2019-01-31 23:51:45 -08001046 p += scnprintf(p, end - p, "]");
1047 }
1048
1049 if (prandom_u32() % 2 == 0) {
1050 cfg->iv_offset = 1 + (prandom_u32() % MAX_ALGAPI_ALIGNMASK);
1051 p += scnprintf(p, end - p, " iv_offset=%u", cfg->iv_offset);
1052 }
1053
Eric Biggersfd8c37c2019-12-01 13:53:28 -08001054 if (prandom_u32() % 2 == 0) {
1055 cfg->key_offset = 1 + (prandom_u32() % MAX_ALGAPI_ALIGNMASK);
1056 p += scnprintf(p, end - p, " key_offset=%u", cfg->key_offset);
1057 }
1058
Eric Biggers25f9ddd2019-01-31 23:51:45 -08001059 WARN_ON_ONCE(!valid_testvec_config(cfg));
1060}
Eric Biggersb55e1a32019-03-12 22:12:47 -07001061
1062static void crypto_disable_simd_for_test(void)
1063{
1064 preempt_disable();
1065 __this_cpu_write(crypto_simd_disabled_for_test, true);
1066}
1067
1068static void crypto_reenable_simd_for_test(void)
1069{
1070 __this_cpu_write(crypto_simd_disabled_for_test, false);
1071 preempt_enable();
1072}
Eric Biggersf2bb770a2019-04-11 21:57:38 -07001073
1074/*
1075 * Given an algorithm name, build the name of the generic implementation of that
1076 * algorithm, assuming the usual naming convention. Specifically, this appends
1077 * "-generic" to every part of the name that is not a template name. Examples:
1078 *
1079 * aes => aes-generic
1080 * cbc(aes) => cbc(aes-generic)
1081 * cts(cbc(aes)) => cts(cbc(aes-generic))
1082 * rfc7539(chacha20,poly1305) => rfc7539(chacha20-generic,poly1305-generic)
1083 *
1084 * Return: 0 on success, or -ENAMETOOLONG if the generic name would be too long
1085 */
1086static int build_generic_driver_name(const char *algname,
1087 char driver_name[CRYPTO_MAX_ALG_NAME])
1088{
1089 const char *in = algname;
1090 char *out = driver_name;
1091 size_t len = strlen(algname);
1092
1093 if (len >= CRYPTO_MAX_ALG_NAME)
1094 goto too_long;
1095 do {
1096 const char *in_saved = in;
1097
1098 while (*in && *in != '(' && *in != ')' && *in != ',')
1099 *out++ = *in++;
1100 if (*in != '(' && in > in_saved) {
1101 len += 8;
1102 if (len >= CRYPTO_MAX_ALG_NAME)
1103 goto too_long;
1104 memcpy(out, "-generic", 8);
1105 out += 8;
1106 }
1107 } while ((*out++ = *in++) != '\0');
1108 return 0;
1109
1110too_long:
1111 pr_err("alg: generic driver name for \"%s\" would be too long\n",
1112 algname);
1113 return -ENAMETOOLONG;
1114}
Eric Biggersb55e1a32019-03-12 22:12:47 -07001115#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1116static void crypto_disable_simd_for_test(void)
1117{
1118}
1119
1120static void crypto_reenable_simd_for_test(void)
1121{
1122}
1123#endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
Eric Biggers25f9ddd2019-01-31 23:51:45 -08001124
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001125static int build_hash_sglist(struct test_sglist *tsgl,
1126 const struct hash_testvec *vec,
1127 const struct testvec_config *cfg,
1128 unsigned int alignmask,
1129 const struct test_sg_division *divs[XBUFSIZE])
1130{
1131 struct kvec kv;
1132 struct iov_iter input;
1133
1134 kv.iov_base = (void *)vec->plaintext;
1135 kv.iov_len = vec->psize;
1136 iov_iter_kvec(&input, WRITE, &kv, 1, vec->psize);
1137 return build_test_sglist(tsgl, cfg->src_divs, alignmask, vec->psize,
1138 &input, divs);
1139}
1140
1141static int check_hash_result(const char *type,
1142 const u8 *result, unsigned int digestsize,
1143 const struct hash_testvec *vec,
1144 const char *vec_name,
1145 const char *driver,
1146 const struct testvec_config *cfg)
1147{
1148 if (memcmp(result, vec->digest, digestsize) != 0) {
1149 pr_err("alg: %s: %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
1150 type, driver, vec_name, cfg->name);
1151 return -EINVAL;
1152 }
1153 if (!testmgr_is_poison(&result[digestsize], TESTMGR_POISON_LEN)) {
1154 pr_err("alg: %s: %s overran result buffer on test vector %s, cfg=\"%s\"\n",
1155 type, driver, vec_name, cfg->name);
1156 return -EOVERFLOW;
1157 }
1158 return 0;
1159}
1160
1161static inline int check_shash_op(const char *op, int err,
1162 const char *driver, const char *vec_name,
1163 const struct testvec_config *cfg)
1164{
1165 if (err)
1166 pr_err("alg: shash: %s %s() failed with err %d on test vector %s, cfg=\"%s\"\n",
1167 driver, op, err, vec_name, cfg->name);
1168 return err;
1169}
1170
1171static inline const void *sg_data(struct scatterlist *sg)
1172{
1173 return page_address(sg_page(sg)) + sg->offset;
1174}
1175
1176/* Test one hash test vector in one configuration, using the shash API */
1177static int test_shash_vec_cfg(const char *driver,
1178 const struct hash_testvec *vec,
1179 const char *vec_name,
1180 const struct testvec_config *cfg,
1181 struct shash_desc *desc,
1182 struct test_sglist *tsgl,
1183 u8 *hashstate)
1184{
1185 struct crypto_shash *tfm = desc->tfm;
1186 const unsigned int alignmask = crypto_shash_alignmask(tfm);
1187 const unsigned int digestsize = crypto_shash_digestsize(tfm);
1188 const unsigned int statesize = crypto_shash_statesize(tfm);
1189 const struct test_sg_division *divs[XBUFSIZE];
1190 unsigned int i;
1191 u8 result[HASH_MAX_DIGESTSIZE + TESTMGR_POISON_LEN];
1192 int err;
1193
1194 /* Set the key, if specified */
1195 if (vec->ksize) {
Eric Biggersfd8c37c2019-12-01 13:53:28 -08001196 err = do_setkey(crypto_shash_setkey, tfm, vec->key, vec->ksize,
1197 cfg, alignmask);
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001198 if (err) {
1199 if (err == vec->setkey_error)
1200 return 0;
1201 pr_err("alg: shash: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1202 driver, vec_name, vec->setkey_error, err,
1203 crypto_shash_get_flags(tfm));
1204 return err;
1205 }
1206 if (vec->setkey_error) {
1207 pr_err("alg: shash: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1208 driver, vec_name, vec->setkey_error);
1209 return -EINVAL;
1210 }
1211 }
1212
1213 /* Build the scatterlist for the source data */
1214 err = build_hash_sglist(tsgl, vec, cfg, alignmask, divs);
1215 if (err) {
1216 pr_err("alg: shash: %s: error preparing scatterlist for test vector %s, cfg=\"%s\"\n",
1217 driver, vec_name, cfg->name);
1218 return err;
1219 }
1220
1221 /* Do the actual hashing */
1222
1223 testmgr_poison(desc->__ctx, crypto_shash_descsize(tfm));
1224 testmgr_poison(result, digestsize + TESTMGR_POISON_LEN);
1225
1226 if (cfg->finalization_type == FINALIZATION_TYPE_DIGEST ||
1227 vec->digest_error) {
1228 /* Just using digest() */
1229 if (tsgl->nents != 1)
1230 return 0;
1231 if (cfg->nosimd)
1232 crypto_disable_simd_for_test();
1233 err = crypto_shash_digest(desc, sg_data(&tsgl->sgl[0]),
1234 tsgl->sgl[0].length, result);
1235 if (cfg->nosimd)
1236 crypto_reenable_simd_for_test();
1237 if (err) {
1238 if (err == vec->digest_error)
1239 return 0;
1240 pr_err("alg: shash: %s digest() failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
1241 driver, vec_name, vec->digest_error, err,
1242 cfg->name);
1243 return err;
1244 }
1245 if (vec->digest_error) {
1246 pr_err("alg: shash: %s digest() unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
1247 driver, vec_name, vec->digest_error, cfg->name);
1248 return -EINVAL;
1249 }
1250 goto result_ready;
1251 }
1252
1253 /* Using init(), zero or more update(), then final() or finup() */
1254
1255 if (cfg->nosimd)
1256 crypto_disable_simd_for_test();
1257 err = crypto_shash_init(desc);
1258 if (cfg->nosimd)
1259 crypto_reenable_simd_for_test();
1260 err = check_shash_op("init", err, driver, vec_name, cfg);
1261 if (err)
1262 return err;
1263
1264 for (i = 0; i < tsgl->nents; i++) {
1265 if (i + 1 == tsgl->nents &&
1266 cfg->finalization_type == FINALIZATION_TYPE_FINUP) {
1267 if (divs[i]->nosimd)
1268 crypto_disable_simd_for_test();
1269 err = crypto_shash_finup(desc, sg_data(&tsgl->sgl[i]),
1270 tsgl->sgl[i].length, result);
1271 if (divs[i]->nosimd)
1272 crypto_reenable_simd_for_test();
1273 err = check_shash_op("finup", err, driver, vec_name,
1274 cfg);
1275 if (err)
1276 return err;
1277 goto result_ready;
1278 }
1279 if (divs[i]->nosimd)
1280 crypto_disable_simd_for_test();
1281 err = crypto_shash_update(desc, sg_data(&tsgl->sgl[i]),
1282 tsgl->sgl[i].length);
1283 if (divs[i]->nosimd)
1284 crypto_reenable_simd_for_test();
1285 err = check_shash_op("update", err, driver, vec_name, cfg);
1286 if (err)
1287 return err;
1288 if (divs[i]->flush_type == FLUSH_TYPE_REIMPORT) {
1289 /* Test ->export() and ->import() */
1290 testmgr_poison(hashstate + statesize,
1291 TESTMGR_POISON_LEN);
1292 err = crypto_shash_export(desc, hashstate);
1293 err = check_shash_op("export", err, driver, vec_name,
1294 cfg);
1295 if (err)
1296 return err;
1297 if (!testmgr_is_poison(hashstate + statesize,
1298 TESTMGR_POISON_LEN)) {
1299 pr_err("alg: shash: %s export() overran state buffer on test vector %s, cfg=\"%s\"\n",
1300 driver, vec_name, cfg->name);
1301 return -EOVERFLOW;
1302 }
1303 testmgr_poison(desc->__ctx, crypto_shash_descsize(tfm));
1304 err = crypto_shash_import(desc, hashstate);
1305 err = check_shash_op("import", err, driver, vec_name,
1306 cfg);
1307 if (err)
1308 return err;
1309 }
1310 }
1311
1312 if (cfg->nosimd)
1313 crypto_disable_simd_for_test();
1314 err = crypto_shash_final(desc, result);
1315 if (cfg->nosimd)
1316 crypto_reenable_simd_for_test();
1317 err = check_shash_op("final", err, driver, vec_name, cfg);
1318 if (err)
1319 return err;
1320result_ready:
1321 return check_hash_result("shash", result, digestsize, vec, vec_name,
1322 driver, cfg);
1323}
1324
Eric Biggers65707372019-03-12 22:12:52 -07001325static int do_ahash_op(int (*op)(struct ahash_request *req),
1326 struct ahash_request *req,
1327 struct crypto_wait *wait, bool nosimd)
1328{
1329 int err;
1330
1331 if (nosimd)
1332 crypto_disable_simd_for_test();
1333
1334 err = op(req);
1335
1336 if (nosimd)
1337 crypto_reenable_simd_for_test();
1338
1339 return crypto_wait_req(err, wait);
1340}
1341
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001342static int check_nonfinal_ahash_op(const char *op, int err,
1343 u8 *result, unsigned int digestsize,
1344 const char *driver, const char *vec_name,
1345 const struct testvec_config *cfg)
Kamil Konieczny466d7b92018-01-16 15:26:13 +01001346{
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001347 if (err) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001348 pr_err("alg: ahash: %s %s() failed with err %d on test vector %s, cfg=\"%s\"\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001349 driver, op, err, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001350 return err;
1351 }
1352 if (!testmgr_is_poison(result, digestsize)) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001353 pr_err("alg: ahash: %s %s() used result buffer on test vector %s, cfg=\"%s\"\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001354 driver, op, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001355 return -EINVAL;
1356 }
1357 return 0;
1358}
Kamil Konieczny466d7b92018-01-16 15:26:13 +01001359
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001360/* Test one hash test vector in one configuration, using the ahash API */
1361static int test_ahash_vec_cfg(const char *driver,
1362 const struct hash_testvec *vec,
1363 const char *vec_name,
1364 const struct testvec_config *cfg,
1365 struct ahash_request *req,
1366 struct test_sglist *tsgl,
1367 u8 *hashstate)
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001368{
1369 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1370 const unsigned int alignmask = crypto_ahash_alignmask(tfm);
1371 const unsigned int digestsize = crypto_ahash_digestsize(tfm);
1372 const unsigned int statesize = crypto_ahash_statesize(tfm);
1373 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1374 const struct test_sg_division *divs[XBUFSIZE];
1375 DECLARE_CRYPTO_WAIT(wait);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001376 unsigned int i;
1377 struct scatterlist *pending_sgl;
1378 unsigned int pending_len;
1379 u8 result[HASH_MAX_DIGESTSIZE + TESTMGR_POISON_LEN];
1380 int err;
1381
1382 /* Set the key, if specified */
1383 if (vec->ksize) {
Eric Biggersfd8c37c2019-12-01 13:53:28 -08001384 err = do_setkey(crypto_ahash_setkey, tfm, vec->key, vec->ksize,
1385 cfg, alignmask);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001386 if (err) {
Eric Biggers5283a8e2019-04-11 21:57:36 -07001387 if (err == vec->setkey_error)
1388 return 0;
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001389 pr_err("alg: ahash: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001390 driver, vec_name, vec->setkey_error, err,
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001391 crypto_ahash_get_flags(tfm));
1392 return err;
1393 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07001394 if (vec->setkey_error) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001395 pr_err("alg: ahash: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001396 driver, vec_name, vec->setkey_error);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001397 return -EINVAL;
1398 }
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001399 }
1400
1401 /* Build the scatterlist for the source data */
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001402 err = build_hash_sglist(tsgl, vec, cfg, alignmask, divs);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001403 if (err) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001404 pr_err("alg: ahash: %s: error preparing scatterlist for test vector %s, cfg=\"%s\"\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001405 driver, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001406 return err;
1407 }
1408
1409 /* Do the actual hashing */
1410
1411 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
1412 testmgr_poison(result, digestsize + TESTMGR_POISON_LEN);
1413
Eric Biggers5283a8e2019-04-11 21:57:36 -07001414 if (cfg->finalization_type == FINALIZATION_TYPE_DIGEST ||
1415 vec->digest_error) {
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001416 /* Just using digest() */
1417 ahash_request_set_callback(req, req_flags, crypto_req_done,
1418 &wait);
1419 ahash_request_set_crypt(req, tsgl->sgl, result, vec->psize);
Eric Biggers65707372019-03-12 22:12:52 -07001420 err = do_ahash_op(crypto_ahash_digest, req, &wait, cfg->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001421 if (err) {
Eric Biggers5283a8e2019-04-11 21:57:36 -07001422 if (err == vec->digest_error)
1423 return 0;
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001424 pr_err("alg: ahash: %s digest() failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001425 driver, vec_name, vec->digest_error, err,
Eric Biggers5283a8e2019-04-11 21:57:36 -07001426 cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001427 return err;
1428 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07001429 if (vec->digest_error) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001430 pr_err("alg: ahash: %s digest() unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001431 driver, vec_name, vec->digest_error, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001432 return -EINVAL;
1433 }
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001434 goto result_ready;
1435 }
1436
1437 /* Using init(), zero or more update(), then final() or finup() */
1438
1439 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
1440 ahash_request_set_crypt(req, NULL, result, 0);
Eric Biggers65707372019-03-12 22:12:52 -07001441 err = do_ahash_op(crypto_ahash_init, req, &wait, cfg->nosimd);
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001442 err = check_nonfinal_ahash_op("init", err, result, digestsize,
1443 driver, vec_name, cfg);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001444 if (err)
1445 return err;
1446
1447 pending_sgl = NULL;
1448 pending_len = 0;
1449 for (i = 0; i < tsgl->nents; i++) {
1450 if (divs[i]->flush_type != FLUSH_TYPE_NONE &&
1451 pending_sgl != NULL) {
1452 /* update() with the pending data */
1453 ahash_request_set_callback(req, req_flags,
1454 crypto_req_done, &wait);
1455 ahash_request_set_crypt(req, pending_sgl, result,
1456 pending_len);
Eric Biggers65707372019-03-12 22:12:52 -07001457 err = do_ahash_op(crypto_ahash_update, req, &wait,
1458 divs[i]->nosimd);
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001459 err = check_nonfinal_ahash_op("update", err,
1460 result, digestsize,
1461 driver, vec_name, cfg);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001462 if (err)
1463 return err;
1464 pending_sgl = NULL;
1465 pending_len = 0;
1466 }
1467 if (divs[i]->flush_type == FLUSH_TYPE_REIMPORT) {
1468 /* Test ->export() and ->import() */
1469 testmgr_poison(hashstate + statesize,
1470 TESTMGR_POISON_LEN);
1471 err = crypto_ahash_export(req, hashstate);
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001472 err = check_nonfinal_ahash_op("export", err,
1473 result, digestsize,
1474 driver, vec_name, cfg);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001475 if (err)
1476 return err;
1477 if (!testmgr_is_poison(hashstate + statesize,
1478 TESTMGR_POISON_LEN)) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001479 pr_err("alg: ahash: %s export() overran state buffer on test vector %s, cfg=\"%s\"\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001480 driver, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001481 return -EOVERFLOW;
1482 }
1483
1484 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
1485 err = crypto_ahash_import(req, hashstate);
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001486 err = check_nonfinal_ahash_op("import", err,
1487 result, digestsize,
1488 driver, vec_name, cfg);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001489 if (err)
1490 return err;
1491 }
1492 if (pending_sgl == NULL)
1493 pending_sgl = &tsgl->sgl[i];
1494 pending_len += tsgl->sgl[i].length;
1495 }
1496
1497 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
1498 ahash_request_set_crypt(req, pending_sgl, result, pending_len);
1499 if (cfg->finalization_type == FINALIZATION_TYPE_FINAL) {
1500 /* finish with update() and final() */
Eric Biggers65707372019-03-12 22:12:52 -07001501 err = do_ahash_op(crypto_ahash_update, req, &wait, cfg->nosimd);
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001502 err = check_nonfinal_ahash_op("update", err, result, digestsize,
1503 driver, vec_name, cfg);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001504 if (err)
1505 return err;
Eric Biggers65707372019-03-12 22:12:52 -07001506 err = do_ahash_op(crypto_ahash_final, req, &wait, cfg->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001507 if (err) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001508 pr_err("alg: ahash: %s final() failed with err %d on test vector %s, cfg=\"%s\"\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001509 driver, err, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001510 return err;
1511 }
1512 } else {
1513 /* finish with finup() */
Eric Biggers65707372019-03-12 22:12:52 -07001514 err = do_ahash_op(crypto_ahash_finup, req, &wait, cfg->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001515 if (err) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001516 pr_err("alg: ahash: %s finup() failed with err %d on test vector %s, cfg=\"%s\"\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001517 driver, err, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001518 return err;
1519 }
1520 }
1521
1522result_ready:
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001523 return check_hash_result("ahash", result, digestsize, vec, vec_name,
1524 driver, cfg);
1525}
1526
1527static int test_hash_vec_cfg(const char *driver,
1528 const struct hash_testvec *vec,
1529 const char *vec_name,
1530 const struct testvec_config *cfg,
1531 struct ahash_request *req,
1532 struct shash_desc *desc,
1533 struct test_sglist *tsgl,
1534 u8 *hashstate)
1535{
1536 int err;
1537
1538 /*
1539 * For algorithms implemented as "shash", most bugs will be detected by
1540 * both the shash and ahash tests. Test the shash API first so that the
1541 * failures involve less indirection, so are easier to debug.
1542 */
1543
1544 if (desc) {
1545 err = test_shash_vec_cfg(driver, vec, vec_name, cfg, desc, tsgl,
1546 hashstate);
1547 if (err)
1548 return err;
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001549 }
1550
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001551 return test_ahash_vec_cfg(driver, vec, vec_name, cfg, req, tsgl,
1552 hashstate);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001553}
1554
1555static int test_hash_vec(const char *driver, const struct hash_testvec *vec,
1556 unsigned int vec_num, struct ahash_request *req,
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001557 struct shash_desc *desc, struct test_sglist *tsgl,
1558 u8 *hashstate)
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001559{
Eric Biggers951d1332019-04-11 21:57:37 -07001560 char vec_name[16];
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001561 unsigned int i;
1562 int err;
1563
Eric Biggers951d1332019-04-11 21:57:37 -07001564 sprintf(vec_name, "%u", vec_num);
1565
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001566 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++) {
Eric Biggers951d1332019-04-11 21:57:37 -07001567 err = test_hash_vec_cfg(driver, vec, vec_name,
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001568 &default_hash_testvec_configs[i],
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001569 req, desc, tsgl, hashstate);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001570 if (err)
1571 return err;
1572 }
1573
1574#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1575 if (!noextratests) {
1576 struct testvec_config cfg;
1577 char cfgname[TESTVEC_CONFIG_NAMELEN];
1578
1579 for (i = 0; i < fuzz_iterations; i++) {
1580 generate_random_testvec_config(&cfg, cfgname,
1581 sizeof(cfgname));
Eric Biggers951d1332019-04-11 21:57:37 -07001582 err = test_hash_vec_cfg(driver, vec, vec_name, &cfg,
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001583 req, desc, tsgl, hashstate);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001584 if (err)
1585 return err;
Eric Biggerse63e1b02019-06-02 22:42:33 -07001586 cond_resched();
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001587 }
1588 }
1589#endif
1590 return 0;
1591}
1592
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001593#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1594/*
1595 * Generate a hash test vector from the given implementation.
1596 * Assumes the buffers in 'vec' were already allocated.
1597 */
Arnd Bergmann149c4e62019-06-18 11:21:53 +02001598static void generate_random_hash_testvec(struct shash_desc *desc,
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001599 struct hash_testvec *vec,
1600 unsigned int maxkeysize,
1601 unsigned int maxdatasize,
1602 char *name, size_t max_namelen)
1603{
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001604 /* Data */
1605 vec->psize = generate_random_length(maxdatasize);
1606 generate_random_bytes((u8 *)vec->plaintext, vec->psize);
1607
1608 /*
1609 * Key: length in range [1, maxkeysize], but usually choose maxkeysize.
1610 * If algorithm is unkeyed, then maxkeysize == 0 and set ksize = 0.
1611 */
1612 vec->setkey_error = 0;
1613 vec->ksize = 0;
1614 if (maxkeysize) {
1615 vec->ksize = maxkeysize;
1616 if (prandom_u32() % 4 == 0)
1617 vec->ksize = 1 + (prandom_u32() % maxkeysize);
1618 generate_random_bytes((u8 *)vec->key, vec->ksize);
1619
Arnd Bergmann149c4e62019-06-18 11:21:53 +02001620 vec->setkey_error = crypto_shash_setkey(desc->tfm, vec->key,
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001621 vec->ksize);
1622 /* If the key couldn't be set, no need to continue to digest. */
1623 if (vec->setkey_error)
1624 goto done;
1625 }
1626
1627 /* Digest */
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001628 vec->digest_error = crypto_shash_digest(desc, vec->plaintext,
1629 vec->psize, (u8 *)vec->digest);
1630done:
1631 snprintf(name, max_namelen, "\"random: psize=%u ksize=%u\"",
1632 vec->psize, vec->ksize);
1633}
1634
1635/*
1636 * Test the hash algorithm represented by @req against the corresponding generic
1637 * implementation, if one is available.
1638 */
1639static int test_hash_vs_generic_impl(const char *driver,
1640 const char *generic_driver,
1641 unsigned int maxkeysize,
1642 struct ahash_request *req,
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001643 struct shash_desc *desc,
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001644 struct test_sglist *tsgl,
1645 u8 *hashstate)
1646{
1647 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1648 const unsigned int digestsize = crypto_ahash_digestsize(tfm);
1649 const unsigned int blocksize = crypto_ahash_blocksize(tfm);
1650 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
1651 const char *algname = crypto_hash_alg_common(tfm)->base.cra_name;
1652 char _generic_driver[CRYPTO_MAX_ALG_NAME];
1653 struct crypto_shash *generic_tfm = NULL;
Arnd Bergmann149c4e62019-06-18 11:21:53 +02001654 struct shash_desc *generic_desc = NULL;
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001655 unsigned int i;
1656 struct hash_testvec vec = { 0 };
1657 char vec_name[64];
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02001658 struct testvec_config *cfg;
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001659 char cfgname[TESTVEC_CONFIG_NAMELEN];
1660 int err;
1661
1662 if (noextratests)
1663 return 0;
1664
1665 if (!generic_driver) { /* Use default naming convention? */
1666 err = build_generic_driver_name(algname, _generic_driver);
1667 if (err)
1668 return err;
1669 generic_driver = _generic_driver;
1670 }
1671
1672 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
1673 return 0;
1674
1675 generic_tfm = crypto_alloc_shash(generic_driver, 0, 0);
1676 if (IS_ERR(generic_tfm)) {
1677 err = PTR_ERR(generic_tfm);
1678 if (err == -ENOENT) {
1679 pr_warn("alg: hash: skipping comparison tests for %s because %s is unavailable\n",
1680 driver, generic_driver);
1681 return 0;
1682 }
1683 pr_err("alg: hash: error allocating %s (generic impl of %s): %d\n",
1684 generic_driver, algname, err);
1685 return err;
1686 }
1687
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02001688 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1689 if (!cfg) {
1690 err = -ENOMEM;
1691 goto out;
1692 }
1693
Arnd Bergmann149c4e62019-06-18 11:21:53 +02001694 generic_desc = kzalloc(sizeof(*desc) +
1695 crypto_shash_descsize(generic_tfm), GFP_KERNEL);
1696 if (!generic_desc) {
1697 err = -ENOMEM;
1698 goto out;
1699 }
1700 generic_desc->tfm = generic_tfm;
1701
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001702 /* Check the algorithm properties for consistency. */
1703
1704 if (digestsize != crypto_shash_digestsize(generic_tfm)) {
1705 pr_err("alg: hash: digestsize for %s (%u) doesn't match generic impl (%u)\n",
1706 driver, digestsize,
1707 crypto_shash_digestsize(generic_tfm));
1708 err = -EINVAL;
1709 goto out;
1710 }
1711
1712 if (blocksize != crypto_shash_blocksize(generic_tfm)) {
1713 pr_err("alg: hash: blocksize for %s (%u) doesn't match generic impl (%u)\n",
1714 driver, blocksize, crypto_shash_blocksize(generic_tfm));
1715 err = -EINVAL;
1716 goto out;
1717 }
1718
1719 /*
1720 * Now generate test vectors using the generic implementation, and test
1721 * the other implementation against them.
1722 */
1723
1724 vec.key = kmalloc(maxkeysize, GFP_KERNEL);
1725 vec.plaintext = kmalloc(maxdatasize, GFP_KERNEL);
1726 vec.digest = kmalloc(digestsize, GFP_KERNEL);
1727 if (!vec.key || !vec.plaintext || !vec.digest) {
1728 err = -ENOMEM;
1729 goto out;
1730 }
1731
1732 for (i = 0; i < fuzz_iterations * 8; i++) {
Arnd Bergmann149c4e62019-06-18 11:21:53 +02001733 generate_random_hash_testvec(generic_desc, &vec,
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001734 maxkeysize, maxdatasize,
1735 vec_name, sizeof(vec_name));
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02001736 generate_random_testvec_config(cfg, cfgname, sizeof(cfgname));
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001737
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02001738 err = test_hash_vec_cfg(driver, &vec, vec_name, cfg,
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001739 req, desc, tsgl, hashstate);
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001740 if (err)
1741 goto out;
1742 cond_resched();
1743 }
1744 err = 0;
1745out:
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02001746 kfree(cfg);
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001747 kfree(vec.key);
1748 kfree(vec.plaintext);
1749 kfree(vec.digest);
1750 crypto_free_shash(generic_tfm);
Waiman Long453431a2020-08-06 23:18:13 -07001751 kfree_sensitive(generic_desc);
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001752 return err;
1753}
1754#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1755static int test_hash_vs_generic_impl(const char *driver,
1756 const char *generic_driver,
1757 unsigned int maxkeysize,
1758 struct ahash_request *req,
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001759 struct shash_desc *desc,
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001760 struct test_sglist *tsgl,
1761 u8 *hashstate)
1762{
1763 return 0;
1764}
1765#endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1766
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001767static int alloc_shash(const char *driver, u32 type, u32 mask,
1768 struct crypto_shash **tfm_ret,
1769 struct shash_desc **desc_ret)
1770{
1771 struct crypto_shash *tfm;
1772 struct shash_desc *desc;
1773
1774 tfm = crypto_alloc_shash(driver, type, mask);
1775 if (IS_ERR(tfm)) {
1776 if (PTR_ERR(tfm) == -ENOENT) {
1777 /*
1778 * This algorithm is only available through the ahash
1779 * API, not the shash API, so skip the shash tests.
1780 */
1781 return 0;
1782 }
1783 pr_err("alg: hash: failed to allocate shash transform for %s: %ld\n",
1784 driver, PTR_ERR(tfm));
1785 return PTR_ERR(tfm);
1786 }
1787
1788 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL);
1789 if (!desc) {
1790 crypto_free_shash(tfm);
1791 return -ENOMEM;
1792 }
1793 desc->tfm = tfm;
1794
1795 *tfm_ret = tfm;
1796 *desc_ret = desc;
1797 return 0;
1798}
1799
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001800static int __alg_test_hash(const struct hash_testvec *vecs,
1801 unsigned int num_vecs, const char *driver,
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001802 u32 type, u32 mask,
1803 const char *generic_driver, unsigned int maxkeysize)
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001804{
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001805 struct crypto_ahash *atfm = NULL;
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001806 struct ahash_request *req = NULL;
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001807 struct crypto_shash *stfm = NULL;
1808 struct shash_desc *desc = NULL;
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001809 struct test_sglist *tsgl = NULL;
1810 u8 *hashstate = NULL;
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001811 unsigned int statesize;
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001812 unsigned int i;
1813 int err;
1814
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001815 /*
1816 * Always test the ahash API. This works regardless of whether the
1817 * algorithm is implemented as ahash or shash.
1818 */
1819
1820 atfm = crypto_alloc_ahash(driver, type, mask);
1821 if (IS_ERR(atfm)) {
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001822 pr_err("alg: hash: failed to allocate transform for %s: %ld\n",
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001823 driver, PTR_ERR(atfm));
1824 return PTR_ERR(atfm);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001825 }
1826
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001827 req = ahash_request_alloc(atfm, GFP_KERNEL);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001828 if (!req) {
1829 pr_err("alg: hash: failed to allocate request for %s\n",
1830 driver);
1831 err = -ENOMEM;
1832 goto out;
1833 }
1834
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001835 /*
1836 * If available also test the shash API, to cover corner cases that may
1837 * be missed by testing the ahash API only.
1838 */
1839 err = alloc_shash(driver, type, mask, &stfm, &desc);
1840 if (err)
1841 goto out;
1842
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001843 tsgl = kmalloc(sizeof(*tsgl), GFP_KERNEL);
1844 if (!tsgl || init_test_sglist(tsgl) != 0) {
1845 pr_err("alg: hash: failed to allocate test buffers for %s\n",
1846 driver);
1847 kfree(tsgl);
1848 tsgl = NULL;
1849 err = -ENOMEM;
1850 goto out;
1851 }
1852
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001853 statesize = crypto_ahash_statesize(atfm);
1854 if (stfm)
1855 statesize = max(statesize, crypto_shash_statesize(stfm));
1856 hashstate = kmalloc(statesize + TESTMGR_POISON_LEN, GFP_KERNEL);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001857 if (!hashstate) {
1858 pr_err("alg: hash: failed to allocate hash state buffer for %s\n",
1859 driver);
1860 err = -ENOMEM;
1861 goto out;
1862 }
1863
1864 for (i = 0; i < num_vecs; i++) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001865 err = test_hash_vec(driver, &vecs[i], i, req, desc, tsgl,
1866 hashstate);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001867 if (err)
1868 goto out;
Eric Biggerse63e1b02019-06-02 22:42:33 -07001869 cond_resched();
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001870 }
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001871 err = test_hash_vs_generic_impl(driver, generic_driver, maxkeysize, req,
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001872 desc, tsgl, hashstate);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001873out:
1874 kfree(hashstate);
1875 if (tsgl) {
1876 destroy_test_sglist(tsgl);
1877 kfree(tsgl);
1878 }
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001879 kfree(desc);
1880 crypto_free_shash(stfm);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001881 ahash_request_free(req);
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001882 crypto_free_ahash(atfm);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001883 return err;
1884}
1885
1886static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1887 u32 type, u32 mask)
1888{
1889 const struct hash_testvec *template = desc->suite.hash.vecs;
1890 unsigned int tcount = desc->suite.hash.count;
1891 unsigned int nr_unkeyed, nr_keyed;
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001892 unsigned int maxkeysize = 0;
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001893 int err;
1894
1895 /*
1896 * For OPTIONAL_KEY algorithms, we have to do all the unkeyed tests
1897 * first, before setting a key on the tfm. To make this easier, we
1898 * require that the unkeyed test vectors (if any) are listed first.
1899 */
1900
1901 for (nr_unkeyed = 0; nr_unkeyed < tcount; nr_unkeyed++) {
1902 if (template[nr_unkeyed].ksize)
1903 break;
1904 }
1905 for (nr_keyed = 0; nr_unkeyed + nr_keyed < tcount; nr_keyed++) {
1906 if (!template[nr_unkeyed + nr_keyed].ksize) {
1907 pr_err("alg: hash: test vectors for %s out of order, "
1908 "unkeyed ones must come first\n", desc->alg);
Kamil Konieczny466d7b92018-01-16 15:26:13 +01001909 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08001910 }
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001911 maxkeysize = max_t(unsigned int, maxkeysize,
1912 template[nr_unkeyed + nr_keyed].ksize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001913 }
1914
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001915 err = 0;
1916 if (nr_unkeyed) {
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001917 err = __alg_test_hash(template, nr_unkeyed, driver, type, mask,
1918 desc->generic_driver, maxkeysize);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001919 template += nr_unkeyed;
Herbert Xuda7f0332008-07-31 17:08:25 +08001920 }
1921
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001922 if (!err && nr_keyed)
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001923 err = __alg_test_hash(template, nr_keyed, driver, type, mask,
1924 desc->generic_driver, maxkeysize);
Wang, Rui Y018ba952016-02-03 18:26:57 +08001925
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001926 return err;
Jussi Kivilinnada5ffe12013-06-13 17:37:55 +03001927}
1928
Eric Biggersed968042019-01-31 23:51:47 -08001929static int test_aead_vec_cfg(const char *driver, int enc,
1930 const struct aead_testvec *vec,
Eric Biggers951d1332019-04-11 21:57:37 -07001931 const char *vec_name,
Eric Biggersed968042019-01-31 23:51:47 -08001932 const struct testvec_config *cfg,
1933 struct aead_request *req,
1934 struct cipher_test_sglists *tsgls)
Herbert Xuda7f0332008-07-31 17:08:25 +08001935{
Eric Biggersed968042019-01-31 23:51:47 -08001936 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1937 const unsigned int alignmask = crypto_aead_alignmask(tfm);
1938 const unsigned int ivsize = crypto_aead_ivsize(tfm);
1939 const unsigned int authsize = vec->clen - vec->plen;
1940 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1941 const char *op = enc ? "encryption" : "decryption";
1942 DECLARE_CRYPTO_WAIT(wait);
1943 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
1944 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
1945 cfg->iv_offset +
1946 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
1947 struct kvec input[2];
1948 int err;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001949
Eric Biggersed968042019-01-31 23:51:47 -08001950 /* Set the key */
1951 if (vec->wk)
1952 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001953 else
Eric Biggersed968042019-01-31 23:51:47 -08001954 crypto_aead_clear_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Eric Biggersfd8c37c2019-12-01 13:53:28 -08001955
1956 err = do_setkey(crypto_aead_setkey, tfm, vec->key, vec->klen,
1957 cfg, alignmask);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001958 if (err && err != vec->setkey_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001959 pr_err("alg: aead: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1960 driver, vec_name, vec->setkey_error, err,
Eric Biggers5283a8e2019-04-11 21:57:36 -07001961 crypto_aead_get_flags(tfm));
Eric Biggersed968042019-01-31 23:51:47 -08001962 return err;
1963 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07001964 if (!err && vec->setkey_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001965 pr_err("alg: aead: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1966 driver, vec_name, vec->setkey_error);
Eric Biggersed968042019-01-31 23:51:47 -08001967 return -EINVAL;
1968 }
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001969
Eric Biggersed968042019-01-31 23:51:47 -08001970 /* Set the authentication tag size */
1971 err = crypto_aead_setauthsize(tfm, authsize);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001972 if (err && err != vec->setauthsize_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001973 pr_err("alg: aead: %s setauthsize failed on test vector %s; expected_error=%d, actual_error=%d\n",
1974 driver, vec_name, vec->setauthsize_error, err);
Eric Biggersed968042019-01-31 23:51:47 -08001975 return err;
1976 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07001977 if (!err && vec->setauthsize_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001978 pr_err("alg: aead: %s setauthsize unexpectedly succeeded on test vector %s; expected_error=%d\n",
1979 driver, vec_name, vec->setauthsize_error);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001980 return -EINVAL;
1981 }
1982
1983 if (vec->setkey_error || vec->setauthsize_error)
1984 return 0;
Eric Biggersed968042019-01-31 23:51:47 -08001985
1986 /* The IV must be copied to a buffer, as the algorithm may modify it */
1987 if (WARN_ON(ivsize > MAX_IVLEN))
1988 return -EINVAL;
1989 if (vec->iv)
1990 memcpy(iv, vec->iv, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001991 else
Eric Biggersed968042019-01-31 23:51:47 -08001992 memset(iv, 0, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001993
Eric Biggersed968042019-01-31 23:51:47 -08001994 /* Build the src/dst scatterlists */
1995 input[0].iov_base = (void *)vec->assoc;
1996 input[0].iov_len = vec->alen;
1997 input[1].iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
1998 input[1].iov_len = enc ? vec->plen : vec->clen;
1999 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
2000 vec->alen + (enc ? vec->plen :
2001 vec->clen),
2002 vec->alen + (enc ? vec->clen :
2003 vec->plen),
2004 input, 2);
2005 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07002006 pr_err("alg: aead: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
2007 driver, op, vec_name, cfg->name);
Eric Biggersed968042019-01-31 23:51:47 -08002008 return err;
Herbert Xuda7f0332008-07-31 17:08:25 +08002009 }
2010
Eric Biggersed968042019-01-31 23:51:47 -08002011 /* Do the actual encryption or decryption */
2012 testmgr_poison(req->__ctx, crypto_aead_reqsize(tfm));
2013 aead_request_set_callback(req, req_flags, crypto_req_done, &wait);
2014 aead_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
2015 enc ? vec->plen : vec->clen, iv);
2016 aead_request_set_ad(req, vec->alen);
Eric Biggers65707372019-03-12 22:12:52 -07002017 if (cfg->nosimd)
2018 crypto_disable_simd_for_test();
2019 err = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
2020 if (cfg->nosimd)
2021 crypto_reenable_simd_for_test();
2022 err = crypto_wait_req(err, &wait);
Herbert Xuda7f0332008-07-31 17:08:25 +08002023
Eric Biggersa6e5ef92019-01-31 23:51:50 -08002024 /* Check that the algorithm didn't overwrite things it shouldn't have */
2025 if (req->cryptlen != (enc ? vec->plen : vec->clen) ||
2026 req->assoclen != vec->alen ||
2027 req->iv != iv ||
2028 req->src != tsgls->src.sgl_ptr ||
2029 req->dst != tsgls->dst.sgl_ptr ||
2030 crypto_aead_reqtfm(req) != tfm ||
2031 req->base.complete != crypto_req_done ||
2032 req->base.flags != req_flags ||
2033 req->base.data != &wait) {
Eric Biggers951d1332019-04-11 21:57:37 -07002034 pr_err("alg: aead: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
2035 driver, op, vec_name, cfg->name);
Eric Biggersa6e5ef92019-01-31 23:51:50 -08002036 if (req->cryptlen != (enc ? vec->plen : vec->clen))
2037 pr_err("alg: aead: changed 'req->cryptlen'\n");
2038 if (req->assoclen != vec->alen)
2039 pr_err("alg: aead: changed 'req->assoclen'\n");
2040 if (req->iv != iv)
2041 pr_err("alg: aead: changed 'req->iv'\n");
2042 if (req->src != tsgls->src.sgl_ptr)
2043 pr_err("alg: aead: changed 'req->src'\n");
2044 if (req->dst != tsgls->dst.sgl_ptr)
2045 pr_err("alg: aead: changed 'req->dst'\n");
2046 if (crypto_aead_reqtfm(req) != tfm)
2047 pr_err("alg: aead: changed 'req->base.tfm'\n");
2048 if (req->base.complete != crypto_req_done)
2049 pr_err("alg: aead: changed 'req->base.complete'\n");
2050 if (req->base.flags != req_flags)
2051 pr_err("alg: aead: changed 'req->base.flags'\n");
2052 if (req->base.data != &wait)
2053 pr_err("alg: aead: changed 'req->base.data'\n");
2054 return -EINVAL;
2055 }
2056 if (is_test_sglist_corrupted(&tsgls->src)) {
Eric Biggers951d1332019-04-11 21:57:37 -07002057 pr_err("alg: aead: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
2058 driver, op, vec_name, cfg->name);
Eric Biggersa6e5ef92019-01-31 23:51:50 -08002059 return -EINVAL;
2060 }
2061 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
2062 is_test_sglist_corrupted(&tsgls->dst)) {
Eric Biggers951d1332019-04-11 21:57:37 -07002063 pr_err("alg: aead: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
2064 driver, op, vec_name, cfg->name);
Eric Biggersa6e5ef92019-01-31 23:51:50 -08002065 return -EINVAL;
2066 }
2067
Eric Biggers49763fc2019-12-01 13:53:30 -08002068 /* Check for unexpected success or failure, or wrong error code */
2069 if ((err == 0 && vec->novrfy) ||
2070 (err != vec->crypt_error && !(err == -EBADMSG && vec->novrfy))) {
2071 char expected_error[32];
2072
2073 if (vec->novrfy &&
2074 vec->crypt_error != 0 && vec->crypt_error != -EBADMSG)
2075 sprintf(expected_error, "-EBADMSG or %d",
2076 vec->crypt_error);
2077 else if (vec->novrfy)
2078 sprintf(expected_error, "-EBADMSG");
2079 else
2080 sprintf(expected_error, "%d", vec->crypt_error);
2081 if (err) {
2082 pr_err("alg: aead: %s %s failed on test vector %s; expected_error=%s, actual_error=%d, cfg=\"%s\"\n",
2083 driver, op, vec_name, expected_error, err,
2084 cfg->name);
2085 return err;
2086 }
2087 pr_err("alg: aead: %s %s unexpectedly succeeded on test vector %s; expected_error=%s, cfg=\"%s\"\n",
Eric Biggers951d1332019-04-11 21:57:37 -07002088 driver, op, vec_name, expected_error, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07002089 return -EINVAL;
2090 }
Eric Biggers49763fc2019-12-01 13:53:30 -08002091 if (err) /* Expectedly failed. */
2092 return 0;
Eric Biggers5283a8e2019-04-11 21:57:36 -07002093
Eric Biggersed968042019-01-31 23:51:47 -08002094 /* Check for the correct output (ciphertext or plaintext) */
2095 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
2096 enc ? vec->clen : vec->plen,
2097 vec->alen, enc || !cfg->inplace);
2098 if (err == -EOVERFLOW) {
Eric Biggers951d1332019-04-11 21:57:37 -07002099 pr_err("alg: aead: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
2100 driver, op, vec_name, cfg->name);
Eric Biggersed968042019-01-31 23:51:47 -08002101 return err;
Herbert Xuda7f0332008-07-31 17:08:25 +08002102 }
Eric Biggersed968042019-01-31 23:51:47 -08002103 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07002104 pr_err("alg: aead: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
2105 driver, op, vec_name, cfg->name);
Eric Biggersed968042019-01-31 23:51:47 -08002106 return err;
Jussi Kivilinna58dcf542013-06-13 17:37:50 +03002107 }
2108
2109 return 0;
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03002110}
2111
Eric Biggersed968042019-01-31 23:51:47 -08002112static int test_aead_vec(const char *driver, int enc,
2113 const struct aead_testvec *vec, unsigned int vec_num,
2114 struct aead_request *req,
2115 struct cipher_test_sglists *tsgls)
2116{
Eric Biggers951d1332019-04-11 21:57:37 -07002117 char vec_name[16];
Eric Biggersed968042019-01-31 23:51:47 -08002118 unsigned int i;
2119 int err;
2120
2121 if (enc && vec->novrfy)
2122 return 0;
2123
Eric Biggers951d1332019-04-11 21:57:37 -07002124 sprintf(vec_name, "%u", vec_num);
2125
Eric Biggersed968042019-01-31 23:51:47 -08002126 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
Eric Biggers951d1332019-04-11 21:57:37 -07002127 err = test_aead_vec_cfg(driver, enc, vec, vec_name,
Eric Biggersed968042019-01-31 23:51:47 -08002128 &default_cipher_testvec_configs[i],
2129 req, tsgls);
2130 if (err)
2131 return err;
2132 }
2133
2134#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2135 if (!noextratests) {
2136 struct testvec_config cfg;
2137 char cfgname[TESTVEC_CONFIG_NAMELEN];
2138
2139 for (i = 0; i < fuzz_iterations; i++) {
2140 generate_random_testvec_config(&cfg, cfgname,
2141 sizeof(cfgname));
Eric Biggers951d1332019-04-11 21:57:37 -07002142 err = test_aead_vec_cfg(driver, enc, vec, vec_name,
Eric Biggersed968042019-01-31 23:51:47 -08002143 &cfg, req, tsgls);
2144 if (err)
2145 return err;
Eric Biggerse63e1b02019-06-02 22:42:33 -07002146 cond_resched();
Eric Biggersed968042019-01-31 23:51:47 -08002147 }
2148 }
2149#endif
2150 return 0;
2151}
2152
Eric Biggers40153b12019-04-11 21:57:41 -07002153#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
Eric Biggers2ea91502019-12-01 13:53:29 -08002154
2155struct aead_extra_tests_ctx {
2156 struct aead_request *req;
2157 struct crypto_aead *tfm;
2158 const char *driver;
2159 const struct alg_test_desc *test_desc;
2160 struct cipher_test_sglists *tsgls;
2161 unsigned int maxdatasize;
2162 unsigned int maxkeysize;
2163
2164 struct aead_testvec vec;
2165 char vec_name[64];
2166 char cfgname[TESTVEC_CONFIG_NAMELEN];
2167 struct testvec_config cfg;
2168};
2169
Eric Biggers40153b12019-04-11 21:57:41 -07002170/*
Eric Biggers49763fc2019-12-01 13:53:30 -08002171 * Make at least one random change to a (ciphertext, AAD) pair. "Ciphertext"
2172 * here means the full ciphertext including the authentication tag. The
2173 * authentication tag (and hence also the ciphertext) is assumed to be nonempty.
2174 */
Eric Biggers6f3a06d2020-03-04 14:44:03 -08002175static void mutate_aead_message(struct aead_testvec *vec, bool aad_iv,
2176 unsigned int ivsize)
Eric Biggers49763fc2019-12-01 13:53:30 -08002177{
Eric Biggers6f3a06d2020-03-04 14:44:03 -08002178 const unsigned int aad_tail_size = aad_iv ? ivsize : 0;
Eric Biggers49763fc2019-12-01 13:53:30 -08002179 const unsigned int authsize = vec->clen - vec->plen;
2180
2181 if (prandom_u32() % 2 == 0 && vec->alen > aad_tail_size) {
2182 /* Mutate the AAD */
2183 flip_random_bit((u8 *)vec->assoc, vec->alen - aad_tail_size);
2184 if (prandom_u32() % 2 == 0)
2185 return;
2186 }
2187 if (prandom_u32() % 2 == 0) {
2188 /* Mutate auth tag (assuming it's at the end of ciphertext) */
2189 flip_random_bit((u8 *)vec->ctext + vec->plen, authsize);
2190 } else {
2191 /* Mutate any part of the ciphertext */
2192 flip_random_bit((u8 *)vec->ctext, vec->clen);
2193 }
2194}
2195
2196/*
2197 * Minimum authentication tag size in bytes at which we assume that we can
2198 * reliably generate inauthentic messages, i.e. not generate an authentic
2199 * message by chance.
2200 */
2201#define MIN_COLLISION_FREE_AUTHSIZE 8
2202
2203static void generate_aead_message(struct aead_request *req,
2204 const struct aead_test_suite *suite,
2205 struct aead_testvec *vec,
2206 bool prefer_inauthentic)
2207{
2208 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2209 const unsigned int ivsize = crypto_aead_ivsize(tfm);
2210 const unsigned int authsize = vec->clen - vec->plen;
2211 const bool inauthentic = (authsize >= MIN_COLLISION_FREE_AUTHSIZE) &&
2212 (prefer_inauthentic || prandom_u32() % 4 == 0);
2213
2214 /* Generate the AAD. */
2215 generate_random_bytes((u8 *)vec->assoc, vec->alen);
Eric Biggers6f3a06d2020-03-04 14:44:03 -08002216 if (suite->aad_iv && vec->alen >= ivsize)
2217 /* Avoid implementation-defined behavior. */
2218 memcpy((u8 *)vec->assoc + vec->alen - ivsize, vec->iv, ivsize);
Eric Biggers49763fc2019-12-01 13:53:30 -08002219
2220 if (inauthentic && prandom_u32() % 2 == 0) {
2221 /* Generate a random ciphertext. */
2222 generate_random_bytes((u8 *)vec->ctext, vec->clen);
2223 } else {
2224 int i = 0;
2225 struct scatterlist src[2], dst;
2226 u8 iv[MAX_IVLEN];
2227 DECLARE_CRYPTO_WAIT(wait);
2228
2229 /* Generate a random plaintext and encrypt it. */
2230 sg_init_table(src, 2);
2231 if (vec->alen)
2232 sg_set_buf(&src[i++], vec->assoc, vec->alen);
2233 if (vec->plen) {
2234 generate_random_bytes((u8 *)vec->ptext, vec->plen);
2235 sg_set_buf(&src[i++], vec->ptext, vec->plen);
2236 }
2237 sg_init_one(&dst, vec->ctext, vec->alen + vec->clen);
2238 memcpy(iv, vec->iv, ivsize);
2239 aead_request_set_callback(req, 0, crypto_req_done, &wait);
2240 aead_request_set_crypt(req, src, &dst, vec->plen, iv);
2241 aead_request_set_ad(req, vec->alen);
2242 vec->crypt_error = crypto_wait_req(crypto_aead_encrypt(req),
2243 &wait);
2244 /* If encryption failed, we're done. */
2245 if (vec->crypt_error != 0)
2246 return;
2247 memmove((u8 *)vec->ctext, vec->ctext + vec->alen, vec->clen);
2248 if (!inauthentic)
2249 return;
2250 /*
2251 * Mutate the authentic (ciphertext, AAD) pair to get an
2252 * inauthentic one.
2253 */
Eric Biggers6f3a06d2020-03-04 14:44:03 -08002254 mutate_aead_message(vec, suite->aad_iv, ivsize);
Eric Biggers49763fc2019-12-01 13:53:30 -08002255 }
2256 vec->novrfy = 1;
2257 if (suite->einval_allowed)
2258 vec->crypt_error = -EINVAL;
2259}
2260
2261/*
2262 * Generate an AEAD test vector 'vec' using the implementation specified by
2263 * 'req'. The buffers in 'vec' must already be allocated.
2264 *
2265 * If 'prefer_inauthentic' is true, then this function will generate inauthentic
2266 * test vectors (i.e. vectors with 'vec->novrfy=1') more often.
Eric Biggers40153b12019-04-11 21:57:41 -07002267 */
2268static void generate_random_aead_testvec(struct aead_request *req,
2269 struct aead_testvec *vec,
Eric Biggers49763fc2019-12-01 13:53:30 -08002270 const struct aead_test_suite *suite,
Eric Biggers40153b12019-04-11 21:57:41 -07002271 unsigned int maxkeysize,
2272 unsigned int maxdatasize,
Eric Biggers49763fc2019-12-01 13:53:30 -08002273 char *name, size_t max_namelen,
2274 bool prefer_inauthentic)
Eric Biggers40153b12019-04-11 21:57:41 -07002275{
2276 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2277 const unsigned int ivsize = crypto_aead_ivsize(tfm);
Eric Biggers2ea91502019-12-01 13:53:29 -08002278 const unsigned int maxauthsize = crypto_aead_maxauthsize(tfm);
Eric Biggers40153b12019-04-11 21:57:41 -07002279 unsigned int authsize;
2280 unsigned int total_len;
Eric Biggers40153b12019-04-11 21:57:41 -07002281
2282 /* Key: length in [0, maxkeysize], but usually choose maxkeysize */
2283 vec->klen = maxkeysize;
2284 if (prandom_u32() % 4 == 0)
2285 vec->klen = prandom_u32() % (maxkeysize + 1);
2286 generate_random_bytes((u8 *)vec->key, vec->klen);
2287 vec->setkey_error = crypto_aead_setkey(tfm, vec->key, vec->klen);
2288
2289 /* IV */
2290 generate_random_bytes((u8 *)vec->iv, ivsize);
2291
2292 /* Tag length: in [0, maxauthsize], but usually choose maxauthsize */
2293 authsize = maxauthsize;
2294 if (prandom_u32() % 4 == 0)
2295 authsize = prandom_u32() % (maxauthsize + 1);
Eric Biggers49763fc2019-12-01 13:53:30 -08002296 if (prefer_inauthentic && authsize < MIN_COLLISION_FREE_AUTHSIZE)
2297 authsize = MIN_COLLISION_FREE_AUTHSIZE;
Eric Biggers40153b12019-04-11 21:57:41 -07002298 if (WARN_ON(authsize > maxdatasize))
2299 authsize = maxdatasize;
2300 maxdatasize -= authsize;
2301 vec->setauthsize_error = crypto_aead_setauthsize(tfm, authsize);
2302
Eric Biggers49763fc2019-12-01 13:53:30 -08002303 /* AAD, plaintext, and ciphertext lengths */
Eric Biggers40153b12019-04-11 21:57:41 -07002304 total_len = generate_random_length(maxdatasize);
2305 if (prandom_u32() % 4 == 0)
2306 vec->alen = 0;
2307 else
2308 vec->alen = generate_random_length(total_len);
2309 vec->plen = total_len - vec->alen;
Eric Biggers40153b12019-04-11 21:57:41 -07002310 vec->clen = vec->plen + authsize;
2311
2312 /*
Eric Biggers49763fc2019-12-01 13:53:30 -08002313 * Generate the AAD, plaintext, and ciphertext. Not applicable if the
2314 * key or the authentication tag size couldn't be set.
Eric Biggers40153b12019-04-11 21:57:41 -07002315 */
Eric Biggers49763fc2019-12-01 13:53:30 -08002316 vec->novrfy = 0;
Eric Biggerseb455db2019-12-01 13:53:26 -08002317 vec->crypt_error = 0;
Eric Biggers49763fc2019-12-01 13:53:30 -08002318 if (vec->setkey_error == 0 && vec->setauthsize_error == 0)
2319 generate_aead_message(req, suite, vec, prefer_inauthentic);
Eric Biggers40153b12019-04-11 21:57:41 -07002320 snprintf(name, max_namelen,
Eric Biggers49763fc2019-12-01 13:53:30 -08002321 "\"random: alen=%u plen=%u authsize=%u klen=%u novrfy=%d\"",
2322 vec->alen, vec->plen, authsize, vec->klen, vec->novrfy);
2323}
2324
2325static void try_to_generate_inauthentic_testvec(
2326 struct aead_extra_tests_ctx *ctx)
2327{
2328 int i;
2329
2330 for (i = 0; i < 10; i++) {
2331 generate_random_aead_testvec(ctx->req, &ctx->vec,
2332 &ctx->test_desc->suite.aead,
2333 ctx->maxkeysize, ctx->maxdatasize,
2334 ctx->vec_name,
2335 sizeof(ctx->vec_name), true);
2336 if (ctx->vec.novrfy)
2337 return;
2338 }
2339}
2340
2341/*
2342 * Generate inauthentic test vectors (i.e. ciphertext, AAD pairs that aren't the
2343 * result of an encryption with the key) and verify that decryption fails.
2344 */
2345static int test_aead_inauthentic_inputs(struct aead_extra_tests_ctx *ctx)
2346{
2347 unsigned int i;
2348 int err;
2349
2350 for (i = 0; i < fuzz_iterations * 8; i++) {
2351 /*
2352 * Since this part of the tests isn't comparing the
2353 * implementation to another, there's no point in testing any
2354 * test vectors other than inauthentic ones (vec.novrfy=1) here.
2355 *
2356 * If we're having trouble generating such a test vector, e.g.
2357 * if the algorithm keeps rejecting the generated keys, don't
2358 * retry forever; just continue on.
2359 */
2360 try_to_generate_inauthentic_testvec(ctx);
2361 if (ctx->vec.novrfy) {
2362 generate_random_testvec_config(&ctx->cfg, ctx->cfgname,
2363 sizeof(ctx->cfgname));
2364 err = test_aead_vec_cfg(ctx->driver, DECRYPT, &ctx->vec,
2365 ctx->vec_name, &ctx->cfg,
2366 ctx->req, ctx->tsgls);
2367 if (err)
2368 return err;
2369 }
2370 cond_resched();
2371 }
2372 return 0;
Eric Biggers40153b12019-04-11 21:57:41 -07002373}
2374
2375/*
Eric Biggers2ea91502019-12-01 13:53:29 -08002376 * Test the AEAD algorithm against the corresponding generic implementation, if
2377 * one is available.
Eric Biggers40153b12019-04-11 21:57:41 -07002378 */
Eric Biggers2ea91502019-12-01 13:53:29 -08002379static int test_aead_vs_generic_impl(struct aead_extra_tests_ctx *ctx)
Eric Biggers40153b12019-04-11 21:57:41 -07002380{
Eric Biggers2ea91502019-12-01 13:53:29 -08002381 struct crypto_aead *tfm = ctx->tfm;
Eric Biggers40153b12019-04-11 21:57:41 -07002382 const char *algname = crypto_aead_alg(tfm)->base.cra_name;
Eric Biggers2ea91502019-12-01 13:53:29 -08002383 const char *driver = ctx->driver;
2384 const char *generic_driver = ctx->test_desc->generic_driver;
Eric Biggers40153b12019-04-11 21:57:41 -07002385 char _generic_driver[CRYPTO_MAX_ALG_NAME];
2386 struct crypto_aead *generic_tfm = NULL;
2387 struct aead_request *generic_req = NULL;
Eric Biggers40153b12019-04-11 21:57:41 -07002388 unsigned int i;
Eric Biggers40153b12019-04-11 21:57:41 -07002389 int err;
2390
Eric Biggers40153b12019-04-11 21:57:41 -07002391 if (!generic_driver) { /* Use default naming convention? */
2392 err = build_generic_driver_name(algname, _generic_driver);
2393 if (err)
2394 return err;
2395 generic_driver = _generic_driver;
2396 }
2397
2398 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
2399 return 0;
2400
2401 generic_tfm = crypto_alloc_aead(generic_driver, 0, 0);
2402 if (IS_ERR(generic_tfm)) {
2403 err = PTR_ERR(generic_tfm);
2404 if (err == -ENOENT) {
2405 pr_warn("alg: aead: skipping comparison tests for %s because %s is unavailable\n",
2406 driver, generic_driver);
2407 return 0;
2408 }
2409 pr_err("alg: aead: error allocating %s (generic impl of %s): %d\n",
2410 generic_driver, algname, err);
2411 return err;
2412 }
2413
2414 generic_req = aead_request_alloc(generic_tfm, GFP_KERNEL);
2415 if (!generic_req) {
2416 err = -ENOMEM;
2417 goto out;
2418 }
2419
2420 /* Check the algorithm properties for consistency. */
2421
Eric Biggers2ea91502019-12-01 13:53:29 -08002422 if (crypto_aead_maxauthsize(tfm) !=
2423 crypto_aead_maxauthsize(generic_tfm)) {
Eric Biggers40153b12019-04-11 21:57:41 -07002424 pr_err("alg: aead: maxauthsize for %s (%u) doesn't match generic impl (%u)\n",
Eric Biggers2ea91502019-12-01 13:53:29 -08002425 driver, crypto_aead_maxauthsize(tfm),
2426 crypto_aead_maxauthsize(generic_tfm));
Eric Biggers40153b12019-04-11 21:57:41 -07002427 err = -EINVAL;
2428 goto out;
2429 }
2430
Eric Biggers2ea91502019-12-01 13:53:29 -08002431 if (crypto_aead_ivsize(tfm) != crypto_aead_ivsize(generic_tfm)) {
Eric Biggers40153b12019-04-11 21:57:41 -07002432 pr_err("alg: aead: ivsize for %s (%u) doesn't match generic impl (%u)\n",
Eric Biggers2ea91502019-12-01 13:53:29 -08002433 driver, crypto_aead_ivsize(tfm),
2434 crypto_aead_ivsize(generic_tfm));
Eric Biggers40153b12019-04-11 21:57:41 -07002435 err = -EINVAL;
2436 goto out;
2437 }
2438
Eric Biggers2ea91502019-12-01 13:53:29 -08002439 if (crypto_aead_blocksize(tfm) != crypto_aead_blocksize(generic_tfm)) {
Eric Biggers40153b12019-04-11 21:57:41 -07002440 pr_err("alg: aead: blocksize for %s (%u) doesn't match generic impl (%u)\n",
Eric Biggers2ea91502019-12-01 13:53:29 -08002441 driver, crypto_aead_blocksize(tfm),
2442 crypto_aead_blocksize(generic_tfm));
Eric Biggers40153b12019-04-11 21:57:41 -07002443 err = -EINVAL;
2444 goto out;
2445 }
2446
2447 /*
2448 * Now generate test vectors using the generic implementation, and test
2449 * the other implementation against them.
2450 */
Eric Biggers40153b12019-04-11 21:57:41 -07002451 for (i = 0; i < fuzz_iterations * 8; i++) {
Eric Biggers2ea91502019-12-01 13:53:29 -08002452 generate_random_aead_testvec(generic_req, &ctx->vec,
Eric Biggers49763fc2019-12-01 13:53:30 -08002453 &ctx->test_desc->suite.aead,
Eric Biggers2ea91502019-12-01 13:53:29 -08002454 ctx->maxkeysize, ctx->maxdatasize,
2455 ctx->vec_name,
Eric Biggers49763fc2019-12-01 13:53:30 -08002456 sizeof(ctx->vec_name), false);
Eric Biggers2ea91502019-12-01 13:53:29 -08002457 generate_random_testvec_config(&ctx->cfg, ctx->cfgname,
2458 sizeof(ctx->cfgname));
Eric Biggers49763fc2019-12-01 13:53:30 -08002459 if (!ctx->vec.novrfy) {
2460 err = test_aead_vec_cfg(driver, ENCRYPT, &ctx->vec,
2461 ctx->vec_name, &ctx->cfg,
2462 ctx->req, ctx->tsgls);
2463 if (err)
2464 goto out;
2465 }
2466 if (ctx->vec.crypt_error == 0 || ctx->vec.novrfy) {
Eric Biggers2ea91502019-12-01 13:53:29 -08002467 err = test_aead_vec_cfg(driver, DECRYPT, &ctx->vec,
2468 ctx->vec_name, &ctx->cfg,
2469 ctx->req, ctx->tsgls);
Eric Biggerseb455db2019-12-01 13:53:26 -08002470 if (err)
2471 goto out;
2472 }
Eric Biggers40153b12019-04-11 21:57:41 -07002473 cond_resched();
2474 }
2475 err = 0;
2476out:
Eric Biggers40153b12019-04-11 21:57:41 -07002477 crypto_free_aead(generic_tfm);
2478 aead_request_free(generic_req);
2479 return err;
2480}
Eric Biggers2ea91502019-12-01 13:53:29 -08002481
2482static int test_aead_extra(const char *driver,
2483 const struct alg_test_desc *test_desc,
2484 struct aead_request *req,
2485 struct cipher_test_sglists *tsgls)
2486{
2487 struct aead_extra_tests_ctx *ctx;
2488 unsigned int i;
2489 int err;
2490
2491 if (noextratests)
2492 return 0;
2493
2494 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2495 if (!ctx)
2496 return -ENOMEM;
2497 ctx->req = req;
2498 ctx->tfm = crypto_aead_reqtfm(req);
2499 ctx->driver = driver;
2500 ctx->test_desc = test_desc;
2501 ctx->tsgls = tsgls;
2502 ctx->maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
2503 ctx->maxkeysize = 0;
2504 for (i = 0; i < test_desc->suite.aead.count; i++)
2505 ctx->maxkeysize = max_t(unsigned int, ctx->maxkeysize,
2506 test_desc->suite.aead.vecs[i].klen);
2507
2508 ctx->vec.key = kmalloc(ctx->maxkeysize, GFP_KERNEL);
2509 ctx->vec.iv = kmalloc(crypto_aead_ivsize(ctx->tfm), GFP_KERNEL);
2510 ctx->vec.assoc = kmalloc(ctx->maxdatasize, GFP_KERNEL);
2511 ctx->vec.ptext = kmalloc(ctx->maxdatasize, GFP_KERNEL);
2512 ctx->vec.ctext = kmalloc(ctx->maxdatasize, GFP_KERNEL);
2513 if (!ctx->vec.key || !ctx->vec.iv || !ctx->vec.assoc ||
2514 !ctx->vec.ptext || !ctx->vec.ctext) {
2515 err = -ENOMEM;
2516 goto out;
2517 }
2518
Eric Biggers8ff357a2020-03-04 14:44:04 -08002519 err = test_aead_vs_generic_impl(ctx);
Eric Biggers49763fc2019-12-01 13:53:30 -08002520 if (err)
2521 goto out;
2522
Eric Biggers8ff357a2020-03-04 14:44:04 -08002523 err = test_aead_inauthentic_inputs(ctx);
Eric Biggers2ea91502019-12-01 13:53:29 -08002524out:
2525 kfree(ctx->vec.key);
2526 kfree(ctx->vec.iv);
2527 kfree(ctx->vec.assoc);
2528 kfree(ctx->vec.ptext);
2529 kfree(ctx->vec.ctext);
2530 kfree(ctx);
2531 return err;
2532}
Eric Biggers40153b12019-04-11 21:57:41 -07002533#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
Eric Biggers2ea91502019-12-01 13:53:29 -08002534static int test_aead_extra(const char *driver,
2535 const struct alg_test_desc *test_desc,
2536 struct aead_request *req,
2537 struct cipher_test_sglists *tsgls)
Eric Biggers40153b12019-04-11 21:57:41 -07002538{
2539 return 0;
2540}
2541#endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
2542
Eric Biggersed968042019-01-31 23:51:47 -08002543static int test_aead(const char *driver, int enc,
2544 const struct aead_test_suite *suite,
2545 struct aead_request *req,
2546 struct cipher_test_sglists *tsgls)
2547{
2548 unsigned int i;
2549 int err;
2550
2551 for (i = 0; i < suite->count; i++) {
2552 err = test_aead_vec(driver, enc, &suite->vecs[i], i, req,
2553 tsgls);
2554 if (err)
2555 return err;
Eric Biggerse63e1b02019-06-02 22:42:33 -07002556 cond_resched();
Eric Biggersed968042019-01-31 23:51:47 -08002557 }
2558 return 0;
2559}
2560
2561static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
2562 u32 type, u32 mask)
2563{
2564 const struct aead_test_suite *suite = &desc->suite.aead;
2565 struct crypto_aead *tfm;
2566 struct aead_request *req = NULL;
2567 struct cipher_test_sglists *tsgls = NULL;
2568 int err;
2569
2570 if (suite->count <= 0) {
2571 pr_err("alg: aead: empty test suite for %s\n", driver);
2572 return -EINVAL;
2573 }
2574
2575 tfm = crypto_alloc_aead(driver, type, mask);
2576 if (IS_ERR(tfm)) {
2577 pr_err("alg: aead: failed to allocate transform for %s: %ld\n",
2578 driver, PTR_ERR(tfm));
2579 return PTR_ERR(tfm);
2580 }
2581
2582 req = aead_request_alloc(tfm, GFP_KERNEL);
2583 if (!req) {
2584 pr_err("alg: aead: failed to allocate request for %s\n",
2585 driver);
2586 err = -ENOMEM;
2587 goto out;
2588 }
2589
2590 tsgls = alloc_cipher_test_sglists();
2591 if (!tsgls) {
2592 pr_err("alg: aead: failed to allocate test buffers for %s\n",
2593 driver);
2594 err = -ENOMEM;
2595 goto out;
2596 }
2597
2598 err = test_aead(driver, ENCRYPT, suite, req, tsgls);
2599 if (err)
2600 goto out;
2601
2602 err = test_aead(driver, DECRYPT, suite, req, tsgls);
Eric Biggers40153b12019-04-11 21:57:41 -07002603 if (err)
2604 goto out;
2605
Eric Biggers2ea91502019-12-01 13:53:29 -08002606 err = test_aead_extra(driver, desc, req, tsgls);
Eric Biggersed968042019-01-31 23:51:47 -08002607out:
2608 free_cipher_test_sglists(tsgls);
2609 aead_request_free(req);
2610 crypto_free_aead(tfm);
2611 return err;
2612}
2613
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002614static int test_cipher(struct crypto_cipher *tfm, int enc,
Eric Biggersb13b1e02017-02-24 15:46:59 -08002615 const struct cipher_testvec *template,
2616 unsigned int tcount)
Herbert Xuda7f0332008-07-31 17:08:25 +08002617{
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002618 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
2619 unsigned int i, j, k;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002620 char *q;
2621 const char *e;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002622 const char *input, *result;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002623 void *data;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08002624 char *xbuf[XBUFSIZE];
2625 int ret = -ENOMEM;
2626
2627 if (testmgr_alloc_buf(xbuf))
2628 goto out_nobuf;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002629
2630 if (enc == ENCRYPT)
2631 e = "encryption";
2632 else
2633 e = "decryption";
2634
2635 j = 0;
2636 for (i = 0; i < tcount; i++) {
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002637
Stephan Mueller10faa8c2016-08-25 15:15:01 +02002638 if (fips_enabled && template[i].fips_skip)
2639 continue;
2640
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002641 input = enc ? template[i].ptext : template[i].ctext;
2642 result = enc ? template[i].ctext : template[i].ptext;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002643 j++;
2644
Herbert Xufd57f222009-05-29 16:05:42 +10002645 ret = -EINVAL;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002646 if (WARN_ON(template[i].len > PAGE_SIZE))
Herbert Xufd57f222009-05-29 16:05:42 +10002647 goto out;
2648
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002649 data = xbuf[0];
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002650 memcpy(data, input, template[i].len);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002651
2652 crypto_cipher_clear_flags(tfm, ~0);
2653 if (template[i].wk)
Eric Biggers231baec2019-01-18 22:48:00 -08002654 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002655
2656 ret = crypto_cipher_setkey(tfm, template[i].key,
2657 template[i].klen);
Eric Biggers5283a8e2019-04-11 21:57:36 -07002658 if (ret) {
2659 if (ret == template[i].setkey_error)
2660 continue;
2661 pr_err("alg: cipher: %s setkey failed on test vector %u; expected_error=%d, actual_error=%d, flags=%#x\n",
2662 algo, j, template[i].setkey_error, ret,
2663 crypto_cipher_get_flags(tfm));
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002664 goto out;
Eric Biggers5283a8e2019-04-11 21:57:36 -07002665 }
2666 if (template[i].setkey_error) {
2667 pr_err("alg: cipher: %s setkey unexpectedly succeeded on test vector %u; expected_error=%d\n",
2668 algo, j, template[i].setkey_error);
2669 ret = -EINVAL;
2670 goto out;
2671 }
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002672
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002673 for (k = 0; k < template[i].len;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002674 k += crypto_cipher_blocksize(tfm)) {
2675 if (enc)
2676 crypto_cipher_encrypt_one(tfm, data + k,
2677 data + k);
2678 else
2679 crypto_cipher_decrypt_one(tfm, data + k,
2680 data + k);
2681 }
2682
2683 q = data;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002684 if (memcmp(q, result, template[i].len)) {
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002685 printk(KERN_ERR "alg: cipher: Test %d failed "
2686 "on %s for %s\n", j, e, algo);
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002687 hexdump(q, template[i].len);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002688 ret = -EINVAL;
2689 goto out;
2690 }
2691 }
2692
2693 ret = 0;
2694
2695out:
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08002696 testmgr_free_buf(xbuf);
2697out_nobuf:
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002698 return ret;
2699}
2700
Eric Biggers4e7babba2019-01-31 23:51:46 -08002701static int test_skcipher_vec_cfg(const char *driver, int enc,
2702 const struct cipher_testvec *vec,
Eric Biggers951d1332019-04-11 21:57:37 -07002703 const char *vec_name,
Eric Biggers4e7babba2019-01-31 23:51:46 -08002704 const struct testvec_config *cfg,
2705 struct skcipher_request *req,
2706 struct cipher_test_sglists *tsgls)
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002707{
Eric Biggers4e7babba2019-01-31 23:51:46 -08002708 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2709 const unsigned int alignmask = crypto_skcipher_alignmask(tfm);
2710 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2711 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
2712 const char *op = enc ? "encryption" : "decryption";
2713 DECLARE_CRYPTO_WAIT(wait);
2714 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
2715 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
2716 cfg->iv_offset +
2717 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
2718 struct kvec input;
2719 int err;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08002720
Eric Biggers4e7babba2019-01-31 23:51:46 -08002721 /* Set the key */
2722 if (vec->wk)
2723 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03002724 else
Eric Biggers4e7babba2019-01-31 23:51:46 -08002725 crypto_skcipher_clear_flags(tfm,
2726 CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Eric Biggersfd8c37c2019-12-01 13:53:28 -08002727 err = do_setkey(crypto_skcipher_setkey, tfm, vec->key, vec->klen,
2728 cfg, alignmask);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002729 if (err) {
Eric Biggers5283a8e2019-04-11 21:57:36 -07002730 if (err == vec->setkey_error)
Eric Biggers4e7babba2019-01-31 23:51:46 -08002731 return 0;
Eric Biggers951d1332019-04-11 21:57:37 -07002732 pr_err("alg: skcipher: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
2733 driver, vec_name, vec->setkey_error, err,
Eric Biggers5283a8e2019-04-11 21:57:36 -07002734 crypto_skcipher_get_flags(tfm));
Eric Biggers4e7babba2019-01-31 23:51:46 -08002735 return err;
2736 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07002737 if (vec->setkey_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07002738 pr_err("alg: skcipher: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
2739 driver, vec_name, vec->setkey_error);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002740 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08002741 }
2742
Eric Biggers4e7babba2019-01-31 23:51:46 -08002743 /* The IV must be copied to a buffer, as the algorithm may modify it */
2744 if (ivsize) {
2745 if (WARN_ON(ivsize > MAX_IVLEN))
2746 return -EINVAL;
Eric Biggers8efd9722019-02-14 00:03:51 -08002747 if (vec->generates_iv && !enc)
2748 memcpy(iv, vec->iv_out, ivsize);
2749 else if (vec->iv)
Eric Biggers4e7babba2019-01-31 23:51:46 -08002750 memcpy(iv, vec->iv, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08002751 else
Eric Biggers4e7babba2019-01-31 23:51:46 -08002752 memset(iv, 0, ivsize);
2753 } else {
2754 if (vec->generates_iv) {
Eric Biggers951d1332019-04-11 21:57:37 -07002755 pr_err("alg: skcipher: %s has ivsize=0 but test vector %s generates IV!\n",
2756 driver, vec_name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002757 return -EINVAL;
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03002758 }
Eric Biggers4e7babba2019-01-31 23:51:46 -08002759 iv = NULL;
Herbert Xuda7f0332008-07-31 17:08:25 +08002760 }
2761
Eric Biggers4e7babba2019-01-31 23:51:46 -08002762 /* Build the src/dst scatterlists */
2763 input.iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
2764 input.iov_len = vec->len;
2765 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
2766 vec->len, vec->len, &input, 1);
2767 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07002768 pr_err("alg: skcipher: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
2769 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002770 return err;
Herbert Xuda7f0332008-07-31 17:08:25 +08002771 }
2772
Eric Biggers4e7babba2019-01-31 23:51:46 -08002773 /* Do the actual encryption or decryption */
2774 testmgr_poison(req->__ctx, crypto_skcipher_reqsize(tfm));
2775 skcipher_request_set_callback(req, req_flags, crypto_req_done, &wait);
2776 skcipher_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
2777 vec->len, iv);
Eric Biggers65707372019-03-12 22:12:52 -07002778 if (cfg->nosimd)
2779 crypto_disable_simd_for_test();
2780 err = enc ? crypto_skcipher_encrypt(req) : crypto_skcipher_decrypt(req);
2781 if (cfg->nosimd)
2782 crypto_reenable_simd_for_test();
2783 err = crypto_wait_req(err, &wait);
Herbert Xuda7f0332008-07-31 17:08:25 +08002784
Eric Biggersfa353c92019-01-31 23:51:49 -08002785 /* Check that the algorithm didn't overwrite things it shouldn't have */
2786 if (req->cryptlen != vec->len ||
2787 req->iv != iv ||
2788 req->src != tsgls->src.sgl_ptr ||
2789 req->dst != tsgls->dst.sgl_ptr ||
2790 crypto_skcipher_reqtfm(req) != tfm ||
2791 req->base.complete != crypto_req_done ||
2792 req->base.flags != req_flags ||
2793 req->base.data != &wait) {
Eric Biggers951d1332019-04-11 21:57:37 -07002794 pr_err("alg: skcipher: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
2795 driver, op, vec_name, cfg->name);
Eric Biggersfa353c92019-01-31 23:51:49 -08002796 if (req->cryptlen != vec->len)
2797 pr_err("alg: skcipher: changed 'req->cryptlen'\n");
2798 if (req->iv != iv)
2799 pr_err("alg: skcipher: changed 'req->iv'\n");
2800 if (req->src != tsgls->src.sgl_ptr)
2801 pr_err("alg: skcipher: changed 'req->src'\n");
2802 if (req->dst != tsgls->dst.sgl_ptr)
2803 pr_err("alg: skcipher: changed 'req->dst'\n");
2804 if (crypto_skcipher_reqtfm(req) != tfm)
2805 pr_err("alg: skcipher: changed 'req->base.tfm'\n");
2806 if (req->base.complete != crypto_req_done)
2807 pr_err("alg: skcipher: changed 'req->base.complete'\n");
2808 if (req->base.flags != req_flags)
2809 pr_err("alg: skcipher: changed 'req->base.flags'\n");
2810 if (req->base.data != &wait)
2811 pr_err("alg: skcipher: changed 'req->base.data'\n");
2812 return -EINVAL;
2813 }
2814 if (is_test_sglist_corrupted(&tsgls->src)) {
Eric Biggers951d1332019-04-11 21:57:37 -07002815 pr_err("alg: skcipher: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
2816 driver, op, vec_name, cfg->name);
Eric Biggersfa353c92019-01-31 23:51:49 -08002817 return -EINVAL;
2818 }
2819 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
2820 is_test_sglist_corrupted(&tsgls->dst)) {
Eric Biggers951d1332019-04-11 21:57:37 -07002821 pr_err("alg: skcipher: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
2822 driver, op, vec_name, cfg->name);
Eric Biggersfa353c92019-01-31 23:51:49 -08002823 return -EINVAL;
2824 }
2825
Eric Biggers5283a8e2019-04-11 21:57:36 -07002826 /* Check for success or failure */
2827 if (err) {
2828 if (err == vec->crypt_error)
2829 return 0;
Eric Biggers951d1332019-04-11 21:57:37 -07002830 pr_err("alg: skcipher: %s %s failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
2831 driver, op, vec_name, vec->crypt_error, err, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07002832 return err;
2833 }
2834 if (vec->crypt_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07002835 pr_err("alg: skcipher: %s %s unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
2836 driver, op, vec_name, vec->crypt_error, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07002837 return -EINVAL;
2838 }
2839
Eric Biggers4e7babba2019-01-31 23:51:46 -08002840 /* Check for the correct output (ciphertext or plaintext) */
2841 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
2842 vec->len, 0, true);
2843 if (err == -EOVERFLOW) {
Eric Biggers951d1332019-04-11 21:57:37 -07002844 pr_err("alg: skcipher: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
2845 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002846 return err;
2847 }
2848 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07002849 pr_err("alg: skcipher: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
2850 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002851 return err;
2852 }
Herbert Xuda7f0332008-07-31 17:08:25 +08002853
Eric Biggers4e7babba2019-01-31 23:51:46 -08002854 /* If applicable, check that the algorithm generated the correct IV */
Eric Biggers8efd9722019-02-14 00:03:51 -08002855 if (vec->iv_out && memcmp(iv, vec->iv_out, ivsize) != 0) {
Eric Biggers951d1332019-04-11 21:57:37 -07002856 pr_err("alg: skcipher: %s %s test failed (wrong output IV) on test vector %s, cfg=\"%s\"\n",
2857 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002858 hexdump(iv, ivsize);
2859 return -EINVAL;
Jussi Kivilinna3a338f22013-06-13 17:37:45 +03002860 }
2861
2862 return 0;
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03002863}
2864
Eric Biggers4e7babba2019-01-31 23:51:46 -08002865static int test_skcipher_vec(const char *driver, int enc,
2866 const struct cipher_testvec *vec,
2867 unsigned int vec_num,
2868 struct skcipher_request *req,
2869 struct cipher_test_sglists *tsgls)
2870{
Eric Biggers951d1332019-04-11 21:57:37 -07002871 char vec_name[16];
Eric Biggers4e7babba2019-01-31 23:51:46 -08002872 unsigned int i;
2873 int err;
2874
2875 if (fips_enabled && vec->fips_skip)
2876 return 0;
2877
Eric Biggers951d1332019-04-11 21:57:37 -07002878 sprintf(vec_name, "%u", vec_num);
2879
Eric Biggers4e7babba2019-01-31 23:51:46 -08002880 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
Eric Biggers951d1332019-04-11 21:57:37 -07002881 err = test_skcipher_vec_cfg(driver, enc, vec, vec_name,
Eric Biggers4e7babba2019-01-31 23:51:46 -08002882 &default_cipher_testvec_configs[i],
2883 req, tsgls);
2884 if (err)
2885 return err;
2886 }
2887
2888#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2889 if (!noextratests) {
2890 struct testvec_config cfg;
2891 char cfgname[TESTVEC_CONFIG_NAMELEN];
2892
2893 for (i = 0; i < fuzz_iterations; i++) {
2894 generate_random_testvec_config(&cfg, cfgname,
2895 sizeof(cfgname));
Eric Biggers951d1332019-04-11 21:57:37 -07002896 err = test_skcipher_vec_cfg(driver, enc, vec, vec_name,
Eric Biggers4e7babba2019-01-31 23:51:46 -08002897 &cfg, req, tsgls);
2898 if (err)
2899 return err;
Eric Biggerse63e1b02019-06-02 22:42:33 -07002900 cond_resched();
Eric Biggers4e7babba2019-01-31 23:51:46 -08002901 }
2902 }
2903#endif
2904 return 0;
2905}
2906
Eric Biggersd435e102019-04-11 21:57:40 -07002907#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2908/*
2909 * Generate a symmetric cipher test vector from the given implementation.
2910 * Assumes the buffers in 'vec' were already allocated.
2911 */
2912static void generate_random_cipher_testvec(struct skcipher_request *req,
2913 struct cipher_testvec *vec,
2914 unsigned int maxdatasize,
2915 char *name, size_t max_namelen)
2916{
2917 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
Eric Biggers9ac0d132019-11-29 10:23:04 -08002918 const unsigned int maxkeysize = crypto_skcipher_max_keysize(tfm);
Eric Biggersd435e102019-04-11 21:57:40 -07002919 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2920 struct scatterlist src, dst;
2921 u8 iv[MAX_IVLEN];
2922 DECLARE_CRYPTO_WAIT(wait);
2923
2924 /* Key: length in [0, maxkeysize], but usually choose maxkeysize */
2925 vec->klen = maxkeysize;
2926 if (prandom_u32() % 4 == 0)
2927 vec->klen = prandom_u32() % (maxkeysize + 1);
2928 generate_random_bytes((u8 *)vec->key, vec->klen);
2929 vec->setkey_error = crypto_skcipher_setkey(tfm, vec->key, vec->klen);
2930
2931 /* IV */
2932 generate_random_bytes((u8 *)vec->iv, ivsize);
2933
2934 /* Plaintext */
2935 vec->len = generate_random_length(maxdatasize);
2936 generate_random_bytes((u8 *)vec->ptext, vec->len);
2937
2938 /* If the key couldn't be set, no need to continue to encrypt. */
2939 if (vec->setkey_error)
2940 goto done;
2941
2942 /* Ciphertext */
2943 sg_init_one(&src, vec->ptext, vec->len);
2944 sg_init_one(&dst, vec->ctext, vec->len);
2945 memcpy(iv, vec->iv, ivsize);
2946 skcipher_request_set_callback(req, 0, crypto_req_done, &wait);
2947 skcipher_request_set_crypt(req, &src, &dst, vec->len, iv);
2948 vec->crypt_error = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
Eric Biggerseb455db2019-12-01 13:53:26 -08002949 if (vec->crypt_error != 0) {
2950 /*
2951 * The only acceptable error here is for an invalid length, so
2952 * skcipher decryption should fail with the same error too.
2953 * We'll test for this. But to keep the API usage well-defined,
2954 * explicitly initialize the ciphertext buffer too.
2955 */
2956 memset((u8 *)vec->ctext, 0, vec->len);
2957 }
Eric Biggersd435e102019-04-11 21:57:40 -07002958done:
2959 snprintf(name, max_namelen, "\"random: len=%u klen=%u\"",
2960 vec->len, vec->klen);
2961}
2962
2963/*
2964 * Test the skcipher algorithm represented by @req against the corresponding
2965 * generic implementation, if one is available.
2966 */
2967static int test_skcipher_vs_generic_impl(const char *driver,
2968 const char *generic_driver,
2969 struct skcipher_request *req,
2970 struct cipher_test_sglists *tsgls)
2971{
2972 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
Eric Biggers9ac0d132019-11-29 10:23:04 -08002973 const unsigned int maxkeysize = crypto_skcipher_max_keysize(tfm);
Eric Biggersd435e102019-04-11 21:57:40 -07002974 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2975 const unsigned int blocksize = crypto_skcipher_blocksize(tfm);
2976 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
2977 const char *algname = crypto_skcipher_alg(tfm)->base.cra_name;
2978 char _generic_driver[CRYPTO_MAX_ALG_NAME];
2979 struct crypto_skcipher *generic_tfm = NULL;
2980 struct skcipher_request *generic_req = NULL;
2981 unsigned int i;
2982 struct cipher_testvec vec = { 0 };
2983 char vec_name[64];
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002984 struct testvec_config *cfg;
Eric Biggersd435e102019-04-11 21:57:40 -07002985 char cfgname[TESTVEC_CONFIG_NAMELEN];
2986 int err;
2987
2988 if (noextratests)
2989 return 0;
2990
2991 /* Keywrap isn't supported here yet as it handles its IV differently. */
2992 if (strncmp(algname, "kw(", 3) == 0)
2993 return 0;
2994
2995 if (!generic_driver) { /* Use default naming convention? */
2996 err = build_generic_driver_name(algname, _generic_driver);
2997 if (err)
2998 return err;
2999 generic_driver = _generic_driver;
3000 }
3001
3002 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
3003 return 0;
3004
3005 generic_tfm = crypto_alloc_skcipher(generic_driver, 0, 0);
3006 if (IS_ERR(generic_tfm)) {
3007 err = PTR_ERR(generic_tfm);
3008 if (err == -ENOENT) {
3009 pr_warn("alg: skcipher: skipping comparison tests for %s because %s is unavailable\n",
3010 driver, generic_driver);
3011 return 0;
3012 }
3013 pr_err("alg: skcipher: error allocating %s (generic impl of %s): %d\n",
3014 generic_driver, algname, err);
3015 return err;
3016 }
3017
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02003018 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
3019 if (!cfg) {
3020 err = -ENOMEM;
3021 goto out;
3022 }
3023
Eric Biggersd435e102019-04-11 21:57:40 -07003024 generic_req = skcipher_request_alloc(generic_tfm, GFP_KERNEL);
3025 if (!generic_req) {
3026 err = -ENOMEM;
3027 goto out;
3028 }
3029
3030 /* Check the algorithm properties for consistency. */
3031
Eric Biggersfd60f722019-12-01 13:53:27 -08003032 if (crypto_skcipher_min_keysize(tfm) !=
3033 crypto_skcipher_min_keysize(generic_tfm)) {
3034 pr_err("alg: skcipher: min keysize for %s (%u) doesn't match generic impl (%u)\n",
3035 driver, crypto_skcipher_min_keysize(tfm),
3036 crypto_skcipher_min_keysize(generic_tfm));
3037 err = -EINVAL;
3038 goto out;
3039 }
3040
Eric Biggers9ac0d132019-11-29 10:23:04 -08003041 if (maxkeysize != crypto_skcipher_max_keysize(generic_tfm)) {
Eric Biggersd435e102019-04-11 21:57:40 -07003042 pr_err("alg: skcipher: max keysize for %s (%u) doesn't match generic impl (%u)\n",
Eric Biggers9ac0d132019-11-29 10:23:04 -08003043 driver, maxkeysize,
3044 crypto_skcipher_max_keysize(generic_tfm));
Eric Biggersd435e102019-04-11 21:57:40 -07003045 err = -EINVAL;
3046 goto out;
3047 }
3048
3049 if (ivsize != crypto_skcipher_ivsize(generic_tfm)) {
3050 pr_err("alg: skcipher: ivsize for %s (%u) doesn't match generic impl (%u)\n",
3051 driver, ivsize, crypto_skcipher_ivsize(generic_tfm));
3052 err = -EINVAL;
3053 goto out;
3054 }
3055
3056 if (blocksize != crypto_skcipher_blocksize(generic_tfm)) {
3057 pr_err("alg: skcipher: blocksize for %s (%u) doesn't match generic impl (%u)\n",
3058 driver, blocksize,
3059 crypto_skcipher_blocksize(generic_tfm));
3060 err = -EINVAL;
3061 goto out;
3062 }
3063
3064 /*
3065 * Now generate test vectors using the generic implementation, and test
3066 * the other implementation against them.
3067 */
3068
Eric Biggers9ac0d132019-11-29 10:23:04 -08003069 vec.key = kmalloc(maxkeysize, GFP_KERNEL);
Eric Biggersd435e102019-04-11 21:57:40 -07003070 vec.iv = kmalloc(ivsize, GFP_KERNEL);
3071 vec.ptext = kmalloc(maxdatasize, GFP_KERNEL);
3072 vec.ctext = kmalloc(maxdatasize, GFP_KERNEL);
3073 if (!vec.key || !vec.iv || !vec.ptext || !vec.ctext) {
3074 err = -ENOMEM;
3075 goto out;
3076 }
3077
3078 for (i = 0; i < fuzz_iterations * 8; i++) {
3079 generate_random_cipher_testvec(generic_req, &vec, maxdatasize,
3080 vec_name, sizeof(vec_name));
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02003081 generate_random_testvec_config(cfg, cfgname, sizeof(cfgname));
Eric Biggersd435e102019-04-11 21:57:40 -07003082
3083 err = test_skcipher_vec_cfg(driver, ENCRYPT, &vec, vec_name,
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02003084 cfg, req, tsgls);
Eric Biggersd435e102019-04-11 21:57:40 -07003085 if (err)
3086 goto out;
3087 err = test_skcipher_vec_cfg(driver, DECRYPT, &vec, vec_name,
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02003088 cfg, req, tsgls);
Eric Biggersd435e102019-04-11 21:57:40 -07003089 if (err)
3090 goto out;
3091 cond_resched();
3092 }
3093 err = 0;
3094out:
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02003095 kfree(cfg);
Eric Biggersd435e102019-04-11 21:57:40 -07003096 kfree(vec.key);
3097 kfree(vec.iv);
3098 kfree(vec.ptext);
3099 kfree(vec.ctext);
3100 crypto_free_skcipher(generic_tfm);
3101 skcipher_request_free(generic_req);
3102 return err;
3103}
3104#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
3105static int test_skcipher_vs_generic_impl(const char *driver,
3106 const char *generic_driver,
3107 struct skcipher_request *req,
3108 struct cipher_test_sglists *tsgls)
3109{
3110 return 0;
3111}
3112#endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
3113
Eric Biggers4e7babba2019-01-31 23:51:46 -08003114static int test_skcipher(const char *driver, int enc,
3115 const struct cipher_test_suite *suite,
3116 struct skcipher_request *req,
3117 struct cipher_test_sglists *tsgls)
3118{
3119 unsigned int i;
3120 int err;
3121
3122 for (i = 0; i < suite->count; i++) {
3123 err = test_skcipher_vec(driver, enc, &suite->vecs[i], i, req,
3124 tsgls);
3125 if (err)
3126 return err;
Eric Biggerse63e1b02019-06-02 22:42:33 -07003127 cond_resched();
Eric Biggers4e7babba2019-01-31 23:51:46 -08003128 }
3129 return 0;
3130}
3131
3132static int alg_test_skcipher(const struct alg_test_desc *desc,
3133 const char *driver, u32 type, u32 mask)
3134{
3135 const struct cipher_test_suite *suite = &desc->suite.cipher;
3136 struct crypto_skcipher *tfm;
3137 struct skcipher_request *req = NULL;
3138 struct cipher_test_sglists *tsgls = NULL;
3139 int err;
3140
3141 if (suite->count <= 0) {
3142 pr_err("alg: skcipher: empty test suite for %s\n", driver);
3143 return -EINVAL;
3144 }
3145
3146 tfm = crypto_alloc_skcipher(driver, type, mask);
3147 if (IS_ERR(tfm)) {
3148 pr_err("alg: skcipher: failed to allocate transform for %s: %ld\n",
3149 driver, PTR_ERR(tfm));
3150 return PTR_ERR(tfm);
3151 }
3152
3153 req = skcipher_request_alloc(tfm, GFP_KERNEL);
3154 if (!req) {
3155 pr_err("alg: skcipher: failed to allocate request for %s\n",
3156 driver);
3157 err = -ENOMEM;
3158 goto out;
3159 }
3160
3161 tsgls = alloc_cipher_test_sglists();
3162 if (!tsgls) {
3163 pr_err("alg: skcipher: failed to allocate test buffers for %s\n",
3164 driver);
3165 err = -ENOMEM;
3166 goto out;
3167 }
3168
3169 err = test_skcipher(driver, ENCRYPT, suite, req, tsgls);
3170 if (err)
3171 goto out;
3172
3173 err = test_skcipher(driver, DECRYPT, suite, req, tsgls);
Eric Biggersd435e102019-04-11 21:57:40 -07003174 if (err)
3175 goto out;
3176
3177 err = test_skcipher_vs_generic_impl(driver, desc->generic_driver, req,
3178 tsgls);
Eric Biggers4e7babba2019-01-31 23:51:46 -08003179out:
3180 free_cipher_test_sglists(tsgls);
3181 skcipher_request_free(req);
3182 crypto_free_skcipher(tfm);
3183 return err;
3184}
3185
Eric Biggersb13b1e02017-02-24 15:46:59 -08003186static int test_comp(struct crypto_comp *tfm,
3187 const struct comp_testvec *ctemplate,
3188 const struct comp_testvec *dtemplate,
3189 int ctcount, int dtcount)
Herbert Xuda7f0332008-07-31 17:08:25 +08003190{
3191 const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
Mahipal Challa33607382018-04-11 20:28:32 +02003192 char *output, *decomp_output;
Herbert Xuda7f0332008-07-31 17:08:25 +08003193 unsigned int i;
Herbert Xuda7f0332008-07-31 17:08:25 +08003194 int ret;
3195
Mahipal Challa33607382018-04-11 20:28:32 +02003196 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3197 if (!output)
3198 return -ENOMEM;
3199
3200 decomp_output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3201 if (!decomp_output) {
3202 kfree(output);
3203 return -ENOMEM;
3204 }
3205
Herbert Xuda7f0332008-07-31 17:08:25 +08003206 for (i = 0; i < ctcount; i++) {
Geert Uytterhoevenc79cf912009-03-29 15:44:19 +08003207 int ilen;
3208 unsigned int dlen = COMP_BUF_SIZE;
Herbert Xuda7f0332008-07-31 17:08:25 +08003209
Michael Schupikov22a81182018-10-07 13:58:10 +02003210 memset(output, 0, COMP_BUF_SIZE);
3211 memset(decomp_output, 0, COMP_BUF_SIZE);
Herbert Xuda7f0332008-07-31 17:08:25 +08003212
3213 ilen = ctemplate[i].inlen;
3214 ret = crypto_comp_compress(tfm, ctemplate[i].input,
Mahipal Challa33607382018-04-11 20:28:32 +02003215 ilen, output, &dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08003216 if (ret) {
3217 printk(KERN_ERR "alg: comp: compression failed "
3218 "on test %d for %s: ret=%d\n", i + 1, algo,
3219 -ret);
3220 goto out;
3221 }
3222
Mahipal Challa33607382018-04-11 20:28:32 +02003223 ilen = dlen;
3224 dlen = COMP_BUF_SIZE;
3225 ret = crypto_comp_decompress(tfm, output,
3226 ilen, decomp_output, &dlen);
3227 if (ret) {
3228 pr_err("alg: comp: compression failed: decompress: on test %d for %s failed: ret=%d\n",
3229 i + 1, algo, -ret);
3230 goto out;
3231 }
3232
3233 if (dlen != ctemplate[i].inlen) {
Geert Uytterhoevenb812eb02008-11-28 20:51:28 +08003234 printk(KERN_ERR "alg: comp: Compression test %d "
3235 "failed for %s: output len = %d\n", i + 1, algo,
3236 dlen);
3237 ret = -EINVAL;
3238 goto out;
3239 }
3240
Mahipal Challa33607382018-04-11 20:28:32 +02003241 if (memcmp(decomp_output, ctemplate[i].input,
3242 ctemplate[i].inlen)) {
3243 pr_err("alg: comp: compression failed: output differs: on test %d for %s\n",
3244 i + 1, algo);
3245 hexdump(decomp_output, dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08003246 ret = -EINVAL;
3247 goto out;
3248 }
3249 }
3250
3251 for (i = 0; i < dtcount; i++) {
Geert Uytterhoevenc79cf912009-03-29 15:44:19 +08003252 int ilen;
3253 unsigned int dlen = COMP_BUF_SIZE;
Herbert Xuda7f0332008-07-31 17:08:25 +08003254
Michael Schupikov22a81182018-10-07 13:58:10 +02003255 memset(decomp_output, 0, COMP_BUF_SIZE);
Herbert Xuda7f0332008-07-31 17:08:25 +08003256
3257 ilen = dtemplate[i].inlen;
3258 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
Mahipal Challa33607382018-04-11 20:28:32 +02003259 ilen, decomp_output, &dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08003260 if (ret) {
3261 printk(KERN_ERR "alg: comp: decompression failed "
3262 "on test %d for %s: ret=%d\n", i + 1, algo,
3263 -ret);
3264 goto out;
3265 }
3266
Geert Uytterhoevenb812eb02008-11-28 20:51:28 +08003267 if (dlen != dtemplate[i].outlen) {
3268 printk(KERN_ERR "alg: comp: Decompression test %d "
3269 "failed for %s: output len = %d\n", i + 1, algo,
3270 dlen);
3271 ret = -EINVAL;
3272 goto out;
3273 }
3274
Mahipal Challa33607382018-04-11 20:28:32 +02003275 if (memcmp(decomp_output, dtemplate[i].output, dlen)) {
Herbert Xuda7f0332008-07-31 17:08:25 +08003276 printk(KERN_ERR "alg: comp: Decompression test %d "
3277 "failed for %s\n", i + 1, algo);
Mahipal Challa33607382018-04-11 20:28:32 +02003278 hexdump(decomp_output, dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08003279 ret = -EINVAL;
3280 goto out;
3281 }
3282 }
3283
3284 ret = 0;
3285
3286out:
Mahipal Challa33607382018-04-11 20:28:32 +02003287 kfree(decomp_output);
3288 kfree(output);
Herbert Xuda7f0332008-07-31 17:08:25 +08003289 return ret;
3290}
3291
Eric Biggersb13b1e02017-02-24 15:46:59 -08003292static int test_acomp(struct crypto_acomp *tfm,
Mahipal Challa33607382018-04-11 20:28:32 +02003293 const struct comp_testvec *ctemplate,
Eric Biggersb13b1e02017-02-24 15:46:59 -08003294 const struct comp_testvec *dtemplate,
3295 int ctcount, int dtcount)
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003296{
3297 const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
3298 unsigned int i;
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003299 char *output, *decomp_out;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003300 int ret;
3301 struct scatterlist src, dst;
3302 struct acomp_req *req;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003303 struct crypto_wait wait;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003304
Eric Biggerseb095592016-11-23 10:24:35 -08003305 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3306 if (!output)
3307 return -ENOMEM;
3308
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003309 decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3310 if (!decomp_out) {
3311 kfree(output);
3312 return -ENOMEM;
3313 }
3314
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003315 for (i = 0; i < ctcount; i++) {
3316 unsigned int dlen = COMP_BUF_SIZE;
3317 int ilen = ctemplate[i].inlen;
Laura Abbott02608e02016-12-21 12:32:54 -08003318 void *input_vec;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003319
Eric Biggersd2110222016-12-30 14:12:00 -06003320 input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL);
Laura Abbott02608e02016-12-21 12:32:54 -08003321 if (!input_vec) {
3322 ret = -ENOMEM;
3323 goto out;
3324 }
3325
Eric Biggerseb095592016-11-23 10:24:35 -08003326 memset(output, 0, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003327 crypto_init_wait(&wait);
Laura Abbott02608e02016-12-21 12:32:54 -08003328 sg_init_one(&src, input_vec, ilen);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003329 sg_init_one(&dst, output, dlen);
3330
3331 req = acomp_request_alloc(tfm);
3332 if (!req) {
3333 pr_err("alg: acomp: request alloc failed for %s\n",
3334 algo);
Laura Abbott02608e02016-12-21 12:32:54 -08003335 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003336 ret = -ENOMEM;
3337 goto out;
3338 }
3339
3340 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3341 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003342 crypto_req_done, &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003343
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003344 ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003345 if (ret) {
3346 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
3347 i + 1, algo, -ret);
Laura Abbott02608e02016-12-21 12:32:54 -08003348 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003349 acomp_request_free(req);
3350 goto out;
3351 }
3352
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003353 ilen = req->dlen;
3354 dlen = COMP_BUF_SIZE;
3355 sg_init_one(&src, output, ilen);
3356 sg_init_one(&dst, decomp_out, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003357 crypto_init_wait(&wait);
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003358 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3359
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003360 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003361 if (ret) {
3362 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
3363 i + 1, algo, -ret);
3364 kfree(input_vec);
3365 acomp_request_free(req);
3366 goto out;
3367 }
3368
3369 if (req->dlen != ctemplate[i].inlen) {
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003370 pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
3371 i + 1, algo, req->dlen);
3372 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08003373 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003374 acomp_request_free(req);
3375 goto out;
3376 }
3377
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003378 if (memcmp(input_vec, decomp_out, req->dlen)) {
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003379 pr_err("alg: acomp: Compression test %d failed for %s\n",
3380 i + 1, algo);
3381 hexdump(output, req->dlen);
3382 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08003383 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003384 acomp_request_free(req);
3385 goto out;
3386 }
3387
Laura Abbott02608e02016-12-21 12:32:54 -08003388 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003389 acomp_request_free(req);
3390 }
3391
3392 for (i = 0; i < dtcount; i++) {
3393 unsigned int dlen = COMP_BUF_SIZE;
3394 int ilen = dtemplate[i].inlen;
Laura Abbott02608e02016-12-21 12:32:54 -08003395 void *input_vec;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003396
Eric Biggersd2110222016-12-30 14:12:00 -06003397 input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL);
Laura Abbott02608e02016-12-21 12:32:54 -08003398 if (!input_vec) {
3399 ret = -ENOMEM;
3400 goto out;
3401 }
3402
Eric Biggerseb095592016-11-23 10:24:35 -08003403 memset(output, 0, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003404 crypto_init_wait(&wait);
Laura Abbott02608e02016-12-21 12:32:54 -08003405 sg_init_one(&src, input_vec, ilen);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003406 sg_init_one(&dst, output, dlen);
3407
3408 req = acomp_request_alloc(tfm);
3409 if (!req) {
3410 pr_err("alg: acomp: request alloc failed for %s\n",
3411 algo);
Laura Abbott02608e02016-12-21 12:32:54 -08003412 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003413 ret = -ENOMEM;
3414 goto out;
3415 }
3416
3417 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3418 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003419 crypto_req_done, &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003420
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003421 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003422 if (ret) {
3423 pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
3424 i + 1, algo, -ret);
Laura Abbott02608e02016-12-21 12:32:54 -08003425 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003426 acomp_request_free(req);
3427 goto out;
3428 }
3429
3430 if (req->dlen != dtemplate[i].outlen) {
3431 pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
3432 i + 1, algo, req->dlen);
3433 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08003434 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003435 acomp_request_free(req);
3436 goto out;
3437 }
3438
3439 if (memcmp(output, dtemplate[i].output, req->dlen)) {
3440 pr_err("alg: acomp: Decompression test %d failed for %s\n",
3441 i + 1, algo);
3442 hexdump(output, req->dlen);
3443 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08003444 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003445 acomp_request_free(req);
3446 goto out;
3447 }
3448
Laura Abbott02608e02016-12-21 12:32:54 -08003449 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003450 acomp_request_free(req);
3451 }
3452
3453 ret = 0;
3454
3455out:
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003456 kfree(decomp_out);
Eric Biggerseb095592016-11-23 10:24:35 -08003457 kfree(output);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003458 return ret;
3459}
3460
Eric Biggersb13b1e02017-02-24 15:46:59 -08003461static int test_cprng(struct crypto_rng *tfm,
3462 const struct cprng_testvec *template,
Jarod Wilson7647d6c2009-05-04 19:44:50 +08003463 unsigned int tcount)
3464{
3465 const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
Felipe Contrerasfa4ef8a2009-10-27 19:04:42 +08003466 int err = 0, i, j, seedsize;
Jarod Wilson7647d6c2009-05-04 19:44:50 +08003467 u8 *seed;
3468 char result[32];
3469
3470 seedsize = crypto_rng_seedsize(tfm);
3471
3472 seed = kmalloc(seedsize, GFP_KERNEL);
3473 if (!seed) {
3474 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
3475 "for %s\n", algo);
3476 return -ENOMEM;
3477 }
3478
3479 for (i = 0; i < tcount; i++) {
3480 memset(result, 0, 32);
3481
3482 memcpy(seed, template[i].v, template[i].vlen);
3483 memcpy(seed + template[i].vlen, template[i].key,
3484 template[i].klen);
3485 memcpy(seed + template[i].vlen + template[i].klen,
3486 template[i].dt, template[i].dtlen);
3487
3488 err = crypto_rng_reset(tfm, seed, seedsize);
3489 if (err) {
3490 printk(KERN_ERR "alg: cprng: Failed to reset rng "
3491 "for %s\n", algo);
3492 goto out;
3493 }
3494
3495 for (j = 0; j < template[i].loops; j++) {
3496 err = crypto_rng_get_bytes(tfm, result,
3497 template[i].rlen);
Stephan Mueller19e60e12015-03-10 17:00:36 +01003498 if (err < 0) {
Jarod Wilson7647d6c2009-05-04 19:44:50 +08003499 printk(KERN_ERR "alg: cprng: Failed to obtain "
3500 "the correct amount of random data for "
Stephan Mueller19e60e12015-03-10 17:00:36 +01003501 "%s (requested %d)\n", algo,
3502 template[i].rlen);
Jarod Wilson7647d6c2009-05-04 19:44:50 +08003503 goto out;
3504 }
3505 }
3506
3507 err = memcmp(result, template[i].result,
3508 template[i].rlen);
3509 if (err) {
3510 printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
3511 i, algo);
3512 hexdump(result, template[i].rlen);
3513 err = -EINVAL;
3514 goto out;
3515 }
3516 }
3517
3518out:
3519 kfree(seed);
3520 return err;
3521}
3522
Herbert Xuda7f0332008-07-31 17:08:25 +08003523static int alg_test_cipher(const struct alg_test_desc *desc,
3524 const char *driver, u32 type, u32 mask)
3525{
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003526 const struct cipher_test_suite *suite = &desc->suite.cipher;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003527 struct crypto_cipher *tfm;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003528 int err;
Herbert Xuda7f0332008-07-31 17:08:25 +08003529
Herbert Xueed93e02016-11-22 20:08:31 +08003530 tfm = crypto_alloc_cipher(driver, type, mask);
Herbert Xuda7f0332008-07-31 17:08:25 +08003531 if (IS_ERR(tfm)) {
3532 printk(KERN_ERR "alg: cipher: Failed to load transform for "
3533 "%s: %ld\n", driver, PTR_ERR(tfm));
3534 return PTR_ERR(tfm);
3535 }
3536
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003537 err = test_cipher(tfm, ENCRYPT, suite->vecs, suite->count);
3538 if (!err)
3539 err = test_cipher(tfm, DECRYPT, suite->vecs, suite->count);
Herbert Xuda7f0332008-07-31 17:08:25 +08003540
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003541 crypto_free_cipher(tfm);
3542 return err;
3543}
3544
Herbert Xuda7f0332008-07-31 17:08:25 +08003545static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
3546 u32 type, u32 mask)
3547{
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003548 struct crypto_comp *comp;
3549 struct crypto_acomp *acomp;
Herbert Xuda7f0332008-07-31 17:08:25 +08003550 int err;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003551 u32 algo_type = type & CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
Herbert Xuda7f0332008-07-31 17:08:25 +08003552
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003553 if (algo_type == CRYPTO_ALG_TYPE_ACOMPRESS) {
3554 acomp = crypto_alloc_acomp(driver, type, mask);
3555 if (IS_ERR(acomp)) {
3556 pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
3557 driver, PTR_ERR(acomp));
3558 return PTR_ERR(acomp);
3559 }
3560 err = test_acomp(acomp, desc->suite.comp.comp.vecs,
3561 desc->suite.comp.decomp.vecs,
3562 desc->suite.comp.comp.count,
3563 desc->suite.comp.decomp.count);
3564 crypto_free_acomp(acomp);
3565 } else {
3566 comp = crypto_alloc_comp(driver, type, mask);
3567 if (IS_ERR(comp)) {
3568 pr_err("alg: comp: Failed to load transform for %s: %ld\n",
3569 driver, PTR_ERR(comp));
3570 return PTR_ERR(comp);
3571 }
3572
3573 err = test_comp(comp, desc->suite.comp.comp.vecs,
3574 desc->suite.comp.decomp.vecs,
3575 desc->suite.comp.comp.count,
3576 desc->suite.comp.decomp.count);
3577
3578 crypto_free_comp(comp);
Herbert Xuda7f0332008-07-31 17:08:25 +08003579 }
Herbert Xuda7f0332008-07-31 17:08:25 +08003580 return err;
3581}
3582
Herbert Xu8e3ee852008-11-07 14:58:52 +08003583static int alg_test_crc32c(const struct alg_test_desc *desc,
3584 const char *driver, u32 type, u32 mask)
3585{
3586 struct crypto_shash *tfm;
Eric Biggerscb9dde82019-01-10 12:17:55 -08003587 __le32 val;
Herbert Xu8e3ee852008-11-07 14:58:52 +08003588 int err;
3589
3590 err = alg_test_hash(desc, driver, type, mask);
3591 if (err)
Eric Biggerseb5e6732019-01-23 20:57:35 -08003592 return err;
Herbert Xu8e3ee852008-11-07 14:58:52 +08003593
Herbert Xueed93e02016-11-22 20:08:31 +08003594 tfm = crypto_alloc_shash(driver, type, mask);
Herbert Xu8e3ee852008-11-07 14:58:52 +08003595 if (IS_ERR(tfm)) {
Eric Biggerseb5e6732019-01-23 20:57:35 -08003596 if (PTR_ERR(tfm) == -ENOENT) {
3597 /*
3598 * This crc32c implementation is only available through
3599 * ahash API, not the shash API, so the remaining part
3600 * of the test is not applicable to it.
3601 */
3602 return 0;
3603 }
Herbert Xu8e3ee852008-11-07 14:58:52 +08003604 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
3605 "%ld\n", driver, PTR_ERR(tfm));
Eric Biggerseb5e6732019-01-23 20:57:35 -08003606 return PTR_ERR(tfm);
Herbert Xu8e3ee852008-11-07 14:58:52 +08003607 }
3608
3609 do {
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02003610 SHASH_DESC_ON_STACK(shash, tfm);
3611 u32 *ctx = (u32 *)shash_desc_ctx(shash);
Herbert Xu8e3ee852008-11-07 14:58:52 +08003612
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02003613 shash->tfm = tfm;
Herbert Xu8e3ee852008-11-07 14:58:52 +08003614
Eric Biggerscb9dde82019-01-10 12:17:55 -08003615 *ctx = 420553207;
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02003616 err = crypto_shash_final(shash, (u8 *)&val);
Herbert Xu8e3ee852008-11-07 14:58:52 +08003617 if (err) {
3618 printk(KERN_ERR "alg: crc32c: Operation failed for "
3619 "%s: %d\n", driver, err);
3620 break;
3621 }
3622
Eric Biggerscb9dde82019-01-10 12:17:55 -08003623 if (val != cpu_to_le32(~420553207)) {
3624 pr_err("alg: crc32c: Test failed for %s: %u\n",
3625 driver, le32_to_cpu(val));
Herbert Xu8e3ee852008-11-07 14:58:52 +08003626 err = -EINVAL;
3627 }
3628 } while (0);
3629
3630 crypto_free_shash(tfm);
3631
Herbert Xu8e3ee852008-11-07 14:58:52 +08003632 return err;
3633}
3634
Jarod Wilson7647d6c2009-05-04 19:44:50 +08003635static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
3636 u32 type, u32 mask)
3637{
3638 struct crypto_rng *rng;
3639 int err;
3640
Herbert Xueed93e02016-11-22 20:08:31 +08003641 rng = crypto_alloc_rng(driver, type, mask);
Jarod Wilson7647d6c2009-05-04 19:44:50 +08003642 if (IS_ERR(rng)) {
3643 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
3644 "%ld\n", driver, PTR_ERR(rng));
3645 return PTR_ERR(rng);
3646 }
3647
3648 err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
3649
3650 crypto_free_rng(rng);
3651
3652 return err;
3653}
3654
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003655
Eric Biggersb13b1e02017-02-24 15:46:59 -08003656static int drbg_cavs_test(const struct drbg_testvec *test, int pr,
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003657 const char *driver, u32 type, u32 mask)
3658{
3659 int ret = -EAGAIN;
3660 struct crypto_rng *drng;
3661 struct drbg_test_data test_data;
3662 struct drbg_string addtl, pers, testentropy;
3663 unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
3664
3665 if (!buf)
3666 return -ENOMEM;
3667
Herbert Xueed93e02016-11-22 20:08:31 +08003668 drng = crypto_alloc_rng(driver, type, mask);
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003669 if (IS_ERR(drng)) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04003670 printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003671 "%s\n", driver);
Waiman Long453431a2020-08-06 23:18:13 -07003672 kfree_sensitive(buf);
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003673 return -ENOMEM;
3674 }
3675
3676 test_data.testentropy = &testentropy;
3677 drbg_string_fill(&testentropy, test->entropy, test->entropylen);
3678 drbg_string_fill(&pers, test->pers, test->perslen);
3679 ret = crypto_drbg_reset_test(drng, &pers, &test_data);
3680 if (ret) {
3681 printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
3682 goto outbuf;
3683 }
3684
3685 drbg_string_fill(&addtl, test->addtla, test->addtllen);
3686 if (pr) {
3687 drbg_string_fill(&testentropy, test->entpra, test->entprlen);
3688 ret = crypto_drbg_get_bytes_addtl_test(drng,
3689 buf, test->expectedlen, &addtl, &test_data);
3690 } else {
3691 ret = crypto_drbg_get_bytes_addtl(drng,
3692 buf, test->expectedlen, &addtl);
3693 }
Stephan Mueller19e60e12015-03-10 17:00:36 +01003694 if (ret < 0) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04003695 printk(KERN_ERR "alg: drbg: could not obtain random data for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003696 "driver %s\n", driver);
3697 goto outbuf;
3698 }
3699
3700 drbg_string_fill(&addtl, test->addtlb, test->addtllen);
3701 if (pr) {
3702 drbg_string_fill(&testentropy, test->entprb, test->entprlen);
3703 ret = crypto_drbg_get_bytes_addtl_test(drng,
3704 buf, test->expectedlen, &addtl, &test_data);
3705 } else {
3706 ret = crypto_drbg_get_bytes_addtl(drng,
3707 buf, test->expectedlen, &addtl);
3708 }
Stephan Mueller19e60e12015-03-10 17:00:36 +01003709 if (ret < 0) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04003710 printk(KERN_ERR "alg: drbg: could not obtain random data for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003711 "driver %s\n", driver);
3712 goto outbuf;
3713 }
3714
3715 ret = memcmp(test->expected, buf, test->expectedlen);
3716
3717outbuf:
3718 crypto_free_rng(drng);
Waiman Long453431a2020-08-06 23:18:13 -07003719 kfree_sensitive(buf);
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003720 return ret;
3721}
3722
3723
3724static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
3725 u32 type, u32 mask)
3726{
3727 int err = 0;
3728 int pr = 0;
3729 int i = 0;
Eric Biggersb13b1e02017-02-24 15:46:59 -08003730 const struct drbg_testvec *template = desc->suite.drbg.vecs;
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003731 unsigned int tcount = desc->suite.drbg.count;
3732
3733 if (0 == memcmp(driver, "drbg_pr_", 8))
3734 pr = 1;
3735
3736 for (i = 0; i < tcount; i++) {
3737 err = drbg_cavs_test(&template[i], pr, driver, type, mask);
3738 if (err) {
3739 printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
3740 i, driver);
3741 err = -EINVAL;
3742 break;
3743 }
3744 }
3745 return err;
3746
3747}
3748
Eric Biggersb13b1e02017-02-24 15:46:59 -08003749static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003750 const char *alg)
3751{
3752 struct kpp_request *req;
3753 void *input_buf = NULL;
3754 void *output_buf = NULL;
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003755 void *a_public = NULL;
3756 void *a_ss = NULL;
3757 void *shared_secret = NULL;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003758 struct crypto_wait wait;
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003759 unsigned int out_len_max;
3760 int err = -ENOMEM;
3761 struct scatterlist src, dst;
3762
3763 req = kpp_request_alloc(tfm, GFP_KERNEL);
3764 if (!req)
3765 return err;
3766
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003767 crypto_init_wait(&wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003768
3769 err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
3770 if (err < 0)
3771 goto free_req;
3772
3773 out_len_max = crypto_kpp_maxsize(tfm);
3774 output_buf = kzalloc(out_len_max, GFP_KERNEL);
3775 if (!output_buf) {
3776 err = -ENOMEM;
3777 goto free_req;
3778 }
3779
3780 /* Use appropriate parameter as base */
3781 kpp_request_set_input(req, NULL, 0);
3782 sg_init_one(&dst, output_buf, out_len_max);
3783 kpp_request_set_output(req, &dst, out_len_max);
3784 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003785 crypto_req_done, &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003786
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003787 /* Compute party A's public key */
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003788 err = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003789 if (err) {
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003790 pr_err("alg: %s: Party A: generate public key test failed. err %d\n",
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003791 alg, err);
3792 goto free_output;
3793 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003794
3795 if (vec->genkey) {
3796 /* Save party A's public key */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05003797 a_public = kmemdup(sg_virt(req->dst), out_len_max, GFP_KERNEL);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003798 if (!a_public) {
3799 err = -ENOMEM;
3800 goto free_output;
3801 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003802 } else {
3803 /* Verify calculated public key */
3804 if (memcmp(vec->expected_a_public, sg_virt(req->dst),
3805 vec->expected_a_public_size)) {
3806 pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n",
3807 alg);
3808 err = -EINVAL;
3809 goto free_output;
3810 }
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003811 }
3812
3813 /* Calculate shared secret key by using counter part (b) public key. */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05003814 input_buf = kmemdup(vec->b_public, vec->b_public_size, GFP_KERNEL);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003815 if (!input_buf) {
3816 err = -ENOMEM;
3817 goto free_output;
3818 }
3819
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003820 sg_init_one(&src, input_buf, vec->b_public_size);
3821 sg_init_one(&dst, output_buf, out_len_max);
3822 kpp_request_set_input(req, &src, vec->b_public_size);
3823 kpp_request_set_output(req, &dst, out_len_max);
3824 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003825 crypto_req_done, &wait);
3826 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003827 if (err) {
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003828 pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n",
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003829 alg, err);
3830 goto free_all;
3831 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003832
3833 if (vec->genkey) {
3834 /* Save the shared secret obtained by party A */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05003835 a_ss = kmemdup(sg_virt(req->dst), vec->expected_ss_size, GFP_KERNEL);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003836 if (!a_ss) {
3837 err = -ENOMEM;
3838 goto free_all;
3839 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003840
3841 /*
3842 * Calculate party B's shared secret by using party A's
3843 * public key.
3844 */
3845 err = crypto_kpp_set_secret(tfm, vec->b_secret,
3846 vec->b_secret_size);
3847 if (err < 0)
3848 goto free_all;
3849
3850 sg_init_one(&src, a_public, vec->expected_a_public_size);
3851 sg_init_one(&dst, output_buf, out_len_max);
3852 kpp_request_set_input(req, &src, vec->expected_a_public_size);
3853 kpp_request_set_output(req, &dst, out_len_max);
3854 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003855 crypto_req_done, &wait);
3856 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req),
3857 &wait);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003858 if (err) {
3859 pr_err("alg: %s: Party B: compute shared secret failed. err %d\n",
3860 alg, err);
3861 goto free_all;
3862 }
3863
3864 shared_secret = a_ss;
3865 } else {
3866 shared_secret = (void *)vec->expected_ss;
3867 }
3868
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003869 /*
3870 * verify shared secret from which the user will derive
3871 * secret key by executing whatever hash it has chosen
3872 */
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003873 if (memcmp(shared_secret, sg_virt(req->dst),
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003874 vec->expected_ss_size)) {
3875 pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
3876 alg);
3877 err = -EINVAL;
3878 }
3879
3880free_all:
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003881 kfree(a_ss);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003882 kfree(input_buf);
3883free_output:
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003884 kfree(a_public);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003885 kfree(output_buf);
3886free_req:
3887 kpp_request_free(req);
3888 return err;
3889}
3890
3891static int test_kpp(struct crypto_kpp *tfm, const char *alg,
Eric Biggersb13b1e02017-02-24 15:46:59 -08003892 const struct kpp_testvec *vecs, unsigned int tcount)
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003893{
3894 int ret, i;
3895
3896 for (i = 0; i < tcount; i++) {
3897 ret = do_test_kpp(tfm, vecs++, alg);
3898 if (ret) {
3899 pr_err("alg: %s: test failed on vector %d, err=%d\n",
3900 alg, i + 1, ret);
3901 return ret;
3902 }
3903 }
3904 return 0;
3905}
3906
3907static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
3908 u32 type, u32 mask)
3909{
3910 struct crypto_kpp *tfm;
3911 int err = 0;
3912
Herbert Xueed93e02016-11-22 20:08:31 +08003913 tfm = crypto_alloc_kpp(driver, type, mask);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003914 if (IS_ERR(tfm)) {
3915 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
3916 driver, PTR_ERR(tfm));
3917 return PTR_ERR(tfm);
3918 }
3919 if (desc->suite.kpp.vecs)
3920 err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
3921 desc->suite.kpp.count);
3922
3923 crypto_free_kpp(tfm);
3924 return err;
3925}
3926
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003927static u8 *test_pack_u32(u8 *dst, u32 val)
3928{
3929 memcpy(dst, &val, sizeof(val));
3930 return dst + sizeof(val);
3931}
3932
Herbert Xu50d2b6432016-06-29 19:32:20 +08003933static int test_akcipher_one(struct crypto_akcipher *tfm,
Eric Biggersb13b1e02017-02-24 15:46:59 -08003934 const struct akcipher_testvec *vecs)
Tadeusz Struk946cc462015-06-16 10:31:06 -07003935{
Herbert Xudf27b262016-05-05 16:42:49 +08003936 char *xbuf[XBUFSIZE];
Tadeusz Struk946cc462015-06-16 10:31:06 -07003937 struct akcipher_request *req;
3938 void *outbuf_enc = NULL;
3939 void *outbuf_dec = NULL;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003940 struct crypto_wait wait;
Tadeusz Struk946cc462015-06-16 10:31:06 -07003941 unsigned int out_len_max, out_len = 0;
3942 int err = -ENOMEM;
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03003943 struct scatterlist src, dst, src_tab[3];
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003944 const char *m, *c;
3945 unsigned int m_size, c_size;
3946 const char *op;
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003947 u8 *key, *ptr;
Tadeusz Struk946cc462015-06-16 10:31:06 -07003948
Herbert Xudf27b262016-05-05 16:42:49 +08003949 if (testmgr_alloc_buf(xbuf))
3950 return err;
3951
Tadeusz Struk946cc462015-06-16 10:31:06 -07003952 req = akcipher_request_alloc(tfm, GFP_KERNEL);
3953 if (!req)
Herbert Xudf27b262016-05-05 16:42:49 +08003954 goto free_xbuf;
Tadeusz Struk946cc462015-06-16 10:31:06 -07003955
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003956 crypto_init_wait(&wait);
Tadeusz Struk22287b02015-10-08 09:26:55 -07003957
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003958 key = kmalloc(vecs->key_len + sizeof(u32) * 2 + vecs->param_len,
3959 GFP_KERNEL);
3960 if (!key)
Tianjia Zhang2b403862020-09-21 00:20:59 +08003961 goto free_req;
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003962 memcpy(key, vecs->key, vecs->key_len);
3963 ptr = key + vecs->key_len;
3964 ptr = test_pack_u32(ptr, vecs->algo);
3965 ptr = test_pack_u32(ptr, vecs->param_len);
3966 memcpy(ptr, vecs->params, vecs->param_len);
3967
Tadeusz Struk22287b02015-10-08 09:26:55 -07003968 if (vecs->public_key_vec)
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003969 err = crypto_akcipher_set_pub_key(tfm, key, vecs->key_len);
Tadeusz Struk22287b02015-10-08 09:26:55 -07003970 else
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003971 err = crypto_akcipher_set_priv_key(tfm, key, vecs->key_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003972 if (err)
Tianjia Zhang2b403862020-09-21 00:20:59 +08003973 goto free_key;
Tadeusz Struk946cc462015-06-16 10:31:06 -07003974
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003975 /*
3976 * First run test which do not require a private key, such as
3977 * encrypt or verify.
3978 */
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03003979 err = -ENOMEM;
3980 out_len_max = crypto_akcipher_maxsize(tfm);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003981 outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
3982 if (!outbuf_enc)
Tianjia Zhang2b403862020-09-21 00:20:59 +08003983 goto free_key;
Tadeusz Struk946cc462015-06-16 10:31:06 -07003984
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003985 if (!vecs->siggen_sigver_test) {
3986 m = vecs->m;
3987 m_size = vecs->m_size;
3988 c = vecs->c;
3989 c_size = vecs->c_size;
3990 op = "encrypt";
3991 } else {
3992 /* Swap args so we could keep plaintext (digest)
3993 * in vecs->m, and cooked signature in vecs->c.
3994 */
3995 m = vecs->c; /* signature */
3996 m_size = vecs->c_size;
3997 c = vecs->m; /* digest */
3998 c_size = vecs->m_size;
3999 op = "verify";
4000 }
Herbert Xudf27b262016-05-05 16:42:49 +08004001
Tianjia Zhang2b403862020-09-21 00:20:59 +08004002 err = -E2BIG;
Vitaly Chikunov0507de92019-01-07 20:54:27 +03004003 if (WARN_ON(m_size > PAGE_SIZE))
4004 goto free_all;
4005 memcpy(xbuf[0], m, m_size);
Herbert Xudf27b262016-05-05 16:42:49 +08004006
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03004007 sg_init_table(src_tab, 3);
Herbert Xudf27b262016-05-05 16:42:49 +08004008 sg_set_buf(&src_tab[0], xbuf[0], 8);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03004009 sg_set_buf(&src_tab[1], xbuf[0] + 8, m_size - 8);
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03004010 if (vecs->siggen_sigver_test) {
4011 if (WARN_ON(c_size > PAGE_SIZE))
4012 goto free_all;
4013 memcpy(xbuf[1], c, c_size);
4014 sg_set_buf(&src_tab[2], xbuf[1], c_size);
4015 akcipher_request_set_crypt(req, src_tab, NULL, m_size, c_size);
4016 } else {
4017 sg_init_one(&dst, outbuf_enc, out_len_max);
4018 akcipher_request_set_crypt(req, src_tab, &dst, m_size,
4019 out_len_max);
4020 }
Tadeusz Struk946cc462015-06-16 10:31:06 -07004021 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01004022 crypto_req_done, &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07004023
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01004024 err = crypto_wait_req(vecs->siggen_sigver_test ?
Vitaly Chikunov0507de92019-01-07 20:54:27 +03004025 /* Run asymmetric signature verification */
4026 crypto_akcipher_verify(req) :
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01004027 /* Run asymmetric encrypt */
4028 crypto_akcipher_encrypt(req), &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07004029 if (err) {
Vitaly Chikunov0507de92019-01-07 20:54:27 +03004030 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
Tadeusz Struk946cc462015-06-16 10:31:06 -07004031 goto free_all;
4032 }
Tianjia Zhanga1f62c22020-09-21 00:20:58 +08004033 if (!vecs->siggen_sigver_test && c) {
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03004034 if (req->dst_len != c_size) {
4035 pr_err("alg: akcipher: %s test failed. Invalid output len\n",
4036 op);
4037 err = -EINVAL;
4038 goto free_all;
4039 }
4040 /* verify that encrypted message is equal to expected */
4041 if (memcmp(c, outbuf_enc, c_size) != 0) {
4042 pr_err("alg: akcipher: %s test failed. Invalid output\n",
4043 op);
4044 hexdump(outbuf_enc, c_size);
4045 err = -EINVAL;
4046 goto free_all;
4047 }
Tadeusz Struk946cc462015-06-16 10:31:06 -07004048 }
Vitaly Chikunov0507de92019-01-07 20:54:27 +03004049
4050 /*
4051 * Don't invoke (decrypt or sign) test which require a private key
4052 * for vectors with only a public key.
4053 */
Tadeusz Struk946cc462015-06-16 10:31:06 -07004054 if (vecs->public_key_vec) {
4055 err = 0;
4056 goto free_all;
4057 }
4058 outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
4059 if (!outbuf_dec) {
4060 err = -ENOMEM;
4061 goto free_all;
4062 }
Herbert Xudf27b262016-05-05 16:42:49 +08004063
Tianjia Zhanga1f62c22020-09-21 00:20:58 +08004064 if (!vecs->siggen_sigver_test && !c) {
4065 c = outbuf_enc;
4066 c_size = req->dst_len;
4067 }
4068
Tianjia Zhang2b403862020-09-21 00:20:59 +08004069 err = -E2BIG;
Vitaly Chikunov0507de92019-01-07 20:54:27 +03004070 op = vecs->siggen_sigver_test ? "sign" : "decrypt";
4071 if (WARN_ON(c_size > PAGE_SIZE))
Herbert Xudf27b262016-05-05 16:42:49 +08004072 goto free_all;
Vitaly Chikunov0507de92019-01-07 20:54:27 +03004073 memcpy(xbuf[0], c, c_size);
Herbert Xudf27b262016-05-05 16:42:49 +08004074
Vitaly Chikunov0507de92019-01-07 20:54:27 +03004075 sg_init_one(&src, xbuf[0], c_size);
Tadeusz Struk22287b02015-10-08 09:26:55 -07004076 sg_init_one(&dst, outbuf_dec, out_len_max);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01004077 crypto_init_wait(&wait);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03004078 akcipher_request_set_crypt(req, &src, &dst, c_size, out_len_max);
Tadeusz Struk946cc462015-06-16 10:31:06 -07004079
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01004080 err = crypto_wait_req(vecs->siggen_sigver_test ?
Vitaly Chikunov0507de92019-01-07 20:54:27 +03004081 /* Run asymmetric signature generation */
4082 crypto_akcipher_sign(req) :
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01004083 /* Run asymmetric decrypt */
4084 crypto_akcipher_decrypt(req), &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07004085 if (err) {
Vitaly Chikunov0507de92019-01-07 20:54:27 +03004086 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
Tadeusz Struk946cc462015-06-16 10:31:06 -07004087 goto free_all;
4088 }
4089 out_len = req->dst_len;
Vitaly Chikunov0507de92019-01-07 20:54:27 +03004090 if (out_len < m_size) {
4091 pr_err("alg: akcipher: %s test failed. Invalid output len %u\n",
4092 op, out_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07004093 err = -EINVAL;
4094 goto free_all;
4095 }
4096 /* verify that decrypted message is equal to the original msg */
Vitaly Chikunov0507de92019-01-07 20:54:27 +03004097 if (memchr_inv(outbuf_dec, 0, out_len - m_size) ||
4098 memcmp(m, outbuf_dec + out_len - m_size, m_size)) {
4099 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
Herbert Xu50d2b6432016-06-29 19:32:20 +08004100 hexdump(outbuf_dec, out_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07004101 err = -EINVAL;
4102 }
4103free_all:
4104 kfree(outbuf_dec);
4105 kfree(outbuf_enc);
Tianjia Zhang2b403862020-09-21 00:20:59 +08004106free_key:
4107 kfree(key);
Tadeusz Struk946cc462015-06-16 10:31:06 -07004108free_req:
4109 akcipher_request_free(req);
Herbert Xudf27b262016-05-05 16:42:49 +08004110free_xbuf:
4111 testmgr_free_buf(xbuf);
Tadeusz Struk946cc462015-06-16 10:31:06 -07004112 return err;
4113}
4114
Herbert Xu50d2b6432016-06-29 19:32:20 +08004115static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
Eric Biggersb13b1e02017-02-24 15:46:59 -08004116 const struct akcipher_testvec *vecs,
4117 unsigned int tcount)
Tadeusz Struk946cc462015-06-16 10:31:06 -07004118{
Herbert Xu15226e42016-07-18 18:20:10 +08004119 const char *algo =
4120 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
Tadeusz Struk946cc462015-06-16 10:31:06 -07004121 int ret, i;
4122
4123 for (i = 0; i < tcount; i++) {
Herbert Xu50d2b6432016-06-29 19:32:20 +08004124 ret = test_akcipher_one(tfm, vecs++);
4125 if (!ret)
4126 continue;
4127
Herbert Xu15226e42016-07-18 18:20:10 +08004128 pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
4129 i + 1, algo, ret);
Herbert Xu50d2b6432016-06-29 19:32:20 +08004130 return ret;
Tadeusz Struk946cc462015-06-16 10:31:06 -07004131 }
4132 return 0;
4133}
4134
Tadeusz Struk946cc462015-06-16 10:31:06 -07004135static int alg_test_akcipher(const struct alg_test_desc *desc,
4136 const char *driver, u32 type, u32 mask)
4137{
4138 struct crypto_akcipher *tfm;
4139 int err = 0;
4140
Herbert Xueed93e02016-11-22 20:08:31 +08004141 tfm = crypto_alloc_akcipher(driver, type, mask);
Tadeusz Struk946cc462015-06-16 10:31:06 -07004142 if (IS_ERR(tfm)) {
4143 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
4144 driver, PTR_ERR(tfm));
4145 return PTR_ERR(tfm);
4146 }
4147 if (desc->suite.akcipher.vecs)
4148 err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
4149 desc->suite.akcipher.count);
4150
4151 crypto_free_akcipher(tfm);
4152 return err;
4153}
4154
Youquan, Song863b5572009-12-23 19:45:20 +08004155static int alg_test_null(const struct alg_test_desc *desc,
4156 const char *driver, u32 type, u32 mask)
4157{
4158 return 0;
4159}
4160
Eric Biggers49763fc2019-12-01 13:53:30 -08004161#define ____VECS(tv) .vecs = tv, .count = ARRAY_SIZE(tv)
4162#define __VECS(tv) { ____VECS(tv) }
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004163
Herbert Xuda7f0332008-07-31 17:08:25 +08004164/* Please keep this list sorted by algorithm name. */
4165static const struct alg_test_desc alg_test_descs[] = {
4166 {
Eric Biggers059c2a42018-11-16 17:26:31 -08004167 .alg = "adiantum(xchacha12,aes)",
Eric Biggersd435e102019-04-11 21:57:40 -07004168 .generic_driver = "adiantum(xchacha12-generic,aes-generic,nhpoly1305-generic)",
Eric Biggers059c2a42018-11-16 17:26:31 -08004169 .test = alg_test_skcipher,
4170 .suite = {
4171 .cipher = __VECS(adiantum_xchacha12_aes_tv_template)
4172 },
4173 }, {
4174 .alg = "adiantum(xchacha20,aes)",
Eric Biggersd435e102019-04-11 21:57:40 -07004175 .generic_driver = "adiantum(xchacha20-generic,aes-generic,nhpoly1305-generic)",
Eric Biggers059c2a42018-11-16 17:26:31 -08004176 .test = alg_test_skcipher,
4177 .suite = {
4178 .cipher = __VECS(adiantum_xchacha20_aes_tv_template)
4179 },
4180 }, {
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02004181 .alg = "aegis128",
4182 .test = alg_test_aead,
4183 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004184 .aead = __VECS(aegis128_tv_template)
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02004185 }
4186 }, {
Jarod Wilsone08ca2d2009-05-04 19:46:29 +08004187 .alg = "ansi_cprng",
4188 .test = alg_test_cprng,
4189 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004190 .cprng = __VECS(ansi_cprng_aes_tv_template)
Jarod Wilsone08ca2d2009-05-04 19:46:29 +08004191 }
4192 }, {
Horia Geantabca4feb2014-03-14 17:46:51 +02004193 .alg = "authenc(hmac(md5),ecb(cipher_null))",
4194 .test = alg_test_aead,
Horia Geantabca4feb2014-03-14 17:46:51 +02004195 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004196 .aead = __VECS(hmac_md5_ecb_cipher_null_tv_template)
Horia Geantabca4feb2014-03-14 17:46:51 +02004197 }
4198 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004199 .alg = "authenc(hmac(sha1),cbc(aes))",
Horia Geantae46e9a42012-07-03 19:16:54 +03004200 .test = alg_test_aead,
Herbert Xubcf741c2017-06-28 19:09:07 +08004201 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03004202 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004203 .aead = __VECS(hmac_sha1_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304204 }
4205 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004206 .alg = "authenc(hmac(sha1),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304207 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304208 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004209 .aead = __VECS(hmac_sha1_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304210 }
4211 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004212 .alg = "authenc(hmac(sha1),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304213 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01004214 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304215 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004216 .aead = __VECS(hmac_sha1_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03004217 }
4218 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01004219 .alg = "authenc(hmac(sha1),ctr(aes))",
4220 .test = alg_test_null,
4221 .fips_allowed = 1,
4222 }, {
Horia Geantabca4feb2014-03-14 17:46:51 +02004223 .alg = "authenc(hmac(sha1),ecb(cipher_null))",
4224 .test = alg_test_aead,
Horia Geantabca4feb2014-03-14 17:46:51 +02004225 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004226 .aead = __VECS(hmac_sha1_ecb_cipher_null_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304227 }
4228 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01004229 .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
4230 .test = alg_test_null,
4231 .fips_allowed = 1,
4232 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004233 .alg = "authenc(hmac(sha224),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304234 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304235 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004236 .aead = __VECS(hmac_sha224_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304237 }
4238 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004239 .alg = "authenc(hmac(sha224),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304240 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01004241 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304242 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004243 .aead = __VECS(hmac_sha224_des3_ede_cbc_tv_temp)
Horia Geantabca4feb2014-03-14 17:46:51 +02004244 }
4245 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004246 .alg = "authenc(hmac(sha256),cbc(aes))",
Horia Geantae46e9a42012-07-03 19:16:54 +03004247 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01004248 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03004249 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004250 .aead = __VECS(hmac_sha256_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304251 }
4252 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004253 .alg = "authenc(hmac(sha256),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304254 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304255 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004256 .aead = __VECS(hmac_sha256_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304257 }
4258 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004259 .alg = "authenc(hmac(sha256),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304260 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01004261 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304262 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004263 .aead = __VECS(hmac_sha256_des3_ede_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304264 }
4265 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01004266 .alg = "authenc(hmac(sha256),ctr(aes))",
4267 .test = alg_test_null,
4268 .fips_allowed = 1,
4269 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01004270 .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
4271 .test = alg_test_null,
4272 .fips_allowed = 1,
4273 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004274 .alg = "authenc(hmac(sha384),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304275 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304276 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004277 .aead = __VECS(hmac_sha384_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304278 }
4279 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004280 .alg = "authenc(hmac(sha384),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304281 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01004282 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304283 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004284 .aead = __VECS(hmac_sha384_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03004285 }
4286 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01004287 .alg = "authenc(hmac(sha384),ctr(aes))",
4288 .test = alg_test_null,
4289 .fips_allowed = 1,
4290 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01004291 .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
4292 .test = alg_test_null,
4293 .fips_allowed = 1,
4294 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004295 .alg = "authenc(hmac(sha512),cbc(aes))",
Marcus Meissnered1afac2016-02-05 14:23:33 +01004296 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03004297 .test = alg_test_aead,
Horia Geantae46e9a42012-07-03 19:16:54 +03004298 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004299 .aead = __VECS(hmac_sha512_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304300 }
4301 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004302 .alg = "authenc(hmac(sha512),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304303 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304304 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004305 .aead = __VECS(hmac_sha512_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304306 }
4307 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004308 .alg = "authenc(hmac(sha512),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304309 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01004310 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304311 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004312 .aead = __VECS(hmac_sha512_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03004313 }
4314 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01004315 .alg = "authenc(hmac(sha512),ctr(aes))",
4316 .test = alg_test_null,
4317 .fips_allowed = 1,
4318 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01004319 .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
4320 .test = alg_test_null,
4321 .fips_allowed = 1,
4322 }, {
David Sterbaa1afe272019-10-24 18:28:32 +02004323 .alg = "blake2b-160",
4324 .test = alg_test_hash,
4325 .fips_allowed = 0,
4326 .suite = {
4327 .hash = __VECS(blake2b_160_tv_template)
4328 }
4329 }, {
4330 .alg = "blake2b-256",
4331 .test = alg_test_hash,
4332 .fips_allowed = 0,
4333 .suite = {
4334 .hash = __VECS(blake2b_256_tv_template)
4335 }
4336 }, {
4337 .alg = "blake2b-384",
4338 .test = alg_test_hash,
4339 .fips_allowed = 0,
4340 .suite = {
4341 .hash = __VECS(blake2b_384_tv_template)
4342 }
4343 }, {
4344 .alg = "blake2b-512",
4345 .test = alg_test_hash,
4346 .fips_allowed = 0,
4347 .suite = {
4348 .hash = __VECS(blake2b_512_tv_template)
4349 }
4350 }, {
Ard Biesheuvel17e1df62019-11-08 13:22:29 +01004351 .alg = "blake2s-128",
4352 .test = alg_test_hash,
4353 .suite = {
4354 .hash = __VECS(blakes2s_128_tv_template)
4355 }
4356 }, {
4357 .alg = "blake2s-160",
4358 .test = alg_test_hash,
4359 .suite = {
4360 .hash = __VECS(blakes2s_160_tv_template)
4361 }
4362 }, {
4363 .alg = "blake2s-224",
4364 .test = alg_test_hash,
4365 .suite = {
4366 .hash = __VECS(blakes2s_224_tv_template)
4367 }
4368 }, {
4369 .alg = "blake2s-256",
4370 .test = alg_test_hash,
4371 .suite = {
4372 .hash = __VECS(blakes2s_256_tv_template)
4373 }
4374 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004375 .alg = "cbc(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004376 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004377 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004378 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004379 .cipher = __VECS(aes_cbc_tv_template)
4380 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004381 }, {
4382 .alg = "cbc(anubis)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004383 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004384 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004385 .cipher = __VECS(anubis_cbc_tv_template)
4386 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004387 }, {
4388 .alg = "cbc(blowfish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004389 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004390 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004391 .cipher = __VECS(bf_cbc_tv_template)
4392 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004393 }, {
4394 .alg = "cbc(camellia)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004395 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004396 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004397 .cipher = __VECS(camellia_cbc_tv_template)
4398 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004399 }, {
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02004400 .alg = "cbc(cast5)",
4401 .test = alg_test_skcipher,
4402 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004403 .cipher = __VECS(cast5_cbc_tv_template)
4404 },
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02004405 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004406 .alg = "cbc(cast6)",
4407 .test = alg_test_skcipher,
4408 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004409 .cipher = __VECS(cast6_cbc_tv_template)
4410 },
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004411 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004412 .alg = "cbc(des)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004413 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004414 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004415 .cipher = __VECS(des_cbc_tv_template)
4416 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004417 }, {
4418 .alg = "cbc(des3_ede)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004419 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004420 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004421 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004422 .cipher = __VECS(des3_ede_cbc_tv_template)
4423 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004424 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01004425 /* Same as cbc(aes) except the key is stored in
4426 * hardware secure memory which we reference by index
4427 */
4428 .alg = "cbc(paes)",
4429 .test = alg_test_null,
4430 .fips_allowed = 1,
4431 }, {
Gilad Ben-Yosseff0372c02019-04-18 16:38:36 +03004432 /* Same as cbc(sm4) except the key is stored in
4433 * hardware secure memory which we reference by index
4434 */
4435 .alg = "cbc(psm4)",
4436 .test = alg_test_null,
4437 }, {
Jussi Kivilinna9d259172011-10-18 00:02:53 +03004438 .alg = "cbc(serpent)",
4439 .test = alg_test_skcipher,
4440 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004441 .cipher = __VECS(serpent_cbc_tv_template)
4442 },
Jussi Kivilinna9d259172011-10-18 00:02:53 +03004443 }, {
Gilad Ben-Yossef95ba5972018-09-20 14:18:38 +01004444 .alg = "cbc(sm4)",
4445 .test = alg_test_skcipher,
4446 .suite = {
4447 .cipher = __VECS(sm4_cbc_tv_template)
4448 }
4449 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004450 .alg = "cbc(twofish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004451 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004452 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004453 .cipher = __VECS(tf_cbc_tv_template)
4454 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004455 }, {
Harald Freudenbergerc7ff8572020-01-22 14:43:23 +01004456#if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
4457 .alg = "cbc-paes-s390",
4458 .fips_allowed = 1,
4459 .test = alg_test_skcipher,
4460 .suite = {
4461 .cipher = __VECS(aes_cbc_tv_template)
4462 }
4463 }, {
4464#endif
Ard Biesheuvel092acf02017-02-03 14:49:35 +00004465 .alg = "cbcmac(aes)",
4466 .fips_allowed = 1,
4467 .test = alg_test_hash,
4468 .suite = {
4469 .hash = __VECS(aes_cbcmac_tv_template)
4470 }
4471 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004472 .alg = "ccm(aes)",
Eric Biggers40153b12019-04-11 21:57:41 -07004473 .generic_driver = "ccm_base(ctr(aes-generic),cbcmac(aes-generic))",
Herbert Xuda7f0332008-07-31 17:08:25 +08004474 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004475 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004476 .suite = {
Eric Biggers49763fc2019-12-01 13:53:30 -08004477 .aead = {
4478 ____VECS(aes_ccm_tv_template),
4479 .einval_allowed = 1,
4480 }
Herbert Xuda7f0332008-07-31 17:08:25 +08004481 }
4482 }, {
Dmitry Eremin-Solenikov7da66672018-10-20 02:01:53 +03004483 .alg = "cfb(aes)",
4484 .test = alg_test_skcipher,
4485 .fips_allowed = 1,
4486 .suite = {
4487 .cipher = __VECS(aes_cfb_tv_template)
4488 },
4489 }, {
Pascal van Leeuwena06b15b2019-09-13 11:10:39 +02004490 .alg = "cfb(sm4)",
4491 .test = alg_test_skcipher,
4492 .suite = {
4493 .cipher = __VECS(sm4_cfb_tv_template)
4494 }
4495 }, {
Martin Willi3590ebf2015-06-01 13:43:57 +02004496 .alg = "chacha20",
4497 .test = alg_test_skcipher,
4498 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004499 .cipher = __VECS(chacha20_tv_template)
4500 },
Martin Willi3590ebf2015-06-01 13:43:57 +02004501 }, {
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03004502 .alg = "cmac(aes)",
Stephan Mueller8f183752015-08-19 08:42:07 +02004503 .fips_allowed = 1,
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03004504 .test = alg_test_hash,
4505 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004506 .hash = __VECS(aes_cmac128_tv_template)
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03004507 }
4508 }, {
4509 .alg = "cmac(des3_ede)",
Stephan Mueller8f183752015-08-19 08:42:07 +02004510 .fips_allowed = 1,
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03004511 .test = alg_test_hash,
4512 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004513 .hash = __VECS(des3_ede_cmac64_tv_template)
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03004514 }
4515 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03004516 .alg = "compress_null",
4517 .test = alg_test_null,
4518 }, {
Ard Biesheuvelebb34722015-05-04 11:00:17 +02004519 .alg = "crc32",
4520 .test = alg_test_hash,
Milan Broza8a34412019-01-25 10:31:47 +01004521 .fips_allowed = 1,
Ard Biesheuvelebb34722015-05-04 11:00:17 +02004522 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004523 .hash = __VECS(crc32_tv_template)
Ard Biesheuvelebb34722015-05-04 11:00:17 +02004524 }
4525 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004526 .alg = "crc32c",
Herbert Xu8e3ee852008-11-07 14:58:52 +08004527 .test = alg_test_crc32c,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004528 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004529 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004530 .hash = __VECS(crc32c_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004531 }
4532 }, {
Herbert Xu684115212013-09-07 12:56:26 +10004533 .alg = "crct10dif",
4534 .test = alg_test_hash,
4535 .fips_allowed = 1,
4536 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004537 .hash = __VECS(crct10dif_tv_template)
Herbert Xu684115212013-09-07 12:56:26 +10004538 }
4539 }, {
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08004540 .alg = "ctr(aes)",
4541 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004542 .fips_allowed = 1,
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08004543 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004544 .cipher = __VECS(aes_ctr_tv_template)
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08004545 }
4546 }, {
Jussi Kivilinna85b63e32011-10-10 23:03:03 +03004547 .alg = "ctr(blowfish)",
4548 .test = alg_test_skcipher,
4549 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004550 .cipher = __VECS(bf_ctr_tv_template)
Jussi Kivilinna85b63e32011-10-10 23:03:03 +03004551 }
4552 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02004553 .alg = "ctr(camellia)",
4554 .test = alg_test_skcipher,
4555 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004556 .cipher = __VECS(camellia_ctr_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02004557 }
4558 }, {
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02004559 .alg = "ctr(cast5)",
4560 .test = alg_test_skcipher,
4561 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004562 .cipher = __VECS(cast5_ctr_tv_template)
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02004563 }
4564 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004565 .alg = "ctr(cast6)",
4566 .test = alg_test_skcipher,
4567 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004568 .cipher = __VECS(cast6_ctr_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004569 }
4570 }, {
Jussi Kivilinna8163fc32012-10-20 14:53:07 +03004571 .alg = "ctr(des)",
4572 .test = alg_test_skcipher,
4573 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004574 .cipher = __VECS(des_ctr_tv_template)
Jussi Kivilinna8163fc32012-10-20 14:53:07 +03004575 }
4576 }, {
Jussi Kivilinnae080b172012-10-20 14:53:12 +03004577 .alg = "ctr(des3_ede)",
4578 .test = alg_test_skcipher,
Marcelo Cerri0d8da102017-03-20 17:28:05 -03004579 .fips_allowed = 1,
Jussi Kivilinnae080b172012-10-20 14:53:12 +03004580 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004581 .cipher = __VECS(des3_ede_ctr_tv_template)
Jussi Kivilinnae080b172012-10-20 14:53:12 +03004582 }
4583 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01004584 /* Same as ctr(aes) except the key is stored in
4585 * hardware secure memory which we reference by index
4586 */
4587 .alg = "ctr(paes)",
4588 .test = alg_test_null,
4589 .fips_allowed = 1,
4590 }, {
Gilad Ben-Yosseff0372c02019-04-18 16:38:36 +03004591
4592 /* Same as ctr(sm4) except the key is stored in
4593 * hardware secure memory which we reference by index
4594 */
4595 .alg = "ctr(psm4)",
4596 .test = alg_test_null,
4597 }, {
Jussi Kivilinna9d259172011-10-18 00:02:53 +03004598 .alg = "ctr(serpent)",
4599 .test = alg_test_skcipher,
4600 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004601 .cipher = __VECS(serpent_ctr_tv_template)
Jussi Kivilinna9d259172011-10-18 00:02:53 +03004602 }
4603 }, {
Gilad Ben-Yossef95ba5972018-09-20 14:18:38 +01004604 .alg = "ctr(sm4)",
4605 .test = alg_test_skcipher,
4606 .suite = {
4607 .cipher = __VECS(sm4_ctr_tv_template)
4608 }
4609 }, {
Jussi Kivilinna573da622011-10-10 23:03:12 +03004610 .alg = "ctr(twofish)",
4611 .test = alg_test_skcipher,
4612 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004613 .cipher = __VECS(tf_ctr_tv_template)
Jussi Kivilinna573da622011-10-10 23:03:12 +03004614 }
4615 }, {
Harald Freudenbergerc7ff8572020-01-22 14:43:23 +01004616#if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
4617 .alg = "ctr-paes-s390",
4618 .fips_allowed = 1,
4619 .test = alg_test_skcipher,
4620 .suite = {
4621 .cipher = __VECS(aes_ctr_tv_template)
4622 }
4623 }, {
4624#endif
Herbert Xuda7f0332008-07-31 17:08:25 +08004625 .alg = "cts(cbc(aes))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004626 .test = alg_test_skcipher,
Gilad Ben-Yossef196ad602018-11-04 10:05:24 +00004627 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004628 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004629 .cipher = __VECS(cts_mode_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004630 }
4631 }, {
Gilad Ben-Yosseff0372c02019-04-18 16:38:36 +03004632 /* Same as cts(cbc((aes)) except the key is stored in
4633 * hardware secure memory which we reference by index
4634 */
4635 .alg = "cts(cbc(paes))",
4636 .test = alg_test_null,
4637 .fips_allowed = 1,
4638 }, {
Ard Biesheuvelf6134572019-11-08 13:22:33 +01004639 .alg = "curve25519",
4640 .test = alg_test_kpp,
4641 .suite = {
4642 .kpp = __VECS(curve25519_tv_template)
4643 }
4644 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004645 .alg = "deflate",
4646 .test = alg_test_comp,
Milan Broz08189042012-12-06 17:16:28 +08004647 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004648 .suite = {
4649 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004650 .comp = __VECS(deflate_comp_tv_template),
4651 .decomp = __VECS(deflate_decomp_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004652 }
4653 }
4654 }, {
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01004655 .alg = "dh",
4656 .test = alg_test_kpp,
4657 .fips_allowed = 1,
4658 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004659 .kpp = __VECS(dh_tv_template)
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01004660 }
4661 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03004662 .alg = "digest_null",
4663 .test = alg_test_null,
4664 }, {
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004665 .alg = "drbg_nopr_ctr_aes128",
4666 .test = alg_test_drbg,
4667 .fips_allowed = 1,
4668 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004669 .drbg = __VECS(drbg_nopr_ctr_aes128_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004670 }
4671 }, {
4672 .alg = "drbg_nopr_ctr_aes192",
4673 .test = alg_test_drbg,
4674 .fips_allowed = 1,
4675 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004676 .drbg = __VECS(drbg_nopr_ctr_aes192_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004677 }
4678 }, {
4679 .alg = "drbg_nopr_ctr_aes256",
4680 .test = alg_test_drbg,
4681 .fips_allowed = 1,
4682 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004683 .drbg = __VECS(drbg_nopr_ctr_aes256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004684 }
4685 }, {
4686 /*
4687 * There is no need to specifically test the DRBG with every
4688 * backend cipher -- covered by drbg_nopr_hmac_sha256 test
4689 */
4690 .alg = "drbg_nopr_hmac_sha1",
4691 .fips_allowed = 1,
4692 .test = alg_test_null,
4693 }, {
4694 .alg = "drbg_nopr_hmac_sha256",
4695 .test = alg_test_drbg,
4696 .fips_allowed = 1,
4697 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004698 .drbg = __VECS(drbg_nopr_hmac_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004699 }
4700 }, {
4701 /* covered by drbg_nopr_hmac_sha256 test */
4702 .alg = "drbg_nopr_hmac_sha384",
4703 .fips_allowed = 1,
4704 .test = alg_test_null,
4705 }, {
4706 .alg = "drbg_nopr_hmac_sha512",
4707 .test = alg_test_null,
4708 .fips_allowed = 1,
4709 }, {
4710 .alg = "drbg_nopr_sha1",
4711 .fips_allowed = 1,
4712 .test = alg_test_null,
4713 }, {
4714 .alg = "drbg_nopr_sha256",
4715 .test = alg_test_drbg,
4716 .fips_allowed = 1,
4717 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004718 .drbg = __VECS(drbg_nopr_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004719 }
4720 }, {
4721 /* covered by drbg_nopr_sha256 test */
4722 .alg = "drbg_nopr_sha384",
4723 .fips_allowed = 1,
4724 .test = alg_test_null,
4725 }, {
4726 .alg = "drbg_nopr_sha512",
4727 .fips_allowed = 1,
4728 .test = alg_test_null,
4729 }, {
4730 .alg = "drbg_pr_ctr_aes128",
4731 .test = alg_test_drbg,
4732 .fips_allowed = 1,
4733 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004734 .drbg = __VECS(drbg_pr_ctr_aes128_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004735 }
4736 }, {
4737 /* covered by drbg_pr_ctr_aes128 test */
4738 .alg = "drbg_pr_ctr_aes192",
4739 .fips_allowed = 1,
4740 .test = alg_test_null,
4741 }, {
4742 .alg = "drbg_pr_ctr_aes256",
4743 .fips_allowed = 1,
4744 .test = alg_test_null,
4745 }, {
4746 .alg = "drbg_pr_hmac_sha1",
4747 .fips_allowed = 1,
4748 .test = alg_test_null,
4749 }, {
4750 .alg = "drbg_pr_hmac_sha256",
4751 .test = alg_test_drbg,
4752 .fips_allowed = 1,
4753 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004754 .drbg = __VECS(drbg_pr_hmac_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004755 }
4756 }, {
4757 /* covered by drbg_pr_hmac_sha256 test */
4758 .alg = "drbg_pr_hmac_sha384",
4759 .fips_allowed = 1,
4760 .test = alg_test_null,
4761 }, {
4762 .alg = "drbg_pr_hmac_sha512",
4763 .test = alg_test_null,
4764 .fips_allowed = 1,
4765 }, {
4766 .alg = "drbg_pr_sha1",
4767 .fips_allowed = 1,
4768 .test = alg_test_null,
4769 }, {
4770 .alg = "drbg_pr_sha256",
4771 .test = alg_test_drbg,
4772 .fips_allowed = 1,
4773 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004774 .drbg = __VECS(drbg_pr_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004775 }
4776 }, {
4777 /* covered by drbg_pr_sha256 test */
4778 .alg = "drbg_pr_sha384",
4779 .fips_allowed = 1,
4780 .test = alg_test_null,
4781 }, {
4782 .alg = "drbg_pr_sha512",
4783 .fips_allowed = 1,
4784 .test = alg_test_null,
4785 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004786 .alg = "ecb(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004787 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004788 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004789 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004790 .cipher = __VECS(aes_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004791 }
4792 }, {
4793 .alg = "ecb(anubis)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004794 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004795 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004796 .cipher = __VECS(anubis_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004797 }
4798 }, {
4799 .alg = "ecb(arc4)",
Ard Biesheuvel611a23c2019-06-12 18:19:57 +02004800 .generic_driver = "ecb(arc4)-generic",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004801 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004802 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004803 .cipher = __VECS(arc4_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004804 }
4805 }, {
4806 .alg = "ecb(blowfish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004807 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004808 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004809 .cipher = __VECS(bf_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004810 }
4811 }, {
4812 .alg = "ecb(camellia)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004813 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004814 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004815 .cipher = __VECS(camellia_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004816 }
4817 }, {
4818 .alg = "ecb(cast5)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004819 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004820 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004821 .cipher = __VECS(cast5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004822 }
4823 }, {
4824 .alg = "ecb(cast6)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004825 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004826 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004827 .cipher = __VECS(cast6_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004828 }
4829 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03004830 .alg = "ecb(cipher_null)",
4831 .test = alg_test_null,
Milan Broz6175ca22017-04-21 13:03:06 +02004832 .fips_allowed = 1,
Jussi Kivilinnae4483702013-04-07 16:43:56 +03004833 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004834 .alg = "ecb(des)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004835 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004836 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004837 .cipher = __VECS(des_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004838 }
4839 }, {
4840 .alg = "ecb(des3_ede)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004841 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004842 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004843 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004844 .cipher = __VECS(des3_ede_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004845 }
4846 }, {
Jussi Kivilinna66e5bd02013-01-19 13:31:36 +02004847 .alg = "ecb(fcrypt)",
4848 .test = alg_test_skcipher,
4849 .suite = {
4850 .cipher = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004851 .vecs = fcrypt_pcbc_tv_template,
4852 .count = 1
Jussi Kivilinna66e5bd02013-01-19 13:31:36 +02004853 }
4854 }
4855 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004856 .alg = "ecb(khazad)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004857 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004858 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004859 .cipher = __VECS(khazad_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004860 }
4861 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01004862 /* Same as ecb(aes) except the key is stored in
4863 * hardware secure memory which we reference by index
4864 */
4865 .alg = "ecb(paes)",
4866 .test = alg_test_null,
4867 .fips_allowed = 1,
4868 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004869 .alg = "ecb(seed)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004870 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004871 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004872 .cipher = __VECS(seed_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004873 }
4874 }, {
4875 .alg = "ecb(serpent)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004876 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004877 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004878 .cipher = __VECS(serpent_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004879 }
4880 }, {
Gilad Ben-Yossefcd83a8a2018-03-06 09:44:43 +00004881 .alg = "ecb(sm4)",
4882 .test = alg_test_skcipher,
4883 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004884 .cipher = __VECS(sm4_tv_template)
Gilad Ben-Yossefcd83a8a2018-03-06 09:44:43 +00004885 }
4886 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004887 .alg = "ecb(tea)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004888 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004889 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004890 .cipher = __VECS(tea_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004891 }
4892 }, {
4893 .alg = "ecb(tnepres)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004894 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004895 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004896 .cipher = __VECS(tnepres_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004897 }
4898 }, {
4899 .alg = "ecb(twofish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004900 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004901 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004902 .cipher = __VECS(tf_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004903 }
4904 }, {
4905 .alg = "ecb(xeta)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004906 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004907 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004908 .cipher = __VECS(xeta_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004909 }
4910 }, {
4911 .alg = "ecb(xtea)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004912 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004913 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004914 .cipher = __VECS(xtea_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004915 }
4916 }, {
Harald Freudenbergerc7ff8572020-01-22 14:43:23 +01004917#if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
4918 .alg = "ecb-paes-s390",
4919 .fips_allowed = 1,
4920 .test = alg_test_skcipher,
4921 .suite = {
4922 .cipher = __VECS(aes_tv_template)
4923 }
4924 }, {
4925#endif
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +01004926 .alg = "ecdh",
4927 .test = alg_test_kpp,
4928 .fips_allowed = 1,
4929 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004930 .kpp = __VECS(ecdh_tv_template)
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +01004931 }
4932 }, {
Vitaly Chikunov32fbdbd2019-04-11 18:51:21 +03004933 .alg = "ecrdsa",
4934 .test = alg_test_akcipher,
4935 .suite = {
4936 .akcipher = __VECS(ecrdsa_tv_template)
4937 }
4938 }, {
Ard Biesheuvelf975abb2019-08-19 17:17:34 +03004939 .alg = "essiv(authenc(hmac(sha256),cbc(aes)),sha256)",
4940 .test = alg_test_aead,
4941 .fips_allowed = 1,
4942 .suite = {
4943 .aead = __VECS(essiv_hmac_sha256_aes_cbc_tv_temp)
4944 }
4945 }, {
4946 .alg = "essiv(cbc(aes),sha256)",
4947 .test = alg_test_skcipher,
4948 .fips_allowed = 1,
4949 .suite = {
4950 .cipher = __VECS(essiv_aes_cbc_tv_template)
4951 }
4952 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004953 .alg = "gcm(aes)",
Eric Biggers40153b12019-04-11 21:57:41 -07004954 .generic_driver = "gcm_base(ctr(aes-generic),ghash-generic)",
Herbert Xuda7f0332008-07-31 17:08:25 +08004955 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004956 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004957 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004958 .aead = __VECS(aes_gcm_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004959 }
4960 }, {
Youquan, Song507069c2009-11-23 20:23:04 +08004961 .alg = "ghash",
4962 .test = alg_test_hash,
Jarod Wilson18c0ebd2011-01-29 15:14:35 +11004963 .fips_allowed = 1,
Youquan, Song507069c2009-11-23 20:23:04 +08004964 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004965 .hash = __VECS(ghash_tv_template)
Youquan, Song507069c2009-11-23 20:23:04 +08004966 }
4967 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004968 .alg = "hmac(md5)",
4969 .test = alg_test_hash,
4970 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004971 .hash = __VECS(hmac_md5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004972 }
4973 }, {
4974 .alg = "hmac(rmd128)",
4975 .test = alg_test_hash,
4976 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004977 .hash = __VECS(hmac_rmd128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004978 }
4979 }, {
4980 .alg = "hmac(rmd160)",
4981 .test = alg_test_hash,
4982 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004983 .hash = __VECS(hmac_rmd160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004984 }
4985 }, {
4986 .alg = "hmac(sha1)",
4987 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004988 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004989 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004990 .hash = __VECS(hmac_sha1_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004991 }
4992 }, {
4993 .alg = "hmac(sha224)",
4994 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004995 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004996 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004997 .hash = __VECS(hmac_sha224_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004998 }
4999 }, {
5000 .alg = "hmac(sha256)",
5001 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10005002 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08005003 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005004 .hash = __VECS(hmac_sha256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005005 }
5006 }, {
raveendra padasalagi98eca722016-07-01 11:16:54 +05305007 .alg = "hmac(sha3-224)",
5008 .test = alg_test_hash,
5009 .fips_allowed = 1,
5010 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005011 .hash = __VECS(hmac_sha3_224_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05305012 }
5013 }, {
5014 .alg = "hmac(sha3-256)",
5015 .test = alg_test_hash,
5016 .fips_allowed = 1,
5017 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005018 .hash = __VECS(hmac_sha3_256_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05305019 }
5020 }, {
5021 .alg = "hmac(sha3-384)",
5022 .test = alg_test_hash,
5023 .fips_allowed = 1,
5024 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005025 .hash = __VECS(hmac_sha3_384_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05305026 }
5027 }, {
5028 .alg = "hmac(sha3-512)",
5029 .test = alg_test_hash,
5030 .fips_allowed = 1,
5031 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005032 .hash = __VECS(hmac_sha3_512_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05305033 }
5034 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005035 .alg = "hmac(sha384)",
5036 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10005037 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08005038 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005039 .hash = __VECS(hmac_sha384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005040 }
5041 }, {
5042 .alg = "hmac(sha512)",
5043 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10005044 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08005045 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005046 .hash = __VECS(hmac_sha512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005047 }
5048 }, {
Pascal van Leeuwen8194fd12019-09-13 17:20:38 +02005049 .alg = "hmac(sm3)",
5050 .test = alg_test_hash,
5051 .suite = {
5052 .hash = __VECS(hmac_sm3_tv_template)
5053 }
5054 }, {
Vitaly Chikunov25a0b9d2018-11-07 00:00:03 +03005055 .alg = "hmac(streebog256)",
5056 .test = alg_test_hash,
5057 .suite = {
5058 .hash = __VECS(hmac_streebog256_tv_template)
5059 }
5060 }, {
5061 .alg = "hmac(streebog512)",
5062 .test = alg_test_hash,
5063 .suite = {
5064 .hash = __VECS(hmac_streebog512_tv_template)
5065 }
5066 }, {
Stephan Muellerbb5530e2015-05-25 15:10:20 +02005067 .alg = "jitterentropy_rng",
5068 .fips_allowed = 1,
5069 .test = alg_test_null,
5070 }, {
Stephan Mueller35351982015-09-21 20:59:56 +02005071 .alg = "kw(aes)",
5072 .test = alg_test_skcipher,
5073 .fips_allowed = 1,
5074 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005075 .cipher = __VECS(aes_kw_tv_template)
Stephan Mueller35351982015-09-21 20:59:56 +02005076 }
5077 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005078 .alg = "lrw(aes)",
Eric Biggersd435e102019-04-11 21:57:40 -07005079 .generic_driver = "lrw(ecb(aes-generic))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005080 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08005081 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005082 .cipher = __VECS(aes_lrw_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005083 }
5084 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02005085 .alg = "lrw(camellia)",
Eric Biggersd435e102019-04-11 21:57:40 -07005086 .generic_driver = "lrw(ecb(camellia-generic))",
Jussi Kivilinna08406052012-03-05 20:26:21 +02005087 .test = alg_test_skcipher,
5088 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005089 .cipher = __VECS(camellia_lrw_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02005090 }
5091 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02005092 .alg = "lrw(cast6)",
Eric Biggersd435e102019-04-11 21:57:40 -07005093 .generic_driver = "lrw(ecb(cast6-generic))",
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02005094 .test = alg_test_skcipher,
5095 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005096 .cipher = __VECS(cast6_lrw_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02005097 }
5098 }, {
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03005099 .alg = "lrw(serpent)",
Eric Biggersd435e102019-04-11 21:57:40 -07005100 .generic_driver = "lrw(ecb(serpent-generic))",
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03005101 .test = alg_test_skcipher,
5102 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005103 .cipher = __VECS(serpent_lrw_tv_template)
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03005104 }
5105 }, {
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03005106 .alg = "lrw(twofish)",
Eric Biggersd435e102019-04-11 21:57:40 -07005107 .generic_driver = "lrw(ecb(twofish-generic))",
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03005108 .test = alg_test_skcipher,
5109 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005110 .cipher = __VECS(tf_lrw_tv_template)
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03005111 }
5112 }, {
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02005113 .alg = "lz4",
5114 .test = alg_test_comp,
5115 .fips_allowed = 1,
5116 .suite = {
5117 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005118 .comp = __VECS(lz4_comp_tv_template),
5119 .decomp = __VECS(lz4_decomp_tv_template)
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02005120 }
5121 }
5122 }, {
5123 .alg = "lz4hc",
5124 .test = alg_test_comp,
5125 .fips_allowed = 1,
5126 .suite = {
5127 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005128 .comp = __VECS(lz4hc_comp_tv_template),
5129 .decomp = __VECS(lz4hc_decomp_tv_template)
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02005130 }
5131 }
5132 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005133 .alg = "lzo",
5134 .test = alg_test_comp,
Milan Broz08189042012-12-06 17:16:28 +08005135 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08005136 .suite = {
5137 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005138 .comp = __VECS(lzo_comp_tv_template),
5139 .decomp = __VECS(lzo_decomp_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005140 }
5141 }
5142 }, {
Hannah Panf248caf2019-07-02 15:16:02 -07005143 .alg = "lzo-rle",
5144 .test = alg_test_comp,
5145 .fips_allowed = 1,
5146 .suite = {
5147 .comp = {
5148 .comp = __VECS(lzorle_comp_tv_template),
5149 .decomp = __VECS(lzorle_decomp_tv_template)
5150 }
5151 }
5152 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005153 .alg = "md4",
5154 .test = alg_test_hash,
5155 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005156 .hash = __VECS(md4_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005157 }
5158 }, {
5159 .alg = "md5",
5160 .test = alg_test_hash,
5161 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005162 .hash = __VECS(md5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005163 }
5164 }, {
5165 .alg = "michael_mic",
5166 .test = alg_test_hash,
5167 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005168 .hash = __VECS(michael_mic_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005169 }
5170 }, {
Eric Biggers26609a22018-11-16 17:26:29 -08005171 .alg = "nhpoly1305",
5172 .test = alg_test_hash,
5173 .suite = {
5174 .hash = __VECS(nhpoly1305_tv_template)
5175 }
5176 }, {
Puneet Saxenaba0e14a2011-05-04 15:04:10 +10005177 .alg = "ofb(aes)",
5178 .test = alg_test_skcipher,
5179 .fips_allowed = 1,
5180 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005181 .cipher = __VECS(aes_ofb_tv_template)
Puneet Saxenaba0e14a2011-05-04 15:04:10 +10005182 }
5183 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01005184 /* Same as ofb(aes) except the key is stored in
5185 * hardware secure memory which we reference by index
5186 */
5187 .alg = "ofb(paes)",
5188 .test = alg_test_null,
5189 .fips_allowed = 1,
5190 }, {
Pascal van Leeuwena06b15b2019-09-13 11:10:39 +02005191 .alg = "ofb(sm4)",
5192 .test = alg_test_skcipher,
5193 .suite = {
5194 .cipher = __VECS(sm4_ofb_tv_template)
5195 }
5196 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005197 .alg = "pcbc(fcrypt)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005198 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08005199 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005200 .cipher = __VECS(fcrypt_pcbc_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005201 }
5202 }, {
Stephan Mueller12071072017-06-12 23:27:51 +02005203 .alg = "pkcs1pad(rsa,sha224)",
5204 .test = alg_test_null,
5205 .fips_allowed = 1,
5206 }, {
5207 .alg = "pkcs1pad(rsa,sha256)",
5208 .test = alg_test_akcipher,
5209 .fips_allowed = 1,
5210 .suite = {
5211 .akcipher = __VECS(pkcs1pad_rsa_tv_template)
5212 }
5213 }, {
5214 .alg = "pkcs1pad(rsa,sha384)",
5215 .test = alg_test_null,
5216 .fips_allowed = 1,
5217 }, {
5218 .alg = "pkcs1pad(rsa,sha512)",
5219 .test = alg_test_null,
5220 .fips_allowed = 1,
5221 }, {
Martin Willieee9dc62015-06-01 13:43:59 +02005222 .alg = "poly1305",
5223 .test = alg_test_hash,
5224 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005225 .hash = __VECS(poly1305_tv_template)
Martin Willieee9dc62015-06-01 13:43:59 +02005226 }
5227 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005228 .alg = "rfc3686(ctr(aes))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005229 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10005230 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08005231 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005232 .cipher = __VECS(aes_ctr_rfc3686_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005233 }
5234 }, {
Pascal van Leeuwene4886212019-09-13 11:10:42 +02005235 .alg = "rfc3686(ctr(sm4))",
5236 .test = alg_test_skcipher,
5237 .suite = {
5238 .cipher = __VECS(sm4_ctr_rfc3686_tv_template)
5239 }
5240 }, {
Herbert Xu3f31a742015-07-09 07:17:34 +08005241 .alg = "rfc4106(gcm(aes))",
Eric Biggers40153b12019-04-11 21:57:41 -07005242 .generic_driver = "rfc4106(gcm_base(ctr(aes-generic),ghash-generic))",
Adrian Hoban69435b92010-11-04 15:02:04 -04005243 .test = alg_test_aead,
Jarod Wilsondb71f29a2015-01-23 12:42:15 -05005244 .fips_allowed = 1,
Adrian Hoban69435b92010-11-04 15:02:04 -04005245 .suite = {
Eric Biggers49763fc2019-12-01 13:53:30 -08005246 .aead = {
5247 ____VECS(aes_gcm_rfc4106_tv_template),
5248 .einval_allowed = 1,
Eric Biggers6f3a06d2020-03-04 14:44:03 -08005249 .aad_iv = 1,
Eric Biggers49763fc2019-12-01 13:53:30 -08005250 }
Adrian Hoban69435b92010-11-04 15:02:04 -04005251 }
5252 }, {
Herbert Xu544c4362015-07-14 16:53:22 +08005253 .alg = "rfc4309(ccm(aes))",
Eric Biggers40153b12019-04-11 21:57:41 -07005254 .generic_driver = "rfc4309(ccm_base(ctr(aes-generic),cbcmac(aes-generic)))",
Jarod Wilson5d667322009-05-04 19:23:40 +08005255 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10005256 .fips_allowed = 1,
Jarod Wilson5d667322009-05-04 19:23:40 +08005257 .suite = {
Eric Biggers49763fc2019-12-01 13:53:30 -08005258 .aead = {
5259 ____VECS(aes_ccm_rfc4309_tv_template),
5260 .einval_allowed = 1,
Eric Biggers6f3a06d2020-03-04 14:44:03 -08005261 .aad_iv = 1,
Eric Biggers49763fc2019-12-01 13:53:30 -08005262 }
Jarod Wilson5d667322009-05-04 19:23:40 +08005263 }
5264 }, {
Herbert Xubb687452015-06-16 13:54:24 +08005265 .alg = "rfc4543(gcm(aes))",
Eric Biggers40153b12019-04-11 21:57:41 -07005266 .generic_driver = "rfc4543(gcm_base(ctr(aes-generic),ghash-generic))",
Jussi Kivilinnae9b74412013-04-07 16:43:51 +03005267 .test = alg_test_aead,
5268 .suite = {
Eric Biggers49763fc2019-12-01 13:53:30 -08005269 .aead = {
5270 ____VECS(aes_gcm_rfc4543_tv_template),
5271 .einval_allowed = 1,
Eric Biggers6f3a06d2020-03-04 14:44:03 -08005272 .aad_iv = 1,
Eric Biggers49763fc2019-12-01 13:53:30 -08005273 }
Jussi Kivilinnae9b74412013-04-07 16:43:51 +03005274 }
5275 }, {
Martin Williaf2b76b2015-06-01 13:44:01 +02005276 .alg = "rfc7539(chacha20,poly1305)",
5277 .test = alg_test_aead,
5278 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08005279 .aead = __VECS(rfc7539_tv_template)
Martin Williaf2b76b2015-06-01 13:44:01 +02005280 }
5281 }, {
Martin Willi59007582015-06-01 13:44:03 +02005282 .alg = "rfc7539esp(chacha20,poly1305)",
5283 .test = alg_test_aead,
5284 .suite = {
Eric Biggers49763fc2019-12-01 13:53:30 -08005285 .aead = {
5286 ____VECS(rfc7539esp_tv_template),
5287 .einval_allowed = 1,
Eric Biggers6f3a06d2020-03-04 14:44:03 -08005288 .aad_iv = 1,
Eric Biggers49763fc2019-12-01 13:53:30 -08005289 }
Martin Willi59007582015-06-01 13:44:03 +02005290 }
5291 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005292 .alg = "rmd128",
5293 .test = alg_test_hash,
5294 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005295 .hash = __VECS(rmd128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005296 }
5297 }, {
5298 .alg = "rmd160",
5299 .test = alg_test_hash,
5300 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005301 .hash = __VECS(rmd160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005302 }
5303 }, {
5304 .alg = "rmd256",
5305 .test = alg_test_hash,
5306 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005307 .hash = __VECS(rmd256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005308 }
5309 }, {
5310 .alg = "rmd320",
5311 .test = alg_test_hash,
5312 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005313 .hash = __VECS(rmd320_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005314 }
5315 }, {
Tadeusz Struk946cc462015-06-16 10:31:06 -07005316 .alg = "rsa",
5317 .test = alg_test_akcipher,
5318 .fips_allowed = 1,
5319 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005320 .akcipher = __VECS(rsa_tv_template)
Tadeusz Struk946cc462015-06-16 10:31:06 -07005321 }
5322 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005323 .alg = "salsa20",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005324 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08005325 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005326 .cipher = __VECS(salsa20_stream_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005327 }
5328 }, {
5329 .alg = "sha1",
5330 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10005331 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08005332 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005333 .hash = __VECS(sha1_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005334 }
5335 }, {
5336 .alg = "sha224",
5337 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10005338 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08005339 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005340 .hash = __VECS(sha224_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005341 }
5342 }, {
5343 .alg = "sha256",
5344 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10005345 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08005346 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005347 .hash = __VECS(sha256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005348 }
5349 }, {
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05305350 .alg = "sha3-224",
5351 .test = alg_test_hash,
5352 .fips_allowed = 1,
5353 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005354 .hash = __VECS(sha3_224_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05305355 }
5356 }, {
5357 .alg = "sha3-256",
5358 .test = alg_test_hash,
5359 .fips_allowed = 1,
5360 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005361 .hash = __VECS(sha3_256_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05305362 }
5363 }, {
5364 .alg = "sha3-384",
5365 .test = alg_test_hash,
5366 .fips_allowed = 1,
5367 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005368 .hash = __VECS(sha3_384_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05305369 }
5370 }, {
5371 .alg = "sha3-512",
5372 .test = alg_test_hash,
5373 .fips_allowed = 1,
5374 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005375 .hash = __VECS(sha3_512_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05305376 }
5377 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005378 .alg = "sha384",
5379 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10005380 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08005381 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005382 .hash = __VECS(sha384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005383 }
5384 }, {
5385 .alg = "sha512",
5386 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10005387 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08005388 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005389 .hash = __VECS(sha512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005390 }
5391 }, {
Tianjia Zhang8b805b92020-09-21 00:21:00 +08005392 .alg = "sm2",
5393 .test = alg_test_akcipher,
5394 .suite = {
5395 .akcipher = __VECS(sm2_tv_template)
5396 }
5397 }, {
Gilad Ben-Yossefb7e27532017-08-21 13:51:29 +03005398 .alg = "sm3",
5399 .test = alg_test_hash,
5400 .suite = {
5401 .hash = __VECS(sm3_tv_template)
5402 }
5403 }, {
Vitaly Chikunov25a0b9d2018-11-07 00:00:03 +03005404 .alg = "streebog256",
5405 .test = alg_test_hash,
5406 .suite = {
5407 .hash = __VECS(streebog256_tv_template)
5408 }
5409 }, {
5410 .alg = "streebog512",
5411 .test = alg_test_hash,
5412 .suite = {
5413 .hash = __VECS(streebog512_tv_template)
5414 }
5415 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005416 .alg = "tgr128",
5417 .test = alg_test_hash,
5418 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005419 .hash = __VECS(tgr128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005420 }
5421 }, {
5422 .alg = "tgr160",
5423 .test = alg_test_hash,
5424 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005425 .hash = __VECS(tgr160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005426 }
5427 }, {
5428 .alg = "tgr192",
5429 .test = alg_test_hash,
5430 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005431 .hash = __VECS(tgr192_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005432 }
5433 }, {
Eric Biggersed331ad2018-06-18 10:22:39 -07005434 .alg = "vmac64(aes)",
5435 .test = alg_test_hash,
5436 .suite = {
5437 .hash = __VECS(vmac64_aes_tv_template)
5438 }
5439 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005440 .alg = "wp256",
5441 .test = alg_test_hash,
5442 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005443 .hash = __VECS(wp256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005444 }
5445 }, {
5446 .alg = "wp384",
5447 .test = alg_test_hash,
5448 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005449 .hash = __VECS(wp384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005450 }
5451 }, {
5452 .alg = "wp512",
5453 .test = alg_test_hash,
5454 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005455 .hash = __VECS(wp512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005456 }
5457 }, {
5458 .alg = "xcbc(aes)",
5459 .test = alg_test_hash,
5460 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005461 .hash = __VECS(aes_xcbc128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005462 }
5463 }, {
Eric Biggersaa762402018-11-16 17:26:22 -08005464 .alg = "xchacha12",
5465 .test = alg_test_skcipher,
5466 .suite = {
5467 .cipher = __VECS(xchacha12_tv_template)
5468 },
5469 }, {
Eric Biggersde61d7a2018-11-16 17:26:20 -08005470 .alg = "xchacha20",
5471 .test = alg_test_skcipher,
5472 .suite = {
5473 .cipher = __VECS(xchacha20_tv_template)
5474 },
5475 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005476 .alg = "xts(aes)",
Eric Biggersd435e102019-04-11 21:57:40 -07005477 .generic_driver = "xts(ecb(aes-generic))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005478 .test = alg_test_skcipher,
Jarod Wilson2918aa82011-01-29 15:14:01 +11005479 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08005480 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005481 .cipher = __VECS(aes_xts_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005482 }
Geert Uytterhoeven0c01aed2009-03-04 15:42:15 +08005483 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02005484 .alg = "xts(camellia)",
Eric Biggersd435e102019-04-11 21:57:40 -07005485 .generic_driver = "xts(ecb(camellia-generic))",
Jussi Kivilinna08406052012-03-05 20:26:21 +02005486 .test = alg_test_skcipher,
5487 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005488 .cipher = __VECS(camellia_xts_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02005489 }
5490 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02005491 .alg = "xts(cast6)",
Eric Biggersd435e102019-04-11 21:57:40 -07005492 .generic_driver = "xts(ecb(cast6-generic))",
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02005493 .test = alg_test_skcipher,
5494 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005495 .cipher = __VECS(cast6_xts_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02005496 }
5497 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01005498 /* Same as xts(aes) except the key is stored in
5499 * hardware secure memory which we reference by index
5500 */
5501 .alg = "xts(paes)",
5502 .test = alg_test_null,
5503 .fips_allowed = 1,
5504 }, {
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03005505 .alg = "xts(serpent)",
Eric Biggersd435e102019-04-11 21:57:40 -07005506 .generic_driver = "xts(ecb(serpent-generic))",
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03005507 .test = alg_test_skcipher,
5508 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005509 .cipher = __VECS(serpent_xts_tv_template)
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03005510 }
5511 }, {
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03005512 .alg = "xts(twofish)",
Eric Biggersd435e102019-04-11 21:57:40 -07005513 .generic_driver = "xts(ecb(twofish-generic))",
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03005514 .test = alg_test_skcipher,
5515 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005516 .cipher = __VECS(tf_xts_tv_template)
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03005517 }
Giovanni Cabiddua368f432017-04-21 21:54:30 +01005518 }, {
Harald Freudenbergerc7ff8572020-01-22 14:43:23 +01005519#if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
5520 .alg = "xts-paes-s390",
5521 .fips_allowed = 1,
5522 .test = alg_test_skcipher,
5523 .suite = {
5524 .cipher = __VECS(aes_xts_tv_template)
5525 }
5526 }, {
5527#endif
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01005528 .alg = "xts4096(paes)",
5529 .test = alg_test_null,
5530 .fips_allowed = 1,
5531 }, {
5532 .alg = "xts512(paes)",
5533 .test = alg_test_null,
5534 .fips_allowed = 1,
5535 }, {
Nikolay Borisov67882e72019-05-30 09:52:57 +03005536 .alg = "xxhash64",
5537 .test = alg_test_hash,
5538 .fips_allowed = 1,
5539 .suite = {
5540 .hash = __VECS(xxhash64_tv_template)
5541 }
5542 }, {
Giovanni Cabiddua368f432017-04-21 21:54:30 +01005543 .alg = "zlib-deflate",
5544 .test = alg_test_comp,
5545 .fips_allowed = 1,
5546 .suite = {
5547 .comp = {
5548 .comp = __VECS(zlib_deflate_comp_tv_template),
5549 .decomp = __VECS(zlib_deflate_decomp_tv_template)
5550 }
5551 }
Nick Terrelld28fc3d2018-03-30 12:14:53 -07005552 }, {
5553 .alg = "zstd",
5554 .test = alg_test_comp,
5555 .fips_allowed = 1,
5556 .suite = {
5557 .comp = {
5558 .comp = __VECS(zstd_comp_tv_template),
5559 .decomp = __VECS(zstd_decomp_tv_template)
5560 }
5561 }
Herbert Xuda7f0332008-07-31 17:08:25 +08005562 }
5563};
5564
Eric Biggers3f47a032019-01-31 23:51:43 -08005565static void alg_check_test_descs_order(void)
Jussi Kivilinna57147582013-06-13 17:37:40 +03005566{
5567 int i;
5568
Jussi Kivilinna57147582013-06-13 17:37:40 +03005569 for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
5570 int diff = strcmp(alg_test_descs[i - 1].alg,
5571 alg_test_descs[i].alg);
5572
5573 if (WARN_ON(diff > 0)) {
5574 pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
5575 alg_test_descs[i - 1].alg,
5576 alg_test_descs[i].alg);
5577 }
5578
5579 if (WARN_ON(diff == 0)) {
5580 pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
5581 alg_test_descs[i].alg);
5582 }
5583 }
5584}
5585
Eric Biggers3f47a032019-01-31 23:51:43 -08005586static void alg_check_testvec_configs(void)
5587{
Eric Biggers4e7babba2019-01-31 23:51:46 -08005588 int i;
5589
5590 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++)
5591 WARN_ON(!valid_testvec_config(
5592 &default_cipher_testvec_configs[i]));
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08005593
5594 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++)
5595 WARN_ON(!valid_testvec_config(
5596 &default_hash_testvec_configs[i]));
Eric Biggers3f47a032019-01-31 23:51:43 -08005597}
5598
5599static void testmgr_onetime_init(void)
5600{
5601 alg_check_test_descs_order();
5602 alg_check_testvec_configs();
Eric Biggers5b2706a2019-01-31 23:51:44 -08005603
5604#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
5605 pr_warn("alg: extra crypto tests enabled. This is intended for developer use only.\n");
5606#endif
Eric Biggers3f47a032019-01-31 23:51:43 -08005607}
5608
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005609static int alg_find_test(const char *alg)
Herbert Xuda7f0332008-07-31 17:08:25 +08005610{
5611 int start = 0;
5612 int end = ARRAY_SIZE(alg_test_descs);
5613
5614 while (start < end) {
5615 int i = (start + end) / 2;
5616 int diff = strcmp(alg_test_descs[i].alg, alg);
5617
5618 if (diff > 0) {
5619 end = i;
5620 continue;
5621 }
5622
5623 if (diff < 0) {
5624 start = i + 1;
5625 continue;
5626 }
5627
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005628 return i;
Herbert Xuda7f0332008-07-31 17:08:25 +08005629 }
5630
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005631 return -1;
5632}
5633
5634int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
5635{
5636 int i;
Herbert Xua68f6612009-07-02 16:32:12 +08005637 int j;
Neil Hormand12d6b62008-10-12 20:36:51 +08005638 int rc;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005639
Richard W.M. Jones9e5c9fe2016-05-03 10:00:17 +01005640 if (!fips_enabled && notests) {
5641 printk_once(KERN_INFO "alg: self-tests disabled\n");
5642 return 0;
5643 }
5644
Eric Biggers3f47a032019-01-31 23:51:43 -08005645 DO_ONCE(testmgr_onetime_init);
Jussi Kivilinna57147582013-06-13 17:37:40 +03005646
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005647 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
5648 char nalg[CRYPTO_MAX_ALG_NAME];
5649
5650 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
5651 sizeof(nalg))
5652 return -ENAMETOOLONG;
5653
5654 i = alg_find_test(nalg);
5655 if (i < 0)
5656 goto notest;
5657
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10005658 if (fips_enabled && !alg_test_descs[i].fips_allowed)
5659 goto non_fips_alg;
5660
Jarod Wilson941fb322009-05-04 19:49:23 +08005661 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
5662 goto test_done;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005663 }
5664
5665 i = alg_find_test(alg);
Herbert Xua68f6612009-07-02 16:32:12 +08005666 j = alg_find_test(driver);
5667 if (i < 0 && j < 0)
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005668 goto notest;
5669
Herbert Xua68f6612009-07-02 16:32:12 +08005670 if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
5671 (j >= 0 && !alg_test_descs[j].fips_allowed)))
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10005672 goto non_fips_alg;
5673
Herbert Xua68f6612009-07-02 16:32:12 +08005674 rc = 0;
5675 if (i >= 0)
5676 rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
5677 type, mask);
Cristian Stoica032c8ca2013-07-18 18:57:07 +03005678 if (j >= 0 && j != i)
Herbert Xua68f6612009-07-02 16:32:12 +08005679 rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
5680 type, mask);
5681
Jarod Wilson941fb322009-05-04 19:49:23 +08005682test_done:
Gilad Ben-Yossef95523892019-07-02 14:39:20 +03005683 if (rc && (fips_enabled || panic_on_fail)) {
5684 fips_fail_notify();
Eric Biggerseda69b02019-03-31 13:09:14 -07005685 panic("alg: self-tests for %s (%s) failed in %s mode!\n",
5686 driver, alg, fips_enabled ? "fips" : "panic_on_fail");
Gilad Ben-Yossef95523892019-07-02 14:39:20 +03005687 }
Neil Hormand12d6b62008-10-12 20:36:51 +08005688
Jarod Wilson29ecd4a2009-05-04 19:51:17 +08005689 if (fips_enabled && !rc)
Masanari Iida3e8cffd2014-10-07 00:37:54 +09005690 pr_info("alg: self-tests for %s (%s) passed\n", driver, alg);
Jarod Wilson29ecd4a2009-05-04 19:51:17 +08005691
Neil Hormand12d6b62008-10-12 20:36:51 +08005692 return rc;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005693
5694notest:
Herbert Xuda7f0332008-07-31 17:08:25 +08005695 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
5696 return 0;
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10005697non_fips_alg:
5698 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08005699}
Alexander Shishkin0b767f92010-06-03 20:53:43 +10005700
Herbert Xu326a6342010-08-06 09:40:28 +08005701#endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
Alexander Shishkin0b767f92010-06-03 20:53:43 +10005702
Herbert Xuda7f0332008-07-31 17:08:25 +08005703EXPORT_SYMBOL_GPL(alg_test);