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