blob: bc382b0c0ac6f0b3ddbc452b39b387f5a3deba57 [file] [log] [blame]
Herbert Xuda7f0332008-07-31 17:08:25 +08001/*
2 * Algorithm testing framework and tests.
3 *
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
6 * Copyright (c) 2007 Nokia Siemens Networks
7 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
Eric Biggers3f47a032019-01-31 23:51:43 -08008 * Copyright (c) 2019 Google LLC
Herbert Xuda7f0332008-07-31 17:08:25 +08009 *
Adrian Hoban69435b92010-11-04 15:02:04 -040010 * Updated RFC4106 AES-GCM testing.
11 * Authors: Aidan O'Mahony (aidan.o.mahony@intel.com)
12 * Adrian Hoban <adrian.hoban@intel.com>
13 * Gabriele Paoloni <gabriele.paoloni@intel.com>
14 * Tadeusz Struk (tadeusz.struk@intel.com)
15 * Copyright (c) 2010, Intel Corporation.
16 *
Herbert Xuda7f0332008-07-31 17:08:25 +080017 * 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
Herbert Xu1ce33112015-04-22 15:06:31 +080024#include <crypto/aead.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080025#include <crypto/hash.h>
Herbert Xu12773d92015-08-20 15:21:46 +080026#include <crypto/skcipher.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080027#include <linux/err.h>
Herbert Xu1c41b882015-04-22 13:25:58 +080028#include <linux/fips.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080029#include <linux/module.h>
Eric Biggers3f47a032019-01-31 23:51:43 -080030#include <linux/once.h>
Eric Biggers25f9ddd2019-01-31 23:51:45 -080031#include <linux/random.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080032#include <linux/scatterlist.h>
33#include <linux/slab.h>
34#include <linux/string.h>
Jarod Wilson7647d6c2009-05-04 19:44:50 +080035#include <crypto/rng.h>
Stephan Mueller64d1cdf2014-05-31 17:25:36 +020036#include <crypto/drbg.h>
Tadeusz Struk946cc462015-06-16 10:31:06 -070037#include <crypto/akcipher.h>
Salvatore Benedetto802c7f12016-06-22 17:49:14 +010038#include <crypto/kpp.h>
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +010039#include <crypto/acompress.h>
Eric Biggersb55e1a32019-03-12 22:12:47 -070040#include <crypto/internal/simd.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080041
42#include "internal.h"
Alexander Shishkin0b767f92010-06-03 20:53:43 +100043
Richard W.M. Jones9e5c9fe2016-05-03 10:00:17 +010044static bool notests;
45module_param(notests, bool, 0644);
46MODULE_PARM_DESC(notests, "disable crypto self-tests");
47
Eric Biggerseda69b02019-03-31 13:09:14 -070048static bool panic_on_fail;
49module_param(panic_on_fail, bool, 0444);
50
Eric Biggers5b2706a2019-01-31 23:51:44 -080051#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
52static bool noextratests;
53module_param(noextratests, bool, 0644);
54MODULE_PARM_DESC(noextratests, "disable expensive crypto self-tests");
55
56static unsigned int fuzz_iterations = 100;
57module_param(fuzz_iterations, uint, 0644);
58MODULE_PARM_DESC(fuzz_iterations, "number of fuzz test iterations");
Eric Biggersb55e1a32019-03-12 22:12:47 -070059
60DEFINE_PER_CPU(bool, crypto_simd_disabled_for_test);
61EXPORT_PER_CPU_SYMBOL_GPL(crypto_simd_disabled_for_test);
Eric Biggers5b2706a2019-01-31 23:51:44 -080062#endif
63
Herbert Xu326a6342010-08-06 09:40:28 +080064#ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
Alexander Shishkin0b767f92010-06-03 20:53:43 +100065
66/* a perfect nop */
67int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
68{
69 return 0;
70}
71
72#else
73
Herbert Xuda7f0332008-07-31 17:08:25 +080074#include "testmgr.h"
75
76/*
77 * Need slab memory for testing (size in number of pages).
78 */
79#define XBUFSIZE 8
80
81/*
Herbert Xuda7f0332008-07-31 17:08:25 +080082* Used by test_cipher()
83*/
84#define ENCRYPT 1
85#define DECRYPT 0
86
Herbert Xuda7f0332008-07-31 17:08:25 +080087struct aead_test_suite {
Eric Biggersa0d608ee2019-01-13 15:32:28 -080088 const struct aead_testvec *vecs;
89 unsigned int count;
Herbert Xuda7f0332008-07-31 17:08:25 +080090};
91
92struct cipher_test_suite {
Eric Biggers92a4c9f2018-05-20 22:50:29 -070093 const struct cipher_testvec *vecs;
94 unsigned int count;
Herbert Xuda7f0332008-07-31 17:08:25 +080095};
96
97struct comp_test_suite {
98 struct {
Eric Biggersb13b1e02017-02-24 15:46:59 -080099 const struct comp_testvec *vecs;
Herbert Xuda7f0332008-07-31 17:08:25 +0800100 unsigned int count;
101 } comp, decomp;
102};
103
104struct hash_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800105 const struct hash_testvec *vecs;
Herbert Xuda7f0332008-07-31 17:08:25 +0800106 unsigned int count;
107};
108
Jarod Wilson7647d6c2009-05-04 19:44:50 +0800109struct cprng_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800110 const struct cprng_testvec *vecs;
Jarod Wilson7647d6c2009-05-04 19:44:50 +0800111 unsigned int count;
112};
113
Stephan Mueller64d1cdf2014-05-31 17:25:36 +0200114struct drbg_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800115 const struct drbg_testvec *vecs;
Stephan Mueller64d1cdf2014-05-31 17:25:36 +0200116 unsigned int count;
117};
118
Tadeusz Struk946cc462015-06-16 10:31:06 -0700119struct akcipher_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800120 const struct akcipher_testvec *vecs;
Tadeusz Struk946cc462015-06-16 10:31:06 -0700121 unsigned int count;
122};
123
Salvatore Benedetto802c7f12016-06-22 17:49:14 +0100124struct kpp_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800125 const struct kpp_testvec *vecs;
Salvatore Benedetto802c7f12016-06-22 17:49:14 +0100126 unsigned int count;
127};
128
Herbert Xuda7f0332008-07-31 17:08:25 +0800129struct alg_test_desc {
130 const char *alg;
131 int (*test)(const struct alg_test_desc *desc, const char *driver,
132 u32 type, u32 mask);
Jarod Wilsona1915d52009-05-15 15:16:03 +1000133 int fips_allowed; /* set if alg is allowed in fips mode */
Herbert Xuda7f0332008-07-31 17:08:25 +0800134
135 union {
136 struct aead_test_suite aead;
137 struct cipher_test_suite cipher;
138 struct comp_test_suite comp;
139 struct hash_test_suite hash;
Jarod Wilson7647d6c2009-05-04 19:44:50 +0800140 struct cprng_test_suite cprng;
Stephan Mueller64d1cdf2014-05-31 17:25:36 +0200141 struct drbg_test_suite drbg;
Tadeusz Struk946cc462015-06-16 10:31:06 -0700142 struct akcipher_test_suite akcipher;
Salvatore Benedetto802c7f12016-06-22 17:49:14 +0100143 struct kpp_test_suite kpp;
Herbert Xuda7f0332008-07-31 17:08:25 +0800144 } suite;
145};
146
Herbert Xuda7f0332008-07-31 17:08:25 +0800147static void hexdump(unsigned char *buf, unsigned int len)
148{
149 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
150 16, 1,
151 buf, len, false);
152}
153
Eric Biggers3f47a032019-01-31 23:51:43 -0800154static int __testmgr_alloc_buf(char *buf[XBUFSIZE], int order)
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800155{
156 int i;
157
158 for (i = 0; i < XBUFSIZE; i++) {
Eric Biggers3f47a032019-01-31 23:51:43 -0800159 buf[i] = (char *)__get_free_pages(GFP_KERNEL, order);
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800160 if (!buf[i])
161 goto err_free_buf;
162 }
163
164 return 0;
165
166err_free_buf:
167 while (i-- > 0)
Eric Biggers3f47a032019-01-31 23:51:43 -0800168 free_pages((unsigned long)buf[i], order);
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800169
170 return -ENOMEM;
171}
172
Eric Biggers3f47a032019-01-31 23:51:43 -0800173static int testmgr_alloc_buf(char *buf[XBUFSIZE])
174{
175 return __testmgr_alloc_buf(buf, 0);
176}
177
178static void __testmgr_free_buf(char *buf[XBUFSIZE], int order)
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800179{
180 int i;
181
182 for (i = 0; i < XBUFSIZE; i++)
Eric Biggers3f47a032019-01-31 23:51:43 -0800183 free_pages((unsigned long)buf[i], order);
184}
185
186static void testmgr_free_buf(char *buf[XBUFSIZE])
187{
188 __testmgr_free_buf(buf, 0);
189}
190
191#define TESTMGR_POISON_BYTE 0xfe
192#define TESTMGR_POISON_LEN 16
193
194static inline void testmgr_poison(void *addr, size_t len)
195{
196 memset(addr, TESTMGR_POISON_BYTE, len);
197}
198
199/* Is the memory region still fully poisoned? */
200static inline bool testmgr_is_poison(const void *addr, size_t len)
201{
202 return memchr_inv(addr, TESTMGR_POISON_BYTE, len) == NULL;
203}
204
205/* flush type for hash algorithms */
206enum flush_type {
207 /* merge with update of previous buffer(s) */
208 FLUSH_TYPE_NONE = 0,
209
210 /* update with previous buffer(s) before doing this one */
211 FLUSH_TYPE_FLUSH,
212
213 /* likewise, but also export and re-import the intermediate state */
214 FLUSH_TYPE_REIMPORT,
215};
216
217/* finalization function for hash algorithms */
218enum finalization_type {
219 FINALIZATION_TYPE_FINAL, /* use final() */
220 FINALIZATION_TYPE_FINUP, /* use finup() */
221 FINALIZATION_TYPE_DIGEST, /* use digest() */
222};
223
224#define TEST_SG_TOTAL 10000
225
226/**
227 * struct test_sg_division - description of a scatterlist entry
228 *
229 * This struct describes one entry of a scatterlist being constructed to check a
230 * crypto test vector.
231 *
232 * @proportion_of_total: length of this chunk relative to the total length,
233 * given as a proportion out of TEST_SG_TOTAL so that it
234 * scales to fit any test vector
235 * @offset: byte offset into a 2-page buffer at which this chunk will start
236 * @offset_relative_to_alignmask: if true, add the algorithm's alignmask to the
237 * @offset
238 * @flush_type: for hashes, whether an update() should be done now vs.
239 * continuing to accumulate data
Eric Biggers65707372019-03-12 22:12:52 -0700240 * @nosimd: if doing the pending update(), do it with SIMD disabled?
Eric Biggers3f47a032019-01-31 23:51:43 -0800241 */
242struct test_sg_division {
243 unsigned int proportion_of_total;
244 unsigned int offset;
245 bool offset_relative_to_alignmask;
246 enum flush_type flush_type;
Eric Biggers65707372019-03-12 22:12:52 -0700247 bool nosimd;
Eric Biggers3f47a032019-01-31 23:51:43 -0800248};
249
250/**
251 * struct testvec_config - configuration for testing a crypto test vector
252 *
253 * This struct describes the data layout and other parameters with which each
254 * crypto test vector can be tested.
255 *
256 * @name: name of this config, logged for debugging purposes if a test fails
257 * @inplace: operate on the data in-place, if applicable for the algorithm type?
258 * @req_flags: extra request_flags, e.g. CRYPTO_TFM_REQ_MAY_SLEEP
259 * @src_divs: description of how to arrange the source scatterlist
260 * @dst_divs: description of how to arrange the dst scatterlist, if applicable
261 * for the algorithm type. Defaults to @src_divs if unset.
262 * @iv_offset: misalignment of the IV in the range [0..MAX_ALGAPI_ALIGNMASK+1],
263 * where 0 is aligned to a 2*(MAX_ALGAPI_ALIGNMASK+1) byte boundary
264 * @iv_offset_relative_to_alignmask: if true, add the algorithm's alignmask to
265 * the @iv_offset
266 * @finalization_type: what finalization function to use for hashes
Eric Biggers65707372019-03-12 22:12:52 -0700267 * @nosimd: execute with SIMD disabled? Requires !CRYPTO_TFM_REQ_MAY_SLEEP.
Eric Biggers3f47a032019-01-31 23:51:43 -0800268 */
269struct testvec_config {
270 const char *name;
271 bool inplace;
272 u32 req_flags;
273 struct test_sg_division src_divs[XBUFSIZE];
274 struct test_sg_division dst_divs[XBUFSIZE];
275 unsigned int iv_offset;
276 bool iv_offset_relative_to_alignmask;
277 enum finalization_type finalization_type;
Eric Biggers65707372019-03-12 22:12:52 -0700278 bool nosimd;
Eric Biggers3f47a032019-01-31 23:51:43 -0800279};
280
281#define TESTVEC_CONFIG_NAMELEN 192
282
Eric Biggers4e7babba2019-01-31 23:51:46 -0800283/*
284 * The following are the lists of testvec_configs to test for each algorithm
285 * type when the basic crypto self-tests are enabled, i.e. when
286 * CONFIG_CRYPTO_MANAGER_DISABLE_TESTS is unset. They aim to provide good test
287 * coverage, while keeping the test time much shorter than the full fuzz tests
288 * so that the basic tests can be enabled in a wider range of circumstances.
289 */
290
291/* Configs for skciphers and aeads */
292static const struct testvec_config default_cipher_testvec_configs[] = {
293 {
294 .name = "in-place",
295 .inplace = true,
296 .src_divs = { { .proportion_of_total = 10000 } },
297 }, {
298 .name = "out-of-place",
299 .src_divs = { { .proportion_of_total = 10000 } },
300 }, {
301 .name = "unaligned buffer, offset=1",
302 .src_divs = { { .proportion_of_total = 10000, .offset = 1 } },
303 .iv_offset = 1,
304 }, {
305 .name = "buffer aligned only to alignmask",
306 .src_divs = {
307 {
308 .proportion_of_total = 10000,
309 .offset = 1,
310 .offset_relative_to_alignmask = true,
311 },
312 },
313 .iv_offset = 1,
314 .iv_offset_relative_to_alignmask = true,
315 }, {
316 .name = "two even aligned splits",
317 .src_divs = {
318 { .proportion_of_total = 5000 },
319 { .proportion_of_total = 5000 },
320 },
321 }, {
322 .name = "uneven misaligned splits, may sleep",
323 .req_flags = CRYPTO_TFM_REQ_MAY_SLEEP,
324 .src_divs = {
325 { .proportion_of_total = 1900, .offset = 33 },
326 { .proportion_of_total = 3300, .offset = 7 },
327 { .proportion_of_total = 4800, .offset = 18 },
328 },
329 .iv_offset = 3,
330 }, {
331 .name = "misaligned splits crossing pages, inplace",
332 .inplace = true,
333 .src_divs = {
334 {
335 .proportion_of_total = 7500,
336 .offset = PAGE_SIZE - 32
337 }, {
338 .proportion_of_total = 2500,
339 .offset = PAGE_SIZE - 7
340 },
341 },
342 }
343};
344
Eric Biggers4cc2dcf2019-01-31 23:51:48 -0800345static const struct testvec_config default_hash_testvec_configs[] = {
346 {
347 .name = "init+update+final aligned buffer",
348 .src_divs = { { .proportion_of_total = 10000 } },
349 .finalization_type = FINALIZATION_TYPE_FINAL,
350 }, {
351 .name = "init+finup aligned buffer",
352 .src_divs = { { .proportion_of_total = 10000 } },
353 .finalization_type = FINALIZATION_TYPE_FINUP,
354 }, {
355 .name = "digest aligned buffer",
356 .src_divs = { { .proportion_of_total = 10000 } },
357 .finalization_type = FINALIZATION_TYPE_DIGEST,
358 }, {
359 .name = "init+update+final misaligned buffer",
360 .src_divs = { { .proportion_of_total = 10000, .offset = 1 } },
361 .finalization_type = FINALIZATION_TYPE_FINAL,
362 }, {
363 .name = "digest buffer aligned only to alignmask",
364 .src_divs = {
365 {
366 .proportion_of_total = 10000,
367 .offset = 1,
368 .offset_relative_to_alignmask = true,
369 },
370 },
371 .finalization_type = FINALIZATION_TYPE_DIGEST,
372 }, {
373 .name = "init+update+update+final two even splits",
374 .src_divs = {
375 { .proportion_of_total = 5000 },
376 {
377 .proportion_of_total = 5000,
378 .flush_type = FLUSH_TYPE_FLUSH,
379 },
380 },
381 .finalization_type = FINALIZATION_TYPE_FINAL,
382 }, {
383 .name = "digest uneven misaligned splits, may sleep",
384 .req_flags = CRYPTO_TFM_REQ_MAY_SLEEP,
385 .src_divs = {
386 { .proportion_of_total = 1900, .offset = 33 },
387 { .proportion_of_total = 3300, .offset = 7 },
388 { .proportion_of_total = 4800, .offset = 18 },
389 },
390 .finalization_type = FINALIZATION_TYPE_DIGEST,
391 }, {
392 .name = "digest misaligned splits crossing pages",
393 .src_divs = {
394 {
395 .proportion_of_total = 7500,
396 .offset = PAGE_SIZE - 32,
397 }, {
398 .proportion_of_total = 2500,
399 .offset = PAGE_SIZE - 7,
400 },
401 },
402 .finalization_type = FINALIZATION_TYPE_DIGEST,
403 }, {
404 .name = "import/export",
405 .src_divs = {
406 {
407 .proportion_of_total = 6500,
408 .flush_type = FLUSH_TYPE_REIMPORT,
409 }, {
410 .proportion_of_total = 3500,
411 .flush_type = FLUSH_TYPE_REIMPORT,
412 },
413 },
414 .finalization_type = FINALIZATION_TYPE_FINAL,
415 }
416};
417
Eric Biggers3f47a032019-01-31 23:51:43 -0800418static unsigned int count_test_sg_divisions(const struct test_sg_division *divs)
419{
420 unsigned int remaining = TEST_SG_TOTAL;
421 unsigned int ndivs = 0;
422
423 do {
424 remaining -= divs[ndivs++].proportion_of_total;
425 } while (remaining);
426
427 return ndivs;
428}
429
Eric Biggers65707372019-03-12 22:12:52 -0700430#define SGDIVS_HAVE_FLUSHES BIT(0)
431#define SGDIVS_HAVE_NOSIMD BIT(1)
432
Eric Biggers3f47a032019-01-31 23:51:43 -0800433static bool valid_sg_divisions(const struct test_sg_division *divs,
Eric Biggers65707372019-03-12 22:12:52 -0700434 unsigned int count, int *flags_ret)
Eric Biggers3f47a032019-01-31 23:51:43 -0800435{
436 unsigned int total = 0;
437 unsigned int i;
438
439 for (i = 0; i < count && total != TEST_SG_TOTAL; i++) {
440 if (divs[i].proportion_of_total <= 0 ||
441 divs[i].proportion_of_total > TEST_SG_TOTAL - total)
442 return false;
443 total += divs[i].proportion_of_total;
444 if (divs[i].flush_type != FLUSH_TYPE_NONE)
Eric Biggers65707372019-03-12 22:12:52 -0700445 *flags_ret |= SGDIVS_HAVE_FLUSHES;
446 if (divs[i].nosimd)
447 *flags_ret |= SGDIVS_HAVE_NOSIMD;
Eric Biggers3f47a032019-01-31 23:51:43 -0800448 }
449 return total == TEST_SG_TOTAL &&
450 memchr_inv(&divs[i], 0, (count - i) * sizeof(divs[0])) == NULL;
451}
452
453/*
454 * Check whether the given testvec_config is valid. This isn't strictly needed
455 * since every testvec_config should be valid, but check anyway so that people
456 * don't unknowingly add broken configs that don't do what they wanted.
457 */
458static bool valid_testvec_config(const struct testvec_config *cfg)
459{
Eric Biggers65707372019-03-12 22:12:52 -0700460 int flags = 0;
Eric Biggers3f47a032019-01-31 23:51:43 -0800461
462 if (cfg->name == NULL)
463 return false;
464
465 if (!valid_sg_divisions(cfg->src_divs, ARRAY_SIZE(cfg->src_divs),
Eric Biggers65707372019-03-12 22:12:52 -0700466 &flags))
Eric Biggers3f47a032019-01-31 23:51:43 -0800467 return false;
468
469 if (cfg->dst_divs[0].proportion_of_total) {
470 if (!valid_sg_divisions(cfg->dst_divs,
Eric Biggers65707372019-03-12 22:12:52 -0700471 ARRAY_SIZE(cfg->dst_divs), &flags))
Eric Biggers3f47a032019-01-31 23:51:43 -0800472 return false;
473 } else {
474 if (memchr_inv(cfg->dst_divs, 0, sizeof(cfg->dst_divs)))
475 return false;
476 /* defaults to dst_divs=src_divs */
477 }
478
479 if (cfg->iv_offset +
480 (cfg->iv_offset_relative_to_alignmask ? MAX_ALGAPI_ALIGNMASK : 0) >
481 MAX_ALGAPI_ALIGNMASK + 1)
482 return false;
483
Eric Biggers65707372019-03-12 22:12:52 -0700484 if ((flags & (SGDIVS_HAVE_FLUSHES | SGDIVS_HAVE_NOSIMD)) &&
485 cfg->finalization_type == FINALIZATION_TYPE_DIGEST)
486 return false;
487
488 if ((cfg->nosimd || (flags & SGDIVS_HAVE_NOSIMD)) &&
489 (cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP))
Eric Biggers3f47a032019-01-31 23:51:43 -0800490 return false;
491
492 return true;
493}
494
495struct test_sglist {
496 char *bufs[XBUFSIZE];
497 struct scatterlist sgl[XBUFSIZE];
498 struct scatterlist sgl_saved[XBUFSIZE];
499 struct scatterlist *sgl_ptr;
500 unsigned int nents;
501};
502
503static int init_test_sglist(struct test_sglist *tsgl)
504{
505 return __testmgr_alloc_buf(tsgl->bufs, 1 /* two pages per buffer */);
506}
507
508static void destroy_test_sglist(struct test_sglist *tsgl)
509{
510 return __testmgr_free_buf(tsgl->bufs, 1 /* two pages per buffer */);
511}
512
513/**
514 * build_test_sglist() - build a scatterlist for a crypto test
515 *
516 * @tsgl: the scatterlist to build. @tsgl->bufs[] contains an array of 2-page
517 * buffers which the scatterlist @tsgl->sgl[] will be made to point into.
518 * @divs: the layout specification on which the scatterlist will be based
519 * @alignmask: the algorithm's alignmask
520 * @total_len: the total length of the scatterlist to build in bytes
521 * @data: if non-NULL, the buffers will be filled with this data until it ends.
522 * Otherwise the buffers will be poisoned. In both cases, some bytes
523 * past the end of each buffer will be poisoned to help detect overruns.
524 * @out_divs: if non-NULL, the test_sg_division to which each scatterlist entry
525 * corresponds will be returned here. This will match @divs except
526 * that divisions resolving to a length of 0 are omitted as they are
527 * not included in the scatterlist.
528 *
529 * Return: 0 or a -errno value
530 */
531static int build_test_sglist(struct test_sglist *tsgl,
532 const struct test_sg_division *divs,
533 const unsigned int alignmask,
534 const unsigned int total_len,
535 struct iov_iter *data,
536 const struct test_sg_division *out_divs[XBUFSIZE])
537{
538 struct {
539 const struct test_sg_division *div;
540 size_t length;
541 } partitions[XBUFSIZE];
542 const unsigned int ndivs = count_test_sg_divisions(divs);
543 unsigned int len_remaining = total_len;
544 unsigned int i;
545
546 BUILD_BUG_ON(ARRAY_SIZE(partitions) != ARRAY_SIZE(tsgl->sgl));
547 if (WARN_ON(ndivs > ARRAY_SIZE(partitions)))
548 return -EINVAL;
549
550 /* Calculate the (div, length) pairs */
551 tsgl->nents = 0;
552 for (i = 0; i < ndivs; i++) {
553 unsigned int len_this_sg =
554 min(len_remaining,
555 (total_len * divs[i].proportion_of_total +
556 TEST_SG_TOTAL / 2) / TEST_SG_TOTAL);
557
558 if (len_this_sg != 0) {
559 partitions[tsgl->nents].div = &divs[i];
560 partitions[tsgl->nents].length = len_this_sg;
561 tsgl->nents++;
562 len_remaining -= len_this_sg;
563 }
564 }
565 if (tsgl->nents == 0) {
566 partitions[tsgl->nents].div = &divs[0];
567 partitions[tsgl->nents].length = 0;
568 tsgl->nents++;
569 }
570 partitions[tsgl->nents - 1].length += len_remaining;
571
572 /* Set up the sgl entries and fill the data or poison */
573 sg_init_table(tsgl->sgl, tsgl->nents);
574 for (i = 0; i < tsgl->nents; i++) {
575 unsigned int offset = partitions[i].div->offset;
576 void *addr;
577
578 if (partitions[i].div->offset_relative_to_alignmask)
579 offset += alignmask;
580
581 while (offset + partitions[i].length + TESTMGR_POISON_LEN >
582 2 * PAGE_SIZE) {
583 if (WARN_ON(offset <= 0))
584 return -EINVAL;
585 offset /= 2;
586 }
587
588 addr = &tsgl->bufs[i][offset];
589 sg_set_buf(&tsgl->sgl[i], addr, partitions[i].length);
590
591 if (out_divs)
592 out_divs[i] = partitions[i].div;
593
594 if (data) {
595 size_t copy_len, copied;
596
597 copy_len = min(partitions[i].length, data->count);
598 copied = copy_from_iter(addr, copy_len, data);
599 if (WARN_ON(copied != copy_len))
600 return -EINVAL;
601 testmgr_poison(addr + copy_len, partitions[i].length +
602 TESTMGR_POISON_LEN - copy_len);
603 } else {
604 testmgr_poison(addr, partitions[i].length +
605 TESTMGR_POISON_LEN);
606 }
607 }
608
609 sg_mark_end(&tsgl->sgl[tsgl->nents - 1]);
610 tsgl->sgl_ptr = tsgl->sgl;
611 memcpy(tsgl->sgl_saved, tsgl->sgl, tsgl->nents * sizeof(tsgl->sgl[0]));
612 return 0;
613}
614
615/*
616 * Verify that a scatterlist crypto operation produced the correct output.
617 *
618 * @tsgl: scatterlist containing the actual output
619 * @expected_output: buffer containing the expected output
620 * @len_to_check: length of @expected_output in bytes
621 * @unchecked_prefix_len: number of ignored bytes in @tsgl prior to real result
622 * @check_poison: verify that the poison bytes after each chunk are intact?
623 *
624 * Return: 0 if correct, -EINVAL if incorrect, -EOVERFLOW if buffer overrun.
625 */
626static int verify_correct_output(const struct test_sglist *tsgl,
627 const char *expected_output,
628 unsigned int len_to_check,
629 unsigned int unchecked_prefix_len,
630 bool check_poison)
631{
632 unsigned int i;
633
634 for (i = 0; i < tsgl->nents; i++) {
635 struct scatterlist *sg = &tsgl->sgl_ptr[i];
636 unsigned int len = sg->length;
637 unsigned int offset = sg->offset;
638 const char *actual_output;
639
640 if (unchecked_prefix_len) {
641 if (unchecked_prefix_len >= len) {
642 unchecked_prefix_len -= len;
643 continue;
644 }
645 offset += unchecked_prefix_len;
646 len -= unchecked_prefix_len;
647 unchecked_prefix_len = 0;
648 }
649 len = min(len, len_to_check);
650 actual_output = page_address(sg_page(sg)) + offset;
651 if (memcmp(expected_output, actual_output, len) != 0)
652 return -EINVAL;
653 if (check_poison &&
654 !testmgr_is_poison(actual_output + len, TESTMGR_POISON_LEN))
655 return -EOVERFLOW;
656 len_to_check -= len;
657 expected_output += len;
658 }
659 if (WARN_ON(len_to_check != 0))
660 return -EINVAL;
661 return 0;
662}
663
664static bool is_test_sglist_corrupted(const struct test_sglist *tsgl)
665{
666 unsigned int i;
667
668 for (i = 0; i < tsgl->nents; i++) {
669 if (tsgl->sgl[i].page_link != tsgl->sgl_saved[i].page_link)
670 return true;
671 if (tsgl->sgl[i].offset != tsgl->sgl_saved[i].offset)
672 return true;
673 if (tsgl->sgl[i].length != tsgl->sgl_saved[i].length)
674 return true;
675 }
676 return false;
677}
678
679struct cipher_test_sglists {
680 struct test_sglist src;
681 struct test_sglist dst;
682};
683
684static struct cipher_test_sglists *alloc_cipher_test_sglists(void)
685{
686 struct cipher_test_sglists *tsgls;
687
688 tsgls = kmalloc(sizeof(*tsgls), GFP_KERNEL);
689 if (!tsgls)
690 return NULL;
691
692 if (init_test_sglist(&tsgls->src) != 0)
693 goto fail_kfree;
694 if (init_test_sglist(&tsgls->dst) != 0)
695 goto fail_destroy_src;
696
697 return tsgls;
698
699fail_destroy_src:
700 destroy_test_sglist(&tsgls->src);
701fail_kfree:
702 kfree(tsgls);
703 return NULL;
704}
705
706static void free_cipher_test_sglists(struct cipher_test_sglists *tsgls)
707{
708 if (tsgls) {
709 destroy_test_sglist(&tsgls->src);
710 destroy_test_sglist(&tsgls->dst);
711 kfree(tsgls);
712 }
713}
714
715/* Build the src and dst scatterlists for an skcipher or AEAD test */
716static int build_cipher_test_sglists(struct cipher_test_sglists *tsgls,
717 const struct testvec_config *cfg,
718 unsigned int alignmask,
719 unsigned int src_total_len,
720 unsigned int dst_total_len,
721 const struct kvec *inputs,
722 unsigned int nr_inputs)
723{
724 struct iov_iter input;
725 int err;
726
727 iov_iter_kvec(&input, WRITE, inputs, nr_inputs, src_total_len);
728 err = build_test_sglist(&tsgls->src, cfg->src_divs, alignmask,
729 cfg->inplace ?
730 max(dst_total_len, src_total_len) :
731 src_total_len,
732 &input, NULL);
733 if (err)
734 return err;
735
736 if (cfg->inplace) {
737 tsgls->dst.sgl_ptr = tsgls->src.sgl;
738 tsgls->dst.nents = tsgls->src.nents;
739 return 0;
740 }
741 return build_test_sglist(&tsgls->dst,
742 cfg->dst_divs[0].proportion_of_total ?
743 cfg->dst_divs : cfg->src_divs,
744 alignmask, dst_total_len, NULL, NULL);
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800745}
746
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800747#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
748static char *generate_random_sgl_divisions(struct test_sg_division *divs,
749 size_t max_divs, char *p, char *end,
Eric Biggers65707372019-03-12 22:12:52 -0700750 bool gen_flushes, u32 req_flags)
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800751{
752 struct test_sg_division *div = divs;
753 unsigned int remaining = TEST_SG_TOTAL;
754
755 do {
756 unsigned int this_len;
Eric Biggers65707372019-03-12 22:12:52 -0700757 const char *flushtype_str;
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800758
759 if (div == &divs[max_divs - 1] || prandom_u32() % 2 == 0)
760 this_len = remaining;
761 else
762 this_len = 1 + (prandom_u32() % remaining);
763 div->proportion_of_total = this_len;
764
765 if (prandom_u32() % 4 == 0)
766 div->offset = (PAGE_SIZE - 128) + (prandom_u32() % 128);
767 else if (prandom_u32() % 2 == 0)
768 div->offset = prandom_u32() % 32;
769 else
770 div->offset = prandom_u32() % PAGE_SIZE;
771 if (prandom_u32() % 8 == 0)
772 div->offset_relative_to_alignmask = true;
773
774 div->flush_type = FLUSH_TYPE_NONE;
775 if (gen_flushes) {
776 switch (prandom_u32() % 4) {
777 case 0:
778 div->flush_type = FLUSH_TYPE_REIMPORT;
779 break;
780 case 1:
781 div->flush_type = FLUSH_TYPE_FLUSH;
782 break;
783 }
784 }
785
Eric Biggers65707372019-03-12 22:12:52 -0700786 if (div->flush_type != FLUSH_TYPE_NONE &&
787 !(req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) &&
788 prandom_u32() % 2 == 0)
789 div->nosimd = true;
790
791 switch (div->flush_type) {
792 case FLUSH_TYPE_FLUSH:
793 if (div->nosimd)
794 flushtype_str = "<flush,nosimd>";
795 else
796 flushtype_str = "<flush>";
797 break;
798 case FLUSH_TYPE_REIMPORT:
799 if (div->nosimd)
800 flushtype_str = "<reimport,nosimd>";
801 else
802 flushtype_str = "<reimport>";
803 break;
804 default:
805 flushtype_str = "";
806 break;
807 }
808
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800809 BUILD_BUG_ON(TEST_SG_TOTAL != 10000); /* for "%u.%u%%" */
Eric Biggers65707372019-03-12 22:12:52 -0700810 p += scnprintf(p, end - p, "%s%u.%u%%@%s+%u%s", flushtype_str,
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800811 this_len / 100, this_len % 100,
812 div->offset_relative_to_alignmask ?
813 "alignmask" : "",
814 div->offset, this_len == remaining ? "" : ", ");
815 remaining -= this_len;
816 div++;
817 } while (remaining);
818
819 return p;
820}
821
822/* Generate a random testvec_config for fuzz testing */
823static void generate_random_testvec_config(struct testvec_config *cfg,
824 char *name, size_t max_namelen)
825{
826 char *p = name;
827 char * const end = name + max_namelen;
828
829 memset(cfg, 0, sizeof(*cfg));
830
831 cfg->name = name;
832
833 p += scnprintf(p, end - p, "random:");
834
835 if (prandom_u32() % 2 == 0) {
836 cfg->inplace = true;
837 p += scnprintf(p, end - p, " inplace");
838 }
839
840 if (prandom_u32() % 2 == 0) {
841 cfg->req_flags |= CRYPTO_TFM_REQ_MAY_SLEEP;
842 p += scnprintf(p, end - p, " may_sleep");
843 }
844
845 switch (prandom_u32() % 4) {
846 case 0:
847 cfg->finalization_type = FINALIZATION_TYPE_FINAL;
848 p += scnprintf(p, end - p, " use_final");
849 break;
850 case 1:
851 cfg->finalization_type = FINALIZATION_TYPE_FINUP;
852 p += scnprintf(p, end - p, " use_finup");
853 break;
854 default:
855 cfg->finalization_type = FINALIZATION_TYPE_DIGEST;
856 p += scnprintf(p, end - p, " use_digest");
857 break;
858 }
859
Eric Biggers65707372019-03-12 22:12:52 -0700860 if (!(cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) &&
861 prandom_u32() % 2 == 0) {
862 cfg->nosimd = true;
863 p += scnprintf(p, end - p, " nosimd");
864 }
865
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800866 p += scnprintf(p, end - p, " src_divs=[");
867 p = generate_random_sgl_divisions(cfg->src_divs,
868 ARRAY_SIZE(cfg->src_divs), p, end,
869 (cfg->finalization_type !=
Eric Biggers65707372019-03-12 22:12:52 -0700870 FINALIZATION_TYPE_DIGEST),
871 cfg->req_flags);
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800872 p += scnprintf(p, end - p, "]");
873
874 if (!cfg->inplace && prandom_u32() % 2 == 0) {
875 p += scnprintf(p, end - p, " dst_divs=[");
876 p = generate_random_sgl_divisions(cfg->dst_divs,
877 ARRAY_SIZE(cfg->dst_divs),
Eric Biggers65707372019-03-12 22:12:52 -0700878 p, end, false,
879 cfg->req_flags);
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800880 p += scnprintf(p, end - p, "]");
881 }
882
883 if (prandom_u32() % 2 == 0) {
884 cfg->iv_offset = 1 + (prandom_u32() % MAX_ALGAPI_ALIGNMASK);
885 p += scnprintf(p, end - p, " iv_offset=%u", cfg->iv_offset);
886 }
887
888 WARN_ON_ONCE(!valid_testvec_config(cfg));
889}
Eric Biggersb55e1a32019-03-12 22:12:47 -0700890
891static void crypto_disable_simd_for_test(void)
892{
893 preempt_disable();
894 __this_cpu_write(crypto_simd_disabled_for_test, true);
895}
896
897static void crypto_reenable_simd_for_test(void)
898{
899 __this_cpu_write(crypto_simd_disabled_for_test, false);
900 preempt_enable();
901}
902#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
903static void crypto_disable_simd_for_test(void)
904{
905}
906
907static void crypto_reenable_simd_for_test(void)
908{
909}
910#endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800911
Eric Biggers65707372019-03-12 22:12:52 -0700912static int do_ahash_op(int (*op)(struct ahash_request *req),
913 struct ahash_request *req,
914 struct crypto_wait *wait, bool nosimd)
915{
916 int err;
917
918 if (nosimd)
919 crypto_disable_simd_for_test();
920
921 err = op(req);
922
923 if (nosimd)
924 crypto_reenable_simd_for_test();
925
926 return crypto_wait_req(err, wait);
927}
928
Eric Biggers4cc2dcf2019-01-31 23:51:48 -0800929static int check_nonfinal_hash_op(const char *op, int err,
930 u8 *result, unsigned int digestsize,
931 const char *driver, unsigned int vec_num,
932 const struct testvec_config *cfg)
Kamil Konieczny466d7b92018-01-16 15:26:13 +0100933{
Eric Biggers4cc2dcf2019-01-31 23:51:48 -0800934 if (err) {
935 pr_err("alg: hash: %s %s() failed with err %d on test vector %u, cfg=\"%s\"\n",
936 driver, op, err, vec_num, cfg->name);
937 return err;
938 }
939 if (!testmgr_is_poison(result, digestsize)) {
940 pr_err("alg: hash: %s %s() used result buffer on test vector %u, cfg=\"%s\"\n",
941 driver, op, vec_num, cfg->name);
942 return -EINVAL;
943 }
944 return 0;
945}
Kamil Konieczny466d7b92018-01-16 15:26:13 +0100946
Eric Biggers4cc2dcf2019-01-31 23:51:48 -0800947static int test_hash_vec_cfg(const char *driver,
948 const struct hash_testvec *vec,
949 unsigned int vec_num,
950 const struct testvec_config *cfg,
951 struct ahash_request *req,
952 struct test_sglist *tsgl,
953 u8 *hashstate)
954{
955 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
956 const unsigned int alignmask = crypto_ahash_alignmask(tfm);
957 const unsigned int digestsize = crypto_ahash_digestsize(tfm);
958 const unsigned int statesize = crypto_ahash_statesize(tfm);
959 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
960 const struct test_sg_division *divs[XBUFSIZE];
961 DECLARE_CRYPTO_WAIT(wait);
962 struct kvec _input;
963 struct iov_iter input;
964 unsigned int i;
965 struct scatterlist *pending_sgl;
966 unsigned int pending_len;
967 u8 result[HASH_MAX_DIGESTSIZE + TESTMGR_POISON_LEN];
968 int err;
969
970 /* Set the key, if specified */
971 if (vec->ksize) {
972 err = crypto_ahash_setkey(tfm, vec->key, vec->ksize);
973 if (err) {
974 pr_err("alg: hash: %s setkey failed with err %d on test vector %u; flags=%#x\n",
975 driver, err, vec_num,
976 crypto_ahash_get_flags(tfm));
977 return err;
978 }
979 }
980
981 /* Build the scatterlist for the source data */
982 _input.iov_base = (void *)vec->plaintext;
983 _input.iov_len = vec->psize;
984 iov_iter_kvec(&input, WRITE, &_input, 1, vec->psize);
985 err = build_test_sglist(tsgl, cfg->src_divs, alignmask, vec->psize,
986 &input, divs);
987 if (err) {
988 pr_err("alg: hash: %s: error preparing scatterlist for test vector %u, cfg=\"%s\"\n",
989 driver, vec_num, cfg->name);
990 return err;
991 }
992
993 /* Do the actual hashing */
994
995 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
996 testmgr_poison(result, digestsize + TESTMGR_POISON_LEN);
997
998 if (cfg->finalization_type == FINALIZATION_TYPE_DIGEST) {
999 /* Just using digest() */
1000 ahash_request_set_callback(req, req_flags, crypto_req_done,
1001 &wait);
1002 ahash_request_set_crypt(req, tsgl->sgl, result, vec->psize);
Eric Biggers65707372019-03-12 22:12:52 -07001003 err = do_ahash_op(crypto_ahash_digest, req, &wait, cfg->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001004 if (err) {
1005 pr_err("alg: hash: %s digest() failed with err %d on test vector %u, cfg=\"%s\"\n",
1006 driver, err, vec_num, cfg->name);
1007 return err;
1008 }
1009 goto result_ready;
1010 }
1011
1012 /* Using init(), zero or more update(), then final() or finup() */
1013
1014 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
1015 ahash_request_set_crypt(req, NULL, result, 0);
Eric Biggers65707372019-03-12 22:12:52 -07001016 err = do_ahash_op(crypto_ahash_init, req, &wait, cfg->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001017 err = check_nonfinal_hash_op("init", err, result, digestsize,
1018 driver, vec_num, cfg);
1019 if (err)
1020 return err;
1021
1022 pending_sgl = NULL;
1023 pending_len = 0;
1024 for (i = 0; i < tsgl->nents; i++) {
1025 if (divs[i]->flush_type != FLUSH_TYPE_NONE &&
1026 pending_sgl != NULL) {
1027 /* update() with the pending data */
1028 ahash_request_set_callback(req, req_flags,
1029 crypto_req_done, &wait);
1030 ahash_request_set_crypt(req, pending_sgl, result,
1031 pending_len);
Eric Biggers65707372019-03-12 22:12:52 -07001032 err = do_ahash_op(crypto_ahash_update, req, &wait,
1033 divs[i]->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001034 err = check_nonfinal_hash_op("update", err,
1035 result, digestsize,
1036 driver, vec_num, cfg);
1037 if (err)
1038 return err;
1039 pending_sgl = NULL;
1040 pending_len = 0;
1041 }
1042 if (divs[i]->flush_type == FLUSH_TYPE_REIMPORT) {
1043 /* Test ->export() and ->import() */
1044 testmgr_poison(hashstate + statesize,
1045 TESTMGR_POISON_LEN);
1046 err = crypto_ahash_export(req, hashstate);
1047 err = check_nonfinal_hash_op("export", err,
1048 result, digestsize,
1049 driver, vec_num, cfg);
1050 if (err)
1051 return err;
1052 if (!testmgr_is_poison(hashstate + statesize,
1053 TESTMGR_POISON_LEN)) {
1054 pr_err("alg: hash: %s export() overran state buffer on test vector %u, cfg=\"%s\"\n",
1055 driver, vec_num, cfg->name);
1056 return -EOVERFLOW;
1057 }
1058
1059 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
1060 err = crypto_ahash_import(req, hashstate);
1061 err = check_nonfinal_hash_op("import", err,
1062 result, digestsize,
1063 driver, vec_num, cfg);
1064 if (err)
1065 return err;
1066 }
1067 if (pending_sgl == NULL)
1068 pending_sgl = &tsgl->sgl[i];
1069 pending_len += tsgl->sgl[i].length;
1070 }
1071
1072 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
1073 ahash_request_set_crypt(req, pending_sgl, result, pending_len);
1074 if (cfg->finalization_type == FINALIZATION_TYPE_FINAL) {
1075 /* finish with update() and final() */
Eric Biggers65707372019-03-12 22:12:52 -07001076 err = do_ahash_op(crypto_ahash_update, req, &wait, cfg->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001077 err = check_nonfinal_hash_op("update", err, result, digestsize,
1078 driver, vec_num, cfg);
1079 if (err)
1080 return err;
Eric Biggers65707372019-03-12 22:12:52 -07001081 err = do_ahash_op(crypto_ahash_final, req, &wait, cfg->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001082 if (err) {
1083 pr_err("alg: hash: %s final() failed with err %d on test vector %u, cfg=\"%s\"\n",
1084 driver, err, vec_num, cfg->name);
1085 return err;
1086 }
1087 } else {
1088 /* finish with finup() */
Eric Biggers65707372019-03-12 22:12:52 -07001089 err = do_ahash_op(crypto_ahash_finup, req, &wait, cfg->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001090 if (err) {
1091 pr_err("alg: hash: %s finup() failed with err %d on test vector %u, cfg=\"%s\"\n",
1092 driver, err, vec_num, cfg->name);
1093 return err;
1094 }
1095 }
1096
1097result_ready:
1098 /* Check that the algorithm produced the correct digest */
1099 if (memcmp(result, vec->digest, digestsize) != 0) {
1100 pr_err("alg: hash: %s test failed (wrong result) on test vector %u, cfg=\"%s\"\n",
1101 driver, vec_num, cfg->name);
1102 return -EINVAL;
1103 }
1104 if (!testmgr_is_poison(&result[digestsize], TESTMGR_POISON_LEN)) {
1105 pr_err("alg: hash: %s overran result buffer on test vector %u, cfg=\"%s\"\n",
1106 driver, vec_num, cfg->name);
1107 return -EOVERFLOW;
1108 }
1109
1110 return 0;
1111}
1112
1113static int test_hash_vec(const char *driver, const struct hash_testvec *vec,
1114 unsigned int vec_num, struct ahash_request *req,
1115 struct test_sglist *tsgl, u8 *hashstate)
1116{
1117 unsigned int i;
1118 int err;
1119
1120 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++) {
1121 err = test_hash_vec_cfg(driver, vec, vec_num,
1122 &default_hash_testvec_configs[i],
1123 req, tsgl, hashstate);
1124 if (err)
1125 return err;
1126 }
1127
1128#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1129 if (!noextratests) {
1130 struct testvec_config cfg;
1131 char cfgname[TESTVEC_CONFIG_NAMELEN];
1132
1133 for (i = 0; i < fuzz_iterations; i++) {
1134 generate_random_testvec_config(&cfg, cfgname,
1135 sizeof(cfgname));
1136 err = test_hash_vec_cfg(driver, vec, vec_num, &cfg,
1137 req, tsgl, hashstate);
1138 if (err)
1139 return err;
1140 }
1141 }
1142#endif
1143 return 0;
1144}
1145
1146static int __alg_test_hash(const struct hash_testvec *vecs,
1147 unsigned int num_vecs, const char *driver,
1148 u32 type, u32 mask)
1149{
1150 struct crypto_ahash *tfm;
1151 struct ahash_request *req = NULL;
1152 struct test_sglist *tsgl = NULL;
1153 u8 *hashstate = NULL;
1154 unsigned int i;
1155 int err;
1156
1157 tfm = crypto_alloc_ahash(driver, type, mask);
1158 if (IS_ERR(tfm)) {
1159 pr_err("alg: hash: failed to allocate transform for %s: %ld\n",
1160 driver, PTR_ERR(tfm));
1161 return PTR_ERR(tfm);
1162 }
1163
1164 req = ahash_request_alloc(tfm, GFP_KERNEL);
1165 if (!req) {
1166 pr_err("alg: hash: failed to allocate request for %s\n",
1167 driver);
1168 err = -ENOMEM;
1169 goto out;
1170 }
1171
1172 tsgl = kmalloc(sizeof(*tsgl), GFP_KERNEL);
1173 if (!tsgl || init_test_sglist(tsgl) != 0) {
1174 pr_err("alg: hash: failed to allocate test buffers for %s\n",
1175 driver);
1176 kfree(tsgl);
1177 tsgl = NULL;
1178 err = -ENOMEM;
1179 goto out;
1180 }
1181
1182 hashstate = kmalloc(crypto_ahash_statesize(tfm) + TESTMGR_POISON_LEN,
1183 GFP_KERNEL);
1184 if (!hashstate) {
1185 pr_err("alg: hash: failed to allocate hash state buffer for %s\n",
1186 driver);
1187 err = -ENOMEM;
1188 goto out;
1189 }
1190
1191 for (i = 0; i < num_vecs; i++) {
1192 err = test_hash_vec(driver, &vecs[i], i, req, tsgl, hashstate);
1193 if (err)
1194 goto out;
1195 }
1196 err = 0;
1197out:
1198 kfree(hashstate);
1199 if (tsgl) {
1200 destroy_test_sglist(tsgl);
1201 kfree(tsgl);
1202 }
1203 ahash_request_free(req);
1204 crypto_free_ahash(tfm);
1205 return err;
1206}
1207
1208static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1209 u32 type, u32 mask)
1210{
1211 const struct hash_testvec *template = desc->suite.hash.vecs;
1212 unsigned int tcount = desc->suite.hash.count;
1213 unsigned int nr_unkeyed, nr_keyed;
1214 int err;
1215
1216 /*
1217 * For OPTIONAL_KEY algorithms, we have to do all the unkeyed tests
1218 * first, before setting a key on the tfm. To make this easier, we
1219 * require that the unkeyed test vectors (if any) are listed first.
1220 */
1221
1222 for (nr_unkeyed = 0; nr_unkeyed < tcount; nr_unkeyed++) {
1223 if (template[nr_unkeyed].ksize)
1224 break;
1225 }
1226 for (nr_keyed = 0; nr_unkeyed + nr_keyed < tcount; nr_keyed++) {
1227 if (!template[nr_unkeyed + nr_keyed].ksize) {
1228 pr_err("alg: hash: test vectors for %s out of order, "
1229 "unkeyed ones must come first\n", desc->alg);
Kamil Konieczny466d7b92018-01-16 15:26:13 +01001230 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08001231 }
1232 }
1233
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001234 err = 0;
1235 if (nr_unkeyed) {
1236 err = __alg_test_hash(template, nr_unkeyed, driver, type, mask);
1237 template += nr_unkeyed;
Herbert Xuda7f0332008-07-31 17:08:25 +08001238 }
1239
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001240 if (!err && nr_keyed)
1241 err = __alg_test_hash(template, nr_keyed, driver, type, mask);
Wang, Rui Y018ba952016-02-03 18:26:57 +08001242
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001243 return err;
Jussi Kivilinnada5ffe12013-06-13 17:37:55 +03001244}
1245
Eric Biggersed968042019-01-31 23:51:47 -08001246static int test_aead_vec_cfg(const char *driver, int enc,
1247 const struct aead_testvec *vec,
1248 unsigned int vec_num,
1249 const struct testvec_config *cfg,
1250 struct aead_request *req,
1251 struct cipher_test_sglists *tsgls)
Herbert Xuda7f0332008-07-31 17:08:25 +08001252{
Eric Biggersed968042019-01-31 23:51:47 -08001253 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1254 const unsigned int alignmask = crypto_aead_alignmask(tfm);
1255 const unsigned int ivsize = crypto_aead_ivsize(tfm);
1256 const unsigned int authsize = vec->clen - vec->plen;
1257 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1258 const char *op = enc ? "encryption" : "decryption";
1259 DECLARE_CRYPTO_WAIT(wait);
1260 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
1261 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
1262 cfg->iv_offset +
1263 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
1264 struct kvec input[2];
1265 int err;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001266
Eric Biggersed968042019-01-31 23:51:47 -08001267 /* Set the key */
1268 if (vec->wk)
1269 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001270 else
Eric Biggersed968042019-01-31 23:51:47 -08001271 crypto_aead_clear_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1272 err = crypto_aead_setkey(tfm, vec->key, vec->klen);
1273 if (err) {
1274 if (vec->fail) /* expectedly failed to set key? */
1275 return 0;
1276 pr_err("alg: aead: %s setkey failed with err %d on test vector %u; flags=%#x\n",
1277 driver, err, vec_num, crypto_aead_get_flags(tfm));
1278 return err;
1279 }
1280 if (vec->fail) {
1281 pr_err("alg: aead: %s setkey unexpectedly succeeded on test vector %u\n",
1282 driver, vec_num);
1283 return -EINVAL;
1284 }
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001285
Eric Biggersed968042019-01-31 23:51:47 -08001286 /* Set the authentication tag size */
1287 err = crypto_aead_setauthsize(tfm, authsize);
1288 if (err) {
1289 pr_err("alg: aead: %s setauthsize failed with err %d on test vector %u\n",
1290 driver, err, vec_num);
1291 return err;
1292 }
1293
1294 /* The IV must be copied to a buffer, as the algorithm may modify it */
1295 if (WARN_ON(ivsize > MAX_IVLEN))
1296 return -EINVAL;
1297 if (vec->iv)
1298 memcpy(iv, vec->iv, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001299 else
Eric Biggersed968042019-01-31 23:51:47 -08001300 memset(iv, 0, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001301
Eric Biggersed968042019-01-31 23:51:47 -08001302 /* Build the src/dst scatterlists */
1303 input[0].iov_base = (void *)vec->assoc;
1304 input[0].iov_len = vec->alen;
1305 input[1].iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
1306 input[1].iov_len = enc ? vec->plen : vec->clen;
1307 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
1308 vec->alen + (enc ? vec->plen :
1309 vec->clen),
1310 vec->alen + (enc ? vec->clen :
1311 vec->plen),
1312 input, 2);
1313 if (err) {
1314 pr_err("alg: aead: %s %s: error preparing scatterlists for test vector %u, cfg=\"%s\"\n",
1315 driver, op, vec_num, cfg->name);
1316 return err;
Herbert Xuda7f0332008-07-31 17:08:25 +08001317 }
1318
Eric Biggersed968042019-01-31 23:51:47 -08001319 /* Do the actual encryption or decryption */
1320 testmgr_poison(req->__ctx, crypto_aead_reqsize(tfm));
1321 aead_request_set_callback(req, req_flags, crypto_req_done, &wait);
1322 aead_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
1323 enc ? vec->plen : vec->clen, iv);
1324 aead_request_set_ad(req, vec->alen);
Eric Biggers65707372019-03-12 22:12:52 -07001325 if (cfg->nosimd)
1326 crypto_disable_simd_for_test();
1327 err = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
1328 if (cfg->nosimd)
1329 crypto_reenable_simd_for_test();
1330 err = crypto_wait_req(err, &wait);
Eric Biggersed968042019-01-31 23:51:47 -08001331 if (err) {
1332 if (err == -EBADMSG && vec->novrfy)
1333 return 0;
1334 pr_err("alg: aead: %s %s failed with err %d on test vector %u, cfg=\"%s\"\n",
1335 driver, op, err, vec_num, cfg->name);
1336 return err;
1337 }
1338 if (vec->novrfy) {
1339 pr_err("alg: aead: %s %s unexpectedly succeeded on test vector %u, cfg=\"%s\"\n",
1340 driver, op, vec_num, cfg->name);
1341 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08001342 }
1343
Eric Biggersa6e5ef92019-01-31 23:51:50 -08001344 /* Check that the algorithm didn't overwrite things it shouldn't have */
1345 if (req->cryptlen != (enc ? vec->plen : vec->clen) ||
1346 req->assoclen != vec->alen ||
1347 req->iv != iv ||
1348 req->src != tsgls->src.sgl_ptr ||
1349 req->dst != tsgls->dst.sgl_ptr ||
1350 crypto_aead_reqtfm(req) != tfm ||
1351 req->base.complete != crypto_req_done ||
1352 req->base.flags != req_flags ||
1353 req->base.data != &wait) {
1354 pr_err("alg: aead: %s %s corrupted request struct on test vector %u, cfg=\"%s\"\n",
1355 driver, op, vec_num, cfg->name);
1356 if (req->cryptlen != (enc ? vec->plen : vec->clen))
1357 pr_err("alg: aead: changed 'req->cryptlen'\n");
1358 if (req->assoclen != vec->alen)
1359 pr_err("alg: aead: changed 'req->assoclen'\n");
1360 if (req->iv != iv)
1361 pr_err("alg: aead: changed 'req->iv'\n");
1362 if (req->src != tsgls->src.sgl_ptr)
1363 pr_err("alg: aead: changed 'req->src'\n");
1364 if (req->dst != tsgls->dst.sgl_ptr)
1365 pr_err("alg: aead: changed 'req->dst'\n");
1366 if (crypto_aead_reqtfm(req) != tfm)
1367 pr_err("alg: aead: changed 'req->base.tfm'\n");
1368 if (req->base.complete != crypto_req_done)
1369 pr_err("alg: aead: changed 'req->base.complete'\n");
1370 if (req->base.flags != req_flags)
1371 pr_err("alg: aead: changed 'req->base.flags'\n");
1372 if (req->base.data != &wait)
1373 pr_err("alg: aead: changed 'req->base.data'\n");
1374 return -EINVAL;
1375 }
1376 if (is_test_sglist_corrupted(&tsgls->src)) {
1377 pr_err("alg: aead: %s %s corrupted src sgl on test vector %u, cfg=\"%s\"\n",
1378 driver, op, vec_num, cfg->name);
1379 return -EINVAL;
1380 }
1381 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
1382 is_test_sglist_corrupted(&tsgls->dst)) {
1383 pr_err("alg: aead: %s %s corrupted dst sgl on test vector %u, cfg=\"%s\"\n",
1384 driver, op, vec_num, cfg->name);
1385 return -EINVAL;
1386 }
1387
Eric Biggersed968042019-01-31 23:51:47 -08001388 /* Check for the correct output (ciphertext or plaintext) */
1389 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
1390 enc ? vec->clen : vec->plen,
1391 vec->alen, enc || !cfg->inplace);
1392 if (err == -EOVERFLOW) {
1393 pr_err("alg: aead: %s %s overran dst buffer on test vector %u, cfg=\"%s\"\n",
1394 driver, op, vec_num, cfg->name);
1395 return err;
Herbert Xuda7f0332008-07-31 17:08:25 +08001396 }
Eric Biggersed968042019-01-31 23:51:47 -08001397 if (err) {
1398 pr_err("alg: aead: %s %s test failed (wrong result) on test vector %u, cfg=\"%s\"\n",
1399 driver, op, vec_num, cfg->name);
1400 return err;
Jussi Kivilinna58dcf542013-06-13 17:37:50 +03001401 }
1402
1403 return 0;
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001404}
1405
Eric Biggersed968042019-01-31 23:51:47 -08001406static int test_aead_vec(const char *driver, int enc,
1407 const struct aead_testvec *vec, unsigned int vec_num,
1408 struct aead_request *req,
1409 struct cipher_test_sglists *tsgls)
1410{
1411 unsigned int i;
1412 int err;
1413
1414 if (enc && vec->novrfy)
1415 return 0;
1416
1417 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
1418 err = test_aead_vec_cfg(driver, enc, vec, vec_num,
1419 &default_cipher_testvec_configs[i],
1420 req, tsgls);
1421 if (err)
1422 return err;
1423 }
1424
1425#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1426 if (!noextratests) {
1427 struct testvec_config cfg;
1428 char cfgname[TESTVEC_CONFIG_NAMELEN];
1429
1430 for (i = 0; i < fuzz_iterations; i++) {
1431 generate_random_testvec_config(&cfg, cfgname,
1432 sizeof(cfgname));
1433 err = test_aead_vec_cfg(driver, enc, vec, vec_num,
1434 &cfg, req, tsgls);
1435 if (err)
1436 return err;
1437 }
1438 }
1439#endif
1440 return 0;
1441}
1442
1443static int test_aead(const char *driver, int enc,
1444 const struct aead_test_suite *suite,
1445 struct aead_request *req,
1446 struct cipher_test_sglists *tsgls)
1447{
1448 unsigned int i;
1449 int err;
1450
1451 for (i = 0; i < suite->count; i++) {
1452 err = test_aead_vec(driver, enc, &suite->vecs[i], i, req,
1453 tsgls);
1454 if (err)
1455 return err;
1456 }
1457 return 0;
1458}
1459
1460static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
1461 u32 type, u32 mask)
1462{
1463 const struct aead_test_suite *suite = &desc->suite.aead;
1464 struct crypto_aead *tfm;
1465 struct aead_request *req = NULL;
1466 struct cipher_test_sglists *tsgls = NULL;
1467 int err;
1468
1469 if (suite->count <= 0) {
1470 pr_err("alg: aead: empty test suite for %s\n", driver);
1471 return -EINVAL;
1472 }
1473
1474 tfm = crypto_alloc_aead(driver, type, mask);
1475 if (IS_ERR(tfm)) {
1476 pr_err("alg: aead: failed to allocate transform for %s: %ld\n",
1477 driver, PTR_ERR(tfm));
1478 return PTR_ERR(tfm);
1479 }
1480
1481 req = aead_request_alloc(tfm, GFP_KERNEL);
1482 if (!req) {
1483 pr_err("alg: aead: failed to allocate request for %s\n",
1484 driver);
1485 err = -ENOMEM;
1486 goto out;
1487 }
1488
1489 tsgls = alloc_cipher_test_sglists();
1490 if (!tsgls) {
1491 pr_err("alg: aead: failed to allocate test buffers for %s\n",
1492 driver);
1493 err = -ENOMEM;
1494 goto out;
1495 }
1496
1497 err = test_aead(driver, ENCRYPT, suite, req, tsgls);
1498 if (err)
1499 goto out;
1500
1501 err = test_aead(driver, DECRYPT, suite, req, tsgls);
1502out:
1503 free_cipher_test_sglists(tsgls);
1504 aead_request_free(req);
1505 crypto_free_aead(tfm);
1506 return err;
1507}
1508
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001509static int test_cipher(struct crypto_cipher *tfm, int enc,
Eric Biggersb13b1e02017-02-24 15:46:59 -08001510 const struct cipher_testvec *template,
1511 unsigned int tcount)
Herbert Xuda7f0332008-07-31 17:08:25 +08001512{
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001513 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
1514 unsigned int i, j, k;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001515 char *q;
1516 const char *e;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001517 const char *input, *result;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001518 void *data;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001519 char *xbuf[XBUFSIZE];
1520 int ret = -ENOMEM;
1521
1522 if (testmgr_alloc_buf(xbuf))
1523 goto out_nobuf;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001524
1525 if (enc == ENCRYPT)
1526 e = "encryption";
1527 else
1528 e = "decryption";
1529
1530 j = 0;
1531 for (i = 0; i < tcount; i++) {
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001532
Stephan Mueller10faa8c2016-08-25 15:15:01 +02001533 if (fips_enabled && template[i].fips_skip)
1534 continue;
1535
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001536 input = enc ? template[i].ptext : template[i].ctext;
1537 result = enc ? template[i].ctext : template[i].ptext;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001538 j++;
1539
Herbert Xufd57f222009-05-29 16:05:42 +10001540 ret = -EINVAL;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001541 if (WARN_ON(template[i].len > PAGE_SIZE))
Herbert Xufd57f222009-05-29 16:05:42 +10001542 goto out;
1543
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001544 data = xbuf[0];
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001545 memcpy(data, input, template[i].len);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001546
1547 crypto_cipher_clear_flags(tfm, ~0);
1548 if (template[i].wk)
Eric Biggers231baec2019-01-18 22:48:00 -08001549 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001550
1551 ret = crypto_cipher_setkey(tfm, template[i].key,
1552 template[i].klen);
Yanjiang Jin0fae0c12016-07-29 16:32:09 +08001553 if (template[i].fail == !ret) {
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001554 printk(KERN_ERR "alg: cipher: setkey failed "
1555 "on test %d for %s: flags=%x\n", j,
1556 algo, crypto_cipher_get_flags(tfm));
1557 goto out;
1558 } else if (ret)
1559 continue;
1560
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001561 for (k = 0; k < template[i].len;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001562 k += crypto_cipher_blocksize(tfm)) {
1563 if (enc)
1564 crypto_cipher_encrypt_one(tfm, data + k,
1565 data + k);
1566 else
1567 crypto_cipher_decrypt_one(tfm, data + k,
1568 data + k);
1569 }
1570
1571 q = data;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001572 if (memcmp(q, result, template[i].len)) {
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001573 printk(KERN_ERR "alg: cipher: Test %d failed "
1574 "on %s for %s\n", j, e, algo);
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001575 hexdump(q, template[i].len);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001576 ret = -EINVAL;
1577 goto out;
1578 }
1579 }
1580
1581 ret = 0;
1582
1583out:
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001584 testmgr_free_buf(xbuf);
1585out_nobuf:
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001586 return ret;
1587}
1588
Eric Biggers4e7babba2019-01-31 23:51:46 -08001589static int test_skcipher_vec_cfg(const char *driver, int enc,
1590 const struct cipher_testvec *vec,
1591 unsigned int vec_num,
1592 const struct testvec_config *cfg,
1593 struct skcipher_request *req,
1594 struct cipher_test_sglists *tsgls)
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001595{
Eric Biggers4e7babba2019-01-31 23:51:46 -08001596 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1597 const unsigned int alignmask = crypto_skcipher_alignmask(tfm);
1598 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
1599 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1600 const char *op = enc ? "encryption" : "decryption";
1601 DECLARE_CRYPTO_WAIT(wait);
1602 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
1603 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
1604 cfg->iv_offset +
1605 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
1606 struct kvec input;
1607 int err;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001608
Eric Biggers4e7babba2019-01-31 23:51:46 -08001609 /* Set the key */
1610 if (vec->wk)
1611 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001612 else
Eric Biggers4e7babba2019-01-31 23:51:46 -08001613 crypto_skcipher_clear_flags(tfm,
1614 CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1615 err = crypto_skcipher_setkey(tfm, vec->key, vec->klen);
1616 if (err) {
1617 if (vec->fail) /* expectedly failed to set key? */
1618 return 0;
1619 pr_err("alg: skcipher: %s setkey failed with err %d on test vector %u; flags=%#x\n",
1620 driver, err, vec_num, crypto_skcipher_get_flags(tfm));
1621 return err;
1622 }
1623 if (vec->fail) {
1624 pr_err("alg: skcipher: %s setkey unexpectedly succeeded on test vector %u\n",
1625 driver, vec_num);
1626 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08001627 }
1628
Eric Biggers4e7babba2019-01-31 23:51:46 -08001629 /* The IV must be copied to a buffer, as the algorithm may modify it */
1630 if (ivsize) {
1631 if (WARN_ON(ivsize > MAX_IVLEN))
1632 return -EINVAL;
Eric Biggers8efd9722019-02-14 00:03:51 -08001633 if (vec->generates_iv && !enc)
1634 memcpy(iv, vec->iv_out, ivsize);
1635 else if (vec->iv)
Eric Biggers4e7babba2019-01-31 23:51:46 -08001636 memcpy(iv, vec->iv, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001637 else
Eric Biggers4e7babba2019-01-31 23:51:46 -08001638 memset(iv, 0, ivsize);
1639 } else {
1640 if (vec->generates_iv) {
1641 pr_err("alg: skcipher: %s has ivsize=0 but test vector %u generates IV!\n",
1642 driver, vec_num);
1643 return -EINVAL;
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001644 }
Eric Biggers4e7babba2019-01-31 23:51:46 -08001645 iv = NULL;
Herbert Xuda7f0332008-07-31 17:08:25 +08001646 }
1647
Eric Biggers4e7babba2019-01-31 23:51:46 -08001648 /* Build the src/dst scatterlists */
1649 input.iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
1650 input.iov_len = vec->len;
1651 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
1652 vec->len, vec->len, &input, 1);
1653 if (err) {
1654 pr_err("alg: skcipher: %s %s: error preparing scatterlists for test vector %u, cfg=\"%s\"\n",
1655 driver, op, vec_num, cfg->name);
1656 return err;
Herbert Xuda7f0332008-07-31 17:08:25 +08001657 }
1658
Eric Biggers4e7babba2019-01-31 23:51:46 -08001659 /* Do the actual encryption or decryption */
1660 testmgr_poison(req->__ctx, crypto_skcipher_reqsize(tfm));
1661 skcipher_request_set_callback(req, req_flags, crypto_req_done, &wait);
1662 skcipher_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
1663 vec->len, iv);
Eric Biggers65707372019-03-12 22:12:52 -07001664 if (cfg->nosimd)
1665 crypto_disable_simd_for_test();
1666 err = enc ? crypto_skcipher_encrypt(req) : crypto_skcipher_decrypt(req);
1667 if (cfg->nosimd)
1668 crypto_reenable_simd_for_test();
1669 err = crypto_wait_req(err, &wait);
Eric Biggers4e7babba2019-01-31 23:51:46 -08001670 if (err) {
1671 pr_err("alg: skcipher: %s %s failed with err %d on test vector %u, cfg=\"%s\"\n",
1672 driver, op, err, vec_num, cfg->name);
1673 return err;
1674 }
Herbert Xuda7f0332008-07-31 17:08:25 +08001675
Eric Biggersfa353c92019-01-31 23:51:49 -08001676 /* Check that the algorithm didn't overwrite things it shouldn't have */
1677 if (req->cryptlen != vec->len ||
1678 req->iv != iv ||
1679 req->src != tsgls->src.sgl_ptr ||
1680 req->dst != tsgls->dst.sgl_ptr ||
1681 crypto_skcipher_reqtfm(req) != tfm ||
1682 req->base.complete != crypto_req_done ||
1683 req->base.flags != req_flags ||
1684 req->base.data != &wait) {
1685 pr_err("alg: skcipher: %s %s corrupted request struct on test vector %u, cfg=\"%s\"\n",
1686 driver, op, vec_num, cfg->name);
1687 if (req->cryptlen != vec->len)
1688 pr_err("alg: skcipher: changed 'req->cryptlen'\n");
1689 if (req->iv != iv)
1690 pr_err("alg: skcipher: changed 'req->iv'\n");
1691 if (req->src != tsgls->src.sgl_ptr)
1692 pr_err("alg: skcipher: changed 'req->src'\n");
1693 if (req->dst != tsgls->dst.sgl_ptr)
1694 pr_err("alg: skcipher: changed 'req->dst'\n");
1695 if (crypto_skcipher_reqtfm(req) != tfm)
1696 pr_err("alg: skcipher: changed 'req->base.tfm'\n");
1697 if (req->base.complete != crypto_req_done)
1698 pr_err("alg: skcipher: changed 'req->base.complete'\n");
1699 if (req->base.flags != req_flags)
1700 pr_err("alg: skcipher: changed 'req->base.flags'\n");
1701 if (req->base.data != &wait)
1702 pr_err("alg: skcipher: changed 'req->base.data'\n");
1703 return -EINVAL;
1704 }
1705 if (is_test_sglist_corrupted(&tsgls->src)) {
1706 pr_err("alg: skcipher: %s %s corrupted src sgl on test vector %u, cfg=\"%s\"\n",
1707 driver, op, vec_num, cfg->name);
1708 return -EINVAL;
1709 }
1710 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
1711 is_test_sglist_corrupted(&tsgls->dst)) {
1712 pr_err("alg: skcipher: %s %s corrupted dst sgl on test vector %u, cfg=\"%s\"\n",
1713 driver, op, vec_num, cfg->name);
1714 return -EINVAL;
1715 }
1716
Eric Biggers4e7babba2019-01-31 23:51:46 -08001717 /* Check for the correct output (ciphertext or plaintext) */
1718 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
1719 vec->len, 0, true);
1720 if (err == -EOVERFLOW) {
1721 pr_err("alg: skcipher: %s %s overran dst buffer on test vector %u, cfg=\"%s\"\n",
1722 driver, op, vec_num, cfg->name);
1723 return err;
1724 }
1725 if (err) {
1726 pr_err("alg: skcipher: %s %s test failed (wrong result) on test vector %u, cfg=\"%s\"\n",
1727 driver, op, vec_num, cfg->name);
1728 return err;
1729 }
Herbert Xuda7f0332008-07-31 17:08:25 +08001730
Eric Biggers4e7babba2019-01-31 23:51:46 -08001731 /* If applicable, check that the algorithm generated the correct IV */
Eric Biggers8efd9722019-02-14 00:03:51 -08001732 if (vec->iv_out && memcmp(iv, vec->iv_out, ivsize) != 0) {
Eric Biggers4e7babba2019-01-31 23:51:46 -08001733 pr_err("alg: skcipher: %s %s test failed (wrong output IV) on test vector %u, cfg=\"%s\"\n",
1734 driver, op, vec_num, cfg->name);
1735 hexdump(iv, ivsize);
1736 return -EINVAL;
Jussi Kivilinna3a338f22013-06-13 17:37:45 +03001737 }
1738
1739 return 0;
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001740}
1741
Eric Biggers4e7babba2019-01-31 23:51:46 -08001742static int test_skcipher_vec(const char *driver, int enc,
1743 const struct cipher_testvec *vec,
1744 unsigned int vec_num,
1745 struct skcipher_request *req,
1746 struct cipher_test_sglists *tsgls)
1747{
1748 unsigned int i;
1749 int err;
1750
1751 if (fips_enabled && vec->fips_skip)
1752 return 0;
1753
1754 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
1755 err = test_skcipher_vec_cfg(driver, enc, vec, vec_num,
1756 &default_cipher_testvec_configs[i],
1757 req, tsgls);
1758 if (err)
1759 return err;
1760 }
1761
1762#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1763 if (!noextratests) {
1764 struct testvec_config cfg;
1765 char cfgname[TESTVEC_CONFIG_NAMELEN];
1766
1767 for (i = 0; i < fuzz_iterations; i++) {
1768 generate_random_testvec_config(&cfg, cfgname,
1769 sizeof(cfgname));
1770 err = test_skcipher_vec_cfg(driver, enc, vec, vec_num,
1771 &cfg, req, tsgls);
1772 if (err)
1773 return err;
1774 }
1775 }
1776#endif
1777 return 0;
1778}
1779
1780static int test_skcipher(const char *driver, int enc,
1781 const struct cipher_test_suite *suite,
1782 struct skcipher_request *req,
1783 struct cipher_test_sglists *tsgls)
1784{
1785 unsigned int i;
1786 int err;
1787
1788 for (i = 0; i < suite->count; i++) {
1789 err = test_skcipher_vec(driver, enc, &suite->vecs[i], i, req,
1790 tsgls);
1791 if (err)
1792 return err;
1793 }
1794 return 0;
1795}
1796
1797static int alg_test_skcipher(const struct alg_test_desc *desc,
1798 const char *driver, u32 type, u32 mask)
1799{
1800 const struct cipher_test_suite *suite = &desc->suite.cipher;
1801 struct crypto_skcipher *tfm;
1802 struct skcipher_request *req = NULL;
1803 struct cipher_test_sglists *tsgls = NULL;
1804 int err;
1805
1806 if (suite->count <= 0) {
1807 pr_err("alg: skcipher: empty test suite for %s\n", driver);
1808 return -EINVAL;
1809 }
1810
1811 tfm = crypto_alloc_skcipher(driver, type, mask);
1812 if (IS_ERR(tfm)) {
1813 pr_err("alg: skcipher: failed to allocate transform for %s: %ld\n",
1814 driver, PTR_ERR(tfm));
1815 return PTR_ERR(tfm);
1816 }
1817
1818 req = skcipher_request_alloc(tfm, GFP_KERNEL);
1819 if (!req) {
1820 pr_err("alg: skcipher: failed to allocate request for %s\n",
1821 driver);
1822 err = -ENOMEM;
1823 goto out;
1824 }
1825
1826 tsgls = alloc_cipher_test_sglists();
1827 if (!tsgls) {
1828 pr_err("alg: skcipher: failed to allocate test buffers for %s\n",
1829 driver);
1830 err = -ENOMEM;
1831 goto out;
1832 }
1833
1834 err = test_skcipher(driver, ENCRYPT, suite, req, tsgls);
1835 if (err)
1836 goto out;
1837
1838 err = test_skcipher(driver, DECRYPT, suite, req, tsgls);
1839out:
1840 free_cipher_test_sglists(tsgls);
1841 skcipher_request_free(req);
1842 crypto_free_skcipher(tfm);
1843 return err;
1844}
1845
Eric Biggersb13b1e02017-02-24 15:46:59 -08001846static int test_comp(struct crypto_comp *tfm,
1847 const struct comp_testvec *ctemplate,
1848 const struct comp_testvec *dtemplate,
1849 int ctcount, int dtcount)
Herbert Xuda7f0332008-07-31 17:08:25 +08001850{
1851 const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
Mahipal Challa33607382018-04-11 20:28:32 +02001852 char *output, *decomp_output;
Herbert Xuda7f0332008-07-31 17:08:25 +08001853 unsigned int i;
Herbert Xuda7f0332008-07-31 17:08:25 +08001854 int ret;
1855
Mahipal Challa33607382018-04-11 20:28:32 +02001856 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1857 if (!output)
1858 return -ENOMEM;
1859
1860 decomp_output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1861 if (!decomp_output) {
1862 kfree(output);
1863 return -ENOMEM;
1864 }
1865
Herbert Xuda7f0332008-07-31 17:08:25 +08001866 for (i = 0; i < ctcount; i++) {
Geert Uytterhoevenc79cf912009-03-29 15:44:19 +08001867 int ilen;
1868 unsigned int dlen = COMP_BUF_SIZE;
Herbert Xuda7f0332008-07-31 17:08:25 +08001869
Michael Schupikov22a81182018-10-07 13:58:10 +02001870 memset(output, 0, COMP_BUF_SIZE);
1871 memset(decomp_output, 0, COMP_BUF_SIZE);
Herbert Xuda7f0332008-07-31 17:08:25 +08001872
1873 ilen = ctemplate[i].inlen;
1874 ret = crypto_comp_compress(tfm, ctemplate[i].input,
Mahipal Challa33607382018-04-11 20:28:32 +02001875 ilen, output, &dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08001876 if (ret) {
1877 printk(KERN_ERR "alg: comp: compression failed "
1878 "on test %d for %s: ret=%d\n", i + 1, algo,
1879 -ret);
1880 goto out;
1881 }
1882
Mahipal Challa33607382018-04-11 20:28:32 +02001883 ilen = dlen;
1884 dlen = COMP_BUF_SIZE;
1885 ret = crypto_comp_decompress(tfm, output,
1886 ilen, decomp_output, &dlen);
1887 if (ret) {
1888 pr_err("alg: comp: compression failed: decompress: on test %d for %s failed: ret=%d\n",
1889 i + 1, algo, -ret);
1890 goto out;
1891 }
1892
1893 if (dlen != ctemplate[i].inlen) {
Geert Uytterhoevenb812eb02008-11-28 20:51:28 +08001894 printk(KERN_ERR "alg: comp: Compression test %d "
1895 "failed for %s: output len = %d\n", i + 1, algo,
1896 dlen);
1897 ret = -EINVAL;
1898 goto out;
1899 }
1900
Mahipal Challa33607382018-04-11 20:28:32 +02001901 if (memcmp(decomp_output, ctemplate[i].input,
1902 ctemplate[i].inlen)) {
1903 pr_err("alg: comp: compression failed: output differs: on test %d for %s\n",
1904 i + 1, algo);
1905 hexdump(decomp_output, dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08001906 ret = -EINVAL;
1907 goto out;
1908 }
1909 }
1910
1911 for (i = 0; i < dtcount; i++) {
Geert Uytterhoevenc79cf912009-03-29 15:44:19 +08001912 int ilen;
1913 unsigned int dlen = COMP_BUF_SIZE;
Herbert Xuda7f0332008-07-31 17:08:25 +08001914
Michael Schupikov22a81182018-10-07 13:58:10 +02001915 memset(decomp_output, 0, COMP_BUF_SIZE);
Herbert Xuda7f0332008-07-31 17:08:25 +08001916
1917 ilen = dtemplate[i].inlen;
1918 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
Mahipal Challa33607382018-04-11 20:28:32 +02001919 ilen, decomp_output, &dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08001920 if (ret) {
1921 printk(KERN_ERR "alg: comp: decompression failed "
1922 "on test %d for %s: ret=%d\n", i + 1, algo,
1923 -ret);
1924 goto out;
1925 }
1926
Geert Uytterhoevenb812eb02008-11-28 20:51:28 +08001927 if (dlen != dtemplate[i].outlen) {
1928 printk(KERN_ERR "alg: comp: Decompression test %d "
1929 "failed for %s: output len = %d\n", i + 1, algo,
1930 dlen);
1931 ret = -EINVAL;
1932 goto out;
1933 }
1934
Mahipal Challa33607382018-04-11 20:28:32 +02001935 if (memcmp(decomp_output, dtemplate[i].output, dlen)) {
Herbert Xuda7f0332008-07-31 17:08:25 +08001936 printk(KERN_ERR "alg: comp: Decompression test %d "
1937 "failed for %s\n", i + 1, algo);
Mahipal Challa33607382018-04-11 20:28:32 +02001938 hexdump(decomp_output, dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08001939 ret = -EINVAL;
1940 goto out;
1941 }
1942 }
1943
1944 ret = 0;
1945
1946out:
Mahipal Challa33607382018-04-11 20:28:32 +02001947 kfree(decomp_output);
1948 kfree(output);
Herbert Xuda7f0332008-07-31 17:08:25 +08001949 return ret;
1950}
1951
Eric Biggersb13b1e02017-02-24 15:46:59 -08001952static int test_acomp(struct crypto_acomp *tfm,
Mahipal Challa33607382018-04-11 20:28:32 +02001953 const struct comp_testvec *ctemplate,
Eric Biggersb13b1e02017-02-24 15:46:59 -08001954 const struct comp_testvec *dtemplate,
1955 int ctcount, int dtcount)
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001956{
1957 const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
1958 unsigned int i;
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01001959 char *output, *decomp_out;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001960 int ret;
1961 struct scatterlist src, dst;
1962 struct acomp_req *req;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001963 struct crypto_wait wait;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001964
Eric Biggerseb095592016-11-23 10:24:35 -08001965 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1966 if (!output)
1967 return -ENOMEM;
1968
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01001969 decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1970 if (!decomp_out) {
1971 kfree(output);
1972 return -ENOMEM;
1973 }
1974
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001975 for (i = 0; i < ctcount; i++) {
1976 unsigned int dlen = COMP_BUF_SIZE;
1977 int ilen = ctemplate[i].inlen;
Laura Abbott02608e02016-12-21 12:32:54 -08001978 void *input_vec;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001979
Eric Biggersd2110222016-12-30 14:12:00 -06001980 input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL);
Laura Abbott02608e02016-12-21 12:32:54 -08001981 if (!input_vec) {
1982 ret = -ENOMEM;
1983 goto out;
1984 }
1985
Eric Biggerseb095592016-11-23 10:24:35 -08001986 memset(output, 0, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001987 crypto_init_wait(&wait);
Laura Abbott02608e02016-12-21 12:32:54 -08001988 sg_init_one(&src, input_vec, ilen);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001989 sg_init_one(&dst, output, dlen);
1990
1991 req = acomp_request_alloc(tfm);
1992 if (!req) {
1993 pr_err("alg: acomp: request alloc failed for %s\n",
1994 algo);
Laura Abbott02608e02016-12-21 12:32:54 -08001995 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001996 ret = -ENOMEM;
1997 goto out;
1998 }
1999
2000 acomp_request_set_params(req, &src, &dst, ilen, dlen);
2001 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002002 crypto_req_done, &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002003
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002004 ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002005 if (ret) {
2006 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
2007 i + 1, algo, -ret);
Laura Abbott02608e02016-12-21 12:32:54 -08002008 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002009 acomp_request_free(req);
2010 goto out;
2011 }
2012
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002013 ilen = req->dlen;
2014 dlen = COMP_BUF_SIZE;
2015 sg_init_one(&src, output, ilen);
2016 sg_init_one(&dst, decomp_out, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002017 crypto_init_wait(&wait);
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002018 acomp_request_set_params(req, &src, &dst, ilen, dlen);
2019
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002020 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002021 if (ret) {
2022 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
2023 i + 1, algo, -ret);
2024 kfree(input_vec);
2025 acomp_request_free(req);
2026 goto out;
2027 }
2028
2029 if (req->dlen != ctemplate[i].inlen) {
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002030 pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
2031 i + 1, algo, req->dlen);
2032 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08002033 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002034 acomp_request_free(req);
2035 goto out;
2036 }
2037
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002038 if (memcmp(input_vec, decomp_out, req->dlen)) {
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002039 pr_err("alg: acomp: Compression test %d failed for %s\n",
2040 i + 1, algo);
2041 hexdump(output, req->dlen);
2042 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08002043 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002044 acomp_request_free(req);
2045 goto out;
2046 }
2047
Laura Abbott02608e02016-12-21 12:32:54 -08002048 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002049 acomp_request_free(req);
2050 }
2051
2052 for (i = 0; i < dtcount; i++) {
2053 unsigned int dlen = COMP_BUF_SIZE;
2054 int ilen = dtemplate[i].inlen;
Laura Abbott02608e02016-12-21 12:32:54 -08002055 void *input_vec;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002056
Eric Biggersd2110222016-12-30 14:12:00 -06002057 input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL);
Laura Abbott02608e02016-12-21 12:32:54 -08002058 if (!input_vec) {
2059 ret = -ENOMEM;
2060 goto out;
2061 }
2062
Eric Biggerseb095592016-11-23 10:24:35 -08002063 memset(output, 0, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002064 crypto_init_wait(&wait);
Laura Abbott02608e02016-12-21 12:32:54 -08002065 sg_init_one(&src, input_vec, ilen);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002066 sg_init_one(&dst, output, dlen);
2067
2068 req = acomp_request_alloc(tfm);
2069 if (!req) {
2070 pr_err("alg: acomp: request alloc failed for %s\n",
2071 algo);
Laura Abbott02608e02016-12-21 12:32:54 -08002072 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002073 ret = -ENOMEM;
2074 goto out;
2075 }
2076
2077 acomp_request_set_params(req, &src, &dst, ilen, dlen);
2078 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002079 crypto_req_done, &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002080
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002081 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002082 if (ret) {
2083 pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
2084 i + 1, algo, -ret);
Laura Abbott02608e02016-12-21 12:32:54 -08002085 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002086 acomp_request_free(req);
2087 goto out;
2088 }
2089
2090 if (req->dlen != dtemplate[i].outlen) {
2091 pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
2092 i + 1, algo, req->dlen);
2093 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08002094 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002095 acomp_request_free(req);
2096 goto out;
2097 }
2098
2099 if (memcmp(output, dtemplate[i].output, req->dlen)) {
2100 pr_err("alg: acomp: Decompression test %d failed for %s\n",
2101 i + 1, algo);
2102 hexdump(output, req->dlen);
2103 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08002104 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002105 acomp_request_free(req);
2106 goto out;
2107 }
2108
Laura Abbott02608e02016-12-21 12:32:54 -08002109 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002110 acomp_request_free(req);
2111 }
2112
2113 ret = 0;
2114
2115out:
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002116 kfree(decomp_out);
Eric Biggerseb095592016-11-23 10:24:35 -08002117 kfree(output);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002118 return ret;
2119}
2120
Eric Biggersb13b1e02017-02-24 15:46:59 -08002121static int test_cprng(struct crypto_rng *tfm,
2122 const struct cprng_testvec *template,
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002123 unsigned int tcount)
2124{
2125 const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
Felipe Contrerasfa4ef8a2009-10-27 19:04:42 +08002126 int err = 0, i, j, seedsize;
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002127 u8 *seed;
2128 char result[32];
2129
2130 seedsize = crypto_rng_seedsize(tfm);
2131
2132 seed = kmalloc(seedsize, GFP_KERNEL);
2133 if (!seed) {
2134 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
2135 "for %s\n", algo);
2136 return -ENOMEM;
2137 }
2138
2139 for (i = 0; i < tcount; i++) {
2140 memset(result, 0, 32);
2141
2142 memcpy(seed, template[i].v, template[i].vlen);
2143 memcpy(seed + template[i].vlen, template[i].key,
2144 template[i].klen);
2145 memcpy(seed + template[i].vlen + template[i].klen,
2146 template[i].dt, template[i].dtlen);
2147
2148 err = crypto_rng_reset(tfm, seed, seedsize);
2149 if (err) {
2150 printk(KERN_ERR "alg: cprng: Failed to reset rng "
2151 "for %s\n", algo);
2152 goto out;
2153 }
2154
2155 for (j = 0; j < template[i].loops; j++) {
2156 err = crypto_rng_get_bytes(tfm, result,
2157 template[i].rlen);
Stephan Mueller19e60e12015-03-10 17:00:36 +01002158 if (err < 0) {
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002159 printk(KERN_ERR "alg: cprng: Failed to obtain "
2160 "the correct amount of random data for "
Stephan Mueller19e60e12015-03-10 17:00:36 +01002161 "%s (requested %d)\n", algo,
2162 template[i].rlen);
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002163 goto out;
2164 }
2165 }
2166
2167 err = memcmp(result, template[i].result,
2168 template[i].rlen);
2169 if (err) {
2170 printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
2171 i, algo);
2172 hexdump(result, template[i].rlen);
2173 err = -EINVAL;
2174 goto out;
2175 }
2176 }
2177
2178out:
2179 kfree(seed);
2180 return err;
2181}
2182
Herbert Xuda7f0332008-07-31 17:08:25 +08002183static int alg_test_cipher(const struct alg_test_desc *desc,
2184 const char *driver, u32 type, u32 mask)
2185{
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002186 const struct cipher_test_suite *suite = &desc->suite.cipher;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002187 struct crypto_cipher *tfm;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002188 int err;
Herbert Xuda7f0332008-07-31 17:08:25 +08002189
Herbert Xueed93e02016-11-22 20:08:31 +08002190 tfm = crypto_alloc_cipher(driver, type, mask);
Herbert Xuda7f0332008-07-31 17:08:25 +08002191 if (IS_ERR(tfm)) {
2192 printk(KERN_ERR "alg: cipher: Failed to load transform for "
2193 "%s: %ld\n", driver, PTR_ERR(tfm));
2194 return PTR_ERR(tfm);
2195 }
2196
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002197 err = test_cipher(tfm, ENCRYPT, suite->vecs, suite->count);
2198 if (!err)
2199 err = test_cipher(tfm, DECRYPT, suite->vecs, suite->count);
Herbert Xuda7f0332008-07-31 17:08:25 +08002200
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002201 crypto_free_cipher(tfm);
2202 return err;
2203}
2204
Herbert Xuda7f0332008-07-31 17:08:25 +08002205static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
2206 u32 type, u32 mask)
2207{
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002208 struct crypto_comp *comp;
2209 struct crypto_acomp *acomp;
Herbert Xuda7f0332008-07-31 17:08:25 +08002210 int err;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002211 u32 algo_type = type & CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
Herbert Xuda7f0332008-07-31 17:08:25 +08002212
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002213 if (algo_type == CRYPTO_ALG_TYPE_ACOMPRESS) {
2214 acomp = crypto_alloc_acomp(driver, type, mask);
2215 if (IS_ERR(acomp)) {
2216 pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
2217 driver, PTR_ERR(acomp));
2218 return PTR_ERR(acomp);
2219 }
2220 err = test_acomp(acomp, desc->suite.comp.comp.vecs,
2221 desc->suite.comp.decomp.vecs,
2222 desc->suite.comp.comp.count,
2223 desc->suite.comp.decomp.count);
2224 crypto_free_acomp(acomp);
2225 } else {
2226 comp = crypto_alloc_comp(driver, type, mask);
2227 if (IS_ERR(comp)) {
2228 pr_err("alg: comp: Failed to load transform for %s: %ld\n",
2229 driver, PTR_ERR(comp));
2230 return PTR_ERR(comp);
2231 }
2232
2233 err = test_comp(comp, desc->suite.comp.comp.vecs,
2234 desc->suite.comp.decomp.vecs,
2235 desc->suite.comp.comp.count,
2236 desc->suite.comp.decomp.count);
2237
2238 crypto_free_comp(comp);
Herbert Xuda7f0332008-07-31 17:08:25 +08002239 }
Herbert Xuda7f0332008-07-31 17:08:25 +08002240 return err;
2241}
2242
Herbert Xu8e3ee852008-11-07 14:58:52 +08002243static int alg_test_crc32c(const struct alg_test_desc *desc,
2244 const char *driver, u32 type, u32 mask)
2245{
2246 struct crypto_shash *tfm;
Eric Biggerscb9dde82019-01-10 12:17:55 -08002247 __le32 val;
Herbert Xu8e3ee852008-11-07 14:58:52 +08002248 int err;
2249
2250 err = alg_test_hash(desc, driver, type, mask);
2251 if (err)
Eric Biggerseb5e6732019-01-23 20:57:35 -08002252 return err;
Herbert Xu8e3ee852008-11-07 14:58:52 +08002253
Herbert Xueed93e02016-11-22 20:08:31 +08002254 tfm = crypto_alloc_shash(driver, type, mask);
Herbert Xu8e3ee852008-11-07 14:58:52 +08002255 if (IS_ERR(tfm)) {
Eric Biggerseb5e6732019-01-23 20:57:35 -08002256 if (PTR_ERR(tfm) == -ENOENT) {
2257 /*
2258 * This crc32c implementation is only available through
2259 * ahash API, not the shash API, so the remaining part
2260 * of the test is not applicable to it.
2261 */
2262 return 0;
2263 }
Herbert Xu8e3ee852008-11-07 14:58:52 +08002264 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
2265 "%ld\n", driver, PTR_ERR(tfm));
Eric Biggerseb5e6732019-01-23 20:57:35 -08002266 return PTR_ERR(tfm);
Herbert Xu8e3ee852008-11-07 14:58:52 +08002267 }
2268
2269 do {
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02002270 SHASH_DESC_ON_STACK(shash, tfm);
2271 u32 *ctx = (u32 *)shash_desc_ctx(shash);
Herbert Xu8e3ee852008-11-07 14:58:52 +08002272
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02002273 shash->tfm = tfm;
2274 shash->flags = 0;
Herbert Xu8e3ee852008-11-07 14:58:52 +08002275
Eric Biggerscb9dde82019-01-10 12:17:55 -08002276 *ctx = 420553207;
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02002277 err = crypto_shash_final(shash, (u8 *)&val);
Herbert Xu8e3ee852008-11-07 14:58:52 +08002278 if (err) {
2279 printk(KERN_ERR "alg: crc32c: Operation failed for "
2280 "%s: %d\n", driver, err);
2281 break;
2282 }
2283
Eric Biggerscb9dde82019-01-10 12:17:55 -08002284 if (val != cpu_to_le32(~420553207)) {
2285 pr_err("alg: crc32c: Test failed for %s: %u\n",
2286 driver, le32_to_cpu(val));
Herbert Xu8e3ee852008-11-07 14:58:52 +08002287 err = -EINVAL;
2288 }
2289 } while (0);
2290
2291 crypto_free_shash(tfm);
2292
Herbert Xu8e3ee852008-11-07 14:58:52 +08002293 return err;
2294}
2295
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002296static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
2297 u32 type, u32 mask)
2298{
2299 struct crypto_rng *rng;
2300 int err;
2301
Herbert Xueed93e02016-11-22 20:08:31 +08002302 rng = crypto_alloc_rng(driver, type, mask);
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002303 if (IS_ERR(rng)) {
2304 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
2305 "%ld\n", driver, PTR_ERR(rng));
2306 return PTR_ERR(rng);
2307 }
2308
2309 err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
2310
2311 crypto_free_rng(rng);
2312
2313 return err;
2314}
2315
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002316
Eric Biggersb13b1e02017-02-24 15:46:59 -08002317static int drbg_cavs_test(const struct drbg_testvec *test, int pr,
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002318 const char *driver, u32 type, u32 mask)
2319{
2320 int ret = -EAGAIN;
2321 struct crypto_rng *drng;
2322 struct drbg_test_data test_data;
2323 struct drbg_string addtl, pers, testentropy;
2324 unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
2325
2326 if (!buf)
2327 return -ENOMEM;
2328
Herbert Xueed93e02016-11-22 20:08:31 +08002329 drng = crypto_alloc_rng(driver, type, mask);
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002330 if (IS_ERR(drng)) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04002331 printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002332 "%s\n", driver);
2333 kzfree(buf);
2334 return -ENOMEM;
2335 }
2336
2337 test_data.testentropy = &testentropy;
2338 drbg_string_fill(&testentropy, test->entropy, test->entropylen);
2339 drbg_string_fill(&pers, test->pers, test->perslen);
2340 ret = crypto_drbg_reset_test(drng, &pers, &test_data);
2341 if (ret) {
2342 printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
2343 goto outbuf;
2344 }
2345
2346 drbg_string_fill(&addtl, test->addtla, test->addtllen);
2347 if (pr) {
2348 drbg_string_fill(&testentropy, test->entpra, test->entprlen);
2349 ret = crypto_drbg_get_bytes_addtl_test(drng,
2350 buf, test->expectedlen, &addtl, &test_data);
2351 } else {
2352 ret = crypto_drbg_get_bytes_addtl(drng,
2353 buf, test->expectedlen, &addtl);
2354 }
Stephan Mueller19e60e12015-03-10 17:00:36 +01002355 if (ret < 0) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04002356 printk(KERN_ERR "alg: drbg: could not obtain random data for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002357 "driver %s\n", driver);
2358 goto outbuf;
2359 }
2360
2361 drbg_string_fill(&addtl, test->addtlb, test->addtllen);
2362 if (pr) {
2363 drbg_string_fill(&testentropy, test->entprb, test->entprlen);
2364 ret = crypto_drbg_get_bytes_addtl_test(drng,
2365 buf, test->expectedlen, &addtl, &test_data);
2366 } else {
2367 ret = crypto_drbg_get_bytes_addtl(drng,
2368 buf, test->expectedlen, &addtl);
2369 }
Stephan Mueller19e60e12015-03-10 17:00:36 +01002370 if (ret < 0) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04002371 printk(KERN_ERR "alg: drbg: could not obtain random data for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002372 "driver %s\n", driver);
2373 goto outbuf;
2374 }
2375
2376 ret = memcmp(test->expected, buf, test->expectedlen);
2377
2378outbuf:
2379 crypto_free_rng(drng);
2380 kzfree(buf);
2381 return ret;
2382}
2383
2384
2385static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
2386 u32 type, u32 mask)
2387{
2388 int err = 0;
2389 int pr = 0;
2390 int i = 0;
Eric Biggersb13b1e02017-02-24 15:46:59 -08002391 const struct drbg_testvec *template = desc->suite.drbg.vecs;
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002392 unsigned int tcount = desc->suite.drbg.count;
2393
2394 if (0 == memcmp(driver, "drbg_pr_", 8))
2395 pr = 1;
2396
2397 for (i = 0; i < tcount; i++) {
2398 err = drbg_cavs_test(&template[i], pr, driver, type, mask);
2399 if (err) {
2400 printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
2401 i, driver);
2402 err = -EINVAL;
2403 break;
2404 }
2405 }
2406 return err;
2407
2408}
2409
Eric Biggersb13b1e02017-02-24 15:46:59 -08002410static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002411 const char *alg)
2412{
2413 struct kpp_request *req;
2414 void *input_buf = NULL;
2415 void *output_buf = NULL;
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002416 void *a_public = NULL;
2417 void *a_ss = NULL;
2418 void *shared_secret = NULL;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002419 struct crypto_wait wait;
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002420 unsigned int out_len_max;
2421 int err = -ENOMEM;
2422 struct scatterlist src, dst;
2423
2424 req = kpp_request_alloc(tfm, GFP_KERNEL);
2425 if (!req)
2426 return err;
2427
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002428 crypto_init_wait(&wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002429
2430 err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
2431 if (err < 0)
2432 goto free_req;
2433
2434 out_len_max = crypto_kpp_maxsize(tfm);
2435 output_buf = kzalloc(out_len_max, GFP_KERNEL);
2436 if (!output_buf) {
2437 err = -ENOMEM;
2438 goto free_req;
2439 }
2440
2441 /* Use appropriate parameter as base */
2442 kpp_request_set_input(req, NULL, 0);
2443 sg_init_one(&dst, output_buf, out_len_max);
2444 kpp_request_set_output(req, &dst, out_len_max);
2445 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002446 crypto_req_done, &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002447
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002448 /* Compute party A's public key */
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002449 err = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002450 if (err) {
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002451 pr_err("alg: %s: Party A: generate public key test failed. err %d\n",
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002452 alg, err);
2453 goto free_output;
2454 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002455
2456 if (vec->genkey) {
2457 /* Save party A's public key */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05002458 a_public = kmemdup(sg_virt(req->dst), out_len_max, GFP_KERNEL);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002459 if (!a_public) {
2460 err = -ENOMEM;
2461 goto free_output;
2462 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002463 } else {
2464 /* Verify calculated public key */
2465 if (memcmp(vec->expected_a_public, sg_virt(req->dst),
2466 vec->expected_a_public_size)) {
2467 pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n",
2468 alg);
2469 err = -EINVAL;
2470 goto free_output;
2471 }
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002472 }
2473
2474 /* Calculate shared secret key by using counter part (b) public key. */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05002475 input_buf = kmemdup(vec->b_public, vec->b_public_size, GFP_KERNEL);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002476 if (!input_buf) {
2477 err = -ENOMEM;
2478 goto free_output;
2479 }
2480
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002481 sg_init_one(&src, input_buf, vec->b_public_size);
2482 sg_init_one(&dst, output_buf, out_len_max);
2483 kpp_request_set_input(req, &src, vec->b_public_size);
2484 kpp_request_set_output(req, &dst, out_len_max);
2485 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002486 crypto_req_done, &wait);
2487 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002488 if (err) {
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002489 pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n",
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002490 alg, err);
2491 goto free_all;
2492 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002493
2494 if (vec->genkey) {
2495 /* Save the shared secret obtained by party A */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05002496 a_ss = kmemdup(sg_virt(req->dst), vec->expected_ss_size, GFP_KERNEL);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002497 if (!a_ss) {
2498 err = -ENOMEM;
2499 goto free_all;
2500 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002501
2502 /*
2503 * Calculate party B's shared secret by using party A's
2504 * public key.
2505 */
2506 err = crypto_kpp_set_secret(tfm, vec->b_secret,
2507 vec->b_secret_size);
2508 if (err < 0)
2509 goto free_all;
2510
2511 sg_init_one(&src, a_public, vec->expected_a_public_size);
2512 sg_init_one(&dst, output_buf, out_len_max);
2513 kpp_request_set_input(req, &src, vec->expected_a_public_size);
2514 kpp_request_set_output(req, &dst, out_len_max);
2515 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002516 crypto_req_done, &wait);
2517 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req),
2518 &wait);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002519 if (err) {
2520 pr_err("alg: %s: Party B: compute shared secret failed. err %d\n",
2521 alg, err);
2522 goto free_all;
2523 }
2524
2525 shared_secret = a_ss;
2526 } else {
2527 shared_secret = (void *)vec->expected_ss;
2528 }
2529
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002530 /*
2531 * verify shared secret from which the user will derive
2532 * secret key by executing whatever hash it has chosen
2533 */
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002534 if (memcmp(shared_secret, sg_virt(req->dst),
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002535 vec->expected_ss_size)) {
2536 pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
2537 alg);
2538 err = -EINVAL;
2539 }
2540
2541free_all:
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002542 kfree(a_ss);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002543 kfree(input_buf);
2544free_output:
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002545 kfree(a_public);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002546 kfree(output_buf);
2547free_req:
2548 kpp_request_free(req);
2549 return err;
2550}
2551
2552static int test_kpp(struct crypto_kpp *tfm, const char *alg,
Eric Biggersb13b1e02017-02-24 15:46:59 -08002553 const struct kpp_testvec *vecs, unsigned int tcount)
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002554{
2555 int ret, i;
2556
2557 for (i = 0; i < tcount; i++) {
2558 ret = do_test_kpp(tfm, vecs++, alg);
2559 if (ret) {
2560 pr_err("alg: %s: test failed on vector %d, err=%d\n",
2561 alg, i + 1, ret);
2562 return ret;
2563 }
2564 }
2565 return 0;
2566}
2567
2568static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
2569 u32 type, u32 mask)
2570{
2571 struct crypto_kpp *tfm;
2572 int err = 0;
2573
Herbert Xueed93e02016-11-22 20:08:31 +08002574 tfm = crypto_alloc_kpp(driver, type, mask);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002575 if (IS_ERR(tfm)) {
2576 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
2577 driver, PTR_ERR(tfm));
2578 return PTR_ERR(tfm);
2579 }
2580 if (desc->suite.kpp.vecs)
2581 err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
2582 desc->suite.kpp.count);
2583
2584 crypto_free_kpp(tfm);
2585 return err;
2586}
2587
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03002588static u8 *test_pack_u32(u8 *dst, u32 val)
2589{
2590 memcpy(dst, &val, sizeof(val));
2591 return dst + sizeof(val);
2592}
2593
Herbert Xu50d2b6432016-06-29 19:32:20 +08002594static int test_akcipher_one(struct crypto_akcipher *tfm,
Eric Biggersb13b1e02017-02-24 15:46:59 -08002595 const struct akcipher_testvec *vecs)
Tadeusz Struk946cc462015-06-16 10:31:06 -07002596{
Herbert Xudf27b262016-05-05 16:42:49 +08002597 char *xbuf[XBUFSIZE];
Tadeusz Struk946cc462015-06-16 10:31:06 -07002598 struct akcipher_request *req;
2599 void *outbuf_enc = NULL;
2600 void *outbuf_dec = NULL;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002601 struct crypto_wait wait;
Tadeusz Struk946cc462015-06-16 10:31:06 -07002602 unsigned int out_len_max, out_len = 0;
2603 int err = -ENOMEM;
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03002604 struct scatterlist src, dst, src_tab[3];
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002605 const char *m, *c;
2606 unsigned int m_size, c_size;
2607 const char *op;
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03002608 u8 *key, *ptr;
Tadeusz Struk946cc462015-06-16 10:31:06 -07002609
Herbert Xudf27b262016-05-05 16:42:49 +08002610 if (testmgr_alloc_buf(xbuf))
2611 return err;
2612
Tadeusz Struk946cc462015-06-16 10:31:06 -07002613 req = akcipher_request_alloc(tfm, GFP_KERNEL);
2614 if (!req)
Herbert Xudf27b262016-05-05 16:42:49 +08002615 goto free_xbuf;
Tadeusz Struk946cc462015-06-16 10:31:06 -07002616
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002617 crypto_init_wait(&wait);
Tadeusz Struk22287b02015-10-08 09:26:55 -07002618
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03002619 key = kmalloc(vecs->key_len + sizeof(u32) * 2 + vecs->param_len,
2620 GFP_KERNEL);
2621 if (!key)
2622 goto free_xbuf;
2623 memcpy(key, vecs->key, vecs->key_len);
2624 ptr = key + vecs->key_len;
2625 ptr = test_pack_u32(ptr, vecs->algo);
2626 ptr = test_pack_u32(ptr, vecs->param_len);
2627 memcpy(ptr, vecs->params, vecs->param_len);
2628
Tadeusz Struk22287b02015-10-08 09:26:55 -07002629 if (vecs->public_key_vec)
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03002630 err = crypto_akcipher_set_pub_key(tfm, key, vecs->key_len);
Tadeusz Struk22287b02015-10-08 09:26:55 -07002631 else
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03002632 err = crypto_akcipher_set_priv_key(tfm, key, vecs->key_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002633 if (err)
2634 goto free_req;
2635
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002636 /*
2637 * First run test which do not require a private key, such as
2638 * encrypt or verify.
2639 */
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03002640 err = -ENOMEM;
2641 out_len_max = crypto_akcipher_maxsize(tfm);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002642 outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
2643 if (!outbuf_enc)
2644 goto free_req;
2645
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002646 if (!vecs->siggen_sigver_test) {
2647 m = vecs->m;
2648 m_size = vecs->m_size;
2649 c = vecs->c;
2650 c_size = vecs->c_size;
2651 op = "encrypt";
2652 } else {
2653 /* Swap args so we could keep plaintext (digest)
2654 * in vecs->m, and cooked signature in vecs->c.
2655 */
2656 m = vecs->c; /* signature */
2657 m_size = vecs->c_size;
2658 c = vecs->m; /* digest */
2659 c_size = vecs->m_size;
2660 op = "verify";
2661 }
Herbert Xudf27b262016-05-05 16:42:49 +08002662
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002663 if (WARN_ON(m_size > PAGE_SIZE))
2664 goto free_all;
2665 memcpy(xbuf[0], m, m_size);
Herbert Xudf27b262016-05-05 16:42:49 +08002666
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03002667 sg_init_table(src_tab, 3);
Herbert Xudf27b262016-05-05 16:42:49 +08002668 sg_set_buf(&src_tab[0], xbuf[0], 8);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002669 sg_set_buf(&src_tab[1], xbuf[0] + 8, m_size - 8);
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03002670 if (vecs->siggen_sigver_test) {
2671 if (WARN_ON(c_size > PAGE_SIZE))
2672 goto free_all;
2673 memcpy(xbuf[1], c, c_size);
2674 sg_set_buf(&src_tab[2], xbuf[1], c_size);
2675 akcipher_request_set_crypt(req, src_tab, NULL, m_size, c_size);
2676 } else {
2677 sg_init_one(&dst, outbuf_enc, out_len_max);
2678 akcipher_request_set_crypt(req, src_tab, &dst, m_size,
2679 out_len_max);
2680 }
Tadeusz Struk946cc462015-06-16 10:31:06 -07002681 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002682 crypto_req_done, &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002683
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002684 err = crypto_wait_req(vecs->siggen_sigver_test ?
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002685 /* Run asymmetric signature verification */
2686 crypto_akcipher_verify(req) :
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002687 /* Run asymmetric encrypt */
2688 crypto_akcipher_encrypt(req), &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002689 if (err) {
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002690 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002691 goto free_all;
2692 }
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03002693 if (!vecs->siggen_sigver_test) {
2694 if (req->dst_len != c_size) {
2695 pr_err("alg: akcipher: %s test failed. Invalid output len\n",
2696 op);
2697 err = -EINVAL;
2698 goto free_all;
2699 }
2700 /* verify that encrypted message is equal to expected */
2701 if (memcmp(c, outbuf_enc, c_size) != 0) {
2702 pr_err("alg: akcipher: %s test failed. Invalid output\n",
2703 op);
2704 hexdump(outbuf_enc, c_size);
2705 err = -EINVAL;
2706 goto free_all;
2707 }
Tadeusz Struk946cc462015-06-16 10:31:06 -07002708 }
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002709
2710 /*
2711 * Don't invoke (decrypt or sign) test which require a private key
2712 * for vectors with only a public key.
2713 */
Tadeusz Struk946cc462015-06-16 10:31:06 -07002714 if (vecs->public_key_vec) {
2715 err = 0;
2716 goto free_all;
2717 }
2718 outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
2719 if (!outbuf_dec) {
2720 err = -ENOMEM;
2721 goto free_all;
2722 }
Herbert Xudf27b262016-05-05 16:42:49 +08002723
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002724 op = vecs->siggen_sigver_test ? "sign" : "decrypt";
2725 if (WARN_ON(c_size > PAGE_SIZE))
Herbert Xudf27b262016-05-05 16:42:49 +08002726 goto free_all;
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002727 memcpy(xbuf[0], c, c_size);
Herbert Xudf27b262016-05-05 16:42:49 +08002728
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002729 sg_init_one(&src, xbuf[0], c_size);
Tadeusz Struk22287b02015-10-08 09:26:55 -07002730 sg_init_one(&dst, outbuf_dec, out_len_max);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002731 crypto_init_wait(&wait);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002732 akcipher_request_set_crypt(req, &src, &dst, c_size, out_len_max);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002733
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002734 err = crypto_wait_req(vecs->siggen_sigver_test ?
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002735 /* Run asymmetric signature generation */
2736 crypto_akcipher_sign(req) :
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002737 /* Run asymmetric decrypt */
2738 crypto_akcipher_decrypt(req), &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002739 if (err) {
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002740 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002741 goto free_all;
2742 }
2743 out_len = req->dst_len;
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002744 if (out_len < m_size) {
2745 pr_err("alg: akcipher: %s test failed. Invalid output len %u\n",
2746 op, out_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002747 err = -EINVAL;
2748 goto free_all;
2749 }
2750 /* verify that decrypted message is equal to the original msg */
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002751 if (memchr_inv(outbuf_dec, 0, out_len - m_size) ||
2752 memcmp(m, outbuf_dec + out_len - m_size, m_size)) {
2753 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
Herbert Xu50d2b6432016-06-29 19:32:20 +08002754 hexdump(outbuf_dec, out_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002755 err = -EINVAL;
2756 }
2757free_all:
2758 kfree(outbuf_dec);
2759 kfree(outbuf_enc);
2760free_req:
2761 akcipher_request_free(req);
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03002762 kfree(key);
Herbert Xudf27b262016-05-05 16:42:49 +08002763free_xbuf:
2764 testmgr_free_buf(xbuf);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002765 return err;
2766}
2767
Herbert Xu50d2b6432016-06-29 19:32:20 +08002768static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
Eric Biggersb13b1e02017-02-24 15:46:59 -08002769 const struct akcipher_testvec *vecs,
2770 unsigned int tcount)
Tadeusz Struk946cc462015-06-16 10:31:06 -07002771{
Herbert Xu15226e42016-07-18 18:20:10 +08002772 const char *algo =
2773 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
Tadeusz Struk946cc462015-06-16 10:31:06 -07002774 int ret, i;
2775
2776 for (i = 0; i < tcount; i++) {
Herbert Xu50d2b6432016-06-29 19:32:20 +08002777 ret = test_akcipher_one(tfm, vecs++);
2778 if (!ret)
2779 continue;
2780
Herbert Xu15226e42016-07-18 18:20:10 +08002781 pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
2782 i + 1, algo, ret);
Herbert Xu50d2b6432016-06-29 19:32:20 +08002783 return ret;
Tadeusz Struk946cc462015-06-16 10:31:06 -07002784 }
2785 return 0;
2786}
2787
Tadeusz Struk946cc462015-06-16 10:31:06 -07002788static int alg_test_akcipher(const struct alg_test_desc *desc,
2789 const char *driver, u32 type, u32 mask)
2790{
2791 struct crypto_akcipher *tfm;
2792 int err = 0;
2793
Herbert Xueed93e02016-11-22 20:08:31 +08002794 tfm = crypto_alloc_akcipher(driver, type, mask);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002795 if (IS_ERR(tfm)) {
2796 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
2797 driver, PTR_ERR(tfm));
2798 return PTR_ERR(tfm);
2799 }
2800 if (desc->suite.akcipher.vecs)
2801 err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
2802 desc->suite.akcipher.count);
2803
2804 crypto_free_akcipher(tfm);
2805 return err;
2806}
2807
Youquan, Song863b5572009-12-23 19:45:20 +08002808static int alg_test_null(const struct alg_test_desc *desc,
2809 const char *driver, u32 type, u32 mask)
2810{
2811 return 0;
2812}
2813
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002814#define __VECS(tv) { .vecs = tv, .count = ARRAY_SIZE(tv) }
2815
Herbert Xuda7f0332008-07-31 17:08:25 +08002816/* Please keep this list sorted by algorithm name. */
2817static const struct alg_test_desc alg_test_descs[] = {
2818 {
Eric Biggers059c2a42018-11-16 17:26:31 -08002819 .alg = "adiantum(xchacha12,aes)",
2820 .test = alg_test_skcipher,
2821 .suite = {
2822 .cipher = __VECS(adiantum_xchacha12_aes_tv_template)
2823 },
2824 }, {
2825 .alg = "adiantum(xchacha20,aes)",
2826 .test = alg_test_skcipher,
2827 .suite = {
2828 .cipher = __VECS(adiantum_xchacha20_aes_tv_template)
2829 },
2830 }, {
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02002831 .alg = "aegis128",
2832 .test = alg_test_aead,
2833 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002834 .aead = __VECS(aegis128_tv_template)
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02002835 }
2836 }, {
2837 .alg = "aegis128l",
2838 .test = alg_test_aead,
2839 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002840 .aead = __VECS(aegis128l_tv_template)
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02002841 }
2842 }, {
2843 .alg = "aegis256",
2844 .test = alg_test_aead,
2845 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002846 .aead = __VECS(aegis256_tv_template)
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02002847 }
2848 }, {
Jarod Wilsone08ca2d2009-05-04 19:46:29 +08002849 .alg = "ansi_cprng",
2850 .test = alg_test_cprng,
2851 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002852 .cprng = __VECS(ansi_cprng_aes_tv_template)
Jarod Wilsone08ca2d2009-05-04 19:46:29 +08002853 }
2854 }, {
Horia Geantabca4feb2014-03-14 17:46:51 +02002855 .alg = "authenc(hmac(md5),ecb(cipher_null))",
2856 .test = alg_test_aead,
Horia Geantabca4feb2014-03-14 17:46:51 +02002857 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002858 .aead = __VECS(hmac_md5_ecb_cipher_null_tv_template)
Horia Geantabca4feb2014-03-14 17:46:51 +02002859 }
2860 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002861 .alg = "authenc(hmac(sha1),cbc(aes))",
Horia Geantae46e9a42012-07-03 19:16:54 +03002862 .test = alg_test_aead,
Herbert Xubcf741c2017-06-28 19:09:07 +08002863 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03002864 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002865 .aead = __VECS(hmac_sha1_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302866 }
2867 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002868 .alg = "authenc(hmac(sha1),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302869 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302870 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002871 .aead = __VECS(hmac_sha1_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302872 }
2873 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002874 .alg = "authenc(hmac(sha1),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302875 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01002876 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302877 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002878 .aead = __VECS(hmac_sha1_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03002879 }
2880 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01002881 .alg = "authenc(hmac(sha1),ctr(aes))",
2882 .test = alg_test_null,
2883 .fips_allowed = 1,
2884 }, {
Horia Geantabca4feb2014-03-14 17:46:51 +02002885 .alg = "authenc(hmac(sha1),ecb(cipher_null))",
2886 .test = alg_test_aead,
Horia Geantabca4feb2014-03-14 17:46:51 +02002887 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002888 .aead = __VECS(hmac_sha1_ecb_cipher_null_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302889 }
2890 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01002891 .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
2892 .test = alg_test_null,
2893 .fips_allowed = 1,
2894 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002895 .alg = "authenc(hmac(sha224),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302896 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302897 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002898 .aead = __VECS(hmac_sha224_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302899 }
2900 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002901 .alg = "authenc(hmac(sha224),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302902 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01002903 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302904 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002905 .aead = __VECS(hmac_sha224_des3_ede_cbc_tv_temp)
Horia Geantabca4feb2014-03-14 17:46:51 +02002906 }
2907 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002908 .alg = "authenc(hmac(sha256),cbc(aes))",
Horia Geantae46e9a42012-07-03 19:16:54 +03002909 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01002910 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03002911 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002912 .aead = __VECS(hmac_sha256_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302913 }
2914 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002915 .alg = "authenc(hmac(sha256),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302916 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302917 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002918 .aead = __VECS(hmac_sha256_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302919 }
2920 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002921 .alg = "authenc(hmac(sha256),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302922 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01002923 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302924 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002925 .aead = __VECS(hmac_sha256_des3_ede_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302926 }
2927 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01002928 .alg = "authenc(hmac(sha256),ctr(aes))",
2929 .test = alg_test_null,
2930 .fips_allowed = 1,
2931 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01002932 .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
2933 .test = alg_test_null,
2934 .fips_allowed = 1,
2935 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002936 .alg = "authenc(hmac(sha384),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302937 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302938 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002939 .aead = __VECS(hmac_sha384_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302940 }
2941 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002942 .alg = "authenc(hmac(sha384),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302943 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01002944 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302945 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002946 .aead = __VECS(hmac_sha384_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03002947 }
2948 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01002949 .alg = "authenc(hmac(sha384),ctr(aes))",
2950 .test = alg_test_null,
2951 .fips_allowed = 1,
2952 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01002953 .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
2954 .test = alg_test_null,
2955 .fips_allowed = 1,
2956 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002957 .alg = "authenc(hmac(sha512),cbc(aes))",
Marcus Meissnered1afac2016-02-05 14:23:33 +01002958 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03002959 .test = alg_test_aead,
Horia Geantae46e9a42012-07-03 19:16:54 +03002960 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002961 .aead = __VECS(hmac_sha512_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302962 }
2963 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002964 .alg = "authenc(hmac(sha512),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302965 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302966 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002967 .aead = __VECS(hmac_sha512_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302968 }
2969 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002970 .alg = "authenc(hmac(sha512),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302971 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01002972 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302973 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002974 .aead = __VECS(hmac_sha512_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03002975 }
2976 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01002977 .alg = "authenc(hmac(sha512),ctr(aes))",
2978 .test = alg_test_null,
2979 .fips_allowed = 1,
2980 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01002981 .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
2982 .test = alg_test_null,
2983 .fips_allowed = 1,
2984 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08002985 .alg = "cbc(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002986 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10002987 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08002988 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002989 .cipher = __VECS(aes_cbc_tv_template)
2990 },
Herbert Xuda7f0332008-07-31 17:08:25 +08002991 }, {
2992 .alg = "cbc(anubis)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002993 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08002994 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002995 .cipher = __VECS(anubis_cbc_tv_template)
2996 },
Herbert Xuda7f0332008-07-31 17:08:25 +08002997 }, {
2998 .alg = "cbc(blowfish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002999 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003000 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003001 .cipher = __VECS(bf_cbc_tv_template)
3002 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003003 }, {
3004 .alg = "cbc(camellia)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003005 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003006 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003007 .cipher = __VECS(camellia_cbc_tv_template)
3008 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003009 }, {
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02003010 .alg = "cbc(cast5)",
3011 .test = alg_test_skcipher,
3012 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003013 .cipher = __VECS(cast5_cbc_tv_template)
3014 },
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02003015 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003016 .alg = "cbc(cast6)",
3017 .test = alg_test_skcipher,
3018 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003019 .cipher = __VECS(cast6_cbc_tv_template)
3020 },
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003021 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003022 .alg = "cbc(des)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003023 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003024 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003025 .cipher = __VECS(des_cbc_tv_template)
3026 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003027 }, {
3028 .alg = "cbc(des3_ede)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003029 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003030 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003031 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003032 .cipher = __VECS(des3_ede_cbc_tv_template)
3033 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003034 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01003035 /* Same as cbc(aes) except the key is stored in
3036 * hardware secure memory which we reference by index
3037 */
3038 .alg = "cbc(paes)",
3039 .test = alg_test_null,
3040 .fips_allowed = 1,
3041 }, {
Jussi Kivilinna9d259172011-10-18 00:02:53 +03003042 .alg = "cbc(serpent)",
3043 .test = alg_test_skcipher,
3044 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003045 .cipher = __VECS(serpent_cbc_tv_template)
3046 },
Jussi Kivilinna9d259172011-10-18 00:02:53 +03003047 }, {
Gilad Ben-Yossef95ba5972018-09-20 14:18:38 +01003048 .alg = "cbc(sm4)",
3049 .test = alg_test_skcipher,
3050 .suite = {
3051 .cipher = __VECS(sm4_cbc_tv_template)
3052 }
3053 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003054 .alg = "cbc(twofish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003055 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003056 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003057 .cipher = __VECS(tf_cbc_tv_template)
3058 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003059 }, {
Ard Biesheuvel092acf02017-02-03 14:49:35 +00003060 .alg = "cbcmac(aes)",
3061 .fips_allowed = 1,
3062 .test = alg_test_hash,
3063 .suite = {
3064 .hash = __VECS(aes_cbcmac_tv_template)
3065 }
3066 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003067 .alg = "ccm(aes)",
3068 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003069 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003070 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003071 .aead = __VECS(aes_ccm_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003072 }
3073 }, {
Dmitry Eremin-Solenikov7da66672018-10-20 02:01:53 +03003074 .alg = "cfb(aes)",
3075 .test = alg_test_skcipher,
3076 .fips_allowed = 1,
3077 .suite = {
3078 .cipher = __VECS(aes_cfb_tv_template)
3079 },
3080 }, {
Martin Willi3590ebf2015-06-01 13:43:57 +02003081 .alg = "chacha20",
3082 .test = alg_test_skcipher,
3083 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003084 .cipher = __VECS(chacha20_tv_template)
3085 },
Martin Willi3590ebf2015-06-01 13:43:57 +02003086 }, {
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03003087 .alg = "cmac(aes)",
Stephan Mueller8f183752015-08-19 08:42:07 +02003088 .fips_allowed = 1,
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03003089 .test = alg_test_hash,
3090 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003091 .hash = __VECS(aes_cmac128_tv_template)
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03003092 }
3093 }, {
3094 .alg = "cmac(des3_ede)",
Stephan Mueller8f183752015-08-19 08:42:07 +02003095 .fips_allowed = 1,
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03003096 .test = alg_test_hash,
3097 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003098 .hash = __VECS(des3_ede_cmac64_tv_template)
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03003099 }
3100 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03003101 .alg = "compress_null",
3102 .test = alg_test_null,
3103 }, {
Ard Biesheuvelebb34722015-05-04 11:00:17 +02003104 .alg = "crc32",
3105 .test = alg_test_hash,
Milan Broza8a34412019-01-25 10:31:47 +01003106 .fips_allowed = 1,
Ard Biesheuvelebb34722015-05-04 11:00:17 +02003107 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003108 .hash = __VECS(crc32_tv_template)
Ard Biesheuvelebb34722015-05-04 11:00:17 +02003109 }
3110 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003111 .alg = "crc32c",
Herbert Xu8e3ee852008-11-07 14:58:52 +08003112 .test = alg_test_crc32c,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003113 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003114 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003115 .hash = __VECS(crc32c_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003116 }
3117 }, {
Herbert Xu684115212013-09-07 12:56:26 +10003118 .alg = "crct10dif",
3119 .test = alg_test_hash,
3120 .fips_allowed = 1,
3121 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003122 .hash = __VECS(crct10dif_tv_template)
Herbert Xu684115212013-09-07 12:56:26 +10003123 }
3124 }, {
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08003125 .alg = "ctr(aes)",
3126 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003127 .fips_allowed = 1,
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08003128 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003129 .cipher = __VECS(aes_ctr_tv_template)
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08003130 }
3131 }, {
Jussi Kivilinna85b63e32011-10-10 23:03:03 +03003132 .alg = "ctr(blowfish)",
3133 .test = alg_test_skcipher,
3134 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003135 .cipher = __VECS(bf_ctr_tv_template)
Jussi Kivilinna85b63e32011-10-10 23:03:03 +03003136 }
3137 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02003138 .alg = "ctr(camellia)",
3139 .test = alg_test_skcipher,
3140 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003141 .cipher = __VECS(camellia_ctr_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02003142 }
3143 }, {
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02003144 .alg = "ctr(cast5)",
3145 .test = alg_test_skcipher,
3146 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003147 .cipher = __VECS(cast5_ctr_tv_template)
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02003148 }
3149 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003150 .alg = "ctr(cast6)",
3151 .test = alg_test_skcipher,
3152 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003153 .cipher = __VECS(cast6_ctr_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003154 }
3155 }, {
Jussi Kivilinna8163fc32012-10-20 14:53:07 +03003156 .alg = "ctr(des)",
3157 .test = alg_test_skcipher,
3158 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003159 .cipher = __VECS(des_ctr_tv_template)
Jussi Kivilinna8163fc32012-10-20 14:53:07 +03003160 }
3161 }, {
Jussi Kivilinnae080b172012-10-20 14:53:12 +03003162 .alg = "ctr(des3_ede)",
3163 .test = alg_test_skcipher,
Marcelo Cerri0d8da102017-03-20 17:28:05 -03003164 .fips_allowed = 1,
Jussi Kivilinnae080b172012-10-20 14:53:12 +03003165 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003166 .cipher = __VECS(des3_ede_ctr_tv_template)
Jussi Kivilinnae080b172012-10-20 14:53:12 +03003167 }
3168 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01003169 /* Same as ctr(aes) except the key is stored in
3170 * hardware secure memory which we reference by index
3171 */
3172 .alg = "ctr(paes)",
3173 .test = alg_test_null,
3174 .fips_allowed = 1,
3175 }, {
Jussi Kivilinna9d259172011-10-18 00:02:53 +03003176 .alg = "ctr(serpent)",
3177 .test = alg_test_skcipher,
3178 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003179 .cipher = __VECS(serpent_ctr_tv_template)
Jussi Kivilinna9d259172011-10-18 00:02:53 +03003180 }
3181 }, {
Gilad Ben-Yossef95ba5972018-09-20 14:18:38 +01003182 .alg = "ctr(sm4)",
3183 .test = alg_test_skcipher,
3184 .suite = {
3185 .cipher = __VECS(sm4_ctr_tv_template)
3186 }
3187 }, {
Jussi Kivilinna573da622011-10-10 23:03:12 +03003188 .alg = "ctr(twofish)",
3189 .test = alg_test_skcipher,
3190 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003191 .cipher = __VECS(tf_ctr_tv_template)
Jussi Kivilinna573da622011-10-10 23:03:12 +03003192 }
3193 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003194 .alg = "cts(cbc(aes))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003195 .test = alg_test_skcipher,
Gilad Ben-Yossef196ad602018-11-04 10:05:24 +00003196 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003197 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003198 .cipher = __VECS(cts_mode_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003199 }
3200 }, {
3201 .alg = "deflate",
3202 .test = alg_test_comp,
Milan Broz08189042012-12-06 17:16:28 +08003203 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003204 .suite = {
3205 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003206 .comp = __VECS(deflate_comp_tv_template),
3207 .decomp = __VECS(deflate_decomp_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003208 }
3209 }
3210 }, {
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003211 .alg = "dh",
3212 .test = alg_test_kpp,
3213 .fips_allowed = 1,
3214 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003215 .kpp = __VECS(dh_tv_template)
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003216 }
3217 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03003218 .alg = "digest_null",
3219 .test = alg_test_null,
3220 }, {
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003221 .alg = "drbg_nopr_ctr_aes128",
3222 .test = alg_test_drbg,
3223 .fips_allowed = 1,
3224 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003225 .drbg = __VECS(drbg_nopr_ctr_aes128_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003226 }
3227 }, {
3228 .alg = "drbg_nopr_ctr_aes192",
3229 .test = alg_test_drbg,
3230 .fips_allowed = 1,
3231 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003232 .drbg = __VECS(drbg_nopr_ctr_aes192_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003233 }
3234 }, {
3235 .alg = "drbg_nopr_ctr_aes256",
3236 .test = alg_test_drbg,
3237 .fips_allowed = 1,
3238 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003239 .drbg = __VECS(drbg_nopr_ctr_aes256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003240 }
3241 }, {
3242 /*
3243 * There is no need to specifically test the DRBG with every
3244 * backend cipher -- covered by drbg_nopr_hmac_sha256 test
3245 */
3246 .alg = "drbg_nopr_hmac_sha1",
3247 .fips_allowed = 1,
3248 .test = alg_test_null,
3249 }, {
3250 .alg = "drbg_nopr_hmac_sha256",
3251 .test = alg_test_drbg,
3252 .fips_allowed = 1,
3253 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003254 .drbg = __VECS(drbg_nopr_hmac_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003255 }
3256 }, {
3257 /* covered by drbg_nopr_hmac_sha256 test */
3258 .alg = "drbg_nopr_hmac_sha384",
3259 .fips_allowed = 1,
3260 .test = alg_test_null,
3261 }, {
3262 .alg = "drbg_nopr_hmac_sha512",
3263 .test = alg_test_null,
3264 .fips_allowed = 1,
3265 }, {
3266 .alg = "drbg_nopr_sha1",
3267 .fips_allowed = 1,
3268 .test = alg_test_null,
3269 }, {
3270 .alg = "drbg_nopr_sha256",
3271 .test = alg_test_drbg,
3272 .fips_allowed = 1,
3273 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003274 .drbg = __VECS(drbg_nopr_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003275 }
3276 }, {
3277 /* covered by drbg_nopr_sha256 test */
3278 .alg = "drbg_nopr_sha384",
3279 .fips_allowed = 1,
3280 .test = alg_test_null,
3281 }, {
3282 .alg = "drbg_nopr_sha512",
3283 .fips_allowed = 1,
3284 .test = alg_test_null,
3285 }, {
3286 .alg = "drbg_pr_ctr_aes128",
3287 .test = alg_test_drbg,
3288 .fips_allowed = 1,
3289 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003290 .drbg = __VECS(drbg_pr_ctr_aes128_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003291 }
3292 }, {
3293 /* covered by drbg_pr_ctr_aes128 test */
3294 .alg = "drbg_pr_ctr_aes192",
3295 .fips_allowed = 1,
3296 .test = alg_test_null,
3297 }, {
3298 .alg = "drbg_pr_ctr_aes256",
3299 .fips_allowed = 1,
3300 .test = alg_test_null,
3301 }, {
3302 .alg = "drbg_pr_hmac_sha1",
3303 .fips_allowed = 1,
3304 .test = alg_test_null,
3305 }, {
3306 .alg = "drbg_pr_hmac_sha256",
3307 .test = alg_test_drbg,
3308 .fips_allowed = 1,
3309 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003310 .drbg = __VECS(drbg_pr_hmac_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003311 }
3312 }, {
3313 /* covered by drbg_pr_hmac_sha256 test */
3314 .alg = "drbg_pr_hmac_sha384",
3315 .fips_allowed = 1,
3316 .test = alg_test_null,
3317 }, {
3318 .alg = "drbg_pr_hmac_sha512",
3319 .test = alg_test_null,
3320 .fips_allowed = 1,
3321 }, {
3322 .alg = "drbg_pr_sha1",
3323 .fips_allowed = 1,
3324 .test = alg_test_null,
3325 }, {
3326 .alg = "drbg_pr_sha256",
3327 .test = alg_test_drbg,
3328 .fips_allowed = 1,
3329 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003330 .drbg = __VECS(drbg_pr_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003331 }
3332 }, {
3333 /* covered by drbg_pr_sha256 test */
3334 .alg = "drbg_pr_sha384",
3335 .fips_allowed = 1,
3336 .test = alg_test_null,
3337 }, {
3338 .alg = "drbg_pr_sha512",
3339 .fips_allowed = 1,
3340 .test = alg_test_null,
3341 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003342 .alg = "ecb(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003343 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003344 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003345 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003346 .cipher = __VECS(aes_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003347 }
3348 }, {
3349 .alg = "ecb(anubis)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003350 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003351 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003352 .cipher = __VECS(anubis_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003353 }
3354 }, {
3355 .alg = "ecb(arc4)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003356 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003357 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003358 .cipher = __VECS(arc4_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003359 }
3360 }, {
3361 .alg = "ecb(blowfish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003362 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003363 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003364 .cipher = __VECS(bf_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003365 }
3366 }, {
3367 .alg = "ecb(camellia)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003368 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003369 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003370 .cipher = __VECS(camellia_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003371 }
3372 }, {
3373 .alg = "ecb(cast5)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003374 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003375 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003376 .cipher = __VECS(cast5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003377 }
3378 }, {
3379 .alg = "ecb(cast6)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003380 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003381 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003382 .cipher = __VECS(cast6_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003383 }
3384 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03003385 .alg = "ecb(cipher_null)",
3386 .test = alg_test_null,
Milan Broz6175ca22017-04-21 13:03:06 +02003387 .fips_allowed = 1,
Jussi Kivilinnae4483702013-04-07 16:43:56 +03003388 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003389 .alg = "ecb(des)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003390 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003391 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003392 .cipher = __VECS(des_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003393 }
3394 }, {
3395 .alg = "ecb(des3_ede)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003396 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003397 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003398 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003399 .cipher = __VECS(des3_ede_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003400 }
3401 }, {
Jussi Kivilinna66e5bd02013-01-19 13:31:36 +02003402 .alg = "ecb(fcrypt)",
3403 .test = alg_test_skcipher,
3404 .suite = {
3405 .cipher = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003406 .vecs = fcrypt_pcbc_tv_template,
3407 .count = 1
Jussi Kivilinna66e5bd02013-01-19 13:31:36 +02003408 }
3409 }
3410 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003411 .alg = "ecb(khazad)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003412 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003413 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003414 .cipher = __VECS(khazad_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003415 }
3416 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01003417 /* Same as ecb(aes) except the key is stored in
3418 * hardware secure memory which we reference by index
3419 */
3420 .alg = "ecb(paes)",
3421 .test = alg_test_null,
3422 .fips_allowed = 1,
3423 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003424 .alg = "ecb(seed)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003425 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003426 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003427 .cipher = __VECS(seed_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003428 }
3429 }, {
3430 .alg = "ecb(serpent)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003431 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003432 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003433 .cipher = __VECS(serpent_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003434 }
3435 }, {
Gilad Ben-Yossefcd83a8a2018-03-06 09:44:43 +00003436 .alg = "ecb(sm4)",
3437 .test = alg_test_skcipher,
3438 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003439 .cipher = __VECS(sm4_tv_template)
Gilad Ben-Yossefcd83a8a2018-03-06 09:44:43 +00003440 }
3441 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003442 .alg = "ecb(tea)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003443 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003444 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003445 .cipher = __VECS(tea_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003446 }
3447 }, {
3448 .alg = "ecb(tnepres)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003449 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003450 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003451 .cipher = __VECS(tnepres_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003452 }
3453 }, {
3454 .alg = "ecb(twofish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003455 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003456 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003457 .cipher = __VECS(tf_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003458 }
3459 }, {
3460 .alg = "ecb(xeta)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003461 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003462 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003463 .cipher = __VECS(xeta_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003464 }
3465 }, {
3466 .alg = "ecb(xtea)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003467 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003468 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003469 .cipher = __VECS(xtea_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003470 }
3471 }, {
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +01003472 .alg = "ecdh",
3473 .test = alg_test_kpp,
3474 .fips_allowed = 1,
3475 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003476 .kpp = __VECS(ecdh_tv_template)
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +01003477 }
3478 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003479 .alg = "gcm(aes)",
3480 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003481 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003482 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003483 .aead = __VECS(aes_gcm_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003484 }
3485 }, {
Youquan, Song507069c2009-11-23 20:23:04 +08003486 .alg = "ghash",
3487 .test = alg_test_hash,
Jarod Wilson18c0ebd2011-01-29 15:14:35 +11003488 .fips_allowed = 1,
Youquan, Song507069c2009-11-23 20:23:04 +08003489 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003490 .hash = __VECS(ghash_tv_template)
Youquan, Song507069c2009-11-23 20:23:04 +08003491 }
3492 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003493 .alg = "hmac(md5)",
3494 .test = alg_test_hash,
3495 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003496 .hash = __VECS(hmac_md5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003497 }
3498 }, {
3499 .alg = "hmac(rmd128)",
3500 .test = alg_test_hash,
3501 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003502 .hash = __VECS(hmac_rmd128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003503 }
3504 }, {
3505 .alg = "hmac(rmd160)",
3506 .test = alg_test_hash,
3507 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003508 .hash = __VECS(hmac_rmd160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003509 }
3510 }, {
3511 .alg = "hmac(sha1)",
3512 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003513 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003514 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003515 .hash = __VECS(hmac_sha1_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003516 }
3517 }, {
3518 .alg = "hmac(sha224)",
3519 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003520 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003521 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003522 .hash = __VECS(hmac_sha224_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003523 }
3524 }, {
3525 .alg = "hmac(sha256)",
3526 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003527 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003528 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003529 .hash = __VECS(hmac_sha256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003530 }
3531 }, {
raveendra padasalagi98eca722016-07-01 11:16:54 +05303532 .alg = "hmac(sha3-224)",
3533 .test = alg_test_hash,
3534 .fips_allowed = 1,
3535 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003536 .hash = __VECS(hmac_sha3_224_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05303537 }
3538 }, {
3539 .alg = "hmac(sha3-256)",
3540 .test = alg_test_hash,
3541 .fips_allowed = 1,
3542 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003543 .hash = __VECS(hmac_sha3_256_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05303544 }
3545 }, {
3546 .alg = "hmac(sha3-384)",
3547 .test = alg_test_hash,
3548 .fips_allowed = 1,
3549 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003550 .hash = __VECS(hmac_sha3_384_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05303551 }
3552 }, {
3553 .alg = "hmac(sha3-512)",
3554 .test = alg_test_hash,
3555 .fips_allowed = 1,
3556 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003557 .hash = __VECS(hmac_sha3_512_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05303558 }
3559 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003560 .alg = "hmac(sha384)",
3561 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003562 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003563 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003564 .hash = __VECS(hmac_sha384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003565 }
3566 }, {
3567 .alg = "hmac(sha512)",
3568 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003569 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003570 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003571 .hash = __VECS(hmac_sha512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003572 }
3573 }, {
Vitaly Chikunov25a0b9d2018-11-07 00:00:03 +03003574 .alg = "hmac(streebog256)",
3575 .test = alg_test_hash,
3576 .suite = {
3577 .hash = __VECS(hmac_streebog256_tv_template)
3578 }
3579 }, {
3580 .alg = "hmac(streebog512)",
3581 .test = alg_test_hash,
3582 .suite = {
3583 .hash = __VECS(hmac_streebog512_tv_template)
3584 }
3585 }, {
Stephan Muellerbb5530e2015-05-25 15:10:20 +02003586 .alg = "jitterentropy_rng",
3587 .fips_allowed = 1,
3588 .test = alg_test_null,
3589 }, {
Stephan Mueller35351982015-09-21 20:59:56 +02003590 .alg = "kw(aes)",
3591 .test = alg_test_skcipher,
3592 .fips_allowed = 1,
3593 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003594 .cipher = __VECS(aes_kw_tv_template)
Stephan Mueller35351982015-09-21 20:59:56 +02003595 }
3596 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003597 .alg = "lrw(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003598 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003599 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003600 .cipher = __VECS(aes_lrw_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003601 }
3602 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02003603 .alg = "lrw(camellia)",
3604 .test = alg_test_skcipher,
3605 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003606 .cipher = __VECS(camellia_lrw_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02003607 }
3608 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003609 .alg = "lrw(cast6)",
3610 .test = alg_test_skcipher,
3611 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003612 .cipher = __VECS(cast6_lrw_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003613 }
3614 }, {
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03003615 .alg = "lrw(serpent)",
3616 .test = alg_test_skcipher,
3617 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003618 .cipher = __VECS(serpent_lrw_tv_template)
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03003619 }
3620 }, {
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03003621 .alg = "lrw(twofish)",
3622 .test = alg_test_skcipher,
3623 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003624 .cipher = __VECS(tf_lrw_tv_template)
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03003625 }
3626 }, {
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02003627 .alg = "lz4",
3628 .test = alg_test_comp,
3629 .fips_allowed = 1,
3630 .suite = {
3631 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003632 .comp = __VECS(lz4_comp_tv_template),
3633 .decomp = __VECS(lz4_decomp_tv_template)
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02003634 }
3635 }
3636 }, {
3637 .alg = "lz4hc",
3638 .test = alg_test_comp,
3639 .fips_allowed = 1,
3640 .suite = {
3641 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003642 .comp = __VECS(lz4hc_comp_tv_template),
3643 .decomp = __VECS(lz4hc_decomp_tv_template)
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02003644 }
3645 }
3646 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003647 .alg = "lzo",
3648 .test = alg_test_comp,
Milan Broz08189042012-12-06 17:16:28 +08003649 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003650 .suite = {
3651 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003652 .comp = __VECS(lzo_comp_tv_template),
3653 .decomp = __VECS(lzo_decomp_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003654 }
3655 }
3656 }, {
3657 .alg = "md4",
3658 .test = alg_test_hash,
3659 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003660 .hash = __VECS(md4_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003661 }
3662 }, {
3663 .alg = "md5",
3664 .test = alg_test_hash,
3665 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003666 .hash = __VECS(md5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003667 }
3668 }, {
3669 .alg = "michael_mic",
3670 .test = alg_test_hash,
3671 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003672 .hash = __VECS(michael_mic_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003673 }
3674 }, {
Ondrej Mosnacek4feb4c52018-05-11 14:19:10 +02003675 .alg = "morus1280",
3676 .test = alg_test_aead,
3677 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003678 .aead = __VECS(morus1280_tv_template)
Ondrej Mosnacek4feb4c52018-05-11 14:19:10 +02003679 }
3680 }, {
3681 .alg = "morus640",
3682 .test = alg_test_aead,
3683 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003684 .aead = __VECS(morus640_tv_template)
Ondrej Mosnacek4feb4c52018-05-11 14:19:10 +02003685 }
3686 }, {
Eric Biggers26609a22018-11-16 17:26:29 -08003687 .alg = "nhpoly1305",
3688 .test = alg_test_hash,
3689 .suite = {
3690 .hash = __VECS(nhpoly1305_tv_template)
3691 }
3692 }, {
Puneet Saxenaba0e14a2011-05-04 15:04:10 +10003693 .alg = "ofb(aes)",
3694 .test = alg_test_skcipher,
3695 .fips_allowed = 1,
3696 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003697 .cipher = __VECS(aes_ofb_tv_template)
Puneet Saxenaba0e14a2011-05-04 15:04:10 +10003698 }
3699 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01003700 /* Same as ofb(aes) except the key is stored in
3701 * hardware secure memory which we reference by index
3702 */
3703 .alg = "ofb(paes)",
3704 .test = alg_test_null,
3705 .fips_allowed = 1,
3706 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003707 .alg = "pcbc(fcrypt)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003708 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003709 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003710 .cipher = __VECS(fcrypt_pcbc_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003711 }
3712 }, {
Stephan Mueller12071072017-06-12 23:27:51 +02003713 .alg = "pkcs1pad(rsa,sha224)",
3714 .test = alg_test_null,
3715 .fips_allowed = 1,
3716 }, {
3717 .alg = "pkcs1pad(rsa,sha256)",
3718 .test = alg_test_akcipher,
3719 .fips_allowed = 1,
3720 .suite = {
3721 .akcipher = __VECS(pkcs1pad_rsa_tv_template)
3722 }
3723 }, {
3724 .alg = "pkcs1pad(rsa,sha384)",
3725 .test = alg_test_null,
3726 .fips_allowed = 1,
3727 }, {
3728 .alg = "pkcs1pad(rsa,sha512)",
3729 .test = alg_test_null,
3730 .fips_allowed = 1,
3731 }, {
Martin Willieee9dc62015-06-01 13:43:59 +02003732 .alg = "poly1305",
3733 .test = alg_test_hash,
3734 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003735 .hash = __VECS(poly1305_tv_template)
Martin Willieee9dc62015-06-01 13:43:59 +02003736 }
3737 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003738 .alg = "rfc3686(ctr(aes))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003739 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003740 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003741 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003742 .cipher = __VECS(aes_ctr_rfc3686_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003743 }
3744 }, {
Herbert Xu3f31a742015-07-09 07:17:34 +08003745 .alg = "rfc4106(gcm(aes))",
Adrian Hoban69435b92010-11-04 15:02:04 -04003746 .test = alg_test_aead,
Jarod Wilsondb71f29a2015-01-23 12:42:15 -05003747 .fips_allowed = 1,
Adrian Hoban69435b92010-11-04 15:02:04 -04003748 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003749 .aead = __VECS(aes_gcm_rfc4106_tv_template)
Adrian Hoban69435b92010-11-04 15:02:04 -04003750 }
3751 }, {
Herbert Xu544c4362015-07-14 16:53:22 +08003752 .alg = "rfc4309(ccm(aes))",
Jarod Wilson5d667322009-05-04 19:23:40 +08003753 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003754 .fips_allowed = 1,
Jarod Wilson5d667322009-05-04 19:23:40 +08003755 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003756 .aead = __VECS(aes_ccm_rfc4309_tv_template)
Jarod Wilson5d667322009-05-04 19:23:40 +08003757 }
3758 }, {
Herbert Xubb687452015-06-16 13:54:24 +08003759 .alg = "rfc4543(gcm(aes))",
Jussi Kivilinnae9b74412013-04-07 16:43:51 +03003760 .test = alg_test_aead,
3761 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003762 .aead = __VECS(aes_gcm_rfc4543_tv_template)
Jussi Kivilinnae9b74412013-04-07 16:43:51 +03003763 }
3764 }, {
Martin Williaf2b76b2015-06-01 13:44:01 +02003765 .alg = "rfc7539(chacha20,poly1305)",
3766 .test = alg_test_aead,
3767 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003768 .aead = __VECS(rfc7539_tv_template)
Martin Williaf2b76b2015-06-01 13:44:01 +02003769 }
3770 }, {
Martin Willi59007582015-06-01 13:44:03 +02003771 .alg = "rfc7539esp(chacha20,poly1305)",
3772 .test = alg_test_aead,
3773 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003774 .aead = __VECS(rfc7539esp_tv_template)
Martin Willi59007582015-06-01 13:44:03 +02003775 }
3776 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003777 .alg = "rmd128",
3778 .test = alg_test_hash,
3779 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003780 .hash = __VECS(rmd128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003781 }
3782 }, {
3783 .alg = "rmd160",
3784 .test = alg_test_hash,
3785 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003786 .hash = __VECS(rmd160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003787 }
3788 }, {
3789 .alg = "rmd256",
3790 .test = alg_test_hash,
3791 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003792 .hash = __VECS(rmd256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003793 }
3794 }, {
3795 .alg = "rmd320",
3796 .test = alg_test_hash,
3797 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003798 .hash = __VECS(rmd320_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003799 }
3800 }, {
Tadeusz Struk946cc462015-06-16 10:31:06 -07003801 .alg = "rsa",
3802 .test = alg_test_akcipher,
3803 .fips_allowed = 1,
3804 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003805 .akcipher = __VECS(rsa_tv_template)
Tadeusz Struk946cc462015-06-16 10:31:06 -07003806 }
3807 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003808 .alg = "salsa20",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003809 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003810 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003811 .cipher = __VECS(salsa20_stream_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003812 }
3813 }, {
3814 .alg = "sha1",
3815 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003816 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003817 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003818 .hash = __VECS(sha1_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003819 }
3820 }, {
3821 .alg = "sha224",
3822 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003823 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003824 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003825 .hash = __VECS(sha224_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003826 }
3827 }, {
3828 .alg = "sha256",
3829 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003830 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003831 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003832 .hash = __VECS(sha256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003833 }
3834 }, {
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05303835 .alg = "sha3-224",
3836 .test = alg_test_hash,
3837 .fips_allowed = 1,
3838 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003839 .hash = __VECS(sha3_224_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05303840 }
3841 }, {
3842 .alg = "sha3-256",
3843 .test = alg_test_hash,
3844 .fips_allowed = 1,
3845 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003846 .hash = __VECS(sha3_256_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05303847 }
3848 }, {
3849 .alg = "sha3-384",
3850 .test = alg_test_hash,
3851 .fips_allowed = 1,
3852 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003853 .hash = __VECS(sha3_384_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05303854 }
3855 }, {
3856 .alg = "sha3-512",
3857 .test = alg_test_hash,
3858 .fips_allowed = 1,
3859 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003860 .hash = __VECS(sha3_512_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05303861 }
3862 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003863 .alg = "sha384",
3864 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003865 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003866 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003867 .hash = __VECS(sha384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003868 }
3869 }, {
3870 .alg = "sha512",
3871 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003872 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003873 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003874 .hash = __VECS(sha512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003875 }
3876 }, {
Gilad Ben-Yossefb7e27532017-08-21 13:51:29 +03003877 .alg = "sm3",
3878 .test = alg_test_hash,
3879 .suite = {
3880 .hash = __VECS(sm3_tv_template)
3881 }
3882 }, {
Vitaly Chikunov25a0b9d2018-11-07 00:00:03 +03003883 .alg = "streebog256",
3884 .test = alg_test_hash,
3885 .suite = {
3886 .hash = __VECS(streebog256_tv_template)
3887 }
3888 }, {
3889 .alg = "streebog512",
3890 .test = alg_test_hash,
3891 .suite = {
3892 .hash = __VECS(streebog512_tv_template)
3893 }
3894 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003895 .alg = "tgr128",
3896 .test = alg_test_hash,
3897 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003898 .hash = __VECS(tgr128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003899 }
3900 }, {
3901 .alg = "tgr160",
3902 .test = alg_test_hash,
3903 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003904 .hash = __VECS(tgr160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003905 }
3906 }, {
3907 .alg = "tgr192",
3908 .test = alg_test_hash,
3909 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003910 .hash = __VECS(tgr192_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003911 }
3912 }, {
Eric Biggersed331ad2018-06-18 10:22:39 -07003913 .alg = "vmac64(aes)",
3914 .test = alg_test_hash,
3915 .suite = {
3916 .hash = __VECS(vmac64_aes_tv_template)
3917 }
3918 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003919 .alg = "wp256",
3920 .test = alg_test_hash,
3921 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003922 .hash = __VECS(wp256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003923 }
3924 }, {
3925 .alg = "wp384",
3926 .test = alg_test_hash,
3927 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003928 .hash = __VECS(wp384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003929 }
3930 }, {
3931 .alg = "wp512",
3932 .test = alg_test_hash,
3933 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003934 .hash = __VECS(wp512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003935 }
3936 }, {
3937 .alg = "xcbc(aes)",
3938 .test = alg_test_hash,
3939 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003940 .hash = __VECS(aes_xcbc128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003941 }
3942 }, {
Eric Biggersaa762402018-11-16 17:26:22 -08003943 .alg = "xchacha12",
3944 .test = alg_test_skcipher,
3945 .suite = {
3946 .cipher = __VECS(xchacha12_tv_template)
3947 },
3948 }, {
Eric Biggersde61d7a2018-11-16 17:26:20 -08003949 .alg = "xchacha20",
3950 .test = alg_test_skcipher,
3951 .suite = {
3952 .cipher = __VECS(xchacha20_tv_template)
3953 },
3954 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003955 .alg = "xts(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003956 .test = alg_test_skcipher,
Jarod Wilson2918aa82011-01-29 15:14:01 +11003957 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003958 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003959 .cipher = __VECS(aes_xts_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003960 }
Geert Uytterhoeven0c01aed2009-03-04 15:42:15 +08003961 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02003962 .alg = "xts(camellia)",
3963 .test = alg_test_skcipher,
3964 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003965 .cipher = __VECS(camellia_xts_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02003966 }
3967 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003968 .alg = "xts(cast6)",
3969 .test = alg_test_skcipher,
3970 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003971 .cipher = __VECS(cast6_xts_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003972 }
3973 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01003974 /* Same as xts(aes) except the key is stored in
3975 * hardware secure memory which we reference by index
3976 */
3977 .alg = "xts(paes)",
3978 .test = alg_test_null,
3979 .fips_allowed = 1,
3980 }, {
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03003981 .alg = "xts(serpent)",
3982 .test = alg_test_skcipher,
3983 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003984 .cipher = __VECS(serpent_xts_tv_template)
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03003985 }
3986 }, {
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03003987 .alg = "xts(twofish)",
3988 .test = alg_test_skcipher,
3989 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003990 .cipher = __VECS(tf_xts_tv_template)
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03003991 }
Giovanni Cabiddua368f432017-04-21 21:54:30 +01003992 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01003993 .alg = "xts4096(paes)",
3994 .test = alg_test_null,
3995 .fips_allowed = 1,
3996 }, {
3997 .alg = "xts512(paes)",
3998 .test = alg_test_null,
3999 .fips_allowed = 1,
4000 }, {
Giovanni Cabiddua368f432017-04-21 21:54:30 +01004001 .alg = "zlib-deflate",
4002 .test = alg_test_comp,
4003 .fips_allowed = 1,
4004 .suite = {
4005 .comp = {
4006 .comp = __VECS(zlib_deflate_comp_tv_template),
4007 .decomp = __VECS(zlib_deflate_decomp_tv_template)
4008 }
4009 }
Nick Terrelld28fc3d2018-03-30 12:14:53 -07004010 }, {
4011 .alg = "zstd",
4012 .test = alg_test_comp,
4013 .fips_allowed = 1,
4014 .suite = {
4015 .comp = {
4016 .comp = __VECS(zstd_comp_tv_template),
4017 .decomp = __VECS(zstd_decomp_tv_template)
4018 }
4019 }
Herbert Xuda7f0332008-07-31 17:08:25 +08004020 }
4021};
4022
Eric Biggers3f47a032019-01-31 23:51:43 -08004023static void alg_check_test_descs_order(void)
Jussi Kivilinna57147582013-06-13 17:37:40 +03004024{
4025 int i;
4026
Jussi Kivilinna57147582013-06-13 17:37:40 +03004027 for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
4028 int diff = strcmp(alg_test_descs[i - 1].alg,
4029 alg_test_descs[i].alg);
4030
4031 if (WARN_ON(diff > 0)) {
4032 pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
4033 alg_test_descs[i - 1].alg,
4034 alg_test_descs[i].alg);
4035 }
4036
4037 if (WARN_ON(diff == 0)) {
4038 pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
4039 alg_test_descs[i].alg);
4040 }
4041 }
4042}
4043
Eric Biggers3f47a032019-01-31 23:51:43 -08004044static void alg_check_testvec_configs(void)
4045{
Eric Biggers4e7babba2019-01-31 23:51:46 -08004046 int i;
4047
4048 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++)
4049 WARN_ON(!valid_testvec_config(
4050 &default_cipher_testvec_configs[i]));
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08004051
4052 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++)
4053 WARN_ON(!valid_testvec_config(
4054 &default_hash_testvec_configs[i]));
Eric Biggers3f47a032019-01-31 23:51:43 -08004055}
4056
4057static void testmgr_onetime_init(void)
4058{
4059 alg_check_test_descs_order();
4060 alg_check_testvec_configs();
Eric Biggers5b2706a2019-01-31 23:51:44 -08004061
4062#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
4063 pr_warn("alg: extra crypto tests enabled. This is intended for developer use only.\n");
4064#endif
Eric Biggers3f47a032019-01-31 23:51:43 -08004065}
4066
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004067static int alg_find_test(const char *alg)
Herbert Xuda7f0332008-07-31 17:08:25 +08004068{
4069 int start = 0;
4070 int end = ARRAY_SIZE(alg_test_descs);
4071
4072 while (start < end) {
4073 int i = (start + end) / 2;
4074 int diff = strcmp(alg_test_descs[i].alg, alg);
4075
4076 if (diff > 0) {
4077 end = i;
4078 continue;
4079 }
4080
4081 if (diff < 0) {
4082 start = i + 1;
4083 continue;
4084 }
4085
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004086 return i;
Herbert Xuda7f0332008-07-31 17:08:25 +08004087 }
4088
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004089 return -1;
4090}
4091
4092int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
4093{
4094 int i;
Herbert Xua68f6612009-07-02 16:32:12 +08004095 int j;
Neil Hormand12d6b62008-10-12 20:36:51 +08004096 int rc;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004097
Richard W.M. Jones9e5c9fe2016-05-03 10:00:17 +01004098 if (!fips_enabled && notests) {
4099 printk_once(KERN_INFO "alg: self-tests disabled\n");
4100 return 0;
4101 }
4102
Eric Biggers3f47a032019-01-31 23:51:43 -08004103 DO_ONCE(testmgr_onetime_init);
Jussi Kivilinna57147582013-06-13 17:37:40 +03004104
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004105 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
4106 char nalg[CRYPTO_MAX_ALG_NAME];
4107
4108 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
4109 sizeof(nalg))
4110 return -ENAMETOOLONG;
4111
4112 i = alg_find_test(nalg);
4113 if (i < 0)
4114 goto notest;
4115
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10004116 if (fips_enabled && !alg_test_descs[i].fips_allowed)
4117 goto non_fips_alg;
4118
Jarod Wilson941fb322009-05-04 19:49:23 +08004119 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
4120 goto test_done;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004121 }
4122
4123 i = alg_find_test(alg);
Herbert Xua68f6612009-07-02 16:32:12 +08004124 j = alg_find_test(driver);
4125 if (i < 0 && j < 0)
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004126 goto notest;
4127
Herbert Xua68f6612009-07-02 16:32:12 +08004128 if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
4129 (j >= 0 && !alg_test_descs[j].fips_allowed)))
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10004130 goto non_fips_alg;
4131
Herbert Xua68f6612009-07-02 16:32:12 +08004132 rc = 0;
4133 if (i >= 0)
4134 rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
4135 type, mask);
Cristian Stoica032c8ca2013-07-18 18:57:07 +03004136 if (j >= 0 && j != i)
Herbert Xua68f6612009-07-02 16:32:12 +08004137 rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
4138 type, mask);
4139
Jarod Wilson941fb322009-05-04 19:49:23 +08004140test_done:
Eric Biggerseda69b02019-03-31 13:09:14 -07004141 if (rc && (fips_enabled || panic_on_fail))
4142 panic("alg: self-tests for %s (%s) failed in %s mode!\n",
4143 driver, alg, fips_enabled ? "fips" : "panic_on_fail");
Neil Hormand12d6b62008-10-12 20:36:51 +08004144
Jarod Wilson29ecd4a2009-05-04 19:51:17 +08004145 if (fips_enabled && !rc)
Masanari Iida3e8cffd2014-10-07 00:37:54 +09004146 pr_info("alg: self-tests for %s (%s) passed\n", driver, alg);
Jarod Wilson29ecd4a2009-05-04 19:51:17 +08004147
Neil Hormand12d6b62008-10-12 20:36:51 +08004148 return rc;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004149
4150notest:
Herbert Xuda7f0332008-07-31 17:08:25 +08004151 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
4152 return 0;
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10004153non_fips_alg:
4154 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08004155}
Alexander Shishkin0b767f92010-06-03 20:53:43 +10004156
Herbert Xu326a6342010-08-06 09:40:28 +08004157#endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
Alexander Shishkin0b767f92010-06-03 20:53:43 +10004158
Herbert Xuda7f0332008-07-31 17:08:25 +08004159EXPORT_SYMBOL_GPL(alg_test);