projects/linux·blob assoc-array-fixes · refs

5722 lines · 148.0 KiB

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