blob: cafeba6ba16c55288e3614dff75786e746d58908 [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;
Eric Biggersf2bb770a2019-04-11 21:57:38 -0700131 const char *generic_driver;
Herbert Xuda7f0332008-07-31 17:08:25 +0800132 int (*test)(const struct alg_test_desc *desc, const char *driver,
133 u32 type, u32 mask);
Jarod Wilsona1915d52009-05-15 15:16:03 +1000134 int fips_allowed; /* set if alg is allowed in fips mode */
Herbert Xuda7f0332008-07-31 17:08:25 +0800135
136 union {
137 struct aead_test_suite aead;
138 struct cipher_test_suite cipher;
139 struct comp_test_suite comp;
140 struct hash_test_suite hash;
Jarod Wilson7647d6c2009-05-04 19:44:50 +0800141 struct cprng_test_suite cprng;
Stephan Mueller64d1cdf2014-05-31 17:25:36 +0200142 struct drbg_test_suite drbg;
Tadeusz Struk946cc462015-06-16 10:31:06 -0700143 struct akcipher_test_suite akcipher;
Salvatore Benedetto802c7f12016-06-22 17:49:14 +0100144 struct kpp_test_suite kpp;
Herbert Xuda7f0332008-07-31 17:08:25 +0800145 } suite;
146};
147
Herbert Xuda7f0332008-07-31 17:08:25 +0800148static void hexdump(unsigned char *buf, unsigned int len)
149{
150 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
151 16, 1,
152 buf, len, false);
153}
154
Eric Biggers3f47a032019-01-31 23:51:43 -0800155static int __testmgr_alloc_buf(char *buf[XBUFSIZE], int order)
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800156{
157 int i;
158
159 for (i = 0; i < XBUFSIZE; i++) {
Eric Biggers3f47a032019-01-31 23:51:43 -0800160 buf[i] = (char *)__get_free_pages(GFP_KERNEL, order);
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800161 if (!buf[i])
162 goto err_free_buf;
163 }
164
165 return 0;
166
167err_free_buf:
168 while (i-- > 0)
Eric Biggers3f47a032019-01-31 23:51:43 -0800169 free_pages((unsigned long)buf[i], order);
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800170
171 return -ENOMEM;
172}
173
Eric Biggers3f47a032019-01-31 23:51:43 -0800174static int testmgr_alloc_buf(char *buf[XBUFSIZE])
175{
176 return __testmgr_alloc_buf(buf, 0);
177}
178
179static void __testmgr_free_buf(char *buf[XBUFSIZE], int order)
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800180{
181 int i;
182
183 for (i = 0; i < XBUFSIZE; i++)
Eric Biggers3f47a032019-01-31 23:51:43 -0800184 free_pages((unsigned long)buf[i], order);
185}
186
187static void testmgr_free_buf(char *buf[XBUFSIZE])
188{
189 __testmgr_free_buf(buf, 0);
190}
191
192#define TESTMGR_POISON_BYTE 0xfe
193#define TESTMGR_POISON_LEN 16
194
195static inline void testmgr_poison(void *addr, size_t len)
196{
197 memset(addr, TESTMGR_POISON_BYTE, len);
198}
199
200/* Is the memory region still fully poisoned? */
201static inline bool testmgr_is_poison(const void *addr, size_t len)
202{
203 return memchr_inv(addr, TESTMGR_POISON_BYTE, len) == NULL;
204}
205
206/* flush type for hash algorithms */
207enum flush_type {
208 /* merge with update of previous buffer(s) */
209 FLUSH_TYPE_NONE = 0,
210
211 /* update with previous buffer(s) before doing this one */
212 FLUSH_TYPE_FLUSH,
213
214 /* likewise, but also export and re-import the intermediate state */
215 FLUSH_TYPE_REIMPORT,
216};
217
218/* finalization function for hash algorithms */
219enum finalization_type {
220 FINALIZATION_TYPE_FINAL, /* use final() */
221 FINALIZATION_TYPE_FINUP, /* use finup() */
222 FINALIZATION_TYPE_DIGEST, /* use digest() */
223};
224
225#define TEST_SG_TOTAL 10000
226
227/**
228 * struct test_sg_division - description of a scatterlist entry
229 *
230 * This struct describes one entry of a scatterlist being constructed to check a
231 * crypto test vector.
232 *
233 * @proportion_of_total: length of this chunk relative to the total length,
234 * given as a proportion out of TEST_SG_TOTAL so that it
235 * scales to fit any test vector
236 * @offset: byte offset into a 2-page buffer at which this chunk will start
237 * @offset_relative_to_alignmask: if true, add the algorithm's alignmask to the
238 * @offset
239 * @flush_type: for hashes, whether an update() should be done now vs.
240 * continuing to accumulate data
Eric Biggers65707372019-03-12 22:12:52 -0700241 * @nosimd: if doing the pending update(), do it with SIMD disabled?
Eric Biggers3f47a032019-01-31 23:51:43 -0800242 */
243struct test_sg_division {
244 unsigned int proportion_of_total;
245 unsigned int offset;
246 bool offset_relative_to_alignmask;
247 enum flush_type flush_type;
Eric Biggers65707372019-03-12 22:12:52 -0700248 bool nosimd;
Eric Biggers3f47a032019-01-31 23:51:43 -0800249};
250
251/**
252 * struct testvec_config - configuration for testing a crypto test vector
253 *
254 * This struct describes the data layout and other parameters with which each
255 * crypto test vector can be tested.
256 *
257 * @name: name of this config, logged for debugging purposes if a test fails
258 * @inplace: operate on the data in-place, if applicable for the algorithm type?
259 * @req_flags: extra request_flags, e.g. CRYPTO_TFM_REQ_MAY_SLEEP
260 * @src_divs: description of how to arrange the source scatterlist
261 * @dst_divs: description of how to arrange the dst scatterlist, if applicable
262 * for the algorithm type. Defaults to @src_divs if unset.
263 * @iv_offset: misalignment of the IV in the range [0..MAX_ALGAPI_ALIGNMASK+1],
264 * where 0 is aligned to a 2*(MAX_ALGAPI_ALIGNMASK+1) byte boundary
265 * @iv_offset_relative_to_alignmask: if true, add the algorithm's alignmask to
266 * the @iv_offset
267 * @finalization_type: what finalization function to use for hashes
Eric Biggers65707372019-03-12 22:12:52 -0700268 * @nosimd: execute with SIMD disabled? Requires !CRYPTO_TFM_REQ_MAY_SLEEP.
Eric Biggers3f47a032019-01-31 23:51:43 -0800269 */
270struct testvec_config {
271 const char *name;
272 bool inplace;
273 u32 req_flags;
274 struct test_sg_division src_divs[XBUFSIZE];
275 struct test_sg_division dst_divs[XBUFSIZE];
276 unsigned int iv_offset;
277 bool iv_offset_relative_to_alignmask;
278 enum finalization_type finalization_type;
Eric Biggers65707372019-03-12 22:12:52 -0700279 bool nosimd;
Eric Biggers3f47a032019-01-31 23:51:43 -0800280};
281
282#define TESTVEC_CONFIG_NAMELEN 192
283
Eric Biggers4e7babba2019-01-31 23:51:46 -0800284/*
285 * The following are the lists of testvec_configs to test for each algorithm
286 * type when the basic crypto self-tests are enabled, i.e. when
287 * CONFIG_CRYPTO_MANAGER_DISABLE_TESTS is unset. They aim to provide good test
288 * coverage, while keeping the test time much shorter than the full fuzz tests
289 * so that the basic tests can be enabled in a wider range of circumstances.
290 */
291
292/* Configs for skciphers and aeads */
293static const struct testvec_config default_cipher_testvec_configs[] = {
294 {
295 .name = "in-place",
296 .inplace = true,
297 .src_divs = { { .proportion_of_total = 10000 } },
298 }, {
299 .name = "out-of-place",
300 .src_divs = { { .proportion_of_total = 10000 } },
301 }, {
302 .name = "unaligned buffer, offset=1",
303 .src_divs = { { .proportion_of_total = 10000, .offset = 1 } },
304 .iv_offset = 1,
305 }, {
306 .name = "buffer aligned only to alignmask",
307 .src_divs = {
308 {
309 .proportion_of_total = 10000,
310 .offset = 1,
311 .offset_relative_to_alignmask = true,
312 },
313 },
314 .iv_offset = 1,
315 .iv_offset_relative_to_alignmask = true,
316 }, {
317 .name = "two even aligned splits",
318 .src_divs = {
319 { .proportion_of_total = 5000 },
320 { .proportion_of_total = 5000 },
321 },
322 }, {
323 .name = "uneven misaligned splits, may sleep",
324 .req_flags = CRYPTO_TFM_REQ_MAY_SLEEP,
325 .src_divs = {
326 { .proportion_of_total = 1900, .offset = 33 },
327 { .proportion_of_total = 3300, .offset = 7 },
328 { .proportion_of_total = 4800, .offset = 18 },
329 },
330 .iv_offset = 3,
331 }, {
332 .name = "misaligned splits crossing pages, inplace",
333 .inplace = true,
334 .src_divs = {
335 {
336 .proportion_of_total = 7500,
337 .offset = PAGE_SIZE - 32
338 }, {
339 .proportion_of_total = 2500,
340 .offset = PAGE_SIZE - 7
341 },
342 },
343 }
344};
345
Eric Biggers4cc2dcf2019-01-31 23:51:48 -0800346static const struct testvec_config default_hash_testvec_configs[] = {
347 {
348 .name = "init+update+final aligned buffer",
349 .src_divs = { { .proportion_of_total = 10000 } },
350 .finalization_type = FINALIZATION_TYPE_FINAL,
351 }, {
352 .name = "init+finup aligned buffer",
353 .src_divs = { { .proportion_of_total = 10000 } },
354 .finalization_type = FINALIZATION_TYPE_FINUP,
355 }, {
356 .name = "digest aligned buffer",
357 .src_divs = { { .proportion_of_total = 10000 } },
358 .finalization_type = FINALIZATION_TYPE_DIGEST,
359 }, {
360 .name = "init+update+final misaligned buffer",
361 .src_divs = { { .proportion_of_total = 10000, .offset = 1 } },
362 .finalization_type = FINALIZATION_TYPE_FINAL,
363 }, {
364 .name = "digest buffer aligned only to alignmask",
365 .src_divs = {
366 {
367 .proportion_of_total = 10000,
368 .offset = 1,
369 .offset_relative_to_alignmask = true,
370 },
371 },
372 .finalization_type = FINALIZATION_TYPE_DIGEST,
373 }, {
374 .name = "init+update+update+final two even splits",
375 .src_divs = {
376 { .proportion_of_total = 5000 },
377 {
378 .proportion_of_total = 5000,
379 .flush_type = FLUSH_TYPE_FLUSH,
380 },
381 },
382 .finalization_type = FINALIZATION_TYPE_FINAL,
383 }, {
384 .name = "digest uneven misaligned splits, may sleep",
385 .req_flags = CRYPTO_TFM_REQ_MAY_SLEEP,
386 .src_divs = {
387 { .proportion_of_total = 1900, .offset = 33 },
388 { .proportion_of_total = 3300, .offset = 7 },
389 { .proportion_of_total = 4800, .offset = 18 },
390 },
391 .finalization_type = FINALIZATION_TYPE_DIGEST,
392 }, {
393 .name = "digest misaligned splits crossing pages",
394 .src_divs = {
395 {
396 .proportion_of_total = 7500,
397 .offset = PAGE_SIZE - 32,
398 }, {
399 .proportion_of_total = 2500,
400 .offset = PAGE_SIZE - 7,
401 },
402 },
403 .finalization_type = FINALIZATION_TYPE_DIGEST,
404 }, {
405 .name = "import/export",
406 .src_divs = {
407 {
408 .proportion_of_total = 6500,
409 .flush_type = FLUSH_TYPE_REIMPORT,
410 }, {
411 .proportion_of_total = 3500,
412 .flush_type = FLUSH_TYPE_REIMPORT,
413 },
414 },
415 .finalization_type = FINALIZATION_TYPE_FINAL,
416 }
417};
418
Eric Biggers3f47a032019-01-31 23:51:43 -0800419static unsigned int count_test_sg_divisions(const struct test_sg_division *divs)
420{
421 unsigned int remaining = TEST_SG_TOTAL;
422 unsigned int ndivs = 0;
423
424 do {
425 remaining -= divs[ndivs++].proportion_of_total;
426 } while (remaining);
427
428 return ndivs;
429}
430
Eric Biggers65707372019-03-12 22:12:52 -0700431#define SGDIVS_HAVE_FLUSHES BIT(0)
432#define SGDIVS_HAVE_NOSIMD BIT(1)
433
Eric Biggers3f47a032019-01-31 23:51:43 -0800434static bool valid_sg_divisions(const struct test_sg_division *divs,
Eric Biggers65707372019-03-12 22:12:52 -0700435 unsigned int count, int *flags_ret)
Eric Biggers3f47a032019-01-31 23:51:43 -0800436{
437 unsigned int total = 0;
438 unsigned int i;
439
440 for (i = 0; i < count && total != TEST_SG_TOTAL; i++) {
441 if (divs[i].proportion_of_total <= 0 ||
442 divs[i].proportion_of_total > TEST_SG_TOTAL - total)
443 return false;
444 total += divs[i].proportion_of_total;
445 if (divs[i].flush_type != FLUSH_TYPE_NONE)
Eric Biggers65707372019-03-12 22:12:52 -0700446 *flags_ret |= SGDIVS_HAVE_FLUSHES;
447 if (divs[i].nosimd)
448 *flags_ret |= SGDIVS_HAVE_NOSIMD;
Eric Biggers3f47a032019-01-31 23:51:43 -0800449 }
450 return total == TEST_SG_TOTAL &&
451 memchr_inv(&divs[i], 0, (count - i) * sizeof(divs[0])) == NULL;
452}
453
454/*
455 * Check whether the given testvec_config is valid. This isn't strictly needed
456 * since every testvec_config should be valid, but check anyway so that people
457 * don't unknowingly add broken configs that don't do what they wanted.
458 */
459static bool valid_testvec_config(const struct testvec_config *cfg)
460{
Eric Biggers65707372019-03-12 22:12:52 -0700461 int flags = 0;
Eric Biggers3f47a032019-01-31 23:51:43 -0800462
463 if (cfg->name == NULL)
464 return false;
465
466 if (!valid_sg_divisions(cfg->src_divs, ARRAY_SIZE(cfg->src_divs),
Eric Biggers65707372019-03-12 22:12:52 -0700467 &flags))
Eric Biggers3f47a032019-01-31 23:51:43 -0800468 return false;
469
470 if (cfg->dst_divs[0].proportion_of_total) {
471 if (!valid_sg_divisions(cfg->dst_divs,
Eric Biggers65707372019-03-12 22:12:52 -0700472 ARRAY_SIZE(cfg->dst_divs), &flags))
Eric Biggers3f47a032019-01-31 23:51:43 -0800473 return false;
474 } else {
475 if (memchr_inv(cfg->dst_divs, 0, sizeof(cfg->dst_divs)))
476 return false;
477 /* defaults to dst_divs=src_divs */
478 }
479
480 if (cfg->iv_offset +
481 (cfg->iv_offset_relative_to_alignmask ? MAX_ALGAPI_ALIGNMASK : 0) >
482 MAX_ALGAPI_ALIGNMASK + 1)
483 return false;
484
Eric Biggers65707372019-03-12 22:12:52 -0700485 if ((flags & (SGDIVS_HAVE_FLUSHES | SGDIVS_HAVE_NOSIMD)) &&
486 cfg->finalization_type == FINALIZATION_TYPE_DIGEST)
487 return false;
488
489 if ((cfg->nosimd || (flags & SGDIVS_HAVE_NOSIMD)) &&
490 (cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP))
Eric Biggers3f47a032019-01-31 23:51:43 -0800491 return false;
492
493 return true;
494}
495
496struct test_sglist {
497 char *bufs[XBUFSIZE];
498 struct scatterlist sgl[XBUFSIZE];
499 struct scatterlist sgl_saved[XBUFSIZE];
500 struct scatterlist *sgl_ptr;
501 unsigned int nents;
502};
503
504static int init_test_sglist(struct test_sglist *tsgl)
505{
506 return __testmgr_alloc_buf(tsgl->bufs, 1 /* two pages per buffer */);
507}
508
509static void destroy_test_sglist(struct test_sglist *tsgl)
510{
511 return __testmgr_free_buf(tsgl->bufs, 1 /* two pages per buffer */);
512}
513
514/**
515 * build_test_sglist() - build a scatterlist for a crypto test
516 *
517 * @tsgl: the scatterlist to build. @tsgl->bufs[] contains an array of 2-page
518 * buffers which the scatterlist @tsgl->sgl[] will be made to point into.
519 * @divs: the layout specification on which the scatterlist will be based
520 * @alignmask: the algorithm's alignmask
521 * @total_len: the total length of the scatterlist to build in bytes
522 * @data: if non-NULL, the buffers will be filled with this data until it ends.
523 * Otherwise the buffers will be poisoned. In both cases, some bytes
524 * past the end of each buffer will be poisoned to help detect overruns.
525 * @out_divs: if non-NULL, the test_sg_division to which each scatterlist entry
526 * corresponds will be returned here. This will match @divs except
527 * that divisions resolving to a length of 0 are omitted as they are
528 * not included in the scatterlist.
529 *
530 * Return: 0 or a -errno value
531 */
532static int build_test_sglist(struct test_sglist *tsgl,
533 const struct test_sg_division *divs,
534 const unsigned int alignmask,
535 const unsigned int total_len,
536 struct iov_iter *data,
537 const struct test_sg_division *out_divs[XBUFSIZE])
538{
539 struct {
540 const struct test_sg_division *div;
541 size_t length;
542 } partitions[XBUFSIZE];
543 const unsigned int ndivs = count_test_sg_divisions(divs);
544 unsigned int len_remaining = total_len;
545 unsigned int i;
546
547 BUILD_BUG_ON(ARRAY_SIZE(partitions) != ARRAY_SIZE(tsgl->sgl));
548 if (WARN_ON(ndivs > ARRAY_SIZE(partitions)))
549 return -EINVAL;
550
551 /* Calculate the (div, length) pairs */
552 tsgl->nents = 0;
553 for (i = 0; i < ndivs; i++) {
554 unsigned int len_this_sg =
555 min(len_remaining,
556 (total_len * divs[i].proportion_of_total +
557 TEST_SG_TOTAL / 2) / TEST_SG_TOTAL);
558
559 if (len_this_sg != 0) {
560 partitions[tsgl->nents].div = &divs[i];
561 partitions[tsgl->nents].length = len_this_sg;
562 tsgl->nents++;
563 len_remaining -= len_this_sg;
564 }
565 }
566 if (tsgl->nents == 0) {
567 partitions[tsgl->nents].div = &divs[0];
568 partitions[tsgl->nents].length = 0;
569 tsgl->nents++;
570 }
571 partitions[tsgl->nents - 1].length += len_remaining;
572
573 /* Set up the sgl entries and fill the data or poison */
574 sg_init_table(tsgl->sgl, tsgl->nents);
575 for (i = 0; i < tsgl->nents; i++) {
576 unsigned int offset = partitions[i].div->offset;
577 void *addr;
578
579 if (partitions[i].div->offset_relative_to_alignmask)
580 offset += alignmask;
581
582 while (offset + partitions[i].length + TESTMGR_POISON_LEN >
583 2 * PAGE_SIZE) {
584 if (WARN_ON(offset <= 0))
585 return -EINVAL;
586 offset /= 2;
587 }
588
589 addr = &tsgl->bufs[i][offset];
590 sg_set_buf(&tsgl->sgl[i], addr, partitions[i].length);
591
592 if (out_divs)
593 out_divs[i] = partitions[i].div;
594
595 if (data) {
596 size_t copy_len, copied;
597
598 copy_len = min(partitions[i].length, data->count);
599 copied = copy_from_iter(addr, copy_len, data);
600 if (WARN_ON(copied != copy_len))
601 return -EINVAL;
602 testmgr_poison(addr + copy_len, partitions[i].length +
603 TESTMGR_POISON_LEN - copy_len);
604 } else {
605 testmgr_poison(addr, partitions[i].length +
606 TESTMGR_POISON_LEN);
607 }
608 }
609
610 sg_mark_end(&tsgl->sgl[tsgl->nents - 1]);
611 tsgl->sgl_ptr = tsgl->sgl;
612 memcpy(tsgl->sgl_saved, tsgl->sgl, tsgl->nents * sizeof(tsgl->sgl[0]));
613 return 0;
614}
615
616/*
617 * Verify that a scatterlist crypto operation produced the correct output.
618 *
619 * @tsgl: scatterlist containing the actual output
620 * @expected_output: buffer containing the expected output
621 * @len_to_check: length of @expected_output in bytes
622 * @unchecked_prefix_len: number of ignored bytes in @tsgl prior to real result
623 * @check_poison: verify that the poison bytes after each chunk are intact?
624 *
625 * Return: 0 if correct, -EINVAL if incorrect, -EOVERFLOW if buffer overrun.
626 */
627static int verify_correct_output(const struct test_sglist *tsgl,
628 const char *expected_output,
629 unsigned int len_to_check,
630 unsigned int unchecked_prefix_len,
631 bool check_poison)
632{
633 unsigned int i;
634
635 for (i = 0; i < tsgl->nents; i++) {
636 struct scatterlist *sg = &tsgl->sgl_ptr[i];
637 unsigned int len = sg->length;
638 unsigned int offset = sg->offset;
639 const char *actual_output;
640
641 if (unchecked_prefix_len) {
642 if (unchecked_prefix_len >= len) {
643 unchecked_prefix_len -= len;
644 continue;
645 }
646 offset += unchecked_prefix_len;
647 len -= unchecked_prefix_len;
648 unchecked_prefix_len = 0;
649 }
650 len = min(len, len_to_check);
651 actual_output = page_address(sg_page(sg)) + offset;
652 if (memcmp(expected_output, actual_output, len) != 0)
653 return -EINVAL;
654 if (check_poison &&
655 !testmgr_is_poison(actual_output + len, TESTMGR_POISON_LEN))
656 return -EOVERFLOW;
657 len_to_check -= len;
658 expected_output += len;
659 }
660 if (WARN_ON(len_to_check != 0))
661 return -EINVAL;
662 return 0;
663}
664
665static bool is_test_sglist_corrupted(const struct test_sglist *tsgl)
666{
667 unsigned int i;
668
669 for (i = 0; i < tsgl->nents; i++) {
670 if (tsgl->sgl[i].page_link != tsgl->sgl_saved[i].page_link)
671 return true;
672 if (tsgl->sgl[i].offset != tsgl->sgl_saved[i].offset)
673 return true;
674 if (tsgl->sgl[i].length != tsgl->sgl_saved[i].length)
675 return true;
676 }
677 return false;
678}
679
680struct cipher_test_sglists {
681 struct test_sglist src;
682 struct test_sglist dst;
683};
684
685static struct cipher_test_sglists *alloc_cipher_test_sglists(void)
686{
687 struct cipher_test_sglists *tsgls;
688
689 tsgls = kmalloc(sizeof(*tsgls), GFP_KERNEL);
690 if (!tsgls)
691 return NULL;
692
693 if (init_test_sglist(&tsgls->src) != 0)
694 goto fail_kfree;
695 if (init_test_sglist(&tsgls->dst) != 0)
696 goto fail_destroy_src;
697
698 return tsgls;
699
700fail_destroy_src:
701 destroy_test_sglist(&tsgls->src);
702fail_kfree:
703 kfree(tsgls);
704 return NULL;
705}
706
707static void free_cipher_test_sglists(struct cipher_test_sglists *tsgls)
708{
709 if (tsgls) {
710 destroy_test_sglist(&tsgls->src);
711 destroy_test_sglist(&tsgls->dst);
712 kfree(tsgls);
713 }
714}
715
716/* Build the src and dst scatterlists for an skcipher or AEAD test */
717static int build_cipher_test_sglists(struct cipher_test_sglists *tsgls,
718 const struct testvec_config *cfg,
719 unsigned int alignmask,
720 unsigned int src_total_len,
721 unsigned int dst_total_len,
722 const struct kvec *inputs,
723 unsigned int nr_inputs)
724{
725 struct iov_iter input;
726 int err;
727
728 iov_iter_kvec(&input, WRITE, inputs, nr_inputs, src_total_len);
729 err = build_test_sglist(&tsgls->src, cfg->src_divs, alignmask,
730 cfg->inplace ?
731 max(dst_total_len, src_total_len) :
732 src_total_len,
733 &input, NULL);
734 if (err)
735 return err;
736
737 if (cfg->inplace) {
738 tsgls->dst.sgl_ptr = tsgls->src.sgl;
739 tsgls->dst.nents = tsgls->src.nents;
740 return 0;
741 }
742 return build_test_sglist(&tsgls->dst,
743 cfg->dst_divs[0].proportion_of_total ?
744 cfg->dst_divs : cfg->src_divs,
745 alignmask, dst_total_len, NULL, NULL);
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800746}
747
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800748#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
Eric Biggersf2bb770a2019-04-11 21:57:38 -0700749
750/* Generate a random length in range [0, max_len], but prefer smaller values */
751static unsigned int generate_random_length(unsigned int max_len)
752{
753 unsigned int len = prandom_u32() % (max_len + 1);
754
755 switch (prandom_u32() % 4) {
756 case 0:
757 return len % 64;
758 case 1:
759 return len % 256;
760 case 2:
761 return len % 1024;
762 default:
763 return len;
764 }
765}
766
767/* Sometimes make some random changes to the given data buffer */
768static void mutate_buffer(u8 *buf, size_t count)
769{
770 size_t num_flips;
771 size_t i;
772 size_t pos;
773
774 /* Sometimes flip some bits */
775 if (prandom_u32() % 4 == 0) {
776 num_flips = min_t(size_t, 1 << (prandom_u32() % 8), count * 8);
777 for (i = 0; i < num_flips; i++) {
778 pos = prandom_u32() % (count * 8);
779 buf[pos / 8] ^= 1 << (pos % 8);
780 }
781 }
782
783 /* Sometimes flip some bytes */
784 if (prandom_u32() % 4 == 0) {
785 num_flips = min_t(size_t, 1 << (prandom_u32() % 8), count);
786 for (i = 0; i < num_flips; i++)
787 buf[prandom_u32() % count] ^= 0xff;
788 }
789}
790
791/* Randomly generate 'count' bytes, but sometimes make them "interesting" */
792static void generate_random_bytes(u8 *buf, size_t count)
793{
794 u8 b;
795 u8 increment;
796 size_t i;
797
798 if (count == 0)
799 return;
800
801 switch (prandom_u32() % 8) { /* Choose a generation strategy */
802 case 0:
803 case 1:
804 /* All the same byte, plus optional mutations */
805 switch (prandom_u32() % 4) {
806 case 0:
807 b = 0x00;
808 break;
809 case 1:
810 b = 0xff;
811 break;
812 default:
813 b = (u8)prandom_u32();
814 break;
815 }
816 memset(buf, b, count);
817 mutate_buffer(buf, count);
818 break;
819 case 2:
820 /* Ascending or descending bytes, plus optional mutations */
821 increment = (u8)prandom_u32();
822 b = (u8)prandom_u32();
823 for (i = 0; i < count; i++, b += increment)
824 buf[i] = b;
825 mutate_buffer(buf, count);
826 break;
827 default:
828 /* Fully random bytes */
829 for (i = 0; i < count; i++)
830 buf[i] = (u8)prandom_u32();
831 }
832}
833
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800834static char *generate_random_sgl_divisions(struct test_sg_division *divs,
835 size_t max_divs, char *p, char *end,
Eric Biggers65707372019-03-12 22:12:52 -0700836 bool gen_flushes, u32 req_flags)
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800837{
838 struct test_sg_division *div = divs;
839 unsigned int remaining = TEST_SG_TOTAL;
840
841 do {
842 unsigned int this_len;
Eric Biggers65707372019-03-12 22:12:52 -0700843 const char *flushtype_str;
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800844
845 if (div == &divs[max_divs - 1] || prandom_u32() % 2 == 0)
846 this_len = remaining;
847 else
848 this_len = 1 + (prandom_u32() % remaining);
849 div->proportion_of_total = this_len;
850
851 if (prandom_u32() % 4 == 0)
852 div->offset = (PAGE_SIZE - 128) + (prandom_u32() % 128);
853 else if (prandom_u32() % 2 == 0)
854 div->offset = prandom_u32() % 32;
855 else
856 div->offset = prandom_u32() % PAGE_SIZE;
857 if (prandom_u32() % 8 == 0)
858 div->offset_relative_to_alignmask = true;
859
860 div->flush_type = FLUSH_TYPE_NONE;
861 if (gen_flushes) {
862 switch (prandom_u32() % 4) {
863 case 0:
864 div->flush_type = FLUSH_TYPE_REIMPORT;
865 break;
866 case 1:
867 div->flush_type = FLUSH_TYPE_FLUSH;
868 break;
869 }
870 }
871
Eric Biggers65707372019-03-12 22:12:52 -0700872 if (div->flush_type != FLUSH_TYPE_NONE &&
873 !(req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) &&
874 prandom_u32() % 2 == 0)
875 div->nosimd = true;
876
877 switch (div->flush_type) {
878 case FLUSH_TYPE_FLUSH:
879 if (div->nosimd)
880 flushtype_str = "<flush,nosimd>";
881 else
882 flushtype_str = "<flush>";
883 break;
884 case FLUSH_TYPE_REIMPORT:
885 if (div->nosimd)
886 flushtype_str = "<reimport,nosimd>";
887 else
888 flushtype_str = "<reimport>";
889 break;
890 default:
891 flushtype_str = "";
892 break;
893 }
894
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800895 BUILD_BUG_ON(TEST_SG_TOTAL != 10000); /* for "%u.%u%%" */
Eric Biggers65707372019-03-12 22:12:52 -0700896 p += scnprintf(p, end - p, "%s%u.%u%%@%s+%u%s", flushtype_str,
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800897 this_len / 100, this_len % 100,
898 div->offset_relative_to_alignmask ?
899 "alignmask" : "",
900 div->offset, this_len == remaining ? "" : ", ");
901 remaining -= this_len;
902 div++;
903 } while (remaining);
904
905 return p;
906}
907
908/* Generate a random testvec_config for fuzz testing */
909static void generate_random_testvec_config(struct testvec_config *cfg,
910 char *name, size_t max_namelen)
911{
912 char *p = name;
913 char * const end = name + max_namelen;
914
915 memset(cfg, 0, sizeof(*cfg));
916
917 cfg->name = name;
918
919 p += scnprintf(p, end - p, "random:");
920
921 if (prandom_u32() % 2 == 0) {
922 cfg->inplace = true;
923 p += scnprintf(p, end - p, " inplace");
924 }
925
926 if (prandom_u32() % 2 == 0) {
927 cfg->req_flags |= CRYPTO_TFM_REQ_MAY_SLEEP;
928 p += scnprintf(p, end - p, " may_sleep");
929 }
930
931 switch (prandom_u32() % 4) {
932 case 0:
933 cfg->finalization_type = FINALIZATION_TYPE_FINAL;
934 p += scnprintf(p, end - p, " use_final");
935 break;
936 case 1:
937 cfg->finalization_type = FINALIZATION_TYPE_FINUP;
938 p += scnprintf(p, end - p, " use_finup");
939 break;
940 default:
941 cfg->finalization_type = FINALIZATION_TYPE_DIGEST;
942 p += scnprintf(p, end - p, " use_digest");
943 break;
944 }
945
Eric Biggers65707372019-03-12 22:12:52 -0700946 if (!(cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) &&
947 prandom_u32() % 2 == 0) {
948 cfg->nosimd = true;
949 p += scnprintf(p, end - p, " nosimd");
950 }
951
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800952 p += scnprintf(p, end - p, " src_divs=[");
953 p = generate_random_sgl_divisions(cfg->src_divs,
954 ARRAY_SIZE(cfg->src_divs), p, end,
955 (cfg->finalization_type !=
Eric Biggers65707372019-03-12 22:12:52 -0700956 FINALIZATION_TYPE_DIGEST),
957 cfg->req_flags);
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800958 p += scnprintf(p, end - p, "]");
959
960 if (!cfg->inplace && prandom_u32() % 2 == 0) {
961 p += scnprintf(p, end - p, " dst_divs=[");
962 p = generate_random_sgl_divisions(cfg->dst_divs,
963 ARRAY_SIZE(cfg->dst_divs),
Eric Biggers65707372019-03-12 22:12:52 -0700964 p, end, false,
965 cfg->req_flags);
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800966 p += scnprintf(p, end - p, "]");
967 }
968
969 if (prandom_u32() % 2 == 0) {
970 cfg->iv_offset = 1 + (prandom_u32() % MAX_ALGAPI_ALIGNMASK);
971 p += scnprintf(p, end - p, " iv_offset=%u", cfg->iv_offset);
972 }
973
974 WARN_ON_ONCE(!valid_testvec_config(cfg));
975}
Eric Biggersb55e1a32019-03-12 22:12:47 -0700976
977static void crypto_disable_simd_for_test(void)
978{
979 preempt_disable();
980 __this_cpu_write(crypto_simd_disabled_for_test, true);
981}
982
983static void crypto_reenable_simd_for_test(void)
984{
985 __this_cpu_write(crypto_simd_disabled_for_test, false);
986 preempt_enable();
987}
Eric Biggersf2bb770a2019-04-11 21:57:38 -0700988
989/*
990 * Given an algorithm name, build the name of the generic implementation of that
991 * algorithm, assuming the usual naming convention. Specifically, this appends
992 * "-generic" to every part of the name that is not a template name. Examples:
993 *
994 * aes => aes-generic
995 * cbc(aes) => cbc(aes-generic)
996 * cts(cbc(aes)) => cts(cbc(aes-generic))
997 * rfc7539(chacha20,poly1305) => rfc7539(chacha20-generic,poly1305-generic)
998 *
999 * Return: 0 on success, or -ENAMETOOLONG if the generic name would be too long
1000 */
1001static int build_generic_driver_name(const char *algname,
1002 char driver_name[CRYPTO_MAX_ALG_NAME])
1003{
1004 const char *in = algname;
1005 char *out = driver_name;
1006 size_t len = strlen(algname);
1007
1008 if (len >= CRYPTO_MAX_ALG_NAME)
1009 goto too_long;
1010 do {
1011 const char *in_saved = in;
1012
1013 while (*in && *in != '(' && *in != ')' && *in != ',')
1014 *out++ = *in++;
1015 if (*in != '(' && in > in_saved) {
1016 len += 8;
1017 if (len >= CRYPTO_MAX_ALG_NAME)
1018 goto too_long;
1019 memcpy(out, "-generic", 8);
1020 out += 8;
1021 }
1022 } while ((*out++ = *in++) != '\0');
1023 return 0;
1024
1025too_long:
1026 pr_err("alg: generic driver name for \"%s\" would be too long\n",
1027 algname);
1028 return -ENAMETOOLONG;
1029}
Eric Biggersb55e1a32019-03-12 22:12:47 -07001030#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1031static void crypto_disable_simd_for_test(void)
1032{
1033}
1034
1035static void crypto_reenable_simd_for_test(void)
1036{
1037}
1038#endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
Eric Biggers25f9ddd2019-01-31 23:51:45 -08001039
Eric Biggers65707372019-03-12 22:12:52 -07001040static int do_ahash_op(int (*op)(struct ahash_request *req),
1041 struct ahash_request *req,
1042 struct crypto_wait *wait, bool nosimd)
1043{
1044 int err;
1045
1046 if (nosimd)
1047 crypto_disable_simd_for_test();
1048
1049 err = op(req);
1050
1051 if (nosimd)
1052 crypto_reenable_simd_for_test();
1053
1054 return crypto_wait_req(err, wait);
1055}
1056
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001057static int check_nonfinal_hash_op(const char *op, int err,
1058 u8 *result, unsigned int digestsize,
Eric Biggers951d1332019-04-11 21:57:37 -07001059 const char *driver, const char *vec_name,
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001060 const struct testvec_config *cfg)
Kamil Konieczny466d7b92018-01-16 15:26:13 +01001061{
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001062 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07001063 pr_err("alg: hash: %s %s() failed with err %d on test vector %s, cfg=\"%s\"\n",
1064 driver, op, err, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001065 return err;
1066 }
1067 if (!testmgr_is_poison(result, digestsize)) {
Eric Biggers951d1332019-04-11 21:57:37 -07001068 pr_err("alg: hash: %s %s() used result buffer on test vector %s, cfg=\"%s\"\n",
1069 driver, op, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001070 return -EINVAL;
1071 }
1072 return 0;
1073}
Kamil Konieczny466d7b92018-01-16 15:26:13 +01001074
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001075static int test_hash_vec_cfg(const char *driver,
1076 const struct hash_testvec *vec,
Eric Biggers951d1332019-04-11 21:57:37 -07001077 const char *vec_name,
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001078 const struct testvec_config *cfg,
1079 struct ahash_request *req,
1080 struct test_sglist *tsgl,
1081 u8 *hashstate)
1082{
1083 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1084 const unsigned int alignmask = crypto_ahash_alignmask(tfm);
1085 const unsigned int digestsize = crypto_ahash_digestsize(tfm);
1086 const unsigned int statesize = crypto_ahash_statesize(tfm);
1087 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1088 const struct test_sg_division *divs[XBUFSIZE];
1089 DECLARE_CRYPTO_WAIT(wait);
1090 struct kvec _input;
1091 struct iov_iter input;
1092 unsigned int i;
1093 struct scatterlist *pending_sgl;
1094 unsigned int pending_len;
1095 u8 result[HASH_MAX_DIGESTSIZE + TESTMGR_POISON_LEN];
1096 int err;
1097
1098 /* Set the key, if specified */
1099 if (vec->ksize) {
1100 err = crypto_ahash_setkey(tfm, vec->key, vec->ksize);
1101 if (err) {
Eric Biggers5283a8e2019-04-11 21:57:36 -07001102 if (err == vec->setkey_error)
1103 return 0;
Eric Biggers951d1332019-04-11 21:57:37 -07001104 pr_err("alg: hash: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1105 driver, vec_name, vec->setkey_error, err,
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001106 crypto_ahash_get_flags(tfm));
1107 return err;
1108 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07001109 if (vec->setkey_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001110 pr_err("alg: hash: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1111 driver, vec_name, vec->setkey_error);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001112 return -EINVAL;
1113 }
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001114 }
1115
1116 /* Build the scatterlist for the source data */
1117 _input.iov_base = (void *)vec->plaintext;
1118 _input.iov_len = vec->psize;
1119 iov_iter_kvec(&input, WRITE, &_input, 1, vec->psize);
1120 err = build_test_sglist(tsgl, cfg->src_divs, alignmask, vec->psize,
1121 &input, divs);
1122 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07001123 pr_err("alg: hash: %s: error preparing scatterlist for test vector %s, cfg=\"%s\"\n",
1124 driver, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001125 return err;
1126 }
1127
1128 /* Do the actual hashing */
1129
1130 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
1131 testmgr_poison(result, digestsize + TESTMGR_POISON_LEN);
1132
Eric Biggers5283a8e2019-04-11 21:57:36 -07001133 if (cfg->finalization_type == FINALIZATION_TYPE_DIGEST ||
1134 vec->digest_error) {
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001135 /* Just using digest() */
1136 ahash_request_set_callback(req, req_flags, crypto_req_done,
1137 &wait);
1138 ahash_request_set_crypt(req, tsgl->sgl, result, vec->psize);
Eric Biggers65707372019-03-12 22:12:52 -07001139 err = do_ahash_op(crypto_ahash_digest, req, &wait, cfg->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001140 if (err) {
Eric Biggers5283a8e2019-04-11 21:57:36 -07001141 if (err == vec->digest_error)
1142 return 0;
Eric Biggers951d1332019-04-11 21:57:37 -07001143 pr_err("alg: hash: %s digest() failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
1144 driver, vec_name, vec->digest_error, err,
Eric Biggers5283a8e2019-04-11 21:57:36 -07001145 cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001146 return err;
1147 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07001148 if (vec->digest_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001149 pr_err("alg: hash: %s digest() unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
1150 driver, vec_name, vec->digest_error, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001151 return -EINVAL;
1152 }
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001153 goto result_ready;
1154 }
1155
1156 /* Using init(), zero or more update(), then final() or finup() */
1157
1158 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
1159 ahash_request_set_crypt(req, NULL, result, 0);
Eric Biggers65707372019-03-12 22:12:52 -07001160 err = do_ahash_op(crypto_ahash_init, req, &wait, cfg->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001161 err = check_nonfinal_hash_op("init", err, result, digestsize,
Eric Biggers951d1332019-04-11 21:57:37 -07001162 driver, vec_name, cfg);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001163 if (err)
1164 return err;
1165
1166 pending_sgl = NULL;
1167 pending_len = 0;
1168 for (i = 0; i < tsgl->nents; i++) {
1169 if (divs[i]->flush_type != FLUSH_TYPE_NONE &&
1170 pending_sgl != NULL) {
1171 /* update() with the pending data */
1172 ahash_request_set_callback(req, req_flags,
1173 crypto_req_done, &wait);
1174 ahash_request_set_crypt(req, pending_sgl, result,
1175 pending_len);
Eric Biggers65707372019-03-12 22:12:52 -07001176 err = do_ahash_op(crypto_ahash_update, req, &wait,
1177 divs[i]->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001178 err = check_nonfinal_hash_op("update", err,
1179 result, digestsize,
Eric Biggers951d1332019-04-11 21:57:37 -07001180 driver, vec_name, cfg);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001181 if (err)
1182 return err;
1183 pending_sgl = NULL;
1184 pending_len = 0;
1185 }
1186 if (divs[i]->flush_type == FLUSH_TYPE_REIMPORT) {
1187 /* Test ->export() and ->import() */
1188 testmgr_poison(hashstate + statesize,
1189 TESTMGR_POISON_LEN);
1190 err = crypto_ahash_export(req, hashstate);
1191 err = check_nonfinal_hash_op("export", err,
1192 result, digestsize,
Eric Biggers951d1332019-04-11 21:57:37 -07001193 driver, vec_name, cfg);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001194 if (err)
1195 return err;
1196 if (!testmgr_is_poison(hashstate + statesize,
1197 TESTMGR_POISON_LEN)) {
Eric Biggers951d1332019-04-11 21:57:37 -07001198 pr_err("alg: hash: %s export() overran state buffer on test vector %s, cfg=\"%s\"\n",
1199 driver, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001200 return -EOVERFLOW;
1201 }
1202
1203 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
1204 err = crypto_ahash_import(req, hashstate);
1205 err = check_nonfinal_hash_op("import", err,
1206 result, digestsize,
Eric Biggers951d1332019-04-11 21:57:37 -07001207 driver, vec_name, cfg);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001208 if (err)
1209 return err;
1210 }
1211 if (pending_sgl == NULL)
1212 pending_sgl = &tsgl->sgl[i];
1213 pending_len += tsgl->sgl[i].length;
1214 }
1215
1216 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
1217 ahash_request_set_crypt(req, pending_sgl, result, pending_len);
1218 if (cfg->finalization_type == FINALIZATION_TYPE_FINAL) {
1219 /* finish with update() and final() */
Eric Biggers65707372019-03-12 22:12:52 -07001220 err = do_ahash_op(crypto_ahash_update, req, &wait, cfg->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001221 err = check_nonfinal_hash_op("update", err, result, digestsize,
Eric Biggers951d1332019-04-11 21:57:37 -07001222 driver, vec_name, cfg);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001223 if (err)
1224 return err;
Eric Biggers65707372019-03-12 22:12:52 -07001225 err = do_ahash_op(crypto_ahash_final, req, &wait, cfg->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001226 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07001227 pr_err("alg: hash: %s final() failed with err %d on test vector %s, cfg=\"%s\"\n",
1228 driver, err, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001229 return err;
1230 }
1231 } else {
1232 /* finish with finup() */
Eric Biggers65707372019-03-12 22:12:52 -07001233 err = do_ahash_op(crypto_ahash_finup, req, &wait, cfg->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001234 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07001235 pr_err("alg: hash: %s finup() failed with err %d on test vector %s, cfg=\"%s\"\n",
1236 driver, err, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001237 return err;
1238 }
1239 }
1240
1241result_ready:
1242 /* Check that the algorithm produced the correct digest */
1243 if (memcmp(result, vec->digest, digestsize) != 0) {
Eric Biggers951d1332019-04-11 21:57:37 -07001244 pr_err("alg: hash: %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
1245 driver, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001246 return -EINVAL;
1247 }
1248 if (!testmgr_is_poison(&result[digestsize], TESTMGR_POISON_LEN)) {
Eric Biggers951d1332019-04-11 21:57:37 -07001249 pr_err("alg: hash: %s overran result buffer on test vector %s, cfg=\"%s\"\n",
1250 driver, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001251 return -EOVERFLOW;
1252 }
1253
1254 return 0;
1255}
1256
1257static int test_hash_vec(const char *driver, const struct hash_testvec *vec,
1258 unsigned int vec_num, struct ahash_request *req,
1259 struct test_sglist *tsgl, u8 *hashstate)
1260{
Eric Biggers951d1332019-04-11 21:57:37 -07001261 char vec_name[16];
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001262 unsigned int i;
1263 int err;
1264
Eric Biggers951d1332019-04-11 21:57:37 -07001265 sprintf(vec_name, "%u", vec_num);
1266
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001267 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++) {
Eric Biggers951d1332019-04-11 21:57:37 -07001268 err = test_hash_vec_cfg(driver, vec, vec_name,
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001269 &default_hash_testvec_configs[i],
1270 req, tsgl, hashstate);
1271 if (err)
1272 return err;
1273 }
1274
1275#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1276 if (!noextratests) {
1277 struct testvec_config cfg;
1278 char cfgname[TESTVEC_CONFIG_NAMELEN];
1279
1280 for (i = 0; i < fuzz_iterations; i++) {
1281 generate_random_testvec_config(&cfg, cfgname,
1282 sizeof(cfgname));
Eric Biggers951d1332019-04-11 21:57:37 -07001283 err = test_hash_vec_cfg(driver, vec, vec_name, &cfg,
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001284 req, tsgl, hashstate);
1285 if (err)
1286 return err;
1287 }
1288 }
1289#endif
1290 return 0;
1291}
1292
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001293#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1294/*
1295 * Generate a hash test vector from the given implementation.
1296 * Assumes the buffers in 'vec' were already allocated.
1297 */
1298static void generate_random_hash_testvec(struct crypto_shash *tfm,
1299 struct hash_testvec *vec,
1300 unsigned int maxkeysize,
1301 unsigned int maxdatasize,
1302 char *name, size_t max_namelen)
1303{
1304 SHASH_DESC_ON_STACK(desc, tfm);
1305
1306 /* Data */
1307 vec->psize = generate_random_length(maxdatasize);
1308 generate_random_bytes((u8 *)vec->plaintext, vec->psize);
1309
1310 /*
1311 * Key: length in range [1, maxkeysize], but usually choose maxkeysize.
1312 * If algorithm is unkeyed, then maxkeysize == 0 and set ksize = 0.
1313 */
1314 vec->setkey_error = 0;
1315 vec->ksize = 0;
1316 if (maxkeysize) {
1317 vec->ksize = maxkeysize;
1318 if (prandom_u32() % 4 == 0)
1319 vec->ksize = 1 + (prandom_u32() % maxkeysize);
1320 generate_random_bytes((u8 *)vec->key, vec->ksize);
1321
1322 vec->setkey_error = crypto_shash_setkey(tfm, vec->key,
1323 vec->ksize);
1324 /* If the key couldn't be set, no need to continue to digest. */
1325 if (vec->setkey_error)
1326 goto done;
1327 }
1328
1329 /* Digest */
1330 desc->tfm = tfm;
1331 desc->flags = 0;
1332 vec->digest_error = crypto_shash_digest(desc, vec->plaintext,
1333 vec->psize, (u8 *)vec->digest);
1334done:
1335 snprintf(name, max_namelen, "\"random: psize=%u ksize=%u\"",
1336 vec->psize, vec->ksize);
1337}
1338
1339/*
1340 * Test the hash algorithm represented by @req against the corresponding generic
1341 * implementation, if one is available.
1342 */
1343static int test_hash_vs_generic_impl(const char *driver,
1344 const char *generic_driver,
1345 unsigned int maxkeysize,
1346 struct ahash_request *req,
1347 struct test_sglist *tsgl,
1348 u8 *hashstate)
1349{
1350 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1351 const unsigned int digestsize = crypto_ahash_digestsize(tfm);
1352 const unsigned int blocksize = crypto_ahash_blocksize(tfm);
1353 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
1354 const char *algname = crypto_hash_alg_common(tfm)->base.cra_name;
1355 char _generic_driver[CRYPTO_MAX_ALG_NAME];
1356 struct crypto_shash *generic_tfm = NULL;
1357 unsigned int i;
1358 struct hash_testvec vec = { 0 };
1359 char vec_name[64];
1360 struct testvec_config cfg;
1361 char cfgname[TESTVEC_CONFIG_NAMELEN];
1362 int err;
1363
1364 if (noextratests)
1365 return 0;
1366
1367 if (!generic_driver) { /* Use default naming convention? */
1368 err = build_generic_driver_name(algname, _generic_driver);
1369 if (err)
1370 return err;
1371 generic_driver = _generic_driver;
1372 }
1373
1374 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
1375 return 0;
1376
1377 generic_tfm = crypto_alloc_shash(generic_driver, 0, 0);
1378 if (IS_ERR(generic_tfm)) {
1379 err = PTR_ERR(generic_tfm);
1380 if (err == -ENOENT) {
1381 pr_warn("alg: hash: skipping comparison tests for %s because %s is unavailable\n",
1382 driver, generic_driver);
1383 return 0;
1384 }
1385 pr_err("alg: hash: error allocating %s (generic impl of %s): %d\n",
1386 generic_driver, algname, err);
1387 return err;
1388 }
1389
1390 /* Check the algorithm properties for consistency. */
1391
1392 if (digestsize != crypto_shash_digestsize(generic_tfm)) {
1393 pr_err("alg: hash: digestsize for %s (%u) doesn't match generic impl (%u)\n",
1394 driver, digestsize,
1395 crypto_shash_digestsize(generic_tfm));
1396 err = -EINVAL;
1397 goto out;
1398 }
1399
1400 if (blocksize != crypto_shash_blocksize(generic_tfm)) {
1401 pr_err("alg: hash: blocksize for %s (%u) doesn't match generic impl (%u)\n",
1402 driver, blocksize, crypto_shash_blocksize(generic_tfm));
1403 err = -EINVAL;
1404 goto out;
1405 }
1406
1407 /*
1408 * Now generate test vectors using the generic implementation, and test
1409 * the other implementation against them.
1410 */
1411
1412 vec.key = kmalloc(maxkeysize, GFP_KERNEL);
1413 vec.plaintext = kmalloc(maxdatasize, GFP_KERNEL);
1414 vec.digest = kmalloc(digestsize, GFP_KERNEL);
1415 if (!vec.key || !vec.plaintext || !vec.digest) {
1416 err = -ENOMEM;
1417 goto out;
1418 }
1419
1420 for (i = 0; i < fuzz_iterations * 8; i++) {
1421 generate_random_hash_testvec(generic_tfm, &vec,
1422 maxkeysize, maxdatasize,
1423 vec_name, sizeof(vec_name));
1424 generate_random_testvec_config(&cfg, cfgname, sizeof(cfgname));
1425
1426 err = test_hash_vec_cfg(driver, &vec, vec_name, &cfg,
1427 req, tsgl, hashstate);
1428 if (err)
1429 goto out;
1430 cond_resched();
1431 }
1432 err = 0;
1433out:
1434 kfree(vec.key);
1435 kfree(vec.plaintext);
1436 kfree(vec.digest);
1437 crypto_free_shash(generic_tfm);
1438 return err;
1439}
1440#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1441static int test_hash_vs_generic_impl(const char *driver,
1442 const char *generic_driver,
1443 unsigned int maxkeysize,
1444 struct ahash_request *req,
1445 struct test_sglist *tsgl,
1446 u8 *hashstate)
1447{
1448 return 0;
1449}
1450#endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1451
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001452static int __alg_test_hash(const struct hash_testvec *vecs,
1453 unsigned int num_vecs, const char *driver,
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001454 u32 type, u32 mask,
1455 const char *generic_driver, unsigned int maxkeysize)
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001456{
1457 struct crypto_ahash *tfm;
1458 struct ahash_request *req = NULL;
1459 struct test_sglist *tsgl = NULL;
1460 u8 *hashstate = NULL;
1461 unsigned int i;
1462 int err;
1463
1464 tfm = crypto_alloc_ahash(driver, type, mask);
1465 if (IS_ERR(tfm)) {
1466 pr_err("alg: hash: failed to allocate transform for %s: %ld\n",
1467 driver, PTR_ERR(tfm));
1468 return PTR_ERR(tfm);
1469 }
1470
1471 req = ahash_request_alloc(tfm, GFP_KERNEL);
1472 if (!req) {
1473 pr_err("alg: hash: failed to allocate request for %s\n",
1474 driver);
1475 err = -ENOMEM;
1476 goto out;
1477 }
1478
1479 tsgl = kmalloc(sizeof(*tsgl), GFP_KERNEL);
1480 if (!tsgl || init_test_sglist(tsgl) != 0) {
1481 pr_err("alg: hash: failed to allocate test buffers for %s\n",
1482 driver);
1483 kfree(tsgl);
1484 tsgl = NULL;
1485 err = -ENOMEM;
1486 goto out;
1487 }
1488
1489 hashstate = kmalloc(crypto_ahash_statesize(tfm) + TESTMGR_POISON_LEN,
1490 GFP_KERNEL);
1491 if (!hashstate) {
1492 pr_err("alg: hash: failed to allocate hash state buffer for %s\n",
1493 driver);
1494 err = -ENOMEM;
1495 goto out;
1496 }
1497
1498 for (i = 0; i < num_vecs; i++) {
1499 err = test_hash_vec(driver, &vecs[i], i, req, tsgl, hashstate);
1500 if (err)
1501 goto out;
1502 }
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001503 err = test_hash_vs_generic_impl(driver, generic_driver, maxkeysize, req,
1504 tsgl, hashstate);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001505out:
1506 kfree(hashstate);
1507 if (tsgl) {
1508 destroy_test_sglist(tsgl);
1509 kfree(tsgl);
1510 }
1511 ahash_request_free(req);
1512 crypto_free_ahash(tfm);
1513 return err;
1514}
1515
1516static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1517 u32 type, u32 mask)
1518{
1519 const struct hash_testvec *template = desc->suite.hash.vecs;
1520 unsigned int tcount = desc->suite.hash.count;
1521 unsigned int nr_unkeyed, nr_keyed;
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001522 unsigned int maxkeysize = 0;
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001523 int err;
1524
1525 /*
1526 * For OPTIONAL_KEY algorithms, we have to do all the unkeyed tests
1527 * first, before setting a key on the tfm. To make this easier, we
1528 * require that the unkeyed test vectors (if any) are listed first.
1529 */
1530
1531 for (nr_unkeyed = 0; nr_unkeyed < tcount; nr_unkeyed++) {
1532 if (template[nr_unkeyed].ksize)
1533 break;
1534 }
1535 for (nr_keyed = 0; nr_unkeyed + nr_keyed < tcount; nr_keyed++) {
1536 if (!template[nr_unkeyed + nr_keyed].ksize) {
1537 pr_err("alg: hash: test vectors for %s out of order, "
1538 "unkeyed ones must come first\n", desc->alg);
Kamil Konieczny466d7b92018-01-16 15:26:13 +01001539 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08001540 }
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001541 maxkeysize = max_t(unsigned int, maxkeysize,
1542 template[nr_unkeyed + nr_keyed].ksize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001543 }
1544
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001545 err = 0;
1546 if (nr_unkeyed) {
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001547 err = __alg_test_hash(template, nr_unkeyed, driver, type, mask,
1548 desc->generic_driver, maxkeysize);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001549 template += nr_unkeyed;
Herbert Xuda7f0332008-07-31 17:08:25 +08001550 }
1551
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001552 if (!err && nr_keyed)
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001553 err = __alg_test_hash(template, nr_keyed, driver, type, mask,
1554 desc->generic_driver, maxkeysize);
Wang, Rui Y018ba952016-02-03 18:26:57 +08001555
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001556 return err;
Jussi Kivilinnada5ffe12013-06-13 17:37:55 +03001557}
1558
Eric Biggersed968042019-01-31 23:51:47 -08001559static int test_aead_vec_cfg(const char *driver, int enc,
1560 const struct aead_testvec *vec,
Eric Biggers951d1332019-04-11 21:57:37 -07001561 const char *vec_name,
Eric Biggersed968042019-01-31 23:51:47 -08001562 const struct testvec_config *cfg,
1563 struct aead_request *req,
1564 struct cipher_test_sglists *tsgls)
Herbert Xuda7f0332008-07-31 17:08:25 +08001565{
Eric Biggersed968042019-01-31 23:51:47 -08001566 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1567 const unsigned int alignmask = crypto_aead_alignmask(tfm);
1568 const unsigned int ivsize = crypto_aead_ivsize(tfm);
1569 const unsigned int authsize = vec->clen - vec->plen;
1570 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1571 const char *op = enc ? "encryption" : "decryption";
1572 DECLARE_CRYPTO_WAIT(wait);
1573 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
1574 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
1575 cfg->iv_offset +
1576 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
1577 struct kvec input[2];
Eric Biggers5283a8e2019-04-11 21:57:36 -07001578 int expected_error;
Eric Biggersed968042019-01-31 23:51:47 -08001579 int err;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001580
Eric Biggersed968042019-01-31 23:51:47 -08001581 /* Set the key */
1582 if (vec->wk)
1583 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001584 else
Eric Biggersed968042019-01-31 23:51:47 -08001585 crypto_aead_clear_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1586 err = crypto_aead_setkey(tfm, vec->key, vec->klen);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001587 if (err && err != vec->setkey_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001588 pr_err("alg: aead: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1589 driver, vec_name, vec->setkey_error, err,
Eric Biggers5283a8e2019-04-11 21:57:36 -07001590 crypto_aead_get_flags(tfm));
Eric Biggersed968042019-01-31 23:51:47 -08001591 return err;
1592 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07001593 if (!err && vec->setkey_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001594 pr_err("alg: aead: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1595 driver, vec_name, vec->setkey_error);
Eric Biggersed968042019-01-31 23:51:47 -08001596 return -EINVAL;
1597 }
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001598
Eric Biggersed968042019-01-31 23:51:47 -08001599 /* Set the authentication tag size */
1600 err = crypto_aead_setauthsize(tfm, authsize);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001601 if (err && err != vec->setauthsize_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001602 pr_err("alg: aead: %s setauthsize failed on test vector %s; expected_error=%d, actual_error=%d\n",
1603 driver, vec_name, vec->setauthsize_error, err);
Eric Biggersed968042019-01-31 23:51:47 -08001604 return err;
1605 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07001606 if (!err && vec->setauthsize_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001607 pr_err("alg: aead: %s setauthsize unexpectedly succeeded on test vector %s; expected_error=%d\n",
1608 driver, vec_name, vec->setauthsize_error);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001609 return -EINVAL;
1610 }
1611
1612 if (vec->setkey_error || vec->setauthsize_error)
1613 return 0;
Eric Biggersed968042019-01-31 23:51:47 -08001614
1615 /* The IV must be copied to a buffer, as the algorithm may modify it */
1616 if (WARN_ON(ivsize > MAX_IVLEN))
1617 return -EINVAL;
1618 if (vec->iv)
1619 memcpy(iv, vec->iv, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001620 else
Eric Biggersed968042019-01-31 23:51:47 -08001621 memset(iv, 0, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001622
Eric Biggersed968042019-01-31 23:51:47 -08001623 /* Build the src/dst scatterlists */
1624 input[0].iov_base = (void *)vec->assoc;
1625 input[0].iov_len = vec->alen;
1626 input[1].iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
1627 input[1].iov_len = enc ? vec->plen : vec->clen;
1628 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
1629 vec->alen + (enc ? vec->plen :
1630 vec->clen),
1631 vec->alen + (enc ? vec->clen :
1632 vec->plen),
1633 input, 2);
1634 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07001635 pr_err("alg: aead: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
1636 driver, op, vec_name, cfg->name);
Eric Biggersed968042019-01-31 23:51:47 -08001637 return err;
Herbert Xuda7f0332008-07-31 17:08:25 +08001638 }
1639
Eric Biggersed968042019-01-31 23:51:47 -08001640 /* Do the actual encryption or decryption */
1641 testmgr_poison(req->__ctx, crypto_aead_reqsize(tfm));
1642 aead_request_set_callback(req, req_flags, crypto_req_done, &wait);
1643 aead_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
1644 enc ? vec->plen : vec->clen, iv);
1645 aead_request_set_ad(req, vec->alen);
Eric Biggers65707372019-03-12 22:12:52 -07001646 if (cfg->nosimd)
1647 crypto_disable_simd_for_test();
1648 err = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
1649 if (cfg->nosimd)
1650 crypto_reenable_simd_for_test();
1651 err = crypto_wait_req(err, &wait);
Herbert Xuda7f0332008-07-31 17:08:25 +08001652
Eric Biggersa6e5ef92019-01-31 23:51:50 -08001653 /* Check that the algorithm didn't overwrite things it shouldn't have */
1654 if (req->cryptlen != (enc ? vec->plen : vec->clen) ||
1655 req->assoclen != vec->alen ||
1656 req->iv != iv ||
1657 req->src != tsgls->src.sgl_ptr ||
1658 req->dst != tsgls->dst.sgl_ptr ||
1659 crypto_aead_reqtfm(req) != tfm ||
1660 req->base.complete != crypto_req_done ||
1661 req->base.flags != req_flags ||
1662 req->base.data != &wait) {
Eric Biggers951d1332019-04-11 21:57:37 -07001663 pr_err("alg: aead: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
1664 driver, op, vec_name, cfg->name);
Eric Biggersa6e5ef92019-01-31 23:51:50 -08001665 if (req->cryptlen != (enc ? vec->plen : vec->clen))
1666 pr_err("alg: aead: changed 'req->cryptlen'\n");
1667 if (req->assoclen != vec->alen)
1668 pr_err("alg: aead: changed 'req->assoclen'\n");
1669 if (req->iv != iv)
1670 pr_err("alg: aead: changed 'req->iv'\n");
1671 if (req->src != tsgls->src.sgl_ptr)
1672 pr_err("alg: aead: changed 'req->src'\n");
1673 if (req->dst != tsgls->dst.sgl_ptr)
1674 pr_err("alg: aead: changed 'req->dst'\n");
1675 if (crypto_aead_reqtfm(req) != tfm)
1676 pr_err("alg: aead: changed 'req->base.tfm'\n");
1677 if (req->base.complete != crypto_req_done)
1678 pr_err("alg: aead: changed 'req->base.complete'\n");
1679 if (req->base.flags != req_flags)
1680 pr_err("alg: aead: changed 'req->base.flags'\n");
1681 if (req->base.data != &wait)
1682 pr_err("alg: aead: changed 'req->base.data'\n");
1683 return -EINVAL;
1684 }
1685 if (is_test_sglist_corrupted(&tsgls->src)) {
Eric Biggers951d1332019-04-11 21:57:37 -07001686 pr_err("alg: aead: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
1687 driver, op, vec_name, cfg->name);
Eric Biggersa6e5ef92019-01-31 23:51:50 -08001688 return -EINVAL;
1689 }
1690 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
1691 is_test_sglist_corrupted(&tsgls->dst)) {
Eric Biggers951d1332019-04-11 21:57:37 -07001692 pr_err("alg: aead: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
1693 driver, op, vec_name, cfg->name);
Eric Biggersa6e5ef92019-01-31 23:51:50 -08001694 return -EINVAL;
1695 }
1696
Eric Biggers5283a8e2019-04-11 21:57:36 -07001697 /* Check for success or failure */
1698 expected_error = vec->novrfy ? -EBADMSG : vec->crypt_error;
1699 if (err) {
1700 if (err == expected_error)
1701 return 0;
Eric Biggers951d1332019-04-11 21:57:37 -07001702 pr_err("alg: aead: %s %s failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
1703 driver, op, vec_name, expected_error, err, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001704 return err;
1705 }
1706 if (expected_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001707 pr_err("alg: aead: %s %s unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
1708 driver, op, vec_name, expected_error, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001709 return -EINVAL;
1710 }
1711
Eric Biggersed968042019-01-31 23:51:47 -08001712 /* Check for the correct output (ciphertext or plaintext) */
1713 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
1714 enc ? vec->clen : vec->plen,
1715 vec->alen, enc || !cfg->inplace);
1716 if (err == -EOVERFLOW) {
Eric Biggers951d1332019-04-11 21:57:37 -07001717 pr_err("alg: aead: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
1718 driver, op, vec_name, cfg->name);
Eric Biggersed968042019-01-31 23:51:47 -08001719 return err;
Herbert Xuda7f0332008-07-31 17:08:25 +08001720 }
Eric Biggersed968042019-01-31 23:51:47 -08001721 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07001722 pr_err("alg: aead: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
1723 driver, op, vec_name, cfg->name);
Eric Biggersed968042019-01-31 23:51:47 -08001724 return err;
Jussi Kivilinna58dcf542013-06-13 17:37:50 +03001725 }
1726
1727 return 0;
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001728}
1729
Eric Biggersed968042019-01-31 23:51:47 -08001730static int test_aead_vec(const char *driver, int enc,
1731 const struct aead_testvec *vec, unsigned int vec_num,
1732 struct aead_request *req,
1733 struct cipher_test_sglists *tsgls)
1734{
Eric Biggers951d1332019-04-11 21:57:37 -07001735 char vec_name[16];
Eric Biggersed968042019-01-31 23:51:47 -08001736 unsigned int i;
1737 int err;
1738
1739 if (enc && vec->novrfy)
1740 return 0;
1741
Eric Biggers951d1332019-04-11 21:57:37 -07001742 sprintf(vec_name, "%u", vec_num);
1743
Eric Biggersed968042019-01-31 23:51:47 -08001744 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
Eric Biggers951d1332019-04-11 21:57:37 -07001745 err = test_aead_vec_cfg(driver, enc, vec, vec_name,
Eric Biggersed968042019-01-31 23:51:47 -08001746 &default_cipher_testvec_configs[i],
1747 req, tsgls);
1748 if (err)
1749 return err;
1750 }
1751
1752#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1753 if (!noextratests) {
1754 struct testvec_config cfg;
1755 char cfgname[TESTVEC_CONFIG_NAMELEN];
1756
1757 for (i = 0; i < fuzz_iterations; i++) {
1758 generate_random_testvec_config(&cfg, cfgname,
1759 sizeof(cfgname));
Eric Biggers951d1332019-04-11 21:57:37 -07001760 err = test_aead_vec_cfg(driver, enc, vec, vec_name,
Eric Biggersed968042019-01-31 23:51:47 -08001761 &cfg, req, tsgls);
1762 if (err)
1763 return err;
1764 }
1765 }
1766#endif
1767 return 0;
1768}
1769
1770static int test_aead(const char *driver, int enc,
1771 const struct aead_test_suite *suite,
1772 struct aead_request *req,
1773 struct cipher_test_sglists *tsgls)
1774{
1775 unsigned int i;
1776 int err;
1777
1778 for (i = 0; i < suite->count; i++) {
1779 err = test_aead_vec(driver, enc, &suite->vecs[i], i, req,
1780 tsgls);
1781 if (err)
1782 return err;
1783 }
1784 return 0;
1785}
1786
1787static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
1788 u32 type, u32 mask)
1789{
1790 const struct aead_test_suite *suite = &desc->suite.aead;
1791 struct crypto_aead *tfm;
1792 struct aead_request *req = NULL;
1793 struct cipher_test_sglists *tsgls = NULL;
1794 int err;
1795
1796 if (suite->count <= 0) {
1797 pr_err("alg: aead: empty test suite for %s\n", driver);
1798 return -EINVAL;
1799 }
1800
1801 tfm = crypto_alloc_aead(driver, type, mask);
1802 if (IS_ERR(tfm)) {
1803 pr_err("alg: aead: failed to allocate transform for %s: %ld\n",
1804 driver, PTR_ERR(tfm));
1805 return PTR_ERR(tfm);
1806 }
1807
1808 req = aead_request_alloc(tfm, GFP_KERNEL);
1809 if (!req) {
1810 pr_err("alg: aead: failed to allocate request for %s\n",
1811 driver);
1812 err = -ENOMEM;
1813 goto out;
1814 }
1815
1816 tsgls = alloc_cipher_test_sglists();
1817 if (!tsgls) {
1818 pr_err("alg: aead: failed to allocate test buffers for %s\n",
1819 driver);
1820 err = -ENOMEM;
1821 goto out;
1822 }
1823
1824 err = test_aead(driver, ENCRYPT, suite, req, tsgls);
1825 if (err)
1826 goto out;
1827
1828 err = test_aead(driver, DECRYPT, suite, req, tsgls);
1829out:
1830 free_cipher_test_sglists(tsgls);
1831 aead_request_free(req);
1832 crypto_free_aead(tfm);
1833 return err;
1834}
1835
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001836static int test_cipher(struct crypto_cipher *tfm, int enc,
Eric Biggersb13b1e02017-02-24 15:46:59 -08001837 const struct cipher_testvec *template,
1838 unsigned int tcount)
Herbert Xuda7f0332008-07-31 17:08:25 +08001839{
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001840 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
1841 unsigned int i, j, k;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001842 char *q;
1843 const char *e;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001844 const char *input, *result;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001845 void *data;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001846 char *xbuf[XBUFSIZE];
1847 int ret = -ENOMEM;
1848
1849 if (testmgr_alloc_buf(xbuf))
1850 goto out_nobuf;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001851
1852 if (enc == ENCRYPT)
1853 e = "encryption";
1854 else
1855 e = "decryption";
1856
1857 j = 0;
1858 for (i = 0; i < tcount; i++) {
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001859
Stephan Mueller10faa8c2016-08-25 15:15:01 +02001860 if (fips_enabled && template[i].fips_skip)
1861 continue;
1862
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001863 input = enc ? template[i].ptext : template[i].ctext;
1864 result = enc ? template[i].ctext : template[i].ptext;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001865 j++;
1866
Herbert Xufd57f222009-05-29 16:05:42 +10001867 ret = -EINVAL;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001868 if (WARN_ON(template[i].len > PAGE_SIZE))
Herbert Xufd57f222009-05-29 16:05:42 +10001869 goto out;
1870
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001871 data = xbuf[0];
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001872 memcpy(data, input, template[i].len);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001873
1874 crypto_cipher_clear_flags(tfm, ~0);
1875 if (template[i].wk)
Eric Biggers231baec2019-01-18 22:48:00 -08001876 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001877
1878 ret = crypto_cipher_setkey(tfm, template[i].key,
1879 template[i].klen);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001880 if (ret) {
1881 if (ret == template[i].setkey_error)
1882 continue;
1883 pr_err("alg: cipher: %s setkey failed on test vector %u; expected_error=%d, actual_error=%d, flags=%#x\n",
1884 algo, j, template[i].setkey_error, ret,
1885 crypto_cipher_get_flags(tfm));
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001886 goto out;
Eric Biggers5283a8e2019-04-11 21:57:36 -07001887 }
1888 if (template[i].setkey_error) {
1889 pr_err("alg: cipher: %s setkey unexpectedly succeeded on test vector %u; expected_error=%d\n",
1890 algo, j, template[i].setkey_error);
1891 ret = -EINVAL;
1892 goto out;
1893 }
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001894
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001895 for (k = 0; k < template[i].len;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001896 k += crypto_cipher_blocksize(tfm)) {
1897 if (enc)
1898 crypto_cipher_encrypt_one(tfm, data + k,
1899 data + k);
1900 else
1901 crypto_cipher_decrypt_one(tfm, data + k,
1902 data + k);
1903 }
1904
1905 q = data;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001906 if (memcmp(q, result, template[i].len)) {
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001907 printk(KERN_ERR "alg: cipher: Test %d failed "
1908 "on %s for %s\n", j, e, algo);
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001909 hexdump(q, template[i].len);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001910 ret = -EINVAL;
1911 goto out;
1912 }
1913 }
1914
1915 ret = 0;
1916
1917out:
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001918 testmgr_free_buf(xbuf);
1919out_nobuf:
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001920 return ret;
1921}
1922
Eric Biggers4e7babba2019-01-31 23:51:46 -08001923static int test_skcipher_vec_cfg(const char *driver, int enc,
1924 const struct cipher_testvec *vec,
Eric Biggers951d1332019-04-11 21:57:37 -07001925 const char *vec_name,
Eric Biggers4e7babba2019-01-31 23:51:46 -08001926 const struct testvec_config *cfg,
1927 struct skcipher_request *req,
1928 struct cipher_test_sglists *tsgls)
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001929{
Eric Biggers4e7babba2019-01-31 23:51:46 -08001930 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1931 const unsigned int alignmask = crypto_skcipher_alignmask(tfm);
1932 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
1933 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1934 const char *op = enc ? "encryption" : "decryption";
1935 DECLARE_CRYPTO_WAIT(wait);
1936 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
1937 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
1938 cfg->iv_offset +
1939 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
1940 struct kvec input;
1941 int err;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001942
Eric Biggers4e7babba2019-01-31 23:51:46 -08001943 /* Set the key */
1944 if (vec->wk)
1945 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001946 else
Eric Biggers4e7babba2019-01-31 23:51:46 -08001947 crypto_skcipher_clear_flags(tfm,
1948 CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1949 err = crypto_skcipher_setkey(tfm, vec->key, vec->klen);
1950 if (err) {
Eric Biggers5283a8e2019-04-11 21:57:36 -07001951 if (err == vec->setkey_error)
Eric Biggers4e7babba2019-01-31 23:51:46 -08001952 return 0;
Eric Biggers951d1332019-04-11 21:57:37 -07001953 pr_err("alg: skcipher: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1954 driver, vec_name, vec->setkey_error, err,
Eric Biggers5283a8e2019-04-11 21:57:36 -07001955 crypto_skcipher_get_flags(tfm));
Eric Biggers4e7babba2019-01-31 23:51:46 -08001956 return err;
1957 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07001958 if (vec->setkey_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001959 pr_err("alg: skcipher: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1960 driver, vec_name, vec->setkey_error);
Eric Biggers4e7babba2019-01-31 23:51:46 -08001961 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08001962 }
1963
Eric Biggers4e7babba2019-01-31 23:51:46 -08001964 /* The IV must be copied to a buffer, as the algorithm may modify it */
1965 if (ivsize) {
1966 if (WARN_ON(ivsize > MAX_IVLEN))
1967 return -EINVAL;
Eric Biggers8efd9722019-02-14 00:03:51 -08001968 if (vec->generates_iv && !enc)
1969 memcpy(iv, vec->iv_out, ivsize);
1970 else if (vec->iv)
Eric Biggers4e7babba2019-01-31 23:51:46 -08001971 memcpy(iv, vec->iv, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001972 else
Eric Biggers4e7babba2019-01-31 23:51:46 -08001973 memset(iv, 0, ivsize);
1974 } else {
1975 if (vec->generates_iv) {
Eric Biggers951d1332019-04-11 21:57:37 -07001976 pr_err("alg: skcipher: %s has ivsize=0 but test vector %s generates IV!\n",
1977 driver, vec_name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08001978 return -EINVAL;
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001979 }
Eric Biggers4e7babba2019-01-31 23:51:46 -08001980 iv = NULL;
Herbert Xuda7f0332008-07-31 17:08:25 +08001981 }
1982
Eric Biggers4e7babba2019-01-31 23:51:46 -08001983 /* Build the src/dst scatterlists */
1984 input.iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
1985 input.iov_len = vec->len;
1986 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
1987 vec->len, vec->len, &input, 1);
1988 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07001989 pr_err("alg: skcipher: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
1990 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08001991 return err;
Herbert Xuda7f0332008-07-31 17:08:25 +08001992 }
1993
Eric Biggers4e7babba2019-01-31 23:51:46 -08001994 /* Do the actual encryption or decryption */
1995 testmgr_poison(req->__ctx, crypto_skcipher_reqsize(tfm));
1996 skcipher_request_set_callback(req, req_flags, crypto_req_done, &wait);
1997 skcipher_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
1998 vec->len, iv);
Eric Biggers65707372019-03-12 22:12:52 -07001999 if (cfg->nosimd)
2000 crypto_disable_simd_for_test();
2001 err = enc ? crypto_skcipher_encrypt(req) : crypto_skcipher_decrypt(req);
2002 if (cfg->nosimd)
2003 crypto_reenable_simd_for_test();
2004 err = crypto_wait_req(err, &wait);
Herbert Xuda7f0332008-07-31 17:08:25 +08002005
Eric Biggersfa353c92019-01-31 23:51:49 -08002006 /* Check that the algorithm didn't overwrite things it shouldn't have */
2007 if (req->cryptlen != vec->len ||
2008 req->iv != iv ||
2009 req->src != tsgls->src.sgl_ptr ||
2010 req->dst != tsgls->dst.sgl_ptr ||
2011 crypto_skcipher_reqtfm(req) != tfm ||
2012 req->base.complete != crypto_req_done ||
2013 req->base.flags != req_flags ||
2014 req->base.data != &wait) {
Eric Biggers951d1332019-04-11 21:57:37 -07002015 pr_err("alg: skcipher: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
2016 driver, op, vec_name, cfg->name);
Eric Biggersfa353c92019-01-31 23:51:49 -08002017 if (req->cryptlen != vec->len)
2018 pr_err("alg: skcipher: changed 'req->cryptlen'\n");
2019 if (req->iv != iv)
2020 pr_err("alg: skcipher: changed 'req->iv'\n");
2021 if (req->src != tsgls->src.sgl_ptr)
2022 pr_err("alg: skcipher: changed 'req->src'\n");
2023 if (req->dst != tsgls->dst.sgl_ptr)
2024 pr_err("alg: skcipher: changed 'req->dst'\n");
2025 if (crypto_skcipher_reqtfm(req) != tfm)
2026 pr_err("alg: skcipher: changed 'req->base.tfm'\n");
2027 if (req->base.complete != crypto_req_done)
2028 pr_err("alg: skcipher: changed 'req->base.complete'\n");
2029 if (req->base.flags != req_flags)
2030 pr_err("alg: skcipher: changed 'req->base.flags'\n");
2031 if (req->base.data != &wait)
2032 pr_err("alg: skcipher: changed 'req->base.data'\n");
2033 return -EINVAL;
2034 }
2035 if (is_test_sglist_corrupted(&tsgls->src)) {
Eric Biggers951d1332019-04-11 21:57:37 -07002036 pr_err("alg: skcipher: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
2037 driver, op, vec_name, cfg->name);
Eric Biggersfa353c92019-01-31 23:51:49 -08002038 return -EINVAL;
2039 }
2040 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
2041 is_test_sglist_corrupted(&tsgls->dst)) {
Eric Biggers951d1332019-04-11 21:57:37 -07002042 pr_err("alg: skcipher: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
2043 driver, op, vec_name, cfg->name);
Eric Biggersfa353c92019-01-31 23:51:49 -08002044 return -EINVAL;
2045 }
2046
Eric Biggers5283a8e2019-04-11 21:57:36 -07002047 /* Check for success or failure */
2048 if (err) {
2049 if (err == vec->crypt_error)
2050 return 0;
Eric Biggers951d1332019-04-11 21:57:37 -07002051 pr_err("alg: skcipher: %s %s failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
2052 driver, op, vec_name, vec->crypt_error, err, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07002053 return err;
2054 }
2055 if (vec->crypt_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07002056 pr_err("alg: skcipher: %s %s unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
2057 driver, op, vec_name, vec->crypt_error, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07002058 return -EINVAL;
2059 }
2060
Eric Biggers4e7babba2019-01-31 23:51:46 -08002061 /* Check for the correct output (ciphertext or plaintext) */
2062 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
2063 vec->len, 0, true);
2064 if (err == -EOVERFLOW) {
Eric Biggers951d1332019-04-11 21:57:37 -07002065 pr_err("alg: skcipher: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
2066 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002067 return err;
2068 }
2069 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07002070 pr_err("alg: skcipher: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
2071 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002072 return err;
2073 }
Herbert Xuda7f0332008-07-31 17:08:25 +08002074
Eric Biggers4e7babba2019-01-31 23:51:46 -08002075 /* If applicable, check that the algorithm generated the correct IV */
Eric Biggers8efd9722019-02-14 00:03:51 -08002076 if (vec->iv_out && memcmp(iv, vec->iv_out, ivsize) != 0) {
Eric Biggers951d1332019-04-11 21:57:37 -07002077 pr_err("alg: skcipher: %s %s test failed (wrong output IV) on test vector %s, cfg=\"%s\"\n",
2078 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002079 hexdump(iv, ivsize);
2080 return -EINVAL;
Jussi Kivilinna3a338f22013-06-13 17:37:45 +03002081 }
2082
2083 return 0;
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03002084}
2085
Eric Biggers4e7babba2019-01-31 23:51:46 -08002086static int test_skcipher_vec(const char *driver, int enc,
2087 const struct cipher_testvec *vec,
2088 unsigned int vec_num,
2089 struct skcipher_request *req,
2090 struct cipher_test_sglists *tsgls)
2091{
Eric Biggers951d1332019-04-11 21:57:37 -07002092 char vec_name[16];
Eric Biggers4e7babba2019-01-31 23:51:46 -08002093 unsigned int i;
2094 int err;
2095
2096 if (fips_enabled && vec->fips_skip)
2097 return 0;
2098
Eric Biggers951d1332019-04-11 21:57:37 -07002099 sprintf(vec_name, "%u", vec_num);
2100
Eric Biggers4e7babba2019-01-31 23:51:46 -08002101 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
Eric Biggers951d1332019-04-11 21:57:37 -07002102 err = test_skcipher_vec_cfg(driver, enc, vec, vec_name,
Eric Biggers4e7babba2019-01-31 23:51:46 -08002103 &default_cipher_testvec_configs[i],
2104 req, tsgls);
2105 if (err)
2106 return err;
2107 }
2108
2109#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2110 if (!noextratests) {
2111 struct testvec_config cfg;
2112 char cfgname[TESTVEC_CONFIG_NAMELEN];
2113
2114 for (i = 0; i < fuzz_iterations; i++) {
2115 generate_random_testvec_config(&cfg, cfgname,
2116 sizeof(cfgname));
Eric Biggers951d1332019-04-11 21:57:37 -07002117 err = test_skcipher_vec_cfg(driver, enc, vec, vec_name,
Eric Biggers4e7babba2019-01-31 23:51:46 -08002118 &cfg, req, tsgls);
2119 if (err)
2120 return err;
2121 }
2122 }
2123#endif
2124 return 0;
2125}
2126
2127static int test_skcipher(const char *driver, int enc,
2128 const struct cipher_test_suite *suite,
2129 struct skcipher_request *req,
2130 struct cipher_test_sglists *tsgls)
2131{
2132 unsigned int i;
2133 int err;
2134
2135 for (i = 0; i < suite->count; i++) {
2136 err = test_skcipher_vec(driver, enc, &suite->vecs[i], i, req,
2137 tsgls);
2138 if (err)
2139 return err;
2140 }
2141 return 0;
2142}
2143
2144static int alg_test_skcipher(const struct alg_test_desc *desc,
2145 const char *driver, u32 type, u32 mask)
2146{
2147 const struct cipher_test_suite *suite = &desc->suite.cipher;
2148 struct crypto_skcipher *tfm;
2149 struct skcipher_request *req = NULL;
2150 struct cipher_test_sglists *tsgls = NULL;
2151 int err;
2152
2153 if (suite->count <= 0) {
2154 pr_err("alg: skcipher: empty test suite for %s\n", driver);
2155 return -EINVAL;
2156 }
2157
2158 tfm = crypto_alloc_skcipher(driver, type, mask);
2159 if (IS_ERR(tfm)) {
2160 pr_err("alg: skcipher: failed to allocate transform for %s: %ld\n",
2161 driver, PTR_ERR(tfm));
2162 return PTR_ERR(tfm);
2163 }
2164
2165 req = skcipher_request_alloc(tfm, GFP_KERNEL);
2166 if (!req) {
2167 pr_err("alg: skcipher: failed to allocate request for %s\n",
2168 driver);
2169 err = -ENOMEM;
2170 goto out;
2171 }
2172
2173 tsgls = alloc_cipher_test_sglists();
2174 if (!tsgls) {
2175 pr_err("alg: skcipher: failed to allocate test buffers for %s\n",
2176 driver);
2177 err = -ENOMEM;
2178 goto out;
2179 }
2180
2181 err = test_skcipher(driver, ENCRYPT, suite, req, tsgls);
2182 if (err)
2183 goto out;
2184
2185 err = test_skcipher(driver, DECRYPT, suite, req, tsgls);
2186out:
2187 free_cipher_test_sglists(tsgls);
2188 skcipher_request_free(req);
2189 crypto_free_skcipher(tfm);
2190 return err;
2191}
2192
Eric Biggersb13b1e02017-02-24 15:46:59 -08002193static int test_comp(struct crypto_comp *tfm,
2194 const struct comp_testvec *ctemplate,
2195 const struct comp_testvec *dtemplate,
2196 int ctcount, int dtcount)
Herbert Xuda7f0332008-07-31 17:08:25 +08002197{
2198 const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
Mahipal Challa33607382018-04-11 20:28:32 +02002199 char *output, *decomp_output;
Herbert Xuda7f0332008-07-31 17:08:25 +08002200 unsigned int i;
Herbert Xuda7f0332008-07-31 17:08:25 +08002201 int ret;
2202
Mahipal Challa33607382018-04-11 20:28:32 +02002203 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
2204 if (!output)
2205 return -ENOMEM;
2206
2207 decomp_output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
2208 if (!decomp_output) {
2209 kfree(output);
2210 return -ENOMEM;
2211 }
2212
Herbert Xuda7f0332008-07-31 17:08:25 +08002213 for (i = 0; i < ctcount; i++) {
Geert Uytterhoevenc79cf912009-03-29 15:44:19 +08002214 int ilen;
2215 unsigned int dlen = COMP_BUF_SIZE;
Herbert Xuda7f0332008-07-31 17:08:25 +08002216
Michael Schupikov22a81182018-10-07 13:58:10 +02002217 memset(output, 0, COMP_BUF_SIZE);
2218 memset(decomp_output, 0, COMP_BUF_SIZE);
Herbert Xuda7f0332008-07-31 17:08:25 +08002219
2220 ilen = ctemplate[i].inlen;
2221 ret = crypto_comp_compress(tfm, ctemplate[i].input,
Mahipal Challa33607382018-04-11 20:28:32 +02002222 ilen, output, &dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08002223 if (ret) {
2224 printk(KERN_ERR "alg: comp: compression failed "
2225 "on test %d for %s: ret=%d\n", i + 1, algo,
2226 -ret);
2227 goto out;
2228 }
2229
Mahipal Challa33607382018-04-11 20:28:32 +02002230 ilen = dlen;
2231 dlen = COMP_BUF_SIZE;
2232 ret = crypto_comp_decompress(tfm, output,
2233 ilen, decomp_output, &dlen);
2234 if (ret) {
2235 pr_err("alg: comp: compression failed: decompress: on test %d for %s failed: ret=%d\n",
2236 i + 1, algo, -ret);
2237 goto out;
2238 }
2239
2240 if (dlen != ctemplate[i].inlen) {
Geert Uytterhoevenb812eb02008-11-28 20:51:28 +08002241 printk(KERN_ERR "alg: comp: Compression test %d "
2242 "failed for %s: output len = %d\n", i + 1, algo,
2243 dlen);
2244 ret = -EINVAL;
2245 goto out;
2246 }
2247
Mahipal Challa33607382018-04-11 20:28:32 +02002248 if (memcmp(decomp_output, ctemplate[i].input,
2249 ctemplate[i].inlen)) {
2250 pr_err("alg: comp: compression failed: output differs: on test %d for %s\n",
2251 i + 1, algo);
2252 hexdump(decomp_output, dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08002253 ret = -EINVAL;
2254 goto out;
2255 }
2256 }
2257
2258 for (i = 0; i < dtcount; i++) {
Geert Uytterhoevenc79cf912009-03-29 15:44:19 +08002259 int ilen;
2260 unsigned int dlen = COMP_BUF_SIZE;
Herbert Xuda7f0332008-07-31 17:08:25 +08002261
Michael Schupikov22a81182018-10-07 13:58:10 +02002262 memset(decomp_output, 0, COMP_BUF_SIZE);
Herbert Xuda7f0332008-07-31 17:08:25 +08002263
2264 ilen = dtemplate[i].inlen;
2265 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
Mahipal Challa33607382018-04-11 20:28:32 +02002266 ilen, decomp_output, &dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08002267 if (ret) {
2268 printk(KERN_ERR "alg: comp: decompression failed "
2269 "on test %d for %s: ret=%d\n", i + 1, algo,
2270 -ret);
2271 goto out;
2272 }
2273
Geert Uytterhoevenb812eb02008-11-28 20:51:28 +08002274 if (dlen != dtemplate[i].outlen) {
2275 printk(KERN_ERR "alg: comp: Decompression test %d "
2276 "failed for %s: output len = %d\n", i + 1, algo,
2277 dlen);
2278 ret = -EINVAL;
2279 goto out;
2280 }
2281
Mahipal Challa33607382018-04-11 20:28:32 +02002282 if (memcmp(decomp_output, dtemplate[i].output, dlen)) {
Herbert Xuda7f0332008-07-31 17:08:25 +08002283 printk(KERN_ERR "alg: comp: Decompression test %d "
2284 "failed for %s\n", i + 1, algo);
Mahipal Challa33607382018-04-11 20:28:32 +02002285 hexdump(decomp_output, dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08002286 ret = -EINVAL;
2287 goto out;
2288 }
2289 }
2290
2291 ret = 0;
2292
2293out:
Mahipal Challa33607382018-04-11 20:28:32 +02002294 kfree(decomp_output);
2295 kfree(output);
Herbert Xuda7f0332008-07-31 17:08:25 +08002296 return ret;
2297}
2298
Eric Biggersb13b1e02017-02-24 15:46:59 -08002299static int test_acomp(struct crypto_acomp *tfm,
Mahipal Challa33607382018-04-11 20:28:32 +02002300 const struct comp_testvec *ctemplate,
Eric Biggersb13b1e02017-02-24 15:46:59 -08002301 const struct comp_testvec *dtemplate,
2302 int ctcount, int dtcount)
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002303{
2304 const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
2305 unsigned int i;
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002306 char *output, *decomp_out;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002307 int ret;
2308 struct scatterlist src, dst;
2309 struct acomp_req *req;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002310 struct crypto_wait wait;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002311
Eric Biggerseb095592016-11-23 10:24:35 -08002312 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
2313 if (!output)
2314 return -ENOMEM;
2315
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002316 decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
2317 if (!decomp_out) {
2318 kfree(output);
2319 return -ENOMEM;
2320 }
2321
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002322 for (i = 0; i < ctcount; i++) {
2323 unsigned int dlen = COMP_BUF_SIZE;
2324 int ilen = ctemplate[i].inlen;
Laura Abbott02608e02016-12-21 12:32:54 -08002325 void *input_vec;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002326
Eric Biggersd2110222016-12-30 14:12:00 -06002327 input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL);
Laura Abbott02608e02016-12-21 12:32:54 -08002328 if (!input_vec) {
2329 ret = -ENOMEM;
2330 goto out;
2331 }
2332
Eric Biggerseb095592016-11-23 10:24:35 -08002333 memset(output, 0, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002334 crypto_init_wait(&wait);
Laura Abbott02608e02016-12-21 12:32:54 -08002335 sg_init_one(&src, input_vec, ilen);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002336 sg_init_one(&dst, output, dlen);
2337
2338 req = acomp_request_alloc(tfm);
2339 if (!req) {
2340 pr_err("alg: acomp: request alloc failed for %s\n",
2341 algo);
Laura Abbott02608e02016-12-21 12:32:54 -08002342 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002343 ret = -ENOMEM;
2344 goto out;
2345 }
2346
2347 acomp_request_set_params(req, &src, &dst, ilen, dlen);
2348 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002349 crypto_req_done, &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002350
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002351 ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002352 if (ret) {
2353 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
2354 i + 1, algo, -ret);
Laura Abbott02608e02016-12-21 12:32:54 -08002355 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002356 acomp_request_free(req);
2357 goto out;
2358 }
2359
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002360 ilen = req->dlen;
2361 dlen = COMP_BUF_SIZE;
2362 sg_init_one(&src, output, ilen);
2363 sg_init_one(&dst, decomp_out, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002364 crypto_init_wait(&wait);
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002365 acomp_request_set_params(req, &src, &dst, ilen, dlen);
2366
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002367 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002368 if (ret) {
2369 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
2370 i + 1, algo, -ret);
2371 kfree(input_vec);
2372 acomp_request_free(req);
2373 goto out;
2374 }
2375
2376 if (req->dlen != ctemplate[i].inlen) {
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002377 pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
2378 i + 1, algo, req->dlen);
2379 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08002380 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002381 acomp_request_free(req);
2382 goto out;
2383 }
2384
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002385 if (memcmp(input_vec, decomp_out, req->dlen)) {
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002386 pr_err("alg: acomp: Compression test %d failed for %s\n",
2387 i + 1, algo);
2388 hexdump(output, req->dlen);
2389 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08002390 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002391 acomp_request_free(req);
2392 goto out;
2393 }
2394
Laura Abbott02608e02016-12-21 12:32:54 -08002395 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002396 acomp_request_free(req);
2397 }
2398
2399 for (i = 0; i < dtcount; i++) {
2400 unsigned int dlen = COMP_BUF_SIZE;
2401 int ilen = dtemplate[i].inlen;
Laura Abbott02608e02016-12-21 12:32:54 -08002402 void *input_vec;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002403
Eric Biggersd2110222016-12-30 14:12:00 -06002404 input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL);
Laura Abbott02608e02016-12-21 12:32:54 -08002405 if (!input_vec) {
2406 ret = -ENOMEM;
2407 goto out;
2408 }
2409
Eric Biggerseb095592016-11-23 10:24:35 -08002410 memset(output, 0, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002411 crypto_init_wait(&wait);
Laura Abbott02608e02016-12-21 12:32:54 -08002412 sg_init_one(&src, input_vec, ilen);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002413 sg_init_one(&dst, output, dlen);
2414
2415 req = acomp_request_alloc(tfm);
2416 if (!req) {
2417 pr_err("alg: acomp: request alloc failed for %s\n",
2418 algo);
Laura Abbott02608e02016-12-21 12:32:54 -08002419 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002420 ret = -ENOMEM;
2421 goto out;
2422 }
2423
2424 acomp_request_set_params(req, &src, &dst, ilen, dlen);
2425 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002426 crypto_req_done, &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002427
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002428 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002429 if (ret) {
2430 pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
2431 i + 1, algo, -ret);
Laura Abbott02608e02016-12-21 12:32:54 -08002432 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002433 acomp_request_free(req);
2434 goto out;
2435 }
2436
2437 if (req->dlen != dtemplate[i].outlen) {
2438 pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
2439 i + 1, algo, req->dlen);
2440 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08002441 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002442 acomp_request_free(req);
2443 goto out;
2444 }
2445
2446 if (memcmp(output, dtemplate[i].output, req->dlen)) {
2447 pr_err("alg: acomp: Decompression test %d failed for %s\n",
2448 i + 1, algo);
2449 hexdump(output, req->dlen);
2450 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08002451 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002452 acomp_request_free(req);
2453 goto out;
2454 }
2455
Laura Abbott02608e02016-12-21 12:32:54 -08002456 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002457 acomp_request_free(req);
2458 }
2459
2460 ret = 0;
2461
2462out:
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002463 kfree(decomp_out);
Eric Biggerseb095592016-11-23 10:24:35 -08002464 kfree(output);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002465 return ret;
2466}
2467
Eric Biggersb13b1e02017-02-24 15:46:59 -08002468static int test_cprng(struct crypto_rng *tfm,
2469 const struct cprng_testvec *template,
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002470 unsigned int tcount)
2471{
2472 const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
Felipe Contrerasfa4ef8a2009-10-27 19:04:42 +08002473 int err = 0, i, j, seedsize;
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002474 u8 *seed;
2475 char result[32];
2476
2477 seedsize = crypto_rng_seedsize(tfm);
2478
2479 seed = kmalloc(seedsize, GFP_KERNEL);
2480 if (!seed) {
2481 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
2482 "for %s\n", algo);
2483 return -ENOMEM;
2484 }
2485
2486 for (i = 0; i < tcount; i++) {
2487 memset(result, 0, 32);
2488
2489 memcpy(seed, template[i].v, template[i].vlen);
2490 memcpy(seed + template[i].vlen, template[i].key,
2491 template[i].klen);
2492 memcpy(seed + template[i].vlen + template[i].klen,
2493 template[i].dt, template[i].dtlen);
2494
2495 err = crypto_rng_reset(tfm, seed, seedsize);
2496 if (err) {
2497 printk(KERN_ERR "alg: cprng: Failed to reset rng "
2498 "for %s\n", algo);
2499 goto out;
2500 }
2501
2502 for (j = 0; j < template[i].loops; j++) {
2503 err = crypto_rng_get_bytes(tfm, result,
2504 template[i].rlen);
Stephan Mueller19e60e12015-03-10 17:00:36 +01002505 if (err < 0) {
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002506 printk(KERN_ERR "alg: cprng: Failed to obtain "
2507 "the correct amount of random data for "
Stephan Mueller19e60e12015-03-10 17:00:36 +01002508 "%s (requested %d)\n", algo,
2509 template[i].rlen);
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002510 goto out;
2511 }
2512 }
2513
2514 err = memcmp(result, template[i].result,
2515 template[i].rlen);
2516 if (err) {
2517 printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
2518 i, algo);
2519 hexdump(result, template[i].rlen);
2520 err = -EINVAL;
2521 goto out;
2522 }
2523 }
2524
2525out:
2526 kfree(seed);
2527 return err;
2528}
2529
Herbert Xuda7f0332008-07-31 17:08:25 +08002530static int alg_test_cipher(const struct alg_test_desc *desc,
2531 const char *driver, u32 type, u32 mask)
2532{
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002533 const struct cipher_test_suite *suite = &desc->suite.cipher;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002534 struct crypto_cipher *tfm;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002535 int err;
Herbert Xuda7f0332008-07-31 17:08:25 +08002536
Herbert Xueed93e02016-11-22 20:08:31 +08002537 tfm = crypto_alloc_cipher(driver, type, mask);
Herbert Xuda7f0332008-07-31 17:08:25 +08002538 if (IS_ERR(tfm)) {
2539 printk(KERN_ERR "alg: cipher: Failed to load transform for "
2540 "%s: %ld\n", driver, PTR_ERR(tfm));
2541 return PTR_ERR(tfm);
2542 }
2543
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002544 err = test_cipher(tfm, ENCRYPT, suite->vecs, suite->count);
2545 if (!err)
2546 err = test_cipher(tfm, DECRYPT, suite->vecs, suite->count);
Herbert Xuda7f0332008-07-31 17:08:25 +08002547
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002548 crypto_free_cipher(tfm);
2549 return err;
2550}
2551
Herbert Xuda7f0332008-07-31 17:08:25 +08002552static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
2553 u32 type, u32 mask)
2554{
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002555 struct crypto_comp *comp;
2556 struct crypto_acomp *acomp;
Herbert Xuda7f0332008-07-31 17:08:25 +08002557 int err;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002558 u32 algo_type = type & CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
Herbert Xuda7f0332008-07-31 17:08:25 +08002559
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002560 if (algo_type == CRYPTO_ALG_TYPE_ACOMPRESS) {
2561 acomp = crypto_alloc_acomp(driver, type, mask);
2562 if (IS_ERR(acomp)) {
2563 pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
2564 driver, PTR_ERR(acomp));
2565 return PTR_ERR(acomp);
2566 }
2567 err = test_acomp(acomp, desc->suite.comp.comp.vecs,
2568 desc->suite.comp.decomp.vecs,
2569 desc->suite.comp.comp.count,
2570 desc->suite.comp.decomp.count);
2571 crypto_free_acomp(acomp);
2572 } else {
2573 comp = crypto_alloc_comp(driver, type, mask);
2574 if (IS_ERR(comp)) {
2575 pr_err("alg: comp: Failed to load transform for %s: %ld\n",
2576 driver, PTR_ERR(comp));
2577 return PTR_ERR(comp);
2578 }
2579
2580 err = test_comp(comp, desc->suite.comp.comp.vecs,
2581 desc->suite.comp.decomp.vecs,
2582 desc->suite.comp.comp.count,
2583 desc->suite.comp.decomp.count);
2584
2585 crypto_free_comp(comp);
Herbert Xuda7f0332008-07-31 17:08:25 +08002586 }
Herbert Xuda7f0332008-07-31 17:08:25 +08002587 return err;
2588}
2589
Herbert Xu8e3ee852008-11-07 14:58:52 +08002590static int alg_test_crc32c(const struct alg_test_desc *desc,
2591 const char *driver, u32 type, u32 mask)
2592{
2593 struct crypto_shash *tfm;
Eric Biggerscb9dde82019-01-10 12:17:55 -08002594 __le32 val;
Herbert Xu8e3ee852008-11-07 14:58:52 +08002595 int err;
2596
2597 err = alg_test_hash(desc, driver, type, mask);
2598 if (err)
Eric Biggerseb5e6732019-01-23 20:57:35 -08002599 return err;
Herbert Xu8e3ee852008-11-07 14:58:52 +08002600
Herbert Xueed93e02016-11-22 20:08:31 +08002601 tfm = crypto_alloc_shash(driver, type, mask);
Herbert Xu8e3ee852008-11-07 14:58:52 +08002602 if (IS_ERR(tfm)) {
Eric Biggerseb5e6732019-01-23 20:57:35 -08002603 if (PTR_ERR(tfm) == -ENOENT) {
2604 /*
2605 * This crc32c implementation is only available through
2606 * ahash API, not the shash API, so the remaining part
2607 * of the test is not applicable to it.
2608 */
2609 return 0;
2610 }
Herbert Xu8e3ee852008-11-07 14:58:52 +08002611 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
2612 "%ld\n", driver, PTR_ERR(tfm));
Eric Biggerseb5e6732019-01-23 20:57:35 -08002613 return PTR_ERR(tfm);
Herbert Xu8e3ee852008-11-07 14:58:52 +08002614 }
2615
2616 do {
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02002617 SHASH_DESC_ON_STACK(shash, tfm);
2618 u32 *ctx = (u32 *)shash_desc_ctx(shash);
Herbert Xu8e3ee852008-11-07 14:58:52 +08002619
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02002620 shash->tfm = tfm;
2621 shash->flags = 0;
Herbert Xu8e3ee852008-11-07 14:58:52 +08002622
Eric Biggerscb9dde82019-01-10 12:17:55 -08002623 *ctx = 420553207;
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02002624 err = crypto_shash_final(shash, (u8 *)&val);
Herbert Xu8e3ee852008-11-07 14:58:52 +08002625 if (err) {
2626 printk(KERN_ERR "alg: crc32c: Operation failed for "
2627 "%s: %d\n", driver, err);
2628 break;
2629 }
2630
Eric Biggerscb9dde82019-01-10 12:17:55 -08002631 if (val != cpu_to_le32(~420553207)) {
2632 pr_err("alg: crc32c: Test failed for %s: %u\n",
2633 driver, le32_to_cpu(val));
Herbert Xu8e3ee852008-11-07 14:58:52 +08002634 err = -EINVAL;
2635 }
2636 } while (0);
2637
2638 crypto_free_shash(tfm);
2639
Herbert Xu8e3ee852008-11-07 14:58:52 +08002640 return err;
2641}
2642
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002643static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
2644 u32 type, u32 mask)
2645{
2646 struct crypto_rng *rng;
2647 int err;
2648
Herbert Xueed93e02016-11-22 20:08:31 +08002649 rng = crypto_alloc_rng(driver, type, mask);
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002650 if (IS_ERR(rng)) {
2651 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
2652 "%ld\n", driver, PTR_ERR(rng));
2653 return PTR_ERR(rng);
2654 }
2655
2656 err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
2657
2658 crypto_free_rng(rng);
2659
2660 return err;
2661}
2662
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002663
Eric Biggersb13b1e02017-02-24 15:46:59 -08002664static int drbg_cavs_test(const struct drbg_testvec *test, int pr,
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002665 const char *driver, u32 type, u32 mask)
2666{
2667 int ret = -EAGAIN;
2668 struct crypto_rng *drng;
2669 struct drbg_test_data test_data;
2670 struct drbg_string addtl, pers, testentropy;
2671 unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
2672
2673 if (!buf)
2674 return -ENOMEM;
2675
Herbert Xueed93e02016-11-22 20:08:31 +08002676 drng = crypto_alloc_rng(driver, type, mask);
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002677 if (IS_ERR(drng)) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04002678 printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002679 "%s\n", driver);
2680 kzfree(buf);
2681 return -ENOMEM;
2682 }
2683
2684 test_data.testentropy = &testentropy;
2685 drbg_string_fill(&testentropy, test->entropy, test->entropylen);
2686 drbg_string_fill(&pers, test->pers, test->perslen);
2687 ret = crypto_drbg_reset_test(drng, &pers, &test_data);
2688 if (ret) {
2689 printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
2690 goto outbuf;
2691 }
2692
2693 drbg_string_fill(&addtl, test->addtla, test->addtllen);
2694 if (pr) {
2695 drbg_string_fill(&testentropy, test->entpra, test->entprlen);
2696 ret = crypto_drbg_get_bytes_addtl_test(drng,
2697 buf, test->expectedlen, &addtl, &test_data);
2698 } else {
2699 ret = crypto_drbg_get_bytes_addtl(drng,
2700 buf, test->expectedlen, &addtl);
2701 }
Stephan Mueller19e60e12015-03-10 17:00:36 +01002702 if (ret < 0) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04002703 printk(KERN_ERR "alg: drbg: could not obtain random data for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002704 "driver %s\n", driver);
2705 goto outbuf;
2706 }
2707
2708 drbg_string_fill(&addtl, test->addtlb, test->addtllen);
2709 if (pr) {
2710 drbg_string_fill(&testentropy, test->entprb, test->entprlen);
2711 ret = crypto_drbg_get_bytes_addtl_test(drng,
2712 buf, test->expectedlen, &addtl, &test_data);
2713 } else {
2714 ret = crypto_drbg_get_bytes_addtl(drng,
2715 buf, test->expectedlen, &addtl);
2716 }
Stephan Mueller19e60e12015-03-10 17:00:36 +01002717 if (ret < 0) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04002718 printk(KERN_ERR "alg: drbg: could not obtain random data for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002719 "driver %s\n", driver);
2720 goto outbuf;
2721 }
2722
2723 ret = memcmp(test->expected, buf, test->expectedlen);
2724
2725outbuf:
2726 crypto_free_rng(drng);
2727 kzfree(buf);
2728 return ret;
2729}
2730
2731
2732static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
2733 u32 type, u32 mask)
2734{
2735 int err = 0;
2736 int pr = 0;
2737 int i = 0;
Eric Biggersb13b1e02017-02-24 15:46:59 -08002738 const struct drbg_testvec *template = desc->suite.drbg.vecs;
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002739 unsigned int tcount = desc->suite.drbg.count;
2740
2741 if (0 == memcmp(driver, "drbg_pr_", 8))
2742 pr = 1;
2743
2744 for (i = 0; i < tcount; i++) {
2745 err = drbg_cavs_test(&template[i], pr, driver, type, mask);
2746 if (err) {
2747 printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
2748 i, driver);
2749 err = -EINVAL;
2750 break;
2751 }
2752 }
2753 return err;
2754
2755}
2756
Eric Biggersb13b1e02017-02-24 15:46:59 -08002757static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002758 const char *alg)
2759{
2760 struct kpp_request *req;
2761 void *input_buf = NULL;
2762 void *output_buf = NULL;
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002763 void *a_public = NULL;
2764 void *a_ss = NULL;
2765 void *shared_secret = NULL;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002766 struct crypto_wait wait;
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002767 unsigned int out_len_max;
2768 int err = -ENOMEM;
2769 struct scatterlist src, dst;
2770
2771 req = kpp_request_alloc(tfm, GFP_KERNEL);
2772 if (!req)
2773 return err;
2774
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002775 crypto_init_wait(&wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002776
2777 err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
2778 if (err < 0)
2779 goto free_req;
2780
2781 out_len_max = crypto_kpp_maxsize(tfm);
2782 output_buf = kzalloc(out_len_max, GFP_KERNEL);
2783 if (!output_buf) {
2784 err = -ENOMEM;
2785 goto free_req;
2786 }
2787
2788 /* Use appropriate parameter as base */
2789 kpp_request_set_input(req, NULL, 0);
2790 sg_init_one(&dst, output_buf, out_len_max);
2791 kpp_request_set_output(req, &dst, out_len_max);
2792 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002793 crypto_req_done, &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002794
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002795 /* Compute party A's public key */
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002796 err = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002797 if (err) {
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002798 pr_err("alg: %s: Party A: generate public key test failed. err %d\n",
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002799 alg, err);
2800 goto free_output;
2801 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002802
2803 if (vec->genkey) {
2804 /* Save party A's public key */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05002805 a_public = kmemdup(sg_virt(req->dst), out_len_max, GFP_KERNEL);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002806 if (!a_public) {
2807 err = -ENOMEM;
2808 goto free_output;
2809 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002810 } else {
2811 /* Verify calculated public key */
2812 if (memcmp(vec->expected_a_public, sg_virt(req->dst),
2813 vec->expected_a_public_size)) {
2814 pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n",
2815 alg);
2816 err = -EINVAL;
2817 goto free_output;
2818 }
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002819 }
2820
2821 /* Calculate shared secret key by using counter part (b) public key. */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05002822 input_buf = kmemdup(vec->b_public, vec->b_public_size, GFP_KERNEL);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002823 if (!input_buf) {
2824 err = -ENOMEM;
2825 goto free_output;
2826 }
2827
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002828 sg_init_one(&src, input_buf, vec->b_public_size);
2829 sg_init_one(&dst, output_buf, out_len_max);
2830 kpp_request_set_input(req, &src, vec->b_public_size);
2831 kpp_request_set_output(req, &dst, out_len_max);
2832 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002833 crypto_req_done, &wait);
2834 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002835 if (err) {
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002836 pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n",
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002837 alg, err);
2838 goto free_all;
2839 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002840
2841 if (vec->genkey) {
2842 /* Save the shared secret obtained by party A */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05002843 a_ss = kmemdup(sg_virt(req->dst), vec->expected_ss_size, GFP_KERNEL);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002844 if (!a_ss) {
2845 err = -ENOMEM;
2846 goto free_all;
2847 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002848
2849 /*
2850 * Calculate party B's shared secret by using party A's
2851 * public key.
2852 */
2853 err = crypto_kpp_set_secret(tfm, vec->b_secret,
2854 vec->b_secret_size);
2855 if (err < 0)
2856 goto free_all;
2857
2858 sg_init_one(&src, a_public, vec->expected_a_public_size);
2859 sg_init_one(&dst, output_buf, out_len_max);
2860 kpp_request_set_input(req, &src, vec->expected_a_public_size);
2861 kpp_request_set_output(req, &dst, out_len_max);
2862 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002863 crypto_req_done, &wait);
2864 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req),
2865 &wait);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002866 if (err) {
2867 pr_err("alg: %s: Party B: compute shared secret failed. err %d\n",
2868 alg, err);
2869 goto free_all;
2870 }
2871
2872 shared_secret = a_ss;
2873 } else {
2874 shared_secret = (void *)vec->expected_ss;
2875 }
2876
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002877 /*
2878 * verify shared secret from which the user will derive
2879 * secret key by executing whatever hash it has chosen
2880 */
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002881 if (memcmp(shared_secret, sg_virt(req->dst),
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002882 vec->expected_ss_size)) {
2883 pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
2884 alg);
2885 err = -EINVAL;
2886 }
2887
2888free_all:
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002889 kfree(a_ss);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002890 kfree(input_buf);
2891free_output:
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002892 kfree(a_public);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002893 kfree(output_buf);
2894free_req:
2895 kpp_request_free(req);
2896 return err;
2897}
2898
2899static int test_kpp(struct crypto_kpp *tfm, const char *alg,
Eric Biggersb13b1e02017-02-24 15:46:59 -08002900 const struct kpp_testvec *vecs, unsigned int tcount)
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002901{
2902 int ret, i;
2903
2904 for (i = 0; i < tcount; i++) {
2905 ret = do_test_kpp(tfm, vecs++, alg);
2906 if (ret) {
2907 pr_err("alg: %s: test failed on vector %d, err=%d\n",
2908 alg, i + 1, ret);
2909 return ret;
2910 }
2911 }
2912 return 0;
2913}
2914
2915static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
2916 u32 type, u32 mask)
2917{
2918 struct crypto_kpp *tfm;
2919 int err = 0;
2920
Herbert Xueed93e02016-11-22 20:08:31 +08002921 tfm = crypto_alloc_kpp(driver, type, mask);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002922 if (IS_ERR(tfm)) {
2923 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
2924 driver, PTR_ERR(tfm));
2925 return PTR_ERR(tfm);
2926 }
2927 if (desc->suite.kpp.vecs)
2928 err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
2929 desc->suite.kpp.count);
2930
2931 crypto_free_kpp(tfm);
2932 return err;
2933}
2934
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03002935static u8 *test_pack_u32(u8 *dst, u32 val)
2936{
2937 memcpy(dst, &val, sizeof(val));
2938 return dst + sizeof(val);
2939}
2940
Herbert Xu50d2b6432016-06-29 19:32:20 +08002941static int test_akcipher_one(struct crypto_akcipher *tfm,
Eric Biggersb13b1e02017-02-24 15:46:59 -08002942 const struct akcipher_testvec *vecs)
Tadeusz Struk946cc462015-06-16 10:31:06 -07002943{
Herbert Xudf27b262016-05-05 16:42:49 +08002944 char *xbuf[XBUFSIZE];
Tadeusz Struk946cc462015-06-16 10:31:06 -07002945 struct akcipher_request *req;
2946 void *outbuf_enc = NULL;
2947 void *outbuf_dec = NULL;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002948 struct crypto_wait wait;
Tadeusz Struk946cc462015-06-16 10:31:06 -07002949 unsigned int out_len_max, out_len = 0;
2950 int err = -ENOMEM;
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03002951 struct scatterlist src, dst, src_tab[3];
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002952 const char *m, *c;
2953 unsigned int m_size, c_size;
2954 const char *op;
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03002955 u8 *key, *ptr;
Tadeusz Struk946cc462015-06-16 10:31:06 -07002956
Herbert Xudf27b262016-05-05 16:42:49 +08002957 if (testmgr_alloc_buf(xbuf))
2958 return err;
2959
Tadeusz Struk946cc462015-06-16 10:31:06 -07002960 req = akcipher_request_alloc(tfm, GFP_KERNEL);
2961 if (!req)
Herbert Xudf27b262016-05-05 16:42:49 +08002962 goto free_xbuf;
Tadeusz Struk946cc462015-06-16 10:31:06 -07002963
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002964 crypto_init_wait(&wait);
Tadeusz Struk22287b02015-10-08 09:26:55 -07002965
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03002966 key = kmalloc(vecs->key_len + sizeof(u32) * 2 + vecs->param_len,
2967 GFP_KERNEL);
2968 if (!key)
2969 goto free_xbuf;
2970 memcpy(key, vecs->key, vecs->key_len);
2971 ptr = key + vecs->key_len;
2972 ptr = test_pack_u32(ptr, vecs->algo);
2973 ptr = test_pack_u32(ptr, vecs->param_len);
2974 memcpy(ptr, vecs->params, vecs->param_len);
2975
Tadeusz Struk22287b02015-10-08 09:26:55 -07002976 if (vecs->public_key_vec)
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03002977 err = crypto_akcipher_set_pub_key(tfm, key, vecs->key_len);
Tadeusz Struk22287b02015-10-08 09:26:55 -07002978 else
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03002979 err = crypto_akcipher_set_priv_key(tfm, key, vecs->key_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002980 if (err)
2981 goto free_req;
2982
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002983 /*
2984 * First run test which do not require a private key, such as
2985 * encrypt or verify.
2986 */
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03002987 err = -ENOMEM;
2988 out_len_max = crypto_akcipher_maxsize(tfm);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002989 outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
2990 if (!outbuf_enc)
2991 goto free_req;
2992
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002993 if (!vecs->siggen_sigver_test) {
2994 m = vecs->m;
2995 m_size = vecs->m_size;
2996 c = vecs->c;
2997 c_size = vecs->c_size;
2998 op = "encrypt";
2999 } else {
3000 /* Swap args so we could keep plaintext (digest)
3001 * in vecs->m, and cooked signature in vecs->c.
3002 */
3003 m = vecs->c; /* signature */
3004 m_size = vecs->c_size;
3005 c = vecs->m; /* digest */
3006 c_size = vecs->m_size;
3007 op = "verify";
3008 }
Herbert Xudf27b262016-05-05 16:42:49 +08003009
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003010 if (WARN_ON(m_size > PAGE_SIZE))
3011 goto free_all;
3012 memcpy(xbuf[0], m, m_size);
Herbert Xudf27b262016-05-05 16:42:49 +08003013
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03003014 sg_init_table(src_tab, 3);
Herbert Xudf27b262016-05-05 16:42:49 +08003015 sg_set_buf(&src_tab[0], xbuf[0], 8);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003016 sg_set_buf(&src_tab[1], xbuf[0] + 8, m_size - 8);
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03003017 if (vecs->siggen_sigver_test) {
3018 if (WARN_ON(c_size > PAGE_SIZE))
3019 goto free_all;
3020 memcpy(xbuf[1], c, c_size);
3021 sg_set_buf(&src_tab[2], xbuf[1], c_size);
3022 akcipher_request_set_crypt(req, src_tab, NULL, m_size, c_size);
3023 } else {
3024 sg_init_one(&dst, outbuf_enc, out_len_max);
3025 akcipher_request_set_crypt(req, src_tab, &dst, m_size,
3026 out_len_max);
3027 }
Tadeusz Struk946cc462015-06-16 10:31:06 -07003028 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003029 crypto_req_done, &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003030
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003031 err = crypto_wait_req(vecs->siggen_sigver_test ?
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003032 /* Run asymmetric signature verification */
3033 crypto_akcipher_verify(req) :
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003034 /* Run asymmetric encrypt */
3035 crypto_akcipher_encrypt(req), &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003036 if (err) {
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003037 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003038 goto free_all;
3039 }
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03003040 if (!vecs->siggen_sigver_test) {
3041 if (req->dst_len != c_size) {
3042 pr_err("alg: akcipher: %s test failed. Invalid output len\n",
3043 op);
3044 err = -EINVAL;
3045 goto free_all;
3046 }
3047 /* verify that encrypted message is equal to expected */
3048 if (memcmp(c, outbuf_enc, c_size) != 0) {
3049 pr_err("alg: akcipher: %s test failed. Invalid output\n",
3050 op);
3051 hexdump(outbuf_enc, c_size);
3052 err = -EINVAL;
3053 goto free_all;
3054 }
Tadeusz Struk946cc462015-06-16 10:31:06 -07003055 }
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003056
3057 /*
3058 * Don't invoke (decrypt or sign) test which require a private key
3059 * for vectors with only a public key.
3060 */
Tadeusz Struk946cc462015-06-16 10:31:06 -07003061 if (vecs->public_key_vec) {
3062 err = 0;
3063 goto free_all;
3064 }
3065 outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
3066 if (!outbuf_dec) {
3067 err = -ENOMEM;
3068 goto free_all;
3069 }
Herbert Xudf27b262016-05-05 16:42:49 +08003070
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003071 op = vecs->siggen_sigver_test ? "sign" : "decrypt";
3072 if (WARN_ON(c_size > PAGE_SIZE))
Herbert Xudf27b262016-05-05 16:42:49 +08003073 goto free_all;
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003074 memcpy(xbuf[0], c, c_size);
Herbert Xudf27b262016-05-05 16:42:49 +08003075
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003076 sg_init_one(&src, xbuf[0], c_size);
Tadeusz Struk22287b02015-10-08 09:26:55 -07003077 sg_init_one(&dst, outbuf_dec, out_len_max);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003078 crypto_init_wait(&wait);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003079 akcipher_request_set_crypt(req, &src, &dst, c_size, out_len_max);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003080
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003081 err = crypto_wait_req(vecs->siggen_sigver_test ?
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003082 /* Run asymmetric signature generation */
3083 crypto_akcipher_sign(req) :
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003084 /* Run asymmetric decrypt */
3085 crypto_akcipher_decrypt(req), &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003086 if (err) {
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003087 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003088 goto free_all;
3089 }
3090 out_len = req->dst_len;
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003091 if (out_len < m_size) {
3092 pr_err("alg: akcipher: %s test failed. Invalid output len %u\n",
3093 op, out_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003094 err = -EINVAL;
3095 goto free_all;
3096 }
3097 /* verify that decrypted message is equal to the original msg */
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003098 if (memchr_inv(outbuf_dec, 0, out_len - m_size) ||
3099 memcmp(m, outbuf_dec + out_len - m_size, m_size)) {
3100 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
Herbert Xu50d2b6432016-06-29 19:32:20 +08003101 hexdump(outbuf_dec, out_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003102 err = -EINVAL;
3103 }
3104free_all:
3105 kfree(outbuf_dec);
3106 kfree(outbuf_enc);
3107free_req:
3108 akcipher_request_free(req);
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003109 kfree(key);
Herbert Xudf27b262016-05-05 16:42:49 +08003110free_xbuf:
3111 testmgr_free_buf(xbuf);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003112 return err;
3113}
3114
Herbert Xu50d2b6432016-06-29 19:32:20 +08003115static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
Eric Biggersb13b1e02017-02-24 15:46:59 -08003116 const struct akcipher_testvec *vecs,
3117 unsigned int tcount)
Tadeusz Struk946cc462015-06-16 10:31:06 -07003118{
Herbert Xu15226e42016-07-18 18:20:10 +08003119 const char *algo =
3120 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
Tadeusz Struk946cc462015-06-16 10:31:06 -07003121 int ret, i;
3122
3123 for (i = 0; i < tcount; i++) {
Herbert Xu50d2b6432016-06-29 19:32:20 +08003124 ret = test_akcipher_one(tfm, vecs++);
3125 if (!ret)
3126 continue;
3127
Herbert Xu15226e42016-07-18 18:20:10 +08003128 pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
3129 i + 1, algo, ret);
Herbert Xu50d2b6432016-06-29 19:32:20 +08003130 return ret;
Tadeusz Struk946cc462015-06-16 10:31:06 -07003131 }
3132 return 0;
3133}
3134
Tadeusz Struk946cc462015-06-16 10:31:06 -07003135static int alg_test_akcipher(const struct alg_test_desc *desc,
3136 const char *driver, u32 type, u32 mask)
3137{
3138 struct crypto_akcipher *tfm;
3139 int err = 0;
3140
Herbert Xueed93e02016-11-22 20:08:31 +08003141 tfm = crypto_alloc_akcipher(driver, type, mask);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003142 if (IS_ERR(tfm)) {
3143 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
3144 driver, PTR_ERR(tfm));
3145 return PTR_ERR(tfm);
3146 }
3147 if (desc->suite.akcipher.vecs)
3148 err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
3149 desc->suite.akcipher.count);
3150
3151 crypto_free_akcipher(tfm);
3152 return err;
3153}
3154
Youquan, Song863b5572009-12-23 19:45:20 +08003155static int alg_test_null(const struct alg_test_desc *desc,
3156 const char *driver, u32 type, u32 mask)
3157{
3158 return 0;
3159}
3160
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003161#define __VECS(tv) { .vecs = tv, .count = ARRAY_SIZE(tv) }
3162
Herbert Xuda7f0332008-07-31 17:08:25 +08003163/* Please keep this list sorted by algorithm name. */
3164static const struct alg_test_desc alg_test_descs[] = {
3165 {
Eric Biggers059c2a42018-11-16 17:26:31 -08003166 .alg = "adiantum(xchacha12,aes)",
3167 .test = alg_test_skcipher,
3168 .suite = {
3169 .cipher = __VECS(adiantum_xchacha12_aes_tv_template)
3170 },
3171 }, {
3172 .alg = "adiantum(xchacha20,aes)",
3173 .test = alg_test_skcipher,
3174 .suite = {
3175 .cipher = __VECS(adiantum_xchacha20_aes_tv_template)
3176 },
3177 }, {
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02003178 .alg = "aegis128",
3179 .test = alg_test_aead,
3180 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003181 .aead = __VECS(aegis128_tv_template)
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02003182 }
3183 }, {
3184 .alg = "aegis128l",
3185 .test = alg_test_aead,
3186 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003187 .aead = __VECS(aegis128l_tv_template)
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02003188 }
3189 }, {
3190 .alg = "aegis256",
3191 .test = alg_test_aead,
3192 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003193 .aead = __VECS(aegis256_tv_template)
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02003194 }
3195 }, {
Jarod Wilsone08ca2d2009-05-04 19:46:29 +08003196 .alg = "ansi_cprng",
3197 .test = alg_test_cprng,
3198 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003199 .cprng = __VECS(ansi_cprng_aes_tv_template)
Jarod Wilsone08ca2d2009-05-04 19:46:29 +08003200 }
3201 }, {
Horia Geantabca4feb2014-03-14 17:46:51 +02003202 .alg = "authenc(hmac(md5),ecb(cipher_null))",
3203 .test = alg_test_aead,
Horia Geantabca4feb2014-03-14 17:46:51 +02003204 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003205 .aead = __VECS(hmac_md5_ecb_cipher_null_tv_template)
Horia Geantabca4feb2014-03-14 17:46:51 +02003206 }
3207 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003208 .alg = "authenc(hmac(sha1),cbc(aes))",
Horia Geantae46e9a42012-07-03 19:16:54 +03003209 .test = alg_test_aead,
Herbert Xubcf741c2017-06-28 19:09:07 +08003210 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03003211 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003212 .aead = __VECS(hmac_sha1_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303213 }
3214 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003215 .alg = "authenc(hmac(sha1),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303216 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303217 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003218 .aead = __VECS(hmac_sha1_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303219 }
3220 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003221 .alg = "authenc(hmac(sha1),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303222 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01003223 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303224 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003225 .aead = __VECS(hmac_sha1_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03003226 }
3227 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01003228 .alg = "authenc(hmac(sha1),ctr(aes))",
3229 .test = alg_test_null,
3230 .fips_allowed = 1,
3231 }, {
Horia Geantabca4feb2014-03-14 17:46:51 +02003232 .alg = "authenc(hmac(sha1),ecb(cipher_null))",
3233 .test = alg_test_aead,
Horia Geantabca4feb2014-03-14 17:46:51 +02003234 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003235 .aead = __VECS(hmac_sha1_ecb_cipher_null_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303236 }
3237 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01003238 .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
3239 .test = alg_test_null,
3240 .fips_allowed = 1,
3241 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003242 .alg = "authenc(hmac(sha224),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303243 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303244 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003245 .aead = __VECS(hmac_sha224_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303246 }
3247 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003248 .alg = "authenc(hmac(sha224),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303249 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01003250 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303251 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003252 .aead = __VECS(hmac_sha224_des3_ede_cbc_tv_temp)
Horia Geantabca4feb2014-03-14 17:46:51 +02003253 }
3254 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003255 .alg = "authenc(hmac(sha256),cbc(aes))",
Horia Geantae46e9a42012-07-03 19:16:54 +03003256 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01003257 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03003258 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003259 .aead = __VECS(hmac_sha256_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303260 }
3261 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003262 .alg = "authenc(hmac(sha256),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303263 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303264 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003265 .aead = __VECS(hmac_sha256_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303266 }
3267 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003268 .alg = "authenc(hmac(sha256),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303269 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01003270 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303271 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003272 .aead = __VECS(hmac_sha256_des3_ede_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303273 }
3274 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01003275 .alg = "authenc(hmac(sha256),ctr(aes))",
3276 .test = alg_test_null,
3277 .fips_allowed = 1,
3278 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01003279 .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
3280 .test = alg_test_null,
3281 .fips_allowed = 1,
3282 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003283 .alg = "authenc(hmac(sha384),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303284 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303285 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003286 .aead = __VECS(hmac_sha384_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303287 }
3288 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003289 .alg = "authenc(hmac(sha384),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303290 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01003291 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303292 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003293 .aead = __VECS(hmac_sha384_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03003294 }
3295 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01003296 .alg = "authenc(hmac(sha384),ctr(aes))",
3297 .test = alg_test_null,
3298 .fips_allowed = 1,
3299 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01003300 .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
3301 .test = alg_test_null,
3302 .fips_allowed = 1,
3303 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003304 .alg = "authenc(hmac(sha512),cbc(aes))",
Marcus Meissnered1afac2016-02-05 14:23:33 +01003305 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03003306 .test = alg_test_aead,
Horia Geantae46e9a42012-07-03 19:16:54 +03003307 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003308 .aead = __VECS(hmac_sha512_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303309 }
3310 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003311 .alg = "authenc(hmac(sha512),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303312 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303313 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003314 .aead = __VECS(hmac_sha512_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303315 }
3316 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003317 .alg = "authenc(hmac(sha512),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303318 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01003319 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303320 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003321 .aead = __VECS(hmac_sha512_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03003322 }
3323 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01003324 .alg = "authenc(hmac(sha512),ctr(aes))",
3325 .test = alg_test_null,
3326 .fips_allowed = 1,
3327 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01003328 .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
3329 .test = alg_test_null,
3330 .fips_allowed = 1,
3331 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003332 .alg = "cbc(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003333 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003334 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003335 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003336 .cipher = __VECS(aes_cbc_tv_template)
3337 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003338 }, {
3339 .alg = "cbc(anubis)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003340 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003341 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003342 .cipher = __VECS(anubis_cbc_tv_template)
3343 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003344 }, {
3345 .alg = "cbc(blowfish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003346 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003347 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003348 .cipher = __VECS(bf_cbc_tv_template)
3349 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003350 }, {
3351 .alg = "cbc(camellia)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003352 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003353 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003354 .cipher = __VECS(camellia_cbc_tv_template)
3355 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003356 }, {
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02003357 .alg = "cbc(cast5)",
3358 .test = alg_test_skcipher,
3359 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003360 .cipher = __VECS(cast5_cbc_tv_template)
3361 },
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02003362 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003363 .alg = "cbc(cast6)",
3364 .test = alg_test_skcipher,
3365 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003366 .cipher = __VECS(cast6_cbc_tv_template)
3367 },
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003368 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003369 .alg = "cbc(des)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003370 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003371 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003372 .cipher = __VECS(des_cbc_tv_template)
3373 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003374 }, {
3375 .alg = "cbc(des3_ede)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003376 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003377 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003378 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003379 .cipher = __VECS(des3_ede_cbc_tv_template)
3380 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003381 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01003382 /* Same as cbc(aes) except the key is stored in
3383 * hardware secure memory which we reference by index
3384 */
3385 .alg = "cbc(paes)",
3386 .test = alg_test_null,
3387 .fips_allowed = 1,
3388 }, {
Jussi Kivilinna9d259172011-10-18 00:02:53 +03003389 .alg = "cbc(serpent)",
3390 .test = alg_test_skcipher,
3391 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003392 .cipher = __VECS(serpent_cbc_tv_template)
3393 },
Jussi Kivilinna9d259172011-10-18 00:02:53 +03003394 }, {
Gilad Ben-Yossef95ba5972018-09-20 14:18:38 +01003395 .alg = "cbc(sm4)",
3396 .test = alg_test_skcipher,
3397 .suite = {
3398 .cipher = __VECS(sm4_cbc_tv_template)
3399 }
3400 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003401 .alg = "cbc(twofish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003402 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003403 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003404 .cipher = __VECS(tf_cbc_tv_template)
3405 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003406 }, {
Ard Biesheuvel092acf02017-02-03 14:49:35 +00003407 .alg = "cbcmac(aes)",
3408 .fips_allowed = 1,
3409 .test = alg_test_hash,
3410 .suite = {
3411 .hash = __VECS(aes_cbcmac_tv_template)
3412 }
3413 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003414 .alg = "ccm(aes)",
3415 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003416 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003417 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003418 .aead = __VECS(aes_ccm_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003419 }
3420 }, {
Dmitry Eremin-Solenikov7da66672018-10-20 02:01:53 +03003421 .alg = "cfb(aes)",
3422 .test = alg_test_skcipher,
3423 .fips_allowed = 1,
3424 .suite = {
3425 .cipher = __VECS(aes_cfb_tv_template)
3426 },
3427 }, {
Martin Willi3590ebf2015-06-01 13:43:57 +02003428 .alg = "chacha20",
3429 .test = alg_test_skcipher,
3430 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003431 .cipher = __VECS(chacha20_tv_template)
3432 },
Martin Willi3590ebf2015-06-01 13:43:57 +02003433 }, {
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03003434 .alg = "cmac(aes)",
Stephan Mueller8f183752015-08-19 08:42:07 +02003435 .fips_allowed = 1,
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03003436 .test = alg_test_hash,
3437 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003438 .hash = __VECS(aes_cmac128_tv_template)
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03003439 }
3440 }, {
3441 .alg = "cmac(des3_ede)",
Stephan Mueller8f183752015-08-19 08:42:07 +02003442 .fips_allowed = 1,
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03003443 .test = alg_test_hash,
3444 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003445 .hash = __VECS(des3_ede_cmac64_tv_template)
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03003446 }
3447 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03003448 .alg = "compress_null",
3449 .test = alg_test_null,
3450 }, {
Ard Biesheuvelebb34722015-05-04 11:00:17 +02003451 .alg = "crc32",
3452 .test = alg_test_hash,
Milan Broza8a34412019-01-25 10:31:47 +01003453 .fips_allowed = 1,
Ard Biesheuvelebb34722015-05-04 11:00:17 +02003454 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003455 .hash = __VECS(crc32_tv_template)
Ard Biesheuvelebb34722015-05-04 11:00:17 +02003456 }
3457 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003458 .alg = "crc32c",
Herbert Xu8e3ee852008-11-07 14:58:52 +08003459 .test = alg_test_crc32c,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003460 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003461 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003462 .hash = __VECS(crc32c_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003463 }
3464 }, {
Herbert Xu684115212013-09-07 12:56:26 +10003465 .alg = "crct10dif",
3466 .test = alg_test_hash,
3467 .fips_allowed = 1,
3468 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003469 .hash = __VECS(crct10dif_tv_template)
Herbert Xu684115212013-09-07 12:56:26 +10003470 }
3471 }, {
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08003472 .alg = "ctr(aes)",
3473 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003474 .fips_allowed = 1,
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08003475 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003476 .cipher = __VECS(aes_ctr_tv_template)
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08003477 }
3478 }, {
Jussi Kivilinna85b63e32011-10-10 23:03:03 +03003479 .alg = "ctr(blowfish)",
3480 .test = alg_test_skcipher,
3481 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003482 .cipher = __VECS(bf_ctr_tv_template)
Jussi Kivilinna85b63e32011-10-10 23:03:03 +03003483 }
3484 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02003485 .alg = "ctr(camellia)",
3486 .test = alg_test_skcipher,
3487 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003488 .cipher = __VECS(camellia_ctr_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02003489 }
3490 }, {
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02003491 .alg = "ctr(cast5)",
3492 .test = alg_test_skcipher,
3493 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003494 .cipher = __VECS(cast5_ctr_tv_template)
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02003495 }
3496 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003497 .alg = "ctr(cast6)",
3498 .test = alg_test_skcipher,
3499 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003500 .cipher = __VECS(cast6_ctr_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003501 }
3502 }, {
Jussi Kivilinna8163fc32012-10-20 14:53:07 +03003503 .alg = "ctr(des)",
3504 .test = alg_test_skcipher,
3505 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003506 .cipher = __VECS(des_ctr_tv_template)
Jussi Kivilinna8163fc32012-10-20 14:53:07 +03003507 }
3508 }, {
Jussi Kivilinnae080b172012-10-20 14:53:12 +03003509 .alg = "ctr(des3_ede)",
3510 .test = alg_test_skcipher,
Marcelo Cerri0d8da102017-03-20 17:28:05 -03003511 .fips_allowed = 1,
Jussi Kivilinnae080b172012-10-20 14:53:12 +03003512 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003513 .cipher = __VECS(des3_ede_ctr_tv_template)
Jussi Kivilinnae080b172012-10-20 14:53:12 +03003514 }
3515 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01003516 /* Same as ctr(aes) except the key is stored in
3517 * hardware secure memory which we reference by index
3518 */
3519 .alg = "ctr(paes)",
3520 .test = alg_test_null,
3521 .fips_allowed = 1,
3522 }, {
Jussi Kivilinna9d259172011-10-18 00:02:53 +03003523 .alg = "ctr(serpent)",
3524 .test = alg_test_skcipher,
3525 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003526 .cipher = __VECS(serpent_ctr_tv_template)
Jussi Kivilinna9d259172011-10-18 00:02:53 +03003527 }
3528 }, {
Gilad Ben-Yossef95ba5972018-09-20 14:18:38 +01003529 .alg = "ctr(sm4)",
3530 .test = alg_test_skcipher,
3531 .suite = {
3532 .cipher = __VECS(sm4_ctr_tv_template)
3533 }
3534 }, {
Jussi Kivilinna573da622011-10-10 23:03:12 +03003535 .alg = "ctr(twofish)",
3536 .test = alg_test_skcipher,
3537 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003538 .cipher = __VECS(tf_ctr_tv_template)
Jussi Kivilinna573da622011-10-10 23:03:12 +03003539 }
3540 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003541 .alg = "cts(cbc(aes))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003542 .test = alg_test_skcipher,
Gilad Ben-Yossef196ad602018-11-04 10:05:24 +00003543 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003544 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003545 .cipher = __VECS(cts_mode_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003546 }
3547 }, {
3548 .alg = "deflate",
3549 .test = alg_test_comp,
Milan Broz08189042012-12-06 17:16:28 +08003550 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003551 .suite = {
3552 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003553 .comp = __VECS(deflate_comp_tv_template),
3554 .decomp = __VECS(deflate_decomp_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003555 }
3556 }
3557 }, {
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003558 .alg = "dh",
3559 .test = alg_test_kpp,
3560 .fips_allowed = 1,
3561 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003562 .kpp = __VECS(dh_tv_template)
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003563 }
3564 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03003565 .alg = "digest_null",
3566 .test = alg_test_null,
3567 }, {
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003568 .alg = "drbg_nopr_ctr_aes128",
3569 .test = alg_test_drbg,
3570 .fips_allowed = 1,
3571 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003572 .drbg = __VECS(drbg_nopr_ctr_aes128_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003573 }
3574 }, {
3575 .alg = "drbg_nopr_ctr_aes192",
3576 .test = alg_test_drbg,
3577 .fips_allowed = 1,
3578 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003579 .drbg = __VECS(drbg_nopr_ctr_aes192_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003580 }
3581 }, {
3582 .alg = "drbg_nopr_ctr_aes256",
3583 .test = alg_test_drbg,
3584 .fips_allowed = 1,
3585 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003586 .drbg = __VECS(drbg_nopr_ctr_aes256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003587 }
3588 }, {
3589 /*
3590 * There is no need to specifically test the DRBG with every
3591 * backend cipher -- covered by drbg_nopr_hmac_sha256 test
3592 */
3593 .alg = "drbg_nopr_hmac_sha1",
3594 .fips_allowed = 1,
3595 .test = alg_test_null,
3596 }, {
3597 .alg = "drbg_nopr_hmac_sha256",
3598 .test = alg_test_drbg,
3599 .fips_allowed = 1,
3600 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003601 .drbg = __VECS(drbg_nopr_hmac_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003602 }
3603 }, {
3604 /* covered by drbg_nopr_hmac_sha256 test */
3605 .alg = "drbg_nopr_hmac_sha384",
3606 .fips_allowed = 1,
3607 .test = alg_test_null,
3608 }, {
3609 .alg = "drbg_nopr_hmac_sha512",
3610 .test = alg_test_null,
3611 .fips_allowed = 1,
3612 }, {
3613 .alg = "drbg_nopr_sha1",
3614 .fips_allowed = 1,
3615 .test = alg_test_null,
3616 }, {
3617 .alg = "drbg_nopr_sha256",
3618 .test = alg_test_drbg,
3619 .fips_allowed = 1,
3620 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003621 .drbg = __VECS(drbg_nopr_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003622 }
3623 }, {
3624 /* covered by drbg_nopr_sha256 test */
3625 .alg = "drbg_nopr_sha384",
3626 .fips_allowed = 1,
3627 .test = alg_test_null,
3628 }, {
3629 .alg = "drbg_nopr_sha512",
3630 .fips_allowed = 1,
3631 .test = alg_test_null,
3632 }, {
3633 .alg = "drbg_pr_ctr_aes128",
3634 .test = alg_test_drbg,
3635 .fips_allowed = 1,
3636 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003637 .drbg = __VECS(drbg_pr_ctr_aes128_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003638 }
3639 }, {
3640 /* covered by drbg_pr_ctr_aes128 test */
3641 .alg = "drbg_pr_ctr_aes192",
3642 .fips_allowed = 1,
3643 .test = alg_test_null,
3644 }, {
3645 .alg = "drbg_pr_ctr_aes256",
3646 .fips_allowed = 1,
3647 .test = alg_test_null,
3648 }, {
3649 .alg = "drbg_pr_hmac_sha1",
3650 .fips_allowed = 1,
3651 .test = alg_test_null,
3652 }, {
3653 .alg = "drbg_pr_hmac_sha256",
3654 .test = alg_test_drbg,
3655 .fips_allowed = 1,
3656 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003657 .drbg = __VECS(drbg_pr_hmac_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003658 }
3659 }, {
3660 /* covered by drbg_pr_hmac_sha256 test */
3661 .alg = "drbg_pr_hmac_sha384",
3662 .fips_allowed = 1,
3663 .test = alg_test_null,
3664 }, {
3665 .alg = "drbg_pr_hmac_sha512",
3666 .test = alg_test_null,
3667 .fips_allowed = 1,
3668 }, {
3669 .alg = "drbg_pr_sha1",
3670 .fips_allowed = 1,
3671 .test = alg_test_null,
3672 }, {
3673 .alg = "drbg_pr_sha256",
3674 .test = alg_test_drbg,
3675 .fips_allowed = 1,
3676 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003677 .drbg = __VECS(drbg_pr_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003678 }
3679 }, {
3680 /* covered by drbg_pr_sha256 test */
3681 .alg = "drbg_pr_sha384",
3682 .fips_allowed = 1,
3683 .test = alg_test_null,
3684 }, {
3685 .alg = "drbg_pr_sha512",
3686 .fips_allowed = 1,
3687 .test = alg_test_null,
3688 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003689 .alg = "ecb(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003690 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003691 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003692 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003693 .cipher = __VECS(aes_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003694 }
3695 }, {
3696 .alg = "ecb(anubis)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003697 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003698 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003699 .cipher = __VECS(anubis_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003700 }
3701 }, {
3702 .alg = "ecb(arc4)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003703 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003704 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003705 .cipher = __VECS(arc4_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003706 }
3707 }, {
3708 .alg = "ecb(blowfish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003709 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003710 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003711 .cipher = __VECS(bf_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003712 }
3713 }, {
3714 .alg = "ecb(camellia)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003715 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003716 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003717 .cipher = __VECS(camellia_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003718 }
3719 }, {
3720 .alg = "ecb(cast5)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003721 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003722 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003723 .cipher = __VECS(cast5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003724 }
3725 }, {
3726 .alg = "ecb(cast6)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003727 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003728 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003729 .cipher = __VECS(cast6_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003730 }
3731 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03003732 .alg = "ecb(cipher_null)",
3733 .test = alg_test_null,
Milan Broz6175ca22017-04-21 13:03:06 +02003734 .fips_allowed = 1,
Jussi Kivilinnae4483702013-04-07 16:43:56 +03003735 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003736 .alg = "ecb(des)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003737 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003738 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003739 .cipher = __VECS(des_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003740 }
3741 }, {
3742 .alg = "ecb(des3_ede)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003743 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003744 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003745 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003746 .cipher = __VECS(des3_ede_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003747 }
3748 }, {
Jussi Kivilinna66e5bd02013-01-19 13:31:36 +02003749 .alg = "ecb(fcrypt)",
3750 .test = alg_test_skcipher,
3751 .suite = {
3752 .cipher = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003753 .vecs = fcrypt_pcbc_tv_template,
3754 .count = 1
Jussi Kivilinna66e5bd02013-01-19 13:31:36 +02003755 }
3756 }
3757 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003758 .alg = "ecb(khazad)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003759 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003760 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003761 .cipher = __VECS(khazad_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003762 }
3763 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01003764 /* Same as ecb(aes) except the key is stored in
3765 * hardware secure memory which we reference by index
3766 */
3767 .alg = "ecb(paes)",
3768 .test = alg_test_null,
3769 .fips_allowed = 1,
3770 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003771 .alg = "ecb(seed)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003772 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003773 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003774 .cipher = __VECS(seed_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003775 }
3776 }, {
3777 .alg = "ecb(serpent)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003778 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003779 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003780 .cipher = __VECS(serpent_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003781 }
3782 }, {
Gilad Ben-Yossefcd83a8a2018-03-06 09:44:43 +00003783 .alg = "ecb(sm4)",
3784 .test = alg_test_skcipher,
3785 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003786 .cipher = __VECS(sm4_tv_template)
Gilad Ben-Yossefcd83a8a2018-03-06 09:44:43 +00003787 }
3788 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003789 .alg = "ecb(tea)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003790 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003791 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003792 .cipher = __VECS(tea_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003793 }
3794 }, {
3795 .alg = "ecb(tnepres)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003796 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003797 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003798 .cipher = __VECS(tnepres_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003799 }
3800 }, {
3801 .alg = "ecb(twofish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003802 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003803 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003804 .cipher = __VECS(tf_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003805 }
3806 }, {
3807 .alg = "ecb(xeta)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003808 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003809 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003810 .cipher = __VECS(xeta_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003811 }
3812 }, {
3813 .alg = "ecb(xtea)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003814 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003815 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003816 .cipher = __VECS(xtea_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003817 }
3818 }, {
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +01003819 .alg = "ecdh",
3820 .test = alg_test_kpp,
3821 .fips_allowed = 1,
3822 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003823 .kpp = __VECS(ecdh_tv_template)
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +01003824 }
3825 }, {
Vitaly Chikunov32fbdbd2019-04-11 18:51:21 +03003826 .alg = "ecrdsa",
3827 .test = alg_test_akcipher,
3828 .suite = {
3829 .akcipher = __VECS(ecrdsa_tv_template)
3830 }
3831 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003832 .alg = "gcm(aes)",
3833 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003834 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003835 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003836 .aead = __VECS(aes_gcm_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003837 }
3838 }, {
Youquan, Song507069c2009-11-23 20:23:04 +08003839 .alg = "ghash",
3840 .test = alg_test_hash,
Jarod Wilson18c0ebd2011-01-29 15:14:35 +11003841 .fips_allowed = 1,
Youquan, Song507069c2009-11-23 20:23:04 +08003842 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003843 .hash = __VECS(ghash_tv_template)
Youquan, Song507069c2009-11-23 20:23:04 +08003844 }
3845 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003846 .alg = "hmac(md5)",
3847 .test = alg_test_hash,
3848 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003849 .hash = __VECS(hmac_md5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003850 }
3851 }, {
3852 .alg = "hmac(rmd128)",
3853 .test = alg_test_hash,
3854 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003855 .hash = __VECS(hmac_rmd128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003856 }
3857 }, {
3858 .alg = "hmac(rmd160)",
3859 .test = alg_test_hash,
3860 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003861 .hash = __VECS(hmac_rmd160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003862 }
3863 }, {
3864 .alg = "hmac(sha1)",
3865 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003866 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003867 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003868 .hash = __VECS(hmac_sha1_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003869 }
3870 }, {
3871 .alg = "hmac(sha224)",
3872 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003873 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003874 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003875 .hash = __VECS(hmac_sha224_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003876 }
3877 }, {
3878 .alg = "hmac(sha256)",
3879 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003880 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003881 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003882 .hash = __VECS(hmac_sha256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003883 }
3884 }, {
raveendra padasalagi98eca722016-07-01 11:16:54 +05303885 .alg = "hmac(sha3-224)",
3886 .test = alg_test_hash,
3887 .fips_allowed = 1,
3888 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003889 .hash = __VECS(hmac_sha3_224_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05303890 }
3891 }, {
3892 .alg = "hmac(sha3-256)",
3893 .test = alg_test_hash,
3894 .fips_allowed = 1,
3895 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003896 .hash = __VECS(hmac_sha3_256_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05303897 }
3898 }, {
3899 .alg = "hmac(sha3-384)",
3900 .test = alg_test_hash,
3901 .fips_allowed = 1,
3902 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003903 .hash = __VECS(hmac_sha3_384_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05303904 }
3905 }, {
3906 .alg = "hmac(sha3-512)",
3907 .test = alg_test_hash,
3908 .fips_allowed = 1,
3909 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003910 .hash = __VECS(hmac_sha3_512_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05303911 }
3912 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003913 .alg = "hmac(sha384)",
3914 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003915 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003916 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003917 .hash = __VECS(hmac_sha384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003918 }
3919 }, {
3920 .alg = "hmac(sha512)",
3921 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003922 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003923 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003924 .hash = __VECS(hmac_sha512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003925 }
3926 }, {
Vitaly Chikunov25a0b9d2018-11-07 00:00:03 +03003927 .alg = "hmac(streebog256)",
3928 .test = alg_test_hash,
3929 .suite = {
3930 .hash = __VECS(hmac_streebog256_tv_template)
3931 }
3932 }, {
3933 .alg = "hmac(streebog512)",
3934 .test = alg_test_hash,
3935 .suite = {
3936 .hash = __VECS(hmac_streebog512_tv_template)
3937 }
3938 }, {
Stephan Muellerbb5530e2015-05-25 15:10:20 +02003939 .alg = "jitterentropy_rng",
3940 .fips_allowed = 1,
3941 .test = alg_test_null,
3942 }, {
Stephan Mueller35351982015-09-21 20:59:56 +02003943 .alg = "kw(aes)",
3944 .test = alg_test_skcipher,
3945 .fips_allowed = 1,
3946 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003947 .cipher = __VECS(aes_kw_tv_template)
Stephan Mueller35351982015-09-21 20:59:56 +02003948 }
3949 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003950 .alg = "lrw(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003951 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003952 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003953 .cipher = __VECS(aes_lrw_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003954 }
3955 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02003956 .alg = "lrw(camellia)",
3957 .test = alg_test_skcipher,
3958 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003959 .cipher = __VECS(camellia_lrw_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02003960 }
3961 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003962 .alg = "lrw(cast6)",
3963 .test = alg_test_skcipher,
3964 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003965 .cipher = __VECS(cast6_lrw_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003966 }
3967 }, {
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03003968 .alg = "lrw(serpent)",
3969 .test = alg_test_skcipher,
3970 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003971 .cipher = __VECS(serpent_lrw_tv_template)
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03003972 }
3973 }, {
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03003974 .alg = "lrw(twofish)",
3975 .test = alg_test_skcipher,
3976 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003977 .cipher = __VECS(tf_lrw_tv_template)
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03003978 }
3979 }, {
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02003980 .alg = "lz4",
3981 .test = alg_test_comp,
3982 .fips_allowed = 1,
3983 .suite = {
3984 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003985 .comp = __VECS(lz4_comp_tv_template),
3986 .decomp = __VECS(lz4_decomp_tv_template)
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02003987 }
3988 }
3989 }, {
3990 .alg = "lz4hc",
3991 .test = alg_test_comp,
3992 .fips_allowed = 1,
3993 .suite = {
3994 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003995 .comp = __VECS(lz4hc_comp_tv_template),
3996 .decomp = __VECS(lz4hc_decomp_tv_template)
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02003997 }
3998 }
3999 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004000 .alg = "lzo",
4001 .test = alg_test_comp,
Milan Broz08189042012-12-06 17:16:28 +08004002 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004003 .suite = {
4004 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004005 .comp = __VECS(lzo_comp_tv_template),
4006 .decomp = __VECS(lzo_decomp_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004007 }
4008 }
4009 }, {
4010 .alg = "md4",
4011 .test = alg_test_hash,
4012 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004013 .hash = __VECS(md4_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004014 }
4015 }, {
4016 .alg = "md5",
4017 .test = alg_test_hash,
4018 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004019 .hash = __VECS(md5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004020 }
4021 }, {
4022 .alg = "michael_mic",
4023 .test = alg_test_hash,
4024 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004025 .hash = __VECS(michael_mic_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004026 }
4027 }, {
Ondrej Mosnacek4feb4c52018-05-11 14:19:10 +02004028 .alg = "morus1280",
4029 .test = alg_test_aead,
4030 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004031 .aead = __VECS(morus1280_tv_template)
Ondrej Mosnacek4feb4c52018-05-11 14:19:10 +02004032 }
4033 }, {
4034 .alg = "morus640",
4035 .test = alg_test_aead,
4036 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004037 .aead = __VECS(morus640_tv_template)
Ondrej Mosnacek4feb4c52018-05-11 14:19:10 +02004038 }
4039 }, {
Eric Biggers26609a22018-11-16 17:26:29 -08004040 .alg = "nhpoly1305",
4041 .test = alg_test_hash,
4042 .suite = {
4043 .hash = __VECS(nhpoly1305_tv_template)
4044 }
4045 }, {
Puneet Saxenaba0e14a2011-05-04 15:04:10 +10004046 .alg = "ofb(aes)",
4047 .test = alg_test_skcipher,
4048 .fips_allowed = 1,
4049 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004050 .cipher = __VECS(aes_ofb_tv_template)
Puneet Saxenaba0e14a2011-05-04 15:04:10 +10004051 }
4052 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01004053 /* Same as ofb(aes) except the key is stored in
4054 * hardware secure memory which we reference by index
4055 */
4056 .alg = "ofb(paes)",
4057 .test = alg_test_null,
4058 .fips_allowed = 1,
4059 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004060 .alg = "pcbc(fcrypt)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004061 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004062 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004063 .cipher = __VECS(fcrypt_pcbc_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004064 }
4065 }, {
Stephan Mueller12071072017-06-12 23:27:51 +02004066 .alg = "pkcs1pad(rsa,sha224)",
4067 .test = alg_test_null,
4068 .fips_allowed = 1,
4069 }, {
4070 .alg = "pkcs1pad(rsa,sha256)",
4071 .test = alg_test_akcipher,
4072 .fips_allowed = 1,
4073 .suite = {
4074 .akcipher = __VECS(pkcs1pad_rsa_tv_template)
4075 }
4076 }, {
4077 .alg = "pkcs1pad(rsa,sha384)",
4078 .test = alg_test_null,
4079 .fips_allowed = 1,
4080 }, {
4081 .alg = "pkcs1pad(rsa,sha512)",
4082 .test = alg_test_null,
4083 .fips_allowed = 1,
4084 }, {
Martin Willieee9dc62015-06-01 13:43:59 +02004085 .alg = "poly1305",
4086 .test = alg_test_hash,
4087 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004088 .hash = __VECS(poly1305_tv_template)
Martin Willieee9dc62015-06-01 13:43:59 +02004089 }
4090 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004091 .alg = "rfc3686(ctr(aes))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004092 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004093 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004094 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004095 .cipher = __VECS(aes_ctr_rfc3686_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004096 }
4097 }, {
Herbert Xu3f31a742015-07-09 07:17:34 +08004098 .alg = "rfc4106(gcm(aes))",
Adrian Hoban69435b92010-11-04 15:02:04 -04004099 .test = alg_test_aead,
Jarod Wilsondb71f29a2015-01-23 12:42:15 -05004100 .fips_allowed = 1,
Adrian Hoban69435b92010-11-04 15:02:04 -04004101 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004102 .aead = __VECS(aes_gcm_rfc4106_tv_template)
Adrian Hoban69435b92010-11-04 15:02:04 -04004103 }
4104 }, {
Herbert Xu544c4362015-07-14 16:53:22 +08004105 .alg = "rfc4309(ccm(aes))",
Jarod Wilson5d667322009-05-04 19:23:40 +08004106 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004107 .fips_allowed = 1,
Jarod Wilson5d667322009-05-04 19:23:40 +08004108 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004109 .aead = __VECS(aes_ccm_rfc4309_tv_template)
Jarod Wilson5d667322009-05-04 19:23:40 +08004110 }
4111 }, {
Herbert Xubb687452015-06-16 13:54:24 +08004112 .alg = "rfc4543(gcm(aes))",
Jussi Kivilinnae9b74412013-04-07 16:43:51 +03004113 .test = alg_test_aead,
4114 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004115 .aead = __VECS(aes_gcm_rfc4543_tv_template)
Jussi Kivilinnae9b74412013-04-07 16:43:51 +03004116 }
4117 }, {
Martin Williaf2b76b2015-06-01 13:44:01 +02004118 .alg = "rfc7539(chacha20,poly1305)",
4119 .test = alg_test_aead,
4120 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004121 .aead = __VECS(rfc7539_tv_template)
Martin Williaf2b76b2015-06-01 13:44:01 +02004122 }
4123 }, {
Martin Willi59007582015-06-01 13:44:03 +02004124 .alg = "rfc7539esp(chacha20,poly1305)",
4125 .test = alg_test_aead,
4126 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004127 .aead = __VECS(rfc7539esp_tv_template)
Martin Willi59007582015-06-01 13:44:03 +02004128 }
4129 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004130 .alg = "rmd128",
4131 .test = alg_test_hash,
4132 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004133 .hash = __VECS(rmd128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004134 }
4135 }, {
4136 .alg = "rmd160",
4137 .test = alg_test_hash,
4138 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004139 .hash = __VECS(rmd160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004140 }
4141 }, {
4142 .alg = "rmd256",
4143 .test = alg_test_hash,
4144 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004145 .hash = __VECS(rmd256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004146 }
4147 }, {
4148 .alg = "rmd320",
4149 .test = alg_test_hash,
4150 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004151 .hash = __VECS(rmd320_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004152 }
4153 }, {
Tadeusz Struk946cc462015-06-16 10:31:06 -07004154 .alg = "rsa",
4155 .test = alg_test_akcipher,
4156 .fips_allowed = 1,
4157 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004158 .akcipher = __VECS(rsa_tv_template)
Tadeusz Struk946cc462015-06-16 10:31:06 -07004159 }
4160 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004161 .alg = "salsa20",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004162 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004163 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004164 .cipher = __VECS(salsa20_stream_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004165 }
4166 }, {
4167 .alg = "sha1",
4168 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004169 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004170 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004171 .hash = __VECS(sha1_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004172 }
4173 }, {
4174 .alg = "sha224",
4175 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004176 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004177 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004178 .hash = __VECS(sha224_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004179 }
4180 }, {
4181 .alg = "sha256",
4182 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004183 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004184 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004185 .hash = __VECS(sha256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004186 }
4187 }, {
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05304188 .alg = "sha3-224",
4189 .test = alg_test_hash,
4190 .fips_allowed = 1,
4191 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004192 .hash = __VECS(sha3_224_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05304193 }
4194 }, {
4195 .alg = "sha3-256",
4196 .test = alg_test_hash,
4197 .fips_allowed = 1,
4198 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004199 .hash = __VECS(sha3_256_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05304200 }
4201 }, {
4202 .alg = "sha3-384",
4203 .test = alg_test_hash,
4204 .fips_allowed = 1,
4205 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004206 .hash = __VECS(sha3_384_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05304207 }
4208 }, {
4209 .alg = "sha3-512",
4210 .test = alg_test_hash,
4211 .fips_allowed = 1,
4212 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004213 .hash = __VECS(sha3_512_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05304214 }
4215 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004216 .alg = "sha384",
4217 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004218 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004219 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004220 .hash = __VECS(sha384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004221 }
4222 }, {
4223 .alg = "sha512",
4224 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004225 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004226 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004227 .hash = __VECS(sha512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004228 }
4229 }, {
Gilad Ben-Yossefb7e27532017-08-21 13:51:29 +03004230 .alg = "sm3",
4231 .test = alg_test_hash,
4232 .suite = {
4233 .hash = __VECS(sm3_tv_template)
4234 }
4235 }, {
Vitaly Chikunov25a0b9d2018-11-07 00:00:03 +03004236 .alg = "streebog256",
4237 .test = alg_test_hash,
4238 .suite = {
4239 .hash = __VECS(streebog256_tv_template)
4240 }
4241 }, {
4242 .alg = "streebog512",
4243 .test = alg_test_hash,
4244 .suite = {
4245 .hash = __VECS(streebog512_tv_template)
4246 }
4247 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004248 .alg = "tgr128",
4249 .test = alg_test_hash,
4250 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004251 .hash = __VECS(tgr128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004252 }
4253 }, {
4254 .alg = "tgr160",
4255 .test = alg_test_hash,
4256 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004257 .hash = __VECS(tgr160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004258 }
4259 }, {
4260 .alg = "tgr192",
4261 .test = alg_test_hash,
4262 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004263 .hash = __VECS(tgr192_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004264 }
4265 }, {
Eric Biggersed331ad2018-06-18 10:22:39 -07004266 .alg = "vmac64(aes)",
4267 .test = alg_test_hash,
4268 .suite = {
4269 .hash = __VECS(vmac64_aes_tv_template)
4270 }
4271 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004272 .alg = "wp256",
4273 .test = alg_test_hash,
4274 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004275 .hash = __VECS(wp256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004276 }
4277 }, {
4278 .alg = "wp384",
4279 .test = alg_test_hash,
4280 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004281 .hash = __VECS(wp384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004282 }
4283 }, {
4284 .alg = "wp512",
4285 .test = alg_test_hash,
4286 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004287 .hash = __VECS(wp512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004288 }
4289 }, {
4290 .alg = "xcbc(aes)",
4291 .test = alg_test_hash,
4292 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004293 .hash = __VECS(aes_xcbc128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004294 }
4295 }, {
Eric Biggersaa762402018-11-16 17:26:22 -08004296 .alg = "xchacha12",
4297 .test = alg_test_skcipher,
4298 .suite = {
4299 .cipher = __VECS(xchacha12_tv_template)
4300 },
4301 }, {
Eric Biggersde61d7a2018-11-16 17:26:20 -08004302 .alg = "xchacha20",
4303 .test = alg_test_skcipher,
4304 .suite = {
4305 .cipher = __VECS(xchacha20_tv_template)
4306 },
4307 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004308 .alg = "xts(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004309 .test = alg_test_skcipher,
Jarod Wilson2918aa82011-01-29 15:14:01 +11004310 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004311 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004312 .cipher = __VECS(aes_xts_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004313 }
Geert Uytterhoeven0c01aed2009-03-04 15:42:15 +08004314 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02004315 .alg = "xts(camellia)",
4316 .test = alg_test_skcipher,
4317 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004318 .cipher = __VECS(camellia_xts_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02004319 }
4320 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004321 .alg = "xts(cast6)",
4322 .test = alg_test_skcipher,
4323 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004324 .cipher = __VECS(cast6_xts_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004325 }
4326 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01004327 /* Same as xts(aes) except the key is stored in
4328 * hardware secure memory which we reference by index
4329 */
4330 .alg = "xts(paes)",
4331 .test = alg_test_null,
4332 .fips_allowed = 1,
4333 }, {
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03004334 .alg = "xts(serpent)",
4335 .test = alg_test_skcipher,
4336 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004337 .cipher = __VECS(serpent_xts_tv_template)
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03004338 }
4339 }, {
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03004340 .alg = "xts(twofish)",
4341 .test = alg_test_skcipher,
4342 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004343 .cipher = __VECS(tf_xts_tv_template)
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03004344 }
Giovanni Cabiddua368f432017-04-21 21:54:30 +01004345 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01004346 .alg = "xts4096(paes)",
4347 .test = alg_test_null,
4348 .fips_allowed = 1,
4349 }, {
4350 .alg = "xts512(paes)",
4351 .test = alg_test_null,
4352 .fips_allowed = 1,
4353 }, {
Giovanni Cabiddua368f432017-04-21 21:54:30 +01004354 .alg = "zlib-deflate",
4355 .test = alg_test_comp,
4356 .fips_allowed = 1,
4357 .suite = {
4358 .comp = {
4359 .comp = __VECS(zlib_deflate_comp_tv_template),
4360 .decomp = __VECS(zlib_deflate_decomp_tv_template)
4361 }
4362 }
Nick Terrelld28fc3d2018-03-30 12:14:53 -07004363 }, {
4364 .alg = "zstd",
4365 .test = alg_test_comp,
4366 .fips_allowed = 1,
4367 .suite = {
4368 .comp = {
4369 .comp = __VECS(zstd_comp_tv_template),
4370 .decomp = __VECS(zstd_decomp_tv_template)
4371 }
4372 }
Herbert Xuda7f0332008-07-31 17:08:25 +08004373 }
4374};
4375
Eric Biggers3f47a032019-01-31 23:51:43 -08004376static void alg_check_test_descs_order(void)
Jussi Kivilinna57147582013-06-13 17:37:40 +03004377{
4378 int i;
4379
Jussi Kivilinna57147582013-06-13 17:37:40 +03004380 for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
4381 int diff = strcmp(alg_test_descs[i - 1].alg,
4382 alg_test_descs[i].alg);
4383
4384 if (WARN_ON(diff > 0)) {
4385 pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
4386 alg_test_descs[i - 1].alg,
4387 alg_test_descs[i].alg);
4388 }
4389
4390 if (WARN_ON(diff == 0)) {
4391 pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
4392 alg_test_descs[i].alg);
4393 }
4394 }
4395}
4396
Eric Biggers3f47a032019-01-31 23:51:43 -08004397static void alg_check_testvec_configs(void)
4398{
Eric Biggers4e7babba2019-01-31 23:51:46 -08004399 int i;
4400
4401 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++)
4402 WARN_ON(!valid_testvec_config(
4403 &default_cipher_testvec_configs[i]));
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08004404
4405 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++)
4406 WARN_ON(!valid_testvec_config(
4407 &default_hash_testvec_configs[i]));
Eric Biggers3f47a032019-01-31 23:51:43 -08004408}
4409
4410static void testmgr_onetime_init(void)
4411{
4412 alg_check_test_descs_order();
4413 alg_check_testvec_configs();
Eric Biggers5b2706a2019-01-31 23:51:44 -08004414
4415#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
4416 pr_warn("alg: extra crypto tests enabled. This is intended for developer use only.\n");
4417#endif
Eric Biggers3f47a032019-01-31 23:51:43 -08004418}
4419
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004420static int alg_find_test(const char *alg)
Herbert Xuda7f0332008-07-31 17:08:25 +08004421{
4422 int start = 0;
4423 int end = ARRAY_SIZE(alg_test_descs);
4424
4425 while (start < end) {
4426 int i = (start + end) / 2;
4427 int diff = strcmp(alg_test_descs[i].alg, alg);
4428
4429 if (diff > 0) {
4430 end = i;
4431 continue;
4432 }
4433
4434 if (diff < 0) {
4435 start = i + 1;
4436 continue;
4437 }
4438
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004439 return i;
Herbert Xuda7f0332008-07-31 17:08:25 +08004440 }
4441
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004442 return -1;
4443}
4444
4445int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
4446{
4447 int i;
Herbert Xua68f6612009-07-02 16:32:12 +08004448 int j;
Neil Hormand12d6b62008-10-12 20:36:51 +08004449 int rc;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004450
Richard W.M. Jones9e5c9fe2016-05-03 10:00:17 +01004451 if (!fips_enabled && notests) {
4452 printk_once(KERN_INFO "alg: self-tests disabled\n");
4453 return 0;
4454 }
4455
Eric Biggers3f47a032019-01-31 23:51:43 -08004456 DO_ONCE(testmgr_onetime_init);
Jussi Kivilinna57147582013-06-13 17:37:40 +03004457
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004458 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
4459 char nalg[CRYPTO_MAX_ALG_NAME];
4460
4461 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
4462 sizeof(nalg))
4463 return -ENAMETOOLONG;
4464
4465 i = alg_find_test(nalg);
4466 if (i < 0)
4467 goto notest;
4468
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10004469 if (fips_enabled && !alg_test_descs[i].fips_allowed)
4470 goto non_fips_alg;
4471
Jarod Wilson941fb322009-05-04 19:49:23 +08004472 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
4473 goto test_done;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004474 }
4475
4476 i = alg_find_test(alg);
Herbert Xua68f6612009-07-02 16:32:12 +08004477 j = alg_find_test(driver);
4478 if (i < 0 && j < 0)
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004479 goto notest;
4480
Herbert Xua68f6612009-07-02 16:32:12 +08004481 if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
4482 (j >= 0 && !alg_test_descs[j].fips_allowed)))
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10004483 goto non_fips_alg;
4484
Herbert Xua68f6612009-07-02 16:32:12 +08004485 rc = 0;
4486 if (i >= 0)
4487 rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
4488 type, mask);
Cristian Stoica032c8ca2013-07-18 18:57:07 +03004489 if (j >= 0 && j != i)
Herbert Xua68f6612009-07-02 16:32:12 +08004490 rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
4491 type, mask);
4492
Jarod Wilson941fb322009-05-04 19:49:23 +08004493test_done:
Eric Biggerseda69b02019-03-31 13:09:14 -07004494 if (rc && (fips_enabled || panic_on_fail))
4495 panic("alg: self-tests for %s (%s) failed in %s mode!\n",
4496 driver, alg, fips_enabled ? "fips" : "panic_on_fail");
Neil Hormand12d6b62008-10-12 20:36:51 +08004497
Jarod Wilson29ecd4a2009-05-04 19:51:17 +08004498 if (fips_enabled && !rc)
Masanari Iida3e8cffd2014-10-07 00:37:54 +09004499 pr_info("alg: self-tests for %s (%s) passed\n", driver, alg);
Jarod Wilson29ecd4a2009-05-04 19:51:17 +08004500
Neil Hormand12d6b62008-10-12 20:36:51 +08004501 return rc;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004502
4503notest:
Herbert Xuda7f0332008-07-31 17:08:25 +08004504 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
4505 return 0;
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10004506non_fips_alg:
4507 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08004508}
Alexander Shishkin0b767f92010-06-03 20:53:43 +10004509
Herbert Xu326a6342010-08-06 09:40:28 +08004510#endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
Alexander Shishkin0b767f92010-06-03 20:53:43 +10004511
Herbert Xuda7f0332008-07-31 17:08:25 +08004512EXPORT_SYMBOL_GPL(alg_test);