blob: feb3ff27e0b319979cf031cbb88e030d7c22c981 [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
1293static int __alg_test_hash(const struct hash_testvec *vecs,
1294 unsigned int num_vecs, const char *driver,
1295 u32 type, u32 mask)
1296{
1297 struct crypto_ahash *tfm;
1298 struct ahash_request *req = NULL;
1299 struct test_sglist *tsgl = NULL;
1300 u8 *hashstate = NULL;
1301 unsigned int i;
1302 int err;
1303
1304 tfm = crypto_alloc_ahash(driver, type, mask);
1305 if (IS_ERR(tfm)) {
1306 pr_err("alg: hash: failed to allocate transform for %s: %ld\n",
1307 driver, PTR_ERR(tfm));
1308 return PTR_ERR(tfm);
1309 }
1310
1311 req = ahash_request_alloc(tfm, GFP_KERNEL);
1312 if (!req) {
1313 pr_err("alg: hash: failed to allocate request for %s\n",
1314 driver);
1315 err = -ENOMEM;
1316 goto out;
1317 }
1318
1319 tsgl = kmalloc(sizeof(*tsgl), GFP_KERNEL);
1320 if (!tsgl || init_test_sglist(tsgl) != 0) {
1321 pr_err("alg: hash: failed to allocate test buffers for %s\n",
1322 driver);
1323 kfree(tsgl);
1324 tsgl = NULL;
1325 err = -ENOMEM;
1326 goto out;
1327 }
1328
1329 hashstate = kmalloc(crypto_ahash_statesize(tfm) + TESTMGR_POISON_LEN,
1330 GFP_KERNEL);
1331 if (!hashstate) {
1332 pr_err("alg: hash: failed to allocate hash state buffer for %s\n",
1333 driver);
1334 err = -ENOMEM;
1335 goto out;
1336 }
1337
1338 for (i = 0; i < num_vecs; i++) {
1339 err = test_hash_vec(driver, &vecs[i], i, req, tsgl, hashstate);
1340 if (err)
1341 goto out;
1342 }
1343 err = 0;
1344out:
1345 kfree(hashstate);
1346 if (tsgl) {
1347 destroy_test_sglist(tsgl);
1348 kfree(tsgl);
1349 }
1350 ahash_request_free(req);
1351 crypto_free_ahash(tfm);
1352 return err;
1353}
1354
1355static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1356 u32 type, u32 mask)
1357{
1358 const struct hash_testvec *template = desc->suite.hash.vecs;
1359 unsigned int tcount = desc->suite.hash.count;
1360 unsigned int nr_unkeyed, nr_keyed;
1361 int err;
1362
1363 /*
1364 * For OPTIONAL_KEY algorithms, we have to do all the unkeyed tests
1365 * first, before setting a key on the tfm. To make this easier, we
1366 * require that the unkeyed test vectors (if any) are listed first.
1367 */
1368
1369 for (nr_unkeyed = 0; nr_unkeyed < tcount; nr_unkeyed++) {
1370 if (template[nr_unkeyed].ksize)
1371 break;
1372 }
1373 for (nr_keyed = 0; nr_unkeyed + nr_keyed < tcount; nr_keyed++) {
1374 if (!template[nr_unkeyed + nr_keyed].ksize) {
1375 pr_err("alg: hash: test vectors for %s out of order, "
1376 "unkeyed ones must come first\n", desc->alg);
Kamil Konieczny466d7b92018-01-16 15:26:13 +01001377 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08001378 }
1379 }
1380
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001381 err = 0;
1382 if (nr_unkeyed) {
1383 err = __alg_test_hash(template, nr_unkeyed, driver, type, mask);
1384 template += nr_unkeyed;
Herbert Xuda7f0332008-07-31 17:08:25 +08001385 }
1386
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001387 if (!err && nr_keyed)
1388 err = __alg_test_hash(template, nr_keyed, driver, type, mask);
Wang, Rui Y018ba952016-02-03 18:26:57 +08001389
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001390 return err;
Jussi Kivilinnada5ffe12013-06-13 17:37:55 +03001391}
1392
Eric Biggersed968042019-01-31 23:51:47 -08001393static int test_aead_vec_cfg(const char *driver, int enc,
1394 const struct aead_testvec *vec,
Eric Biggers951d1332019-04-11 21:57:37 -07001395 const char *vec_name,
Eric Biggersed968042019-01-31 23:51:47 -08001396 const struct testvec_config *cfg,
1397 struct aead_request *req,
1398 struct cipher_test_sglists *tsgls)
Herbert Xuda7f0332008-07-31 17:08:25 +08001399{
Eric Biggersed968042019-01-31 23:51:47 -08001400 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1401 const unsigned int alignmask = crypto_aead_alignmask(tfm);
1402 const unsigned int ivsize = crypto_aead_ivsize(tfm);
1403 const unsigned int authsize = vec->clen - vec->plen;
1404 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1405 const char *op = enc ? "encryption" : "decryption";
1406 DECLARE_CRYPTO_WAIT(wait);
1407 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
1408 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
1409 cfg->iv_offset +
1410 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
1411 struct kvec input[2];
Eric Biggers5283a8e2019-04-11 21:57:36 -07001412 int expected_error;
Eric Biggersed968042019-01-31 23:51:47 -08001413 int err;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001414
Eric Biggersed968042019-01-31 23:51:47 -08001415 /* Set the key */
1416 if (vec->wk)
1417 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001418 else
Eric Biggersed968042019-01-31 23:51:47 -08001419 crypto_aead_clear_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1420 err = crypto_aead_setkey(tfm, vec->key, vec->klen);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001421 if (err && err != vec->setkey_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001422 pr_err("alg: aead: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1423 driver, vec_name, vec->setkey_error, err,
Eric Biggers5283a8e2019-04-11 21:57:36 -07001424 crypto_aead_get_flags(tfm));
Eric Biggersed968042019-01-31 23:51:47 -08001425 return err;
1426 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07001427 if (!err && vec->setkey_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001428 pr_err("alg: aead: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1429 driver, vec_name, vec->setkey_error);
Eric Biggersed968042019-01-31 23:51:47 -08001430 return -EINVAL;
1431 }
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001432
Eric Biggersed968042019-01-31 23:51:47 -08001433 /* Set the authentication tag size */
1434 err = crypto_aead_setauthsize(tfm, authsize);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001435 if (err && err != vec->setauthsize_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001436 pr_err("alg: aead: %s setauthsize failed on test vector %s; expected_error=%d, actual_error=%d\n",
1437 driver, vec_name, vec->setauthsize_error, err);
Eric Biggersed968042019-01-31 23:51:47 -08001438 return err;
1439 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07001440 if (!err && vec->setauthsize_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001441 pr_err("alg: aead: %s setauthsize unexpectedly succeeded on test vector %s; expected_error=%d\n",
1442 driver, vec_name, vec->setauthsize_error);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001443 return -EINVAL;
1444 }
1445
1446 if (vec->setkey_error || vec->setauthsize_error)
1447 return 0;
Eric Biggersed968042019-01-31 23:51:47 -08001448
1449 /* The IV must be copied to a buffer, as the algorithm may modify it */
1450 if (WARN_ON(ivsize > MAX_IVLEN))
1451 return -EINVAL;
1452 if (vec->iv)
1453 memcpy(iv, vec->iv, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001454 else
Eric Biggersed968042019-01-31 23:51:47 -08001455 memset(iv, 0, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001456
Eric Biggersed968042019-01-31 23:51:47 -08001457 /* Build the src/dst scatterlists */
1458 input[0].iov_base = (void *)vec->assoc;
1459 input[0].iov_len = vec->alen;
1460 input[1].iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
1461 input[1].iov_len = enc ? vec->plen : vec->clen;
1462 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
1463 vec->alen + (enc ? vec->plen :
1464 vec->clen),
1465 vec->alen + (enc ? vec->clen :
1466 vec->plen),
1467 input, 2);
1468 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07001469 pr_err("alg: aead: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
1470 driver, op, vec_name, cfg->name);
Eric Biggersed968042019-01-31 23:51:47 -08001471 return err;
Herbert Xuda7f0332008-07-31 17:08:25 +08001472 }
1473
Eric Biggersed968042019-01-31 23:51:47 -08001474 /* Do the actual encryption or decryption */
1475 testmgr_poison(req->__ctx, crypto_aead_reqsize(tfm));
1476 aead_request_set_callback(req, req_flags, crypto_req_done, &wait);
1477 aead_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
1478 enc ? vec->plen : vec->clen, iv);
1479 aead_request_set_ad(req, vec->alen);
Eric Biggers65707372019-03-12 22:12:52 -07001480 if (cfg->nosimd)
1481 crypto_disable_simd_for_test();
1482 err = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
1483 if (cfg->nosimd)
1484 crypto_reenable_simd_for_test();
1485 err = crypto_wait_req(err, &wait);
Herbert Xuda7f0332008-07-31 17:08:25 +08001486
Eric Biggersa6e5ef92019-01-31 23:51:50 -08001487 /* Check that the algorithm didn't overwrite things it shouldn't have */
1488 if (req->cryptlen != (enc ? vec->plen : vec->clen) ||
1489 req->assoclen != vec->alen ||
1490 req->iv != iv ||
1491 req->src != tsgls->src.sgl_ptr ||
1492 req->dst != tsgls->dst.sgl_ptr ||
1493 crypto_aead_reqtfm(req) != tfm ||
1494 req->base.complete != crypto_req_done ||
1495 req->base.flags != req_flags ||
1496 req->base.data != &wait) {
Eric Biggers951d1332019-04-11 21:57:37 -07001497 pr_err("alg: aead: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
1498 driver, op, vec_name, cfg->name);
Eric Biggersa6e5ef92019-01-31 23:51:50 -08001499 if (req->cryptlen != (enc ? vec->plen : vec->clen))
1500 pr_err("alg: aead: changed 'req->cryptlen'\n");
1501 if (req->assoclen != vec->alen)
1502 pr_err("alg: aead: changed 'req->assoclen'\n");
1503 if (req->iv != iv)
1504 pr_err("alg: aead: changed 'req->iv'\n");
1505 if (req->src != tsgls->src.sgl_ptr)
1506 pr_err("alg: aead: changed 'req->src'\n");
1507 if (req->dst != tsgls->dst.sgl_ptr)
1508 pr_err("alg: aead: changed 'req->dst'\n");
1509 if (crypto_aead_reqtfm(req) != tfm)
1510 pr_err("alg: aead: changed 'req->base.tfm'\n");
1511 if (req->base.complete != crypto_req_done)
1512 pr_err("alg: aead: changed 'req->base.complete'\n");
1513 if (req->base.flags != req_flags)
1514 pr_err("alg: aead: changed 'req->base.flags'\n");
1515 if (req->base.data != &wait)
1516 pr_err("alg: aead: changed 'req->base.data'\n");
1517 return -EINVAL;
1518 }
1519 if (is_test_sglist_corrupted(&tsgls->src)) {
Eric Biggers951d1332019-04-11 21:57:37 -07001520 pr_err("alg: aead: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
1521 driver, op, vec_name, cfg->name);
Eric Biggersa6e5ef92019-01-31 23:51:50 -08001522 return -EINVAL;
1523 }
1524 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
1525 is_test_sglist_corrupted(&tsgls->dst)) {
Eric Biggers951d1332019-04-11 21:57:37 -07001526 pr_err("alg: aead: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
1527 driver, op, vec_name, cfg->name);
Eric Biggersa6e5ef92019-01-31 23:51:50 -08001528 return -EINVAL;
1529 }
1530
Eric Biggers5283a8e2019-04-11 21:57:36 -07001531 /* Check for success or failure */
1532 expected_error = vec->novrfy ? -EBADMSG : vec->crypt_error;
1533 if (err) {
1534 if (err == expected_error)
1535 return 0;
Eric Biggers951d1332019-04-11 21:57:37 -07001536 pr_err("alg: aead: %s %s failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
1537 driver, op, vec_name, expected_error, err, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001538 return err;
1539 }
1540 if (expected_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001541 pr_err("alg: aead: %s %s unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
1542 driver, op, vec_name, expected_error, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001543 return -EINVAL;
1544 }
1545
Eric Biggersed968042019-01-31 23:51:47 -08001546 /* Check for the correct output (ciphertext or plaintext) */
1547 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
1548 enc ? vec->clen : vec->plen,
1549 vec->alen, enc || !cfg->inplace);
1550 if (err == -EOVERFLOW) {
Eric Biggers951d1332019-04-11 21:57:37 -07001551 pr_err("alg: aead: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
1552 driver, op, vec_name, cfg->name);
Eric Biggersed968042019-01-31 23:51:47 -08001553 return err;
Herbert Xuda7f0332008-07-31 17:08:25 +08001554 }
Eric Biggersed968042019-01-31 23:51:47 -08001555 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07001556 pr_err("alg: aead: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
1557 driver, op, vec_name, cfg->name);
Eric Biggersed968042019-01-31 23:51:47 -08001558 return err;
Jussi Kivilinna58dcf542013-06-13 17:37:50 +03001559 }
1560
1561 return 0;
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001562}
1563
Eric Biggersed968042019-01-31 23:51:47 -08001564static int test_aead_vec(const char *driver, int enc,
1565 const struct aead_testvec *vec, unsigned int vec_num,
1566 struct aead_request *req,
1567 struct cipher_test_sglists *tsgls)
1568{
Eric Biggers951d1332019-04-11 21:57:37 -07001569 char vec_name[16];
Eric Biggersed968042019-01-31 23:51:47 -08001570 unsigned int i;
1571 int err;
1572
1573 if (enc && vec->novrfy)
1574 return 0;
1575
Eric Biggers951d1332019-04-11 21:57:37 -07001576 sprintf(vec_name, "%u", vec_num);
1577
Eric Biggersed968042019-01-31 23:51:47 -08001578 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
Eric Biggers951d1332019-04-11 21:57:37 -07001579 err = test_aead_vec_cfg(driver, enc, vec, vec_name,
Eric Biggersed968042019-01-31 23:51:47 -08001580 &default_cipher_testvec_configs[i],
1581 req, tsgls);
1582 if (err)
1583 return err;
1584 }
1585
1586#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1587 if (!noextratests) {
1588 struct testvec_config cfg;
1589 char cfgname[TESTVEC_CONFIG_NAMELEN];
1590
1591 for (i = 0; i < fuzz_iterations; i++) {
1592 generate_random_testvec_config(&cfg, cfgname,
1593 sizeof(cfgname));
Eric Biggers951d1332019-04-11 21:57:37 -07001594 err = test_aead_vec_cfg(driver, enc, vec, vec_name,
Eric Biggersed968042019-01-31 23:51:47 -08001595 &cfg, req, tsgls);
1596 if (err)
1597 return err;
1598 }
1599 }
1600#endif
1601 return 0;
1602}
1603
1604static int test_aead(const char *driver, int enc,
1605 const struct aead_test_suite *suite,
1606 struct aead_request *req,
1607 struct cipher_test_sglists *tsgls)
1608{
1609 unsigned int i;
1610 int err;
1611
1612 for (i = 0; i < suite->count; i++) {
1613 err = test_aead_vec(driver, enc, &suite->vecs[i], i, req,
1614 tsgls);
1615 if (err)
1616 return err;
1617 }
1618 return 0;
1619}
1620
1621static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
1622 u32 type, u32 mask)
1623{
1624 const struct aead_test_suite *suite = &desc->suite.aead;
1625 struct crypto_aead *tfm;
1626 struct aead_request *req = NULL;
1627 struct cipher_test_sglists *tsgls = NULL;
1628 int err;
1629
1630 if (suite->count <= 0) {
1631 pr_err("alg: aead: empty test suite for %s\n", driver);
1632 return -EINVAL;
1633 }
1634
1635 tfm = crypto_alloc_aead(driver, type, mask);
1636 if (IS_ERR(tfm)) {
1637 pr_err("alg: aead: failed to allocate transform for %s: %ld\n",
1638 driver, PTR_ERR(tfm));
1639 return PTR_ERR(tfm);
1640 }
1641
1642 req = aead_request_alloc(tfm, GFP_KERNEL);
1643 if (!req) {
1644 pr_err("alg: aead: failed to allocate request for %s\n",
1645 driver);
1646 err = -ENOMEM;
1647 goto out;
1648 }
1649
1650 tsgls = alloc_cipher_test_sglists();
1651 if (!tsgls) {
1652 pr_err("alg: aead: failed to allocate test buffers for %s\n",
1653 driver);
1654 err = -ENOMEM;
1655 goto out;
1656 }
1657
1658 err = test_aead(driver, ENCRYPT, suite, req, tsgls);
1659 if (err)
1660 goto out;
1661
1662 err = test_aead(driver, DECRYPT, suite, req, tsgls);
1663out:
1664 free_cipher_test_sglists(tsgls);
1665 aead_request_free(req);
1666 crypto_free_aead(tfm);
1667 return err;
1668}
1669
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001670static int test_cipher(struct crypto_cipher *tfm, int enc,
Eric Biggersb13b1e02017-02-24 15:46:59 -08001671 const struct cipher_testvec *template,
1672 unsigned int tcount)
Herbert Xuda7f0332008-07-31 17:08:25 +08001673{
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001674 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
1675 unsigned int i, j, k;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001676 char *q;
1677 const char *e;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001678 const char *input, *result;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001679 void *data;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001680 char *xbuf[XBUFSIZE];
1681 int ret = -ENOMEM;
1682
1683 if (testmgr_alloc_buf(xbuf))
1684 goto out_nobuf;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001685
1686 if (enc == ENCRYPT)
1687 e = "encryption";
1688 else
1689 e = "decryption";
1690
1691 j = 0;
1692 for (i = 0; i < tcount; i++) {
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001693
Stephan Mueller10faa8c2016-08-25 15:15:01 +02001694 if (fips_enabled && template[i].fips_skip)
1695 continue;
1696
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001697 input = enc ? template[i].ptext : template[i].ctext;
1698 result = enc ? template[i].ctext : template[i].ptext;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001699 j++;
1700
Herbert Xufd57f222009-05-29 16:05:42 +10001701 ret = -EINVAL;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001702 if (WARN_ON(template[i].len > PAGE_SIZE))
Herbert Xufd57f222009-05-29 16:05:42 +10001703 goto out;
1704
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001705 data = xbuf[0];
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001706 memcpy(data, input, template[i].len);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001707
1708 crypto_cipher_clear_flags(tfm, ~0);
1709 if (template[i].wk)
Eric Biggers231baec2019-01-18 22:48:00 -08001710 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001711
1712 ret = crypto_cipher_setkey(tfm, template[i].key,
1713 template[i].klen);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001714 if (ret) {
1715 if (ret == template[i].setkey_error)
1716 continue;
1717 pr_err("alg: cipher: %s setkey failed on test vector %u; expected_error=%d, actual_error=%d, flags=%#x\n",
1718 algo, j, template[i].setkey_error, ret,
1719 crypto_cipher_get_flags(tfm));
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001720 goto out;
Eric Biggers5283a8e2019-04-11 21:57:36 -07001721 }
1722 if (template[i].setkey_error) {
1723 pr_err("alg: cipher: %s setkey unexpectedly succeeded on test vector %u; expected_error=%d\n",
1724 algo, j, template[i].setkey_error);
1725 ret = -EINVAL;
1726 goto out;
1727 }
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001728
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001729 for (k = 0; k < template[i].len;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001730 k += crypto_cipher_blocksize(tfm)) {
1731 if (enc)
1732 crypto_cipher_encrypt_one(tfm, data + k,
1733 data + k);
1734 else
1735 crypto_cipher_decrypt_one(tfm, data + k,
1736 data + k);
1737 }
1738
1739 q = data;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001740 if (memcmp(q, result, template[i].len)) {
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001741 printk(KERN_ERR "alg: cipher: Test %d failed "
1742 "on %s for %s\n", j, e, algo);
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001743 hexdump(q, template[i].len);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001744 ret = -EINVAL;
1745 goto out;
1746 }
1747 }
1748
1749 ret = 0;
1750
1751out:
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001752 testmgr_free_buf(xbuf);
1753out_nobuf:
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001754 return ret;
1755}
1756
Eric Biggers4e7babba2019-01-31 23:51:46 -08001757static int test_skcipher_vec_cfg(const char *driver, int enc,
1758 const struct cipher_testvec *vec,
Eric Biggers951d1332019-04-11 21:57:37 -07001759 const char *vec_name,
Eric Biggers4e7babba2019-01-31 23:51:46 -08001760 const struct testvec_config *cfg,
1761 struct skcipher_request *req,
1762 struct cipher_test_sglists *tsgls)
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001763{
Eric Biggers4e7babba2019-01-31 23:51:46 -08001764 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1765 const unsigned int alignmask = crypto_skcipher_alignmask(tfm);
1766 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
1767 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1768 const char *op = enc ? "encryption" : "decryption";
1769 DECLARE_CRYPTO_WAIT(wait);
1770 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
1771 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
1772 cfg->iv_offset +
1773 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
1774 struct kvec input;
1775 int err;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001776
Eric Biggers4e7babba2019-01-31 23:51:46 -08001777 /* Set the key */
1778 if (vec->wk)
1779 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001780 else
Eric Biggers4e7babba2019-01-31 23:51:46 -08001781 crypto_skcipher_clear_flags(tfm,
1782 CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1783 err = crypto_skcipher_setkey(tfm, vec->key, vec->klen);
1784 if (err) {
Eric Biggers5283a8e2019-04-11 21:57:36 -07001785 if (err == vec->setkey_error)
Eric Biggers4e7babba2019-01-31 23:51:46 -08001786 return 0;
Eric Biggers951d1332019-04-11 21:57:37 -07001787 pr_err("alg: skcipher: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1788 driver, vec_name, vec->setkey_error, err,
Eric Biggers5283a8e2019-04-11 21:57:36 -07001789 crypto_skcipher_get_flags(tfm));
Eric Biggers4e7babba2019-01-31 23:51:46 -08001790 return err;
1791 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07001792 if (vec->setkey_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001793 pr_err("alg: skcipher: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1794 driver, vec_name, vec->setkey_error);
Eric Biggers4e7babba2019-01-31 23:51:46 -08001795 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08001796 }
1797
Eric Biggers4e7babba2019-01-31 23:51:46 -08001798 /* The IV must be copied to a buffer, as the algorithm may modify it */
1799 if (ivsize) {
1800 if (WARN_ON(ivsize > MAX_IVLEN))
1801 return -EINVAL;
Eric Biggers8efd9722019-02-14 00:03:51 -08001802 if (vec->generates_iv && !enc)
1803 memcpy(iv, vec->iv_out, ivsize);
1804 else if (vec->iv)
Eric Biggers4e7babba2019-01-31 23:51:46 -08001805 memcpy(iv, vec->iv, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001806 else
Eric Biggers4e7babba2019-01-31 23:51:46 -08001807 memset(iv, 0, ivsize);
1808 } else {
1809 if (vec->generates_iv) {
Eric Biggers951d1332019-04-11 21:57:37 -07001810 pr_err("alg: skcipher: %s has ivsize=0 but test vector %s generates IV!\n",
1811 driver, vec_name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08001812 return -EINVAL;
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001813 }
Eric Biggers4e7babba2019-01-31 23:51:46 -08001814 iv = NULL;
Herbert Xuda7f0332008-07-31 17:08:25 +08001815 }
1816
Eric Biggers4e7babba2019-01-31 23:51:46 -08001817 /* Build the src/dst scatterlists */
1818 input.iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
1819 input.iov_len = vec->len;
1820 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
1821 vec->len, vec->len, &input, 1);
1822 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07001823 pr_err("alg: skcipher: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
1824 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08001825 return err;
Herbert Xuda7f0332008-07-31 17:08:25 +08001826 }
1827
Eric Biggers4e7babba2019-01-31 23:51:46 -08001828 /* Do the actual encryption or decryption */
1829 testmgr_poison(req->__ctx, crypto_skcipher_reqsize(tfm));
1830 skcipher_request_set_callback(req, req_flags, crypto_req_done, &wait);
1831 skcipher_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
1832 vec->len, iv);
Eric Biggers65707372019-03-12 22:12:52 -07001833 if (cfg->nosimd)
1834 crypto_disable_simd_for_test();
1835 err = enc ? crypto_skcipher_encrypt(req) : crypto_skcipher_decrypt(req);
1836 if (cfg->nosimd)
1837 crypto_reenable_simd_for_test();
1838 err = crypto_wait_req(err, &wait);
Herbert Xuda7f0332008-07-31 17:08:25 +08001839
Eric Biggersfa353c92019-01-31 23:51:49 -08001840 /* Check that the algorithm didn't overwrite things it shouldn't have */
1841 if (req->cryptlen != vec->len ||
1842 req->iv != iv ||
1843 req->src != tsgls->src.sgl_ptr ||
1844 req->dst != tsgls->dst.sgl_ptr ||
1845 crypto_skcipher_reqtfm(req) != tfm ||
1846 req->base.complete != crypto_req_done ||
1847 req->base.flags != req_flags ||
1848 req->base.data != &wait) {
Eric Biggers951d1332019-04-11 21:57:37 -07001849 pr_err("alg: skcipher: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
1850 driver, op, vec_name, cfg->name);
Eric Biggersfa353c92019-01-31 23:51:49 -08001851 if (req->cryptlen != vec->len)
1852 pr_err("alg: skcipher: changed 'req->cryptlen'\n");
1853 if (req->iv != iv)
1854 pr_err("alg: skcipher: changed 'req->iv'\n");
1855 if (req->src != tsgls->src.sgl_ptr)
1856 pr_err("alg: skcipher: changed 'req->src'\n");
1857 if (req->dst != tsgls->dst.sgl_ptr)
1858 pr_err("alg: skcipher: changed 'req->dst'\n");
1859 if (crypto_skcipher_reqtfm(req) != tfm)
1860 pr_err("alg: skcipher: changed 'req->base.tfm'\n");
1861 if (req->base.complete != crypto_req_done)
1862 pr_err("alg: skcipher: changed 'req->base.complete'\n");
1863 if (req->base.flags != req_flags)
1864 pr_err("alg: skcipher: changed 'req->base.flags'\n");
1865 if (req->base.data != &wait)
1866 pr_err("alg: skcipher: changed 'req->base.data'\n");
1867 return -EINVAL;
1868 }
1869 if (is_test_sglist_corrupted(&tsgls->src)) {
Eric Biggers951d1332019-04-11 21:57:37 -07001870 pr_err("alg: skcipher: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
1871 driver, op, vec_name, cfg->name);
Eric Biggersfa353c92019-01-31 23:51:49 -08001872 return -EINVAL;
1873 }
1874 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
1875 is_test_sglist_corrupted(&tsgls->dst)) {
Eric Biggers951d1332019-04-11 21:57:37 -07001876 pr_err("alg: skcipher: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
1877 driver, op, vec_name, cfg->name);
Eric Biggersfa353c92019-01-31 23:51:49 -08001878 return -EINVAL;
1879 }
1880
Eric Biggers5283a8e2019-04-11 21:57:36 -07001881 /* Check for success or failure */
1882 if (err) {
1883 if (err == vec->crypt_error)
1884 return 0;
Eric Biggers951d1332019-04-11 21:57:37 -07001885 pr_err("alg: skcipher: %s %s failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
1886 driver, op, vec_name, vec->crypt_error, err, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001887 return err;
1888 }
1889 if (vec->crypt_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001890 pr_err("alg: skcipher: %s %s unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
1891 driver, op, vec_name, vec->crypt_error, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001892 return -EINVAL;
1893 }
1894
Eric Biggers4e7babba2019-01-31 23:51:46 -08001895 /* Check for the correct output (ciphertext or plaintext) */
1896 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
1897 vec->len, 0, true);
1898 if (err == -EOVERFLOW) {
Eric Biggers951d1332019-04-11 21:57:37 -07001899 pr_err("alg: skcipher: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
1900 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08001901 return err;
1902 }
1903 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07001904 pr_err("alg: skcipher: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
1905 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08001906 return err;
1907 }
Herbert Xuda7f0332008-07-31 17:08:25 +08001908
Eric Biggers4e7babba2019-01-31 23:51:46 -08001909 /* If applicable, check that the algorithm generated the correct IV */
Eric Biggers8efd9722019-02-14 00:03:51 -08001910 if (vec->iv_out && memcmp(iv, vec->iv_out, ivsize) != 0) {
Eric Biggers951d1332019-04-11 21:57:37 -07001911 pr_err("alg: skcipher: %s %s test failed (wrong output IV) on test vector %s, cfg=\"%s\"\n",
1912 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08001913 hexdump(iv, ivsize);
1914 return -EINVAL;
Jussi Kivilinna3a338f22013-06-13 17:37:45 +03001915 }
1916
1917 return 0;
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001918}
1919
Eric Biggers4e7babba2019-01-31 23:51:46 -08001920static int test_skcipher_vec(const char *driver, int enc,
1921 const struct cipher_testvec *vec,
1922 unsigned int vec_num,
1923 struct skcipher_request *req,
1924 struct cipher_test_sglists *tsgls)
1925{
Eric Biggers951d1332019-04-11 21:57:37 -07001926 char vec_name[16];
Eric Biggers4e7babba2019-01-31 23:51:46 -08001927 unsigned int i;
1928 int err;
1929
1930 if (fips_enabled && vec->fips_skip)
1931 return 0;
1932
Eric Biggers951d1332019-04-11 21:57:37 -07001933 sprintf(vec_name, "%u", vec_num);
1934
Eric Biggers4e7babba2019-01-31 23:51:46 -08001935 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
Eric Biggers951d1332019-04-11 21:57:37 -07001936 err = test_skcipher_vec_cfg(driver, enc, vec, vec_name,
Eric Biggers4e7babba2019-01-31 23:51:46 -08001937 &default_cipher_testvec_configs[i],
1938 req, tsgls);
1939 if (err)
1940 return err;
1941 }
1942
1943#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1944 if (!noextratests) {
1945 struct testvec_config cfg;
1946 char cfgname[TESTVEC_CONFIG_NAMELEN];
1947
1948 for (i = 0; i < fuzz_iterations; i++) {
1949 generate_random_testvec_config(&cfg, cfgname,
1950 sizeof(cfgname));
Eric Biggers951d1332019-04-11 21:57:37 -07001951 err = test_skcipher_vec_cfg(driver, enc, vec, vec_name,
Eric Biggers4e7babba2019-01-31 23:51:46 -08001952 &cfg, req, tsgls);
1953 if (err)
1954 return err;
1955 }
1956 }
1957#endif
1958 return 0;
1959}
1960
1961static int test_skcipher(const char *driver, int enc,
1962 const struct cipher_test_suite *suite,
1963 struct skcipher_request *req,
1964 struct cipher_test_sglists *tsgls)
1965{
1966 unsigned int i;
1967 int err;
1968
1969 for (i = 0; i < suite->count; i++) {
1970 err = test_skcipher_vec(driver, enc, &suite->vecs[i], i, req,
1971 tsgls);
1972 if (err)
1973 return err;
1974 }
1975 return 0;
1976}
1977
1978static int alg_test_skcipher(const struct alg_test_desc *desc,
1979 const char *driver, u32 type, u32 mask)
1980{
1981 const struct cipher_test_suite *suite = &desc->suite.cipher;
1982 struct crypto_skcipher *tfm;
1983 struct skcipher_request *req = NULL;
1984 struct cipher_test_sglists *tsgls = NULL;
1985 int err;
1986
1987 if (suite->count <= 0) {
1988 pr_err("alg: skcipher: empty test suite for %s\n", driver);
1989 return -EINVAL;
1990 }
1991
1992 tfm = crypto_alloc_skcipher(driver, type, mask);
1993 if (IS_ERR(tfm)) {
1994 pr_err("alg: skcipher: failed to allocate transform for %s: %ld\n",
1995 driver, PTR_ERR(tfm));
1996 return PTR_ERR(tfm);
1997 }
1998
1999 req = skcipher_request_alloc(tfm, GFP_KERNEL);
2000 if (!req) {
2001 pr_err("alg: skcipher: failed to allocate request for %s\n",
2002 driver);
2003 err = -ENOMEM;
2004 goto out;
2005 }
2006
2007 tsgls = alloc_cipher_test_sglists();
2008 if (!tsgls) {
2009 pr_err("alg: skcipher: failed to allocate test buffers for %s\n",
2010 driver);
2011 err = -ENOMEM;
2012 goto out;
2013 }
2014
2015 err = test_skcipher(driver, ENCRYPT, suite, req, tsgls);
2016 if (err)
2017 goto out;
2018
2019 err = test_skcipher(driver, DECRYPT, suite, req, tsgls);
2020out:
2021 free_cipher_test_sglists(tsgls);
2022 skcipher_request_free(req);
2023 crypto_free_skcipher(tfm);
2024 return err;
2025}
2026
Eric Biggersb13b1e02017-02-24 15:46:59 -08002027static int test_comp(struct crypto_comp *tfm,
2028 const struct comp_testvec *ctemplate,
2029 const struct comp_testvec *dtemplate,
2030 int ctcount, int dtcount)
Herbert Xuda7f0332008-07-31 17:08:25 +08002031{
2032 const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
Mahipal Challa33607382018-04-11 20:28:32 +02002033 char *output, *decomp_output;
Herbert Xuda7f0332008-07-31 17:08:25 +08002034 unsigned int i;
Herbert Xuda7f0332008-07-31 17:08:25 +08002035 int ret;
2036
Mahipal Challa33607382018-04-11 20:28:32 +02002037 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
2038 if (!output)
2039 return -ENOMEM;
2040
2041 decomp_output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
2042 if (!decomp_output) {
2043 kfree(output);
2044 return -ENOMEM;
2045 }
2046
Herbert Xuda7f0332008-07-31 17:08:25 +08002047 for (i = 0; i < ctcount; i++) {
Geert Uytterhoevenc79cf912009-03-29 15:44:19 +08002048 int ilen;
2049 unsigned int dlen = COMP_BUF_SIZE;
Herbert Xuda7f0332008-07-31 17:08:25 +08002050
Michael Schupikov22a81182018-10-07 13:58:10 +02002051 memset(output, 0, COMP_BUF_SIZE);
2052 memset(decomp_output, 0, COMP_BUF_SIZE);
Herbert Xuda7f0332008-07-31 17:08:25 +08002053
2054 ilen = ctemplate[i].inlen;
2055 ret = crypto_comp_compress(tfm, ctemplate[i].input,
Mahipal Challa33607382018-04-11 20:28:32 +02002056 ilen, output, &dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08002057 if (ret) {
2058 printk(KERN_ERR "alg: comp: compression failed "
2059 "on test %d for %s: ret=%d\n", i + 1, algo,
2060 -ret);
2061 goto out;
2062 }
2063
Mahipal Challa33607382018-04-11 20:28:32 +02002064 ilen = dlen;
2065 dlen = COMP_BUF_SIZE;
2066 ret = crypto_comp_decompress(tfm, output,
2067 ilen, decomp_output, &dlen);
2068 if (ret) {
2069 pr_err("alg: comp: compression failed: decompress: on test %d for %s failed: ret=%d\n",
2070 i + 1, algo, -ret);
2071 goto out;
2072 }
2073
2074 if (dlen != ctemplate[i].inlen) {
Geert Uytterhoevenb812eb02008-11-28 20:51:28 +08002075 printk(KERN_ERR "alg: comp: Compression test %d "
2076 "failed for %s: output len = %d\n", i + 1, algo,
2077 dlen);
2078 ret = -EINVAL;
2079 goto out;
2080 }
2081
Mahipal Challa33607382018-04-11 20:28:32 +02002082 if (memcmp(decomp_output, ctemplate[i].input,
2083 ctemplate[i].inlen)) {
2084 pr_err("alg: comp: compression failed: output differs: on test %d for %s\n",
2085 i + 1, algo);
2086 hexdump(decomp_output, dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08002087 ret = -EINVAL;
2088 goto out;
2089 }
2090 }
2091
2092 for (i = 0; i < dtcount; i++) {
Geert Uytterhoevenc79cf912009-03-29 15:44:19 +08002093 int ilen;
2094 unsigned int dlen = COMP_BUF_SIZE;
Herbert Xuda7f0332008-07-31 17:08:25 +08002095
Michael Schupikov22a81182018-10-07 13:58:10 +02002096 memset(decomp_output, 0, COMP_BUF_SIZE);
Herbert Xuda7f0332008-07-31 17:08:25 +08002097
2098 ilen = dtemplate[i].inlen;
2099 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
Mahipal Challa33607382018-04-11 20:28:32 +02002100 ilen, decomp_output, &dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08002101 if (ret) {
2102 printk(KERN_ERR "alg: comp: decompression failed "
2103 "on test %d for %s: ret=%d\n", i + 1, algo,
2104 -ret);
2105 goto out;
2106 }
2107
Geert Uytterhoevenb812eb02008-11-28 20:51:28 +08002108 if (dlen != dtemplate[i].outlen) {
2109 printk(KERN_ERR "alg: comp: Decompression test %d "
2110 "failed for %s: output len = %d\n", i + 1, algo,
2111 dlen);
2112 ret = -EINVAL;
2113 goto out;
2114 }
2115
Mahipal Challa33607382018-04-11 20:28:32 +02002116 if (memcmp(decomp_output, dtemplate[i].output, dlen)) {
Herbert Xuda7f0332008-07-31 17:08:25 +08002117 printk(KERN_ERR "alg: comp: Decompression test %d "
2118 "failed for %s\n", i + 1, algo);
Mahipal Challa33607382018-04-11 20:28:32 +02002119 hexdump(decomp_output, dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08002120 ret = -EINVAL;
2121 goto out;
2122 }
2123 }
2124
2125 ret = 0;
2126
2127out:
Mahipal Challa33607382018-04-11 20:28:32 +02002128 kfree(decomp_output);
2129 kfree(output);
Herbert Xuda7f0332008-07-31 17:08:25 +08002130 return ret;
2131}
2132
Eric Biggersb13b1e02017-02-24 15:46:59 -08002133static int test_acomp(struct crypto_acomp *tfm,
Mahipal Challa33607382018-04-11 20:28:32 +02002134 const struct comp_testvec *ctemplate,
Eric Biggersb13b1e02017-02-24 15:46:59 -08002135 const struct comp_testvec *dtemplate,
2136 int ctcount, int dtcount)
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002137{
2138 const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
2139 unsigned int i;
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002140 char *output, *decomp_out;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002141 int ret;
2142 struct scatterlist src, dst;
2143 struct acomp_req *req;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002144 struct crypto_wait wait;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002145
Eric Biggerseb095592016-11-23 10:24:35 -08002146 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
2147 if (!output)
2148 return -ENOMEM;
2149
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002150 decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
2151 if (!decomp_out) {
2152 kfree(output);
2153 return -ENOMEM;
2154 }
2155
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002156 for (i = 0; i < ctcount; i++) {
2157 unsigned int dlen = COMP_BUF_SIZE;
2158 int ilen = ctemplate[i].inlen;
Laura Abbott02608e02016-12-21 12:32:54 -08002159 void *input_vec;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002160
Eric Biggersd2110222016-12-30 14:12:00 -06002161 input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL);
Laura Abbott02608e02016-12-21 12:32:54 -08002162 if (!input_vec) {
2163 ret = -ENOMEM;
2164 goto out;
2165 }
2166
Eric Biggerseb095592016-11-23 10:24:35 -08002167 memset(output, 0, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002168 crypto_init_wait(&wait);
Laura Abbott02608e02016-12-21 12:32:54 -08002169 sg_init_one(&src, input_vec, ilen);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002170 sg_init_one(&dst, output, dlen);
2171
2172 req = acomp_request_alloc(tfm);
2173 if (!req) {
2174 pr_err("alg: acomp: request alloc failed for %s\n",
2175 algo);
Laura Abbott02608e02016-12-21 12:32:54 -08002176 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002177 ret = -ENOMEM;
2178 goto out;
2179 }
2180
2181 acomp_request_set_params(req, &src, &dst, ilen, dlen);
2182 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002183 crypto_req_done, &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002184
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002185 ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002186 if (ret) {
2187 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
2188 i + 1, algo, -ret);
Laura Abbott02608e02016-12-21 12:32:54 -08002189 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002190 acomp_request_free(req);
2191 goto out;
2192 }
2193
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002194 ilen = req->dlen;
2195 dlen = COMP_BUF_SIZE;
2196 sg_init_one(&src, output, ilen);
2197 sg_init_one(&dst, decomp_out, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002198 crypto_init_wait(&wait);
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002199 acomp_request_set_params(req, &src, &dst, ilen, dlen);
2200
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002201 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002202 if (ret) {
2203 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
2204 i + 1, algo, -ret);
2205 kfree(input_vec);
2206 acomp_request_free(req);
2207 goto out;
2208 }
2209
2210 if (req->dlen != ctemplate[i].inlen) {
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002211 pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
2212 i + 1, algo, req->dlen);
2213 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08002214 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002215 acomp_request_free(req);
2216 goto out;
2217 }
2218
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002219 if (memcmp(input_vec, decomp_out, req->dlen)) {
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002220 pr_err("alg: acomp: Compression test %d failed for %s\n",
2221 i + 1, algo);
2222 hexdump(output, req->dlen);
2223 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08002224 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002225 acomp_request_free(req);
2226 goto out;
2227 }
2228
Laura Abbott02608e02016-12-21 12:32:54 -08002229 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002230 acomp_request_free(req);
2231 }
2232
2233 for (i = 0; i < dtcount; i++) {
2234 unsigned int dlen = COMP_BUF_SIZE;
2235 int ilen = dtemplate[i].inlen;
Laura Abbott02608e02016-12-21 12:32:54 -08002236 void *input_vec;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002237
Eric Biggersd2110222016-12-30 14:12:00 -06002238 input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL);
Laura Abbott02608e02016-12-21 12:32:54 -08002239 if (!input_vec) {
2240 ret = -ENOMEM;
2241 goto out;
2242 }
2243
Eric Biggerseb095592016-11-23 10:24:35 -08002244 memset(output, 0, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002245 crypto_init_wait(&wait);
Laura Abbott02608e02016-12-21 12:32:54 -08002246 sg_init_one(&src, input_vec, ilen);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002247 sg_init_one(&dst, output, dlen);
2248
2249 req = acomp_request_alloc(tfm);
2250 if (!req) {
2251 pr_err("alg: acomp: request alloc failed for %s\n",
2252 algo);
Laura Abbott02608e02016-12-21 12:32:54 -08002253 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002254 ret = -ENOMEM;
2255 goto out;
2256 }
2257
2258 acomp_request_set_params(req, &src, &dst, ilen, dlen);
2259 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002260 crypto_req_done, &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002261
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002262 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002263 if (ret) {
2264 pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
2265 i + 1, algo, -ret);
Laura Abbott02608e02016-12-21 12:32:54 -08002266 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002267 acomp_request_free(req);
2268 goto out;
2269 }
2270
2271 if (req->dlen != dtemplate[i].outlen) {
2272 pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
2273 i + 1, algo, req->dlen);
2274 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08002275 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002276 acomp_request_free(req);
2277 goto out;
2278 }
2279
2280 if (memcmp(output, dtemplate[i].output, req->dlen)) {
2281 pr_err("alg: acomp: Decompression test %d failed for %s\n",
2282 i + 1, algo);
2283 hexdump(output, req->dlen);
2284 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08002285 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002286 acomp_request_free(req);
2287 goto out;
2288 }
2289
Laura Abbott02608e02016-12-21 12:32:54 -08002290 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002291 acomp_request_free(req);
2292 }
2293
2294 ret = 0;
2295
2296out:
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002297 kfree(decomp_out);
Eric Biggerseb095592016-11-23 10:24:35 -08002298 kfree(output);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002299 return ret;
2300}
2301
Eric Biggersb13b1e02017-02-24 15:46:59 -08002302static int test_cprng(struct crypto_rng *tfm,
2303 const struct cprng_testvec *template,
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002304 unsigned int tcount)
2305{
2306 const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
Felipe Contrerasfa4ef8a2009-10-27 19:04:42 +08002307 int err = 0, i, j, seedsize;
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002308 u8 *seed;
2309 char result[32];
2310
2311 seedsize = crypto_rng_seedsize(tfm);
2312
2313 seed = kmalloc(seedsize, GFP_KERNEL);
2314 if (!seed) {
2315 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
2316 "for %s\n", algo);
2317 return -ENOMEM;
2318 }
2319
2320 for (i = 0; i < tcount; i++) {
2321 memset(result, 0, 32);
2322
2323 memcpy(seed, template[i].v, template[i].vlen);
2324 memcpy(seed + template[i].vlen, template[i].key,
2325 template[i].klen);
2326 memcpy(seed + template[i].vlen + template[i].klen,
2327 template[i].dt, template[i].dtlen);
2328
2329 err = crypto_rng_reset(tfm, seed, seedsize);
2330 if (err) {
2331 printk(KERN_ERR "alg: cprng: Failed to reset rng "
2332 "for %s\n", algo);
2333 goto out;
2334 }
2335
2336 for (j = 0; j < template[i].loops; j++) {
2337 err = crypto_rng_get_bytes(tfm, result,
2338 template[i].rlen);
Stephan Mueller19e60e12015-03-10 17:00:36 +01002339 if (err < 0) {
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002340 printk(KERN_ERR "alg: cprng: Failed to obtain "
2341 "the correct amount of random data for "
Stephan Mueller19e60e12015-03-10 17:00:36 +01002342 "%s (requested %d)\n", algo,
2343 template[i].rlen);
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002344 goto out;
2345 }
2346 }
2347
2348 err = memcmp(result, template[i].result,
2349 template[i].rlen);
2350 if (err) {
2351 printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
2352 i, algo);
2353 hexdump(result, template[i].rlen);
2354 err = -EINVAL;
2355 goto out;
2356 }
2357 }
2358
2359out:
2360 kfree(seed);
2361 return err;
2362}
2363
Herbert Xuda7f0332008-07-31 17:08:25 +08002364static int alg_test_cipher(const struct alg_test_desc *desc,
2365 const char *driver, u32 type, u32 mask)
2366{
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002367 const struct cipher_test_suite *suite = &desc->suite.cipher;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002368 struct crypto_cipher *tfm;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002369 int err;
Herbert Xuda7f0332008-07-31 17:08:25 +08002370
Herbert Xueed93e02016-11-22 20:08:31 +08002371 tfm = crypto_alloc_cipher(driver, type, mask);
Herbert Xuda7f0332008-07-31 17:08:25 +08002372 if (IS_ERR(tfm)) {
2373 printk(KERN_ERR "alg: cipher: Failed to load transform for "
2374 "%s: %ld\n", driver, PTR_ERR(tfm));
2375 return PTR_ERR(tfm);
2376 }
2377
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002378 err = test_cipher(tfm, ENCRYPT, suite->vecs, suite->count);
2379 if (!err)
2380 err = test_cipher(tfm, DECRYPT, suite->vecs, suite->count);
Herbert Xuda7f0332008-07-31 17:08:25 +08002381
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002382 crypto_free_cipher(tfm);
2383 return err;
2384}
2385
Herbert Xuda7f0332008-07-31 17:08:25 +08002386static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
2387 u32 type, u32 mask)
2388{
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002389 struct crypto_comp *comp;
2390 struct crypto_acomp *acomp;
Herbert Xuda7f0332008-07-31 17:08:25 +08002391 int err;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002392 u32 algo_type = type & CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
Herbert Xuda7f0332008-07-31 17:08:25 +08002393
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002394 if (algo_type == CRYPTO_ALG_TYPE_ACOMPRESS) {
2395 acomp = crypto_alloc_acomp(driver, type, mask);
2396 if (IS_ERR(acomp)) {
2397 pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
2398 driver, PTR_ERR(acomp));
2399 return PTR_ERR(acomp);
2400 }
2401 err = test_acomp(acomp, desc->suite.comp.comp.vecs,
2402 desc->suite.comp.decomp.vecs,
2403 desc->suite.comp.comp.count,
2404 desc->suite.comp.decomp.count);
2405 crypto_free_acomp(acomp);
2406 } else {
2407 comp = crypto_alloc_comp(driver, type, mask);
2408 if (IS_ERR(comp)) {
2409 pr_err("alg: comp: Failed to load transform for %s: %ld\n",
2410 driver, PTR_ERR(comp));
2411 return PTR_ERR(comp);
2412 }
2413
2414 err = test_comp(comp, desc->suite.comp.comp.vecs,
2415 desc->suite.comp.decomp.vecs,
2416 desc->suite.comp.comp.count,
2417 desc->suite.comp.decomp.count);
2418
2419 crypto_free_comp(comp);
Herbert Xuda7f0332008-07-31 17:08:25 +08002420 }
Herbert Xuda7f0332008-07-31 17:08:25 +08002421 return err;
2422}
2423
Herbert Xu8e3ee852008-11-07 14:58:52 +08002424static int alg_test_crc32c(const struct alg_test_desc *desc,
2425 const char *driver, u32 type, u32 mask)
2426{
2427 struct crypto_shash *tfm;
Eric Biggerscb9dde82019-01-10 12:17:55 -08002428 __le32 val;
Herbert Xu8e3ee852008-11-07 14:58:52 +08002429 int err;
2430
2431 err = alg_test_hash(desc, driver, type, mask);
2432 if (err)
Eric Biggerseb5e6732019-01-23 20:57:35 -08002433 return err;
Herbert Xu8e3ee852008-11-07 14:58:52 +08002434
Herbert Xueed93e02016-11-22 20:08:31 +08002435 tfm = crypto_alloc_shash(driver, type, mask);
Herbert Xu8e3ee852008-11-07 14:58:52 +08002436 if (IS_ERR(tfm)) {
Eric Biggerseb5e6732019-01-23 20:57:35 -08002437 if (PTR_ERR(tfm) == -ENOENT) {
2438 /*
2439 * This crc32c implementation is only available through
2440 * ahash API, not the shash API, so the remaining part
2441 * of the test is not applicable to it.
2442 */
2443 return 0;
2444 }
Herbert Xu8e3ee852008-11-07 14:58:52 +08002445 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
2446 "%ld\n", driver, PTR_ERR(tfm));
Eric Biggerseb5e6732019-01-23 20:57:35 -08002447 return PTR_ERR(tfm);
Herbert Xu8e3ee852008-11-07 14:58:52 +08002448 }
2449
2450 do {
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02002451 SHASH_DESC_ON_STACK(shash, tfm);
2452 u32 *ctx = (u32 *)shash_desc_ctx(shash);
Herbert Xu8e3ee852008-11-07 14:58:52 +08002453
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02002454 shash->tfm = tfm;
2455 shash->flags = 0;
Herbert Xu8e3ee852008-11-07 14:58:52 +08002456
Eric Biggerscb9dde82019-01-10 12:17:55 -08002457 *ctx = 420553207;
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02002458 err = crypto_shash_final(shash, (u8 *)&val);
Herbert Xu8e3ee852008-11-07 14:58:52 +08002459 if (err) {
2460 printk(KERN_ERR "alg: crc32c: Operation failed for "
2461 "%s: %d\n", driver, err);
2462 break;
2463 }
2464
Eric Biggerscb9dde82019-01-10 12:17:55 -08002465 if (val != cpu_to_le32(~420553207)) {
2466 pr_err("alg: crc32c: Test failed for %s: %u\n",
2467 driver, le32_to_cpu(val));
Herbert Xu8e3ee852008-11-07 14:58:52 +08002468 err = -EINVAL;
2469 }
2470 } while (0);
2471
2472 crypto_free_shash(tfm);
2473
Herbert Xu8e3ee852008-11-07 14:58:52 +08002474 return err;
2475}
2476
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002477static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
2478 u32 type, u32 mask)
2479{
2480 struct crypto_rng *rng;
2481 int err;
2482
Herbert Xueed93e02016-11-22 20:08:31 +08002483 rng = crypto_alloc_rng(driver, type, mask);
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002484 if (IS_ERR(rng)) {
2485 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
2486 "%ld\n", driver, PTR_ERR(rng));
2487 return PTR_ERR(rng);
2488 }
2489
2490 err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
2491
2492 crypto_free_rng(rng);
2493
2494 return err;
2495}
2496
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002497
Eric Biggersb13b1e02017-02-24 15:46:59 -08002498static int drbg_cavs_test(const struct drbg_testvec *test, int pr,
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002499 const char *driver, u32 type, u32 mask)
2500{
2501 int ret = -EAGAIN;
2502 struct crypto_rng *drng;
2503 struct drbg_test_data test_data;
2504 struct drbg_string addtl, pers, testentropy;
2505 unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
2506
2507 if (!buf)
2508 return -ENOMEM;
2509
Herbert Xueed93e02016-11-22 20:08:31 +08002510 drng = crypto_alloc_rng(driver, type, mask);
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002511 if (IS_ERR(drng)) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04002512 printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002513 "%s\n", driver);
2514 kzfree(buf);
2515 return -ENOMEM;
2516 }
2517
2518 test_data.testentropy = &testentropy;
2519 drbg_string_fill(&testentropy, test->entropy, test->entropylen);
2520 drbg_string_fill(&pers, test->pers, test->perslen);
2521 ret = crypto_drbg_reset_test(drng, &pers, &test_data);
2522 if (ret) {
2523 printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
2524 goto outbuf;
2525 }
2526
2527 drbg_string_fill(&addtl, test->addtla, test->addtllen);
2528 if (pr) {
2529 drbg_string_fill(&testentropy, test->entpra, test->entprlen);
2530 ret = crypto_drbg_get_bytes_addtl_test(drng,
2531 buf, test->expectedlen, &addtl, &test_data);
2532 } else {
2533 ret = crypto_drbg_get_bytes_addtl(drng,
2534 buf, test->expectedlen, &addtl);
2535 }
Stephan Mueller19e60e12015-03-10 17:00:36 +01002536 if (ret < 0) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04002537 printk(KERN_ERR "alg: drbg: could not obtain random data for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002538 "driver %s\n", driver);
2539 goto outbuf;
2540 }
2541
2542 drbg_string_fill(&addtl, test->addtlb, test->addtllen);
2543 if (pr) {
2544 drbg_string_fill(&testentropy, test->entprb, test->entprlen);
2545 ret = crypto_drbg_get_bytes_addtl_test(drng,
2546 buf, test->expectedlen, &addtl, &test_data);
2547 } else {
2548 ret = crypto_drbg_get_bytes_addtl(drng,
2549 buf, test->expectedlen, &addtl);
2550 }
Stephan Mueller19e60e12015-03-10 17:00:36 +01002551 if (ret < 0) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04002552 printk(KERN_ERR "alg: drbg: could not obtain random data for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002553 "driver %s\n", driver);
2554 goto outbuf;
2555 }
2556
2557 ret = memcmp(test->expected, buf, test->expectedlen);
2558
2559outbuf:
2560 crypto_free_rng(drng);
2561 kzfree(buf);
2562 return ret;
2563}
2564
2565
2566static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
2567 u32 type, u32 mask)
2568{
2569 int err = 0;
2570 int pr = 0;
2571 int i = 0;
Eric Biggersb13b1e02017-02-24 15:46:59 -08002572 const struct drbg_testvec *template = desc->suite.drbg.vecs;
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002573 unsigned int tcount = desc->suite.drbg.count;
2574
2575 if (0 == memcmp(driver, "drbg_pr_", 8))
2576 pr = 1;
2577
2578 for (i = 0; i < tcount; i++) {
2579 err = drbg_cavs_test(&template[i], pr, driver, type, mask);
2580 if (err) {
2581 printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
2582 i, driver);
2583 err = -EINVAL;
2584 break;
2585 }
2586 }
2587 return err;
2588
2589}
2590
Eric Biggersb13b1e02017-02-24 15:46:59 -08002591static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002592 const char *alg)
2593{
2594 struct kpp_request *req;
2595 void *input_buf = NULL;
2596 void *output_buf = NULL;
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002597 void *a_public = NULL;
2598 void *a_ss = NULL;
2599 void *shared_secret = NULL;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002600 struct crypto_wait wait;
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002601 unsigned int out_len_max;
2602 int err = -ENOMEM;
2603 struct scatterlist src, dst;
2604
2605 req = kpp_request_alloc(tfm, GFP_KERNEL);
2606 if (!req)
2607 return err;
2608
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002609 crypto_init_wait(&wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002610
2611 err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
2612 if (err < 0)
2613 goto free_req;
2614
2615 out_len_max = crypto_kpp_maxsize(tfm);
2616 output_buf = kzalloc(out_len_max, GFP_KERNEL);
2617 if (!output_buf) {
2618 err = -ENOMEM;
2619 goto free_req;
2620 }
2621
2622 /* Use appropriate parameter as base */
2623 kpp_request_set_input(req, NULL, 0);
2624 sg_init_one(&dst, output_buf, out_len_max);
2625 kpp_request_set_output(req, &dst, out_len_max);
2626 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002627 crypto_req_done, &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002628
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002629 /* Compute party A's public key */
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002630 err = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002631 if (err) {
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002632 pr_err("alg: %s: Party A: generate public key test failed. err %d\n",
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002633 alg, err);
2634 goto free_output;
2635 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002636
2637 if (vec->genkey) {
2638 /* Save party A's public key */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05002639 a_public = kmemdup(sg_virt(req->dst), out_len_max, GFP_KERNEL);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002640 if (!a_public) {
2641 err = -ENOMEM;
2642 goto free_output;
2643 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002644 } else {
2645 /* Verify calculated public key */
2646 if (memcmp(vec->expected_a_public, sg_virt(req->dst),
2647 vec->expected_a_public_size)) {
2648 pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n",
2649 alg);
2650 err = -EINVAL;
2651 goto free_output;
2652 }
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002653 }
2654
2655 /* Calculate shared secret key by using counter part (b) public key. */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05002656 input_buf = kmemdup(vec->b_public, vec->b_public_size, GFP_KERNEL);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002657 if (!input_buf) {
2658 err = -ENOMEM;
2659 goto free_output;
2660 }
2661
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002662 sg_init_one(&src, input_buf, vec->b_public_size);
2663 sg_init_one(&dst, output_buf, out_len_max);
2664 kpp_request_set_input(req, &src, vec->b_public_size);
2665 kpp_request_set_output(req, &dst, out_len_max);
2666 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002667 crypto_req_done, &wait);
2668 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002669 if (err) {
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002670 pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n",
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002671 alg, err);
2672 goto free_all;
2673 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002674
2675 if (vec->genkey) {
2676 /* Save the shared secret obtained by party A */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05002677 a_ss = kmemdup(sg_virt(req->dst), vec->expected_ss_size, GFP_KERNEL);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002678 if (!a_ss) {
2679 err = -ENOMEM;
2680 goto free_all;
2681 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002682
2683 /*
2684 * Calculate party B's shared secret by using party A's
2685 * public key.
2686 */
2687 err = crypto_kpp_set_secret(tfm, vec->b_secret,
2688 vec->b_secret_size);
2689 if (err < 0)
2690 goto free_all;
2691
2692 sg_init_one(&src, a_public, vec->expected_a_public_size);
2693 sg_init_one(&dst, output_buf, out_len_max);
2694 kpp_request_set_input(req, &src, vec->expected_a_public_size);
2695 kpp_request_set_output(req, &dst, out_len_max);
2696 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002697 crypto_req_done, &wait);
2698 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req),
2699 &wait);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002700 if (err) {
2701 pr_err("alg: %s: Party B: compute shared secret failed. err %d\n",
2702 alg, err);
2703 goto free_all;
2704 }
2705
2706 shared_secret = a_ss;
2707 } else {
2708 shared_secret = (void *)vec->expected_ss;
2709 }
2710
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002711 /*
2712 * verify shared secret from which the user will derive
2713 * secret key by executing whatever hash it has chosen
2714 */
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002715 if (memcmp(shared_secret, sg_virt(req->dst),
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002716 vec->expected_ss_size)) {
2717 pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
2718 alg);
2719 err = -EINVAL;
2720 }
2721
2722free_all:
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002723 kfree(a_ss);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002724 kfree(input_buf);
2725free_output:
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002726 kfree(a_public);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002727 kfree(output_buf);
2728free_req:
2729 kpp_request_free(req);
2730 return err;
2731}
2732
2733static int test_kpp(struct crypto_kpp *tfm, const char *alg,
Eric Biggersb13b1e02017-02-24 15:46:59 -08002734 const struct kpp_testvec *vecs, unsigned int tcount)
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002735{
2736 int ret, i;
2737
2738 for (i = 0; i < tcount; i++) {
2739 ret = do_test_kpp(tfm, vecs++, alg);
2740 if (ret) {
2741 pr_err("alg: %s: test failed on vector %d, err=%d\n",
2742 alg, i + 1, ret);
2743 return ret;
2744 }
2745 }
2746 return 0;
2747}
2748
2749static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
2750 u32 type, u32 mask)
2751{
2752 struct crypto_kpp *tfm;
2753 int err = 0;
2754
Herbert Xueed93e02016-11-22 20:08:31 +08002755 tfm = crypto_alloc_kpp(driver, type, mask);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002756 if (IS_ERR(tfm)) {
2757 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
2758 driver, PTR_ERR(tfm));
2759 return PTR_ERR(tfm);
2760 }
2761 if (desc->suite.kpp.vecs)
2762 err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
2763 desc->suite.kpp.count);
2764
2765 crypto_free_kpp(tfm);
2766 return err;
2767}
2768
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03002769static u8 *test_pack_u32(u8 *dst, u32 val)
2770{
2771 memcpy(dst, &val, sizeof(val));
2772 return dst + sizeof(val);
2773}
2774
Herbert Xu50d2b6432016-06-29 19:32:20 +08002775static int test_akcipher_one(struct crypto_akcipher *tfm,
Eric Biggersb13b1e02017-02-24 15:46:59 -08002776 const struct akcipher_testvec *vecs)
Tadeusz Struk946cc462015-06-16 10:31:06 -07002777{
Herbert Xudf27b262016-05-05 16:42:49 +08002778 char *xbuf[XBUFSIZE];
Tadeusz Struk946cc462015-06-16 10:31:06 -07002779 struct akcipher_request *req;
2780 void *outbuf_enc = NULL;
2781 void *outbuf_dec = NULL;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002782 struct crypto_wait wait;
Tadeusz Struk946cc462015-06-16 10:31:06 -07002783 unsigned int out_len_max, out_len = 0;
2784 int err = -ENOMEM;
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03002785 struct scatterlist src, dst, src_tab[3];
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002786 const char *m, *c;
2787 unsigned int m_size, c_size;
2788 const char *op;
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03002789 u8 *key, *ptr;
Tadeusz Struk946cc462015-06-16 10:31:06 -07002790
Herbert Xudf27b262016-05-05 16:42:49 +08002791 if (testmgr_alloc_buf(xbuf))
2792 return err;
2793
Tadeusz Struk946cc462015-06-16 10:31:06 -07002794 req = akcipher_request_alloc(tfm, GFP_KERNEL);
2795 if (!req)
Herbert Xudf27b262016-05-05 16:42:49 +08002796 goto free_xbuf;
Tadeusz Struk946cc462015-06-16 10:31:06 -07002797
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002798 crypto_init_wait(&wait);
Tadeusz Struk22287b02015-10-08 09:26:55 -07002799
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03002800 key = kmalloc(vecs->key_len + sizeof(u32) * 2 + vecs->param_len,
2801 GFP_KERNEL);
2802 if (!key)
2803 goto free_xbuf;
2804 memcpy(key, vecs->key, vecs->key_len);
2805 ptr = key + vecs->key_len;
2806 ptr = test_pack_u32(ptr, vecs->algo);
2807 ptr = test_pack_u32(ptr, vecs->param_len);
2808 memcpy(ptr, vecs->params, vecs->param_len);
2809
Tadeusz Struk22287b02015-10-08 09:26:55 -07002810 if (vecs->public_key_vec)
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03002811 err = crypto_akcipher_set_pub_key(tfm, key, vecs->key_len);
Tadeusz Struk22287b02015-10-08 09:26:55 -07002812 else
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03002813 err = crypto_akcipher_set_priv_key(tfm, key, vecs->key_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002814 if (err)
2815 goto free_req;
2816
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002817 /*
2818 * First run test which do not require a private key, such as
2819 * encrypt or verify.
2820 */
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03002821 err = -ENOMEM;
2822 out_len_max = crypto_akcipher_maxsize(tfm);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002823 outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
2824 if (!outbuf_enc)
2825 goto free_req;
2826
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002827 if (!vecs->siggen_sigver_test) {
2828 m = vecs->m;
2829 m_size = vecs->m_size;
2830 c = vecs->c;
2831 c_size = vecs->c_size;
2832 op = "encrypt";
2833 } else {
2834 /* Swap args so we could keep plaintext (digest)
2835 * in vecs->m, and cooked signature in vecs->c.
2836 */
2837 m = vecs->c; /* signature */
2838 m_size = vecs->c_size;
2839 c = vecs->m; /* digest */
2840 c_size = vecs->m_size;
2841 op = "verify";
2842 }
Herbert Xudf27b262016-05-05 16:42:49 +08002843
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002844 if (WARN_ON(m_size > PAGE_SIZE))
2845 goto free_all;
2846 memcpy(xbuf[0], m, m_size);
Herbert Xudf27b262016-05-05 16:42:49 +08002847
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03002848 sg_init_table(src_tab, 3);
Herbert Xudf27b262016-05-05 16:42:49 +08002849 sg_set_buf(&src_tab[0], xbuf[0], 8);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002850 sg_set_buf(&src_tab[1], xbuf[0] + 8, m_size - 8);
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03002851 if (vecs->siggen_sigver_test) {
2852 if (WARN_ON(c_size > PAGE_SIZE))
2853 goto free_all;
2854 memcpy(xbuf[1], c, c_size);
2855 sg_set_buf(&src_tab[2], xbuf[1], c_size);
2856 akcipher_request_set_crypt(req, src_tab, NULL, m_size, c_size);
2857 } else {
2858 sg_init_one(&dst, outbuf_enc, out_len_max);
2859 akcipher_request_set_crypt(req, src_tab, &dst, m_size,
2860 out_len_max);
2861 }
Tadeusz Struk946cc462015-06-16 10:31:06 -07002862 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002863 crypto_req_done, &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002864
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002865 err = crypto_wait_req(vecs->siggen_sigver_test ?
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002866 /* Run asymmetric signature verification */
2867 crypto_akcipher_verify(req) :
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002868 /* Run asymmetric encrypt */
2869 crypto_akcipher_encrypt(req), &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002870 if (err) {
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002871 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002872 goto free_all;
2873 }
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03002874 if (!vecs->siggen_sigver_test) {
2875 if (req->dst_len != c_size) {
2876 pr_err("alg: akcipher: %s test failed. Invalid output len\n",
2877 op);
2878 err = -EINVAL;
2879 goto free_all;
2880 }
2881 /* verify that encrypted message is equal to expected */
2882 if (memcmp(c, outbuf_enc, c_size) != 0) {
2883 pr_err("alg: akcipher: %s test failed. Invalid output\n",
2884 op);
2885 hexdump(outbuf_enc, c_size);
2886 err = -EINVAL;
2887 goto free_all;
2888 }
Tadeusz Struk946cc462015-06-16 10:31:06 -07002889 }
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002890
2891 /*
2892 * Don't invoke (decrypt or sign) test which require a private key
2893 * for vectors with only a public key.
2894 */
Tadeusz Struk946cc462015-06-16 10:31:06 -07002895 if (vecs->public_key_vec) {
2896 err = 0;
2897 goto free_all;
2898 }
2899 outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
2900 if (!outbuf_dec) {
2901 err = -ENOMEM;
2902 goto free_all;
2903 }
Herbert Xudf27b262016-05-05 16:42:49 +08002904
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002905 op = vecs->siggen_sigver_test ? "sign" : "decrypt";
2906 if (WARN_ON(c_size > PAGE_SIZE))
Herbert Xudf27b262016-05-05 16:42:49 +08002907 goto free_all;
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002908 memcpy(xbuf[0], c, c_size);
Herbert Xudf27b262016-05-05 16:42:49 +08002909
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002910 sg_init_one(&src, xbuf[0], c_size);
Tadeusz Struk22287b02015-10-08 09:26:55 -07002911 sg_init_one(&dst, outbuf_dec, out_len_max);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002912 crypto_init_wait(&wait);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002913 akcipher_request_set_crypt(req, &src, &dst, c_size, out_len_max);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002914
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002915 err = crypto_wait_req(vecs->siggen_sigver_test ?
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002916 /* Run asymmetric signature generation */
2917 crypto_akcipher_sign(req) :
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002918 /* Run asymmetric decrypt */
2919 crypto_akcipher_decrypt(req), &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002920 if (err) {
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002921 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002922 goto free_all;
2923 }
2924 out_len = req->dst_len;
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002925 if (out_len < m_size) {
2926 pr_err("alg: akcipher: %s test failed. Invalid output len %u\n",
2927 op, out_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002928 err = -EINVAL;
2929 goto free_all;
2930 }
2931 /* verify that decrypted message is equal to the original msg */
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002932 if (memchr_inv(outbuf_dec, 0, out_len - m_size) ||
2933 memcmp(m, outbuf_dec + out_len - m_size, m_size)) {
2934 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
Herbert Xu50d2b6432016-06-29 19:32:20 +08002935 hexdump(outbuf_dec, out_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002936 err = -EINVAL;
2937 }
2938free_all:
2939 kfree(outbuf_dec);
2940 kfree(outbuf_enc);
2941free_req:
2942 akcipher_request_free(req);
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03002943 kfree(key);
Herbert Xudf27b262016-05-05 16:42:49 +08002944free_xbuf:
2945 testmgr_free_buf(xbuf);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002946 return err;
2947}
2948
Herbert Xu50d2b6432016-06-29 19:32:20 +08002949static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
Eric Biggersb13b1e02017-02-24 15:46:59 -08002950 const struct akcipher_testvec *vecs,
2951 unsigned int tcount)
Tadeusz Struk946cc462015-06-16 10:31:06 -07002952{
Herbert Xu15226e42016-07-18 18:20:10 +08002953 const char *algo =
2954 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
Tadeusz Struk946cc462015-06-16 10:31:06 -07002955 int ret, i;
2956
2957 for (i = 0; i < tcount; i++) {
Herbert Xu50d2b6432016-06-29 19:32:20 +08002958 ret = test_akcipher_one(tfm, vecs++);
2959 if (!ret)
2960 continue;
2961
Herbert Xu15226e42016-07-18 18:20:10 +08002962 pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
2963 i + 1, algo, ret);
Herbert Xu50d2b6432016-06-29 19:32:20 +08002964 return ret;
Tadeusz Struk946cc462015-06-16 10:31:06 -07002965 }
2966 return 0;
2967}
2968
Tadeusz Struk946cc462015-06-16 10:31:06 -07002969static int alg_test_akcipher(const struct alg_test_desc *desc,
2970 const char *driver, u32 type, u32 mask)
2971{
2972 struct crypto_akcipher *tfm;
2973 int err = 0;
2974
Herbert Xueed93e02016-11-22 20:08:31 +08002975 tfm = crypto_alloc_akcipher(driver, type, mask);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002976 if (IS_ERR(tfm)) {
2977 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
2978 driver, PTR_ERR(tfm));
2979 return PTR_ERR(tfm);
2980 }
2981 if (desc->suite.akcipher.vecs)
2982 err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
2983 desc->suite.akcipher.count);
2984
2985 crypto_free_akcipher(tfm);
2986 return err;
2987}
2988
Youquan, Song863b5572009-12-23 19:45:20 +08002989static int alg_test_null(const struct alg_test_desc *desc,
2990 const char *driver, u32 type, u32 mask)
2991{
2992 return 0;
2993}
2994
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002995#define __VECS(tv) { .vecs = tv, .count = ARRAY_SIZE(tv) }
2996
Herbert Xuda7f0332008-07-31 17:08:25 +08002997/* Please keep this list sorted by algorithm name. */
2998static const struct alg_test_desc alg_test_descs[] = {
2999 {
Eric Biggers059c2a42018-11-16 17:26:31 -08003000 .alg = "adiantum(xchacha12,aes)",
3001 .test = alg_test_skcipher,
3002 .suite = {
3003 .cipher = __VECS(adiantum_xchacha12_aes_tv_template)
3004 },
3005 }, {
3006 .alg = "adiantum(xchacha20,aes)",
3007 .test = alg_test_skcipher,
3008 .suite = {
3009 .cipher = __VECS(adiantum_xchacha20_aes_tv_template)
3010 },
3011 }, {
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02003012 .alg = "aegis128",
3013 .test = alg_test_aead,
3014 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003015 .aead = __VECS(aegis128_tv_template)
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02003016 }
3017 }, {
3018 .alg = "aegis128l",
3019 .test = alg_test_aead,
3020 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003021 .aead = __VECS(aegis128l_tv_template)
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02003022 }
3023 }, {
3024 .alg = "aegis256",
3025 .test = alg_test_aead,
3026 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003027 .aead = __VECS(aegis256_tv_template)
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02003028 }
3029 }, {
Jarod Wilsone08ca2d2009-05-04 19:46:29 +08003030 .alg = "ansi_cprng",
3031 .test = alg_test_cprng,
3032 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003033 .cprng = __VECS(ansi_cprng_aes_tv_template)
Jarod Wilsone08ca2d2009-05-04 19:46:29 +08003034 }
3035 }, {
Horia Geantabca4feb2014-03-14 17:46:51 +02003036 .alg = "authenc(hmac(md5),ecb(cipher_null))",
3037 .test = alg_test_aead,
Horia Geantabca4feb2014-03-14 17:46:51 +02003038 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003039 .aead = __VECS(hmac_md5_ecb_cipher_null_tv_template)
Horia Geantabca4feb2014-03-14 17:46:51 +02003040 }
3041 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003042 .alg = "authenc(hmac(sha1),cbc(aes))",
Horia Geantae46e9a42012-07-03 19:16:54 +03003043 .test = alg_test_aead,
Herbert Xubcf741c2017-06-28 19:09:07 +08003044 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03003045 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003046 .aead = __VECS(hmac_sha1_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303047 }
3048 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003049 .alg = "authenc(hmac(sha1),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303050 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303051 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003052 .aead = __VECS(hmac_sha1_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303053 }
3054 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003055 .alg = "authenc(hmac(sha1),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303056 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01003057 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303058 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003059 .aead = __VECS(hmac_sha1_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03003060 }
3061 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01003062 .alg = "authenc(hmac(sha1),ctr(aes))",
3063 .test = alg_test_null,
3064 .fips_allowed = 1,
3065 }, {
Horia Geantabca4feb2014-03-14 17:46:51 +02003066 .alg = "authenc(hmac(sha1),ecb(cipher_null))",
3067 .test = alg_test_aead,
Horia Geantabca4feb2014-03-14 17:46:51 +02003068 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003069 .aead = __VECS(hmac_sha1_ecb_cipher_null_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303070 }
3071 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01003072 .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
3073 .test = alg_test_null,
3074 .fips_allowed = 1,
3075 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003076 .alg = "authenc(hmac(sha224),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303077 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303078 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003079 .aead = __VECS(hmac_sha224_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303080 }
3081 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003082 .alg = "authenc(hmac(sha224),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303083 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01003084 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303085 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003086 .aead = __VECS(hmac_sha224_des3_ede_cbc_tv_temp)
Horia Geantabca4feb2014-03-14 17:46:51 +02003087 }
3088 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003089 .alg = "authenc(hmac(sha256),cbc(aes))",
Horia Geantae46e9a42012-07-03 19:16:54 +03003090 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01003091 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03003092 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003093 .aead = __VECS(hmac_sha256_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303094 }
3095 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003096 .alg = "authenc(hmac(sha256),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303097 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303098 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003099 .aead = __VECS(hmac_sha256_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303100 }
3101 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003102 .alg = "authenc(hmac(sha256),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303103 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01003104 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303105 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003106 .aead = __VECS(hmac_sha256_des3_ede_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303107 }
3108 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01003109 .alg = "authenc(hmac(sha256),ctr(aes))",
3110 .test = alg_test_null,
3111 .fips_allowed = 1,
3112 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01003113 .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
3114 .test = alg_test_null,
3115 .fips_allowed = 1,
3116 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003117 .alg = "authenc(hmac(sha384),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303118 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303119 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003120 .aead = __VECS(hmac_sha384_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303121 }
3122 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003123 .alg = "authenc(hmac(sha384),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303124 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01003125 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303126 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003127 .aead = __VECS(hmac_sha384_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03003128 }
3129 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01003130 .alg = "authenc(hmac(sha384),ctr(aes))",
3131 .test = alg_test_null,
3132 .fips_allowed = 1,
3133 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01003134 .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
3135 .test = alg_test_null,
3136 .fips_allowed = 1,
3137 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003138 .alg = "authenc(hmac(sha512),cbc(aes))",
Marcus Meissnered1afac2016-02-05 14:23:33 +01003139 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03003140 .test = alg_test_aead,
Horia Geantae46e9a42012-07-03 19:16:54 +03003141 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003142 .aead = __VECS(hmac_sha512_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303143 }
3144 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003145 .alg = "authenc(hmac(sha512),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303146 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303147 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003148 .aead = __VECS(hmac_sha512_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303149 }
3150 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003151 .alg = "authenc(hmac(sha512),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303152 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01003153 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303154 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003155 .aead = __VECS(hmac_sha512_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03003156 }
3157 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01003158 .alg = "authenc(hmac(sha512),ctr(aes))",
3159 .test = alg_test_null,
3160 .fips_allowed = 1,
3161 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01003162 .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
3163 .test = alg_test_null,
3164 .fips_allowed = 1,
3165 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003166 .alg = "cbc(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003167 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003168 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003169 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003170 .cipher = __VECS(aes_cbc_tv_template)
3171 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003172 }, {
3173 .alg = "cbc(anubis)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003174 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003175 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003176 .cipher = __VECS(anubis_cbc_tv_template)
3177 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003178 }, {
3179 .alg = "cbc(blowfish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003180 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003181 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003182 .cipher = __VECS(bf_cbc_tv_template)
3183 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003184 }, {
3185 .alg = "cbc(camellia)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003186 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003187 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003188 .cipher = __VECS(camellia_cbc_tv_template)
3189 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003190 }, {
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02003191 .alg = "cbc(cast5)",
3192 .test = alg_test_skcipher,
3193 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003194 .cipher = __VECS(cast5_cbc_tv_template)
3195 },
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02003196 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003197 .alg = "cbc(cast6)",
3198 .test = alg_test_skcipher,
3199 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003200 .cipher = __VECS(cast6_cbc_tv_template)
3201 },
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003202 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003203 .alg = "cbc(des)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003204 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003205 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003206 .cipher = __VECS(des_cbc_tv_template)
3207 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003208 }, {
3209 .alg = "cbc(des3_ede)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003210 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003211 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003212 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003213 .cipher = __VECS(des3_ede_cbc_tv_template)
3214 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003215 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01003216 /* Same as cbc(aes) except the key is stored in
3217 * hardware secure memory which we reference by index
3218 */
3219 .alg = "cbc(paes)",
3220 .test = alg_test_null,
3221 .fips_allowed = 1,
3222 }, {
Jussi Kivilinna9d259172011-10-18 00:02:53 +03003223 .alg = "cbc(serpent)",
3224 .test = alg_test_skcipher,
3225 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003226 .cipher = __VECS(serpent_cbc_tv_template)
3227 },
Jussi Kivilinna9d259172011-10-18 00:02:53 +03003228 }, {
Gilad Ben-Yossef95ba5972018-09-20 14:18:38 +01003229 .alg = "cbc(sm4)",
3230 .test = alg_test_skcipher,
3231 .suite = {
3232 .cipher = __VECS(sm4_cbc_tv_template)
3233 }
3234 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003235 .alg = "cbc(twofish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003236 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003237 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003238 .cipher = __VECS(tf_cbc_tv_template)
3239 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003240 }, {
Ard Biesheuvel092acf02017-02-03 14:49:35 +00003241 .alg = "cbcmac(aes)",
3242 .fips_allowed = 1,
3243 .test = alg_test_hash,
3244 .suite = {
3245 .hash = __VECS(aes_cbcmac_tv_template)
3246 }
3247 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003248 .alg = "ccm(aes)",
3249 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003250 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003251 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003252 .aead = __VECS(aes_ccm_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003253 }
3254 }, {
Dmitry Eremin-Solenikov7da66672018-10-20 02:01:53 +03003255 .alg = "cfb(aes)",
3256 .test = alg_test_skcipher,
3257 .fips_allowed = 1,
3258 .suite = {
3259 .cipher = __VECS(aes_cfb_tv_template)
3260 },
3261 }, {
Martin Willi3590ebf2015-06-01 13:43:57 +02003262 .alg = "chacha20",
3263 .test = alg_test_skcipher,
3264 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003265 .cipher = __VECS(chacha20_tv_template)
3266 },
Martin Willi3590ebf2015-06-01 13:43:57 +02003267 }, {
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03003268 .alg = "cmac(aes)",
Stephan Mueller8f183752015-08-19 08:42:07 +02003269 .fips_allowed = 1,
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03003270 .test = alg_test_hash,
3271 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003272 .hash = __VECS(aes_cmac128_tv_template)
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03003273 }
3274 }, {
3275 .alg = "cmac(des3_ede)",
Stephan Mueller8f183752015-08-19 08:42:07 +02003276 .fips_allowed = 1,
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03003277 .test = alg_test_hash,
3278 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003279 .hash = __VECS(des3_ede_cmac64_tv_template)
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03003280 }
3281 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03003282 .alg = "compress_null",
3283 .test = alg_test_null,
3284 }, {
Ard Biesheuvelebb34722015-05-04 11:00:17 +02003285 .alg = "crc32",
3286 .test = alg_test_hash,
Milan Broza8a34412019-01-25 10:31:47 +01003287 .fips_allowed = 1,
Ard Biesheuvelebb34722015-05-04 11:00:17 +02003288 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003289 .hash = __VECS(crc32_tv_template)
Ard Biesheuvelebb34722015-05-04 11:00:17 +02003290 }
3291 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003292 .alg = "crc32c",
Herbert Xu8e3ee852008-11-07 14:58:52 +08003293 .test = alg_test_crc32c,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003294 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003295 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003296 .hash = __VECS(crc32c_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003297 }
3298 }, {
Herbert Xu684115212013-09-07 12:56:26 +10003299 .alg = "crct10dif",
3300 .test = alg_test_hash,
3301 .fips_allowed = 1,
3302 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003303 .hash = __VECS(crct10dif_tv_template)
Herbert Xu684115212013-09-07 12:56:26 +10003304 }
3305 }, {
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08003306 .alg = "ctr(aes)",
3307 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003308 .fips_allowed = 1,
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08003309 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003310 .cipher = __VECS(aes_ctr_tv_template)
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08003311 }
3312 }, {
Jussi Kivilinna85b63e32011-10-10 23:03:03 +03003313 .alg = "ctr(blowfish)",
3314 .test = alg_test_skcipher,
3315 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003316 .cipher = __VECS(bf_ctr_tv_template)
Jussi Kivilinna85b63e32011-10-10 23:03:03 +03003317 }
3318 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02003319 .alg = "ctr(camellia)",
3320 .test = alg_test_skcipher,
3321 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003322 .cipher = __VECS(camellia_ctr_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02003323 }
3324 }, {
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02003325 .alg = "ctr(cast5)",
3326 .test = alg_test_skcipher,
3327 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003328 .cipher = __VECS(cast5_ctr_tv_template)
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02003329 }
3330 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003331 .alg = "ctr(cast6)",
3332 .test = alg_test_skcipher,
3333 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003334 .cipher = __VECS(cast6_ctr_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003335 }
3336 }, {
Jussi Kivilinna8163fc32012-10-20 14:53:07 +03003337 .alg = "ctr(des)",
3338 .test = alg_test_skcipher,
3339 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003340 .cipher = __VECS(des_ctr_tv_template)
Jussi Kivilinna8163fc32012-10-20 14:53:07 +03003341 }
3342 }, {
Jussi Kivilinnae080b172012-10-20 14:53:12 +03003343 .alg = "ctr(des3_ede)",
3344 .test = alg_test_skcipher,
Marcelo Cerri0d8da102017-03-20 17:28:05 -03003345 .fips_allowed = 1,
Jussi Kivilinnae080b172012-10-20 14:53:12 +03003346 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003347 .cipher = __VECS(des3_ede_ctr_tv_template)
Jussi Kivilinnae080b172012-10-20 14:53:12 +03003348 }
3349 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01003350 /* Same as ctr(aes) except the key is stored in
3351 * hardware secure memory which we reference by index
3352 */
3353 .alg = "ctr(paes)",
3354 .test = alg_test_null,
3355 .fips_allowed = 1,
3356 }, {
Jussi Kivilinna9d259172011-10-18 00:02:53 +03003357 .alg = "ctr(serpent)",
3358 .test = alg_test_skcipher,
3359 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003360 .cipher = __VECS(serpent_ctr_tv_template)
Jussi Kivilinna9d259172011-10-18 00:02:53 +03003361 }
3362 }, {
Gilad Ben-Yossef95ba5972018-09-20 14:18:38 +01003363 .alg = "ctr(sm4)",
3364 .test = alg_test_skcipher,
3365 .suite = {
3366 .cipher = __VECS(sm4_ctr_tv_template)
3367 }
3368 }, {
Jussi Kivilinna573da622011-10-10 23:03:12 +03003369 .alg = "ctr(twofish)",
3370 .test = alg_test_skcipher,
3371 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003372 .cipher = __VECS(tf_ctr_tv_template)
Jussi Kivilinna573da622011-10-10 23:03:12 +03003373 }
3374 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003375 .alg = "cts(cbc(aes))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003376 .test = alg_test_skcipher,
Gilad Ben-Yossef196ad602018-11-04 10:05:24 +00003377 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003378 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003379 .cipher = __VECS(cts_mode_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003380 }
3381 }, {
3382 .alg = "deflate",
3383 .test = alg_test_comp,
Milan Broz08189042012-12-06 17:16:28 +08003384 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003385 .suite = {
3386 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003387 .comp = __VECS(deflate_comp_tv_template),
3388 .decomp = __VECS(deflate_decomp_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003389 }
3390 }
3391 }, {
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003392 .alg = "dh",
3393 .test = alg_test_kpp,
3394 .fips_allowed = 1,
3395 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003396 .kpp = __VECS(dh_tv_template)
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003397 }
3398 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03003399 .alg = "digest_null",
3400 .test = alg_test_null,
3401 }, {
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003402 .alg = "drbg_nopr_ctr_aes128",
3403 .test = alg_test_drbg,
3404 .fips_allowed = 1,
3405 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003406 .drbg = __VECS(drbg_nopr_ctr_aes128_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003407 }
3408 }, {
3409 .alg = "drbg_nopr_ctr_aes192",
3410 .test = alg_test_drbg,
3411 .fips_allowed = 1,
3412 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003413 .drbg = __VECS(drbg_nopr_ctr_aes192_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003414 }
3415 }, {
3416 .alg = "drbg_nopr_ctr_aes256",
3417 .test = alg_test_drbg,
3418 .fips_allowed = 1,
3419 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003420 .drbg = __VECS(drbg_nopr_ctr_aes256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003421 }
3422 }, {
3423 /*
3424 * There is no need to specifically test the DRBG with every
3425 * backend cipher -- covered by drbg_nopr_hmac_sha256 test
3426 */
3427 .alg = "drbg_nopr_hmac_sha1",
3428 .fips_allowed = 1,
3429 .test = alg_test_null,
3430 }, {
3431 .alg = "drbg_nopr_hmac_sha256",
3432 .test = alg_test_drbg,
3433 .fips_allowed = 1,
3434 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003435 .drbg = __VECS(drbg_nopr_hmac_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003436 }
3437 }, {
3438 /* covered by drbg_nopr_hmac_sha256 test */
3439 .alg = "drbg_nopr_hmac_sha384",
3440 .fips_allowed = 1,
3441 .test = alg_test_null,
3442 }, {
3443 .alg = "drbg_nopr_hmac_sha512",
3444 .test = alg_test_null,
3445 .fips_allowed = 1,
3446 }, {
3447 .alg = "drbg_nopr_sha1",
3448 .fips_allowed = 1,
3449 .test = alg_test_null,
3450 }, {
3451 .alg = "drbg_nopr_sha256",
3452 .test = alg_test_drbg,
3453 .fips_allowed = 1,
3454 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003455 .drbg = __VECS(drbg_nopr_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003456 }
3457 }, {
3458 /* covered by drbg_nopr_sha256 test */
3459 .alg = "drbg_nopr_sha384",
3460 .fips_allowed = 1,
3461 .test = alg_test_null,
3462 }, {
3463 .alg = "drbg_nopr_sha512",
3464 .fips_allowed = 1,
3465 .test = alg_test_null,
3466 }, {
3467 .alg = "drbg_pr_ctr_aes128",
3468 .test = alg_test_drbg,
3469 .fips_allowed = 1,
3470 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003471 .drbg = __VECS(drbg_pr_ctr_aes128_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003472 }
3473 }, {
3474 /* covered by drbg_pr_ctr_aes128 test */
3475 .alg = "drbg_pr_ctr_aes192",
3476 .fips_allowed = 1,
3477 .test = alg_test_null,
3478 }, {
3479 .alg = "drbg_pr_ctr_aes256",
3480 .fips_allowed = 1,
3481 .test = alg_test_null,
3482 }, {
3483 .alg = "drbg_pr_hmac_sha1",
3484 .fips_allowed = 1,
3485 .test = alg_test_null,
3486 }, {
3487 .alg = "drbg_pr_hmac_sha256",
3488 .test = alg_test_drbg,
3489 .fips_allowed = 1,
3490 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003491 .drbg = __VECS(drbg_pr_hmac_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003492 }
3493 }, {
3494 /* covered by drbg_pr_hmac_sha256 test */
3495 .alg = "drbg_pr_hmac_sha384",
3496 .fips_allowed = 1,
3497 .test = alg_test_null,
3498 }, {
3499 .alg = "drbg_pr_hmac_sha512",
3500 .test = alg_test_null,
3501 .fips_allowed = 1,
3502 }, {
3503 .alg = "drbg_pr_sha1",
3504 .fips_allowed = 1,
3505 .test = alg_test_null,
3506 }, {
3507 .alg = "drbg_pr_sha256",
3508 .test = alg_test_drbg,
3509 .fips_allowed = 1,
3510 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003511 .drbg = __VECS(drbg_pr_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003512 }
3513 }, {
3514 /* covered by drbg_pr_sha256 test */
3515 .alg = "drbg_pr_sha384",
3516 .fips_allowed = 1,
3517 .test = alg_test_null,
3518 }, {
3519 .alg = "drbg_pr_sha512",
3520 .fips_allowed = 1,
3521 .test = alg_test_null,
3522 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003523 .alg = "ecb(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003524 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003525 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003526 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003527 .cipher = __VECS(aes_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003528 }
3529 }, {
3530 .alg = "ecb(anubis)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003531 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003532 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003533 .cipher = __VECS(anubis_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003534 }
3535 }, {
3536 .alg = "ecb(arc4)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003537 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003538 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003539 .cipher = __VECS(arc4_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003540 }
3541 }, {
3542 .alg = "ecb(blowfish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003543 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003544 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003545 .cipher = __VECS(bf_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003546 }
3547 }, {
3548 .alg = "ecb(camellia)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003549 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003550 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003551 .cipher = __VECS(camellia_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003552 }
3553 }, {
3554 .alg = "ecb(cast5)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003555 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003556 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003557 .cipher = __VECS(cast5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003558 }
3559 }, {
3560 .alg = "ecb(cast6)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003561 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003562 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003563 .cipher = __VECS(cast6_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003564 }
3565 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03003566 .alg = "ecb(cipher_null)",
3567 .test = alg_test_null,
Milan Broz6175ca22017-04-21 13:03:06 +02003568 .fips_allowed = 1,
Jussi Kivilinnae4483702013-04-07 16:43:56 +03003569 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003570 .alg = "ecb(des)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003571 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003572 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003573 .cipher = __VECS(des_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003574 }
3575 }, {
3576 .alg = "ecb(des3_ede)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003577 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003578 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003579 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003580 .cipher = __VECS(des3_ede_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003581 }
3582 }, {
Jussi Kivilinna66e5bd02013-01-19 13:31:36 +02003583 .alg = "ecb(fcrypt)",
3584 .test = alg_test_skcipher,
3585 .suite = {
3586 .cipher = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003587 .vecs = fcrypt_pcbc_tv_template,
3588 .count = 1
Jussi Kivilinna66e5bd02013-01-19 13:31:36 +02003589 }
3590 }
3591 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003592 .alg = "ecb(khazad)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003593 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003594 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003595 .cipher = __VECS(khazad_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003596 }
3597 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01003598 /* Same as ecb(aes) except the key is stored in
3599 * hardware secure memory which we reference by index
3600 */
3601 .alg = "ecb(paes)",
3602 .test = alg_test_null,
3603 .fips_allowed = 1,
3604 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003605 .alg = "ecb(seed)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003606 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003607 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003608 .cipher = __VECS(seed_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003609 }
3610 }, {
3611 .alg = "ecb(serpent)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003612 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003613 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003614 .cipher = __VECS(serpent_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003615 }
3616 }, {
Gilad Ben-Yossefcd83a8a2018-03-06 09:44:43 +00003617 .alg = "ecb(sm4)",
3618 .test = alg_test_skcipher,
3619 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003620 .cipher = __VECS(sm4_tv_template)
Gilad Ben-Yossefcd83a8a2018-03-06 09:44:43 +00003621 }
3622 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003623 .alg = "ecb(tea)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003624 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003625 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003626 .cipher = __VECS(tea_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003627 }
3628 }, {
3629 .alg = "ecb(tnepres)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003630 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003631 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003632 .cipher = __VECS(tnepres_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003633 }
3634 }, {
3635 .alg = "ecb(twofish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003636 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003637 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003638 .cipher = __VECS(tf_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003639 }
3640 }, {
3641 .alg = "ecb(xeta)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003642 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003643 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003644 .cipher = __VECS(xeta_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003645 }
3646 }, {
3647 .alg = "ecb(xtea)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003648 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003649 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003650 .cipher = __VECS(xtea_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003651 }
3652 }, {
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +01003653 .alg = "ecdh",
3654 .test = alg_test_kpp,
3655 .fips_allowed = 1,
3656 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003657 .kpp = __VECS(ecdh_tv_template)
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +01003658 }
3659 }, {
Vitaly Chikunov32fbdbd2019-04-11 18:51:21 +03003660 .alg = "ecrdsa",
3661 .test = alg_test_akcipher,
3662 .suite = {
3663 .akcipher = __VECS(ecrdsa_tv_template)
3664 }
3665 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003666 .alg = "gcm(aes)",
3667 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003668 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003669 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003670 .aead = __VECS(aes_gcm_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003671 }
3672 }, {
Youquan, Song507069c2009-11-23 20:23:04 +08003673 .alg = "ghash",
3674 .test = alg_test_hash,
Jarod Wilson18c0ebd2011-01-29 15:14:35 +11003675 .fips_allowed = 1,
Youquan, Song507069c2009-11-23 20:23:04 +08003676 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003677 .hash = __VECS(ghash_tv_template)
Youquan, Song507069c2009-11-23 20:23:04 +08003678 }
3679 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003680 .alg = "hmac(md5)",
3681 .test = alg_test_hash,
3682 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003683 .hash = __VECS(hmac_md5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003684 }
3685 }, {
3686 .alg = "hmac(rmd128)",
3687 .test = alg_test_hash,
3688 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003689 .hash = __VECS(hmac_rmd128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003690 }
3691 }, {
3692 .alg = "hmac(rmd160)",
3693 .test = alg_test_hash,
3694 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003695 .hash = __VECS(hmac_rmd160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003696 }
3697 }, {
3698 .alg = "hmac(sha1)",
3699 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003700 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003701 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003702 .hash = __VECS(hmac_sha1_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003703 }
3704 }, {
3705 .alg = "hmac(sha224)",
3706 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003707 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003708 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003709 .hash = __VECS(hmac_sha224_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003710 }
3711 }, {
3712 .alg = "hmac(sha256)",
3713 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003714 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003715 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003716 .hash = __VECS(hmac_sha256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003717 }
3718 }, {
raveendra padasalagi98eca722016-07-01 11:16:54 +05303719 .alg = "hmac(sha3-224)",
3720 .test = alg_test_hash,
3721 .fips_allowed = 1,
3722 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003723 .hash = __VECS(hmac_sha3_224_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05303724 }
3725 }, {
3726 .alg = "hmac(sha3-256)",
3727 .test = alg_test_hash,
3728 .fips_allowed = 1,
3729 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003730 .hash = __VECS(hmac_sha3_256_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05303731 }
3732 }, {
3733 .alg = "hmac(sha3-384)",
3734 .test = alg_test_hash,
3735 .fips_allowed = 1,
3736 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003737 .hash = __VECS(hmac_sha3_384_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05303738 }
3739 }, {
3740 .alg = "hmac(sha3-512)",
3741 .test = alg_test_hash,
3742 .fips_allowed = 1,
3743 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003744 .hash = __VECS(hmac_sha3_512_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05303745 }
3746 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003747 .alg = "hmac(sha384)",
3748 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003749 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003750 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003751 .hash = __VECS(hmac_sha384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003752 }
3753 }, {
3754 .alg = "hmac(sha512)",
3755 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003756 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003757 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003758 .hash = __VECS(hmac_sha512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003759 }
3760 }, {
Vitaly Chikunov25a0b9d2018-11-07 00:00:03 +03003761 .alg = "hmac(streebog256)",
3762 .test = alg_test_hash,
3763 .suite = {
3764 .hash = __VECS(hmac_streebog256_tv_template)
3765 }
3766 }, {
3767 .alg = "hmac(streebog512)",
3768 .test = alg_test_hash,
3769 .suite = {
3770 .hash = __VECS(hmac_streebog512_tv_template)
3771 }
3772 }, {
Stephan Muellerbb5530e2015-05-25 15:10:20 +02003773 .alg = "jitterentropy_rng",
3774 .fips_allowed = 1,
3775 .test = alg_test_null,
3776 }, {
Stephan Mueller35351982015-09-21 20:59:56 +02003777 .alg = "kw(aes)",
3778 .test = alg_test_skcipher,
3779 .fips_allowed = 1,
3780 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003781 .cipher = __VECS(aes_kw_tv_template)
Stephan Mueller35351982015-09-21 20:59:56 +02003782 }
3783 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003784 .alg = "lrw(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003785 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003786 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003787 .cipher = __VECS(aes_lrw_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003788 }
3789 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02003790 .alg = "lrw(camellia)",
3791 .test = alg_test_skcipher,
3792 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003793 .cipher = __VECS(camellia_lrw_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02003794 }
3795 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003796 .alg = "lrw(cast6)",
3797 .test = alg_test_skcipher,
3798 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003799 .cipher = __VECS(cast6_lrw_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003800 }
3801 }, {
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03003802 .alg = "lrw(serpent)",
3803 .test = alg_test_skcipher,
3804 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003805 .cipher = __VECS(serpent_lrw_tv_template)
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03003806 }
3807 }, {
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03003808 .alg = "lrw(twofish)",
3809 .test = alg_test_skcipher,
3810 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003811 .cipher = __VECS(tf_lrw_tv_template)
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03003812 }
3813 }, {
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02003814 .alg = "lz4",
3815 .test = alg_test_comp,
3816 .fips_allowed = 1,
3817 .suite = {
3818 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003819 .comp = __VECS(lz4_comp_tv_template),
3820 .decomp = __VECS(lz4_decomp_tv_template)
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02003821 }
3822 }
3823 }, {
3824 .alg = "lz4hc",
3825 .test = alg_test_comp,
3826 .fips_allowed = 1,
3827 .suite = {
3828 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003829 .comp = __VECS(lz4hc_comp_tv_template),
3830 .decomp = __VECS(lz4hc_decomp_tv_template)
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02003831 }
3832 }
3833 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003834 .alg = "lzo",
3835 .test = alg_test_comp,
Milan Broz08189042012-12-06 17:16:28 +08003836 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003837 .suite = {
3838 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003839 .comp = __VECS(lzo_comp_tv_template),
3840 .decomp = __VECS(lzo_decomp_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003841 }
3842 }
3843 }, {
3844 .alg = "md4",
3845 .test = alg_test_hash,
3846 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003847 .hash = __VECS(md4_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003848 }
3849 }, {
3850 .alg = "md5",
3851 .test = alg_test_hash,
3852 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003853 .hash = __VECS(md5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003854 }
3855 }, {
3856 .alg = "michael_mic",
3857 .test = alg_test_hash,
3858 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003859 .hash = __VECS(michael_mic_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003860 }
3861 }, {
Ondrej Mosnacek4feb4c52018-05-11 14:19:10 +02003862 .alg = "morus1280",
3863 .test = alg_test_aead,
3864 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003865 .aead = __VECS(morus1280_tv_template)
Ondrej Mosnacek4feb4c52018-05-11 14:19:10 +02003866 }
3867 }, {
3868 .alg = "morus640",
3869 .test = alg_test_aead,
3870 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003871 .aead = __VECS(morus640_tv_template)
Ondrej Mosnacek4feb4c52018-05-11 14:19:10 +02003872 }
3873 }, {
Eric Biggers26609a22018-11-16 17:26:29 -08003874 .alg = "nhpoly1305",
3875 .test = alg_test_hash,
3876 .suite = {
3877 .hash = __VECS(nhpoly1305_tv_template)
3878 }
3879 }, {
Puneet Saxenaba0e14a2011-05-04 15:04:10 +10003880 .alg = "ofb(aes)",
3881 .test = alg_test_skcipher,
3882 .fips_allowed = 1,
3883 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003884 .cipher = __VECS(aes_ofb_tv_template)
Puneet Saxenaba0e14a2011-05-04 15:04:10 +10003885 }
3886 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01003887 /* Same as ofb(aes) except the key is stored in
3888 * hardware secure memory which we reference by index
3889 */
3890 .alg = "ofb(paes)",
3891 .test = alg_test_null,
3892 .fips_allowed = 1,
3893 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003894 .alg = "pcbc(fcrypt)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003895 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003896 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003897 .cipher = __VECS(fcrypt_pcbc_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003898 }
3899 }, {
Stephan Mueller12071072017-06-12 23:27:51 +02003900 .alg = "pkcs1pad(rsa,sha224)",
3901 .test = alg_test_null,
3902 .fips_allowed = 1,
3903 }, {
3904 .alg = "pkcs1pad(rsa,sha256)",
3905 .test = alg_test_akcipher,
3906 .fips_allowed = 1,
3907 .suite = {
3908 .akcipher = __VECS(pkcs1pad_rsa_tv_template)
3909 }
3910 }, {
3911 .alg = "pkcs1pad(rsa,sha384)",
3912 .test = alg_test_null,
3913 .fips_allowed = 1,
3914 }, {
3915 .alg = "pkcs1pad(rsa,sha512)",
3916 .test = alg_test_null,
3917 .fips_allowed = 1,
3918 }, {
Martin Willieee9dc62015-06-01 13:43:59 +02003919 .alg = "poly1305",
3920 .test = alg_test_hash,
3921 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003922 .hash = __VECS(poly1305_tv_template)
Martin Willieee9dc62015-06-01 13:43:59 +02003923 }
3924 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003925 .alg = "rfc3686(ctr(aes))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003926 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003927 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003928 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003929 .cipher = __VECS(aes_ctr_rfc3686_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003930 }
3931 }, {
Herbert Xu3f31a742015-07-09 07:17:34 +08003932 .alg = "rfc4106(gcm(aes))",
Adrian Hoban69435b92010-11-04 15:02:04 -04003933 .test = alg_test_aead,
Jarod Wilsondb71f29a2015-01-23 12:42:15 -05003934 .fips_allowed = 1,
Adrian Hoban69435b92010-11-04 15:02:04 -04003935 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003936 .aead = __VECS(aes_gcm_rfc4106_tv_template)
Adrian Hoban69435b92010-11-04 15:02:04 -04003937 }
3938 }, {
Herbert Xu544c4362015-07-14 16:53:22 +08003939 .alg = "rfc4309(ccm(aes))",
Jarod Wilson5d667322009-05-04 19:23:40 +08003940 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003941 .fips_allowed = 1,
Jarod Wilson5d667322009-05-04 19:23:40 +08003942 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003943 .aead = __VECS(aes_ccm_rfc4309_tv_template)
Jarod Wilson5d667322009-05-04 19:23:40 +08003944 }
3945 }, {
Herbert Xubb687452015-06-16 13:54:24 +08003946 .alg = "rfc4543(gcm(aes))",
Jussi Kivilinnae9b74412013-04-07 16:43:51 +03003947 .test = alg_test_aead,
3948 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003949 .aead = __VECS(aes_gcm_rfc4543_tv_template)
Jussi Kivilinnae9b74412013-04-07 16:43:51 +03003950 }
3951 }, {
Martin Williaf2b76b2015-06-01 13:44:01 +02003952 .alg = "rfc7539(chacha20,poly1305)",
3953 .test = alg_test_aead,
3954 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003955 .aead = __VECS(rfc7539_tv_template)
Martin Williaf2b76b2015-06-01 13:44:01 +02003956 }
3957 }, {
Martin Willi59007582015-06-01 13:44:03 +02003958 .alg = "rfc7539esp(chacha20,poly1305)",
3959 .test = alg_test_aead,
3960 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003961 .aead = __VECS(rfc7539esp_tv_template)
Martin Willi59007582015-06-01 13:44:03 +02003962 }
3963 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003964 .alg = "rmd128",
3965 .test = alg_test_hash,
3966 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003967 .hash = __VECS(rmd128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003968 }
3969 }, {
3970 .alg = "rmd160",
3971 .test = alg_test_hash,
3972 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003973 .hash = __VECS(rmd160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003974 }
3975 }, {
3976 .alg = "rmd256",
3977 .test = alg_test_hash,
3978 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003979 .hash = __VECS(rmd256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003980 }
3981 }, {
3982 .alg = "rmd320",
3983 .test = alg_test_hash,
3984 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003985 .hash = __VECS(rmd320_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003986 }
3987 }, {
Tadeusz Struk946cc462015-06-16 10:31:06 -07003988 .alg = "rsa",
3989 .test = alg_test_akcipher,
3990 .fips_allowed = 1,
3991 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003992 .akcipher = __VECS(rsa_tv_template)
Tadeusz Struk946cc462015-06-16 10:31:06 -07003993 }
3994 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003995 .alg = "salsa20",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003996 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003997 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003998 .cipher = __VECS(salsa20_stream_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003999 }
4000 }, {
4001 .alg = "sha1",
4002 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004003 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004004 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004005 .hash = __VECS(sha1_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004006 }
4007 }, {
4008 .alg = "sha224",
4009 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004010 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004011 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004012 .hash = __VECS(sha224_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004013 }
4014 }, {
4015 .alg = "sha256",
4016 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004017 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004018 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004019 .hash = __VECS(sha256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004020 }
4021 }, {
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05304022 .alg = "sha3-224",
4023 .test = alg_test_hash,
4024 .fips_allowed = 1,
4025 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004026 .hash = __VECS(sha3_224_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05304027 }
4028 }, {
4029 .alg = "sha3-256",
4030 .test = alg_test_hash,
4031 .fips_allowed = 1,
4032 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004033 .hash = __VECS(sha3_256_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05304034 }
4035 }, {
4036 .alg = "sha3-384",
4037 .test = alg_test_hash,
4038 .fips_allowed = 1,
4039 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004040 .hash = __VECS(sha3_384_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05304041 }
4042 }, {
4043 .alg = "sha3-512",
4044 .test = alg_test_hash,
4045 .fips_allowed = 1,
4046 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004047 .hash = __VECS(sha3_512_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05304048 }
4049 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004050 .alg = "sha384",
4051 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004052 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004053 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004054 .hash = __VECS(sha384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004055 }
4056 }, {
4057 .alg = "sha512",
4058 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004059 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004060 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004061 .hash = __VECS(sha512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004062 }
4063 }, {
Gilad Ben-Yossefb7e27532017-08-21 13:51:29 +03004064 .alg = "sm3",
4065 .test = alg_test_hash,
4066 .suite = {
4067 .hash = __VECS(sm3_tv_template)
4068 }
4069 }, {
Vitaly Chikunov25a0b9d2018-11-07 00:00:03 +03004070 .alg = "streebog256",
4071 .test = alg_test_hash,
4072 .suite = {
4073 .hash = __VECS(streebog256_tv_template)
4074 }
4075 }, {
4076 .alg = "streebog512",
4077 .test = alg_test_hash,
4078 .suite = {
4079 .hash = __VECS(streebog512_tv_template)
4080 }
4081 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004082 .alg = "tgr128",
4083 .test = alg_test_hash,
4084 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004085 .hash = __VECS(tgr128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004086 }
4087 }, {
4088 .alg = "tgr160",
4089 .test = alg_test_hash,
4090 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004091 .hash = __VECS(tgr160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004092 }
4093 }, {
4094 .alg = "tgr192",
4095 .test = alg_test_hash,
4096 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004097 .hash = __VECS(tgr192_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004098 }
4099 }, {
Eric Biggersed331ad2018-06-18 10:22:39 -07004100 .alg = "vmac64(aes)",
4101 .test = alg_test_hash,
4102 .suite = {
4103 .hash = __VECS(vmac64_aes_tv_template)
4104 }
4105 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004106 .alg = "wp256",
4107 .test = alg_test_hash,
4108 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004109 .hash = __VECS(wp256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004110 }
4111 }, {
4112 .alg = "wp384",
4113 .test = alg_test_hash,
4114 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004115 .hash = __VECS(wp384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004116 }
4117 }, {
4118 .alg = "wp512",
4119 .test = alg_test_hash,
4120 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004121 .hash = __VECS(wp512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004122 }
4123 }, {
4124 .alg = "xcbc(aes)",
4125 .test = alg_test_hash,
4126 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004127 .hash = __VECS(aes_xcbc128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004128 }
4129 }, {
Eric Biggersaa762402018-11-16 17:26:22 -08004130 .alg = "xchacha12",
4131 .test = alg_test_skcipher,
4132 .suite = {
4133 .cipher = __VECS(xchacha12_tv_template)
4134 },
4135 }, {
Eric Biggersde61d7a2018-11-16 17:26:20 -08004136 .alg = "xchacha20",
4137 .test = alg_test_skcipher,
4138 .suite = {
4139 .cipher = __VECS(xchacha20_tv_template)
4140 },
4141 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004142 .alg = "xts(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004143 .test = alg_test_skcipher,
Jarod Wilson2918aa82011-01-29 15:14:01 +11004144 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004145 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004146 .cipher = __VECS(aes_xts_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004147 }
Geert Uytterhoeven0c01aed2009-03-04 15:42:15 +08004148 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02004149 .alg = "xts(camellia)",
4150 .test = alg_test_skcipher,
4151 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004152 .cipher = __VECS(camellia_xts_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02004153 }
4154 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004155 .alg = "xts(cast6)",
4156 .test = alg_test_skcipher,
4157 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004158 .cipher = __VECS(cast6_xts_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004159 }
4160 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01004161 /* Same as xts(aes) except the key is stored in
4162 * hardware secure memory which we reference by index
4163 */
4164 .alg = "xts(paes)",
4165 .test = alg_test_null,
4166 .fips_allowed = 1,
4167 }, {
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03004168 .alg = "xts(serpent)",
4169 .test = alg_test_skcipher,
4170 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004171 .cipher = __VECS(serpent_xts_tv_template)
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03004172 }
4173 }, {
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03004174 .alg = "xts(twofish)",
4175 .test = alg_test_skcipher,
4176 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004177 .cipher = __VECS(tf_xts_tv_template)
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03004178 }
Giovanni Cabiddua368f432017-04-21 21:54:30 +01004179 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01004180 .alg = "xts4096(paes)",
4181 .test = alg_test_null,
4182 .fips_allowed = 1,
4183 }, {
4184 .alg = "xts512(paes)",
4185 .test = alg_test_null,
4186 .fips_allowed = 1,
4187 }, {
Giovanni Cabiddua368f432017-04-21 21:54:30 +01004188 .alg = "zlib-deflate",
4189 .test = alg_test_comp,
4190 .fips_allowed = 1,
4191 .suite = {
4192 .comp = {
4193 .comp = __VECS(zlib_deflate_comp_tv_template),
4194 .decomp = __VECS(zlib_deflate_decomp_tv_template)
4195 }
4196 }
Nick Terrelld28fc3d2018-03-30 12:14:53 -07004197 }, {
4198 .alg = "zstd",
4199 .test = alg_test_comp,
4200 .fips_allowed = 1,
4201 .suite = {
4202 .comp = {
4203 .comp = __VECS(zstd_comp_tv_template),
4204 .decomp = __VECS(zstd_decomp_tv_template)
4205 }
4206 }
Herbert Xuda7f0332008-07-31 17:08:25 +08004207 }
4208};
4209
Eric Biggers3f47a032019-01-31 23:51:43 -08004210static void alg_check_test_descs_order(void)
Jussi Kivilinna57147582013-06-13 17:37:40 +03004211{
4212 int i;
4213
Jussi Kivilinna57147582013-06-13 17:37:40 +03004214 for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
4215 int diff = strcmp(alg_test_descs[i - 1].alg,
4216 alg_test_descs[i].alg);
4217
4218 if (WARN_ON(diff > 0)) {
4219 pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
4220 alg_test_descs[i - 1].alg,
4221 alg_test_descs[i].alg);
4222 }
4223
4224 if (WARN_ON(diff == 0)) {
4225 pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
4226 alg_test_descs[i].alg);
4227 }
4228 }
4229}
4230
Eric Biggers3f47a032019-01-31 23:51:43 -08004231static void alg_check_testvec_configs(void)
4232{
Eric Biggers4e7babba2019-01-31 23:51:46 -08004233 int i;
4234
4235 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++)
4236 WARN_ON(!valid_testvec_config(
4237 &default_cipher_testvec_configs[i]));
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08004238
4239 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++)
4240 WARN_ON(!valid_testvec_config(
4241 &default_hash_testvec_configs[i]));
Eric Biggers3f47a032019-01-31 23:51:43 -08004242}
4243
4244static void testmgr_onetime_init(void)
4245{
4246 alg_check_test_descs_order();
4247 alg_check_testvec_configs();
Eric Biggers5b2706a2019-01-31 23:51:44 -08004248
4249#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
4250 pr_warn("alg: extra crypto tests enabled. This is intended for developer use only.\n");
4251#endif
Eric Biggers3f47a032019-01-31 23:51:43 -08004252}
4253
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004254static int alg_find_test(const char *alg)
Herbert Xuda7f0332008-07-31 17:08:25 +08004255{
4256 int start = 0;
4257 int end = ARRAY_SIZE(alg_test_descs);
4258
4259 while (start < end) {
4260 int i = (start + end) / 2;
4261 int diff = strcmp(alg_test_descs[i].alg, alg);
4262
4263 if (diff > 0) {
4264 end = i;
4265 continue;
4266 }
4267
4268 if (diff < 0) {
4269 start = i + 1;
4270 continue;
4271 }
4272
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004273 return i;
Herbert Xuda7f0332008-07-31 17:08:25 +08004274 }
4275
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004276 return -1;
4277}
4278
4279int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
4280{
4281 int i;
Herbert Xua68f6612009-07-02 16:32:12 +08004282 int j;
Neil Hormand12d6b62008-10-12 20:36:51 +08004283 int rc;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004284
Richard W.M. Jones9e5c9fe2016-05-03 10:00:17 +01004285 if (!fips_enabled && notests) {
4286 printk_once(KERN_INFO "alg: self-tests disabled\n");
4287 return 0;
4288 }
4289
Eric Biggers3f47a032019-01-31 23:51:43 -08004290 DO_ONCE(testmgr_onetime_init);
Jussi Kivilinna57147582013-06-13 17:37:40 +03004291
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004292 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
4293 char nalg[CRYPTO_MAX_ALG_NAME];
4294
4295 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
4296 sizeof(nalg))
4297 return -ENAMETOOLONG;
4298
4299 i = alg_find_test(nalg);
4300 if (i < 0)
4301 goto notest;
4302
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10004303 if (fips_enabled && !alg_test_descs[i].fips_allowed)
4304 goto non_fips_alg;
4305
Jarod Wilson941fb322009-05-04 19:49:23 +08004306 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
4307 goto test_done;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004308 }
4309
4310 i = alg_find_test(alg);
Herbert Xua68f6612009-07-02 16:32:12 +08004311 j = alg_find_test(driver);
4312 if (i < 0 && j < 0)
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004313 goto notest;
4314
Herbert Xua68f6612009-07-02 16:32:12 +08004315 if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
4316 (j >= 0 && !alg_test_descs[j].fips_allowed)))
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10004317 goto non_fips_alg;
4318
Herbert Xua68f6612009-07-02 16:32:12 +08004319 rc = 0;
4320 if (i >= 0)
4321 rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
4322 type, mask);
Cristian Stoica032c8ca2013-07-18 18:57:07 +03004323 if (j >= 0 && j != i)
Herbert Xua68f6612009-07-02 16:32:12 +08004324 rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
4325 type, mask);
4326
Jarod Wilson941fb322009-05-04 19:49:23 +08004327test_done:
Eric Biggerseda69b02019-03-31 13:09:14 -07004328 if (rc && (fips_enabled || panic_on_fail))
4329 panic("alg: self-tests for %s (%s) failed in %s mode!\n",
4330 driver, alg, fips_enabled ? "fips" : "panic_on_fail");
Neil Hormand12d6b62008-10-12 20:36:51 +08004331
Jarod Wilson29ecd4a2009-05-04 19:51:17 +08004332 if (fips_enabled && !rc)
Masanari Iida3e8cffd2014-10-07 00:37:54 +09004333 pr_info("alg: self-tests for %s (%s) passed\n", driver, alg);
Jarod Wilson29ecd4a2009-05-04 19:51:17 +08004334
Neil Hormand12d6b62008-10-12 20:36:51 +08004335 return rc;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004336
4337notest:
Herbert Xuda7f0332008-07-31 17:08:25 +08004338 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
4339 return 0;
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10004340non_fips_alg:
4341 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08004342}
Alexander Shishkin0b767f92010-06-03 20:53:43 +10004343
Herbert Xu326a6342010-08-06 09:40:28 +08004344#endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
Alexander Shishkin0b767f92010-06-03 20:53:43 +10004345
Herbert Xuda7f0332008-07-31 17:08:25 +08004346EXPORT_SYMBOL_GPL(alg_test);