blob: d760f5cd35b2df021a152e88750f6284c005044f [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 Biggersd8ea98a2019-05-28 09:40:55 -07001040static int build_hash_sglist(struct test_sglist *tsgl,
1041 const struct hash_testvec *vec,
1042 const struct testvec_config *cfg,
1043 unsigned int alignmask,
1044 const struct test_sg_division *divs[XBUFSIZE])
1045{
1046 struct kvec kv;
1047 struct iov_iter input;
1048
1049 kv.iov_base = (void *)vec->plaintext;
1050 kv.iov_len = vec->psize;
1051 iov_iter_kvec(&input, WRITE, &kv, 1, vec->psize);
1052 return build_test_sglist(tsgl, cfg->src_divs, alignmask, vec->psize,
1053 &input, divs);
1054}
1055
1056static int check_hash_result(const char *type,
1057 const u8 *result, unsigned int digestsize,
1058 const struct hash_testvec *vec,
1059 const char *vec_name,
1060 const char *driver,
1061 const struct testvec_config *cfg)
1062{
1063 if (memcmp(result, vec->digest, digestsize) != 0) {
1064 pr_err("alg: %s: %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
1065 type, driver, vec_name, cfg->name);
1066 return -EINVAL;
1067 }
1068 if (!testmgr_is_poison(&result[digestsize], TESTMGR_POISON_LEN)) {
1069 pr_err("alg: %s: %s overran result buffer on test vector %s, cfg=\"%s\"\n",
1070 type, driver, vec_name, cfg->name);
1071 return -EOVERFLOW;
1072 }
1073 return 0;
1074}
1075
1076static inline int check_shash_op(const char *op, int err,
1077 const char *driver, const char *vec_name,
1078 const struct testvec_config *cfg)
1079{
1080 if (err)
1081 pr_err("alg: shash: %s %s() failed with err %d on test vector %s, cfg=\"%s\"\n",
1082 driver, op, err, vec_name, cfg->name);
1083 return err;
1084}
1085
1086static inline const void *sg_data(struct scatterlist *sg)
1087{
1088 return page_address(sg_page(sg)) + sg->offset;
1089}
1090
1091/* Test one hash test vector in one configuration, using the shash API */
1092static int test_shash_vec_cfg(const char *driver,
1093 const struct hash_testvec *vec,
1094 const char *vec_name,
1095 const struct testvec_config *cfg,
1096 struct shash_desc *desc,
1097 struct test_sglist *tsgl,
1098 u8 *hashstate)
1099{
1100 struct crypto_shash *tfm = desc->tfm;
1101 const unsigned int alignmask = crypto_shash_alignmask(tfm);
1102 const unsigned int digestsize = crypto_shash_digestsize(tfm);
1103 const unsigned int statesize = crypto_shash_statesize(tfm);
1104 const struct test_sg_division *divs[XBUFSIZE];
1105 unsigned int i;
1106 u8 result[HASH_MAX_DIGESTSIZE + TESTMGR_POISON_LEN];
1107 int err;
1108
1109 /* Set the key, if specified */
1110 if (vec->ksize) {
1111 err = crypto_shash_setkey(tfm, vec->key, vec->ksize);
1112 if (err) {
1113 if (err == vec->setkey_error)
1114 return 0;
1115 pr_err("alg: shash: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1116 driver, vec_name, vec->setkey_error, err,
1117 crypto_shash_get_flags(tfm));
1118 return err;
1119 }
1120 if (vec->setkey_error) {
1121 pr_err("alg: shash: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1122 driver, vec_name, vec->setkey_error);
1123 return -EINVAL;
1124 }
1125 }
1126
1127 /* Build the scatterlist for the source data */
1128 err = build_hash_sglist(tsgl, vec, cfg, alignmask, divs);
1129 if (err) {
1130 pr_err("alg: shash: %s: error preparing scatterlist for test vector %s, cfg=\"%s\"\n",
1131 driver, vec_name, cfg->name);
1132 return err;
1133 }
1134
1135 /* Do the actual hashing */
1136
1137 testmgr_poison(desc->__ctx, crypto_shash_descsize(tfm));
1138 testmgr_poison(result, digestsize + TESTMGR_POISON_LEN);
1139
1140 if (cfg->finalization_type == FINALIZATION_TYPE_DIGEST ||
1141 vec->digest_error) {
1142 /* Just using digest() */
1143 if (tsgl->nents != 1)
1144 return 0;
1145 if (cfg->nosimd)
1146 crypto_disable_simd_for_test();
1147 err = crypto_shash_digest(desc, sg_data(&tsgl->sgl[0]),
1148 tsgl->sgl[0].length, result);
1149 if (cfg->nosimd)
1150 crypto_reenable_simd_for_test();
1151 if (err) {
1152 if (err == vec->digest_error)
1153 return 0;
1154 pr_err("alg: shash: %s digest() failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
1155 driver, vec_name, vec->digest_error, err,
1156 cfg->name);
1157 return err;
1158 }
1159 if (vec->digest_error) {
1160 pr_err("alg: shash: %s digest() unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
1161 driver, vec_name, vec->digest_error, cfg->name);
1162 return -EINVAL;
1163 }
1164 goto result_ready;
1165 }
1166
1167 /* Using init(), zero or more update(), then final() or finup() */
1168
1169 if (cfg->nosimd)
1170 crypto_disable_simd_for_test();
1171 err = crypto_shash_init(desc);
1172 if (cfg->nosimd)
1173 crypto_reenable_simd_for_test();
1174 err = check_shash_op("init", err, driver, vec_name, cfg);
1175 if (err)
1176 return err;
1177
1178 for (i = 0; i < tsgl->nents; i++) {
1179 if (i + 1 == tsgl->nents &&
1180 cfg->finalization_type == FINALIZATION_TYPE_FINUP) {
1181 if (divs[i]->nosimd)
1182 crypto_disable_simd_for_test();
1183 err = crypto_shash_finup(desc, sg_data(&tsgl->sgl[i]),
1184 tsgl->sgl[i].length, result);
1185 if (divs[i]->nosimd)
1186 crypto_reenable_simd_for_test();
1187 err = check_shash_op("finup", err, driver, vec_name,
1188 cfg);
1189 if (err)
1190 return err;
1191 goto result_ready;
1192 }
1193 if (divs[i]->nosimd)
1194 crypto_disable_simd_for_test();
1195 err = crypto_shash_update(desc, sg_data(&tsgl->sgl[i]),
1196 tsgl->sgl[i].length);
1197 if (divs[i]->nosimd)
1198 crypto_reenable_simd_for_test();
1199 err = check_shash_op("update", err, driver, vec_name, cfg);
1200 if (err)
1201 return err;
1202 if (divs[i]->flush_type == FLUSH_TYPE_REIMPORT) {
1203 /* Test ->export() and ->import() */
1204 testmgr_poison(hashstate + statesize,
1205 TESTMGR_POISON_LEN);
1206 err = crypto_shash_export(desc, hashstate);
1207 err = check_shash_op("export", err, driver, vec_name,
1208 cfg);
1209 if (err)
1210 return err;
1211 if (!testmgr_is_poison(hashstate + statesize,
1212 TESTMGR_POISON_LEN)) {
1213 pr_err("alg: shash: %s export() overran state buffer on test vector %s, cfg=\"%s\"\n",
1214 driver, vec_name, cfg->name);
1215 return -EOVERFLOW;
1216 }
1217 testmgr_poison(desc->__ctx, crypto_shash_descsize(tfm));
1218 err = crypto_shash_import(desc, hashstate);
1219 err = check_shash_op("import", err, driver, vec_name,
1220 cfg);
1221 if (err)
1222 return err;
1223 }
1224 }
1225
1226 if (cfg->nosimd)
1227 crypto_disable_simd_for_test();
1228 err = crypto_shash_final(desc, result);
1229 if (cfg->nosimd)
1230 crypto_reenable_simd_for_test();
1231 err = check_shash_op("final", err, driver, vec_name, cfg);
1232 if (err)
1233 return err;
1234result_ready:
1235 return check_hash_result("shash", result, digestsize, vec, vec_name,
1236 driver, cfg);
1237}
1238
Eric Biggers65707372019-03-12 22:12:52 -07001239static int do_ahash_op(int (*op)(struct ahash_request *req),
1240 struct ahash_request *req,
1241 struct crypto_wait *wait, bool nosimd)
1242{
1243 int err;
1244
1245 if (nosimd)
1246 crypto_disable_simd_for_test();
1247
1248 err = op(req);
1249
1250 if (nosimd)
1251 crypto_reenable_simd_for_test();
1252
1253 return crypto_wait_req(err, wait);
1254}
1255
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001256static int check_nonfinal_ahash_op(const char *op, int err,
1257 u8 *result, unsigned int digestsize,
1258 const char *driver, const char *vec_name,
1259 const struct testvec_config *cfg)
Kamil Konieczny466d7b92018-01-16 15:26:13 +01001260{
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001261 if (err) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001262 pr_err("alg: ahash: %s %s() failed with err %d on test vector %s, cfg=\"%s\"\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001263 driver, op, err, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001264 return err;
1265 }
1266 if (!testmgr_is_poison(result, digestsize)) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001267 pr_err("alg: ahash: %s %s() used result buffer on test vector %s, cfg=\"%s\"\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001268 driver, op, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001269 return -EINVAL;
1270 }
1271 return 0;
1272}
Kamil Konieczny466d7b92018-01-16 15:26:13 +01001273
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001274/* Test one hash test vector in one configuration, using the ahash API */
1275static int test_ahash_vec_cfg(const char *driver,
1276 const struct hash_testvec *vec,
1277 const char *vec_name,
1278 const struct testvec_config *cfg,
1279 struct ahash_request *req,
1280 struct test_sglist *tsgl,
1281 u8 *hashstate)
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001282{
1283 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1284 const unsigned int alignmask = crypto_ahash_alignmask(tfm);
1285 const unsigned int digestsize = crypto_ahash_digestsize(tfm);
1286 const unsigned int statesize = crypto_ahash_statesize(tfm);
1287 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1288 const struct test_sg_division *divs[XBUFSIZE];
1289 DECLARE_CRYPTO_WAIT(wait);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001290 unsigned int i;
1291 struct scatterlist *pending_sgl;
1292 unsigned int pending_len;
1293 u8 result[HASH_MAX_DIGESTSIZE + TESTMGR_POISON_LEN];
1294 int err;
1295
1296 /* Set the key, if specified */
1297 if (vec->ksize) {
1298 err = crypto_ahash_setkey(tfm, vec->key, vec->ksize);
1299 if (err) {
Eric Biggers5283a8e2019-04-11 21:57:36 -07001300 if (err == vec->setkey_error)
1301 return 0;
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001302 pr_err("alg: ahash: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001303 driver, vec_name, vec->setkey_error, err,
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001304 crypto_ahash_get_flags(tfm));
1305 return err;
1306 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07001307 if (vec->setkey_error) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001308 pr_err("alg: ahash: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001309 driver, vec_name, vec->setkey_error);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001310 return -EINVAL;
1311 }
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001312 }
1313
1314 /* Build the scatterlist for the source data */
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001315 err = build_hash_sglist(tsgl, vec, cfg, alignmask, divs);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001316 if (err) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001317 pr_err("alg: ahash: %s: error preparing scatterlist for test vector %s, cfg=\"%s\"\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001318 driver, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001319 return err;
1320 }
1321
1322 /* Do the actual hashing */
1323
1324 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
1325 testmgr_poison(result, digestsize + TESTMGR_POISON_LEN);
1326
Eric Biggers5283a8e2019-04-11 21:57:36 -07001327 if (cfg->finalization_type == FINALIZATION_TYPE_DIGEST ||
1328 vec->digest_error) {
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001329 /* Just using digest() */
1330 ahash_request_set_callback(req, req_flags, crypto_req_done,
1331 &wait);
1332 ahash_request_set_crypt(req, tsgl->sgl, result, vec->psize);
Eric Biggers65707372019-03-12 22:12:52 -07001333 err = do_ahash_op(crypto_ahash_digest, req, &wait, cfg->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001334 if (err) {
Eric Biggers5283a8e2019-04-11 21:57:36 -07001335 if (err == vec->digest_error)
1336 return 0;
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001337 pr_err("alg: ahash: %s digest() failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001338 driver, vec_name, vec->digest_error, err,
Eric Biggers5283a8e2019-04-11 21:57:36 -07001339 cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001340 return err;
1341 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07001342 if (vec->digest_error) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001343 pr_err("alg: ahash: %s digest() unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001344 driver, vec_name, vec->digest_error, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001345 return -EINVAL;
1346 }
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001347 goto result_ready;
1348 }
1349
1350 /* Using init(), zero or more update(), then final() or finup() */
1351
1352 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
1353 ahash_request_set_crypt(req, NULL, result, 0);
Eric Biggers65707372019-03-12 22:12:52 -07001354 err = do_ahash_op(crypto_ahash_init, req, &wait, cfg->nosimd);
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001355 err = check_nonfinal_ahash_op("init", err, result, digestsize,
1356 driver, vec_name, cfg);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001357 if (err)
1358 return err;
1359
1360 pending_sgl = NULL;
1361 pending_len = 0;
1362 for (i = 0; i < tsgl->nents; i++) {
1363 if (divs[i]->flush_type != FLUSH_TYPE_NONE &&
1364 pending_sgl != NULL) {
1365 /* update() with the pending data */
1366 ahash_request_set_callback(req, req_flags,
1367 crypto_req_done, &wait);
1368 ahash_request_set_crypt(req, pending_sgl, result,
1369 pending_len);
Eric Biggers65707372019-03-12 22:12:52 -07001370 err = do_ahash_op(crypto_ahash_update, req, &wait,
1371 divs[i]->nosimd);
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001372 err = check_nonfinal_ahash_op("update", err,
1373 result, digestsize,
1374 driver, vec_name, cfg);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001375 if (err)
1376 return err;
1377 pending_sgl = NULL;
1378 pending_len = 0;
1379 }
1380 if (divs[i]->flush_type == FLUSH_TYPE_REIMPORT) {
1381 /* Test ->export() and ->import() */
1382 testmgr_poison(hashstate + statesize,
1383 TESTMGR_POISON_LEN);
1384 err = crypto_ahash_export(req, hashstate);
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001385 err = check_nonfinal_ahash_op("export", err,
1386 result, digestsize,
1387 driver, vec_name, cfg);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001388 if (err)
1389 return err;
1390 if (!testmgr_is_poison(hashstate + statesize,
1391 TESTMGR_POISON_LEN)) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001392 pr_err("alg: ahash: %s export() overran state buffer on test vector %s, cfg=\"%s\"\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001393 driver, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001394 return -EOVERFLOW;
1395 }
1396
1397 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
1398 err = crypto_ahash_import(req, hashstate);
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001399 err = check_nonfinal_ahash_op("import", err,
1400 result, digestsize,
1401 driver, vec_name, cfg);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001402 if (err)
1403 return err;
1404 }
1405 if (pending_sgl == NULL)
1406 pending_sgl = &tsgl->sgl[i];
1407 pending_len += tsgl->sgl[i].length;
1408 }
1409
1410 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
1411 ahash_request_set_crypt(req, pending_sgl, result, pending_len);
1412 if (cfg->finalization_type == FINALIZATION_TYPE_FINAL) {
1413 /* finish with update() and final() */
Eric Biggers65707372019-03-12 22:12:52 -07001414 err = do_ahash_op(crypto_ahash_update, req, &wait, cfg->nosimd);
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001415 err = check_nonfinal_ahash_op("update", err, result, digestsize,
1416 driver, vec_name, cfg);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001417 if (err)
1418 return err;
Eric Biggers65707372019-03-12 22:12:52 -07001419 err = do_ahash_op(crypto_ahash_final, req, &wait, cfg->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001420 if (err) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001421 pr_err("alg: ahash: %s final() failed with err %d on test vector %s, cfg=\"%s\"\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001422 driver, err, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001423 return err;
1424 }
1425 } else {
1426 /* finish with finup() */
Eric Biggers65707372019-03-12 22:12:52 -07001427 err = do_ahash_op(crypto_ahash_finup, req, &wait, cfg->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001428 if (err) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001429 pr_err("alg: ahash: %s finup() failed with err %d on test vector %s, cfg=\"%s\"\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001430 driver, err, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001431 return err;
1432 }
1433 }
1434
1435result_ready:
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001436 return check_hash_result("ahash", result, digestsize, vec, vec_name,
1437 driver, cfg);
1438}
1439
1440static int test_hash_vec_cfg(const char *driver,
1441 const struct hash_testvec *vec,
1442 const char *vec_name,
1443 const struct testvec_config *cfg,
1444 struct ahash_request *req,
1445 struct shash_desc *desc,
1446 struct test_sglist *tsgl,
1447 u8 *hashstate)
1448{
1449 int err;
1450
1451 /*
1452 * For algorithms implemented as "shash", most bugs will be detected by
1453 * both the shash and ahash tests. Test the shash API first so that the
1454 * failures involve less indirection, so are easier to debug.
1455 */
1456
1457 if (desc) {
1458 err = test_shash_vec_cfg(driver, vec, vec_name, cfg, desc, tsgl,
1459 hashstate);
1460 if (err)
1461 return err;
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001462 }
1463
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001464 return test_ahash_vec_cfg(driver, vec, vec_name, cfg, req, tsgl,
1465 hashstate);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001466}
1467
1468static int test_hash_vec(const char *driver, const struct hash_testvec *vec,
1469 unsigned int vec_num, struct ahash_request *req,
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001470 struct shash_desc *desc, struct test_sglist *tsgl,
1471 u8 *hashstate)
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001472{
Eric Biggers951d1332019-04-11 21:57:37 -07001473 char vec_name[16];
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001474 unsigned int i;
1475 int err;
1476
Eric Biggers951d1332019-04-11 21:57:37 -07001477 sprintf(vec_name, "%u", vec_num);
1478
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001479 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++) {
Eric Biggers951d1332019-04-11 21:57:37 -07001480 err = test_hash_vec_cfg(driver, vec, vec_name,
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001481 &default_hash_testvec_configs[i],
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001482 req, desc, tsgl, hashstate);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001483 if (err)
1484 return err;
1485 }
1486
1487#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1488 if (!noextratests) {
1489 struct testvec_config cfg;
1490 char cfgname[TESTVEC_CONFIG_NAMELEN];
1491
1492 for (i = 0; i < fuzz_iterations; i++) {
1493 generate_random_testvec_config(&cfg, cfgname,
1494 sizeof(cfgname));
Eric Biggers951d1332019-04-11 21:57:37 -07001495 err = test_hash_vec_cfg(driver, vec, vec_name, &cfg,
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001496 req, desc, tsgl, hashstate);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001497 if (err)
1498 return err;
Eric Biggerse63e1b02019-06-02 22:42:33 -07001499 cond_resched();
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001500 }
1501 }
1502#endif
1503 return 0;
1504}
1505
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001506#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1507/*
1508 * Generate a hash test vector from the given implementation.
1509 * Assumes the buffers in 'vec' were already allocated.
1510 */
Arnd Bergmann149c4e62019-06-18 11:21:53 +02001511static void generate_random_hash_testvec(struct shash_desc *desc,
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001512 struct hash_testvec *vec,
1513 unsigned int maxkeysize,
1514 unsigned int maxdatasize,
1515 char *name, size_t max_namelen)
1516{
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001517 /* Data */
1518 vec->psize = generate_random_length(maxdatasize);
1519 generate_random_bytes((u8 *)vec->plaintext, vec->psize);
1520
1521 /*
1522 * Key: length in range [1, maxkeysize], but usually choose maxkeysize.
1523 * If algorithm is unkeyed, then maxkeysize == 0 and set ksize = 0.
1524 */
1525 vec->setkey_error = 0;
1526 vec->ksize = 0;
1527 if (maxkeysize) {
1528 vec->ksize = maxkeysize;
1529 if (prandom_u32() % 4 == 0)
1530 vec->ksize = 1 + (prandom_u32() % maxkeysize);
1531 generate_random_bytes((u8 *)vec->key, vec->ksize);
1532
Arnd Bergmann149c4e62019-06-18 11:21:53 +02001533 vec->setkey_error = crypto_shash_setkey(desc->tfm, vec->key,
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001534 vec->ksize);
1535 /* If the key couldn't be set, no need to continue to digest. */
1536 if (vec->setkey_error)
1537 goto done;
1538 }
1539
1540 /* Digest */
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001541 vec->digest_error = crypto_shash_digest(desc, vec->plaintext,
1542 vec->psize, (u8 *)vec->digest);
1543done:
1544 snprintf(name, max_namelen, "\"random: psize=%u ksize=%u\"",
1545 vec->psize, vec->ksize);
1546}
1547
1548/*
1549 * Test the hash algorithm represented by @req against the corresponding generic
1550 * implementation, if one is available.
1551 */
1552static int test_hash_vs_generic_impl(const char *driver,
1553 const char *generic_driver,
1554 unsigned int maxkeysize,
1555 struct ahash_request *req,
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001556 struct shash_desc *desc,
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001557 struct test_sglist *tsgl,
1558 u8 *hashstate)
1559{
1560 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1561 const unsigned int digestsize = crypto_ahash_digestsize(tfm);
1562 const unsigned int blocksize = crypto_ahash_blocksize(tfm);
1563 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
1564 const char *algname = crypto_hash_alg_common(tfm)->base.cra_name;
1565 char _generic_driver[CRYPTO_MAX_ALG_NAME];
1566 struct crypto_shash *generic_tfm = NULL;
Arnd Bergmann149c4e62019-06-18 11:21:53 +02001567 struct shash_desc *generic_desc = NULL;
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001568 unsigned int i;
1569 struct hash_testvec vec = { 0 };
1570 char vec_name[64];
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02001571 struct testvec_config *cfg;
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001572 char cfgname[TESTVEC_CONFIG_NAMELEN];
1573 int err;
1574
1575 if (noextratests)
1576 return 0;
1577
1578 if (!generic_driver) { /* Use default naming convention? */
1579 err = build_generic_driver_name(algname, _generic_driver);
1580 if (err)
1581 return err;
1582 generic_driver = _generic_driver;
1583 }
1584
1585 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
1586 return 0;
1587
1588 generic_tfm = crypto_alloc_shash(generic_driver, 0, 0);
1589 if (IS_ERR(generic_tfm)) {
1590 err = PTR_ERR(generic_tfm);
1591 if (err == -ENOENT) {
1592 pr_warn("alg: hash: skipping comparison tests for %s because %s is unavailable\n",
1593 driver, generic_driver);
1594 return 0;
1595 }
1596 pr_err("alg: hash: error allocating %s (generic impl of %s): %d\n",
1597 generic_driver, algname, err);
1598 return err;
1599 }
1600
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02001601 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1602 if (!cfg) {
1603 err = -ENOMEM;
1604 goto out;
1605 }
1606
Arnd Bergmann149c4e62019-06-18 11:21:53 +02001607 generic_desc = kzalloc(sizeof(*desc) +
1608 crypto_shash_descsize(generic_tfm), GFP_KERNEL);
1609 if (!generic_desc) {
1610 err = -ENOMEM;
1611 goto out;
1612 }
1613 generic_desc->tfm = generic_tfm;
1614
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001615 /* Check the algorithm properties for consistency. */
1616
1617 if (digestsize != crypto_shash_digestsize(generic_tfm)) {
1618 pr_err("alg: hash: digestsize for %s (%u) doesn't match generic impl (%u)\n",
1619 driver, digestsize,
1620 crypto_shash_digestsize(generic_tfm));
1621 err = -EINVAL;
1622 goto out;
1623 }
1624
1625 if (blocksize != crypto_shash_blocksize(generic_tfm)) {
1626 pr_err("alg: hash: blocksize for %s (%u) doesn't match generic impl (%u)\n",
1627 driver, blocksize, crypto_shash_blocksize(generic_tfm));
1628 err = -EINVAL;
1629 goto out;
1630 }
1631
1632 /*
1633 * Now generate test vectors using the generic implementation, and test
1634 * the other implementation against them.
1635 */
1636
1637 vec.key = kmalloc(maxkeysize, GFP_KERNEL);
1638 vec.plaintext = kmalloc(maxdatasize, GFP_KERNEL);
1639 vec.digest = kmalloc(digestsize, GFP_KERNEL);
1640 if (!vec.key || !vec.plaintext || !vec.digest) {
1641 err = -ENOMEM;
1642 goto out;
1643 }
1644
1645 for (i = 0; i < fuzz_iterations * 8; i++) {
Arnd Bergmann149c4e62019-06-18 11:21:53 +02001646 generate_random_hash_testvec(generic_desc, &vec,
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001647 maxkeysize, maxdatasize,
1648 vec_name, sizeof(vec_name));
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02001649 generate_random_testvec_config(cfg, cfgname, sizeof(cfgname));
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001650
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02001651 err = test_hash_vec_cfg(driver, &vec, vec_name, cfg,
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001652 req, desc, tsgl, hashstate);
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001653 if (err)
1654 goto out;
1655 cond_resched();
1656 }
1657 err = 0;
1658out:
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02001659 kfree(cfg);
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001660 kfree(vec.key);
1661 kfree(vec.plaintext);
1662 kfree(vec.digest);
1663 crypto_free_shash(generic_tfm);
Arnd Bergmann149c4e62019-06-18 11:21:53 +02001664 kzfree(generic_desc);
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001665 return err;
1666}
1667#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1668static int test_hash_vs_generic_impl(const char *driver,
1669 const char *generic_driver,
1670 unsigned int maxkeysize,
1671 struct ahash_request *req,
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001672 struct shash_desc *desc,
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001673 struct test_sglist *tsgl,
1674 u8 *hashstate)
1675{
1676 return 0;
1677}
1678#endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1679
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001680static int alloc_shash(const char *driver, u32 type, u32 mask,
1681 struct crypto_shash **tfm_ret,
1682 struct shash_desc **desc_ret)
1683{
1684 struct crypto_shash *tfm;
1685 struct shash_desc *desc;
1686
1687 tfm = crypto_alloc_shash(driver, type, mask);
1688 if (IS_ERR(tfm)) {
1689 if (PTR_ERR(tfm) == -ENOENT) {
1690 /*
1691 * This algorithm is only available through the ahash
1692 * API, not the shash API, so skip the shash tests.
1693 */
1694 return 0;
1695 }
1696 pr_err("alg: hash: failed to allocate shash transform for %s: %ld\n",
1697 driver, PTR_ERR(tfm));
1698 return PTR_ERR(tfm);
1699 }
1700
1701 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL);
1702 if (!desc) {
1703 crypto_free_shash(tfm);
1704 return -ENOMEM;
1705 }
1706 desc->tfm = tfm;
1707
1708 *tfm_ret = tfm;
1709 *desc_ret = desc;
1710 return 0;
1711}
1712
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001713static int __alg_test_hash(const struct hash_testvec *vecs,
1714 unsigned int num_vecs, const char *driver,
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001715 u32 type, u32 mask,
1716 const char *generic_driver, unsigned int maxkeysize)
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001717{
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001718 struct crypto_ahash *atfm = NULL;
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001719 struct ahash_request *req = NULL;
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001720 struct crypto_shash *stfm = NULL;
1721 struct shash_desc *desc = NULL;
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001722 struct test_sglist *tsgl = NULL;
1723 u8 *hashstate = NULL;
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001724 unsigned int statesize;
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001725 unsigned int i;
1726 int err;
1727
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001728 /*
1729 * Always test the ahash API. This works regardless of whether the
1730 * algorithm is implemented as ahash or shash.
1731 */
1732
1733 atfm = crypto_alloc_ahash(driver, type, mask);
1734 if (IS_ERR(atfm)) {
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001735 pr_err("alg: hash: failed to allocate transform for %s: %ld\n",
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001736 driver, PTR_ERR(atfm));
1737 return PTR_ERR(atfm);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001738 }
1739
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001740 req = ahash_request_alloc(atfm, GFP_KERNEL);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001741 if (!req) {
1742 pr_err("alg: hash: failed to allocate request for %s\n",
1743 driver);
1744 err = -ENOMEM;
1745 goto out;
1746 }
1747
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001748 /*
1749 * If available also test the shash API, to cover corner cases that may
1750 * be missed by testing the ahash API only.
1751 */
1752 err = alloc_shash(driver, type, mask, &stfm, &desc);
1753 if (err)
1754 goto out;
1755
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001756 tsgl = kmalloc(sizeof(*tsgl), GFP_KERNEL);
1757 if (!tsgl || init_test_sglist(tsgl) != 0) {
1758 pr_err("alg: hash: failed to allocate test buffers for %s\n",
1759 driver);
1760 kfree(tsgl);
1761 tsgl = NULL;
1762 err = -ENOMEM;
1763 goto out;
1764 }
1765
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001766 statesize = crypto_ahash_statesize(atfm);
1767 if (stfm)
1768 statesize = max(statesize, crypto_shash_statesize(stfm));
1769 hashstate = kmalloc(statesize + TESTMGR_POISON_LEN, GFP_KERNEL);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001770 if (!hashstate) {
1771 pr_err("alg: hash: failed to allocate hash state buffer for %s\n",
1772 driver);
1773 err = -ENOMEM;
1774 goto out;
1775 }
1776
1777 for (i = 0; i < num_vecs; i++) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001778 err = test_hash_vec(driver, &vecs[i], i, req, desc, tsgl,
1779 hashstate);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001780 if (err)
1781 goto out;
Eric Biggerse63e1b02019-06-02 22:42:33 -07001782 cond_resched();
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001783 }
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001784 err = test_hash_vs_generic_impl(driver, generic_driver, maxkeysize, req,
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001785 desc, tsgl, hashstate);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001786out:
1787 kfree(hashstate);
1788 if (tsgl) {
1789 destroy_test_sglist(tsgl);
1790 kfree(tsgl);
1791 }
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001792 kfree(desc);
1793 crypto_free_shash(stfm);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001794 ahash_request_free(req);
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001795 crypto_free_ahash(atfm);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001796 return err;
1797}
1798
1799static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1800 u32 type, u32 mask)
1801{
1802 const struct hash_testvec *template = desc->suite.hash.vecs;
1803 unsigned int tcount = desc->suite.hash.count;
1804 unsigned int nr_unkeyed, nr_keyed;
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001805 unsigned int maxkeysize = 0;
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001806 int err;
1807
1808 /*
1809 * For OPTIONAL_KEY algorithms, we have to do all the unkeyed tests
1810 * first, before setting a key on the tfm. To make this easier, we
1811 * require that the unkeyed test vectors (if any) are listed first.
1812 */
1813
1814 for (nr_unkeyed = 0; nr_unkeyed < tcount; nr_unkeyed++) {
1815 if (template[nr_unkeyed].ksize)
1816 break;
1817 }
1818 for (nr_keyed = 0; nr_unkeyed + nr_keyed < tcount; nr_keyed++) {
1819 if (!template[nr_unkeyed + nr_keyed].ksize) {
1820 pr_err("alg: hash: test vectors for %s out of order, "
1821 "unkeyed ones must come first\n", desc->alg);
Kamil Konieczny466d7b92018-01-16 15:26:13 +01001822 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08001823 }
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001824 maxkeysize = max_t(unsigned int, maxkeysize,
1825 template[nr_unkeyed + nr_keyed].ksize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001826 }
1827
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001828 err = 0;
1829 if (nr_unkeyed) {
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001830 err = __alg_test_hash(template, nr_unkeyed, driver, type, mask,
1831 desc->generic_driver, maxkeysize);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001832 template += nr_unkeyed;
Herbert Xuda7f0332008-07-31 17:08:25 +08001833 }
1834
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001835 if (!err && nr_keyed)
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001836 err = __alg_test_hash(template, nr_keyed, driver, type, mask,
1837 desc->generic_driver, maxkeysize);
Wang, Rui Y018ba952016-02-03 18:26:57 +08001838
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001839 return err;
Jussi Kivilinnada5ffe12013-06-13 17:37:55 +03001840}
1841
Eric Biggersed968042019-01-31 23:51:47 -08001842static int test_aead_vec_cfg(const char *driver, int enc,
1843 const struct aead_testvec *vec,
Eric Biggers951d1332019-04-11 21:57:37 -07001844 const char *vec_name,
Eric Biggersed968042019-01-31 23:51:47 -08001845 const struct testvec_config *cfg,
1846 struct aead_request *req,
1847 struct cipher_test_sglists *tsgls)
Herbert Xuda7f0332008-07-31 17:08:25 +08001848{
Eric Biggersed968042019-01-31 23:51:47 -08001849 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1850 const unsigned int alignmask = crypto_aead_alignmask(tfm);
1851 const unsigned int ivsize = crypto_aead_ivsize(tfm);
1852 const unsigned int authsize = vec->clen - vec->plen;
1853 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1854 const char *op = enc ? "encryption" : "decryption";
1855 DECLARE_CRYPTO_WAIT(wait);
1856 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
1857 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
1858 cfg->iv_offset +
1859 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
1860 struct kvec input[2];
Eric Biggers5283a8e2019-04-11 21:57:36 -07001861 int expected_error;
Eric Biggersed968042019-01-31 23:51:47 -08001862 int err;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001863
Eric Biggersed968042019-01-31 23:51:47 -08001864 /* Set the key */
1865 if (vec->wk)
1866 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001867 else
Eric Biggersed968042019-01-31 23:51:47 -08001868 crypto_aead_clear_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1869 err = crypto_aead_setkey(tfm, vec->key, vec->klen);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001870 if (err && err != vec->setkey_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001871 pr_err("alg: aead: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1872 driver, vec_name, vec->setkey_error, err,
Eric Biggers5283a8e2019-04-11 21:57:36 -07001873 crypto_aead_get_flags(tfm));
Eric Biggersed968042019-01-31 23:51:47 -08001874 return err;
1875 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07001876 if (!err && vec->setkey_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001877 pr_err("alg: aead: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1878 driver, vec_name, vec->setkey_error);
Eric Biggersed968042019-01-31 23:51:47 -08001879 return -EINVAL;
1880 }
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001881
Eric Biggersed968042019-01-31 23:51:47 -08001882 /* Set the authentication tag size */
1883 err = crypto_aead_setauthsize(tfm, authsize);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001884 if (err && err != vec->setauthsize_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001885 pr_err("alg: aead: %s setauthsize failed on test vector %s; expected_error=%d, actual_error=%d\n",
1886 driver, vec_name, vec->setauthsize_error, err);
Eric Biggersed968042019-01-31 23:51:47 -08001887 return err;
1888 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07001889 if (!err && vec->setauthsize_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001890 pr_err("alg: aead: %s setauthsize unexpectedly succeeded on test vector %s; expected_error=%d\n",
1891 driver, vec_name, vec->setauthsize_error);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001892 return -EINVAL;
1893 }
1894
1895 if (vec->setkey_error || vec->setauthsize_error)
1896 return 0;
Eric Biggersed968042019-01-31 23:51:47 -08001897
1898 /* The IV must be copied to a buffer, as the algorithm may modify it */
1899 if (WARN_ON(ivsize > MAX_IVLEN))
1900 return -EINVAL;
1901 if (vec->iv)
1902 memcpy(iv, vec->iv, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001903 else
Eric Biggersed968042019-01-31 23:51:47 -08001904 memset(iv, 0, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001905
Eric Biggersed968042019-01-31 23:51:47 -08001906 /* Build the src/dst scatterlists */
1907 input[0].iov_base = (void *)vec->assoc;
1908 input[0].iov_len = vec->alen;
1909 input[1].iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
1910 input[1].iov_len = enc ? vec->plen : vec->clen;
1911 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
1912 vec->alen + (enc ? vec->plen :
1913 vec->clen),
1914 vec->alen + (enc ? vec->clen :
1915 vec->plen),
1916 input, 2);
1917 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07001918 pr_err("alg: aead: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
1919 driver, op, vec_name, cfg->name);
Eric Biggersed968042019-01-31 23:51:47 -08001920 return err;
Herbert Xuda7f0332008-07-31 17:08:25 +08001921 }
1922
Eric Biggersed968042019-01-31 23:51:47 -08001923 /* Do the actual encryption or decryption */
1924 testmgr_poison(req->__ctx, crypto_aead_reqsize(tfm));
1925 aead_request_set_callback(req, req_flags, crypto_req_done, &wait);
1926 aead_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
1927 enc ? vec->plen : vec->clen, iv);
1928 aead_request_set_ad(req, vec->alen);
Eric Biggers65707372019-03-12 22:12:52 -07001929 if (cfg->nosimd)
1930 crypto_disable_simd_for_test();
1931 err = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
1932 if (cfg->nosimd)
1933 crypto_reenable_simd_for_test();
1934 err = crypto_wait_req(err, &wait);
Herbert Xuda7f0332008-07-31 17:08:25 +08001935
Eric Biggersa6e5ef92019-01-31 23:51:50 -08001936 /* Check that the algorithm didn't overwrite things it shouldn't have */
1937 if (req->cryptlen != (enc ? vec->plen : vec->clen) ||
1938 req->assoclen != vec->alen ||
1939 req->iv != iv ||
1940 req->src != tsgls->src.sgl_ptr ||
1941 req->dst != tsgls->dst.sgl_ptr ||
1942 crypto_aead_reqtfm(req) != tfm ||
1943 req->base.complete != crypto_req_done ||
1944 req->base.flags != req_flags ||
1945 req->base.data != &wait) {
Eric Biggers951d1332019-04-11 21:57:37 -07001946 pr_err("alg: aead: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
1947 driver, op, vec_name, cfg->name);
Eric Biggersa6e5ef92019-01-31 23:51:50 -08001948 if (req->cryptlen != (enc ? vec->plen : vec->clen))
1949 pr_err("alg: aead: changed 'req->cryptlen'\n");
1950 if (req->assoclen != vec->alen)
1951 pr_err("alg: aead: changed 'req->assoclen'\n");
1952 if (req->iv != iv)
1953 pr_err("alg: aead: changed 'req->iv'\n");
1954 if (req->src != tsgls->src.sgl_ptr)
1955 pr_err("alg: aead: changed 'req->src'\n");
1956 if (req->dst != tsgls->dst.sgl_ptr)
1957 pr_err("alg: aead: changed 'req->dst'\n");
1958 if (crypto_aead_reqtfm(req) != tfm)
1959 pr_err("alg: aead: changed 'req->base.tfm'\n");
1960 if (req->base.complete != crypto_req_done)
1961 pr_err("alg: aead: changed 'req->base.complete'\n");
1962 if (req->base.flags != req_flags)
1963 pr_err("alg: aead: changed 'req->base.flags'\n");
1964 if (req->base.data != &wait)
1965 pr_err("alg: aead: changed 'req->base.data'\n");
1966 return -EINVAL;
1967 }
1968 if (is_test_sglist_corrupted(&tsgls->src)) {
Eric Biggers951d1332019-04-11 21:57:37 -07001969 pr_err("alg: aead: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
1970 driver, op, vec_name, cfg->name);
Eric Biggersa6e5ef92019-01-31 23:51:50 -08001971 return -EINVAL;
1972 }
1973 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
1974 is_test_sglist_corrupted(&tsgls->dst)) {
Eric Biggers951d1332019-04-11 21:57:37 -07001975 pr_err("alg: aead: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
1976 driver, op, vec_name, cfg->name);
Eric Biggersa6e5ef92019-01-31 23:51:50 -08001977 return -EINVAL;
1978 }
1979
Eric Biggers5283a8e2019-04-11 21:57:36 -07001980 /* Check for success or failure */
1981 expected_error = vec->novrfy ? -EBADMSG : vec->crypt_error;
1982 if (err) {
1983 if (err == expected_error)
1984 return 0;
Eric Biggers951d1332019-04-11 21:57:37 -07001985 pr_err("alg: aead: %s %s failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
1986 driver, op, vec_name, expected_error, err, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001987 return err;
1988 }
1989 if (expected_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001990 pr_err("alg: aead: %s %s unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
1991 driver, op, vec_name, expected_error, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001992 return -EINVAL;
1993 }
1994
Eric Biggersed968042019-01-31 23:51:47 -08001995 /* Check for the correct output (ciphertext or plaintext) */
1996 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
1997 enc ? vec->clen : vec->plen,
1998 vec->alen, enc || !cfg->inplace);
1999 if (err == -EOVERFLOW) {
Eric Biggers951d1332019-04-11 21:57:37 -07002000 pr_err("alg: aead: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
2001 driver, op, vec_name, cfg->name);
Eric Biggersed968042019-01-31 23:51:47 -08002002 return err;
Herbert Xuda7f0332008-07-31 17:08:25 +08002003 }
Eric Biggersed968042019-01-31 23:51:47 -08002004 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07002005 pr_err("alg: aead: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
2006 driver, op, vec_name, cfg->name);
Eric Biggersed968042019-01-31 23:51:47 -08002007 return err;
Jussi Kivilinna58dcf542013-06-13 17:37:50 +03002008 }
2009
2010 return 0;
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03002011}
2012
Eric Biggersed968042019-01-31 23:51:47 -08002013static int test_aead_vec(const char *driver, int enc,
2014 const struct aead_testvec *vec, unsigned int vec_num,
2015 struct aead_request *req,
2016 struct cipher_test_sglists *tsgls)
2017{
Eric Biggers951d1332019-04-11 21:57:37 -07002018 char vec_name[16];
Eric Biggersed968042019-01-31 23:51:47 -08002019 unsigned int i;
2020 int err;
2021
2022 if (enc && vec->novrfy)
2023 return 0;
2024
Eric Biggers951d1332019-04-11 21:57:37 -07002025 sprintf(vec_name, "%u", vec_num);
2026
Eric Biggersed968042019-01-31 23:51:47 -08002027 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
Eric Biggers951d1332019-04-11 21:57:37 -07002028 err = test_aead_vec_cfg(driver, enc, vec, vec_name,
Eric Biggersed968042019-01-31 23:51:47 -08002029 &default_cipher_testvec_configs[i],
2030 req, tsgls);
2031 if (err)
2032 return err;
2033 }
2034
2035#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2036 if (!noextratests) {
2037 struct testvec_config cfg;
2038 char cfgname[TESTVEC_CONFIG_NAMELEN];
2039
2040 for (i = 0; i < fuzz_iterations; i++) {
2041 generate_random_testvec_config(&cfg, cfgname,
2042 sizeof(cfgname));
Eric Biggers951d1332019-04-11 21:57:37 -07002043 err = test_aead_vec_cfg(driver, enc, vec, vec_name,
Eric Biggersed968042019-01-31 23:51:47 -08002044 &cfg, req, tsgls);
2045 if (err)
2046 return err;
Eric Biggerse63e1b02019-06-02 22:42:33 -07002047 cond_resched();
Eric Biggersed968042019-01-31 23:51:47 -08002048 }
2049 }
2050#endif
2051 return 0;
2052}
2053
Eric Biggers40153b12019-04-11 21:57:41 -07002054#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2055/*
2056 * Generate an AEAD test vector from the given implementation.
2057 * Assumes the buffers in 'vec' were already allocated.
2058 */
2059static void generate_random_aead_testvec(struct aead_request *req,
2060 struct aead_testvec *vec,
2061 unsigned int maxkeysize,
2062 unsigned int maxdatasize,
2063 char *name, size_t max_namelen)
2064{
2065 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2066 const unsigned int ivsize = crypto_aead_ivsize(tfm);
2067 unsigned int maxauthsize = crypto_aead_alg(tfm)->maxauthsize;
2068 unsigned int authsize;
2069 unsigned int total_len;
2070 int i;
2071 struct scatterlist src[2], dst;
2072 u8 iv[MAX_IVLEN];
2073 DECLARE_CRYPTO_WAIT(wait);
2074
2075 /* Key: length in [0, maxkeysize], but usually choose maxkeysize */
2076 vec->klen = maxkeysize;
2077 if (prandom_u32() % 4 == 0)
2078 vec->klen = prandom_u32() % (maxkeysize + 1);
2079 generate_random_bytes((u8 *)vec->key, vec->klen);
2080 vec->setkey_error = crypto_aead_setkey(tfm, vec->key, vec->klen);
2081
2082 /* IV */
2083 generate_random_bytes((u8 *)vec->iv, ivsize);
2084
2085 /* Tag length: in [0, maxauthsize], but usually choose maxauthsize */
2086 authsize = maxauthsize;
2087 if (prandom_u32() % 4 == 0)
2088 authsize = prandom_u32() % (maxauthsize + 1);
2089 if (WARN_ON(authsize > maxdatasize))
2090 authsize = maxdatasize;
2091 maxdatasize -= authsize;
2092 vec->setauthsize_error = crypto_aead_setauthsize(tfm, authsize);
2093
2094 /* Plaintext and associated data */
2095 total_len = generate_random_length(maxdatasize);
2096 if (prandom_u32() % 4 == 0)
2097 vec->alen = 0;
2098 else
2099 vec->alen = generate_random_length(total_len);
2100 vec->plen = total_len - vec->alen;
2101 generate_random_bytes((u8 *)vec->assoc, vec->alen);
2102 generate_random_bytes((u8 *)vec->ptext, vec->plen);
2103
2104 vec->clen = vec->plen + authsize;
2105
2106 /*
2107 * If the key or authentication tag size couldn't be set, no need to
2108 * continue to encrypt.
2109 */
2110 if (vec->setkey_error || vec->setauthsize_error)
2111 goto done;
2112
2113 /* Ciphertext */
2114 sg_init_table(src, 2);
2115 i = 0;
2116 if (vec->alen)
2117 sg_set_buf(&src[i++], vec->assoc, vec->alen);
2118 if (vec->plen)
2119 sg_set_buf(&src[i++], vec->ptext, vec->plen);
2120 sg_init_one(&dst, vec->ctext, vec->alen + vec->clen);
2121 memcpy(iv, vec->iv, ivsize);
2122 aead_request_set_callback(req, 0, crypto_req_done, &wait);
2123 aead_request_set_crypt(req, src, &dst, vec->plen, iv);
2124 aead_request_set_ad(req, vec->alen);
2125 vec->crypt_error = crypto_wait_req(crypto_aead_encrypt(req), &wait);
2126 if (vec->crypt_error == 0)
2127 memmove((u8 *)vec->ctext, vec->ctext + vec->alen, vec->clen);
2128done:
2129 snprintf(name, max_namelen,
2130 "\"random: alen=%u plen=%u authsize=%u klen=%u\"",
2131 vec->alen, vec->plen, authsize, vec->klen);
2132}
2133
2134/*
2135 * Test the AEAD algorithm represented by @req against the corresponding generic
2136 * implementation, if one is available.
2137 */
2138static int test_aead_vs_generic_impl(const char *driver,
2139 const struct alg_test_desc *test_desc,
2140 struct aead_request *req,
2141 struct cipher_test_sglists *tsgls)
2142{
2143 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2144 const unsigned int ivsize = crypto_aead_ivsize(tfm);
2145 const unsigned int maxauthsize = crypto_aead_alg(tfm)->maxauthsize;
2146 const unsigned int blocksize = crypto_aead_blocksize(tfm);
2147 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
2148 const char *algname = crypto_aead_alg(tfm)->base.cra_name;
2149 const char *generic_driver = test_desc->generic_driver;
2150 char _generic_driver[CRYPTO_MAX_ALG_NAME];
2151 struct crypto_aead *generic_tfm = NULL;
2152 struct aead_request *generic_req = NULL;
2153 unsigned int maxkeysize;
2154 unsigned int i;
2155 struct aead_testvec vec = { 0 };
2156 char vec_name[64];
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002157 struct testvec_config *cfg;
Eric Biggers40153b12019-04-11 21:57:41 -07002158 char cfgname[TESTVEC_CONFIG_NAMELEN];
2159 int err;
2160
2161 if (noextratests)
2162 return 0;
2163
2164 if (!generic_driver) { /* Use default naming convention? */
2165 err = build_generic_driver_name(algname, _generic_driver);
2166 if (err)
2167 return err;
2168 generic_driver = _generic_driver;
2169 }
2170
2171 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
2172 return 0;
2173
2174 generic_tfm = crypto_alloc_aead(generic_driver, 0, 0);
2175 if (IS_ERR(generic_tfm)) {
2176 err = PTR_ERR(generic_tfm);
2177 if (err == -ENOENT) {
2178 pr_warn("alg: aead: skipping comparison tests for %s because %s is unavailable\n",
2179 driver, generic_driver);
2180 return 0;
2181 }
2182 pr_err("alg: aead: error allocating %s (generic impl of %s): %d\n",
2183 generic_driver, algname, err);
2184 return err;
2185 }
2186
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002187 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
2188 if (!cfg) {
2189 err = -ENOMEM;
2190 goto out;
2191 }
2192
Eric Biggers40153b12019-04-11 21:57:41 -07002193 generic_req = aead_request_alloc(generic_tfm, GFP_KERNEL);
2194 if (!generic_req) {
2195 err = -ENOMEM;
2196 goto out;
2197 }
2198
2199 /* Check the algorithm properties for consistency. */
2200
2201 if (maxauthsize != crypto_aead_alg(generic_tfm)->maxauthsize) {
2202 pr_err("alg: aead: maxauthsize for %s (%u) doesn't match generic impl (%u)\n",
2203 driver, maxauthsize,
2204 crypto_aead_alg(generic_tfm)->maxauthsize);
2205 err = -EINVAL;
2206 goto out;
2207 }
2208
2209 if (ivsize != crypto_aead_ivsize(generic_tfm)) {
2210 pr_err("alg: aead: ivsize for %s (%u) doesn't match generic impl (%u)\n",
2211 driver, ivsize, crypto_aead_ivsize(generic_tfm));
2212 err = -EINVAL;
2213 goto out;
2214 }
2215
2216 if (blocksize != crypto_aead_blocksize(generic_tfm)) {
2217 pr_err("alg: aead: blocksize for %s (%u) doesn't match generic impl (%u)\n",
2218 driver, blocksize, crypto_aead_blocksize(generic_tfm));
2219 err = -EINVAL;
2220 goto out;
2221 }
2222
2223 /*
2224 * Now generate test vectors using the generic implementation, and test
2225 * the other implementation against them.
2226 */
2227
2228 maxkeysize = 0;
2229 for (i = 0; i < test_desc->suite.aead.count; i++)
2230 maxkeysize = max_t(unsigned int, maxkeysize,
2231 test_desc->suite.aead.vecs[i].klen);
2232
2233 vec.key = kmalloc(maxkeysize, GFP_KERNEL);
2234 vec.iv = kmalloc(ivsize, GFP_KERNEL);
2235 vec.assoc = kmalloc(maxdatasize, GFP_KERNEL);
2236 vec.ptext = kmalloc(maxdatasize, GFP_KERNEL);
2237 vec.ctext = kmalloc(maxdatasize, GFP_KERNEL);
2238 if (!vec.key || !vec.iv || !vec.assoc || !vec.ptext || !vec.ctext) {
2239 err = -ENOMEM;
2240 goto out;
2241 }
2242
2243 for (i = 0; i < fuzz_iterations * 8; i++) {
2244 generate_random_aead_testvec(generic_req, &vec,
2245 maxkeysize, maxdatasize,
2246 vec_name, sizeof(vec_name));
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002247 generate_random_testvec_config(cfg, cfgname, sizeof(cfgname));
Eric Biggers40153b12019-04-11 21:57:41 -07002248
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002249 err = test_aead_vec_cfg(driver, ENCRYPT, &vec, vec_name, cfg,
Eric Biggers40153b12019-04-11 21:57:41 -07002250 req, tsgls);
2251 if (err)
2252 goto out;
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002253 err = test_aead_vec_cfg(driver, DECRYPT, &vec, vec_name, cfg,
Eric Biggers40153b12019-04-11 21:57:41 -07002254 req, tsgls);
2255 if (err)
2256 goto out;
2257 cond_resched();
2258 }
2259 err = 0;
2260out:
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002261 kfree(cfg);
Eric Biggers40153b12019-04-11 21:57:41 -07002262 kfree(vec.key);
2263 kfree(vec.iv);
2264 kfree(vec.assoc);
2265 kfree(vec.ptext);
2266 kfree(vec.ctext);
2267 crypto_free_aead(generic_tfm);
2268 aead_request_free(generic_req);
2269 return err;
2270}
2271#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
2272static int test_aead_vs_generic_impl(const char *driver,
2273 const struct alg_test_desc *test_desc,
2274 struct aead_request *req,
2275 struct cipher_test_sglists *tsgls)
2276{
2277 return 0;
2278}
2279#endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
2280
Eric Biggersed968042019-01-31 23:51:47 -08002281static int test_aead(const char *driver, int enc,
2282 const struct aead_test_suite *suite,
2283 struct aead_request *req,
2284 struct cipher_test_sglists *tsgls)
2285{
2286 unsigned int i;
2287 int err;
2288
2289 for (i = 0; i < suite->count; i++) {
2290 err = test_aead_vec(driver, enc, &suite->vecs[i], i, req,
2291 tsgls);
2292 if (err)
2293 return err;
Eric Biggerse63e1b02019-06-02 22:42:33 -07002294 cond_resched();
Eric Biggersed968042019-01-31 23:51:47 -08002295 }
2296 return 0;
2297}
2298
2299static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
2300 u32 type, u32 mask)
2301{
2302 const struct aead_test_suite *suite = &desc->suite.aead;
2303 struct crypto_aead *tfm;
2304 struct aead_request *req = NULL;
2305 struct cipher_test_sglists *tsgls = NULL;
2306 int err;
2307
2308 if (suite->count <= 0) {
2309 pr_err("alg: aead: empty test suite for %s\n", driver);
2310 return -EINVAL;
2311 }
2312
2313 tfm = crypto_alloc_aead(driver, type, mask);
2314 if (IS_ERR(tfm)) {
2315 pr_err("alg: aead: failed to allocate transform for %s: %ld\n",
2316 driver, PTR_ERR(tfm));
2317 return PTR_ERR(tfm);
2318 }
2319
2320 req = aead_request_alloc(tfm, GFP_KERNEL);
2321 if (!req) {
2322 pr_err("alg: aead: failed to allocate request for %s\n",
2323 driver);
2324 err = -ENOMEM;
2325 goto out;
2326 }
2327
2328 tsgls = alloc_cipher_test_sglists();
2329 if (!tsgls) {
2330 pr_err("alg: aead: failed to allocate test buffers for %s\n",
2331 driver);
2332 err = -ENOMEM;
2333 goto out;
2334 }
2335
2336 err = test_aead(driver, ENCRYPT, suite, req, tsgls);
2337 if (err)
2338 goto out;
2339
2340 err = test_aead(driver, DECRYPT, suite, req, tsgls);
Eric Biggers40153b12019-04-11 21:57:41 -07002341 if (err)
2342 goto out;
2343
2344 err = test_aead_vs_generic_impl(driver, desc, req, tsgls);
Eric Biggersed968042019-01-31 23:51:47 -08002345out:
2346 free_cipher_test_sglists(tsgls);
2347 aead_request_free(req);
2348 crypto_free_aead(tfm);
2349 return err;
2350}
2351
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002352static int test_cipher(struct crypto_cipher *tfm, int enc,
Eric Biggersb13b1e02017-02-24 15:46:59 -08002353 const struct cipher_testvec *template,
2354 unsigned int tcount)
Herbert Xuda7f0332008-07-31 17:08:25 +08002355{
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002356 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
2357 unsigned int i, j, k;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002358 char *q;
2359 const char *e;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002360 const char *input, *result;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002361 void *data;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08002362 char *xbuf[XBUFSIZE];
2363 int ret = -ENOMEM;
2364
2365 if (testmgr_alloc_buf(xbuf))
2366 goto out_nobuf;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002367
2368 if (enc == ENCRYPT)
2369 e = "encryption";
2370 else
2371 e = "decryption";
2372
2373 j = 0;
2374 for (i = 0; i < tcount; i++) {
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002375
Stephan Mueller10faa8c2016-08-25 15:15:01 +02002376 if (fips_enabled && template[i].fips_skip)
2377 continue;
2378
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002379 input = enc ? template[i].ptext : template[i].ctext;
2380 result = enc ? template[i].ctext : template[i].ptext;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002381 j++;
2382
Herbert Xufd57f222009-05-29 16:05:42 +10002383 ret = -EINVAL;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002384 if (WARN_ON(template[i].len > PAGE_SIZE))
Herbert Xufd57f222009-05-29 16:05:42 +10002385 goto out;
2386
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002387 data = xbuf[0];
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002388 memcpy(data, input, template[i].len);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002389
2390 crypto_cipher_clear_flags(tfm, ~0);
2391 if (template[i].wk)
Eric Biggers231baec2019-01-18 22:48:00 -08002392 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002393
2394 ret = crypto_cipher_setkey(tfm, template[i].key,
2395 template[i].klen);
Eric Biggers5283a8e2019-04-11 21:57:36 -07002396 if (ret) {
2397 if (ret == template[i].setkey_error)
2398 continue;
2399 pr_err("alg: cipher: %s setkey failed on test vector %u; expected_error=%d, actual_error=%d, flags=%#x\n",
2400 algo, j, template[i].setkey_error, ret,
2401 crypto_cipher_get_flags(tfm));
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002402 goto out;
Eric Biggers5283a8e2019-04-11 21:57:36 -07002403 }
2404 if (template[i].setkey_error) {
2405 pr_err("alg: cipher: %s setkey unexpectedly succeeded on test vector %u; expected_error=%d\n",
2406 algo, j, template[i].setkey_error);
2407 ret = -EINVAL;
2408 goto out;
2409 }
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002410
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002411 for (k = 0; k < template[i].len;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002412 k += crypto_cipher_blocksize(tfm)) {
2413 if (enc)
2414 crypto_cipher_encrypt_one(tfm, data + k,
2415 data + k);
2416 else
2417 crypto_cipher_decrypt_one(tfm, data + k,
2418 data + k);
2419 }
2420
2421 q = data;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002422 if (memcmp(q, result, template[i].len)) {
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002423 printk(KERN_ERR "alg: cipher: Test %d failed "
2424 "on %s for %s\n", j, e, algo);
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002425 hexdump(q, template[i].len);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002426 ret = -EINVAL;
2427 goto out;
2428 }
2429 }
2430
2431 ret = 0;
2432
2433out:
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08002434 testmgr_free_buf(xbuf);
2435out_nobuf:
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002436 return ret;
2437}
2438
Eric Biggers4e7babba2019-01-31 23:51:46 -08002439static int test_skcipher_vec_cfg(const char *driver, int enc,
2440 const struct cipher_testvec *vec,
Eric Biggers951d1332019-04-11 21:57:37 -07002441 const char *vec_name,
Eric Biggers4e7babba2019-01-31 23:51:46 -08002442 const struct testvec_config *cfg,
2443 struct skcipher_request *req,
2444 struct cipher_test_sglists *tsgls)
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002445{
Eric Biggers4e7babba2019-01-31 23:51:46 -08002446 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2447 const unsigned int alignmask = crypto_skcipher_alignmask(tfm);
2448 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2449 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
2450 const char *op = enc ? "encryption" : "decryption";
2451 DECLARE_CRYPTO_WAIT(wait);
2452 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
2453 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
2454 cfg->iv_offset +
2455 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
2456 struct kvec input;
2457 int err;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08002458
Eric Biggers4e7babba2019-01-31 23:51:46 -08002459 /* Set the key */
2460 if (vec->wk)
2461 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03002462 else
Eric Biggers4e7babba2019-01-31 23:51:46 -08002463 crypto_skcipher_clear_flags(tfm,
2464 CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
2465 err = crypto_skcipher_setkey(tfm, vec->key, vec->klen);
2466 if (err) {
Eric Biggers5283a8e2019-04-11 21:57:36 -07002467 if (err == vec->setkey_error)
Eric Biggers4e7babba2019-01-31 23:51:46 -08002468 return 0;
Eric Biggers951d1332019-04-11 21:57:37 -07002469 pr_err("alg: skcipher: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
2470 driver, vec_name, vec->setkey_error, err,
Eric Biggers5283a8e2019-04-11 21:57:36 -07002471 crypto_skcipher_get_flags(tfm));
Eric Biggers4e7babba2019-01-31 23:51:46 -08002472 return err;
2473 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07002474 if (vec->setkey_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07002475 pr_err("alg: skcipher: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
2476 driver, vec_name, vec->setkey_error);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002477 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08002478 }
2479
Eric Biggers4e7babba2019-01-31 23:51:46 -08002480 /* The IV must be copied to a buffer, as the algorithm may modify it */
2481 if (ivsize) {
2482 if (WARN_ON(ivsize > MAX_IVLEN))
2483 return -EINVAL;
Eric Biggers8efd9722019-02-14 00:03:51 -08002484 if (vec->generates_iv && !enc)
2485 memcpy(iv, vec->iv_out, ivsize);
2486 else if (vec->iv)
Eric Biggers4e7babba2019-01-31 23:51:46 -08002487 memcpy(iv, vec->iv, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08002488 else
Eric Biggers4e7babba2019-01-31 23:51:46 -08002489 memset(iv, 0, ivsize);
2490 } else {
2491 if (vec->generates_iv) {
Eric Biggers951d1332019-04-11 21:57:37 -07002492 pr_err("alg: skcipher: %s has ivsize=0 but test vector %s generates IV!\n",
2493 driver, vec_name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002494 return -EINVAL;
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03002495 }
Eric Biggers4e7babba2019-01-31 23:51:46 -08002496 iv = NULL;
Herbert Xuda7f0332008-07-31 17:08:25 +08002497 }
2498
Eric Biggers4e7babba2019-01-31 23:51:46 -08002499 /* Build the src/dst scatterlists */
2500 input.iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
2501 input.iov_len = vec->len;
2502 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
2503 vec->len, vec->len, &input, 1);
2504 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07002505 pr_err("alg: skcipher: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
2506 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002507 return err;
Herbert Xuda7f0332008-07-31 17:08:25 +08002508 }
2509
Eric Biggers4e7babba2019-01-31 23:51:46 -08002510 /* Do the actual encryption or decryption */
2511 testmgr_poison(req->__ctx, crypto_skcipher_reqsize(tfm));
2512 skcipher_request_set_callback(req, req_flags, crypto_req_done, &wait);
2513 skcipher_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
2514 vec->len, iv);
Eric Biggers65707372019-03-12 22:12:52 -07002515 if (cfg->nosimd)
2516 crypto_disable_simd_for_test();
2517 err = enc ? crypto_skcipher_encrypt(req) : crypto_skcipher_decrypt(req);
2518 if (cfg->nosimd)
2519 crypto_reenable_simd_for_test();
2520 err = crypto_wait_req(err, &wait);
Herbert Xuda7f0332008-07-31 17:08:25 +08002521
Eric Biggersfa353c92019-01-31 23:51:49 -08002522 /* Check that the algorithm didn't overwrite things it shouldn't have */
2523 if (req->cryptlen != vec->len ||
2524 req->iv != iv ||
2525 req->src != tsgls->src.sgl_ptr ||
2526 req->dst != tsgls->dst.sgl_ptr ||
2527 crypto_skcipher_reqtfm(req) != tfm ||
2528 req->base.complete != crypto_req_done ||
2529 req->base.flags != req_flags ||
2530 req->base.data != &wait) {
Eric Biggers951d1332019-04-11 21:57:37 -07002531 pr_err("alg: skcipher: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
2532 driver, op, vec_name, cfg->name);
Eric Biggersfa353c92019-01-31 23:51:49 -08002533 if (req->cryptlen != vec->len)
2534 pr_err("alg: skcipher: changed 'req->cryptlen'\n");
2535 if (req->iv != iv)
2536 pr_err("alg: skcipher: changed 'req->iv'\n");
2537 if (req->src != tsgls->src.sgl_ptr)
2538 pr_err("alg: skcipher: changed 'req->src'\n");
2539 if (req->dst != tsgls->dst.sgl_ptr)
2540 pr_err("alg: skcipher: changed 'req->dst'\n");
2541 if (crypto_skcipher_reqtfm(req) != tfm)
2542 pr_err("alg: skcipher: changed 'req->base.tfm'\n");
2543 if (req->base.complete != crypto_req_done)
2544 pr_err("alg: skcipher: changed 'req->base.complete'\n");
2545 if (req->base.flags != req_flags)
2546 pr_err("alg: skcipher: changed 'req->base.flags'\n");
2547 if (req->base.data != &wait)
2548 pr_err("alg: skcipher: changed 'req->base.data'\n");
2549 return -EINVAL;
2550 }
2551 if (is_test_sglist_corrupted(&tsgls->src)) {
Eric Biggers951d1332019-04-11 21:57:37 -07002552 pr_err("alg: skcipher: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
2553 driver, op, vec_name, cfg->name);
Eric Biggersfa353c92019-01-31 23:51:49 -08002554 return -EINVAL;
2555 }
2556 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
2557 is_test_sglist_corrupted(&tsgls->dst)) {
Eric Biggers951d1332019-04-11 21:57:37 -07002558 pr_err("alg: skcipher: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
2559 driver, op, vec_name, cfg->name);
Eric Biggersfa353c92019-01-31 23:51:49 -08002560 return -EINVAL;
2561 }
2562
Eric Biggers5283a8e2019-04-11 21:57:36 -07002563 /* Check for success or failure */
2564 if (err) {
2565 if (err == vec->crypt_error)
2566 return 0;
Eric Biggers951d1332019-04-11 21:57:37 -07002567 pr_err("alg: skcipher: %s %s failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
2568 driver, op, vec_name, vec->crypt_error, err, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07002569 return err;
2570 }
2571 if (vec->crypt_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07002572 pr_err("alg: skcipher: %s %s unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
2573 driver, op, vec_name, vec->crypt_error, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07002574 return -EINVAL;
2575 }
2576
Eric Biggers4e7babba2019-01-31 23:51:46 -08002577 /* Check for the correct output (ciphertext or plaintext) */
2578 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
2579 vec->len, 0, true);
2580 if (err == -EOVERFLOW) {
Eric Biggers951d1332019-04-11 21:57:37 -07002581 pr_err("alg: skcipher: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
2582 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002583 return err;
2584 }
2585 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07002586 pr_err("alg: skcipher: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
2587 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002588 return err;
2589 }
Herbert Xuda7f0332008-07-31 17:08:25 +08002590
Eric Biggers4e7babba2019-01-31 23:51:46 -08002591 /* If applicable, check that the algorithm generated the correct IV */
Eric Biggers8efd9722019-02-14 00:03:51 -08002592 if (vec->iv_out && memcmp(iv, vec->iv_out, ivsize) != 0) {
Eric Biggers951d1332019-04-11 21:57:37 -07002593 pr_err("alg: skcipher: %s %s test failed (wrong output IV) on test vector %s, cfg=\"%s\"\n",
2594 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002595 hexdump(iv, ivsize);
2596 return -EINVAL;
Jussi Kivilinna3a338f22013-06-13 17:37:45 +03002597 }
2598
2599 return 0;
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03002600}
2601
Eric Biggers4e7babba2019-01-31 23:51:46 -08002602static int test_skcipher_vec(const char *driver, int enc,
2603 const struct cipher_testvec *vec,
2604 unsigned int vec_num,
2605 struct skcipher_request *req,
2606 struct cipher_test_sglists *tsgls)
2607{
Eric Biggers951d1332019-04-11 21:57:37 -07002608 char vec_name[16];
Eric Biggers4e7babba2019-01-31 23:51:46 -08002609 unsigned int i;
2610 int err;
2611
2612 if (fips_enabled && vec->fips_skip)
2613 return 0;
2614
Eric Biggers951d1332019-04-11 21:57:37 -07002615 sprintf(vec_name, "%u", vec_num);
2616
Eric Biggers4e7babba2019-01-31 23:51:46 -08002617 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
Eric Biggers951d1332019-04-11 21:57:37 -07002618 err = test_skcipher_vec_cfg(driver, enc, vec, vec_name,
Eric Biggers4e7babba2019-01-31 23:51:46 -08002619 &default_cipher_testvec_configs[i],
2620 req, tsgls);
2621 if (err)
2622 return err;
2623 }
2624
2625#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2626 if (!noextratests) {
2627 struct testvec_config cfg;
2628 char cfgname[TESTVEC_CONFIG_NAMELEN];
2629
2630 for (i = 0; i < fuzz_iterations; i++) {
2631 generate_random_testvec_config(&cfg, cfgname,
2632 sizeof(cfgname));
Eric Biggers951d1332019-04-11 21:57:37 -07002633 err = test_skcipher_vec_cfg(driver, enc, vec, vec_name,
Eric Biggers4e7babba2019-01-31 23:51:46 -08002634 &cfg, req, tsgls);
2635 if (err)
2636 return err;
Eric Biggerse63e1b02019-06-02 22:42:33 -07002637 cond_resched();
Eric Biggers4e7babba2019-01-31 23:51:46 -08002638 }
2639 }
2640#endif
2641 return 0;
2642}
2643
Eric Biggersd435e102019-04-11 21:57:40 -07002644#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2645/*
2646 * Generate a symmetric cipher test vector from the given implementation.
2647 * Assumes the buffers in 'vec' were already allocated.
2648 */
2649static void generate_random_cipher_testvec(struct skcipher_request *req,
2650 struct cipher_testvec *vec,
2651 unsigned int maxdatasize,
2652 char *name, size_t max_namelen)
2653{
2654 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2655 const unsigned int maxkeysize = tfm->keysize;
2656 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2657 struct scatterlist src, dst;
2658 u8 iv[MAX_IVLEN];
2659 DECLARE_CRYPTO_WAIT(wait);
2660
2661 /* Key: length in [0, maxkeysize], but usually choose maxkeysize */
2662 vec->klen = maxkeysize;
2663 if (prandom_u32() % 4 == 0)
2664 vec->klen = prandom_u32() % (maxkeysize + 1);
2665 generate_random_bytes((u8 *)vec->key, vec->klen);
2666 vec->setkey_error = crypto_skcipher_setkey(tfm, vec->key, vec->klen);
2667
2668 /* IV */
2669 generate_random_bytes((u8 *)vec->iv, ivsize);
2670
2671 /* Plaintext */
2672 vec->len = generate_random_length(maxdatasize);
2673 generate_random_bytes((u8 *)vec->ptext, vec->len);
2674
2675 /* If the key couldn't be set, no need to continue to encrypt. */
2676 if (vec->setkey_error)
2677 goto done;
2678
2679 /* Ciphertext */
2680 sg_init_one(&src, vec->ptext, vec->len);
2681 sg_init_one(&dst, vec->ctext, vec->len);
2682 memcpy(iv, vec->iv, ivsize);
2683 skcipher_request_set_callback(req, 0, crypto_req_done, &wait);
2684 skcipher_request_set_crypt(req, &src, &dst, vec->len, iv);
2685 vec->crypt_error = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
2686done:
2687 snprintf(name, max_namelen, "\"random: len=%u klen=%u\"",
2688 vec->len, vec->klen);
2689}
2690
2691/*
2692 * Test the skcipher algorithm represented by @req against the corresponding
2693 * generic implementation, if one is available.
2694 */
2695static int test_skcipher_vs_generic_impl(const char *driver,
2696 const char *generic_driver,
2697 struct skcipher_request *req,
2698 struct cipher_test_sglists *tsgls)
2699{
2700 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2701 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2702 const unsigned int blocksize = crypto_skcipher_blocksize(tfm);
2703 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
2704 const char *algname = crypto_skcipher_alg(tfm)->base.cra_name;
2705 char _generic_driver[CRYPTO_MAX_ALG_NAME];
2706 struct crypto_skcipher *generic_tfm = NULL;
2707 struct skcipher_request *generic_req = NULL;
2708 unsigned int i;
2709 struct cipher_testvec vec = { 0 };
2710 char vec_name[64];
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002711 struct testvec_config *cfg;
Eric Biggersd435e102019-04-11 21:57:40 -07002712 char cfgname[TESTVEC_CONFIG_NAMELEN];
2713 int err;
2714
2715 if (noextratests)
2716 return 0;
2717
2718 /* Keywrap isn't supported here yet as it handles its IV differently. */
2719 if (strncmp(algname, "kw(", 3) == 0)
2720 return 0;
2721
2722 if (!generic_driver) { /* Use default naming convention? */
2723 err = build_generic_driver_name(algname, _generic_driver);
2724 if (err)
2725 return err;
2726 generic_driver = _generic_driver;
2727 }
2728
2729 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
2730 return 0;
2731
2732 generic_tfm = crypto_alloc_skcipher(generic_driver, 0, 0);
2733 if (IS_ERR(generic_tfm)) {
2734 err = PTR_ERR(generic_tfm);
2735 if (err == -ENOENT) {
2736 pr_warn("alg: skcipher: skipping comparison tests for %s because %s is unavailable\n",
2737 driver, generic_driver);
2738 return 0;
2739 }
2740 pr_err("alg: skcipher: error allocating %s (generic impl of %s): %d\n",
2741 generic_driver, algname, err);
2742 return err;
2743 }
2744
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002745 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
2746 if (!cfg) {
2747 err = -ENOMEM;
2748 goto out;
2749 }
2750
Eric Biggersd435e102019-04-11 21:57:40 -07002751 generic_req = skcipher_request_alloc(generic_tfm, GFP_KERNEL);
2752 if (!generic_req) {
2753 err = -ENOMEM;
2754 goto out;
2755 }
2756
2757 /* Check the algorithm properties for consistency. */
2758
2759 if (tfm->keysize != generic_tfm->keysize) {
2760 pr_err("alg: skcipher: max keysize for %s (%u) doesn't match generic impl (%u)\n",
2761 driver, tfm->keysize, generic_tfm->keysize);
2762 err = -EINVAL;
2763 goto out;
2764 }
2765
2766 if (ivsize != crypto_skcipher_ivsize(generic_tfm)) {
2767 pr_err("alg: skcipher: ivsize for %s (%u) doesn't match generic impl (%u)\n",
2768 driver, ivsize, crypto_skcipher_ivsize(generic_tfm));
2769 err = -EINVAL;
2770 goto out;
2771 }
2772
2773 if (blocksize != crypto_skcipher_blocksize(generic_tfm)) {
2774 pr_err("alg: skcipher: blocksize for %s (%u) doesn't match generic impl (%u)\n",
2775 driver, blocksize,
2776 crypto_skcipher_blocksize(generic_tfm));
2777 err = -EINVAL;
2778 goto out;
2779 }
2780
2781 /*
2782 * Now generate test vectors using the generic implementation, and test
2783 * the other implementation against them.
2784 */
2785
2786 vec.key = kmalloc(tfm->keysize, GFP_KERNEL);
2787 vec.iv = kmalloc(ivsize, GFP_KERNEL);
2788 vec.ptext = kmalloc(maxdatasize, GFP_KERNEL);
2789 vec.ctext = kmalloc(maxdatasize, GFP_KERNEL);
2790 if (!vec.key || !vec.iv || !vec.ptext || !vec.ctext) {
2791 err = -ENOMEM;
2792 goto out;
2793 }
2794
2795 for (i = 0; i < fuzz_iterations * 8; i++) {
2796 generate_random_cipher_testvec(generic_req, &vec, maxdatasize,
2797 vec_name, sizeof(vec_name));
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002798 generate_random_testvec_config(cfg, cfgname, sizeof(cfgname));
Eric Biggersd435e102019-04-11 21:57:40 -07002799
2800 err = test_skcipher_vec_cfg(driver, ENCRYPT, &vec, vec_name,
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002801 cfg, req, tsgls);
Eric Biggersd435e102019-04-11 21:57:40 -07002802 if (err)
2803 goto out;
2804 err = test_skcipher_vec_cfg(driver, DECRYPT, &vec, vec_name,
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002805 cfg, req, tsgls);
Eric Biggersd435e102019-04-11 21:57:40 -07002806 if (err)
2807 goto out;
2808 cond_resched();
2809 }
2810 err = 0;
2811out:
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002812 kfree(cfg);
Eric Biggersd435e102019-04-11 21:57:40 -07002813 kfree(vec.key);
2814 kfree(vec.iv);
2815 kfree(vec.ptext);
2816 kfree(vec.ctext);
2817 crypto_free_skcipher(generic_tfm);
2818 skcipher_request_free(generic_req);
2819 return err;
2820}
2821#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
2822static int test_skcipher_vs_generic_impl(const char *driver,
2823 const char *generic_driver,
2824 struct skcipher_request *req,
2825 struct cipher_test_sglists *tsgls)
2826{
2827 return 0;
2828}
2829#endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
2830
Eric Biggers4e7babba2019-01-31 23:51:46 -08002831static int test_skcipher(const char *driver, int enc,
2832 const struct cipher_test_suite *suite,
2833 struct skcipher_request *req,
2834 struct cipher_test_sglists *tsgls)
2835{
2836 unsigned int i;
2837 int err;
2838
2839 for (i = 0; i < suite->count; i++) {
2840 err = test_skcipher_vec(driver, enc, &suite->vecs[i], i, req,
2841 tsgls);
2842 if (err)
2843 return err;
Eric Biggerse63e1b02019-06-02 22:42:33 -07002844 cond_resched();
Eric Biggers4e7babba2019-01-31 23:51:46 -08002845 }
2846 return 0;
2847}
2848
2849static int alg_test_skcipher(const struct alg_test_desc *desc,
2850 const char *driver, u32 type, u32 mask)
2851{
2852 const struct cipher_test_suite *suite = &desc->suite.cipher;
2853 struct crypto_skcipher *tfm;
2854 struct skcipher_request *req = NULL;
2855 struct cipher_test_sglists *tsgls = NULL;
2856 int err;
2857
2858 if (suite->count <= 0) {
2859 pr_err("alg: skcipher: empty test suite for %s\n", driver);
2860 return -EINVAL;
2861 }
2862
2863 tfm = crypto_alloc_skcipher(driver, type, mask);
2864 if (IS_ERR(tfm)) {
2865 pr_err("alg: skcipher: failed to allocate transform for %s: %ld\n",
2866 driver, PTR_ERR(tfm));
2867 return PTR_ERR(tfm);
2868 }
2869
2870 req = skcipher_request_alloc(tfm, GFP_KERNEL);
2871 if (!req) {
2872 pr_err("alg: skcipher: failed to allocate request for %s\n",
2873 driver);
2874 err = -ENOMEM;
2875 goto out;
2876 }
2877
2878 tsgls = alloc_cipher_test_sglists();
2879 if (!tsgls) {
2880 pr_err("alg: skcipher: failed to allocate test buffers for %s\n",
2881 driver);
2882 err = -ENOMEM;
2883 goto out;
2884 }
2885
2886 err = test_skcipher(driver, ENCRYPT, suite, req, tsgls);
2887 if (err)
2888 goto out;
2889
2890 err = test_skcipher(driver, DECRYPT, suite, req, tsgls);
Eric Biggersd435e102019-04-11 21:57:40 -07002891 if (err)
2892 goto out;
2893
2894 err = test_skcipher_vs_generic_impl(driver, desc->generic_driver, req,
2895 tsgls);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002896out:
2897 free_cipher_test_sglists(tsgls);
2898 skcipher_request_free(req);
2899 crypto_free_skcipher(tfm);
2900 return err;
2901}
2902
Eric Biggersb13b1e02017-02-24 15:46:59 -08002903static int test_comp(struct crypto_comp *tfm,
2904 const struct comp_testvec *ctemplate,
2905 const struct comp_testvec *dtemplate,
2906 int ctcount, int dtcount)
Herbert Xuda7f0332008-07-31 17:08:25 +08002907{
2908 const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
Mahipal Challa33607382018-04-11 20:28:32 +02002909 char *output, *decomp_output;
Herbert Xuda7f0332008-07-31 17:08:25 +08002910 unsigned int i;
Herbert Xuda7f0332008-07-31 17:08:25 +08002911 int ret;
2912
Mahipal Challa33607382018-04-11 20:28:32 +02002913 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
2914 if (!output)
2915 return -ENOMEM;
2916
2917 decomp_output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
2918 if (!decomp_output) {
2919 kfree(output);
2920 return -ENOMEM;
2921 }
2922
Herbert Xuda7f0332008-07-31 17:08:25 +08002923 for (i = 0; i < ctcount; i++) {
Geert Uytterhoevenc79cf912009-03-29 15:44:19 +08002924 int ilen;
2925 unsigned int dlen = COMP_BUF_SIZE;
Herbert Xuda7f0332008-07-31 17:08:25 +08002926
Michael Schupikov22a81182018-10-07 13:58:10 +02002927 memset(output, 0, COMP_BUF_SIZE);
2928 memset(decomp_output, 0, COMP_BUF_SIZE);
Herbert Xuda7f0332008-07-31 17:08:25 +08002929
2930 ilen = ctemplate[i].inlen;
2931 ret = crypto_comp_compress(tfm, ctemplate[i].input,
Mahipal Challa33607382018-04-11 20:28:32 +02002932 ilen, output, &dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08002933 if (ret) {
2934 printk(KERN_ERR "alg: comp: compression failed "
2935 "on test %d for %s: ret=%d\n", i + 1, algo,
2936 -ret);
2937 goto out;
2938 }
2939
Mahipal Challa33607382018-04-11 20:28:32 +02002940 ilen = dlen;
2941 dlen = COMP_BUF_SIZE;
2942 ret = crypto_comp_decompress(tfm, output,
2943 ilen, decomp_output, &dlen);
2944 if (ret) {
2945 pr_err("alg: comp: compression failed: decompress: on test %d for %s failed: ret=%d\n",
2946 i + 1, algo, -ret);
2947 goto out;
2948 }
2949
2950 if (dlen != ctemplate[i].inlen) {
Geert Uytterhoevenb812eb02008-11-28 20:51:28 +08002951 printk(KERN_ERR "alg: comp: Compression test %d "
2952 "failed for %s: output len = %d\n", i + 1, algo,
2953 dlen);
2954 ret = -EINVAL;
2955 goto out;
2956 }
2957
Mahipal Challa33607382018-04-11 20:28:32 +02002958 if (memcmp(decomp_output, ctemplate[i].input,
2959 ctemplate[i].inlen)) {
2960 pr_err("alg: comp: compression failed: output differs: on test %d for %s\n",
2961 i + 1, algo);
2962 hexdump(decomp_output, dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08002963 ret = -EINVAL;
2964 goto out;
2965 }
2966 }
2967
2968 for (i = 0; i < dtcount; i++) {
Geert Uytterhoevenc79cf912009-03-29 15:44:19 +08002969 int ilen;
2970 unsigned int dlen = COMP_BUF_SIZE;
Herbert Xuda7f0332008-07-31 17:08:25 +08002971
Michael Schupikov22a81182018-10-07 13:58:10 +02002972 memset(decomp_output, 0, COMP_BUF_SIZE);
Herbert Xuda7f0332008-07-31 17:08:25 +08002973
2974 ilen = dtemplate[i].inlen;
2975 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
Mahipal Challa33607382018-04-11 20:28:32 +02002976 ilen, decomp_output, &dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08002977 if (ret) {
2978 printk(KERN_ERR "alg: comp: decompression failed "
2979 "on test %d for %s: ret=%d\n", i + 1, algo,
2980 -ret);
2981 goto out;
2982 }
2983
Geert Uytterhoevenb812eb02008-11-28 20:51:28 +08002984 if (dlen != dtemplate[i].outlen) {
2985 printk(KERN_ERR "alg: comp: Decompression test %d "
2986 "failed for %s: output len = %d\n", i + 1, algo,
2987 dlen);
2988 ret = -EINVAL;
2989 goto out;
2990 }
2991
Mahipal Challa33607382018-04-11 20:28:32 +02002992 if (memcmp(decomp_output, dtemplate[i].output, dlen)) {
Herbert Xuda7f0332008-07-31 17:08:25 +08002993 printk(KERN_ERR "alg: comp: Decompression test %d "
2994 "failed for %s\n", i + 1, algo);
Mahipal Challa33607382018-04-11 20:28:32 +02002995 hexdump(decomp_output, dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08002996 ret = -EINVAL;
2997 goto out;
2998 }
2999 }
3000
3001 ret = 0;
3002
3003out:
Mahipal Challa33607382018-04-11 20:28:32 +02003004 kfree(decomp_output);
3005 kfree(output);
Herbert Xuda7f0332008-07-31 17:08:25 +08003006 return ret;
3007}
3008
Eric Biggersb13b1e02017-02-24 15:46:59 -08003009static int test_acomp(struct crypto_acomp *tfm,
Mahipal Challa33607382018-04-11 20:28:32 +02003010 const struct comp_testvec *ctemplate,
Eric Biggersb13b1e02017-02-24 15:46:59 -08003011 const struct comp_testvec *dtemplate,
3012 int ctcount, int dtcount)
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003013{
3014 const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
3015 unsigned int i;
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003016 char *output, *decomp_out;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003017 int ret;
3018 struct scatterlist src, dst;
3019 struct acomp_req *req;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003020 struct crypto_wait wait;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003021
Eric Biggerseb095592016-11-23 10:24:35 -08003022 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3023 if (!output)
3024 return -ENOMEM;
3025
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003026 decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3027 if (!decomp_out) {
3028 kfree(output);
3029 return -ENOMEM;
3030 }
3031
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003032 for (i = 0; i < ctcount; i++) {
3033 unsigned int dlen = COMP_BUF_SIZE;
3034 int ilen = ctemplate[i].inlen;
Laura Abbott02608e02016-12-21 12:32:54 -08003035 void *input_vec;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003036
Eric Biggersd2110222016-12-30 14:12:00 -06003037 input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL);
Laura Abbott02608e02016-12-21 12:32:54 -08003038 if (!input_vec) {
3039 ret = -ENOMEM;
3040 goto out;
3041 }
3042
Eric Biggerseb095592016-11-23 10:24:35 -08003043 memset(output, 0, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003044 crypto_init_wait(&wait);
Laura Abbott02608e02016-12-21 12:32:54 -08003045 sg_init_one(&src, input_vec, ilen);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003046 sg_init_one(&dst, output, dlen);
3047
3048 req = acomp_request_alloc(tfm);
3049 if (!req) {
3050 pr_err("alg: acomp: request alloc failed for %s\n",
3051 algo);
Laura Abbott02608e02016-12-21 12:32:54 -08003052 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003053 ret = -ENOMEM;
3054 goto out;
3055 }
3056
3057 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3058 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003059 crypto_req_done, &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003060
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003061 ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003062 if (ret) {
3063 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
3064 i + 1, algo, -ret);
Laura Abbott02608e02016-12-21 12:32:54 -08003065 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003066 acomp_request_free(req);
3067 goto out;
3068 }
3069
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003070 ilen = req->dlen;
3071 dlen = COMP_BUF_SIZE;
3072 sg_init_one(&src, output, ilen);
3073 sg_init_one(&dst, decomp_out, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003074 crypto_init_wait(&wait);
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003075 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3076
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003077 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003078 if (ret) {
3079 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
3080 i + 1, algo, -ret);
3081 kfree(input_vec);
3082 acomp_request_free(req);
3083 goto out;
3084 }
3085
3086 if (req->dlen != ctemplate[i].inlen) {
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003087 pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
3088 i + 1, algo, req->dlen);
3089 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08003090 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003091 acomp_request_free(req);
3092 goto out;
3093 }
3094
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003095 if (memcmp(input_vec, decomp_out, req->dlen)) {
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003096 pr_err("alg: acomp: Compression test %d failed for %s\n",
3097 i + 1, algo);
3098 hexdump(output, req->dlen);
3099 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08003100 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003101 acomp_request_free(req);
3102 goto out;
3103 }
3104
Laura Abbott02608e02016-12-21 12:32:54 -08003105 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003106 acomp_request_free(req);
3107 }
3108
3109 for (i = 0; i < dtcount; i++) {
3110 unsigned int dlen = COMP_BUF_SIZE;
3111 int ilen = dtemplate[i].inlen;
Laura Abbott02608e02016-12-21 12:32:54 -08003112 void *input_vec;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003113
Eric Biggersd2110222016-12-30 14:12:00 -06003114 input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL);
Laura Abbott02608e02016-12-21 12:32:54 -08003115 if (!input_vec) {
3116 ret = -ENOMEM;
3117 goto out;
3118 }
3119
Eric Biggerseb095592016-11-23 10:24:35 -08003120 memset(output, 0, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003121 crypto_init_wait(&wait);
Laura Abbott02608e02016-12-21 12:32:54 -08003122 sg_init_one(&src, input_vec, ilen);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003123 sg_init_one(&dst, output, dlen);
3124
3125 req = acomp_request_alloc(tfm);
3126 if (!req) {
3127 pr_err("alg: acomp: request alloc failed for %s\n",
3128 algo);
Laura Abbott02608e02016-12-21 12:32:54 -08003129 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003130 ret = -ENOMEM;
3131 goto out;
3132 }
3133
3134 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3135 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003136 crypto_req_done, &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003137
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003138 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003139 if (ret) {
3140 pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
3141 i + 1, algo, -ret);
Laura Abbott02608e02016-12-21 12:32:54 -08003142 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003143 acomp_request_free(req);
3144 goto out;
3145 }
3146
3147 if (req->dlen != dtemplate[i].outlen) {
3148 pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
3149 i + 1, algo, req->dlen);
3150 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08003151 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003152 acomp_request_free(req);
3153 goto out;
3154 }
3155
3156 if (memcmp(output, dtemplate[i].output, req->dlen)) {
3157 pr_err("alg: acomp: Decompression test %d failed for %s\n",
3158 i + 1, algo);
3159 hexdump(output, req->dlen);
3160 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08003161 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003162 acomp_request_free(req);
3163 goto out;
3164 }
3165
Laura Abbott02608e02016-12-21 12:32:54 -08003166 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003167 acomp_request_free(req);
3168 }
3169
3170 ret = 0;
3171
3172out:
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003173 kfree(decomp_out);
Eric Biggerseb095592016-11-23 10:24:35 -08003174 kfree(output);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003175 return ret;
3176}
3177
Eric Biggersb13b1e02017-02-24 15:46:59 -08003178static int test_cprng(struct crypto_rng *tfm,
3179 const struct cprng_testvec *template,
Jarod Wilson7647d6c2009-05-04 19:44:50 +08003180 unsigned int tcount)
3181{
3182 const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
Felipe Contrerasfa4ef8a2009-10-27 19:04:42 +08003183 int err = 0, i, j, seedsize;
Jarod Wilson7647d6c2009-05-04 19:44:50 +08003184 u8 *seed;
3185 char result[32];
3186
3187 seedsize = crypto_rng_seedsize(tfm);
3188
3189 seed = kmalloc(seedsize, GFP_KERNEL);
3190 if (!seed) {
3191 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
3192 "for %s\n", algo);
3193 return -ENOMEM;
3194 }
3195
3196 for (i = 0; i < tcount; i++) {
3197 memset(result, 0, 32);
3198
3199 memcpy(seed, template[i].v, template[i].vlen);
3200 memcpy(seed + template[i].vlen, template[i].key,
3201 template[i].klen);
3202 memcpy(seed + template[i].vlen + template[i].klen,
3203 template[i].dt, template[i].dtlen);
3204
3205 err = crypto_rng_reset(tfm, seed, seedsize);
3206 if (err) {
3207 printk(KERN_ERR "alg: cprng: Failed to reset rng "
3208 "for %s\n", algo);
3209 goto out;
3210 }
3211
3212 for (j = 0; j < template[i].loops; j++) {
3213 err = crypto_rng_get_bytes(tfm, result,
3214 template[i].rlen);
Stephan Mueller19e60e12015-03-10 17:00:36 +01003215 if (err < 0) {
Jarod Wilson7647d6c2009-05-04 19:44:50 +08003216 printk(KERN_ERR "alg: cprng: Failed to obtain "
3217 "the correct amount of random data for "
Stephan Mueller19e60e12015-03-10 17:00:36 +01003218 "%s (requested %d)\n", algo,
3219 template[i].rlen);
Jarod Wilson7647d6c2009-05-04 19:44:50 +08003220 goto out;
3221 }
3222 }
3223
3224 err = memcmp(result, template[i].result,
3225 template[i].rlen);
3226 if (err) {
3227 printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
3228 i, algo);
3229 hexdump(result, template[i].rlen);
3230 err = -EINVAL;
3231 goto out;
3232 }
3233 }
3234
3235out:
3236 kfree(seed);
3237 return err;
3238}
3239
Herbert Xuda7f0332008-07-31 17:08:25 +08003240static int alg_test_cipher(const struct alg_test_desc *desc,
3241 const char *driver, u32 type, u32 mask)
3242{
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003243 const struct cipher_test_suite *suite = &desc->suite.cipher;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003244 struct crypto_cipher *tfm;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003245 int err;
Herbert Xuda7f0332008-07-31 17:08:25 +08003246
Herbert Xueed93e02016-11-22 20:08:31 +08003247 tfm = crypto_alloc_cipher(driver, type, mask);
Herbert Xuda7f0332008-07-31 17:08:25 +08003248 if (IS_ERR(tfm)) {
3249 printk(KERN_ERR "alg: cipher: Failed to load transform for "
3250 "%s: %ld\n", driver, PTR_ERR(tfm));
3251 return PTR_ERR(tfm);
3252 }
3253
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003254 err = test_cipher(tfm, ENCRYPT, suite->vecs, suite->count);
3255 if (!err)
3256 err = test_cipher(tfm, DECRYPT, suite->vecs, suite->count);
Herbert Xuda7f0332008-07-31 17:08:25 +08003257
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003258 crypto_free_cipher(tfm);
3259 return err;
3260}
3261
Herbert Xuda7f0332008-07-31 17:08:25 +08003262static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
3263 u32 type, u32 mask)
3264{
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003265 struct crypto_comp *comp;
3266 struct crypto_acomp *acomp;
Herbert Xuda7f0332008-07-31 17:08:25 +08003267 int err;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003268 u32 algo_type = type & CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
Herbert Xuda7f0332008-07-31 17:08:25 +08003269
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003270 if (algo_type == CRYPTO_ALG_TYPE_ACOMPRESS) {
3271 acomp = crypto_alloc_acomp(driver, type, mask);
3272 if (IS_ERR(acomp)) {
3273 pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
3274 driver, PTR_ERR(acomp));
3275 return PTR_ERR(acomp);
3276 }
3277 err = test_acomp(acomp, desc->suite.comp.comp.vecs,
3278 desc->suite.comp.decomp.vecs,
3279 desc->suite.comp.comp.count,
3280 desc->suite.comp.decomp.count);
3281 crypto_free_acomp(acomp);
3282 } else {
3283 comp = crypto_alloc_comp(driver, type, mask);
3284 if (IS_ERR(comp)) {
3285 pr_err("alg: comp: Failed to load transform for %s: %ld\n",
3286 driver, PTR_ERR(comp));
3287 return PTR_ERR(comp);
3288 }
3289
3290 err = test_comp(comp, desc->suite.comp.comp.vecs,
3291 desc->suite.comp.decomp.vecs,
3292 desc->suite.comp.comp.count,
3293 desc->suite.comp.decomp.count);
3294
3295 crypto_free_comp(comp);
Herbert Xuda7f0332008-07-31 17:08:25 +08003296 }
Herbert Xuda7f0332008-07-31 17:08:25 +08003297 return err;
3298}
3299
Herbert Xu8e3ee852008-11-07 14:58:52 +08003300static int alg_test_crc32c(const struct alg_test_desc *desc,
3301 const char *driver, u32 type, u32 mask)
3302{
3303 struct crypto_shash *tfm;
Eric Biggerscb9dde82019-01-10 12:17:55 -08003304 __le32 val;
Herbert Xu8e3ee852008-11-07 14:58:52 +08003305 int err;
3306
3307 err = alg_test_hash(desc, driver, type, mask);
3308 if (err)
Eric Biggerseb5e6732019-01-23 20:57:35 -08003309 return err;
Herbert Xu8e3ee852008-11-07 14:58:52 +08003310
Herbert Xueed93e02016-11-22 20:08:31 +08003311 tfm = crypto_alloc_shash(driver, type, mask);
Herbert Xu8e3ee852008-11-07 14:58:52 +08003312 if (IS_ERR(tfm)) {
Eric Biggerseb5e6732019-01-23 20:57:35 -08003313 if (PTR_ERR(tfm) == -ENOENT) {
3314 /*
3315 * This crc32c implementation is only available through
3316 * ahash API, not the shash API, so the remaining part
3317 * of the test is not applicable to it.
3318 */
3319 return 0;
3320 }
Herbert Xu8e3ee852008-11-07 14:58:52 +08003321 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
3322 "%ld\n", driver, PTR_ERR(tfm));
Eric Biggerseb5e6732019-01-23 20:57:35 -08003323 return PTR_ERR(tfm);
Herbert Xu8e3ee852008-11-07 14:58:52 +08003324 }
3325
3326 do {
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02003327 SHASH_DESC_ON_STACK(shash, tfm);
3328 u32 *ctx = (u32 *)shash_desc_ctx(shash);
Herbert Xu8e3ee852008-11-07 14:58:52 +08003329
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02003330 shash->tfm = tfm;
Herbert Xu8e3ee852008-11-07 14:58:52 +08003331
Eric Biggerscb9dde82019-01-10 12:17:55 -08003332 *ctx = 420553207;
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02003333 err = crypto_shash_final(shash, (u8 *)&val);
Herbert Xu8e3ee852008-11-07 14:58:52 +08003334 if (err) {
3335 printk(KERN_ERR "alg: crc32c: Operation failed for "
3336 "%s: %d\n", driver, err);
3337 break;
3338 }
3339
Eric Biggerscb9dde82019-01-10 12:17:55 -08003340 if (val != cpu_to_le32(~420553207)) {
3341 pr_err("alg: crc32c: Test failed for %s: %u\n",
3342 driver, le32_to_cpu(val));
Herbert Xu8e3ee852008-11-07 14:58:52 +08003343 err = -EINVAL;
3344 }
3345 } while (0);
3346
3347 crypto_free_shash(tfm);
3348
Herbert Xu8e3ee852008-11-07 14:58:52 +08003349 return err;
3350}
3351
Jarod Wilson7647d6c2009-05-04 19:44:50 +08003352static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
3353 u32 type, u32 mask)
3354{
3355 struct crypto_rng *rng;
3356 int err;
3357
Herbert Xueed93e02016-11-22 20:08:31 +08003358 rng = crypto_alloc_rng(driver, type, mask);
Jarod Wilson7647d6c2009-05-04 19:44:50 +08003359 if (IS_ERR(rng)) {
3360 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
3361 "%ld\n", driver, PTR_ERR(rng));
3362 return PTR_ERR(rng);
3363 }
3364
3365 err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
3366
3367 crypto_free_rng(rng);
3368
3369 return err;
3370}
3371
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003372
Eric Biggersb13b1e02017-02-24 15:46:59 -08003373static int drbg_cavs_test(const struct drbg_testvec *test, int pr,
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003374 const char *driver, u32 type, u32 mask)
3375{
3376 int ret = -EAGAIN;
3377 struct crypto_rng *drng;
3378 struct drbg_test_data test_data;
3379 struct drbg_string addtl, pers, testentropy;
3380 unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
3381
3382 if (!buf)
3383 return -ENOMEM;
3384
Herbert Xueed93e02016-11-22 20:08:31 +08003385 drng = crypto_alloc_rng(driver, type, mask);
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003386 if (IS_ERR(drng)) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04003387 printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003388 "%s\n", driver);
3389 kzfree(buf);
3390 return -ENOMEM;
3391 }
3392
3393 test_data.testentropy = &testentropy;
3394 drbg_string_fill(&testentropy, test->entropy, test->entropylen);
3395 drbg_string_fill(&pers, test->pers, test->perslen);
3396 ret = crypto_drbg_reset_test(drng, &pers, &test_data);
3397 if (ret) {
3398 printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
3399 goto outbuf;
3400 }
3401
3402 drbg_string_fill(&addtl, test->addtla, test->addtllen);
3403 if (pr) {
3404 drbg_string_fill(&testentropy, test->entpra, test->entprlen);
3405 ret = crypto_drbg_get_bytes_addtl_test(drng,
3406 buf, test->expectedlen, &addtl, &test_data);
3407 } else {
3408 ret = crypto_drbg_get_bytes_addtl(drng,
3409 buf, test->expectedlen, &addtl);
3410 }
Stephan Mueller19e60e12015-03-10 17:00:36 +01003411 if (ret < 0) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04003412 printk(KERN_ERR "alg: drbg: could not obtain random data for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003413 "driver %s\n", driver);
3414 goto outbuf;
3415 }
3416
3417 drbg_string_fill(&addtl, test->addtlb, test->addtllen);
3418 if (pr) {
3419 drbg_string_fill(&testentropy, test->entprb, test->entprlen);
3420 ret = crypto_drbg_get_bytes_addtl_test(drng,
3421 buf, test->expectedlen, &addtl, &test_data);
3422 } else {
3423 ret = crypto_drbg_get_bytes_addtl(drng,
3424 buf, test->expectedlen, &addtl);
3425 }
Stephan Mueller19e60e12015-03-10 17:00:36 +01003426 if (ret < 0) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04003427 printk(KERN_ERR "alg: drbg: could not obtain random data for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003428 "driver %s\n", driver);
3429 goto outbuf;
3430 }
3431
3432 ret = memcmp(test->expected, buf, test->expectedlen);
3433
3434outbuf:
3435 crypto_free_rng(drng);
3436 kzfree(buf);
3437 return ret;
3438}
3439
3440
3441static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
3442 u32 type, u32 mask)
3443{
3444 int err = 0;
3445 int pr = 0;
3446 int i = 0;
Eric Biggersb13b1e02017-02-24 15:46:59 -08003447 const struct drbg_testvec *template = desc->suite.drbg.vecs;
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003448 unsigned int tcount = desc->suite.drbg.count;
3449
3450 if (0 == memcmp(driver, "drbg_pr_", 8))
3451 pr = 1;
3452
3453 for (i = 0; i < tcount; i++) {
3454 err = drbg_cavs_test(&template[i], pr, driver, type, mask);
3455 if (err) {
3456 printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
3457 i, driver);
3458 err = -EINVAL;
3459 break;
3460 }
3461 }
3462 return err;
3463
3464}
3465
Eric Biggersb13b1e02017-02-24 15:46:59 -08003466static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003467 const char *alg)
3468{
3469 struct kpp_request *req;
3470 void *input_buf = NULL;
3471 void *output_buf = NULL;
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003472 void *a_public = NULL;
3473 void *a_ss = NULL;
3474 void *shared_secret = NULL;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003475 struct crypto_wait wait;
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003476 unsigned int out_len_max;
3477 int err = -ENOMEM;
3478 struct scatterlist src, dst;
3479
3480 req = kpp_request_alloc(tfm, GFP_KERNEL);
3481 if (!req)
3482 return err;
3483
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003484 crypto_init_wait(&wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003485
3486 err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
3487 if (err < 0)
3488 goto free_req;
3489
3490 out_len_max = crypto_kpp_maxsize(tfm);
3491 output_buf = kzalloc(out_len_max, GFP_KERNEL);
3492 if (!output_buf) {
3493 err = -ENOMEM;
3494 goto free_req;
3495 }
3496
3497 /* Use appropriate parameter as base */
3498 kpp_request_set_input(req, NULL, 0);
3499 sg_init_one(&dst, output_buf, out_len_max);
3500 kpp_request_set_output(req, &dst, out_len_max);
3501 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003502 crypto_req_done, &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003503
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003504 /* Compute party A's public key */
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003505 err = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003506 if (err) {
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003507 pr_err("alg: %s: Party A: generate public key test failed. err %d\n",
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003508 alg, err);
3509 goto free_output;
3510 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003511
3512 if (vec->genkey) {
3513 /* Save party A's public key */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05003514 a_public = kmemdup(sg_virt(req->dst), out_len_max, GFP_KERNEL);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003515 if (!a_public) {
3516 err = -ENOMEM;
3517 goto free_output;
3518 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003519 } else {
3520 /* Verify calculated public key */
3521 if (memcmp(vec->expected_a_public, sg_virt(req->dst),
3522 vec->expected_a_public_size)) {
3523 pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n",
3524 alg);
3525 err = -EINVAL;
3526 goto free_output;
3527 }
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003528 }
3529
3530 /* Calculate shared secret key by using counter part (b) public key. */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05003531 input_buf = kmemdup(vec->b_public, vec->b_public_size, GFP_KERNEL);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003532 if (!input_buf) {
3533 err = -ENOMEM;
3534 goto free_output;
3535 }
3536
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003537 sg_init_one(&src, input_buf, vec->b_public_size);
3538 sg_init_one(&dst, output_buf, out_len_max);
3539 kpp_request_set_input(req, &src, vec->b_public_size);
3540 kpp_request_set_output(req, &dst, out_len_max);
3541 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003542 crypto_req_done, &wait);
3543 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003544 if (err) {
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003545 pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n",
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003546 alg, err);
3547 goto free_all;
3548 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003549
3550 if (vec->genkey) {
3551 /* Save the shared secret obtained by party A */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05003552 a_ss = kmemdup(sg_virt(req->dst), vec->expected_ss_size, GFP_KERNEL);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003553 if (!a_ss) {
3554 err = -ENOMEM;
3555 goto free_all;
3556 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003557
3558 /*
3559 * Calculate party B's shared secret by using party A's
3560 * public key.
3561 */
3562 err = crypto_kpp_set_secret(tfm, vec->b_secret,
3563 vec->b_secret_size);
3564 if (err < 0)
3565 goto free_all;
3566
3567 sg_init_one(&src, a_public, vec->expected_a_public_size);
3568 sg_init_one(&dst, output_buf, out_len_max);
3569 kpp_request_set_input(req, &src, vec->expected_a_public_size);
3570 kpp_request_set_output(req, &dst, out_len_max);
3571 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003572 crypto_req_done, &wait);
3573 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req),
3574 &wait);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003575 if (err) {
3576 pr_err("alg: %s: Party B: compute shared secret failed. err %d\n",
3577 alg, err);
3578 goto free_all;
3579 }
3580
3581 shared_secret = a_ss;
3582 } else {
3583 shared_secret = (void *)vec->expected_ss;
3584 }
3585
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003586 /*
3587 * verify shared secret from which the user will derive
3588 * secret key by executing whatever hash it has chosen
3589 */
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003590 if (memcmp(shared_secret, sg_virt(req->dst),
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003591 vec->expected_ss_size)) {
3592 pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
3593 alg);
3594 err = -EINVAL;
3595 }
3596
3597free_all:
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003598 kfree(a_ss);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003599 kfree(input_buf);
3600free_output:
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003601 kfree(a_public);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003602 kfree(output_buf);
3603free_req:
3604 kpp_request_free(req);
3605 return err;
3606}
3607
3608static int test_kpp(struct crypto_kpp *tfm, const char *alg,
Eric Biggersb13b1e02017-02-24 15:46:59 -08003609 const struct kpp_testvec *vecs, unsigned int tcount)
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003610{
3611 int ret, i;
3612
3613 for (i = 0; i < tcount; i++) {
3614 ret = do_test_kpp(tfm, vecs++, alg);
3615 if (ret) {
3616 pr_err("alg: %s: test failed on vector %d, err=%d\n",
3617 alg, i + 1, ret);
3618 return ret;
3619 }
3620 }
3621 return 0;
3622}
3623
3624static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
3625 u32 type, u32 mask)
3626{
3627 struct crypto_kpp *tfm;
3628 int err = 0;
3629
Herbert Xueed93e02016-11-22 20:08:31 +08003630 tfm = crypto_alloc_kpp(driver, type, mask);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003631 if (IS_ERR(tfm)) {
3632 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
3633 driver, PTR_ERR(tfm));
3634 return PTR_ERR(tfm);
3635 }
3636 if (desc->suite.kpp.vecs)
3637 err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
3638 desc->suite.kpp.count);
3639
3640 crypto_free_kpp(tfm);
3641 return err;
3642}
3643
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003644static u8 *test_pack_u32(u8 *dst, u32 val)
3645{
3646 memcpy(dst, &val, sizeof(val));
3647 return dst + sizeof(val);
3648}
3649
Herbert Xu50d2b6432016-06-29 19:32:20 +08003650static int test_akcipher_one(struct crypto_akcipher *tfm,
Eric Biggersb13b1e02017-02-24 15:46:59 -08003651 const struct akcipher_testvec *vecs)
Tadeusz Struk946cc462015-06-16 10:31:06 -07003652{
Herbert Xudf27b262016-05-05 16:42:49 +08003653 char *xbuf[XBUFSIZE];
Tadeusz Struk946cc462015-06-16 10:31:06 -07003654 struct akcipher_request *req;
3655 void *outbuf_enc = NULL;
3656 void *outbuf_dec = NULL;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003657 struct crypto_wait wait;
Tadeusz Struk946cc462015-06-16 10:31:06 -07003658 unsigned int out_len_max, out_len = 0;
3659 int err = -ENOMEM;
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03003660 struct scatterlist src, dst, src_tab[3];
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003661 const char *m, *c;
3662 unsigned int m_size, c_size;
3663 const char *op;
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003664 u8 *key, *ptr;
Tadeusz Struk946cc462015-06-16 10:31:06 -07003665
Herbert Xudf27b262016-05-05 16:42:49 +08003666 if (testmgr_alloc_buf(xbuf))
3667 return err;
3668
Tadeusz Struk946cc462015-06-16 10:31:06 -07003669 req = akcipher_request_alloc(tfm, GFP_KERNEL);
3670 if (!req)
Herbert Xudf27b262016-05-05 16:42:49 +08003671 goto free_xbuf;
Tadeusz Struk946cc462015-06-16 10:31:06 -07003672
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003673 crypto_init_wait(&wait);
Tadeusz Struk22287b02015-10-08 09:26:55 -07003674
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003675 key = kmalloc(vecs->key_len + sizeof(u32) * 2 + vecs->param_len,
3676 GFP_KERNEL);
3677 if (!key)
3678 goto free_xbuf;
3679 memcpy(key, vecs->key, vecs->key_len);
3680 ptr = key + vecs->key_len;
3681 ptr = test_pack_u32(ptr, vecs->algo);
3682 ptr = test_pack_u32(ptr, vecs->param_len);
3683 memcpy(ptr, vecs->params, vecs->param_len);
3684
Tadeusz Struk22287b02015-10-08 09:26:55 -07003685 if (vecs->public_key_vec)
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003686 err = crypto_akcipher_set_pub_key(tfm, key, vecs->key_len);
Tadeusz Struk22287b02015-10-08 09:26:55 -07003687 else
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003688 err = crypto_akcipher_set_priv_key(tfm, key, vecs->key_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003689 if (err)
3690 goto free_req;
3691
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003692 /*
3693 * First run test which do not require a private key, such as
3694 * encrypt or verify.
3695 */
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03003696 err = -ENOMEM;
3697 out_len_max = crypto_akcipher_maxsize(tfm);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003698 outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
3699 if (!outbuf_enc)
3700 goto free_req;
3701
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003702 if (!vecs->siggen_sigver_test) {
3703 m = vecs->m;
3704 m_size = vecs->m_size;
3705 c = vecs->c;
3706 c_size = vecs->c_size;
3707 op = "encrypt";
3708 } else {
3709 /* Swap args so we could keep plaintext (digest)
3710 * in vecs->m, and cooked signature in vecs->c.
3711 */
3712 m = vecs->c; /* signature */
3713 m_size = vecs->c_size;
3714 c = vecs->m; /* digest */
3715 c_size = vecs->m_size;
3716 op = "verify";
3717 }
Herbert Xudf27b262016-05-05 16:42:49 +08003718
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003719 if (WARN_ON(m_size > PAGE_SIZE))
3720 goto free_all;
3721 memcpy(xbuf[0], m, m_size);
Herbert Xudf27b262016-05-05 16:42:49 +08003722
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03003723 sg_init_table(src_tab, 3);
Herbert Xudf27b262016-05-05 16:42:49 +08003724 sg_set_buf(&src_tab[0], xbuf[0], 8);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003725 sg_set_buf(&src_tab[1], xbuf[0] + 8, m_size - 8);
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03003726 if (vecs->siggen_sigver_test) {
3727 if (WARN_ON(c_size > PAGE_SIZE))
3728 goto free_all;
3729 memcpy(xbuf[1], c, c_size);
3730 sg_set_buf(&src_tab[2], xbuf[1], c_size);
3731 akcipher_request_set_crypt(req, src_tab, NULL, m_size, c_size);
3732 } else {
3733 sg_init_one(&dst, outbuf_enc, out_len_max);
3734 akcipher_request_set_crypt(req, src_tab, &dst, m_size,
3735 out_len_max);
3736 }
Tadeusz Struk946cc462015-06-16 10:31:06 -07003737 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003738 crypto_req_done, &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003739
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003740 err = crypto_wait_req(vecs->siggen_sigver_test ?
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003741 /* Run asymmetric signature verification */
3742 crypto_akcipher_verify(req) :
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003743 /* Run asymmetric encrypt */
3744 crypto_akcipher_encrypt(req), &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003745 if (err) {
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003746 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003747 goto free_all;
3748 }
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03003749 if (!vecs->siggen_sigver_test) {
3750 if (req->dst_len != c_size) {
3751 pr_err("alg: akcipher: %s test failed. Invalid output len\n",
3752 op);
3753 err = -EINVAL;
3754 goto free_all;
3755 }
3756 /* verify that encrypted message is equal to expected */
3757 if (memcmp(c, outbuf_enc, c_size) != 0) {
3758 pr_err("alg: akcipher: %s test failed. Invalid output\n",
3759 op);
3760 hexdump(outbuf_enc, c_size);
3761 err = -EINVAL;
3762 goto free_all;
3763 }
Tadeusz Struk946cc462015-06-16 10:31:06 -07003764 }
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003765
3766 /*
3767 * Don't invoke (decrypt or sign) test which require a private key
3768 * for vectors with only a public key.
3769 */
Tadeusz Struk946cc462015-06-16 10:31:06 -07003770 if (vecs->public_key_vec) {
3771 err = 0;
3772 goto free_all;
3773 }
3774 outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
3775 if (!outbuf_dec) {
3776 err = -ENOMEM;
3777 goto free_all;
3778 }
Herbert Xudf27b262016-05-05 16:42:49 +08003779
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003780 op = vecs->siggen_sigver_test ? "sign" : "decrypt";
3781 if (WARN_ON(c_size > PAGE_SIZE))
Herbert Xudf27b262016-05-05 16:42:49 +08003782 goto free_all;
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003783 memcpy(xbuf[0], c, c_size);
Herbert Xudf27b262016-05-05 16:42:49 +08003784
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003785 sg_init_one(&src, xbuf[0], c_size);
Tadeusz Struk22287b02015-10-08 09:26:55 -07003786 sg_init_one(&dst, outbuf_dec, out_len_max);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003787 crypto_init_wait(&wait);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003788 akcipher_request_set_crypt(req, &src, &dst, c_size, out_len_max);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003789
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003790 err = crypto_wait_req(vecs->siggen_sigver_test ?
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003791 /* Run asymmetric signature generation */
3792 crypto_akcipher_sign(req) :
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003793 /* Run asymmetric decrypt */
3794 crypto_akcipher_decrypt(req), &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003795 if (err) {
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003796 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003797 goto free_all;
3798 }
3799 out_len = req->dst_len;
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003800 if (out_len < m_size) {
3801 pr_err("alg: akcipher: %s test failed. Invalid output len %u\n",
3802 op, out_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003803 err = -EINVAL;
3804 goto free_all;
3805 }
3806 /* verify that decrypted message is equal to the original msg */
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003807 if (memchr_inv(outbuf_dec, 0, out_len - m_size) ||
3808 memcmp(m, outbuf_dec + out_len - m_size, m_size)) {
3809 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
Herbert Xu50d2b6432016-06-29 19:32:20 +08003810 hexdump(outbuf_dec, out_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003811 err = -EINVAL;
3812 }
3813free_all:
3814 kfree(outbuf_dec);
3815 kfree(outbuf_enc);
3816free_req:
3817 akcipher_request_free(req);
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003818 kfree(key);
Herbert Xudf27b262016-05-05 16:42:49 +08003819free_xbuf:
3820 testmgr_free_buf(xbuf);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003821 return err;
3822}
3823
Herbert Xu50d2b6432016-06-29 19:32:20 +08003824static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
Eric Biggersb13b1e02017-02-24 15:46:59 -08003825 const struct akcipher_testvec *vecs,
3826 unsigned int tcount)
Tadeusz Struk946cc462015-06-16 10:31:06 -07003827{
Herbert Xu15226e42016-07-18 18:20:10 +08003828 const char *algo =
3829 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
Tadeusz Struk946cc462015-06-16 10:31:06 -07003830 int ret, i;
3831
3832 for (i = 0; i < tcount; i++) {
Herbert Xu50d2b6432016-06-29 19:32:20 +08003833 ret = test_akcipher_one(tfm, vecs++);
3834 if (!ret)
3835 continue;
3836
Herbert Xu15226e42016-07-18 18:20:10 +08003837 pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
3838 i + 1, algo, ret);
Herbert Xu50d2b6432016-06-29 19:32:20 +08003839 return ret;
Tadeusz Struk946cc462015-06-16 10:31:06 -07003840 }
3841 return 0;
3842}
3843
Tadeusz Struk946cc462015-06-16 10:31:06 -07003844static int alg_test_akcipher(const struct alg_test_desc *desc,
3845 const char *driver, u32 type, u32 mask)
3846{
3847 struct crypto_akcipher *tfm;
3848 int err = 0;
3849
Herbert Xueed93e02016-11-22 20:08:31 +08003850 tfm = crypto_alloc_akcipher(driver, type, mask);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003851 if (IS_ERR(tfm)) {
3852 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
3853 driver, PTR_ERR(tfm));
3854 return PTR_ERR(tfm);
3855 }
3856 if (desc->suite.akcipher.vecs)
3857 err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
3858 desc->suite.akcipher.count);
3859
3860 crypto_free_akcipher(tfm);
3861 return err;
3862}
3863
Youquan, Song863b5572009-12-23 19:45:20 +08003864static int alg_test_null(const struct alg_test_desc *desc,
3865 const char *driver, u32 type, u32 mask)
3866{
3867 return 0;
3868}
3869
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003870#define __VECS(tv) { .vecs = tv, .count = ARRAY_SIZE(tv) }
3871
Herbert Xuda7f0332008-07-31 17:08:25 +08003872/* Please keep this list sorted by algorithm name. */
3873static const struct alg_test_desc alg_test_descs[] = {
3874 {
Eric Biggers059c2a42018-11-16 17:26:31 -08003875 .alg = "adiantum(xchacha12,aes)",
Eric Biggersd435e102019-04-11 21:57:40 -07003876 .generic_driver = "adiantum(xchacha12-generic,aes-generic,nhpoly1305-generic)",
Eric Biggers059c2a42018-11-16 17:26:31 -08003877 .test = alg_test_skcipher,
3878 .suite = {
3879 .cipher = __VECS(adiantum_xchacha12_aes_tv_template)
3880 },
3881 }, {
3882 .alg = "adiantum(xchacha20,aes)",
Eric Biggersd435e102019-04-11 21:57:40 -07003883 .generic_driver = "adiantum(xchacha20-generic,aes-generic,nhpoly1305-generic)",
Eric Biggers059c2a42018-11-16 17:26:31 -08003884 .test = alg_test_skcipher,
3885 .suite = {
3886 .cipher = __VECS(adiantum_xchacha20_aes_tv_template)
3887 },
3888 }, {
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02003889 .alg = "aegis128",
3890 .test = alg_test_aead,
3891 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003892 .aead = __VECS(aegis128_tv_template)
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02003893 }
3894 }, {
3895 .alg = "aegis128l",
3896 .test = alg_test_aead,
3897 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003898 .aead = __VECS(aegis128l_tv_template)
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02003899 }
3900 }, {
3901 .alg = "aegis256",
3902 .test = alg_test_aead,
3903 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003904 .aead = __VECS(aegis256_tv_template)
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02003905 }
3906 }, {
Jarod Wilsone08ca2d2009-05-04 19:46:29 +08003907 .alg = "ansi_cprng",
3908 .test = alg_test_cprng,
3909 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003910 .cprng = __VECS(ansi_cprng_aes_tv_template)
Jarod Wilsone08ca2d2009-05-04 19:46:29 +08003911 }
3912 }, {
Horia Geantabca4feb2014-03-14 17:46:51 +02003913 .alg = "authenc(hmac(md5),ecb(cipher_null))",
3914 .test = alg_test_aead,
Horia Geantabca4feb2014-03-14 17:46:51 +02003915 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003916 .aead = __VECS(hmac_md5_ecb_cipher_null_tv_template)
Horia Geantabca4feb2014-03-14 17:46:51 +02003917 }
3918 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003919 .alg = "authenc(hmac(sha1),cbc(aes))",
Horia Geantae46e9a42012-07-03 19:16:54 +03003920 .test = alg_test_aead,
Herbert Xubcf741c2017-06-28 19:09:07 +08003921 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03003922 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003923 .aead = __VECS(hmac_sha1_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303924 }
3925 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003926 .alg = "authenc(hmac(sha1),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303927 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303928 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003929 .aead = __VECS(hmac_sha1_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303930 }
3931 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003932 .alg = "authenc(hmac(sha1),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303933 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01003934 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303935 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003936 .aead = __VECS(hmac_sha1_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03003937 }
3938 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01003939 .alg = "authenc(hmac(sha1),ctr(aes))",
3940 .test = alg_test_null,
3941 .fips_allowed = 1,
3942 }, {
Horia Geantabca4feb2014-03-14 17:46:51 +02003943 .alg = "authenc(hmac(sha1),ecb(cipher_null))",
3944 .test = alg_test_aead,
Horia Geantabca4feb2014-03-14 17:46:51 +02003945 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003946 .aead = __VECS(hmac_sha1_ecb_cipher_null_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303947 }
3948 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01003949 .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
3950 .test = alg_test_null,
3951 .fips_allowed = 1,
3952 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003953 .alg = "authenc(hmac(sha224),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303954 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303955 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003956 .aead = __VECS(hmac_sha224_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303957 }
3958 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003959 .alg = "authenc(hmac(sha224),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303960 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01003961 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303962 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003963 .aead = __VECS(hmac_sha224_des3_ede_cbc_tv_temp)
Horia Geantabca4feb2014-03-14 17:46:51 +02003964 }
3965 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003966 .alg = "authenc(hmac(sha256),cbc(aes))",
Horia Geantae46e9a42012-07-03 19:16:54 +03003967 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01003968 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03003969 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003970 .aead = __VECS(hmac_sha256_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303971 }
3972 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003973 .alg = "authenc(hmac(sha256),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303974 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303975 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003976 .aead = __VECS(hmac_sha256_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303977 }
3978 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003979 .alg = "authenc(hmac(sha256),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303980 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01003981 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303982 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003983 .aead = __VECS(hmac_sha256_des3_ede_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303984 }
3985 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01003986 .alg = "authenc(hmac(sha256),ctr(aes))",
3987 .test = alg_test_null,
3988 .fips_allowed = 1,
3989 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01003990 .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
3991 .test = alg_test_null,
3992 .fips_allowed = 1,
3993 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003994 .alg = "authenc(hmac(sha384),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303995 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303996 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003997 .aead = __VECS(hmac_sha384_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303998 }
3999 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004000 .alg = "authenc(hmac(sha384),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304001 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01004002 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304003 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004004 .aead = __VECS(hmac_sha384_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03004005 }
4006 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01004007 .alg = "authenc(hmac(sha384),ctr(aes))",
4008 .test = alg_test_null,
4009 .fips_allowed = 1,
4010 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01004011 .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
4012 .test = alg_test_null,
4013 .fips_allowed = 1,
4014 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004015 .alg = "authenc(hmac(sha512),cbc(aes))",
Marcus Meissnered1afac2016-02-05 14:23:33 +01004016 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03004017 .test = alg_test_aead,
Horia Geantae46e9a42012-07-03 19:16:54 +03004018 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004019 .aead = __VECS(hmac_sha512_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304020 }
4021 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004022 .alg = "authenc(hmac(sha512),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304023 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304024 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004025 .aead = __VECS(hmac_sha512_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304026 }
4027 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004028 .alg = "authenc(hmac(sha512),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304029 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01004030 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304031 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004032 .aead = __VECS(hmac_sha512_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03004033 }
4034 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01004035 .alg = "authenc(hmac(sha512),ctr(aes))",
4036 .test = alg_test_null,
4037 .fips_allowed = 1,
4038 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01004039 .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
4040 .test = alg_test_null,
4041 .fips_allowed = 1,
4042 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004043 .alg = "cbc(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004044 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004045 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004046 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004047 .cipher = __VECS(aes_cbc_tv_template)
4048 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004049 }, {
4050 .alg = "cbc(anubis)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004051 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004052 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004053 .cipher = __VECS(anubis_cbc_tv_template)
4054 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004055 }, {
4056 .alg = "cbc(blowfish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004057 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004058 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004059 .cipher = __VECS(bf_cbc_tv_template)
4060 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004061 }, {
4062 .alg = "cbc(camellia)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004063 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004064 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004065 .cipher = __VECS(camellia_cbc_tv_template)
4066 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004067 }, {
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02004068 .alg = "cbc(cast5)",
4069 .test = alg_test_skcipher,
4070 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004071 .cipher = __VECS(cast5_cbc_tv_template)
4072 },
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02004073 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004074 .alg = "cbc(cast6)",
4075 .test = alg_test_skcipher,
4076 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004077 .cipher = __VECS(cast6_cbc_tv_template)
4078 },
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004079 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004080 .alg = "cbc(des)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004081 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004082 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004083 .cipher = __VECS(des_cbc_tv_template)
4084 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004085 }, {
4086 .alg = "cbc(des3_ede)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004087 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004088 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004089 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004090 .cipher = __VECS(des3_ede_cbc_tv_template)
4091 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004092 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01004093 /* Same as cbc(aes) except the key is stored in
4094 * hardware secure memory which we reference by index
4095 */
4096 .alg = "cbc(paes)",
4097 .test = alg_test_null,
4098 .fips_allowed = 1,
4099 }, {
Gilad Ben-Yosseff0372c02019-04-18 16:38:36 +03004100 /* Same as cbc(sm4) except the key is stored in
4101 * hardware secure memory which we reference by index
4102 */
4103 .alg = "cbc(psm4)",
4104 .test = alg_test_null,
4105 }, {
Jussi Kivilinna9d259172011-10-18 00:02:53 +03004106 .alg = "cbc(serpent)",
4107 .test = alg_test_skcipher,
4108 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004109 .cipher = __VECS(serpent_cbc_tv_template)
4110 },
Jussi Kivilinna9d259172011-10-18 00:02:53 +03004111 }, {
Gilad Ben-Yossef95ba5972018-09-20 14:18:38 +01004112 .alg = "cbc(sm4)",
4113 .test = alg_test_skcipher,
4114 .suite = {
4115 .cipher = __VECS(sm4_cbc_tv_template)
4116 }
4117 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004118 .alg = "cbc(twofish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004119 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004120 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004121 .cipher = __VECS(tf_cbc_tv_template)
4122 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004123 }, {
Ard Biesheuvel092acf02017-02-03 14:49:35 +00004124 .alg = "cbcmac(aes)",
4125 .fips_allowed = 1,
4126 .test = alg_test_hash,
4127 .suite = {
4128 .hash = __VECS(aes_cbcmac_tv_template)
4129 }
4130 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004131 .alg = "ccm(aes)",
Eric Biggers40153b12019-04-11 21:57:41 -07004132 .generic_driver = "ccm_base(ctr(aes-generic),cbcmac(aes-generic))",
Herbert Xuda7f0332008-07-31 17:08:25 +08004133 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004134 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004135 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004136 .aead = __VECS(aes_ccm_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004137 }
4138 }, {
Dmitry Eremin-Solenikov7da66672018-10-20 02:01:53 +03004139 .alg = "cfb(aes)",
4140 .test = alg_test_skcipher,
4141 .fips_allowed = 1,
4142 .suite = {
4143 .cipher = __VECS(aes_cfb_tv_template)
4144 },
4145 }, {
Martin Willi3590ebf2015-06-01 13:43:57 +02004146 .alg = "chacha20",
4147 .test = alg_test_skcipher,
4148 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004149 .cipher = __VECS(chacha20_tv_template)
4150 },
Martin Willi3590ebf2015-06-01 13:43:57 +02004151 }, {
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03004152 .alg = "cmac(aes)",
Stephan Mueller8f183752015-08-19 08:42:07 +02004153 .fips_allowed = 1,
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03004154 .test = alg_test_hash,
4155 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004156 .hash = __VECS(aes_cmac128_tv_template)
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03004157 }
4158 }, {
4159 .alg = "cmac(des3_ede)",
Stephan Mueller8f183752015-08-19 08:42:07 +02004160 .fips_allowed = 1,
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03004161 .test = alg_test_hash,
4162 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004163 .hash = __VECS(des3_ede_cmac64_tv_template)
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03004164 }
4165 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03004166 .alg = "compress_null",
4167 .test = alg_test_null,
4168 }, {
Ard Biesheuvelebb34722015-05-04 11:00:17 +02004169 .alg = "crc32",
4170 .test = alg_test_hash,
Milan Broza8a34412019-01-25 10:31:47 +01004171 .fips_allowed = 1,
Ard Biesheuvelebb34722015-05-04 11:00:17 +02004172 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004173 .hash = __VECS(crc32_tv_template)
Ard Biesheuvelebb34722015-05-04 11:00:17 +02004174 }
4175 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004176 .alg = "crc32c",
Herbert Xu8e3ee852008-11-07 14:58:52 +08004177 .test = alg_test_crc32c,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004178 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004179 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004180 .hash = __VECS(crc32c_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004181 }
4182 }, {
Herbert Xu684115212013-09-07 12:56:26 +10004183 .alg = "crct10dif",
4184 .test = alg_test_hash,
4185 .fips_allowed = 1,
4186 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004187 .hash = __VECS(crct10dif_tv_template)
Herbert Xu684115212013-09-07 12:56:26 +10004188 }
4189 }, {
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08004190 .alg = "ctr(aes)",
4191 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004192 .fips_allowed = 1,
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08004193 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004194 .cipher = __VECS(aes_ctr_tv_template)
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08004195 }
4196 }, {
Jussi Kivilinna85b63e32011-10-10 23:03:03 +03004197 .alg = "ctr(blowfish)",
4198 .test = alg_test_skcipher,
4199 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004200 .cipher = __VECS(bf_ctr_tv_template)
Jussi Kivilinna85b63e32011-10-10 23:03:03 +03004201 }
4202 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02004203 .alg = "ctr(camellia)",
4204 .test = alg_test_skcipher,
4205 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004206 .cipher = __VECS(camellia_ctr_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02004207 }
4208 }, {
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02004209 .alg = "ctr(cast5)",
4210 .test = alg_test_skcipher,
4211 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004212 .cipher = __VECS(cast5_ctr_tv_template)
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02004213 }
4214 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004215 .alg = "ctr(cast6)",
4216 .test = alg_test_skcipher,
4217 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004218 .cipher = __VECS(cast6_ctr_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004219 }
4220 }, {
Jussi Kivilinna8163fc32012-10-20 14:53:07 +03004221 .alg = "ctr(des)",
4222 .test = alg_test_skcipher,
4223 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004224 .cipher = __VECS(des_ctr_tv_template)
Jussi Kivilinna8163fc32012-10-20 14:53:07 +03004225 }
4226 }, {
Jussi Kivilinnae080b172012-10-20 14:53:12 +03004227 .alg = "ctr(des3_ede)",
4228 .test = alg_test_skcipher,
Marcelo Cerri0d8da102017-03-20 17:28:05 -03004229 .fips_allowed = 1,
Jussi Kivilinnae080b172012-10-20 14:53:12 +03004230 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004231 .cipher = __VECS(des3_ede_ctr_tv_template)
Jussi Kivilinnae080b172012-10-20 14:53:12 +03004232 }
4233 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01004234 /* Same as ctr(aes) except the key is stored in
4235 * hardware secure memory which we reference by index
4236 */
4237 .alg = "ctr(paes)",
4238 .test = alg_test_null,
4239 .fips_allowed = 1,
4240 }, {
Gilad Ben-Yosseff0372c02019-04-18 16:38:36 +03004241
4242 /* Same as ctr(sm4) except the key is stored in
4243 * hardware secure memory which we reference by index
4244 */
4245 .alg = "ctr(psm4)",
4246 .test = alg_test_null,
4247 }, {
Jussi Kivilinna9d259172011-10-18 00:02:53 +03004248 .alg = "ctr(serpent)",
4249 .test = alg_test_skcipher,
4250 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004251 .cipher = __VECS(serpent_ctr_tv_template)
Jussi Kivilinna9d259172011-10-18 00:02:53 +03004252 }
4253 }, {
Gilad Ben-Yossef95ba5972018-09-20 14:18:38 +01004254 .alg = "ctr(sm4)",
4255 .test = alg_test_skcipher,
4256 .suite = {
4257 .cipher = __VECS(sm4_ctr_tv_template)
4258 }
4259 }, {
Jussi Kivilinna573da622011-10-10 23:03:12 +03004260 .alg = "ctr(twofish)",
4261 .test = alg_test_skcipher,
4262 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004263 .cipher = __VECS(tf_ctr_tv_template)
Jussi Kivilinna573da622011-10-10 23:03:12 +03004264 }
4265 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004266 .alg = "cts(cbc(aes))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004267 .test = alg_test_skcipher,
Gilad Ben-Yossef196ad602018-11-04 10:05:24 +00004268 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004269 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004270 .cipher = __VECS(cts_mode_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004271 }
4272 }, {
Gilad Ben-Yosseff0372c02019-04-18 16:38:36 +03004273 /* Same as cts(cbc((aes)) except the key is stored in
4274 * hardware secure memory which we reference by index
4275 */
4276 .alg = "cts(cbc(paes))",
4277 .test = alg_test_null,
4278 .fips_allowed = 1,
4279 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004280 .alg = "deflate",
4281 .test = alg_test_comp,
Milan Broz08189042012-12-06 17:16:28 +08004282 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004283 .suite = {
4284 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004285 .comp = __VECS(deflate_comp_tv_template),
4286 .decomp = __VECS(deflate_decomp_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004287 }
4288 }
4289 }, {
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01004290 .alg = "dh",
4291 .test = alg_test_kpp,
4292 .fips_allowed = 1,
4293 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004294 .kpp = __VECS(dh_tv_template)
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01004295 }
4296 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03004297 .alg = "digest_null",
4298 .test = alg_test_null,
4299 }, {
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004300 .alg = "drbg_nopr_ctr_aes128",
4301 .test = alg_test_drbg,
4302 .fips_allowed = 1,
4303 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004304 .drbg = __VECS(drbg_nopr_ctr_aes128_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004305 }
4306 }, {
4307 .alg = "drbg_nopr_ctr_aes192",
4308 .test = alg_test_drbg,
4309 .fips_allowed = 1,
4310 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004311 .drbg = __VECS(drbg_nopr_ctr_aes192_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004312 }
4313 }, {
4314 .alg = "drbg_nopr_ctr_aes256",
4315 .test = alg_test_drbg,
4316 .fips_allowed = 1,
4317 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004318 .drbg = __VECS(drbg_nopr_ctr_aes256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004319 }
4320 }, {
4321 /*
4322 * There is no need to specifically test the DRBG with every
4323 * backend cipher -- covered by drbg_nopr_hmac_sha256 test
4324 */
4325 .alg = "drbg_nopr_hmac_sha1",
4326 .fips_allowed = 1,
4327 .test = alg_test_null,
4328 }, {
4329 .alg = "drbg_nopr_hmac_sha256",
4330 .test = alg_test_drbg,
4331 .fips_allowed = 1,
4332 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004333 .drbg = __VECS(drbg_nopr_hmac_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004334 }
4335 }, {
4336 /* covered by drbg_nopr_hmac_sha256 test */
4337 .alg = "drbg_nopr_hmac_sha384",
4338 .fips_allowed = 1,
4339 .test = alg_test_null,
4340 }, {
4341 .alg = "drbg_nopr_hmac_sha512",
4342 .test = alg_test_null,
4343 .fips_allowed = 1,
4344 }, {
4345 .alg = "drbg_nopr_sha1",
4346 .fips_allowed = 1,
4347 .test = alg_test_null,
4348 }, {
4349 .alg = "drbg_nopr_sha256",
4350 .test = alg_test_drbg,
4351 .fips_allowed = 1,
4352 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004353 .drbg = __VECS(drbg_nopr_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004354 }
4355 }, {
4356 /* covered by drbg_nopr_sha256 test */
4357 .alg = "drbg_nopr_sha384",
4358 .fips_allowed = 1,
4359 .test = alg_test_null,
4360 }, {
4361 .alg = "drbg_nopr_sha512",
4362 .fips_allowed = 1,
4363 .test = alg_test_null,
4364 }, {
4365 .alg = "drbg_pr_ctr_aes128",
4366 .test = alg_test_drbg,
4367 .fips_allowed = 1,
4368 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004369 .drbg = __VECS(drbg_pr_ctr_aes128_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004370 }
4371 }, {
4372 /* covered by drbg_pr_ctr_aes128 test */
4373 .alg = "drbg_pr_ctr_aes192",
4374 .fips_allowed = 1,
4375 .test = alg_test_null,
4376 }, {
4377 .alg = "drbg_pr_ctr_aes256",
4378 .fips_allowed = 1,
4379 .test = alg_test_null,
4380 }, {
4381 .alg = "drbg_pr_hmac_sha1",
4382 .fips_allowed = 1,
4383 .test = alg_test_null,
4384 }, {
4385 .alg = "drbg_pr_hmac_sha256",
4386 .test = alg_test_drbg,
4387 .fips_allowed = 1,
4388 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004389 .drbg = __VECS(drbg_pr_hmac_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004390 }
4391 }, {
4392 /* covered by drbg_pr_hmac_sha256 test */
4393 .alg = "drbg_pr_hmac_sha384",
4394 .fips_allowed = 1,
4395 .test = alg_test_null,
4396 }, {
4397 .alg = "drbg_pr_hmac_sha512",
4398 .test = alg_test_null,
4399 .fips_allowed = 1,
4400 }, {
4401 .alg = "drbg_pr_sha1",
4402 .fips_allowed = 1,
4403 .test = alg_test_null,
4404 }, {
4405 .alg = "drbg_pr_sha256",
4406 .test = alg_test_drbg,
4407 .fips_allowed = 1,
4408 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004409 .drbg = __VECS(drbg_pr_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004410 }
4411 }, {
4412 /* covered by drbg_pr_sha256 test */
4413 .alg = "drbg_pr_sha384",
4414 .fips_allowed = 1,
4415 .test = alg_test_null,
4416 }, {
4417 .alg = "drbg_pr_sha512",
4418 .fips_allowed = 1,
4419 .test = alg_test_null,
4420 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004421 .alg = "ecb(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004422 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004423 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004424 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004425 .cipher = __VECS(aes_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004426 }
4427 }, {
4428 .alg = "ecb(anubis)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004429 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004430 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004431 .cipher = __VECS(anubis_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004432 }
4433 }, {
4434 .alg = "ecb(arc4)",
Ard Biesheuvel611a23c2019-06-12 18:19:57 +02004435 .generic_driver = "ecb(arc4)-generic",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004436 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004437 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004438 .cipher = __VECS(arc4_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004439 }
4440 }, {
4441 .alg = "ecb(blowfish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004442 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004443 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004444 .cipher = __VECS(bf_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004445 }
4446 }, {
4447 .alg = "ecb(camellia)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004448 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004449 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004450 .cipher = __VECS(camellia_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004451 }
4452 }, {
4453 .alg = "ecb(cast5)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004454 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004455 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004456 .cipher = __VECS(cast5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004457 }
4458 }, {
4459 .alg = "ecb(cast6)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004460 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004461 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004462 .cipher = __VECS(cast6_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004463 }
4464 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03004465 .alg = "ecb(cipher_null)",
4466 .test = alg_test_null,
Milan Broz6175ca22017-04-21 13:03:06 +02004467 .fips_allowed = 1,
Jussi Kivilinnae4483702013-04-07 16:43:56 +03004468 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004469 .alg = "ecb(des)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004470 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004471 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004472 .cipher = __VECS(des_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004473 }
4474 }, {
4475 .alg = "ecb(des3_ede)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004476 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004477 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004478 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004479 .cipher = __VECS(des3_ede_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004480 }
4481 }, {
Jussi Kivilinna66e5bd02013-01-19 13:31:36 +02004482 .alg = "ecb(fcrypt)",
4483 .test = alg_test_skcipher,
4484 .suite = {
4485 .cipher = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004486 .vecs = fcrypt_pcbc_tv_template,
4487 .count = 1
Jussi Kivilinna66e5bd02013-01-19 13:31:36 +02004488 }
4489 }
4490 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004491 .alg = "ecb(khazad)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004492 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004493 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004494 .cipher = __VECS(khazad_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004495 }
4496 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01004497 /* Same as ecb(aes) except the key is stored in
4498 * hardware secure memory which we reference by index
4499 */
4500 .alg = "ecb(paes)",
4501 .test = alg_test_null,
4502 .fips_allowed = 1,
4503 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004504 .alg = "ecb(seed)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004505 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004506 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004507 .cipher = __VECS(seed_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004508 }
4509 }, {
4510 .alg = "ecb(serpent)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004511 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004512 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004513 .cipher = __VECS(serpent_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004514 }
4515 }, {
Gilad Ben-Yossefcd83a8a2018-03-06 09:44:43 +00004516 .alg = "ecb(sm4)",
4517 .test = alg_test_skcipher,
4518 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004519 .cipher = __VECS(sm4_tv_template)
Gilad Ben-Yossefcd83a8a2018-03-06 09:44:43 +00004520 }
4521 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004522 .alg = "ecb(tea)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004523 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004524 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004525 .cipher = __VECS(tea_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004526 }
4527 }, {
4528 .alg = "ecb(tnepres)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004529 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004530 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004531 .cipher = __VECS(tnepres_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004532 }
4533 }, {
4534 .alg = "ecb(twofish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004535 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004536 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004537 .cipher = __VECS(tf_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004538 }
4539 }, {
4540 .alg = "ecb(xeta)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004541 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004542 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004543 .cipher = __VECS(xeta_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004544 }
4545 }, {
4546 .alg = "ecb(xtea)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004547 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004548 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004549 .cipher = __VECS(xtea_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004550 }
4551 }, {
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +01004552 .alg = "ecdh",
4553 .test = alg_test_kpp,
4554 .fips_allowed = 1,
4555 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004556 .kpp = __VECS(ecdh_tv_template)
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +01004557 }
4558 }, {
Vitaly Chikunov32fbdbd2019-04-11 18:51:21 +03004559 .alg = "ecrdsa",
4560 .test = alg_test_akcipher,
4561 .suite = {
4562 .akcipher = __VECS(ecrdsa_tv_template)
4563 }
4564 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004565 .alg = "gcm(aes)",
Eric Biggers40153b12019-04-11 21:57:41 -07004566 .generic_driver = "gcm_base(ctr(aes-generic),ghash-generic)",
Herbert Xuda7f0332008-07-31 17:08:25 +08004567 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004568 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004569 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004570 .aead = __VECS(aes_gcm_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004571 }
4572 }, {
Youquan, Song507069c2009-11-23 20:23:04 +08004573 .alg = "ghash",
4574 .test = alg_test_hash,
Jarod Wilson18c0ebd2011-01-29 15:14:35 +11004575 .fips_allowed = 1,
Youquan, Song507069c2009-11-23 20:23:04 +08004576 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004577 .hash = __VECS(ghash_tv_template)
Youquan, Song507069c2009-11-23 20:23:04 +08004578 }
4579 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004580 .alg = "hmac(md5)",
4581 .test = alg_test_hash,
4582 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004583 .hash = __VECS(hmac_md5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004584 }
4585 }, {
4586 .alg = "hmac(rmd128)",
4587 .test = alg_test_hash,
4588 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004589 .hash = __VECS(hmac_rmd128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004590 }
4591 }, {
4592 .alg = "hmac(rmd160)",
4593 .test = alg_test_hash,
4594 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004595 .hash = __VECS(hmac_rmd160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004596 }
4597 }, {
4598 .alg = "hmac(sha1)",
4599 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004600 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004601 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004602 .hash = __VECS(hmac_sha1_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004603 }
4604 }, {
4605 .alg = "hmac(sha224)",
4606 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004607 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004608 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004609 .hash = __VECS(hmac_sha224_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004610 }
4611 }, {
4612 .alg = "hmac(sha256)",
4613 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004614 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004615 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004616 .hash = __VECS(hmac_sha256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004617 }
4618 }, {
raveendra padasalagi98eca722016-07-01 11:16:54 +05304619 .alg = "hmac(sha3-224)",
4620 .test = alg_test_hash,
4621 .fips_allowed = 1,
4622 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004623 .hash = __VECS(hmac_sha3_224_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05304624 }
4625 }, {
4626 .alg = "hmac(sha3-256)",
4627 .test = alg_test_hash,
4628 .fips_allowed = 1,
4629 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004630 .hash = __VECS(hmac_sha3_256_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05304631 }
4632 }, {
4633 .alg = "hmac(sha3-384)",
4634 .test = alg_test_hash,
4635 .fips_allowed = 1,
4636 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004637 .hash = __VECS(hmac_sha3_384_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05304638 }
4639 }, {
4640 .alg = "hmac(sha3-512)",
4641 .test = alg_test_hash,
4642 .fips_allowed = 1,
4643 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004644 .hash = __VECS(hmac_sha3_512_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05304645 }
4646 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004647 .alg = "hmac(sha384)",
4648 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004649 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004650 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004651 .hash = __VECS(hmac_sha384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004652 }
4653 }, {
4654 .alg = "hmac(sha512)",
4655 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004656 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004657 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004658 .hash = __VECS(hmac_sha512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004659 }
4660 }, {
Vitaly Chikunov25a0b9d2018-11-07 00:00:03 +03004661 .alg = "hmac(streebog256)",
4662 .test = alg_test_hash,
4663 .suite = {
4664 .hash = __VECS(hmac_streebog256_tv_template)
4665 }
4666 }, {
4667 .alg = "hmac(streebog512)",
4668 .test = alg_test_hash,
4669 .suite = {
4670 .hash = __VECS(hmac_streebog512_tv_template)
4671 }
4672 }, {
Stephan Muellerbb5530e2015-05-25 15:10:20 +02004673 .alg = "jitterentropy_rng",
4674 .fips_allowed = 1,
4675 .test = alg_test_null,
4676 }, {
Stephan Mueller35351982015-09-21 20:59:56 +02004677 .alg = "kw(aes)",
4678 .test = alg_test_skcipher,
4679 .fips_allowed = 1,
4680 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004681 .cipher = __VECS(aes_kw_tv_template)
Stephan Mueller35351982015-09-21 20:59:56 +02004682 }
4683 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004684 .alg = "lrw(aes)",
Eric Biggersd435e102019-04-11 21:57:40 -07004685 .generic_driver = "lrw(ecb(aes-generic))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004686 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004687 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004688 .cipher = __VECS(aes_lrw_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004689 }
4690 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02004691 .alg = "lrw(camellia)",
Eric Biggersd435e102019-04-11 21:57:40 -07004692 .generic_driver = "lrw(ecb(camellia-generic))",
Jussi Kivilinna08406052012-03-05 20:26:21 +02004693 .test = alg_test_skcipher,
4694 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004695 .cipher = __VECS(camellia_lrw_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02004696 }
4697 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004698 .alg = "lrw(cast6)",
Eric Biggersd435e102019-04-11 21:57:40 -07004699 .generic_driver = "lrw(ecb(cast6-generic))",
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004700 .test = alg_test_skcipher,
4701 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004702 .cipher = __VECS(cast6_lrw_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004703 }
4704 }, {
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03004705 .alg = "lrw(serpent)",
Eric Biggersd435e102019-04-11 21:57:40 -07004706 .generic_driver = "lrw(ecb(serpent-generic))",
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03004707 .test = alg_test_skcipher,
4708 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004709 .cipher = __VECS(serpent_lrw_tv_template)
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03004710 }
4711 }, {
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03004712 .alg = "lrw(twofish)",
Eric Biggersd435e102019-04-11 21:57:40 -07004713 .generic_driver = "lrw(ecb(twofish-generic))",
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03004714 .test = alg_test_skcipher,
4715 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004716 .cipher = __VECS(tf_lrw_tv_template)
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03004717 }
4718 }, {
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02004719 .alg = "lz4",
4720 .test = alg_test_comp,
4721 .fips_allowed = 1,
4722 .suite = {
4723 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004724 .comp = __VECS(lz4_comp_tv_template),
4725 .decomp = __VECS(lz4_decomp_tv_template)
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02004726 }
4727 }
4728 }, {
4729 .alg = "lz4hc",
4730 .test = alg_test_comp,
4731 .fips_allowed = 1,
4732 .suite = {
4733 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004734 .comp = __VECS(lz4hc_comp_tv_template),
4735 .decomp = __VECS(lz4hc_decomp_tv_template)
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02004736 }
4737 }
4738 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004739 .alg = "lzo",
4740 .test = alg_test_comp,
Milan Broz08189042012-12-06 17:16:28 +08004741 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004742 .suite = {
4743 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004744 .comp = __VECS(lzo_comp_tv_template),
4745 .decomp = __VECS(lzo_decomp_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004746 }
4747 }
4748 }, {
4749 .alg = "md4",
4750 .test = alg_test_hash,
4751 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004752 .hash = __VECS(md4_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004753 }
4754 }, {
4755 .alg = "md5",
4756 .test = alg_test_hash,
4757 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004758 .hash = __VECS(md5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004759 }
4760 }, {
4761 .alg = "michael_mic",
4762 .test = alg_test_hash,
4763 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004764 .hash = __VECS(michael_mic_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004765 }
4766 }, {
Ondrej Mosnacek4feb4c52018-05-11 14:19:10 +02004767 .alg = "morus1280",
4768 .test = alg_test_aead,
4769 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004770 .aead = __VECS(morus1280_tv_template)
Ondrej Mosnacek4feb4c52018-05-11 14:19:10 +02004771 }
4772 }, {
4773 .alg = "morus640",
4774 .test = alg_test_aead,
4775 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004776 .aead = __VECS(morus640_tv_template)
Ondrej Mosnacek4feb4c52018-05-11 14:19:10 +02004777 }
4778 }, {
Eric Biggers26609a22018-11-16 17:26:29 -08004779 .alg = "nhpoly1305",
4780 .test = alg_test_hash,
4781 .suite = {
4782 .hash = __VECS(nhpoly1305_tv_template)
4783 }
4784 }, {
Puneet Saxenaba0e14a2011-05-04 15:04:10 +10004785 .alg = "ofb(aes)",
4786 .test = alg_test_skcipher,
4787 .fips_allowed = 1,
4788 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004789 .cipher = __VECS(aes_ofb_tv_template)
Puneet Saxenaba0e14a2011-05-04 15:04:10 +10004790 }
4791 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01004792 /* Same as ofb(aes) except the key is stored in
4793 * hardware secure memory which we reference by index
4794 */
4795 .alg = "ofb(paes)",
4796 .test = alg_test_null,
4797 .fips_allowed = 1,
4798 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004799 .alg = "pcbc(fcrypt)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004800 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004801 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004802 .cipher = __VECS(fcrypt_pcbc_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004803 }
4804 }, {
Stephan Mueller12071072017-06-12 23:27:51 +02004805 .alg = "pkcs1pad(rsa,sha224)",
4806 .test = alg_test_null,
4807 .fips_allowed = 1,
4808 }, {
4809 .alg = "pkcs1pad(rsa,sha256)",
4810 .test = alg_test_akcipher,
4811 .fips_allowed = 1,
4812 .suite = {
4813 .akcipher = __VECS(pkcs1pad_rsa_tv_template)
4814 }
4815 }, {
4816 .alg = "pkcs1pad(rsa,sha384)",
4817 .test = alg_test_null,
4818 .fips_allowed = 1,
4819 }, {
4820 .alg = "pkcs1pad(rsa,sha512)",
4821 .test = alg_test_null,
4822 .fips_allowed = 1,
4823 }, {
Martin Willieee9dc62015-06-01 13:43:59 +02004824 .alg = "poly1305",
4825 .test = alg_test_hash,
4826 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004827 .hash = __VECS(poly1305_tv_template)
Martin Willieee9dc62015-06-01 13:43:59 +02004828 }
4829 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004830 .alg = "rfc3686(ctr(aes))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004831 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004832 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004833 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004834 .cipher = __VECS(aes_ctr_rfc3686_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004835 }
4836 }, {
Herbert Xu3f31a742015-07-09 07:17:34 +08004837 .alg = "rfc4106(gcm(aes))",
Eric Biggers40153b12019-04-11 21:57:41 -07004838 .generic_driver = "rfc4106(gcm_base(ctr(aes-generic),ghash-generic))",
Adrian Hoban69435b92010-11-04 15:02:04 -04004839 .test = alg_test_aead,
Jarod Wilsondb71f29a2015-01-23 12:42:15 -05004840 .fips_allowed = 1,
Adrian Hoban69435b92010-11-04 15:02:04 -04004841 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004842 .aead = __VECS(aes_gcm_rfc4106_tv_template)
Adrian Hoban69435b92010-11-04 15:02:04 -04004843 }
4844 }, {
Herbert Xu544c4362015-07-14 16:53:22 +08004845 .alg = "rfc4309(ccm(aes))",
Eric Biggers40153b12019-04-11 21:57:41 -07004846 .generic_driver = "rfc4309(ccm_base(ctr(aes-generic),cbcmac(aes-generic)))",
Jarod Wilson5d667322009-05-04 19:23:40 +08004847 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004848 .fips_allowed = 1,
Jarod Wilson5d667322009-05-04 19:23:40 +08004849 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004850 .aead = __VECS(aes_ccm_rfc4309_tv_template)
Jarod Wilson5d667322009-05-04 19:23:40 +08004851 }
4852 }, {
Herbert Xubb687452015-06-16 13:54:24 +08004853 .alg = "rfc4543(gcm(aes))",
Eric Biggers40153b12019-04-11 21:57:41 -07004854 .generic_driver = "rfc4543(gcm_base(ctr(aes-generic),ghash-generic))",
Jussi Kivilinnae9b74412013-04-07 16:43:51 +03004855 .test = alg_test_aead,
4856 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004857 .aead = __VECS(aes_gcm_rfc4543_tv_template)
Jussi Kivilinnae9b74412013-04-07 16:43:51 +03004858 }
4859 }, {
Martin Williaf2b76b2015-06-01 13:44:01 +02004860 .alg = "rfc7539(chacha20,poly1305)",
4861 .test = alg_test_aead,
4862 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004863 .aead = __VECS(rfc7539_tv_template)
Martin Williaf2b76b2015-06-01 13:44:01 +02004864 }
4865 }, {
Martin Willi59007582015-06-01 13:44:03 +02004866 .alg = "rfc7539esp(chacha20,poly1305)",
4867 .test = alg_test_aead,
4868 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004869 .aead = __VECS(rfc7539esp_tv_template)
Martin Willi59007582015-06-01 13:44:03 +02004870 }
4871 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004872 .alg = "rmd128",
4873 .test = alg_test_hash,
4874 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004875 .hash = __VECS(rmd128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004876 }
4877 }, {
4878 .alg = "rmd160",
4879 .test = alg_test_hash,
4880 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004881 .hash = __VECS(rmd160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004882 }
4883 }, {
4884 .alg = "rmd256",
4885 .test = alg_test_hash,
4886 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004887 .hash = __VECS(rmd256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004888 }
4889 }, {
4890 .alg = "rmd320",
4891 .test = alg_test_hash,
4892 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004893 .hash = __VECS(rmd320_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004894 }
4895 }, {
Tadeusz Struk946cc462015-06-16 10:31:06 -07004896 .alg = "rsa",
4897 .test = alg_test_akcipher,
4898 .fips_allowed = 1,
4899 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004900 .akcipher = __VECS(rsa_tv_template)
Tadeusz Struk946cc462015-06-16 10:31:06 -07004901 }
4902 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004903 .alg = "salsa20",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004904 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004905 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004906 .cipher = __VECS(salsa20_stream_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004907 }
4908 }, {
4909 .alg = "sha1",
4910 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004911 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004912 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004913 .hash = __VECS(sha1_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004914 }
4915 }, {
4916 .alg = "sha224",
4917 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004918 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004919 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004920 .hash = __VECS(sha224_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004921 }
4922 }, {
4923 .alg = "sha256",
4924 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004925 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004926 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004927 .hash = __VECS(sha256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004928 }
4929 }, {
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05304930 .alg = "sha3-224",
4931 .test = alg_test_hash,
4932 .fips_allowed = 1,
4933 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004934 .hash = __VECS(sha3_224_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05304935 }
4936 }, {
4937 .alg = "sha3-256",
4938 .test = alg_test_hash,
4939 .fips_allowed = 1,
4940 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004941 .hash = __VECS(sha3_256_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05304942 }
4943 }, {
4944 .alg = "sha3-384",
4945 .test = alg_test_hash,
4946 .fips_allowed = 1,
4947 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004948 .hash = __VECS(sha3_384_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05304949 }
4950 }, {
4951 .alg = "sha3-512",
4952 .test = alg_test_hash,
4953 .fips_allowed = 1,
4954 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004955 .hash = __VECS(sha3_512_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05304956 }
4957 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004958 .alg = "sha384",
4959 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004960 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004961 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004962 .hash = __VECS(sha384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004963 }
4964 }, {
4965 .alg = "sha512",
4966 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004967 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004968 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004969 .hash = __VECS(sha512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004970 }
4971 }, {
Gilad Ben-Yossefb7e27532017-08-21 13:51:29 +03004972 .alg = "sm3",
4973 .test = alg_test_hash,
4974 .suite = {
4975 .hash = __VECS(sm3_tv_template)
4976 }
4977 }, {
Vitaly Chikunov25a0b9d2018-11-07 00:00:03 +03004978 .alg = "streebog256",
4979 .test = alg_test_hash,
4980 .suite = {
4981 .hash = __VECS(streebog256_tv_template)
4982 }
4983 }, {
4984 .alg = "streebog512",
4985 .test = alg_test_hash,
4986 .suite = {
4987 .hash = __VECS(streebog512_tv_template)
4988 }
4989 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004990 .alg = "tgr128",
4991 .test = alg_test_hash,
4992 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004993 .hash = __VECS(tgr128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004994 }
4995 }, {
4996 .alg = "tgr160",
4997 .test = alg_test_hash,
4998 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004999 .hash = __VECS(tgr160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005000 }
5001 }, {
5002 .alg = "tgr192",
5003 .test = alg_test_hash,
5004 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005005 .hash = __VECS(tgr192_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005006 }
5007 }, {
Eric Biggersed331ad2018-06-18 10:22:39 -07005008 .alg = "vmac64(aes)",
5009 .test = alg_test_hash,
5010 .suite = {
5011 .hash = __VECS(vmac64_aes_tv_template)
5012 }
5013 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005014 .alg = "wp256",
5015 .test = alg_test_hash,
5016 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005017 .hash = __VECS(wp256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005018 }
5019 }, {
5020 .alg = "wp384",
5021 .test = alg_test_hash,
5022 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005023 .hash = __VECS(wp384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005024 }
5025 }, {
5026 .alg = "wp512",
5027 .test = alg_test_hash,
5028 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005029 .hash = __VECS(wp512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005030 }
5031 }, {
5032 .alg = "xcbc(aes)",
5033 .test = alg_test_hash,
5034 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005035 .hash = __VECS(aes_xcbc128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005036 }
5037 }, {
Eric Biggersaa762402018-11-16 17:26:22 -08005038 .alg = "xchacha12",
5039 .test = alg_test_skcipher,
5040 .suite = {
5041 .cipher = __VECS(xchacha12_tv_template)
5042 },
5043 }, {
Eric Biggersde61d7a2018-11-16 17:26:20 -08005044 .alg = "xchacha20",
5045 .test = alg_test_skcipher,
5046 .suite = {
5047 .cipher = __VECS(xchacha20_tv_template)
5048 },
5049 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005050 .alg = "xts(aes)",
Eric Biggersd435e102019-04-11 21:57:40 -07005051 .generic_driver = "xts(ecb(aes-generic))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005052 .test = alg_test_skcipher,
Jarod Wilson2918aa82011-01-29 15:14:01 +11005053 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08005054 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005055 .cipher = __VECS(aes_xts_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005056 }
Geert Uytterhoeven0c01aed2009-03-04 15:42:15 +08005057 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02005058 .alg = "xts(camellia)",
Eric Biggersd435e102019-04-11 21:57:40 -07005059 .generic_driver = "xts(ecb(camellia-generic))",
Jussi Kivilinna08406052012-03-05 20:26:21 +02005060 .test = alg_test_skcipher,
5061 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005062 .cipher = __VECS(camellia_xts_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02005063 }
5064 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02005065 .alg = "xts(cast6)",
Eric Biggersd435e102019-04-11 21:57:40 -07005066 .generic_driver = "xts(ecb(cast6-generic))",
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02005067 .test = alg_test_skcipher,
5068 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005069 .cipher = __VECS(cast6_xts_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02005070 }
5071 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01005072 /* Same as xts(aes) except the key is stored in
5073 * hardware secure memory which we reference by index
5074 */
5075 .alg = "xts(paes)",
5076 .test = alg_test_null,
5077 .fips_allowed = 1,
5078 }, {
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03005079 .alg = "xts(serpent)",
Eric Biggersd435e102019-04-11 21:57:40 -07005080 .generic_driver = "xts(ecb(serpent-generic))",
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03005081 .test = alg_test_skcipher,
5082 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005083 .cipher = __VECS(serpent_xts_tv_template)
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03005084 }
5085 }, {
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03005086 .alg = "xts(twofish)",
Eric Biggersd435e102019-04-11 21:57:40 -07005087 .generic_driver = "xts(ecb(twofish-generic))",
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03005088 .test = alg_test_skcipher,
5089 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005090 .cipher = __VECS(tf_xts_tv_template)
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03005091 }
Giovanni Cabiddua368f432017-04-21 21:54:30 +01005092 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01005093 .alg = "xts4096(paes)",
5094 .test = alg_test_null,
5095 .fips_allowed = 1,
5096 }, {
5097 .alg = "xts512(paes)",
5098 .test = alg_test_null,
5099 .fips_allowed = 1,
5100 }, {
Nikolay Borisov67882e72019-05-30 09:52:57 +03005101 .alg = "xxhash64",
5102 .test = alg_test_hash,
5103 .fips_allowed = 1,
5104 .suite = {
5105 .hash = __VECS(xxhash64_tv_template)
5106 }
5107 }, {
Giovanni Cabiddua368f432017-04-21 21:54:30 +01005108 .alg = "zlib-deflate",
5109 .test = alg_test_comp,
5110 .fips_allowed = 1,
5111 .suite = {
5112 .comp = {
5113 .comp = __VECS(zlib_deflate_comp_tv_template),
5114 .decomp = __VECS(zlib_deflate_decomp_tv_template)
5115 }
5116 }
Nick Terrelld28fc3d2018-03-30 12:14:53 -07005117 }, {
5118 .alg = "zstd",
5119 .test = alg_test_comp,
5120 .fips_allowed = 1,
5121 .suite = {
5122 .comp = {
5123 .comp = __VECS(zstd_comp_tv_template),
5124 .decomp = __VECS(zstd_decomp_tv_template)
5125 }
5126 }
Herbert Xuda7f0332008-07-31 17:08:25 +08005127 }
5128};
5129
Eric Biggers3f47a032019-01-31 23:51:43 -08005130static void alg_check_test_descs_order(void)
Jussi Kivilinna57147582013-06-13 17:37:40 +03005131{
5132 int i;
5133
Jussi Kivilinna57147582013-06-13 17:37:40 +03005134 for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
5135 int diff = strcmp(alg_test_descs[i - 1].alg,
5136 alg_test_descs[i].alg);
5137
5138 if (WARN_ON(diff > 0)) {
5139 pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
5140 alg_test_descs[i - 1].alg,
5141 alg_test_descs[i].alg);
5142 }
5143
5144 if (WARN_ON(diff == 0)) {
5145 pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
5146 alg_test_descs[i].alg);
5147 }
5148 }
5149}
5150
Eric Biggers3f47a032019-01-31 23:51:43 -08005151static void alg_check_testvec_configs(void)
5152{
Eric Biggers4e7babba2019-01-31 23:51:46 -08005153 int i;
5154
5155 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++)
5156 WARN_ON(!valid_testvec_config(
5157 &default_cipher_testvec_configs[i]));
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08005158
5159 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++)
5160 WARN_ON(!valid_testvec_config(
5161 &default_hash_testvec_configs[i]));
Eric Biggers3f47a032019-01-31 23:51:43 -08005162}
5163
5164static void testmgr_onetime_init(void)
5165{
5166 alg_check_test_descs_order();
5167 alg_check_testvec_configs();
Eric Biggers5b2706a2019-01-31 23:51:44 -08005168
5169#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
5170 pr_warn("alg: extra crypto tests enabled. This is intended for developer use only.\n");
5171#endif
Eric Biggers3f47a032019-01-31 23:51:43 -08005172}
5173
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005174static int alg_find_test(const char *alg)
Herbert Xuda7f0332008-07-31 17:08:25 +08005175{
5176 int start = 0;
5177 int end = ARRAY_SIZE(alg_test_descs);
5178
5179 while (start < end) {
5180 int i = (start + end) / 2;
5181 int diff = strcmp(alg_test_descs[i].alg, alg);
5182
5183 if (diff > 0) {
5184 end = i;
5185 continue;
5186 }
5187
5188 if (diff < 0) {
5189 start = i + 1;
5190 continue;
5191 }
5192
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005193 return i;
Herbert Xuda7f0332008-07-31 17:08:25 +08005194 }
5195
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005196 return -1;
5197}
5198
5199int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
5200{
5201 int i;
Herbert Xua68f6612009-07-02 16:32:12 +08005202 int j;
Neil Hormand12d6b62008-10-12 20:36:51 +08005203 int rc;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005204
Richard W.M. Jones9e5c9fe2016-05-03 10:00:17 +01005205 if (!fips_enabled && notests) {
5206 printk_once(KERN_INFO "alg: self-tests disabled\n");
5207 return 0;
5208 }
5209
Eric Biggers3f47a032019-01-31 23:51:43 -08005210 DO_ONCE(testmgr_onetime_init);
Jussi Kivilinna57147582013-06-13 17:37:40 +03005211
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005212 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
5213 char nalg[CRYPTO_MAX_ALG_NAME];
5214
5215 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
5216 sizeof(nalg))
5217 return -ENAMETOOLONG;
5218
5219 i = alg_find_test(nalg);
5220 if (i < 0)
5221 goto notest;
5222
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10005223 if (fips_enabled && !alg_test_descs[i].fips_allowed)
5224 goto non_fips_alg;
5225
Jarod Wilson941fb322009-05-04 19:49:23 +08005226 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
5227 goto test_done;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005228 }
5229
5230 i = alg_find_test(alg);
Herbert Xua68f6612009-07-02 16:32:12 +08005231 j = alg_find_test(driver);
5232 if (i < 0 && j < 0)
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005233 goto notest;
5234
Herbert Xua68f6612009-07-02 16:32:12 +08005235 if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
5236 (j >= 0 && !alg_test_descs[j].fips_allowed)))
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10005237 goto non_fips_alg;
5238
Herbert Xua68f6612009-07-02 16:32:12 +08005239 rc = 0;
5240 if (i >= 0)
5241 rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
5242 type, mask);
Cristian Stoica032c8ca2013-07-18 18:57:07 +03005243 if (j >= 0 && j != i)
Herbert Xua68f6612009-07-02 16:32:12 +08005244 rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
5245 type, mask);
5246
Jarod Wilson941fb322009-05-04 19:49:23 +08005247test_done:
Eric Biggerseda69b02019-03-31 13:09:14 -07005248 if (rc && (fips_enabled || panic_on_fail))
5249 panic("alg: self-tests for %s (%s) failed in %s mode!\n",
5250 driver, alg, fips_enabled ? "fips" : "panic_on_fail");
Neil Hormand12d6b62008-10-12 20:36:51 +08005251
Jarod Wilson29ecd4a2009-05-04 19:51:17 +08005252 if (fips_enabled && !rc)
Masanari Iida3e8cffd2014-10-07 00:37:54 +09005253 pr_info("alg: self-tests for %s (%s) passed\n", driver, alg);
Jarod Wilson29ecd4a2009-05-04 19:51:17 +08005254
Neil Hormand12d6b62008-10-12 20:36:51 +08005255 return rc;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005256
5257notest:
Herbert Xuda7f0332008-07-31 17:08:25 +08005258 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
5259 return 0;
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10005260non_fips_alg:
5261 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08005262}
Alexander Shishkin0b767f92010-06-03 20:53:43 +10005263
Herbert Xu326a6342010-08-06 09:40:28 +08005264#endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
Alexander Shishkin0b767f92010-06-03 20:53:43 +10005265
Herbert Xuda7f0332008-07-31 17:08:25 +08005266EXPORT_SYMBOL_GPL(alg_test);