blob: 87abfd1ce2329fb7d2ae48a2aaafbbe58351d19c [file] [log] [blame]
Herbert Xuda7f0332008-07-31 17:08:25 +08001/*
2 * Algorithm testing framework and tests.
3 *
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
6 * Copyright (c) 2007 Nokia Siemens Networks
7 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
Eric Biggers3f47a032019-01-31 23:51:43 -08008 * Copyright (c) 2019 Google LLC
Herbert Xuda7f0332008-07-31 17:08:25 +08009 *
Adrian Hoban69435b92010-11-04 15:02:04 -040010 * Updated RFC4106 AES-GCM testing.
11 * Authors: Aidan O'Mahony (aidan.o.mahony@intel.com)
12 * Adrian Hoban <adrian.hoban@intel.com>
13 * Gabriele Paoloni <gabriele.paoloni@intel.com>
14 * Tadeusz Struk (tadeusz.struk@intel.com)
15 * Copyright (c) 2010, Intel Corporation.
16 *
Herbert Xuda7f0332008-07-31 17:08:25 +080017 * This program is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the Free
19 * Software Foundation; either version 2 of the License, or (at your option)
20 * any later version.
21 *
22 */
23
Herbert Xu1ce33112015-04-22 15:06:31 +080024#include <crypto/aead.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080025#include <crypto/hash.h>
Herbert Xu12773d92015-08-20 15:21:46 +080026#include <crypto/skcipher.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080027#include <linux/err.h>
Herbert Xu1c41b882015-04-22 13:25:58 +080028#include <linux/fips.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080029#include <linux/module.h>
Eric Biggers3f47a032019-01-31 23:51:43 -080030#include <linux/once.h>
Eric Biggers25f9ddd2019-01-31 23:51:45 -080031#include <linux/random.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080032#include <linux/scatterlist.h>
33#include <linux/slab.h>
34#include <linux/string.h>
Jarod Wilson7647d6c2009-05-04 19:44:50 +080035#include <crypto/rng.h>
Stephan Mueller64d1cdf2014-05-31 17:25:36 +020036#include <crypto/drbg.h>
Tadeusz Struk946cc462015-06-16 10:31:06 -070037#include <crypto/akcipher.h>
Salvatore Benedetto802c7f12016-06-22 17:49:14 +010038#include <crypto/kpp.h>
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +010039#include <crypto/acompress.h>
Eric Biggersb55e1a32019-03-12 22:12:47 -070040#include <crypto/internal/simd.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080041
42#include "internal.h"
Alexander Shishkin0b767f92010-06-03 20:53:43 +100043
Richard W.M. Jones9e5c9fe2016-05-03 10:00:17 +010044static bool notests;
45module_param(notests, bool, 0644);
46MODULE_PARM_DESC(notests, "disable crypto self-tests");
47
Eric Biggerseda69b02019-03-31 13:09:14 -070048static bool panic_on_fail;
49module_param(panic_on_fail, bool, 0444);
50
Eric Biggers5b2706a2019-01-31 23:51:44 -080051#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
52static bool noextratests;
53module_param(noextratests, bool, 0644);
54MODULE_PARM_DESC(noextratests, "disable expensive crypto self-tests");
55
56static unsigned int fuzz_iterations = 100;
57module_param(fuzz_iterations, uint, 0644);
58MODULE_PARM_DESC(fuzz_iterations, "number of fuzz test iterations");
Eric Biggersb55e1a32019-03-12 22:12:47 -070059
60DEFINE_PER_CPU(bool, crypto_simd_disabled_for_test);
61EXPORT_PER_CPU_SYMBOL_GPL(crypto_simd_disabled_for_test);
Eric Biggers5b2706a2019-01-31 23:51:44 -080062#endif
63
Herbert Xu326a6342010-08-06 09:40:28 +080064#ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
Alexander Shishkin0b767f92010-06-03 20:53:43 +100065
66/* a perfect nop */
67int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
68{
69 return 0;
70}
71
72#else
73
Herbert Xuda7f0332008-07-31 17:08:25 +080074#include "testmgr.h"
75
76/*
77 * Need slab memory for testing (size in number of pages).
78 */
79#define XBUFSIZE 8
80
81/*
Herbert Xuda7f0332008-07-31 17:08:25 +080082* Used by test_cipher()
83*/
84#define ENCRYPT 1
85#define DECRYPT 0
86
Herbert Xuda7f0332008-07-31 17:08:25 +080087struct aead_test_suite {
Eric Biggersa0d608ee2019-01-13 15:32:28 -080088 const struct aead_testvec *vecs;
89 unsigned int count;
Herbert Xuda7f0332008-07-31 17:08:25 +080090};
91
92struct cipher_test_suite {
Eric Biggers92a4c9f2018-05-20 22:50:29 -070093 const struct cipher_testvec *vecs;
94 unsigned int count;
Herbert Xuda7f0332008-07-31 17:08:25 +080095};
96
97struct comp_test_suite {
98 struct {
Eric Biggersb13b1e02017-02-24 15:46:59 -080099 const struct comp_testvec *vecs;
Herbert Xuda7f0332008-07-31 17:08:25 +0800100 unsigned int count;
101 } comp, decomp;
102};
103
104struct hash_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800105 const struct hash_testvec *vecs;
Herbert Xuda7f0332008-07-31 17:08:25 +0800106 unsigned int count;
107};
108
Jarod Wilson7647d6c2009-05-04 19:44:50 +0800109struct cprng_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800110 const struct cprng_testvec *vecs;
Jarod Wilson7647d6c2009-05-04 19:44:50 +0800111 unsigned int count;
112};
113
Stephan Mueller64d1cdf2014-05-31 17:25:36 +0200114struct drbg_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800115 const struct drbg_testvec *vecs;
Stephan Mueller64d1cdf2014-05-31 17:25:36 +0200116 unsigned int count;
117};
118
Tadeusz Struk946cc462015-06-16 10:31:06 -0700119struct akcipher_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800120 const struct akcipher_testvec *vecs;
Tadeusz Struk946cc462015-06-16 10:31:06 -0700121 unsigned int count;
122};
123
Salvatore Benedetto802c7f12016-06-22 17:49:14 +0100124struct kpp_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800125 const struct kpp_testvec *vecs;
Salvatore Benedetto802c7f12016-06-22 17:49:14 +0100126 unsigned int count;
127};
128
Herbert Xuda7f0332008-07-31 17:08:25 +0800129struct alg_test_desc {
130 const char *alg;
Eric Biggersf2bb770a2019-04-11 21:57:38 -0700131 const char *generic_driver;
Herbert Xuda7f0332008-07-31 17:08:25 +0800132 int (*test)(const struct alg_test_desc *desc, const char *driver,
133 u32 type, u32 mask);
Jarod Wilsona1915d52009-05-15 15:16:03 +1000134 int fips_allowed; /* set if alg is allowed in fips mode */
Herbert Xuda7f0332008-07-31 17:08:25 +0800135
136 union {
137 struct aead_test_suite aead;
138 struct cipher_test_suite cipher;
139 struct comp_test_suite comp;
140 struct hash_test_suite hash;
Jarod Wilson7647d6c2009-05-04 19:44:50 +0800141 struct cprng_test_suite cprng;
Stephan Mueller64d1cdf2014-05-31 17:25:36 +0200142 struct drbg_test_suite drbg;
Tadeusz Struk946cc462015-06-16 10:31:06 -0700143 struct akcipher_test_suite akcipher;
Salvatore Benedetto802c7f12016-06-22 17:49:14 +0100144 struct kpp_test_suite kpp;
Herbert Xuda7f0332008-07-31 17:08:25 +0800145 } suite;
146};
147
Herbert Xuda7f0332008-07-31 17:08:25 +0800148static void hexdump(unsigned char *buf, unsigned int len)
149{
150 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
151 16, 1,
152 buf, len, false);
153}
154
Eric Biggers3f47a032019-01-31 23:51:43 -0800155static int __testmgr_alloc_buf(char *buf[XBUFSIZE], int order)
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800156{
157 int i;
158
159 for (i = 0; i < XBUFSIZE; i++) {
Eric Biggers3f47a032019-01-31 23:51:43 -0800160 buf[i] = (char *)__get_free_pages(GFP_KERNEL, order);
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800161 if (!buf[i])
162 goto err_free_buf;
163 }
164
165 return 0;
166
167err_free_buf:
168 while (i-- > 0)
Eric Biggers3f47a032019-01-31 23:51:43 -0800169 free_pages((unsigned long)buf[i], order);
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800170
171 return -ENOMEM;
172}
173
Eric Biggers3f47a032019-01-31 23:51:43 -0800174static int testmgr_alloc_buf(char *buf[XBUFSIZE])
175{
176 return __testmgr_alloc_buf(buf, 0);
177}
178
179static void __testmgr_free_buf(char *buf[XBUFSIZE], int order)
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800180{
181 int i;
182
183 for (i = 0; i < XBUFSIZE; i++)
Eric Biggers3f47a032019-01-31 23:51:43 -0800184 free_pages((unsigned long)buf[i], order);
185}
186
187static void testmgr_free_buf(char *buf[XBUFSIZE])
188{
189 __testmgr_free_buf(buf, 0);
190}
191
192#define TESTMGR_POISON_BYTE 0xfe
193#define TESTMGR_POISON_LEN 16
194
195static inline void testmgr_poison(void *addr, size_t len)
196{
197 memset(addr, TESTMGR_POISON_BYTE, len);
198}
199
200/* Is the memory region still fully poisoned? */
201static inline bool testmgr_is_poison(const void *addr, size_t len)
202{
203 return memchr_inv(addr, TESTMGR_POISON_BYTE, len) == NULL;
204}
205
206/* flush type for hash algorithms */
207enum flush_type {
208 /* merge with update of previous buffer(s) */
209 FLUSH_TYPE_NONE = 0,
210
211 /* update with previous buffer(s) before doing this one */
212 FLUSH_TYPE_FLUSH,
213
214 /* likewise, but also export and re-import the intermediate state */
215 FLUSH_TYPE_REIMPORT,
216};
217
218/* finalization function for hash algorithms */
219enum finalization_type {
220 FINALIZATION_TYPE_FINAL, /* use final() */
221 FINALIZATION_TYPE_FINUP, /* use finup() */
222 FINALIZATION_TYPE_DIGEST, /* use digest() */
223};
224
225#define TEST_SG_TOTAL 10000
226
227/**
228 * struct test_sg_division - description of a scatterlist entry
229 *
230 * This struct describes one entry of a scatterlist being constructed to check a
231 * crypto test vector.
232 *
233 * @proportion_of_total: length of this chunk relative to the total length,
234 * given as a proportion out of TEST_SG_TOTAL so that it
235 * scales to fit any test vector
236 * @offset: byte offset into a 2-page buffer at which this chunk will start
237 * @offset_relative_to_alignmask: if true, add the algorithm's alignmask to the
238 * @offset
239 * @flush_type: for hashes, whether an update() should be done now vs.
240 * continuing to accumulate data
Eric Biggers65707372019-03-12 22:12:52 -0700241 * @nosimd: if doing the pending update(), do it with SIMD disabled?
Eric Biggers3f47a032019-01-31 23:51:43 -0800242 */
243struct test_sg_division {
244 unsigned int proportion_of_total;
245 unsigned int offset;
246 bool offset_relative_to_alignmask;
247 enum flush_type flush_type;
Eric Biggers65707372019-03-12 22:12:52 -0700248 bool nosimd;
Eric Biggers3f47a032019-01-31 23:51:43 -0800249};
250
251/**
252 * struct testvec_config - configuration for testing a crypto test vector
253 *
254 * This struct describes the data layout and other parameters with which each
255 * crypto test vector can be tested.
256 *
257 * @name: name of this config, logged for debugging purposes if a test fails
258 * @inplace: operate on the data in-place, if applicable for the algorithm type?
259 * @req_flags: extra request_flags, e.g. CRYPTO_TFM_REQ_MAY_SLEEP
260 * @src_divs: description of how to arrange the source scatterlist
261 * @dst_divs: description of how to arrange the dst scatterlist, if applicable
262 * for the algorithm type. Defaults to @src_divs if unset.
263 * @iv_offset: misalignment of the IV in the range [0..MAX_ALGAPI_ALIGNMASK+1],
264 * where 0 is aligned to a 2*(MAX_ALGAPI_ALIGNMASK+1) byte boundary
265 * @iv_offset_relative_to_alignmask: if true, add the algorithm's alignmask to
266 * the @iv_offset
267 * @finalization_type: what finalization function to use for hashes
Eric Biggers65707372019-03-12 22:12:52 -0700268 * @nosimd: execute with SIMD disabled? Requires !CRYPTO_TFM_REQ_MAY_SLEEP.
Eric Biggers3f47a032019-01-31 23:51:43 -0800269 */
270struct testvec_config {
271 const char *name;
272 bool inplace;
273 u32 req_flags;
274 struct test_sg_division src_divs[XBUFSIZE];
275 struct test_sg_division dst_divs[XBUFSIZE];
276 unsigned int iv_offset;
277 bool iv_offset_relative_to_alignmask;
278 enum finalization_type finalization_type;
Eric Biggers65707372019-03-12 22:12:52 -0700279 bool nosimd;
Eric Biggers3f47a032019-01-31 23:51:43 -0800280};
281
282#define TESTVEC_CONFIG_NAMELEN 192
283
Eric Biggers4e7babba2019-01-31 23:51:46 -0800284/*
285 * The following are the lists of testvec_configs to test for each algorithm
286 * type when the basic crypto self-tests are enabled, i.e. when
287 * CONFIG_CRYPTO_MANAGER_DISABLE_TESTS is unset. They aim to provide good test
288 * coverage, while keeping the test time much shorter than the full fuzz tests
289 * so that the basic tests can be enabled in a wider range of circumstances.
290 */
291
292/* Configs for skciphers and aeads */
293static const struct testvec_config default_cipher_testvec_configs[] = {
294 {
295 .name = "in-place",
296 .inplace = true,
297 .src_divs = { { .proportion_of_total = 10000 } },
298 }, {
299 .name = "out-of-place",
300 .src_divs = { { .proportion_of_total = 10000 } },
301 }, {
302 .name = "unaligned buffer, offset=1",
303 .src_divs = { { .proportion_of_total = 10000, .offset = 1 } },
304 .iv_offset = 1,
305 }, {
306 .name = "buffer aligned only to alignmask",
307 .src_divs = {
308 {
309 .proportion_of_total = 10000,
310 .offset = 1,
311 .offset_relative_to_alignmask = true,
312 },
313 },
314 .iv_offset = 1,
315 .iv_offset_relative_to_alignmask = true,
316 }, {
317 .name = "two even aligned splits",
318 .src_divs = {
319 { .proportion_of_total = 5000 },
320 { .proportion_of_total = 5000 },
321 },
322 }, {
323 .name = "uneven misaligned splits, may sleep",
324 .req_flags = CRYPTO_TFM_REQ_MAY_SLEEP,
325 .src_divs = {
326 { .proportion_of_total = 1900, .offset = 33 },
327 { .proportion_of_total = 3300, .offset = 7 },
328 { .proportion_of_total = 4800, .offset = 18 },
329 },
330 .iv_offset = 3,
331 }, {
332 .name = "misaligned splits crossing pages, inplace",
333 .inplace = true,
334 .src_divs = {
335 {
336 .proportion_of_total = 7500,
337 .offset = PAGE_SIZE - 32
338 }, {
339 .proportion_of_total = 2500,
340 .offset = PAGE_SIZE - 7
341 },
342 },
343 }
344};
345
Eric Biggers4cc2dcf2019-01-31 23:51:48 -0800346static const struct testvec_config default_hash_testvec_configs[] = {
347 {
348 .name = "init+update+final aligned buffer",
349 .src_divs = { { .proportion_of_total = 10000 } },
350 .finalization_type = FINALIZATION_TYPE_FINAL,
351 }, {
352 .name = "init+finup aligned buffer",
353 .src_divs = { { .proportion_of_total = 10000 } },
354 .finalization_type = FINALIZATION_TYPE_FINUP,
355 }, {
356 .name = "digest aligned buffer",
357 .src_divs = { { .proportion_of_total = 10000 } },
358 .finalization_type = FINALIZATION_TYPE_DIGEST,
359 }, {
360 .name = "init+update+final misaligned buffer",
361 .src_divs = { { .proportion_of_total = 10000, .offset = 1 } },
362 .finalization_type = FINALIZATION_TYPE_FINAL,
363 }, {
364 .name = "digest buffer aligned only to alignmask",
365 .src_divs = {
366 {
367 .proportion_of_total = 10000,
368 .offset = 1,
369 .offset_relative_to_alignmask = true,
370 },
371 },
372 .finalization_type = FINALIZATION_TYPE_DIGEST,
373 }, {
374 .name = "init+update+update+final two even splits",
375 .src_divs = {
376 { .proportion_of_total = 5000 },
377 {
378 .proportion_of_total = 5000,
379 .flush_type = FLUSH_TYPE_FLUSH,
380 },
381 },
382 .finalization_type = FINALIZATION_TYPE_FINAL,
383 }, {
384 .name = "digest uneven misaligned splits, may sleep",
385 .req_flags = CRYPTO_TFM_REQ_MAY_SLEEP,
386 .src_divs = {
387 { .proportion_of_total = 1900, .offset = 33 },
388 { .proportion_of_total = 3300, .offset = 7 },
389 { .proportion_of_total = 4800, .offset = 18 },
390 },
391 .finalization_type = FINALIZATION_TYPE_DIGEST,
392 }, {
393 .name = "digest misaligned splits crossing pages",
394 .src_divs = {
395 {
396 .proportion_of_total = 7500,
397 .offset = PAGE_SIZE - 32,
398 }, {
399 .proportion_of_total = 2500,
400 .offset = PAGE_SIZE - 7,
401 },
402 },
403 .finalization_type = FINALIZATION_TYPE_DIGEST,
404 }, {
405 .name = "import/export",
406 .src_divs = {
407 {
408 .proportion_of_total = 6500,
409 .flush_type = FLUSH_TYPE_REIMPORT,
410 }, {
411 .proportion_of_total = 3500,
412 .flush_type = FLUSH_TYPE_REIMPORT,
413 },
414 },
415 .finalization_type = FINALIZATION_TYPE_FINAL,
416 }
417};
418
Eric Biggers3f47a032019-01-31 23:51:43 -0800419static unsigned int count_test_sg_divisions(const struct test_sg_division *divs)
420{
421 unsigned int remaining = TEST_SG_TOTAL;
422 unsigned int ndivs = 0;
423
424 do {
425 remaining -= divs[ndivs++].proportion_of_total;
426 } while (remaining);
427
428 return ndivs;
429}
430
Eric Biggers65707372019-03-12 22:12:52 -0700431#define SGDIVS_HAVE_FLUSHES BIT(0)
432#define SGDIVS_HAVE_NOSIMD BIT(1)
433
Eric Biggers3f47a032019-01-31 23:51:43 -0800434static bool valid_sg_divisions(const struct test_sg_division *divs,
Eric Biggers65707372019-03-12 22:12:52 -0700435 unsigned int count, int *flags_ret)
Eric Biggers3f47a032019-01-31 23:51:43 -0800436{
437 unsigned int total = 0;
438 unsigned int i;
439
440 for (i = 0; i < count && total != TEST_SG_TOTAL; i++) {
441 if (divs[i].proportion_of_total <= 0 ||
442 divs[i].proportion_of_total > TEST_SG_TOTAL - total)
443 return false;
444 total += divs[i].proportion_of_total;
445 if (divs[i].flush_type != FLUSH_TYPE_NONE)
Eric Biggers65707372019-03-12 22:12:52 -0700446 *flags_ret |= SGDIVS_HAVE_FLUSHES;
447 if (divs[i].nosimd)
448 *flags_ret |= SGDIVS_HAVE_NOSIMD;
Eric Biggers3f47a032019-01-31 23:51:43 -0800449 }
450 return total == TEST_SG_TOTAL &&
451 memchr_inv(&divs[i], 0, (count - i) * sizeof(divs[0])) == NULL;
452}
453
454/*
455 * Check whether the given testvec_config is valid. This isn't strictly needed
456 * since every testvec_config should be valid, but check anyway so that people
457 * don't unknowingly add broken configs that don't do what they wanted.
458 */
459static bool valid_testvec_config(const struct testvec_config *cfg)
460{
Eric Biggers65707372019-03-12 22:12:52 -0700461 int flags = 0;
Eric Biggers3f47a032019-01-31 23:51:43 -0800462
463 if (cfg->name == NULL)
464 return false;
465
466 if (!valid_sg_divisions(cfg->src_divs, ARRAY_SIZE(cfg->src_divs),
Eric Biggers65707372019-03-12 22:12:52 -0700467 &flags))
Eric Biggers3f47a032019-01-31 23:51:43 -0800468 return false;
469
470 if (cfg->dst_divs[0].proportion_of_total) {
471 if (!valid_sg_divisions(cfg->dst_divs,
Eric Biggers65707372019-03-12 22:12:52 -0700472 ARRAY_SIZE(cfg->dst_divs), &flags))
Eric Biggers3f47a032019-01-31 23:51:43 -0800473 return false;
474 } else {
475 if (memchr_inv(cfg->dst_divs, 0, sizeof(cfg->dst_divs)))
476 return false;
477 /* defaults to dst_divs=src_divs */
478 }
479
480 if (cfg->iv_offset +
481 (cfg->iv_offset_relative_to_alignmask ? MAX_ALGAPI_ALIGNMASK : 0) >
482 MAX_ALGAPI_ALIGNMASK + 1)
483 return false;
484
Eric Biggers65707372019-03-12 22:12:52 -0700485 if ((flags & (SGDIVS_HAVE_FLUSHES | SGDIVS_HAVE_NOSIMD)) &&
486 cfg->finalization_type == FINALIZATION_TYPE_DIGEST)
487 return false;
488
489 if ((cfg->nosimd || (flags & SGDIVS_HAVE_NOSIMD)) &&
490 (cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP))
Eric Biggers3f47a032019-01-31 23:51:43 -0800491 return false;
492
493 return true;
494}
495
496struct test_sglist {
497 char *bufs[XBUFSIZE];
498 struct scatterlist sgl[XBUFSIZE];
499 struct scatterlist sgl_saved[XBUFSIZE];
500 struct scatterlist *sgl_ptr;
501 unsigned int nents;
502};
503
504static int init_test_sglist(struct test_sglist *tsgl)
505{
506 return __testmgr_alloc_buf(tsgl->bufs, 1 /* two pages per buffer */);
507}
508
509static void destroy_test_sglist(struct test_sglist *tsgl)
510{
511 return __testmgr_free_buf(tsgl->bufs, 1 /* two pages per buffer */);
512}
513
514/**
515 * build_test_sglist() - build a scatterlist for a crypto test
516 *
517 * @tsgl: the scatterlist to build. @tsgl->bufs[] contains an array of 2-page
518 * buffers which the scatterlist @tsgl->sgl[] will be made to point into.
519 * @divs: the layout specification on which the scatterlist will be based
520 * @alignmask: the algorithm's alignmask
521 * @total_len: the total length of the scatterlist to build in bytes
522 * @data: if non-NULL, the buffers will be filled with this data until it ends.
523 * Otherwise the buffers will be poisoned. In both cases, some bytes
524 * past the end of each buffer will be poisoned to help detect overruns.
525 * @out_divs: if non-NULL, the test_sg_division to which each scatterlist entry
526 * corresponds will be returned here. This will match @divs except
527 * that divisions resolving to a length of 0 are omitted as they are
528 * not included in the scatterlist.
529 *
530 * Return: 0 or a -errno value
531 */
532static int build_test_sglist(struct test_sglist *tsgl,
533 const struct test_sg_division *divs,
534 const unsigned int alignmask,
535 const unsigned int total_len,
536 struct iov_iter *data,
537 const struct test_sg_division *out_divs[XBUFSIZE])
538{
539 struct {
540 const struct test_sg_division *div;
541 size_t length;
542 } partitions[XBUFSIZE];
543 const unsigned int ndivs = count_test_sg_divisions(divs);
544 unsigned int len_remaining = total_len;
545 unsigned int i;
546
547 BUILD_BUG_ON(ARRAY_SIZE(partitions) != ARRAY_SIZE(tsgl->sgl));
548 if (WARN_ON(ndivs > ARRAY_SIZE(partitions)))
549 return -EINVAL;
550
551 /* Calculate the (div, length) pairs */
552 tsgl->nents = 0;
553 for (i = 0; i < ndivs; i++) {
554 unsigned int len_this_sg =
555 min(len_remaining,
556 (total_len * divs[i].proportion_of_total +
557 TEST_SG_TOTAL / 2) / TEST_SG_TOTAL);
558
559 if (len_this_sg != 0) {
560 partitions[tsgl->nents].div = &divs[i];
561 partitions[tsgl->nents].length = len_this_sg;
562 tsgl->nents++;
563 len_remaining -= len_this_sg;
564 }
565 }
566 if (tsgl->nents == 0) {
567 partitions[tsgl->nents].div = &divs[0];
568 partitions[tsgl->nents].length = 0;
569 tsgl->nents++;
570 }
571 partitions[tsgl->nents - 1].length += len_remaining;
572
573 /* Set up the sgl entries and fill the data or poison */
574 sg_init_table(tsgl->sgl, tsgl->nents);
575 for (i = 0; i < tsgl->nents; i++) {
576 unsigned int offset = partitions[i].div->offset;
577 void *addr;
578
579 if (partitions[i].div->offset_relative_to_alignmask)
580 offset += alignmask;
581
582 while (offset + partitions[i].length + TESTMGR_POISON_LEN >
583 2 * PAGE_SIZE) {
584 if (WARN_ON(offset <= 0))
585 return -EINVAL;
586 offset /= 2;
587 }
588
589 addr = &tsgl->bufs[i][offset];
590 sg_set_buf(&tsgl->sgl[i], addr, partitions[i].length);
591
592 if (out_divs)
593 out_divs[i] = partitions[i].div;
594
595 if (data) {
596 size_t copy_len, copied;
597
598 copy_len = min(partitions[i].length, data->count);
599 copied = copy_from_iter(addr, copy_len, data);
600 if (WARN_ON(copied != copy_len))
601 return -EINVAL;
602 testmgr_poison(addr + copy_len, partitions[i].length +
603 TESTMGR_POISON_LEN - copy_len);
604 } else {
605 testmgr_poison(addr, partitions[i].length +
606 TESTMGR_POISON_LEN);
607 }
608 }
609
610 sg_mark_end(&tsgl->sgl[tsgl->nents - 1]);
611 tsgl->sgl_ptr = tsgl->sgl;
612 memcpy(tsgl->sgl_saved, tsgl->sgl, tsgl->nents * sizeof(tsgl->sgl[0]));
613 return 0;
614}
615
616/*
617 * Verify that a scatterlist crypto operation produced the correct output.
618 *
619 * @tsgl: scatterlist containing the actual output
620 * @expected_output: buffer containing the expected output
621 * @len_to_check: length of @expected_output in bytes
622 * @unchecked_prefix_len: number of ignored bytes in @tsgl prior to real result
623 * @check_poison: verify that the poison bytes after each chunk are intact?
624 *
625 * Return: 0 if correct, -EINVAL if incorrect, -EOVERFLOW if buffer overrun.
626 */
627static int verify_correct_output(const struct test_sglist *tsgl,
628 const char *expected_output,
629 unsigned int len_to_check,
630 unsigned int unchecked_prefix_len,
631 bool check_poison)
632{
633 unsigned int i;
634
635 for (i = 0; i < tsgl->nents; i++) {
636 struct scatterlist *sg = &tsgl->sgl_ptr[i];
637 unsigned int len = sg->length;
638 unsigned int offset = sg->offset;
639 const char *actual_output;
640
641 if (unchecked_prefix_len) {
642 if (unchecked_prefix_len >= len) {
643 unchecked_prefix_len -= len;
644 continue;
645 }
646 offset += unchecked_prefix_len;
647 len -= unchecked_prefix_len;
648 unchecked_prefix_len = 0;
649 }
650 len = min(len, len_to_check);
651 actual_output = page_address(sg_page(sg)) + offset;
652 if (memcmp(expected_output, actual_output, len) != 0)
653 return -EINVAL;
654 if (check_poison &&
655 !testmgr_is_poison(actual_output + len, TESTMGR_POISON_LEN))
656 return -EOVERFLOW;
657 len_to_check -= len;
658 expected_output += len;
659 }
660 if (WARN_ON(len_to_check != 0))
661 return -EINVAL;
662 return 0;
663}
664
665static bool is_test_sglist_corrupted(const struct test_sglist *tsgl)
666{
667 unsigned int i;
668
669 for (i = 0; i < tsgl->nents; i++) {
670 if (tsgl->sgl[i].page_link != tsgl->sgl_saved[i].page_link)
671 return true;
672 if (tsgl->sgl[i].offset != tsgl->sgl_saved[i].offset)
673 return true;
674 if (tsgl->sgl[i].length != tsgl->sgl_saved[i].length)
675 return true;
676 }
677 return false;
678}
679
680struct cipher_test_sglists {
681 struct test_sglist src;
682 struct test_sglist dst;
683};
684
685static struct cipher_test_sglists *alloc_cipher_test_sglists(void)
686{
687 struct cipher_test_sglists *tsgls;
688
689 tsgls = kmalloc(sizeof(*tsgls), GFP_KERNEL);
690 if (!tsgls)
691 return NULL;
692
693 if (init_test_sglist(&tsgls->src) != 0)
694 goto fail_kfree;
695 if (init_test_sglist(&tsgls->dst) != 0)
696 goto fail_destroy_src;
697
698 return tsgls;
699
700fail_destroy_src:
701 destroy_test_sglist(&tsgls->src);
702fail_kfree:
703 kfree(tsgls);
704 return NULL;
705}
706
707static void free_cipher_test_sglists(struct cipher_test_sglists *tsgls)
708{
709 if (tsgls) {
710 destroy_test_sglist(&tsgls->src);
711 destroy_test_sglist(&tsgls->dst);
712 kfree(tsgls);
713 }
714}
715
716/* Build the src and dst scatterlists for an skcipher or AEAD test */
717static int build_cipher_test_sglists(struct cipher_test_sglists *tsgls,
718 const struct testvec_config *cfg,
719 unsigned int alignmask,
720 unsigned int src_total_len,
721 unsigned int dst_total_len,
722 const struct kvec *inputs,
723 unsigned int nr_inputs)
724{
725 struct iov_iter input;
726 int err;
727
728 iov_iter_kvec(&input, WRITE, inputs, nr_inputs, src_total_len);
729 err = build_test_sglist(&tsgls->src, cfg->src_divs, alignmask,
730 cfg->inplace ?
731 max(dst_total_len, src_total_len) :
732 src_total_len,
733 &input, NULL);
734 if (err)
735 return err;
736
737 if (cfg->inplace) {
738 tsgls->dst.sgl_ptr = tsgls->src.sgl;
739 tsgls->dst.nents = tsgls->src.nents;
740 return 0;
741 }
742 return build_test_sglist(&tsgls->dst,
743 cfg->dst_divs[0].proportion_of_total ?
744 cfg->dst_divs : cfg->src_divs,
745 alignmask, dst_total_len, NULL, NULL);
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800746}
747
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800748#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
Eric Biggersf2bb770a2019-04-11 21:57:38 -0700749
750/* Generate a random length in range [0, max_len], but prefer smaller values */
751static unsigned int generate_random_length(unsigned int max_len)
752{
753 unsigned int len = prandom_u32() % (max_len + 1);
754
755 switch (prandom_u32() % 4) {
756 case 0:
757 return len % 64;
758 case 1:
759 return len % 256;
760 case 2:
761 return len % 1024;
762 default:
763 return len;
764 }
765}
766
767/* Sometimes make some random changes to the given data buffer */
768static void mutate_buffer(u8 *buf, size_t count)
769{
770 size_t num_flips;
771 size_t i;
772 size_t pos;
773
774 /* Sometimes flip some bits */
775 if (prandom_u32() % 4 == 0) {
776 num_flips = min_t(size_t, 1 << (prandom_u32() % 8), count * 8);
777 for (i = 0; i < num_flips; i++) {
778 pos = prandom_u32() % (count * 8);
779 buf[pos / 8] ^= 1 << (pos % 8);
780 }
781 }
782
783 /* Sometimes flip some bytes */
784 if (prandom_u32() % 4 == 0) {
785 num_flips = min_t(size_t, 1 << (prandom_u32() % 8), count);
786 for (i = 0; i < num_flips; i++)
787 buf[prandom_u32() % count] ^= 0xff;
788 }
789}
790
791/* Randomly generate 'count' bytes, but sometimes make them "interesting" */
792static void generate_random_bytes(u8 *buf, size_t count)
793{
794 u8 b;
795 u8 increment;
796 size_t i;
797
798 if (count == 0)
799 return;
800
801 switch (prandom_u32() % 8) { /* Choose a generation strategy */
802 case 0:
803 case 1:
804 /* All the same byte, plus optional mutations */
805 switch (prandom_u32() % 4) {
806 case 0:
807 b = 0x00;
808 break;
809 case 1:
810 b = 0xff;
811 break;
812 default:
813 b = (u8)prandom_u32();
814 break;
815 }
816 memset(buf, b, count);
817 mutate_buffer(buf, count);
818 break;
819 case 2:
820 /* Ascending or descending bytes, plus optional mutations */
821 increment = (u8)prandom_u32();
822 b = (u8)prandom_u32();
823 for (i = 0; i < count; i++, b += increment)
824 buf[i] = b;
825 mutate_buffer(buf, count);
826 break;
827 default:
828 /* Fully random bytes */
829 for (i = 0; i < count; i++)
830 buf[i] = (u8)prandom_u32();
831 }
832}
833
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800834static char *generate_random_sgl_divisions(struct test_sg_division *divs,
835 size_t max_divs, char *p, char *end,
Eric Biggers65707372019-03-12 22:12:52 -0700836 bool gen_flushes, u32 req_flags)
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800837{
838 struct test_sg_division *div = divs;
839 unsigned int remaining = TEST_SG_TOTAL;
840
841 do {
842 unsigned int this_len;
Eric Biggers65707372019-03-12 22:12:52 -0700843 const char *flushtype_str;
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800844
845 if (div == &divs[max_divs - 1] || prandom_u32() % 2 == 0)
846 this_len = remaining;
847 else
848 this_len = 1 + (prandom_u32() % remaining);
849 div->proportion_of_total = this_len;
850
851 if (prandom_u32() % 4 == 0)
852 div->offset = (PAGE_SIZE - 128) + (prandom_u32() % 128);
853 else if (prandom_u32() % 2 == 0)
854 div->offset = prandom_u32() % 32;
855 else
856 div->offset = prandom_u32() % PAGE_SIZE;
857 if (prandom_u32() % 8 == 0)
858 div->offset_relative_to_alignmask = true;
859
860 div->flush_type = FLUSH_TYPE_NONE;
861 if (gen_flushes) {
862 switch (prandom_u32() % 4) {
863 case 0:
864 div->flush_type = FLUSH_TYPE_REIMPORT;
865 break;
866 case 1:
867 div->flush_type = FLUSH_TYPE_FLUSH;
868 break;
869 }
870 }
871
Eric Biggers65707372019-03-12 22:12:52 -0700872 if (div->flush_type != FLUSH_TYPE_NONE &&
873 !(req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) &&
874 prandom_u32() % 2 == 0)
875 div->nosimd = true;
876
877 switch (div->flush_type) {
878 case FLUSH_TYPE_FLUSH:
879 if (div->nosimd)
880 flushtype_str = "<flush,nosimd>";
881 else
882 flushtype_str = "<flush>";
883 break;
884 case FLUSH_TYPE_REIMPORT:
885 if (div->nosimd)
886 flushtype_str = "<reimport,nosimd>";
887 else
888 flushtype_str = "<reimport>";
889 break;
890 default:
891 flushtype_str = "";
892 break;
893 }
894
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800895 BUILD_BUG_ON(TEST_SG_TOTAL != 10000); /* for "%u.%u%%" */
Eric Biggers65707372019-03-12 22:12:52 -0700896 p += scnprintf(p, end - p, "%s%u.%u%%@%s+%u%s", flushtype_str,
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800897 this_len / 100, this_len % 100,
898 div->offset_relative_to_alignmask ?
899 "alignmask" : "",
900 div->offset, this_len == remaining ? "" : ", ");
901 remaining -= this_len;
902 div++;
903 } while (remaining);
904
905 return p;
906}
907
908/* Generate a random testvec_config for fuzz testing */
909static void generate_random_testvec_config(struct testvec_config *cfg,
910 char *name, size_t max_namelen)
911{
912 char *p = name;
913 char * const end = name + max_namelen;
914
915 memset(cfg, 0, sizeof(*cfg));
916
917 cfg->name = name;
918
919 p += scnprintf(p, end - p, "random:");
920
921 if (prandom_u32() % 2 == 0) {
922 cfg->inplace = true;
923 p += scnprintf(p, end - p, " inplace");
924 }
925
926 if (prandom_u32() % 2 == 0) {
927 cfg->req_flags |= CRYPTO_TFM_REQ_MAY_SLEEP;
928 p += scnprintf(p, end - p, " may_sleep");
929 }
930
931 switch (prandom_u32() % 4) {
932 case 0:
933 cfg->finalization_type = FINALIZATION_TYPE_FINAL;
934 p += scnprintf(p, end - p, " use_final");
935 break;
936 case 1:
937 cfg->finalization_type = FINALIZATION_TYPE_FINUP;
938 p += scnprintf(p, end - p, " use_finup");
939 break;
940 default:
941 cfg->finalization_type = FINALIZATION_TYPE_DIGEST;
942 p += scnprintf(p, end - p, " use_digest");
943 break;
944 }
945
Eric Biggers65707372019-03-12 22:12:52 -0700946 if (!(cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) &&
947 prandom_u32() % 2 == 0) {
948 cfg->nosimd = true;
949 p += scnprintf(p, end - p, " nosimd");
950 }
951
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800952 p += scnprintf(p, end - p, " src_divs=[");
953 p = generate_random_sgl_divisions(cfg->src_divs,
954 ARRAY_SIZE(cfg->src_divs), p, end,
955 (cfg->finalization_type !=
Eric Biggers65707372019-03-12 22:12:52 -0700956 FINALIZATION_TYPE_DIGEST),
957 cfg->req_flags);
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800958 p += scnprintf(p, end - p, "]");
959
960 if (!cfg->inplace && prandom_u32() % 2 == 0) {
961 p += scnprintf(p, end - p, " dst_divs=[");
962 p = generate_random_sgl_divisions(cfg->dst_divs,
963 ARRAY_SIZE(cfg->dst_divs),
Eric Biggers65707372019-03-12 22:12:52 -0700964 p, end, false,
965 cfg->req_flags);
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800966 p += scnprintf(p, end - p, "]");
967 }
968
969 if (prandom_u32() % 2 == 0) {
970 cfg->iv_offset = 1 + (prandom_u32() % MAX_ALGAPI_ALIGNMASK);
971 p += scnprintf(p, end - p, " iv_offset=%u", cfg->iv_offset);
972 }
973
974 WARN_ON_ONCE(!valid_testvec_config(cfg));
975}
Eric Biggersb55e1a32019-03-12 22:12:47 -0700976
977static void crypto_disable_simd_for_test(void)
978{
979 preempt_disable();
980 __this_cpu_write(crypto_simd_disabled_for_test, true);
981}
982
983static void crypto_reenable_simd_for_test(void)
984{
985 __this_cpu_write(crypto_simd_disabled_for_test, false);
986 preempt_enable();
987}
Eric Biggersf2bb770a2019-04-11 21:57:38 -0700988
989/*
990 * Given an algorithm name, build the name of the generic implementation of that
991 * algorithm, assuming the usual naming convention. Specifically, this appends
992 * "-generic" to every part of the name that is not a template name. Examples:
993 *
994 * aes => aes-generic
995 * cbc(aes) => cbc(aes-generic)
996 * cts(cbc(aes)) => cts(cbc(aes-generic))
997 * rfc7539(chacha20,poly1305) => rfc7539(chacha20-generic,poly1305-generic)
998 *
999 * Return: 0 on success, or -ENAMETOOLONG if the generic name would be too long
1000 */
1001static int build_generic_driver_name(const char *algname,
1002 char driver_name[CRYPTO_MAX_ALG_NAME])
1003{
1004 const char *in = algname;
1005 char *out = driver_name;
1006 size_t len = strlen(algname);
1007
1008 if (len >= CRYPTO_MAX_ALG_NAME)
1009 goto too_long;
1010 do {
1011 const char *in_saved = in;
1012
1013 while (*in && *in != '(' && *in != ')' && *in != ',')
1014 *out++ = *in++;
1015 if (*in != '(' && in > in_saved) {
1016 len += 8;
1017 if (len >= CRYPTO_MAX_ALG_NAME)
1018 goto too_long;
1019 memcpy(out, "-generic", 8);
1020 out += 8;
1021 }
1022 } while ((*out++ = *in++) != '\0');
1023 return 0;
1024
1025too_long:
1026 pr_err("alg: generic driver name for \"%s\" would be too long\n",
1027 algname);
1028 return -ENAMETOOLONG;
1029}
Eric Biggersb55e1a32019-03-12 22:12:47 -07001030#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1031static void crypto_disable_simd_for_test(void)
1032{
1033}
1034
1035static void crypto_reenable_simd_for_test(void)
1036{
1037}
1038#endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
Eric Biggers25f9ddd2019-01-31 23:51:45 -08001039
Eric Biggers65707372019-03-12 22:12:52 -07001040static int do_ahash_op(int (*op)(struct ahash_request *req),
1041 struct ahash_request *req,
1042 struct crypto_wait *wait, bool nosimd)
1043{
1044 int err;
1045
1046 if (nosimd)
1047 crypto_disable_simd_for_test();
1048
1049 err = op(req);
1050
1051 if (nosimd)
1052 crypto_reenable_simd_for_test();
1053
1054 return crypto_wait_req(err, wait);
1055}
1056
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001057static int check_nonfinal_hash_op(const char *op, int err,
1058 u8 *result, unsigned int digestsize,
Eric Biggers951d1332019-04-11 21:57:37 -07001059 const char *driver, const char *vec_name,
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001060 const struct testvec_config *cfg)
Kamil Konieczny466d7b92018-01-16 15:26:13 +01001061{
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001062 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07001063 pr_err("alg: hash: %s %s() failed with err %d on test vector %s, cfg=\"%s\"\n",
1064 driver, op, err, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001065 return err;
1066 }
1067 if (!testmgr_is_poison(result, digestsize)) {
Eric Biggers951d1332019-04-11 21:57:37 -07001068 pr_err("alg: hash: %s %s() used result buffer on test vector %s, cfg=\"%s\"\n",
1069 driver, op, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001070 return -EINVAL;
1071 }
1072 return 0;
1073}
Kamil Konieczny466d7b92018-01-16 15:26:13 +01001074
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001075static int test_hash_vec_cfg(const char *driver,
1076 const struct hash_testvec *vec,
Eric Biggers951d1332019-04-11 21:57:37 -07001077 const char *vec_name,
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001078 const struct testvec_config *cfg,
1079 struct ahash_request *req,
1080 struct test_sglist *tsgl,
1081 u8 *hashstate)
1082{
1083 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1084 const unsigned int alignmask = crypto_ahash_alignmask(tfm);
1085 const unsigned int digestsize = crypto_ahash_digestsize(tfm);
1086 const unsigned int statesize = crypto_ahash_statesize(tfm);
1087 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1088 const struct test_sg_division *divs[XBUFSIZE];
1089 DECLARE_CRYPTO_WAIT(wait);
1090 struct kvec _input;
1091 struct iov_iter input;
1092 unsigned int i;
1093 struct scatterlist *pending_sgl;
1094 unsigned int pending_len;
1095 u8 result[HASH_MAX_DIGESTSIZE + TESTMGR_POISON_LEN];
1096 int err;
1097
1098 /* Set the key, if specified */
1099 if (vec->ksize) {
1100 err = crypto_ahash_setkey(tfm, vec->key, vec->ksize);
1101 if (err) {
Eric Biggers5283a8e2019-04-11 21:57:36 -07001102 if (err == vec->setkey_error)
1103 return 0;
Eric Biggers951d1332019-04-11 21:57:37 -07001104 pr_err("alg: hash: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1105 driver, vec_name, vec->setkey_error, err,
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001106 crypto_ahash_get_flags(tfm));
1107 return err;
1108 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07001109 if (vec->setkey_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001110 pr_err("alg: hash: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1111 driver, vec_name, vec->setkey_error);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001112 return -EINVAL;
1113 }
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001114 }
1115
1116 /* Build the scatterlist for the source data */
1117 _input.iov_base = (void *)vec->plaintext;
1118 _input.iov_len = vec->psize;
1119 iov_iter_kvec(&input, WRITE, &_input, 1, vec->psize);
1120 err = build_test_sglist(tsgl, cfg->src_divs, alignmask, vec->psize,
1121 &input, divs);
1122 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07001123 pr_err("alg: hash: %s: error preparing scatterlist for test vector %s, cfg=\"%s\"\n",
1124 driver, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001125 return err;
1126 }
1127
1128 /* Do the actual hashing */
1129
1130 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
1131 testmgr_poison(result, digestsize + TESTMGR_POISON_LEN);
1132
Eric Biggers5283a8e2019-04-11 21:57:36 -07001133 if (cfg->finalization_type == FINALIZATION_TYPE_DIGEST ||
1134 vec->digest_error) {
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001135 /* Just using digest() */
1136 ahash_request_set_callback(req, req_flags, crypto_req_done,
1137 &wait);
1138 ahash_request_set_crypt(req, tsgl->sgl, result, vec->psize);
Eric Biggers65707372019-03-12 22:12:52 -07001139 err = do_ahash_op(crypto_ahash_digest, req, &wait, cfg->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001140 if (err) {
Eric Biggers5283a8e2019-04-11 21:57:36 -07001141 if (err == vec->digest_error)
1142 return 0;
Eric Biggers951d1332019-04-11 21:57:37 -07001143 pr_err("alg: hash: %s digest() failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
1144 driver, vec_name, vec->digest_error, err,
Eric Biggers5283a8e2019-04-11 21:57:36 -07001145 cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001146 return err;
1147 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07001148 if (vec->digest_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001149 pr_err("alg: hash: %s digest() unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
1150 driver, vec_name, vec->digest_error, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001151 return -EINVAL;
1152 }
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001153 goto result_ready;
1154 }
1155
1156 /* Using init(), zero or more update(), then final() or finup() */
1157
1158 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
1159 ahash_request_set_crypt(req, NULL, result, 0);
Eric Biggers65707372019-03-12 22:12:52 -07001160 err = do_ahash_op(crypto_ahash_init, req, &wait, cfg->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001161 err = check_nonfinal_hash_op("init", err, result, digestsize,
Eric Biggers951d1332019-04-11 21:57:37 -07001162 driver, vec_name, cfg);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001163 if (err)
1164 return err;
1165
1166 pending_sgl = NULL;
1167 pending_len = 0;
1168 for (i = 0; i < tsgl->nents; i++) {
1169 if (divs[i]->flush_type != FLUSH_TYPE_NONE &&
1170 pending_sgl != NULL) {
1171 /* update() with the pending data */
1172 ahash_request_set_callback(req, req_flags,
1173 crypto_req_done, &wait);
1174 ahash_request_set_crypt(req, pending_sgl, result,
1175 pending_len);
Eric Biggers65707372019-03-12 22:12:52 -07001176 err = do_ahash_op(crypto_ahash_update, req, &wait,
1177 divs[i]->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001178 err = check_nonfinal_hash_op("update", err,
1179 result, digestsize,
Eric Biggers951d1332019-04-11 21:57:37 -07001180 driver, vec_name, cfg);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001181 if (err)
1182 return err;
1183 pending_sgl = NULL;
1184 pending_len = 0;
1185 }
1186 if (divs[i]->flush_type == FLUSH_TYPE_REIMPORT) {
1187 /* Test ->export() and ->import() */
1188 testmgr_poison(hashstate + statesize,
1189 TESTMGR_POISON_LEN);
1190 err = crypto_ahash_export(req, hashstate);
1191 err = check_nonfinal_hash_op("export", err,
1192 result, digestsize,
Eric Biggers951d1332019-04-11 21:57:37 -07001193 driver, vec_name, cfg);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001194 if (err)
1195 return err;
1196 if (!testmgr_is_poison(hashstate + statesize,
1197 TESTMGR_POISON_LEN)) {
Eric Biggers951d1332019-04-11 21:57:37 -07001198 pr_err("alg: hash: %s export() overran state buffer on test vector %s, cfg=\"%s\"\n",
1199 driver, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001200 return -EOVERFLOW;
1201 }
1202
1203 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
1204 err = crypto_ahash_import(req, hashstate);
1205 err = check_nonfinal_hash_op("import", err,
1206 result, digestsize,
Eric Biggers951d1332019-04-11 21:57:37 -07001207 driver, vec_name, cfg);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001208 if (err)
1209 return err;
1210 }
1211 if (pending_sgl == NULL)
1212 pending_sgl = &tsgl->sgl[i];
1213 pending_len += tsgl->sgl[i].length;
1214 }
1215
1216 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
1217 ahash_request_set_crypt(req, pending_sgl, result, pending_len);
1218 if (cfg->finalization_type == FINALIZATION_TYPE_FINAL) {
1219 /* finish with update() and final() */
Eric Biggers65707372019-03-12 22:12:52 -07001220 err = do_ahash_op(crypto_ahash_update, req, &wait, cfg->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001221 err = check_nonfinal_hash_op("update", err, result, digestsize,
Eric Biggers951d1332019-04-11 21:57:37 -07001222 driver, vec_name, cfg);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001223 if (err)
1224 return err;
Eric Biggers65707372019-03-12 22:12:52 -07001225 err = do_ahash_op(crypto_ahash_final, req, &wait, cfg->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001226 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07001227 pr_err("alg: hash: %s final() failed with err %d on test vector %s, cfg=\"%s\"\n",
1228 driver, err, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001229 return err;
1230 }
1231 } else {
1232 /* finish with finup() */
Eric Biggers65707372019-03-12 22:12:52 -07001233 err = do_ahash_op(crypto_ahash_finup, req, &wait, cfg->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001234 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07001235 pr_err("alg: hash: %s finup() failed with err %d on test vector %s, cfg=\"%s\"\n",
1236 driver, err, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001237 return err;
1238 }
1239 }
1240
1241result_ready:
1242 /* Check that the algorithm produced the correct digest */
1243 if (memcmp(result, vec->digest, digestsize) != 0) {
Eric Biggers951d1332019-04-11 21:57:37 -07001244 pr_err("alg: hash: %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
1245 driver, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001246 return -EINVAL;
1247 }
1248 if (!testmgr_is_poison(&result[digestsize], TESTMGR_POISON_LEN)) {
Eric Biggers951d1332019-04-11 21:57:37 -07001249 pr_err("alg: hash: %s overran result buffer on test vector %s, cfg=\"%s\"\n",
1250 driver, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001251 return -EOVERFLOW;
1252 }
1253
1254 return 0;
1255}
1256
1257static int test_hash_vec(const char *driver, const struct hash_testvec *vec,
1258 unsigned int vec_num, struct ahash_request *req,
1259 struct test_sglist *tsgl, u8 *hashstate)
1260{
Eric Biggers951d1332019-04-11 21:57:37 -07001261 char vec_name[16];
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001262 unsigned int i;
1263 int err;
1264
Eric Biggers951d1332019-04-11 21:57:37 -07001265 sprintf(vec_name, "%u", vec_num);
1266
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001267 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++) {
Eric Biggers951d1332019-04-11 21:57:37 -07001268 err = test_hash_vec_cfg(driver, vec, vec_name,
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001269 &default_hash_testvec_configs[i],
1270 req, tsgl, hashstate);
1271 if (err)
1272 return err;
1273 }
1274
1275#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1276 if (!noextratests) {
1277 struct testvec_config cfg;
1278 char cfgname[TESTVEC_CONFIG_NAMELEN];
1279
1280 for (i = 0; i < fuzz_iterations; i++) {
1281 generate_random_testvec_config(&cfg, cfgname,
1282 sizeof(cfgname));
Eric Biggers951d1332019-04-11 21:57:37 -07001283 err = test_hash_vec_cfg(driver, vec, vec_name, &cfg,
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001284 req, tsgl, hashstate);
1285 if (err)
1286 return err;
1287 }
1288 }
1289#endif
1290 return 0;
1291}
1292
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001293#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1294/*
1295 * Generate a hash test vector from the given implementation.
1296 * Assumes the buffers in 'vec' were already allocated.
1297 */
1298static void generate_random_hash_testvec(struct crypto_shash *tfm,
1299 struct hash_testvec *vec,
1300 unsigned int maxkeysize,
1301 unsigned int maxdatasize,
1302 char *name, size_t max_namelen)
1303{
1304 SHASH_DESC_ON_STACK(desc, tfm);
1305
1306 /* Data */
1307 vec->psize = generate_random_length(maxdatasize);
1308 generate_random_bytes((u8 *)vec->plaintext, vec->psize);
1309
1310 /*
1311 * Key: length in range [1, maxkeysize], but usually choose maxkeysize.
1312 * If algorithm is unkeyed, then maxkeysize == 0 and set ksize = 0.
1313 */
1314 vec->setkey_error = 0;
1315 vec->ksize = 0;
1316 if (maxkeysize) {
1317 vec->ksize = maxkeysize;
1318 if (prandom_u32() % 4 == 0)
1319 vec->ksize = 1 + (prandom_u32() % maxkeysize);
1320 generate_random_bytes((u8 *)vec->key, vec->ksize);
1321
1322 vec->setkey_error = crypto_shash_setkey(tfm, vec->key,
1323 vec->ksize);
1324 /* If the key couldn't be set, no need to continue to digest. */
1325 if (vec->setkey_error)
1326 goto done;
1327 }
1328
1329 /* Digest */
1330 desc->tfm = tfm;
1331 desc->flags = 0;
1332 vec->digest_error = crypto_shash_digest(desc, vec->plaintext,
1333 vec->psize, (u8 *)vec->digest);
1334done:
1335 snprintf(name, max_namelen, "\"random: psize=%u ksize=%u\"",
1336 vec->psize, vec->ksize);
1337}
1338
1339/*
1340 * Test the hash algorithm represented by @req against the corresponding generic
1341 * implementation, if one is available.
1342 */
1343static int test_hash_vs_generic_impl(const char *driver,
1344 const char *generic_driver,
1345 unsigned int maxkeysize,
1346 struct ahash_request *req,
1347 struct test_sglist *tsgl,
1348 u8 *hashstate)
1349{
1350 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1351 const unsigned int digestsize = crypto_ahash_digestsize(tfm);
1352 const unsigned int blocksize = crypto_ahash_blocksize(tfm);
1353 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
1354 const char *algname = crypto_hash_alg_common(tfm)->base.cra_name;
1355 char _generic_driver[CRYPTO_MAX_ALG_NAME];
1356 struct crypto_shash *generic_tfm = NULL;
1357 unsigned int i;
1358 struct hash_testvec vec = { 0 };
1359 char vec_name[64];
1360 struct testvec_config cfg;
1361 char cfgname[TESTVEC_CONFIG_NAMELEN];
1362 int err;
1363
1364 if (noextratests)
1365 return 0;
1366
1367 if (!generic_driver) { /* Use default naming convention? */
1368 err = build_generic_driver_name(algname, _generic_driver);
1369 if (err)
1370 return err;
1371 generic_driver = _generic_driver;
1372 }
1373
1374 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
1375 return 0;
1376
1377 generic_tfm = crypto_alloc_shash(generic_driver, 0, 0);
1378 if (IS_ERR(generic_tfm)) {
1379 err = PTR_ERR(generic_tfm);
1380 if (err == -ENOENT) {
1381 pr_warn("alg: hash: skipping comparison tests for %s because %s is unavailable\n",
1382 driver, generic_driver);
1383 return 0;
1384 }
1385 pr_err("alg: hash: error allocating %s (generic impl of %s): %d\n",
1386 generic_driver, algname, err);
1387 return err;
1388 }
1389
1390 /* Check the algorithm properties for consistency. */
1391
1392 if (digestsize != crypto_shash_digestsize(generic_tfm)) {
1393 pr_err("alg: hash: digestsize for %s (%u) doesn't match generic impl (%u)\n",
1394 driver, digestsize,
1395 crypto_shash_digestsize(generic_tfm));
1396 err = -EINVAL;
1397 goto out;
1398 }
1399
1400 if (blocksize != crypto_shash_blocksize(generic_tfm)) {
1401 pr_err("alg: hash: blocksize for %s (%u) doesn't match generic impl (%u)\n",
1402 driver, blocksize, crypto_shash_blocksize(generic_tfm));
1403 err = -EINVAL;
1404 goto out;
1405 }
1406
1407 /*
1408 * Now generate test vectors using the generic implementation, and test
1409 * the other implementation against them.
1410 */
1411
1412 vec.key = kmalloc(maxkeysize, GFP_KERNEL);
1413 vec.plaintext = kmalloc(maxdatasize, GFP_KERNEL);
1414 vec.digest = kmalloc(digestsize, GFP_KERNEL);
1415 if (!vec.key || !vec.plaintext || !vec.digest) {
1416 err = -ENOMEM;
1417 goto out;
1418 }
1419
1420 for (i = 0; i < fuzz_iterations * 8; i++) {
1421 generate_random_hash_testvec(generic_tfm, &vec,
1422 maxkeysize, maxdatasize,
1423 vec_name, sizeof(vec_name));
1424 generate_random_testvec_config(&cfg, cfgname, sizeof(cfgname));
1425
1426 err = test_hash_vec_cfg(driver, &vec, vec_name, &cfg,
1427 req, tsgl, hashstate);
1428 if (err)
1429 goto out;
1430 cond_resched();
1431 }
1432 err = 0;
1433out:
1434 kfree(vec.key);
1435 kfree(vec.plaintext);
1436 kfree(vec.digest);
1437 crypto_free_shash(generic_tfm);
1438 return err;
1439}
1440#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1441static int test_hash_vs_generic_impl(const char *driver,
1442 const char *generic_driver,
1443 unsigned int maxkeysize,
1444 struct ahash_request *req,
1445 struct test_sglist *tsgl,
1446 u8 *hashstate)
1447{
1448 return 0;
1449}
1450#endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1451
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001452static int __alg_test_hash(const struct hash_testvec *vecs,
1453 unsigned int num_vecs, const char *driver,
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001454 u32 type, u32 mask,
1455 const char *generic_driver, unsigned int maxkeysize)
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001456{
1457 struct crypto_ahash *tfm;
1458 struct ahash_request *req = NULL;
1459 struct test_sglist *tsgl = NULL;
1460 u8 *hashstate = NULL;
1461 unsigned int i;
1462 int err;
1463
1464 tfm = crypto_alloc_ahash(driver, type, mask);
1465 if (IS_ERR(tfm)) {
1466 pr_err("alg: hash: failed to allocate transform for %s: %ld\n",
1467 driver, PTR_ERR(tfm));
1468 return PTR_ERR(tfm);
1469 }
1470
1471 req = ahash_request_alloc(tfm, GFP_KERNEL);
1472 if (!req) {
1473 pr_err("alg: hash: failed to allocate request for %s\n",
1474 driver);
1475 err = -ENOMEM;
1476 goto out;
1477 }
1478
1479 tsgl = kmalloc(sizeof(*tsgl), GFP_KERNEL);
1480 if (!tsgl || init_test_sglist(tsgl) != 0) {
1481 pr_err("alg: hash: failed to allocate test buffers for %s\n",
1482 driver);
1483 kfree(tsgl);
1484 tsgl = NULL;
1485 err = -ENOMEM;
1486 goto out;
1487 }
1488
1489 hashstate = kmalloc(crypto_ahash_statesize(tfm) + TESTMGR_POISON_LEN,
1490 GFP_KERNEL);
1491 if (!hashstate) {
1492 pr_err("alg: hash: failed to allocate hash state buffer for %s\n",
1493 driver);
1494 err = -ENOMEM;
1495 goto out;
1496 }
1497
1498 for (i = 0; i < num_vecs; i++) {
1499 err = test_hash_vec(driver, &vecs[i], i, req, tsgl, hashstate);
1500 if (err)
1501 goto out;
1502 }
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001503 err = test_hash_vs_generic_impl(driver, generic_driver, maxkeysize, req,
1504 tsgl, hashstate);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001505out:
1506 kfree(hashstate);
1507 if (tsgl) {
1508 destroy_test_sglist(tsgl);
1509 kfree(tsgl);
1510 }
1511 ahash_request_free(req);
1512 crypto_free_ahash(tfm);
1513 return err;
1514}
1515
1516static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1517 u32 type, u32 mask)
1518{
1519 const struct hash_testvec *template = desc->suite.hash.vecs;
1520 unsigned int tcount = desc->suite.hash.count;
1521 unsigned int nr_unkeyed, nr_keyed;
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001522 unsigned int maxkeysize = 0;
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001523 int err;
1524
1525 /*
1526 * For OPTIONAL_KEY algorithms, we have to do all the unkeyed tests
1527 * first, before setting a key on the tfm. To make this easier, we
1528 * require that the unkeyed test vectors (if any) are listed first.
1529 */
1530
1531 for (nr_unkeyed = 0; nr_unkeyed < tcount; nr_unkeyed++) {
1532 if (template[nr_unkeyed].ksize)
1533 break;
1534 }
1535 for (nr_keyed = 0; nr_unkeyed + nr_keyed < tcount; nr_keyed++) {
1536 if (!template[nr_unkeyed + nr_keyed].ksize) {
1537 pr_err("alg: hash: test vectors for %s out of order, "
1538 "unkeyed ones must come first\n", desc->alg);
Kamil Konieczny466d7b92018-01-16 15:26:13 +01001539 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08001540 }
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001541 maxkeysize = max_t(unsigned int, maxkeysize,
1542 template[nr_unkeyed + nr_keyed].ksize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001543 }
1544
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001545 err = 0;
1546 if (nr_unkeyed) {
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001547 err = __alg_test_hash(template, nr_unkeyed, driver, type, mask,
1548 desc->generic_driver, maxkeysize);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001549 template += nr_unkeyed;
Herbert Xuda7f0332008-07-31 17:08:25 +08001550 }
1551
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001552 if (!err && nr_keyed)
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001553 err = __alg_test_hash(template, nr_keyed, driver, type, mask,
1554 desc->generic_driver, maxkeysize);
Wang, Rui Y018ba952016-02-03 18:26:57 +08001555
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001556 return err;
Jussi Kivilinnada5ffe12013-06-13 17:37:55 +03001557}
1558
Eric Biggersed968042019-01-31 23:51:47 -08001559static int test_aead_vec_cfg(const char *driver, int enc,
1560 const struct aead_testvec *vec,
Eric Biggers951d1332019-04-11 21:57:37 -07001561 const char *vec_name,
Eric Biggersed968042019-01-31 23:51:47 -08001562 const struct testvec_config *cfg,
1563 struct aead_request *req,
1564 struct cipher_test_sglists *tsgls)
Herbert Xuda7f0332008-07-31 17:08:25 +08001565{
Eric Biggersed968042019-01-31 23:51:47 -08001566 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1567 const unsigned int alignmask = crypto_aead_alignmask(tfm);
1568 const unsigned int ivsize = crypto_aead_ivsize(tfm);
1569 const unsigned int authsize = vec->clen - vec->plen;
1570 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1571 const char *op = enc ? "encryption" : "decryption";
1572 DECLARE_CRYPTO_WAIT(wait);
1573 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
1574 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
1575 cfg->iv_offset +
1576 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
1577 struct kvec input[2];
Eric Biggers5283a8e2019-04-11 21:57:36 -07001578 int expected_error;
Eric Biggersed968042019-01-31 23:51:47 -08001579 int err;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001580
Eric Biggersed968042019-01-31 23:51:47 -08001581 /* Set the key */
1582 if (vec->wk)
1583 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001584 else
Eric Biggersed968042019-01-31 23:51:47 -08001585 crypto_aead_clear_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1586 err = crypto_aead_setkey(tfm, vec->key, vec->klen);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001587 if (err && err != vec->setkey_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001588 pr_err("alg: aead: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1589 driver, vec_name, vec->setkey_error, err,
Eric Biggers5283a8e2019-04-11 21:57:36 -07001590 crypto_aead_get_flags(tfm));
Eric Biggersed968042019-01-31 23:51:47 -08001591 return err;
1592 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07001593 if (!err && vec->setkey_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001594 pr_err("alg: aead: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1595 driver, vec_name, vec->setkey_error);
Eric Biggersed968042019-01-31 23:51:47 -08001596 return -EINVAL;
1597 }
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001598
Eric Biggersed968042019-01-31 23:51:47 -08001599 /* Set the authentication tag size */
1600 err = crypto_aead_setauthsize(tfm, authsize);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001601 if (err && err != vec->setauthsize_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001602 pr_err("alg: aead: %s setauthsize failed on test vector %s; expected_error=%d, actual_error=%d\n",
1603 driver, vec_name, vec->setauthsize_error, err);
Eric Biggersed968042019-01-31 23:51:47 -08001604 return err;
1605 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07001606 if (!err && vec->setauthsize_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001607 pr_err("alg: aead: %s setauthsize unexpectedly succeeded on test vector %s; expected_error=%d\n",
1608 driver, vec_name, vec->setauthsize_error);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001609 return -EINVAL;
1610 }
1611
1612 if (vec->setkey_error || vec->setauthsize_error)
1613 return 0;
Eric Biggersed968042019-01-31 23:51:47 -08001614
1615 /* The IV must be copied to a buffer, as the algorithm may modify it */
1616 if (WARN_ON(ivsize > MAX_IVLEN))
1617 return -EINVAL;
1618 if (vec->iv)
1619 memcpy(iv, vec->iv, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001620 else
Eric Biggersed968042019-01-31 23:51:47 -08001621 memset(iv, 0, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001622
Eric Biggersed968042019-01-31 23:51:47 -08001623 /* Build the src/dst scatterlists */
1624 input[0].iov_base = (void *)vec->assoc;
1625 input[0].iov_len = vec->alen;
1626 input[1].iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
1627 input[1].iov_len = enc ? vec->plen : vec->clen;
1628 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
1629 vec->alen + (enc ? vec->plen :
1630 vec->clen),
1631 vec->alen + (enc ? vec->clen :
1632 vec->plen),
1633 input, 2);
1634 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07001635 pr_err("alg: aead: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
1636 driver, op, vec_name, cfg->name);
Eric Biggersed968042019-01-31 23:51:47 -08001637 return err;
Herbert Xuda7f0332008-07-31 17:08:25 +08001638 }
1639
Eric Biggersed968042019-01-31 23:51:47 -08001640 /* Do the actual encryption or decryption */
1641 testmgr_poison(req->__ctx, crypto_aead_reqsize(tfm));
1642 aead_request_set_callback(req, req_flags, crypto_req_done, &wait);
1643 aead_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
1644 enc ? vec->plen : vec->clen, iv);
1645 aead_request_set_ad(req, vec->alen);
Eric Biggers65707372019-03-12 22:12:52 -07001646 if (cfg->nosimd)
1647 crypto_disable_simd_for_test();
1648 err = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
1649 if (cfg->nosimd)
1650 crypto_reenable_simd_for_test();
1651 err = crypto_wait_req(err, &wait);
Herbert Xuda7f0332008-07-31 17:08:25 +08001652
Eric Biggersa6e5ef92019-01-31 23:51:50 -08001653 /* Check that the algorithm didn't overwrite things it shouldn't have */
1654 if (req->cryptlen != (enc ? vec->plen : vec->clen) ||
1655 req->assoclen != vec->alen ||
1656 req->iv != iv ||
1657 req->src != tsgls->src.sgl_ptr ||
1658 req->dst != tsgls->dst.sgl_ptr ||
1659 crypto_aead_reqtfm(req) != tfm ||
1660 req->base.complete != crypto_req_done ||
1661 req->base.flags != req_flags ||
1662 req->base.data != &wait) {
Eric Biggers951d1332019-04-11 21:57:37 -07001663 pr_err("alg: aead: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
1664 driver, op, vec_name, cfg->name);
Eric Biggersa6e5ef92019-01-31 23:51:50 -08001665 if (req->cryptlen != (enc ? vec->plen : vec->clen))
1666 pr_err("alg: aead: changed 'req->cryptlen'\n");
1667 if (req->assoclen != vec->alen)
1668 pr_err("alg: aead: changed 'req->assoclen'\n");
1669 if (req->iv != iv)
1670 pr_err("alg: aead: changed 'req->iv'\n");
1671 if (req->src != tsgls->src.sgl_ptr)
1672 pr_err("alg: aead: changed 'req->src'\n");
1673 if (req->dst != tsgls->dst.sgl_ptr)
1674 pr_err("alg: aead: changed 'req->dst'\n");
1675 if (crypto_aead_reqtfm(req) != tfm)
1676 pr_err("alg: aead: changed 'req->base.tfm'\n");
1677 if (req->base.complete != crypto_req_done)
1678 pr_err("alg: aead: changed 'req->base.complete'\n");
1679 if (req->base.flags != req_flags)
1680 pr_err("alg: aead: changed 'req->base.flags'\n");
1681 if (req->base.data != &wait)
1682 pr_err("alg: aead: changed 'req->base.data'\n");
1683 return -EINVAL;
1684 }
1685 if (is_test_sglist_corrupted(&tsgls->src)) {
Eric Biggers951d1332019-04-11 21:57:37 -07001686 pr_err("alg: aead: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
1687 driver, op, vec_name, cfg->name);
Eric Biggersa6e5ef92019-01-31 23:51:50 -08001688 return -EINVAL;
1689 }
1690 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
1691 is_test_sglist_corrupted(&tsgls->dst)) {
Eric Biggers951d1332019-04-11 21:57:37 -07001692 pr_err("alg: aead: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
1693 driver, op, vec_name, cfg->name);
Eric Biggersa6e5ef92019-01-31 23:51:50 -08001694 return -EINVAL;
1695 }
1696
Eric Biggers5283a8e2019-04-11 21:57:36 -07001697 /* Check for success or failure */
1698 expected_error = vec->novrfy ? -EBADMSG : vec->crypt_error;
1699 if (err) {
1700 if (err == expected_error)
1701 return 0;
Eric Biggers951d1332019-04-11 21:57:37 -07001702 pr_err("alg: aead: %s %s failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
1703 driver, op, vec_name, expected_error, err, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001704 return err;
1705 }
1706 if (expected_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001707 pr_err("alg: aead: %s %s unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
1708 driver, op, vec_name, expected_error, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001709 return -EINVAL;
1710 }
1711
Eric Biggersed968042019-01-31 23:51:47 -08001712 /* Check for the correct output (ciphertext or plaintext) */
1713 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
1714 enc ? vec->clen : vec->plen,
1715 vec->alen, enc || !cfg->inplace);
1716 if (err == -EOVERFLOW) {
Eric Biggers951d1332019-04-11 21:57:37 -07001717 pr_err("alg: aead: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
1718 driver, op, vec_name, cfg->name);
Eric Biggersed968042019-01-31 23:51:47 -08001719 return err;
Herbert Xuda7f0332008-07-31 17:08:25 +08001720 }
Eric Biggersed968042019-01-31 23:51:47 -08001721 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07001722 pr_err("alg: aead: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
1723 driver, op, vec_name, cfg->name);
Eric Biggersed968042019-01-31 23:51:47 -08001724 return err;
Jussi Kivilinna58dcf542013-06-13 17:37:50 +03001725 }
1726
1727 return 0;
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001728}
1729
Eric Biggersed968042019-01-31 23:51:47 -08001730static int test_aead_vec(const char *driver, int enc,
1731 const struct aead_testvec *vec, unsigned int vec_num,
1732 struct aead_request *req,
1733 struct cipher_test_sglists *tsgls)
1734{
Eric Biggers951d1332019-04-11 21:57:37 -07001735 char vec_name[16];
Eric Biggersed968042019-01-31 23:51:47 -08001736 unsigned int i;
1737 int err;
1738
1739 if (enc && vec->novrfy)
1740 return 0;
1741
Eric Biggers951d1332019-04-11 21:57:37 -07001742 sprintf(vec_name, "%u", vec_num);
1743
Eric Biggersed968042019-01-31 23:51:47 -08001744 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
Eric Biggers951d1332019-04-11 21:57:37 -07001745 err = test_aead_vec_cfg(driver, enc, vec, vec_name,
Eric Biggersed968042019-01-31 23:51:47 -08001746 &default_cipher_testvec_configs[i],
1747 req, tsgls);
1748 if (err)
1749 return err;
1750 }
1751
1752#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1753 if (!noextratests) {
1754 struct testvec_config cfg;
1755 char cfgname[TESTVEC_CONFIG_NAMELEN];
1756
1757 for (i = 0; i < fuzz_iterations; i++) {
1758 generate_random_testvec_config(&cfg, cfgname,
1759 sizeof(cfgname));
Eric Biggers951d1332019-04-11 21:57:37 -07001760 err = test_aead_vec_cfg(driver, enc, vec, vec_name,
Eric Biggersed968042019-01-31 23:51:47 -08001761 &cfg, req, tsgls);
1762 if (err)
1763 return err;
1764 }
1765 }
1766#endif
1767 return 0;
1768}
1769
Eric Biggers40153b12019-04-11 21:57:41 -07001770#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1771/*
1772 * Generate an AEAD test vector from the given implementation.
1773 * Assumes the buffers in 'vec' were already allocated.
1774 */
1775static void generate_random_aead_testvec(struct aead_request *req,
1776 struct aead_testvec *vec,
1777 unsigned int maxkeysize,
1778 unsigned int maxdatasize,
1779 char *name, size_t max_namelen)
1780{
1781 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1782 const unsigned int ivsize = crypto_aead_ivsize(tfm);
1783 unsigned int maxauthsize = crypto_aead_alg(tfm)->maxauthsize;
1784 unsigned int authsize;
1785 unsigned int total_len;
1786 int i;
1787 struct scatterlist src[2], dst;
1788 u8 iv[MAX_IVLEN];
1789 DECLARE_CRYPTO_WAIT(wait);
1790
1791 /* Key: length in [0, maxkeysize], but usually choose maxkeysize */
1792 vec->klen = maxkeysize;
1793 if (prandom_u32() % 4 == 0)
1794 vec->klen = prandom_u32() % (maxkeysize + 1);
1795 generate_random_bytes((u8 *)vec->key, vec->klen);
1796 vec->setkey_error = crypto_aead_setkey(tfm, vec->key, vec->klen);
1797
1798 /* IV */
1799 generate_random_bytes((u8 *)vec->iv, ivsize);
1800
1801 /* Tag length: in [0, maxauthsize], but usually choose maxauthsize */
1802 authsize = maxauthsize;
1803 if (prandom_u32() % 4 == 0)
1804 authsize = prandom_u32() % (maxauthsize + 1);
1805 if (WARN_ON(authsize > maxdatasize))
1806 authsize = maxdatasize;
1807 maxdatasize -= authsize;
1808 vec->setauthsize_error = crypto_aead_setauthsize(tfm, authsize);
1809
1810 /* Plaintext and associated data */
1811 total_len = generate_random_length(maxdatasize);
1812 if (prandom_u32() % 4 == 0)
1813 vec->alen = 0;
1814 else
1815 vec->alen = generate_random_length(total_len);
1816 vec->plen = total_len - vec->alen;
1817 generate_random_bytes((u8 *)vec->assoc, vec->alen);
1818 generate_random_bytes((u8 *)vec->ptext, vec->plen);
1819
1820 vec->clen = vec->plen + authsize;
1821
1822 /*
1823 * If the key or authentication tag size couldn't be set, no need to
1824 * continue to encrypt.
1825 */
1826 if (vec->setkey_error || vec->setauthsize_error)
1827 goto done;
1828
1829 /* Ciphertext */
1830 sg_init_table(src, 2);
1831 i = 0;
1832 if (vec->alen)
1833 sg_set_buf(&src[i++], vec->assoc, vec->alen);
1834 if (vec->plen)
1835 sg_set_buf(&src[i++], vec->ptext, vec->plen);
1836 sg_init_one(&dst, vec->ctext, vec->alen + vec->clen);
1837 memcpy(iv, vec->iv, ivsize);
1838 aead_request_set_callback(req, 0, crypto_req_done, &wait);
1839 aead_request_set_crypt(req, src, &dst, vec->plen, iv);
1840 aead_request_set_ad(req, vec->alen);
1841 vec->crypt_error = crypto_wait_req(crypto_aead_encrypt(req), &wait);
1842 if (vec->crypt_error == 0)
1843 memmove((u8 *)vec->ctext, vec->ctext + vec->alen, vec->clen);
1844done:
1845 snprintf(name, max_namelen,
1846 "\"random: alen=%u plen=%u authsize=%u klen=%u\"",
1847 vec->alen, vec->plen, authsize, vec->klen);
1848}
1849
1850/*
1851 * Test the AEAD algorithm represented by @req against the corresponding generic
1852 * implementation, if one is available.
1853 */
1854static int test_aead_vs_generic_impl(const char *driver,
1855 const struct alg_test_desc *test_desc,
1856 struct aead_request *req,
1857 struct cipher_test_sglists *tsgls)
1858{
1859 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1860 const unsigned int ivsize = crypto_aead_ivsize(tfm);
1861 const unsigned int maxauthsize = crypto_aead_alg(tfm)->maxauthsize;
1862 const unsigned int blocksize = crypto_aead_blocksize(tfm);
1863 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
1864 const char *algname = crypto_aead_alg(tfm)->base.cra_name;
1865 const char *generic_driver = test_desc->generic_driver;
1866 char _generic_driver[CRYPTO_MAX_ALG_NAME];
1867 struct crypto_aead *generic_tfm = NULL;
1868 struct aead_request *generic_req = NULL;
1869 unsigned int maxkeysize;
1870 unsigned int i;
1871 struct aead_testvec vec = { 0 };
1872 char vec_name[64];
1873 struct testvec_config cfg;
1874 char cfgname[TESTVEC_CONFIG_NAMELEN];
1875 int err;
1876
1877 if (noextratests)
1878 return 0;
1879
1880 if (!generic_driver) { /* Use default naming convention? */
1881 err = build_generic_driver_name(algname, _generic_driver);
1882 if (err)
1883 return err;
1884 generic_driver = _generic_driver;
1885 }
1886
1887 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
1888 return 0;
1889
1890 generic_tfm = crypto_alloc_aead(generic_driver, 0, 0);
1891 if (IS_ERR(generic_tfm)) {
1892 err = PTR_ERR(generic_tfm);
1893 if (err == -ENOENT) {
1894 pr_warn("alg: aead: skipping comparison tests for %s because %s is unavailable\n",
1895 driver, generic_driver);
1896 return 0;
1897 }
1898 pr_err("alg: aead: error allocating %s (generic impl of %s): %d\n",
1899 generic_driver, algname, err);
1900 return err;
1901 }
1902
1903 generic_req = aead_request_alloc(generic_tfm, GFP_KERNEL);
1904 if (!generic_req) {
1905 err = -ENOMEM;
1906 goto out;
1907 }
1908
1909 /* Check the algorithm properties for consistency. */
1910
1911 if (maxauthsize != crypto_aead_alg(generic_tfm)->maxauthsize) {
1912 pr_err("alg: aead: maxauthsize for %s (%u) doesn't match generic impl (%u)\n",
1913 driver, maxauthsize,
1914 crypto_aead_alg(generic_tfm)->maxauthsize);
1915 err = -EINVAL;
1916 goto out;
1917 }
1918
1919 if (ivsize != crypto_aead_ivsize(generic_tfm)) {
1920 pr_err("alg: aead: ivsize for %s (%u) doesn't match generic impl (%u)\n",
1921 driver, ivsize, crypto_aead_ivsize(generic_tfm));
1922 err = -EINVAL;
1923 goto out;
1924 }
1925
1926 if (blocksize != crypto_aead_blocksize(generic_tfm)) {
1927 pr_err("alg: aead: blocksize for %s (%u) doesn't match generic impl (%u)\n",
1928 driver, blocksize, crypto_aead_blocksize(generic_tfm));
1929 err = -EINVAL;
1930 goto out;
1931 }
1932
1933 /*
1934 * Now generate test vectors using the generic implementation, and test
1935 * the other implementation against them.
1936 */
1937
1938 maxkeysize = 0;
1939 for (i = 0; i < test_desc->suite.aead.count; i++)
1940 maxkeysize = max_t(unsigned int, maxkeysize,
1941 test_desc->suite.aead.vecs[i].klen);
1942
1943 vec.key = kmalloc(maxkeysize, GFP_KERNEL);
1944 vec.iv = kmalloc(ivsize, GFP_KERNEL);
1945 vec.assoc = kmalloc(maxdatasize, GFP_KERNEL);
1946 vec.ptext = kmalloc(maxdatasize, GFP_KERNEL);
1947 vec.ctext = kmalloc(maxdatasize, GFP_KERNEL);
1948 if (!vec.key || !vec.iv || !vec.assoc || !vec.ptext || !vec.ctext) {
1949 err = -ENOMEM;
1950 goto out;
1951 }
1952
1953 for (i = 0; i < fuzz_iterations * 8; i++) {
1954 generate_random_aead_testvec(generic_req, &vec,
1955 maxkeysize, maxdatasize,
1956 vec_name, sizeof(vec_name));
1957 generate_random_testvec_config(&cfg, cfgname, sizeof(cfgname));
1958
1959 err = test_aead_vec_cfg(driver, ENCRYPT, &vec, vec_name, &cfg,
1960 req, tsgls);
1961 if (err)
1962 goto out;
1963 err = test_aead_vec_cfg(driver, DECRYPT, &vec, vec_name, &cfg,
1964 req, tsgls);
1965 if (err)
1966 goto out;
1967 cond_resched();
1968 }
1969 err = 0;
1970out:
1971 kfree(vec.key);
1972 kfree(vec.iv);
1973 kfree(vec.assoc);
1974 kfree(vec.ptext);
1975 kfree(vec.ctext);
1976 crypto_free_aead(generic_tfm);
1977 aead_request_free(generic_req);
1978 return err;
1979}
1980#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1981static int test_aead_vs_generic_impl(const char *driver,
1982 const struct alg_test_desc *test_desc,
1983 struct aead_request *req,
1984 struct cipher_test_sglists *tsgls)
1985{
1986 return 0;
1987}
1988#endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1989
Eric Biggersed968042019-01-31 23:51:47 -08001990static int test_aead(const char *driver, int enc,
1991 const struct aead_test_suite *suite,
1992 struct aead_request *req,
1993 struct cipher_test_sglists *tsgls)
1994{
1995 unsigned int i;
1996 int err;
1997
1998 for (i = 0; i < suite->count; i++) {
1999 err = test_aead_vec(driver, enc, &suite->vecs[i], i, req,
2000 tsgls);
2001 if (err)
2002 return err;
2003 }
2004 return 0;
2005}
2006
2007static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
2008 u32 type, u32 mask)
2009{
2010 const struct aead_test_suite *suite = &desc->suite.aead;
2011 struct crypto_aead *tfm;
2012 struct aead_request *req = NULL;
2013 struct cipher_test_sglists *tsgls = NULL;
2014 int err;
2015
2016 if (suite->count <= 0) {
2017 pr_err("alg: aead: empty test suite for %s\n", driver);
2018 return -EINVAL;
2019 }
2020
2021 tfm = crypto_alloc_aead(driver, type, mask);
2022 if (IS_ERR(tfm)) {
2023 pr_err("alg: aead: failed to allocate transform for %s: %ld\n",
2024 driver, PTR_ERR(tfm));
2025 return PTR_ERR(tfm);
2026 }
2027
2028 req = aead_request_alloc(tfm, GFP_KERNEL);
2029 if (!req) {
2030 pr_err("alg: aead: failed to allocate request for %s\n",
2031 driver);
2032 err = -ENOMEM;
2033 goto out;
2034 }
2035
2036 tsgls = alloc_cipher_test_sglists();
2037 if (!tsgls) {
2038 pr_err("alg: aead: failed to allocate test buffers for %s\n",
2039 driver);
2040 err = -ENOMEM;
2041 goto out;
2042 }
2043
2044 err = test_aead(driver, ENCRYPT, suite, req, tsgls);
2045 if (err)
2046 goto out;
2047
2048 err = test_aead(driver, DECRYPT, suite, req, tsgls);
Eric Biggers40153b12019-04-11 21:57:41 -07002049 if (err)
2050 goto out;
2051
2052 err = test_aead_vs_generic_impl(driver, desc, req, tsgls);
Eric Biggersed968042019-01-31 23:51:47 -08002053out:
2054 free_cipher_test_sglists(tsgls);
2055 aead_request_free(req);
2056 crypto_free_aead(tfm);
2057 return err;
2058}
2059
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002060static int test_cipher(struct crypto_cipher *tfm, int enc,
Eric Biggersb13b1e02017-02-24 15:46:59 -08002061 const struct cipher_testvec *template,
2062 unsigned int tcount)
Herbert Xuda7f0332008-07-31 17:08:25 +08002063{
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002064 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
2065 unsigned int i, j, k;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002066 char *q;
2067 const char *e;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002068 const char *input, *result;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002069 void *data;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08002070 char *xbuf[XBUFSIZE];
2071 int ret = -ENOMEM;
2072
2073 if (testmgr_alloc_buf(xbuf))
2074 goto out_nobuf;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002075
2076 if (enc == ENCRYPT)
2077 e = "encryption";
2078 else
2079 e = "decryption";
2080
2081 j = 0;
2082 for (i = 0; i < tcount; i++) {
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002083
Stephan Mueller10faa8c2016-08-25 15:15:01 +02002084 if (fips_enabled && template[i].fips_skip)
2085 continue;
2086
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002087 input = enc ? template[i].ptext : template[i].ctext;
2088 result = enc ? template[i].ctext : template[i].ptext;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002089 j++;
2090
Herbert Xufd57f222009-05-29 16:05:42 +10002091 ret = -EINVAL;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002092 if (WARN_ON(template[i].len > PAGE_SIZE))
Herbert Xufd57f222009-05-29 16:05:42 +10002093 goto out;
2094
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002095 data = xbuf[0];
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002096 memcpy(data, input, template[i].len);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002097
2098 crypto_cipher_clear_flags(tfm, ~0);
2099 if (template[i].wk)
Eric Biggers231baec2019-01-18 22:48:00 -08002100 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002101
2102 ret = crypto_cipher_setkey(tfm, template[i].key,
2103 template[i].klen);
Eric Biggers5283a8e2019-04-11 21:57:36 -07002104 if (ret) {
2105 if (ret == template[i].setkey_error)
2106 continue;
2107 pr_err("alg: cipher: %s setkey failed on test vector %u; expected_error=%d, actual_error=%d, flags=%#x\n",
2108 algo, j, template[i].setkey_error, ret,
2109 crypto_cipher_get_flags(tfm));
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002110 goto out;
Eric Biggers5283a8e2019-04-11 21:57:36 -07002111 }
2112 if (template[i].setkey_error) {
2113 pr_err("alg: cipher: %s setkey unexpectedly succeeded on test vector %u; expected_error=%d\n",
2114 algo, j, template[i].setkey_error);
2115 ret = -EINVAL;
2116 goto out;
2117 }
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002118
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002119 for (k = 0; k < template[i].len;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002120 k += crypto_cipher_blocksize(tfm)) {
2121 if (enc)
2122 crypto_cipher_encrypt_one(tfm, data + k,
2123 data + k);
2124 else
2125 crypto_cipher_decrypt_one(tfm, data + k,
2126 data + k);
2127 }
2128
2129 q = data;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002130 if (memcmp(q, result, template[i].len)) {
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002131 printk(KERN_ERR "alg: cipher: Test %d failed "
2132 "on %s for %s\n", j, e, algo);
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002133 hexdump(q, template[i].len);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002134 ret = -EINVAL;
2135 goto out;
2136 }
2137 }
2138
2139 ret = 0;
2140
2141out:
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08002142 testmgr_free_buf(xbuf);
2143out_nobuf:
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002144 return ret;
2145}
2146
Eric Biggers4e7babba2019-01-31 23:51:46 -08002147static int test_skcipher_vec_cfg(const char *driver, int enc,
2148 const struct cipher_testvec *vec,
Eric Biggers951d1332019-04-11 21:57:37 -07002149 const char *vec_name,
Eric Biggers4e7babba2019-01-31 23:51:46 -08002150 const struct testvec_config *cfg,
2151 struct skcipher_request *req,
2152 struct cipher_test_sglists *tsgls)
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002153{
Eric Biggers4e7babba2019-01-31 23:51:46 -08002154 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2155 const unsigned int alignmask = crypto_skcipher_alignmask(tfm);
2156 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2157 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
2158 const char *op = enc ? "encryption" : "decryption";
2159 DECLARE_CRYPTO_WAIT(wait);
2160 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
2161 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
2162 cfg->iv_offset +
2163 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
2164 struct kvec input;
2165 int err;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08002166
Eric Biggers4e7babba2019-01-31 23:51:46 -08002167 /* Set the key */
2168 if (vec->wk)
2169 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03002170 else
Eric Biggers4e7babba2019-01-31 23:51:46 -08002171 crypto_skcipher_clear_flags(tfm,
2172 CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
2173 err = crypto_skcipher_setkey(tfm, vec->key, vec->klen);
2174 if (err) {
Eric Biggers5283a8e2019-04-11 21:57:36 -07002175 if (err == vec->setkey_error)
Eric Biggers4e7babba2019-01-31 23:51:46 -08002176 return 0;
Eric Biggers951d1332019-04-11 21:57:37 -07002177 pr_err("alg: skcipher: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
2178 driver, vec_name, vec->setkey_error, err,
Eric Biggers5283a8e2019-04-11 21:57:36 -07002179 crypto_skcipher_get_flags(tfm));
Eric Biggers4e7babba2019-01-31 23:51:46 -08002180 return err;
2181 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07002182 if (vec->setkey_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07002183 pr_err("alg: skcipher: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
2184 driver, vec_name, vec->setkey_error);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002185 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08002186 }
2187
Eric Biggers4e7babba2019-01-31 23:51:46 -08002188 /* The IV must be copied to a buffer, as the algorithm may modify it */
2189 if (ivsize) {
2190 if (WARN_ON(ivsize > MAX_IVLEN))
2191 return -EINVAL;
Eric Biggers8efd9722019-02-14 00:03:51 -08002192 if (vec->generates_iv && !enc)
2193 memcpy(iv, vec->iv_out, ivsize);
2194 else if (vec->iv)
Eric Biggers4e7babba2019-01-31 23:51:46 -08002195 memcpy(iv, vec->iv, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08002196 else
Eric Biggers4e7babba2019-01-31 23:51:46 -08002197 memset(iv, 0, ivsize);
2198 } else {
2199 if (vec->generates_iv) {
Eric Biggers951d1332019-04-11 21:57:37 -07002200 pr_err("alg: skcipher: %s has ivsize=0 but test vector %s generates IV!\n",
2201 driver, vec_name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002202 return -EINVAL;
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03002203 }
Eric Biggers4e7babba2019-01-31 23:51:46 -08002204 iv = NULL;
Herbert Xuda7f0332008-07-31 17:08:25 +08002205 }
2206
Eric Biggers4e7babba2019-01-31 23:51:46 -08002207 /* Build the src/dst scatterlists */
2208 input.iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
2209 input.iov_len = vec->len;
2210 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
2211 vec->len, vec->len, &input, 1);
2212 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07002213 pr_err("alg: skcipher: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
2214 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002215 return err;
Herbert Xuda7f0332008-07-31 17:08:25 +08002216 }
2217
Eric Biggers4e7babba2019-01-31 23:51:46 -08002218 /* Do the actual encryption or decryption */
2219 testmgr_poison(req->__ctx, crypto_skcipher_reqsize(tfm));
2220 skcipher_request_set_callback(req, req_flags, crypto_req_done, &wait);
2221 skcipher_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
2222 vec->len, iv);
Eric Biggers65707372019-03-12 22:12:52 -07002223 if (cfg->nosimd)
2224 crypto_disable_simd_for_test();
2225 err = enc ? crypto_skcipher_encrypt(req) : crypto_skcipher_decrypt(req);
2226 if (cfg->nosimd)
2227 crypto_reenable_simd_for_test();
2228 err = crypto_wait_req(err, &wait);
Herbert Xuda7f0332008-07-31 17:08:25 +08002229
Eric Biggersfa353c92019-01-31 23:51:49 -08002230 /* Check that the algorithm didn't overwrite things it shouldn't have */
2231 if (req->cryptlen != vec->len ||
2232 req->iv != iv ||
2233 req->src != tsgls->src.sgl_ptr ||
2234 req->dst != tsgls->dst.sgl_ptr ||
2235 crypto_skcipher_reqtfm(req) != tfm ||
2236 req->base.complete != crypto_req_done ||
2237 req->base.flags != req_flags ||
2238 req->base.data != &wait) {
Eric Biggers951d1332019-04-11 21:57:37 -07002239 pr_err("alg: skcipher: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
2240 driver, op, vec_name, cfg->name);
Eric Biggersfa353c92019-01-31 23:51:49 -08002241 if (req->cryptlen != vec->len)
2242 pr_err("alg: skcipher: changed 'req->cryptlen'\n");
2243 if (req->iv != iv)
2244 pr_err("alg: skcipher: changed 'req->iv'\n");
2245 if (req->src != tsgls->src.sgl_ptr)
2246 pr_err("alg: skcipher: changed 'req->src'\n");
2247 if (req->dst != tsgls->dst.sgl_ptr)
2248 pr_err("alg: skcipher: changed 'req->dst'\n");
2249 if (crypto_skcipher_reqtfm(req) != tfm)
2250 pr_err("alg: skcipher: changed 'req->base.tfm'\n");
2251 if (req->base.complete != crypto_req_done)
2252 pr_err("alg: skcipher: changed 'req->base.complete'\n");
2253 if (req->base.flags != req_flags)
2254 pr_err("alg: skcipher: changed 'req->base.flags'\n");
2255 if (req->base.data != &wait)
2256 pr_err("alg: skcipher: changed 'req->base.data'\n");
2257 return -EINVAL;
2258 }
2259 if (is_test_sglist_corrupted(&tsgls->src)) {
Eric Biggers951d1332019-04-11 21:57:37 -07002260 pr_err("alg: skcipher: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
2261 driver, op, vec_name, cfg->name);
Eric Biggersfa353c92019-01-31 23:51:49 -08002262 return -EINVAL;
2263 }
2264 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
2265 is_test_sglist_corrupted(&tsgls->dst)) {
Eric Biggers951d1332019-04-11 21:57:37 -07002266 pr_err("alg: skcipher: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
2267 driver, op, vec_name, cfg->name);
Eric Biggersfa353c92019-01-31 23:51:49 -08002268 return -EINVAL;
2269 }
2270
Eric Biggers5283a8e2019-04-11 21:57:36 -07002271 /* Check for success or failure */
2272 if (err) {
2273 if (err == vec->crypt_error)
2274 return 0;
Eric Biggers951d1332019-04-11 21:57:37 -07002275 pr_err("alg: skcipher: %s %s failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
2276 driver, op, vec_name, vec->crypt_error, err, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07002277 return err;
2278 }
2279 if (vec->crypt_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07002280 pr_err("alg: skcipher: %s %s unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
2281 driver, op, vec_name, vec->crypt_error, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07002282 return -EINVAL;
2283 }
2284
Eric Biggers4e7babba2019-01-31 23:51:46 -08002285 /* Check for the correct output (ciphertext or plaintext) */
2286 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
2287 vec->len, 0, true);
2288 if (err == -EOVERFLOW) {
Eric Biggers951d1332019-04-11 21:57:37 -07002289 pr_err("alg: skcipher: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
2290 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002291 return err;
2292 }
2293 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07002294 pr_err("alg: skcipher: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
2295 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002296 return err;
2297 }
Herbert Xuda7f0332008-07-31 17:08:25 +08002298
Eric Biggers4e7babba2019-01-31 23:51:46 -08002299 /* If applicable, check that the algorithm generated the correct IV */
Eric Biggers8efd9722019-02-14 00:03:51 -08002300 if (vec->iv_out && memcmp(iv, vec->iv_out, ivsize) != 0) {
Eric Biggers951d1332019-04-11 21:57:37 -07002301 pr_err("alg: skcipher: %s %s test failed (wrong output IV) on test vector %s, cfg=\"%s\"\n",
2302 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002303 hexdump(iv, ivsize);
2304 return -EINVAL;
Jussi Kivilinna3a338f22013-06-13 17:37:45 +03002305 }
2306
2307 return 0;
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03002308}
2309
Eric Biggers4e7babba2019-01-31 23:51:46 -08002310static int test_skcipher_vec(const char *driver, int enc,
2311 const struct cipher_testvec *vec,
2312 unsigned int vec_num,
2313 struct skcipher_request *req,
2314 struct cipher_test_sglists *tsgls)
2315{
Eric Biggers951d1332019-04-11 21:57:37 -07002316 char vec_name[16];
Eric Biggers4e7babba2019-01-31 23:51:46 -08002317 unsigned int i;
2318 int err;
2319
2320 if (fips_enabled && vec->fips_skip)
2321 return 0;
2322
Eric Biggers951d1332019-04-11 21:57:37 -07002323 sprintf(vec_name, "%u", vec_num);
2324
Eric Biggers4e7babba2019-01-31 23:51:46 -08002325 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
Eric Biggers951d1332019-04-11 21:57:37 -07002326 err = test_skcipher_vec_cfg(driver, enc, vec, vec_name,
Eric Biggers4e7babba2019-01-31 23:51:46 -08002327 &default_cipher_testvec_configs[i],
2328 req, tsgls);
2329 if (err)
2330 return err;
2331 }
2332
2333#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2334 if (!noextratests) {
2335 struct testvec_config cfg;
2336 char cfgname[TESTVEC_CONFIG_NAMELEN];
2337
2338 for (i = 0; i < fuzz_iterations; i++) {
2339 generate_random_testvec_config(&cfg, cfgname,
2340 sizeof(cfgname));
Eric Biggers951d1332019-04-11 21:57:37 -07002341 err = test_skcipher_vec_cfg(driver, enc, vec, vec_name,
Eric Biggers4e7babba2019-01-31 23:51:46 -08002342 &cfg, req, tsgls);
2343 if (err)
2344 return err;
2345 }
2346 }
2347#endif
2348 return 0;
2349}
2350
Eric Biggersd435e102019-04-11 21:57:40 -07002351#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2352/*
2353 * Generate a symmetric cipher test vector from the given implementation.
2354 * Assumes the buffers in 'vec' were already allocated.
2355 */
2356static void generate_random_cipher_testvec(struct skcipher_request *req,
2357 struct cipher_testvec *vec,
2358 unsigned int maxdatasize,
2359 char *name, size_t max_namelen)
2360{
2361 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2362 const unsigned int maxkeysize = tfm->keysize;
2363 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2364 struct scatterlist src, dst;
2365 u8 iv[MAX_IVLEN];
2366 DECLARE_CRYPTO_WAIT(wait);
2367
2368 /* Key: length in [0, maxkeysize], but usually choose maxkeysize */
2369 vec->klen = maxkeysize;
2370 if (prandom_u32() % 4 == 0)
2371 vec->klen = prandom_u32() % (maxkeysize + 1);
2372 generate_random_bytes((u8 *)vec->key, vec->klen);
2373 vec->setkey_error = crypto_skcipher_setkey(tfm, vec->key, vec->klen);
2374
2375 /* IV */
2376 generate_random_bytes((u8 *)vec->iv, ivsize);
2377
2378 /* Plaintext */
2379 vec->len = generate_random_length(maxdatasize);
2380 generate_random_bytes((u8 *)vec->ptext, vec->len);
2381
2382 /* If the key couldn't be set, no need to continue to encrypt. */
2383 if (vec->setkey_error)
2384 goto done;
2385
2386 /* Ciphertext */
2387 sg_init_one(&src, vec->ptext, vec->len);
2388 sg_init_one(&dst, vec->ctext, vec->len);
2389 memcpy(iv, vec->iv, ivsize);
2390 skcipher_request_set_callback(req, 0, crypto_req_done, &wait);
2391 skcipher_request_set_crypt(req, &src, &dst, vec->len, iv);
2392 vec->crypt_error = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
2393done:
2394 snprintf(name, max_namelen, "\"random: len=%u klen=%u\"",
2395 vec->len, vec->klen);
2396}
2397
2398/*
2399 * Test the skcipher algorithm represented by @req against the corresponding
2400 * generic implementation, if one is available.
2401 */
2402static int test_skcipher_vs_generic_impl(const char *driver,
2403 const char *generic_driver,
2404 struct skcipher_request *req,
2405 struct cipher_test_sglists *tsgls)
2406{
2407 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2408 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2409 const unsigned int blocksize = crypto_skcipher_blocksize(tfm);
2410 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
2411 const char *algname = crypto_skcipher_alg(tfm)->base.cra_name;
2412 char _generic_driver[CRYPTO_MAX_ALG_NAME];
2413 struct crypto_skcipher *generic_tfm = NULL;
2414 struct skcipher_request *generic_req = NULL;
2415 unsigned int i;
2416 struct cipher_testvec vec = { 0 };
2417 char vec_name[64];
2418 struct testvec_config cfg;
2419 char cfgname[TESTVEC_CONFIG_NAMELEN];
2420 int err;
2421
2422 if (noextratests)
2423 return 0;
2424
2425 /* Keywrap isn't supported here yet as it handles its IV differently. */
2426 if (strncmp(algname, "kw(", 3) == 0)
2427 return 0;
2428
2429 if (!generic_driver) { /* Use default naming convention? */
2430 err = build_generic_driver_name(algname, _generic_driver);
2431 if (err)
2432 return err;
2433 generic_driver = _generic_driver;
2434 }
2435
2436 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
2437 return 0;
2438
2439 generic_tfm = crypto_alloc_skcipher(generic_driver, 0, 0);
2440 if (IS_ERR(generic_tfm)) {
2441 err = PTR_ERR(generic_tfm);
2442 if (err == -ENOENT) {
2443 pr_warn("alg: skcipher: skipping comparison tests for %s because %s is unavailable\n",
2444 driver, generic_driver);
2445 return 0;
2446 }
2447 pr_err("alg: skcipher: error allocating %s (generic impl of %s): %d\n",
2448 generic_driver, algname, err);
2449 return err;
2450 }
2451
2452 generic_req = skcipher_request_alloc(generic_tfm, GFP_KERNEL);
2453 if (!generic_req) {
2454 err = -ENOMEM;
2455 goto out;
2456 }
2457
2458 /* Check the algorithm properties for consistency. */
2459
2460 if (tfm->keysize != generic_tfm->keysize) {
2461 pr_err("alg: skcipher: max keysize for %s (%u) doesn't match generic impl (%u)\n",
2462 driver, tfm->keysize, generic_tfm->keysize);
2463 err = -EINVAL;
2464 goto out;
2465 }
2466
2467 if (ivsize != crypto_skcipher_ivsize(generic_tfm)) {
2468 pr_err("alg: skcipher: ivsize for %s (%u) doesn't match generic impl (%u)\n",
2469 driver, ivsize, crypto_skcipher_ivsize(generic_tfm));
2470 err = -EINVAL;
2471 goto out;
2472 }
2473
2474 if (blocksize != crypto_skcipher_blocksize(generic_tfm)) {
2475 pr_err("alg: skcipher: blocksize for %s (%u) doesn't match generic impl (%u)\n",
2476 driver, blocksize,
2477 crypto_skcipher_blocksize(generic_tfm));
2478 err = -EINVAL;
2479 goto out;
2480 }
2481
2482 /*
2483 * Now generate test vectors using the generic implementation, and test
2484 * the other implementation against them.
2485 */
2486
2487 vec.key = kmalloc(tfm->keysize, GFP_KERNEL);
2488 vec.iv = kmalloc(ivsize, GFP_KERNEL);
2489 vec.ptext = kmalloc(maxdatasize, GFP_KERNEL);
2490 vec.ctext = kmalloc(maxdatasize, GFP_KERNEL);
2491 if (!vec.key || !vec.iv || !vec.ptext || !vec.ctext) {
2492 err = -ENOMEM;
2493 goto out;
2494 }
2495
2496 for (i = 0; i < fuzz_iterations * 8; i++) {
2497 generate_random_cipher_testvec(generic_req, &vec, maxdatasize,
2498 vec_name, sizeof(vec_name));
2499 generate_random_testvec_config(&cfg, cfgname, sizeof(cfgname));
2500
2501 err = test_skcipher_vec_cfg(driver, ENCRYPT, &vec, vec_name,
2502 &cfg, req, tsgls);
2503 if (err)
2504 goto out;
2505 err = test_skcipher_vec_cfg(driver, DECRYPT, &vec, vec_name,
2506 &cfg, req, tsgls);
2507 if (err)
2508 goto out;
2509 cond_resched();
2510 }
2511 err = 0;
2512out:
2513 kfree(vec.key);
2514 kfree(vec.iv);
2515 kfree(vec.ptext);
2516 kfree(vec.ctext);
2517 crypto_free_skcipher(generic_tfm);
2518 skcipher_request_free(generic_req);
2519 return err;
2520}
2521#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
2522static int test_skcipher_vs_generic_impl(const char *driver,
2523 const char *generic_driver,
2524 struct skcipher_request *req,
2525 struct cipher_test_sglists *tsgls)
2526{
2527 return 0;
2528}
2529#endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
2530
Eric Biggers4e7babba2019-01-31 23:51:46 -08002531static int test_skcipher(const char *driver, int enc,
2532 const struct cipher_test_suite *suite,
2533 struct skcipher_request *req,
2534 struct cipher_test_sglists *tsgls)
2535{
2536 unsigned int i;
2537 int err;
2538
2539 for (i = 0; i < suite->count; i++) {
2540 err = test_skcipher_vec(driver, enc, &suite->vecs[i], i, req,
2541 tsgls);
2542 if (err)
2543 return err;
2544 }
2545 return 0;
2546}
2547
2548static int alg_test_skcipher(const struct alg_test_desc *desc,
2549 const char *driver, u32 type, u32 mask)
2550{
2551 const struct cipher_test_suite *suite = &desc->suite.cipher;
2552 struct crypto_skcipher *tfm;
2553 struct skcipher_request *req = NULL;
2554 struct cipher_test_sglists *tsgls = NULL;
2555 int err;
2556
2557 if (suite->count <= 0) {
2558 pr_err("alg: skcipher: empty test suite for %s\n", driver);
2559 return -EINVAL;
2560 }
2561
2562 tfm = crypto_alloc_skcipher(driver, type, mask);
2563 if (IS_ERR(tfm)) {
2564 pr_err("alg: skcipher: failed to allocate transform for %s: %ld\n",
2565 driver, PTR_ERR(tfm));
2566 return PTR_ERR(tfm);
2567 }
2568
2569 req = skcipher_request_alloc(tfm, GFP_KERNEL);
2570 if (!req) {
2571 pr_err("alg: skcipher: failed to allocate request for %s\n",
2572 driver);
2573 err = -ENOMEM;
2574 goto out;
2575 }
2576
2577 tsgls = alloc_cipher_test_sglists();
2578 if (!tsgls) {
2579 pr_err("alg: skcipher: failed to allocate test buffers for %s\n",
2580 driver);
2581 err = -ENOMEM;
2582 goto out;
2583 }
2584
2585 err = test_skcipher(driver, ENCRYPT, suite, req, tsgls);
2586 if (err)
2587 goto out;
2588
2589 err = test_skcipher(driver, DECRYPT, suite, req, tsgls);
Eric Biggersd435e102019-04-11 21:57:40 -07002590 if (err)
2591 goto out;
2592
2593 err = test_skcipher_vs_generic_impl(driver, desc->generic_driver, req,
2594 tsgls);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002595out:
2596 free_cipher_test_sglists(tsgls);
2597 skcipher_request_free(req);
2598 crypto_free_skcipher(tfm);
2599 return err;
2600}
2601
Eric Biggersb13b1e02017-02-24 15:46:59 -08002602static int test_comp(struct crypto_comp *tfm,
2603 const struct comp_testvec *ctemplate,
2604 const struct comp_testvec *dtemplate,
2605 int ctcount, int dtcount)
Herbert Xuda7f0332008-07-31 17:08:25 +08002606{
2607 const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
Mahipal Challa33607382018-04-11 20:28:32 +02002608 char *output, *decomp_output;
Herbert Xuda7f0332008-07-31 17:08:25 +08002609 unsigned int i;
Herbert Xuda7f0332008-07-31 17:08:25 +08002610 int ret;
2611
Mahipal Challa33607382018-04-11 20:28:32 +02002612 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
2613 if (!output)
2614 return -ENOMEM;
2615
2616 decomp_output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
2617 if (!decomp_output) {
2618 kfree(output);
2619 return -ENOMEM;
2620 }
2621
Herbert Xuda7f0332008-07-31 17:08:25 +08002622 for (i = 0; i < ctcount; i++) {
Geert Uytterhoevenc79cf912009-03-29 15:44:19 +08002623 int ilen;
2624 unsigned int dlen = COMP_BUF_SIZE;
Herbert Xuda7f0332008-07-31 17:08:25 +08002625
Michael Schupikov22a81182018-10-07 13:58:10 +02002626 memset(output, 0, COMP_BUF_SIZE);
2627 memset(decomp_output, 0, COMP_BUF_SIZE);
Herbert Xuda7f0332008-07-31 17:08:25 +08002628
2629 ilen = ctemplate[i].inlen;
2630 ret = crypto_comp_compress(tfm, ctemplate[i].input,
Mahipal Challa33607382018-04-11 20:28:32 +02002631 ilen, output, &dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08002632 if (ret) {
2633 printk(KERN_ERR "alg: comp: compression failed "
2634 "on test %d for %s: ret=%d\n", i + 1, algo,
2635 -ret);
2636 goto out;
2637 }
2638
Mahipal Challa33607382018-04-11 20:28:32 +02002639 ilen = dlen;
2640 dlen = COMP_BUF_SIZE;
2641 ret = crypto_comp_decompress(tfm, output,
2642 ilen, decomp_output, &dlen);
2643 if (ret) {
2644 pr_err("alg: comp: compression failed: decompress: on test %d for %s failed: ret=%d\n",
2645 i + 1, algo, -ret);
2646 goto out;
2647 }
2648
2649 if (dlen != ctemplate[i].inlen) {
Geert Uytterhoevenb812eb02008-11-28 20:51:28 +08002650 printk(KERN_ERR "alg: comp: Compression test %d "
2651 "failed for %s: output len = %d\n", i + 1, algo,
2652 dlen);
2653 ret = -EINVAL;
2654 goto out;
2655 }
2656
Mahipal Challa33607382018-04-11 20:28:32 +02002657 if (memcmp(decomp_output, ctemplate[i].input,
2658 ctemplate[i].inlen)) {
2659 pr_err("alg: comp: compression failed: output differs: on test %d for %s\n",
2660 i + 1, algo);
2661 hexdump(decomp_output, dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08002662 ret = -EINVAL;
2663 goto out;
2664 }
2665 }
2666
2667 for (i = 0; i < dtcount; i++) {
Geert Uytterhoevenc79cf912009-03-29 15:44:19 +08002668 int ilen;
2669 unsigned int dlen = COMP_BUF_SIZE;
Herbert Xuda7f0332008-07-31 17:08:25 +08002670
Michael Schupikov22a81182018-10-07 13:58:10 +02002671 memset(decomp_output, 0, COMP_BUF_SIZE);
Herbert Xuda7f0332008-07-31 17:08:25 +08002672
2673 ilen = dtemplate[i].inlen;
2674 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
Mahipal Challa33607382018-04-11 20:28:32 +02002675 ilen, decomp_output, &dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08002676 if (ret) {
2677 printk(KERN_ERR "alg: comp: decompression failed "
2678 "on test %d for %s: ret=%d\n", i + 1, algo,
2679 -ret);
2680 goto out;
2681 }
2682
Geert Uytterhoevenb812eb02008-11-28 20:51:28 +08002683 if (dlen != dtemplate[i].outlen) {
2684 printk(KERN_ERR "alg: comp: Decompression test %d "
2685 "failed for %s: output len = %d\n", i + 1, algo,
2686 dlen);
2687 ret = -EINVAL;
2688 goto out;
2689 }
2690
Mahipal Challa33607382018-04-11 20:28:32 +02002691 if (memcmp(decomp_output, dtemplate[i].output, dlen)) {
Herbert Xuda7f0332008-07-31 17:08:25 +08002692 printk(KERN_ERR "alg: comp: Decompression test %d "
2693 "failed for %s\n", i + 1, algo);
Mahipal Challa33607382018-04-11 20:28:32 +02002694 hexdump(decomp_output, dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08002695 ret = -EINVAL;
2696 goto out;
2697 }
2698 }
2699
2700 ret = 0;
2701
2702out:
Mahipal Challa33607382018-04-11 20:28:32 +02002703 kfree(decomp_output);
2704 kfree(output);
Herbert Xuda7f0332008-07-31 17:08:25 +08002705 return ret;
2706}
2707
Eric Biggersb13b1e02017-02-24 15:46:59 -08002708static int test_acomp(struct crypto_acomp *tfm,
Mahipal Challa33607382018-04-11 20:28:32 +02002709 const struct comp_testvec *ctemplate,
Eric Biggersb13b1e02017-02-24 15:46:59 -08002710 const struct comp_testvec *dtemplate,
2711 int ctcount, int dtcount)
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002712{
2713 const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
2714 unsigned int i;
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002715 char *output, *decomp_out;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002716 int ret;
2717 struct scatterlist src, dst;
2718 struct acomp_req *req;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002719 struct crypto_wait wait;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002720
Eric Biggerseb095592016-11-23 10:24:35 -08002721 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
2722 if (!output)
2723 return -ENOMEM;
2724
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002725 decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
2726 if (!decomp_out) {
2727 kfree(output);
2728 return -ENOMEM;
2729 }
2730
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002731 for (i = 0; i < ctcount; i++) {
2732 unsigned int dlen = COMP_BUF_SIZE;
2733 int ilen = ctemplate[i].inlen;
Laura Abbott02608e02016-12-21 12:32:54 -08002734 void *input_vec;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002735
Eric Biggersd2110222016-12-30 14:12:00 -06002736 input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL);
Laura Abbott02608e02016-12-21 12:32:54 -08002737 if (!input_vec) {
2738 ret = -ENOMEM;
2739 goto out;
2740 }
2741
Eric Biggerseb095592016-11-23 10:24:35 -08002742 memset(output, 0, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002743 crypto_init_wait(&wait);
Laura Abbott02608e02016-12-21 12:32:54 -08002744 sg_init_one(&src, input_vec, ilen);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002745 sg_init_one(&dst, output, dlen);
2746
2747 req = acomp_request_alloc(tfm);
2748 if (!req) {
2749 pr_err("alg: acomp: request alloc failed for %s\n",
2750 algo);
Laura Abbott02608e02016-12-21 12:32:54 -08002751 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002752 ret = -ENOMEM;
2753 goto out;
2754 }
2755
2756 acomp_request_set_params(req, &src, &dst, ilen, dlen);
2757 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002758 crypto_req_done, &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002759
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002760 ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002761 if (ret) {
2762 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
2763 i + 1, algo, -ret);
Laura Abbott02608e02016-12-21 12:32:54 -08002764 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002765 acomp_request_free(req);
2766 goto out;
2767 }
2768
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002769 ilen = req->dlen;
2770 dlen = COMP_BUF_SIZE;
2771 sg_init_one(&src, output, ilen);
2772 sg_init_one(&dst, decomp_out, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002773 crypto_init_wait(&wait);
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002774 acomp_request_set_params(req, &src, &dst, ilen, dlen);
2775
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002776 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002777 if (ret) {
2778 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
2779 i + 1, algo, -ret);
2780 kfree(input_vec);
2781 acomp_request_free(req);
2782 goto out;
2783 }
2784
2785 if (req->dlen != ctemplate[i].inlen) {
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002786 pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
2787 i + 1, algo, req->dlen);
2788 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08002789 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002790 acomp_request_free(req);
2791 goto out;
2792 }
2793
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002794 if (memcmp(input_vec, decomp_out, req->dlen)) {
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002795 pr_err("alg: acomp: Compression test %d failed for %s\n",
2796 i + 1, algo);
2797 hexdump(output, req->dlen);
2798 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08002799 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002800 acomp_request_free(req);
2801 goto out;
2802 }
2803
Laura Abbott02608e02016-12-21 12:32:54 -08002804 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002805 acomp_request_free(req);
2806 }
2807
2808 for (i = 0; i < dtcount; i++) {
2809 unsigned int dlen = COMP_BUF_SIZE;
2810 int ilen = dtemplate[i].inlen;
Laura Abbott02608e02016-12-21 12:32:54 -08002811 void *input_vec;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002812
Eric Biggersd2110222016-12-30 14:12:00 -06002813 input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL);
Laura Abbott02608e02016-12-21 12:32:54 -08002814 if (!input_vec) {
2815 ret = -ENOMEM;
2816 goto out;
2817 }
2818
Eric Biggerseb095592016-11-23 10:24:35 -08002819 memset(output, 0, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002820 crypto_init_wait(&wait);
Laura Abbott02608e02016-12-21 12:32:54 -08002821 sg_init_one(&src, input_vec, ilen);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002822 sg_init_one(&dst, output, dlen);
2823
2824 req = acomp_request_alloc(tfm);
2825 if (!req) {
2826 pr_err("alg: acomp: request alloc failed for %s\n",
2827 algo);
Laura Abbott02608e02016-12-21 12:32:54 -08002828 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002829 ret = -ENOMEM;
2830 goto out;
2831 }
2832
2833 acomp_request_set_params(req, &src, &dst, ilen, dlen);
2834 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002835 crypto_req_done, &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002836
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002837 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002838 if (ret) {
2839 pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
2840 i + 1, algo, -ret);
Laura Abbott02608e02016-12-21 12:32:54 -08002841 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002842 acomp_request_free(req);
2843 goto out;
2844 }
2845
2846 if (req->dlen != dtemplate[i].outlen) {
2847 pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
2848 i + 1, algo, req->dlen);
2849 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08002850 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002851 acomp_request_free(req);
2852 goto out;
2853 }
2854
2855 if (memcmp(output, dtemplate[i].output, req->dlen)) {
2856 pr_err("alg: acomp: Decompression test %d failed for %s\n",
2857 i + 1, algo);
2858 hexdump(output, req->dlen);
2859 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08002860 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002861 acomp_request_free(req);
2862 goto out;
2863 }
2864
Laura Abbott02608e02016-12-21 12:32:54 -08002865 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002866 acomp_request_free(req);
2867 }
2868
2869 ret = 0;
2870
2871out:
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002872 kfree(decomp_out);
Eric Biggerseb095592016-11-23 10:24:35 -08002873 kfree(output);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002874 return ret;
2875}
2876
Eric Biggersb13b1e02017-02-24 15:46:59 -08002877static int test_cprng(struct crypto_rng *tfm,
2878 const struct cprng_testvec *template,
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002879 unsigned int tcount)
2880{
2881 const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
Felipe Contrerasfa4ef8a2009-10-27 19:04:42 +08002882 int err = 0, i, j, seedsize;
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002883 u8 *seed;
2884 char result[32];
2885
2886 seedsize = crypto_rng_seedsize(tfm);
2887
2888 seed = kmalloc(seedsize, GFP_KERNEL);
2889 if (!seed) {
2890 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
2891 "for %s\n", algo);
2892 return -ENOMEM;
2893 }
2894
2895 for (i = 0; i < tcount; i++) {
2896 memset(result, 0, 32);
2897
2898 memcpy(seed, template[i].v, template[i].vlen);
2899 memcpy(seed + template[i].vlen, template[i].key,
2900 template[i].klen);
2901 memcpy(seed + template[i].vlen + template[i].klen,
2902 template[i].dt, template[i].dtlen);
2903
2904 err = crypto_rng_reset(tfm, seed, seedsize);
2905 if (err) {
2906 printk(KERN_ERR "alg: cprng: Failed to reset rng "
2907 "for %s\n", algo);
2908 goto out;
2909 }
2910
2911 for (j = 0; j < template[i].loops; j++) {
2912 err = crypto_rng_get_bytes(tfm, result,
2913 template[i].rlen);
Stephan Mueller19e60e12015-03-10 17:00:36 +01002914 if (err < 0) {
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002915 printk(KERN_ERR "alg: cprng: Failed to obtain "
2916 "the correct amount of random data for "
Stephan Mueller19e60e12015-03-10 17:00:36 +01002917 "%s (requested %d)\n", algo,
2918 template[i].rlen);
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002919 goto out;
2920 }
2921 }
2922
2923 err = memcmp(result, template[i].result,
2924 template[i].rlen);
2925 if (err) {
2926 printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
2927 i, algo);
2928 hexdump(result, template[i].rlen);
2929 err = -EINVAL;
2930 goto out;
2931 }
2932 }
2933
2934out:
2935 kfree(seed);
2936 return err;
2937}
2938
Herbert Xuda7f0332008-07-31 17:08:25 +08002939static int alg_test_cipher(const struct alg_test_desc *desc,
2940 const char *driver, u32 type, u32 mask)
2941{
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002942 const struct cipher_test_suite *suite = &desc->suite.cipher;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002943 struct crypto_cipher *tfm;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002944 int err;
Herbert Xuda7f0332008-07-31 17:08:25 +08002945
Herbert Xueed93e02016-11-22 20:08:31 +08002946 tfm = crypto_alloc_cipher(driver, type, mask);
Herbert Xuda7f0332008-07-31 17:08:25 +08002947 if (IS_ERR(tfm)) {
2948 printk(KERN_ERR "alg: cipher: Failed to load transform for "
2949 "%s: %ld\n", driver, PTR_ERR(tfm));
2950 return PTR_ERR(tfm);
2951 }
2952
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002953 err = test_cipher(tfm, ENCRYPT, suite->vecs, suite->count);
2954 if (!err)
2955 err = test_cipher(tfm, DECRYPT, suite->vecs, suite->count);
Herbert Xuda7f0332008-07-31 17:08:25 +08002956
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002957 crypto_free_cipher(tfm);
2958 return err;
2959}
2960
Herbert Xuda7f0332008-07-31 17:08:25 +08002961static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
2962 u32 type, u32 mask)
2963{
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002964 struct crypto_comp *comp;
2965 struct crypto_acomp *acomp;
Herbert Xuda7f0332008-07-31 17:08:25 +08002966 int err;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002967 u32 algo_type = type & CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
Herbert Xuda7f0332008-07-31 17:08:25 +08002968
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002969 if (algo_type == CRYPTO_ALG_TYPE_ACOMPRESS) {
2970 acomp = crypto_alloc_acomp(driver, type, mask);
2971 if (IS_ERR(acomp)) {
2972 pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
2973 driver, PTR_ERR(acomp));
2974 return PTR_ERR(acomp);
2975 }
2976 err = test_acomp(acomp, desc->suite.comp.comp.vecs,
2977 desc->suite.comp.decomp.vecs,
2978 desc->suite.comp.comp.count,
2979 desc->suite.comp.decomp.count);
2980 crypto_free_acomp(acomp);
2981 } else {
2982 comp = crypto_alloc_comp(driver, type, mask);
2983 if (IS_ERR(comp)) {
2984 pr_err("alg: comp: Failed to load transform for %s: %ld\n",
2985 driver, PTR_ERR(comp));
2986 return PTR_ERR(comp);
2987 }
2988
2989 err = test_comp(comp, desc->suite.comp.comp.vecs,
2990 desc->suite.comp.decomp.vecs,
2991 desc->suite.comp.comp.count,
2992 desc->suite.comp.decomp.count);
2993
2994 crypto_free_comp(comp);
Herbert Xuda7f0332008-07-31 17:08:25 +08002995 }
Herbert Xuda7f0332008-07-31 17:08:25 +08002996 return err;
2997}
2998
Herbert Xu8e3ee852008-11-07 14:58:52 +08002999static int alg_test_crc32c(const struct alg_test_desc *desc,
3000 const char *driver, u32 type, u32 mask)
3001{
3002 struct crypto_shash *tfm;
Eric Biggerscb9dde82019-01-10 12:17:55 -08003003 __le32 val;
Herbert Xu8e3ee852008-11-07 14:58:52 +08003004 int err;
3005
3006 err = alg_test_hash(desc, driver, type, mask);
3007 if (err)
Eric Biggerseb5e6732019-01-23 20:57:35 -08003008 return err;
Herbert Xu8e3ee852008-11-07 14:58:52 +08003009
Herbert Xueed93e02016-11-22 20:08:31 +08003010 tfm = crypto_alloc_shash(driver, type, mask);
Herbert Xu8e3ee852008-11-07 14:58:52 +08003011 if (IS_ERR(tfm)) {
Eric Biggerseb5e6732019-01-23 20:57:35 -08003012 if (PTR_ERR(tfm) == -ENOENT) {
3013 /*
3014 * This crc32c implementation is only available through
3015 * ahash API, not the shash API, so the remaining part
3016 * of the test is not applicable to it.
3017 */
3018 return 0;
3019 }
Herbert Xu8e3ee852008-11-07 14:58:52 +08003020 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
3021 "%ld\n", driver, PTR_ERR(tfm));
Eric Biggerseb5e6732019-01-23 20:57:35 -08003022 return PTR_ERR(tfm);
Herbert Xu8e3ee852008-11-07 14:58:52 +08003023 }
3024
3025 do {
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02003026 SHASH_DESC_ON_STACK(shash, tfm);
3027 u32 *ctx = (u32 *)shash_desc_ctx(shash);
Herbert Xu8e3ee852008-11-07 14:58:52 +08003028
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02003029 shash->tfm = tfm;
3030 shash->flags = 0;
Herbert Xu8e3ee852008-11-07 14:58:52 +08003031
Eric Biggerscb9dde82019-01-10 12:17:55 -08003032 *ctx = 420553207;
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02003033 err = crypto_shash_final(shash, (u8 *)&val);
Herbert Xu8e3ee852008-11-07 14:58:52 +08003034 if (err) {
3035 printk(KERN_ERR "alg: crc32c: Operation failed for "
3036 "%s: %d\n", driver, err);
3037 break;
3038 }
3039
Eric Biggerscb9dde82019-01-10 12:17:55 -08003040 if (val != cpu_to_le32(~420553207)) {
3041 pr_err("alg: crc32c: Test failed for %s: %u\n",
3042 driver, le32_to_cpu(val));
Herbert Xu8e3ee852008-11-07 14:58:52 +08003043 err = -EINVAL;
3044 }
3045 } while (0);
3046
3047 crypto_free_shash(tfm);
3048
Herbert Xu8e3ee852008-11-07 14:58:52 +08003049 return err;
3050}
3051
Jarod Wilson7647d6c2009-05-04 19:44:50 +08003052static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
3053 u32 type, u32 mask)
3054{
3055 struct crypto_rng *rng;
3056 int err;
3057
Herbert Xueed93e02016-11-22 20:08:31 +08003058 rng = crypto_alloc_rng(driver, type, mask);
Jarod Wilson7647d6c2009-05-04 19:44:50 +08003059 if (IS_ERR(rng)) {
3060 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
3061 "%ld\n", driver, PTR_ERR(rng));
3062 return PTR_ERR(rng);
3063 }
3064
3065 err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
3066
3067 crypto_free_rng(rng);
3068
3069 return err;
3070}
3071
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003072
Eric Biggersb13b1e02017-02-24 15:46:59 -08003073static int drbg_cavs_test(const struct drbg_testvec *test, int pr,
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003074 const char *driver, u32 type, u32 mask)
3075{
3076 int ret = -EAGAIN;
3077 struct crypto_rng *drng;
3078 struct drbg_test_data test_data;
3079 struct drbg_string addtl, pers, testentropy;
3080 unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
3081
3082 if (!buf)
3083 return -ENOMEM;
3084
Herbert Xueed93e02016-11-22 20:08:31 +08003085 drng = crypto_alloc_rng(driver, type, mask);
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003086 if (IS_ERR(drng)) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04003087 printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003088 "%s\n", driver);
3089 kzfree(buf);
3090 return -ENOMEM;
3091 }
3092
3093 test_data.testentropy = &testentropy;
3094 drbg_string_fill(&testentropy, test->entropy, test->entropylen);
3095 drbg_string_fill(&pers, test->pers, test->perslen);
3096 ret = crypto_drbg_reset_test(drng, &pers, &test_data);
3097 if (ret) {
3098 printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
3099 goto outbuf;
3100 }
3101
3102 drbg_string_fill(&addtl, test->addtla, test->addtllen);
3103 if (pr) {
3104 drbg_string_fill(&testentropy, test->entpra, test->entprlen);
3105 ret = crypto_drbg_get_bytes_addtl_test(drng,
3106 buf, test->expectedlen, &addtl, &test_data);
3107 } else {
3108 ret = crypto_drbg_get_bytes_addtl(drng,
3109 buf, test->expectedlen, &addtl);
3110 }
Stephan Mueller19e60e12015-03-10 17:00:36 +01003111 if (ret < 0) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04003112 printk(KERN_ERR "alg: drbg: could not obtain random data for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003113 "driver %s\n", driver);
3114 goto outbuf;
3115 }
3116
3117 drbg_string_fill(&addtl, test->addtlb, test->addtllen);
3118 if (pr) {
3119 drbg_string_fill(&testentropy, test->entprb, test->entprlen);
3120 ret = crypto_drbg_get_bytes_addtl_test(drng,
3121 buf, test->expectedlen, &addtl, &test_data);
3122 } else {
3123 ret = crypto_drbg_get_bytes_addtl(drng,
3124 buf, test->expectedlen, &addtl);
3125 }
Stephan Mueller19e60e12015-03-10 17:00:36 +01003126 if (ret < 0) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04003127 printk(KERN_ERR "alg: drbg: could not obtain random data for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003128 "driver %s\n", driver);
3129 goto outbuf;
3130 }
3131
3132 ret = memcmp(test->expected, buf, test->expectedlen);
3133
3134outbuf:
3135 crypto_free_rng(drng);
3136 kzfree(buf);
3137 return ret;
3138}
3139
3140
3141static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
3142 u32 type, u32 mask)
3143{
3144 int err = 0;
3145 int pr = 0;
3146 int i = 0;
Eric Biggersb13b1e02017-02-24 15:46:59 -08003147 const struct drbg_testvec *template = desc->suite.drbg.vecs;
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003148 unsigned int tcount = desc->suite.drbg.count;
3149
3150 if (0 == memcmp(driver, "drbg_pr_", 8))
3151 pr = 1;
3152
3153 for (i = 0; i < tcount; i++) {
3154 err = drbg_cavs_test(&template[i], pr, driver, type, mask);
3155 if (err) {
3156 printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
3157 i, driver);
3158 err = -EINVAL;
3159 break;
3160 }
3161 }
3162 return err;
3163
3164}
3165
Eric Biggersb13b1e02017-02-24 15:46:59 -08003166static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003167 const char *alg)
3168{
3169 struct kpp_request *req;
3170 void *input_buf = NULL;
3171 void *output_buf = NULL;
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003172 void *a_public = NULL;
3173 void *a_ss = NULL;
3174 void *shared_secret = NULL;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003175 struct crypto_wait wait;
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003176 unsigned int out_len_max;
3177 int err = -ENOMEM;
3178 struct scatterlist src, dst;
3179
3180 req = kpp_request_alloc(tfm, GFP_KERNEL);
3181 if (!req)
3182 return err;
3183
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003184 crypto_init_wait(&wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003185
3186 err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
3187 if (err < 0)
3188 goto free_req;
3189
3190 out_len_max = crypto_kpp_maxsize(tfm);
3191 output_buf = kzalloc(out_len_max, GFP_KERNEL);
3192 if (!output_buf) {
3193 err = -ENOMEM;
3194 goto free_req;
3195 }
3196
3197 /* Use appropriate parameter as base */
3198 kpp_request_set_input(req, NULL, 0);
3199 sg_init_one(&dst, output_buf, out_len_max);
3200 kpp_request_set_output(req, &dst, out_len_max);
3201 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003202 crypto_req_done, &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003203
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003204 /* Compute party A's public key */
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003205 err = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003206 if (err) {
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003207 pr_err("alg: %s: Party A: generate public key test failed. err %d\n",
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003208 alg, err);
3209 goto free_output;
3210 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003211
3212 if (vec->genkey) {
3213 /* Save party A's public key */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05003214 a_public = kmemdup(sg_virt(req->dst), out_len_max, GFP_KERNEL);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003215 if (!a_public) {
3216 err = -ENOMEM;
3217 goto free_output;
3218 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003219 } else {
3220 /* Verify calculated public key */
3221 if (memcmp(vec->expected_a_public, sg_virt(req->dst),
3222 vec->expected_a_public_size)) {
3223 pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n",
3224 alg);
3225 err = -EINVAL;
3226 goto free_output;
3227 }
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003228 }
3229
3230 /* Calculate shared secret key by using counter part (b) public key. */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05003231 input_buf = kmemdup(vec->b_public, vec->b_public_size, GFP_KERNEL);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003232 if (!input_buf) {
3233 err = -ENOMEM;
3234 goto free_output;
3235 }
3236
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003237 sg_init_one(&src, input_buf, vec->b_public_size);
3238 sg_init_one(&dst, output_buf, out_len_max);
3239 kpp_request_set_input(req, &src, vec->b_public_size);
3240 kpp_request_set_output(req, &dst, out_len_max);
3241 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003242 crypto_req_done, &wait);
3243 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003244 if (err) {
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003245 pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n",
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003246 alg, err);
3247 goto free_all;
3248 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003249
3250 if (vec->genkey) {
3251 /* Save the shared secret obtained by party A */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05003252 a_ss = kmemdup(sg_virt(req->dst), vec->expected_ss_size, GFP_KERNEL);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003253 if (!a_ss) {
3254 err = -ENOMEM;
3255 goto free_all;
3256 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003257
3258 /*
3259 * Calculate party B's shared secret by using party A's
3260 * public key.
3261 */
3262 err = crypto_kpp_set_secret(tfm, vec->b_secret,
3263 vec->b_secret_size);
3264 if (err < 0)
3265 goto free_all;
3266
3267 sg_init_one(&src, a_public, vec->expected_a_public_size);
3268 sg_init_one(&dst, output_buf, out_len_max);
3269 kpp_request_set_input(req, &src, vec->expected_a_public_size);
3270 kpp_request_set_output(req, &dst, out_len_max);
3271 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003272 crypto_req_done, &wait);
3273 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req),
3274 &wait);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003275 if (err) {
3276 pr_err("alg: %s: Party B: compute shared secret failed. err %d\n",
3277 alg, err);
3278 goto free_all;
3279 }
3280
3281 shared_secret = a_ss;
3282 } else {
3283 shared_secret = (void *)vec->expected_ss;
3284 }
3285
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003286 /*
3287 * verify shared secret from which the user will derive
3288 * secret key by executing whatever hash it has chosen
3289 */
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003290 if (memcmp(shared_secret, sg_virt(req->dst),
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003291 vec->expected_ss_size)) {
3292 pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
3293 alg);
3294 err = -EINVAL;
3295 }
3296
3297free_all:
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003298 kfree(a_ss);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003299 kfree(input_buf);
3300free_output:
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003301 kfree(a_public);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003302 kfree(output_buf);
3303free_req:
3304 kpp_request_free(req);
3305 return err;
3306}
3307
3308static int test_kpp(struct crypto_kpp *tfm, const char *alg,
Eric Biggersb13b1e02017-02-24 15:46:59 -08003309 const struct kpp_testvec *vecs, unsigned int tcount)
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003310{
3311 int ret, i;
3312
3313 for (i = 0; i < tcount; i++) {
3314 ret = do_test_kpp(tfm, vecs++, alg);
3315 if (ret) {
3316 pr_err("alg: %s: test failed on vector %d, err=%d\n",
3317 alg, i + 1, ret);
3318 return ret;
3319 }
3320 }
3321 return 0;
3322}
3323
3324static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
3325 u32 type, u32 mask)
3326{
3327 struct crypto_kpp *tfm;
3328 int err = 0;
3329
Herbert Xueed93e02016-11-22 20:08:31 +08003330 tfm = crypto_alloc_kpp(driver, type, mask);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003331 if (IS_ERR(tfm)) {
3332 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
3333 driver, PTR_ERR(tfm));
3334 return PTR_ERR(tfm);
3335 }
3336 if (desc->suite.kpp.vecs)
3337 err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
3338 desc->suite.kpp.count);
3339
3340 crypto_free_kpp(tfm);
3341 return err;
3342}
3343
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003344static u8 *test_pack_u32(u8 *dst, u32 val)
3345{
3346 memcpy(dst, &val, sizeof(val));
3347 return dst + sizeof(val);
3348}
3349
Herbert Xu50d2b6432016-06-29 19:32:20 +08003350static int test_akcipher_one(struct crypto_akcipher *tfm,
Eric Biggersb13b1e02017-02-24 15:46:59 -08003351 const struct akcipher_testvec *vecs)
Tadeusz Struk946cc462015-06-16 10:31:06 -07003352{
Herbert Xudf27b262016-05-05 16:42:49 +08003353 char *xbuf[XBUFSIZE];
Tadeusz Struk946cc462015-06-16 10:31:06 -07003354 struct akcipher_request *req;
3355 void *outbuf_enc = NULL;
3356 void *outbuf_dec = NULL;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003357 struct crypto_wait wait;
Tadeusz Struk946cc462015-06-16 10:31:06 -07003358 unsigned int out_len_max, out_len = 0;
3359 int err = -ENOMEM;
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03003360 struct scatterlist src, dst, src_tab[3];
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003361 const char *m, *c;
3362 unsigned int m_size, c_size;
3363 const char *op;
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003364 u8 *key, *ptr;
Tadeusz Struk946cc462015-06-16 10:31:06 -07003365
Herbert Xudf27b262016-05-05 16:42:49 +08003366 if (testmgr_alloc_buf(xbuf))
3367 return err;
3368
Tadeusz Struk946cc462015-06-16 10:31:06 -07003369 req = akcipher_request_alloc(tfm, GFP_KERNEL);
3370 if (!req)
Herbert Xudf27b262016-05-05 16:42:49 +08003371 goto free_xbuf;
Tadeusz Struk946cc462015-06-16 10:31:06 -07003372
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003373 crypto_init_wait(&wait);
Tadeusz Struk22287b02015-10-08 09:26:55 -07003374
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003375 key = kmalloc(vecs->key_len + sizeof(u32) * 2 + vecs->param_len,
3376 GFP_KERNEL);
3377 if (!key)
3378 goto free_xbuf;
3379 memcpy(key, vecs->key, vecs->key_len);
3380 ptr = key + vecs->key_len;
3381 ptr = test_pack_u32(ptr, vecs->algo);
3382 ptr = test_pack_u32(ptr, vecs->param_len);
3383 memcpy(ptr, vecs->params, vecs->param_len);
3384
Tadeusz Struk22287b02015-10-08 09:26:55 -07003385 if (vecs->public_key_vec)
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003386 err = crypto_akcipher_set_pub_key(tfm, key, vecs->key_len);
Tadeusz Struk22287b02015-10-08 09:26:55 -07003387 else
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003388 err = crypto_akcipher_set_priv_key(tfm, key, vecs->key_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003389 if (err)
3390 goto free_req;
3391
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003392 /*
3393 * First run test which do not require a private key, such as
3394 * encrypt or verify.
3395 */
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03003396 err = -ENOMEM;
3397 out_len_max = crypto_akcipher_maxsize(tfm);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003398 outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
3399 if (!outbuf_enc)
3400 goto free_req;
3401
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003402 if (!vecs->siggen_sigver_test) {
3403 m = vecs->m;
3404 m_size = vecs->m_size;
3405 c = vecs->c;
3406 c_size = vecs->c_size;
3407 op = "encrypt";
3408 } else {
3409 /* Swap args so we could keep plaintext (digest)
3410 * in vecs->m, and cooked signature in vecs->c.
3411 */
3412 m = vecs->c; /* signature */
3413 m_size = vecs->c_size;
3414 c = vecs->m; /* digest */
3415 c_size = vecs->m_size;
3416 op = "verify";
3417 }
Herbert Xudf27b262016-05-05 16:42:49 +08003418
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003419 if (WARN_ON(m_size > PAGE_SIZE))
3420 goto free_all;
3421 memcpy(xbuf[0], m, m_size);
Herbert Xudf27b262016-05-05 16:42:49 +08003422
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03003423 sg_init_table(src_tab, 3);
Herbert Xudf27b262016-05-05 16:42:49 +08003424 sg_set_buf(&src_tab[0], xbuf[0], 8);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003425 sg_set_buf(&src_tab[1], xbuf[0] + 8, m_size - 8);
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03003426 if (vecs->siggen_sigver_test) {
3427 if (WARN_ON(c_size > PAGE_SIZE))
3428 goto free_all;
3429 memcpy(xbuf[1], c, c_size);
3430 sg_set_buf(&src_tab[2], xbuf[1], c_size);
3431 akcipher_request_set_crypt(req, src_tab, NULL, m_size, c_size);
3432 } else {
3433 sg_init_one(&dst, outbuf_enc, out_len_max);
3434 akcipher_request_set_crypt(req, src_tab, &dst, m_size,
3435 out_len_max);
3436 }
Tadeusz Struk946cc462015-06-16 10:31:06 -07003437 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003438 crypto_req_done, &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003439
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003440 err = crypto_wait_req(vecs->siggen_sigver_test ?
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003441 /* Run asymmetric signature verification */
3442 crypto_akcipher_verify(req) :
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003443 /* Run asymmetric encrypt */
3444 crypto_akcipher_encrypt(req), &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003445 if (err) {
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003446 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003447 goto free_all;
3448 }
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03003449 if (!vecs->siggen_sigver_test) {
3450 if (req->dst_len != c_size) {
3451 pr_err("alg: akcipher: %s test failed. Invalid output len\n",
3452 op);
3453 err = -EINVAL;
3454 goto free_all;
3455 }
3456 /* verify that encrypted message is equal to expected */
3457 if (memcmp(c, outbuf_enc, c_size) != 0) {
3458 pr_err("alg: akcipher: %s test failed. Invalid output\n",
3459 op);
3460 hexdump(outbuf_enc, c_size);
3461 err = -EINVAL;
3462 goto free_all;
3463 }
Tadeusz Struk946cc462015-06-16 10:31:06 -07003464 }
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003465
3466 /*
3467 * Don't invoke (decrypt or sign) test which require a private key
3468 * for vectors with only a public key.
3469 */
Tadeusz Struk946cc462015-06-16 10:31:06 -07003470 if (vecs->public_key_vec) {
3471 err = 0;
3472 goto free_all;
3473 }
3474 outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
3475 if (!outbuf_dec) {
3476 err = -ENOMEM;
3477 goto free_all;
3478 }
Herbert Xudf27b262016-05-05 16:42:49 +08003479
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003480 op = vecs->siggen_sigver_test ? "sign" : "decrypt";
3481 if (WARN_ON(c_size > PAGE_SIZE))
Herbert Xudf27b262016-05-05 16:42:49 +08003482 goto free_all;
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003483 memcpy(xbuf[0], c, c_size);
Herbert Xudf27b262016-05-05 16:42:49 +08003484
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003485 sg_init_one(&src, xbuf[0], c_size);
Tadeusz Struk22287b02015-10-08 09:26:55 -07003486 sg_init_one(&dst, outbuf_dec, out_len_max);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003487 crypto_init_wait(&wait);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003488 akcipher_request_set_crypt(req, &src, &dst, c_size, out_len_max);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003489
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003490 err = crypto_wait_req(vecs->siggen_sigver_test ?
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003491 /* Run asymmetric signature generation */
3492 crypto_akcipher_sign(req) :
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003493 /* Run asymmetric decrypt */
3494 crypto_akcipher_decrypt(req), &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003495 if (err) {
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003496 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003497 goto free_all;
3498 }
3499 out_len = req->dst_len;
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003500 if (out_len < m_size) {
3501 pr_err("alg: akcipher: %s test failed. Invalid output len %u\n",
3502 op, out_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003503 err = -EINVAL;
3504 goto free_all;
3505 }
3506 /* verify that decrypted message is equal to the original msg */
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003507 if (memchr_inv(outbuf_dec, 0, out_len - m_size) ||
3508 memcmp(m, outbuf_dec + out_len - m_size, m_size)) {
3509 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
Herbert Xu50d2b6432016-06-29 19:32:20 +08003510 hexdump(outbuf_dec, out_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003511 err = -EINVAL;
3512 }
3513free_all:
3514 kfree(outbuf_dec);
3515 kfree(outbuf_enc);
3516free_req:
3517 akcipher_request_free(req);
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003518 kfree(key);
Herbert Xudf27b262016-05-05 16:42:49 +08003519free_xbuf:
3520 testmgr_free_buf(xbuf);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003521 return err;
3522}
3523
Herbert Xu50d2b6432016-06-29 19:32:20 +08003524static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
Eric Biggersb13b1e02017-02-24 15:46:59 -08003525 const struct akcipher_testvec *vecs,
3526 unsigned int tcount)
Tadeusz Struk946cc462015-06-16 10:31:06 -07003527{
Herbert Xu15226e42016-07-18 18:20:10 +08003528 const char *algo =
3529 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
Tadeusz Struk946cc462015-06-16 10:31:06 -07003530 int ret, i;
3531
3532 for (i = 0; i < tcount; i++) {
Herbert Xu50d2b6432016-06-29 19:32:20 +08003533 ret = test_akcipher_one(tfm, vecs++);
3534 if (!ret)
3535 continue;
3536
Herbert Xu15226e42016-07-18 18:20:10 +08003537 pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
3538 i + 1, algo, ret);
Herbert Xu50d2b6432016-06-29 19:32:20 +08003539 return ret;
Tadeusz Struk946cc462015-06-16 10:31:06 -07003540 }
3541 return 0;
3542}
3543
Tadeusz Struk946cc462015-06-16 10:31:06 -07003544static int alg_test_akcipher(const struct alg_test_desc *desc,
3545 const char *driver, u32 type, u32 mask)
3546{
3547 struct crypto_akcipher *tfm;
3548 int err = 0;
3549
Herbert Xueed93e02016-11-22 20:08:31 +08003550 tfm = crypto_alloc_akcipher(driver, type, mask);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003551 if (IS_ERR(tfm)) {
3552 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
3553 driver, PTR_ERR(tfm));
3554 return PTR_ERR(tfm);
3555 }
3556 if (desc->suite.akcipher.vecs)
3557 err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
3558 desc->suite.akcipher.count);
3559
3560 crypto_free_akcipher(tfm);
3561 return err;
3562}
3563
Youquan, Song863b5572009-12-23 19:45:20 +08003564static int alg_test_null(const struct alg_test_desc *desc,
3565 const char *driver, u32 type, u32 mask)
3566{
3567 return 0;
3568}
3569
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003570#define __VECS(tv) { .vecs = tv, .count = ARRAY_SIZE(tv) }
3571
Herbert Xuda7f0332008-07-31 17:08:25 +08003572/* Please keep this list sorted by algorithm name. */
3573static const struct alg_test_desc alg_test_descs[] = {
3574 {
Eric Biggers059c2a42018-11-16 17:26:31 -08003575 .alg = "adiantum(xchacha12,aes)",
Eric Biggersd435e102019-04-11 21:57:40 -07003576 .generic_driver = "adiantum(xchacha12-generic,aes-generic,nhpoly1305-generic)",
Eric Biggers059c2a42018-11-16 17:26:31 -08003577 .test = alg_test_skcipher,
3578 .suite = {
3579 .cipher = __VECS(adiantum_xchacha12_aes_tv_template)
3580 },
3581 }, {
3582 .alg = "adiantum(xchacha20,aes)",
Eric Biggersd435e102019-04-11 21:57:40 -07003583 .generic_driver = "adiantum(xchacha20-generic,aes-generic,nhpoly1305-generic)",
Eric Biggers059c2a42018-11-16 17:26:31 -08003584 .test = alg_test_skcipher,
3585 .suite = {
3586 .cipher = __VECS(adiantum_xchacha20_aes_tv_template)
3587 },
3588 }, {
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02003589 .alg = "aegis128",
3590 .test = alg_test_aead,
3591 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003592 .aead = __VECS(aegis128_tv_template)
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02003593 }
3594 }, {
3595 .alg = "aegis128l",
3596 .test = alg_test_aead,
3597 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003598 .aead = __VECS(aegis128l_tv_template)
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02003599 }
3600 }, {
3601 .alg = "aegis256",
3602 .test = alg_test_aead,
3603 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003604 .aead = __VECS(aegis256_tv_template)
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02003605 }
3606 }, {
Jarod Wilsone08ca2d2009-05-04 19:46:29 +08003607 .alg = "ansi_cprng",
3608 .test = alg_test_cprng,
3609 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003610 .cprng = __VECS(ansi_cprng_aes_tv_template)
Jarod Wilsone08ca2d2009-05-04 19:46:29 +08003611 }
3612 }, {
Horia Geantabca4feb2014-03-14 17:46:51 +02003613 .alg = "authenc(hmac(md5),ecb(cipher_null))",
3614 .test = alg_test_aead,
Horia Geantabca4feb2014-03-14 17:46:51 +02003615 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003616 .aead = __VECS(hmac_md5_ecb_cipher_null_tv_template)
Horia Geantabca4feb2014-03-14 17:46:51 +02003617 }
3618 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003619 .alg = "authenc(hmac(sha1),cbc(aes))",
Horia Geantae46e9a42012-07-03 19:16:54 +03003620 .test = alg_test_aead,
Herbert Xubcf741c2017-06-28 19:09:07 +08003621 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03003622 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003623 .aead = __VECS(hmac_sha1_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303624 }
3625 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003626 .alg = "authenc(hmac(sha1),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303627 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303628 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003629 .aead = __VECS(hmac_sha1_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303630 }
3631 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003632 .alg = "authenc(hmac(sha1),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303633 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01003634 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303635 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003636 .aead = __VECS(hmac_sha1_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03003637 }
3638 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01003639 .alg = "authenc(hmac(sha1),ctr(aes))",
3640 .test = alg_test_null,
3641 .fips_allowed = 1,
3642 }, {
Horia Geantabca4feb2014-03-14 17:46:51 +02003643 .alg = "authenc(hmac(sha1),ecb(cipher_null))",
3644 .test = alg_test_aead,
Horia Geantabca4feb2014-03-14 17:46:51 +02003645 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003646 .aead = __VECS(hmac_sha1_ecb_cipher_null_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303647 }
3648 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01003649 .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
3650 .test = alg_test_null,
3651 .fips_allowed = 1,
3652 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003653 .alg = "authenc(hmac(sha224),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303654 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303655 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003656 .aead = __VECS(hmac_sha224_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303657 }
3658 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003659 .alg = "authenc(hmac(sha224),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303660 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01003661 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303662 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003663 .aead = __VECS(hmac_sha224_des3_ede_cbc_tv_temp)
Horia Geantabca4feb2014-03-14 17:46:51 +02003664 }
3665 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003666 .alg = "authenc(hmac(sha256),cbc(aes))",
Horia Geantae46e9a42012-07-03 19:16:54 +03003667 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01003668 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03003669 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003670 .aead = __VECS(hmac_sha256_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303671 }
3672 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003673 .alg = "authenc(hmac(sha256),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303674 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303675 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003676 .aead = __VECS(hmac_sha256_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303677 }
3678 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003679 .alg = "authenc(hmac(sha256),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303680 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01003681 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303682 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003683 .aead = __VECS(hmac_sha256_des3_ede_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303684 }
3685 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01003686 .alg = "authenc(hmac(sha256),ctr(aes))",
3687 .test = alg_test_null,
3688 .fips_allowed = 1,
3689 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01003690 .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
3691 .test = alg_test_null,
3692 .fips_allowed = 1,
3693 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003694 .alg = "authenc(hmac(sha384),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303695 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303696 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003697 .aead = __VECS(hmac_sha384_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303698 }
3699 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003700 .alg = "authenc(hmac(sha384),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303701 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01003702 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303703 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003704 .aead = __VECS(hmac_sha384_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03003705 }
3706 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01003707 .alg = "authenc(hmac(sha384),ctr(aes))",
3708 .test = alg_test_null,
3709 .fips_allowed = 1,
3710 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01003711 .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
3712 .test = alg_test_null,
3713 .fips_allowed = 1,
3714 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003715 .alg = "authenc(hmac(sha512),cbc(aes))",
Marcus Meissnered1afac2016-02-05 14:23:33 +01003716 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03003717 .test = alg_test_aead,
Horia Geantae46e9a42012-07-03 19:16:54 +03003718 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003719 .aead = __VECS(hmac_sha512_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303720 }
3721 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003722 .alg = "authenc(hmac(sha512),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303723 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303724 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003725 .aead = __VECS(hmac_sha512_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303726 }
3727 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003728 .alg = "authenc(hmac(sha512),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303729 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01003730 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303731 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003732 .aead = __VECS(hmac_sha512_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03003733 }
3734 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01003735 .alg = "authenc(hmac(sha512),ctr(aes))",
3736 .test = alg_test_null,
3737 .fips_allowed = 1,
3738 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01003739 .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
3740 .test = alg_test_null,
3741 .fips_allowed = 1,
3742 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003743 .alg = "cbc(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003744 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003745 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003746 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003747 .cipher = __VECS(aes_cbc_tv_template)
3748 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003749 }, {
3750 .alg = "cbc(anubis)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003751 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003752 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003753 .cipher = __VECS(anubis_cbc_tv_template)
3754 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003755 }, {
3756 .alg = "cbc(blowfish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003757 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003758 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003759 .cipher = __VECS(bf_cbc_tv_template)
3760 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003761 }, {
3762 .alg = "cbc(camellia)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003763 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003764 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003765 .cipher = __VECS(camellia_cbc_tv_template)
3766 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003767 }, {
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02003768 .alg = "cbc(cast5)",
3769 .test = alg_test_skcipher,
3770 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003771 .cipher = __VECS(cast5_cbc_tv_template)
3772 },
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02003773 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003774 .alg = "cbc(cast6)",
3775 .test = alg_test_skcipher,
3776 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003777 .cipher = __VECS(cast6_cbc_tv_template)
3778 },
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003779 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003780 .alg = "cbc(des)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003781 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003782 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003783 .cipher = __VECS(des_cbc_tv_template)
3784 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003785 }, {
3786 .alg = "cbc(des3_ede)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003787 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003788 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003789 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003790 .cipher = __VECS(des3_ede_cbc_tv_template)
3791 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003792 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01003793 /* Same as cbc(aes) except the key is stored in
3794 * hardware secure memory which we reference by index
3795 */
3796 .alg = "cbc(paes)",
3797 .test = alg_test_null,
3798 .fips_allowed = 1,
3799 }, {
Jussi Kivilinna9d259172011-10-18 00:02:53 +03003800 .alg = "cbc(serpent)",
3801 .test = alg_test_skcipher,
3802 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003803 .cipher = __VECS(serpent_cbc_tv_template)
3804 },
Jussi Kivilinna9d259172011-10-18 00:02:53 +03003805 }, {
Gilad Ben-Yossef95ba5972018-09-20 14:18:38 +01003806 .alg = "cbc(sm4)",
3807 .test = alg_test_skcipher,
3808 .suite = {
3809 .cipher = __VECS(sm4_cbc_tv_template)
3810 }
3811 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003812 .alg = "cbc(twofish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003813 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003814 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003815 .cipher = __VECS(tf_cbc_tv_template)
3816 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003817 }, {
Ard Biesheuvel092acf02017-02-03 14:49:35 +00003818 .alg = "cbcmac(aes)",
3819 .fips_allowed = 1,
3820 .test = alg_test_hash,
3821 .suite = {
3822 .hash = __VECS(aes_cbcmac_tv_template)
3823 }
3824 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003825 .alg = "ccm(aes)",
Eric Biggers40153b12019-04-11 21:57:41 -07003826 .generic_driver = "ccm_base(ctr(aes-generic),cbcmac(aes-generic))",
Herbert Xuda7f0332008-07-31 17:08:25 +08003827 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003828 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003829 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003830 .aead = __VECS(aes_ccm_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003831 }
3832 }, {
Dmitry Eremin-Solenikov7da66672018-10-20 02:01:53 +03003833 .alg = "cfb(aes)",
3834 .test = alg_test_skcipher,
3835 .fips_allowed = 1,
3836 .suite = {
3837 .cipher = __VECS(aes_cfb_tv_template)
3838 },
3839 }, {
Martin Willi3590ebf2015-06-01 13:43:57 +02003840 .alg = "chacha20",
3841 .test = alg_test_skcipher,
3842 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003843 .cipher = __VECS(chacha20_tv_template)
3844 },
Martin Willi3590ebf2015-06-01 13:43:57 +02003845 }, {
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03003846 .alg = "cmac(aes)",
Stephan Mueller8f183752015-08-19 08:42:07 +02003847 .fips_allowed = 1,
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03003848 .test = alg_test_hash,
3849 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003850 .hash = __VECS(aes_cmac128_tv_template)
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03003851 }
3852 }, {
3853 .alg = "cmac(des3_ede)",
Stephan Mueller8f183752015-08-19 08:42:07 +02003854 .fips_allowed = 1,
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03003855 .test = alg_test_hash,
3856 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003857 .hash = __VECS(des3_ede_cmac64_tv_template)
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03003858 }
3859 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03003860 .alg = "compress_null",
3861 .test = alg_test_null,
3862 }, {
Ard Biesheuvelebb34722015-05-04 11:00:17 +02003863 .alg = "crc32",
3864 .test = alg_test_hash,
Milan Broza8a34412019-01-25 10:31:47 +01003865 .fips_allowed = 1,
Ard Biesheuvelebb34722015-05-04 11:00:17 +02003866 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003867 .hash = __VECS(crc32_tv_template)
Ard Biesheuvelebb34722015-05-04 11:00:17 +02003868 }
3869 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003870 .alg = "crc32c",
Herbert Xu8e3ee852008-11-07 14:58:52 +08003871 .test = alg_test_crc32c,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003872 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003873 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003874 .hash = __VECS(crc32c_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003875 }
3876 }, {
Herbert Xu684115212013-09-07 12:56:26 +10003877 .alg = "crct10dif",
3878 .test = alg_test_hash,
3879 .fips_allowed = 1,
3880 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003881 .hash = __VECS(crct10dif_tv_template)
Herbert Xu684115212013-09-07 12:56:26 +10003882 }
3883 }, {
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08003884 .alg = "ctr(aes)",
3885 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003886 .fips_allowed = 1,
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08003887 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003888 .cipher = __VECS(aes_ctr_tv_template)
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08003889 }
3890 }, {
Jussi Kivilinna85b63e32011-10-10 23:03:03 +03003891 .alg = "ctr(blowfish)",
3892 .test = alg_test_skcipher,
3893 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003894 .cipher = __VECS(bf_ctr_tv_template)
Jussi Kivilinna85b63e32011-10-10 23:03:03 +03003895 }
3896 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02003897 .alg = "ctr(camellia)",
3898 .test = alg_test_skcipher,
3899 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003900 .cipher = __VECS(camellia_ctr_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02003901 }
3902 }, {
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02003903 .alg = "ctr(cast5)",
3904 .test = alg_test_skcipher,
3905 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003906 .cipher = __VECS(cast5_ctr_tv_template)
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02003907 }
3908 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003909 .alg = "ctr(cast6)",
3910 .test = alg_test_skcipher,
3911 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003912 .cipher = __VECS(cast6_ctr_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003913 }
3914 }, {
Jussi Kivilinna8163fc32012-10-20 14:53:07 +03003915 .alg = "ctr(des)",
3916 .test = alg_test_skcipher,
3917 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003918 .cipher = __VECS(des_ctr_tv_template)
Jussi Kivilinna8163fc32012-10-20 14:53:07 +03003919 }
3920 }, {
Jussi Kivilinnae080b172012-10-20 14:53:12 +03003921 .alg = "ctr(des3_ede)",
3922 .test = alg_test_skcipher,
Marcelo Cerri0d8da102017-03-20 17:28:05 -03003923 .fips_allowed = 1,
Jussi Kivilinnae080b172012-10-20 14:53:12 +03003924 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003925 .cipher = __VECS(des3_ede_ctr_tv_template)
Jussi Kivilinnae080b172012-10-20 14:53:12 +03003926 }
3927 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01003928 /* Same as ctr(aes) except the key is stored in
3929 * hardware secure memory which we reference by index
3930 */
3931 .alg = "ctr(paes)",
3932 .test = alg_test_null,
3933 .fips_allowed = 1,
3934 }, {
Jussi Kivilinna9d259172011-10-18 00:02:53 +03003935 .alg = "ctr(serpent)",
3936 .test = alg_test_skcipher,
3937 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003938 .cipher = __VECS(serpent_ctr_tv_template)
Jussi Kivilinna9d259172011-10-18 00:02:53 +03003939 }
3940 }, {
Gilad Ben-Yossef95ba5972018-09-20 14:18:38 +01003941 .alg = "ctr(sm4)",
3942 .test = alg_test_skcipher,
3943 .suite = {
3944 .cipher = __VECS(sm4_ctr_tv_template)
3945 }
3946 }, {
Jussi Kivilinna573da622011-10-10 23:03:12 +03003947 .alg = "ctr(twofish)",
3948 .test = alg_test_skcipher,
3949 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003950 .cipher = __VECS(tf_ctr_tv_template)
Jussi Kivilinna573da622011-10-10 23:03:12 +03003951 }
3952 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003953 .alg = "cts(cbc(aes))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003954 .test = alg_test_skcipher,
Gilad Ben-Yossef196ad602018-11-04 10:05:24 +00003955 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003956 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003957 .cipher = __VECS(cts_mode_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003958 }
3959 }, {
3960 .alg = "deflate",
3961 .test = alg_test_comp,
Milan Broz08189042012-12-06 17:16:28 +08003962 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003963 .suite = {
3964 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003965 .comp = __VECS(deflate_comp_tv_template),
3966 .decomp = __VECS(deflate_decomp_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003967 }
3968 }
3969 }, {
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003970 .alg = "dh",
3971 .test = alg_test_kpp,
3972 .fips_allowed = 1,
3973 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003974 .kpp = __VECS(dh_tv_template)
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003975 }
3976 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03003977 .alg = "digest_null",
3978 .test = alg_test_null,
3979 }, {
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003980 .alg = "drbg_nopr_ctr_aes128",
3981 .test = alg_test_drbg,
3982 .fips_allowed = 1,
3983 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003984 .drbg = __VECS(drbg_nopr_ctr_aes128_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003985 }
3986 }, {
3987 .alg = "drbg_nopr_ctr_aes192",
3988 .test = alg_test_drbg,
3989 .fips_allowed = 1,
3990 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003991 .drbg = __VECS(drbg_nopr_ctr_aes192_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003992 }
3993 }, {
3994 .alg = "drbg_nopr_ctr_aes256",
3995 .test = alg_test_drbg,
3996 .fips_allowed = 1,
3997 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003998 .drbg = __VECS(drbg_nopr_ctr_aes256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003999 }
4000 }, {
4001 /*
4002 * There is no need to specifically test the DRBG with every
4003 * backend cipher -- covered by drbg_nopr_hmac_sha256 test
4004 */
4005 .alg = "drbg_nopr_hmac_sha1",
4006 .fips_allowed = 1,
4007 .test = alg_test_null,
4008 }, {
4009 .alg = "drbg_nopr_hmac_sha256",
4010 .test = alg_test_drbg,
4011 .fips_allowed = 1,
4012 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004013 .drbg = __VECS(drbg_nopr_hmac_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004014 }
4015 }, {
4016 /* covered by drbg_nopr_hmac_sha256 test */
4017 .alg = "drbg_nopr_hmac_sha384",
4018 .fips_allowed = 1,
4019 .test = alg_test_null,
4020 }, {
4021 .alg = "drbg_nopr_hmac_sha512",
4022 .test = alg_test_null,
4023 .fips_allowed = 1,
4024 }, {
4025 .alg = "drbg_nopr_sha1",
4026 .fips_allowed = 1,
4027 .test = alg_test_null,
4028 }, {
4029 .alg = "drbg_nopr_sha256",
4030 .test = alg_test_drbg,
4031 .fips_allowed = 1,
4032 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004033 .drbg = __VECS(drbg_nopr_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004034 }
4035 }, {
4036 /* covered by drbg_nopr_sha256 test */
4037 .alg = "drbg_nopr_sha384",
4038 .fips_allowed = 1,
4039 .test = alg_test_null,
4040 }, {
4041 .alg = "drbg_nopr_sha512",
4042 .fips_allowed = 1,
4043 .test = alg_test_null,
4044 }, {
4045 .alg = "drbg_pr_ctr_aes128",
4046 .test = alg_test_drbg,
4047 .fips_allowed = 1,
4048 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004049 .drbg = __VECS(drbg_pr_ctr_aes128_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004050 }
4051 }, {
4052 /* covered by drbg_pr_ctr_aes128 test */
4053 .alg = "drbg_pr_ctr_aes192",
4054 .fips_allowed = 1,
4055 .test = alg_test_null,
4056 }, {
4057 .alg = "drbg_pr_ctr_aes256",
4058 .fips_allowed = 1,
4059 .test = alg_test_null,
4060 }, {
4061 .alg = "drbg_pr_hmac_sha1",
4062 .fips_allowed = 1,
4063 .test = alg_test_null,
4064 }, {
4065 .alg = "drbg_pr_hmac_sha256",
4066 .test = alg_test_drbg,
4067 .fips_allowed = 1,
4068 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004069 .drbg = __VECS(drbg_pr_hmac_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004070 }
4071 }, {
4072 /* covered by drbg_pr_hmac_sha256 test */
4073 .alg = "drbg_pr_hmac_sha384",
4074 .fips_allowed = 1,
4075 .test = alg_test_null,
4076 }, {
4077 .alg = "drbg_pr_hmac_sha512",
4078 .test = alg_test_null,
4079 .fips_allowed = 1,
4080 }, {
4081 .alg = "drbg_pr_sha1",
4082 .fips_allowed = 1,
4083 .test = alg_test_null,
4084 }, {
4085 .alg = "drbg_pr_sha256",
4086 .test = alg_test_drbg,
4087 .fips_allowed = 1,
4088 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004089 .drbg = __VECS(drbg_pr_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004090 }
4091 }, {
4092 /* covered by drbg_pr_sha256 test */
4093 .alg = "drbg_pr_sha384",
4094 .fips_allowed = 1,
4095 .test = alg_test_null,
4096 }, {
4097 .alg = "drbg_pr_sha512",
4098 .fips_allowed = 1,
4099 .test = alg_test_null,
4100 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004101 .alg = "ecb(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004102 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004103 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004104 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004105 .cipher = __VECS(aes_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004106 }
4107 }, {
4108 .alg = "ecb(anubis)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004109 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004110 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004111 .cipher = __VECS(anubis_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004112 }
4113 }, {
4114 .alg = "ecb(arc4)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004115 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004116 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004117 .cipher = __VECS(arc4_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004118 }
4119 }, {
4120 .alg = "ecb(blowfish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004121 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004122 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004123 .cipher = __VECS(bf_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004124 }
4125 }, {
4126 .alg = "ecb(camellia)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004127 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004128 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004129 .cipher = __VECS(camellia_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004130 }
4131 }, {
4132 .alg = "ecb(cast5)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004133 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004134 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004135 .cipher = __VECS(cast5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004136 }
4137 }, {
4138 .alg = "ecb(cast6)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004139 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004140 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004141 .cipher = __VECS(cast6_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004142 }
4143 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03004144 .alg = "ecb(cipher_null)",
4145 .test = alg_test_null,
Milan Broz6175ca22017-04-21 13:03:06 +02004146 .fips_allowed = 1,
Jussi Kivilinnae4483702013-04-07 16:43:56 +03004147 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004148 .alg = "ecb(des)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004149 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004150 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004151 .cipher = __VECS(des_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004152 }
4153 }, {
4154 .alg = "ecb(des3_ede)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004155 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004156 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004157 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004158 .cipher = __VECS(des3_ede_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004159 }
4160 }, {
Jussi Kivilinna66e5bd02013-01-19 13:31:36 +02004161 .alg = "ecb(fcrypt)",
4162 .test = alg_test_skcipher,
4163 .suite = {
4164 .cipher = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004165 .vecs = fcrypt_pcbc_tv_template,
4166 .count = 1
Jussi Kivilinna66e5bd02013-01-19 13:31:36 +02004167 }
4168 }
4169 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004170 .alg = "ecb(khazad)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004171 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004172 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004173 .cipher = __VECS(khazad_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004174 }
4175 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01004176 /* Same as ecb(aes) except the key is stored in
4177 * hardware secure memory which we reference by index
4178 */
4179 .alg = "ecb(paes)",
4180 .test = alg_test_null,
4181 .fips_allowed = 1,
4182 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004183 .alg = "ecb(seed)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004184 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004185 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004186 .cipher = __VECS(seed_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004187 }
4188 }, {
4189 .alg = "ecb(serpent)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004190 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004191 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004192 .cipher = __VECS(serpent_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004193 }
4194 }, {
Gilad Ben-Yossefcd83a8a2018-03-06 09:44:43 +00004195 .alg = "ecb(sm4)",
4196 .test = alg_test_skcipher,
4197 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004198 .cipher = __VECS(sm4_tv_template)
Gilad Ben-Yossefcd83a8a2018-03-06 09:44:43 +00004199 }
4200 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004201 .alg = "ecb(tea)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004202 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004203 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004204 .cipher = __VECS(tea_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004205 }
4206 }, {
4207 .alg = "ecb(tnepres)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004208 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004209 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004210 .cipher = __VECS(tnepres_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004211 }
4212 }, {
4213 .alg = "ecb(twofish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004214 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004215 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004216 .cipher = __VECS(tf_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004217 }
4218 }, {
4219 .alg = "ecb(xeta)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004220 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004221 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004222 .cipher = __VECS(xeta_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004223 }
4224 }, {
4225 .alg = "ecb(xtea)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004226 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004227 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004228 .cipher = __VECS(xtea_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004229 }
4230 }, {
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +01004231 .alg = "ecdh",
4232 .test = alg_test_kpp,
4233 .fips_allowed = 1,
4234 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004235 .kpp = __VECS(ecdh_tv_template)
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +01004236 }
4237 }, {
Vitaly Chikunov32fbdbd2019-04-11 18:51:21 +03004238 .alg = "ecrdsa",
4239 .test = alg_test_akcipher,
4240 .suite = {
4241 .akcipher = __VECS(ecrdsa_tv_template)
4242 }
4243 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004244 .alg = "gcm(aes)",
Eric Biggers40153b12019-04-11 21:57:41 -07004245 .generic_driver = "gcm_base(ctr(aes-generic),ghash-generic)",
Herbert Xuda7f0332008-07-31 17:08:25 +08004246 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004247 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004248 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004249 .aead = __VECS(aes_gcm_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004250 }
4251 }, {
Youquan, Song507069c2009-11-23 20:23:04 +08004252 .alg = "ghash",
4253 .test = alg_test_hash,
Jarod Wilson18c0ebd2011-01-29 15:14:35 +11004254 .fips_allowed = 1,
Youquan, Song507069c2009-11-23 20:23:04 +08004255 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004256 .hash = __VECS(ghash_tv_template)
Youquan, Song507069c2009-11-23 20:23:04 +08004257 }
4258 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004259 .alg = "hmac(md5)",
4260 .test = alg_test_hash,
4261 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004262 .hash = __VECS(hmac_md5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004263 }
4264 }, {
4265 .alg = "hmac(rmd128)",
4266 .test = alg_test_hash,
4267 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004268 .hash = __VECS(hmac_rmd128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004269 }
4270 }, {
4271 .alg = "hmac(rmd160)",
4272 .test = alg_test_hash,
4273 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004274 .hash = __VECS(hmac_rmd160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004275 }
4276 }, {
4277 .alg = "hmac(sha1)",
4278 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004279 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004280 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004281 .hash = __VECS(hmac_sha1_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004282 }
4283 }, {
4284 .alg = "hmac(sha224)",
4285 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004286 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004287 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004288 .hash = __VECS(hmac_sha224_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004289 }
4290 }, {
4291 .alg = "hmac(sha256)",
4292 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004293 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004294 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004295 .hash = __VECS(hmac_sha256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004296 }
4297 }, {
raveendra padasalagi98eca722016-07-01 11:16:54 +05304298 .alg = "hmac(sha3-224)",
4299 .test = alg_test_hash,
4300 .fips_allowed = 1,
4301 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004302 .hash = __VECS(hmac_sha3_224_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05304303 }
4304 }, {
4305 .alg = "hmac(sha3-256)",
4306 .test = alg_test_hash,
4307 .fips_allowed = 1,
4308 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004309 .hash = __VECS(hmac_sha3_256_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05304310 }
4311 }, {
4312 .alg = "hmac(sha3-384)",
4313 .test = alg_test_hash,
4314 .fips_allowed = 1,
4315 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004316 .hash = __VECS(hmac_sha3_384_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05304317 }
4318 }, {
4319 .alg = "hmac(sha3-512)",
4320 .test = alg_test_hash,
4321 .fips_allowed = 1,
4322 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004323 .hash = __VECS(hmac_sha3_512_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05304324 }
4325 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004326 .alg = "hmac(sha384)",
4327 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004328 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004329 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004330 .hash = __VECS(hmac_sha384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004331 }
4332 }, {
4333 .alg = "hmac(sha512)",
4334 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004335 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004336 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004337 .hash = __VECS(hmac_sha512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004338 }
4339 }, {
Vitaly Chikunov25a0b9d2018-11-07 00:00:03 +03004340 .alg = "hmac(streebog256)",
4341 .test = alg_test_hash,
4342 .suite = {
4343 .hash = __VECS(hmac_streebog256_tv_template)
4344 }
4345 }, {
4346 .alg = "hmac(streebog512)",
4347 .test = alg_test_hash,
4348 .suite = {
4349 .hash = __VECS(hmac_streebog512_tv_template)
4350 }
4351 }, {
Stephan Muellerbb5530e2015-05-25 15:10:20 +02004352 .alg = "jitterentropy_rng",
4353 .fips_allowed = 1,
4354 .test = alg_test_null,
4355 }, {
Stephan Mueller35351982015-09-21 20:59:56 +02004356 .alg = "kw(aes)",
4357 .test = alg_test_skcipher,
4358 .fips_allowed = 1,
4359 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004360 .cipher = __VECS(aes_kw_tv_template)
Stephan Mueller35351982015-09-21 20:59:56 +02004361 }
4362 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004363 .alg = "lrw(aes)",
Eric Biggersd435e102019-04-11 21:57:40 -07004364 .generic_driver = "lrw(ecb(aes-generic))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004365 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004366 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004367 .cipher = __VECS(aes_lrw_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004368 }
4369 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02004370 .alg = "lrw(camellia)",
Eric Biggersd435e102019-04-11 21:57:40 -07004371 .generic_driver = "lrw(ecb(camellia-generic))",
Jussi Kivilinna08406052012-03-05 20:26:21 +02004372 .test = alg_test_skcipher,
4373 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004374 .cipher = __VECS(camellia_lrw_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02004375 }
4376 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004377 .alg = "lrw(cast6)",
Eric Biggersd435e102019-04-11 21:57:40 -07004378 .generic_driver = "lrw(ecb(cast6-generic))",
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004379 .test = alg_test_skcipher,
4380 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004381 .cipher = __VECS(cast6_lrw_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004382 }
4383 }, {
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03004384 .alg = "lrw(serpent)",
Eric Biggersd435e102019-04-11 21:57:40 -07004385 .generic_driver = "lrw(ecb(serpent-generic))",
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03004386 .test = alg_test_skcipher,
4387 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004388 .cipher = __VECS(serpent_lrw_tv_template)
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03004389 }
4390 }, {
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03004391 .alg = "lrw(twofish)",
Eric Biggersd435e102019-04-11 21:57:40 -07004392 .generic_driver = "lrw(ecb(twofish-generic))",
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03004393 .test = alg_test_skcipher,
4394 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004395 .cipher = __VECS(tf_lrw_tv_template)
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03004396 }
4397 }, {
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02004398 .alg = "lz4",
4399 .test = alg_test_comp,
4400 .fips_allowed = 1,
4401 .suite = {
4402 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004403 .comp = __VECS(lz4_comp_tv_template),
4404 .decomp = __VECS(lz4_decomp_tv_template)
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02004405 }
4406 }
4407 }, {
4408 .alg = "lz4hc",
4409 .test = alg_test_comp,
4410 .fips_allowed = 1,
4411 .suite = {
4412 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004413 .comp = __VECS(lz4hc_comp_tv_template),
4414 .decomp = __VECS(lz4hc_decomp_tv_template)
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02004415 }
4416 }
4417 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004418 .alg = "lzo",
4419 .test = alg_test_comp,
Milan Broz08189042012-12-06 17:16:28 +08004420 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004421 .suite = {
4422 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004423 .comp = __VECS(lzo_comp_tv_template),
4424 .decomp = __VECS(lzo_decomp_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004425 }
4426 }
4427 }, {
4428 .alg = "md4",
4429 .test = alg_test_hash,
4430 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004431 .hash = __VECS(md4_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004432 }
4433 }, {
4434 .alg = "md5",
4435 .test = alg_test_hash,
4436 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004437 .hash = __VECS(md5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004438 }
4439 }, {
4440 .alg = "michael_mic",
4441 .test = alg_test_hash,
4442 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004443 .hash = __VECS(michael_mic_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004444 }
4445 }, {
Ondrej Mosnacek4feb4c52018-05-11 14:19:10 +02004446 .alg = "morus1280",
4447 .test = alg_test_aead,
4448 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004449 .aead = __VECS(morus1280_tv_template)
Ondrej Mosnacek4feb4c52018-05-11 14:19:10 +02004450 }
4451 }, {
4452 .alg = "morus640",
4453 .test = alg_test_aead,
4454 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004455 .aead = __VECS(morus640_tv_template)
Ondrej Mosnacek4feb4c52018-05-11 14:19:10 +02004456 }
4457 }, {
Eric Biggers26609a22018-11-16 17:26:29 -08004458 .alg = "nhpoly1305",
4459 .test = alg_test_hash,
4460 .suite = {
4461 .hash = __VECS(nhpoly1305_tv_template)
4462 }
4463 }, {
Puneet Saxenaba0e14a2011-05-04 15:04:10 +10004464 .alg = "ofb(aes)",
4465 .test = alg_test_skcipher,
4466 .fips_allowed = 1,
4467 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004468 .cipher = __VECS(aes_ofb_tv_template)
Puneet Saxenaba0e14a2011-05-04 15:04:10 +10004469 }
4470 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01004471 /* Same as ofb(aes) except the key is stored in
4472 * hardware secure memory which we reference by index
4473 */
4474 .alg = "ofb(paes)",
4475 .test = alg_test_null,
4476 .fips_allowed = 1,
4477 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004478 .alg = "pcbc(fcrypt)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004479 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004480 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004481 .cipher = __VECS(fcrypt_pcbc_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004482 }
4483 }, {
Stephan Mueller12071072017-06-12 23:27:51 +02004484 .alg = "pkcs1pad(rsa,sha224)",
4485 .test = alg_test_null,
4486 .fips_allowed = 1,
4487 }, {
4488 .alg = "pkcs1pad(rsa,sha256)",
4489 .test = alg_test_akcipher,
4490 .fips_allowed = 1,
4491 .suite = {
4492 .akcipher = __VECS(pkcs1pad_rsa_tv_template)
4493 }
4494 }, {
4495 .alg = "pkcs1pad(rsa,sha384)",
4496 .test = alg_test_null,
4497 .fips_allowed = 1,
4498 }, {
4499 .alg = "pkcs1pad(rsa,sha512)",
4500 .test = alg_test_null,
4501 .fips_allowed = 1,
4502 }, {
Martin Willieee9dc62015-06-01 13:43:59 +02004503 .alg = "poly1305",
4504 .test = alg_test_hash,
4505 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004506 .hash = __VECS(poly1305_tv_template)
Martin Willieee9dc62015-06-01 13:43:59 +02004507 }
4508 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004509 .alg = "rfc3686(ctr(aes))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004510 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004511 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004512 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004513 .cipher = __VECS(aes_ctr_rfc3686_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004514 }
4515 }, {
Herbert Xu3f31a742015-07-09 07:17:34 +08004516 .alg = "rfc4106(gcm(aes))",
Eric Biggers40153b12019-04-11 21:57:41 -07004517 .generic_driver = "rfc4106(gcm_base(ctr(aes-generic),ghash-generic))",
Adrian Hoban69435b92010-11-04 15:02:04 -04004518 .test = alg_test_aead,
Jarod Wilsondb71f29a2015-01-23 12:42:15 -05004519 .fips_allowed = 1,
Adrian Hoban69435b92010-11-04 15:02:04 -04004520 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004521 .aead = __VECS(aes_gcm_rfc4106_tv_template)
Adrian Hoban69435b92010-11-04 15:02:04 -04004522 }
4523 }, {
Herbert Xu544c4362015-07-14 16:53:22 +08004524 .alg = "rfc4309(ccm(aes))",
Eric Biggers40153b12019-04-11 21:57:41 -07004525 .generic_driver = "rfc4309(ccm_base(ctr(aes-generic),cbcmac(aes-generic)))",
Jarod Wilson5d667322009-05-04 19:23:40 +08004526 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004527 .fips_allowed = 1,
Jarod Wilson5d667322009-05-04 19:23:40 +08004528 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004529 .aead = __VECS(aes_ccm_rfc4309_tv_template)
Jarod Wilson5d667322009-05-04 19:23:40 +08004530 }
4531 }, {
Herbert Xubb687452015-06-16 13:54:24 +08004532 .alg = "rfc4543(gcm(aes))",
Eric Biggers40153b12019-04-11 21:57:41 -07004533 .generic_driver = "rfc4543(gcm_base(ctr(aes-generic),ghash-generic))",
Jussi Kivilinnae9b74412013-04-07 16:43:51 +03004534 .test = alg_test_aead,
4535 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004536 .aead = __VECS(aes_gcm_rfc4543_tv_template)
Jussi Kivilinnae9b74412013-04-07 16:43:51 +03004537 }
4538 }, {
Martin Williaf2b76b2015-06-01 13:44:01 +02004539 .alg = "rfc7539(chacha20,poly1305)",
4540 .test = alg_test_aead,
4541 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004542 .aead = __VECS(rfc7539_tv_template)
Martin Williaf2b76b2015-06-01 13:44:01 +02004543 }
4544 }, {
Martin Willi59007582015-06-01 13:44:03 +02004545 .alg = "rfc7539esp(chacha20,poly1305)",
4546 .test = alg_test_aead,
4547 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004548 .aead = __VECS(rfc7539esp_tv_template)
Martin Willi59007582015-06-01 13:44:03 +02004549 }
4550 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004551 .alg = "rmd128",
4552 .test = alg_test_hash,
4553 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004554 .hash = __VECS(rmd128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004555 }
4556 }, {
4557 .alg = "rmd160",
4558 .test = alg_test_hash,
4559 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004560 .hash = __VECS(rmd160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004561 }
4562 }, {
4563 .alg = "rmd256",
4564 .test = alg_test_hash,
4565 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004566 .hash = __VECS(rmd256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004567 }
4568 }, {
4569 .alg = "rmd320",
4570 .test = alg_test_hash,
4571 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004572 .hash = __VECS(rmd320_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004573 }
4574 }, {
Tadeusz Struk946cc462015-06-16 10:31:06 -07004575 .alg = "rsa",
4576 .test = alg_test_akcipher,
4577 .fips_allowed = 1,
4578 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004579 .akcipher = __VECS(rsa_tv_template)
Tadeusz Struk946cc462015-06-16 10:31:06 -07004580 }
4581 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004582 .alg = "salsa20",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004583 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004584 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004585 .cipher = __VECS(salsa20_stream_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004586 }
4587 }, {
4588 .alg = "sha1",
4589 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004590 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004591 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004592 .hash = __VECS(sha1_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004593 }
4594 }, {
4595 .alg = "sha224",
4596 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004597 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004598 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004599 .hash = __VECS(sha224_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004600 }
4601 }, {
4602 .alg = "sha256",
4603 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004604 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004605 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004606 .hash = __VECS(sha256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004607 }
4608 }, {
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05304609 .alg = "sha3-224",
4610 .test = alg_test_hash,
4611 .fips_allowed = 1,
4612 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004613 .hash = __VECS(sha3_224_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05304614 }
4615 }, {
4616 .alg = "sha3-256",
4617 .test = alg_test_hash,
4618 .fips_allowed = 1,
4619 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004620 .hash = __VECS(sha3_256_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05304621 }
4622 }, {
4623 .alg = "sha3-384",
4624 .test = alg_test_hash,
4625 .fips_allowed = 1,
4626 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004627 .hash = __VECS(sha3_384_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05304628 }
4629 }, {
4630 .alg = "sha3-512",
4631 .test = alg_test_hash,
4632 .fips_allowed = 1,
4633 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004634 .hash = __VECS(sha3_512_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05304635 }
4636 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004637 .alg = "sha384",
4638 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004639 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004640 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004641 .hash = __VECS(sha384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004642 }
4643 }, {
4644 .alg = "sha512",
4645 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004646 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004647 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004648 .hash = __VECS(sha512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004649 }
4650 }, {
Gilad Ben-Yossefb7e27532017-08-21 13:51:29 +03004651 .alg = "sm3",
4652 .test = alg_test_hash,
4653 .suite = {
4654 .hash = __VECS(sm3_tv_template)
4655 }
4656 }, {
Vitaly Chikunov25a0b9d2018-11-07 00:00:03 +03004657 .alg = "streebog256",
4658 .test = alg_test_hash,
4659 .suite = {
4660 .hash = __VECS(streebog256_tv_template)
4661 }
4662 }, {
4663 .alg = "streebog512",
4664 .test = alg_test_hash,
4665 .suite = {
4666 .hash = __VECS(streebog512_tv_template)
4667 }
4668 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004669 .alg = "tgr128",
4670 .test = alg_test_hash,
4671 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004672 .hash = __VECS(tgr128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004673 }
4674 }, {
4675 .alg = "tgr160",
4676 .test = alg_test_hash,
4677 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004678 .hash = __VECS(tgr160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004679 }
4680 }, {
4681 .alg = "tgr192",
4682 .test = alg_test_hash,
4683 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004684 .hash = __VECS(tgr192_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004685 }
4686 }, {
Eric Biggersed331ad2018-06-18 10:22:39 -07004687 .alg = "vmac64(aes)",
4688 .test = alg_test_hash,
4689 .suite = {
4690 .hash = __VECS(vmac64_aes_tv_template)
4691 }
4692 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004693 .alg = "wp256",
4694 .test = alg_test_hash,
4695 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004696 .hash = __VECS(wp256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004697 }
4698 }, {
4699 .alg = "wp384",
4700 .test = alg_test_hash,
4701 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004702 .hash = __VECS(wp384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004703 }
4704 }, {
4705 .alg = "wp512",
4706 .test = alg_test_hash,
4707 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004708 .hash = __VECS(wp512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004709 }
4710 }, {
4711 .alg = "xcbc(aes)",
4712 .test = alg_test_hash,
4713 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004714 .hash = __VECS(aes_xcbc128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004715 }
4716 }, {
Eric Biggersaa762402018-11-16 17:26:22 -08004717 .alg = "xchacha12",
4718 .test = alg_test_skcipher,
4719 .suite = {
4720 .cipher = __VECS(xchacha12_tv_template)
4721 },
4722 }, {
Eric Biggersde61d7a2018-11-16 17:26:20 -08004723 .alg = "xchacha20",
4724 .test = alg_test_skcipher,
4725 .suite = {
4726 .cipher = __VECS(xchacha20_tv_template)
4727 },
4728 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004729 .alg = "xts(aes)",
Eric Biggersd435e102019-04-11 21:57:40 -07004730 .generic_driver = "xts(ecb(aes-generic))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004731 .test = alg_test_skcipher,
Jarod Wilson2918aa82011-01-29 15:14:01 +11004732 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004733 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004734 .cipher = __VECS(aes_xts_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004735 }
Geert Uytterhoeven0c01aed2009-03-04 15:42:15 +08004736 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02004737 .alg = "xts(camellia)",
Eric Biggersd435e102019-04-11 21:57:40 -07004738 .generic_driver = "xts(ecb(camellia-generic))",
Jussi Kivilinna08406052012-03-05 20:26:21 +02004739 .test = alg_test_skcipher,
4740 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004741 .cipher = __VECS(camellia_xts_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02004742 }
4743 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004744 .alg = "xts(cast6)",
Eric Biggersd435e102019-04-11 21:57:40 -07004745 .generic_driver = "xts(ecb(cast6-generic))",
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004746 .test = alg_test_skcipher,
4747 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004748 .cipher = __VECS(cast6_xts_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004749 }
4750 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01004751 /* Same as xts(aes) except the key is stored in
4752 * hardware secure memory which we reference by index
4753 */
4754 .alg = "xts(paes)",
4755 .test = alg_test_null,
4756 .fips_allowed = 1,
4757 }, {
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03004758 .alg = "xts(serpent)",
Eric Biggersd435e102019-04-11 21:57:40 -07004759 .generic_driver = "xts(ecb(serpent-generic))",
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03004760 .test = alg_test_skcipher,
4761 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004762 .cipher = __VECS(serpent_xts_tv_template)
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03004763 }
4764 }, {
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03004765 .alg = "xts(twofish)",
Eric Biggersd435e102019-04-11 21:57:40 -07004766 .generic_driver = "xts(ecb(twofish-generic))",
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03004767 .test = alg_test_skcipher,
4768 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004769 .cipher = __VECS(tf_xts_tv_template)
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03004770 }
Giovanni Cabiddua368f432017-04-21 21:54:30 +01004771 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01004772 .alg = "xts4096(paes)",
4773 .test = alg_test_null,
4774 .fips_allowed = 1,
4775 }, {
4776 .alg = "xts512(paes)",
4777 .test = alg_test_null,
4778 .fips_allowed = 1,
4779 }, {
Giovanni Cabiddua368f432017-04-21 21:54:30 +01004780 .alg = "zlib-deflate",
4781 .test = alg_test_comp,
4782 .fips_allowed = 1,
4783 .suite = {
4784 .comp = {
4785 .comp = __VECS(zlib_deflate_comp_tv_template),
4786 .decomp = __VECS(zlib_deflate_decomp_tv_template)
4787 }
4788 }
Nick Terrelld28fc3d2018-03-30 12:14:53 -07004789 }, {
4790 .alg = "zstd",
4791 .test = alg_test_comp,
4792 .fips_allowed = 1,
4793 .suite = {
4794 .comp = {
4795 .comp = __VECS(zstd_comp_tv_template),
4796 .decomp = __VECS(zstd_decomp_tv_template)
4797 }
4798 }
Herbert Xuda7f0332008-07-31 17:08:25 +08004799 }
4800};
4801
Eric Biggers3f47a032019-01-31 23:51:43 -08004802static void alg_check_test_descs_order(void)
Jussi Kivilinna57147582013-06-13 17:37:40 +03004803{
4804 int i;
4805
Jussi Kivilinna57147582013-06-13 17:37:40 +03004806 for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
4807 int diff = strcmp(alg_test_descs[i - 1].alg,
4808 alg_test_descs[i].alg);
4809
4810 if (WARN_ON(diff > 0)) {
4811 pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
4812 alg_test_descs[i - 1].alg,
4813 alg_test_descs[i].alg);
4814 }
4815
4816 if (WARN_ON(diff == 0)) {
4817 pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
4818 alg_test_descs[i].alg);
4819 }
4820 }
4821}
4822
Eric Biggers3f47a032019-01-31 23:51:43 -08004823static void alg_check_testvec_configs(void)
4824{
Eric Biggers4e7babba2019-01-31 23:51:46 -08004825 int i;
4826
4827 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++)
4828 WARN_ON(!valid_testvec_config(
4829 &default_cipher_testvec_configs[i]));
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08004830
4831 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++)
4832 WARN_ON(!valid_testvec_config(
4833 &default_hash_testvec_configs[i]));
Eric Biggers3f47a032019-01-31 23:51:43 -08004834}
4835
4836static void testmgr_onetime_init(void)
4837{
4838 alg_check_test_descs_order();
4839 alg_check_testvec_configs();
Eric Biggers5b2706a2019-01-31 23:51:44 -08004840
4841#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
4842 pr_warn("alg: extra crypto tests enabled. This is intended for developer use only.\n");
4843#endif
Eric Biggers3f47a032019-01-31 23:51:43 -08004844}
4845
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004846static int alg_find_test(const char *alg)
Herbert Xuda7f0332008-07-31 17:08:25 +08004847{
4848 int start = 0;
4849 int end = ARRAY_SIZE(alg_test_descs);
4850
4851 while (start < end) {
4852 int i = (start + end) / 2;
4853 int diff = strcmp(alg_test_descs[i].alg, alg);
4854
4855 if (diff > 0) {
4856 end = i;
4857 continue;
4858 }
4859
4860 if (diff < 0) {
4861 start = i + 1;
4862 continue;
4863 }
4864
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004865 return i;
Herbert Xuda7f0332008-07-31 17:08:25 +08004866 }
4867
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004868 return -1;
4869}
4870
4871int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
4872{
4873 int i;
Herbert Xua68f6612009-07-02 16:32:12 +08004874 int j;
Neil Hormand12d6b62008-10-12 20:36:51 +08004875 int rc;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004876
Richard W.M. Jones9e5c9fe2016-05-03 10:00:17 +01004877 if (!fips_enabled && notests) {
4878 printk_once(KERN_INFO "alg: self-tests disabled\n");
4879 return 0;
4880 }
4881
Eric Biggers3f47a032019-01-31 23:51:43 -08004882 DO_ONCE(testmgr_onetime_init);
Jussi Kivilinna57147582013-06-13 17:37:40 +03004883
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004884 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
4885 char nalg[CRYPTO_MAX_ALG_NAME];
4886
4887 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
4888 sizeof(nalg))
4889 return -ENAMETOOLONG;
4890
4891 i = alg_find_test(nalg);
4892 if (i < 0)
4893 goto notest;
4894
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10004895 if (fips_enabled && !alg_test_descs[i].fips_allowed)
4896 goto non_fips_alg;
4897
Jarod Wilson941fb322009-05-04 19:49:23 +08004898 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
4899 goto test_done;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004900 }
4901
4902 i = alg_find_test(alg);
Herbert Xua68f6612009-07-02 16:32:12 +08004903 j = alg_find_test(driver);
4904 if (i < 0 && j < 0)
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004905 goto notest;
4906
Herbert Xua68f6612009-07-02 16:32:12 +08004907 if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
4908 (j >= 0 && !alg_test_descs[j].fips_allowed)))
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10004909 goto non_fips_alg;
4910
Herbert Xua68f6612009-07-02 16:32:12 +08004911 rc = 0;
4912 if (i >= 0)
4913 rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
4914 type, mask);
Cristian Stoica032c8ca2013-07-18 18:57:07 +03004915 if (j >= 0 && j != i)
Herbert Xua68f6612009-07-02 16:32:12 +08004916 rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
4917 type, mask);
4918
Jarod Wilson941fb322009-05-04 19:49:23 +08004919test_done:
Eric Biggerseda69b02019-03-31 13:09:14 -07004920 if (rc && (fips_enabled || panic_on_fail))
4921 panic("alg: self-tests for %s (%s) failed in %s mode!\n",
4922 driver, alg, fips_enabled ? "fips" : "panic_on_fail");
Neil Hormand12d6b62008-10-12 20:36:51 +08004923
Jarod Wilson29ecd4a2009-05-04 19:51:17 +08004924 if (fips_enabled && !rc)
Masanari Iida3e8cffd2014-10-07 00:37:54 +09004925 pr_info("alg: self-tests for %s (%s) passed\n", driver, alg);
Jarod Wilson29ecd4a2009-05-04 19:51:17 +08004926
Neil Hormand12d6b62008-10-12 20:36:51 +08004927 return rc;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004928
4929notest:
Herbert Xuda7f0332008-07-31 17:08:25 +08004930 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
4931 return 0;
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10004932non_fips_alg:
4933 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08004934}
Alexander Shishkin0b767f92010-06-03 20:53:43 +10004935
Herbert Xu326a6342010-08-06 09:40:28 +08004936#endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
Alexander Shishkin0b767f92010-06-03 20:53:43 +10004937
Herbert Xuda7f0332008-07-31 17:08:25 +08004938EXPORT_SYMBOL_GPL(alg_test);