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