Merge tag 'riscv-for-linus-6.4-mw1' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / crypto / testmgr.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
da7f033d
HX
2/*
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>
3f47a03d 9 * Copyright (c) 2019 Google LLC
da7f033d 10 *
69435b94
AH
11 * Updated RFC4106 AES-GCM testing.
12 * Authors: Aidan O'Mahony (aidan.o.mahony@intel.com)
13 * Adrian Hoban <adrian.hoban@intel.com>
14 * Gabriele Paoloni <gabriele.paoloni@intel.com>
15 * Tadeusz Struk (tadeusz.struk@intel.com)
16 * Copyright (c) 2010, Intel Corporation.
da7f033d
HX
17 */
18
1ce33115 19#include <crypto/aead.h>
da7f033d 20#include <crypto/hash.h>
12773d93 21#include <crypto/skcipher.h>
da7f033d 22#include <linux/err.h>
1c41b882 23#include <linux/fips.h>
da7f033d 24#include <linux/module.h>
3f47a03d 25#include <linux/once.h>
25f9dddb 26#include <linux/random.h>
da7f033d
HX
27#include <linux/scatterlist.h>
28#include <linux/slab.h>
29#include <linux/string.h>
0c3dc787 30#include <linux/uio.h>
7647d6ce 31#include <crypto/rng.h>
64d1cdfb 32#include <crypto/drbg.h>
946cc463 33#include <crypto/akcipher.h>
802c7f1c 34#include <crypto/kpp.h>
d7db7a88 35#include <crypto/acompress.h>
0eb76ba2 36#include <crypto/internal/cipher.h>
b55e1a39 37#include <crypto/internal/simd.h>
da7f033d
HX
38
39#include "internal.h"
0b767f96 40
0eb76ba2
AB
41MODULE_IMPORT_NS(CRYPTO_INTERNAL);
42
9e5c9fe4
RJ
43static bool notests;
44module_param(notests, bool, 0644);
45MODULE_PARM_DESC(notests, "disable crypto self-tests");
46
eda69b0c
EB
47static bool panic_on_fail;
48module_param(panic_on_fail, bool, 0444);
49
5b2706a4
EB
50#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");
58#endif
59
326a6346 60#ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
0b767f96
AS
61
62/* a perfect nop */
63int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
64{
65 return 0;
66}
67
68#else
69
da7f033d
HX
70#include "testmgr.h"
71
72/*
73 * Need slab memory for testing (size in number of pages).
74 */
75#define XBUFSIZE 8
76
da7f033d
HX
77/*
78* Used by test_cipher()
79*/
80#define ENCRYPT 1
81#define DECRYPT 0
82
da7f033d 83struct aead_test_suite {
a0d608ee
EB
84 const struct aead_testvec *vecs;
85 unsigned int count;
49763fc6
EB
86
87 /*
88 * Set if trying to decrypt an inauthentic ciphertext with this
89 * algorithm might result in EINVAL rather than EBADMSG, due to other
90 * validation the algorithm does on the inputs such as length checks.
91 */
92 unsigned int einval_allowed : 1;
93
94 /*
6f3a06d9
EB
95 * Set if this algorithm requires that the IV be located at the end of
96 * the AAD buffer, in addition to being given in the normal way. The
97 * behavior when the two IV copies differ is implementation-defined.
49763fc6 98 */
6f3a06d9 99 unsigned int aad_iv : 1;
da7f033d
HX
100};
101
102struct cipher_test_suite {
92a4c9fe
EB
103 const struct cipher_testvec *vecs;
104 unsigned int count;
da7f033d
HX
105};
106
107struct comp_test_suite {
108 struct {
b13b1e0c 109 const struct comp_testvec *vecs;
da7f033d
HX
110 unsigned int count;
111 } comp, decomp;
112};
113
114struct hash_test_suite {
b13b1e0c 115 const struct hash_testvec *vecs;
da7f033d
HX
116 unsigned int count;
117};
118
7647d6ce 119struct cprng_test_suite {
b13b1e0c 120 const struct cprng_testvec *vecs;
7647d6ce
JW
121 unsigned int count;
122};
123
64d1cdfb 124struct drbg_test_suite {
b13b1e0c 125 const struct drbg_testvec *vecs;
64d1cdfb
SM
126 unsigned int count;
127};
128
946cc463 129struct akcipher_test_suite {
b13b1e0c 130 const struct akcipher_testvec *vecs;
946cc463
TS
131 unsigned int count;
132};
133
802c7f1c 134struct kpp_test_suite {
b13b1e0c 135 const struct kpp_testvec *vecs;
802c7f1c
SB
136 unsigned int count;
137};
138
da7f033d
HX
139struct alg_test_desc {
140 const char *alg;
f2bb770a 141 const char *generic_driver;
da7f033d
HX
142 int (*test)(const struct alg_test_desc *desc, const char *driver,
143 u32 type, u32 mask);
a1915d51 144 int fips_allowed; /* set if alg is allowed in fips mode */
da7f033d
HX
145
146 union {
147 struct aead_test_suite aead;
148 struct cipher_test_suite cipher;
149 struct comp_test_suite comp;
150 struct hash_test_suite hash;
7647d6ce 151 struct cprng_test_suite cprng;
64d1cdfb 152 struct drbg_test_suite drbg;
946cc463 153 struct akcipher_test_suite akcipher;
802c7f1c 154 struct kpp_test_suite kpp;
da7f033d
HX
155 } suite;
156};
157
da7f033d
HX
158static void hexdump(unsigned char *buf, unsigned int len)
159{
160 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
161 16, 1,
162 buf, len, false);
163}
164
3f47a03d 165static int __testmgr_alloc_buf(char *buf[XBUFSIZE], int order)
f8b0d4d0
HX
166{
167 int i;
168
169 for (i = 0; i < XBUFSIZE; i++) {
3f47a03d 170 buf[i] = (char *)__get_free_pages(GFP_KERNEL, order);
f8b0d4d0
HX
171 if (!buf[i])
172 goto err_free_buf;
173 }
174
175 return 0;
176
177err_free_buf:
178 while (i-- > 0)
3f47a03d 179 free_pages((unsigned long)buf[i], order);
f8b0d4d0
HX
180
181 return -ENOMEM;
182}
183
3f47a03d
EB
184static int testmgr_alloc_buf(char *buf[XBUFSIZE])
185{
186 return __testmgr_alloc_buf(buf, 0);
187}
188
189static void __testmgr_free_buf(char *buf[XBUFSIZE], int order)
f8b0d4d0
HX
190{
191 int i;
192
193 for (i = 0; i < XBUFSIZE; i++)
3f47a03d
EB
194 free_pages((unsigned long)buf[i], order);
195}
196
197static void testmgr_free_buf(char *buf[XBUFSIZE])
198{
199 __testmgr_free_buf(buf, 0);
200}
201
202#define TESTMGR_POISON_BYTE 0xfe
203#define TESTMGR_POISON_LEN 16
204
205static inline void testmgr_poison(void *addr, size_t len)
206{
207 memset(addr, TESTMGR_POISON_BYTE, len);
208}
209
210/* Is the memory region still fully poisoned? */
211static inline bool testmgr_is_poison(const void *addr, size_t len)
212{
213 return memchr_inv(addr, TESTMGR_POISON_BYTE, len) == NULL;
214}
215
216/* flush type for hash algorithms */
217enum flush_type {
218 /* merge with update of previous buffer(s) */
219 FLUSH_TYPE_NONE = 0,
220
221 /* update with previous buffer(s) before doing this one */
222 FLUSH_TYPE_FLUSH,
223
224 /* likewise, but also export and re-import the intermediate state */
225 FLUSH_TYPE_REIMPORT,
226};
227
228/* finalization function for hash algorithms */
229enum finalization_type {
230 FINALIZATION_TYPE_FINAL, /* use final() */
231 FINALIZATION_TYPE_FINUP, /* use finup() */
232 FINALIZATION_TYPE_DIGEST, /* use digest() */
233};
234
f17f9e90
EB
235/*
236 * Whether the crypto operation will occur in-place, and if so whether the
237 * source and destination scatterlist pointers will coincide (req->src ==
238 * req->dst), or whether they'll merely point to two separate scatterlists
239 * (req->src != req->dst) that reference the same underlying memory.
240 *
241 * This is only relevant for algorithm types that support in-place operation.
242 */
243enum inplace_mode {
244 OUT_OF_PLACE,
245 INPLACE_ONE_SGLIST,
246 INPLACE_TWO_SGLISTS,
247};
248
3f47a03d
EB
249#define TEST_SG_TOTAL 10000
250
251/**
252 * struct test_sg_division - description of a scatterlist entry
253 *
254 * This struct describes one entry of a scatterlist being constructed to check a
255 * crypto test vector.
256 *
257 * @proportion_of_total: length of this chunk relative to the total length,
258 * given as a proportion out of TEST_SG_TOTAL so that it
259 * scales to fit any test vector
260 * @offset: byte offset into a 2-page buffer at which this chunk will start
261 * @offset_relative_to_alignmask: if true, add the algorithm's alignmask to the
262 * @offset
263 * @flush_type: for hashes, whether an update() should be done now vs.
264 * continuing to accumulate data
6570737c 265 * @nosimd: if doing the pending update(), do it with SIMD disabled?
3f47a03d
EB
266 */
267struct test_sg_division {
268 unsigned int proportion_of_total;
269 unsigned int offset;
270 bool offset_relative_to_alignmask;
271 enum flush_type flush_type;
6570737c 272 bool nosimd;
3f47a03d
EB
273};
274
275/**
276 * struct testvec_config - configuration for testing a crypto test vector
277 *
278 * This struct describes the data layout and other parameters with which each
279 * crypto test vector can be tested.
280 *
281 * @name: name of this config, logged for debugging purposes if a test fails
f17f9e90 282 * @inplace_mode: whether and how to operate on the data in-place, if applicable
3f47a03d
EB
283 * @req_flags: extra request_flags, e.g. CRYPTO_TFM_REQ_MAY_SLEEP
284 * @src_divs: description of how to arrange the source scatterlist
285 * @dst_divs: description of how to arrange the dst scatterlist, if applicable
286 * for the algorithm type. Defaults to @src_divs if unset.
287 * @iv_offset: misalignment of the IV in the range [0..MAX_ALGAPI_ALIGNMASK+1],
288 * where 0 is aligned to a 2*(MAX_ALGAPI_ALIGNMASK+1) byte boundary
289 * @iv_offset_relative_to_alignmask: if true, add the algorithm's alignmask to
290 * the @iv_offset
fd8c37c7
EB
291 * @key_offset: misalignment of the key, where 0 is default alignment
292 * @key_offset_relative_to_alignmask: if true, add the algorithm's alignmask to
293 * the @key_offset
3f47a03d 294 * @finalization_type: what finalization function to use for hashes
6570737c 295 * @nosimd: execute with SIMD disabled? Requires !CRYPTO_TFM_REQ_MAY_SLEEP.
3f47a03d
EB
296 */
297struct testvec_config {
298 const char *name;
f17f9e90 299 enum inplace_mode inplace_mode;
3f47a03d
EB
300 u32 req_flags;
301 struct test_sg_division src_divs[XBUFSIZE];
302 struct test_sg_division dst_divs[XBUFSIZE];
303 unsigned int iv_offset;
fd8c37c7 304 unsigned int key_offset;
3f47a03d 305 bool iv_offset_relative_to_alignmask;
fd8c37c7 306 bool key_offset_relative_to_alignmask;
3f47a03d 307 enum finalization_type finalization_type;
6570737c 308 bool nosimd;
3f47a03d
EB
309};
310
311#define TESTVEC_CONFIG_NAMELEN 192
312
4e7babba
EB
313/*
314 * The following are the lists of testvec_configs to test for each algorithm
315 * type when the basic crypto self-tests are enabled, i.e. when
316 * CONFIG_CRYPTO_MANAGER_DISABLE_TESTS is unset. They aim to provide good test
317 * coverage, while keeping the test time much shorter than the full fuzz tests
318 * so that the basic tests can be enabled in a wider range of circumstances.
319 */
320
321/* Configs for skciphers and aeads */
322static const struct testvec_config default_cipher_testvec_configs[] = {
323 {
f17f9e90
EB
324 .name = "in-place (one sglist)",
325 .inplace_mode = INPLACE_ONE_SGLIST,
326 .src_divs = { { .proportion_of_total = 10000 } },
327 }, {
328 .name = "in-place (two sglists)",
329 .inplace_mode = INPLACE_TWO_SGLISTS,
4e7babba
EB
330 .src_divs = { { .proportion_of_total = 10000 } },
331 }, {
332 .name = "out-of-place",
f17f9e90 333 .inplace_mode = OUT_OF_PLACE,
4e7babba
EB
334 .src_divs = { { .proportion_of_total = 10000 } },
335 }, {
336 .name = "unaligned buffer, offset=1",
337 .src_divs = { { .proportion_of_total = 10000, .offset = 1 } },
338 .iv_offset = 1,
fd8c37c7 339 .key_offset = 1,
4e7babba
EB
340 }, {
341 .name = "buffer aligned only to alignmask",
342 .src_divs = {
343 {
344 .proportion_of_total = 10000,
345 .offset = 1,
346 .offset_relative_to_alignmask = true,
347 },
348 },
349 .iv_offset = 1,
350 .iv_offset_relative_to_alignmask = true,
fd8c37c7
EB
351 .key_offset = 1,
352 .key_offset_relative_to_alignmask = true,
4e7babba
EB
353 }, {
354 .name = "two even aligned splits",
355 .src_divs = {
356 { .proportion_of_total = 5000 },
357 { .proportion_of_total = 5000 },
358 },
acd4045d
ZY
359 }, {
360 .name = "one src, two even splits dst",
361 .inplace_mode = OUT_OF_PLACE,
362 .src_divs = { { .proportion_of_total = 10000 } },
363 .dst_divs = {
364 { .proportion_of_total = 5000 },
365 { .proportion_of_total = 5000 },
366 },
4e7babba
EB
367 }, {
368 .name = "uneven misaligned splits, may sleep",
369 .req_flags = CRYPTO_TFM_REQ_MAY_SLEEP,
370 .src_divs = {
371 { .proportion_of_total = 1900, .offset = 33 },
372 { .proportion_of_total = 3300, .offset = 7 },
373 { .proportion_of_total = 4800, .offset = 18 },
374 },
375 .iv_offset = 3,
fd8c37c7 376 .key_offset = 3,
4e7babba
EB
377 }, {
378 .name = "misaligned splits crossing pages, inplace",
f17f9e90 379 .inplace_mode = INPLACE_ONE_SGLIST,
4e7babba
EB
380 .src_divs = {
381 {
382 .proportion_of_total = 7500,
383 .offset = PAGE_SIZE - 32
384 }, {
385 .proportion_of_total = 2500,
386 .offset = PAGE_SIZE - 7
387 },
388 },
389 }
390};
391
4cc2dcf9
EB
392static const struct testvec_config default_hash_testvec_configs[] = {
393 {
394 .name = "init+update+final aligned buffer",
395 .src_divs = { { .proportion_of_total = 10000 } },
396 .finalization_type = FINALIZATION_TYPE_FINAL,
397 }, {
398 .name = "init+finup aligned buffer",
399 .src_divs = { { .proportion_of_total = 10000 } },
400 .finalization_type = FINALIZATION_TYPE_FINUP,
401 }, {
402 .name = "digest aligned buffer",
403 .src_divs = { { .proportion_of_total = 10000 } },
404 .finalization_type = FINALIZATION_TYPE_DIGEST,
405 }, {
406 .name = "init+update+final misaligned buffer",
407 .src_divs = { { .proportion_of_total = 10000, .offset = 1 } },
408 .finalization_type = FINALIZATION_TYPE_FINAL,
fd8c37c7 409 .key_offset = 1,
4cc2dcf9
EB
410 }, {
411 .name = "digest buffer aligned only to alignmask",
412 .src_divs = {
413 {
414 .proportion_of_total = 10000,
415 .offset = 1,
416 .offset_relative_to_alignmask = true,
417 },
418 },
419 .finalization_type = FINALIZATION_TYPE_DIGEST,
fd8c37c7
EB
420 .key_offset = 1,
421 .key_offset_relative_to_alignmask = true,
4cc2dcf9
EB
422 }, {
423 .name = "init+update+update+final two even splits",
424 .src_divs = {
425 { .proportion_of_total = 5000 },
426 {
427 .proportion_of_total = 5000,
428 .flush_type = FLUSH_TYPE_FLUSH,
429 },
430 },
431 .finalization_type = FINALIZATION_TYPE_FINAL,
432 }, {
433 .name = "digest uneven misaligned splits, may sleep",
434 .req_flags = CRYPTO_TFM_REQ_MAY_SLEEP,
435 .src_divs = {
436 { .proportion_of_total = 1900, .offset = 33 },
437 { .proportion_of_total = 3300, .offset = 7 },
438 { .proportion_of_total = 4800, .offset = 18 },
439 },
440 .finalization_type = FINALIZATION_TYPE_DIGEST,
441 }, {
442 .name = "digest misaligned splits crossing pages",
443 .src_divs = {
444 {
445 .proportion_of_total = 7500,
446 .offset = PAGE_SIZE - 32,
447 }, {
448 .proportion_of_total = 2500,
449 .offset = PAGE_SIZE - 7,
450 },
451 },
452 .finalization_type = FINALIZATION_TYPE_DIGEST,
453 }, {
454 .name = "import/export",
455 .src_divs = {
456 {
457 .proportion_of_total = 6500,
458 .flush_type = FLUSH_TYPE_REIMPORT,
459 }, {
460 .proportion_of_total = 3500,
461 .flush_type = FLUSH_TYPE_REIMPORT,
462 },
463 },
464 .finalization_type = FINALIZATION_TYPE_FINAL,
465 }
466};
467
3f47a03d
EB
468static unsigned int count_test_sg_divisions(const struct test_sg_division *divs)
469{
470 unsigned int remaining = TEST_SG_TOTAL;
471 unsigned int ndivs = 0;
472
473 do {
474 remaining -= divs[ndivs++].proportion_of_total;
475 } while (remaining);
476
477 return ndivs;
478}
479
6570737c
EB
480#define SGDIVS_HAVE_FLUSHES BIT(0)
481#define SGDIVS_HAVE_NOSIMD BIT(1)
482
3f47a03d 483static bool valid_sg_divisions(const struct test_sg_division *divs,
6570737c 484 unsigned int count, int *flags_ret)
3f47a03d
EB
485{
486 unsigned int total = 0;
487 unsigned int i;
488
489 for (i = 0; i < count && total != TEST_SG_TOTAL; i++) {
490 if (divs[i].proportion_of_total <= 0 ||
491 divs[i].proportion_of_total > TEST_SG_TOTAL - total)
492 return false;
493 total += divs[i].proportion_of_total;
494 if (divs[i].flush_type != FLUSH_TYPE_NONE)
6570737c
EB
495 *flags_ret |= SGDIVS_HAVE_FLUSHES;
496 if (divs[i].nosimd)
497 *flags_ret |= SGDIVS_HAVE_NOSIMD;
3f47a03d
EB
498 }
499 return total == TEST_SG_TOTAL &&
500 memchr_inv(&divs[i], 0, (count - i) * sizeof(divs[0])) == NULL;
501}
502
503/*
504 * Check whether the given testvec_config is valid. This isn't strictly needed
505 * since every testvec_config should be valid, but check anyway so that people
506 * don't unknowingly add broken configs that don't do what they wanted.
507 */
508static bool valid_testvec_config(const struct testvec_config *cfg)
509{
6570737c 510 int flags = 0;
3f47a03d
EB
511
512 if (cfg->name == NULL)
513 return false;
514
515 if (!valid_sg_divisions(cfg->src_divs, ARRAY_SIZE(cfg->src_divs),
6570737c 516 &flags))
3f47a03d
EB
517 return false;
518
519 if (cfg->dst_divs[0].proportion_of_total) {
520 if (!valid_sg_divisions(cfg->dst_divs,
6570737c 521 ARRAY_SIZE(cfg->dst_divs), &flags))
3f47a03d
EB
522 return false;
523 } else {
524 if (memchr_inv(cfg->dst_divs, 0, sizeof(cfg->dst_divs)))
525 return false;
526 /* defaults to dst_divs=src_divs */
527 }
528
529 if (cfg->iv_offset +
530 (cfg->iv_offset_relative_to_alignmask ? MAX_ALGAPI_ALIGNMASK : 0) >
531 MAX_ALGAPI_ALIGNMASK + 1)
532 return false;
533
6570737c
EB
534 if ((flags & (SGDIVS_HAVE_FLUSHES | SGDIVS_HAVE_NOSIMD)) &&
535 cfg->finalization_type == FINALIZATION_TYPE_DIGEST)
536 return false;
537
538 if ((cfg->nosimd || (flags & SGDIVS_HAVE_NOSIMD)) &&
539 (cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP))
3f47a03d
EB
540 return false;
541
542 return true;
543}
544
545struct test_sglist {
546 char *bufs[XBUFSIZE];
547 struct scatterlist sgl[XBUFSIZE];
548 struct scatterlist sgl_saved[XBUFSIZE];
549 struct scatterlist *sgl_ptr;
550 unsigned int nents;
551};
552
553static int init_test_sglist(struct test_sglist *tsgl)
554{
555 return __testmgr_alloc_buf(tsgl->bufs, 1 /* two pages per buffer */);
556}
557
558static void destroy_test_sglist(struct test_sglist *tsgl)
559{
560 return __testmgr_free_buf(tsgl->bufs, 1 /* two pages per buffer */);
561}
562
563/**
564 * build_test_sglist() - build a scatterlist for a crypto test
565 *
566 * @tsgl: the scatterlist to build. @tsgl->bufs[] contains an array of 2-page
567 * buffers which the scatterlist @tsgl->sgl[] will be made to point into.
568 * @divs: the layout specification on which the scatterlist will be based
569 * @alignmask: the algorithm's alignmask
570 * @total_len: the total length of the scatterlist to build in bytes
571 * @data: if non-NULL, the buffers will be filled with this data until it ends.
572 * Otherwise the buffers will be poisoned. In both cases, some bytes
573 * past the end of each buffer will be poisoned to help detect overruns.
574 * @out_divs: if non-NULL, the test_sg_division to which each scatterlist entry
575 * corresponds will be returned here. This will match @divs except
576 * that divisions resolving to a length of 0 are omitted as they are
577 * not included in the scatterlist.
578 *
579 * Return: 0 or a -errno value
580 */
581static int build_test_sglist(struct test_sglist *tsgl,
582 const struct test_sg_division *divs,
583 const unsigned int alignmask,
584 const unsigned int total_len,
585 struct iov_iter *data,
586 const struct test_sg_division *out_divs[XBUFSIZE])
587{
588 struct {
589 const struct test_sg_division *div;
590 size_t length;
591 } partitions[XBUFSIZE];
592 const unsigned int ndivs = count_test_sg_divisions(divs);
593 unsigned int len_remaining = total_len;
594 unsigned int i;
595
596 BUILD_BUG_ON(ARRAY_SIZE(partitions) != ARRAY_SIZE(tsgl->sgl));
597 if (WARN_ON(ndivs > ARRAY_SIZE(partitions)))
598 return -EINVAL;
599
600 /* Calculate the (div, length) pairs */
601 tsgl->nents = 0;
602 for (i = 0; i < ndivs; i++) {
603 unsigned int len_this_sg =
604 min(len_remaining,
605 (total_len * divs[i].proportion_of_total +
606 TEST_SG_TOTAL / 2) / TEST_SG_TOTAL);
607
608 if (len_this_sg != 0) {
609 partitions[tsgl->nents].div = &divs[i];
610 partitions[tsgl->nents].length = len_this_sg;
611 tsgl->nents++;
612 len_remaining -= len_this_sg;
613 }
614 }
615 if (tsgl->nents == 0) {
616 partitions[tsgl->nents].div = &divs[0];
617 partitions[tsgl->nents].length = 0;
618 tsgl->nents++;
619 }
620 partitions[tsgl->nents - 1].length += len_remaining;
621
622 /* Set up the sgl entries and fill the data or poison */
623 sg_init_table(tsgl->sgl, tsgl->nents);
624 for (i = 0; i < tsgl->nents; i++) {
625 unsigned int offset = partitions[i].div->offset;
626 void *addr;
627
628 if (partitions[i].div->offset_relative_to_alignmask)
629 offset += alignmask;
630
631 while (offset + partitions[i].length + TESTMGR_POISON_LEN >
632 2 * PAGE_SIZE) {
633 if (WARN_ON(offset <= 0))
634 return -EINVAL;
635 offset /= 2;
636 }
637
638 addr = &tsgl->bufs[i][offset];
639 sg_set_buf(&tsgl->sgl[i], addr, partitions[i].length);
640
641 if (out_divs)
642 out_divs[i] = partitions[i].div;
643
644 if (data) {
645 size_t copy_len, copied;
646
647 copy_len = min(partitions[i].length, data->count);
648 copied = copy_from_iter(addr, copy_len, data);
649 if (WARN_ON(copied != copy_len))
650 return -EINVAL;
651 testmgr_poison(addr + copy_len, partitions[i].length +
652 TESTMGR_POISON_LEN - copy_len);
653 } else {
654 testmgr_poison(addr, partitions[i].length +
655 TESTMGR_POISON_LEN);
656 }
657 }
658
659 sg_mark_end(&tsgl->sgl[tsgl->nents - 1]);
660 tsgl->sgl_ptr = tsgl->sgl;
661 memcpy(tsgl->sgl_saved, tsgl->sgl, tsgl->nents * sizeof(tsgl->sgl[0]));
662 return 0;
663}
664
665/*
666 * Verify that a scatterlist crypto operation produced the correct output.
667 *
668 * @tsgl: scatterlist containing the actual output
669 * @expected_output: buffer containing the expected output
670 * @len_to_check: length of @expected_output in bytes
671 * @unchecked_prefix_len: number of ignored bytes in @tsgl prior to real result
672 * @check_poison: verify that the poison bytes after each chunk are intact?
673 *
674 * Return: 0 if correct, -EINVAL if incorrect, -EOVERFLOW if buffer overrun.
675 */
676static int verify_correct_output(const struct test_sglist *tsgl,
677 const char *expected_output,
678 unsigned int len_to_check,
679 unsigned int unchecked_prefix_len,
680 bool check_poison)
681{
682 unsigned int i;
683
684 for (i = 0; i < tsgl->nents; i++) {
685 struct scatterlist *sg = &tsgl->sgl_ptr[i];
686 unsigned int len = sg->length;
687 unsigned int offset = sg->offset;
688 const char *actual_output;
689
690 if (unchecked_prefix_len) {
691 if (unchecked_prefix_len >= len) {
692 unchecked_prefix_len -= len;
693 continue;
694 }
695 offset += unchecked_prefix_len;
696 len -= unchecked_prefix_len;
697 unchecked_prefix_len = 0;
698 }
699 len = min(len, len_to_check);
700 actual_output = page_address(sg_page(sg)) + offset;
701 if (memcmp(expected_output, actual_output, len) != 0)
702 return -EINVAL;
703 if (check_poison &&
704 !testmgr_is_poison(actual_output + len, TESTMGR_POISON_LEN))
705 return -EOVERFLOW;
706 len_to_check -= len;
707 expected_output += len;
708 }
709 if (WARN_ON(len_to_check != 0))
710 return -EINVAL;
711 return 0;
712}
713
714static bool is_test_sglist_corrupted(const struct test_sglist *tsgl)
715{
716 unsigned int i;
717
718 for (i = 0; i < tsgl->nents; i++) {
719 if (tsgl->sgl[i].page_link != tsgl->sgl_saved[i].page_link)
720 return true;
721 if (tsgl->sgl[i].offset != tsgl->sgl_saved[i].offset)
722 return true;
723 if (tsgl->sgl[i].length != tsgl->sgl_saved[i].length)
724 return true;
725 }
726 return false;
727}
728
729struct cipher_test_sglists {
730 struct test_sglist src;
731 struct test_sglist dst;
732};
733
734static struct cipher_test_sglists *alloc_cipher_test_sglists(void)
735{
736 struct cipher_test_sglists *tsgls;
737
738 tsgls = kmalloc(sizeof(*tsgls), GFP_KERNEL);
739 if (!tsgls)
740 return NULL;
741
742 if (init_test_sglist(&tsgls->src) != 0)
743 goto fail_kfree;
744 if (init_test_sglist(&tsgls->dst) != 0)
745 goto fail_destroy_src;
746
747 return tsgls;
748
749fail_destroy_src:
750 destroy_test_sglist(&tsgls->src);
751fail_kfree:
752 kfree(tsgls);
753 return NULL;
754}
755
756static void free_cipher_test_sglists(struct cipher_test_sglists *tsgls)
757{
758 if (tsgls) {
759 destroy_test_sglist(&tsgls->src);
760 destroy_test_sglist(&tsgls->dst);
761 kfree(tsgls);
762 }
763}
764
765/* Build the src and dst scatterlists for an skcipher or AEAD test */
766static int build_cipher_test_sglists(struct cipher_test_sglists *tsgls,
767 const struct testvec_config *cfg,
768 unsigned int alignmask,
769 unsigned int src_total_len,
770 unsigned int dst_total_len,
771 const struct kvec *inputs,
772 unsigned int nr_inputs)
773{
774 struct iov_iter input;
775 int err;
776
de4eda9d 777 iov_iter_kvec(&input, ITER_SOURCE, inputs, nr_inputs, src_total_len);
3f47a03d 778 err = build_test_sglist(&tsgls->src, cfg->src_divs, alignmask,
f17f9e90 779 cfg->inplace_mode != OUT_OF_PLACE ?
3f47a03d
EB
780 max(dst_total_len, src_total_len) :
781 src_total_len,
782 &input, NULL);
783 if (err)
784 return err;
785
f17f9e90
EB
786 /*
787 * In-place crypto operations can use the same scatterlist for both the
788 * source and destination (req->src == req->dst), or can use separate
789 * scatterlists (req->src != req->dst) which point to the same
790 * underlying memory. Make sure to test both cases.
791 */
792 if (cfg->inplace_mode == INPLACE_ONE_SGLIST) {
3f47a03d
EB
793 tsgls->dst.sgl_ptr = tsgls->src.sgl;
794 tsgls->dst.nents = tsgls->src.nents;
795 return 0;
796 }
f17f9e90
EB
797 if (cfg->inplace_mode == INPLACE_TWO_SGLISTS) {
798 /*
799 * For now we keep it simple and only test the case where the
800 * two scatterlists have identical entries, rather than
801 * different entries that split up the same memory differently.
802 */
803 memcpy(tsgls->dst.sgl, tsgls->src.sgl,
804 tsgls->src.nents * sizeof(tsgls->src.sgl[0]));
805 memcpy(tsgls->dst.sgl_saved, tsgls->src.sgl,
806 tsgls->src.nents * sizeof(tsgls->src.sgl[0]));
807 tsgls->dst.sgl_ptr = tsgls->dst.sgl;
808 tsgls->dst.nents = tsgls->src.nents;
809 return 0;
810 }
811 /* Out of place */
3f47a03d
EB
812 return build_test_sglist(&tsgls->dst,
813 cfg->dst_divs[0].proportion_of_total ?
814 cfg->dst_divs : cfg->src_divs,
815 alignmask, dst_total_len, NULL, NULL);
f8b0d4d0
HX
816}
817
fd8c37c7
EB
818/*
819 * Support for testing passing a misaligned key to setkey():
820 *
821 * If cfg->key_offset is set, copy the key into a new buffer at that offset,
822 * optionally adding alignmask. Else, just use the key directly.
823 */
824static int prepare_keybuf(const u8 *key, unsigned int ksize,
825 const struct testvec_config *cfg,
826 unsigned int alignmask,
827 const u8 **keybuf_ret, const u8 **keyptr_ret)
828{
829 unsigned int key_offset = cfg->key_offset;
830 u8 *keybuf = NULL, *keyptr = (u8 *)key;
831
832 if (key_offset != 0) {
833 if (cfg->key_offset_relative_to_alignmask)
834 key_offset += alignmask;
835 keybuf = kmalloc(key_offset + ksize, GFP_KERNEL);
836 if (!keybuf)
837 return -ENOMEM;
838 keyptr = keybuf + key_offset;
839 memcpy(keyptr, key, ksize);
840 }
841 *keybuf_ret = keybuf;
842 *keyptr_ret = keyptr;
843 return 0;
844}
845
846/* Like setkey_f(tfm, key, ksize), but sometimes misalign the key */
847#define do_setkey(setkey_f, tfm, key, ksize, cfg, alignmask) \
848({ \
849 const u8 *keybuf, *keyptr; \
850 int err; \
851 \
852 err = prepare_keybuf((key), (ksize), (cfg), (alignmask), \
853 &keybuf, &keyptr); \
854 if (err == 0) { \
855 err = setkey_f((tfm), keyptr, (ksize)); \
856 kfree(keybuf); \
857 } \
858 err; \
859})
860
25f9dddb 861#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
f2bb770a 862
f900fde2
EB
863/*
864 * The fuzz tests use prandom instead of the normal Linux RNG since they don't
865 * need cryptographically secure random numbers. This greatly improves the
866 * performance of these tests, especially if they are run before the Linux RNG
867 * has been initialized or if they are run on a lockdep-enabled kernel.
868 */
869
870static inline void init_rnd_state(struct rnd_state *rng)
871{
872 prandom_seed_state(rng, get_random_u64());
873}
874
875static inline u8 prandom_u8(struct rnd_state *rng)
876{
877 return prandom_u32_state(rng);
878}
879
880static inline u32 prandom_u32_below(struct rnd_state *rng, u32 ceil)
881{
882 /*
883 * This is slightly biased for non-power-of-2 values of 'ceil', but this
884 * isn't important here.
885 */
886 return prandom_u32_state(rng) % ceil;
887}
888
889static inline bool prandom_bool(struct rnd_state *rng)
890{
891 return prandom_u32_below(rng, 2);
892}
893
894static inline u32 prandom_u32_inclusive(struct rnd_state *rng,
895 u32 floor, u32 ceil)
896{
897 return floor + prandom_u32_below(rng, ceil - floor + 1);
898}
899
f2bb770a 900/* Generate a random length in range [0, max_len], but prefer smaller values */
f900fde2
EB
901static unsigned int generate_random_length(struct rnd_state *rng,
902 unsigned int max_len)
f2bb770a 903{
f900fde2 904 unsigned int len = prandom_u32_below(rng, max_len + 1);
f2bb770a 905
f900fde2 906 switch (prandom_u32_below(rng, 4)) {
f2bb770a
EB
907 case 0:
908 return len % 64;
909 case 1:
910 return len % 256;
911 case 2:
912 return len % 1024;
913 default:
914 return len;
915 }
916}
917
49763fc6 918/* Flip a random bit in the given nonempty data buffer */
f900fde2 919static void flip_random_bit(struct rnd_state *rng, u8 *buf, size_t size)
49763fc6
EB
920{
921 size_t bitpos;
922
f900fde2 923 bitpos = prandom_u32_below(rng, size * 8);
49763fc6
EB
924 buf[bitpos / 8] ^= 1 << (bitpos % 8);
925}
926
927/* Flip a random byte in the given nonempty data buffer */
f900fde2 928static void flip_random_byte(struct rnd_state *rng, u8 *buf, size_t size)
49763fc6 929{
f900fde2 930 buf[prandom_u32_below(rng, size)] ^= 0xff;
49763fc6
EB
931}
932
933/* Sometimes make some random changes to the given nonempty data buffer */
f900fde2 934static void mutate_buffer(struct rnd_state *rng, u8 *buf, size_t size)
f2bb770a
EB
935{
936 size_t num_flips;
937 size_t i;
f2bb770a
EB
938
939 /* Sometimes flip some bits */
f900fde2
EB
940 if (prandom_u32_below(rng, 4) == 0) {
941 num_flips = min_t(size_t, 1 << prandom_u32_below(rng, 8),
942 size * 8);
49763fc6 943 for (i = 0; i < num_flips; i++)
f900fde2 944 flip_random_bit(rng, buf, size);
f2bb770a
EB
945 }
946
947 /* Sometimes flip some bytes */
f900fde2
EB
948 if (prandom_u32_below(rng, 4) == 0) {
949 num_flips = min_t(size_t, 1 << prandom_u32_below(rng, 8), size);
f2bb770a 950 for (i = 0; i < num_flips; i++)
f900fde2 951 flip_random_byte(rng, buf, size);
f2bb770a
EB
952 }
953}
954
955/* Randomly generate 'count' bytes, but sometimes make them "interesting" */
f900fde2 956static void generate_random_bytes(struct rnd_state *rng, u8 *buf, size_t count)
f2bb770a
EB
957{
958 u8 b;
959 u8 increment;
960 size_t i;
961
962 if (count == 0)
963 return;
964
f900fde2 965 switch (prandom_u32_below(rng, 8)) { /* Choose a generation strategy */
f2bb770a
EB
966 case 0:
967 case 1:
968 /* All the same byte, plus optional mutations */
f900fde2 969 switch (prandom_u32_below(rng, 4)) {
f2bb770a
EB
970 case 0:
971 b = 0x00;
972 break;
973 case 1:
974 b = 0xff;
975 break;
976 default:
f900fde2 977 b = prandom_u8(rng);
f2bb770a
EB
978 break;
979 }
980 memset(buf, b, count);
f900fde2 981 mutate_buffer(rng, buf, count);
f2bb770a
EB
982 break;
983 case 2:
984 /* Ascending or descending bytes, plus optional mutations */
f900fde2
EB
985 increment = prandom_u8(rng);
986 b = prandom_u8(rng);
f2bb770a
EB
987 for (i = 0; i < count; i++, b += increment)
988 buf[i] = b;
f900fde2 989 mutate_buffer(rng, buf, count);
f2bb770a
EB
990 break;
991 default:
992 /* Fully random bytes */
f900fde2 993 prandom_bytes_state(rng, buf, count);
f2bb770a
EB
994 }
995}
996
f900fde2
EB
997static char *generate_random_sgl_divisions(struct rnd_state *rng,
998 struct test_sg_division *divs,
25f9dddb 999 size_t max_divs, char *p, char *end,
6570737c 1000 bool gen_flushes, u32 req_flags)
25f9dddb
EB
1001{
1002 struct test_sg_division *div = divs;
1003 unsigned int remaining = TEST_SG_TOTAL;
1004
1005 do {
1006 unsigned int this_len;
6570737c 1007 const char *flushtype_str;
25f9dddb 1008
f900fde2 1009 if (div == &divs[max_divs - 1] || prandom_bool(rng))
25f9dddb
EB
1010 this_len = remaining;
1011 else
f900fde2 1012 this_len = prandom_u32_inclusive(rng, 1, remaining);
25f9dddb
EB
1013 div->proportion_of_total = this_len;
1014
f900fde2
EB
1015 if (prandom_u32_below(rng, 4) == 0)
1016 div->offset = prandom_u32_inclusive(rng,
1017 PAGE_SIZE - 128,
1018 PAGE_SIZE - 1);
1019 else if (prandom_bool(rng))
1020 div->offset = prandom_u32_below(rng, 32);
25f9dddb 1021 else
f900fde2
EB
1022 div->offset = prandom_u32_below(rng, PAGE_SIZE);
1023 if (prandom_u32_below(rng, 8) == 0)
25f9dddb
EB
1024 div->offset_relative_to_alignmask = true;
1025
1026 div->flush_type = FLUSH_TYPE_NONE;
1027 if (gen_flushes) {
f900fde2 1028 switch (prandom_u32_below(rng, 4)) {
25f9dddb
EB
1029 case 0:
1030 div->flush_type = FLUSH_TYPE_REIMPORT;
1031 break;
1032 case 1:
1033 div->flush_type = FLUSH_TYPE_FLUSH;
1034 break;
1035 }
1036 }
1037
6570737c
EB
1038 if (div->flush_type != FLUSH_TYPE_NONE &&
1039 !(req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) &&
f900fde2 1040 prandom_bool(rng))
6570737c
EB
1041 div->nosimd = true;
1042
1043 switch (div->flush_type) {
1044 case FLUSH_TYPE_FLUSH:
1045 if (div->nosimd)
1046 flushtype_str = "<flush,nosimd>";
1047 else
1048 flushtype_str = "<flush>";
1049 break;
1050 case FLUSH_TYPE_REIMPORT:
1051 if (div->nosimd)
1052 flushtype_str = "<reimport,nosimd>";
1053 else
1054 flushtype_str = "<reimport>";
1055 break;
1056 default:
1057 flushtype_str = "";
1058 break;
1059 }
1060
25f9dddb 1061 BUILD_BUG_ON(TEST_SG_TOTAL != 10000); /* for "%u.%u%%" */
6570737c 1062 p += scnprintf(p, end - p, "%s%u.%u%%@%s+%u%s", flushtype_str,
25f9dddb
EB
1063 this_len / 100, this_len % 100,
1064 div->offset_relative_to_alignmask ?
1065 "alignmask" : "",
1066 div->offset, this_len == remaining ? "" : ", ");
1067 remaining -= this_len;
1068 div++;
1069 } while (remaining);
1070
1071 return p;
1072}
1073
1074/* Generate a random testvec_config for fuzz testing */
f900fde2
EB
1075static void generate_random_testvec_config(struct rnd_state *rng,
1076 struct testvec_config *cfg,
25f9dddb
EB
1077 char *name, size_t max_namelen)
1078{
1079 char *p = name;
1080 char * const end = name + max_namelen;
1081
1082 memset(cfg, 0, sizeof(*cfg));
1083
1084 cfg->name = name;
1085
1086 p += scnprintf(p, end - p, "random:");
1087
f900fde2 1088 switch (prandom_u32_below(rng, 4)) {
f17f9e90
EB
1089 case 0:
1090 case 1:
1091 cfg->inplace_mode = OUT_OF_PLACE;
1092 break;
1093 case 2:
1094 cfg->inplace_mode = INPLACE_ONE_SGLIST;
1095 p += scnprintf(p, end - p, " inplace_one_sglist");
1096 break;
1097 default:
1098 cfg->inplace_mode = INPLACE_TWO_SGLISTS;
1099 p += scnprintf(p, end - p, " inplace_two_sglists");
1100 break;
25f9dddb
EB
1101 }
1102
f900fde2 1103 if (prandom_bool(rng)) {
25f9dddb
EB
1104 cfg->req_flags |= CRYPTO_TFM_REQ_MAY_SLEEP;
1105 p += scnprintf(p, end - p, " may_sleep");
1106 }
1107
f900fde2 1108 switch (prandom_u32_below(rng, 4)) {
25f9dddb
EB
1109 case 0:
1110 cfg->finalization_type = FINALIZATION_TYPE_FINAL;
1111 p += scnprintf(p, end - p, " use_final");
1112 break;
1113 case 1:
1114 cfg->finalization_type = FINALIZATION_TYPE_FINUP;
1115 p += scnprintf(p, end - p, " use_finup");
1116 break;
1117 default:
1118 cfg->finalization_type = FINALIZATION_TYPE_DIGEST;
1119 p += scnprintf(p, end - p, " use_digest");
1120 break;
1121 }
1122
f900fde2 1123 if (!(cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) && prandom_bool(rng)) {
6570737c
EB
1124 cfg->nosimd = true;
1125 p += scnprintf(p, end - p, " nosimd");
1126 }
1127
25f9dddb 1128 p += scnprintf(p, end - p, " src_divs=[");
f900fde2 1129 p = generate_random_sgl_divisions(rng, cfg->src_divs,
25f9dddb
EB
1130 ARRAY_SIZE(cfg->src_divs), p, end,
1131 (cfg->finalization_type !=
6570737c
EB
1132 FINALIZATION_TYPE_DIGEST),
1133 cfg->req_flags);
25f9dddb
EB
1134 p += scnprintf(p, end - p, "]");
1135
f900fde2 1136 if (cfg->inplace_mode == OUT_OF_PLACE && prandom_bool(rng)) {
25f9dddb 1137 p += scnprintf(p, end - p, " dst_divs=[");
f900fde2 1138 p = generate_random_sgl_divisions(rng, cfg->dst_divs,
25f9dddb 1139 ARRAY_SIZE(cfg->dst_divs),
6570737c
EB
1140 p, end, false,
1141 cfg->req_flags);
25f9dddb
EB
1142 p += scnprintf(p, end - p, "]");
1143 }
1144
f900fde2
EB
1145 if (prandom_bool(rng)) {
1146 cfg->iv_offset = prandom_u32_inclusive(rng, 1,
1147 MAX_ALGAPI_ALIGNMASK);
25f9dddb
EB
1148 p += scnprintf(p, end - p, " iv_offset=%u", cfg->iv_offset);
1149 }
1150
f900fde2
EB
1151 if (prandom_bool(rng)) {
1152 cfg->key_offset = prandom_u32_inclusive(rng, 1,
1153 MAX_ALGAPI_ALIGNMASK);
fd8c37c7
EB
1154 p += scnprintf(p, end - p, " key_offset=%u", cfg->key_offset);
1155 }
1156
25f9dddb
EB
1157 WARN_ON_ONCE(!valid_testvec_config(cfg));
1158}
b55e1a39
EB
1159
1160static void crypto_disable_simd_for_test(void)
1161{
82e269ad 1162 migrate_disable();
b55e1a39
EB
1163 __this_cpu_write(crypto_simd_disabled_for_test, true);
1164}
1165
1166static void crypto_reenable_simd_for_test(void)
1167{
1168 __this_cpu_write(crypto_simd_disabled_for_test, false);
82e269ad 1169 migrate_enable();
b55e1a39 1170}
f2bb770a
EB
1171
1172/*
1173 * Given an algorithm name, build the name of the generic implementation of that
1174 * algorithm, assuming the usual naming convention. Specifically, this appends
1175 * "-generic" to every part of the name that is not a template name. Examples:
1176 *
1177 * aes => aes-generic
1178 * cbc(aes) => cbc(aes-generic)
1179 * cts(cbc(aes)) => cts(cbc(aes-generic))
1180 * rfc7539(chacha20,poly1305) => rfc7539(chacha20-generic,poly1305-generic)
1181 *
1182 * Return: 0 on success, or -ENAMETOOLONG if the generic name would be too long
1183 */
1184static int build_generic_driver_name(const char *algname,
1185 char driver_name[CRYPTO_MAX_ALG_NAME])
1186{
1187 const char *in = algname;
1188 char *out = driver_name;
1189 size_t len = strlen(algname);
1190
1191 if (len >= CRYPTO_MAX_ALG_NAME)
1192 goto too_long;
1193 do {
1194 const char *in_saved = in;
1195
1196 while (*in && *in != '(' && *in != ')' && *in != ',')
1197 *out++ = *in++;
1198 if (*in != '(' && in > in_saved) {
1199 len += 8;
1200 if (len >= CRYPTO_MAX_ALG_NAME)
1201 goto too_long;
1202 memcpy(out, "-generic", 8);
1203 out += 8;
1204 }
1205 } while ((*out++ = *in++) != '\0');
1206 return 0;
1207
1208too_long:
1209 pr_err("alg: generic driver name for \"%s\" would be too long\n",
1210 algname);
1211 return -ENAMETOOLONG;
1212}
b55e1a39
EB
1213#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1214static void crypto_disable_simd_for_test(void)
1215{
1216}
1217
1218static void crypto_reenable_simd_for_test(void)
1219{
1220}
1221#endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
25f9dddb 1222
d8ea98aa
EB
1223static int build_hash_sglist(struct test_sglist *tsgl,
1224 const struct hash_testvec *vec,
1225 const struct testvec_config *cfg,
1226 unsigned int alignmask,
1227 const struct test_sg_division *divs[XBUFSIZE])
1228{
1229 struct kvec kv;
1230 struct iov_iter input;
1231
1232 kv.iov_base = (void *)vec->plaintext;
1233 kv.iov_len = vec->psize;
de4eda9d 1234 iov_iter_kvec(&input, ITER_SOURCE, &kv, 1, vec->psize);
d8ea98aa
EB
1235 return build_test_sglist(tsgl, cfg->src_divs, alignmask, vec->psize,
1236 &input, divs);
1237}
1238
1239static int check_hash_result(const char *type,
1240 const u8 *result, unsigned int digestsize,
1241 const struct hash_testvec *vec,
1242 const char *vec_name,
1243 const char *driver,
1244 const struct testvec_config *cfg)
1245{
1246 if (memcmp(result, vec->digest, digestsize) != 0) {
1247 pr_err("alg: %s: %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
1248 type, driver, vec_name, cfg->name);
1249 return -EINVAL;
1250 }
1251 if (!testmgr_is_poison(&result[digestsize], TESTMGR_POISON_LEN)) {
1252 pr_err("alg: %s: %s overran result buffer on test vector %s, cfg=\"%s\"\n",
1253 type, driver, vec_name, cfg->name);
1254 return -EOVERFLOW;
1255 }
1256 return 0;
1257}
1258
1259static inline int check_shash_op(const char *op, int err,
1260 const char *driver, const char *vec_name,
1261 const struct testvec_config *cfg)
1262{
1263 if (err)
1264 pr_err("alg: shash: %s %s() failed with err %d on test vector %s, cfg=\"%s\"\n",
1265 driver, op, err, vec_name, cfg->name);
1266 return err;
1267}
1268
d8ea98aa 1269/* Test one hash test vector in one configuration, using the shash API */
79cafe9a 1270static int test_shash_vec_cfg(const struct hash_testvec *vec,
d8ea98aa
EB
1271 const char *vec_name,
1272 const struct testvec_config *cfg,
1273 struct shash_desc *desc,
1274 struct test_sglist *tsgl,
1275 u8 *hashstate)
1276{
1277 struct crypto_shash *tfm = desc->tfm;
1278 const unsigned int alignmask = crypto_shash_alignmask(tfm);
1279 const unsigned int digestsize = crypto_shash_digestsize(tfm);
1280 const unsigned int statesize = crypto_shash_statesize(tfm);
79cafe9a 1281 const char *driver = crypto_shash_driver_name(tfm);
d8ea98aa
EB
1282 const struct test_sg_division *divs[XBUFSIZE];
1283 unsigned int i;
1284 u8 result[HASH_MAX_DIGESTSIZE + TESTMGR_POISON_LEN];
1285 int err;
1286
1287 /* Set the key, if specified */
1288 if (vec->ksize) {
fd8c37c7
EB
1289 err = do_setkey(crypto_shash_setkey, tfm, vec->key, vec->ksize,
1290 cfg, alignmask);
d8ea98aa
EB
1291 if (err) {
1292 if (err == vec->setkey_error)
1293 return 0;
1294 pr_err("alg: shash: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1295 driver, vec_name, vec->setkey_error, err,
1296 crypto_shash_get_flags(tfm));
1297 return err;
1298 }
1299 if (vec->setkey_error) {
1300 pr_err("alg: shash: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1301 driver, vec_name, vec->setkey_error);
1302 return -EINVAL;
1303 }
1304 }
1305
1306 /* Build the scatterlist for the source data */
1307 err = build_hash_sglist(tsgl, vec, cfg, alignmask, divs);
1308 if (err) {
1309 pr_err("alg: shash: %s: error preparing scatterlist for test vector %s, cfg=\"%s\"\n",
1310 driver, vec_name, cfg->name);
1311 return err;
1312 }
1313
1314 /* Do the actual hashing */
1315
1316 testmgr_poison(desc->__ctx, crypto_shash_descsize(tfm));
1317 testmgr_poison(result, digestsize + TESTMGR_POISON_LEN);
1318
1319 if (cfg->finalization_type == FINALIZATION_TYPE_DIGEST ||
1320 vec->digest_error) {
1321 /* Just using digest() */
1322 if (tsgl->nents != 1)
1323 return 0;
1324 if (cfg->nosimd)
1325 crypto_disable_simd_for_test();
e40ff6f3 1326 err = crypto_shash_digest(desc, sg_virt(&tsgl->sgl[0]),
d8ea98aa
EB
1327 tsgl->sgl[0].length, result);
1328 if (cfg->nosimd)
1329 crypto_reenable_simd_for_test();
1330 if (err) {
1331 if (err == vec->digest_error)
1332 return 0;
1333 pr_err("alg: shash: %s digest() failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
1334 driver, vec_name, vec->digest_error, err,
1335 cfg->name);
1336 return err;
1337 }
1338 if (vec->digest_error) {
1339 pr_err("alg: shash: %s digest() unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
1340 driver, vec_name, vec->digest_error, cfg->name);
1341 return -EINVAL;
1342 }
1343 goto result_ready;
1344 }
1345
1346 /* Using init(), zero or more update(), then final() or finup() */
1347
1348 if (cfg->nosimd)
1349 crypto_disable_simd_for_test();
1350 err = crypto_shash_init(desc);
1351 if (cfg->nosimd)
1352 crypto_reenable_simd_for_test();
1353 err = check_shash_op("init", err, driver, vec_name, cfg);
1354 if (err)
1355 return err;
1356
1357 for (i = 0; i < tsgl->nents; i++) {
1358 if (i + 1 == tsgl->nents &&
1359 cfg->finalization_type == FINALIZATION_TYPE_FINUP) {
1360 if (divs[i]->nosimd)
1361 crypto_disable_simd_for_test();
e40ff6f3 1362 err = crypto_shash_finup(desc, sg_virt(&tsgl->sgl[i]),
d8ea98aa
EB
1363 tsgl->sgl[i].length, result);
1364 if (divs[i]->nosimd)
1365 crypto_reenable_simd_for_test();
1366 err = check_shash_op("finup", err, driver, vec_name,
1367 cfg);
1368 if (err)
1369 return err;
1370 goto result_ready;
1371 }
1372 if (divs[i]->nosimd)
1373 crypto_disable_simd_for_test();
e40ff6f3 1374 err = crypto_shash_update(desc, sg_virt(&tsgl->sgl[i]),
d8ea98aa
EB
1375 tsgl->sgl[i].length);
1376 if (divs[i]->nosimd)
1377 crypto_reenable_simd_for_test();
1378 err = check_shash_op("update", err, driver, vec_name, cfg);
1379 if (err)
1380 return err;
1381 if (divs[i]->flush_type == FLUSH_TYPE_REIMPORT) {
1382 /* Test ->export() and ->import() */
1383 testmgr_poison(hashstate + statesize,
1384 TESTMGR_POISON_LEN);
1385 err = crypto_shash_export(desc, hashstate);
1386 err = check_shash_op("export", err, driver, vec_name,
1387 cfg);
1388 if (err)
1389 return err;
1390 if (!testmgr_is_poison(hashstate + statesize,
1391 TESTMGR_POISON_LEN)) {
1392 pr_err("alg: shash: %s export() overran state buffer on test vector %s, cfg=\"%s\"\n",
1393 driver, vec_name, cfg->name);
1394 return -EOVERFLOW;
1395 }
1396 testmgr_poison(desc->__ctx, crypto_shash_descsize(tfm));
1397 err = crypto_shash_import(desc, hashstate);
1398 err = check_shash_op("import", err, driver, vec_name,
1399 cfg);
1400 if (err)
1401 return err;
1402 }
1403 }
1404
1405 if (cfg->nosimd)
1406 crypto_disable_simd_for_test();
1407 err = crypto_shash_final(desc, result);
1408 if (cfg->nosimd)
1409 crypto_reenable_simd_for_test();
1410 err = check_shash_op("final", err, driver, vec_name, cfg);
1411 if (err)
1412 return err;
1413result_ready:
1414 return check_hash_result("shash", result, digestsize, vec, vec_name,
1415 driver, cfg);
1416}
1417
6570737c
EB
1418static int do_ahash_op(int (*op)(struct ahash_request *req),
1419 struct ahash_request *req,
1420 struct crypto_wait *wait, bool nosimd)
1421{
1422 int err;
1423
1424 if (nosimd)
1425 crypto_disable_simd_for_test();
1426
1427 err = op(req);
1428
1429 if (nosimd)
1430 crypto_reenable_simd_for_test();
1431
1432 return crypto_wait_req(err, wait);
1433}
1434
d8ea98aa
EB
1435static int check_nonfinal_ahash_op(const char *op, int err,
1436 u8 *result, unsigned int digestsize,
1437 const char *driver, const char *vec_name,
1438 const struct testvec_config *cfg)
466d7b9f 1439{
4cc2dcf9 1440 if (err) {
d8ea98aa 1441 pr_err("alg: ahash: %s %s() failed with err %d on test vector %s, cfg=\"%s\"\n",
951d1332 1442 driver, op, err, vec_name, cfg->name);
4cc2dcf9 1443 return err;
018ba95c 1444 }
4cc2dcf9 1445 if (!testmgr_is_poison(result, digestsize)) {
d8ea98aa 1446 pr_err("alg: ahash: %s %s() used result buffer on test vector %s, cfg=\"%s\"\n",
951d1332 1447 driver, op, vec_name, cfg->name);
4cc2dcf9 1448 return -EINVAL;
466d7b9f 1449 }
4cc2dcf9 1450 return 0;
018ba95c
WR
1451}
1452
d8ea98aa 1453/* Test one hash test vector in one configuration, using the ahash API */
79cafe9a 1454static int test_ahash_vec_cfg(const struct hash_testvec *vec,
d8ea98aa
EB
1455 const char *vec_name,
1456 const struct testvec_config *cfg,
1457 struct ahash_request *req,
1458 struct test_sglist *tsgl,
1459 u8 *hashstate)
da7f033d 1460{
4cc2dcf9
EB
1461 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1462 const unsigned int alignmask = crypto_ahash_alignmask(tfm);
1463 const unsigned int digestsize = crypto_ahash_digestsize(tfm);
1464 const unsigned int statesize = crypto_ahash_statesize(tfm);
79cafe9a 1465 const char *driver = crypto_ahash_driver_name(tfm);
4cc2dcf9
EB
1466 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1467 const struct test_sg_division *divs[XBUFSIZE];
1468 DECLARE_CRYPTO_WAIT(wait);
4cc2dcf9
EB
1469 unsigned int i;
1470 struct scatterlist *pending_sgl;
1471 unsigned int pending_len;
1472 u8 result[HASH_MAX_DIGESTSIZE + TESTMGR_POISON_LEN];
1473 int err;
da7f033d 1474
4cc2dcf9
EB
1475 /* Set the key, if specified */
1476 if (vec->ksize) {
fd8c37c7
EB
1477 err = do_setkey(crypto_ahash_setkey, tfm, vec->key, vec->ksize,
1478 cfg, alignmask);
4cc2dcf9 1479 if (err) {
5283a8ee
EB
1480 if (err == vec->setkey_error)
1481 return 0;
d8ea98aa 1482 pr_err("alg: ahash: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
951d1332 1483 driver, vec_name, vec->setkey_error, err,
4cc2dcf9
EB
1484 crypto_ahash_get_flags(tfm));
1485 return err;
1486 }
5283a8ee 1487 if (vec->setkey_error) {
d8ea98aa 1488 pr_err("alg: ahash: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
951d1332 1489 driver, vec_name, vec->setkey_error);
5283a8ee
EB
1490 return -EINVAL;
1491 }
4cc2dcf9 1492 }
da7f033d 1493
4cc2dcf9 1494 /* Build the scatterlist for the source data */
d8ea98aa 1495 err = build_hash_sglist(tsgl, vec, cfg, alignmask, divs);
4cc2dcf9 1496 if (err) {
d8ea98aa 1497 pr_err("alg: ahash: %s: error preparing scatterlist for test vector %s, cfg=\"%s\"\n",
951d1332 1498 driver, vec_name, cfg->name);
4cc2dcf9 1499 return err;
da7f033d 1500 }
da7f033d 1501
4cc2dcf9 1502 /* Do the actual hashing */
a0cfae59 1503
4cc2dcf9
EB
1504 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
1505 testmgr_poison(result, digestsize + TESTMGR_POISON_LEN);
da5ffe11 1506
5283a8ee
EB
1507 if (cfg->finalization_type == FINALIZATION_TYPE_DIGEST ||
1508 vec->digest_error) {
4cc2dcf9
EB
1509 /* Just using digest() */
1510 ahash_request_set_callback(req, req_flags, crypto_req_done,
1511 &wait);
1512 ahash_request_set_crypt(req, tsgl->sgl, result, vec->psize);
6570737c 1513 err = do_ahash_op(crypto_ahash_digest, req, &wait, cfg->nosimd);
4cc2dcf9 1514 if (err) {
5283a8ee
EB
1515 if (err == vec->digest_error)
1516 return 0;
d8ea98aa 1517 pr_err("alg: ahash: %s digest() failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
951d1332 1518 driver, vec_name, vec->digest_error, err,
5283a8ee 1519 cfg->name);
4cc2dcf9
EB
1520 return err;
1521 }
5283a8ee 1522 if (vec->digest_error) {
d8ea98aa 1523 pr_err("alg: ahash: %s digest() unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
951d1332 1524 driver, vec_name, vec->digest_error, cfg->name);
5283a8ee
EB
1525 return -EINVAL;
1526 }
4cc2dcf9
EB
1527 goto result_ready;
1528 }
da7f033d 1529
4cc2dcf9 1530 /* Using init(), zero or more update(), then final() or finup() */
da7f033d 1531
4cc2dcf9
EB
1532 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
1533 ahash_request_set_crypt(req, NULL, result, 0);
6570737c 1534 err = do_ahash_op(crypto_ahash_init, req, &wait, cfg->nosimd);
d8ea98aa
EB
1535 err = check_nonfinal_ahash_op("init", err, result, digestsize,
1536 driver, vec_name, cfg);
4cc2dcf9
EB
1537 if (err)
1538 return err;
da7f033d 1539
4cc2dcf9
EB
1540 pending_sgl = NULL;
1541 pending_len = 0;
1542 for (i = 0; i < tsgl->nents; i++) {
1543 if (divs[i]->flush_type != FLUSH_TYPE_NONE &&
1544 pending_sgl != NULL) {
1545 /* update() with the pending data */
1546 ahash_request_set_callback(req, req_flags,
1547 crypto_req_done, &wait);
1548 ahash_request_set_crypt(req, pending_sgl, result,
1549 pending_len);
6570737c
EB
1550 err = do_ahash_op(crypto_ahash_update, req, &wait,
1551 divs[i]->nosimd);
d8ea98aa
EB
1552 err = check_nonfinal_ahash_op("update", err,
1553 result, digestsize,
1554 driver, vec_name, cfg);
4cc2dcf9
EB
1555 if (err)
1556 return err;
1557 pending_sgl = NULL;
1558 pending_len = 0;
da7f033d 1559 }
4cc2dcf9
EB
1560 if (divs[i]->flush_type == FLUSH_TYPE_REIMPORT) {
1561 /* Test ->export() and ->import() */
1562 testmgr_poison(hashstate + statesize,
1563 TESTMGR_POISON_LEN);
1564 err = crypto_ahash_export(req, hashstate);
d8ea98aa
EB
1565 err = check_nonfinal_ahash_op("export", err,
1566 result, digestsize,
1567 driver, vec_name, cfg);
4cc2dcf9
EB
1568 if (err)
1569 return err;
1570 if (!testmgr_is_poison(hashstate + statesize,
1571 TESTMGR_POISON_LEN)) {
d8ea98aa 1572 pr_err("alg: ahash: %s export() overran state buffer on test vector %s, cfg=\"%s\"\n",
951d1332 1573 driver, vec_name, cfg->name);
4cc2dcf9 1574 return -EOVERFLOW;
da7f033d 1575 }
76715095 1576
4cc2dcf9
EB
1577 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
1578 err = crypto_ahash_import(req, hashstate);
d8ea98aa
EB
1579 err = check_nonfinal_ahash_op("import", err,
1580 result, digestsize,
1581 driver, vec_name, cfg);
4cc2dcf9
EB
1582 if (err)
1583 return err;
da7f033d 1584 }
4cc2dcf9
EB
1585 if (pending_sgl == NULL)
1586 pending_sgl = &tsgl->sgl[i];
1587 pending_len += tsgl->sgl[i].length;
1588 }
da7f033d 1589
4cc2dcf9
EB
1590 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
1591 ahash_request_set_crypt(req, pending_sgl, result, pending_len);
1592 if (cfg->finalization_type == FINALIZATION_TYPE_FINAL) {
1593 /* finish with update() and final() */
6570737c 1594 err = do_ahash_op(crypto_ahash_update, req, &wait, cfg->nosimd);
d8ea98aa
EB
1595 err = check_nonfinal_ahash_op("update", err, result, digestsize,
1596 driver, vec_name, cfg);
4cc2dcf9
EB
1597 if (err)
1598 return err;
6570737c 1599 err = do_ahash_op(crypto_ahash_final, req, &wait, cfg->nosimd);
4cc2dcf9 1600 if (err) {
d8ea98aa 1601 pr_err("alg: ahash: %s final() failed with err %d on test vector %s, cfg=\"%s\"\n",
951d1332 1602 driver, err, vec_name, cfg->name);
4cc2dcf9
EB
1603 return err;
1604 }
1605 } else {
1606 /* finish with finup() */
6570737c 1607 err = do_ahash_op(crypto_ahash_finup, req, &wait, cfg->nosimd);
4cc2dcf9 1608 if (err) {
d8ea98aa 1609 pr_err("alg: ahash: %s finup() failed with err %d on test vector %s, cfg=\"%s\"\n",
951d1332 1610 driver, err, vec_name, cfg->name);
4cc2dcf9 1611 return err;
da7f033d
HX
1612 }
1613 }
1614
4cc2dcf9 1615result_ready:
d8ea98aa
EB
1616 return check_hash_result("ahash", result, digestsize, vec, vec_name,
1617 driver, cfg);
1618}
1619
79cafe9a 1620static int test_hash_vec_cfg(const struct hash_testvec *vec,
d8ea98aa
EB
1621 const char *vec_name,
1622 const struct testvec_config *cfg,
1623 struct ahash_request *req,
1624 struct shash_desc *desc,
1625 struct test_sglist *tsgl,
1626 u8 *hashstate)
1627{
1628 int err;
1629
1630 /*
1631 * For algorithms implemented as "shash", most bugs will be detected by
1632 * both the shash and ahash tests. Test the shash API first so that the
1633 * failures involve less indirection, so are easier to debug.
1634 */
1635
1636 if (desc) {
79cafe9a 1637 err = test_shash_vec_cfg(vec, vec_name, cfg, desc, tsgl,
d8ea98aa
EB
1638 hashstate);
1639 if (err)
1640 return err;
4cc2dcf9 1641 }
da5ffe11 1642
79cafe9a 1643 return test_ahash_vec_cfg(vec, vec_name, cfg, req, tsgl, hashstate);
4cc2dcf9 1644}
da7f033d 1645
79cafe9a
EB
1646static int test_hash_vec(const struct hash_testvec *vec, unsigned int vec_num,
1647 struct ahash_request *req, struct shash_desc *desc,
1648 struct test_sglist *tsgl, u8 *hashstate)
4cc2dcf9 1649{
951d1332 1650 char vec_name[16];
4cc2dcf9
EB
1651 unsigned int i;
1652 int err;
da7f033d 1653
951d1332
EB
1654 sprintf(vec_name, "%u", vec_num);
1655
4cc2dcf9 1656 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++) {
79cafe9a 1657 err = test_hash_vec_cfg(vec, vec_name,
4cc2dcf9 1658 &default_hash_testvec_configs[i],
d8ea98aa 1659 req, desc, tsgl, hashstate);
4cc2dcf9
EB
1660 if (err)
1661 return err;
1662 }
5f2b424e 1663
4cc2dcf9
EB
1664#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1665 if (!noextratests) {
f900fde2 1666 struct rnd_state rng;
4cc2dcf9
EB
1667 struct testvec_config cfg;
1668 char cfgname[TESTVEC_CONFIG_NAMELEN];
5f2b424e 1669
f900fde2
EB
1670 init_rnd_state(&rng);
1671
4cc2dcf9 1672 for (i = 0; i < fuzz_iterations; i++) {
f900fde2 1673 generate_random_testvec_config(&rng, &cfg, cfgname,
4cc2dcf9 1674 sizeof(cfgname));
79cafe9a 1675 err = test_hash_vec_cfg(vec, vec_name, &cfg,
d8ea98aa 1676 req, desc, tsgl, hashstate);
4cc2dcf9
EB
1677 if (err)
1678 return err;
e63e1b0d 1679 cond_resched();
018ba95c
WR
1680 }
1681 }
4cc2dcf9
EB
1682#endif
1683 return 0;
1684}
018ba95c 1685
9a8a6b3f
EB
1686#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1687/*
1688 * Generate a hash test vector from the given implementation.
1689 * Assumes the buffers in 'vec' were already allocated.
1690 */
f900fde2
EB
1691static void generate_random_hash_testvec(struct rnd_state *rng,
1692 struct shash_desc *desc,
9a8a6b3f
EB
1693 struct hash_testvec *vec,
1694 unsigned int maxkeysize,
1695 unsigned int maxdatasize,
1696 char *name, size_t max_namelen)
1697{
9a8a6b3f 1698 /* Data */
f900fde2
EB
1699 vec->psize = generate_random_length(rng, maxdatasize);
1700 generate_random_bytes(rng, (u8 *)vec->plaintext, vec->psize);
9a8a6b3f
EB
1701
1702 /*
1703 * Key: length in range [1, maxkeysize], but usually choose maxkeysize.
1704 * If algorithm is unkeyed, then maxkeysize == 0 and set ksize = 0.
1705 */
1706 vec->setkey_error = 0;
1707 vec->ksize = 0;
1708 if (maxkeysize) {
1709 vec->ksize = maxkeysize;
f900fde2
EB
1710 if (prandom_u32_below(rng, 4) == 0)
1711 vec->ksize = prandom_u32_inclusive(rng, 1, maxkeysize);
1712 generate_random_bytes(rng, (u8 *)vec->key, vec->ksize);
9a8a6b3f 1713
149c4e6e 1714 vec->setkey_error = crypto_shash_setkey(desc->tfm, vec->key,
9a8a6b3f
EB
1715 vec->ksize);
1716 /* If the key couldn't be set, no need to continue to digest. */
1717 if (vec->setkey_error)
1718 goto done;
1719 }
1720
1721 /* Digest */
9a8a6b3f
EB
1722 vec->digest_error = crypto_shash_digest(desc, vec->plaintext,
1723 vec->psize, (u8 *)vec->digest);
1724done:
1725 snprintf(name, max_namelen, "\"random: psize=%u ksize=%u\"",
1726 vec->psize, vec->ksize);
1727}
1728
1729/*
1730 * Test the hash algorithm represented by @req against the corresponding generic
1731 * implementation, if one is available.
1732 */
79cafe9a 1733static int test_hash_vs_generic_impl(const char *generic_driver,
9a8a6b3f
EB
1734 unsigned int maxkeysize,
1735 struct ahash_request *req,
d8ea98aa 1736 struct shash_desc *desc,
9a8a6b3f
EB
1737 struct test_sglist *tsgl,
1738 u8 *hashstate)
1739{
1740 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1741 const unsigned int digestsize = crypto_ahash_digestsize(tfm);
1742 const unsigned int blocksize = crypto_ahash_blocksize(tfm);
1743 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
1744 const char *algname = crypto_hash_alg_common(tfm)->base.cra_name;
79cafe9a 1745 const char *driver = crypto_ahash_driver_name(tfm);
f900fde2 1746 struct rnd_state rng;
9a8a6b3f
EB
1747 char _generic_driver[CRYPTO_MAX_ALG_NAME];
1748 struct crypto_shash *generic_tfm = NULL;
149c4e6e 1749 struct shash_desc *generic_desc = NULL;
9a8a6b3f
EB
1750 unsigned int i;
1751 struct hash_testvec vec = { 0 };
1752 char vec_name[64];
6b5ca646 1753 struct testvec_config *cfg;
9a8a6b3f
EB
1754 char cfgname[TESTVEC_CONFIG_NAMELEN];
1755 int err;
1756
1757 if (noextratests)
1758 return 0;
1759
f900fde2
EB
1760 init_rnd_state(&rng);
1761
9a8a6b3f
EB
1762 if (!generic_driver) { /* Use default naming convention? */
1763 err = build_generic_driver_name(algname, _generic_driver);
1764 if (err)
1765 return err;
1766 generic_driver = _generic_driver;
1767 }
1768
1769 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
1770 return 0;
1771
1772 generic_tfm = crypto_alloc_shash(generic_driver, 0, 0);
1773 if (IS_ERR(generic_tfm)) {
1774 err = PTR_ERR(generic_tfm);
1775 if (err == -ENOENT) {
1776 pr_warn("alg: hash: skipping comparison tests for %s because %s is unavailable\n",
1777 driver, generic_driver);
1778 return 0;
1779 }
1780 pr_err("alg: hash: error allocating %s (generic impl of %s): %d\n",
1781 generic_driver, algname, err);
1782 return err;
1783 }
1784
6b5ca646
AB
1785 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1786 if (!cfg) {
1787 err = -ENOMEM;
1788 goto out;
1789 }
1790
149c4e6e
AB
1791 generic_desc = kzalloc(sizeof(*desc) +
1792 crypto_shash_descsize(generic_tfm), GFP_KERNEL);
1793 if (!generic_desc) {
1794 err = -ENOMEM;
1795 goto out;
1796 }
1797 generic_desc->tfm = generic_tfm;
1798
9a8a6b3f
EB
1799 /* Check the algorithm properties for consistency. */
1800
1801 if (digestsize != crypto_shash_digestsize(generic_tfm)) {
1802 pr_err("alg: hash: digestsize for %s (%u) doesn't match generic impl (%u)\n",
1803 driver, digestsize,
1804 crypto_shash_digestsize(generic_tfm));
1805 err = -EINVAL;
1806 goto out;
1807 }
1808
1809 if (blocksize != crypto_shash_blocksize(generic_tfm)) {
1810 pr_err("alg: hash: blocksize for %s (%u) doesn't match generic impl (%u)\n",
1811 driver, blocksize, crypto_shash_blocksize(generic_tfm));
1812 err = -EINVAL;
1813 goto out;
1814 }
1815
1816 /*
1817 * Now generate test vectors using the generic implementation, and test
1818 * the other implementation against them.
1819 */
1820
1821 vec.key = kmalloc(maxkeysize, GFP_KERNEL);
1822 vec.plaintext = kmalloc(maxdatasize, GFP_KERNEL);
1823 vec.digest = kmalloc(digestsize, GFP_KERNEL);
1824 if (!vec.key || !vec.plaintext || !vec.digest) {
1825 err = -ENOMEM;
1826 goto out;
1827 }
1828
1829 for (i = 0; i < fuzz_iterations * 8; i++) {
f900fde2 1830 generate_random_hash_testvec(&rng, generic_desc, &vec,
9a8a6b3f
EB
1831 maxkeysize, maxdatasize,
1832 vec_name, sizeof(vec_name));
f900fde2
EB
1833 generate_random_testvec_config(&rng, cfg, cfgname,
1834 sizeof(cfgname));
9a8a6b3f 1835
79cafe9a 1836 err = test_hash_vec_cfg(&vec, vec_name, cfg,
d8ea98aa 1837 req, desc, tsgl, hashstate);
9a8a6b3f
EB
1838 if (err)
1839 goto out;
1840 cond_resched();
1841 }
1842 err = 0;
1843out:
6b5ca646 1844 kfree(cfg);
9a8a6b3f
EB
1845 kfree(vec.key);
1846 kfree(vec.plaintext);
1847 kfree(vec.digest);
1848 crypto_free_shash(generic_tfm);
453431a5 1849 kfree_sensitive(generic_desc);
9a8a6b3f
EB
1850 return err;
1851}
1852#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
79cafe9a 1853static int test_hash_vs_generic_impl(const char *generic_driver,
9a8a6b3f
EB
1854 unsigned int maxkeysize,
1855 struct ahash_request *req,
d8ea98aa 1856 struct shash_desc *desc,
9a8a6b3f
EB
1857 struct test_sglist *tsgl,
1858 u8 *hashstate)
1859{
1860 return 0;
1861}
1862#endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1863
d8ea98aa
EB
1864static int alloc_shash(const char *driver, u32 type, u32 mask,
1865 struct crypto_shash **tfm_ret,
1866 struct shash_desc **desc_ret)
1867{
1868 struct crypto_shash *tfm;
1869 struct shash_desc *desc;
1870
1871 tfm = crypto_alloc_shash(driver, type, mask);
1872 if (IS_ERR(tfm)) {
1873 if (PTR_ERR(tfm) == -ENOENT) {
1874 /*
1875 * This algorithm is only available through the ahash
1876 * API, not the shash API, so skip the shash tests.
1877 */
1878 return 0;
1879 }
1880 pr_err("alg: hash: failed to allocate shash transform for %s: %ld\n",
1881 driver, PTR_ERR(tfm));
1882 return PTR_ERR(tfm);
1883 }
1884
1885 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL);
1886 if (!desc) {
1887 crypto_free_shash(tfm);
1888 return -ENOMEM;
1889 }
1890 desc->tfm = tfm;
1891
1892 *tfm_ret = tfm;
1893 *desc_ret = desc;
1894 return 0;
1895}
1896
4cc2dcf9
EB
1897static int __alg_test_hash(const struct hash_testvec *vecs,
1898 unsigned int num_vecs, const char *driver,
9a8a6b3f
EB
1899 u32 type, u32 mask,
1900 const char *generic_driver, unsigned int maxkeysize)
4cc2dcf9 1901{
d8ea98aa 1902 struct crypto_ahash *atfm = NULL;
4cc2dcf9 1903 struct ahash_request *req = NULL;
d8ea98aa
EB
1904 struct crypto_shash *stfm = NULL;
1905 struct shash_desc *desc = NULL;
4cc2dcf9
EB
1906 struct test_sglist *tsgl = NULL;
1907 u8 *hashstate = NULL;
d8ea98aa 1908 unsigned int statesize;
4cc2dcf9
EB
1909 unsigned int i;
1910 int err;
018ba95c 1911
d8ea98aa
EB
1912 /*
1913 * Always test the ahash API. This works regardless of whether the
1914 * algorithm is implemented as ahash or shash.
1915 */
1916
1917 atfm = crypto_alloc_ahash(driver, type, mask);
1918 if (IS_ERR(atfm)) {
4cc2dcf9 1919 pr_err("alg: hash: failed to allocate transform for %s: %ld\n",
d8ea98aa
EB
1920 driver, PTR_ERR(atfm));
1921 return PTR_ERR(atfm);
4cc2dcf9 1922 }
79cafe9a 1923 driver = crypto_ahash_driver_name(atfm);
018ba95c 1924
d8ea98aa 1925 req = ahash_request_alloc(atfm, GFP_KERNEL);
4cc2dcf9
EB
1926 if (!req) {
1927 pr_err("alg: hash: failed to allocate request for %s\n",
1928 driver);
1929 err = -ENOMEM;
1930 goto out;
1931 }
018ba95c 1932
d8ea98aa
EB
1933 /*
1934 * If available also test the shash API, to cover corner cases that may
1935 * be missed by testing the ahash API only.
1936 */
1937 err = alloc_shash(driver, type, mask, &stfm, &desc);
1938 if (err)
1939 goto out;
1940
4cc2dcf9
EB
1941 tsgl = kmalloc(sizeof(*tsgl), GFP_KERNEL);
1942 if (!tsgl || init_test_sglist(tsgl) != 0) {
1943 pr_err("alg: hash: failed to allocate test buffers for %s\n",
1944 driver);
1945 kfree(tsgl);
1946 tsgl = NULL;
1947 err = -ENOMEM;
1948 goto out;
1949 }
018ba95c 1950
d8ea98aa
EB
1951 statesize = crypto_ahash_statesize(atfm);
1952 if (stfm)
1953 statesize = max(statesize, crypto_shash_statesize(stfm));
1954 hashstate = kmalloc(statesize + TESTMGR_POISON_LEN, GFP_KERNEL);
4cc2dcf9
EB
1955 if (!hashstate) {
1956 pr_err("alg: hash: failed to allocate hash state buffer for %s\n",
1957 driver);
1958 err = -ENOMEM;
1959 goto out;
1960 }
018ba95c 1961
4cc2dcf9 1962 for (i = 0; i < num_vecs; i++) {
c9c28ed0
SM
1963 if (fips_enabled && vecs[i].fips_skip)
1964 continue;
1965
79cafe9a 1966 err = test_hash_vec(&vecs[i], i, req, desc, tsgl, hashstate);
4cc2dcf9 1967 if (err)
5f2b424e 1968 goto out;
e63e1b0d 1969 cond_resched();
da7f033d 1970 }
79cafe9a 1971 err = test_hash_vs_generic_impl(generic_driver, maxkeysize, req,
d8ea98aa 1972 desc, tsgl, hashstate);
da7f033d 1973out:
4cc2dcf9
EB
1974 kfree(hashstate);
1975 if (tsgl) {
1976 destroy_test_sglist(tsgl);
1977 kfree(tsgl);
1978 }
d8ea98aa
EB
1979 kfree(desc);
1980 crypto_free_shash(stfm);
da7f033d 1981 ahash_request_free(req);
d8ea98aa 1982 crypto_free_ahash(atfm);
4cc2dcf9 1983 return err;
da7f033d
HX
1984}
1985
4cc2dcf9
EB
1986static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1987 u32 type, u32 mask)
da5ffe11 1988{
4cc2dcf9
EB
1989 const struct hash_testvec *template = desc->suite.hash.vecs;
1990 unsigned int tcount = desc->suite.hash.count;
1991 unsigned int nr_unkeyed, nr_keyed;
9a8a6b3f 1992 unsigned int maxkeysize = 0;
4cc2dcf9 1993 int err;
da5ffe11 1994
4cc2dcf9
EB
1995 /*
1996 * For OPTIONAL_KEY algorithms, we have to do all the unkeyed tests
1997 * first, before setting a key on the tfm. To make this easier, we
1998 * require that the unkeyed test vectors (if any) are listed first.
1999 */
da5ffe11 2000
4cc2dcf9
EB
2001 for (nr_unkeyed = 0; nr_unkeyed < tcount; nr_unkeyed++) {
2002 if (template[nr_unkeyed].ksize)
2003 break;
2004 }
2005 for (nr_keyed = 0; nr_unkeyed + nr_keyed < tcount; nr_keyed++) {
2006 if (!template[nr_unkeyed + nr_keyed].ksize) {
2007 pr_err("alg: hash: test vectors for %s out of order, "
2008 "unkeyed ones must come first\n", desc->alg);
2009 return -EINVAL;
2010 }
9a8a6b3f
EB
2011 maxkeysize = max_t(unsigned int, maxkeysize,
2012 template[nr_unkeyed + nr_keyed].ksize);
4cc2dcf9 2013 }
da5ffe11 2014
4cc2dcf9
EB
2015 err = 0;
2016 if (nr_unkeyed) {
9a8a6b3f
EB
2017 err = __alg_test_hash(template, nr_unkeyed, driver, type, mask,
2018 desc->generic_driver, maxkeysize);
4cc2dcf9 2019 template += nr_unkeyed;
da5ffe11
JK
2020 }
2021
4cc2dcf9 2022 if (!err && nr_keyed)
9a8a6b3f
EB
2023 err = __alg_test_hash(template, nr_keyed, driver, type, mask,
2024 desc->generic_driver, maxkeysize);
4cc2dcf9
EB
2025
2026 return err;
da5ffe11
JK
2027}
2028
2257f471 2029static int test_aead_vec_cfg(int enc, const struct aead_testvec *vec,
951d1332 2030 const char *vec_name,
ed96804f
EB
2031 const struct testvec_config *cfg,
2032 struct aead_request *req,
2033 struct cipher_test_sglists *tsgls)
da7f033d 2034{
ed96804f
EB
2035 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2036 const unsigned int alignmask = crypto_aead_alignmask(tfm);
2037 const unsigned int ivsize = crypto_aead_ivsize(tfm);
2038 const unsigned int authsize = vec->clen - vec->plen;
2257f471 2039 const char *driver = crypto_aead_driver_name(tfm);
ed96804f
EB
2040 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
2041 const char *op = enc ? "encryption" : "decryption";
2042 DECLARE_CRYPTO_WAIT(wait);
2043 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
2044 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
2045 cfg->iv_offset +
2046 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
2047 struct kvec input[2];
2048 int err;
d8a32ac2 2049
ed96804f
EB
2050 /* Set the key */
2051 if (vec->wk)
2052 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
da7f033d 2053 else
ed96804f 2054 crypto_aead_clear_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
fd8c37c7
EB
2055
2056 err = do_setkey(crypto_aead_setkey, tfm, vec->key, vec->klen,
2057 cfg, alignmask);
5283a8ee 2058 if (err && err != vec->setkey_error) {
951d1332
EB
2059 pr_err("alg: aead: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
2060 driver, vec_name, vec->setkey_error, err,
5283a8ee 2061 crypto_aead_get_flags(tfm));
ed96804f 2062 return err;
da7f033d 2063 }
5283a8ee 2064 if (!err && vec->setkey_error) {
951d1332
EB
2065 pr_err("alg: aead: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
2066 driver, vec_name, vec->setkey_error);
ed96804f 2067 return -EINVAL;
da7f033d
HX
2068 }
2069
ed96804f
EB
2070 /* Set the authentication tag size */
2071 err = crypto_aead_setauthsize(tfm, authsize);
5283a8ee 2072 if (err && err != vec->setauthsize_error) {
951d1332
EB
2073 pr_err("alg: aead: %s setauthsize failed on test vector %s; expected_error=%d, actual_error=%d\n",
2074 driver, vec_name, vec->setauthsize_error, err);
ed96804f
EB
2075 return err;
2076 }
5283a8ee 2077 if (!err && vec->setauthsize_error) {
951d1332
EB
2078 pr_err("alg: aead: %s setauthsize unexpectedly succeeded on test vector %s; expected_error=%d\n",
2079 driver, vec_name, vec->setauthsize_error);
5283a8ee
EB
2080 return -EINVAL;
2081 }
2082
2083 if (vec->setkey_error || vec->setauthsize_error)
2084 return 0;
8ec25c51 2085
ed96804f
EB
2086 /* The IV must be copied to a buffer, as the algorithm may modify it */
2087 if (WARN_ON(ivsize > MAX_IVLEN))
2088 return -EINVAL;
2089 if (vec->iv)
2090 memcpy(iv, vec->iv, ivsize);
2091 else
2092 memset(iv, 0, ivsize);
da7f033d 2093
ed96804f
EB
2094 /* Build the src/dst scatterlists */
2095 input[0].iov_base = (void *)vec->assoc;
2096 input[0].iov_len = vec->alen;
2097 input[1].iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
2098 input[1].iov_len = enc ? vec->plen : vec->clen;
2099 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
2100 vec->alen + (enc ? vec->plen :
2101 vec->clen),
2102 vec->alen + (enc ? vec->clen :
2103 vec->plen),
2104 input, 2);
2105 if (err) {
951d1332
EB
2106 pr_err("alg: aead: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
2107 driver, op, vec_name, cfg->name);
ed96804f
EB
2108 return err;
2109 }
da7f033d 2110
ed96804f
EB
2111 /* Do the actual encryption or decryption */
2112 testmgr_poison(req->__ctx, crypto_aead_reqsize(tfm));
2113 aead_request_set_callback(req, req_flags, crypto_req_done, &wait);
2114 aead_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
2115 enc ? vec->plen : vec->clen, iv);
2116 aead_request_set_ad(req, vec->alen);
6570737c
EB
2117 if (cfg->nosimd)
2118 crypto_disable_simd_for_test();
2119 err = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
2120 if (cfg->nosimd)
2121 crypto_reenable_simd_for_test();
2122 err = crypto_wait_req(err, &wait);
a6e5ef9b
EB
2123
2124 /* Check that the algorithm didn't overwrite things it shouldn't have */
2125 if (req->cryptlen != (enc ? vec->plen : vec->clen) ||
2126 req->assoclen != vec->alen ||
2127 req->iv != iv ||
2128 req->src != tsgls->src.sgl_ptr ||
2129 req->dst != tsgls->dst.sgl_ptr ||
2130 crypto_aead_reqtfm(req) != tfm ||
2131 req->base.complete != crypto_req_done ||
2132 req->base.flags != req_flags ||
2133 req->base.data != &wait) {
951d1332
EB
2134 pr_err("alg: aead: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
2135 driver, op, vec_name, cfg->name);
a6e5ef9b
EB
2136 if (req->cryptlen != (enc ? vec->plen : vec->clen))
2137 pr_err("alg: aead: changed 'req->cryptlen'\n");
2138 if (req->assoclen != vec->alen)
2139 pr_err("alg: aead: changed 'req->assoclen'\n");
2140 if (req->iv != iv)
2141 pr_err("alg: aead: changed 'req->iv'\n");
2142 if (req->src != tsgls->src.sgl_ptr)
2143 pr_err("alg: aead: changed 'req->src'\n");
2144 if (req->dst != tsgls->dst.sgl_ptr)
2145 pr_err("alg: aead: changed 'req->dst'\n");
2146 if (crypto_aead_reqtfm(req) != tfm)
2147 pr_err("alg: aead: changed 'req->base.tfm'\n");
2148 if (req->base.complete != crypto_req_done)
2149 pr_err("alg: aead: changed 'req->base.complete'\n");
2150 if (req->base.flags != req_flags)
2151 pr_err("alg: aead: changed 'req->base.flags'\n");
2152 if (req->base.data != &wait)
2153 pr_err("alg: aead: changed 'req->base.data'\n");
2154 return -EINVAL;
2155 }
2156 if (is_test_sglist_corrupted(&tsgls->src)) {
951d1332
EB
2157 pr_err("alg: aead: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
2158 driver, op, vec_name, cfg->name);
a6e5ef9b
EB
2159 return -EINVAL;
2160 }
2161 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
2162 is_test_sglist_corrupted(&tsgls->dst)) {
951d1332
EB
2163 pr_err("alg: aead: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
2164 driver, op, vec_name, cfg->name);
a6e5ef9b 2165 return -EINVAL;
ed96804f 2166 }
da7f033d 2167
49763fc6
EB
2168 /* Check for unexpected success or failure, or wrong error code */
2169 if ((err == 0 && vec->novrfy) ||
2170 (err != vec->crypt_error && !(err == -EBADMSG && vec->novrfy))) {
2171 char expected_error[32];
2172
2173 if (vec->novrfy &&
2174 vec->crypt_error != 0 && vec->crypt_error != -EBADMSG)
2175 sprintf(expected_error, "-EBADMSG or %d",
2176 vec->crypt_error);
2177 else if (vec->novrfy)
2178 sprintf(expected_error, "-EBADMSG");
2179 else
2180 sprintf(expected_error, "%d", vec->crypt_error);
2181 if (err) {
2182 pr_err("alg: aead: %s %s failed on test vector %s; expected_error=%s, actual_error=%d, cfg=\"%s\"\n",
2183 driver, op, vec_name, expected_error, err,
2184 cfg->name);
2185 return err;
2186 }
2187 pr_err("alg: aead: %s %s unexpectedly succeeded on test vector %s; expected_error=%s, cfg=\"%s\"\n",
951d1332 2188 driver, op, vec_name, expected_error, cfg->name);
5283a8ee
EB
2189 return -EINVAL;
2190 }
49763fc6
EB
2191 if (err) /* Expectedly failed. */
2192 return 0;
5283a8ee 2193
ed96804f
EB
2194 /* Check for the correct output (ciphertext or plaintext) */
2195 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
2196 enc ? vec->clen : vec->plen,
f17f9e90
EB
2197 vec->alen,
2198 enc || cfg->inplace_mode == OUT_OF_PLACE);
ed96804f 2199 if (err == -EOVERFLOW) {
951d1332
EB
2200 pr_err("alg: aead: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
2201 driver, op, vec_name, cfg->name);
ed96804f
EB
2202 return err;
2203 }
2204 if (err) {
951d1332
EB
2205 pr_err("alg: aead: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
2206 driver, op, vec_name, cfg->name);
ed96804f
EB
2207 return err;
2208 }
da7f033d 2209
ed96804f
EB
2210 return 0;
2211}
da7f033d 2212
2257f471
EB
2213static int test_aead_vec(int enc, const struct aead_testvec *vec,
2214 unsigned int vec_num, struct aead_request *req,
ed96804f
EB
2215 struct cipher_test_sglists *tsgls)
2216{
951d1332 2217 char vec_name[16];
ed96804f
EB
2218 unsigned int i;
2219 int err;
da7f033d 2220
ed96804f
EB
2221 if (enc && vec->novrfy)
2222 return 0;
da7f033d 2223
951d1332
EB
2224 sprintf(vec_name, "%u", vec_num);
2225
ed96804f 2226 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
2257f471 2227 err = test_aead_vec_cfg(enc, vec, vec_name,
ed96804f
EB
2228 &default_cipher_testvec_configs[i],
2229 req, tsgls);
2230 if (err)
2231 return err;
2232 }
da7f033d 2233
ed96804f
EB
2234#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2235 if (!noextratests) {
f900fde2 2236 struct rnd_state rng;
ed96804f
EB
2237 struct testvec_config cfg;
2238 char cfgname[TESTVEC_CONFIG_NAMELEN];
05b1d338 2239
f900fde2
EB
2240 init_rnd_state(&rng);
2241
ed96804f 2242 for (i = 0; i < fuzz_iterations; i++) {
f900fde2 2243 generate_random_testvec_config(&rng, &cfg, cfgname,
ed96804f 2244 sizeof(cfgname));
2257f471 2245 err = test_aead_vec_cfg(enc, vec, vec_name,
ed96804f
EB
2246 &cfg, req, tsgls);
2247 if (err)
2248 return err;
e63e1b0d 2249 cond_resched();
da7f033d
HX
2250 }
2251 }
ed96804f
EB
2252#endif
2253 return 0;
2254}
da7f033d 2255
40153b10 2256#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2ea91505
EB
2257
2258struct aead_extra_tests_ctx {
f900fde2 2259 struct rnd_state rng;
2ea91505
EB
2260 struct aead_request *req;
2261 struct crypto_aead *tfm;
2ea91505
EB
2262 const struct alg_test_desc *test_desc;
2263 struct cipher_test_sglists *tsgls;
2264 unsigned int maxdatasize;
2265 unsigned int maxkeysize;
2266
2267 struct aead_testvec vec;
2268 char vec_name[64];
2269 char cfgname[TESTVEC_CONFIG_NAMELEN];
2270 struct testvec_config cfg;
2271};
2272
40153b10 2273/*
49763fc6
EB
2274 * Make at least one random change to a (ciphertext, AAD) pair. "Ciphertext"
2275 * here means the full ciphertext including the authentication tag. The
2276 * authentication tag (and hence also the ciphertext) is assumed to be nonempty.
2277 */
f900fde2
EB
2278static void mutate_aead_message(struct rnd_state *rng,
2279 struct aead_testvec *vec, bool aad_iv,
6f3a06d9 2280 unsigned int ivsize)
49763fc6 2281{
6f3a06d9 2282 const unsigned int aad_tail_size = aad_iv ? ivsize : 0;
49763fc6
EB
2283 const unsigned int authsize = vec->clen - vec->plen;
2284
f900fde2 2285 if (prandom_bool(rng) && vec->alen > aad_tail_size) {
49763fc6 2286 /* Mutate the AAD */
f900fde2
EB
2287 flip_random_bit(rng, (u8 *)vec->assoc,
2288 vec->alen - aad_tail_size);
2289 if (prandom_bool(rng))
49763fc6
EB
2290 return;
2291 }
f900fde2 2292 if (prandom_bool(rng)) {
49763fc6 2293 /* Mutate auth tag (assuming it's at the end of ciphertext) */
f900fde2 2294 flip_random_bit(rng, (u8 *)vec->ctext + vec->plen, authsize);
49763fc6
EB
2295 } else {
2296 /* Mutate any part of the ciphertext */
f900fde2 2297 flip_random_bit(rng, (u8 *)vec->ctext, vec->clen);
49763fc6
EB
2298 }
2299}
2300
2301/*
2302 * Minimum authentication tag size in bytes at which we assume that we can
2303 * reliably generate inauthentic messages, i.e. not generate an authentic
2304 * message by chance.
2305 */
2306#define MIN_COLLISION_FREE_AUTHSIZE 8
2307
f900fde2
EB
2308static void generate_aead_message(struct rnd_state *rng,
2309 struct aead_request *req,
49763fc6
EB
2310 const struct aead_test_suite *suite,
2311 struct aead_testvec *vec,
2312 bool prefer_inauthentic)
2313{
2314 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2315 const unsigned int ivsize = crypto_aead_ivsize(tfm);
2316 const unsigned int authsize = vec->clen - vec->plen;
2317 const bool inauthentic = (authsize >= MIN_COLLISION_FREE_AUTHSIZE) &&
f900fde2
EB
2318 (prefer_inauthentic ||
2319 prandom_u32_below(rng, 4) == 0);
49763fc6
EB
2320
2321 /* Generate the AAD. */
f900fde2 2322 generate_random_bytes(rng, (u8 *)vec->assoc, vec->alen);
6f3a06d9
EB
2323 if (suite->aad_iv && vec->alen >= ivsize)
2324 /* Avoid implementation-defined behavior. */
2325 memcpy((u8 *)vec->assoc + vec->alen - ivsize, vec->iv, ivsize);
49763fc6 2326
f900fde2 2327 if (inauthentic && prandom_bool(rng)) {
49763fc6 2328 /* Generate a random ciphertext. */
f900fde2 2329 generate_random_bytes(rng, (u8 *)vec->ctext, vec->clen);
49763fc6
EB
2330 } else {
2331 int i = 0;
2332 struct scatterlist src[2], dst;
2333 u8 iv[MAX_IVLEN];
2334 DECLARE_CRYPTO_WAIT(wait);
2335
2336 /* Generate a random plaintext and encrypt it. */
2337 sg_init_table(src, 2);
2338 if (vec->alen)
2339 sg_set_buf(&src[i++], vec->assoc, vec->alen);
2340 if (vec->plen) {
f900fde2 2341 generate_random_bytes(rng, (u8 *)vec->ptext, vec->plen);
49763fc6
EB
2342 sg_set_buf(&src[i++], vec->ptext, vec->plen);
2343 }
2344 sg_init_one(&dst, vec->ctext, vec->alen + vec->clen);
2345 memcpy(iv, vec->iv, ivsize);
2346 aead_request_set_callback(req, 0, crypto_req_done, &wait);
2347 aead_request_set_crypt(req, src, &dst, vec->plen, iv);
2348 aead_request_set_ad(req, vec->alen);
2349 vec->crypt_error = crypto_wait_req(crypto_aead_encrypt(req),
2350 &wait);
2351 /* If encryption failed, we're done. */
2352 if (vec->crypt_error != 0)
2353 return;
2354 memmove((u8 *)vec->ctext, vec->ctext + vec->alen, vec->clen);
2355 if (!inauthentic)
2356 return;
2357 /*
2358 * Mutate the authentic (ciphertext, AAD) pair to get an
2359 * inauthentic one.
2360 */
f900fde2 2361 mutate_aead_message(rng, vec, suite->aad_iv, ivsize);
49763fc6
EB
2362 }
2363 vec->novrfy = 1;
2364 if (suite->einval_allowed)
2365 vec->crypt_error = -EINVAL;
2366}
2367
2368/*
2369 * Generate an AEAD test vector 'vec' using the implementation specified by
2370 * 'req'. The buffers in 'vec' must already be allocated.
2371 *
2372 * If 'prefer_inauthentic' is true, then this function will generate inauthentic
2373 * test vectors (i.e. vectors with 'vec->novrfy=1') more often.
40153b10 2374 */
f900fde2
EB
2375static void generate_random_aead_testvec(struct rnd_state *rng,
2376 struct aead_request *req,
40153b10 2377 struct aead_testvec *vec,
49763fc6 2378 const struct aead_test_suite *suite,
40153b10
EB
2379 unsigned int maxkeysize,
2380 unsigned int maxdatasize,
49763fc6
EB
2381 char *name, size_t max_namelen,
2382 bool prefer_inauthentic)
40153b10
EB
2383{
2384 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2385 const unsigned int ivsize = crypto_aead_ivsize(tfm);
2ea91505 2386 const unsigned int maxauthsize = crypto_aead_maxauthsize(tfm);
40153b10
EB
2387 unsigned int authsize;
2388 unsigned int total_len;
40153b10
EB
2389
2390 /* Key: length in [0, maxkeysize], but usually choose maxkeysize */
2391 vec->klen = maxkeysize;
f900fde2
EB
2392 if (prandom_u32_below(rng, 4) == 0)
2393 vec->klen = prandom_u32_below(rng, maxkeysize + 1);
2394 generate_random_bytes(rng, (u8 *)vec->key, vec->klen);
40153b10
EB
2395 vec->setkey_error = crypto_aead_setkey(tfm, vec->key, vec->klen);
2396
2397 /* IV */
f900fde2 2398 generate_random_bytes(rng, (u8 *)vec->iv, ivsize);
40153b10
EB
2399
2400 /* Tag length: in [0, maxauthsize], but usually choose maxauthsize */
2401 authsize = maxauthsize;
f900fde2
EB
2402 if (prandom_u32_below(rng, 4) == 0)
2403 authsize = prandom_u32_below(rng, maxauthsize + 1);
49763fc6
EB
2404 if (prefer_inauthentic && authsize < MIN_COLLISION_FREE_AUTHSIZE)
2405 authsize = MIN_COLLISION_FREE_AUTHSIZE;
40153b10
EB
2406 if (WARN_ON(authsize > maxdatasize))
2407 authsize = maxdatasize;
2408 maxdatasize -= authsize;
2409 vec->setauthsize_error = crypto_aead_setauthsize(tfm, authsize);
2410
49763fc6 2411 /* AAD, plaintext, and ciphertext lengths */
f900fde2
EB
2412 total_len = generate_random_length(rng, maxdatasize);
2413 if (prandom_u32_below(rng, 4) == 0)
40153b10
EB
2414 vec->alen = 0;
2415 else
f900fde2 2416 vec->alen = generate_random_length(rng, total_len);
40153b10 2417 vec->plen = total_len - vec->alen;
40153b10
EB
2418 vec->clen = vec->plen + authsize;
2419
2420 /*
49763fc6
EB
2421 * Generate the AAD, plaintext, and ciphertext. Not applicable if the
2422 * key or the authentication tag size couldn't be set.
40153b10 2423 */
49763fc6 2424 vec->novrfy = 0;
eb455dbd 2425 vec->crypt_error = 0;
49763fc6 2426 if (vec->setkey_error == 0 && vec->setauthsize_error == 0)
f900fde2 2427 generate_aead_message(rng, req, suite, vec, prefer_inauthentic);
40153b10 2428 snprintf(name, max_namelen,
49763fc6
EB
2429 "\"random: alen=%u plen=%u authsize=%u klen=%u novrfy=%d\"",
2430 vec->alen, vec->plen, authsize, vec->klen, vec->novrfy);
2431}
2432
2433static void try_to_generate_inauthentic_testvec(
2434 struct aead_extra_tests_ctx *ctx)
2435{
2436 int i;
2437
2438 for (i = 0; i < 10; i++) {
f900fde2 2439 generate_random_aead_testvec(&ctx->rng, ctx->req, &ctx->vec,
49763fc6
EB
2440 &ctx->test_desc->suite.aead,
2441 ctx->maxkeysize, ctx->maxdatasize,
2442 ctx->vec_name,
2443 sizeof(ctx->vec_name), true);
2444 if (ctx->vec.novrfy)
2445 return;
2446 }
2447}
2448
2449/*
2450 * Generate inauthentic test vectors (i.e. ciphertext, AAD pairs that aren't the
2451 * result of an encryption with the key) and verify that decryption fails.
2452 */
2453static int test_aead_inauthentic_inputs(struct aead_extra_tests_ctx *ctx)
2454{
2455 unsigned int i;
2456 int err;
2457
2458 for (i = 0; i < fuzz_iterations * 8; i++) {
2459 /*
2460 * Since this part of the tests isn't comparing the
2461 * implementation to another, there's no point in testing any
2462 * test vectors other than inauthentic ones (vec.novrfy=1) here.
2463 *
2464 * If we're having trouble generating such a test vector, e.g.
2465 * if the algorithm keeps rejecting the generated keys, don't
2466 * retry forever; just continue on.
2467 */
2468 try_to_generate_inauthentic_testvec(ctx);
2469 if (ctx->vec.novrfy) {
f900fde2
EB
2470 generate_random_testvec_config(&ctx->rng, &ctx->cfg,
2471 ctx->cfgname,
49763fc6 2472 sizeof(ctx->cfgname));
2257f471 2473 err = test_aead_vec_cfg(DECRYPT, &ctx->vec,
49763fc6
EB
2474 ctx->vec_name, &ctx->cfg,
2475 ctx->req, ctx->tsgls);
2476 if (err)
2477 return err;
2478 }
2479 cond_resched();
2480 }
2481 return 0;
40153b10
EB
2482}
2483
2484/*
2ea91505
EB
2485 * Test the AEAD algorithm against the corresponding generic implementation, if
2486 * one is available.
40153b10 2487 */
2ea91505 2488static int test_aead_vs_generic_impl(struct aead_extra_tests_ctx *ctx)
40153b10 2489{
2ea91505 2490 struct crypto_aead *tfm = ctx->tfm;
40153b10 2491 const char *algname = crypto_aead_alg(tfm)->base.cra_name;
2257f471 2492 const char *driver = crypto_aead_driver_name(tfm);
2ea91505 2493 const char *generic_driver = ctx->test_desc->generic_driver;
40153b10
EB
2494 char _generic_driver[CRYPTO_MAX_ALG_NAME];
2495 struct crypto_aead *generic_tfm = NULL;
2496 struct aead_request *generic_req = NULL;
40153b10 2497 unsigned int i;
40153b10
EB
2498 int err;
2499
40153b10
EB
2500 if (!generic_driver) { /* Use default naming convention? */
2501 err = build_generic_driver_name(algname, _generic_driver);
2502 if (err)
2503 return err;
2504 generic_driver = _generic_driver;
2505 }
2506
2507 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
2508 return 0;
2509
2510 generic_tfm = crypto_alloc_aead(generic_driver, 0, 0);
2511 if (IS_ERR(generic_tfm)) {
2512 err = PTR_ERR(generic_tfm);
2513 if (err == -ENOENT) {
2514 pr_warn("alg: aead: skipping comparison tests for %s because %s is unavailable\n",
2515 driver, generic_driver);
2516 return 0;
2517 }
2518 pr_err("alg: aead: error allocating %s (generic impl of %s): %d\n",
2519 generic_driver, algname, err);
2520 return err;
2521 }
2522
2523 generic_req = aead_request_alloc(generic_tfm, GFP_KERNEL);
2524 if (!generic_req) {
2525 err = -ENOMEM;
2526 goto out;
2527 }
2528
2529 /* Check the algorithm properties for consistency. */
2530
2ea91505
EB
2531 if (crypto_aead_maxauthsize(tfm) !=
2532 crypto_aead_maxauthsize(generic_tfm)) {
40153b10 2533 pr_err("alg: aead: maxauthsize for %s (%u) doesn't match generic impl (%u)\n",
2ea91505
EB
2534 driver, crypto_aead_maxauthsize(tfm),
2535 crypto_aead_maxauthsize(generic_tfm));
40153b10
EB
2536 err = -EINVAL;
2537 goto out;
2538 }
2539
2ea91505 2540 if (crypto_aead_ivsize(tfm) != crypto_aead_ivsize(generic_tfm)) {
40153b10 2541 pr_err("alg: aead: ivsize for %s (%u) doesn't match generic impl (%u)\n",
2ea91505
EB
2542 driver, crypto_aead_ivsize(tfm),
2543 crypto_aead_ivsize(generic_tfm));
40153b10
EB
2544 err = -EINVAL;
2545 goto out;
2546 }
2547
2ea91505 2548 if (crypto_aead_blocksize(tfm) != crypto_aead_blocksize(generic_tfm)) {
40153b10 2549 pr_err("alg: aead: blocksize for %s (%u) doesn't match generic impl (%u)\n",
2ea91505
EB
2550 driver, crypto_aead_blocksize(tfm),
2551 crypto_aead_blocksize(generic_tfm));
40153b10
EB
2552 err = -EINVAL;
2553 goto out;
2554 }
2555
2556 /*
2557 * Now generate test vectors using the generic implementation, and test
2558 * the other implementation against them.
2559 */
40153b10 2560 for (i = 0; i < fuzz_iterations * 8; i++) {
f900fde2 2561 generate_random_aead_testvec(&ctx->rng, generic_req, &ctx->vec,
49763fc6 2562 &ctx->test_desc->suite.aead,
2ea91505
EB
2563 ctx->maxkeysize, ctx->maxdatasize,
2564 ctx->vec_name,
49763fc6 2565 sizeof(ctx->vec_name), false);
f900fde2
EB
2566 generate_random_testvec_config(&ctx->rng, &ctx->cfg,
2567 ctx->cfgname,
2ea91505 2568 sizeof(ctx->cfgname));
49763fc6 2569 if (!ctx->vec.novrfy) {
2257f471 2570 err = test_aead_vec_cfg(ENCRYPT, &ctx->vec,
49763fc6
EB
2571 ctx->vec_name, &ctx->cfg,
2572 ctx->req, ctx->tsgls);
2573 if (err)
2574 goto out;
2575 }
2576 if (ctx->vec.crypt_error == 0 || ctx->vec.novrfy) {
2257f471 2577 err = test_aead_vec_cfg(DECRYPT, &ctx->vec,
2ea91505
EB
2578 ctx->vec_name, &ctx->cfg,
2579 ctx->req, ctx->tsgls);
eb455dbd
EB
2580 if (err)
2581 goto out;
2582 }
40153b10
EB
2583 cond_resched();
2584 }
2585 err = 0;
2586out:
40153b10
EB
2587 crypto_free_aead(generic_tfm);
2588 aead_request_free(generic_req);
2589 return err;
2590}
2ea91505 2591
2257f471 2592static int test_aead_extra(const struct alg_test_desc *test_desc,
2ea91505
EB
2593 struct aead_request *req,
2594 struct cipher_test_sglists *tsgls)
2595{
2596 struct aead_extra_tests_ctx *ctx;
2597 unsigned int i;
2598 int err;
2599
2600 if (noextratests)
2601 return 0;
2602
2603 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2604 if (!ctx)
2605 return -ENOMEM;
f900fde2 2606 init_rnd_state(&ctx->rng);
2ea91505
EB
2607 ctx->req = req;
2608 ctx->tfm = crypto_aead_reqtfm(req);
2ea91505
EB
2609 ctx->test_desc = test_desc;
2610 ctx->tsgls = tsgls;
2611 ctx->maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
2612 ctx->maxkeysize = 0;
2613 for (i = 0; i < test_desc->suite.aead.count; i++)
2614 ctx->maxkeysize = max_t(unsigned int, ctx->maxkeysize,
2615 test_desc->suite.aead.vecs[i].klen);
2616
2617 ctx->vec.key = kmalloc(ctx->maxkeysize, GFP_KERNEL);
2618 ctx->vec.iv = kmalloc(crypto_aead_ivsize(ctx->tfm), GFP_KERNEL);
2619 ctx->vec.assoc = kmalloc(ctx->maxdatasize, GFP_KERNEL);
2620 ctx->vec.ptext = kmalloc(ctx->maxdatasize, GFP_KERNEL);
2621 ctx->vec.ctext = kmalloc(ctx->maxdatasize, GFP_KERNEL);
2622 if (!ctx->vec.key || !ctx->vec.iv || !ctx->vec.assoc ||
2623 !ctx->vec.ptext || !ctx->vec.ctext) {
2624 err = -ENOMEM;
2625 goto out;
2626 }
2627
8ff357a9 2628 err = test_aead_vs_generic_impl(ctx);
49763fc6
EB
2629 if (err)
2630 goto out;
2631
8ff357a9 2632 err = test_aead_inauthentic_inputs(ctx);
2ea91505
EB
2633out:
2634 kfree(ctx->vec.key);
2635 kfree(ctx->vec.iv);
2636 kfree(ctx->vec.assoc);
2637 kfree(ctx->vec.ptext);
2638 kfree(ctx->vec.ctext);
2639 kfree(ctx);
2640 return err;
2641}
40153b10 2642#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
2257f471 2643static int test_aead_extra(const struct alg_test_desc *test_desc,
2ea91505
EB
2644 struct aead_request *req,
2645 struct cipher_test_sglists *tsgls)
40153b10
EB
2646{
2647 return 0;
2648}
2649#endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
2650
2257f471 2651static int test_aead(int enc, const struct aead_test_suite *suite,
ed96804f
EB
2652 struct aead_request *req,
2653 struct cipher_test_sglists *tsgls)
2654{
2655 unsigned int i;
2656 int err;
da7f033d 2657
ed96804f 2658 for (i = 0; i < suite->count; i++) {
2257f471 2659 err = test_aead_vec(enc, &suite->vecs[i], i, req, tsgls);
ed96804f
EB
2660 if (err)
2661 return err;
e63e1b0d 2662 cond_resched();
ed96804f
EB
2663 }
2664 return 0;
da7f033d
HX
2665}
2666
ed96804f
EB
2667static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
2668 u32 type, u32 mask)
d8a32ac2 2669{
ed96804f
EB
2670 const struct aead_test_suite *suite = &desc->suite.aead;
2671 struct crypto_aead *tfm;
2672 struct aead_request *req = NULL;
2673 struct cipher_test_sglists *tsgls = NULL;
2674 int err;
d8a32ac2 2675
ed96804f
EB
2676 if (suite->count <= 0) {
2677 pr_err("alg: aead: empty test suite for %s\n", driver);
2678 return -EINVAL;
2679 }
d8a32ac2 2680
ed96804f
EB
2681 tfm = crypto_alloc_aead(driver, type, mask);
2682 if (IS_ERR(tfm)) {
2683 pr_err("alg: aead: failed to allocate transform for %s: %ld\n",
2684 driver, PTR_ERR(tfm));
2685 return PTR_ERR(tfm);
2686 }
2257f471 2687 driver = crypto_aead_driver_name(tfm);
58dcf548 2688
ed96804f
EB
2689 req = aead_request_alloc(tfm, GFP_KERNEL);
2690 if (!req) {
2691 pr_err("alg: aead: failed to allocate request for %s\n",
2692 driver);
2693 err = -ENOMEM;
2694 goto out;
2695 }
58dcf548 2696
ed96804f
EB
2697 tsgls = alloc_cipher_test_sglists();
2698 if (!tsgls) {
2699 pr_err("alg: aead: failed to allocate test buffers for %s\n",
2700 driver);
2701 err = -ENOMEM;
2702 goto out;
58dcf548
JK
2703 }
2704
2257f471 2705 err = test_aead(ENCRYPT, suite, req, tsgls);
ed96804f
EB
2706 if (err)
2707 goto out;
2708
2257f471 2709 err = test_aead(DECRYPT, suite, req, tsgls);
40153b10
EB
2710 if (err)
2711 goto out;
2712
2257f471 2713 err = test_aead_extra(desc, req, tsgls);
ed96804f
EB
2714out:
2715 free_cipher_test_sglists(tsgls);
2716 aead_request_free(req);
2717 crypto_free_aead(tfm);
2718 return err;
d8a32ac2
JK
2719}
2720
1aa4ecd9 2721static int test_cipher(struct crypto_cipher *tfm, int enc,
b13b1e0c
EB
2722 const struct cipher_testvec *template,
2723 unsigned int tcount)
1aa4ecd9
HX
2724{
2725 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
2726 unsigned int i, j, k;
1aa4ecd9
HX
2727 char *q;
2728 const char *e;
92a4c9fe 2729 const char *input, *result;
1aa4ecd9 2730 void *data;
f8b0d4d0
HX
2731 char *xbuf[XBUFSIZE];
2732 int ret = -ENOMEM;
2733
2734 if (testmgr_alloc_buf(xbuf))
2735 goto out_nobuf;
1aa4ecd9
HX
2736
2737 if (enc == ENCRYPT)
2738 e = "encryption";
2739 else
2740 e = "decryption";
2741
2742 j = 0;
2743 for (i = 0; i < tcount; i++) {
1aa4ecd9 2744
10faa8c0
SM
2745 if (fips_enabled && template[i].fips_skip)
2746 continue;
2747
92a4c9fe
EB
2748 input = enc ? template[i].ptext : template[i].ctext;
2749 result = enc ? template[i].ctext : template[i].ptext;
1aa4ecd9
HX
2750 j++;
2751
fd57f22a 2752 ret = -EINVAL;
92a4c9fe 2753 if (WARN_ON(template[i].len > PAGE_SIZE))
fd57f22a
HX
2754 goto out;
2755
1aa4ecd9 2756 data = xbuf[0];
92a4c9fe 2757 memcpy(data, input, template[i].len);
1aa4ecd9
HX
2758
2759 crypto_cipher_clear_flags(tfm, ~0);
2760 if (template[i].wk)
231baecd 2761 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1aa4ecd9
HX
2762
2763 ret = crypto_cipher_setkey(tfm, template[i].key,
2764 template[i].klen);
5283a8ee
EB
2765 if (ret) {
2766 if (ret == template[i].setkey_error)
2767 continue;
2768 pr_err("alg: cipher: %s setkey failed on test vector %u; expected_error=%d, actual_error=%d, flags=%#x\n",
2769 algo, j, template[i].setkey_error, ret,
2770 crypto_cipher_get_flags(tfm));
1aa4ecd9 2771 goto out;
5283a8ee
EB
2772 }
2773 if (template[i].setkey_error) {
2774 pr_err("alg: cipher: %s setkey unexpectedly succeeded on test vector %u; expected_error=%d\n",
2775 algo, j, template[i].setkey_error);
2776 ret = -EINVAL;
2777 goto out;
2778 }
1aa4ecd9 2779
92a4c9fe 2780 for (k = 0; k < template[i].len;
1aa4ecd9
HX
2781 k += crypto_cipher_blocksize(tfm)) {
2782 if (enc)
2783 crypto_cipher_encrypt_one(tfm, data + k,
2784 data + k);
2785 else
2786 crypto_cipher_decrypt_one(tfm, data + k,
2787 data + k);
2788 }
2789
2790 q = data;
92a4c9fe 2791 if (memcmp(q, result, template[i].len)) {
1aa4ecd9
HX
2792 printk(KERN_ERR "alg: cipher: Test %d failed "
2793 "on %s for %s\n", j, e, algo);
92a4c9fe 2794 hexdump(q, template[i].len);
1aa4ecd9
HX
2795 ret = -EINVAL;
2796 goto out;
2797 }
2798 }
2799
2800 ret = 0;
2801
2802out:
f8b0d4d0
HX
2803 testmgr_free_buf(xbuf);
2804out_nobuf:
1aa4ecd9
HX
2805 return ret;
2806}
2807
6e5972fa 2808static int test_skcipher_vec_cfg(int enc, const struct cipher_testvec *vec,
951d1332 2809 const char *vec_name,
4e7babba
EB
2810 const struct testvec_config *cfg,
2811 struct skcipher_request *req,
2812 struct cipher_test_sglists *tsgls)
da7f033d 2813{
4e7babba
EB
2814 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2815 const unsigned int alignmask = crypto_skcipher_alignmask(tfm);
2816 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
6e5972fa 2817 const char *driver = crypto_skcipher_driver_name(tfm);
4e7babba
EB
2818 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
2819 const char *op = enc ? "encryption" : "decryption";
2820 DECLARE_CRYPTO_WAIT(wait);
2821 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
2822 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
2823 cfg->iv_offset +
2824 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
2825 struct kvec input;
2826 int err;
08d6af8c 2827
4e7babba
EB
2828 /* Set the key */
2829 if (vec->wk)
2830 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
da7f033d 2831 else
4e7babba
EB
2832 crypto_skcipher_clear_flags(tfm,
2833 CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
fd8c37c7
EB
2834 err = do_setkey(crypto_skcipher_setkey, tfm, vec->key, vec->klen,
2835 cfg, alignmask);
4e7babba 2836 if (err) {
5283a8ee 2837 if (err == vec->setkey_error)
4e7babba 2838 return 0;
951d1332
EB
2839 pr_err("alg: skcipher: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
2840 driver, vec_name, vec->setkey_error, err,
5283a8ee 2841 crypto_skcipher_get_flags(tfm));
4e7babba
EB
2842 return err;
2843 }
5283a8ee 2844 if (vec->setkey_error) {
951d1332
EB
2845 pr_err("alg: skcipher: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
2846 driver, vec_name, vec->setkey_error);
4e7babba 2847 return -EINVAL;
da7f033d
HX
2848 }
2849
4e7babba
EB
2850 /* The IV must be copied to a buffer, as the algorithm may modify it */
2851 if (ivsize) {
2852 if (WARN_ON(ivsize > MAX_IVLEN))
2853 return -EINVAL;
8efd972e
EB
2854 if (vec->generates_iv && !enc)
2855 memcpy(iv, vec->iv_out, ivsize);
2856 else if (vec->iv)
4e7babba 2857 memcpy(iv, vec->iv, ivsize);
da7f033d 2858 else
4e7babba
EB
2859 memset(iv, 0, ivsize);
2860 } else {
2861 if (vec->generates_iv) {
951d1332
EB
2862 pr_err("alg: skcipher: %s has ivsize=0 but test vector %s generates IV!\n",
2863 driver, vec_name);
4e7babba 2864 return -EINVAL;
8a826a34 2865 }
4e7babba 2866 iv = NULL;
da7f033d
HX
2867 }
2868
4e7babba
EB
2869 /* Build the src/dst scatterlists */
2870 input.iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
2871 input.iov_len = vec->len;
2872 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
2873 vec->len, vec->len, &input, 1);
2874 if (err) {
951d1332
EB
2875 pr_err("alg: skcipher: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
2876 driver, op, vec_name, cfg->name);
4e7babba
EB
2877 return err;
2878 }
da7f033d 2879
4e7babba
EB
2880 /* Do the actual encryption or decryption */
2881 testmgr_poison(req->__ctx, crypto_skcipher_reqsize(tfm));
2882 skcipher_request_set_callback(req, req_flags, crypto_req_done, &wait);
2883 skcipher_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
2884 vec->len, iv);
6570737c
EB
2885 if (cfg->nosimd)
2886 crypto_disable_simd_for_test();
2887 err = enc ? crypto_skcipher_encrypt(req) : crypto_skcipher_decrypt(req);
2888 if (cfg->nosimd)
2889 crypto_reenable_simd_for_test();
2890 err = crypto_wait_req(err, &wait);
da7f033d 2891
fa353c99
EB
2892 /* Check that the algorithm didn't overwrite things it shouldn't have */
2893 if (req->cryptlen != vec->len ||
2894 req->iv != iv ||
2895 req->src != tsgls->src.sgl_ptr ||
2896 req->dst != tsgls->dst.sgl_ptr ||
2897 crypto_skcipher_reqtfm(req) != tfm ||
2898 req->base.complete != crypto_req_done ||
2899 req->base.flags != req_flags ||
2900 req->base.data != &wait) {
951d1332
EB
2901 pr_err("alg: skcipher: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
2902 driver, op, vec_name, cfg->name);
fa353c99
EB
2903 if (req->cryptlen != vec->len)
2904 pr_err("alg: skcipher: changed 'req->cryptlen'\n");
2905 if (req->iv != iv)
2906 pr_err("alg: skcipher: changed 'req->iv'\n");
2907 if (req->src != tsgls->src.sgl_ptr)
2908 pr_err("alg: skcipher: changed 'req->src'\n");
2909 if (req->dst != tsgls->dst.sgl_ptr)
2910 pr_err("alg: skcipher: changed 'req->dst'\n");
2911 if (crypto_skcipher_reqtfm(req) != tfm)
2912 pr_err("alg: skcipher: changed 'req->base.tfm'\n");
2913 if (req->base.complete != crypto_req_done)
2914 pr_err("alg: skcipher: changed 'req->base.complete'\n");
2915 if (req->base.flags != req_flags)
2916 pr_err("alg: skcipher: changed 'req->base.flags'\n");
2917 if (req->base.data != &wait)
2918 pr_err("alg: skcipher: changed 'req->base.data'\n");
2919 return -EINVAL;
2920 }
2921 if (is_test_sglist_corrupted(&tsgls->src)) {
951d1332
EB
2922 pr_err("alg: skcipher: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
2923 driver, op, vec_name, cfg->name);
fa353c99
EB
2924 return -EINVAL;
2925 }
2926 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
2927 is_test_sglist_corrupted(&tsgls->dst)) {
951d1332
EB
2928 pr_err("alg: skcipher: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
2929 driver, op, vec_name, cfg->name);
fa353c99
EB
2930 return -EINVAL;
2931 }
2932
5283a8ee
EB
2933 /* Check for success or failure */
2934 if (err) {
2935 if (err == vec->crypt_error)
2936 return 0;
951d1332
EB
2937 pr_err("alg: skcipher: %s %s failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
2938 driver, op, vec_name, vec->crypt_error, err, cfg->name);
5283a8ee
EB
2939 return err;
2940 }
2941 if (vec->crypt_error) {
951d1332
EB
2942 pr_err("alg: skcipher: %s %s unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
2943 driver, op, vec_name, vec->crypt_error, cfg->name);
5283a8ee
EB
2944 return -EINVAL;
2945 }
2946
4e7babba
EB
2947 /* Check for the correct output (ciphertext or plaintext) */
2948 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
2949 vec->len, 0, true);
2950 if (err == -EOVERFLOW) {
951d1332
EB
2951 pr_err("alg: skcipher: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
2952 driver, op, vec_name, cfg->name);
4e7babba
EB
2953 return err;
2954 }
2955 if (err) {
951d1332
EB
2956 pr_err("alg: skcipher: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
2957 driver, op, vec_name, cfg->name);
4e7babba
EB
2958 return err;
2959 }
08d6af8c 2960
4e7babba 2961 /* If applicable, check that the algorithm generated the correct IV */
8efd972e 2962 if (vec->iv_out && memcmp(iv, vec->iv_out, ivsize) != 0) {
951d1332
EB
2963 pr_err("alg: skcipher: %s %s test failed (wrong output IV) on test vector %s, cfg=\"%s\"\n",
2964 driver, op, vec_name, cfg->name);
4e7babba
EB
2965 hexdump(iv, ivsize);
2966 return -EINVAL;
2967 }
08d6af8c 2968
4e7babba
EB
2969 return 0;
2970}
da7f033d 2971
6e5972fa 2972static int test_skcipher_vec(int enc, const struct cipher_testvec *vec,
4e7babba
EB
2973 unsigned int vec_num,
2974 struct skcipher_request *req,
2975 struct cipher_test_sglists *tsgls)
2976{
951d1332 2977 char vec_name[16];
4e7babba
EB
2978 unsigned int i;
2979 int err;
da7f033d 2980
4e7babba
EB
2981 if (fips_enabled && vec->fips_skip)
2982 return 0;
da7f033d 2983
951d1332
EB
2984 sprintf(vec_name, "%u", vec_num);
2985
4e7babba 2986 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
6e5972fa 2987 err = test_skcipher_vec_cfg(enc, vec, vec_name,
4e7babba
EB
2988 &default_cipher_testvec_configs[i],
2989 req, tsgls);
2990 if (err)
2991 return err;
2992 }
da7f033d 2993
4e7babba
EB
2994#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2995 if (!noextratests) {
f900fde2 2996 struct rnd_state rng;
4e7babba
EB
2997 struct testvec_config cfg;
2998 char cfgname[TESTVEC_CONFIG_NAMELEN];
2999
f900fde2
EB
3000 init_rnd_state(&rng);
3001
4e7babba 3002 for (i = 0; i < fuzz_iterations; i++) {
f900fde2 3003 generate_random_testvec_config(&rng, &cfg, cfgname,
4e7babba 3004 sizeof(cfgname));
6e5972fa 3005 err = test_skcipher_vec_cfg(enc, vec, vec_name,
4e7babba
EB
3006 &cfg, req, tsgls);
3007 if (err)
3008 return err;
e63e1b0d 3009 cond_resched();
da7f033d
HX
3010 }
3011 }
4e7babba
EB
3012#endif
3013 return 0;
3014}
da7f033d 3015
d435e10e
EB
3016#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
3017/*
3018 * Generate a symmetric cipher test vector from the given implementation.
3019 * Assumes the buffers in 'vec' were already allocated.
3020 */
f900fde2
EB
3021static void generate_random_cipher_testvec(struct rnd_state *rng,
3022 struct skcipher_request *req,
d435e10e
EB
3023 struct cipher_testvec *vec,
3024 unsigned int maxdatasize,
3025 char *name, size_t max_namelen)
3026{
3027 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
9ac0d136 3028 const unsigned int maxkeysize = crypto_skcipher_max_keysize(tfm);
d435e10e
EB
3029 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
3030 struct scatterlist src, dst;
3031 u8 iv[MAX_IVLEN];
3032 DECLARE_CRYPTO_WAIT(wait);
3033
3034 /* Key: length in [0, maxkeysize], but usually choose maxkeysize */
3035 vec->klen = maxkeysize;
f900fde2
EB
3036 if (prandom_u32_below(rng, 4) == 0)
3037 vec->klen = prandom_u32_below(rng, maxkeysize + 1);
3038 generate_random_bytes(rng, (u8 *)vec->key, vec->klen);
d435e10e
EB
3039 vec->setkey_error = crypto_skcipher_setkey(tfm, vec->key, vec->klen);
3040
3041 /* IV */
f900fde2 3042 generate_random_bytes(rng, (u8 *)vec->iv, ivsize);
d435e10e
EB
3043
3044 /* Plaintext */
f900fde2
EB
3045 vec->len = generate_random_length(rng, maxdatasize);
3046 generate_random_bytes(rng, (u8 *)vec->ptext, vec->len);
d435e10e
EB
3047
3048 /* If the key couldn't be set, no need to continue to encrypt. */
3049 if (vec->setkey_error)
3050 goto done;
3051
3052 /* Ciphertext */
3053 sg_init_one(&src, vec->ptext, vec->len);
3054 sg_init_one(&dst, vec->ctext, vec->len);
3055 memcpy(iv, vec->iv, ivsize);
3056 skcipher_request_set_callback(req, 0, crypto_req_done, &wait);
3057 skcipher_request_set_crypt(req, &src, &dst, vec->len, iv);
3058 vec->crypt_error = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
eb455dbd
EB
3059 if (vec->crypt_error != 0) {
3060 /*
3061 * The only acceptable error here is for an invalid length, so
3062 * skcipher decryption should fail with the same error too.
3063 * We'll test for this. But to keep the API usage well-defined,
3064 * explicitly initialize the ciphertext buffer too.
3065 */
3066 memset((u8 *)vec->ctext, 0, vec->len);
3067 }
d435e10e
EB
3068done:
3069 snprintf(name, max_namelen, "\"random: len=%u klen=%u\"",
3070 vec->len, vec->klen);
3071}
3072
3073/*
3074 * Test the skcipher algorithm represented by @req against the corresponding
3075 * generic implementation, if one is available.
3076 */
6e5972fa 3077static int test_skcipher_vs_generic_impl(const char *generic_driver,
d435e10e
EB
3078 struct skcipher_request *req,
3079 struct cipher_test_sglists *tsgls)
3080{
3081 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
9ac0d136 3082 const unsigned int maxkeysize = crypto_skcipher_max_keysize(tfm);
d435e10e
EB
3083 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
3084 const unsigned int blocksize = crypto_skcipher_blocksize(tfm);
3085 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
3086 const char *algname = crypto_skcipher_alg(tfm)->base.cra_name;
6e5972fa 3087 const char *driver = crypto_skcipher_driver_name(tfm);
f900fde2 3088 struct rnd_state rng;
d435e10e
EB
3089 char _generic_driver[CRYPTO_MAX_ALG_NAME];
3090 struct crypto_skcipher *generic_tfm = NULL;
3091 struct skcipher_request *generic_req = NULL;
3092 unsigned int i;
3093 struct cipher_testvec vec = { 0 };
3094 char vec_name[64];
6b5ca646 3095 struct testvec_config *cfg;
d435e10e
EB
3096 char cfgname[TESTVEC_CONFIG_NAMELEN];
3097 int err;
3098
3099 if (noextratests)
3100 return 0;
3101
3102 /* Keywrap isn't supported here yet as it handles its IV differently. */
3103 if (strncmp(algname, "kw(", 3) == 0)
3104 return 0;
3105
f900fde2
EB
3106 init_rnd_state(&rng);
3107
d435e10e
EB
3108 if (!generic_driver) { /* Use default naming convention? */
3109 err = build_generic_driver_name(algname, _generic_driver);
3110 if (err)
3111 return err;
3112 generic_driver = _generic_driver;
3113 }
3114
3115 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
3116 return 0;
3117
3118 generic_tfm = crypto_alloc_skcipher(generic_driver, 0, 0);
3119 if (IS_ERR(generic_tfm)) {
3120 err = PTR_ERR(generic_tfm);
3121 if (err == -ENOENT) {
3122 pr_warn("alg: skcipher: skipping comparison tests for %s because %s is unavailable\n",
3123 driver, generic_driver);
3124 return 0;
3125 }
3126 pr_err("alg: skcipher: error allocating %s (generic impl of %s): %d\n",
3127 generic_driver, algname, err);
3128 return err;
3129 }
3130
6b5ca646
AB
3131 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
3132 if (!cfg) {
3133 err = -ENOMEM;
3134 goto out;
3135 }
3136
d435e10e
EB
3137 generic_req = skcipher_request_alloc(generic_tfm, GFP_KERNEL);
3138 if (!generic_req) {
3139 err = -ENOMEM;
3140 goto out;
3141 }
3142
3143 /* Check the algorithm properties for consistency. */
3144
fd60f727
EB
3145 if (crypto_skcipher_min_keysize(tfm) !=
3146 crypto_skcipher_min_keysize(generic_tfm)) {
3147 pr_err("alg: skcipher: min keysize for %s (%u) doesn't match generic impl (%u)\n",
3148 driver, crypto_skcipher_min_keysize(tfm),
3149 crypto_skcipher_min_keysize(generic_tfm));
3150 err = -EINVAL;
3151 goto out;
3152 }
3153
9ac0d136 3154 if (maxkeysize != crypto_skcipher_max_keysize(generic_tfm)) {
d435e10e 3155 pr_err("alg: skcipher: max keysize for %s (%u) doesn't match generic impl (%u)\n",
9ac0d136
EB
3156 driver, maxkeysize,
3157 crypto_skcipher_max_keysize(generic_tfm));
d435e10e
EB
3158 err = -EINVAL;
3159 goto out;
3160 }
3161
3162 if (ivsize != crypto_skcipher_ivsize(generic_tfm)) {
3163 pr_err("alg: skcipher: ivsize for %s (%u) doesn't match generic impl (%u)\n",
3164 driver, ivsize, crypto_skcipher_ivsize(generic_tfm));
3165 err = -EINVAL;
3166 goto out;
3167 }
3168
3169 if (blocksize != crypto_skcipher_blocksize(generic_tfm)) {
3170 pr_err("alg: skcipher: blocksize for %s (%u) doesn't match generic impl (%u)\n",
3171 driver, blocksize,
3172 crypto_skcipher_blocksize(generic_tfm));
3173 err = -EINVAL;
3174 goto out;
3175 }
3176
3177 /*
3178 * Now generate test vectors using the generic implementation, and test
3179 * the other implementation against them.
3180 */
3181
9ac0d136 3182 vec.key = kmalloc(maxkeysize, GFP_KERNEL);
d435e10e
EB
3183 vec.iv = kmalloc(ivsize, GFP_KERNEL);
3184 vec.ptext = kmalloc(maxdatasize, GFP_KERNEL);
3185 vec.ctext = kmalloc(maxdatasize, GFP_KERNEL);
3186 if (!vec.key || !vec.iv || !vec.ptext || !vec.ctext) {
3187 err = -ENOMEM;
3188 goto out;
3189 }
3190
3191 for (i = 0; i < fuzz_iterations * 8; i++) {
f900fde2
EB
3192 generate_random_cipher_testvec(&rng, generic_req, &vec,
3193 maxdatasize,
d435e10e 3194 vec_name, sizeof(vec_name));
f900fde2
EB
3195 generate_random_testvec_config(&rng, cfg, cfgname,
3196 sizeof(cfgname));
d435e10e 3197
6e5972fa 3198 err = test_skcipher_vec_cfg(ENCRYPT, &vec, vec_name,
6b5ca646 3199 cfg, req, tsgls);
d435e10e
EB
3200 if (err)
3201 goto out;
6e5972fa 3202 err = test_skcipher_vec_cfg(DECRYPT, &vec, vec_name,
6b5ca646 3203 cfg, req, tsgls);
d435e10e
EB
3204 if (err)
3205 goto out;
3206 cond_resched();
3207 }
3208 err = 0;
3209out:
6b5ca646 3210 kfree(cfg);
d435e10e
EB
3211 kfree(vec.key);
3212 kfree(vec.iv);
3213 kfree(vec.ptext);
3214 kfree(vec.ctext);
3215 crypto_free_skcipher(generic_tfm);
3216 skcipher_request_free(generic_req);
3217 return err;
3218}
3219#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
6e5972fa 3220static int test_skcipher_vs_generic_impl(const char *generic_driver,
d435e10e
EB
3221 struct skcipher_request *req,
3222 struct cipher_test_sglists *tsgls)
3223{
3224 return 0;
3225}
3226#endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
3227
6e5972fa 3228static int test_skcipher(int enc, const struct cipher_test_suite *suite,
4e7babba
EB
3229 struct skcipher_request *req,
3230 struct cipher_test_sglists *tsgls)
3231{
3232 unsigned int i;
3233 int err;
da7f033d 3234
4e7babba 3235 for (i = 0; i < suite->count; i++) {
6e5972fa 3236 err = test_skcipher_vec(enc, &suite->vecs[i], i, req, tsgls);
4e7babba
EB
3237 if (err)
3238 return err;
e63e1b0d 3239 cond_resched();
4e7babba
EB
3240 }
3241 return 0;
da7f033d
HX
3242}
3243
4e7babba
EB
3244static int alg_test_skcipher(const struct alg_test_desc *desc,
3245 const char *driver, u32 type, u32 mask)
08d6af8c 3246{
4e7babba
EB
3247 const struct cipher_test_suite *suite = &desc->suite.cipher;
3248 struct crypto_skcipher *tfm;
3249 struct skcipher_request *req = NULL;
3250 struct cipher_test_sglists *tsgls = NULL;
3251 int err;
08d6af8c 3252
4e7babba
EB
3253 if (suite->count <= 0) {
3254 pr_err("alg: skcipher: empty test suite for %s\n", driver);
3255 return -EINVAL;
3256 }
08d6af8c 3257
4e7babba
EB
3258 tfm = crypto_alloc_skcipher(driver, type, mask);
3259 if (IS_ERR(tfm)) {
3260 pr_err("alg: skcipher: failed to allocate transform for %s: %ld\n",
3261 driver, PTR_ERR(tfm));
3262 return PTR_ERR(tfm);
3263 }
6e5972fa 3264 driver = crypto_skcipher_driver_name(tfm);
3a338f20 3265
4e7babba
EB
3266 req = skcipher_request_alloc(tfm, GFP_KERNEL);
3267 if (!req) {
3268 pr_err("alg: skcipher: failed to allocate request for %s\n",
3269 driver);
3270 err = -ENOMEM;
3271 goto out;
3272 }
3a338f20 3273
4e7babba
EB
3274 tsgls = alloc_cipher_test_sglists();
3275 if (!tsgls) {
3276 pr_err("alg: skcipher: failed to allocate test buffers for %s\n",
3277 driver);
3278 err = -ENOMEM;
3279 goto out;
3a338f20
JK
3280 }
3281
6e5972fa 3282 err = test_skcipher(ENCRYPT, suite, req, tsgls);
4e7babba
EB
3283 if (err)
3284 goto out;
3285
6e5972fa 3286 err = test_skcipher(DECRYPT, suite, req, tsgls);
d435e10e
EB
3287 if (err)
3288 goto out;
3289
6e5972fa 3290 err = test_skcipher_vs_generic_impl(desc->generic_driver, req, tsgls);
4e7babba
EB
3291out:
3292 free_cipher_test_sglists(tsgls);
3293 skcipher_request_free(req);
3294 crypto_free_skcipher(tfm);
3295 return err;
08d6af8c
JK
3296}
3297
b13b1e0c
EB
3298static int test_comp(struct crypto_comp *tfm,
3299 const struct comp_testvec *ctemplate,
3300 const struct comp_testvec *dtemplate,
3301 int ctcount, int dtcount)
da7f033d
HX
3302{
3303 const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
33607384 3304 char *output, *decomp_output;
da7f033d 3305 unsigned int i;
da7f033d
HX
3306 int ret;
3307
33607384
MC
3308 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3309 if (!output)
3310 return -ENOMEM;
3311
3312 decomp_output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3313 if (!decomp_output) {
3314 kfree(output);
3315 return -ENOMEM;
3316 }
3317
da7f033d 3318 for (i = 0; i < ctcount; i++) {
c79cf910
GU
3319 int ilen;
3320 unsigned int dlen = COMP_BUF_SIZE;
da7f033d 3321
22a8118d
MS
3322 memset(output, 0, COMP_BUF_SIZE);
3323 memset(decomp_output, 0, COMP_BUF_SIZE);
da7f033d
HX
3324
3325 ilen = ctemplate[i].inlen;
3326 ret = crypto_comp_compress(tfm, ctemplate[i].input,
33607384 3327 ilen, output, &dlen);
da7f033d
HX
3328 if (ret) {
3329 printk(KERN_ERR "alg: comp: compression failed "
3330 "on test %d for %s: ret=%d\n", i + 1, algo,
3331 -ret);
3332 goto out;
3333 }
3334
33607384
MC
3335 ilen = dlen;
3336 dlen = COMP_BUF_SIZE;
3337 ret = crypto_comp_decompress(tfm, output,
3338 ilen, decomp_output, &dlen);
3339 if (ret) {
3340 pr_err("alg: comp: compression failed: decompress: on test %d for %s failed: ret=%d\n",
3341 i + 1, algo, -ret);
3342 goto out;
3343 }
3344
3345 if (dlen != ctemplate[i].inlen) {
b812eb00
GU
3346 printk(KERN_ERR "alg: comp: Compression test %d "
3347 "failed for %s: output len = %d\n", i + 1, algo,
3348 dlen);
3349 ret = -EINVAL;
3350 goto out;
3351 }
3352
33607384
MC
3353 if (memcmp(decomp_output, ctemplate[i].input,
3354 ctemplate[i].inlen)) {
3355 pr_err("alg: comp: compression failed: output differs: on test %d for %s\n",
3356 i + 1, algo);
3357 hexdump(decomp_output, dlen);
da7f033d
HX
3358 ret = -EINVAL;
3359 goto out;
3360 }
3361 }
3362
3363 for (i = 0; i < dtcount; i++) {
c79cf910
GU
3364 int ilen;
3365 unsigned int dlen = COMP_BUF_SIZE;
da7f033d 3366
22a8118d 3367 memset(decomp_output, 0, COMP_BUF_SIZE);
da7f033d
HX
3368
3369 ilen = dtemplate[i].inlen;
3370 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
33607384 3371 ilen, decomp_output, &dlen);
da7f033d
HX
3372 if (ret) {
3373 printk(KERN_ERR "alg: comp: decompression failed "
3374 "on test %d for %s: ret=%d\n", i + 1, algo,
3375 -ret);
3376 goto out;
3377 }
3378
b812eb00
GU
3379 if (dlen != dtemplate[i].outlen) {
3380 printk(KERN_ERR "alg: comp: Decompression test %d "
3381 "failed for %s: output len = %d\n", i + 1, algo,
3382 dlen);
3383 ret = -EINVAL;
3384 goto out;
3385 }
3386
33607384 3387 if (memcmp(decomp_output, dtemplate[i].output, dlen)) {
da7f033d
HX
3388 printk(KERN_ERR "alg: comp: Decompression test %d "
3389 "failed for %s\n", i + 1, algo);
33607384 3390 hexdump(decomp_output, dlen);
da7f033d
HX
3391 ret = -EINVAL;
3392 goto out;
3393 }
3394 }
3395
3396 ret = 0;
3397
3398out:
33607384
MC
3399 kfree(decomp_output);
3400 kfree(output);
da7f033d
HX
3401 return ret;
3402}
3403
b13b1e0c 3404static int test_acomp(struct crypto_acomp *tfm,
442f0606 3405 const struct comp_testvec *ctemplate,
b13b1e0c
EB
3406 const struct comp_testvec *dtemplate,
3407 int ctcount, int dtcount)
d7db7a88
GC
3408{
3409 const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
3410 unsigned int i;
a9943a0a 3411 char *output, *decomp_out;
d7db7a88
GC
3412 int ret;
3413 struct scatterlist src, dst;
3414 struct acomp_req *req;
7f397136 3415 struct crypto_wait wait;
d7db7a88 3416
eb095593
EB
3417 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3418 if (!output)
3419 return -ENOMEM;
3420
a9943a0a
GC
3421 decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3422 if (!decomp_out) {
3423 kfree(output);
3424 return -ENOMEM;
3425 }
3426
d7db7a88
GC
3427 for (i = 0; i < ctcount; i++) {
3428 unsigned int dlen = COMP_BUF_SIZE;
3429 int ilen = ctemplate[i].inlen;
02608e02 3430 void *input_vec;
d7db7a88 3431
d2110224 3432 input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL);
02608e02
LA
3433 if (!input_vec) {
3434 ret = -ENOMEM;
3435 goto out;
3436 }
3437
eb095593 3438 memset(output, 0, dlen);
7f397136 3439 crypto_init_wait(&wait);
02608e02 3440 sg_init_one(&src, input_vec, ilen);
d7db7a88
GC
3441 sg_init_one(&dst, output, dlen);
3442
3443 req = acomp_request_alloc(tfm);
3444 if (!req) {
3445 pr_err("alg: acomp: request alloc failed for %s\n",
3446 algo);
02608e02 3447 kfree(input_vec);
d7db7a88
GC
3448 ret = -ENOMEM;
3449 goto out;
3450 }
3451
3452 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3453 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
7f397136 3454 crypto_req_done, &wait);
d7db7a88 3455
7f397136 3456 ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
d7db7a88
GC
3457 if (ret) {
3458 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
3459 i + 1, algo, -ret);
02608e02 3460 kfree(input_vec);
d7db7a88
GC
3461 acomp_request_free(req);
3462 goto out;
3463 }
3464
a9943a0a
GC
3465 ilen = req->dlen;
3466 dlen = COMP_BUF_SIZE;
3467 sg_init_one(&src, output, ilen);
3468 sg_init_one(&dst, decomp_out, dlen);
7f397136 3469 crypto_init_wait(&wait);
a9943a0a
GC
3470 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3471
7f397136 3472 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
a9943a0a
GC
3473 if (ret) {
3474 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
3475 i + 1, algo, -ret);
3476 kfree(input_vec);
3477 acomp_request_free(req);
3478 goto out;
3479 }
3480
3481 if (req->dlen != ctemplate[i].inlen) {
d7db7a88
GC
3482 pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
3483 i + 1, algo, req->dlen);
3484 ret = -EINVAL;
02608e02 3485 kfree(input_vec);
d7db7a88
GC
3486 acomp_request_free(req);
3487 goto out;
3488 }
3489
a9943a0a 3490 if (memcmp(input_vec, decomp_out, req->dlen)) {
d7db7a88
GC
3491 pr_err("alg: acomp: Compression test %d failed for %s\n",
3492 i + 1, algo);
3493 hexdump(output, req->dlen);
3494 ret = -EINVAL;
02608e02 3495 kfree(input_vec);
d7db7a88
GC
3496 acomp_request_free(req);
3497 goto out;
3498 }
3499
5a4c2936
LSF
3500#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
3501 crypto_init_wait(&wait);
3502 sg_init_one(&src, input_vec, ilen);
3503 acomp_request_set_params(req, &src, NULL, ilen, 0);
3504
3505 ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
3506 if (ret) {
3507 pr_err("alg: acomp: compression failed on NULL dst buffer test %d for %s: ret=%d\n",
3508 i + 1, algo, -ret);
3509 kfree(input_vec);
3510 acomp_request_free(req);
3511 goto out;
3512 }
3513#endif
3514
02608e02 3515 kfree(input_vec);
d7db7a88
GC
3516 acomp_request_free(req);
3517 }
3518
3519 for (i = 0; i < dtcount; i++) {
3520 unsigned int dlen = COMP_BUF_SIZE;
3521 int ilen = dtemplate[i].inlen;
02608e02
LA
3522 void *input_vec;
3523
d2110224 3524 input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL);
02608e02
LA
3525 if (!input_vec) {
3526 ret = -ENOMEM;
3527 goto out;
3528 }
d7db7a88 3529
eb095593 3530 memset(output, 0, dlen);
7f397136 3531 crypto_init_wait(&wait);
02608e02 3532 sg_init_one(&src, input_vec, ilen);
d7db7a88
GC
3533 sg_init_one(&dst, output, dlen);
3534
3535 req = acomp_request_alloc(tfm);
3536 if (!req) {
3537 pr_err("alg: acomp: request alloc failed for %s\n",
3538 algo);
02608e02 3539 kfree(input_vec);
d7db7a88
GC
3540 ret = -ENOMEM;
3541 goto out;
3542 }
3543
3544 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3545 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
7f397136 3546 crypto_req_done, &wait);
d7db7a88 3547
7f397136 3548 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
d7db7a88
GC
3549 if (ret) {
3550 pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
3551 i + 1, algo, -ret);
02608e02 3552 kfree(input_vec);
d7db7a88
GC
3553 acomp_request_free(req);
3554 goto out;
3555 }
3556
3557 if (req->dlen != dtemplate[i].outlen) {
3558 pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
3559 i + 1, algo, req->dlen);
3560 ret = -EINVAL;
02608e02 3561 kfree(input_vec);
d7db7a88
GC
3562 acomp_request_free(req);
3563 goto out;
3564 }
3565
3566 if (memcmp(output, dtemplate[i].output, req->dlen)) {
3567 pr_err("alg: acomp: Decompression test %d failed for %s\n",
3568 i + 1, algo);
3569 hexdump(output, req->dlen);
3570 ret = -EINVAL;
02608e02 3571 kfree(input_vec);
d7db7a88
GC
3572 acomp_request_free(req);
3573 goto out;
3574 }
3575
5a4c2936
LSF
3576#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
3577 crypto_init_wait(&wait);
3578 acomp_request_set_params(req, &src, NULL, ilen, 0);
3579
3580 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
3581 if (ret) {
3582 pr_err("alg: acomp: decompression failed on NULL dst buffer test %d for %s: ret=%d\n",
3583 i + 1, algo, -ret);
3584 kfree(input_vec);
3585 acomp_request_free(req);
3586 goto out;
3587 }
3588#endif
3589
02608e02 3590 kfree(input_vec);
d7db7a88
GC
3591 acomp_request_free(req);
3592 }
3593
3594 ret = 0;
3595
3596out:
a9943a0a 3597 kfree(decomp_out);
eb095593 3598 kfree(output);
d7db7a88
GC
3599 return ret;
3600}
3601
b13b1e0c
EB
3602static int test_cprng(struct crypto_rng *tfm,
3603 const struct cprng_testvec *template,
7647d6ce
JW
3604 unsigned int tcount)
3605{
3606 const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
fa4ef8a6 3607 int err = 0, i, j, seedsize;
7647d6ce
JW
3608 u8 *seed;
3609 char result[32];
3610
3611 seedsize = crypto_rng_seedsize(tfm);
3612
3613 seed = kmalloc(seedsize, GFP_KERNEL);
3614 if (!seed) {
3615 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
3616 "for %s\n", algo);
3617 return -ENOMEM;
3618 }
3619
3620 for (i = 0; i < tcount; i++) {
3621 memset(result, 0, 32);
3622
3623 memcpy(seed, template[i].v, template[i].vlen);
3624 memcpy(seed + template[i].vlen, template[i].key,
3625 template[i].klen);
3626 memcpy(seed + template[i].vlen + template[i].klen,
3627 template[i].dt, template[i].dtlen);
3628
3629 err = crypto_rng_reset(tfm, seed, seedsize);
3630 if (err) {
3631 printk(KERN_ERR "alg: cprng: Failed to reset rng "
3632 "for %s\n", algo);
3633 goto out;
3634 }
3635
3636 for (j = 0; j < template[i].loops; j++) {
3637 err = crypto_rng_get_bytes(tfm, result,
3638 template[i].rlen);
19e60e13 3639 if (err < 0) {
7647d6ce
JW
3640 printk(KERN_ERR "alg: cprng: Failed to obtain "
3641 "the correct amount of random data for "
19e60e13
SM
3642 "%s (requested %d)\n", algo,
3643 template[i].rlen);
7647d6ce
JW
3644 goto out;
3645 }
3646 }
3647
3648 err = memcmp(result, template[i].result,
3649 template[i].rlen);
3650 if (err) {
3651 printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
3652 i, algo);
3653 hexdump(result, template[i].rlen);
3654 err = -EINVAL;
3655 goto out;
3656 }
3657 }
3658
3659out:
3660 kfree(seed);
3661 return err;
3662}
3663
da7f033d
HX
3664static int alg_test_cipher(const struct alg_test_desc *desc,
3665 const char *driver, u32 type, u32 mask)
3666{
92a4c9fe 3667 const struct cipher_test_suite *suite = &desc->suite.cipher;
1aa4ecd9 3668 struct crypto_cipher *tfm;
92a4c9fe 3669 int err;
da7f033d 3670
eed93e0c 3671 tfm = crypto_alloc_cipher(driver, type, mask);
da7f033d
HX
3672 if (IS_ERR(tfm)) {
3673 printk(KERN_ERR "alg: cipher: Failed to load transform for "
3674 "%s: %ld\n", driver, PTR_ERR(tfm));
3675 return PTR_ERR(tfm);
3676 }
3677
92a4c9fe
EB
3678 err = test_cipher(tfm, ENCRYPT, suite->vecs, suite->count);
3679 if (!err)
3680 err = test_cipher(tfm, DECRYPT, suite->vecs, suite->count);
da7f033d 3681
1aa4ecd9
HX
3682 crypto_free_cipher(tfm);
3683 return err;
3684}
3685
da7f033d
HX
3686static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
3687 u32 type, u32 mask)
3688{
d7db7a88
GC
3689 struct crypto_comp *comp;
3690 struct crypto_acomp *acomp;
da7f033d 3691 int err;
d7db7a88
GC
3692 u32 algo_type = type & CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
3693
3694 if (algo_type == CRYPTO_ALG_TYPE_ACOMPRESS) {
3695 acomp = crypto_alloc_acomp(driver, type, mask);
3696 if (IS_ERR(acomp)) {
3697 pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
3698 driver, PTR_ERR(acomp));
3699 return PTR_ERR(acomp);
3700 }
3701 err = test_acomp(acomp, desc->suite.comp.comp.vecs,
3702 desc->suite.comp.decomp.vecs,
3703 desc->suite.comp.comp.count,
3704 desc->suite.comp.decomp.count);
3705 crypto_free_acomp(acomp);
3706 } else {
3707 comp = crypto_alloc_comp(driver, type, mask);
3708 if (IS_ERR(comp)) {
3709 pr_err("alg: comp: Failed to load transform for %s: %ld\n",
3710 driver, PTR_ERR(comp));
3711 return PTR_ERR(comp);
3712 }
da7f033d 3713
d7db7a88
GC
3714 err = test_comp(comp, desc->suite.comp.comp.vecs,
3715 desc->suite.comp.decomp.vecs,
3716 desc->suite.comp.comp.count,
3717 desc->suite.comp.decomp.count);
da7f033d 3718
d7db7a88
GC
3719 crypto_free_comp(comp);
3720 }
da7f033d
HX
3721 return err;
3722}
3723
8e3ee85e
HX
3724static int alg_test_crc32c(const struct alg_test_desc *desc,
3725 const char *driver, u32 type, u32 mask)
3726{
3727 struct crypto_shash *tfm;
cb9dde88 3728 __le32 val;
8e3ee85e
HX
3729 int err;
3730
3731 err = alg_test_hash(desc, driver, type, mask);
3732 if (err)
eb5e6730 3733 return err;
8e3ee85e 3734
eed93e0c 3735 tfm = crypto_alloc_shash(driver, type, mask);
8e3ee85e 3736 if (IS_ERR(tfm)) {
eb5e6730
EB
3737 if (PTR_ERR(tfm) == -ENOENT) {
3738 /*
3739 * This crc32c implementation is only available through
3740 * ahash API, not the shash API, so the remaining part
3741 * of the test is not applicable to it.
3742 */
3743 return 0;
3744 }
8e3ee85e
HX
3745 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
3746 "%ld\n", driver, PTR_ERR(tfm));
eb5e6730 3747 return PTR_ERR(tfm);
8e3ee85e 3748 }
79cafe9a 3749 driver = crypto_shash_driver_name(tfm);
8e3ee85e
HX
3750
3751 do {
4c5c3024
JSM
3752 SHASH_DESC_ON_STACK(shash, tfm);
3753 u32 *ctx = (u32 *)shash_desc_ctx(shash);
8e3ee85e 3754
4c5c3024 3755 shash->tfm = tfm;
8e3ee85e 3756
cb9dde88 3757 *ctx = 420553207;
4c5c3024 3758 err = crypto_shash_final(shash, (u8 *)&val);
8e3ee85e
HX
3759 if (err) {
3760 printk(KERN_ERR "alg: crc32c: Operation failed for "
3761 "%s: %d\n", driver, err);
3762 break;
3763 }
3764
cb9dde88
EB
3765 if (val != cpu_to_le32(~420553207)) {
3766 pr_err("alg: crc32c: Test failed for %s: %u\n",
3767 driver, le32_to_cpu(val));
8e3ee85e
HX
3768 err = -EINVAL;
3769 }
3770 } while (0);
3771
3772 crypto_free_shash(tfm);
3773
8e3ee85e
HX
3774 return err;
3775}
3776
7647d6ce
JW
3777static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
3778 u32 type, u32 mask)
3779{
3780 struct crypto_rng *rng;
3781 int err;
3782
eed93e0c 3783 rng = crypto_alloc_rng(driver, type, mask);
7647d6ce
JW
3784 if (IS_ERR(rng)) {
3785 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
3786 "%ld\n", driver, PTR_ERR(rng));
3787 return PTR_ERR(rng);
3788 }
3789
3790 err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
3791
3792 crypto_free_rng(rng);
3793
3794 return err;
3795}
3796
64d1cdfb 3797
b13b1e0c 3798static int drbg_cavs_test(const struct drbg_testvec *test, int pr,
64d1cdfb
SM
3799 const char *driver, u32 type, u32 mask)
3800{
3801 int ret = -EAGAIN;
3802 struct crypto_rng *drng;
3803 struct drbg_test_data test_data;
3804 struct drbg_string addtl, pers, testentropy;
3805 unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
3806
3807 if (!buf)
3808 return -ENOMEM;
3809
eed93e0c 3810 drng = crypto_alloc_rng(driver, type, mask);
64d1cdfb 3811 if (IS_ERR(drng)) {
2fc0d258 3812 printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
64d1cdfb 3813 "%s\n", driver);
453431a5 3814 kfree_sensitive(buf);
64d1cdfb
SM
3815 return -ENOMEM;
3816 }
3817
3818 test_data.testentropy = &testentropy;
3819 drbg_string_fill(&testentropy, test->entropy, test->entropylen);
3820 drbg_string_fill(&pers, test->pers, test->perslen);
3821 ret = crypto_drbg_reset_test(drng, &pers, &test_data);
3822 if (ret) {
3823 printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
3824 goto outbuf;
3825 }
3826
3827 drbg_string_fill(&addtl, test->addtla, test->addtllen);
3828 if (pr) {
3829 drbg_string_fill(&testentropy, test->entpra, test->entprlen);
3830 ret = crypto_drbg_get_bytes_addtl_test(drng,
3831 buf, test->expectedlen, &addtl, &test_data);
3832 } else {
3833 ret = crypto_drbg_get_bytes_addtl(drng,
3834 buf, test->expectedlen, &addtl);
3835 }
19e60e13 3836 if (ret < 0) {
2fc0d258 3837 printk(KERN_ERR "alg: drbg: could not obtain random data for "
64d1cdfb
SM
3838 "driver %s\n", driver);
3839 goto outbuf;
3840 }
3841
3842 drbg_string_fill(&addtl, test->addtlb, test->addtllen);
3843 if (pr) {
3844 drbg_string_fill(&testentropy, test->entprb, test->entprlen);
3845 ret = crypto_drbg_get_bytes_addtl_test(drng,
3846 buf, test->expectedlen, &addtl, &test_data);
3847 } else {
3848 ret = crypto_drbg_get_bytes_addtl(drng,
3849 buf, test->expectedlen, &addtl);
3850 }
19e60e13 3851 if (ret < 0) {
2fc0d258 3852 printk(KERN_ERR "alg: drbg: could not obtain random data for "
64d1cdfb
SM
3853 "driver %s\n", driver);
3854 goto outbuf;
3855 }
3856
3857 ret = memcmp(test->expected, buf, test->expectedlen);
3858
3859outbuf:
3860 crypto_free_rng(drng);
453431a5 3861 kfree_sensitive(buf);
64d1cdfb
SM
3862 return ret;
3863}
3864
3865
3866static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
3867 u32 type, u32 mask)
3868{
3869 int err = 0;
3870 int pr = 0;
3871 int i = 0;
b13b1e0c 3872 const struct drbg_testvec *template = desc->suite.drbg.vecs;
64d1cdfb
SM
3873 unsigned int tcount = desc->suite.drbg.count;
3874
3875 if (0 == memcmp(driver, "drbg_pr_", 8))
3876 pr = 1;
3877
3878 for (i = 0; i < tcount; i++) {
3879 err = drbg_cavs_test(&template[i], pr, driver, type, mask);
3880 if (err) {
3881 printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
3882 i, driver);
3883 err = -EINVAL;
3884 break;
3885 }
3886 }
3887 return err;
3888
3889}
3890
b13b1e0c 3891static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
802c7f1c
SB
3892 const char *alg)
3893{
3894 struct kpp_request *req;
3895 void *input_buf = NULL;
3896 void *output_buf = NULL;
47d3fd39
TDA
3897 void *a_public = NULL;
3898 void *a_ss = NULL;
3899 void *shared_secret = NULL;
7f397136 3900 struct crypto_wait wait;
802c7f1c
SB
3901 unsigned int out_len_max;
3902 int err = -ENOMEM;
3903 struct scatterlist src, dst;
3904
3905 req = kpp_request_alloc(tfm, GFP_KERNEL);
3906 if (!req)
3907 return err;
3908
7f397136 3909 crypto_init_wait(&wait);
802c7f1c
SB
3910
3911 err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
3912 if (err < 0)
3913 goto free_req;
3914
3915 out_len_max = crypto_kpp_maxsize(tfm);
3916 output_buf = kzalloc(out_len_max, GFP_KERNEL);
3917 if (!output_buf) {
3918 err = -ENOMEM;
3919 goto free_req;
3920 }
3921
3922 /* Use appropriate parameter as base */
3923 kpp_request_set_input(req, NULL, 0);
3924 sg_init_one(&dst, output_buf, out_len_max);
3925 kpp_request_set_output(req, &dst, out_len_max);
3926 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
7f397136 3927 crypto_req_done, &wait);
802c7f1c 3928
47d3fd39 3929 /* Compute party A's public key */
7f397136 3930 err = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
802c7f1c 3931 if (err) {
47d3fd39 3932 pr_err("alg: %s: Party A: generate public key test failed. err %d\n",
802c7f1c
SB
3933 alg, err);
3934 goto free_output;
3935 }
47d3fd39
TDA
3936
3937 if (vec->genkey) {
3938 /* Save party A's public key */
e3d90e52 3939 a_public = kmemdup(sg_virt(req->dst), out_len_max, GFP_KERNEL);
47d3fd39
TDA
3940 if (!a_public) {
3941 err = -ENOMEM;
3942 goto free_output;
3943 }
47d3fd39
TDA
3944 } else {
3945 /* Verify calculated public key */
3946 if (memcmp(vec->expected_a_public, sg_virt(req->dst),
3947 vec->expected_a_public_size)) {
3948 pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n",
3949 alg);
3950 err = -EINVAL;
3951 goto free_output;
3952 }
802c7f1c
SB
3953 }
3954
3955 /* Calculate shared secret key by using counter part (b) public key. */
e3d90e52 3956 input_buf = kmemdup(vec->b_public, vec->b_public_size, GFP_KERNEL);
802c7f1c
SB
3957 if (!input_buf) {
3958 err = -ENOMEM;
3959 goto free_output;
3960 }
3961
802c7f1c
SB
3962 sg_init_one(&src, input_buf, vec->b_public_size);
3963 sg_init_one(&dst, output_buf, out_len_max);
3964 kpp_request_set_input(req, &src, vec->b_public_size);
3965 kpp_request_set_output(req, &dst, out_len_max);
3966 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
7f397136
GBY
3967 crypto_req_done, &wait);
3968 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait);
802c7f1c 3969 if (err) {
47d3fd39 3970 pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n",
802c7f1c
SB
3971 alg, err);
3972 goto free_all;
3973 }
47d3fd39
TDA
3974
3975 if (vec->genkey) {
3976 /* Save the shared secret obtained by party A */
e3d90e52 3977 a_ss = kmemdup(sg_virt(req->dst), vec->expected_ss_size, GFP_KERNEL);
47d3fd39
TDA
3978 if (!a_ss) {
3979 err = -ENOMEM;
3980 goto free_all;
3981 }
47d3fd39
TDA
3982
3983 /*
3984 * Calculate party B's shared secret by using party A's
3985 * public key.
3986 */
3987 err = crypto_kpp_set_secret(tfm, vec->b_secret,
3988 vec->b_secret_size);
3989 if (err < 0)
3990 goto free_all;
3991
3992 sg_init_one(&src, a_public, vec->expected_a_public_size);
3993 sg_init_one(&dst, output_buf, out_len_max);
3994 kpp_request_set_input(req, &src, vec->expected_a_public_size);
3995 kpp_request_set_output(req, &dst, out_len_max);
3996 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
7f397136
GBY
3997 crypto_req_done, &wait);
3998 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req),
3999 &wait);
47d3fd39
TDA
4000 if (err) {
4001 pr_err("alg: %s: Party B: compute shared secret failed. err %d\n",
4002 alg, err);
4003 goto free_all;
4004 }
4005
4006 shared_secret = a_ss;
4007 } else {
4008 shared_secret = (void *)vec->expected_ss;
4009 }
4010
802c7f1c
SB
4011 /*
4012 * verify shared secret from which the user will derive
4013 * secret key by executing whatever hash it has chosen
4014 */
47d3fd39 4015 if (memcmp(shared_secret, sg_virt(req->dst),
802c7f1c
SB
4016 vec->expected_ss_size)) {
4017 pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
4018 alg);
4019 err = -EINVAL;
4020 }
4021
4022free_all:
47d3fd39 4023 kfree(a_ss);
802c7f1c
SB
4024 kfree(input_buf);
4025free_output:
47d3fd39 4026 kfree(a_public);
802c7f1c
SB
4027 kfree(output_buf);
4028free_req:
4029 kpp_request_free(req);
4030 return err;
4031}
4032
4033static int test_kpp(struct crypto_kpp *tfm, const char *alg,
b13b1e0c 4034 const struct kpp_testvec *vecs, unsigned int tcount)
802c7f1c
SB
4035{
4036 int ret, i;
4037
4038 for (i = 0; i < tcount; i++) {
4039 ret = do_test_kpp(tfm, vecs++, alg);
4040 if (ret) {
4041 pr_err("alg: %s: test failed on vector %d, err=%d\n",
4042 alg, i + 1, ret);
4043 return ret;
4044 }
4045 }
4046 return 0;
4047}
4048
4049static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
4050 u32 type, u32 mask)
4051{
4052 struct crypto_kpp *tfm;
4053 int err = 0;
4054
eed93e0c 4055 tfm = crypto_alloc_kpp(driver, type, mask);
802c7f1c
SB
4056 if (IS_ERR(tfm)) {
4057 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
4058 driver, PTR_ERR(tfm));
4059 return PTR_ERR(tfm);
4060 }
4061 if (desc->suite.kpp.vecs)
4062 err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
4063 desc->suite.kpp.count);
4064
4065 crypto_free_kpp(tfm);
4066 return err;
4067}
4068
f1774cb8
VC
4069static u8 *test_pack_u32(u8 *dst, u32 val)
4070{
4071 memcpy(dst, &val, sizeof(val));
4072 return dst + sizeof(val);
4073}
4074
50d2b643 4075static int test_akcipher_one(struct crypto_akcipher *tfm,
b13b1e0c 4076 const struct akcipher_testvec *vecs)
946cc463 4077{
df27b26f 4078 char *xbuf[XBUFSIZE];
946cc463
TS
4079 struct akcipher_request *req;
4080 void *outbuf_enc = NULL;
4081 void *outbuf_dec = NULL;
7f397136 4082 struct crypto_wait wait;
946cc463
TS
4083 unsigned int out_len_max, out_len = 0;
4084 int err = -ENOMEM;
c7381b01 4085 struct scatterlist src, dst, src_tab[3];
0507de94
VC
4086 const char *m, *c;
4087 unsigned int m_size, c_size;
4088 const char *op;
f1774cb8 4089 u8 *key, *ptr;
946cc463 4090
df27b26f
HX
4091 if (testmgr_alloc_buf(xbuf))
4092 return err;
4093
946cc463
TS
4094 req = akcipher_request_alloc(tfm, GFP_KERNEL);
4095 if (!req)
df27b26f 4096 goto free_xbuf;
946cc463 4097
7f397136 4098 crypto_init_wait(&wait);
946cc463 4099
f1774cb8
VC
4100 key = kmalloc(vecs->key_len + sizeof(u32) * 2 + vecs->param_len,
4101 GFP_KERNEL);
4102 if (!key)
2b403867 4103 goto free_req;
f1774cb8
VC
4104 memcpy(key, vecs->key, vecs->key_len);
4105 ptr = key + vecs->key_len;
4106 ptr = test_pack_u32(ptr, vecs->algo);
4107 ptr = test_pack_u32(ptr, vecs->param_len);
4108 memcpy(ptr, vecs->params, vecs->param_len);
4109
22287b0b 4110 if (vecs->public_key_vec)
f1774cb8 4111 err = crypto_akcipher_set_pub_key(tfm, key, vecs->key_len);
22287b0b 4112 else
f1774cb8 4113 err = crypto_akcipher_set_priv_key(tfm, key, vecs->key_len);
22287b0b 4114 if (err)
2b403867 4115 goto free_key;
946cc463 4116
0507de94
VC
4117 /*
4118 * First run test which do not require a private key, such as
4119 * encrypt or verify.
4120 */
c7381b01
VC
4121 err = -ENOMEM;
4122 out_len_max = crypto_akcipher_maxsize(tfm);
946cc463
TS
4123 outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
4124 if (!outbuf_enc)
2b403867 4125 goto free_key;
946cc463 4126
0507de94
VC
4127 if (!vecs->siggen_sigver_test) {
4128 m = vecs->m;
4129 m_size = vecs->m_size;
4130 c = vecs->c;
4131 c_size = vecs->c_size;
4132 op = "encrypt";
4133 } else {
4134 /* Swap args so we could keep plaintext (digest)
4135 * in vecs->m, and cooked signature in vecs->c.
4136 */
4137 m = vecs->c; /* signature */
4138 m_size = vecs->c_size;
4139 c = vecs->m; /* digest */
4140 c_size = vecs->m_size;
4141 op = "verify";
4142 }
df27b26f 4143
2b403867 4144 err = -E2BIG;
0507de94
VC
4145 if (WARN_ON(m_size > PAGE_SIZE))
4146 goto free_all;
4147 memcpy(xbuf[0], m, m_size);
df27b26f 4148
c7381b01 4149 sg_init_table(src_tab, 3);
df27b26f 4150 sg_set_buf(&src_tab[0], xbuf[0], 8);
0507de94 4151 sg_set_buf(&src_tab[1], xbuf[0] + 8, m_size - 8);
c7381b01
VC
4152 if (vecs->siggen_sigver_test) {
4153 if (WARN_ON(c_size > PAGE_SIZE))
4154 goto free_all;
4155 memcpy(xbuf[1], c, c_size);
4156 sg_set_buf(&src_tab[2], xbuf[1], c_size);
4157 akcipher_request_set_crypt(req, src_tab, NULL, m_size, c_size);
4158 } else {
4159 sg_init_one(&dst, outbuf_enc, out_len_max);
4160 akcipher_request_set_crypt(req, src_tab, &dst, m_size,
4161 out_len_max);
4162 }
946cc463 4163 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
7f397136 4164 crypto_req_done, &wait);
946cc463 4165
7f397136 4166 err = crypto_wait_req(vecs->siggen_sigver_test ?
0507de94
VC
4167 /* Run asymmetric signature verification */
4168 crypto_akcipher_verify(req) :
7f397136
GBY
4169 /* Run asymmetric encrypt */
4170 crypto_akcipher_encrypt(req), &wait);
946cc463 4171 if (err) {
0507de94 4172 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
946cc463
TS
4173 goto free_all;
4174 }
a1f62c21 4175 if (!vecs->siggen_sigver_test && c) {
c7381b01
VC
4176 if (req->dst_len != c_size) {
4177 pr_err("alg: akcipher: %s test failed. Invalid output len\n",
4178 op);
4179 err = -EINVAL;
4180 goto free_all;
4181 }
4182 /* verify that encrypted message is equal to expected */
4183 if (memcmp(c, outbuf_enc, c_size) != 0) {
4184 pr_err("alg: akcipher: %s test failed. Invalid output\n",
4185 op);
4186 hexdump(outbuf_enc, c_size);
4187 err = -EINVAL;
4188 goto free_all;
4189 }
946cc463 4190 }
0507de94
VC
4191
4192 /*
4193 * Don't invoke (decrypt or sign) test which require a private key
4194 * for vectors with only a public key.
4195 */
946cc463
TS
4196 if (vecs->public_key_vec) {
4197 err = 0;
4198 goto free_all;
4199 }
4200 outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
4201 if (!outbuf_dec) {
4202 err = -ENOMEM;
4203 goto free_all;
4204 }
df27b26f 4205
a1f62c21
TZ
4206 if (!vecs->siggen_sigver_test && !c) {
4207 c = outbuf_enc;
4208 c_size = req->dst_len;
4209 }
4210
2b403867 4211 err = -E2BIG;
0507de94
VC
4212 op = vecs->siggen_sigver_test ? "sign" : "decrypt";
4213 if (WARN_ON(c_size > PAGE_SIZE))
df27b26f 4214 goto free_all;
0507de94 4215 memcpy(xbuf[0], c, c_size);
df27b26f 4216
0507de94 4217 sg_init_one(&src, xbuf[0], c_size);
22287b0b 4218 sg_init_one(&dst, outbuf_dec, out_len_max);
7f397136 4219 crypto_init_wait(&wait);
0507de94 4220 akcipher_request_set_crypt(req, &src, &dst, c_size, out_len_max);
946cc463 4221
7f397136 4222 err = crypto_wait_req(vecs->siggen_sigver_test ?
0507de94
VC
4223 /* Run asymmetric signature generation */
4224 crypto_akcipher_sign(req) :
7f397136
GBY
4225 /* Run asymmetric decrypt */
4226 crypto_akcipher_decrypt(req), &wait);
946cc463 4227 if (err) {
0507de94 4228 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
946cc463
TS
4229 goto free_all;
4230 }
4231 out_len = req->dst_len;
0507de94
VC
4232 if (out_len < m_size) {
4233 pr_err("alg: akcipher: %s test failed. Invalid output len %u\n",
4234 op, out_len);
946cc463
TS
4235 err = -EINVAL;
4236 goto free_all;
4237 }
4238 /* verify that decrypted message is equal to the original msg */
0507de94
VC
4239 if (memchr_inv(outbuf_dec, 0, out_len - m_size) ||
4240 memcmp(m, outbuf_dec + out_len - m_size, m_size)) {
4241 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
50d2b643 4242 hexdump(outbuf_dec, out_len);
946cc463
TS
4243 err = -EINVAL;
4244 }
4245free_all:
4246 kfree(outbuf_dec);
4247 kfree(outbuf_enc);
2b403867
TZ
4248free_key:
4249 kfree(key);
946cc463
TS
4250free_req:
4251 akcipher_request_free(req);
df27b26f
HX
4252free_xbuf:
4253 testmgr_free_buf(xbuf);
946cc463
TS
4254 return err;
4255}
4256
50d2b643 4257static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
b13b1e0c
EB
4258 const struct akcipher_testvec *vecs,
4259 unsigned int tcount)
946cc463 4260{
15226e48
HX
4261 const char *algo =
4262 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
946cc463
TS
4263 int ret, i;
4264
4265 for (i = 0; i < tcount; i++) {
50d2b643
HX
4266 ret = test_akcipher_one(tfm, vecs++);
4267 if (!ret)
4268 continue;
946cc463 4269
15226e48
HX
4270 pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
4271 i + 1, algo, ret);
50d2b643
HX
4272 return ret;
4273 }
946cc463
TS
4274 return 0;
4275}
4276
4277static int alg_test_akcipher(const struct alg_test_desc *desc,
4278 const char *driver, u32 type, u32 mask)
4279{
4280 struct crypto_akcipher *tfm;
4281 int err = 0;
4282
eed93e0c 4283 tfm = crypto_alloc_akcipher(driver, type, mask);
946cc463
TS
4284 if (IS_ERR(tfm)) {
4285 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
4286 driver, PTR_ERR(tfm));
4287 return PTR_ERR(tfm);
4288 }
4289 if (desc->suite.akcipher.vecs)
4290 err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
4291 desc->suite.akcipher.count);
4292
4293 crypto_free_akcipher(tfm);
4294 return err;
4295}
4296
863b557a
YS
4297static int alg_test_null(const struct alg_test_desc *desc,
4298 const char *driver, u32 type, u32 mask)
4299{
4300 return 0;
4301}
4302
49763fc6
EB
4303#define ____VECS(tv) .vecs = tv, .count = ARRAY_SIZE(tv)
4304#define __VECS(tv) { ____VECS(tv) }
21c8e720 4305
da7f033d
HX
4306/* Please keep this list sorted by algorithm name. */
4307static const struct alg_test_desc alg_test_descs[] = {
4308 {
059c2a4d 4309 .alg = "adiantum(xchacha12,aes)",
d435e10e 4310 .generic_driver = "adiantum(xchacha12-generic,aes-generic,nhpoly1305-generic)",
059c2a4d
EB
4311 .test = alg_test_skcipher,
4312 .suite = {
4313 .cipher = __VECS(adiantum_xchacha12_aes_tv_template)
4314 },
4315 }, {
4316 .alg = "adiantum(xchacha20,aes)",
d435e10e 4317 .generic_driver = "adiantum(xchacha20-generic,aes-generic,nhpoly1305-generic)",
059c2a4d
EB
4318 .test = alg_test_skcipher,
4319 .suite = {
4320 .cipher = __VECS(adiantum_xchacha20_aes_tv_template)
4321 },
4322 }, {
b87dc203
OM
4323 .alg = "aegis128",
4324 .test = alg_test_aead,
4325 .suite = {
a0d608ee 4326 .aead = __VECS(aegis128_tv_template)
b87dc203 4327 }
b87dc203 4328 }, {
e08ca2da
JW
4329 .alg = "ansi_cprng",
4330 .test = alg_test_cprng,
4331 .suite = {
21c8e720 4332 .cprng = __VECS(ansi_cprng_aes_tv_template)
e08ca2da 4333 }
bca4feb0
HG
4334 }, {
4335 .alg = "authenc(hmac(md5),ecb(cipher_null))",
4336 .test = alg_test_aead,
bca4feb0 4337 .suite = {
a0d608ee 4338 .aead = __VECS(hmac_md5_ecb_cipher_null_tv_template)
bca4feb0 4339 }
e46e9a46 4340 }, {
a4198fd4 4341 .alg = "authenc(hmac(sha1),cbc(aes))",
e46e9a46 4342 .test = alg_test_aead,
bcf741cb 4343 .fips_allowed = 1,
e46e9a46 4344 .suite = {
a0d608ee 4345 .aead = __VECS(hmac_sha1_aes_cbc_tv_temp)
5208ed2c
NL
4346 }
4347 }, {
a4198fd4 4348 .alg = "authenc(hmac(sha1),cbc(des))",
5208ed2c 4349 .test = alg_test_aead,
5208ed2c 4350 .suite = {
a0d608ee 4351 .aead = __VECS(hmac_sha1_des_cbc_tv_temp)
5208ed2c
NL
4352 }
4353 }, {
a4198fd4 4354 .alg = "authenc(hmac(sha1),cbc(des3_ede))",
5208ed2c 4355 .test = alg_test_aead,
5208ed2c 4356 .suite = {
a0d608ee 4357 .aead = __VECS(hmac_sha1_des3_ede_cbc_tv_temp)
e46e9a46 4358 }
fb16abc2
MM
4359 }, {
4360 .alg = "authenc(hmac(sha1),ctr(aes))",
4361 .test = alg_test_null,
4362 .fips_allowed = 1,
bca4feb0
HG
4363 }, {
4364 .alg = "authenc(hmac(sha1),ecb(cipher_null))",
4365 .test = alg_test_aead,
bca4feb0 4366 .suite = {
a0d608ee 4367 .aead = __VECS(hmac_sha1_ecb_cipher_null_tv_temp)
5208ed2c 4368 }
8888690e
MM
4369 }, {
4370 .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
4371 .test = alg_test_null,
4372 .fips_allowed = 1,
5208ed2c 4373 }, {
a4198fd4 4374 .alg = "authenc(hmac(sha224),cbc(des))",
5208ed2c 4375 .test = alg_test_aead,
5208ed2c 4376 .suite = {
a0d608ee 4377 .aead = __VECS(hmac_sha224_des_cbc_tv_temp)
5208ed2c
NL
4378 }
4379 }, {
a4198fd4 4380 .alg = "authenc(hmac(sha224),cbc(des3_ede))",
5208ed2c 4381 .test = alg_test_aead,
5208ed2c 4382 .suite = {
a0d608ee 4383 .aead = __VECS(hmac_sha224_des3_ede_cbc_tv_temp)
bca4feb0 4384 }
e46e9a46 4385 }, {
a4198fd4 4386 .alg = "authenc(hmac(sha256),cbc(aes))",
e46e9a46 4387 .test = alg_test_aead,
ed1afac9 4388 .fips_allowed = 1,
e46e9a46 4389 .suite = {
a0d608ee 4390 .aead = __VECS(hmac_sha256_aes_cbc_tv_temp)
5208ed2c
NL
4391 }
4392 }, {
a4198fd4 4393 .alg = "authenc(hmac(sha256),cbc(des))",
5208ed2c 4394 .test = alg_test_aead,
5208ed2c 4395 .suite = {
a0d608ee 4396 .aead = __VECS(hmac_sha256_des_cbc_tv_temp)
5208ed2c
NL
4397 }
4398 }, {
a4198fd4 4399 .alg = "authenc(hmac(sha256),cbc(des3_ede))",
5208ed2c 4400 .test = alg_test_aead,
5208ed2c 4401 .suite = {
a0d608ee 4402 .aead = __VECS(hmac_sha256_des3_ede_cbc_tv_temp)
5208ed2c 4403 }
fb16abc2
MM
4404 }, {
4405 .alg = "authenc(hmac(sha256),ctr(aes))",
4406 .test = alg_test_null,
4407 .fips_allowed = 1,
8888690e
MM
4408 }, {
4409 .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
4410 .test = alg_test_null,
4411 .fips_allowed = 1,
5208ed2c 4412 }, {
a4198fd4 4413 .alg = "authenc(hmac(sha384),cbc(des))",
5208ed2c 4414 .test = alg_test_aead,
5208ed2c 4415 .suite = {
a0d608ee 4416 .aead = __VECS(hmac_sha384_des_cbc_tv_temp)
5208ed2c
NL
4417 }
4418 }, {
a4198fd4 4419 .alg = "authenc(hmac(sha384),cbc(des3_ede))",
5208ed2c 4420 .test = alg_test_aead,
5208ed2c 4421 .suite = {
a0d608ee 4422 .aead = __VECS(hmac_sha384_des3_ede_cbc_tv_temp)
e46e9a46 4423 }
fb16abc2
MM
4424 }, {
4425 .alg = "authenc(hmac(sha384),ctr(aes))",
4426 .test = alg_test_null,
4427 .fips_allowed = 1,
8888690e
MM
4428 }, {
4429 .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
4430 .test = alg_test_null,
4431 .fips_allowed = 1,
e46e9a46 4432 }, {
a4198fd4 4433 .alg = "authenc(hmac(sha512),cbc(aes))",
ed1afac9 4434 .fips_allowed = 1,
e46e9a46 4435 .test = alg_test_aead,
e46e9a46 4436 .suite = {
a0d608ee 4437 .aead = __VECS(hmac_sha512_aes_cbc_tv_temp)
5208ed2c
NL
4438 }
4439 }, {
a4198fd4 4440 .alg = "authenc(hmac(sha512),cbc(des))",
5208ed2c 4441 .test = alg_test_aead,
5208ed2c 4442 .suite = {
a0d608ee 4443 .aead = __VECS(hmac_sha512_des_cbc_tv_temp)
5208ed2c
NL
4444 }
4445 }, {
a4198fd4 4446 .alg = "authenc(hmac(sha512),cbc(des3_ede))",
5208ed2c 4447 .test = alg_test_aead,
5208ed2c 4448 .suite = {
a0d608ee 4449 .aead = __VECS(hmac_sha512_des3_ede_cbc_tv_temp)
e46e9a46 4450 }
fb16abc2
MM
4451 }, {
4452 .alg = "authenc(hmac(sha512),ctr(aes))",
4453 .test = alg_test_null,
4454 .fips_allowed = 1,
8888690e
MM
4455 }, {
4456 .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
4457 .test = alg_test_null,
4458 .fips_allowed = 1,
a1afe274
DS
4459 }, {
4460 .alg = "blake2b-160",
4461 .test = alg_test_hash,
4462 .fips_allowed = 0,
4463 .suite = {
4464 .hash = __VECS(blake2b_160_tv_template)
4465 }
4466 }, {
4467 .alg = "blake2b-256",
4468 .test = alg_test_hash,
4469 .fips_allowed = 0,
4470 .suite = {
4471 .hash = __VECS(blake2b_256_tv_template)
4472 }
4473 }, {
4474 .alg = "blake2b-384",
4475 .test = alg_test_hash,
4476 .fips_allowed = 0,
4477 .suite = {
4478 .hash = __VECS(blake2b_384_tv_template)
4479 }
4480 }, {
4481 .alg = "blake2b-512",
4482 .test = alg_test_hash,
4483 .fips_allowed = 0,
4484 .suite = {
4485 .hash = __VECS(blake2b_512_tv_template)
4486 }
e08ca2da 4487 }, {
da7f033d 4488 .alg = "cbc(aes)",
1aa4ecd9 4489 .test = alg_test_skcipher,
a1915d51 4490 .fips_allowed = 1,
da7f033d 4491 .suite = {
92a4c9fe
EB
4492 .cipher = __VECS(aes_cbc_tv_template)
4493 },
da7f033d
HX
4494 }, {
4495 .alg = "cbc(anubis)",
1aa4ecd9 4496 .test = alg_test_skcipher,
da7f033d 4497 .suite = {
92a4c9fe
EB
4498 .cipher = __VECS(anubis_cbc_tv_template)
4499 },
01ce31de
TY
4500 }, {
4501 .alg = "cbc(aria)",
4502 .test = alg_test_skcipher,
4503 .suite = {
4504 .cipher = __VECS(aria_cbc_tv_template)
4505 },
da7f033d
HX
4506 }, {
4507 .alg = "cbc(blowfish)",
1aa4ecd9 4508 .test = alg_test_skcipher,
da7f033d 4509 .suite = {
92a4c9fe
EB
4510 .cipher = __VECS(bf_cbc_tv_template)
4511 },
da7f033d
HX
4512 }, {
4513 .alg = "cbc(camellia)",
1aa4ecd9 4514 .test = alg_test_skcipher,
da7f033d 4515 .suite = {
92a4c9fe
EB
4516 .cipher = __VECS(camellia_cbc_tv_template)
4517 },
a2c58260
JG
4518 }, {
4519 .alg = "cbc(cast5)",
4520 .test = alg_test_skcipher,
4521 .suite = {
92a4c9fe
EB
4522 .cipher = __VECS(cast5_cbc_tv_template)
4523 },
9b8b0405
JG
4524 }, {
4525 .alg = "cbc(cast6)",
4526 .test = alg_test_skcipher,
4527 .suite = {
92a4c9fe
EB
4528 .cipher = __VECS(cast6_cbc_tv_template)
4529 },
da7f033d
HX
4530 }, {
4531 .alg = "cbc(des)",
1aa4ecd9 4532 .test = alg_test_skcipher,
da7f033d 4533 .suite = {
92a4c9fe
EB
4534 .cipher = __VECS(des_cbc_tv_template)
4535 },
da7f033d
HX
4536 }, {
4537 .alg = "cbc(des3_ede)",
1aa4ecd9 4538 .test = alg_test_skcipher,
da7f033d 4539 .suite = {
92a4c9fe
EB
4540 .cipher = __VECS(des3_ede_cbc_tv_template)
4541 },
a794d8d8
GBY
4542 }, {
4543 /* Same as cbc(aes) except the key is stored in
4544 * hardware secure memory which we reference by index
4545 */
4546 .alg = "cbc(paes)",
4547 .test = alg_test_null,
4548 .fips_allowed = 1,
f0372c00
GBY
4549 }, {
4550 /* Same as cbc(sm4) except the key is stored in
4551 * hardware secure memory which we reference by index
4552 */
4553 .alg = "cbc(psm4)",
4554 .test = alg_test_null,
9d25917d
JK
4555 }, {
4556 .alg = "cbc(serpent)",
4557 .test = alg_test_skcipher,
4558 .suite = {
92a4c9fe
EB
4559 .cipher = __VECS(serpent_cbc_tv_template)
4560 },
95ba5973
GBY
4561 }, {
4562 .alg = "cbc(sm4)",
4563 .test = alg_test_skcipher,
4564 .suite = {
4565 .cipher = __VECS(sm4_cbc_tv_template)
4566 }
da7f033d
HX
4567 }, {
4568 .alg = "cbc(twofish)",
1aa4ecd9 4569 .test = alg_test_skcipher,
da7f033d 4570 .suite = {
92a4c9fe
EB
4571 .cipher = __VECS(tf_cbc_tv_template)
4572 },
092acf06 4573 }, {
c7ff8573
HF
4574#if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
4575 .alg = "cbc-paes-s390",
4576 .fips_allowed = 1,
4577 .test = alg_test_skcipher,
4578 .suite = {
4579 .cipher = __VECS(aes_cbc_tv_template)
4580 }
4581 }, {
4582#endif
092acf06 4583 .alg = "cbcmac(aes)",
092acf06
AB
4584 .test = alg_test_hash,
4585 .suite = {
4586 .hash = __VECS(aes_cbcmac_tv_template)
4587 }
68039d60
TZ
4588 }, {
4589 .alg = "cbcmac(sm4)",
4590 .test = alg_test_hash,
4591 .suite = {
4592 .hash = __VECS(sm4_cbcmac_tv_template)
4593 }
da7f033d
HX
4594 }, {
4595 .alg = "ccm(aes)",
40153b10 4596 .generic_driver = "ccm_base(ctr(aes-generic),cbcmac(aes-generic))",
da7f033d 4597 .test = alg_test_aead,
a1915d51 4598 .fips_allowed = 1,
da7f033d 4599 .suite = {
49763fc6
EB
4600 .aead = {
4601 ____VECS(aes_ccm_tv_template),
4602 .einval_allowed = 1,
4603 }
da7f033d 4604 }
68039d60
TZ
4605 }, {
4606 .alg = "ccm(sm4)",
4607 .generic_driver = "ccm_base(ctr(sm4-generic),cbcmac(sm4-generic))",
4608 .test = alg_test_aead,
4609 .suite = {
4610 .aead = {
4611 ____VECS(sm4_ccm_tv_template),
4612 .einval_allowed = 1,
4613 }
4614 }
7da66670
DES
4615 }, {
4616 .alg = "cfb(aes)",
4617 .test = alg_test_skcipher,
4618 .fips_allowed = 1,
4619 .suite = {
4620 .cipher = __VECS(aes_cfb_tv_template)
4621 },
01ce31de
TY
4622 }, {
4623 .alg = "cfb(aria)",
4624 .test = alg_test_skcipher,
4625 .suite = {
4626 .cipher = __VECS(aria_cfb_tv_template)
4627 },
a06b15b2
PL
4628 }, {
4629 .alg = "cfb(sm4)",
4630 .test = alg_test_skcipher,
4631 .suite = {
4632 .cipher = __VECS(sm4_cfb_tv_template)
4633 }
3590ebf2
MW
4634 }, {
4635 .alg = "chacha20",
4636 .test = alg_test_skcipher,
4637 .suite = {
92a4c9fe
EB
4638 .cipher = __VECS(chacha20_tv_template)
4639 },
93b5e86a
JK
4640 }, {
4641 .alg = "cmac(aes)",
8f183751 4642 .fips_allowed = 1,
93b5e86a
JK
4643 .test = alg_test_hash,
4644 .suite = {
21c8e720 4645 .hash = __VECS(aes_cmac128_tv_template)
93b5e86a 4646 }
ba24b8eb
DH
4647 }, {
4648 .alg = "cmac(camellia)",
4649 .test = alg_test_hash,
4650 .suite = {
4651 .hash = __VECS(camellia_cmac128_tv_template)
4652 }
93b5e86a
JK
4653 }, {
4654 .alg = "cmac(des3_ede)",
4655 .test = alg_test_hash,
4656 .suite = {
21c8e720 4657 .hash = __VECS(des3_ede_cmac64_tv_template)
93b5e86a 4658 }
68039d60
TZ
4659 }, {
4660 .alg = "cmac(sm4)",
4661 .test = alg_test_hash,
4662 .suite = {
4663 .hash = __VECS(sm4_cmac128_tv_template)
4664 }
e448370d
JK
4665 }, {
4666 .alg = "compress_null",
4667 .test = alg_test_null,
ebb3472f
AB
4668 }, {
4669 .alg = "crc32",
4670 .test = alg_test_hash,
a8a34416 4671 .fips_allowed = 1,
ebb3472f 4672 .suite = {
21c8e720 4673 .hash = __VECS(crc32_tv_template)
ebb3472f 4674 }
da7f033d
HX
4675 }, {
4676 .alg = "crc32c",
8e3ee85e 4677 .test = alg_test_crc32c,
a1915d51 4678 .fips_allowed = 1,
da7f033d 4679 .suite = {
21c8e720 4680 .hash = __VECS(crc32c_tv_template)
da7f033d 4681 }
f3813f4b
KB
4682 }, {
4683 .alg = "crc64-rocksoft",
4684 .test = alg_test_hash,
4685 .fips_allowed = 1,
4686 .suite = {
4687 .hash = __VECS(crc64_rocksoft_tv_template)
4688 }
68411521
HX
4689 }, {
4690 .alg = "crct10dif",
4691 .test = alg_test_hash,
4692 .fips_allowed = 1,
4693 .suite = {
21c8e720 4694 .hash = __VECS(crct10dif_tv_template)
68411521 4695 }
f7cb80f2
JW
4696 }, {
4697 .alg = "ctr(aes)",
4698 .test = alg_test_skcipher,
a1915d51 4699 .fips_allowed = 1,
f7cb80f2 4700 .suite = {
92a4c9fe 4701 .cipher = __VECS(aes_ctr_tv_template)
f7cb80f2 4702 }
01ce31de
TY
4703 }, {
4704 .alg = "ctr(aria)",
4705 .test = alg_test_skcipher,
4706 .suite = {
4707 .cipher = __VECS(aria_ctr_tv_template)
4708 }
85b63e34
JK
4709 }, {
4710 .alg = "ctr(blowfish)",
4711 .test = alg_test_skcipher,
4712 .suite = {
92a4c9fe 4713 .cipher = __VECS(bf_ctr_tv_template)
85b63e34 4714 }
0840605e
JK
4715 }, {
4716 .alg = "ctr(camellia)",
4717 .test = alg_test_skcipher,
4718 .suite = {
92a4c9fe 4719 .cipher = __VECS(camellia_ctr_tv_template)
0840605e 4720 }
a2c58260
JG
4721 }, {
4722 .alg = "ctr(cast5)",
4723 .test = alg_test_skcipher,
4724 .suite = {
92a4c9fe 4725 .cipher = __VECS(cast5_ctr_tv_template)
a2c58260 4726 }
9b8b0405
JG
4727 }, {
4728 .alg = "ctr(cast6)",
4729 .test = alg_test_skcipher,
4730 .suite = {
92a4c9fe 4731 .cipher = __VECS(cast6_ctr_tv_template)
9b8b0405 4732 }
8163fc30
JK
4733 }, {
4734 .alg = "ctr(des)",
4735 .test = alg_test_skcipher,
4736 .suite = {
92a4c9fe 4737 .cipher = __VECS(des_ctr_tv_template)
8163fc30 4738 }
e080b17a
JK
4739 }, {
4740 .alg = "ctr(des3_ede)",
4741 .test = alg_test_skcipher,
4742 .suite = {
92a4c9fe 4743 .cipher = __VECS(des3_ede_ctr_tv_template)
e080b17a 4744 }
a794d8d8
GBY
4745 }, {
4746 /* Same as ctr(aes) except the key is stored in
4747 * hardware secure memory which we reference by index
4748 */
4749 .alg = "ctr(paes)",
4750 .test = alg_test_null,
4751 .fips_allowed = 1,
9d25917d 4752 }, {
f0372c00
GBY
4753
4754 /* Same as ctr(sm4) except the key is stored in
4755 * hardware secure memory which we reference by index
4756 */
4757 .alg = "ctr(psm4)",
4758 .test = alg_test_null,
4759 }, {
9d25917d
JK
4760 .alg = "ctr(serpent)",
4761 .test = alg_test_skcipher,
4762 .suite = {
92a4c9fe 4763 .cipher = __VECS(serpent_ctr_tv_template)
9d25917d 4764 }
95ba5973
GBY
4765 }, {
4766 .alg = "ctr(sm4)",
4767 .test = alg_test_skcipher,
4768 .suite = {
4769 .cipher = __VECS(sm4_ctr_tv_template)
4770 }
573da620
JK
4771 }, {
4772 .alg = "ctr(twofish)",
4773 .test = alg_test_skcipher,
4774 .suite = {
92a4c9fe 4775 .cipher = __VECS(tf_ctr_tv_template)
573da620 4776 }
da7f033d 4777 }, {
c7ff8573
HF
4778#if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
4779 .alg = "ctr-paes-s390",
4780 .fips_allowed = 1,
4781 .test = alg_test_skcipher,
4782 .suite = {
4783 .cipher = __VECS(aes_ctr_tv_template)
4784 }
4785 }, {
4786#endif
da7f033d 4787 .alg = "cts(cbc(aes))",
1aa4ecd9 4788 .test = alg_test_skcipher,
196ad604 4789 .fips_allowed = 1,
da7f033d 4790 .suite = {
92a4c9fe 4791 .cipher = __VECS(cts_mode_tv_template)
da7f033d 4792 }
f0372c00
GBY
4793 }, {
4794 /* Same as cts(cbc((aes)) except the key is stored in
4795 * hardware secure memory which we reference by index
4796 */
4797 .alg = "cts(cbc(paes))",
4798 .test = alg_test_null,
4799 .fips_allowed = 1,
c24ee936
TZ
4800 }, {
4801 .alg = "cts(cbc(sm4))",
4802 .test = alg_test_skcipher,
4803 .suite = {
4804 .cipher = __VECS(sm4_cts_tv_template)
4805 }
f613457a
AB
4806 }, {
4807 .alg = "curve25519",
4808 .test = alg_test_kpp,
4809 .suite = {
4810 .kpp = __VECS(curve25519_tv_template)
4811 }
da7f033d
HX
4812 }, {
4813 .alg = "deflate",
4814 .test = alg_test_comp,
0818904d 4815 .fips_allowed = 1,
da7f033d
HX
4816 .suite = {
4817 .comp = {
21c8e720
AB
4818 .comp = __VECS(deflate_comp_tv_template),
4819 .decomp = __VECS(deflate_decomp_tv_template)
da7f033d
HX
4820 }
4821 }
802c7f1c
SB
4822 }, {
4823 .alg = "dh",
4824 .test = alg_test_kpp,
802c7f1c 4825 .suite = {
21c8e720 4826 .kpp = __VECS(dh_tv_template)
802c7f1c 4827 }
e448370d
JK
4828 }, {
4829 .alg = "digest_null",
4830 .test = alg_test_null,
64d1cdfb
SM
4831 }, {
4832 .alg = "drbg_nopr_ctr_aes128",
4833 .test = alg_test_drbg,
4834 .fips_allowed = 1,
4835 .suite = {
21c8e720 4836 .drbg = __VECS(drbg_nopr_ctr_aes128_tv_template)
64d1cdfb
SM
4837 }
4838 }, {
4839 .alg = "drbg_nopr_ctr_aes192",
4840 .test = alg_test_drbg,
4841 .fips_allowed = 1,
4842 .suite = {
21c8e720 4843 .drbg = __VECS(drbg_nopr_ctr_aes192_tv_template)
64d1cdfb
SM
4844 }
4845 }, {
4846 .alg = "drbg_nopr_ctr_aes256",
4847 .test = alg_test_drbg,
4848 .fips_allowed = 1,
4849 .suite = {
21c8e720 4850 .drbg = __VECS(drbg_nopr_ctr_aes256_tv_template)
64d1cdfb
SM
4851 }
4852 }, {
4853 /*
4854 * There is no need to specifically test the DRBG with every
4855 * backend cipher -- covered by drbg_nopr_hmac_sha256 test
4856 */
4857 .alg = "drbg_nopr_hmac_sha1",
4858 .fips_allowed = 1,
4859 .test = alg_test_null,
4860 }, {
4861 .alg = "drbg_nopr_hmac_sha256",
4862 .test = alg_test_drbg,
4863 .fips_allowed = 1,
4864 .suite = {
21c8e720 4865 .drbg = __VECS(drbg_nopr_hmac_sha256_tv_template)
64d1cdfb
SM
4866 }
4867 }, {
4868 /* covered by drbg_nopr_hmac_sha256 test */
4869 .alg = "drbg_nopr_hmac_sha384",
64d1cdfb
SM
4870 .test = alg_test_null,
4871 }, {
4872 .alg = "drbg_nopr_hmac_sha512",
8833272d 4873 .test = alg_test_drbg,
64d1cdfb 4874 .fips_allowed = 1,
8833272d
SM
4875 .suite = {
4876 .drbg = __VECS(drbg_nopr_hmac_sha512_tv_template)
4877 }
64d1cdfb
SM
4878 }, {
4879 .alg = "drbg_nopr_sha1",
4880 .fips_allowed = 1,
4881 .test = alg_test_null,
4882 }, {
4883 .alg = "drbg_nopr_sha256",
4884 .test = alg_test_drbg,
4885 .fips_allowed = 1,
4886 .suite = {
21c8e720 4887 .drbg = __VECS(drbg_nopr_sha256_tv_template)
64d1cdfb
SM
4888 }
4889 }, {
4890 /* covered by drbg_nopr_sha256 test */
4891 .alg = "drbg_nopr_sha384",
64d1cdfb
SM
4892 .test = alg_test_null,
4893 }, {
4894 .alg = "drbg_nopr_sha512",
4895 .fips_allowed = 1,
4896 .test = alg_test_null,
4897 }, {
4898 .alg = "drbg_pr_ctr_aes128",
4899 .test = alg_test_drbg,
4900 .fips_allowed = 1,
4901 .suite = {
21c8e720 4902 .drbg = __VECS(drbg_pr_ctr_aes128_tv_template)
64d1cdfb
SM
4903 }
4904 }, {
4905 /* covered by drbg_pr_ctr_aes128 test */
4906 .alg = "drbg_pr_ctr_aes192",
4907 .fips_allowed = 1,
4908 .test = alg_test_null,
4909 }, {
4910 .alg = "drbg_pr_ctr_aes256",
4911 .fips_allowed = 1,
4912 .test = alg_test_null,
4913 }, {
4914 .alg = "drbg_pr_hmac_sha1",
4915 .fips_allowed = 1,
4916 .test = alg_test_null,
4917 }, {
4918 .alg = "drbg_pr_hmac_sha256",
4919 .test = alg_test_drbg,
4920 .fips_allowed = 1,
4921 .suite = {
21c8e720 4922 .drbg = __VECS(drbg_pr_hmac_sha256_tv_template)
64d1cdfb
SM
4923 }
4924 }, {
4925 /* covered by drbg_pr_hmac_sha256 test */
4926 .alg = "drbg_pr_hmac_sha384",
64d1cdfb
SM
4927 .test = alg_test_null,
4928 }, {
4929 .alg = "drbg_pr_hmac_sha512",
4930 .test = alg_test_null,
4931 .fips_allowed = 1,
4932 }, {
4933 .alg = "drbg_pr_sha1",
4934 .fips_allowed = 1,
4935 .test = alg_test_null,
4936 }, {
4937 .alg = "drbg_pr_sha256",
4938 .test = alg_test_drbg,
4939 .fips_allowed = 1,
4940 .suite = {
21c8e720 4941 .drbg = __VECS(drbg_pr_sha256_tv_template)
64d1cdfb
SM
4942 }
4943 }, {
4944 /* covered by drbg_pr_sha256 test */
4945 .alg = "drbg_pr_sha384",
64d1cdfb
SM
4946 .test = alg_test_null,
4947 }, {
4948 .alg = "drbg_pr_sha512",
4949 .fips_allowed = 1,
4950 .test = alg_test_null,
da7f033d
HX
4951 }, {
4952 .alg = "ecb(aes)",
1aa4ecd9 4953 .test = alg_test_skcipher,
a1915d51 4954 .fips_allowed = 1,
da7f033d 4955 .suite = {
92a4c9fe 4956 .cipher = __VECS(aes_tv_template)
da7f033d
HX
4957 }
4958 }, {
4959 .alg = "ecb(anubis)",
1aa4ecd9 4960 .test = alg_test_skcipher,
da7f033d 4961 .suite = {
92a4c9fe 4962 .cipher = __VECS(anubis_tv_template)
da7f033d
HX
4963 }
4964 }, {
4965 .alg = "ecb(arc4)",
611a23c2 4966 .generic_driver = "ecb(arc4)-generic",
1aa4ecd9 4967 .test = alg_test_skcipher,
da7f033d 4968 .suite = {
92a4c9fe 4969 .cipher = __VECS(arc4_tv_template)
da7f033d 4970 }
01ce31de
TY
4971 }, {
4972 .alg = "ecb(aria)",
4973 .test = alg_test_skcipher,
4974 .suite = {
4975 .cipher = __VECS(aria_tv_template)
4976 }
da7f033d
HX
4977 }, {
4978 .alg = "ecb(blowfish)",
1aa4ecd9 4979 .test = alg_test_skcipher,
da7f033d 4980 .suite = {
92a4c9fe 4981 .cipher = __VECS(bf_tv_template)
da7f033d
HX
4982 }
4983 }, {
4984 .alg = "ecb(camellia)",
1aa4ecd9 4985 .test = alg_test_skcipher,
da7f033d 4986 .suite = {
92a4c9fe 4987 .cipher = __VECS(camellia_tv_template)
da7f033d
HX
4988 }
4989 }, {
4990 .alg = "ecb(cast5)",
1aa4ecd9 4991 .test = alg_test_skcipher,
da7f033d 4992 .suite = {
92a4c9fe 4993 .cipher = __VECS(cast5_tv_template)
da7f033d
HX
4994 }
4995 }, {
4996 .alg = "ecb(cast6)",
1aa4ecd9 4997 .test = alg_test_skcipher,
da7f033d 4998 .suite = {
92a4c9fe 4999 .cipher = __VECS(cast6_tv_template)
da7f033d 5000 }
e448370d
JK
5001 }, {
5002 .alg = "ecb(cipher_null)",
5003 .test = alg_test_null,
6175ca2b 5004 .fips_allowed = 1,
da7f033d
HX
5005 }, {
5006 .alg = "ecb(des)",
1aa4ecd9 5007 .test = alg_test_skcipher,
da7f033d 5008 .suite = {
92a4c9fe 5009 .cipher = __VECS(des_tv_template)
da7f033d
HX
5010 }
5011 }, {
5012 .alg = "ecb(des3_ede)",
1aa4ecd9 5013 .test = alg_test_skcipher,
da7f033d 5014 .suite = {
92a4c9fe 5015 .cipher = __VECS(des3_ede_tv_template)
da7f033d 5016 }
66e5bd00
JK
5017 }, {
5018 .alg = "ecb(fcrypt)",
5019 .test = alg_test_skcipher,
5020 .suite = {
5021 .cipher = {
92a4c9fe
EB
5022 .vecs = fcrypt_pcbc_tv_template,
5023 .count = 1
66e5bd00
JK
5024 }
5025 }
da7f033d
HX
5026 }, {
5027 .alg = "ecb(khazad)",
1aa4ecd9 5028 .test = alg_test_skcipher,
da7f033d 5029 .suite = {
92a4c9fe 5030 .cipher = __VECS(khazad_tv_template)
da7f033d 5031 }
15f47ce5
GBY
5032 }, {
5033 /* Same as ecb(aes) except the key is stored in
5034 * hardware secure memory which we reference by index
5035 */
5036 .alg = "ecb(paes)",
5037 .test = alg_test_null,
5038 .fips_allowed = 1,
da7f033d
HX
5039 }, {
5040 .alg = "ecb(seed)",
1aa4ecd9 5041 .test = alg_test_skcipher,
da7f033d 5042 .suite = {
92a4c9fe 5043 .cipher = __VECS(seed_tv_template)
da7f033d
HX
5044 }
5045 }, {
5046 .alg = "ecb(serpent)",
1aa4ecd9 5047 .test = alg_test_skcipher,
da7f033d 5048 .suite = {
92a4c9fe 5049 .cipher = __VECS(serpent_tv_template)
da7f033d 5050 }
cd83a8a7
GBY
5051 }, {
5052 .alg = "ecb(sm4)",
5053 .test = alg_test_skcipher,
5054 .suite = {
92a4c9fe 5055 .cipher = __VECS(sm4_tv_template)
cd83a8a7 5056 }
da7f033d
HX
5057 }, {
5058 .alg = "ecb(tea)",
1aa4ecd9 5059 .test = alg_test_skcipher,
da7f033d 5060 .suite = {
92a4c9fe 5061 .cipher = __VECS(tea_tv_template)
da7f033d 5062 }
da7f033d
HX
5063 }, {
5064 .alg = "ecb(twofish)",
1aa4ecd9 5065 .test = alg_test_skcipher,
da7f033d 5066 .suite = {
92a4c9fe 5067 .cipher = __VECS(tf_tv_template)
da7f033d
HX
5068 }
5069 }, {
5070 .alg = "ecb(xeta)",
1aa4ecd9 5071 .test = alg_test_skcipher,
da7f033d 5072 .suite = {
92a4c9fe 5073 .cipher = __VECS(xeta_tv_template)
da7f033d
HX
5074 }
5075 }, {
5076 .alg = "ecb(xtea)",
1aa4ecd9 5077 .test = alg_test_skcipher,
da7f033d 5078 .suite = {
92a4c9fe 5079 .cipher = __VECS(xtea_tv_template)
da7f033d 5080 }
3c4b2390 5081 }, {
c7ff8573
HF
5082#if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
5083 .alg = "ecb-paes-s390",
5084 .fips_allowed = 1,
5085 .test = alg_test_skcipher,
5086 .suite = {
5087 .cipher = __VECS(aes_tv_template)
5088 }
5089 }, {
5090#endif
6763f5ea 5091 .alg = "ecdh-nist-p192",
3c4b2390 5092 .test = alg_test_kpp,
3c4b2390 5093 .suite = {
6763f5ea
MY
5094 .kpp = __VECS(ecdh_p192_tv_template)
5095 }
5096 }, {
6763f5ea
MY
5097 .alg = "ecdh-nist-p256",
5098 .test = alg_test_kpp,
5099 .fips_allowed = 1,
5100 .suite = {
5101 .kpp = __VECS(ecdh_p256_tv_template)
3c4b2390 5102 }
8e568fc2
HT
5103 }, {
5104 .alg = "ecdh-nist-p384",
5105 .test = alg_test_kpp,
5106 .fips_allowed = 1,
5107 .suite = {
5108 .kpp = __VECS(ecdh_p384_tv_template)
5109 }
4e660291
SB
5110 }, {
5111 .alg = "ecdsa-nist-p192",
5112 .test = alg_test_akcipher,
5113 .suite = {
5114 .akcipher = __VECS(ecdsa_nist_p192_tv_template)
5115 }
5116 }, {
5117 .alg = "ecdsa-nist-p256",
5118 .test = alg_test_akcipher,
c27b2d20 5119 .fips_allowed = 1,
4e660291
SB
5120 .suite = {
5121 .akcipher = __VECS(ecdsa_nist_p256_tv_template)
5122 }
c12d448b
SA
5123 }, {
5124 .alg = "ecdsa-nist-p384",
5125 .test = alg_test_akcipher,
c27b2d20 5126 .fips_allowed = 1,
c12d448b
SA
5127 .suite = {
5128 .akcipher = __VECS(ecdsa_nist_p384_tv_template)
5129 }
32fbdbd3
VC
5130 }, {
5131 .alg = "ecrdsa",
5132 .test = alg_test_akcipher,
5133 .suite = {
5134 .akcipher = __VECS(ecrdsa_tv_template)
5135 }
f975abb2
AB
5136 }, {
5137 .alg = "essiv(authenc(hmac(sha256),cbc(aes)),sha256)",
5138 .test = alg_test_aead,
5139 .fips_allowed = 1,
5140 .suite = {
5141 .aead = __VECS(essiv_hmac_sha256_aes_cbc_tv_temp)
5142 }
5143 }, {
5144 .alg = "essiv(cbc(aes),sha256)",
5145 .test = alg_test_skcipher,
5146 .fips_allowed = 1,
5147 .suite = {
5148 .cipher = __VECS(essiv_aes_cbc_tv_template)
5149 }
da7f033d 5150 }, {
60a273e9
NS
5151#if IS_ENABLED(CONFIG_CRYPTO_DH_RFC7919_GROUPS)
5152 .alg = "ffdhe2048(dh)",
5153 .test = alg_test_kpp,
5154 .fips_allowed = 1,
5155 .suite = {
5156 .kpp = __VECS(ffdhe2048_dh_tv_template)
5157 }
5158 }, {
5159 .alg = "ffdhe3072(dh)",
5160 .test = alg_test_kpp,
5161 .fips_allowed = 1,
5162 .suite = {
5163 .kpp = __VECS(ffdhe3072_dh_tv_template)
5164 }
5165 }, {
5166 .alg = "ffdhe4096(dh)",
5167 .test = alg_test_kpp,
5168 .fips_allowed = 1,
5169 .suite = {
5170 .kpp = __VECS(ffdhe4096_dh_tv_template)
5171 }
5172 }, {
5173 .alg = "ffdhe6144(dh)",
5174 .test = alg_test_kpp,
5175 .fips_allowed = 1,
5176 .suite = {
5177 .kpp = __VECS(ffdhe6144_dh_tv_template)
5178 }
5179 }, {
5180 .alg = "ffdhe8192(dh)",
5181 .test = alg_test_kpp,
5182 .fips_allowed = 1,
5183 .suite = {
5184 .kpp = __VECS(ffdhe8192_dh_tv_template)
5185 }
5186 }, {
5187#endif /* CONFIG_CRYPTO_DH_RFC7919_GROUPS */
da7f033d 5188 .alg = "gcm(aes)",
40153b10 5189 .generic_driver = "gcm_base(ctr(aes-generic),ghash-generic)",
da7f033d 5190 .test = alg_test_aead,
a1915d51 5191 .fips_allowed = 1,
da7f033d 5192 .suite = {
a0d608ee 5193 .aead = __VECS(aes_gcm_tv_template)
68039d60 5194 }
01ce31de
TY
5195 }, {
5196 .alg = "gcm(aria)",
5197 .generic_driver = "gcm_base(ctr(aria-generic),ghash-generic)",
5198 .test = alg_test_aead,
5199 .suite = {
5200 .aead = __VECS(aria_gcm_tv_template)
5201 }
68039d60
TZ
5202 }, {
5203 .alg = "gcm(sm4)",
5204 .generic_driver = "gcm_base(ctr(sm4-generic),ghash-generic)",
5205 .test = alg_test_aead,
5206 .suite = {
5207 .aead = __VECS(sm4_gcm_tv_template)
da7f033d 5208 }
507069c9
YS
5209 }, {
5210 .alg = "ghash",
5211 .test = alg_test_hash,
5212 .suite = {
21c8e720 5213 .hash = __VECS(ghash_tv_template)
507069c9 5214 }
7ff554ce
NH
5215 }, {
5216 .alg = "hctr2(aes)",
5217 .generic_driver =
5218 "hctr2_base(xctr(aes-generic),polyval-generic)",
5219 .test = alg_test_skcipher,
5220 .suite = {
5221 .cipher = __VECS(aes_hctr2_tv_template)
5222 }
da7f033d
HX
5223 }, {
5224 .alg = "hmac(md5)",
5225 .test = alg_test_hash,
5226 .suite = {
21c8e720 5227 .hash = __VECS(hmac_md5_tv_template)
da7f033d 5228 }
da7f033d
HX
5229 }, {
5230 .alg = "hmac(rmd160)",
5231 .test = alg_test_hash,
5232 .suite = {
21c8e720 5233 .hash = __VECS(hmac_rmd160_tv_template)
da7f033d
HX
5234 }
5235 }, {
5236 .alg = "hmac(sha1)",
5237 .test = alg_test_hash,
a1915d51 5238 .fips_allowed = 1,
da7f033d 5239 .suite = {
21c8e720 5240 .hash = __VECS(hmac_sha1_tv_template)
da7f033d
HX
5241 }
5242 }, {
5243 .alg = "hmac(sha224)",
5244 .test = alg_test_hash,
a1915d51 5245 .fips_allowed = 1,
da7f033d 5246 .suite = {
21c8e720 5247 .hash = __VECS(hmac_sha224_tv_template)
da7f033d
HX
5248 }
5249 }, {
5250 .alg = "hmac(sha256)",
5251 .test = alg_test_hash,
a1915d51 5252 .fips_allowed = 1,
da7f033d 5253 .suite = {
21c8e720 5254 .hash = __VECS(hmac_sha256_tv_template)
da7f033d 5255 }
98eca72f 5256 }, {
5257 .alg = "hmac(sha3-224)",
5258 .test = alg_test_hash,
5259 .fips_allowed = 1,
5260 .suite = {
21c8e720 5261 .hash = __VECS(hmac_sha3_224_tv_template)
98eca72f 5262 }
5263 }, {
5264 .alg = "hmac(sha3-256)",
5265 .test = alg_test_hash,
5266 .fips_allowed = 1,
5267 .suite = {
21c8e720 5268 .hash = __VECS(hmac_sha3_256_tv_template)
98eca72f 5269 }
5270 }, {
5271 .alg = "hmac(sha3-384)",
5272 .test = alg_test_hash,
5273 .fips_allowed = 1,
5274 .suite = {
21c8e720 5275 .hash = __VECS(hmac_sha3_384_tv_template)
98eca72f 5276 }
5277 }, {
5278 .alg = "hmac(sha3-512)",
5279 .test = alg_test_hash,
5280 .fips_allowed = 1,
5281 .suite = {
21c8e720 5282 .hash = __VECS(hmac_sha3_512_tv_template)
98eca72f 5283 }
da7f033d
HX
5284 }, {
5285 .alg = "hmac(sha384)",
5286 .test = alg_test_hash,
a1915d51 5287 .fips_allowed = 1,
da7f033d 5288 .suite = {
21c8e720 5289 .hash = __VECS(hmac_sha384_tv_template)
da7f033d
HX
5290 }
5291 }, {
5292 .alg = "hmac(sha512)",
5293 .test = alg_test_hash,
a1915d51 5294 .fips_allowed = 1,
da7f033d 5295 .suite = {
21c8e720 5296 .hash = __VECS(hmac_sha512_tv_template)
da7f033d 5297 }
8194fd1d
PL
5298 }, {
5299 .alg = "hmac(sm3)",
5300 .test = alg_test_hash,
5301 .suite = {
5302 .hash = __VECS(hmac_sm3_tv_template)
5303 }
25a0b9d4
VC
5304 }, {
5305 .alg = "hmac(streebog256)",
5306 .test = alg_test_hash,
5307 .suite = {
5308 .hash = __VECS(hmac_streebog256_tv_template)
5309 }
5310 }, {
5311 .alg = "hmac(streebog512)",
5312 .test = alg_test_hash,
5313 .suite = {
5314 .hash = __VECS(hmac_streebog512_tv_template)
5315 }
bb5530e4
SM
5316 }, {
5317 .alg = "jitterentropy_rng",
5318 .fips_allowed = 1,
5319 .test = alg_test_null,
35351988
SM
5320 }, {
5321 .alg = "kw(aes)",
5322 .test = alg_test_skcipher,
5323 .fips_allowed = 1,
5324 .suite = {
92a4c9fe 5325 .cipher = __VECS(aes_kw_tv_template)
35351988 5326 }
da7f033d
HX
5327 }, {
5328 .alg = "lrw(aes)",
d435e10e 5329 .generic_driver = "lrw(ecb(aes-generic))",
1aa4ecd9 5330 .test = alg_test_skcipher,
da7f033d 5331 .suite = {
92a4c9fe 5332 .cipher = __VECS(aes_lrw_tv_template)
da7f033d 5333 }
0840605e
JK
5334 }, {
5335 .alg = "lrw(camellia)",
d435e10e 5336 .generic_driver = "lrw(ecb(camellia-generic))",
0840605e
JK
5337 .test = alg_test_skcipher,
5338 .suite = {
92a4c9fe 5339 .cipher = __VECS(camellia_lrw_tv_template)
0840605e 5340 }
9b8b0405
JG
5341 }, {
5342 .alg = "lrw(cast6)",
d435e10e 5343 .generic_driver = "lrw(ecb(cast6-generic))",
9b8b0405
JG
5344 .test = alg_test_skcipher,
5345 .suite = {
92a4c9fe 5346 .cipher = __VECS(cast6_lrw_tv_template)
9b8b0405 5347 }
d7bfc0fa
JK
5348 }, {
5349 .alg = "lrw(serpent)",
d435e10e 5350 .generic_driver = "lrw(ecb(serpent-generic))",
d7bfc0fa
JK
5351 .test = alg_test_skcipher,
5352 .suite = {
92a4c9fe 5353 .cipher = __VECS(serpent_lrw_tv_template)
d7bfc0fa 5354 }
0b2a1551
JK
5355 }, {
5356 .alg = "lrw(twofish)",
d435e10e 5357 .generic_driver = "lrw(ecb(twofish-generic))",
0b2a1551
JK
5358 .test = alg_test_skcipher,
5359 .suite = {
92a4c9fe 5360 .cipher = __VECS(tf_lrw_tv_template)
0b2a1551 5361 }
1443cc9b
KK
5362 }, {
5363 .alg = "lz4",
5364 .test = alg_test_comp,
5365 .fips_allowed = 1,
5366 .suite = {
5367 .comp = {
21c8e720
AB
5368 .comp = __VECS(lz4_comp_tv_template),
5369 .decomp = __VECS(lz4_decomp_tv_template)
1443cc9b
KK
5370 }
5371 }
5372 }, {
5373 .alg = "lz4hc",
5374 .test = alg_test_comp,
5375 .fips_allowed = 1,
5376 .suite = {
5377 .comp = {
21c8e720
AB
5378 .comp = __VECS(lz4hc_comp_tv_template),
5379 .decomp = __VECS(lz4hc_decomp_tv_template)
1443cc9b
KK
5380 }
5381 }
da7f033d
HX
5382 }, {
5383 .alg = "lzo",
5384 .test = alg_test_comp,
0818904d 5385 .fips_allowed = 1,
da7f033d
HX
5386 .suite = {
5387 .comp = {
21c8e720
AB
5388 .comp = __VECS(lzo_comp_tv_template),
5389 .decomp = __VECS(lzo_decomp_tv_template)
da7f033d
HX
5390 }
5391 }
f248caf9
HP
5392 }, {
5393 .alg = "lzo-rle",
5394 .test = alg_test_comp,
5395 .fips_allowed = 1,
5396 .suite = {
5397 .comp = {
5398 .comp = __VECS(lzorle_comp_tv_template),
5399 .decomp = __VECS(lzorle_decomp_tv_template)
5400 }
5401 }
da7f033d
HX
5402 }, {
5403 .alg = "md4",
5404 .test = alg_test_hash,
5405 .suite = {
21c8e720 5406 .hash = __VECS(md4_tv_template)
da7f033d
HX
5407 }
5408 }, {
5409 .alg = "md5",
5410 .test = alg_test_hash,
5411 .suite = {
21c8e720 5412 .hash = __VECS(md5_tv_template)
da7f033d
HX
5413 }
5414 }, {
5415 .alg = "michael_mic",
5416 .test = alg_test_hash,
5417 .suite = {
21c8e720 5418 .hash = __VECS(michael_mic_tv_template)
da7f033d 5419 }
26609a21
EB
5420 }, {
5421 .alg = "nhpoly1305",
5422 .test = alg_test_hash,
5423 .suite = {
5424 .hash = __VECS(nhpoly1305_tv_template)
5425 }
ba0e14ac
PS
5426 }, {
5427 .alg = "ofb(aes)",
5428 .test = alg_test_skcipher,
5429 .fips_allowed = 1,
5430 .suite = {
92a4c9fe 5431 .cipher = __VECS(aes_ofb_tv_template)
ba0e14ac 5432 }
a794d8d8
GBY
5433 }, {
5434 /* Same as ofb(aes) except the key is stored in
5435 * hardware secure memory which we reference by index
5436 */
5437 .alg = "ofb(paes)",
5438 .test = alg_test_null,
5439 .fips_allowed = 1,
a06b15b2
PL
5440 }, {
5441 .alg = "ofb(sm4)",
5442 .test = alg_test_skcipher,
5443 .suite = {
5444 .cipher = __VECS(sm4_ofb_tv_template)
5445 }
da7f033d
HX
5446 }, {
5447 .alg = "pcbc(fcrypt)",
1aa4ecd9 5448 .test = alg_test_skcipher,
da7f033d 5449 .suite = {
92a4c9fe 5450 .cipher = __VECS(fcrypt_pcbc_tv_template)
da7f033d 5451 }
1207107c
SM
5452 }, {
5453 .alg = "pkcs1pad(rsa,sha224)",
5454 .test = alg_test_null,
5455 .fips_allowed = 1,
5456 }, {
5457 .alg = "pkcs1pad(rsa,sha256)",
5458 .test = alg_test_akcipher,
5459 .fips_allowed = 1,
5460 .suite = {
5461 .akcipher = __VECS(pkcs1pad_rsa_tv_template)
5462 }
5463 }, {
5464 .alg = "pkcs1pad(rsa,sha384)",
5465 .test = alg_test_null,
5466 .fips_allowed = 1,
5467 }, {
5468 .alg = "pkcs1pad(rsa,sha512)",
5469 .test = alg_test_null,
5470 .fips_allowed = 1,
eee9dc61
MW
5471 }, {
5472 .alg = "poly1305",
5473 .test = alg_test_hash,
5474 .suite = {
21c8e720 5475 .hash = __VECS(poly1305_tv_template)
eee9dc61 5476 }
f3c923a0
NH
5477 }, {
5478 .alg = "polyval",
5479 .test = alg_test_hash,
5480 .suite = {
5481 .hash = __VECS(polyval_tv_template)
5482 }
da7f033d
HX
5483 }, {
5484 .alg = "rfc3686(ctr(aes))",
1aa4ecd9 5485 .test = alg_test_skcipher,
a1915d51 5486 .fips_allowed = 1,
da7f033d 5487 .suite = {
92a4c9fe 5488 .cipher = __VECS(aes_ctr_rfc3686_tv_template)
da7f033d 5489 }
e4886214
PL
5490 }, {
5491 .alg = "rfc3686(ctr(sm4))",
5492 .test = alg_test_skcipher,
5493 .suite = {
5494 .cipher = __VECS(sm4_ctr_rfc3686_tv_template)
5495 }
5d667322 5496 }, {
3f31a740 5497 .alg = "rfc4106(gcm(aes))",
40153b10 5498 .generic_driver = "rfc4106(gcm_base(ctr(aes-generic),ghash-generic))",
69435b94 5499 .test = alg_test_aead,
db71f29a 5500 .fips_allowed = 1,
69435b94 5501 .suite = {
49763fc6
EB
5502 .aead = {
5503 ____VECS(aes_gcm_rfc4106_tv_template),
5504 .einval_allowed = 1,
6f3a06d9 5505 .aad_iv = 1,
49763fc6 5506 }
69435b94
AH
5507 }
5508 }, {
544c436a 5509 .alg = "rfc4309(ccm(aes))",
40153b10 5510 .generic_driver = "rfc4309(ccm_base(ctr(aes-generic),cbcmac(aes-generic)))",
5d667322 5511 .test = alg_test_aead,
a1915d51 5512 .fips_allowed = 1,
5d667322 5513 .suite = {
49763fc6
EB
5514 .aead = {
5515 ____VECS(aes_ccm_rfc4309_tv_template),
5516 .einval_allowed = 1,
6f3a06d9 5517 .aad_iv = 1,
49763fc6 5518 }
5d667322 5519 }
e9b7441a 5520 }, {
bb68745e 5521 .alg = "rfc4543(gcm(aes))",
40153b10 5522 .generic_driver = "rfc4543(gcm_base(ctr(aes-generic),ghash-generic))",
e9b7441a
JK
5523 .test = alg_test_aead,
5524 .suite = {
49763fc6
EB
5525 .aead = {
5526 ____VECS(aes_gcm_rfc4543_tv_template),
5527 .einval_allowed = 1,
6f3a06d9 5528 .aad_iv = 1,
49763fc6 5529 }
e9b7441a 5530 }
af2b76b5
MW
5531 }, {
5532 .alg = "rfc7539(chacha20,poly1305)",
5533 .test = alg_test_aead,
5534 .suite = {
a0d608ee 5535 .aead = __VECS(rfc7539_tv_template)
af2b76b5 5536 }
5900758d
MW
5537 }, {
5538 .alg = "rfc7539esp(chacha20,poly1305)",
5539 .test = alg_test_aead,
5540 .suite = {
49763fc6
EB
5541 .aead = {
5542 ____VECS(rfc7539esp_tv_template),
5543 .einval_allowed = 1,
6f3a06d9 5544 .aad_iv = 1,
49763fc6 5545 }
5900758d 5546 }
da7f033d
HX
5547 }, {
5548 .alg = "rmd160",
5549 .test = alg_test_hash,
5550 .suite = {
21c8e720 5551 .hash = __VECS(rmd160_tv_template)
da7f033d 5552 }
946cc463
TS
5553 }, {
5554 .alg = "rsa",
5555 .test = alg_test_akcipher,
5556 .fips_allowed = 1,
5557 .suite = {
21c8e720 5558 .akcipher = __VECS(rsa_tv_template)
946cc463 5559 }
da7f033d
HX
5560 }, {
5561 .alg = "sha1",
5562 .test = alg_test_hash,
a1915d51 5563 .fips_allowed = 1,
da7f033d 5564 .suite = {
21c8e720 5565 .hash = __VECS(sha1_tv_template)
da7f033d
HX
5566 }
5567 }, {
5568 .alg = "sha224",
5569 .test = alg_test_hash,
a1915d51 5570 .fips_allowed = 1,
da7f033d 5571 .suite = {
21c8e720 5572 .hash = __VECS(sha224_tv_template)
da7f033d
HX
5573 }
5574 }, {
5575 .alg = "sha256",
5576 .test = alg_test_hash,
a1915d51 5577 .fips_allowed = 1,
da7f033d 5578 .suite = {
21c8e720 5579 .hash = __VECS(sha256_tv_template)
da7f033d 5580 }
79cc6ab8 5581 }, {
5582 .alg = "sha3-224",
5583 .test = alg_test_hash,
5584 .fips_allowed = 1,
5585 .suite = {
21c8e720 5586 .hash = __VECS(sha3_224_tv_template)
79cc6ab8 5587 }
5588 }, {
5589 .alg = "sha3-256",
5590 .test = alg_test_hash,
5591 .fips_allowed = 1,
5592 .suite = {
21c8e720 5593 .hash = __VECS(sha3_256_tv_template)
79cc6ab8 5594 }
5595 }, {
5596 .alg = "sha3-384",
5597 .test = alg_test_hash,
5598 .fips_allowed = 1,
5599 .suite = {
21c8e720 5600 .hash = __VECS(sha3_384_tv_template)
79cc6ab8 5601 }
5602 }, {
5603 .alg = "sha3-512",
5604 .test = alg_test_hash,
5605 .fips_allowed = 1,
5606 .suite = {
21c8e720 5607 .hash = __VECS(sha3_512_tv_template)
79cc6ab8 5608 }
da7f033d
HX
5609 }, {
5610 .alg = "sha384",
5611 .test = alg_test_hash,
a1915d51 5612 .fips_allowed = 1,
da7f033d 5613 .suite = {
21c8e720 5614 .hash = __VECS(sha384_tv_template)
da7f033d
HX
5615 }
5616 }, {
5617 .alg = "sha512",
5618 .test = alg_test_hash,
a1915d51 5619 .fips_allowed = 1,
da7f033d 5620 .suite = {
21c8e720 5621 .hash = __VECS(sha512_tv_template)
da7f033d 5622 }
8b805b97
TZ
5623 }, {
5624 .alg = "sm2",
5625 .test = alg_test_akcipher,
5626 .suite = {
5627 .akcipher = __VECS(sm2_tv_template)
5628 }
b7e27530
GBY
5629 }, {
5630 .alg = "sm3",
5631 .test = alg_test_hash,
5632 .suite = {
5633 .hash = __VECS(sm3_tv_template)
5634 }
25a0b9d4
VC
5635 }, {
5636 .alg = "streebog256",
5637 .test = alg_test_hash,
5638 .suite = {
5639 .hash = __VECS(streebog256_tv_template)
5640 }
5641 }, {
5642 .alg = "streebog512",
5643 .test = alg_test_hash,
5644 .suite = {
5645 .hash = __VECS(streebog512_tv_template)
5646 }
ed331ada
EB
5647 }, {
5648 .alg = "vmac64(aes)",
5649 .test = alg_test_hash,
5650 .suite = {
5651 .hash = __VECS(vmac64_aes_tv_template)
5652 }
da7f033d
HX
5653 }, {
5654 .alg = "wp256",
5655 .test = alg_test_hash,
5656 .suite = {
21c8e720 5657 .hash = __VECS(wp256_tv_template)
da7f033d
HX
5658 }
5659 }, {
5660 .alg = "wp384",
5661 .test = alg_test_hash,
5662 .suite = {
21c8e720 5663 .hash = __VECS(wp384_tv_template)
da7f033d
HX
5664 }
5665 }, {
5666 .alg = "wp512",
5667 .test = alg_test_hash,
5668 .suite = {
21c8e720 5669 .hash = __VECS(wp512_tv_template)
da7f033d
HX
5670 }
5671 }, {
5672 .alg = "xcbc(aes)",
5673 .test = alg_test_hash,
5674 .suite = {
21c8e720 5675 .hash = __VECS(aes_xcbc128_tv_template)
da7f033d 5676 }
c24ee936
TZ
5677 }, {
5678 .alg = "xcbc(sm4)",
5679 .test = alg_test_hash,
5680 .suite = {
5681 .hash = __VECS(sm4_xcbc128_tv_template)
5682 }
aa762409
EB
5683 }, {
5684 .alg = "xchacha12",
5685 .test = alg_test_skcipher,
5686 .suite = {
5687 .cipher = __VECS(xchacha12_tv_template)
5688 },
de61d7ae
EB
5689 }, {
5690 .alg = "xchacha20",
5691 .test = alg_test_skcipher,
5692 .suite = {
5693 .cipher = __VECS(xchacha20_tv_template)
5694 },
17fee07a
NH
5695 }, {
5696 .alg = "xctr(aes)",
5697 .test = alg_test_skcipher,
5698 .suite = {
5699 .cipher = __VECS(aes_xctr_tv_template)
5700 }
da7f033d
HX
5701 }, {
5702 .alg = "xts(aes)",
d435e10e 5703 .generic_driver = "xts(ecb(aes-generic))",
1aa4ecd9 5704 .test = alg_test_skcipher,
2918aa8d 5705 .fips_allowed = 1,
da7f033d 5706 .suite = {
92a4c9fe 5707 .cipher = __VECS(aes_xts_tv_template)
da7f033d 5708 }
0840605e
JK
5709 }, {
5710 .alg = "xts(camellia)",
d435e10e 5711 .generic_driver = "xts(ecb(camellia-generic))",
0840605e
JK
5712 .test = alg_test_skcipher,
5713 .suite = {
92a4c9fe 5714 .cipher = __VECS(camellia_xts_tv_template)
0840605e 5715 }
9b8b0405
JG
5716 }, {
5717 .alg = "xts(cast6)",
d435e10e 5718 .generic_driver = "xts(ecb(cast6-generic))",
9b8b0405
JG
5719 .test = alg_test_skcipher,
5720 .suite = {
92a4c9fe 5721 .cipher = __VECS(cast6_xts_tv_template)
9b8b0405 5722 }
15f47ce5
GBY
5723 }, {
5724 /* Same as xts(aes) except the key is stored in
5725 * hardware secure memory which we reference by index
5726 */
5727 .alg = "xts(paes)",
5728 .test = alg_test_null,
5729 .fips_allowed = 1,
18be20b9
JK
5730 }, {
5731 .alg = "xts(serpent)",
d435e10e 5732 .generic_driver = "xts(ecb(serpent-generic))",
18be20b9
JK
5733 .test = alg_test_skcipher,
5734 .suite = {
92a4c9fe 5735 .cipher = __VECS(serpent_xts_tv_template)
18be20b9 5736 }
c24ee936
TZ
5737 }, {
5738 .alg = "xts(sm4)",
5739 .generic_driver = "xts(ecb(sm4-generic))",
5740 .test = alg_test_skcipher,
5741 .suite = {
5742 .cipher = __VECS(sm4_xts_tv_template)
5743 }
aed265b9
JK
5744 }, {
5745 .alg = "xts(twofish)",
d435e10e 5746 .generic_driver = "xts(ecb(twofish-generic))",
aed265b9
JK
5747 .test = alg_test_skcipher,
5748 .suite = {
92a4c9fe 5749 .cipher = __VECS(tf_xts_tv_template)
aed265b9 5750 }
15f47ce5 5751 }, {
c7ff8573
HF
5752#if IS_ENABLED(CONFIG_CRYPTO_PAES_S390)
5753 .alg = "xts-paes-s390",
5754 .fips_allowed = 1,
5755 .test = alg_test_skcipher,
5756 .suite = {
5757 .cipher = __VECS(aes_xts_tv_template)
5758 }
5759 }, {
5760#endif
15f47ce5
GBY
5761 .alg = "xts4096(paes)",
5762 .test = alg_test_null,
5763 .fips_allowed = 1,
5764 }, {
5765 .alg = "xts512(paes)",
5766 .test = alg_test_null,
5767 .fips_allowed = 1,
67882e76
NB
5768 }, {
5769 .alg = "xxhash64",
5770 .test = alg_test_hash,
5771 .fips_allowed = 1,
5772 .suite = {
5773 .hash = __VECS(xxhash64_tv_template)
5774 }
a368f43d
GC
5775 }, {
5776 .alg = "zlib-deflate",
5777 .test = alg_test_comp,
5778 .fips_allowed = 1,
5779 .suite = {
5780 .comp = {
5781 .comp = __VECS(zlib_deflate_comp_tv_template),
5782 .decomp = __VECS(zlib_deflate_decomp_tv_template)
5783 }
5784 }
d28fc3db
NT
5785 }, {
5786 .alg = "zstd",
5787 .test = alg_test_comp,
5788 .fips_allowed = 1,
5789 .suite = {
5790 .comp = {
5791 .comp = __VECS(zstd_comp_tv_template),
5792 .decomp = __VECS(zstd_decomp_tv_template)
5793 }
5794 }
da7f033d
HX
5795 }
5796};
5797
3f47a03d 5798static void alg_check_test_descs_order(void)
5714758b
JK
5799{
5800 int i;
5801
5714758b
JK
5802 for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
5803 int diff = strcmp(alg_test_descs[i - 1].alg,
5804 alg_test_descs[i].alg);
5805
5806 if (WARN_ON(diff > 0)) {
5807 pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
5808 alg_test_descs[i - 1].alg,
5809 alg_test_descs[i].alg);
5810 }
5811
5812 if (WARN_ON(diff == 0)) {
5813 pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
5814 alg_test_descs[i].alg);
5815 }
5816 }
5817}
5818
3f47a03d
EB
5819static void alg_check_testvec_configs(void)
5820{
4e7babba
EB
5821 int i;
5822
5823 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++)
5824 WARN_ON(!valid_testvec_config(
5825 &default_cipher_testvec_configs[i]));
4cc2dcf9
EB
5826
5827 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++)
5828 WARN_ON(!valid_testvec_config(
5829 &default_hash_testvec_configs[i]));
3f47a03d
EB
5830}
5831
5832static void testmgr_onetime_init(void)
5833{
5834 alg_check_test_descs_order();
5835 alg_check_testvec_configs();
5b2706a4
EB
5836
5837#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
5838 pr_warn("alg: extra crypto tests enabled. This is intended for developer use only.\n");
5839#endif
3f47a03d
EB
5840}
5841
1aa4ecd9 5842static int alg_find_test(const char *alg)
da7f033d
HX
5843{
5844 int start = 0;
5845 int end = ARRAY_SIZE(alg_test_descs);
5846
5847 while (start < end) {
5848 int i = (start + end) / 2;
5849 int diff = strcmp(alg_test_descs[i].alg, alg);
5850
5851 if (diff > 0) {
5852 end = i;
5853 continue;
5854 }
5855
5856 if (diff < 0) {
5857 start = i + 1;
5858 continue;
5859 }
5860
1aa4ecd9
HX
5861 return i;
5862 }
5863
5864 return -1;
5865}
5866
d6097b8d
NS
5867static int alg_fips_disabled(const char *driver, const char *alg)
5868{
5869 pr_info("alg: %s (%s) is disabled due to FIPS\n", alg, driver);
5870
5871 return -ECANCELED;
5872}
5873
1aa4ecd9
HX
5874int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
5875{
5876 int i;
a68f6610 5877 int j;
d12d6b6d 5878 int rc;
1aa4ecd9 5879
9e5c9fe4
RJ
5880 if (!fips_enabled && notests) {
5881 printk_once(KERN_INFO "alg: self-tests disabled\n");
5882 return 0;
5883 }
5884
3f47a03d 5885 DO_ONCE(testmgr_onetime_init);
5714758b 5886
1aa4ecd9
HX
5887 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
5888 char nalg[CRYPTO_MAX_ALG_NAME];
5889
5890 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
5891 sizeof(nalg))
5892 return -ENAMETOOLONG;
5893
5894 i = alg_find_test(nalg);
5895 if (i < 0)
5896 goto notest;
5897
a3bef3a3
JW
5898 if (fips_enabled && !alg_test_descs[i].fips_allowed)
5899 goto non_fips_alg;
5900
941fb328
JW
5901 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
5902 goto test_done;
da7f033d
HX
5903 }
5904
1aa4ecd9 5905 i = alg_find_test(alg);
a68f6610
HX
5906 j = alg_find_test(driver);
5907 if (i < 0 && j < 0)
1aa4ecd9
HX
5908 goto notest;
5909
d6097b8d
NS
5910 if (fips_enabled) {
5911 if (j >= 0 && !alg_test_descs[j].fips_allowed)
5912 return -EINVAL;
5913
5914 if (i >= 0 && !alg_test_descs[i].fips_allowed)
5915 goto non_fips_alg;
5916 }
a3bef3a3 5917
a68f6610
HX
5918 rc = 0;
5919 if (i >= 0)
5920 rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
5921 type, mask);
032c8cac 5922 if (j >= 0 && j != i)
a68f6610
HX
5923 rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
5924 type, mask);
5925
941fb328 5926test_done:
09a5ef96
EB
5927 if (rc) {
5928 if (fips_enabled || panic_on_fail) {
5929 fips_fail_notify();
5930 panic("alg: self-tests for %s (%s) failed in %s mode!\n",
5931 driver, alg,
5932 fips_enabled ? "fips" : "panic_on_fail");
5933 }
a76bd86a
RE
5934 pr_warn("alg: self-tests for %s using %s failed (rc=%d)",
5935 alg, driver, rc);
5936 WARN(rc != -ENOENT,
5937 "alg: self-tests for %s using %s failed (rc=%d)",
5938 alg, driver, rc);
09a5ef96
EB
5939 } else {
5940 if (fips_enabled)
5941 pr_info("alg: self-tests for %s (%s) passed\n",
5942 driver, alg);
9552389c 5943 }
d12d6b6d
NH
5944
5945 return rc;
1aa4ecd9
HX
5946
5947notest:
da7f033d 5948 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
d6097b8d
NS
5949
5950 if (type & CRYPTO_ALG_FIPS_INTERNAL)
5951 return alg_fips_disabled(driver, alg);
5952
da7f033d 5953 return 0;
a3bef3a3 5954non_fips_alg:
d6097b8d 5955 return alg_fips_disabled(driver, alg);
da7f033d 5956}
0b767f96 5957
326a6346 5958#endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
0b767f96 5959
da7f033d 5960EXPORT_SYMBOL_GPL(alg_test);