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