blob: d1ffa8f73948862dffe8c4b62e135be468576094 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Herbert Xuda7f0332008-07-31 17:08:25 +08002/*
3 * Algorithm testing framework and tests.
4 *
5 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
6 * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
7 * Copyright (c) 2007 Nokia Siemens Networks
8 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
Eric Biggers3f47a032019-01-31 23:51:43 -08009 * Copyright (c) 2019 Google LLC
Herbert Xuda7f0332008-07-31 17:08:25 +080010 *
Adrian Hoban69435b92010-11-04 15:02:04 -040011 * Updated RFC4106 AES-GCM testing.
12 * Authors: Aidan O'Mahony (aidan.o.mahony@intel.com)
13 * Adrian Hoban <adrian.hoban@intel.com>
14 * Gabriele Paoloni <gabriele.paoloni@intel.com>
15 * Tadeusz Struk (tadeusz.struk@intel.com)
16 * Copyright (c) 2010, Intel Corporation.
Herbert Xuda7f0332008-07-31 17:08:25 +080017 */
18
Herbert Xu1ce33112015-04-22 15:06:31 +080019#include <crypto/aead.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080020#include <crypto/hash.h>
Herbert Xu12773d92015-08-20 15:21:46 +080021#include <crypto/skcipher.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080022#include <linux/err.h>
Herbert Xu1c41b882015-04-22 13:25:58 +080023#include <linux/fips.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080024#include <linux/module.h>
Eric Biggers3f47a032019-01-31 23:51:43 -080025#include <linux/once.h>
Eric Biggers25f9ddd2019-01-31 23:51:45 -080026#include <linux/random.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080027#include <linux/scatterlist.h>
28#include <linux/slab.h>
29#include <linux/string.h>
Jarod Wilson7647d6c2009-05-04 19:44:50 +080030#include <crypto/rng.h>
Stephan Mueller64d1cdf2014-05-31 17:25:36 +020031#include <crypto/drbg.h>
Tadeusz Struk946cc462015-06-16 10:31:06 -070032#include <crypto/akcipher.h>
Salvatore Benedetto802c7f12016-06-22 17:49:14 +010033#include <crypto/kpp.h>
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +010034#include <crypto/acompress.h>
Eric Biggersb55e1a32019-03-12 22:12:47 -070035#include <crypto/internal/simd.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080036
37#include "internal.h"
Alexander Shishkin0b767f92010-06-03 20:53:43 +100038
Richard W.M. Jones9e5c9fe2016-05-03 10:00:17 +010039static bool notests;
40module_param(notests, bool, 0644);
41MODULE_PARM_DESC(notests, "disable crypto self-tests");
42
Eric Biggerseda69b02019-03-31 13:09:14 -070043static bool panic_on_fail;
44module_param(panic_on_fail, bool, 0444);
45
Eric Biggers5b2706a2019-01-31 23:51:44 -080046#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
47static bool noextratests;
48module_param(noextratests, bool, 0644);
49MODULE_PARM_DESC(noextratests, "disable expensive crypto self-tests");
50
51static unsigned int fuzz_iterations = 100;
52module_param(fuzz_iterations, uint, 0644);
53MODULE_PARM_DESC(fuzz_iterations, "number of fuzz test iterations");
Eric Biggersb55e1a32019-03-12 22:12:47 -070054
55DEFINE_PER_CPU(bool, crypto_simd_disabled_for_test);
56EXPORT_PER_CPU_SYMBOL_GPL(crypto_simd_disabled_for_test);
Eric Biggers5b2706a2019-01-31 23:51:44 -080057#endif
58
Herbert Xu326a6342010-08-06 09:40:28 +080059#ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
Alexander Shishkin0b767f92010-06-03 20:53:43 +100060
61/* a perfect nop */
62int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
63{
64 return 0;
65}
66
67#else
68
Herbert Xuda7f0332008-07-31 17:08:25 +080069#include "testmgr.h"
70
71/*
72 * Need slab memory for testing (size in number of pages).
73 */
74#define XBUFSIZE 8
75
76/*
Herbert Xuda7f0332008-07-31 17:08:25 +080077* Used by test_cipher()
78*/
79#define ENCRYPT 1
80#define DECRYPT 0
81
Herbert Xuda7f0332008-07-31 17:08:25 +080082struct aead_test_suite {
Eric Biggersa0d608ee2019-01-13 15:32:28 -080083 const struct aead_testvec *vecs;
84 unsigned int count;
Herbert Xuda7f0332008-07-31 17:08:25 +080085};
86
87struct cipher_test_suite {
Eric Biggers92a4c9f2018-05-20 22:50:29 -070088 const struct cipher_testvec *vecs;
89 unsigned int count;
Herbert Xuda7f0332008-07-31 17:08:25 +080090};
91
92struct comp_test_suite {
93 struct {
Eric Biggersb13b1e02017-02-24 15:46:59 -080094 const struct comp_testvec *vecs;
Herbert Xuda7f0332008-07-31 17:08:25 +080095 unsigned int count;
96 } comp, decomp;
97};
98
99struct hash_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800100 const struct hash_testvec *vecs;
Herbert Xuda7f0332008-07-31 17:08:25 +0800101 unsigned int count;
102};
103
Jarod Wilson7647d6c2009-05-04 19:44:50 +0800104struct cprng_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800105 const struct cprng_testvec *vecs;
Jarod Wilson7647d6c2009-05-04 19:44:50 +0800106 unsigned int count;
107};
108
Stephan Mueller64d1cdf2014-05-31 17:25:36 +0200109struct drbg_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800110 const struct drbg_testvec *vecs;
Stephan Mueller64d1cdf2014-05-31 17:25:36 +0200111 unsigned int count;
112};
113
Tadeusz Struk946cc462015-06-16 10:31:06 -0700114struct akcipher_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800115 const struct akcipher_testvec *vecs;
Tadeusz Struk946cc462015-06-16 10:31:06 -0700116 unsigned int count;
117};
118
Salvatore Benedetto802c7f12016-06-22 17:49:14 +0100119struct kpp_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800120 const struct kpp_testvec *vecs;
Salvatore Benedetto802c7f12016-06-22 17:49:14 +0100121 unsigned int count;
122};
123
Herbert Xuda7f0332008-07-31 17:08:25 +0800124struct alg_test_desc {
125 const char *alg;
Eric Biggersf2bb770a2019-04-11 21:57:38 -0700126 const char *generic_driver;
Herbert Xuda7f0332008-07-31 17:08:25 +0800127 int (*test)(const struct alg_test_desc *desc, const char *driver,
128 u32 type, u32 mask);
Jarod Wilsona1915d52009-05-15 15:16:03 +1000129 int fips_allowed; /* set if alg is allowed in fips mode */
Herbert Xuda7f0332008-07-31 17:08:25 +0800130
131 union {
132 struct aead_test_suite aead;
133 struct cipher_test_suite cipher;
134 struct comp_test_suite comp;
135 struct hash_test_suite hash;
Jarod Wilson7647d6c2009-05-04 19:44:50 +0800136 struct cprng_test_suite cprng;
Stephan Mueller64d1cdf2014-05-31 17:25:36 +0200137 struct drbg_test_suite drbg;
Tadeusz Struk946cc462015-06-16 10:31:06 -0700138 struct akcipher_test_suite akcipher;
Salvatore Benedetto802c7f12016-06-22 17:49:14 +0100139 struct kpp_test_suite kpp;
Herbert Xuda7f0332008-07-31 17:08:25 +0800140 } suite;
141};
142
Herbert Xuda7f0332008-07-31 17:08:25 +0800143static void hexdump(unsigned char *buf, unsigned int len)
144{
145 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
146 16, 1,
147 buf, len, false);
148}
149
Eric Biggers3f47a032019-01-31 23:51:43 -0800150static int __testmgr_alloc_buf(char *buf[XBUFSIZE], int order)
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800151{
152 int i;
153
154 for (i = 0; i < XBUFSIZE; i++) {
Eric Biggers3f47a032019-01-31 23:51:43 -0800155 buf[i] = (char *)__get_free_pages(GFP_KERNEL, order);
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800156 if (!buf[i])
157 goto err_free_buf;
158 }
159
160 return 0;
161
162err_free_buf:
163 while (i-- > 0)
Eric Biggers3f47a032019-01-31 23:51:43 -0800164 free_pages((unsigned long)buf[i], order);
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800165
166 return -ENOMEM;
167}
168
Eric Biggers3f47a032019-01-31 23:51:43 -0800169static int testmgr_alloc_buf(char *buf[XBUFSIZE])
170{
171 return __testmgr_alloc_buf(buf, 0);
172}
173
174static void __testmgr_free_buf(char *buf[XBUFSIZE], int order)
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800175{
176 int i;
177
178 for (i = 0; i < XBUFSIZE; i++)
Eric Biggers3f47a032019-01-31 23:51:43 -0800179 free_pages((unsigned long)buf[i], order);
180}
181
182static void testmgr_free_buf(char *buf[XBUFSIZE])
183{
184 __testmgr_free_buf(buf, 0);
185}
186
187#define TESTMGR_POISON_BYTE 0xfe
188#define TESTMGR_POISON_LEN 16
189
190static inline void testmgr_poison(void *addr, size_t len)
191{
192 memset(addr, TESTMGR_POISON_BYTE, len);
193}
194
195/* Is the memory region still fully poisoned? */
196static inline bool testmgr_is_poison(const void *addr, size_t len)
197{
198 return memchr_inv(addr, TESTMGR_POISON_BYTE, len) == NULL;
199}
200
201/* flush type for hash algorithms */
202enum flush_type {
203 /* merge with update of previous buffer(s) */
204 FLUSH_TYPE_NONE = 0,
205
206 /* update with previous buffer(s) before doing this one */
207 FLUSH_TYPE_FLUSH,
208
209 /* likewise, but also export and re-import the intermediate state */
210 FLUSH_TYPE_REIMPORT,
211};
212
213/* finalization function for hash algorithms */
214enum finalization_type {
215 FINALIZATION_TYPE_FINAL, /* use final() */
216 FINALIZATION_TYPE_FINUP, /* use finup() */
217 FINALIZATION_TYPE_DIGEST, /* use digest() */
218};
219
220#define TEST_SG_TOTAL 10000
221
222/**
223 * struct test_sg_division - description of a scatterlist entry
224 *
225 * This struct describes one entry of a scatterlist being constructed to check a
226 * crypto test vector.
227 *
228 * @proportion_of_total: length of this chunk relative to the total length,
229 * given as a proportion out of TEST_SG_TOTAL so that it
230 * scales to fit any test vector
231 * @offset: byte offset into a 2-page buffer at which this chunk will start
232 * @offset_relative_to_alignmask: if true, add the algorithm's alignmask to the
233 * @offset
234 * @flush_type: for hashes, whether an update() should be done now vs.
235 * continuing to accumulate data
Eric Biggers65707372019-03-12 22:12:52 -0700236 * @nosimd: if doing the pending update(), do it with SIMD disabled?
Eric Biggers3f47a032019-01-31 23:51:43 -0800237 */
238struct test_sg_division {
239 unsigned int proportion_of_total;
240 unsigned int offset;
241 bool offset_relative_to_alignmask;
242 enum flush_type flush_type;
Eric Biggers65707372019-03-12 22:12:52 -0700243 bool nosimd;
Eric Biggers3f47a032019-01-31 23:51:43 -0800244};
245
246/**
247 * struct testvec_config - configuration for testing a crypto test vector
248 *
249 * This struct describes the data layout and other parameters with which each
250 * crypto test vector can be tested.
251 *
252 * @name: name of this config, logged for debugging purposes if a test fails
253 * @inplace: operate on the data in-place, if applicable for the algorithm type?
254 * @req_flags: extra request_flags, e.g. CRYPTO_TFM_REQ_MAY_SLEEP
255 * @src_divs: description of how to arrange the source scatterlist
256 * @dst_divs: description of how to arrange the dst scatterlist, if applicable
257 * for the algorithm type. Defaults to @src_divs if unset.
258 * @iv_offset: misalignment of the IV in the range [0..MAX_ALGAPI_ALIGNMASK+1],
259 * where 0 is aligned to a 2*(MAX_ALGAPI_ALIGNMASK+1) byte boundary
260 * @iv_offset_relative_to_alignmask: if true, add the algorithm's alignmask to
261 * the @iv_offset
Eric Biggersfd8c37c2019-12-01 13:53:28 -0800262 * @key_offset: misalignment of the key, where 0 is default alignment
263 * @key_offset_relative_to_alignmask: if true, add the algorithm's alignmask to
264 * the @key_offset
Eric Biggers3f47a032019-01-31 23:51:43 -0800265 * @finalization_type: what finalization function to use for hashes
Eric Biggers65707372019-03-12 22:12:52 -0700266 * @nosimd: execute with SIMD disabled? Requires !CRYPTO_TFM_REQ_MAY_SLEEP.
Eric Biggers3f47a032019-01-31 23:51:43 -0800267 */
268struct testvec_config {
269 const char *name;
270 bool inplace;
271 u32 req_flags;
272 struct test_sg_division src_divs[XBUFSIZE];
273 struct test_sg_division dst_divs[XBUFSIZE];
274 unsigned int iv_offset;
Eric Biggersfd8c37c2019-12-01 13:53:28 -0800275 unsigned int key_offset;
Eric Biggers3f47a032019-01-31 23:51:43 -0800276 bool iv_offset_relative_to_alignmask;
Eric Biggersfd8c37c2019-12-01 13:53:28 -0800277 bool key_offset_relative_to_alignmask;
Eric Biggers3f47a032019-01-31 23:51:43 -0800278 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,
Eric Biggersfd8c37c2019-12-01 13:53:28 -0800305 .key_offset = 1,
Eric Biggers4e7babba2019-01-31 23:51:46 -0800306 }, {
307 .name = "buffer aligned only to alignmask",
308 .src_divs = {
309 {
310 .proportion_of_total = 10000,
311 .offset = 1,
312 .offset_relative_to_alignmask = true,
313 },
314 },
315 .iv_offset = 1,
316 .iv_offset_relative_to_alignmask = true,
Eric Biggersfd8c37c2019-12-01 13:53:28 -0800317 .key_offset = 1,
318 .key_offset_relative_to_alignmask = true,
Eric Biggers4e7babba2019-01-31 23:51:46 -0800319 }, {
320 .name = "two even aligned splits",
321 .src_divs = {
322 { .proportion_of_total = 5000 },
323 { .proportion_of_total = 5000 },
324 },
325 }, {
326 .name = "uneven misaligned splits, may sleep",
327 .req_flags = CRYPTO_TFM_REQ_MAY_SLEEP,
328 .src_divs = {
329 { .proportion_of_total = 1900, .offset = 33 },
330 { .proportion_of_total = 3300, .offset = 7 },
331 { .proportion_of_total = 4800, .offset = 18 },
332 },
333 .iv_offset = 3,
Eric Biggersfd8c37c2019-12-01 13:53:28 -0800334 .key_offset = 3,
Eric Biggers4e7babba2019-01-31 23:51:46 -0800335 }, {
336 .name = "misaligned splits crossing pages, inplace",
337 .inplace = true,
338 .src_divs = {
339 {
340 .proportion_of_total = 7500,
341 .offset = PAGE_SIZE - 32
342 }, {
343 .proportion_of_total = 2500,
344 .offset = PAGE_SIZE - 7
345 },
346 },
347 }
348};
349
Eric Biggers4cc2dcf2019-01-31 23:51:48 -0800350static const struct testvec_config default_hash_testvec_configs[] = {
351 {
352 .name = "init+update+final aligned buffer",
353 .src_divs = { { .proportion_of_total = 10000 } },
354 .finalization_type = FINALIZATION_TYPE_FINAL,
355 }, {
356 .name = "init+finup aligned buffer",
357 .src_divs = { { .proportion_of_total = 10000 } },
358 .finalization_type = FINALIZATION_TYPE_FINUP,
359 }, {
360 .name = "digest aligned buffer",
361 .src_divs = { { .proportion_of_total = 10000 } },
362 .finalization_type = FINALIZATION_TYPE_DIGEST,
363 }, {
364 .name = "init+update+final misaligned buffer",
365 .src_divs = { { .proportion_of_total = 10000, .offset = 1 } },
366 .finalization_type = FINALIZATION_TYPE_FINAL,
Eric Biggersfd8c37c2019-12-01 13:53:28 -0800367 .key_offset = 1,
Eric Biggers4cc2dcf2019-01-31 23:51:48 -0800368 }, {
369 .name = "digest buffer aligned only to alignmask",
370 .src_divs = {
371 {
372 .proportion_of_total = 10000,
373 .offset = 1,
374 .offset_relative_to_alignmask = true,
375 },
376 },
377 .finalization_type = FINALIZATION_TYPE_DIGEST,
Eric Biggersfd8c37c2019-12-01 13:53:28 -0800378 .key_offset = 1,
379 .key_offset_relative_to_alignmask = true,
Eric Biggers4cc2dcf2019-01-31 23:51:48 -0800380 }, {
381 .name = "init+update+update+final two even splits",
382 .src_divs = {
383 { .proportion_of_total = 5000 },
384 {
385 .proportion_of_total = 5000,
386 .flush_type = FLUSH_TYPE_FLUSH,
387 },
388 },
389 .finalization_type = FINALIZATION_TYPE_FINAL,
390 }, {
391 .name = "digest uneven misaligned splits, may sleep",
392 .req_flags = CRYPTO_TFM_REQ_MAY_SLEEP,
393 .src_divs = {
394 { .proportion_of_total = 1900, .offset = 33 },
395 { .proportion_of_total = 3300, .offset = 7 },
396 { .proportion_of_total = 4800, .offset = 18 },
397 },
398 .finalization_type = FINALIZATION_TYPE_DIGEST,
399 }, {
400 .name = "digest misaligned splits crossing pages",
401 .src_divs = {
402 {
403 .proportion_of_total = 7500,
404 .offset = PAGE_SIZE - 32,
405 }, {
406 .proportion_of_total = 2500,
407 .offset = PAGE_SIZE - 7,
408 },
409 },
410 .finalization_type = FINALIZATION_TYPE_DIGEST,
411 }, {
412 .name = "import/export",
413 .src_divs = {
414 {
415 .proportion_of_total = 6500,
416 .flush_type = FLUSH_TYPE_REIMPORT,
417 }, {
418 .proportion_of_total = 3500,
419 .flush_type = FLUSH_TYPE_REIMPORT,
420 },
421 },
422 .finalization_type = FINALIZATION_TYPE_FINAL,
423 }
424};
425
Eric Biggers3f47a032019-01-31 23:51:43 -0800426static unsigned int count_test_sg_divisions(const struct test_sg_division *divs)
427{
428 unsigned int remaining = TEST_SG_TOTAL;
429 unsigned int ndivs = 0;
430
431 do {
432 remaining -= divs[ndivs++].proportion_of_total;
433 } while (remaining);
434
435 return ndivs;
436}
437
Eric Biggers65707372019-03-12 22:12:52 -0700438#define SGDIVS_HAVE_FLUSHES BIT(0)
439#define SGDIVS_HAVE_NOSIMD BIT(1)
440
Eric Biggers3f47a032019-01-31 23:51:43 -0800441static bool valid_sg_divisions(const struct test_sg_division *divs,
Eric Biggers65707372019-03-12 22:12:52 -0700442 unsigned int count, int *flags_ret)
Eric Biggers3f47a032019-01-31 23:51:43 -0800443{
444 unsigned int total = 0;
445 unsigned int i;
446
447 for (i = 0; i < count && total != TEST_SG_TOTAL; i++) {
448 if (divs[i].proportion_of_total <= 0 ||
449 divs[i].proportion_of_total > TEST_SG_TOTAL - total)
450 return false;
451 total += divs[i].proportion_of_total;
452 if (divs[i].flush_type != FLUSH_TYPE_NONE)
Eric Biggers65707372019-03-12 22:12:52 -0700453 *flags_ret |= SGDIVS_HAVE_FLUSHES;
454 if (divs[i].nosimd)
455 *flags_ret |= SGDIVS_HAVE_NOSIMD;
Eric Biggers3f47a032019-01-31 23:51:43 -0800456 }
457 return total == TEST_SG_TOTAL &&
458 memchr_inv(&divs[i], 0, (count - i) * sizeof(divs[0])) == NULL;
459}
460
461/*
462 * Check whether the given testvec_config is valid. This isn't strictly needed
463 * since every testvec_config should be valid, but check anyway so that people
464 * don't unknowingly add broken configs that don't do what they wanted.
465 */
466static bool valid_testvec_config(const struct testvec_config *cfg)
467{
Eric Biggers65707372019-03-12 22:12:52 -0700468 int flags = 0;
Eric Biggers3f47a032019-01-31 23:51:43 -0800469
470 if (cfg->name == NULL)
471 return false;
472
473 if (!valid_sg_divisions(cfg->src_divs, ARRAY_SIZE(cfg->src_divs),
Eric Biggers65707372019-03-12 22:12:52 -0700474 &flags))
Eric Biggers3f47a032019-01-31 23:51:43 -0800475 return false;
476
477 if (cfg->dst_divs[0].proportion_of_total) {
478 if (!valid_sg_divisions(cfg->dst_divs,
Eric Biggers65707372019-03-12 22:12:52 -0700479 ARRAY_SIZE(cfg->dst_divs), &flags))
Eric Biggers3f47a032019-01-31 23:51:43 -0800480 return false;
481 } else {
482 if (memchr_inv(cfg->dst_divs, 0, sizeof(cfg->dst_divs)))
483 return false;
484 /* defaults to dst_divs=src_divs */
485 }
486
487 if (cfg->iv_offset +
488 (cfg->iv_offset_relative_to_alignmask ? MAX_ALGAPI_ALIGNMASK : 0) >
489 MAX_ALGAPI_ALIGNMASK + 1)
490 return false;
491
Eric Biggers65707372019-03-12 22:12:52 -0700492 if ((flags & (SGDIVS_HAVE_FLUSHES | SGDIVS_HAVE_NOSIMD)) &&
493 cfg->finalization_type == FINALIZATION_TYPE_DIGEST)
494 return false;
495
496 if ((cfg->nosimd || (flags & SGDIVS_HAVE_NOSIMD)) &&
497 (cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP))
Eric Biggers3f47a032019-01-31 23:51:43 -0800498 return false;
499
500 return true;
501}
502
503struct test_sglist {
504 char *bufs[XBUFSIZE];
505 struct scatterlist sgl[XBUFSIZE];
506 struct scatterlist sgl_saved[XBUFSIZE];
507 struct scatterlist *sgl_ptr;
508 unsigned int nents;
509};
510
511static int init_test_sglist(struct test_sglist *tsgl)
512{
513 return __testmgr_alloc_buf(tsgl->bufs, 1 /* two pages per buffer */);
514}
515
516static void destroy_test_sglist(struct test_sglist *tsgl)
517{
518 return __testmgr_free_buf(tsgl->bufs, 1 /* two pages per buffer */);
519}
520
521/**
522 * build_test_sglist() - build a scatterlist for a crypto test
523 *
524 * @tsgl: the scatterlist to build. @tsgl->bufs[] contains an array of 2-page
525 * buffers which the scatterlist @tsgl->sgl[] will be made to point into.
526 * @divs: the layout specification on which the scatterlist will be based
527 * @alignmask: the algorithm's alignmask
528 * @total_len: the total length of the scatterlist to build in bytes
529 * @data: if non-NULL, the buffers will be filled with this data until it ends.
530 * Otherwise the buffers will be poisoned. In both cases, some bytes
531 * past the end of each buffer will be poisoned to help detect overruns.
532 * @out_divs: if non-NULL, the test_sg_division to which each scatterlist entry
533 * corresponds will be returned here. This will match @divs except
534 * that divisions resolving to a length of 0 are omitted as they are
535 * not included in the scatterlist.
536 *
537 * Return: 0 or a -errno value
538 */
539static int build_test_sglist(struct test_sglist *tsgl,
540 const struct test_sg_division *divs,
541 const unsigned int alignmask,
542 const unsigned int total_len,
543 struct iov_iter *data,
544 const struct test_sg_division *out_divs[XBUFSIZE])
545{
546 struct {
547 const struct test_sg_division *div;
548 size_t length;
549 } partitions[XBUFSIZE];
550 const unsigned int ndivs = count_test_sg_divisions(divs);
551 unsigned int len_remaining = total_len;
552 unsigned int i;
553
554 BUILD_BUG_ON(ARRAY_SIZE(partitions) != ARRAY_SIZE(tsgl->sgl));
555 if (WARN_ON(ndivs > ARRAY_SIZE(partitions)))
556 return -EINVAL;
557
558 /* Calculate the (div, length) pairs */
559 tsgl->nents = 0;
560 for (i = 0; i < ndivs; i++) {
561 unsigned int len_this_sg =
562 min(len_remaining,
563 (total_len * divs[i].proportion_of_total +
564 TEST_SG_TOTAL / 2) / TEST_SG_TOTAL);
565
566 if (len_this_sg != 0) {
567 partitions[tsgl->nents].div = &divs[i];
568 partitions[tsgl->nents].length = len_this_sg;
569 tsgl->nents++;
570 len_remaining -= len_this_sg;
571 }
572 }
573 if (tsgl->nents == 0) {
574 partitions[tsgl->nents].div = &divs[0];
575 partitions[tsgl->nents].length = 0;
576 tsgl->nents++;
577 }
578 partitions[tsgl->nents - 1].length += len_remaining;
579
580 /* Set up the sgl entries and fill the data or poison */
581 sg_init_table(tsgl->sgl, tsgl->nents);
582 for (i = 0; i < tsgl->nents; i++) {
583 unsigned int offset = partitions[i].div->offset;
584 void *addr;
585
586 if (partitions[i].div->offset_relative_to_alignmask)
587 offset += alignmask;
588
589 while (offset + partitions[i].length + TESTMGR_POISON_LEN >
590 2 * PAGE_SIZE) {
591 if (WARN_ON(offset <= 0))
592 return -EINVAL;
593 offset /= 2;
594 }
595
596 addr = &tsgl->bufs[i][offset];
597 sg_set_buf(&tsgl->sgl[i], addr, partitions[i].length);
598
599 if (out_divs)
600 out_divs[i] = partitions[i].div;
601
602 if (data) {
603 size_t copy_len, copied;
604
605 copy_len = min(partitions[i].length, data->count);
606 copied = copy_from_iter(addr, copy_len, data);
607 if (WARN_ON(copied != copy_len))
608 return -EINVAL;
609 testmgr_poison(addr + copy_len, partitions[i].length +
610 TESTMGR_POISON_LEN - copy_len);
611 } else {
612 testmgr_poison(addr, partitions[i].length +
613 TESTMGR_POISON_LEN);
614 }
615 }
616
617 sg_mark_end(&tsgl->sgl[tsgl->nents - 1]);
618 tsgl->sgl_ptr = tsgl->sgl;
619 memcpy(tsgl->sgl_saved, tsgl->sgl, tsgl->nents * sizeof(tsgl->sgl[0]));
620 return 0;
621}
622
623/*
624 * Verify that a scatterlist crypto operation produced the correct output.
625 *
626 * @tsgl: scatterlist containing the actual output
627 * @expected_output: buffer containing the expected output
628 * @len_to_check: length of @expected_output in bytes
629 * @unchecked_prefix_len: number of ignored bytes in @tsgl prior to real result
630 * @check_poison: verify that the poison bytes after each chunk are intact?
631 *
632 * Return: 0 if correct, -EINVAL if incorrect, -EOVERFLOW if buffer overrun.
633 */
634static int verify_correct_output(const struct test_sglist *tsgl,
635 const char *expected_output,
636 unsigned int len_to_check,
637 unsigned int unchecked_prefix_len,
638 bool check_poison)
639{
640 unsigned int i;
641
642 for (i = 0; i < tsgl->nents; i++) {
643 struct scatterlist *sg = &tsgl->sgl_ptr[i];
644 unsigned int len = sg->length;
645 unsigned int offset = sg->offset;
646 const char *actual_output;
647
648 if (unchecked_prefix_len) {
649 if (unchecked_prefix_len >= len) {
650 unchecked_prefix_len -= len;
651 continue;
652 }
653 offset += unchecked_prefix_len;
654 len -= unchecked_prefix_len;
655 unchecked_prefix_len = 0;
656 }
657 len = min(len, len_to_check);
658 actual_output = page_address(sg_page(sg)) + offset;
659 if (memcmp(expected_output, actual_output, len) != 0)
660 return -EINVAL;
661 if (check_poison &&
662 !testmgr_is_poison(actual_output + len, TESTMGR_POISON_LEN))
663 return -EOVERFLOW;
664 len_to_check -= len;
665 expected_output += len;
666 }
667 if (WARN_ON(len_to_check != 0))
668 return -EINVAL;
669 return 0;
670}
671
672static bool is_test_sglist_corrupted(const struct test_sglist *tsgl)
673{
674 unsigned int i;
675
676 for (i = 0; i < tsgl->nents; i++) {
677 if (tsgl->sgl[i].page_link != tsgl->sgl_saved[i].page_link)
678 return true;
679 if (tsgl->sgl[i].offset != tsgl->sgl_saved[i].offset)
680 return true;
681 if (tsgl->sgl[i].length != tsgl->sgl_saved[i].length)
682 return true;
683 }
684 return false;
685}
686
687struct cipher_test_sglists {
688 struct test_sglist src;
689 struct test_sglist dst;
690};
691
692static struct cipher_test_sglists *alloc_cipher_test_sglists(void)
693{
694 struct cipher_test_sglists *tsgls;
695
696 tsgls = kmalloc(sizeof(*tsgls), GFP_KERNEL);
697 if (!tsgls)
698 return NULL;
699
700 if (init_test_sglist(&tsgls->src) != 0)
701 goto fail_kfree;
702 if (init_test_sglist(&tsgls->dst) != 0)
703 goto fail_destroy_src;
704
705 return tsgls;
706
707fail_destroy_src:
708 destroy_test_sglist(&tsgls->src);
709fail_kfree:
710 kfree(tsgls);
711 return NULL;
712}
713
714static void free_cipher_test_sglists(struct cipher_test_sglists *tsgls)
715{
716 if (tsgls) {
717 destroy_test_sglist(&tsgls->src);
718 destroy_test_sglist(&tsgls->dst);
719 kfree(tsgls);
720 }
721}
722
723/* Build the src and dst scatterlists for an skcipher or AEAD test */
724static int build_cipher_test_sglists(struct cipher_test_sglists *tsgls,
725 const struct testvec_config *cfg,
726 unsigned int alignmask,
727 unsigned int src_total_len,
728 unsigned int dst_total_len,
729 const struct kvec *inputs,
730 unsigned int nr_inputs)
731{
732 struct iov_iter input;
733 int err;
734
735 iov_iter_kvec(&input, WRITE, inputs, nr_inputs, src_total_len);
736 err = build_test_sglist(&tsgls->src, cfg->src_divs, alignmask,
737 cfg->inplace ?
738 max(dst_total_len, src_total_len) :
739 src_total_len,
740 &input, NULL);
741 if (err)
742 return err;
743
744 if (cfg->inplace) {
745 tsgls->dst.sgl_ptr = tsgls->src.sgl;
746 tsgls->dst.nents = tsgls->src.nents;
747 return 0;
748 }
749 return build_test_sglist(&tsgls->dst,
750 cfg->dst_divs[0].proportion_of_total ?
751 cfg->dst_divs : cfg->src_divs,
752 alignmask, dst_total_len, NULL, NULL);
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800753}
754
Eric Biggersfd8c37c2019-12-01 13:53:28 -0800755/*
756 * Support for testing passing a misaligned key to setkey():
757 *
758 * If cfg->key_offset is set, copy the key into a new buffer at that offset,
759 * optionally adding alignmask. Else, just use the key directly.
760 */
761static int prepare_keybuf(const u8 *key, unsigned int ksize,
762 const struct testvec_config *cfg,
763 unsigned int alignmask,
764 const u8 **keybuf_ret, const u8 **keyptr_ret)
765{
766 unsigned int key_offset = cfg->key_offset;
767 u8 *keybuf = NULL, *keyptr = (u8 *)key;
768
769 if (key_offset != 0) {
770 if (cfg->key_offset_relative_to_alignmask)
771 key_offset += alignmask;
772 keybuf = kmalloc(key_offset + ksize, GFP_KERNEL);
773 if (!keybuf)
774 return -ENOMEM;
775 keyptr = keybuf + key_offset;
776 memcpy(keyptr, key, ksize);
777 }
778 *keybuf_ret = keybuf;
779 *keyptr_ret = keyptr;
780 return 0;
781}
782
783/* Like setkey_f(tfm, key, ksize), but sometimes misalign the key */
784#define do_setkey(setkey_f, tfm, key, ksize, cfg, alignmask) \
785({ \
786 const u8 *keybuf, *keyptr; \
787 int err; \
788 \
789 err = prepare_keybuf((key), (ksize), (cfg), (alignmask), \
790 &keybuf, &keyptr); \
791 if (err == 0) { \
792 err = setkey_f((tfm), keyptr, (ksize)); \
793 kfree(keybuf); \
794 } \
795 err; \
796})
797
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800798#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
Eric Biggersf2bb770a2019-04-11 21:57:38 -0700799
800/* Generate a random length in range [0, max_len], but prefer smaller values */
801static unsigned int generate_random_length(unsigned int max_len)
802{
803 unsigned int len = prandom_u32() % (max_len + 1);
804
805 switch (prandom_u32() % 4) {
806 case 0:
807 return len % 64;
808 case 1:
809 return len % 256;
810 case 2:
811 return len % 1024;
812 default:
813 return len;
814 }
815}
816
817/* Sometimes make some random changes to the given data buffer */
818static void mutate_buffer(u8 *buf, size_t count)
819{
820 size_t num_flips;
821 size_t i;
822 size_t pos;
823
824 /* Sometimes flip some bits */
825 if (prandom_u32() % 4 == 0) {
826 num_flips = min_t(size_t, 1 << (prandom_u32() % 8), count * 8);
827 for (i = 0; i < num_flips; i++) {
828 pos = prandom_u32() % (count * 8);
829 buf[pos / 8] ^= 1 << (pos % 8);
830 }
831 }
832
833 /* Sometimes flip some bytes */
834 if (prandom_u32() % 4 == 0) {
835 num_flips = min_t(size_t, 1 << (prandom_u32() % 8), count);
836 for (i = 0; i < num_flips; i++)
837 buf[prandom_u32() % count] ^= 0xff;
838 }
839}
840
841/* Randomly generate 'count' bytes, but sometimes make them "interesting" */
842static void generate_random_bytes(u8 *buf, size_t count)
843{
844 u8 b;
845 u8 increment;
846 size_t i;
847
848 if (count == 0)
849 return;
850
851 switch (prandom_u32() % 8) { /* Choose a generation strategy */
852 case 0:
853 case 1:
854 /* All the same byte, plus optional mutations */
855 switch (prandom_u32() % 4) {
856 case 0:
857 b = 0x00;
858 break;
859 case 1:
860 b = 0xff;
861 break;
862 default:
863 b = (u8)prandom_u32();
864 break;
865 }
866 memset(buf, b, count);
867 mutate_buffer(buf, count);
868 break;
869 case 2:
870 /* Ascending or descending bytes, plus optional mutations */
871 increment = (u8)prandom_u32();
872 b = (u8)prandom_u32();
873 for (i = 0; i < count; i++, b += increment)
874 buf[i] = b;
875 mutate_buffer(buf, count);
876 break;
877 default:
878 /* Fully random bytes */
879 for (i = 0; i < count; i++)
880 buf[i] = (u8)prandom_u32();
881 }
882}
883
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800884static char *generate_random_sgl_divisions(struct test_sg_division *divs,
885 size_t max_divs, char *p, char *end,
Eric Biggers65707372019-03-12 22:12:52 -0700886 bool gen_flushes, u32 req_flags)
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800887{
888 struct test_sg_division *div = divs;
889 unsigned int remaining = TEST_SG_TOTAL;
890
891 do {
892 unsigned int this_len;
Eric Biggers65707372019-03-12 22:12:52 -0700893 const char *flushtype_str;
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800894
895 if (div == &divs[max_divs - 1] || prandom_u32() % 2 == 0)
896 this_len = remaining;
897 else
898 this_len = 1 + (prandom_u32() % remaining);
899 div->proportion_of_total = this_len;
900
901 if (prandom_u32() % 4 == 0)
902 div->offset = (PAGE_SIZE - 128) + (prandom_u32() % 128);
903 else if (prandom_u32() % 2 == 0)
904 div->offset = prandom_u32() % 32;
905 else
906 div->offset = prandom_u32() % PAGE_SIZE;
907 if (prandom_u32() % 8 == 0)
908 div->offset_relative_to_alignmask = true;
909
910 div->flush_type = FLUSH_TYPE_NONE;
911 if (gen_flushes) {
912 switch (prandom_u32() % 4) {
913 case 0:
914 div->flush_type = FLUSH_TYPE_REIMPORT;
915 break;
916 case 1:
917 div->flush_type = FLUSH_TYPE_FLUSH;
918 break;
919 }
920 }
921
Eric Biggers65707372019-03-12 22:12:52 -0700922 if (div->flush_type != FLUSH_TYPE_NONE &&
923 !(req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) &&
924 prandom_u32() % 2 == 0)
925 div->nosimd = true;
926
927 switch (div->flush_type) {
928 case FLUSH_TYPE_FLUSH:
929 if (div->nosimd)
930 flushtype_str = "<flush,nosimd>";
931 else
932 flushtype_str = "<flush>";
933 break;
934 case FLUSH_TYPE_REIMPORT:
935 if (div->nosimd)
936 flushtype_str = "<reimport,nosimd>";
937 else
938 flushtype_str = "<reimport>";
939 break;
940 default:
941 flushtype_str = "";
942 break;
943 }
944
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800945 BUILD_BUG_ON(TEST_SG_TOTAL != 10000); /* for "%u.%u%%" */
Eric Biggers65707372019-03-12 22:12:52 -0700946 p += scnprintf(p, end - p, "%s%u.%u%%@%s+%u%s", flushtype_str,
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800947 this_len / 100, this_len % 100,
948 div->offset_relative_to_alignmask ?
949 "alignmask" : "",
950 div->offset, this_len == remaining ? "" : ", ");
951 remaining -= this_len;
952 div++;
953 } while (remaining);
954
955 return p;
956}
957
958/* Generate a random testvec_config for fuzz testing */
959static void generate_random_testvec_config(struct testvec_config *cfg,
960 char *name, size_t max_namelen)
961{
962 char *p = name;
963 char * const end = name + max_namelen;
964
965 memset(cfg, 0, sizeof(*cfg));
966
967 cfg->name = name;
968
969 p += scnprintf(p, end - p, "random:");
970
971 if (prandom_u32() % 2 == 0) {
972 cfg->inplace = true;
973 p += scnprintf(p, end - p, " inplace");
974 }
975
976 if (prandom_u32() % 2 == 0) {
977 cfg->req_flags |= CRYPTO_TFM_REQ_MAY_SLEEP;
978 p += scnprintf(p, end - p, " may_sleep");
979 }
980
981 switch (prandom_u32() % 4) {
982 case 0:
983 cfg->finalization_type = FINALIZATION_TYPE_FINAL;
984 p += scnprintf(p, end - p, " use_final");
985 break;
986 case 1:
987 cfg->finalization_type = FINALIZATION_TYPE_FINUP;
988 p += scnprintf(p, end - p, " use_finup");
989 break;
990 default:
991 cfg->finalization_type = FINALIZATION_TYPE_DIGEST;
992 p += scnprintf(p, end - p, " use_digest");
993 break;
994 }
995
Eric Biggers65707372019-03-12 22:12:52 -0700996 if (!(cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) &&
997 prandom_u32() % 2 == 0) {
998 cfg->nosimd = true;
999 p += scnprintf(p, end - p, " nosimd");
1000 }
1001
Eric Biggers25f9ddd2019-01-31 23:51:45 -08001002 p += scnprintf(p, end - p, " src_divs=[");
1003 p = generate_random_sgl_divisions(cfg->src_divs,
1004 ARRAY_SIZE(cfg->src_divs), p, end,
1005 (cfg->finalization_type !=
Eric Biggers65707372019-03-12 22:12:52 -07001006 FINALIZATION_TYPE_DIGEST),
1007 cfg->req_flags);
Eric Biggers25f9ddd2019-01-31 23:51:45 -08001008 p += scnprintf(p, end - p, "]");
1009
1010 if (!cfg->inplace && prandom_u32() % 2 == 0) {
1011 p += scnprintf(p, end - p, " dst_divs=[");
1012 p = generate_random_sgl_divisions(cfg->dst_divs,
1013 ARRAY_SIZE(cfg->dst_divs),
Eric Biggers65707372019-03-12 22:12:52 -07001014 p, end, false,
1015 cfg->req_flags);
Eric Biggers25f9ddd2019-01-31 23:51:45 -08001016 p += scnprintf(p, end - p, "]");
1017 }
1018
1019 if (prandom_u32() % 2 == 0) {
1020 cfg->iv_offset = 1 + (prandom_u32() % MAX_ALGAPI_ALIGNMASK);
1021 p += scnprintf(p, end - p, " iv_offset=%u", cfg->iv_offset);
1022 }
1023
Eric Biggersfd8c37c2019-12-01 13:53:28 -08001024 if (prandom_u32() % 2 == 0) {
1025 cfg->key_offset = 1 + (prandom_u32() % MAX_ALGAPI_ALIGNMASK);
1026 p += scnprintf(p, end - p, " key_offset=%u", cfg->key_offset);
1027 }
1028
Eric Biggers25f9ddd2019-01-31 23:51:45 -08001029 WARN_ON_ONCE(!valid_testvec_config(cfg));
1030}
Eric Biggersb55e1a32019-03-12 22:12:47 -07001031
1032static void crypto_disable_simd_for_test(void)
1033{
1034 preempt_disable();
1035 __this_cpu_write(crypto_simd_disabled_for_test, true);
1036}
1037
1038static void crypto_reenable_simd_for_test(void)
1039{
1040 __this_cpu_write(crypto_simd_disabled_for_test, false);
1041 preempt_enable();
1042}
Eric Biggersf2bb770a2019-04-11 21:57:38 -07001043
1044/*
1045 * Given an algorithm name, build the name of the generic implementation of that
1046 * algorithm, assuming the usual naming convention. Specifically, this appends
1047 * "-generic" to every part of the name that is not a template name. Examples:
1048 *
1049 * aes => aes-generic
1050 * cbc(aes) => cbc(aes-generic)
1051 * cts(cbc(aes)) => cts(cbc(aes-generic))
1052 * rfc7539(chacha20,poly1305) => rfc7539(chacha20-generic,poly1305-generic)
1053 *
1054 * Return: 0 on success, or -ENAMETOOLONG if the generic name would be too long
1055 */
1056static int build_generic_driver_name(const char *algname,
1057 char driver_name[CRYPTO_MAX_ALG_NAME])
1058{
1059 const char *in = algname;
1060 char *out = driver_name;
1061 size_t len = strlen(algname);
1062
1063 if (len >= CRYPTO_MAX_ALG_NAME)
1064 goto too_long;
1065 do {
1066 const char *in_saved = in;
1067
1068 while (*in && *in != '(' && *in != ')' && *in != ',')
1069 *out++ = *in++;
1070 if (*in != '(' && in > in_saved) {
1071 len += 8;
1072 if (len >= CRYPTO_MAX_ALG_NAME)
1073 goto too_long;
1074 memcpy(out, "-generic", 8);
1075 out += 8;
1076 }
1077 } while ((*out++ = *in++) != '\0');
1078 return 0;
1079
1080too_long:
1081 pr_err("alg: generic driver name for \"%s\" would be too long\n",
1082 algname);
1083 return -ENAMETOOLONG;
1084}
Eric Biggersb55e1a32019-03-12 22:12:47 -07001085#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1086static void crypto_disable_simd_for_test(void)
1087{
1088}
1089
1090static void crypto_reenable_simd_for_test(void)
1091{
1092}
1093#endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
Eric Biggers25f9ddd2019-01-31 23:51:45 -08001094
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001095static int build_hash_sglist(struct test_sglist *tsgl,
1096 const struct hash_testvec *vec,
1097 const struct testvec_config *cfg,
1098 unsigned int alignmask,
1099 const struct test_sg_division *divs[XBUFSIZE])
1100{
1101 struct kvec kv;
1102 struct iov_iter input;
1103
1104 kv.iov_base = (void *)vec->plaintext;
1105 kv.iov_len = vec->psize;
1106 iov_iter_kvec(&input, WRITE, &kv, 1, vec->psize);
1107 return build_test_sglist(tsgl, cfg->src_divs, alignmask, vec->psize,
1108 &input, divs);
1109}
1110
1111static int check_hash_result(const char *type,
1112 const u8 *result, unsigned int digestsize,
1113 const struct hash_testvec *vec,
1114 const char *vec_name,
1115 const char *driver,
1116 const struct testvec_config *cfg)
1117{
1118 if (memcmp(result, vec->digest, digestsize) != 0) {
1119 pr_err("alg: %s: %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
1120 type, driver, vec_name, cfg->name);
1121 return -EINVAL;
1122 }
1123 if (!testmgr_is_poison(&result[digestsize], TESTMGR_POISON_LEN)) {
1124 pr_err("alg: %s: %s overran result buffer on test vector %s, cfg=\"%s\"\n",
1125 type, driver, vec_name, cfg->name);
1126 return -EOVERFLOW;
1127 }
1128 return 0;
1129}
1130
1131static inline int check_shash_op(const char *op, int err,
1132 const char *driver, const char *vec_name,
1133 const struct testvec_config *cfg)
1134{
1135 if (err)
1136 pr_err("alg: shash: %s %s() failed with err %d on test vector %s, cfg=\"%s\"\n",
1137 driver, op, err, vec_name, cfg->name);
1138 return err;
1139}
1140
1141static inline const void *sg_data(struct scatterlist *sg)
1142{
1143 return page_address(sg_page(sg)) + sg->offset;
1144}
1145
1146/* Test one hash test vector in one configuration, using the shash API */
1147static int test_shash_vec_cfg(const char *driver,
1148 const struct hash_testvec *vec,
1149 const char *vec_name,
1150 const struct testvec_config *cfg,
1151 struct shash_desc *desc,
1152 struct test_sglist *tsgl,
1153 u8 *hashstate)
1154{
1155 struct crypto_shash *tfm = desc->tfm;
1156 const unsigned int alignmask = crypto_shash_alignmask(tfm);
1157 const unsigned int digestsize = crypto_shash_digestsize(tfm);
1158 const unsigned int statesize = crypto_shash_statesize(tfm);
1159 const struct test_sg_division *divs[XBUFSIZE];
1160 unsigned int i;
1161 u8 result[HASH_MAX_DIGESTSIZE + TESTMGR_POISON_LEN];
1162 int err;
1163
1164 /* Set the key, if specified */
1165 if (vec->ksize) {
Eric Biggersfd8c37c2019-12-01 13:53:28 -08001166 err = do_setkey(crypto_shash_setkey, tfm, vec->key, vec->ksize,
1167 cfg, alignmask);
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001168 if (err) {
1169 if (err == vec->setkey_error)
1170 return 0;
1171 pr_err("alg: shash: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1172 driver, vec_name, vec->setkey_error, err,
1173 crypto_shash_get_flags(tfm));
1174 return err;
1175 }
1176 if (vec->setkey_error) {
1177 pr_err("alg: shash: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1178 driver, vec_name, vec->setkey_error);
1179 return -EINVAL;
1180 }
1181 }
1182
1183 /* Build the scatterlist for the source data */
1184 err = build_hash_sglist(tsgl, vec, cfg, alignmask, divs);
1185 if (err) {
1186 pr_err("alg: shash: %s: error preparing scatterlist for test vector %s, cfg=\"%s\"\n",
1187 driver, vec_name, cfg->name);
1188 return err;
1189 }
1190
1191 /* Do the actual hashing */
1192
1193 testmgr_poison(desc->__ctx, crypto_shash_descsize(tfm));
1194 testmgr_poison(result, digestsize + TESTMGR_POISON_LEN);
1195
1196 if (cfg->finalization_type == FINALIZATION_TYPE_DIGEST ||
1197 vec->digest_error) {
1198 /* Just using digest() */
1199 if (tsgl->nents != 1)
1200 return 0;
1201 if (cfg->nosimd)
1202 crypto_disable_simd_for_test();
1203 err = crypto_shash_digest(desc, sg_data(&tsgl->sgl[0]),
1204 tsgl->sgl[0].length, result);
1205 if (cfg->nosimd)
1206 crypto_reenable_simd_for_test();
1207 if (err) {
1208 if (err == vec->digest_error)
1209 return 0;
1210 pr_err("alg: shash: %s digest() failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
1211 driver, vec_name, vec->digest_error, err,
1212 cfg->name);
1213 return err;
1214 }
1215 if (vec->digest_error) {
1216 pr_err("alg: shash: %s digest() unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
1217 driver, vec_name, vec->digest_error, cfg->name);
1218 return -EINVAL;
1219 }
1220 goto result_ready;
1221 }
1222
1223 /* Using init(), zero or more update(), then final() or finup() */
1224
1225 if (cfg->nosimd)
1226 crypto_disable_simd_for_test();
1227 err = crypto_shash_init(desc);
1228 if (cfg->nosimd)
1229 crypto_reenable_simd_for_test();
1230 err = check_shash_op("init", err, driver, vec_name, cfg);
1231 if (err)
1232 return err;
1233
1234 for (i = 0; i < tsgl->nents; i++) {
1235 if (i + 1 == tsgl->nents &&
1236 cfg->finalization_type == FINALIZATION_TYPE_FINUP) {
1237 if (divs[i]->nosimd)
1238 crypto_disable_simd_for_test();
1239 err = crypto_shash_finup(desc, sg_data(&tsgl->sgl[i]),
1240 tsgl->sgl[i].length, result);
1241 if (divs[i]->nosimd)
1242 crypto_reenable_simd_for_test();
1243 err = check_shash_op("finup", err, driver, vec_name,
1244 cfg);
1245 if (err)
1246 return err;
1247 goto result_ready;
1248 }
1249 if (divs[i]->nosimd)
1250 crypto_disable_simd_for_test();
1251 err = crypto_shash_update(desc, sg_data(&tsgl->sgl[i]),
1252 tsgl->sgl[i].length);
1253 if (divs[i]->nosimd)
1254 crypto_reenable_simd_for_test();
1255 err = check_shash_op("update", err, driver, vec_name, cfg);
1256 if (err)
1257 return err;
1258 if (divs[i]->flush_type == FLUSH_TYPE_REIMPORT) {
1259 /* Test ->export() and ->import() */
1260 testmgr_poison(hashstate + statesize,
1261 TESTMGR_POISON_LEN);
1262 err = crypto_shash_export(desc, hashstate);
1263 err = check_shash_op("export", err, driver, vec_name,
1264 cfg);
1265 if (err)
1266 return err;
1267 if (!testmgr_is_poison(hashstate + statesize,
1268 TESTMGR_POISON_LEN)) {
1269 pr_err("alg: shash: %s export() overran state buffer on test vector %s, cfg=\"%s\"\n",
1270 driver, vec_name, cfg->name);
1271 return -EOVERFLOW;
1272 }
1273 testmgr_poison(desc->__ctx, crypto_shash_descsize(tfm));
1274 err = crypto_shash_import(desc, hashstate);
1275 err = check_shash_op("import", err, driver, vec_name,
1276 cfg);
1277 if (err)
1278 return err;
1279 }
1280 }
1281
1282 if (cfg->nosimd)
1283 crypto_disable_simd_for_test();
1284 err = crypto_shash_final(desc, result);
1285 if (cfg->nosimd)
1286 crypto_reenable_simd_for_test();
1287 err = check_shash_op("final", err, driver, vec_name, cfg);
1288 if (err)
1289 return err;
1290result_ready:
1291 return check_hash_result("shash", result, digestsize, vec, vec_name,
1292 driver, cfg);
1293}
1294
Eric Biggers65707372019-03-12 22:12:52 -07001295static int do_ahash_op(int (*op)(struct ahash_request *req),
1296 struct ahash_request *req,
1297 struct crypto_wait *wait, bool nosimd)
1298{
1299 int err;
1300
1301 if (nosimd)
1302 crypto_disable_simd_for_test();
1303
1304 err = op(req);
1305
1306 if (nosimd)
1307 crypto_reenable_simd_for_test();
1308
1309 return crypto_wait_req(err, wait);
1310}
1311
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001312static int check_nonfinal_ahash_op(const char *op, int err,
1313 u8 *result, unsigned int digestsize,
1314 const char *driver, const char *vec_name,
1315 const struct testvec_config *cfg)
Kamil Konieczny466d7b92018-01-16 15:26:13 +01001316{
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001317 if (err) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001318 pr_err("alg: ahash: %s %s() failed with err %d on test vector %s, cfg=\"%s\"\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001319 driver, op, err, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001320 return err;
1321 }
1322 if (!testmgr_is_poison(result, digestsize)) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001323 pr_err("alg: ahash: %s %s() used result buffer on test vector %s, cfg=\"%s\"\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001324 driver, op, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001325 return -EINVAL;
1326 }
1327 return 0;
1328}
Kamil Konieczny466d7b92018-01-16 15:26:13 +01001329
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001330/* Test one hash test vector in one configuration, using the ahash API */
1331static int test_ahash_vec_cfg(const char *driver,
1332 const struct hash_testvec *vec,
1333 const char *vec_name,
1334 const struct testvec_config *cfg,
1335 struct ahash_request *req,
1336 struct test_sglist *tsgl,
1337 u8 *hashstate)
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001338{
1339 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1340 const unsigned int alignmask = crypto_ahash_alignmask(tfm);
1341 const unsigned int digestsize = crypto_ahash_digestsize(tfm);
1342 const unsigned int statesize = crypto_ahash_statesize(tfm);
1343 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1344 const struct test_sg_division *divs[XBUFSIZE];
1345 DECLARE_CRYPTO_WAIT(wait);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001346 unsigned int i;
1347 struct scatterlist *pending_sgl;
1348 unsigned int pending_len;
1349 u8 result[HASH_MAX_DIGESTSIZE + TESTMGR_POISON_LEN];
1350 int err;
1351
1352 /* Set the key, if specified */
1353 if (vec->ksize) {
Eric Biggersfd8c37c2019-12-01 13:53:28 -08001354 err = do_setkey(crypto_ahash_setkey, tfm, vec->key, vec->ksize,
1355 cfg, alignmask);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001356 if (err) {
Eric Biggers5283a8e2019-04-11 21:57:36 -07001357 if (err == vec->setkey_error)
1358 return 0;
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001359 pr_err("alg: ahash: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001360 driver, vec_name, vec->setkey_error, err,
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001361 crypto_ahash_get_flags(tfm));
1362 return err;
1363 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07001364 if (vec->setkey_error) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001365 pr_err("alg: ahash: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001366 driver, vec_name, vec->setkey_error);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001367 return -EINVAL;
1368 }
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001369 }
1370
1371 /* Build the scatterlist for the source data */
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001372 err = build_hash_sglist(tsgl, vec, cfg, alignmask, divs);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001373 if (err) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001374 pr_err("alg: ahash: %s: error preparing scatterlist for test vector %s, cfg=\"%s\"\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001375 driver, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001376 return err;
1377 }
1378
1379 /* Do the actual hashing */
1380
1381 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
1382 testmgr_poison(result, digestsize + TESTMGR_POISON_LEN);
1383
Eric Biggers5283a8e2019-04-11 21:57:36 -07001384 if (cfg->finalization_type == FINALIZATION_TYPE_DIGEST ||
1385 vec->digest_error) {
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001386 /* Just using digest() */
1387 ahash_request_set_callback(req, req_flags, crypto_req_done,
1388 &wait);
1389 ahash_request_set_crypt(req, tsgl->sgl, result, vec->psize);
Eric Biggers65707372019-03-12 22:12:52 -07001390 err = do_ahash_op(crypto_ahash_digest, req, &wait, cfg->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001391 if (err) {
Eric Biggers5283a8e2019-04-11 21:57:36 -07001392 if (err == vec->digest_error)
1393 return 0;
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001394 pr_err("alg: ahash: %s digest() failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001395 driver, vec_name, vec->digest_error, err,
Eric Biggers5283a8e2019-04-11 21:57:36 -07001396 cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001397 return err;
1398 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07001399 if (vec->digest_error) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001400 pr_err("alg: ahash: %s digest() unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001401 driver, vec_name, vec->digest_error, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001402 return -EINVAL;
1403 }
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001404 goto result_ready;
1405 }
1406
1407 /* Using init(), zero or more update(), then final() or finup() */
1408
1409 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
1410 ahash_request_set_crypt(req, NULL, result, 0);
Eric Biggers65707372019-03-12 22:12:52 -07001411 err = do_ahash_op(crypto_ahash_init, req, &wait, cfg->nosimd);
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001412 err = check_nonfinal_ahash_op("init", err, result, digestsize,
1413 driver, vec_name, cfg);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001414 if (err)
1415 return err;
1416
1417 pending_sgl = NULL;
1418 pending_len = 0;
1419 for (i = 0; i < tsgl->nents; i++) {
1420 if (divs[i]->flush_type != FLUSH_TYPE_NONE &&
1421 pending_sgl != NULL) {
1422 /* update() with the pending data */
1423 ahash_request_set_callback(req, req_flags,
1424 crypto_req_done, &wait);
1425 ahash_request_set_crypt(req, pending_sgl, result,
1426 pending_len);
Eric Biggers65707372019-03-12 22:12:52 -07001427 err = do_ahash_op(crypto_ahash_update, req, &wait,
1428 divs[i]->nosimd);
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001429 err = check_nonfinal_ahash_op("update", err,
1430 result, digestsize,
1431 driver, vec_name, cfg);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001432 if (err)
1433 return err;
1434 pending_sgl = NULL;
1435 pending_len = 0;
1436 }
1437 if (divs[i]->flush_type == FLUSH_TYPE_REIMPORT) {
1438 /* Test ->export() and ->import() */
1439 testmgr_poison(hashstate + statesize,
1440 TESTMGR_POISON_LEN);
1441 err = crypto_ahash_export(req, hashstate);
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001442 err = check_nonfinal_ahash_op("export", err,
1443 result, digestsize,
1444 driver, vec_name, cfg);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001445 if (err)
1446 return err;
1447 if (!testmgr_is_poison(hashstate + statesize,
1448 TESTMGR_POISON_LEN)) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001449 pr_err("alg: ahash: %s export() overran state buffer on test vector %s, cfg=\"%s\"\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001450 driver, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001451 return -EOVERFLOW;
1452 }
1453
1454 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
1455 err = crypto_ahash_import(req, hashstate);
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001456 err = check_nonfinal_ahash_op("import", err,
1457 result, digestsize,
1458 driver, vec_name, cfg);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001459 if (err)
1460 return err;
1461 }
1462 if (pending_sgl == NULL)
1463 pending_sgl = &tsgl->sgl[i];
1464 pending_len += tsgl->sgl[i].length;
1465 }
1466
1467 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
1468 ahash_request_set_crypt(req, pending_sgl, result, pending_len);
1469 if (cfg->finalization_type == FINALIZATION_TYPE_FINAL) {
1470 /* finish with update() and final() */
Eric Biggers65707372019-03-12 22:12:52 -07001471 err = do_ahash_op(crypto_ahash_update, req, &wait, cfg->nosimd);
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001472 err = check_nonfinal_ahash_op("update", err, result, digestsize,
1473 driver, vec_name, cfg);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001474 if (err)
1475 return err;
Eric Biggers65707372019-03-12 22:12:52 -07001476 err = do_ahash_op(crypto_ahash_final, req, &wait, cfg->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001477 if (err) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001478 pr_err("alg: ahash: %s final() failed with err %d on test vector %s, cfg=\"%s\"\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001479 driver, err, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001480 return err;
1481 }
1482 } else {
1483 /* finish with finup() */
Eric Biggers65707372019-03-12 22:12:52 -07001484 err = do_ahash_op(crypto_ahash_finup, req, &wait, cfg->nosimd);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001485 if (err) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001486 pr_err("alg: ahash: %s finup() failed with err %d on test vector %s, cfg=\"%s\"\n",
Eric Biggers951d1332019-04-11 21:57:37 -07001487 driver, err, vec_name, cfg->name);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001488 return err;
1489 }
1490 }
1491
1492result_ready:
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001493 return check_hash_result("ahash", result, digestsize, vec, vec_name,
1494 driver, cfg);
1495}
1496
1497static int test_hash_vec_cfg(const char *driver,
1498 const struct hash_testvec *vec,
1499 const char *vec_name,
1500 const struct testvec_config *cfg,
1501 struct ahash_request *req,
1502 struct shash_desc *desc,
1503 struct test_sglist *tsgl,
1504 u8 *hashstate)
1505{
1506 int err;
1507
1508 /*
1509 * For algorithms implemented as "shash", most bugs will be detected by
1510 * both the shash and ahash tests. Test the shash API first so that the
1511 * failures involve less indirection, so are easier to debug.
1512 */
1513
1514 if (desc) {
1515 err = test_shash_vec_cfg(driver, vec, vec_name, cfg, desc, tsgl,
1516 hashstate);
1517 if (err)
1518 return err;
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001519 }
1520
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001521 return test_ahash_vec_cfg(driver, vec, vec_name, cfg, req, tsgl,
1522 hashstate);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001523}
1524
1525static int test_hash_vec(const char *driver, const struct hash_testvec *vec,
1526 unsigned int vec_num, struct ahash_request *req,
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001527 struct shash_desc *desc, struct test_sglist *tsgl,
1528 u8 *hashstate)
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001529{
Eric Biggers951d1332019-04-11 21:57:37 -07001530 char vec_name[16];
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001531 unsigned int i;
1532 int err;
1533
Eric Biggers951d1332019-04-11 21:57:37 -07001534 sprintf(vec_name, "%u", vec_num);
1535
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001536 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++) {
Eric Biggers951d1332019-04-11 21:57:37 -07001537 err = test_hash_vec_cfg(driver, vec, vec_name,
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001538 &default_hash_testvec_configs[i],
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001539 req, desc, tsgl, hashstate);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001540 if (err)
1541 return err;
1542 }
1543
1544#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1545 if (!noextratests) {
1546 struct testvec_config cfg;
1547 char cfgname[TESTVEC_CONFIG_NAMELEN];
1548
1549 for (i = 0; i < fuzz_iterations; i++) {
1550 generate_random_testvec_config(&cfg, cfgname,
1551 sizeof(cfgname));
Eric Biggers951d1332019-04-11 21:57:37 -07001552 err = test_hash_vec_cfg(driver, vec, vec_name, &cfg,
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001553 req, desc, tsgl, hashstate);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001554 if (err)
1555 return err;
Eric Biggerse63e1b02019-06-02 22:42:33 -07001556 cond_resched();
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001557 }
1558 }
1559#endif
1560 return 0;
1561}
1562
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001563#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1564/*
1565 * Generate a hash test vector from the given implementation.
1566 * Assumes the buffers in 'vec' were already allocated.
1567 */
Arnd Bergmann149c4e62019-06-18 11:21:53 +02001568static void generate_random_hash_testvec(struct shash_desc *desc,
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001569 struct hash_testvec *vec,
1570 unsigned int maxkeysize,
1571 unsigned int maxdatasize,
1572 char *name, size_t max_namelen)
1573{
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001574 /* Data */
1575 vec->psize = generate_random_length(maxdatasize);
1576 generate_random_bytes((u8 *)vec->plaintext, vec->psize);
1577
1578 /*
1579 * Key: length in range [1, maxkeysize], but usually choose maxkeysize.
1580 * If algorithm is unkeyed, then maxkeysize == 0 and set ksize = 0.
1581 */
1582 vec->setkey_error = 0;
1583 vec->ksize = 0;
1584 if (maxkeysize) {
1585 vec->ksize = maxkeysize;
1586 if (prandom_u32() % 4 == 0)
1587 vec->ksize = 1 + (prandom_u32() % maxkeysize);
1588 generate_random_bytes((u8 *)vec->key, vec->ksize);
1589
Arnd Bergmann149c4e62019-06-18 11:21:53 +02001590 vec->setkey_error = crypto_shash_setkey(desc->tfm, vec->key,
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001591 vec->ksize);
1592 /* If the key couldn't be set, no need to continue to digest. */
1593 if (vec->setkey_error)
1594 goto done;
1595 }
1596
1597 /* Digest */
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001598 vec->digest_error = crypto_shash_digest(desc, vec->plaintext,
1599 vec->psize, (u8 *)vec->digest);
1600done:
1601 snprintf(name, max_namelen, "\"random: psize=%u ksize=%u\"",
1602 vec->psize, vec->ksize);
1603}
1604
1605/*
1606 * Test the hash algorithm represented by @req against the corresponding generic
1607 * implementation, if one is available.
1608 */
1609static int test_hash_vs_generic_impl(const char *driver,
1610 const char *generic_driver,
1611 unsigned int maxkeysize,
1612 struct ahash_request *req,
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001613 struct shash_desc *desc,
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001614 struct test_sglist *tsgl,
1615 u8 *hashstate)
1616{
1617 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1618 const unsigned int digestsize = crypto_ahash_digestsize(tfm);
1619 const unsigned int blocksize = crypto_ahash_blocksize(tfm);
1620 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
1621 const char *algname = crypto_hash_alg_common(tfm)->base.cra_name;
1622 char _generic_driver[CRYPTO_MAX_ALG_NAME];
1623 struct crypto_shash *generic_tfm = NULL;
Arnd Bergmann149c4e62019-06-18 11:21:53 +02001624 struct shash_desc *generic_desc = NULL;
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001625 unsigned int i;
1626 struct hash_testvec vec = { 0 };
1627 char vec_name[64];
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02001628 struct testvec_config *cfg;
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001629 char cfgname[TESTVEC_CONFIG_NAMELEN];
1630 int err;
1631
1632 if (noextratests)
1633 return 0;
1634
1635 if (!generic_driver) { /* Use default naming convention? */
1636 err = build_generic_driver_name(algname, _generic_driver);
1637 if (err)
1638 return err;
1639 generic_driver = _generic_driver;
1640 }
1641
1642 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
1643 return 0;
1644
1645 generic_tfm = crypto_alloc_shash(generic_driver, 0, 0);
1646 if (IS_ERR(generic_tfm)) {
1647 err = PTR_ERR(generic_tfm);
1648 if (err == -ENOENT) {
1649 pr_warn("alg: hash: skipping comparison tests for %s because %s is unavailable\n",
1650 driver, generic_driver);
1651 return 0;
1652 }
1653 pr_err("alg: hash: error allocating %s (generic impl of %s): %d\n",
1654 generic_driver, algname, err);
1655 return err;
1656 }
1657
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02001658 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1659 if (!cfg) {
1660 err = -ENOMEM;
1661 goto out;
1662 }
1663
Arnd Bergmann149c4e62019-06-18 11:21:53 +02001664 generic_desc = kzalloc(sizeof(*desc) +
1665 crypto_shash_descsize(generic_tfm), GFP_KERNEL);
1666 if (!generic_desc) {
1667 err = -ENOMEM;
1668 goto out;
1669 }
1670 generic_desc->tfm = generic_tfm;
1671
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001672 /* Check the algorithm properties for consistency. */
1673
1674 if (digestsize != crypto_shash_digestsize(generic_tfm)) {
1675 pr_err("alg: hash: digestsize for %s (%u) doesn't match generic impl (%u)\n",
1676 driver, digestsize,
1677 crypto_shash_digestsize(generic_tfm));
1678 err = -EINVAL;
1679 goto out;
1680 }
1681
1682 if (blocksize != crypto_shash_blocksize(generic_tfm)) {
1683 pr_err("alg: hash: blocksize for %s (%u) doesn't match generic impl (%u)\n",
1684 driver, blocksize, crypto_shash_blocksize(generic_tfm));
1685 err = -EINVAL;
1686 goto out;
1687 }
1688
1689 /*
1690 * Now generate test vectors using the generic implementation, and test
1691 * the other implementation against them.
1692 */
1693
1694 vec.key = kmalloc(maxkeysize, GFP_KERNEL);
1695 vec.plaintext = kmalloc(maxdatasize, GFP_KERNEL);
1696 vec.digest = kmalloc(digestsize, GFP_KERNEL);
1697 if (!vec.key || !vec.plaintext || !vec.digest) {
1698 err = -ENOMEM;
1699 goto out;
1700 }
1701
1702 for (i = 0; i < fuzz_iterations * 8; i++) {
Arnd Bergmann149c4e62019-06-18 11:21:53 +02001703 generate_random_hash_testvec(generic_desc, &vec,
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001704 maxkeysize, maxdatasize,
1705 vec_name, sizeof(vec_name));
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02001706 generate_random_testvec_config(cfg, cfgname, sizeof(cfgname));
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001707
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02001708 err = test_hash_vec_cfg(driver, &vec, vec_name, cfg,
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001709 req, desc, tsgl, hashstate);
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001710 if (err)
1711 goto out;
1712 cond_resched();
1713 }
1714 err = 0;
1715out:
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02001716 kfree(cfg);
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001717 kfree(vec.key);
1718 kfree(vec.plaintext);
1719 kfree(vec.digest);
1720 crypto_free_shash(generic_tfm);
Arnd Bergmann149c4e62019-06-18 11:21:53 +02001721 kzfree(generic_desc);
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001722 return err;
1723}
1724#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1725static int test_hash_vs_generic_impl(const char *driver,
1726 const char *generic_driver,
1727 unsigned int maxkeysize,
1728 struct ahash_request *req,
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001729 struct shash_desc *desc,
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001730 struct test_sglist *tsgl,
1731 u8 *hashstate)
1732{
1733 return 0;
1734}
1735#endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
1736
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001737static int alloc_shash(const char *driver, u32 type, u32 mask,
1738 struct crypto_shash **tfm_ret,
1739 struct shash_desc **desc_ret)
1740{
1741 struct crypto_shash *tfm;
1742 struct shash_desc *desc;
1743
1744 tfm = crypto_alloc_shash(driver, type, mask);
1745 if (IS_ERR(tfm)) {
1746 if (PTR_ERR(tfm) == -ENOENT) {
1747 /*
1748 * This algorithm is only available through the ahash
1749 * API, not the shash API, so skip the shash tests.
1750 */
1751 return 0;
1752 }
1753 pr_err("alg: hash: failed to allocate shash transform for %s: %ld\n",
1754 driver, PTR_ERR(tfm));
1755 return PTR_ERR(tfm);
1756 }
1757
1758 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL);
1759 if (!desc) {
1760 crypto_free_shash(tfm);
1761 return -ENOMEM;
1762 }
1763 desc->tfm = tfm;
1764
1765 *tfm_ret = tfm;
1766 *desc_ret = desc;
1767 return 0;
1768}
1769
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001770static int __alg_test_hash(const struct hash_testvec *vecs,
1771 unsigned int num_vecs, const char *driver,
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001772 u32 type, u32 mask,
1773 const char *generic_driver, unsigned int maxkeysize)
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001774{
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001775 struct crypto_ahash *atfm = NULL;
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001776 struct ahash_request *req = NULL;
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001777 struct crypto_shash *stfm = NULL;
1778 struct shash_desc *desc = NULL;
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001779 struct test_sglist *tsgl = NULL;
1780 u8 *hashstate = NULL;
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001781 unsigned int statesize;
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001782 unsigned int i;
1783 int err;
1784
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001785 /*
1786 * Always test the ahash API. This works regardless of whether the
1787 * algorithm is implemented as ahash or shash.
1788 */
1789
1790 atfm = crypto_alloc_ahash(driver, type, mask);
1791 if (IS_ERR(atfm)) {
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001792 pr_err("alg: hash: failed to allocate transform for %s: %ld\n",
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001793 driver, PTR_ERR(atfm));
1794 return PTR_ERR(atfm);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001795 }
1796
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001797 req = ahash_request_alloc(atfm, GFP_KERNEL);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001798 if (!req) {
1799 pr_err("alg: hash: failed to allocate request for %s\n",
1800 driver);
1801 err = -ENOMEM;
1802 goto out;
1803 }
1804
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001805 /*
1806 * If available also test the shash API, to cover corner cases that may
1807 * be missed by testing the ahash API only.
1808 */
1809 err = alloc_shash(driver, type, mask, &stfm, &desc);
1810 if (err)
1811 goto out;
1812
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001813 tsgl = kmalloc(sizeof(*tsgl), GFP_KERNEL);
1814 if (!tsgl || init_test_sglist(tsgl) != 0) {
1815 pr_err("alg: hash: failed to allocate test buffers for %s\n",
1816 driver);
1817 kfree(tsgl);
1818 tsgl = NULL;
1819 err = -ENOMEM;
1820 goto out;
1821 }
1822
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001823 statesize = crypto_ahash_statesize(atfm);
1824 if (stfm)
1825 statesize = max(statesize, crypto_shash_statesize(stfm));
1826 hashstate = kmalloc(statesize + TESTMGR_POISON_LEN, GFP_KERNEL);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001827 if (!hashstate) {
1828 pr_err("alg: hash: failed to allocate hash state buffer for %s\n",
1829 driver);
1830 err = -ENOMEM;
1831 goto out;
1832 }
1833
1834 for (i = 0; i < num_vecs; i++) {
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001835 err = test_hash_vec(driver, &vecs[i], i, req, desc, tsgl,
1836 hashstate);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001837 if (err)
1838 goto out;
Eric Biggerse63e1b02019-06-02 22:42:33 -07001839 cond_resched();
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001840 }
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001841 err = test_hash_vs_generic_impl(driver, generic_driver, maxkeysize, req,
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001842 desc, tsgl, hashstate);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001843out:
1844 kfree(hashstate);
1845 if (tsgl) {
1846 destroy_test_sglist(tsgl);
1847 kfree(tsgl);
1848 }
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001849 kfree(desc);
1850 crypto_free_shash(stfm);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001851 ahash_request_free(req);
Eric Biggersd8ea98a2019-05-28 09:40:55 -07001852 crypto_free_ahash(atfm);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001853 return err;
1854}
1855
1856static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1857 u32 type, u32 mask)
1858{
1859 const struct hash_testvec *template = desc->suite.hash.vecs;
1860 unsigned int tcount = desc->suite.hash.count;
1861 unsigned int nr_unkeyed, nr_keyed;
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001862 unsigned int maxkeysize = 0;
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001863 int err;
1864
1865 /*
1866 * For OPTIONAL_KEY algorithms, we have to do all the unkeyed tests
1867 * first, before setting a key on the tfm. To make this easier, we
1868 * require that the unkeyed test vectors (if any) are listed first.
1869 */
1870
1871 for (nr_unkeyed = 0; nr_unkeyed < tcount; nr_unkeyed++) {
1872 if (template[nr_unkeyed].ksize)
1873 break;
1874 }
1875 for (nr_keyed = 0; nr_unkeyed + nr_keyed < tcount; nr_keyed++) {
1876 if (!template[nr_unkeyed + nr_keyed].ksize) {
1877 pr_err("alg: hash: test vectors for %s out of order, "
1878 "unkeyed ones must come first\n", desc->alg);
Kamil Konieczny466d7b92018-01-16 15:26:13 +01001879 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08001880 }
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001881 maxkeysize = max_t(unsigned int, maxkeysize,
1882 template[nr_unkeyed + nr_keyed].ksize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001883 }
1884
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001885 err = 0;
1886 if (nr_unkeyed) {
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001887 err = __alg_test_hash(template, nr_unkeyed, driver, type, mask,
1888 desc->generic_driver, maxkeysize);
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001889 template += nr_unkeyed;
Herbert Xuda7f0332008-07-31 17:08:25 +08001890 }
1891
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001892 if (!err && nr_keyed)
Eric Biggers9a8a6b32019-04-11 21:57:39 -07001893 err = __alg_test_hash(template, nr_keyed, driver, type, mask,
1894 desc->generic_driver, maxkeysize);
Wang, Rui Y018ba952016-02-03 18:26:57 +08001895
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001896 return err;
Jussi Kivilinnada5ffe12013-06-13 17:37:55 +03001897}
1898
Eric Biggersed968042019-01-31 23:51:47 -08001899static int test_aead_vec_cfg(const char *driver, int enc,
1900 const struct aead_testvec *vec,
Eric Biggers951d1332019-04-11 21:57:37 -07001901 const char *vec_name,
Eric Biggersed968042019-01-31 23:51:47 -08001902 const struct testvec_config *cfg,
1903 struct aead_request *req,
1904 struct cipher_test_sglists *tsgls)
Herbert Xuda7f0332008-07-31 17:08:25 +08001905{
Eric Biggersed968042019-01-31 23:51:47 -08001906 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1907 const unsigned int alignmask = crypto_aead_alignmask(tfm);
1908 const unsigned int ivsize = crypto_aead_ivsize(tfm);
1909 const unsigned int authsize = vec->clen - vec->plen;
1910 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1911 const char *op = enc ? "encryption" : "decryption";
1912 DECLARE_CRYPTO_WAIT(wait);
1913 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
1914 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
1915 cfg->iv_offset +
1916 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
1917 struct kvec input[2];
Eric Biggers5283a8e2019-04-11 21:57:36 -07001918 int expected_error;
Eric Biggersed968042019-01-31 23:51:47 -08001919 int err;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001920
Eric Biggersed968042019-01-31 23:51:47 -08001921 /* Set the key */
1922 if (vec->wk)
1923 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001924 else
Eric Biggersed968042019-01-31 23:51:47 -08001925 crypto_aead_clear_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Eric Biggersfd8c37c2019-12-01 13:53:28 -08001926
1927 err = do_setkey(crypto_aead_setkey, tfm, vec->key, vec->klen,
1928 cfg, alignmask);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001929 if (err && err != vec->setkey_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001930 pr_err("alg: aead: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
1931 driver, vec_name, vec->setkey_error, err,
Eric Biggers5283a8e2019-04-11 21:57:36 -07001932 crypto_aead_get_flags(tfm));
Eric Biggersed968042019-01-31 23:51:47 -08001933 return err;
1934 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07001935 if (!err && vec->setkey_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001936 pr_err("alg: aead: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
1937 driver, vec_name, vec->setkey_error);
Eric Biggersed968042019-01-31 23:51:47 -08001938 return -EINVAL;
1939 }
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001940
Eric Biggersed968042019-01-31 23:51:47 -08001941 /* Set the authentication tag size */
1942 err = crypto_aead_setauthsize(tfm, authsize);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001943 if (err && err != vec->setauthsize_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001944 pr_err("alg: aead: %s setauthsize failed on test vector %s; expected_error=%d, actual_error=%d\n",
1945 driver, vec_name, vec->setauthsize_error, err);
Eric Biggersed968042019-01-31 23:51:47 -08001946 return err;
1947 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07001948 if (!err && vec->setauthsize_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07001949 pr_err("alg: aead: %s setauthsize unexpectedly succeeded on test vector %s; expected_error=%d\n",
1950 driver, vec_name, vec->setauthsize_error);
Eric Biggers5283a8e2019-04-11 21:57:36 -07001951 return -EINVAL;
1952 }
1953
1954 if (vec->setkey_error || vec->setauthsize_error)
1955 return 0;
Eric Biggersed968042019-01-31 23:51:47 -08001956
1957 /* The IV must be copied to a buffer, as the algorithm may modify it */
1958 if (WARN_ON(ivsize > MAX_IVLEN))
1959 return -EINVAL;
1960 if (vec->iv)
1961 memcpy(iv, vec->iv, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001962 else
Eric Biggersed968042019-01-31 23:51:47 -08001963 memset(iv, 0, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001964
Eric Biggersed968042019-01-31 23:51:47 -08001965 /* Build the src/dst scatterlists */
1966 input[0].iov_base = (void *)vec->assoc;
1967 input[0].iov_len = vec->alen;
1968 input[1].iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
1969 input[1].iov_len = enc ? vec->plen : vec->clen;
1970 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
1971 vec->alen + (enc ? vec->plen :
1972 vec->clen),
1973 vec->alen + (enc ? vec->clen :
1974 vec->plen),
1975 input, 2);
1976 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07001977 pr_err("alg: aead: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
1978 driver, op, vec_name, cfg->name);
Eric Biggersed968042019-01-31 23:51:47 -08001979 return err;
Herbert Xuda7f0332008-07-31 17:08:25 +08001980 }
1981
Eric Biggersed968042019-01-31 23:51:47 -08001982 /* Do the actual encryption or decryption */
1983 testmgr_poison(req->__ctx, crypto_aead_reqsize(tfm));
1984 aead_request_set_callback(req, req_flags, crypto_req_done, &wait);
1985 aead_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
1986 enc ? vec->plen : vec->clen, iv);
1987 aead_request_set_ad(req, vec->alen);
Eric Biggers65707372019-03-12 22:12:52 -07001988 if (cfg->nosimd)
1989 crypto_disable_simd_for_test();
1990 err = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
1991 if (cfg->nosimd)
1992 crypto_reenable_simd_for_test();
1993 err = crypto_wait_req(err, &wait);
Herbert Xuda7f0332008-07-31 17:08:25 +08001994
Eric Biggersa6e5ef92019-01-31 23:51:50 -08001995 /* Check that the algorithm didn't overwrite things it shouldn't have */
1996 if (req->cryptlen != (enc ? vec->plen : vec->clen) ||
1997 req->assoclen != vec->alen ||
1998 req->iv != iv ||
1999 req->src != tsgls->src.sgl_ptr ||
2000 req->dst != tsgls->dst.sgl_ptr ||
2001 crypto_aead_reqtfm(req) != tfm ||
2002 req->base.complete != crypto_req_done ||
2003 req->base.flags != req_flags ||
2004 req->base.data != &wait) {
Eric Biggers951d1332019-04-11 21:57:37 -07002005 pr_err("alg: aead: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
2006 driver, op, vec_name, cfg->name);
Eric Biggersa6e5ef92019-01-31 23:51:50 -08002007 if (req->cryptlen != (enc ? vec->plen : vec->clen))
2008 pr_err("alg: aead: changed 'req->cryptlen'\n");
2009 if (req->assoclen != vec->alen)
2010 pr_err("alg: aead: changed 'req->assoclen'\n");
2011 if (req->iv != iv)
2012 pr_err("alg: aead: changed 'req->iv'\n");
2013 if (req->src != tsgls->src.sgl_ptr)
2014 pr_err("alg: aead: changed 'req->src'\n");
2015 if (req->dst != tsgls->dst.sgl_ptr)
2016 pr_err("alg: aead: changed 'req->dst'\n");
2017 if (crypto_aead_reqtfm(req) != tfm)
2018 pr_err("alg: aead: changed 'req->base.tfm'\n");
2019 if (req->base.complete != crypto_req_done)
2020 pr_err("alg: aead: changed 'req->base.complete'\n");
2021 if (req->base.flags != req_flags)
2022 pr_err("alg: aead: changed 'req->base.flags'\n");
2023 if (req->base.data != &wait)
2024 pr_err("alg: aead: changed 'req->base.data'\n");
2025 return -EINVAL;
2026 }
2027 if (is_test_sglist_corrupted(&tsgls->src)) {
Eric Biggers951d1332019-04-11 21:57:37 -07002028 pr_err("alg: aead: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
2029 driver, op, vec_name, cfg->name);
Eric Biggersa6e5ef92019-01-31 23:51:50 -08002030 return -EINVAL;
2031 }
2032 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
2033 is_test_sglist_corrupted(&tsgls->dst)) {
Eric Biggers951d1332019-04-11 21:57:37 -07002034 pr_err("alg: aead: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
2035 driver, op, vec_name, cfg->name);
Eric Biggersa6e5ef92019-01-31 23:51:50 -08002036 return -EINVAL;
2037 }
2038
Eric Biggers5283a8e2019-04-11 21:57:36 -07002039 /* Check for success or failure */
2040 expected_error = vec->novrfy ? -EBADMSG : vec->crypt_error;
2041 if (err) {
2042 if (err == expected_error)
2043 return 0;
Eric Biggers951d1332019-04-11 21:57:37 -07002044 pr_err("alg: aead: %s %s failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
2045 driver, op, vec_name, expected_error, err, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07002046 return err;
2047 }
2048 if (expected_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07002049 pr_err("alg: aead: %s %s unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
2050 driver, op, vec_name, expected_error, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07002051 return -EINVAL;
2052 }
2053
Eric Biggersed968042019-01-31 23:51:47 -08002054 /* Check for the correct output (ciphertext or plaintext) */
2055 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
2056 enc ? vec->clen : vec->plen,
2057 vec->alen, enc || !cfg->inplace);
2058 if (err == -EOVERFLOW) {
Eric Biggers951d1332019-04-11 21:57:37 -07002059 pr_err("alg: aead: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
2060 driver, op, vec_name, cfg->name);
Eric Biggersed968042019-01-31 23:51:47 -08002061 return err;
Herbert Xuda7f0332008-07-31 17:08:25 +08002062 }
Eric Biggersed968042019-01-31 23:51:47 -08002063 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07002064 pr_err("alg: aead: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
2065 driver, op, vec_name, cfg->name);
Eric Biggersed968042019-01-31 23:51:47 -08002066 return err;
Jussi Kivilinna58dcf542013-06-13 17:37:50 +03002067 }
2068
2069 return 0;
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03002070}
2071
Eric Biggersed968042019-01-31 23:51:47 -08002072static int test_aead_vec(const char *driver, int enc,
2073 const struct aead_testvec *vec, unsigned int vec_num,
2074 struct aead_request *req,
2075 struct cipher_test_sglists *tsgls)
2076{
Eric Biggers951d1332019-04-11 21:57:37 -07002077 char vec_name[16];
Eric Biggersed968042019-01-31 23:51:47 -08002078 unsigned int i;
2079 int err;
2080
2081 if (enc && vec->novrfy)
2082 return 0;
2083
Eric Biggers951d1332019-04-11 21:57:37 -07002084 sprintf(vec_name, "%u", vec_num);
2085
Eric Biggersed968042019-01-31 23:51:47 -08002086 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
Eric Biggers951d1332019-04-11 21:57:37 -07002087 err = test_aead_vec_cfg(driver, enc, vec, vec_name,
Eric Biggersed968042019-01-31 23:51:47 -08002088 &default_cipher_testvec_configs[i],
2089 req, tsgls);
2090 if (err)
2091 return err;
2092 }
2093
2094#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2095 if (!noextratests) {
2096 struct testvec_config cfg;
2097 char cfgname[TESTVEC_CONFIG_NAMELEN];
2098
2099 for (i = 0; i < fuzz_iterations; i++) {
2100 generate_random_testvec_config(&cfg, cfgname,
2101 sizeof(cfgname));
Eric Biggers951d1332019-04-11 21:57:37 -07002102 err = test_aead_vec_cfg(driver, enc, vec, vec_name,
Eric Biggersed968042019-01-31 23:51:47 -08002103 &cfg, req, tsgls);
2104 if (err)
2105 return err;
Eric Biggerse63e1b02019-06-02 22:42:33 -07002106 cond_resched();
Eric Biggersed968042019-01-31 23:51:47 -08002107 }
2108 }
2109#endif
2110 return 0;
2111}
2112
Eric Biggers40153b12019-04-11 21:57:41 -07002113#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2114/*
2115 * Generate an AEAD test vector from the given implementation.
2116 * Assumes the buffers in 'vec' were already allocated.
2117 */
2118static void generate_random_aead_testvec(struct aead_request *req,
2119 struct aead_testvec *vec,
2120 unsigned int maxkeysize,
2121 unsigned int maxdatasize,
2122 char *name, size_t max_namelen)
2123{
2124 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2125 const unsigned int ivsize = crypto_aead_ivsize(tfm);
2126 unsigned int maxauthsize = crypto_aead_alg(tfm)->maxauthsize;
2127 unsigned int authsize;
2128 unsigned int total_len;
2129 int i;
2130 struct scatterlist src[2], dst;
2131 u8 iv[MAX_IVLEN];
2132 DECLARE_CRYPTO_WAIT(wait);
2133
2134 /* Key: length in [0, maxkeysize], but usually choose maxkeysize */
2135 vec->klen = maxkeysize;
2136 if (prandom_u32() % 4 == 0)
2137 vec->klen = prandom_u32() % (maxkeysize + 1);
2138 generate_random_bytes((u8 *)vec->key, vec->klen);
2139 vec->setkey_error = crypto_aead_setkey(tfm, vec->key, vec->klen);
2140
2141 /* IV */
2142 generate_random_bytes((u8 *)vec->iv, ivsize);
2143
2144 /* Tag length: in [0, maxauthsize], but usually choose maxauthsize */
2145 authsize = maxauthsize;
2146 if (prandom_u32() % 4 == 0)
2147 authsize = prandom_u32() % (maxauthsize + 1);
2148 if (WARN_ON(authsize > maxdatasize))
2149 authsize = maxdatasize;
2150 maxdatasize -= authsize;
2151 vec->setauthsize_error = crypto_aead_setauthsize(tfm, authsize);
2152
2153 /* Plaintext and associated data */
2154 total_len = generate_random_length(maxdatasize);
2155 if (prandom_u32() % 4 == 0)
2156 vec->alen = 0;
2157 else
2158 vec->alen = generate_random_length(total_len);
2159 vec->plen = total_len - vec->alen;
2160 generate_random_bytes((u8 *)vec->assoc, vec->alen);
2161 generate_random_bytes((u8 *)vec->ptext, vec->plen);
2162
2163 vec->clen = vec->plen + authsize;
2164
2165 /*
2166 * If the key or authentication tag size couldn't be set, no need to
2167 * continue to encrypt.
2168 */
Eric Biggerseb455db2019-12-01 13:53:26 -08002169 vec->crypt_error = 0;
Eric Biggers40153b12019-04-11 21:57:41 -07002170 if (vec->setkey_error || vec->setauthsize_error)
2171 goto done;
2172
2173 /* Ciphertext */
2174 sg_init_table(src, 2);
2175 i = 0;
2176 if (vec->alen)
2177 sg_set_buf(&src[i++], vec->assoc, vec->alen);
2178 if (vec->plen)
2179 sg_set_buf(&src[i++], vec->ptext, vec->plen);
2180 sg_init_one(&dst, vec->ctext, vec->alen + vec->clen);
2181 memcpy(iv, vec->iv, ivsize);
2182 aead_request_set_callback(req, 0, crypto_req_done, &wait);
2183 aead_request_set_crypt(req, src, &dst, vec->plen, iv);
2184 aead_request_set_ad(req, vec->alen);
2185 vec->crypt_error = crypto_wait_req(crypto_aead_encrypt(req), &wait);
2186 if (vec->crypt_error == 0)
2187 memmove((u8 *)vec->ctext, vec->ctext + vec->alen, vec->clen);
2188done:
2189 snprintf(name, max_namelen,
2190 "\"random: alen=%u plen=%u authsize=%u klen=%u\"",
2191 vec->alen, vec->plen, authsize, vec->klen);
2192}
2193
2194/*
2195 * Test the AEAD algorithm represented by @req against the corresponding generic
2196 * implementation, if one is available.
2197 */
2198static int test_aead_vs_generic_impl(const char *driver,
2199 const struct alg_test_desc *test_desc,
2200 struct aead_request *req,
2201 struct cipher_test_sglists *tsgls)
2202{
2203 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2204 const unsigned int ivsize = crypto_aead_ivsize(tfm);
2205 const unsigned int maxauthsize = crypto_aead_alg(tfm)->maxauthsize;
2206 const unsigned int blocksize = crypto_aead_blocksize(tfm);
2207 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
2208 const char *algname = crypto_aead_alg(tfm)->base.cra_name;
2209 const char *generic_driver = test_desc->generic_driver;
2210 char _generic_driver[CRYPTO_MAX_ALG_NAME];
2211 struct crypto_aead *generic_tfm = NULL;
2212 struct aead_request *generic_req = NULL;
2213 unsigned int maxkeysize;
2214 unsigned int i;
2215 struct aead_testvec vec = { 0 };
2216 char vec_name[64];
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002217 struct testvec_config *cfg;
Eric Biggers40153b12019-04-11 21:57:41 -07002218 char cfgname[TESTVEC_CONFIG_NAMELEN];
2219 int err;
2220
2221 if (noextratests)
2222 return 0;
2223
2224 if (!generic_driver) { /* Use default naming convention? */
2225 err = build_generic_driver_name(algname, _generic_driver);
2226 if (err)
2227 return err;
2228 generic_driver = _generic_driver;
2229 }
2230
2231 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
2232 return 0;
2233
2234 generic_tfm = crypto_alloc_aead(generic_driver, 0, 0);
2235 if (IS_ERR(generic_tfm)) {
2236 err = PTR_ERR(generic_tfm);
2237 if (err == -ENOENT) {
2238 pr_warn("alg: aead: skipping comparison tests for %s because %s is unavailable\n",
2239 driver, generic_driver);
2240 return 0;
2241 }
2242 pr_err("alg: aead: error allocating %s (generic impl of %s): %d\n",
2243 generic_driver, algname, err);
2244 return err;
2245 }
2246
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002247 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
2248 if (!cfg) {
2249 err = -ENOMEM;
2250 goto out;
2251 }
2252
Eric Biggers40153b12019-04-11 21:57:41 -07002253 generic_req = aead_request_alloc(generic_tfm, GFP_KERNEL);
2254 if (!generic_req) {
2255 err = -ENOMEM;
2256 goto out;
2257 }
2258
2259 /* Check the algorithm properties for consistency. */
2260
2261 if (maxauthsize != crypto_aead_alg(generic_tfm)->maxauthsize) {
2262 pr_err("alg: aead: maxauthsize for %s (%u) doesn't match generic impl (%u)\n",
2263 driver, maxauthsize,
2264 crypto_aead_alg(generic_tfm)->maxauthsize);
2265 err = -EINVAL;
2266 goto out;
2267 }
2268
2269 if (ivsize != crypto_aead_ivsize(generic_tfm)) {
2270 pr_err("alg: aead: ivsize for %s (%u) doesn't match generic impl (%u)\n",
2271 driver, ivsize, crypto_aead_ivsize(generic_tfm));
2272 err = -EINVAL;
2273 goto out;
2274 }
2275
2276 if (blocksize != crypto_aead_blocksize(generic_tfm)) {
2277 pr_err("alg: aead: blocksize for %s (%u) doesn't match generic impl (%u)\n",
2278 driver, blocksize, crypto_aead_blocksize(generic_tfm));
2279 err = -EINVAL;
2280 goto out;
2281 }
2282
2283 /*
2284 * Now generate test vectors using the generic implementation, and test
2285 * the other implementation against them.
2286 */
2287
2288 maxkeysize = 0;
2289 for (i = 0; i < test_desc->suite.aead.count; i++)
2290 maxkeysize = max_t(unsigned int, maxkeysize,
2291 test_desc->suite.aead.vecs[i].klen);
2292
2293 vec.key = kmalloc(maxkeysize, GFP_KERNEL);
2294 vec.iv = kmalloc(ivsize, GFP_KERNEL);
2295 vec.assoc = kmalloc(maxdatasize, GFP_KERNEL);
2296 vec.ptext = kmalloc(maxdatasize, GFP_KERNEL);
2297 vec.ctext = kmalloc(maxdatasize, GFP_KERNEL);
2298 if (!vec.key || !vec.iv || !vec.assoc || !vec.ptext || !vec.ctext) {
2299 err = -ENOMEM;
2300 goto out;
2301 }
2302
2303 for (i = 0; i < fuzz_iterations * 8; i++) {
2304 generate_random_aead_testvec(generic_req, &vec,
2305 maxkeysize, maxdatasize,
2306 vec_name, sizeof(vec_name));
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002307 generate_random_testvec_config(cfg, cfgname, sizeof(cfgname));
Eric Biggers40153b12019-04-11 21:57:41 -07002308
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002309 err = test_aead_vec_cfg(driver, ENCRYPT, &vec, vec_name, cfg,
Eric Biggers40153b12019-04-11 21:57:41 -07002310 req, tsgls);
2311 if (err)
2312 goto out;
Eric Biggerseb455db2019-12-01 13:53:26 -08002313 if (vec.crypt_error == 0) {
2314 err = test_aead_vec_cfg(driver, DECRYPT, &vec, vec_name,
2315 cfg, req, tsgls);
2316 if (err)
2317 goto out;
2318 }
Eric Biggers40153b12019-04-11 21:57:41 -07002319 cond_resched();
2320 }
2321 err = 0;
2322out:
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002323 kfree(cfg);
Eric Biggers40153b12019-04-11 21:57:41 -07002324 kfree(vec.key);
2325 kfree(vec.iv);
2326 kfree(vec.assoc);
2327 kfree(vec.ptext);
2328 kfree(vec.ctext);
2329 crypto_free_aead(generic_tfm);
2330 aead_request_free(generic_req);
2331 return err;
2332}
2333#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
2334static int test_aead_vs_generic_impl(const char *driver,
2335 const struct alg_test_desc *test_desc,
2336 struct aead_request *req,
2337 struct cipher_test_sglists *tsgls)
2338{
2339 return 0;
2340}
2341#endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
2342
Eric Biggersed968042019-01-31 23:51:47 -08002343static int test_aead(const char *driver, int enc,
2344 const struct aead_test_suite *suite,
2345 struct aead_request *req,
2346 struct cipher_test_sglists *tsgls)
2347{
2348 unsigned int i;
2349 int err;
2350
2351 for (i = 0; i < suite->count; i++) {
2352 err = test_aead_vec(driver, enc, &suite->vecs[i], i, req,
2353 tsgls);
2354 if (err)
2355 return err;
Eric Biggerse63e1b02019-06-02 22:42:33 -07002356 cond_resched();
Eric Biggersed968042019-01-31 23:51:47 -08002357 }
2358 return 0;
2359}
2360
2361static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
2362 u32 type, u32 mask)
2363{
2364 const struct aead_test_suite *suite = &desc->suite.aead;
2365 struct crypto_aead *tfm;
2366 struct aead_request *req = NULL;
2367 struct cipher_test_sglists *tsgls = NULL;
2368 int err;
2369
2370 if (suite->count <= 0) {
2371 pr_err("alg: aead: empty test suite for %s\n", driver);
2372 return -EINVAL;
2373 }
2374
2375 tfm = crypto_alloc_aead(driver, type, mask);
2376 if (IS_ERR(tfm)) {
2377 pr_err("alg: aead: failed to allocate transform for %s: %ld\n",
2378 driver, PTR_ERR(tfm));
2379 return PTR_ERR(tfm);
2380 }
2381
2382 req = aead_request_alloc(tfm, GFP_KERNEL);
2383 if (!req) {
2384 pr_err("alg: aead: failed to allocate request for %s\n",
2385 driver);
2386 err = -ENOMEM;
2387 goto out;
2388 }
2389
2390 tsgls = alloc_cipher_test_sglists();
2391 if (!tsgls) {
2392 pr_err("alg: aead: failed to allocate test buffers for %s\n",
2393 driver);
2394 err = -ENOMEM;
2395 goto out;
2396 }
2397
2398 err = test_aead(driver, ENCRYPT, suite, req, tsgls);
2399 if (err)
2400 goto out;
2401
2402 err = test_aead(driver, DECRYPT, suite, req, tsgls);
Eric Biggers40153b12019-04-11 21:57:41 -07002403 if (err)
2404 goto out;
2405
2406 err = test_aead_vs_generic_impl(driver, desc, req, tsgls);
Eric Biggersed968042019-01-31 23:51:47 -08002407out:
2408 free_cipher_test_sglists(tsgls);
2409 aead_request_free(req);
2410 crypto_free_aead(tfm);
2411 return err;
2412}
2413
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002414static int test_cipher(struct crypto_cipher *tfm, int enc,
Eric Biggersb13b1e02017-02-24 15:46:59 -08002415 const struct cipher_testvec *template,
2416 unsigned int tcount)
Herbert Xuda7f0332008-07-31 17:08:25 +08002417{
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002418 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
2419 unsigned int i, j, k;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002420 char *q;
2421 const char *e;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002422 const char *input, *result;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002423 void *data;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08002424 char *xbuf[XBUFSIZE];
2425 int ret = -ENOMEM;
2426
2427 if (testmgr_alloc_buf(xbuf))
2428 goto out_nobuf;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002429
2430 if (enc == ENCRYPT)
2431 e = "encryption";
2432 else
2433 e = "decryption";
2434
2435 j = 0;
2436 for (i = 0; i < tcount; i++) {
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002437
Stephan Mueller10faa8c2016-08-25 15:15:01 +02002438 if (fips_enabled && template[i].fips_skip)
2439 continue;
2440
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002441 input = enc ? template[i].ptext : template[i].ctext;
2442 result = enc ? template[i].ctext : template[i].ptext;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002443 j++;
2444
Herbert Xufd57f222009-05-29 16:05:42 +10002445 ret = -EINVAL;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002446 if (WARN_ON(template[i].len > PAGE_SIZE))
Herbert Xufd57f222009-05-29 16:05:42 +10002447 goto out;
2448
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002449 data = xbuf[0];
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002450 memcpy(data, input, template[i].len);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002451
2452 crypto_cipher_clear_flags(tfm, ~0);
2453 if (template[i].wk)
Eric Biggers231baec2019-01-18 22:48:00 -08002454 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002455
2456 ret = crypto_cipher_setkey(tfm, template[i].key,
2457 template[i].klen);
Eric Biggers5283a8e2019-04-11 21:57:36 -07002458 if (ret) {
2459 if (ret == template[i].setkey_error)
2460 continue;
2461 pr_err("alg: cipher: %s setkey failed on test vector %u; expected_error=%d, actual_error=%d, flags=%#x\n",
2462 algo, j, template[i].setkey_error, ret,
2463 crypto_cipher_get_flags(tfm));
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002464 goto out;
Eric Biggers5283a8e2019-04-11 21:57:36 -07002465 }
2466 if (template[i].setkey_error) {
2467 pr_err("alg: cipher: %s setkey unexpectedly succeeded on test vector %u; expected_error=%d\n",
2468 algo, j, template[i].setkey_error);
2469 ret = -EINVAL;
2470 goto out;
2471 }
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002472
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002473 for (k = 0; k < template[i].len;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002474 k += crypto_cipher_blocksize(tfm)) {
2475 if (enc)
2476 crypto_cipher_encrypt_one(tfm, data + k,
2477 data + k);
2478 else
2479 crypto_cipher_decrypt_one(tfm, data + k,
2480 data + k);
2481 }
2482
2483 q = data;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002484 if (memcmp(q, result, template[i].len)) {
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002485 printk(KERN_ERR "alg: cipher: Test %d failed "
2486 "on %s for %s\n", j, e, algo);
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002487 hexdump(q, template[i].len);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002488 ret = -EINVAL;
2489 goto out;
2490 }
2491 }
2492
2493 ret = 0;
2494
2495out:
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08002496 testmgr_free_buf(xbuf);
2497out_nobuf:
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002498 return ret;
2499}
2500
Eric Biggers4e7babba2019-01-31 23:51:46 -08002501static int test_skcipher_vec_cfg(const char *driver, int enc,
2502 const struct cipher_testvec *vec,
Eric Biggers951d1332019-04-11 21:57:37 -07002503 const char *vec_name,
Eric Biggers4e7babba2019-01-31 23:51:46 -08002504 const struct testvec_config *cfg,
2505 struct skcipher_request *req,
2506 struct cipher_test_sglists *tsgls)
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002507{
Eric Biggers4e7babba2019-01-31 23:51:46 -08002508 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2509 const unsigned int alignmask = crypto_skcipher_alignmask(tfm);
2510 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2511 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
2512 const char *op = enc ? "encryption" : "decryption";
2513 DECLARE_CRYPTO_WAIT(wait);
2514 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
2515 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
2516 cfg->iv_offset +
2517 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
2518 struct kvec input;
2519 int err;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08002520
Eric Biggers4e7babba2019-01-31 23:51:46 -08002521 /* Set the key */
2522 if (vec->wk)
2523 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03002524 else
Eric Biggers4e7babba2019-01-31 23:51:46 -08002525 crypto_skcipher_clear_flags(tfm,
2526 CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Eric Biggersfd8c37c2019-12-01 13:53:28 -08002527 err = do_setkey(crypto_skcipher_setkey, tfm, vec->key, vec->klen,
2528 cfg, alignmask);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002529 if (err) {
Eric Biggers5283a8e2019-04-11 21:57:36 -07002530 if (err == vec->setkey_error)
Eric Biggers4e7babba2019-01-31 23:51:46 -08002531 return 0;
Eric Biggers951d1332019-04-11 21:57:37 -07002532 pr_err("alg: skcipher: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
2533 driver, vec_name, vec->setkey_error, err,
Eric Biggers5283a8e2019-04-11 21:57:36 -07002534 crypto_skcipher_get_flags(tfm));
Eric Biggers4e7babba2019-01-31 23:51:46 -08002535 return err;
2536 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07002537 if (vec->setkey_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07002538 pr_err("alg: skcipher: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
2539 driver, vec_name, vec->setkey_error);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002540 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08002541 }
2542
Eric Biggers4e7babba2019-01-31 23:51:46 -08002543 /* The IV must be copied to a buffer, as the algorithm may modify it */
2544 if (ivsize) {
2545 if (WARN_ON(ivsize > MAX_IVLEN))
2546 return -EINVAL;
Eric Biggers8efd9722019-02-14 00:03:51 -08002547 if (vec->generates_iv && !enc)
2548 memcpy(iv, vec->iv_out, ivsize);
2549 else if (vec->iv)
Eric Biggers4e7babba2019-01-31 23:51:46 -08002550 memcpy(iv, vec->iv, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08002551 else
Eric Biggers4e7babba2019-01-31 23:51:46 -08002552 memset(iv, 0, ivsize);
2553 } else {
2554 if (vec->generates_iv) {
Eric Biggers951d1332019-04-11 21:57:37 -07002555 pr_err("alg: skcipher: %s has ivsize=0 but test vector %s generates IV!\n",
2556 driver, vec_name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002557 return -EINVAL;
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03002558 }
Eric Biggers4e7babba2019-01-31 23:51:46 -08002559 iv = NULL;
Herbert Xuda7f0332008-07-31 17:08:25 +08002560 }
2561
Eric Biggers4e7babba2019-01-31 23:51:46 -08002562 /* Build the src/dst scatterlists */
2563 input.iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
2564 input.iov_len = vec->len;
2565 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
2566 vec->len, vec->len, &input, 1);
2567 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07002568 pr_err("alg: skcipher: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
2569 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002570 return err;
Herbert Xuda7f0332008-07-31 17:08:25 +08002571 }
2572
Eric Biggers4e7babba2019-01-31 23:51:46 -08002573 /* Do the actual encryption or decryption */
2574 testmgr_poison(req->__ctx, crypto_skcipher_reqsize(tfm));
2575 skcipher_request_set_callback(req, req_flags, crypto_req_done, &wait);
2576 skcipher_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
2577 vec->len, iv);
Eric Biggers65707372019-03-12 22:12:52 -07002578 if (cfg->nosimd)
2579 crypto_disable_simd_for_test();
2580 err = enc ? crypto_skcipher_encrypt(req) : crypto_skcipher_decrypt(req);
2581 if (cfg->nosimd)
2582 crypto_reenable_simd_for_test();
2583 err = crypto_wait_req(err, &wait);
Herbert Xuda7f0332008-07-31 17:08:25 +08002584
Eric Biggersfa353c92019-01-31 23:51:49 -08002585 /* Check that the algorithm didn't overwrite things it shouldn't have */
2586 if (req->cryptlen != vec->len ||
2587 req->iv != iv ||
2588 req->src != tsgls->src.sgl_ptr ||
2589 req->dst != tsgls->dst.sgl_ptr ||
2590 crypto_skcipher_reqtfm(req) != tfm ||
2591 req->base.complete != crypto_req_done ||
2592 req->base.flags != req_flags ||
2593 req->base.data != &wait) {
Eric Biggers951d1332019-04-11 21:57:37 -07002594 pr_err("alg: skcipher: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
2595 driver, op, vec_name, cfg->name);
Eric Biggersfa353c92019-01-31 23:51:49 -08002596 if (req->cryptlen != vec->len)
2597 pr_err("alg: skcipher: changed 'req->cryptlen'\n");
2598 if (req->iv != iv)
2599 pr_err("alg: skcipher: changed 'req->iv'\n");
2600 if (req->src != tsgls->src.sgl_ptr)
2601 pr_err("alg: skcipher: changed 'req->src'\n");
2602 if (req->dst != tsgls->dst.sgl_ptr)
2603 pr_err("alg: skcipher: changed 'req->dst'\n");
2604 if (crypto_skcipher_reqtfm(req) != tfm)
2605 pr_err("alg: skcipher: changed 'req->base.tfm'\n");
2606 if (req->base.complete != crypto_req_done)
2607 pr_err("alg: skcipher: changed 'req->base.complete'\n");
2608 if (req->base.flags != req_flags)
2609 pr_err("alg: skcipher: changed 'req->base.flags'\n");
2610 if (req->base.data != &wait)
2611 pr_err("alg: skcipher: changed 'req->base.data'\n");
2612 return -EINVAL;
2613 }
2614 if (is_test_sglist_corrupted(&tsgls->src)) {
Eric Biggers951d1332019-04-11 21:57:37 -07002615 pr_err("alg: skcipher: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
2616 driver, op, vec_name, cfg->name);
Eric Biggersfa353c92019-01-31 23:51:49 -08002617 return -EINVAL;
2618 }
2619 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
2620 is_test_sglist_corrupted(&tsgls->dst)) {
Eric Biggers951d1332019-04-11 21:57:37 -07002621 pr_err("alg: skcipher: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
2622 driver, op, vec_name, cfg->name);
Eric Biggersfa353c92019-01-31 23:51:49 -08002623 return -EINVAL;
2624 }
2625
Eric Biggers5283a8e2019-04-11 21:57:36 -07002626 /* Check for success or failure */
2627 if (err) {
2628 if (err == vec->crypt_error)
2629 return 0;
Eric Biggers951d1332019-04-11 21:57:37 -07002630 pr_err("alg: skcipher: %s %s failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
2631 driver, op, vec_name, vec->crypt_error, err, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07002632 return err;
2633 }
2634 if (vec->crypt_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07002635 pr_err("alg: skcipher: %s %s unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
2636 driver, op, vec_name, vec->crypt_error, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07002637 return -EINVAL;
2638 }
2639
Eric Biggers4e7babba2019-01-31 23:51:46 -08002640 /* Check for the correct output (ciphertext or plaintext) */
2641 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
2642 vec->len, 0, true);
2643 if (err == -EOVERFLOW) {
Eric Biggers951d1332019-04-11 21:57:37 -07002644 pr_err("alg: skcipher: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
2645 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002646 return err;
2647 }
2648 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07002649 pr_err("alg: skcipher: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
2650 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002651 return err;
2652 }
Herbert Xuda7f0332008-07-31 17:08:25 +08002653
Eric Biggers4e7babba2019-01-31 23:51:46 -08002654 /* If applicable, check that the algorithm generated the correct IV */
Eric Biggers8efd9722019-02-14 00:03:51 -08002655 if (vec->iv_out && memcmp(iv, vec->iv_out, ivsize) != 0) {
Eric Biggers951d1332019-04-11 21:57:37 -07002656 pr_err("alg: skcipher: %s %s test failed (wrong output IV) on test vector %s, cfg=\"%s\"\n",
2657 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002658 hexdump(iv, ivsize);
2659 return -EINVAL;
Jussi Kivilinna3a338f22013-06-13 17:37:45 +03002660 }
2661
2662 return 0;
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03002663}
2664
Eric Biggers4e7babba2019-01-31 23:51:46 -08002665static int test_skcipher_vec(const char *driver, int enc,
2666 const struct cipher_testvec *vec,
2667 unsigned int vec_num,
2668 struct skcipher_request *req,
2669 struct cipher_test_sglists *tsgls)
2670{
Eric Biggers951d1332019-04-11 21:57:37 -07002671 char vec_name[16];
Eric Biggers4e7babba2019-01-31 23:51:46 -08002672 unsigned int i;
2673 int err;
2674
2675 if (fips_enabled && vec->fips_skip)
2676 return 0;
2677
Eric Biggers951d1332019-04-11 21:57:37 -07002678 sprintf(vec_name, "%u", vec_num);
2679
Eric Biggers4e7babba2019-01-31 23:51:46 -08002680 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
Eric Biggers951d1332019-04-11 21:57:37 -07002681 err = test_skcipher_vec_cfg(driver, enc, vec, vec_name,
Eric Biggers4e7babba2019-01-31 23:51:46 -08002682 &default_cipher_testvec_configs[i],
2683 req, tsgls);
2684 if (err)
2685 return err;
2686 }
2687
2688#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2689 if (!noextratests) {
2690 struct testvec_config cfg;
2691 char cfgname[TESTVEC_CONFIG_NAMELEN];
2692
2693 for (i = 0; i < fuzz_iterations; i++) {
2694 generate_random_testvec_config(&cfg, cfgname,
2695 sizeof(cfgname));
Eric Biggers951d1332019-04-11 21:57:37 -07002696 err = test_skcipher_vec_cfg(driver, enc, vec, vec_name,
Eric Biggers4e7babba2019-01-31 23:51:46 -08002697 &cfg, req, tsgls);
2698 if (err)
2699 return err;
Eric Biggerse63e1b02019-06-02 22:42:33 -07002700 cond_resched();
Eric Biggers4e7babba2019-01-31 23:51:46 -08002701 }
2702 }
2703#endif
2704 return 0;
2705}
2706
Eric Biggersd435e102019-04-11 21:57:40 -07002707#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2708/*
2709 * Generate a symmetric cipher test vector from the given implementation.
2710 * Assumes the buffers in 'vec' were already allocated.
2711 */
2712static void generate_random_cipher_testvec(struct skcipher_request *req,
2713 struct cipher_testvec *vec,
2714 unsigned int maxdatasize,
2715 char *name, size_t max_namelen)
2716{
2717 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
Eric Biggers9ac0d132019-11-29 10:23:04 -08002718 const unsigned int maxkeysize = crypto_skcipher_max_keysize(tfm);
Eric Biggersd435e102019-04-11 21:57:40 -07002719 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2720 struct scatterlist src, dst;
2721 u8 iv[MAX_IVLEN];
2722 DECLARE_CRYPTO_WAIT(wait);
2723
2724 /* Key: length in [0, maxkeysize], but usually choose maxkeysize */
2725 vec->klen = maxkeysize;
2726 if (prandom_u32() % 4 == 0)
2727 vec->klen = prandom_u32() % (maxkeysize + 1);
2728 generate_random_bytes((u8 *)vec->key, vec->klen);
2729 vec->setkey_error = crypto_skcipher_setkey(tfm, vec->key, vec->klen);
2730
2731 /* IV */
2732 generate_random_bytes((u8 *)vec->iv, ivsize);
2733
2734 /* Plaintext */
2735 vec->len = generate_random_length(maxdatasize);
2736 generate_random_bytes((u8 *)vec->ptext, vec->len);
2737
2738 /* If the key couldn't be set, no need to continue to encrypt. */
2739 if (vec->setkey_error)
2740 goto done;
2741
2742 /* Ciphertext */
2743 sg_init_one(&src, vec->ptext, vec->len);
2744 sg_init_one(&dst, vec->ctext, vec->len);
2745 memcpy(iv, vec->iv, ivsize);
2746 skcipher_request_set_callback(req, 0, crypto_req_done, &wait);
2747 skcipher_request_set_crypt(req, &src, &dst, vec->len, iv);
2748 vec->crypt_error = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
Eric Biggerseb455db2019-12-01 13:53:26 -08002749 if (vec->crypt_error != 0) {
2750 /*
2751 * The only acceptable error here is for an invalid length, so
2752 * skcipher decryption should fail with the same error too.
2753 * We'll test for this. But to keep the API usage well-defined,
2754 * explicitly initialize the ciphertext buffer too.
2755 */
2756 memset((u8 *)vec->ctext, 0, vec->len);
2757 }
Eric Biggersd435e102019-04-11 21:57:40 -07002758done:
2759 snprintf(name, max_namelen, "\"random: len=%u klen=%u\"",
2760 vec->len, vec->klen);
2761}
2762
2763/*
2764 * Test the skcipher algorithm represented by @req against the corresponding
2765 * generic implementation, if one is available.
2766 */
2767static int test_skcipher_vs_generic_impl(const char *driver,
2768 const char *generic_driver,
2769 struct skcipher_request *req,
2770 struct cipher_test_sglists *tsgls)
2771{
2772 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
Eric Biggers9ac0d132019-11-29 10:23:04 -08002773 const unsigned int maxkeysize = crypto_skcipher_max_keysize(tfm);
Eric Biggersd435e102019-04-11 21:57:40 -07002774 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2775 const unsigned int blocksize = crypto_skcipher_blocksize(tfm);
2776 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
2777 const char *algname = crypto_skcipher_alg(tfm)->base.cra_name;
2778 char _generic_driver[CRYPTO_MAX_ALG_NAME];
2779 struct crypto_skcipher *generic_tfm = NULL;
2780 struct skcipher_request *generic_req = NULL;
2781 unsigned int i;
2782 struct cipher_testvec vec = { 0 };
2783 char vec_name[64];
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002784 struct testvec_config *cfg;
Eric Biggersd435e102019-04-11 21:57:40 -07002785 char cfgname[TESTVEC_CONFIG_NAMELEN];
2786 int err;
2787
2788 if (noextratests)
2789 return 0;
2790
2791 /* Keywrap isn't supported here yet as it handles its IV differently. */
2792 if (strncmp(algname, "kw(", 3) == 0)
2793 return 0;
2794
2795 if (!generic_driver) { /* Use default naming convention? */
2796 err = build_generic_driver_name(algname, _generic_driver);
2797 if (err)
2798 return err;
2799 generic_driver = _generic_driver;
2800 }
2801
2802 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
2803 return 0;
2804
2805 generic_tfm = crypto_alloc_skcipher(generic_driver, 0, 0);
2806 if (IS_ERR(generic_tfm)) {
2807 err = PTR_ERR(generic_tfm);
2808 if (err == -ENOENT) {
2809 pr_warn("alg: skcipher: skipping comparison tests for %s because %s is unavailable\n",
2810 driver, generic_driver);
2811 return 0;
2812 }
2813 pr_err("alg: skcipher: error allocating %s (generic impl of %s): %d\n",
2814 generic_driver, algname, err);
2815 return err;
2816 }
2817
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002818 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
2819 if (!cfg) {
2820 err = -ENOMEM;
2821 goto out;
2822 }
2823
Eric Biggersd435e102019-04-11 21:57:40 -07002824 generic_req = skcipher_request_alloc(generic_tfm, GFP_KERNEL);
2825 if (!generic_req) {
2826 err = -ENOMEM;
2827 goto out;
2828 }
2829
2830 /* Check the algorithm properties for consistency. */
2831
Eric Biggersfd60f722019-12-01 13:53:27 -08002832 if (crypto_skcipher_min_keysize(tfm) !=
2833 crypto_skcipher_min_keysize(generic_tfm)) {
2834 pr_err("alg: skcipher: min keysize for %s (%u) doesn't match generic impl (%u)\n",
2835 driver, crypto_skcipher_min_keysize(tfm),
2836 crypto_skcipher_min_keysize(generic_tfm));
2837 err = -EINVAL;
2838 goto out;
2839 }
2840
Eric Biggers9ac0d132019-11-29 10:23:04 -08002841 if (maxkeysize != crypto_skcipher_max_keysize(generic_tfm)) {
Eric Biggersd435e102019-04-11 21:57:40 -07002842 pr_err("alg: skcipher: max keysize for %s (%u) doesn't match generic impl (%u)\n",
Eric Biggers9ac0d132019-11-29 10:23:04 -08002843 driver, maxkeysize,
2844 crypto_skcipher_max_keysize(generic_tfm));
Eric Biggersd435e102019-04-11 21:57:40 -07002845 err = -EINVAL;
2846 goto out;
2847 }
2848
2849 if (ivsize != crypto_skcipher_ivsize(generic_tfm)) {
2850 pr_err("alg: skcipher: ivsize for %s (%u) doesn't match generic impl (%u)\n",
2851 driver, ivsize, crypto_skcipher_ivsize(generic_tfm));
2852 err = -EINVAL;
2853 goto out;
2854 }
2855
2856 if (blocksize != crypto_skcipher_blocksize(generic_tfm)) {
2857 pr_err("alg: skcipher: blocksize for %s (%u) doesn't match generic impl (%u)\n",
2858 driver, blocksize,
2859 crypto_skcipher_blocksize(generic_tfm));
2860 err = -EINVAL;
2861 goto out;
2862 }
2863
2864 /*
2865 * Now generate test vectors using the generic implementation, and test
2866 * the other implementation against them.
2867 */
2868
Eric Biggers9ac0d132019-11-29 10:23:04 -08002869 vec.key = kmalloc(maxkeysize, GFP_KERNEL);
Eric Biggersd435e102019-04-11 21:57:40 -07002870 vec.iv = kmalloc(ivsize, GFP_KERNEL);
2871 vec.ptext = kmalloc(maxdatasize, GFP_KERNEL);
2872 vec.ctext = kmalloc(maxdatasize, GFP_KERNEL);
2873 if (!vec.key || !vec.iv || !vec.ptext || !vec.ctext) {
2874 err = -ENOMEM;
2875 goto out;
2876 }
2877
2878 for (i = 0; i < fuzz_iterations * 8; i++) {
2879 generate_random_cipher_testvec(generic_req, &vec, maxdatasize,
2880 vec_name, sizeof(vec_name));
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002881 generate_random_testvec_config(cfg, cfgname, sizeof(cfgname));
Eric Biggersd435e102019-04-11 21:57:40 -07002882
2883 err = test_skcipher_vec_cfg(driver, ENCRYPT, &vec, vec_name,
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002884 cfg, req, tsgls);
Eric Biggersd435e102019-04-11 21:57:40 -07002885 if (err)
2886 goto out;
2887 err = test_skcipher_vec_cfg(driver, DECRYPT, &vec, vec_name,
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002888 cfg, req, tsgls);
Eric Biggersd435e102019-04-11 21:57:40 -07002889 if (err)
2890 goto out;
2891 cond_resched();
2892 }
2893 err = 0;
2894out:
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002895 kfree(cfg);
Eric Biggersd435e102019-04-11 21:57:40 -07002896 kfree(vec.key);
2897 kfree(vec.iv);
2898 kfree(vec.ptext);
2899 kfree(vec.ctext);
2900 crypto_free_skcipher(generic_tfm);
2901 skcipher_request_free(generic_req);
2902 return err;
2903}
2904#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
2905static int test_skcipher_vs_generic_impl(const char *driver,
2906 const char *generic_driver,
2907 struct skcipher_request *req,
2908 struct cipher_test_sglists *tsgls)
2909{
2910 return 0;
2911}
2912#endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
2913
Eric Biggers4e7babba2019-01-31 23:51:46 -08002914static int test_skcipher(const char *driver, int enc,
2915 const struct cipher_test_suite *suite,
2916 struct skcipher_request *req,
2917 struct cipher_test_sglists *tsgls)
2918{
2919 unsigned int i;
2920 int err;
2921
2922 for (i = 0; i < suite->count; i++) {
2923 err = test_skcipher_vec(driver, enc, &suite->vecs[i], i, req,
2924 tsgls);
2925 if (err)
2926 return err;
Eric Biggerse63e1b02019-06-02 22:42:33 -07002927 cond_resched();
Eric Biggers4e7babba2019-01-31 23:51:46 -08002928 }
2929 return 0;
2930}
2931
2932static int alg_test_skcipher(const struct alg_test_desc *desc,
2933 const char *driver, u32 type, u32 mask)
2934{
2935 const struct cipher_test_suite *suite = &desc->suite.cipher;
2936 struct crypto_skcipher *tfm;
2937 struct skcipher_request *req = NULL;
2938 struct cipher_test_sglists *tsgls = NULL;
2939 int err;
2940
2941 if (suite->count <= 0) {
2942 pr_err("alg: skcipher: empty test suite for %s\n", driver);
2943 return -EINVAL;
2944 }
2945
2946 tfm = crypto_alloc_skcipher(driver, type, mask);
2947 if (IS_ERR(tfm)) {
2948 pr_err("alg: skcipher: failed to allocate transform for %s: %ld\n",
2949 driver, PTR_ERR(tfm));
2950 return PTR_ERR(tfm);
2951 }
2952
2953 req = skcipher_request_alloc(tfm, GFP_KERNEL);
2954 if (!req) {
2955 pr_err("alg: skcipher: failed to allocate request for %s\n",
2956 driver);
2957 err = -ENOMEM;
2958 goto out;
2959 }
2960
2961 tsgls = alloc_cipher_test_sglists();
2962 if (!tsgls) {
2963 pr_err("alg: skcipher: failed to allocate test buffers for %s\n",
2964 driver);
2965 err = -ENOMEM;
2966 goto out;
2967 }
2968
2969 err = test_skcipher(driver, ENCRYPT, suite, req, tsgls);
2970 if (err)
2971 goto out;
2972
2973 err = test_skcipher(driver, DECRYPT, suite, req, tsgls);
Eric Biggersd435e102019-04-11 21:57:40 -07002974 if (err)
2975 goto out;
2976
2977 err = test_skcipher_vs_generic_impl(driver, desc->generic_driver, req,
2978 tsgls);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002979out:
2980 free_cipher_test_sglists(tsgls);
2981 skcipher_request_free(req);
2982 crypto_free_skcipher(tfm);
2983 return err;
2984}
2985
Eric Biggersb13b1e02017-02-24 15:46:59 -08002986static int test_comp(struct crypto_comp *tfm,
2987 const struct comp_testvec *ctemplate,
2988 const struct comp_testvec *dtemplate,
2989 int ctcount, int dtcount)
Herbert Xuda7f0332008-07-31 17:08:25 +08002990{
2991 const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
Mahipal Challa33607382018-04-11 20:28:32 +02002992 char *output, *decomp_output;
Herbert Xuda7f0332008-07-31 17:08:25 +08002993 unsigned int i;
Herbert Xuda7f0332008-07-31 17:08:25 +08002994 int ret;
2995
Mahipal Challa33607382018-04-11 20:28:32 +02002996 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
2997 if (!output)
2998 return -ENOMEM;
2999
3000 decomp_output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3001 if (!decomp_output) {
3002 kfree(output);
3003 return -ENOMEM;
3004 }
3005
Herbert Xuda7f0332008-07-31 17:08:25 +08003006 for (i = 0; i < ctcount; i++) {
Geert Uytterhoevenc79cf912009-03-29 15:44:19 +08003007 int ilen;
3008 unsigned int dlen = COMP_BUF_SIZE;
Herbert Xuda7f0332008-07-31 17:08:25 +08003009
Michael Schupikov22a81182018-10-07 13:58:10 +02003010 memset(output, 0, COMP_BUF_SIZE);
3011 memset(decomp_output, 0, COMP_BUF_SIZE);
Herbert Xuda7f0332008-07-31 17:08:25 +08003012
3013 ilen = ctemplate[i].inlen;
3014 ret = crypto_comp_compress(tfm, ctemplate[i].input,
Mahipal Challa33607382018-04-11 20:28:32 +02003015 ilen, output, &dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08003016 if (ret) {
3017 printk(KERN_ERR "alg: comp: compression failed "
3018 "on test %d for %s: ret=%d\n", i + 1, algo,
3019 -ret);
3020 goto out;
3021 }
3022
Mahipal Challa33607382018-04-11 20:28:32 +02003023 ilen = dlen;
3024 dlen = COMP_BUF_SIZE;
3025 ret = crypto_comp_decompress(tfm, output,
3026 ilen, decomp_output, &dlen);
3027 if (ret) {
3028 pr_err("alg: comp: compression failed: decompress: on test %d for %s failed: ret=%d\n",
3029 i + 1, algo, -ret);
3030 goto out;
3031 }
3032
3033 if (dlen != ctemplate[i].inlen) {
Geert Uytterhoevenb812eb02008-11-28 20:51:28 +08003034 printk(KERN_ERR "alg: comp: Compression test %d "
3035 "failed for %s: output len = %d\n", i + 1, algo,
3036 dlen);
3037 ret = -EINVAL;
3038 goto out;
3039 }
3040
Mahipal Challa33607382018-04-11 20:28:32 +02003041 if (memcmp(decomp_output, ctemplate[i].input,
3042 ctemplate[i].inlen)) {
3043 pr_err("alg: comp: compression failed: output differs: on test %d for %s\n",
3044 i + 1, algo);
3045 hexdump(decomp_output, dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08003046 ret = -EINVAL;
3047 goto out;
3048 }
3049 }
3050
3051 for (i = 0; i < dtcount; i++) {
Geert Uytterhoevenc79cf912009-03-29 15:44:19 +08003052 int ilen;
3053 unsigned int dlen = COMP_BUF_SIZE;
Herbert Xuda7f0332008-07-31 17:08:25 +08003054
Michael Schupikov22a81182018-10-07 13:58:10 +02003055 memset(decomp_output, 0, COMP_BUF_SIZE);
Herbert Xuda7f0332008-07-31 17:08:25 +08003056
3057 ilen = dtemplate[i].inlen;
3058 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
Mahipal Challa33607382018-04-11 20:28:32 +02003059 ilen, decomp_output, &dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08003060 if (ret) {
3061 printk(KERN_ERR "alg: comp: decompression failed "
3062 "on test %d for %s: ret=%d\n", i + 1, algo,
3063 -ret);
3064 goto out;
3065 }
3066
Geert Uytterhoevenb812eb02008-11-28 20:51:28 +08003067 if (dlen != dtemplate[i].outlen) {
3068 printk(KERN_ERR "alg: comp: Decompression test %d "
3069 "failed for %s: output len = %d\n", i + 1, algo,
3070 dlen);
3071 ret = -EINVAL;
3072 goto out;
3073 }
3074
Mahipal Challa33607382018-04-11 20:28:32 +02003075 if (memcmp(decomp_output, dtemplate[i].output, dlen)) {
Herbert Xuda7f0332008-07-31 17:08:25 +08003076 printk(KERN_ERR "alg: comp: Decompression test %d "
3077 "failed for %s\n", i + 1, algo);
Mahipal Challa33607382018-04-11 20:28:32 +02003078 hexdump(decomp_output, dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08003079 ret = -EINVAL;
3080 goto out;
3081 }
3082 }
3083
3084 ret = 0;
3085
3086out:
Mahipal Challa33607382018-04-11 20:28:32 +02003087 kfree(decomp_output);
3088 kfree(output);
Herbert Xuda7f0332008-07-31 17:08:25 +08003089 return ret;
3090}
3091
Eric Biggersb13b1e02017-02-24 15:46:59 -08003092static int test_acomp(struct crypto_acomp *tfm,
Mahipal Challa33607382018-04-11 20:28:32 +02003093 const struct comp_testvec *ctemplate,
Eric Biggersb13b1e02017-02-24 15:46:59 -08003094 const struct comp_testvec *dtemplate,
3095 int ctcount, int dtcount)
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003096{
3097 const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
3098 unsigned int i;
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003099 char *output, *decomp_out;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003100 int ret;
3101 struct scatterlist src, dst;
3102 struct acomp_req *req;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003103 struct crypto_wait wait;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003104
Eric Biggerseb095592016-11-23 10:24:35 -08003105 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3106 if (!output)
3107 return -ENOMEM;
3108
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003109 decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3110 if (!decomp_out) {
3111 kfree(output);
3112 return -ENOMEM;
3113 }
3114
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003115 for (i = 0; i < ctcount; i++) {
3116 unsigned int dlen = COMP_BUF_SIZE;
3117 int ilen = ctemplate[i].inlen;
Laura Abbott02608e02016-12-21 12:32:54 -08003118 void *input_vec;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003119
Eric Biggersd2110222016-12-30 14:12:00 -06003120 input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL);
Laura Abbott02608e02016-12-21 12:32:54 -08003121 if (!input_vec) {
3122 ret = -ENOMEM;
3123 goto out;
3124 }
3125
Eric Biggerseb095592016-11-23 10:24:35 -08003126 memset(output, 0, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003127 crypto_init_wait(&wait);
Laura Abbott02608e02016-12-21 12:32:54 -08003128 sg_init_one(&src, input_vec, ilen);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003129 sg_init_one(&dst, output, dlen);
3130
3131 req = acomp_request_alloc(tfm);
3132 if (!req) {
3133 pr_err("alg: acomp: request alloc failed for %s\n",
3134 algo);
Laura Abbott02608e02016-12-21 12:32:54 -08003135 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003136 ret = -ENOMEM;
3137 goto out;
3138 }
3139
3140 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3141 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003142 crypto_req_done, &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003143
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003144 ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003145 if (ret) {
3146 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
3147 i + 1, algo, -ret);
Laura Abbott02608e02016-12-21 12:32:54 -08003148 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003149 acomp_request_free(req);
3150 goto out;
3151 }
3152
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003153 ilen = req->dlen;
3154 dlen = COMP_BUF_SIZE;
3155 sg_init_one(&src, output, ilen);
3156 sg_init_one(&dst, decomp_out, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003157 crypto_init_wait(&wait);
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003158 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3159
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003160 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003161 if (ret) {
3162 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
3163 i + 1, algo, -ret);
3164 kfree(input_vec);
3165 acomp_request_free(req);
3166 goto out;
3167 }
3168
3169 if (req->dlen != ctemplate[i].inlen) {
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003170 pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
3171 i + 1, algo, req->dlen);
3172 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08003173 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003174 acomp_request_free(req);
3175 goto out;
3176 }
3177
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003178 if (memcmp(input_vec, decomp_out, req->dlen)) {
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003179 pr_err("alg: acomp: Compression test %d failed for %s\n",
3180 i + 1, algo);
3181 hexdump(output, req->dlen);
3182 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08003183 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003184 acomp_request_free(req);
3185 goto out;
3186 }
3187
Laura Abbott02608e02016-12-21 12:32:54 -08003188 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003189 acomp_request_free(req);
3190 }
3191
3192 for (i = 0; i < dtcount; i++) {
3193 unsigned int dlen = COMP_BUF_SIZE;
3194 int ilen = dtemplate[i].inlen;
Laura Abbott02608e02016-12-21 12:32:54 -08003195 void *input_vec;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003196
Eric Biggersd2110222016-12-30 14:12:00 -06003197 input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL);
Laura Abbott02608e02016-12-21 12:32:54 -08003198 if (!input_vec) {
3199 ret = -ENOMEM;
3200 goto out;
3201 }
3202
Eric Biggerseb095592016-11-23 10:24:35 -08003203 memset(output, 0, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003204 crypto_init_wait(&wait);
Laura Abbott02608e02016-12-21 12:32:54 -08003205 sg_init_one(&src, input_vec, ilen);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003206 sg_init_one(&dst, output, dlen);
3207
3208 req = acomp_request_alloc(tfm);
3209 if (!req) {
3210 pr_err("alg: acomp: request alloc failed for %s\n",
3211 algo);
Laura Abbott02608e02016-12-21 12:32:54 -08003212 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003213 ret = -ENOMEM;
3214 goto out;
3215 }
3216
3217 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3218 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003219 crypto_req_done, &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003220
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003221 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003222 if (ret) {
3223 pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
3224 i + 1, algo, -ret);
Laura Abbott02608e02016-12-21 12:32:54 -08003225 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003226 acomp_request_free(req);
3227 goto out;
3228 }
3229
3230 if (req->dlen != dtemplate[i].outlen) {
3231 pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
3232 i + 1, algo, req->dlen);
3233 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08003234 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003235 acomp_request_free(req);
3236 goto out;
3237 }
3238
3239 if (memcmp(output, dtemplate[i].output, req->dlen)) {
3240 pr_err("alg: acomp: Decompression test %d failed for %s\n",
3241 i + 1, algo);
3242 hexdump(output, req->dlen);
3243 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08003244 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003245 acomp_request_free(req);
3246 goto out;
3247 }
3248
Laura Abbott02608e02016-12-21 12:32:54 -08003249 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003250 acomp_request_free(req);
3251 }
3252
3253 ret = 0;
3254
3255out:
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003256 kfree(decomp_out);
Eric Biggerseb095592016-11-23 10:24:35 -08003257 kfree(output);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003258 return ret;
3259}
3260
Eric Biggersb13b1e02017-02-24 15:46:59 -08003261static int test_cprng(struct crypto_rng *tfm,
3262 const struct cprng_testvec *template,
Jarod Wilson7647d6c2009-05-04 19:44:50 +08003263 unsigned int tcount)
3264{
3265 const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
Felipe Contrerasfa4ef8a2009-10-27 19:04:42 +08003266 int err = 0, i, j, seedsize;
Jarod Wilson7647d6c2009-05-04 19:44:50 +08003267 u8 *seed;
3268 char result[32];
3269
3270 seedsize = crypto_rng_seedsize(tfm);
3271
3272 seed = kmalloc(seedsize, GFP_KERNEL);
3273 if (!seed) {
3274 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
3275 "for %s\n", algo);
3276 return -ENOMEM;
3277 }
3278
3279 for (i = 0; i < tcount; i++) {
3280 memset(result, 0, 32);
3281
3282 memcpy(seed, template[i].v, template[i].vlen);
3283 memcpy(seed + template[i].vlen, template[i].key,
3284 template[i].klen);
3285 memcpy(seed + template[i].vlen + template[i].klen,
3286 template[i].dt, template[i].dtlen);
3287
3288 err = crypto_rng_reset(tfm, seed, seedsize);
3289 if (err) {
3290 printk(KERN_ERR "alg: cprng: Failed to reset rng "
3291 "for %s\n", algo);
3292 goto out;
3293 }
3294
3295 for (j = 0; j < template[i].loops; j++) {
3296 err = crypto_rng_get_bytes(tfm, result,
3297 template[i].rlen);
Stephan Mueller19e60e12015-03-10 17:00:36 +01003298 if (err < 0) {
Jarod Wilson7647d6c2009-05-04 19:44:50 +08003299 printk(KERN_ERR "alg: cprng: Failed to obtain "
3300 "the correct amount of random data for "
Stephan Mueller19e60e12015-03-10 17:00:36 +01003301 "%s (requested %d)\n", algo,
3302 template[i].rlen);
Jarod Wilson7647d6c2009-05-04 19:44:50 +08003303 goto out;
3304 }
3305 }
3306
3307 err = memcmp(result, template[i].result,
3308 template[i].rlen);
3309 if (err) {
3310 printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
3311 i, algo);
3312 hexdump(result, template[i].rlen);
3313 err = -EINVAL;
3314 goto out;
3315 }
3316 }
3317
3318out:
3319 kfree(seed);
3320 return err;
3321}
3322
Herbert Xuda7f0332008-07-31 17:08:25 +08003323static int alg_test_cipher(const struct alg_test_desc *desc,
3324 const char *driver, u32 type, u32 mask)
3325{
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003326 const struct cipher_test_suite *suite = &desc->suite.cipher;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003327 struct crypto_cipher *tfm;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003328 int err;
Herbert Xuda7f0332008-07-31 17:08:25 +08003329
Herbert Xueed93e02016-11-22 20:08:31 +08003330 tfm = crypto_alloc_cipher(driver, type, mask);
Herbert Xuda7f0332008-07-31 17:08:25 +08003331 if (IS_ERR(tfm)) {
3332 printk(KERN_ERR "alg: cipher: Failed to load transform for "
3333 "%s: %ld\n", driver, PTR_ERR(tfm));
3334 return PTR_ERR(tfm);
3335 }
3336
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003337 err = test_cipher(tfm, ENCRYPT, suite->vecs, suite->count);
3338 if (!err)
3339 err = test_cipher(tfm, DECRYPT, suite->vecs, suite->count);
Herbert Xuda7f0332008-07-31 17:08:25 +08003340
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003341 crypto_free_cipher(tfm);
3342 return err;
3343}
3344
Herbert Xuda7f0332008-07-31 17:08:25 +08003345static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
3346 u32 type, u32 mask)
3347{
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003348 struct crypto_comp *comp;
3349 struct crypto_acomp *acomp;
Herbert Xuda7f0332008-07-31 17:08:25 +08003350 int err;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003351 u32 algo_type = type & CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
Herbert Xuda7f0332008-07-31 17:08:25 +08003352
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003353 if (algo_type == CRYPTO_ALG_TYPE_ACOMPRESS) {
3354 acomp = crypto_alloc_acomp(driver, type, mask);
3355 if (IS_ERR(acomp)) {
3356 pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
3357 driver, PTR_ERR(acomp));
3358 return PTR_ERR(acomp);
3359 }
3360 err = test_acomp(acomp, desc->suite.comp.comp.vecs,
3361 desc->suite.comp.decomp.vecs,
3362 desc->suite.comp.comp.count,
3363 desc->suite.comp.decomp.count);
3364 crypto_free_acomp(acomp);
3365 } else {
3366 comp = crypto_alloc_comp(driver, type, mask);
3367 if (IS_ERR(comp)) {
3368 pr_err("alg: comp: Failed to load transform for %s: %ld\n",
3369 driver, PTR_ERR(comp));
3370 return PTR_ERR(comp);
3371 }
3372
3373 err = test_comp(comp, desc->suite.comp.comp.vecs,
3374 desc->suite.comp.decomp.vecs,
3375 desc->suite.comp.comp.count,
3376 desc->suite.comp.decomp.count);
3377
3378 crypto_free_comp(comp);
Herbert Xuda7f0332008-07-31 17:08:25 +08003379 }
Herbert Xuda7f0332008-07-31 17:08:25 +08003380 return err;
3381}
3382
Herbert Xu8e3ee852008-11-07 14:58:52 +08003383static int alg_test_crc32c(const struct alg_test_desc *desc,
3384 const char *driver, u32 type, u32 mask)
3385{
3386 struct crypto_shash *tfm;
Eric Biggerscb9dde82019-01-10 12:17:55 -08003387 __le32 val;
Herbert Xu8e3ee852008-11-07 14:58:52 +08003388 int err;
3389
3390 err = alg_test_hash(desc, driver, type, mask);
3391 if (err)
Eric Biggerseb5e6732019-01-23 20:57:35 -08003392 return err;
Herbert Xu8e3ee852008-11-07 14:58:52 +08003393
Herbert Xueed93e02016-11-22 20:08:31 +08003394 tfm = crypto_alloc_shash(driver, type, mask);
Herbert Xu8e3ee852008-11-07 14:58:52 +08003395 if (IS_ERR(tfm)) {
Eric Biggerseb5e6732019-01-23 20:57:35 -08003396 if (PTR_ERR(tfm) == -ENOENT) {
3397 /*
3398 * This crc32c implementation is only available through
3399 * ahash API, not the shash API, so the remaining part
3400 * of the test is not applicable to it.
3401 */
3402 return 0;
3403 }
Herbert Xu8e3ee852008-11-07 14:58:52 +08003404 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
3405 "%ld\n", driver, PTR_ERR(tfm));
Eric Biggerseb5e6732019-01-23 20:57:35 -08003406 return PTR_ERR(tfm);
Herbert Xu8e3ee852008-11-07 14:58:52 +08003407 }
3408
3409 do {
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02003410 SHASH_DESC_ON_STACK(shash, tfm);
3411 u32 *ctx = (u32 *)shash_desc_ctx(shash);
Herbert Xu8e3ee852008-11-07 14:58:52 +08003412
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02003413 shash->tfm = tfm;
Herbert Xu8e3ee852008-11-07 14:58:52 +08003414
Eric Biggerscb9dde82019-01-10 12:17:55 -08003415 *ctx = 420553207;
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02003416 err = crypto_shash_final(shash, (u8 *)&val);
Herbert Xu8e3ee852008-11-07 14:58:52 +08003417 if (err) {
3418 printk(KERN_ERR "alg: crc32c: Operation failed for "
3419 "%s: %d\n", driver, err);
3420 break;
3421 }
3422
Eric Biggerscb9dde82019-01-10 12:17:55 -08003423 if (val != cpu_to_le32(~420553207)) {
3424 pr_err("alg: crc32c: Test failed for %s: %u\n",
3425 driver, le32_to_cpu(val));
Herbert Xu8e3ee852008-11-07 14:58:52 +08003426 err = -EINVAL;
3427 }
3428 } while (0);
3429
3430 crypto_free_shash(tfm);
3431
Herbert Xu8e3ee852008-11-07 14:58:52 +08003432 return err;
3433}
3434
Jarod Wilson7647d6c2009-05-04 19:44:50 +08003435static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
3436 u32 type, u32 mask)
3437{
3438 struct crypto_rng *rng;
3439 int err;
3440
Herbert Xueed93e02016-11-22 20:08:31 +08003441 rng = crypto_alloc_rng(driver, type, mask);
Jarod Wilson7647d6c2009-05-04 19:44:50 +08003442 if (IS_ERR(rng)) {
3443 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
3444 "%ld\n", driver, PTR_ERR(rng));
3445 return PTR_ERR(rng);
3446 }
3447
3448 err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
3449
3450 crypto_free_rng(rng);
3451
3452 return err;
3453}
3454
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003455
Eric Biggersb13b1e02017-02-24 15:46:59 -08003456static int drbg_cavs_test(const struct drbg_testvec *test, int pr,
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003457 const char *driver, u32 type, u32 mask)
3458{
3459 int ret = -EAGAIN;
3460 struct crypto_rng *drng;
3461 struct drbg_test_data test_data;
3462 struct drbg_string addtl, pers, testentropy;
3463 unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
3464
3465 if (!buf)
3466 return -ENOMEM;
3467
Herbert Xueed93e02016-11-22 20:08:31 +08003468 drng = crypto_alloc_rng(driver, type, mask);
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003469 if (IS_ERR(drng)) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04003470 printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003471 "%s\n", driver);
3472 kzfree(buf);
3473 return -ENOMEM;
3474 }
3475
3476 test_data.testentropy = &testentropy;
3477 drbg_string_fill(&testentropy, test->entropy, test->entropylen);
3478 drbg_string_fill(&pers, test->pers, test->perslen);
3479 ret = crypto_drbg_reset_test(drng, &pers, &test_data);
3480 if (ret) {
3481 printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
3482 goto outbuf;
3483 }
3484
3485 drbg_string_fill(&addtl, test->addtla, test->addtllen);
3486 if (pr) {
3487 drbg_string_fill(&testentropy, test->entpra, test->entprlen);
3488 ret = crypto_drbg_get_bytes_addtl_test(drng,
3489 buf, test->expectedlen, &addtl, &test_data);
3490 } else {
3491 ret = crypto_drbg_get_bytes_addtl(drng,
3492 buf, test->expectedlen, &addtl);
3493 }
Stephan Mueller19e60e12015-03-10 17:00:36 +01003494 if (ret < 0) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04003495 printk(KERN_ERR "alg: drbg: could not obtain random data for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003496 "driver %s\n", driver);
3497 goto outbuf;
3498 }
3499
3500 drbg_string_fill(&addtl, test->addtlb, test->addtllen);
3501 if (pr) {
3502 drbg_string_fill(&testentropy, test->entprb, test->entprlen);
3503 ret = crypto_drbg_get_bytes_addtl_test(drng,
3504 buf, test->expectedlen, &addtl, &test_data);
3505 } else {
3506 ret = crypto_drbg_get_bytes_addtl(drng,
3507 buf, test->expectedlen, &addtl);
3508 }
Stephan Mueller19e60e12015-03-10 17:00:36 +01003509 if (ret < 0) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04003510 printk(KERN_ERR "alg: drbg: could not obtain random data for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003511 "driver %s\n", driver);
3512 goto outbuf;
3513 }
3514
3515 ret = memcmp(test->expected, buf, test->expectedlen);
3516
3517outbuf:
3518 crypto_free_rng(drng);
3519 kzfree(buf);
3520 return ret;
3521}
3522
3523
3524static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
3525 u32 type, u32 mask)
3526{
3527 int err = 0;
3528 int pr = 0;
3529 int i = 0;
Eric Biggersb13b1e02017-02-24 15:46:59 -08003530 const struct drbg_testvec *template = desc->suite.drbg.vecs;
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003531 unsigned int tcount = desc->suite.drbg.count;
3532
3533 if (0 == memcmp(driver, "drbg_pr_", 8))
3534 pr = 1;
3535
3536 for (i = 0; i < tcount; i++) {
3537 err = drbg_cavs_test(&template[i], pr, driver, type, mask);
3538 if (err) {
3539 printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
3540 i, driver);
3541 err = -EINVAL;
3542 break;
3543 }
3544 }
3545 return err;
3546
3547}
3548
Eric Biggersb13b1e02017-02-24 15:46:59 -08003549static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003550 const char *alg)
3551{
3552 struct kpp_request *req;
3553 void *input_buf = NULL;
3554 void *output_buf = NULL;
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003555 void *a_public = NULL;
3556 void *a_ss = NULL;
3557 void *shared_secret = NULL;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003558 struct crypto_wait wait;
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003559 unsigned int out_len_max;
3560 int err = -ENOMEM;
3561 struct scatterlist src, dst;
3562
3563 req = kpp_request_alloc(tfm, GFP_KERNEL);
3564 if (!req)
3565 return err;
3566
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003567 crypto_init_wait(&wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003568
3569 err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
3570 if (err < 0)
3571 goto free_req;
3572
3573 out_len_max = crypto_kpp_maxsize(tfm);
3574 output_buf = kzalloc(out_len_max, GFP_KERNEL);
3575 if (!output_buf) {
3576 err = -ENOMEM;
3577 goto free_req;
3578 }
3579
3580 /* Use appropriate parameter as base */
3581 kpp_request_set_input(req, NULL, 0);
3582 sg_init_one(&dst, output_buf, out_len_max);
3583 kpp_request_set_output(req, &dst, out_len_max);
3584 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003585 crypto_req_done, &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003586
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003587 /* Compute party A's public key */
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003588 err = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003589 if (err) {
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003590 pr_err("alg: %s: Party A: generate public key test failed. err %d\n",
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003591 alg, err);
3592 goto free_output;
3593 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003594
3595 if (vec->genkey) {
3596 /* Save party A's public key */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05003597 a_public = kmemdup(sg_virt(req->dst), out_len_max, GFP_KERNEL);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003598 if (!a_public) {
3599 err = -ENOMEM;
3600 goto free_output;
3601 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003602 } else {
3603 /* Verify calculated public key */
3604 if (memcmp(vec->expected_a_public, sg_virt(req->dst),
3605 vec->expected_a_public_size)) {
3606 pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n",
3607 alg);
3608 err = -EINVAL;
3609 goto free_output;
3610 }
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003611 }
3612
3613 /* Calculate shared secret key by using counter part (b) public key. */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05003614 input_buf = kmemdup(vec->b_public, vec->b_public_size, GFP_KERNEL);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003615 if (!input_buf) {
3616 err = -ENOMEM;
3617 goto free_output;
3618 }
3619
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003620 sg_init_one(&src, input_buf, vec->b_public_size);
3621 sg_init_one(&dst, output_buf, out_len_max);
3622 kpp_request_set_input(req, &src, vec->b_public_size);
3623 kpp_request_set_output(req, &dst, out_len_max);
3624 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003625 crypto_req_done, &wait);
3626 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003627 if (err) {
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003628 pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n",
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003629 alg, err);
3630 goto free_all;
3631 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003632
3633 if (vec->genkey) {
3634 /* Save the shared secret obtained by party A */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05003635 a_ss = kmemdup(sg_virt(req->dst), vec->expected_ss_size, GFP_KERNEL);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003636 if (!a_ss) {
3637 err = -ENOMEM;
3638 goto free_all;
3639 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003640
3641 /*
3642 * Calculate party B's shared secret by using party A's
3643 * public key.
3644 */
3645 err = crypto_kpp_set_secret(tfm, vec->b_secret,
3646 vec->b_secret_size);
3647 if (err < 0)
3648 goto free_all;
3649
3650 sg_init_one(&src, a_public, vec->expected_a_public_size);
3651 sg_init_one(&dst, output_buf, out_len_max);
3652 kpp_request_set_input(req, &src, vec->expected_a_public_size);
3653 kpp_request_set_output(req, &dst, out_len_max);
3654 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003655 crypto_req_done, &wait);
3656 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req),
3657 &wait);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003658 if (err) {
3659 pr_err("alg: %s: Party B: compute shared secret failed. err %d\n",
3660 alg, err);
3661 goto free_all;
3662 }
3663
3664 shared_secret = a_ss;
3665 } else {
3666 shared_secret = (void *)vec->expected_ss;
3667 }
3668
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003669 /*
3670 * verify shared secret from which the user will derive
3671 * secret key by executing whatever hash it has chosen
3672 */
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003673 if (memcmp(shared_secret, sg_virt(req->dst),
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003674 vec->expected_ss_size)) {
3675 pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
3676 alg);
3677 err = -EINVAL;
3678 }
3679
3680free_all:
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003681 kfree(a_ss);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003682 kfree(input_buf);
3683free_output:
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003684 kfree(a_public);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003685 kfree(output_buf);
3686free_req:
3687 kpp_request_free(req);
3688 return err;
3689}
3690
3691static int test_kpp(struct crypto_kpp *tfm, const char *alg,
Eric Biggersb13b1e02017-02-24 15:46:59 -08003692 const struct kpp_testvec *vecs, unsigned int tcount)
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003693{
3694 int ret, i;
3695
3696 for (i = 0; i < tcount; i++) {
3697 ret = do_test_kpp(tfm, vecs++, alg);
3698 if (ret) {
3699 pr_err("alg: %s: test failed on vector %d, err=%d\n",
3700 alg, i + 1, ret);
3701 return ret;
3702 }
3703 }
3704 return 0;
3705}
3706
3707static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
3708 u32 type, u32 mask)
3709{
3710 struct crypto_kpp *tfm;
3711 int err = 0;
3712
Herbert Xueed93e02016-11-22 20:08:31 +08003713 tfm = crypto_alloc_kpp(driver, type, mask);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003714 if (IS_ERR(tfm)) {
3715 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
3716 driver, PTR_ERR(tfm));
3717 return PTR_ERR(tfm);
3718 }
3719 if (desc->suite.kpp.vecs)
3720 err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
3721 desc->suite.kpp.count);
3722
3723 crypto_free_kpp(tfm);
3724 return err;
3725}
3726
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003727static u8 *test_pack_u32(u8 *dst, u32 val)
3728{
3729 memcpy(dst, &val, sizeof(val));
3730 return dst + sizeof(val);
3731}
3732
Herbert Xu50d2b6432016-06-29 19:32:20 +08003733static int test_akcipher_one(struct crypto_akcipher *tfm,
Eric Biggersb13b1e02017-02-24 15:46:59 -08003734 const struct akcipher_testvec *vecs)
Tadeusz Struk946cc462015-06-16 10:31:06 -07003735{
Herbert Xudf27b262016-05-05 16:42:49 +08003736 char *xbuf[XBUFSIZE];
Tadeusz Struk946cc462015-06-16 10:31:06 -07003737 struct akcipher_request *req;
3738 void *outbuf_enc = NULL;
3739 void *outbuf_dec = NULL;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003740 struct crypto_wait wait;
Tadeusz Struk946cc462015-06-16 10:31:06 -07003741 unsigned int out_len_max, out_len = 0;
3742 int err = -ENOMEM;
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03003743 struct scatterlist src, dst, src_tab[3];
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003744 const char *m, *c;
3745 unsigned int m_size, c_size;
3746 const char *op;
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003747 u8 *key, *ptr;
Tadeusz Struk946cc462015-06-16 10:31:06 -07003748
Herbert Xudf27b262016-05-05 16:42:49 +08003749 if (testmgr_alloc_buf(xbuf))
3750 return err;
3751
Tadeusz Struk946cc462015-06-16 10:31:06 -07003752 req = akcipher_request_alloc(tfm, GFP_KERNEL);
3753 if (!req)
Herbert Xudf27b262016-05-05 16:42:49 +08003754 goto free_xbuf;
Tadeusz Struk946cc462015-06-16 10:31:06 -07003755
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003756 crypto_init_wait(&wait);
Tadeusz Struk22287b02015-10-08 09:26:55 -07003757
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003758 key = kmalloc(vecs->key_len + sizeof(u32) * 2 + vecs->param_len,
3759 GFP_KERNEL);
3760 if (!key)
3761 goto free_xbuf;
3762 memcpy(key, vecs->key, vecs->key_len);
3763 ptr = key + vecs->key_len;
3764 ptr = test_pack_u32(ptr, vecs->algo);
3765 ptr = test_pack_u32(ptr, vecs->param_len);
3766 memcpy(ptr, vecs->params, vecs->param_len);
3767
Tadeusz Struk22287b02015-10-08 09:26:55 -07003768 if (vecs->public_key_vec)
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003769 err = crypto_akcipher_set_pub_key(tfm, key, vecs->key_len);
Tadeusz Struk22287b02015-10-08 09:26:55 -07003770 else
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003771 err = crypto_akcipher_set_priv_key(tfm, key, vecs->key_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003772 if (err)
3773 goto free_req;
3774
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003775 /*
3776 * First run test which do not require a private key, such as
3777 * encrypt or verify.
3778 */
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03003779 err = -ENOMEM;
3780 out_len_max = crypto_akcipher_maxsize(tfm);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003781 outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
3782 if (!outbuf_enc)
3783 goto free_req;
3784
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003785 if (!vecs->siggen_sigver_test) {
3786 m = vecs->m;
3787 m_size = vecs->m_size;
3788 c = vecs->c;
3789 c_size = vecs->c_size;
3790 op = "encrypt";
3791 } else {
3792 /* Swap args so we could keep plaintext (digest)
3793 * in vecs->m, and cooked signature in vecs->c.
3794 */
3795 m = vecs->c; /* signature */
3796 m_size = vecs->c_size;
3797 c = vecs->m; /* digest */
3798 c_size = vecs->m_size;
3799 op = "verify";
3800 }
Herbert Xudf27b262016-05-05 16:42:49 +08003801
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003802 if (WARN_ON(m_size > PAGE_SIZE))
3803 goto free_all;
3804 memcpy(xbuf[0], m, m_size);
Herbert Xudf27b262016-05-05 16:42:49 +08003805
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03003806 sg_init_table(src_tab, 3);
Herbert Xudf27b262016-05-05 16:42:49 +08003807 sg_set_buf(&src_tab[0], xbuf[0], 8);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003808 sg_set_buf(&src_tab[1], xbuf[0] + 8, m_size - 8);
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03003809 if (vecs->siggen_sigver_test) {
3810 if (WARN_ON(c_size > PAGE_SIZE))
3811 goto free_all;
3812 memcpy(xbuf[1], c, c_size);
3813 sg_set_buf(&src_tab[2], xbuf[1], c_size);
3814 akcipher_request_set_crypt(req, src_tab, NULL, m_size, c_size);
3815 } else {
3816 sg_init_one(&dst, outbuf_enc, out_len_max);
3817 akcipher_request_set_crypt(req, src_tab, &dst, m_size,
3818 out_len_max);
3819 }
Tadeusz Struk946cc462015-06-16 10:31:06 -07003820 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003821 crypto_req_done, &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003822
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003823 err = crypto_wait_req(vecs->siggen_sigver_test ?
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003824 /* Run asymmetric signature verification */
3825 crypto_akcipher_verify(req) :
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003826 /* Run asymmetric encrypt */
3827 crypto_akcipher_encrypt(req), &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003828 if (err) {
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003829 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003830 goto free_all;
3831 }
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03003832 if (!vecs->siggen_sigver_test) {
3833 if (req->dst_len != c_size) {
3834 pr_err("alg: akcipher: %s test failed. Invalid output len\n",
3835 op);
3836 err = -EINVAL;
3837 goto free_all;
3838 }
3839 /* verify that encrypted message is equal to expected */
3840 if (memcmp(c, outbuf_enc, c_size) != 0) {
3841 pr_err("alg: akcipher: %s test failed. Invalid output\n",
3842 op);
3843 hexdump(outbuf_enc, c_size);
3844 err = -EINVAL;
3845 goto free_all;
3846 }
Tadeusz Struk946cc462015-06-16 10:31:06 -07003847 }
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003848
3849 /*
3850 * Don't invoke (decrypt or sign) test which require a private key
3851 * for vectors with only a public key.
3852 */
Tadeusz Struk946cc462015-06-16 10:31:06 -07003853 if (vecs->public_key_vec) {
3854 err = 0;
3855 goto free_all;
3856 }
3857 outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
3858 if (!outbuf_dec) {
3859 err = -ENOMEM;
3860 goto free_all;
3861 }
Herbert Xudf27b262016-05-05 16:42:49 +08003862
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003863 op = vecs->siggen_sigver_test ? "sign" : "decrypt";
3864 if (WARN_ON(c_size > PAGE_SIZE))
Herbert Xudf27b262016-05-05 16:42:49 +08003865 goto free_all;
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003866 memcpy(xbuf[0], c, c_size);
Herbert Xudf27b262016-05-05 16:42:49 +08003867
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003868 sg_init_one(&src, xbuf[0], c_size);
Tadeusz Struk22287b02015-10-08 09:26:55 -07003869 sg_init_one(&dst, outbuf_dec, out_len_max);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003870 crypto_init_wait(&wait);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003871 akcipher_request_set_crypt(req, &src, &dst, c_size, out_len_max);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003872
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003873 err = crypto_wait_req(vecs->siggen_sigver_test ?
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003874 /* Run asymmetric signature generation */
3875 crypto_akcipher_sign(req) :
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003876 /* Run asymmetric decrypt */
3877 crypto_akcipher_decrypt(req), &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003878 if (err) {
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003879 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003880 goto free_all;
3881 }
3882 out_len = req->dst_len;
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003883 if (out_len < m_size) {
3884 pr_err("alg: akcipher: %s test failed. Invalid output len %u\n",
3885 op, out_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003886 err = -EINVAL;
3887 goto free_all;
3888 }
3889 /* verify that decrypted message is equal to the original msg */
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003890 if (memchr_inv(outbuf_dec, 0, out_len - m_size) ||
3891 memcmp(m, outbuf_dec + out_len - m_size, m_size)) {
3892 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
Herbert Xu50d2b6432016-06-29 19:32:20 +08003893 hexdump(outbuf_dec, out_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003894 err = -EINVAL;
3895 }
3896free_all:
3897 kfree(outbuf_dec);
3898 kfree(outbuf_enc);
3899free_req:
3900 akcipher_request_free(req);
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003901 kfree(key);
Herbert Xudf27b262016-05-05 16:42:49 +08003902free_xbuf:
3903 testmgr_free_buf(xbuf);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003904 return err;
3905}
3906
Herbert Xu50d2b6432016-06-29 19:32:20 +08003907static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
Eric Biggersb13b1e02017-02-24 15:46:59 -08003908 const struct akcipher_testvec *vecs,
3909 unsigned int tcount)
Tadeusz Struk946cc462015-06-16 10:31:06 -07003910{
Herbert Xu15226e42016-07-18 18:20:10 +08003911 const char *algo =
3912 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
Tadeusz Struk946cc462015-06-16 10:31:06 -07003913 int ret, i;
3914
3915 for (i = 0; i < tcount; i++) {
Herbert Xu50d2b6432016-06-29 19:32:20 +08003916 ret = test_akcipher_one(tfm, vecs++);
3917 if (!ret)
3918 continue;
3919
Herbert Xu15226e42016-07-18 18:20:10 +08003920 pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
3921 i + 1, algo, ret);
Herbert Xu50d2b6432016-06-29 19:32:20 +08003922 return ret;
Tadeusz Struk946cc462015-06-16 10:31:06 -07003923 }
3924 return 0;
3925}
3926
Tadeusz Struk946cc462015-06-16 10:31:06 -07003927static int alg_test_akcipher(const struct alg_test_desc *desc,
3928 const char *driver, u32 type, u32 mask)
3929{
3930 struct crypto_akcipher *tfm;
3931 int err = 0;
3932
Herbert Xueed93e02016-11-22 20:08:31 +08003933 tfm = crypto_alloc_akcipher(driver, type, mask);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003934 if (IS_ERR(tfm)) {
3935 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
3936 driver, PTR_ERR(tfm));
3937 return PTR_ERR(tfm);
3938 }
3939 if (desc->suite.akcipher.vecs)
3940 err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
3941 desc->suite.akcipher.count);
3942
3943 crypto_free_akcipher(tfm);
3944 return err;
3945}
3946
Youquan, Song863b5572009-12-23 19:45:20 +08003947static int alg_test_null(const struct alg_test_desc *desc,
3948 const char *driver, u32 type, u32 mask)
3949{
3950 return 0;
3951}
3952
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003953#define __VECS(tv) { .vecs = tv, .count = ARRAY_SIZE(tv) }
3954
Herbert Xuda7f0332008-07-31 17:08:25 +08003955/* Please keep this list sorted by algorithm name. */
3956static const struct alg_test_desc alg_test_descs[] = {
3957 {
Eric Biggers059c2a42018-11-16 17:26:31 -08003958 .alg = "adiantum(xchacha12,aes)",
Eric Biggersd435e102019-04-11 21:57:40 -07003959 .generic_driver = "adiantum(xchacha12-generic,aes-generic,nhpoly1305-generic)",
Eric Biggers059c2a42018-11-16 17:26:31 -08003960 .test = alg_test_skcipher,
3961 .suite = {
3962 .cipher = __VECS(adiantum_xchacha12_aes_tv_template)
3963 },
3964 }, {
3965 .alg = "adiantum(xchacha20,aes)",
Eric Biggersd435e102019-04-11 21:57:40 -07003966 .generic_driver = "adiantum(xchacha20-generic,aes-generic,nhpoly1305-generic)",
Eric Biggers059c2a42018-11-16 17:26:31 -08003967 .test = alg_test_skcipher,
3968 .suite = {
3969 .cipher = __VECS(adiantum_xchacha20_aes_tv_template)
3970 },
3971 }, {
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02003972 .alg = "aegis128",
3973 .test = alg_test_aead,
3974 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003975 .aead = __VECS(aegis128_tv_template)
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02003976 }
3977 }, {
Jarod Wilsone08ca2d2009-05-04 19:46:29 +08003978 .alg = "ansi_cprng",
3979 .test = alg_test_cprng,
3980 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003981 .cprng = __VECS(ansi_cprng_aes_tv_template)
Jarod Wilsone08ca2d2009-05-04 19:46:29 +08003982 }
3983 }, {
Horia Geantabca4feb2014-03-14 17:46:51 +02003984 .alg = "authenc(hmac(md5),ecb(cipher_null))",
3985 .test = alg_test_aead,
Horia Geantabca4feb2014-03-14 17:46:51 +02003986 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003987 .aead = __VECS(hmac_md5_ecb_cipher_null_tv_template)
Horia Geantabca4feb2014-03-14 17:46:51 +02003988 }
3989 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003990 .alg = "authenc(hmac(sha1),cbc(aes))",
Horia Geantae46e9a42012-07-03 19:16:54 +03003991 .test = alg_test_aead,
Herbert Xubcf741c2017-06-28 19:09:07 +08003992 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03003993 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003994 .aead = __VECS(hmac_sha1_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303995 }
3996 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003997 .alg = "authenc(hmac(sha1),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303998 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303999 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004000 .aead = __VECS(hmac_sha1_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304001 }
4002 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004003 .alg = "authenc(hmac(sha1),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304004 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01004005 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304006 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004007 .aead = __VECS(hmac_sha1_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03004008 }
4009 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01004010 .alg = "authenc(hmac(sha1),ctr(aes))",
4011 .test = alg_test_null,
4012 .fips_allowed = 1,
4013 }, {
Horia Geantabca4feb2014-03-14 17:46:51 +02004014 .alg = "authenc(hmac(sha1),ecb(cipher_null))",
4015 .test = alg_test_aead,
Horia Geantabca4feb2014-03-14 17:46:51 +02004016 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004017 .aead = __VECS(hmac_sha1_ecb_cipher_null_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304018 }
4019 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01004020 .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
4021 .test = alg_test_null,
4022 .fips_allowed = 1,
4023 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004024 .alg = "authenc(hmac(sha224),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304025 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304026 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004027 .aead = __VECS(hmac_sha224_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304028 }
4029 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004030 .alg = "authenc(hmac(sha224),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304031 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01004032 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304033 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004034 .aead = __VECS(hmac_sha224_des3_ede_cbc_tv_temp)
Horia Geantabca4feb2014-03-14 17:46:51 +02004035 }
4036 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004037 .alg = "authenc(hmac(sha256),cbc(aes))",
Horia Geantae46e9a42012-07-03 19:16:54 +03004038 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01004039 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03004040 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004041 .aead = __VECS(hmac_sha256_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304042 }
4043 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004044 .alg = "authenc(hmac(sha256),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304045 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304046 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004047 .aead = __VECS(hmac_sha256_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304048 }
4049 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004050 .alg = "authenc(hmac(sha256),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304051 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01004052 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304053 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004054 .aead = __VECS(hmac_sha256_des3_ede_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304055 }
4056 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01004057 .alg = "authenc(hmac(sha256),ctr(aes))",
4058 .test = alg_test_null,
4059 .fips_allowed = 1,
4060 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01004061 .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
4062 .test = alg_test_null,
4063 .fips_allowed = 1,
4064 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004065 .alg = "authenc(hmac(sha384),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304066 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304067 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004068 .aead = __VECS(hmac_sha384_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304069 }
4070 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004071 .alg = "authenc(hmac(sha384),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304072 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01004073 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304074 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004075 .aead = __VECS(hmac_sha384_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03004076 }
4077 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01004078 .alg = "authenc(hmac(sha384),ctr(aes))",
4079 .test = alg_test_null,
4080 .fips_allowed = 1,
4081 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01004082 .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
4083 .test = alg_test_null,
4084 .fips_allowed = 1,
4085 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004086 .alg = "authenc(hmac(sha512),cbc(aes))",
Marcus Meissnered1afac2016-02-05 14:23:33 +01004087 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03004088 .test = alg_test_aead,
Horia Geantae46e9a42012-07-03 19:16:54 +03004089 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004090 .aead = __VECS(hmac_sha512_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304091 }
4092 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004093 .alg = "authenc(hmac(sha512),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304094 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304095 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004096 .aead = __VECS(hmac_sha512_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304097 }
4098 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004099 .alg = "authenc(hmac(sha512),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304100 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01004101 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304102 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004103 .aead = __VECS(hmac_sha512_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03004104 }
4105 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01004106 .alg = "authenc(hmac(sha512),ctr(aes))",
4107 .test = alg_test_null,
4108 .fips_allowed = 1,
4109 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01004110 .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
4111 .test = alg_test_null,
4112 .fips_allowed = 1,
4113 }, {
David Sterbaa1afe272019-10-24 18:28:32 +02004114 .alg = "blake2b-160",
4115 .test = alg_test_hash,
4116 .fips_allowed = 0,
4117 .suite = {
4118 .hash = __VECS(blake2b_160_tv_template)
4119 }
4120 }, {
4121 .alg = "blake2b-256",
4122 .test = alg_test_hash,
4123 .fips_allowed = 0,
4124 .suite = {
4125 .hash = __VECS(blake2b_256_tv_template)
4126 }
4127 }, {
4128 .alg = "blake2b-384",
4129 .test = alg_test_hash,
4130 .fips_allowed = 0,
4131 .suite = {
4132 .hash = __VECS(blake2b_384_tv_template)
4133 }
4134 }, {
4135 .alg = "blake2b-512",
4136 .test = alg_test_hash,
4137 .fips_allowed = 0,
4138 .suite = {
4139 .hash = __VECS(blake2b_512_tv_template)
4140 }
4141 }, {
Ard Biesheuvel17e1df62019-11-08 13:22:29 +01004142 .alg = "blake2s-128",
4143 .test = alg_test_hash,
4144 .suite = {
4145 .hash = __VECS(blakes2s_128_tv_template)
4146 }
4147 }, {
4148 .alg = "blake2s-160",
4149 .test = alg_test_hash,
4150 .suite = {
4151 .hash = __VECS(blakes2s_160_tv_template)
4152 }
4153 }, {
4154 .alg = "blake2s-224",
4155 .test = alg_test_hash,
4156 .suite = {
4157 .hash = __VECS(blakes2s_224_tv_template)
4158 }
4159 }, {
4160 .alg = "blake2s-256",
4161 .test = alg_test_hash,
4162 .suite = {
4163 .hash = __VECS(blakes2s_256_tv_template)
4164 }
4165 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004166 .alg = "cbc(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004167 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004168 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004169 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004170 .cipher = __VECS(aes_cbc_tv_template)
4171 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004172 }, {
4173 .alg = "cbc(anubis)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004174 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004175 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004176 .cipher = __VECS(anubis_cbc_tv_template)
4177 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004178 }, {
4179 .alg = "cbc(blowfish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004180 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004181 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004182 .cipher = __VECS(bf_cbc_tv_template)
4183 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004184 }, {
4185 .alg = "cbc(camellia)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004186 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004187 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004188 .cipher = __VECS(camellia_cbc_tv_template)
4189 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004190 }, {
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02004191 .alg = "cbc(cast5)",
4192 .test = alg_test_skcipher,
4193 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004194 .cipher = __VECS(cast5_cbc_tv_template)
4195 },
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02004196 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004197 .alg = "cbc(cast6)",
4198 .test = alg_test_skcipher,
4199 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004200 .cipher = __VECS(cast6_cbc_tv_template)
4201 },
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004202 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004203 .alg = "cbc(des)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004204 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004205 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004206 .cipher = __VECS(des_cbc_tv_template)
4207 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004208 }, {
4209 .alg = "cbc(des3_ede)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004210 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004211 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004212 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004213 .cipher = __VECS(des3_ede_cbc_tv_template)
4214 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004215 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01004216 /* Same as cbc(aes) except the key is stored in
4217 * hardware secure memory which we reference by index
4218 */
4219 .alg = "cbc(paes)",
4220 .test = alg_test_null,
4221 .fips_allowed = 1,
4222 }, {
Gilad Ben-Yosseff0372c02019-04-18 16:38:36 +03004223 /* Same as cbc(sm4) except the key is stored in
4224 * hardware secure memory which we reference by index
4225 */
4226 .alg = "cbc(psm4)",
4227 .test = alg_test_null,
4228 }, {
Jussi Kivilinna9d259172011-10-18 00:02:53 +03004229 .alg = "cbc(serpent)",
4230 .test = alg_test_skcipher,
4231 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004232 .cipher = __VECS(serpent_cbc_tv_template)
4233 },
Jussi Kivilinna9d259172011-10-18 00:02:53 +03004234 }, {
Gilad Ben-Yossef95ba5972018-09-20 14:18:38 +01004235 .alg = "cbc(sm4)",
4236 .test = alg_test_skcipher,
4237 .suite = {
4238 .cipher = __VECS(sm4_cbc_tv_template)
4239 }
4240 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004241 .alg = "cbc(twofish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004242 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004243 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004244 .cipher = __VECS(tf_cbc_tv_template)
4245 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004246 }, {
Ard Biesheuvel092acf02017-02-03 14:49:35 +00004247 .alg = "cbcmac(aes)",
4248 .fips_allowed = 1,
4249 .test = alg_test_hash,
4250 .suite = {
4251 .hash = __VECS(aes_cbcmac_tv_template)
4252 }
4253 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004254 .alg = "ccm(aes)",
Eric Biggers40153b12019-04-11 21:57:41 -07004255 .generic_driver = "ccm_base(ctr(aes-generic),cbcmac(aes-generic))",
Herbert Xuda7f0332008-07-31 17:08:25 +08004256 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004257 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004258 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004259 .aead = __VECS(aes_ccm_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004260 }
4261 }, {
Dmitry Eremin-Solenikov7da66672018-10-20 02:01:53 +03004262 .alg = "cfb(aes)",
4263 .test = alg_test_skcipher,
4264 .fips_allowed = 1,
4265 .suite = {
4266 .cipher = __VECS(aes_cfb_tv_template)
4267 },
4268 }, {
Pascal van Leeuwena06b15b2019-09-13 11:10:39 +02004269 .alg = "cfb(sm4)",
4270 .test = alg_test_skcipher,
4271 .suite = {
4272 .cipher = __VECS(sm4_cfb_tv_template)
4273 }
4274 }, {
Martin Willi3590ebf2015-06-01 13:43:57 +02004275 .alg = "chacha20",
4276 .test = alg_test_skcipher,
4277 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004278 .cipher = __VECS(chacha20_tv_template)
4279 },
Martin Willi3590ebf2015-06-01 13:43:57 +02004280 }, {
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03004281 .alg = "cmac(aes)",
Stephan Mueller8f183752015-08-19 08:42:07 +02004282 .fips_allowed = 1,
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03004283 .test = alg_test_hash,
4284 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004285 .hash = __VECS(aes_cmac128_tv_template)
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03004286 }
4287 }, {
4288 .alg = "cmac(des3_ede)",
Stephan Mueller8f183752015-08-19 08:42:07 +02004289 .fips_allowed = 1,
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03004290 .test = alg_test_hash,
4291 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004292 .hash = __VECS(des3_ede_cmac64_tv_template)
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03004293 }
4294 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03004295 .alg = "compress_null",
4296 .test = alg_test_null,
4297 }, {
Ard Biesheuvelebb34722015-05-04 11:00:17 +02004298 .alg = "crc32",
4299 .test = alg_test_hash,
Milan Broza8a34412019-01-25 10:31:47 +01004300 .fips_allowed = 1,
Ard Biesheuvelebb34722015-05-04 11:00:17 +02004301 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004302 .hash = __VECS(crc32_tv_template)
Ard Biesheuvelebb34722015-05-04 11:00:17 +02004303 }
4304 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004305 .alg = "crc32c",
Herbert Xu8e3ee852008-11-07 14:58:52 +08004306 .test = alg_test_crc32c,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004307 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004308 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004309 .hash = __VECS(crc32c_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004310 }
4311 }, {
Herbert Xu684115212013-09-07 12:56:26 +10004312 .alg = "crct10dif",
4313 .test = alg_test_hash,
4314 .fips_allowed = 1,
4315 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004316 .hash = __VECS(crct10dif_tv_template)
Herbert Xu684115212013-09-07 12:56:26 +10004317 }
4318 }, {
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08004319 .alg = "ctr(aes)",
4320 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004321 .fips_allowed = 1,
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08004322 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004323 .cipher = __VECS(aes_ctr_tv_template)
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08004324 }
4325 }, {
Jussi Kivilinna85b63e32011-10-10 23:03:03 +03004326 .alg = "ctr(blowfish)",
4327 .test = alg_test_skcipher,
4328 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004329 .cipher = __VECS(bf_ctr_tv_template)
Jussi Kivilinna85b63e32011-10-10 23:03:03 +03004330 }
4331 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02004332 .alg = "ctr(camellia)",
4333 .test = alg_test_skcipher,
4334 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004335 .cipher = __VECS(camellia_ctr_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02004336 }
4337 }, {
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02004338 .alg = "ctr(cast5)",
4339 .test = alg_test_skcipher,
4340 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004341 .cipher = __VECS(cast5_ctr_tv_template)
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02004342 }
4343 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004344 .alg = "ctr(cast6)",
4345 .test = alg_test_skcipher,
4346 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004347 .cipher = __VECS(cast6_ctr_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004348 }
4349 }, {
Jussi Kivilinna8163fc32012-10-20 14:53:07 +03004350 .alg = "ctr(des)",
4351 .test = alg_test_skcipher,
4352 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004353 .cipher = __VECS(des_ctr_tv_template)
Jussi Kivilinna8163fc32012-10-20 14:53:07 +03004354 }
4355 }, {
Jussi Kivilinnae080b172012-10-20 14:53:12 +03004356 .alg = "ctr(des3_ede)",
4357 .test = alg_test_skcipher,
Marcelo Cerri0d8da102017-03-20 17:28:05 -03004358 .fips_allowed = 1,
Jussi Kivilinnae080b172012-10-20 14:53:12 +03004359 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004360 .cipher = __VECS(des3_ede_ctr_tv_template)
Jussi Kivilinnae080b172012-10-20 14:53:12 +03004361 }
4362 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01004363 /* Same as ctr(aes) except the key is stored in
4364 * hardware secure memory which we reference by index
4365 */
4366 .alg = "ctr(paes)",
4367 .test = alg_test_null,
4368 .fips_allowed = 1,
4369 }, {
Gilad Ben-Yosseff0372c02019-04-18 16:38:36 +03004370
4371 /* Same as ctr(sm4) except the key is stored in
4372 * hardware secure memory which we reference by index
4373 */
4374 .alg = "ctr(psm4)",
4375 .test = alg_test_null,
4376 }, {
Jussi Kivilinna9d259172011-10-18 00:02:53 +03004377 .alg = "ctr(serpent)",
4378 .test = alg_test_skcipher,
4379 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004380 .cipher = __VECS(serpent_ctr_tv_template)
Jussi Kivilinna9d259172011-10-18 00:02:53 +03004381 }
4382 }, {
Gilad Ben-Yossef95ba5972018-09-20 14:18:38 +01004383 .alg = "ctr(sm4)",
4384 .test = alg_test_skcipher,
4385 .suite = {
4386 .cipher = __VECS(sm4_ctr_tv_template)
4387 }
4388 }, {
Jussi Kivilinna573da622011-10-10 23:03:12 +03004389 .alg = "ctr(twofish)",
4390 .test = alg_test_skcipher,
4391 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004392 .cipher = __VECS(tf_ctr_tv_template)
Jussi Kivilinna573da622011-10-10 23:03:12 +03004393 }
4394 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004395 .alg = "cts(cbc(aes))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004396 .test = alg_test_skcipher,
Gilad Ben-Yossef196ad602018-11-04 10:05:24 +00004397 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004398 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004399 .cipher = __VECS(cts_mode_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004400 }
4401 }, {
Gilad Ben-Yosseff0372c02019-04-18 16:38:36 +03004402 /* Same as cts(cbc((aes)) except the key is stored in
4403 * hardware secure memory which we reference by index
4404 */
4405 .alg = "cts(cbc(paes))",
4406 .test = alg_test_null,
4407 .fips_allowed = 1,
4408 }, {
Ard Biesheuvelf6134572019-11-08 13:22:33 +01004409 .alg = "curve25519",
4410 .test = alg_test_kpp,
4411 .suite = {
4412 .kpp = __VECS(curve25519_tv_template)
4413 }
4414 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004415 .alg = "deflate",
4416 .test = alg_test_comp,
Milan Broz08189042012-12-06 17:16:28 +08004417 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004418 .suite = {
4419 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004420 .comp = __VECS(deflate_comp_tv_template),
4421 .decomp = __VECS(deflate_decomp_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004422 }
4423 }
4424 }, {
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01004425 .alg = "dh",
4426 .test = alg_test_kpp,
4427 .fips_allowed = 1,
4428 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004429 .kpp = __VECS(dh_tv_template)
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01004430 }
4431 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03004432 .alg = "digest_null",
4433 .test = alg_test_null,
4434 }, {
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004435 .alg = "drbg_nopr_ctr_aes128",
4436 .test = alg_test_drbg,
4437 .fips_allowed = 1,
4438 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004439 .drbg = __VECS(drbg_nopr_ctr_aes128_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004440 }
4441 }, {
4442 .alg = "drbg_nopr_ctr_aes192",
4443 .test = alg_test_drbg,
4444 .fips_allowed = 1,
4445 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004446 .drbg = __VECS(drbg_nopr_ctr_aes192_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004447 }
4448 }, {
4449 .alg = "drbg_nopr_ctr_aes256",
4450 .test = alg_test_drbg,
4451 .fips_allowed = 1,
4452 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004453 .drbg = __VECS(drbg_nopr_ctr_aes256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004454 }
4455 }, {
4456 /*
4457 * There is no need to specifically test the DRBG with every
4458 * backend cipher -- covered by drbg_nopr_hmac_sha256 test
4459 */
4460 .alg = "drbg_nopr_hmac_sha1",
4461 .fips_allowed = 1,
4462 .test = alg_test_null,
4463 }, {
4464 .alg = "drbg_nopr_hmac_sha256",
4465 .test = alg_test_drbg,
4466 .fips_allowed = 1,
4467 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004468 .drbg = __VECS(drbg_nopr_hmac_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004469 }
4470 }, {
4471 /* covered by drbg_nopr_hmac_sha256 test */
4472 .alg = "drbg_nopr_hmac_sha384",
4473 .fips_allowed = 1,
4474 .test = alg_test_null,
4475 }, {
4476 .alg = "drbg_nopr_hmac_sha512",
4477 .test = alg_test_null,
4478 .fips_allowed = 1,
4479 }, {
4480 .alg = "drbg_nopr_sha1",
4481 .fips_allowed = 1,
4482 .test = alg_test_null,
4483 }, {
4484 .alg = "drbg_nopr_sha256",
4485 .test = alg_test_drbg,
4486 .fips_allowed = 1,
4487 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004488 .drbg = __VECS(drbg_nopr_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004489 }
4490 }, {
4491 /* covered by drbg_nopr_sha256 test */
4492 .alg = "drbg_nopr_sha384",
4493 .fips_allowed = 1,
4494 .test = alg_test_null,
4495 }, {
4496 .alg = "drbg_nopr_sha512",
4497 .fips_allowed = 1,
4498 .test = alg_test_null,
4499 }, {
4500 .alg = "drbg_pr_ctr_aes128",
4501 .test = alg_test_drbg,
4502 .fips_allowed = 1,
4503 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004504 .drbg = __VECS(drbg_pr_ctr_aes128_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004505 }
4506 }, {
4507 /* covered by drbg_pr_ctr_aes128 test */
4508 .alg = "drbg_pr_ctr_aes192",
4509 .fips_allowed = 1,
4510 .test = alg_test_null,
4511 }, {
4512 .alg = "drbg_pr_ctr_aes256",
4513 .fips_allowed = 1,
4514 .test = alg_test_null,
4515 }, {
4516 .alg = "drbg_pr_hmac_sha1",
4517 .fips_allowed = 1,
4518 .test = alg_test_null,
4519 }, {
4520 .alg = "drbg_pr_hmac_sha256",
4521 .test = alg_test_drbg,
4522 .fips_allowed = 1,
4523 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004524 .drbg = __VECS(drbg_pr_hmac_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004525 }
4526 }, {
4527 /* covered by drbg_pr_hmac_sha256 test */
4528 .alg = "drbg_pr_hmac_sha384",
4529 .fips_allowed = 1,
4530 .test = alg_test_null,
4531 }, {
4532 .alg = "drbg_pr_hmac_sha512",
4533 .test = alg_test_null,
4534 .fips_allowed = 1,
4535 }, {
4536 .alg = "drbg_pr_sha1",
4537 .fips_allowed = 1,
4538 .test = alg_test_null,
4539 }, {
4540 .alg = "drbg_pr_sha256",
4541 .test = alg_test_drbg,
4542 .fips_allowed = 1,
4543 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004544 .drbg = __VECS(drbg_pr_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004545 }
4546 }, {
4547 /* covered by drbg_pr_sha256 test */
4548 .alg = "drbg_pr_sha384",
4549 .fips_allowed = 1,
4550 .test = alg_test_null,
4551 }, {
4552 .alg = "drbg_pr_sha512",
4553 .fips_allowed = 1,
4554 .test = alg_test_null,
4555 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004556 .alg = "ecb(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004557 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004558 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004559 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004560 .cipher = __VECS(aes_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004561 }
4562 }, {
4563 .alg = "ecb(anubis)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004564 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004565 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004566 .cipher = __VECS(anubis_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004567 }
4568 }, {
4569 .alg = "ecb(arc4)",
Ard Biesheuvel611a23c2019-06-12 18:19:57 +02004570 .generic_driver = "ecb(arc4)-generic",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004571 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004572 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004573 .cipher = __VECS(arc4_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004574 }
4575 }, {
4576 .alg = "ecb(blowfish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004577 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004578 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004579 .cipher = __VECS(bf_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004580 }
4581 }, {
4582 .alg = "ecb(camellia)",
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(camellia_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004586 }
4587 }, {
4588 .alg = "ecb(cast5)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004589 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004590 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004591 .cipher = __VECS(cast5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004592 }
4593 }, {
4594 .alg = "ecb(cast6)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004595 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004596 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004597 .cipher = __VECS(cast6_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004598 }
4599 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03004600 .alg = "ecb(cipher_null)",
4601 .test = alg_test_null,
Milan Broz6175ca22017-04-21 13:03:06 +02004602 .fips_allowed = 1,
Jussi Kivilinnae4483702013-04-07 16:43:56 +03004603 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004604 .alg = "ecb(des)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004605 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004606 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004607 .cipher = __VECS(des_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004608 }
4609 }, {
4610 .alg = "ecb(des3_ede)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004611 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004612 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004613 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004614 .cipher = __VECS(des3_ede_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004615 }
4616 }, {
Jussi Kivilinna66e5bd02013-01-19 13:31:36 +02004617 .alg = "ecb(fcrypt)",
4618 .test = alg_test_skcipher,
4619 .suite = {
4620 .cipher = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004621 .vecs = fcrypt_pcbc_tv_template,
4622 .count = 1
Jussi Kivilinna66e5bd02013-01-19 13:31:36 +02004623 }
4624 }
4625 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004626 .alg = "ecb(khazad)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004627 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004628 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004629 .cipher = __VECS(khazad_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004630 }
4631 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01004632 /* Same as ecb(aes) except the key is stored in
4633 * hardware secure memory which we reference by index
4634 */
4635 .alg = "ecb(paes)",
4636 .test = alg_test_null,
4637 .fips_allowed = 1,
4638 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004639 .alg = "ecb(seed)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004640 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004641 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004642 .cipher = __VECS(seed_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004643 }
4644 }, {
4645 .alg = "ecb(serpent)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004646 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004647 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004648 .cipher = __VECS(serpent_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004649 }
4650 }, {
Gilad Ben-Yossefcd83a8a2018-03-06 09:44:43 +00004651 .alg = "ecb(sm4)",
4652 .test = alg_test_skcipher,
4653 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004654 .cipher = __VECS(sm4_tv_template)
Gilad Ben-Yossefcd83a8a2018-03-06 09:44:43 +00004655 }
4656 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004657 .alg = "ecb(tea)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004658 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004659 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004660 .cipher = __VECS(tea_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004661 }
4662 }, {
4663 .alg = "ecb(tnepres)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004664 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004665 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004666 .cipher = __VECS(tnepres_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004667 }
4668 }, {
4669 .alg = "ecb(twofish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004670 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004671 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004672 .cipher = __VECS(tf_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004673 }
4674 }, {
4675 .alg = "ecb(xeta)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004676 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004677 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004678 .cipher = __VECS(xeta_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004679 }
4680 }, {
4681 .alg = "ecb(xtea)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004682 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004683 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004684 .cipher = __VECS(xtea_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004685 }
4686 }, {
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +01004687 .alg = "ecdh",
4688 .test = alg_test_kpp,
4689 .fips_allowed = 1,
4690 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004691 .kpp = __VECS(ecdh_tv_template)
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +01004692 }
4693 }, {
Vitaly Chikunov32fbdbd2019-04-11 18:51:21 +03004694 .alg = "ecrdsa",
4695 .test = alg_test_akcipher,
4696 .suite = {
4697 .akcipher = __VECS(ecrdsa_tv_template)
4698 }
4699 }, {
Ard Biesheuvelf975abb2019-08-19 17:17:34 +03004700 .alg = "essiv(authenc(hmac(sha256),cbc(aes)),sha256)",
4701 .test = alg_test_aead,
4702 .fips_allowed = 1,
4703 .suite = {
4704 .aead = __VECS(essiv_hmac_sha256_aes_cbc_tv_temp)
4705 }
4706 }, {
4707 .alg = "essiv(cbc(aes),sha256)",
4708 .test = alg_test_skcipher,
4709 .fips_allowed = 1,
4710 .suite = {
4711 .cipher = __VECS(essiv_aes_cbc_tv_template)
4712 }
4713 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004714 .alg = "gcm(aes)",
Eric Biggers40153b12019-04-11 21:57:41 -07004715 .generic_driver = "gcm_base(ctr(aes-generic),ghash-generic)",
Herbert Xuda7f0332008-07-31 17:08:25 +08004716 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004717 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004718 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004719 .aead = __VECS(aes_gcm_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004720 }
4721 }, {
Youquan, Song507069c2009-11-23 20:23:04 +08004722 .alg = "ghash",
4723 .test = alg_test_hash,
Jarod Wilson18c0ebd2011-01-29 15:14:35 +11004724 .fips_allowed = 1,
Youquan, Song507069c2009-11-23 20:23:04 +08004725 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004726 .hash = __VECS(ghash_tv_template)
Youquan, Song507069c2009-11-23 20:23:04 +08004727 }
4728 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004729 .alg = "hmac(md5)",
4730 .test = alg_test_hash,
4731 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004732 .hash = __VECS(hmac_md5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004733 }
4734 }, {
4735 .alg = "hmac(rmd128)",
4736 .test = alg_test_hash,
4737 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004738 .hash = __VECS(hmac_rmd128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004739 }
4740 }, {
4741 .alg = "hmac(rmd160)",
4742 .test = alg_test_hash,
4743 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004744 .hash = __VECS(hmac_rmd160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004745 }
4746 }, {
4747 .alg = "hmac(sha1)",
4748 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004749 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004750 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004751 .hash = __VECS(hmac_sha1_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004752 }
4753 }, {
4754 .alg = "hmac(sha224)",
4755 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004756 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004757 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004758 .hash = __VECS(hmac_sha224_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004759 }
4760 }, {
4761 .alg = "hmac(sha256)",
4762 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004763 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004764 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004765 .hash = __VECS(hmac_sha256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004766 }
4767 }, {
raveendra padasalagi98eca722016-07-01 11:16:54 +05304768 .alg = "hmac(sha3-224)",
4769 .test = alg_test_hash,
4770 .fips_allowed = 1,
4771 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004772 .hash = __VECS(hmac_sha3_224_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05304773 }
4774 }, {
4775 .alg = "hmac(sha3-256)",
4776 .test = alg_test_hash,
4777 .fips_allowed = 1,
4778 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004779 .hash = __VECS(hmac_sha3_256_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05304780 }
4781 }, {
4782 .alg = "hmac(sha3-384)",
4783 .test = alg_test_hash,
4784 .fips_allowed = 1,
4785 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004786 .hash = __VECS(hmac_sha3_384_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05304787 }
4788 }, {
4789 .alg = "hmac(sha3-512)",
4790 .test = alg_test_hash,
4791 .fips_allowed = 1,
4792 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004793 .hash = __VECS(hmac_sha3_512_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05304794 }
4795 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004796 .alg = "hmac(sha384)",
4797 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004798 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004799 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004800 .hash = __VECS(hmac_sha384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004801 }
4802 }, {
4803 .alg = "hmac(sha512)",
4804 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004805 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004806 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004807 .hash = __VECS(hmac_sha512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004808 }
4809 }, {
Pascal van Leeuwen8194fd12019-09-13 17:20:38 +02004810 .alg = "hmac(sm3)",
4811 .test = alg_test_hash,
4812 .suite = {
4813 .hash = __VECS(hmac_sm3_tv_template)
4814 }
4815 }, {
Vitaly Chikunov25a0b9d2018-11-07 00:00:03 +03004816 .alg = "hmac(streebog256)",
4817 .test = alg_test_hash,
4818 .suite = {
4819 .hash = __VECS(hmac_streebog256_tv_template)
4820 }
4821 }, {
4822 .alg = "hmac(streebog512)",
4823 .test = alg_test_hash,
4824 .suite = {
4825 .hash = __VECS(hmac_streebog512_tv_template)
4826 }
4827 }, {
Stephan Muellerbb5530e2015-05-25 15:10:20 +02004828 .alg = "jitterentropy_rng",
4829 .fips_allowed = 1,
4830 .test = alg_test_null,
4831 }, {
Stephan Mueller35351982015-09-21 20:59:56 +02004832 .alg = "kw(aes)",
4833 .test = alg_test_skcipher,
4834 .fips_allowed = 1,
4835 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004836 .cipher = __VECS(aes_kw_tv_template)
Stephan Mueller35351982015-09-21 20:59:56 +02004837 }
4838 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004839 .alg = "lrw(aes)",
Eric Biggersd435e102019-04-11 21:57:40 -07004840 .generic_driver = "lrw(ecb(aes-generic))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004841 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004842 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004843 .cipher = __VECS(aes_lrw_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004844 }
4845 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02004846 .alg = "lrw(camellia)",
Eric Biggersd435e102019-04-11 21:57:40 -07004847 .generic_driver = "lrw(ecb(camellia-generic))",
Jussi Kivilinna08406052012-03-05 20:26:21 +02004848 .test = alg_test_skcipher,
4849 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004850 .cipher = __VECS(camellia_lrw_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02004851 }
4852 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004853 .alg = "lrw(cast6)",
Eric Biggersd435e102019-04-11 21:57:40 -07004854 .generic_driver = "lrw(ecb(cast6-generic))",
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004855 .test = alg_test_skcipher,
4856 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004857 .cipher = __VECS(cast6_lrw_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004858 }
4859 }, {
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03004860 .alg = "lrw(serpent)",
Eric Biggersd435e102019-04-11 21:57:40 -07004861 .generic_driver = "lrw(ecb(serpent-generic))",
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03004862 .test = alg_test_skcipher,
4863 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004864 .cipher = __VECS(serpent_lrw_tv_template)
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03004865 }
4866 }, {
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03004867 .alg = "lrw(twofish)",
Eric Biggersd435e102019-04-11 21:57:40 -07004868 .generic_driver = "lrw(ecb(twofish-generic))",
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03004869 .test = alg_test_skcipher,
4870 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004871 .cipher = __VECS(tf_lrw_tv_template)
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03004872 }
4873 }, {
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02004874 .alg = "lz4",
4875 .test = alg_test_comp,
4876 .fips_allowed = 1,
4877 .suite = {
4878 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004879 .comp = __VECS(lz4_comp_tv_template),
4880 .decomp = __VECS(lz4_decomp_tv_template)
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02004881 }
4882 }
4883 }, {
4884 .alg = "lz4hc",
4885 .test = alg_test_comp,
4886 .fips_allowed = 1,
4887 .suite = {
4888 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004889 .comp = __VECS(lz4hc_comp_tv_template),
4890 .decomp = __VECS(lz4hc_decomp_tv_template)
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02004891 }
4892 }
4893 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004894 .alg = "lzo",
4895 .test = alg_test_comp,
Milan Broz08189042012-12-06 17:16:28 +08004896 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004897 .suite = {
4898 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004899 .comp = __VECS(lzo_comp_tv_template),
4900 .decomp = __VECS(lzo_decomp_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004901 }
4902 }
4903 }, {
Hannah Panf248caf2019-07-02 15:16:02 -07004904 .alg = "lzo-rle",
4905 .test = alg_test_comp,
4906 .fips_allowed = 1,
4907 .suite = {
4908 .comp = {
4909 .comp = __VECS(lzorle_comp_tv_template),
4910 .decomp = __VECS(lzorle_decomp_tv_template)
4911 }
4912 }
4913 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004914 .alg = "md4",
4915 .test = alg_test_hash,
4916 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004917 .hash = __VECS(md4_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004918 }
4919 }, {
4920 .alg = "md5",
4921 .test = alg_test_hash,
4922 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004923 .hash = __VECS(md5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004924 }
4925 }, {
4926 .alg = "michael_mic",
4927 .test = alg_test_hash,
4928 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004929 .hash = __VECS(michael_mic_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004930 }
4931 }, {
Eric Biggers26609a22018-11-16 17:26:29 -08004932 .alg = "nhpoly1305",
4933 .test = alg_test_hash,
4934 .suite = {
4935 .hash = __VECS(nhpoly1305_tv_template)
4936 }
4937 }, {
Puneet Saxenaba0e14a2011-05-04 15:04:10 +10004938 .alg = "ofb(aes)",
4939 .test = alg_test_skcipher,
4940 .fips_allowed = 1,
4941 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004942 .cipher = __VECS(aes_ofb_tv_template)
Puneet Saxenaba0e14a2011-05-04 15:04:10 +10004943 }
4944 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01004945 /* Same as ofb(aes) except the key is stored in
4946 * hardware secure memory which we reference by index
4947 */
4948 .alg = "ofb(paes)",
4949 .test = alg_test_null,
4950 .fips_allowed = 1,
4951 }, {
Pascal van Leeuwena06b15b2019-09-13 11:10:39 +02004952 .alg = "ofb(sm4)",
4953 .test = alg_test_skcipher,
4954 .suite = {
4955 .cipher = __VECS(sm4_ofb_tv_template)
4956 }
4957 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004958 .alg = "pcbc(fcrypt)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004959 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004960 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004961 .cipher = __VECS(fcrypt_pcbc_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004962 }
4963 }, {
Stephan Mueller12071072017-06-12 23:27:51 +02004964 .alg = "pkcs1pad(rsa,sha224)",
4965 .test = alg_test_null,
4966 .fips_allowed = 1,
4967 }, {
4968 .alg = "pkcs1pad(rsa,sha256)",
4969 .test = alg_test_akcipher,
4970 .fips_allowed = 1,
4971 .suite = {
4972 .akcipher = __VECS(pkcs1pad_rsa_tv_template)
4973 }
4974 }, {
4975 .alg = "pkcs1pad(rsa,sha384)",
4976 .test = alg_test_null,
4977 .fips_allowed = 1,
4978 }, {
4979 .alg = "pkcs1pad(rsa,sha512)",
4980 .test = alg_test_null,
4981 .fips_allowed = 1,
4982 }, {
Martin Willieee9dc62015-06-01 13:43:59 +02004983 .alg = "poly1305",
4984 .test = alg_test_hash,
4985 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004986 .hash = __VECS(poly1305_tv_template)
Martin Willieee9dc62015-06-01 13:43:59 +02004987 }
4988 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004989 .alg = "rfc3686(ctr(aes))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004990 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004991 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004992 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004993 .cipher = __VECS(aes_ctr_rfc3686_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004994 }
4995 }, {
Pascal van Leeuwene4886212019-09-13 11:10:42 +02004996 .alg = "rfc3686(ctr(sm4))",
4997 .test = alg_test_skcipher,
4998 .suite = {
4999 .cipher = __VECS(sm4_ctr_rfc3686_tv_template)
5000 }
5001 }, {
Herbert Xu3f31a742015-07-09 07:17:34 +08005002 .alg = "rfc4106(gcm(aes))",
Eric Biggers40153b12019-04-11 21:57:41 -07005003 .generic_driver = "rfc4106(gcm_base(ctr(aes-generic),ghash-generic))",
Adrian Hoban69435b92010-11-04 15:02:04 -04005004 .test = alg_test_aead,
Jarod Wilsondb71f29a2015-01-23 12:42:15 -05005005 .fips_allowed = 1,
Adrian Hoban69435b92010-11-04 15:02:04 -04005006 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08005007 .aead = __VECS(aes_gcm_rfc4106_tv_template)
Adrian Hoban69435b92010-11-04 15:02:04 -04005008 }
5009 }, {
Herbert Xu544c4362015-07-14 16:53:22 +08005010 .alg = "rfc4309(ccm(aes))",
Eric Biggers40153b12019-04-11 21:57:41 -07005011 .generic_driver = "rfc4309(ccm_base(ctr(aes-generic),cbcmac(aes-generic)))",
Jarod Wilson5d667322009-05-04 19:23:40 +08005012 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10005013 .fips_allowed = 1,
Jarod Wilson5d667322009-05-04 19:23:40 +08005014 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08005015 .aead = __VECS(aes_ccm_rfc4309_tv_template)
Jarod Wilson5d667322009-05-04 19:23:40 +08005016 }
5017 }, {
Herbert Xubb687452015-06-16 13:54:24 +08005018 .alg = "rfc4543(gcm(aes))",
Eric Biggers40153b12019-04-11 21:57:41 -07005019 .generic_driver = "rfc4543(gcm_base(ctr(aes-generic),ghash-generic))",
Jussi Kivilinnae9b74412013-04-07 16:43:51 +03005020 .test = alg_test_aead,
5021 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08005022 .aead = __VECS(aes_gcm_rfc4543_tv_template)
Jussi Kivilinnae9b74412013-04-07 16:43:51 +03005023 }
5024 }, {
Martin Williaf2b76b2015-06-01 13:44:01 +02005025 .alg = "rfc7539(chacha20,poly1305)",
5026 .test = alg_test_aead,
5027 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08005028 .aead = __VECS(rfc7539_tv_template)
Martin Williaf2b76b2015-06-01 13:44:01 +02005029 }
5030 }, {
Martin Willi59007582015-06-01 13:44:03 +02005031 .alg = "rfc7539esp(chacha20,poly1305)",
5032 .test = alg_test_aead,
5033 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08005034 .aead = __VECS(rfc7539esp_tv_template)
Martin Willi59007582015-06-01 13:44:03 +02005035 }
5036 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005037 .alg = "rmd128",
5038 .test = alg_test_hash,
5039 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005040 .hash = __VECS(rmd128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005041 }
5042 }, {
5043 .alg = "rmd160",
5044 .test = alg_test_hash,
5045 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005046 .hash = __VECS(rmd160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005047 }
5048 }, {
5049 .alg = "rmd256",
5050 .test = alg_test_hash,
5051 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005052 .hash = __VECS(rmd256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005053 }
5054 }, {
5055 .alg = "rmd320",
5056 .test = alg_test_hash,
5057 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005058 .hash = __VECS(rmd320_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005059 }
5060 }, {
Tadeusz Struk946cc462015-06-16 10:31:06 -07005061 .alg = "rsa",
5062 .test = alg_test_akcipher,
5063 .fips_allowed = 1,
5064 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005065 .akcipher = __VECS(rsa_tv_template)
Tadeusz Struk946cc462015-06-16 10:31:06 -07005066 }
5067 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005068 .alg = "salsa20",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005069 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08005070 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005071 .cipher = __VECS(salsa20_stream_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005072 }
5073 }, {
5074 .alg = "sha1",
5075 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10005076 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08005077 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005078 .hash = __VECS(sha1_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005079 }
5080 }, {
5081 .alg = "sha224",
5082 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10005083 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08005084 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005085 .hash = __VECS(sha224_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005086 }
5087 }, {
5088 .alg = "sha256",
5089 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10005090 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08005091 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005092 .hash = __VECS(sha256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005093 }
5094 }, {
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05305095 .alg = "sha3-224",
5096 .test = alg_test_hash,
5097 .fips_allowed = 1,
5098 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005099 .hash = __VECS(sha3_224_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05305100 }
5101 }, {
5102 .alg = "sha3-256",
5103 .test = alg_test_hash,
5104 .fips_allowed = 1,
5105 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005106 .hash = __VECS(sha3_256_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05305107 }
5108 }, {
5109 .alg = "sha3-384",
5110 .test = alg_test_hash,
5111 .fips_allowed = 1,
5112 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005113 .hash = __VECS(sha3_384_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05305114 }
5115 }, {
5116 .alg = "sha3-512",
5117 .test = alg_test_hash,
5118 .fips_allowed = 1,
5119 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005120 .hash = __VECS(sha3_512_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05305121 }
5122 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005123 .alg = "sha384",
5124 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10005125 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08005126 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005127 .hash = __VECS(sha384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005128 }
5129 }, {
5130 .alg = "sha512",
5131 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10005132 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08005133 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005134 .hash = __VECS(sha512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005135 }
5136 }, {
Gilad Ben-Yossefb7e27532017-08-21 13:51:29 +03005137 .alg = "sm3",
5138 .test = alg_test_hash,
5139 .suite = {
5140 .hash = __VECS(sm3_tv_template)
5141 }
5142 }, {
Vitaly Chikunov25a0b9d2018-11-07 00:00:03 +03005143 .alg = "streebog256",
5144 .test = alg_test_hash,
5145 .suite = {
5146 .hash = __VECS(streebog256_tv_template)
5147 }
5148 }, {
5149 .alg = "streebog512",
5150 .test = alg_test_hash,
5151 .suite = {
5152 .hash = __VECS(streebog512_tv_template)
5153 }
5154 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005155 .alg = "tgr128",
5156 .test = alg_test_hash,
5157 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005158 .hash = __VECS(tgr128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005159 }
5160 }, {
5161 .alg = "tgr160",
5162 .test = alg_test_hash,
5163 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005164 .hash = __VECS(tgr160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005165 }
5166 }, {
5167 .alg = "tgr192",
5168 .test = alg_test_hash,
5169 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005170 .hash = __VECS(tgr192_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005171 }
5172 }, {
Eric Biggersed331ad2018-06-18 10:22:39 -07005173 .alg = "vmac64(aes)",
5174 .test = alg_test_hash,
5175 .suite = {
5176 .hash = __VECS(vmac64_aes_tv_template)
5177 }
5178 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005179 .alg = "wp256",
5180 .test = alg_test_hash,
5181 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005182 .hash = __VECS(wp256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005183 }
5184 }, {
5185 .alg = "wp384",
5186 .test = alg_test_hash,
5187 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005188 .hash = __VECS(wp384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005189 }
5190 }, {
5191 .alg = "wp512",
5192 .test = alg_test_hash,
5193 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005194 .hash = __VECS(wp512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005195 }
5196 }, {
5197 .alg = "xcbc(aes)",
5198 .test = alg_test_hash,
5199 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005200 .hash = __VECS(aes_xcbc128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005201 }
5202 }, {
Eric Biggersaa762402018-11-16 17:26:22 -08005203 .alg = "xchacha12",
5204 .test = alg_test_skcipher,
5205 .suite = {
5206 .cipher = __VECS(xchacha12_tv_template)
5207 },
5208 }, {
Eric Biggersde61d7a2018-11-16 17:26:20 -08005209 .alg = "xchacha20",
5210 .test = alg_test_skcipher,
5211 .suite = {
5212 .cipher = __VECS(xchacha20_tv_template)
5213 },
5214 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005215 .alg = "xts(aes)",
Eric Biggersd435e102019-04-11 21:57:40 -07005216 .generic_driver = "xts(ecb(aes-generic))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005217 .test = alg_test_skcipher,
Jarod Wilson2918aa82011-01-29 15:14:01 +11005218 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08005219 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005220 .cipher = __VECS(aes_xts_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005221 }
Geert Uytterhoeven0c01aed2009-03-04 15:42:15 +08005222 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02005223 .alg = "xts(camellia)",
Eric Biggersd435e102019-04-11 21:57:40 -07005224 .generic_driver = "xts(ecb(camellia-generic))",
Jussi Kivilinna08406052012-03-05 20:26:21 +02005225 .test = alg_test_skcipher,
5226 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005227 .cipher = __VECS(camellia_xts_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02005228 }
5229 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02005230 .alg = "xts(cast6)",
Eric Biggersd435e102019-04-11 21:57:40 -07005231 .generic_driver = "xts(ecb(cast6-generic))",
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02005232 .test = alg_test_skcipher,
5233 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005234 .cipher = __VECS(cast6_xts_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02005235 }
5236 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01005237 /* Same as xts(aes) except the key is stored in
5238 * hardware secure memory which we reference by index
5239 */
5240 .alg = "xts(paes)",
5241 .test = alg_test_null,
5242 .fips_allowed = 1,
5243 }, {
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03005244 .alg = "xts(serpent)",
Eric Biggersd435e102019-04-11 21:57:40 -07005245 .generic_driver = "xts(ecb(serpent-generic))",
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03005246 .test = alg_test_skcipher,
5247 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005248 .cipher = __VECS(serpent_xts_tv_template)
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03005249 }
5250 }, {
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03005251 .alg = "xts(twofish)",
Eric Biggersd435e102019-04-11 21:57:40 -07005252 .generic_driver = "xts(ecb(twofish-generic))",
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03005253 .test = alg_test_skcipher,
5254 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005255 .cipher = __VECS(tf_xts_tv_template)
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03005256 }
Giovanni Cabiddua368f432017-04-21 21:54:30 +01005257 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01005258 .alg = "xts4096(paes)",
5259 .test = alg_test_null,
5260 .fips_allowed = 1,
5261 }, {
5262 .alg = "xts512(paes)",
5263 .test = alg_test_null,
5264 .fips_allowed = 1,
5265 }, {
Nikolay Borisov67882e72019-05-30 09:52:57 +03005266 .alg = "xxhash64",
5267 .test = alg_test_hash,
5268 .fips_allowed = 1,
5269 .suite = {
5270 .hash = __VECS(xxhash64_tv_template)
5271 }
5272 }, {
Giovanni Cabiddua368f432017-04-21 21:54:30 +01005273 .alg = "zlib-deflate",
5274 .test = alg_test_comp,
5275 .fips_allowed = 1,
5276 .suite = {
5277 .comp = {
5278 .comp = __VECS(zlib_deflate_comp_tv_template),
5279 .decomp = __VECS(zlib_deflate_decomp_tv_template)
5280 }
5281 }
Nick Terrelld28fc3d2018-03-30 12:14:53 -07005282 }, {
5283 .alg = "zstd",
5284 .test = alg_test_comp,
5285 .fips_allowed = 1,
5286 .suite = {
5287 .comp = {
5288 .comp = __VECS(zstd_comp_tv_template),
5289 .decomp = __VECS(zstd_decomp_tv_template)
5290 }
5291 }
Herbert Xuda7f0332008-07-31 17:08:25 +08005292 }
5293};
5294
Eric Biggers3f47a032019-01-31 23:51:43 -08005295static void alg_check_test_descs_order(void)
Jussi Kivilinna57147582013-06-13 17:37:40 +03005296{
5297 int i;
5298
Jussi Kivilinna57147582013-06-13 17:37:40 +03005299 for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
5300 int diff = strcmp(alg_test_descs[i - 1].alg,
5301 alg_test_descs[i].alg);
5302
5303 if (WARN_ON(diff > 0)) {
5304 pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
5305 alg_test_descs[i - 1].alg,
5306 alg_test_descs[i].alg);
5307 }
5308
5309 if (WARN_ON(diff == 0)) {
5310 pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
5311 alg_test_descs[i].alg);
5312 }
5313 }
5314}
5315
Eric Biggers3f47a032019-01-31 23:51:43 -08005316static void alg_check_testvec_configs(void)
5317{
Eric Biggers4e7babba2019-01-31 23:51:46 -08005318 int i;
5319
5320 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++)
5321 WARN_ON(!valid_testvec_config(
5322 &default_cipher_testvec_configs[i]));
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08005323
5324 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++)
5325 WARN_ON(!valid_testvec_config(
5326 &default_hash_testvec_configs[i]));
Eric Biggers3f47a032019-01-31 23:51:43 -08005327}
5328
5329static void testmgr_onetime_init(void)
5330{
5331 alg_check_test_descs_order();
5332 alg_check_testvec_configs();
Eric Biggers5b2706a2019-01-31 23:51:44 -08005333
5334#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
5335 pr_warn("alg: extra crypto tests enabled. This is intended for developer use only.\n");
5336#endif
Eric Biggers3f47a032019-01-31 23:51:43 -08005337}
5338
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005339static int alg_find_test(const char *alg)
Herbert Xuda7f0332008-07-31 17:08:25 +08005340{
5341 int start = 0;
5342 int end = ARRAY_SIZE(alg_test_descs);
5343
5344 while (start < end) {
5345 int i = (start + end) / 2;
5346 int diff = strcmp(alg_test_descs[i].alg, alg);
5347
5348 if (diff > 0) {
5349 end = i;
5350 continue;
5351 }
5352
5353 if (diff < 0) {
5354 start = i + 1;
5355 continue;
5356 }
5357
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005358 return i;
Herbert Xuda7f0332008-07-31 17:08:25 +08005359 }
5360
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005361 return -1;
5362}
5363
5364int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
5365{
5366 int i;
Herbert Xua68f6612009-07-02 16:32:12 +08005367 int j;
Neil Hormand12d6b62008-10-12 20:36:51 +08005368 int rc;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005369
Richard W.M. Jones9e5c9fe2016-05-03 10:00:17 +01005370 if (!fips_enabled && notests) {
5371 printk_once(KERN_INFO "alg: self-tests disabled\n");
5372 return 0;
5373 }
5374
Eric Biggers3f47a032019-01-31 23:51:43 -08005375 DO_ONCE(testmgr_onetime_init);
Jussi Kivilinna57147582013-06-13 17:37:40 +03005376
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005377 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
5378 char nalg[CRYPTO_MAX_ALG_NAME];
5379
5380 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
5381 sizeof(nalg))
5382 return -ENAMETOOLONG;
5383
5384 i = alg_find_test(nalg);
5385 if (i < 0)
5386 goto notest;
5387
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10005388 if (fips_enabled && !alg_test_descs[i].fips_allowed)
5389 goto non_fips_alg;
5390
Jarod Wilson941fb322009-05-04 19:49:23 +08005391 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
5392 goto test_done;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005393 }
5394
5395 i = alg_find_test(alg);
Herbert Xua68f6612009-07-02 16:32:12 +08005396 j = alg_find_test(driver);
5397 if (i < 0 && j < 0)
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005398 goto notest;
5399
Herbert Xua68f6612009-07-02 16:32:12 +08005400 if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
5401 (j >= 0 && !alg_test_descs[j].fips_allowed)))
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10005402 goto non_fips_alg;
5403
Herbert Xua68f6612009-07-02 16:32:12 +08005404 rc = 0;
5405 if (i >= 0)
5406 rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
5407 type, mask);
Cristian Stoica032c8ca2013-07-18 18:57:07 +03005408 if (j >= 0 && j != i)
Herbert Xua68f6612009-07-02 16:32:12 +08005409 rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
5410 type, mask);
5411
Jarod Wilson941fb322009-05-04 19:49:23 +08005412test_done:
Gilad Ben-Yossef95523892019-07-02 14:39:20 +03005413 if (rc && (fips_enabled || panic_on_fail)) {
5414 fips_fail_notify();
Eric Biggerseda69b02019-03-31 13:09:14 -07005415 panic("alg: self-tests for %s (%s) failed in %s mode!\n",
5416 driver, alg, fips_enabled ? "fips" : "panic_on_fail");
Gilad Ben-Yossef95523892019-07-02 14:39:20 +03005417 }
Neil Hormand12d6b62008-10-12 20:36:51 +08005418
Jarod Wilson29ecd4a2009-05-04 19:51:17 +08005419 if (fips_enabled && !rc)
Masanari Iida3e8cffd2014-10-07 00:37:54 +09005420 pr_info("alg: self-tests for %s (%s) passed\n", driver, alg);
Jarod Wilson29ecd4a2009-05-04 19:51:17 +08005421
Neil Hormand12d6b62008-10-12 20:36:51 +08005422 return rc;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005423
5424notest:
Herbert Xuda7f0332008-07-31 17:08:25 +08005425 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
5426 return 0;
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10005427non_fips_alg:
5428 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08005429}
Alexander Shishkin0b767f92010-06-03 20:53:43 +10005430
Herbert Xu326a6342010-08-06 09:40:28 +08005431#endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
Alexander Shishkin0b767f92010-06-03 20:53:43 +10005432
Herbert Xuda7f0332008-07-31 17:08:25 +08005433EXPORT_SYMBOL_GPL(alg_test);