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