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