]> Git Repo - linux.git/blame - crypto/testmgr.c
crypto: testmgr - fuzz hashes against their generic implementation
[linux.git] / crypto / testmgr.c
CommitLineData
da7f033d
HX
1/*
2 * Algorithm testing framework and tests.
3 *
4 * Copyright (c) 2002 James Morris <[email protected]>
5 * Copyright (c) 2002 Jean-Francois Dive <[email protected]>
6 * Copyright (c) 2007 Nokia Siemens Networks
7 * Copyright (c) 2008 Herbert Xu <[email protected]>
3f47a03d 8 * Copyright (c) 2019 Google LLC
da7f033d 9 *
69435b94
AH
10 * Updated RFC4106 AES-GCM testing.
11 * Authors: Aidan O'Mahony ([email protected])
12 * Adrian Hoban <[email protected]>
13 * Gabriele Paoloni <[email protected]>
14 * Tadeusz Struk ([email protected])
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;
1331 desc->flags = 0;
1332 vec->digest_error = crypto_shash_digest(desc, vec->plaintext,
1333 vec->psize, (u8 *)vec->digest);
1334done:
1335 snprintf(name, max_namelen, "\"random: psize=%u ksize=%u\"",
1336 vec->psize, vec->ksize);
1337}
1338
1339/*
1340 * Test the hash algorithm represented by @req against the corresponding generic
1341 * implementation, if one is available.
1342 */
1343static int test_hash_vs_generic_impl(const char *driver,
1344 const char *generic_driver,
1345 unsigned int maxkeysize,
1346 struct ahash_request *req,
1347 struct test_sglist *tsgl,
1348 u8 *hashstate)
1349{
1350 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1351 const unsigned int digestsize = crypto_ahash_digestsize(tfm);
1352 const unsigned int blocksize = crypto_ahash_blocksize(tfm);
1353 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
1354 const char *algname = crypto_hash_alg_common(tfm)->base.cra_name;
1355 char _generic_driver[CRYPTO_MAX_ALG_NAME];
1356 struct crypto_shash *generic_tfm = NULL;
1357 unsigned int i;
1358 struct hash_testvec vec = { 0 };
1359 char vec_name[64];
1360 struct testvec_config cfg;
1361 char cfgname[TESTVEC_CONFIG_NAMELEN];
1362 int err;
1363
1364 if (noextratests)
1365 return 0;
1366
1367 if (!generic_driver) { /* Use default naming convention? */
1368 err = build_generic_driver_name(algname, _generic_driver);
1369 if (err)
1370 return err;
1371 generic_driver = _generic_driver;
1372 }
1373
1374 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
1375 return 0;
1376
1377 generic_tfm = crypto_alloc_shash(generic_driver, 0, 0);
1378 if (IS_ERR(generic_tfm)) {
1379 err = PTR_ERR(generic_tfm);
1380 if (err == -ENOENT) {
1381 pr_warn("alg: hash: skipping comparison tests for %s because %s is unavailable\n",
1382 driver, generic_driver);
1383 return 0;
1384 }
1385 pr_err("alg: hash: error allocating %s (generic impl of %s): %d\n",
1386 generic_driver, algname, err);
1387 return err;
1388 }
1389
1390 /* Check the algorithm properties for consistency. */
1391
1392 if (digestsize != crypto_shash_digestsize(generic_tfm)) {
1393 pr_err("alg: hash: digestsize for %s (%u) doesn't match generic impl (%u)\n",
1394 driver, digestsize,
1395 crypto_shash_digestsize(generic_tfm));
1396 err = -EINVAL;
1397 goto out;
1398 }
1399
1400 if (blocksize != crypto_shash_blocksize(generic_tfm)) {
1401 pr_err("alg: hash: blocksize for %s (%u) doesn't match generic impl (%u)\n",
1402 driver, blocksize, crypto_shash_blocksize(generic_tfm));
1403 err = -EINVAL;
1404 goto out;
1405 }
1406
1407 /*
1408 * Now generate test vectors using the generic implementation, and test
1409 * the other implementation against them.
1410 */
1411
1412 vec.key = kmalloc(maxkeysize, GFP_KERNEL);
1413 vec.plaintext = kmalloc(maxdatasize, GFP_KERNEL);
1414 vec.digest = kmalloc(digestsize, GFP_KERNEL);
1415 if (!vec.key || !vec.plaintext || !vec.digest) {
1416 err = -ENOMEM;
1417 goto out;
1418 }
1419
1420 for (i = 0; i < fuzz_iterations * 8; i++) {
1421 generate_random_hash_testvec(generic_tfm, &vec,
1422 maxkeysize, maxdatasize,
1423 vec_name, sizeof(vec_name));
1424 generate_random_testvec_config(&cfg, cfgname, sizeof(cfgname));
1425
1426 err = test_hash_vec_cfg(driver, &vec, vec_name, &cfg,
1427 req, tsgl, hashstate);
1428 if (err)
1429 goto out;
1430 cond_resched();
1431 }
1432 err = 0;
1433out:
1434 kfree(vec.key);
1435 kfree(vec.plaintext);
1436 kfree(vec.digest);
1437 crypto_free_shash(generic_tfm);
1438 return err;
1439}
1440#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1441static int test_hash_vs_generic_impl(const char *driver,
1442 const char *generic_driver,
1443 unsigned int maxkeysize,
1444 struct ahash_request *req,
1445 struct test_sglist *tsgl,
1446 u8 *hashstate)
1447{
1448 return 0;
1449}
1450#endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1451
4cc2dcf9
EB
1452static int __alg_test_hash(const struct hash_testvec *vecs,
1453 unsigned int num_vecs, const char *driver,
9a8a6b3f
EB
1454 u32 type, u32 mask,
1455 const char *generic_driver, unsigned int maxkeysize)
4cc2dcf9
EB
1456{
1457 struct crypto_ahash *tfm;
1458 struct ahash_request *req = NULL;
1459 struct test_sglist *tsgl = NULL;
1460 u8 *hashstate = NULL;
1461 unsigned int i;
1462 int err;
018ba95c 1463
4cc2dcf9
EB
1464 tfm = crypto_alloc_ahash(driver, type, mask);
1465 if (IS_ERR(tfm)) {
1466 pr_err("alg: hash: failed to allocate transform for %s: %ld\n",
1467 driver, PTR_ERR(tfm));
1468 return PTR_ERR(tfm);
1469 }
018ba95c 1470
4cc2dcf9
EB
1471 req = ahash_request_alloc(tfm, GFP_KERNEL);
1472 if (!req) {
1473 pr_err("alg: hash: failed to allocate request for %s\n",
1474 driver);
1475 err = -ENOMEM;
1476 goto out;
1477 }
018ba95c 1478
4cc2dcf9
EB
1479 tsgl = kmalloc(sizeof(*tsgl), GFP_KERNEL);
1480 if (!tsgl || init_test_sglist(tsgl) != 0) {
1481 pr_err("alg: hash: failed to allocate test buffers for %s\n",
1482 driver);
1483 kfree(tsgl);
1484 tsgl = NULL;
1485 err = -ENOMEM;
1486 goto out;
1487 }
018ba95c 1488
4cc2dcf9
EB
1489 hashstate = kmalloc(crypto_ahash_statesize(tfm) + TESTMGR_POISON_LEN,
1490 GFP_KERNEL);
1491 if (!hashstate) {
1492 pr_err("alg: hash: failed to allocate hash state buffer for %s\n",
1493 driver);
1494 err = -ENOMEM;
1495 goto out;
1496 }
018ba95c 1497
4cc2dcf9
EB
1498 for (i = 0; i < num_vecs; i++) {
1499 err = test_hash_vec(driver, &vecs[i], i, req, tsgl, hashstate);
1500 if (err)
5f2b424e 1501 goto out;
da7f033d 1502 }
9a8a6b3f
EB
1503 err = test_hash_vs_generic_impl(driver, generic_driver, maxkeysize, req,
1504 tsgl, hashstate);
da7f033d 1505out:
4cc2dcf9
EB
1506 kfree(hashstate);
1507 if (tsgl) {
1508 destroy_test_sglist(tsgl);
1509 kfree(tsgl);
1510 }
da7f033d 1511 ahash_request_free(req);
4cc2dcf9
EB
1512 crypto_free_ahash(tfm);
1513 return err;
da7f033d
HX
1514}
1515
4cc2dcf9
EB
1516static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1517 u32 type, u32 mask)
da5ffe11 1518{
4cc2dcf9
EB
1519 const struct hash_testvec *template = desc->suite.hash.vecs;
1520 unsigned int tcount = desc->suite.hash.count;
1521 unsigned int nr_unkeyed, nr_keyed;
9a8a6b3f 1522 unsigned int maxkeysize = 0;
4cc2dcf9 1523 int err;
da5ffe11 1524
4cc2dcf9
EB
1525 /*
1526 * For OPTIONAL_KEY algorithms, we have to do all the unkeyed tests
1527 * first, before setting a key on the tfm. To make this easier, we
1528 * require that the unkeyed test vectors (if any) are listed first.
1529 */
da5ffe11 1530
4cc2dcf9
EB
1531 for (nr_unkeyed = 0; nr_unkeyed < tcount; nr_unkeyed++) {
1532 if (template[nr_unkeyed].ksize)
1533 break;
1534 }
1535 for (nr_keyed = 0; nr_unkeyed + nr_keyed < tcount; nr_keyed++) {
1536 if (!template[nr_unkeyed + nr_keyed].ksize) {
1537 pr_err("alg: hash: test vectors for %s out of order, "
1538 "unkeyed ones must come first\n", desc->alg);
1539 return -EINVAL;
1540 }
9a8a6b3f
EB
1541 maxkeysize = max_t(unsigned int, maxkeysize,
1542 template[nr_unkeyed + nr_keyed].ksize);
4cc2dcf9 1543 }
da5ffe11 1544
4cc2dcf9
EB
1545 err = 0;
1546 if (nr_unkeyed) {
9a8a6b3f
EB
1547 err = __alg_test_hash(template, nr_unkeyed, driver, type, mask,
1548 desc->generic_driver, maxkeysize);
4cc2dcf9 1549 template += nr_unkeyed;
da5ffe11
JK
1550 }
1551
4cc2dcf9 1552 if (!err && nr_keyed)
9a8a6b3f
EB
1553 err = __alg_test_hash(template, nr_keyed, driver, type, mask,
1554 desc->generic_driver, maxkeysize);
4cc2dcf9
EB
1555
1556 return err;
da5ffe11
JK
1557}
1558
ed96804f
EB
1559static int test_aead_vec_cfg(const char *driver, int enc,
1560 const struct aead_testvec *vec,
951d1332 1561 const char *vec_name,
ed96804f
EB
1562 const struct testvec_config *cfg,
1563 struct aead_request *req,
1564 struct cipher_test_sglists *tsgls)
da7f033d 1565{
ed96804f
EB
1566 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1567 const unsigned int alignmask = crypto_aead_alignmask(tfm);
1568 const unsigned int ivsize = crypto_aead_ivsize(tfm);
1569 const unsigned int authsize = vec->clen - vec->plen;
1570 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1571 const char *op = enc ? "encryption" : "decryption";
1572 DECLARE_CRYPTO_WAIT(wait);
1573 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
1574 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
1575 cfg->iv_offset +
1576 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
1577 struct kvec input[2];
5283a8ee 1578 int expected_error;
ed96804f 1579 int err;
d8a32ac2 1580
ed96804f
EB
1581 /* Set the key */
1582 if (vec->wk)
1583 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
da7f033d 1584 else
ed96804f
EB
1585 crypto_aead_clear_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1586 err = crypto_aead_setkey(tfm, vec->key, vec->klen);
5283a8ee 1587 if (err && err != vec->setkey_error) {
951d1332
EB
1588 pr_err("alg: aead: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1589 driver, vec_name, vec->setkey_error, err,
5283a8ee 1590 crypto_aead_get_flags(tfm));
ed96804f 1591 return err;
da7f033d 1592 }
5283a8ee 1593 if (!err && vec->setkey_error) {
951d1332
EB
1594 pr_err("alg: aead: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1595 driver, vec_name, vec->setkey_error);
ed96804f 1596 return -EINVAL;
da7f033d
HX
1597 }
1598
ed96804f
EB
1599 /* Set the authentication tag size */
1600 err = crypto_aead_setauthsize(tfm, authsize);
5283a8ee 1601 if (err && err != vec->setauthsize_error) {
951d1332
EB
1602 pr_err("alg: aead: %s setauthsize failed on test vector %s; expected_error=%d, actual_error=%d\n",
1603 driver, vec_name, vec->setauthsize_error, err);
ed96804f
EB
1604 return err;
1605 }
5283a8ee 1606 if (!err && vec->setauthsize_error) {
951d1332
EB
1607 pr_err("alg: aead: %s setauthsize unexpectedly succeeded on test vector %s; expected_error=%d\n",
1608 driver, vec_name, vec->setauthsize_error);
5283a8ee
EB
1609 return -EINVAL;
1610 }
1611
1612 if (vec->setkey_error || vec->setauthsize_error)
1613 return 0;
8ec25c51 1614
ed96804f
EB
1615 /* The IV must be copied to a buffer, as the algorithm may modify it */
1616 if (WARN_ON(ivsize > MAX_IVLEN))
1617 return -EINVAL;
1618 if (vec->iv)
1619 memcpy(iv, vec->iv, ivsize);
1620 else
1621 memset(iv, 0, ivsize);
da7f033d 1622
ed96804f
EB
1623 /* Build the src/dst scatterlists */
1624 input[0].iov_base = (void *)vec->assoc;
1625 input[0].iov_len = vec->alen;
1626 input[1].iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
1627 input[1].iov_len = enc ? vec->plen : vec->clen;
1628 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
1629 vec->alen + (enc ? vec->plen :
1630 vec->clen),
1631 vec->alen + (enc ? vec->clen :
1632 vec->plen),
1633 input, 2);
1634 if (err) {
951d1332
EB
1635 pr_err("alg: aead: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
1636 driver, op, vec_name, cfg->name);
ed96804f
EB
1637 return err;
1638 }
da7f033d 1639
ed96804f
EB
1640 /* Do the actual encryption or decryption */
1641 testmgr_poison(req->__ctx, crypto_aead_reqsize(tfm));
1642 aead_request_set_callback(req, req_flags, crypto_req_done, &wait);
1643 aead_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
1644 enc ? vec->plen : vec->clen, iv);
1645 aead_request_set_ad(req, vec->alen);
6570737c
EB
1646 if (cfg->nosimd)
1647 crypto_disable_simd_for_test();
1648 err = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
1649 if (cfg->nosimd)
1650 crypto_reenable_simd_for_test();
1651 err = crypto_wait_req(err, &wait);
a6e5ef9b
EB
1652
1653 /* Check that the algorithm didn't overwrite things it shouldn't have */
1654 if (req->cryptlen != (enc ? vec->plen : vec->clen) ||
1655 req->assoclen != vec->alen ||
1656 req->iv != iv ||
1657 req->src != tsgls->src.sgl_ptr ||
1658 req->dst != tsgls->dst.sgl_ptr ||
1659 crypto_aead_reqtfm(req) != tfm ||
1660 req->base.complete != crypto_req_done ||
1661 req->base.flags != req_flags ||
1662 req->base.data != &wait) {
951d1332
EB
1663 pr_err("alg: aead: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
1664 driver, op, vec_name, cfg->name);
a6e5ef9b
EB
1665 if (req->cryptlen != (enc ? vec->plen : vec->clen))
1666 pr_err("alg: aead: changed 'req->cryptlen'\n");
1667 if (req->assoclen != vec->alen)
1668 pr_err("alg: aead: changed 'req->assoclen'\n");
1669 if (req->iv != iv)
1670 pr_err("alg: aead: changed 'req->iv'\n");
1671 if (req->src != tsgls->src.sgl_ptr)
1672 pr_err("alg: aead: changed 'req->src'\n");
1673 if (req->dst != tsgls->dst.sgl_ptr)
1674 pr_err("alg: aead: changed 'req->dst'\n");
1675 if (crypto_aead_reqtfm(req) != tfm)
1676 pr_err("alg: aead: changed 'req->base.tfm'\n");
1677 if (req->base.complete != crypto_req_done)
1678 pr_err("alg: aead: changed 'req->base.complete'\n");
1679 if (req->base.flags != req_flags)
1680 pr_err("alg: aead: changed 'req->base.flags'\n");
1681 if (req->base.data != &wait)
1682 pr_err("alg: aead: changed 'req->base.data'\n");
1683 return -EINVAL;
1684 }
1685 if (is_test_sglist_corrupted(&tsgls->src)) {
951d1332
EB
1686 pr_err("alg: aead: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
1687 driver, op, vec_name, cfg->name);
a6e5ef9b
EB
1688 return -EINVAL;
1689 }
1690 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
1691 is_test_sglist_corrupted(&tsgls->dst)) {
951d1332
EB
1692 pr_err("alg: aead: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
1693 driver, op, vec_name, cfg->name);
a6e5ef9b 1694 return -EINVAL;
ed96804f 1695 }
da7f033d 1696
5283a8ee
EB
1697 /* Check for success or failure */
1698 expected_error = vec->novrfy ? -EBADMSG : vec->crypt_error;
1699 if (err) {
1700 if (err == expected_error)
1701 return 0;
951d1332
EB
1702 pr_err("alg: aead: %s %s failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
1703 driver, op, vec_name, expected_error, err, cfg->name);
5283a8ee
EB
1704 return err;
1705 }
1706 if (expected_error) {
951d1332
EB
1707 pr_err("alg: aead: %s %s unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
1708 driver, op, vec_name, expected_error, cfg->name);
5283a8ee
EB
1709 return -EINVAL;
1710 }
1711
ed96804f
EB
1712 /* Check for the correct output (ciphertext or plaintext) */
1713 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
1714 enc ? vec->clen : vec->plen,
1715 vec->alen, enc || !cfg->inplace);
1716 if (err == -EOVERFLOW) {
951d1332
EB
1717 pr_err("alg: aead: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
1718 driver, op, vec_name, cfg->name);
ed96804f
EB
1719 return err;
1720 }
1721 if (err) {
951d1332
EB
1722 pr_err("alg: aead: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
1723 driver, op, vec_name, cfg->name);
ed96804f
EB
1724 return err;
1725 }
da7f033d 1726
ed96804f
EB
1727 return 0;
1728}
da7f033d 1729
ed96804f
EB
1730static int test_aead_vec(const char *driver, int enc,
1731 const struct aead_testvec *vec, unsigned int vec_num,
1732 struct aead_request *req,
1733 struct cipher_test_sglists *tsgls)
1734{
951d1332 1735 char vec_name[16];
ed96804f
EB
1736 unsigned int i;
1737 int err;
da7f033d 1738
ed96804f
EB
1739 if (enc && vec->novrfy)
1740 return 0;
da7f033d 1741
951d1332
EB
1742 sprintf(vec_name, "%u", vec_num);
1743
ed96804f 1744 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
951d1332 1745 err = test_aead_vec_cfg(driver, enc, vec, vec_name,
ed96804f
EB
1746 &default_cipher_testvec_configs[i],
1747 req, tsgls);
1748 if (err)
1749 return err;
1750 }
da7f033d 1751
ed96804f
EB
1752#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1753 if (!noextratests) {
1754 struct testvec_config cfg;
1755 char cfgname[TESTVEC_CONFIG_NAMELEN];
05b1d338 1756
ed96804f
EB
1757 for (i = 0; i < fuzz_iterations; i++) {
1758 generate_random_testvec_config(&cfg, cfgname,
1759 sizeof(cfgname));
951d1332 1760 err = test_aead_vec_cfg(driver, enc, vec, vec_name,
ed96804f
EB
1761 &cfg, req, tsgls);
1762 if (err)
1763 return err;
da7f033d
HX
1764 }
1765 }
ed96804f
EB
1766#endif
1767 return 0;
1768}
da7f033d 1769
ed96804f
EB
1770static int test_aead(const char *driver, int enc,
1771 const struct aead_test_suite *suite,
1772 struct aead_request *req,
1773 struct cipher_test_sglists *tsgls)
1774{
1775 unsigned int i;
1776 int err;
da7f033d 1777
ed96804f
EB
1778 for (i = 0; i < suite->count; i++) {
1779 err = test_aead_vec(driver, enc, &suite->vecs[i], i, req,
1780 tsgls);
1781 if (err)
1782 return err;
1783 }
1784 return 0;
da7f033d
HX
1785}
1786
ed96804f
EB
1787static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
1788 u32 type, u32 mask)
d8a32ac2 1789{
ed96804f
EB
1790 const struct aead_test_suite *suite = &desc->suite.aead;
1791 struct crypto_aead *tfm;
1792 struct aead_request *req = NULL;
1793 struct cipher_test_sglists *tsgls = NULL;
1794 int err;
d8a32ac2 1795
ed96804f
EB
1796 if (suite->count <= 0) {
1797 pr_err("alg: aead: empty test suite for %s\n", driver);
1798 return -EINVAL;
1799 }
d8a32ac2 1800
ed96804f
EB
1801 tfm = crypto_alloc_aead(driver, type, mask);
1802 if (IS_ERR(tfm)) {
1803 pr_err("alg: aead: failed to allocate transform for %s: %ld\n",
1804 driver, PTR_ERR(tfm));
1805 return PTR_ERR(tfm);
1806 }
58dcf548 1807
ed96804f
EB
1808 req = aead_request_alloc(tfm, GFP_KERNEL);
1809 if (!req) {
1810 pr_err("alg: aead: failed to allocate request for %s\n",
1811 driver);
1812 err = -ENOMEM;
1813 goto out;
1814 }
58dcf548 1815
ed96804f
EB
1816 tsgls = alloc_cipher_test_sglists();
1817 if (!tsgls) {
1818 pr_err("alg: aead: failed to allocate test buffers for %s\n",
1819 driver);
1820 err = -ENOMEM;
1821 goto out;
58dcf548
JK
1822 }
1823
ed96804f
EB
1824 err = test_aead(driver, ENCRYPT, suite, req, tsgls);
1825 if (err)
1826 goto out;
1827
1828 err = test_aead(driver, DECRYPT, suite, req, tsgls);
1829out:
1830 free_cipher_test_sglists(tsgls);
1831 aead_request_free(req);
1832 crypto_free_aead(tfm);
1833 return err;
d8a32ac2
JK
1834}
1835
1aa4ecd9 1836static int test_cipher(struct crypto_cipher *tfm, int enc,
b13b1e0c
EB
1837 const struct cipher_testvec *template,
1838 unsigned int tcount)
1aa4ecd9
HX
1839{
1840 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
1841 unsigned int i, j, k;
1aa4ecd9
HX
1842 char *q;
1843 const char *e;
92a4c9fe 1844 const char *input, *result;
1aa4ecd9 1845 void *data;
f8b0d4d0
HX
1846 char *xbuf[XBUFSIZE];
1847 int ret = -ENOMEM;
1848
1849 if (testmgr_alloc_buf(xbuf))
1850 goto out_nobuf;
1aa4ecd9
HX
1851
1852 if (enc == ENCRYPT)
1853 e = "encryption";
1854 else
1855 e = "decryption";
1856
1857 j = 0;
1858 for (i = 0; i < tcount; i++) {
1aa4ecd9 1859
10faa8c0
SM
1860 if (fips_enabled && template[i].fips_skip)
1861 continue;
1862
92a4c9fe
EB
1863 input = enc ? template[i].ptext : template[i].ctext;
1864 result = enc ? template[i].ctext : template[i].ptext;
1aa4ecd9
HX
1865 j++;
1866
fd57f22a 1867 ret = -EINVAL;
92a4c9fe 1868 if (WARN_ON(template[i].len > PAGE_SIZE))
fd57f22a
HX
1869 goto out;
1870
1aa4ecd9 1871 data = xbuf[0];
92a4c9fe 1872 memcpy(data, input, template[i].len);
1aa4ecd9
HX
1873
1874 crypto_cipher_clear_flags(tfm, ~0);
1875 if (template[i].wk)
231baecd 1876 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1aa4ecd9
HX
1877
1878 ret = crypto_cipher_setkey(tfm, template[i].key,
1879 template[i].klen);
5283a8ee
EB
1880 if (ret) {
1881 if (ret == template[i].setkey_error)
1882 continue;
1883 pr_err("alg: cipher: %s setkey failed on test vector %u; expected_error=%d, actual_error=%d, flags=%#x\n",
1884 algo, j, template[i].setkey_error, ret,
1885 crypto_cipher_get_flags(tfm));
1aa4ecd9 1886 goto out;
5283a8ee
EB
1887 }
1888 if (template[i].setkey_error) {
1889 pr_err("alg: cipher: %s setkey unexpectedly succeeded on test vector %u; expected_error=%d\n",
1890 algo, j, template[i].setkey_error);
1891 ret = -EINVAL;
1892 goto out;
1893 }
1aa4ecd9 1894
92a4c9fe 1895 for (k = 0; k < template[i].len;
1aa4ecd9
HX
1896 k += crypto_cipher_blocksize(tfm)) {
1897 if (enc)
1898 crypto_cipher_encrypt_one(tfm, data + k,
1899 data + k);
1900 else
1901 crypto_cipher_decrypt_one(tfm, data + k,
1902 data + k);
1903 }
1904
1905 q = data;
92a4c9fe 1906 if (memcmp(q, result, template[i].len)) {
1aa4ecd9
HX
1907 printk(KERN_ERR "alg: cipher: Test %d failed "
1908 "on %s for %s\n", j, e, algo);
92a4c9fe 1909 hexdump(q, template[i].len);
1aa4ecd9
HX
1910 ret = -EINVAL;
1911 goto out;
1912 }
1913 }
1914
1915 ret = 0;
1916
1917out:
f8b0d4d0
HX
1918 testmgr_free_buf(xbuf);
1919out_nobuf:
1aa4ecd9
HX
1920 return ret;
1921}
1922
4e7babba
EB
1923static int test_skcipher_vec_cfg(const char *driver, int enc,
1924 const struct cipher_testvec *vec,
951d1332 1925 const char *vec_name,
4e7babba
EB
1926 const struct testvec_config *cfg,
1927 struct skcipher_request *req,
1928 struct cipher_test_sglists *tsgls)
da7f033d 1929{
4e7babba
EB
1930 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1931 const unsigned int alignmask = crypto_skcipher_alignmask(tfm);
1932 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
1933 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1934 const char *op = enc ? "encryption" : "decryption";
1935 DECLARE_CRYPTO_WAIT(wait);
1936 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
1937 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
1938 cfg->iv_offset +
1939 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
1940 struct kvec input;
1941 int err;
08d6af8c 1942
4e7babba
EB
1943 /* Set the key */
1944 if (vec->wk)
1945 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
da7f033d 1946 else
4e7babba
EB
1947 crypto_skcipher_clear_flags(tfm,
1948 CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1949 err = crypto_skcipher_setkey(tfm, vec->key, vec->klen);
1950 if (err) {
5283a8ee 1951 if (err == vec->setkey_error)
4e7babba 1952 return 0;
951d1332
EB
1953 pr_err("alg: skcipher: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1954 driver, vec_name, vec->setkey_error, err,
5283a8ee 1955 crypto_skcipher_get_flags(tfm));
4e7babba
EB
1956 return err;
1957 }
5283a8ee 1958 if (vec->setkey_error) {
951d1332
EB
1959 pr_err("alg: skcipher: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1960 driver, vec_name, vec->setkey_error);
4e7babba 1961 return -EINVAL;
da7f033d
HX
1962 }
1963
4e7babba
EB
1964 /* The IV must be copied to a buffer, as the algorithm may modify it */
1965 if (ivsize) {
1966 if (WARN_ON(ivsize > MAX_IVLEN))
1967 return -EINVAL;
8efd972e
EB
1968 if (vec->generates_iv && !enc)
1969 memcpy(iv, vec->iv_out, ivsize);
1970 else if (vec->iv)
4e7babba 1971 memcpy(iv, vec->iv, ivsize);
da7f033d 1972 else
4e7babba
EB
1973 memset(iv, 0, ivsize);
1974 } else {
1975 if (vec->generates_iv) {
951d1332
EB
1976 pr_err("alg: skcipher: %s has ivsize=0 but test vector %s generates IV!\n",
1977 driver, vec_name);
4e7babba 1978 return -EINVAL;
8a826a34 1979 }
4e7babba 1980 iv = NULL;
da7f033d
HX
1981 }
1982
4e7babba
EB
1983 /* Build the src/dst scatterlists */
1984 input.iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
1985 input.iov_len = vec->len;
1986 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
1987 vec->len, vec->len, &input, 1);
1988 if (err) {
951d1332
EB
1989 pr_err("alg: skcipher: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
1990 driver, op, vec_name, cfg->name);
4e7babba
EB
1991 return err;
1992 }
da7f033d 1993
4e7babba
EB
1994 /* Do the actual encryption or decryption */
1995 testmgr_poison(req->__ctx, crypto_skcipher_reqsize(tfm));
1996 skcipher_request_set_callback(req, req_flags, crypto_req_done, &wait);
1997 skcipher_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
1998 vec->len, iv);
6570737c
EB
1999 if (cfg->nosimd)
2000 crypto_disable_simd_for_test();
2001 err = enc ? crypto_skcipher_encrypt(req) : crypto_skcipher_decrypt(req);
2002 if (cfg->nosimd)
2003 crypto_reenable_simd_for_test();
2004 err = crypto_wait_req(err, &wait);
da7f033d 2005
fa353c99
EB
2006 /* Check that the algorithm didn't overwrite things it shouldn't have */
2007 if (req->cryptlen != vec->len ||
2008 req->iv != iv ||
2009 req->src != tsgls->src.sgl_ptr ||
2010 req->dst != tsgls->dst.sgl_ptr ||
2011 crypto_skcipher_reqtfm(req) != tfm ||
2012 req->base.complete != crypto_req_done ||
2013 req->base.flags != req_flags ||
2014 req->base.data != &wait) {
951d1332
EB
2015 pr_err("alg: skcipher: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
2016 driver, op, vec_name, cfg->name);
fa353c99
EB
2017 if (req->cryptlen != vec->len)
2018 pr_err("alg: skcipher: changed 'req->cryptlen'\n");
2019 if (req->iv != iv)
2020 pr_err("alg: skcipher: changed 'req->iv'\n");
2021 if (req->src != tsgls->src.sgl_ptr)
2022 pr_err("alg: skcipher: changed 'req->src'\n");
2023 if (req->dst != tsgls->dst.sgl_ptr)
2024 pr_err("alg: skcipher: changed 'req->dst'\n");
2025 if (crypto_skcipher_reqtfm(req) != tfm)
2026 pr_err("alg: skcipher: changed 'req->base.tfm'\n");
2027 if (req->base.complete != crypto_req_done)
2028 pr_err("alg: skcipher: changed 'req->base.complete'\n");
2029 if (req->base.flags != req_flags)
2030 pr_err("alg: skcipher: changed 'req->base.flags'\n");
2031 if (req->base.data != &wait)
2032 pr_err("alg: skcipher: changed 'req->base.data'\n");
2033 return -EINVAL;
2034 }
2035 if (is_test_sglist_corrupted(&tsgls->src)) {
951d1332
EB
2036 pr_err("alg: skcipher: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
2037 driver, op, vec_name, cfg->name);
fa353c99
EB
2038 return -EINVAL;
2039 }
2040 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
2041 is_test_sglist_corrupted(&tsgls->dst)) {
951d1332
EB
2042 pr_err("alg: skcipher: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
2043 driver, op, vec_name, cfg->name);
fa353c99
EB
2044 return -EINVAL;
2045 }
2046
5283a8ee
EB
2047 /* Check for success or failure */
2048 if (err) {
2049 if (err == vec->crypt_error)
2050 return 0;
951d1332
EB
2051 pr_err("alg: skcipher: %s %s failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
2052 driver, op, vec_name, vec->crypt_error, err, cfg->name);
5283a8ee
EB
2053 return err;
2054 }
2055 if (vec->crypt_error) {
951d1332
EB
2056 pr_err("alg: skcipher: %s %s unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
2057 driver, op, vec_name, vec->crypt_error, cfg->name);
5283a8ee
EB
2058 return -EINVAL;
2059 }
2060
4e7babba
EB
2061 /* Check for the correct output (ciphertext or plaintext) */
2062 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
2063 vec->len, 0, true);
2064 if (err == -EOVERFLOW) {
951d1332
EB
2065 pr_err("alg: skcipher: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
2066 driver, op, vec_name, cfg->name);
4e7babba
EB
2067 return err;
2068 }
2069 if (err) {
951d1332
EB
2070 pr_err("alg: skcipher: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
2071 driver, op, vec_name, cfg->name);
4e7babba
EB
2072 return err;
2073 }
08d6af8c 2074
4e7babba 2075 /* If applicable, check that the algorithm generated the correct IV */
8efd972e 2076 if (vec->iv_out && memcmp(iv, vec->iv_out, ivsize) != 0) {
951d1332
EB
2077 pr_err("alg: skcipher: %s %s test failed (wrong output IV) on test vector %s, cfg=\"%s\"\n",
2078 driver, op, vec_name, cfg->name);
4e7babba
EB
2079 hexdump(iv, ivsize);
2080 return -EINVAL;
2081 }
08d6af8c 2082
4e7babba
EB
2083 return 0;
2084}
da7f033d 2085
4e7babba
EB
2086static int test_skcipher_vec(const char *driver, int enc,
2087 const struct cipher_testvec *vec,
2088 unsigned int vec_num,
2089 struct skcipher_request *req,
2090 struct cipher_test_sglists *tsgls)
2091{
951d1332 2092 char vec_name[16];
4e7babba
EB
2093 unsigned int i;
2094 int err;
da7f033d 2095
4e7babba
EB
2096 if (fips_enabled && vec->fips_skip)
2097 return 0;
da7f033d 2098
951d1332
EB
2099 sprintf(vec_name, "%u", vec_num);
2100
4e7babba 2101 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
951d1332 2102 err = test_skcipher_vec_cfg(driver, enc, vec, vec_name,
4e7babba
EB
2103 &default_cipher_testvec_configs[i],
2104 req, tsgls);
2105 if (err)
2106 return err;
2107 }
da7f033d 2108
4e7babba
EB
2109#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2110 if (!noextratests) {
2111 struct testvec_config cfg;
2112 char cfgname[TESTVEC_CONFIG_NAMELEN];
2113
2114 for (i = 0; i < fuzz_iterations; i++) {
2115 generate_random_testvec_config(&cfg, cfgname,
2116 sizeof(cfgname));
951d1332 2117 err = test_skcipher_vec_cfg(driver, enc, vec, vec_name,
4e7babba
EB
2118 &cfg, req, tsgls);
2119 if (err)
2120 return err;
da7f033d
HX
2121 }
2122 }
4e7babba
EB
2123#endif
2124 return 0;
2125}
da7f033d 2126
4e7babba
EB
2127static int test_skcipher(const char *driver, int enc,
2128 const struct cipher_test_suite *suite,
2129 struct skcipher_request *req,
2130 struct cipher_test_sglists *tsgls)
2131{
2132 unsigned int i;
2133 int err;
da7f033d 2134
4e7babba
EB
2135 for (i = 0; i < suite->count; i++) {
2136 err = test_skcipher_vec(driver, enc, &suite->vecs[i], i, req,
2137 tsgls);
2138 if (err)
2139 return err;
2140 }
2141 return 0;
da7f033d
HX
2142}
2143
4e7babba
EB
2144static int alg_test_skcipher(const struct alg_test_desc *desc,
2145 const char *driver, u32 type, u32 mask)
08d6af8c 2146{
4e7babba
EB
2147 const struct cipher_test_suite *suite = &desc->suite.cipher;
2148 struct crypto_skcipher *tfm;
2149 struct skcipher_request *req = NULL;
2150 struct cipher_test_sglists *tsgls = NULL;
2151 int err;
08d6af8c 2152
4e7babba
EB
2153 if (suite->count <= 0) {
2154 pr_err("alg: skcipher: empty test suite for %s\n", driver);
2155 return -EINVAL;
2156 }
08d6af8c 2157
4e7babba
EB
2158 tfm = crypto_alloc_skcipher(driver, type, mask);
2159 if (IS_ERR(tfm)) {
2160 pr_err("alg: skcipher: failed to allocate transform for %s: %ld\n",
2161 driver, PTR_ERR(tfm));
2162 return PTR_ERR(tfm);
2163 }
3a338f20 2164
4e7babba
EB
2165 req = skcipher_request_alloc(tfm, GFP_KERNEL);
2166 if (!req) {
2167 pr_err("alg: skcipher: failed to allocate request for %s\n",
2168 driver);
2169 err = -ENOMEM;
2170 goto out;
2171 }
3a338f20 2172
4e7babba
EB
2173 tsgls = alloc_cipher_test_sglists();
2174 if (!tsgls) {
2175 pr_err("alg: skcipher: failed to allocate test buffers for %s\n",
2176 driver);
2177 err = -ENOMEM;
2178 goto out;
3a338f20
JK
2179 }
2180
4e7babba
EB
2181 err = test_skcipher(driver, ENCRYPT, suite, req, tsgls);
2182 if (err)
2183 goto out;
2184
2185 err = test_skcipher(driver, DECRYPT, suite, req, tsgls);
2186out:
2187 free_cipher_test_sglists(tsgls);
2188 skcipher_request_free(req);
2189 crypto_free_skcipher(tfm);
2190 return err;
08d6af8c
JK
2191}
2192
b13b1e0c
EB
2193static int test_comp(struct crypto_comp *tfm,
2194 const struct comp_testvec *ctemplate,
2195 const struct comp_testvec *dtemplate,
2196 int ctcount, int dtcount)
da7f033d
HX
2197{
2198 const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
33607384 2199 char *output, *decomp_output;
da7f033d 2200 unsigned int i;
da7f033d
HX
2201 int ret;
2202
33607384
MC
2203 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
2204 if (!output)
2205 return -ENOMEM;
2206
2207 decomp_output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
2208 if (!decomp_output) {
2209 kfree(output);
2210 return -ENOMEM;
2211 }
2212
da7f033d 2213 for (i = 0; i < ctcount; i++) {
c79cf910
GU
2214 int ilen;
2215 unsigned int dlen = COMP_BUF_SIZE;
da7f033d 2216
22a8118d
MS
2217 memset(output, 0, COMP_BUF_SIZE);
2218 memset(decomp_output, 0, COMP_BUF_SIZE);
da7f033d
HX
2219
2220 ilen = ctemplate[i].inlen;
2221 ret = crypto_comp_compress(tfm, ctemplate[i].input,
33607384 2222 ilen, output, &dlen);
da7f033d
HX
2223 if (ret) {
2224 printk(KERN_ERR "alg: comp: compression failed "
2225 "on test %d for %s: ret=%d\n", i + 1, algo,
2226 -ret);
2227 goto out;
2228 }
2229
33607384
MC
2230 ilen = dlen;
2231 dlen = COMP_BUF_SIZE;
2232 ret = crypto_comp_decompress(tfm, output,
2233 ilen, decomp_output, &dlen);
2234 if (ret) {
2235 pr_err("alg: comp: compression failed: decompress: on test %d for %s failed: ret=%d\n",
2236 i + 1, algo, -ret);
2237 goto out;
2238 }
2239
2240 if (dlen != ctemplate[i].inlen) {
b812eb00
GU
2241 printk(KERN_ERR "alg: comp: Compression test %d "
2242 "failed for %s: output len = %d\n", i + 1, algo,
2243 dlen);
2244 ret = -EINVAL;
2245 goto out;
2246 }
2247
33607384
MC
2248 if (memcmp(decomp_output, ctemplate[i].input,
2249 ctemplate[i].inlen)) {
2250 pr_err("alg: comp: compression failed: output differs: on test %d for %s\n",
2251 i + 1, algo);
2252 hexdump(decomp_output, dlen);
da7f033d
HX
2253 ret = -EINVAL;
2254 goto out;
2255 }
2256 }
2257
2258 for (i = 0; i < dtcount; i++) {
c79cf910
GU
2259 int ilen;
2260 unsigned int dlen = COMP_BUF_SIZE;
da7f033d 2261
22a8118d 2262 memset(decomp_output, 0, COMP_BUF_SIZE);
da7f033d
HX
2263
2264 ilen = dtemplate[i].inlen;
2265 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
33607384 2266 ilen, decomp_output, &dlen);
da7f033d
HX
2267 if (ret) {
2268 printk(KERN_ERR "alg: comp: decompression failed "
2269 "on test %d for %s: ret=%d\n", i + 1, algo,
2270 -ret);
2271 goto out;
2272 }
2273
b812eb00
GU
2274 if (dlen != dtemplate[i].outlen) {
2275 printk(KERN_ERR "alg: comp: Decompression test %d "
2276 "failed for %s: output len = %d\n", i + 1, algo,
2277 dlen);
2278 ret = -EINVAL;
2279 goto out;
2280 }
2281
33607384 2282 if (memcmp(decomp_output, dtemplate[i].output, dlen)) {
da7f033d
HX
2283 printk(KERN_ERR "alg: comp: Decompression test %d "
2284 "failed for %s\n", i + 1, algo);
33607384 2285 hexdump(decomp_output, dlen);
da7f033d
HX
2286 ret = -EINVAL;
2287 goto out;
2288 }
2289 }
2290
2291 ret = 0;
2292
2293out:
33607384
MC
2294 kfree(decomp_output);
2295 kfree(output);
da7f033d
HX
2296 return ret;
2297}
2298
b13b1e0c 2299static int test_acomp(struct crypto_acomp *tfm,
33607384 2300 const struct comp_testvec *ctemplate,
b13b1e0c
EB
2301 const struct comp_testvec *dtemplate,
2302 int ctcount, int dtcount)
d7db7a88
GC
2303{
2304 const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
2305 unsigned int i;
a9943a0a 2306 char *output, *decomp_out;
d7db7a88
GC
2307 int ret;
2308 struct scatterlist src, dst;
2309 struct acomp_req *req;
7f397136 2310 struct crypto_wait wait;
d7db7a88 2311
eb095593
EB
2312 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
2313 if (!output)
2314 return -ENOMEM;
2315
a9943a0a
GC
2316 decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
2317 if (!decomp_out) {
2318 kfree(output);
2319 return -ENOMEM;
2320 }
2321
d7db7a88
GC
2322 for (i = 0; i < ctcount; i++) {
2323 unsigned int dlen = COMP_BUF_SIZE;
2324 int ilen = ctemplate[i].inlen;
02608e02 2325 void *input_vec;
d7db7a88 2326
d2110224 2327 input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL);
02608e02
LA
2328 if (!input_vec) {
2329 ret = -ENOMEM;
2330 goto out;
2331 }
2332
eb095593 2333 memset(output, 0, dlen);
7f397136 2334 crypto_init_wait(&wait);
02608e02 2335 sg_init_one(&src, input_vec, ilen);
d7db7a88
GC
2336 sg_init_one(&dst, output, dlen);
2337
2338 req = acomp_request_alloc(tfm);
2339 if (!req) {
2340 pr_err("alg: acomp: request alloc failed for %s\n",
2341 algo);
02608e02 2342 kfree(input_vec);
d7db7a88
GC
2343 ret = -ENOMEM;
2344 goto out;
2345 }
2346
2347 acomp_request_set_params(req, &src, &dst, ilen, dlen);
2348 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
7f397136 2349 crypto_req_done, &wait);
d7db7a88 2350
7f397136 2351 ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
d7db7a88
GC
2352 if (ret) {
2353 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
2354 i + 1, algo, -ret);
02608e02 2355 kfree(input_vec);
d7db7a88
GC
2356 acomp_request_free(req);
2357 goto out;
2358 }
2359
a9943a0a
GC
2360 ilen = req->dlen;
2361 dlen = COMP_BUF_SIZE;
2362 sg_init_one(&src, output, ilen);
2363 sg_init_one(&dst, decomp_out, dlen);
7f397136 2364 crypto_init_wait(&wait);
a9943a0a
GC
2365 acomp_request_set_params(req, &src, &dst, ilen, dlen);
2366
7f397136 2367 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
a9943a0a
GC
2368 if (ret) {
2369 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
2370 i + 1, algo, -ret);
2371 kfree(input_vec);
2372 acomp_request_free(req);
2373 goto out;
2374 }
2375
2376 if (req->dlen != ctemplate[i].inlen) {
d7db7a88
GC
2377 pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
2378 i + 1, algo, req->dlen);
2379 ret = -EINVAL;
02608e02 2380 kfree(input_vec);
d7db7a88
GC
2381 acomp_request_free(req);
2382 goto out;
2383 }
2384
a9943a0a 2385 if (memcmp(input_vec, decomp_out, req->dlen)) {
d7db7a88
GC
2386 pr_err("alg: acomp: Compression test %d failed for %s\n",
2387 i + 1, algo);
2388 hexdump(output, req->dlen);
2389 ret = -EINVAL;
02608e02 2390 kfree(input_vec);
d7db7a88
GC
2391 acomp_request_free(req);
2392 goto out;
2393 }
2394
02608e02 2395 kfree(input_vec);
d7db7a88
GC
2396 acomp_request_free(req);
2397 }
2398
2399 for (i = 0; i < dtcount; i++) {
2400 unsigned int dlen = COMP_BUF_SIZE;
2401 int ilen = dtemplate[i].inlen;
02608e02
LA
2402 void *input_vec;
2403
d2110224 2404 input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL);
02608e02
LA
2405 if (!input_vec) {
2406 ret = -ENOMEM;
2407 goto out;
2408 }
d7db7a88 2409
eb095593 2410 memset(output, 0, dlen);
7f397136 2411 crypto_init_wait(&wait);
02608e02 2412 sg_init_one(&src, input_vec, ilen);
d7db7a88
GC
2413 sg_init_one(&dst, output, dlen);
2414
2415 req = acomp_request_alloc(tfm);
2416 if (!req) {
2417 pr_err("alg: acomp: request alloc failed for %s\n",
2418 algo);
02608e02 2419 kfree(input_vec);
d7db7a88
GC
2420 ret = -ENOMEM;
2421 goto out;
2422 }
2423
2424 acomp_request_set_params(req, &src, &dst, ilen, dlen);
2425 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
7f397136 2426 crypto_req_done, &wait);
d7db7a88 2427
7f397136 2428 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
d7db7a88
GC
2429 if (ret) {
2430 pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
2431 i + 1, algo, -ret);
02608e02 2432 kfree(input_vec);
d7db7a88
GC
2433 acomp_request_free(req);
2434 goto out;
2435 }
2436
2437 if (req->dlen != dtemplate[i].outlen) {
2438 pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
2439 i + 1, algo, req->dlen);
2440 ret = -EINVAL;
02608e02 2441 kfree(input_vec);
d7db7a88
GC
2442 acomp_request_free(req);
2443 goto out;
2444 }
2445
2446 if (memcmp(output, dtemplate[i].output, req->dlen)) {
2447 pr_err("alg: acomp: Decompression test %d failed for %s\n",
2448 i + 1, algo);
2449 hexdump(output, req->dlen);
2450 ret = -EINVAL;
02608e02 2451 kfree(input_vec);
d7db7a88
GC
2452 acomp_request_free(req);
2453 goto out;
2454 }
2455
02608e02 2456 kfree(input_vec);
d7db7a88
GC
2457 acomp_request_free(req);
2458 }
2459
2460 ret = 0;
2461
2462out:
a9943a0a 2463 kfree(decomp_out);
eb095593 2464 kfree(output);
d7db7a88
GC
2465 return ret;
2466}
2467
b13b1e0c
EB
2468static int test_cprng(struct crypto_rng *tfm,
2469 const struct cprng_testvec *template,
7647d6ce
JW
2470 unsigned int tcount)
2471{
2472 const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
fa4ef8a6 2473 int err = 0, i, j, seedsize;
7647d6ce
JW
2474 u8 *seed;
2475 char result[32];
2476
2477 seedsize = crypto_rng_seedsize(tfm);
2478
2479 seed = kmalloc(seedsize, GFP_KERNEL);
2480 if (!seed) {
2481 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
2482 "for %s\n", algo);
2483 return -ENOMEM;
2484 }
2485
2486 for (i = 0; i < tcount; i++) {
2487 memset(result, 0, 32);
2488
2489 memcpy(seed, template[i].v, template[i].vlen);
2490 memcpy(seed + template[i].vlen, template[i].key,
2491 template[i].klen);
2492 memcpy(seed + template[i].vlen + template[i].klen,
2493 template[i].dt, template[i].dtlen);
2494
2495 err = crypto_rng_reset(tfm, seed, seedsize);
2496 if (err) {
2497 printk(KERN_ERR "alg: cprng: Failed to reset rng "
2498 "for %s\n", algo);
2499 goto out;
2500 }
2501
2502 for (j = 0; j < template[i].loops; j++) {
2503 err = crypto_rng_get_bytes(tfm, result,
2504 template[i].rlen);
19e60e13 2505 if (err < 0) {
7647d6ce
JW
2506 printk(KERN_ERR "alg: cprng: Failed to obtain "
2507 "the correct amount of random data for "
19e60e13
SM
2508 "%s (requested %d)\n", algo,
2509 template[i].rlen);
7647d6ce
JW
2510 goto out;
2511 }
2512 }
2513
2514 err = memcmp(result, template[i].result,
2515 template[i].rlen);
2516 if (err) {
2517 printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
2518 i, algo);
2519 hexdump(result, template[i].rlen);
2520 err = -EINVAL;
2521 goto out;
2522 }
2523 }
2524
2525out:
2526 kfree(seed);
2527 return err;
2528}
2529
da7f033d
HX
2530static int alg_test_cipher(const struct alg_test_desc *desc,
2531 const char *driver, u32 type, u32 mask)
2532{
92a4c9fe 2533 const struct cipher_test_suite *suite = &desc->suite.cipher;
1aa4ecd9 2534 struct crypto_cipher *tfm;
92a4c9fe 2535 int err;
da7f033d 2536
eed93e0c 2537 tfm = crypto_alloc_cipher(driver, type, mask);
da7f033d
HX
2538 if (IS_ERR(tfm)) {
2539 printk(KERN_ERR "alg: cipher: Failed to load transform for "
2540 "%s: %ld\n", driver, PTR_ERR(tfm));
2541 return PTR_ERR(tfm);
2542 }
2543
92a4c9fe
EB
2544 err = test_cipher(tfm, ENCRYPT, suite->vecs, suite->count);
2545 if (!err)
2546 err = test_cipher(tfm, DECRYPT, suite->vecs, suite->count);
da7f033d 2547
1aa4ecd9
HX
2548 crypto_free_cipher(tfm);
2549 return err;
2550}
2551
da7f033d
HX
2552static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
2553 u32 type, u32 mask)
2554{
d7db7a88
GC
2555 struct crypto_comp *comp;
2556 struct crypto_acomp *acomp;
da7f033d 2557 int err;
d7db7a88
GC
2558 u32 algo_type = type & CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
2559
2560 if (algo_type == CRYPTO_ALG_TYPE_ACOMPRESS) {
2561 acomp = crypto_alloc_acomp(driver, type, mask);
2562 if (IS_ERR(acomp)) {
2563 pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
2564 driver, PTR_ERR(acomp));
2565 return PTR_ERR(acomp);
2566 }
2567 err = test_acomp(acomp, desc->suite.comp.comp.vecs,
2568 desc->suite.comp.decomp.vecs,
2569 desc->suite.comp.comp.count,
2570 desc->suite.comp.decomp.count);
2571 crypto_free_acomp(acomp);
2572 } else {
2573 comp = crypto_alloc_comp(driver, type, mask);
2574 if (IS_ERR(comp)) {
2575 pr_err("alg: comp: Failed to load transform for %s: %ld\n",
2576 driver, PTR_ERR(comp));
2577 return PTR_ERR(comp);
2578 }
da7f033d 2579
d7db7a88
GC
2580 err = test_comp(comp, desc->suite.comp.comp.vecs,
2581 desc->suite.comp.decomp.vecs,
2582 desc->suite.comp.comp.count,
2583 desc->suite.comp.decomp.count);
da7f033d 2584
d7db7a88
GC
2585 crypto_free_comp(comp);
2586 }
da7f033d
HX
2587 return err;
2588}
2589
8e3ee85e
HX
2590static int alg_test_crc32c(const struct alg_test_desc *desc,
2591 const char *driver, u32 type, u32 mask)
2592{
2593 struct crypto_shash *tfm;
cb9dde88 2594 __le32 val;
8e3ee85e
HX
2595 int err;
2596
2597 err = alg_test_hash(desc, driver, type, mask);
2598 if (err)
eb5e6730 2599 return err;
8e3ee85e 2600
eed93e0c 2601 tfm = crypto_alloc_shash(driver, type, mask);
8e3ee85e 2602 if (IS_ERR(tfm)) {
eb5e6730
EB
2603 if (PTR_ERR(tfm) == -ENOENT) {
2604 /*
2605 * This crc32c implementation is only available through
2606 * ahash API, not the shash API, so the remaining part
2607 * of the test is not applicable to it.
2608 */
2609 return 0;
2610 }
8e3ee85e
HX
2611 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
2612 "%ld\n", driver, PTR_ERR(tfm));
eb5e6730 2613 return PTR_ERR(tfm);
8e3ee85e
HX
2614 }
2615
2616 do {
4c5c3024
JSM
2617 SHASH_DESC_ON_STACK(shash, tfm);
2618 u32 *ctx = (u32 *)shash_desc_ctx(shash);
8e3ee85e 2619
4c5c3024
JSM
2620 shash->tfm = tfm;
2621 shash->flags = 0;
8e3ee85e 2622
cb9dde88 2623 *ctx = 420553207;
4c5c3024 2624 err = crypto_shash_final(shash, (u8 *)&val);
8e3ee85e
HX
2625 if (err) {
2626 printk(KERN_ERR "alg: crc32c: Operation failed for "
2627 "%s: %d\n", driver, err);
2628 break;
2629 }
2630
cb9dde88
EB
2631 if (val != cpu_to_le32(~420553207)) {
2632 pr_err("alg: crc32c: Test failed for %s: %u\n",
2633 driver, le32_to_cpu(val));
8e3ee85e
HX
2634 err = -EINVAL;
2635 }
2636 } while (0);
2637
2638 crypto_free_shash(tfm);
2639
8e3ee85e
HX
2640 return err;
2641}
2642
7647d6ce
JW
2643static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
2644 u32 type, u32 mask)
2645{
2646 struct crypto_rng *rng;
2647 int err;
2648
eed93e0c 2649 rng = crypto_alloc_rng(driver, type, mask);
7647d6ce
JW
2650 if (IS_ERR(rng)) {
2651 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
2652 "%ld\n", driver, PTR_ERR(rng));
2653 return PTR_ERR(rng);
2654 }
2655
2656 err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
2657
2658 crypto_free_rng(rng);
2659
2660 return err;
2661}
2662
64d1cdfb 2663
b13b1e0c 2664static int drbg_cavs_test(const struct drbg_testvec *test, int pr,
64d1cdfb
SM
2665 const char *driver, u32 type, u32 mask)
2666{
2667 int ret = -EAGAIN;
2668 struct crypto_rng *drng;
2669 struct drbg_test_data test_data;
2670 struct drbg_string addtl, pers, testentropy;
2671 unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
2672
2673 if (!buf)
2674 return -ENOMEM;
2675
eed93e0c 2676 drng = crypto_alloc_rng(driver, type, mask);
64d1cdfb 2677 if (IS_ERR(drng)) {
2fc0d258 2678 printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
64d1cdfb
SM
2679 "%s\n", driver);
2680 kzfree(buf);
2681 return -ENOMEM;
2682 }
2683
2684 test_data.testentropy = &testentropy;
2685 drbg_string_fill(&testentropy, test->entropy, test->entropylen);
2686 drbg_string_fill(&pers, test->pers, test->perslen);
2687 ret = crypto_drbg_reset_test(drng, &pers, &test_data);
2688 if (ret) {
2689 printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
2690 goto outbuf;
2691 }
2692
2693 drbg_string_fill(&addtl, test->addtla, test->addtllen);
2694 if (pr) {
2695 drbg_string_fill(&testentropy, test->entpra, test->entprlen);
2696 ret = crypto_drbg_get_bytes_addtl_test(drng,
2697 buf, test->expectedlen, &addtl, &test_data);
2698 } else {
2699 ret = crypto_drbg_get_bytes_addtl(drng,
2700 buf, test->expectedlen, &addtl);
2701 }
19e60e13 2702 if (ret < 0) {
2fc0d258 2703 printk(KERN_ERR "alg: drbg: could not obtain random data for "
64d1cdfb
SM
2704 "driver %s\n", driver);
2705 goto outbuf;
2706 }
2707
2708 drbg_string_fill(&addtl, test->addtlb, test->addtllen);
2709 if (pr) {
2710 drbg_string_fill(&testentropy, test->entprb, test->entprlen);
2711 ret = crypto_drbg_get_bytes_addtl_test(drng,
2712 buf, test->expectedlen, &addtl, &test_data);
2713 } else {
2714 ret = crypto_drbg_get_bytes_addtl(drng,
2715 buf, test->expectedlen, &addtl);
2716 }
19e60e13 2717 if (ret < 0) {
2fc0d258 2718 printk(KERN_ERR "alg: drbg: could not obtain random data for "
64d1cdfb
SM
2719 "driver %s\n", driver);
2720 goto outbuf;
2721 }
2722
2723 ret = memcmp(test->expected, buf, test->expectedlen);
2724
2725outbuf:
2726 crypto_free_rng(drng);
2727 kzfree(buf);
2728 return ret;
2729}
2730
2731
2732static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
2733 u32 type, u32 mask)
2734{
2735 int err = 0;
2736 int pr = 0;
2737 int i = 0;
b13b1e0c 2738 const struct drbg_testvec *template = desc->suite.drbg.vecs;
64d1cdfb
SM
2739 unsigned int tcount = desc->suite.drbg.count;
2740
2741 if (0 == memcmp(driver, "drbg_pr_", 8))
2742 pr = 1;
2743
2744 for (i = 0; i < tcount; i++) {
2745 err = drbg_cavs_test(&template[i], pr, driver, type, mask);
2746 if (err) {
2747 printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
2748 i, driver);
2749 err = -EINVAL;
2750 break;
2751 }
2752 }
2753 return err;
2754
2755}
2756
b13b1e0c 2757static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
802c7f1c
SB
2758 const char *alg)
2759{
2760 struct kpp_request *req;
2761 void *input_buf = NULL;
2762 void *output_buf = NULL;
47d3fd39
TA
2763 void *a_public = NULL;
2764 void *a_ss = NULL;
2765 void *shared_secret = NULL;
7f397136 2766 struct crypto_wait wait;
802c7f1c
SB
2767 unsigned int out_len_max;
2768 int err = -ENOMEM;
2769 struct scatterlist src, dst;
2770
2771 req = kpp_request_alloc(tfm, GFP_KERNEL);
2772 if (!req)
2773 return err;
2774
7f397136 2775 crypto_init_wait(&wait);
802c7f1c
SB
2776
2777 err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
2778 if (err < 0)
2779 goto free_req;
2780
2781 out_len_max = crypto_kpp_maxsize(tfm);
2782 output_buf = kzalloc(out_len_max, GFP_KERNEL);
2783 if (!output_buf) {
2784 err = -ENOMEM;
2785 goto free_req;
2786 }
2787
2788 /* Use appropriate parameter as base */
2789 kpp_request_set_input(req, NULL, 0);
2790 sg_init_one(&dst, output_buf, out_len_max);
2791 kpp_request_set_output(req, &dst, out_len_max);
2792 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
7f397136 2793 crypto_req_done, &wait);
802c7f1c 2794
47d3fd39 2795 /* Compute party A's public key */
7f397136 2796 err = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
802c7f1c 2797 if (err) {
47d3fd39 2798 pr_err("alg: %s: Party A: generate public key test failed. err %d\n",
802c7f1c
SB
2799 alg, err);
2800 goto free_output;
2801 }
47d3fd39
TA
2802
2803 if (vec->genkey) {
2804 /* Save party A's public key */
e3d90e52 2805 a_public = kmemdup(sg_virt(req->dst), out_len_max, GFP_KERNEL);
47d3fd39
TA
2806 if (!a_public) {
2807 err = -ENOMEM;
2808 goto free_output;
2809 }
47d3fd39
TA
2810 } else {
2811 /* Verify calculated public key */
2812 if (memcmp(vec->expected_a_public, sg_virt(req->dst),
2813 vec->expected_a_public_size)) {
2814 pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n",
2815 alg);
2816 err = -EINVAL;
2817 goto free_output;
2818 }
802c7f1c
SB
2819 }
2820
2821 /* Calculate shared secret key by using counter part (b) public key. */
e3d90e52 2822 input_buf = kmemdup(vec->b_public, vec->b_public_size, GFP_KERNEL);
802c7f1c
SB
2823 if (!input_buf) {
2824 err = -ENOMEM;
2825 goto free_output;
2826 }
2827
802c7f1c
SB
2828 sg_init_one(&src, input_buf, vec->b_public_size);
2829 sg_init_one(&dst, output_buf, out_len_max);
2830 kpp_request_set_input(req, &src, vec->b_public_size);
2831 kpp_request_set_output(req, &dst, out_len_max);
2832 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
7f397136
GBY
2833 crypto_req_done, &wait);
2834 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait);
802c7f1c 2835 if (err) {
47d3fd39 2836 pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n",
802c7f1c
SB
2837 alg, err);
2838 goto free_all;
2839 }
47d3fd39
TA
2840
2841 if (vec->genkey) {
2842 /* Save the shared secret obtained by party A */
e3d90e52 2843 a_ss = kmemdup(sg_virt(req->dst), vec->expected_ss_size, GFP_KERNEL);
47d3fd39
TA
2844 if (!a_ss) {
2845 err = -ENOMEM;
2846 goto free_all;
2847 }
47d3fd39
TA
2848
2849 /*
2850 * Calculate party B's shared secret by using party A's
2851 * public key.
2852 */
2853 err = crypto_kpp_set_secret(tfm, vec->b_secret,
2854 vec->b_secret_size);
2855 if (err < 0)
2856 goto free_all;
2857
2858 sg_init_one(&src, a_public, vec->expected_a_public_size);
2859 sg_init_one(&dst, output_buf, out_len_max);
2860 kpp_request_set_input(req, &src, vec->expected_a_public_size);
2861 kpp_request_set_output(req, &dst, out_len_max);
2862 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
7f397136
GBY
2863 crypto_req_done, &wait);
2864 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req),
2865 &wait);
47d3fd39
TA
2866 if (err) {
2867 pr_err("alg: %s: Party B: compute shared secret failed. err %d\n",
2868 alg, err);
2869 goto free_all;
2870 }
2871
2872 shared_secret = a_ss;
2873 } else {
2874 shared_secret = (void *)vec->expected_ss;
2875 }
2876
802c7f1c
SB
2877 /*
2878 * verify shared secret from which the user will derive
2879 * secret key by executing whatever hash it has chosen
2880 */
47d3fd39 2881 if (memcmp(shared_secret, sg_virt(req->dst),
802c7f1c
SB
2882 vec->expected_ss_size)) {
2883 pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
2884 alg);
2885 err = -EINVAL;
2886 }
2887
2888free_all:
47d3fd39 2889 kfree(a_ss);
802c7f1c
SB
2890 kfree(input_buf);
2891free_output:
47d3fd39 2892 kfree(a_public);
802c7f1c
SB
2893 kfree(output_buf);
2894free_req:
2895 kpp_request_free(req);
2896 return err;
2897}
2898
2899static int test_kpp(struct crypto_kpp *tfm, const char *alg,
b13b1e0c 2900 const struct kpp_testvec *vecs, unsigned int tcount)
802c7f1c
SB
2901{
2902 int ret, i;
2903
2904 for (i = 0; i < tcount; i++) {
2905 ret = do_test_kpp(tfm, vecs++, alg);
2906 if (ret) {
2907 pr_err("alg: %s: test failed on vector %d, err=%d\n",
2908 alg, i + 1, ret);
2909 return ret;
2910 }
2911 }
2912 return 0;
2913}
2914
2915static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
2916 u32 type, u32 mask)
2917{
2918 struct crypto_kpp *tfm;
2919 int err = 0;
2920
eed93e0c 2921 tfm = crypto_alloc_kpp(driver, type, mask);
802c7f1c
SB
2922 if (IS_ERR(tfm)) {
2923 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
2924 driver, PTR_ERR(tfm));
2925 return PTR_ERR(tfm);
2926 }
2927 if (desc->suite.kpp.vecs)
2928 err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
2929 desc->suite.kpp.count);
2930
2931 crypto_free_kpp(tfm);
2932 return err;
2933}
2934
f1774cb8
VC
2935static u8 *test_pack_u32(u8 *dst, u32 val)
2936{
2937 memcpy(dst, &val, sizeof(val));
2938 return dst + sizeof(val);
2939}
2940
50d2b643 2941static int test_akcipher_one(struct crypto_akcipher *tfm,
b13b1e0c 2942 const struct akcipher_testvec *vecs)
946cc463 2943{
df27b26f 2944 char *xbuf[XBUFSIZE];
946cc463
TS
2945 struct akcipher_request *req;
2946 void *outbuf_enc = NULL;
2947 void *outbuf_dec = NULL;
7f397136 2948 struct crypto_wait wait;
946cc463
TS
2949 unsigned int out_len_max, out_len = 0;
2950 int err = -ENOMEM;
c7381b01 2951 struct scatterlist src, dst, src_tab[3];
0507de94
VC
2952 const char *m, *c;
2953 unsigned int m_size, c_size;
2954 const char *op;
f1774cb8 2955 u8 *key, *ptr;
946cc463 2956
df27b26f
HX
2957 if (testmgr_alloc_buf(xbuf))
2958 return err;
2959
946cc463
TS
2960 req = akcipher_request_alloc(tfm, GFP_KERNEL);
2961 if (!req)
df27b26f 2962 goto free_xbuf;
946cc463 2963
7f397136 2964 crypto_init_wait(&wait);
946cc463 2965
f1774cb8
VC
2966 key = kmalloc(vecs->key_len + sizeof(u32) * 2 + vecs->param_len,
2967 GFP_KERNEL);
2968 if (!key)
2969 goto free_xbuf;
2970 memcpy(key, vecs->key, vecs->key_len);
2971 ptr = key + vecs->key_len;
2972 ptr = test_pack_u32(ptr, vecs->algo);
2973 ptr = test_pack_u32(ptr, vecs->param_len);
2974 memcpy(ptr, vecs->params, vecs->param_len);
2975
22287b0b 2976 if (vecs->public_key_vec)
f1774cb8 2977 err = crypto_akcipher_set_pub_key(tfm, key, vecs->key_len);
22287b0b 2978 else
f1774cb8 2979 err = crypto_akcipher_set_priv_key(tfm, key, vecs->key_len);
22287b0b 2980 if (err)
946cc463 2981 goto free_req;
946cc463 2982
0507de94
VC
2983 /*
2984 * First run test which do not require a private key, such as
2985 * encrypt or verify.
2986 */
c7381b01
VC
2987 err = -ENOMEM;
2988 out_len_max = crypto_akcipher_maxsize(tfm);
946cc463
TS
2989 outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
2990 if (!outbuf_enc)
2991 goto free_req;
2992
0507de94
VC
2993 if (!vecs->siggen_sigver_test) {
2994 m = vecs->m;
2995 m_size = vecs->m_size;
2996 c = vecs->c;
2997 c_size = vecs->c_size;
2998 op = "encrypt";
2999 } else {
3000 /* Swap args so we could keep plaintext (digest)
3001 * in vecs->m, and cooked signature in vecs->c.
3002 */
3003 m = vecs->c; /* signature */
3004 m_size = vecs->c_size;
3005 c = vecs->m; /* digest */
3006 c_size = vecs->m_size;
3007 op = "verify";
3008 }
df27b26f 3009
0507de94
VC
3010 if (WARN_ON(m_size > PAGE_SIZE))
3011 goto free_all;
3012 memcpy(xbuf[0], m, m_size);
df27b26f 3013
c7381b01 3014 sg_init_table(src_tab, 3);
df27b26f 3015 sg_set_buf(&src_tab[0], xbuf[0], 8);
0507de94 3016 sg_set_buf(&src_tab[1], xbuf[0] + 8, m_size - 8);
c7381b01
VC
3017 if (vecs->siggen_sigver_test) {
3018 if (WARN_ON(c_size > PAGE_SIZE))
3019 goto free_all;
3020 memcpy(xbuf[1], c, c_size);
3021 sg_set_buf(&src_tab[2], xbuf[1], c_size);
3022 akcipher_request_set_crypt(req, src_tab, NULL, m_size, c_size);
3023 } else {
3024 sg_init_one(&dst, outbuf_enc, out_len_max);
3025 akcipher_request_set_crypt(req, src_tab, &dst, m_size,
3026 out_len_max);
3027 }
946cc463 3028 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
7f397136 3029 crypto_req_done, &wait);
946cc463 3030
7f397136 3031 err = crypto_wait_req(vecs->siggen_sigver_test ?
0507de94
VC
3032 /* Run asymmetric signature verification */
3033 crypto_akcipher_verify(req) :
7f397136
GBY
3034 /* Run asymmetric encrypt */
3035 crypto_akcipher_encrypt(req), &wait);
946cc463 3036 if (err) {
0507de94 3037 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
946cc463
TS
3038 goto free_all;
3039 }
c7381b01
VC
3040 if (!vecs->siggen_sigver_test) {
3041 if (req->dst_len != c_size) {
3042 pr_err("alg: akcipher: %s test failed. Invalid output len\n",
3043 op);
3044 err = -EINVAL;
3045 goto free_all;
3046 }
3047 /* verify that encrypted message is equal to expected */
3048 if (memcmp(c, outbuf_enc, c_size) != 0) {
3049 pr_err("alg: akcipher: %s test failed. Invalid output\n",
3050 op);
3051 hexdump(outbuf_enc, c_size);
3052 err = -EINVAL;
3053 goto free_all;
3054 }
946cc463 3055 }
0507de94
VC
3056
3057 /*
3058 * Don't invoke (decrypt or sign) test which require a private key
3059 * for vectors with only a public key.
3060 */
946cc463
TS
3061 if (vecs->public_key_vec) {
3062 err = 0;
3063 goto free_all;
3064 }
3065 outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
3066 if (!outbuf_dec) {
3067 err = -ENOMEM;
3068 goto free_all;
3069 }
df27b26f 3070
0507de94
VC
3071 op = vecs->siggen_sigver_test ? "sign" : "decrypt";
3072 if (WARN_ON(c_size > PAGE_SIZE))
df27b26f 3073 goto free_all;
0507de94 3074 memcpy(xbuf[0], c, c_size);
df27b26f 3075
0507de94 3076 sg_init_one(&src, xbuf[0], c_size);
22287b0b 3077 sg_init_one(&dst, outbuf_dec, out_len_max);
7f397136 3078 crypto_init_wait(&wait);
0507de94 3079 akcipher_request_set_crypt(req, &src, &dst, c_size, out_len_max);
946cc463 3080
7f397136 3081 err = crypto_wait_req(vecs->siggen_sigver_test ?
0507de94
VC
3082 /* Run asymmetric signature generation */
3083 crypto_akcipher_sign(req) :
7f397136
GBY
3084 /* Run asymmetric decrypt */
3085 crypto_akcipher_decrypt(req), &wait);
946cc463 3086 if (err) {
0507de94 3087 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
946cc463
TS
3088 goto free_all;
3089 }
3090 out_len = req->dst_len;
0507de94
VC
3091 if (out_len < m_size) {
3092 pr_err("alg: akcipher: %s test failed. Invalid output len %u\n",
3093 op, out_len);
946cc463
TS
3094 err = -EINVAL;
3095 goto free_all;
3096 }
3097 /* verify that decrypted message is equal to the original msg */
0507de94
VC
3098 if (memchr_inv(outbuf_dec, 0, out_len - m_size) ||
3099 memcmp(m, outbuf_dec + out_len - m_size, m_size)) {
3100 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
50d2b643 3101 hexdump(outbuf_dec, out_len);
946cc463
TS
3102 err = -EINVAL;
3103 }
3104free_all:
3105 kfree(outbuf_dec);
3106 kfree(outbuf_enc);
3107free_req:
3108 akcipher_request_free(req);
f1774cb8 3109 kfree(key);
df27b26f
HX
3110free_xbuf:
3111 testmgr_free_buf(xbuf);
946cc463
TS
3112 return err;
3113}
3114
50d2b643 3115static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
b13b1e0c
EB
3116 const struct akcipher_testvec *vecs,
3117 unsigned int tcount)
946cc463 3118{
15226e48
HX
3119 const char *algo =
3120 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
946cc463
TS
3121 int ret, i;
3122
3123 for (i = 0; i < tcount; i++) {
50d2b643
HX
3124 ret = test_akcipher_one(tfm, vecs++);
3125 if (!ret)
3126 continue;
946cc463 3127
15226e48
HX
3128 pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
3129 i + 1, algo, ret);
50d2b643
HX
3130 return ret;
3131 }
946cc463
TS
3132 return 0;
3133}
3134
3135static int alg_test_akcipher(const struct alg_test_desc *desc,
3136 const char *driver, u32 type, u32 mask)
3137{
3138 struct crypto_akcipher *tfm;
3139 int err = 0;
3140
eed93e0c 3141 tfm = crypto_alloc_akcipher(driver, type, mask);
946cc463
TS
3142 if (IS_ERR(tfm)) {
3143 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
3144 driver, PTR_ERR(tfm));
3145 return PTR_ERR(tfm);
3146 }
3147 if (desc->suite.akcipher.vecs)
3148 err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
3149 desc->suite.akcipher.count);
3150
3151 crypto_free_akcipher(tfm);
3152 return err;
3153}
3154
863b557a
YS
3155static int alg_test_null(const struct alg_test_desc *desc,
3156 const char *driver, u32 type, u32 mask)
3157{
3158 return 0;
3159}
3160
21c8e720
AB
3161#define __VECS(tv) { .vecs = tv, .count = ARRAY_SIZE(tv) }
3162
da7f033d
HX
3163/* Please keep this list sorted by algorithm name. */
3164static const struct alg_test_desc alg_test_descs[] = {
3165 {
059c2a4d
EB
3166 .alg = "adiantum(xchacha12,aes)",
3167 .test = alg_test_skcipher,
3168 .suite = {
3169 .cipher = __VECS(adiantum_xchacha12_aes_tv_template)
3170 },
3171 }, {
3172 .alg = "adiantum(xchacha20,aes)",
3173 .test = alg_test_skcipher,
3174 .suite = {
3175 .cipher = __VECS(adiantum_xchacha20_aes_tv_template)
3176 },
3177 }, {
b87dc203
OM
3178 .alg = "aegis128",
3179 .test = alg_test_aead,
3180 .suite = {
a0d608ee 3181 .aead = __VECS(aegis128_tv_template)
b87dc203
OM
3182 }
3183 }, {
3184 .alg = "aegis128l",
3185 .test = alg_test_aead,
3186 .suite = {
a0d608ee 3187 .aead = __VECS(aegis128l_tv_template)
b87dc203
OM
3188 }
3189 }, {
3190 .alg = "aegis256",
3191 .test = alg_test_aead,
3192 .suite = {
a0d608ee 3193 .aead = __VECS(aegis256_tv_template)
b87dc203
OM
3194 }
3195 }, {
e08ca2da
JW
3196 .alg = "ansi_cprng",
3197 .test = alg_test_cprng,
3198 .suite = {
21c8e720 3199 .cprng = __VECS(ansi_cprng_aes_tv_template)
e08ca2da 3200 }
bca4feb0
HG
3201 }, {
3202 .alg = "authenc(hmac(md5),ecb(cipher_null))",
3203 .test = alg_test_aead,
bca4feb0 3204 .suite = {
a0d608ee 3205 .aead = __VECS(hmac_md5_ecb_cipher_null_tv_template)
bca4feb0 3206 }
e46e9a46 3207 }, {
a4198fd4 3208 .alg = "authenc(hmac(sha1),cbc(aes))",
e46e9a46 3209 .test = alg_test_aead,
bcf741cb 3210 .fips_allowed = 1,
e46e9a46 3211 .suite = {
a0d608ee 3212 .aead = __VECS(hmac_sha1_aes_cbc_tv_temp)
5208ed2c
NL
3213 }
3214 }, {
a4198fd4 3215 .alg = "authenc(hmac(sha1),cbc(des))",
5208ed2c 3216 .test = alg_test_aead,
5208ed2c 3217 .suite = {
a0d608ee 3218 .aead = __VECS(hmac_sha1_des_cbc_tv_temp)
5208ed2c
NL
3219 }
3220 }, {
a4198fd4 3221 .alg = "authenc(hmac(sha1),cbc(des3_ede))",
5208ed2c 3222 .test = alg_test_aead,
ed1afac9 3223 .fips_allowed = 1,
5208ed2c 3224 .suite = {
a0d608ee 3225 .aead = __VECS(hmac_sha1_des3_ede_cbc_tv_temp)
e46e9a46 3226 }
fb16abc2
MM
3227 }, {
3228 .alg = "authenc(hmac(sha1),ctr(aes))",
3229 .test = alg_test_null,
3230 .fips_allowed = 1,
bca4feb0
HG
3231 }, {
3232 .alg = "authenc(hmac(sha1),ecb(cipher_null))",
3233 .test = alg_test_aead,
bca4feb0 3234 .suite = {
a0d608ee 3235 .aead = __VECS(hmac_sha1_ecb_cipher_null_tv_temp)
5208ed2c 3236 }
8888690e
MM
3237 }, {
3238 .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
3239 .test = alg_test_null,
3240 .fips_allowed = 1,
5208ed2c 3241 }, {
a4198fd4 3242 .alg = "authenc(hmac(sha224),cbc(des))",
5208ed2c 3243 .test = alg_test_aead,
5208ed2c 3244 .suite = {
a0d608ee 3245 .aead = __VECS(hmac_sha224_des_cbc_tv_temp)
5208ed2c
NL
3246 }
3247 }, {
a4198fd4 3248 .alg = "authenc(hmac(sha224),cbc(des3_ede))",
5208ed2c 3249 .test = alg_test_aead,
ed1afac9 3250 .fips_allowed = 1,
5208ed2c 3251 .suite = {
a0d608ee 3252 .aead = __VECS(hmac_sha224_des3_ede_cbc_tv_temp)
bca4feb0 3253 }
e46e9a46 3254 }, {
a4198fd4 3255 .alg = "authenc(hmac(sha256),cbc(aes))",
e46e9a46 3256 .test = alg_test_aead,
ed1afac9 3257 .fips_allowed = 1,
e46e9a46 3258 .suite = {
a0d608ee 3259 .aead = __VECS(hmac_sha256_aes_cbc_tv_temp)
5208ed2c
NL
3260 }
3261 }, {
a4198fd4 3262 .alg = "authenc(hmac(sha256),cbc(des))",
5208ed2c 3263 .test = alg_test_aead,
5208ed2c 3264 .suite = {
a0d608ee 3265 .aead = __VECS(hmac_sha256_des_cbc_tv_temp)
5208ed2c
NL
3266 }
3267 }, {
a4198fd4 3268 .alg = "authenc(hmac(sha256),cbc(des3_ede))",
5208ed2c 3269 .test = alg_test_aead,
ed1afac9 3270 .fips_allowed = 1,
5208ed2c 3271 .suite = {
a0d608ee 3272 .aead = __VECS(hmac_sha256_des3_ede_cbc_tv_temp)
5208ed2c 3273 }
fb16abc2
MM
3274 }, {
3275 .alg = "authenc(hmac(sha256),ctr(aes))",
3276 .test = alg_test_null,
3277 .fips_allowed = 1,
8888690e
MM
3278 }, {
3279 .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
3280 .test = alg_test_null,
3281 .fips_allowed = 1,
5208ed2c 3282 }, {
a4198fd4 3283 .alg = "authenc(hmac(sha384),cbc(des))",
5208ed2c 3284 .test = alg_test_aead,
5208ed2c 3285 .suite = {
a0d608ee 3286 .aead = __VECS(hmac_sha384_des_cbc_tv_temp)
5208ed2c
NL
3287 }
3288 }, {
a4198fd4 3289 .alg = "authenc(hmac(sha384),cbc(des3_ede))",
5208ed2c 3290 .test = alg_test_aead,
ed1afac9 3291 .fips_allowed = 1,
5208ed2c 3292 .suite = {
a0d608ee 3293 .aead = __VECS(hmac_sha384_des3_ede_cbc_tv_temp)
e46e9a46 3294 }
fb16abc2
MM
3295 }, {
3296 .alg = "authenc(hmac(sha384),ctr(aes))",
3297 .test = alg_test_null,
3298 .fips_allowed = 1,
8888690e
MM
3299 }, {
3300 .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
3301 .test = alg_test_null,
3302 .fips_allowed = 1,
e46e9a46 3303 }, {
a4198fd4 3304 .alg = "authenc(hmac(sha512),cbc(aes))",
ed1afac9 3305 .fips_allowed = 1,
e46e9a46 3306 .test = alg_test_aead,
e46e9a46 3307 .suite = {
a0d608ee 3308 .aead = __VECS(hmac_sha512_aes_cbc_tv_temp)
5208ed2c
NL
3309 }
3310 }, {
a4198fd4 3311 .alg = "authenc(hmac(sha512),cbc(des))",
5208ed2c 3312 .test = alg_test_aead,
5208ed2c 3313 .suite = {
a0d608ee 3314 .aead = __VECS(hmac_sha512_des_cbc_tv_temp)
5208ed2c
NL
3315 }
3316 }, {
a4198fd4 3317 .alg = "authenc(hmac(sha512),cbc(des3_ede))",
5208ed2c 3318 .test = alg_test_aead,
ed1afac9 3319 .fips_allowed = 1,
5208ed2c 3320 .suite = {
a0d608ee 3321 .aead = __VECS(hmac_sha512_des3_ede_cbc_tv_temp)
e46e9a46 3322 }
fb16abc2
MM
3323 }, {
3324 .alg = "authenc(hmac(sha512),ctr(aes))",
3325 .test = alg_test_null,
3326 .fips_allowed = 1,
8888690e
MM
3327 }, {
3328 .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
3329 .test = alg_test_null,
3330 .fips_allowed = 1,
e08ca2da 3331 }, {
da7f033d 3332 .alg = "cbc(aes)",
1aa4ecd9 3333 .test = alg_test_skcipher,
a1915d51 3334 .fips_allowed = 1,
da7f033d 3335 .suite = {
92a4c9fe
EB
3336 .cipher = __VECS(aes_cbc_tv_template)
3337 },
da7f033d
HX
3338 }, {
3339 .alg = "cbc(anubis)",
1aa4ecd9 3340 .test = alg_test_skcipher,
da7f033d 3341 .suite = {
92a4c9fe
EB
3342 .cipher = __VECS(anubis_cbc_tv_template)
3343 },
da7f033d
HX
3344 }, {
3345 .alg = "cbc(blowfish)",
1aa4ecd9 3346 .test = alg_test_skcipher,
da7f033d 3347 .suite = {
92a4c9fe
EB
3348 .cipher = __VECS(bf_cbc_tv_template)
3349 },
da7f033d
HX
3350 }, {
3351 .alg = "cbc(camellia)",
1aa4ecd9 3352 .test = alg_test_skcipher,
da7f033d 3353 .suite = {
92a4c9fe
EB
3354 .cipher = __VECS(camellia_cbc_tv_template)
3355 },
a2c58260
JG
3356 }, {
3357 .alg = "cbc(cast5)",
3358 .test = alg_test_skcipher,
3359 .suite = {
92a4c9fe
EB
3360 .cipher = __VECS(cast5_cbc_tv_template)
3361 },
9b8b0405
JG
3362 }, {
3363 .alg = "cbc(cast6)",
3364 .test = alg_test_skcipher,
3365 .suite = {
92a4c9fe
EB
3366 .cipher = __VECS(cast6_cbc_tv_template)
3367 },
da7f033d
HX
3368 }, {
3369 .alg = "cbc(des)",
1aa4ecd9 3370 .test = alg_test_skcipher,
da7f033d 3371 .suite = {
92a4c9fe
EB
3372 .cipher = __VECS(des_cbc_tv_template)
3373 },
da7f033d
HX
3374 }, {
3375 .alg = "cbc(des3_ede)",
1aa4ecd9 3376 .test = alg_test_skcipher,
a1915d51 3377 .fips_allowed = 1,
da7f033d 3378 .suite = {
92a4c9fe
EB
3379 .cipher = __VECS(des3_ede_cbc_tv_template)
3380 },
a794d8d8
GBY
3381 }, {
3382 /* Same as cbc(aes) except the key is stored in
3383 * hardware secure memory which we reference by index
3384 */
3385 .alg = "cbc(paes)",
3386 .test = alg_test_null,
3387 .fips_allowed = 1,
9d25917d
JK
3388 }, {
3389 .alg = "cbc(serpent)",
3390 .test = alg_test_skcipher,
3391 .suite = {
92a4c9fe
EB
3392 .cipher = __VECS(serpent_cbc_tv_template)
3393 },
95ba5973
GBY
3394 }, {
3395 .alg = "cbc(sm4)",
3396 .test = alg_test_skcipher,
3397 .suite = {
3398 .cipher = __VECS(sm4_cbc_tv_template)
3399 }
da7f033d
HX
3400 }, {
3401 .alg = "cbc(twofish)",
1aa4ecd9 3402 .test = alg_test_skcipher,
da7f033d 3403 .suite = {
92a4c9fe
EB
3404 .cipher = __VECS(tf_cbc_tv_template)
3405 },
092acf06
AB
3406 }, {
3407 .alg = "cbcmac(aes)",
3408 .fips_allowed = 1,
3409 .test = alg_test_hash,
3410 .suite = {
3411 .hash = __VECS(aes_cbcmac_tv_template)
3412 }
da7f033d
HX
3413 }, {
3414 .alg = "ccm(aes)",
3415 .test = alg_test_aead,
a1915d51 3416 .fips_allowed = 1,
da7f033d 3417 .suite = {
a0d608ee 3418 .aead = __VECS(aes_ccm_tv_template)
da7f033d 3419 }
7da66670
DB
3420 }, {
3421 .alg = "cfb(aes)",
3422 .test = alg_test_skcipher,
3423 .fips_allowed = 1,
3424 .suite = {
3425 .cipher = __VECS(aes_cfb_tv_template)
3426 },
3590ebf2
MW
3427 }, {
3428 .alg = "chacha20",
3429 .test = alg_test_skcipher,
3430 .suite = {
92a4c9fe
EB
3431 .cipher = __VECS(chacha20_tv_template)
3432 },
93b5e86a
JK
3433 }, {
3434 .alg = "cmac(aes)",
8f183751 3435 .fips_allowed = 1,
93b5e86a
JK
3436 .test = alg_test_hash,
3437 .suite = {
21c8e720 3438 .hash = __VECS(aes_cmac128_tv_template)
93b5e86a
JK
3439 }
3440 }, {
3441 .alg = "cmac(des3_ede)",
8f183751 3442 .fips_allowed = 1,
93b5e86a
JK
3443 .test = alg_test_hash,
3444 .suite = {
21c8e720 3445 .hash = __VECS(des3_ede_cmac64_tv_template)
93b5e86a 3446 }
e448370d
JK
3447 }, {
3448 .alg = "compress_null",
3449 .test = alg_test_null,
ebb3472f
AB
3450 }, {
3451 .alg = "crc32",
3452 .test = alg_test_hash,
a8a34416 3453 .fips_allowed = 1,
ebb3472f 3454 .suite = {
21c8e720 3455 .hash = __VECS(crc32_tv_template)
ebb3472f 3456 }
da7f033d
HX
3457 }, {
3458 .alg = "crc32c",
8e3ee85e 3459 .test = alg_test_crc32c,
a1915d51 3460 .fips_allowed = 1,
da7f033d 3461 .suite = {
21c8e720 3462 .hash = __VECS(crc32c_tv_template)
da7f033d 3463 }
68411521
HX
3464 }, {
3465 .alg = "crct10dif",
3466 .test = alg_test_hash,
3467 .fips_allowed = 1,
3468 .suite = {
21c8e720 3469 .hash = __VECS(crct10dif_tv_template)
68411521 3470 }
f7cb80f2
JW
3471 }, {
3472 .alg = "ctr(aes)",
3473 .test = alg_test_skcipher,
a1915d51 3474 .fips_allowed = 1,
f7cb80f2 3475 .suite = {
92a4c9fe 3476 .cipher = __VECS(aes_ctr_tv_template)
f7cb80f2 3477 }
85b63e34
JK
3478 }, {
3479 .alg = "ctr(blowfish)",
3480 .test = alg_test_skcipher,
3481 .suite = {
92a4c9fe 3482 .cipher = __VECS(bf_ctr_tv_template)
85b63e34 3483 }
0840605e
JK
3484 }, {
3485 .alg = "ctr(camellia)",
3486 .test = alg_test_skcipher,
3487 .suite = {
92a4c9fe 3488 .cipher = __VECS(camellia_ctr_tv_template)
0840605e 3489 }
a2c58260
JG
3490 }, {
3491 .alg = "ctr(cast5)",
3492 .test = alg_test_skcipher,
3493 .suite = {
92a4c9fe 3494 .cipher = __VECS(cast5_ctr_tv_template)
a2c58260 3495 }
9b8b0405
JG
3496 }, {
3497 .alg = "ctr(cast6)",
3498 .test = alg_test_skcipher,
3499 .suite = {
92a4c9fe 3500 .cipher = __VECS(cast6_ctr_tv_template)
9b8b0405 3501 }
8163fc30
JK
3502 }, {
3503 .alg = "ctr(des)",
3504 .test = alg_test_skcipher,
3505 .suite = {
92a4c9fe 3506 .cipher = __VECS(des_ctr_tv_template)
8163fc30 3507 }
e080b17a
JK
3508 }, {
3509 .alg = "ctr(des3_ede)",
3510 .test = alg_test_skcipher,
0d8da104 3511 .fips_allowed = 1,
e080b17a 3512 .suite = {
92a4c9fe 3513 .cipher = __VECS(des3_ede_ctr_tv_template)
e080b17a 3514 }
a794d8d8
GBY
3515 }, {
3516 /* Same as ctr(aes) except the key is stored in
3517 * hardware secure memory which we reference by index
3518 */
3519 .alg = "ctr(paes)",
3520 .test = alg_test_null,
3521 .fips_allowed = 1,
9d25917d
JK
3522 }, {
3523 .alg = "ctr(serpent)",
3524 .test = alg_test_skcipher,
3525 .suite = {
92a4c9fe 3526 .cipher = __VECS(serpent_ctr_tv_template)
9d25917d 3527 }
95ba5973
GBY
3528 }, {
3529 .alg = "ctr(sm4)",
3530 .test = alg_test_skcipher,
3531 .suite = {
3532 .cipher = __VECS(sm4_ctr_tv_template)
3533 }
573da620
JK
3534 }, {
3535 .alg = "ctr(twofish)",
3536 .test = alg_test_skcipher,
3537 .suite = {
92a4c9fe 3538 .cipher = __VECS(tf_ctr_tv_template)
573da620 3539 }
da7f033d
HX
3540 }, {
3541 .alg = "cts(cbc(aes))",
1aa4ecd9 3542 .test = alg_test_skcipher,
196ad604 3543 .fips_allowed = 1,
da7f033d 3544 .suite = {
92a4c9fe 3545 .cipher = __VECS(cts_mode_tv_template)
da7f033d
HX
3546 }
3547 }, {
3548 .alg = "deflate",
3549 .test = alg_test_comp,
0818904d 3550 .fips_allowed = 1,
da7f033d
HX
3551 .suite = {
3552 .comp = {
21c8e720
AB
3553 .comp = __VECS(deflate_comp_tv_template),
3554 .decomp = __VECS(deflate_decomp_tv_template)
da7f033d
HX
3555 }
3556 }
802c7f1c
SB
3557 }, {
3558 .alg = "dh",
3559 .test = alg_test_kpp,
3560 .fips_allowed = 1,
3561 .suite = {
21c8e720 3562 .kpp = __VECS(dh_tv_template)
802c7f1c 3563 }
e448370d
JK
3564 }, {
3565 .alg = "digest_null",
3566 .test = alg_test_null,
64d1cdfb
SM
3567 }, {
3568 .alg = "drbg_nopr_ctr_aes128",
3569 .test = alg_test_drbg,
3570 .fips_allowed = 1,
3571 .suite = {
21c8e720 3572 .drbg = __VECS(drbg_nopr_ctr_aes128_tv_template)
64d1cdfb
SM
3573 }
3574 }, {
3575 .alg = "drbg_nopr_ctr_aes192",
3576 .test = alg_test_drbg,
3577 .fips_allowed = 1,
3578 .suite = {
21c8e720 3579 .drbg = __VECS(drbg_nopr_ctr_aes192_tv_template)
64d1cdfb
SM
3580 }
3581 }, {
3582 .alg = "drbg_nopr_ctr_aes256",
3583 .test = alg_test_drbg,
3584 .fips_allowed = 1,
3585 .suite = {
21c8e720 3586 .drbg = __VECS(drbg_nopr_ctr_aes256_tv_template)
64d1cdfb
SM
3587 }
3588 }, {
3589 /*
3590 * There is no need to specifically test the DRBG with every
3591 * backend cipher -- covered by drbg_nopr_hmac_sha256 test
3592 */
3593 .alg = "drbg_nopr_hmac_sha1",
3594 .fips_allowed = 1,
3595 .test = alg_test_null,
3596 }, {
3597 .alg = "drbg_nopr_hmac_sha256",
3598 .test = alg_test_drbg,
3599 .fips_allowed = 1,
3600 .suite = {
21c8e720 3601 .drbg = __VECS(drbg_nopr_hmac_sha256_tv_template)
64d1cdfb
SM
3602 }
3603 }, {
3604 /* covered by drbg_nopr_hmac_sha256 test */
3605 .alg = "drbg_nopr_hmac_sha384",
3606 .fips_allowed = 1,
3607 .test = alg_test_null,
3608 }, {
3609 .alg = "drbg_nopr_hmac_sha512",
3610 .test = alg_test_null,
3611 .fips_allowed = 1,
3612 }, {
3613 .alg = "drbg_nopr_sha1",
3614 .fips_allowed = 1,
3615 .test = alg_test_null,
3616 }, {
3617 .alg = "drbg_nopr_sha256",
3618 .test = alg_test_drbg,
3619 .fips_allowed = 1,
3620 .suite = {
21c8e720 3621 .drbg = __VECS(drbg_nopr_sha256_tv_template)
64d1cdfb
SM
3622 }
3623 }, {
3624 /* covered by drbg_nopr_sha256 test */
3625 .alg = "drbg_nopr_sha384",
3626 .fips_allowed = 1,
3627 .test = alg_test_null,
3628 }, {
3629 .alg = "drbg_nopr_sha512",
3630 .fips_allowed = 1,
3631 .test = alg_test_null,
3632 }, {
3633 .alg = "drbg_pr_ctr_aes128",
3634 .test = alg_test_drbg,
3635 .fips_allowed = 1,
3636 .suite = {
21c8e720 3637 .drbg = __VECS(drbg_pr_ctr_aes128_tv_template)
64d1cdfb
SM
3638 }
3639 }, {
3640 /* covered by drbg_pr_ctr_aes128 test */
3641 .alg = "drbg_pr_ctr_aes192",
3642 .fips_allowed = 1,
3643 .test = alg_test_null,
3644 }, {
3645 .alg = "drbg_pr_ctr_aes256",
3646 .fips_allowed = 1,
3647 .test = alg_test_null,
3648 }, {
3649 .alg = "drbg_pr_hmac_sha1",
3650 .fips_allowed = 1,
3651 .test = alg_test_null,
3652 }, {
3653 .alg = "drbg_pr_hmac_sha256",
3654 .test = alg_test_drbg,
3655 .fips_allowed = 1,
3656 .suite = {
21c8e720 3657 .drbg = __VECS(drbg_pr_hmac_sha256_tv_template)
64d1cdfb
SM
3658 }
3659 }, {
3660 /* covered by drbg_pr_hmac_sha256 test */
3661 .alg = "drbg_pr_hmac_sha384",
3662 .fips_allowed = 1,
3663 .test = alg_test_null,
3664 }, {
3665 .alg = "drbg_pr_hmac_sha512",
3666 .test = alg_test_null,
3667 .fips_allowed = 1,
3668 }, {
3669 .alg = "drbg_pr_sha1",
3670 .fips_allowed = 1,
3671 .test = alg_test_null,
3672 }, {
3673 .alg = "drbg_pr_sha256",
3674 .test = alg_test_drbg,
3675 .fips_allowed = 1,
3676 .suite = {
21c8e720 3677 .drbg = __VECS(drbg_pr_sha256_tv_template)
64d1cdfb
SM
3678 }
3679 }, {
3680 /* covered by drbg_pr_sha256 test */
3681 .alg = "drbg_pr_sha384",
3682 .fips_allowed = 1,
3683 .test = alg_test_null,
3684 }, {
3685 .alg = "drbg_pr_sha512",
3686 .fips_allowed = 1,
3687 .test = alg_test_null,
da7f033d
HX
3688 }, {
3689 .alg = "ecb(aes)",
1aa4ecd9 3690 .test = alg_test_skcipher,
a1915d51 3691 .fips_allowed = 1,
da7f033d 3692 .suite = {
92a4c9fe 3693 .cipher = __VECS(aes_tv_template)
da7f033d
HX
3694 }
3695 }, {
3696 .alg = "ecb(anubis)",
1aa4ecd9 3697 .test = alg_test_skcipher,
da7f033d 3698 .suite = {
92a4c9fe 3699 .cipher = __VECS(anubis_tv_template)
da7f033d
HX
3700 }
3701 }, {
3702 .alg = "ecb(arc4)",
1aa4ecd9 3703 .test = alg_test_skcipher,
da7f033d 3704 .suite = {
92a4c9fe 3705 .cipher = __VECS(arc4_tv_template)
da7f033d
HX
3706 }
3707 }, {
3708 .alg = "ecb(blowfish)",
1aa4ecd9 3709 .test = alg_test_skcipher,
da7f033d 3710 .suite = {
92a4c9fe 3711 .cipher = __VECS(bf_tv_template)
da7f033d
HX
3712 }
3713 }, {
3714 .alg = "ecb(camellia)",
1aa4ecd9 3715 .test = alg_test_skcipher,
da7f033d 3716 .suite = {
92a4c9fe 3717 .cipher = __VECS(camellia_tv_template)
da7f033d
HX
3718 }
3719 }, {
3720 .alg = "ecb(cast5)",
1aa4ecd9 3721 .test = alg_test_skcipher,
da7f033d 3722 .suite = {
92a4c9fe 3723 .cipher = __VECS(cast5_tv_template)
da7f033d
HX
3724 }
3725 }, {
3726 .alg = "ecb(cast6)",
1aa4ecd9 3727 .test = alg_test_skcipher,
da7f033d 3728 .suite = {
92a4c9fe 3729 .cipher = __VECS(cast6_tv_template)
da7f033d 3730 }
e448370d
JK
3731 }, {
3732 .alg = "ecb(cipher_null)",
3733 .test = alg_test_null,
6175ca2b 3734 .fips_allowed = 1,
da7f033d
HX
3735 }, {
3736 .alg = "ecb(des)",
1aa4ecd9 3737 .test = alg_test_skcipher,
da7f033d 3738 .suite = {
92a4c9fe 3739 .cipher = __VECS(des_tv_template)
da7f033d
HX
3740 }
3741 }, {
3742 .alg = "ecb(des3_ede)",
1aa4ecd9 3743 .test = alg_test_skcipher,
a1915d51 3744 .fips_allowed = 1,
da7f033d 3745 .suite = {
92a4c9fe 3746 .cipher = __VECS(des3_ede_tv_template)
da7f033d 3747 }
66e5bd00
JK
3748 }, {
3749 .alg = "ecb(fcrypt)",
3750 .test = alg_test_skcipher,
3751 .suite = {
3752 .cipher = {
92a4c9fe
EB
3753 .vecs = fcrypt_pcbc_tv_template,
3754 .count = 1
66e5bd00
JK
3755 }
3756 }
da7f033d
HX
3757 }, {
3758 .alg = "ecb(khazad)",
1aa4ecd9 3759 .test = alg_test_skcipher,
da7f033d 3760 .suite = {
92a4c9fe 3761 .cipher = __VECS(khazad_tv_template)
da7f033d 3762 }
15f47ce5
GBY
3763 }, {
3764 /* Same as ecb(aes) except the key is stored in
3765 * hardware secure memory which we reference by index
3766 */
3767 .alg = "ecb(paes)",
3768 .test = alg_test_null,
3769 .fips_allowed = 1,
da7f033d
HX
3770 }, {
3771 .alg = "ecb(seed)",
1aa4ecd9 3772 .test = alg_test_skcipher,
da7f033d 3773 .suite = {
92a4c9fe 3774 .cipher = __VECS(seed_tv_template)
da7f033d
HX
3775 }
3776 }, {
3777 .alg = "ecb(serpent)",
1aa4ecd9 3778 .test = alg_test_skcipher,
da7f033d 3779 .suite = {
92a4c9fe 3780 .cipher = __VECS(serpent_tv_template)
da7f033d 3781 }
cd83a8a7
GBY
3782 }, {
3783 .alg = "ecb(sm4)",
3784 .test = alg_test_skcipher,
3785 .suite = {
92a4c9fe 3786 .cipher = __VECS(sm4_tv_template)
cd83a8a7 3787 }
da7f033d
HX
3788 }, {
3789 .alg = "ecb(tea)",
1aa4ecd9 3790 .test = alg_test_skcipher,
da7f033d 3791 .suite = {
92a4c9fe 3792 .cipher = __VECS(tea_tv_template)
da7f033d
HX
3793 }
3794 }, {
3795 .alg = "ecb(tnepres)",
1aa4ecd9 3796 .test = alg_test_skcipher,
da7f033d 3797 .suite = {
92a4c9fe 3798 .cipher = __VECS(tnepres_tv_template)
da7f033d
HX
3799 }
3800 }, {
3801 .alg = "ecb(twofish)",
1aa4ecd9 3802 .test = alg_test_skcipher,
da7f033d 3803 .suite = {
92a4c9fe 3804 .cipher = __VECS(tf_tv_template)
da7f033d
HX
3805 }
3806 }, {
3807 .alg = "ecb(xeta)",
1aa4ecd9 3808 .test = alg_test_skcipher,
da7f033d 3809 .suite = {
92a4c9fe 3810 .cipher = __VECS(xeta_tv_template)
da7f033d
HX
3811 }
3812 }, {
3813 .alg = "ecb(xtea)",
1aa4ecd9 3814 .test = alg_test_skcipher,
da7f033d 3815 .suite = {
92a4c9fe 3816 .cipher = __VECS(xtea_tv_template)
da7f033d 3817 }
3c4b2390
SB
3818 }, {
3819 .alg = "ecdh",
3820 .test = alg_test_kpp,
3821 .fips_allowed = 1,
3822 .suite = {
21c8e720 3823 .kpp = __VECS(ecdh_tv_template)
3c4b2390 3824 }
32fbdbd3
VC
3825 }, {
3826 .alg = "ecrdsa",
3827 .test = alg_test_akcipher,
3828 .suite = {
3829 .akcipher = __VECS(ecrdsa_tv_template)
3830 }
da7f033d
HX
3831 }, {
3832 .alg = "gcm(aes)",
3833 .test = alg_test_aead,
a1915d51 3834 .fips_allowed = 1,
da7f033d 3835 .suite = {
a0d608ee 3836 .aead = __VECS(aes_gcm_tv_template)
da7f033d 3837 }
507069c9
YS
3838 }, {
3839 .alg = "ghash",
3840 .test = alg_test_hash,
18c0ebd2 3841 .fips_allowed = 1,
507069c9 3842 .suite = {
21c8e720 3843 .hash = __VECS(ghash_tv_template)
507069c9 3844 }
da7f033d
HX
3845 }, {
3846 .alg = "hmac(md5)",
3847 .test = alg_test_hash,
3848 .suite = {
21c8e720 3849 .hash = __VECS(hmac_md5_tv_template)
da7f033d
HX
3850 }
3851 }, {
3852 .alg = "hmac(rmd128)",
3853 .test = alg_test_hash,
3854 .suite = {
21c8e720 3855 .hash = __VECS(hmac_rmd128_tv_template)
da7f033d
HX
3856 }
3857 }, {
3858 .alg = "hmac(rmd160)",
3859 .test = alg_test_hash,
3860 .suite = {
21c8e720 3861 .hash = __VECS(hmac_rmd160_tv_template)
da7f033d
HX
3862 }
3863 }, {
3864 .alg = "hmac(sha1)",
3865 .test = alg_test_hash,
a1915d51 3866 .fips_allowed = 1,
da7f033d 3867 .suite = {
21c8e720 3868 .hash = __VECS(hmac_sha1_tv_template)
da7f033d
HX
3869 }
3870 }, {
3871 .alg = "hmac(sha224)",
3872 .test = alg_test_hash,
a1915d51 3873 .fips_allowed = 1,
da7f033d 3874 .suite = {
21c8e720 3875 .hash = __VECS(hmac_sha224_tv_template)
da7f033d
HX
3876 }
3877 }, {
3878 .alg = "hmac(sha256)",
3879 .test = alg_test_hash,
a1915d51 3880 .fips_allowed = 1,
da7f033d 3881 .suite = {
21c8e720 3882 .hash = __VECS(hmac_sha256_tv_template)
da7f033d 3883 }
98eca72f 3884 }, {
3885 .alg = "hmac(sha3-224)",
3886 .test = alg_test_hash,
3887 .fips_allowed = 1,
3888 .suite = {
21c8e720 3889 .hash = __VECS(hmac_sha3_224_tv_template)
98eca72f 3890 }
3891 }, {
3892 .alg = "hmac(sha3-256)",
3893 .test = alg_test_hash,
3894 .fips_allowed = 1,
3895 .suite = {
21c8e720 3896 .hash = __VECS(hmac_sha3_256_tv_template)
98eca72f 3897 }
3898 }, {
3899 .alg = "hmac(sha3-384)",
3900 .test = alg_test_hash,
3901 .fips_allowed = 1,
3902 .suite = {
21c8e720 3903 .hash = __VECS(hmac_sha3_384_tv_template)
98eca72f 3904 }
3905 }, {
3906 .alg = "hmac(sha3-512)",
3907 .test = alg_test_hash,
3908 .fips_allowed = 1,
3909 .suite = {
21c8e720 3910 .hash = __VECS(hmac_sha3_512_tv_template)
98eca72f 3911 }
da7f033d
HX
3912 }, {
3913 .alg = "hmac(sha384)",
3914 .test = alg_test_hash,
a1915d51 3915 .fips_allowed = 1,
da7f033d 3916 .suite = {
21c8e720 3917 .hash = __VECS(hmac_sha384_tv_template)
da7f033d
HX
3918 }
3919 }, {
3920 .alg = "hmac(sha512)",
3921 .test = alg_test_hash,
a1915d51 3922 .fips_allowed = 1,
da7f033d 3923 .suite = {
21c8e720 3924 .hash = __VECS(hmac_sha512_tv_template)
da7f033d 3925 }
25a0b9d4
VC
3926 }, {
3927 .alg = "hmac(streebog256)",
3928 .test = alg_test_hash,
3929 .suite = {
3930 .hash = __VECS(hmac_streebog256_tv_template)
3931 }
3932 }, {
3933 .alg = "hmac(streebog512)",
3934 .test = alg_test_hash,
3935 .suite = {
3936 .hash = __VECS(hmac_streebog512_tv_template)
3937 }
bb5530e4
SM
3938 }, {
3939 .alg = "jitterentropy_rng",
3940 .fips_allowed = 1,
3941 .test = alg_test_null,
35351988
SM
3942 }, {
3943 .alg = "kw(aes)",
3944 .test = alg_test_skcipher,
3945 .fips_allowed = 1,
3946 .suite = {
92a4c9fe 3947 .cipher = __VECS(aes_kw_tv_template)
35351988 3948 }
da7f033d
HX
3949 }, {
3950 .alg = "lrw(aes)",
1aa4ecd9 3951 .test = alg_test_skcipher,
da7f033d 3952 .suite = {
92a4c9fe 3953 .cipher = __VECS(aes_lrw_tv_template)
da7f033d 3954 }
0840605e
JK
3955 }, {
3956 .alg = "lrw(camellia)",
3957 .test = alg_test_skcipher,
3958 .suite = {
92a4c9fe 3959 .cipher = __VECS(camellia_lrw_tv_template)
0840605e 3960 }
9b8b0405
JG
3961 }, {
3962 .alg = "lrw(cast6)",
3963 .test = alg_test_skcipher,
3964 .suite = {
92a4c9fe 3965 .cipher = __VECS(cast6_lrw_tv_template)
9b8b0405 3966 }
d7bfc0fa
JK
3967 }, {
3968 .alg = "lrw(serpent)",
3969 .test = alg_test_skcipher,
3970 .suite = {
92a4c9fe 3971 .cipher = __VECS(serpent_lrw_tv_template)
d7bfc0fa 3972 }
0b2a1551
JK
3973 }, {
3974 .alg = "lrw(twofish)",
3975 .test = alg_test_skcipher,
3976 .suite = {
92a4c9fe 3977 .cipher = __VECS(tf_lrw_tv_template)
0b2a1551 3978 }
1443cc9b
KK
3979 }, {
3980 .alg = "lz4",
3981 .test = alg_test_comp,
3982 .fips_allowed = 1,
3983 .suite = {
3984 .comp = {
21c8e720
AB
3985 .comp = __VECS(lz4_comp_tv_template),
3986 .decomp = __VECS(lz4_decomp_tv_template)
1443cc9b
KK
3987 }
3988 }
3989 }, {
3990 .alg = "lz4hc",
3991 .test = alg_test_comp,
3992 .fips_allowed = 1,
3993 .suite = {
3994 .comp = {
21c8e720
AB
3995 .comp = __VECS(lz4hc_comp_tv_template),
3996 .decomp = __VECS(lz4hc_decomp_tv_template)
1443cc9b
KK
3997 }
3998 }
da7f033d
HX
3999 }, {
4000 .alg = "lzo",
4001 .test = alg_test_comp,
0818904d 4002 .fips_allowed = 1,
da7f033d
HX
4003 .suite = {
4004 .comp = {
21c8e720
AB
4005 .comp = __VECS(lzo_comp_tv_template),
4006 .decomp = __VECS(lzo_decomp_tv_template)
da7f033d
HX
4007 }
4008 }
4009 }, {
4010 .alg = "md4",
4011 .test = alg_test_hash,
4012 .suite = {
21c8e720 4013 .hash = __VECS(md4_tv_template)
da7f033d
HX
4014 }
4015 }, {
4016 .alg = "md5",
4017 .test = alg_test_hash,
4018 .suite = {
21c8e720 4019 .hash = __VECS(md5_tv_template)
da7f033d
HX
4020 }
4021 }, {
4022 .alg = "michael_mic",
4023 .test = alg_test_hash,
4024 .suite = {
21c8e720 4025 .hash = __VECS(michael_mic_tv_template)
da7f033d 4026 }
4feb4c59
OM
4027 }, {
4028 .alg = "morus1280",
4029 .test = alg_test_aead,
4030 .suite = {
a0d608ee 4031 .aead = __VECS(morus1280_tv_template)
4feb4c59
OM
4032 }
4033 }, {
4034 .alg = "morus640",
4035 .test = alg_test_aead,
4036 .suite = {
a0d608ee 4037 .aead = __VECS(morus640_tv_template)
4feb4c59 4038 }
26609a21
EB
4039 }, {
4040 .alg = "nhpoly1305",
4041 .test = alg_test_hash,
4042 .suite = {
4043 .hash = __VECS(nhpoly1305_tv_template)
4044 }
ba0e14ac
PS
4045 }, {
4046 .alg = "ofb(aes)",
4047 .test = alg_test_skcipher,
4048 .fips_allowed = 1,
4049 .suite = {
92a4c9fe 4050 .cipher = __VECS(aes_ofb_tv_template)
ba0e14ac 4051 }
a794d8d8
GBY
4052 }, {
4053 /* Same as ofb(aes) except the key is stored in
4054 * hardware secure memory which we reference by index
4055 */
4056 .alg = "ofb(paes)",
4057 .test = alg_test_null,
4058 .fips_allowed = 1,
da7f033d
HX
4059 }, {
4060 .alg = "pcbc(fcrypt)",
1aa4ecd9 4061 .test = alg_test_skcipher,
da7f033d 4062 .suite = {
92a4c9fe 4063 .cipher = __VECS(fcrypt_pcbc_tv_template)
da7f033d 4064 }
1207107c
SM
4065 }, {
4066 .alg = "pkcs1pad(rsa,sha224)",
4067 .test = alg_test_null,
4068 .fips_allowed = 1,
4069 }, {
4070 .alg = "pkcs1pad(rsa,sha256)",
4071 .test = alg_test_akcipher,
4072 .fips_allowed = 1,
4073 .suite = {
4074 .akcipher = __VECS(pkcs1pad_rsa_tv_template)
4075 }
4076 }, {
4077 .alg = "pkcs1pad(rsa,sha384)",
4078 .test = alg_test_null,
4079 .fips_allowed = 1,
4080 }, {
4081 .alg = "pkcs1pad(rsa,sha512)",
4082 .test = alg_test_null,
4083 .fips_allowed = 1,
eee9dc61
MW
4084 }, {
4085 .alg = "poly1305",
4086 .test = alg_test_hash,
4087 .suite = {
21c8e720 4088 .hash = __VECS(poly1305_tv_template)
eee9dc61 4089 }
da7f033d
HX
4090 }, {
4091 .alg = "rfc3686(ctr(aes))",
1aa4ecd9 4092 .test = alg_test_skcipher,
a1915d51 4093 .fips_allowed = 1,
da7f033d 4094 .suite = {
92a4c9fe 4095 .cipher = __VECS(aes_ctr_rfc3686_tv_template)
da7f033d 4096 }
5d667322 4097 }, {
3f31a740 4098 .alg = "rfc4106(gcm(aes))",
69435b94 4099 .test = alg_test_aead,
db71f29a 4100 .fips_allowed = 1,
69435b94 4101 .suite = {
a0d608ee 4102 .aead = __VECS(aes_gcm_rfc4106_tv_template)
69435b94
AH
4103 }
4104 }, {
544c436a 4105 .alg = "rfc4309(ccm(aes))",
5d667322 4106 .test = alg_test_aead,
a1915d51 4107 .fips_allowed = 1,
5d667322 4108 .suite = {
a0d608ee 4109 .aead = __VECS(aes_ccm_rfc4309_tv_template)
5d667322 4110 }
e9b7441a 4111 }, {
bb68745e 4112 .alg = "rfc4543(gcm(aes))",
e9b7441a
JK
4113 .test = alg_test_aead,
4114 .suite = {
a0d608ee 4115 .aead = __VECS(aes_gcm_rfc4543_tv_template)
e9b7441a 4116 }
af2b76b5
MW
4117 }, {
4118 .alg = "rfc7539(chacha20,poly1305)",
4119 .test = alg_test_aead,
4120 .suite = {
a0d608ee 4121 .aead = __VECS(rfc7539_tv_template)
af2b76b5 4122 }
5900758d
MW
4123 }, {
4124 .alg = "rfc7539esp(chacha20,poly1305)",
4125 .test = alg_test_aead,
4126 .suite = {
a0d608ee 4127 .aead = __VECS(rfc7539esp_tv_template)
5900758d 4128 }
da7f033d
HX
4129 }, {
4130 .alg = "rmd128",
4131 .test = alg_test_hash,
4132 .suite = {
21c8e720 4133 .hash = __VECS(rmd128_tv_template)
da7f033d
HX
4134 }
4135 }, {
4136 .alg = "rmd160",
4137 .test = alg_test_hash,
4138 .suite = {
21c8e720 4139 .hash = __VECS(rmd160_tv_template)
da7f033d
HX
4140 }
4141 }, {
4142 .alg = "rmd256",
4143 .test = alg_test_hash,
4144 .suite = {
21c8e720 4145 .hash = __VECS(rmd256_tv_template)
da7f033d
HX
4146 }
4147 }, {
4148 .alg = "rmd320",
4149 .test = alg_test_hash,
4150 .suite = {
21c8e720 4151 .hash = __VECS(rmd320_tv_template)
da7f033d 4152 }
946cc463
TS
4153 }, {
4154 .alg = "rsa",
4155 .test = alg_test_akcipher,
4156 .fips_allowed = 1,
4157 .suite = {
21c8e720 4158 .akcipher = __VECS(rsa_tv_template)
946cc463 4159 }
da7f033d
HX
4160 }, {
4161 .alg = "salsa20",
1aa4ecd9 4162 .test = alg_test_skcipher,
da7f033d 4163 .suite = {
92a4c9fe 4164 .cipher = __VECS(salsa20_stream_tv_template)
da7f033d
HX
4165 }
4166 }, {
4167 .alg = "sha1",
4168 .test = alg_test_hash,
a1915d51 4169 .fips_allowed = 1,
da7f033d 4170 .suite = {
21c8e720 4171 .hash = __VECS(sha1_tv_template)
da7f033d
HX
4172 }
4173 }, {
4174 .alg = "sha224",
4175 .test = alg_test_hash,
a1915d51 4176 .fips_allowed = 1,
da7f033d 4177 .suite = {
21c8e720 4178 .hash = __VECS(sha224_tv_template)
da7f033d
HX
4179 }
4180 }, {
4181 .alg = "sha256",
4182 .test = alg_test_hash,
a1915d51 4183 .fips_allowed = 1,
da7f033d 4184 .suite = {
21c8e720 4185 .hash = __VECS(sha256_tv_template)
da7f033d 4186 }
79cc6ab8 4187 }, {
4188 .alg = "sha3-224",
4189 .test = alg_test_hash,
4190 .fips_allowed = 1,
4191 .suite = {
21c8e720 4192 .hash = __VECS(sha3_224_tv_template)
79cc6ab8 4193 }
4194 }, {
4195 .alg = "sha3-256",
4196 .test = alg_test_hash,
4197 .fips_allowed = 1,
4198 .suite = {
21c8e720 4199 .hash = __VECS(sha3_256_tv_template)
79cc6ab8 4200 }
4201 }, {
4202 .alg = "sha3-384",
4203 .test = alg_test_hash,
4204 .fips_allowed = 1,
4205 .suite = {
21c8e720 4206 .hash = __VECS(sha3_384_tv_template)
79cc6ab8 4207 }
4208 }, {
4209 .alg = "sha3-512",
4210 .test = alg_test_hash,
4211 .fips_allowed = 1,
4212 .suite = {
21c8e720 4213 .hash = __VECS(sha3_512_tv_template)
79cc6ab8 4214 }
da7f033d
HX
4215 }, {
4216 .alg = "sha384",
4217 .test = alg_test_hash,
a1915d51 4218 .fips_allowed = 1,
da7f033d 4219 .suite = {
21c8e720 4220 .hash = __VECS(sha384_tv_template)
da7f033d
HX
4221 }
4222 }, {
4223 .alg = "sha512",
4224 .test = alg_test_hash,
a1915d51 4225 .fips_allowed = 1,
da7f033d 4226 .suite = {
21c8e720 4227 .hash = __VECS(sha512_tv_template)
da7f033d 4228 }
b7e27530
GBY
4229 }, {
4230 .alg = "sm3",
4231 .test = alg_test_hash,
4232 .suite = {
4233 .hash = __VECS(sm3_tv_template)
4234 }
25a0b9d4
VC
4235 }, {
4236 .alg = "streebog256",
4237 .test = alg_test_hash,
4238 .suite = {
4239 .hash = __VECS(streebog256_tv_template)
4240 }
4241 }, {
4242 .alg = "streebog512",
4243 .test = alg_test_hash,
4244 .suite = {
4245 .hash = __VECS(streebog512_tv_template)
4246 }
da7f033d
HX
4247 }, {
4248 .alg = "tgr128",
4249 .test = alg_test_hash,
4250 .suite = {
21c8e720 4251 .hash = __VECS(tgr128_tv_template)
da7f033d
HX
4252 }
4253 }, {
4254 .alg = "tgr160",
4255 .test = alg_test_hash,
4256 .suite = {
21c8e720 4257 .hash = __VECS(tgr160_tv_template)
da7f033d
HX
4258 }
4259 }, {
4260 .alg = "tgr192",
4261 .test = alg_test_hash,
4262 .suite = {
21c8e720 4263 .hash = __VECS(tgr192_tv_template)
da7f033d 4264 }
ed331ada
EB
4265 }, {
4266 .alg = "vmac64(aes)",
4267 .test = alg_test_hash,
4268 .suite = {
4269 .hash = __VECS(vmac64_aes_tv_template)
4270 }
da7f033d
HX
4271 }, {
4272 .alg = "wp256",
4273 .test = alg_test_hash,
4274 .suite = {
21c8e720 4275 .hash = __VECS(wp256_tv_template)
da7f033d
HX
4276 }
4277 }, {
4278 .alg = "wp384",
4279 .test = alg_test_hash,
4280 .suite = {
21c8e720 4281 .hash = __VECS(wp384_tv_template)
da7f033d
HX
4282 }
4283 }, {
4284 .alg = "wp512",
4285 .test = alg_test_hash,
4286 .suite = {
21c8e720 4287 .hash = __VECS(wp512_tv_template)
da7f033d
HX
4288 }
4289 }, {
4290 .alg = "xcbc(aes)",
4291 .test = alg_test_hash,
4292 .suite = {
21c8e720 4293 .hash = __VECS(aes_xcbc128_tv_template)
da7f033d 4294 }
aa762409
EB
4295 }, {
4296 .alg = "xchacha12",
4297 .test = alg_test_skcipher,
4298 .suite = {
4299 .cipher = __VECS(xchacha12_tv_template)
4300 },
de61d7ae
EB
4301 }, {
4302 .alg = "xchacha20",
4303 .test = alg_test_skcipher,
4304 .suite = {
4305 .cipher = __VECS(xchacha20_tv_template)
4306 },
da7f033d
HX
4307 }, {
4308 .alg = "xts(aes)",
1aa4ecd9 4309 .test = alg_test_skcipher,
2918aa8d 4310 .fips_allowed = 1,
da7f033d 4311 .suite = {
92a4c9fe 4312 .cipher = __VECS(aes_xts_tv_template)
da7f033d 4313 }
0840605e
JK
4314 }, {
4315 .alg = "xts(camellia)",
4316 .test = alg_test_skcipher,
4317 .suite = {
92a4c9fe 4318 .cipher = __VECS(camellia_xts_tv_template)
0840605e 4319 }
9b8b0405
JG
4320 }, {
4321 .alg = "xts(cast6)",
4322 .test = alg_test_skcipher,
4323 .suite = {
92a4c9fe 4324 .cipher = __VECS(cast6_xts_tv_template)
9b8b0405 4325 }
15f47ce5
GBY
4326 }, {
4327 /* Same as xts(aes) except the key is stored in
4328 * hardware secure memory which we reference by index
4329 */
4330 .alg = "xts(paes)",
4331 .test = alg_test_null,
4332 .fips_allowed = 1,
18be20b9
JK
4333 }, {
4334 .alg = "xts(serpent)",
4335 .test = alg_test_skcipher,
4336 .suite = {
92a4c9fe 4337 .cipher = __VECS(serpent_xts_tv_template)
18be20b9 4338 }
aed265b9
JK
4339 }, {
4340 .alg = "xts(twofish)",
4341 .test = alg_test_skcipher,
4342 .suite = {
92a4c9fe 4343 .cipher = __VECS(tf_xts_tv_template)
aed265b9 4344 }
15f47ce5
GBY
4345 }, {
4346 .alg = "xts4096(paes)",
4347 .test = alg_test_null,
4348 .fips_allowed = 1,
4349 }, {
4350 .alg = "xts512(paes)",
4351 .test = alg_test_null,
4352 .fips_allowed = 1,
a368f43d
GC
4353 }, {
4354 .alg = "zlib-deflate",
4355 .test = alg_test_comp,
4356 .fips_allowed = 1,
4357 .suite = {
4358 .comp = {
4359 .comp = __VECS(zlib_deflate_comp_tv_template),
4360 .decomp = __VECS(zlib_deflate_decomp_tv_template)
4361 }
4362 }
d28fc3db
NT
4363 }, {
4364 .alg = "zstd",
4365 .test = alg_test_comp,
4366 .fips_allowed = 1,
4367 .suite = {
4368 .comp = {
4369 .comp = __VECS(zstd_comp_tv_template),
4370 .decomp = __VECS(zstd_decomp_tv_template)
4371 }
4372 }
da7f033d
HX
4373 }
4374};
4375
3f47a03d 4376static void alg_check_test_descs_order(void)
5714758b
JK
4377{
4378 int i;
4379
5714758b
JK
4380 for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
4381 int diff = strcmp(alg_test_descs[i - 1].alg,
4382 alg_test_descs[i].alg);
4383
4384 if (WARN_ON(diff > 0)) {
4385 pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
4386 alg_test_descs[i - 1].alg,
4387 alg_test_descs[i].alg);
4388 }
4389
4390 if (WARN_ON(diff == 0)) {
4391 pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
4392 alg_test_descs[i].alg);
4393 }
4394 }
4395}
4396
3f47a03d
EB
4397static void alg_check_testvec_configs(void)
4398{
4e7babba
EB
4399 int i;
4400
4401 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++)
4402 WARN_ON(!valid_testvec_config(
4403 &default_cipher_testvec_configs[i]));
4cc2dcf9
EB
4404
4405 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++)
4406 WARN_ON(!valid_testvec_config(
4407 &default_hash_testvec_configs[i]));
3f47a03d
EB
4408}
4409
4410static void testmgr_onetime_init(void)
4411{
4412 alg_check_test_descs_order();
4413 alg_check_testvec_configs();
5b2706a4
EB
4414
4415#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
4416 pr_warn("alg: extra crypto tests enabled. This is intended for developer use only.\n");
4417#endif
3f47a03d
EB
4418}
4419
1aa4ecd9 4420static int alg_find_test(const char *alg)
da7f033d
HX
4421{
4422 int start = 0;
4423 int end = ARRAY_SIZE(alg_test_descs);
4424
4425 while (start < end) {
4426 int i = (start + end) / 2;
4427 int diff = strcmp(alg_test_descs[i].alg, alg);
4428
4429 if (diff > 0) {
4430 end = i;
4431 continue;
4432 }
4433
4434 if (diff < 0) {
4435 start = i + 1;
4436 continue;
4437 }
4438
1aa4ecd9
HX
4439 return i;
4440 }
4441
4442 return -1;
4443}
4444
4445int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
4446{
4447 int i;
a68f6610 4448 int j;
d12d6b6d 4449 int rc;
1aa4ecd9 4450
9e5c9fe4
RJ
4451 if (!fips_enabled && notests) {
4452 printk_once(KERN_INFO "alg: self-tests disabled\n");
4453 return 0;
4454 }
4455
3f47a03d 4456 DO_ONCE(testmgr_onetime_init);
5714758b 4457
1aa4ecd9
HX
4458 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
4459 char nalg[CRYPTO_MAX_ALG_NAME];
4460
4461 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
4462 sizeof(nalg))
4463 return -ENAMETOOLONG;
4464
4465 i = alg_find_test(nalg);
4466 if (i < 0)
4467 goto notest;
4468
a3bef3a3
JW
4469 if (fips_enabled && !alg_test_descs[i].fips_allowed)
4470 goto non_fips_alg;
4471
941fb328
JW
4472 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
4473 goto test_done;
da7f033d
HX
4474 }
4475
1aa4ecd9 4476 i = alg_find_test(alg);
a68f6610
HX
4477 j = alg_find_test(driver);
4478 if (i < 0 && j < 0)
1aa4ecd9
HX
4479 goto notest;
4480
a68f6610
HX
4481 if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
4482 (j >= 0 && !alg_test_descs[j].fips_allowed)))
a3bef3a3
JW
4483 goto non_fips_alg;
4484
a68f6610
HX
4485 rc = 0;
4486 if (i >= 0)
4487 rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
4488 type, mask);
032c8cac 4489 if (j >= 0 && j != i)
a68f6610
HX
4490 rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
4491 type, mask);
4492
941fb328 4493test_done:
eda69b0c
EB
4494 if (rc && (fips_enabled || panic_on_fail))
4495 panic("alg: self-tests for %s (%s) failed in %s mode!\n",
4496 driver, alg, fips_enabled ? "fips" : "panic_on_fail");
d12d6b6d 4497
29ecd4ab 4498 if (fips_enabled && !rc)
3e8cffd4 4499 pr_info("alg: self-tests for %s (%s) passed\n", driver, alg);
29ecd4ab 4500
d12d6b6d 4501 return rc;
1aa4ecd9
HX
4502
4503notest:
da7f033d
HX
4504 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
4505 return 0;
a3bef3a3
JW
4506non_fips_alg:
4507 return -EINVAL;
da7f033d 4508}
0b767f96 4509
326a6346 4510#endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
0b767f96 4511
da7f033d 4512EXPORT_SYMBOL_GPL(alg_test);
This page took 1.074805 seconds and 4 git commands to generate.