blob: 4fe210845e78a2343bd301cbdbf2701a96f9ec41 [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
Eric Biggers2ea91502019-12-01 13:53:29 -08002114
2115struct aead_extra_tests_ctx {
2116 struct aead_request *req;
2117 struct crypto_aead *tfm;
2118 const char *driver;
2119 const struct alg_test_desc *test_desc;
2120 struct cipher_test_sglists *tsgls;
2121 unsigned int maxdatasize;
2122 unsigned int maxkeysize;
2123
2124 struct aead_testvec vec;
2125 char vec_name[64];
2126 char cfgname[TESTVEC_CONFIG_NAMELEN];
2127 struct testvec_config cfg;
2128};
2129
Eric Biggers40153b12019-04-11 21:57:41 -07002130/*
2131 * Generate an AEAD test vector from the given implementation.
2132 * Assumes the buffers in 'vec' were already allocated.
2133 */
2134static void generate_random_aead_testvec(struct aead_request *req,
2135 struct aead_testvec *vec,
2136 unsigned int maxkeysize,
2137 unsigned int maxdatasize,
2138 char *name, size_t max_namelen)
2139{
2140 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2141 const unsigned int ivsize = crypto_aead_ivsize(tfm);
Eric Biggers2ea91502019-12-01 13:53:29 -08002142 const unsigned int maxauthsize = crypto_aead_maxauthsize(tfm);
Eric Biggers40153b12019-04-11 21:57:41 -07002143 unsigned int authsize;
2144 unsigned int total_len;
2145 int i;
2146 struct scatterlist src[2], dst;
2147 u8 iv[MAX_IVLEN];
2148 DECLARE_CRYPTO_WAIT(wait);
2149
2150 /* Key: length in [0, maxkeysize], but usually choose maxkeysize */
2151 vec->klen = maxkeysize;
2152 if (prandom_u32() % 4 == 0)
2153 vec->klen = prandom_u32() % (maxkeysize + 1);
2154 generate_random_bytes((u8 *)vec->key, vec->klen);
2155 vec->setkey_error = crypto_aead_setkey(tfm, vec->key, vec->klen);
2156
2157 /* IV */
2158 generate_random_bytes((u8 *)vec->iv, ivsize);
2159
2160 /* Tag length: in [0, maxauthsize], but usually choose maxauthsize */
2161 authsize = maxauthsize;
2162 if (prandom_u32() % 4 == 0)
2163 authsize = prandom_u32() % (maxauthsize + 1);
2164 if (WARN_ON(authsize > maxdatasize))
2165 authsize = maxdatasize;
2166 maxdatasize -= authsize;
2167 vec->setauthsize_error = crypto_aead_setauthsize(tfm, authsize);
2168
2169 /* Plaintext and associated data */
2170 total_len = generate_random_length(maxdatasize);
2171 if (prandom_u32() % 4 == 0)
2172 vec->alen = 0;
2173 else
2174 vec->alen = generate_random_length(total_len);
2175 vec->plen = total_len - vec->alen;
2176 generate_random_bytes((u8 *)vec->assoc, vec->alen);
2177 generate_random_bytes((u8 *)vec->ptext, vec->plen);
2178
2179 vec->clen = vec->plen + authsize;
2180
2181 /*
2182 * If the key or authentication tag size couldn't be set, no need to
2183 * continue to encrypt.
2184 */
Eric Biggerseb455db2019-12-01 13:53:26 -08002185 vec->crypt_error = 0;
Eric Biggers40153b12019-04-11 21:57:41 -07002186 if (vec->setkey_error || vec->setauthsize_error)
2187 goto done;
2188
2189 /* Ciphertext */
2190 sg_init_table(src, 2);
2191 i = 0;
2192 if (vec->alen)
2193 sg_set_buf(&src[i++], vec->assoc, vec->alen);
2194 if (vec->plen)
2195 sg_set_buf(&src[i++], vec->ptext, vec->plen);
2196 sg_init_one(&dst, vec->ctext, vec->alen + vec->clen);
2197 memcpy(iv, vec->iv, ivsize);
2198 aead_request_set_callback(req, 0, crypto_req_done, &wait);
2199 aead_request_set_crypt(req, src, &dst, vec->plen, iv);
2200 aead_request_set_ad(req, vec->alen);
2201 vec->crypt_error = crypto_wait_req(crypto_aead_encrypt(req), &wait);
2202 if (vec->crypt_error == 0)
2203 memmove((u8 *)vec->ctext, vec->ctext + vec->alen, vec->clen);
2204done:
2205 snprintf(name, max_namelen,
2206 "\"random: alen=%u plen=%u authsize=%u klen=%u\"",
2207 vec->alen, vec->plen, authsize, vec->klen);
2208}
2209
2210/*
Eric Biggers2ea91502019-12-01 13:53:29 -08002211 * Test the AEAD algorithm against the corresponding generic implementation, if
2212 * one is available.
Eric Biggers40153b12019-04-11 21:57:41 -07002213 */
Eric Biggers2ea91502019-12-01 13:53:29 -08002214static int test_aead_vs_generic_impl(struct aead_extra_tests_ctx *ctx)
Eric Biggers40153b12019-04-11 21:57:41 -07002215{
Eric Biggers2ea91502019-12-01 13:53:29 -08002216 struct crypto_aead *tfm = ctx->tfm;
Eric Biggers40153b12019-04-11 21:57:41 -07002217 const char *algname = crypto_aead_alg(tfm)->base.cra_name;
Eric Biggers2ea91502019-12-01 13:53:29 -08002218 const char *driver = ctx->driver;
2219 const char *generic_driver = ctx->test_desc->generic_driver;
Eric Biggers40153b12019-04-11 21:57:41 -07002220 char _generic_driver[CRYPTO_MAX_ALG_NAME];
2221 struct crypto_aead *generic_tfm = NULL;
2222 struct aead_request *generic_req = NULL;
Eric Biggers40153b12019-04-11 21:57:41 -07002223 unsigned int i;
Eric Biggers40153b12019-04-11 21:57:41 -07002224 int err;
2225
Eric Biggers40153b12019-04-11 21:57:41 -07002226 if (!generic_driver) { /* Use default naming convention? */
2227 err = build_generic_driver_name(algname, _generic_driver);
2228 if (err)
2229 return err;
2230 generic_driver = _generic_driver;
2231 }
2232
2233 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
2234 return 0;
2235
2236 generic_tfm = crypto_alloc_aead(generic_driver, 0, 0);
2237 if (IS_ERR(generic_tfm)) {
2238 err = PTR_ERR(generic_tfm);
2239 if (err == -ENOENT) {
2240 pr_warn("alg: aead: skipping comparison tests for %s because %s is unavailable\n",
2241 driver, generic_driver);
2242 return 0;
2243 }
2244 pr_err("alg: aead: error allocating %s (generic impl of %s): %d\n",
2245 generic_driver, algname, err);
2246 return err;
2247 }
2248
2249 generic_req = aead_request_alloc(generic_tfm, GFP_KERNEL);
2250 if (!generic_req) {
2251 err = -ENOMEM;
2252 goto out;
2253 }
2254
2255 /* Check the algorithm properties for consistency. */
2256
Eric Biggers2ea91502019-12-01 13:53:29 -08002257 if (crypto_aead_maxauthsize(tfm) !=
2258 crypto_aead_maxauthsize(generic_tfm)) {
Eric Biggers40153b12019-04-11 21:57:41 -07002259 pr_err("alg: aead: maxauthsize for %s (%u) doesn't match generic impl (%u)\n",
Eric Biggers2ea91502019-12-01 13:53:29 -08002260 driver, crypto_aead_maxauthsize(tfm),
2261 crypto_aead_maxauthsize(generic_tfm));
Eric Biggers40153b12019-04-11 21:57:41 -07002262 err = -EINVAL;
2263 goto out;
2264 }
2265
Eric Biggers2ea91502019-12-01 13:53:29 -08002266 if (crypto_aead_ivsize(tfm) != crypto_aead_ivsize(generic_tfm)) {
Eric Biggers40153b12019-04-11 21:57:41 -07002267 pr_err("alg: aead: ivsize for %s (%u) doesn't match generic impl (%u)\n",
Eric Biggers2ea91502019-12-01 13:53:29 -08002268 driver, crypto_aead_ivsize(tfm),
2269 crypto_aead_ivsize(generic_tfm));
Eric Biggers40153b12019-04-11 21:57:41 -07002270 err = -EINVAL;
2271 goto out;
2272 }
2273
Eric Biggers2ea91502019-12-01 13:53:29 -08002274 if (crypto_aead_blocksize(tfm) != crypto_aead_blocksize(generic_tfm)) {
Eric Biggers40153b12019-04-11 21:57:41 -07002275 pr_err("alg: aead: blocksize for %s (%u) doesn't match generic impl (%u)\n",
Eric Biggers2ea91502019-12-01 13:53:29 -08002276 driver, crypto_aead_blocksize(tfm),
2277 crypto_aead_blocksize(generic_tfm));
Eric Biggers40153b12019-04-11 21:57:41 -07002278 err = -EINVAL;
2279 goto out;
2280 }
2281
2282 /*
2283 * Now generate test vectors using the generic implementation, and test
2284 * the other implementation against them.
2285 */
Eric Biggers40153b12019-04-11 21:57:41 -07002286 for (i = 0; i < fuzz_iterations * 8; i++) {
Eric Biggers2ea91502019-12-01 13:53:29 -08002287 generate_random_aead_testvec(generic_req, &ctx->vec,
2288 ctx->maxkeysize, ctx->maxdatasize,
2289 ctx->vec_name,
2290 sizeof(ctx->vec_name));
2291 generate_random_testvec_config(&ctx->cfg, ctx->cfgname,
2292 sizeof(ctx->cfgname));
2293 err = test_aead_vec_cfg(driver, ENCRYPT, &ctx->vec,
2294 ctx->vec_name, &ctx->cfg,
2295 ctx->req, ctx->tsgls);
Eric Biggers40153b12019-04-11 21:57:41 -07002296 if (err)
2297 goto out;
Eric Biggers2ea91502019-12-01 13:53:29 -08002298 if (ctx->vec.crypt_error == 0) {
2299 err = test_aead_vec_cfg(driver, DECRYPT, &ctx->vec,
2300 ctx->vec_name, &ctx->cfg,
2301 ctx->req, ctx->tsgls);
Eric Biggerseb455db2019-12-01 13:53:26 -08002302 if (err)
2303 goto out;
2304 }
Eric Biggers40153b12019-04-11 21:57:41 -07002305 cond_resched();
2306 }
2307 err = 0;
2308out:
Eric Biggers40153b12019-04-11 21:57:41 -07002309 crypto_free_aead(generic_tfm);
2310 aead_request_free(generic_req);
2311 return err;
2312}
Eric Biggers2ea91502019-12-01 13:53:29 -08002313
2314static int test_aead_extra(const char *driver,
2315 const struct alg_test_desc *test_desc,
2316 struct aead_request *req,
2317 struct cipher_test_sglists *tsgls)
2318{
2319 struct aead_extra_tests_ctx *ctx;
2320 unsigned int i;
2321 int err;
2322
2323 if (noextratests)
2324 return 0;
2325
2326 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2327 if (!ctx)
2328 return -ENOMEM;
2329 ctx->req = req;
2330 ctx->tfm = crypto_aead_reqtfm(req);
2331 ctx->driver = driver;
2332 ctx->test_desc = test_desc;
2333 ctx->tsgls = tsgls;
2334 ctx->maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
2335 ctx->maxkeysize = 0;
2336 for (i = 0; i < test_desc->suite.aead.count; i++)
2337 ctx->maxkeysize = max_t(unsigned int, ctx->maxkeysize,
2338 test_desc->suite.aead.vecs[i].klen);
2339
2340 ctx->vec.key = kmalloc(ctx->maxkeysize, GFP_KERNEL);
2341 ctx->vec.iv = kmalloc(crypto_aead_ivsize(ctx->tfm), GFP_KERNEL);
2342 ctx->vec.assoc = kmalloc(ctx->maxdatasize, GFP_KERNEL);
2343 ctx->vec.ptext = kmalloc(ctx->maxdatasize, GFP_KERNEL);
2344 ctx->vec.ctext = kmalloc(ctx->maxdatasize, GFP_KERNEL);
2345 if (!ctx->vec.key || !ctx->vec.iv || !ctx->vec.assoc ||
2346 !ctx->vec.ptext || !ctx->vec.ctext) {
2347 err = -ENOMEM;
2348 goto out;
2349 }
2350
2351 err = test_aead_vs_generic_impl(ctx);
2352out:
2353 kfree(ctx->vec.key);
2354 kfree(ctx->vec.iv);
2355 kfree(ctx->vec.assoc);
2356 kfree(ctx->vec.ptext);
2357 kfree(ctx->vec.ctext);
2358 kfree(ctx);
2359 return err;
2360}
Eric Biggers40153b12019-04-11 21:57:41 -07002361#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
Eric Biggers2ea91502019-12-01 13:53:29 -08002362static int test_aead_extra(const char *driver,
2363 const struct alg_test_desc *test_desc,
2364 struct aead_request *req,
2365 struct cipher_test_sglists *tsgls)
Eric Biggers40153b12019-04-11 21:57:41 -07002366{
2367 return 0;
2368}
2369#endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
2370
Eric Biggersed968042019-01-31 23:51:47 -08002371static int test_aead(const char *driver, int enc,
2372 const struct aead_test_suite *suite,
2373 struct aead_request *req,
2374 struct cipher_test_sglists *tsgls)
2375{
2376 unsigned int i;
2377 int err;
2378
2379 for (i = 0; i < suite->count; i++) {
2380 err = test_aead_vec(driver, enc, &suite->vecs[i], i, req,
2381 tsgls);
2382 if (err)
2383 return err;
Eric Biggerse63e1b02019-06-02 22:42:33 -07002384 cond_resched();
Eric Biggersed968042019-01-31 23:51:47 -08002385 }
2386 return 0;
2387}
2388
2389static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
2390 u32 type, u32 mask)
2391{
2392 const struct aead_test_suite *suite = &desc->suite.aead;
2393 struct crypto_aead *tfm;
2394 struct aead_request *req = NULL;
2395 struct cipher_test_sglists *tsgls = NULL;
2396 int err;
2397
2398 if (suite->count <= 0) {
2399 pr_err("alg: aead: empty test suite for %s\n", driver);
2400 return -EINVAL;
2401 }
2402
2403 tfm = crypto_alloc_aead(driver, type, mask);
2404 if (IS_ERR(tfm)) {
2405 pr_err("alg: aead: failed to allocate transform for %s: %ld\n",
2406 driver, PTR_ERR(tfm));
2407 return PTR_ERR(tfm);
2408 }
2409
2410 req = aead_request_alloc(tfm, GFP_KERNEL);
2411 if (!req) {
2412 pr_err("alg: aead: failed to allocate request for %s\n",
2413 driver);
2414 err = -ENOMEM;
2415 goto out;
2416 }
2417
2418 tsgls = alloc_cipher_test_sglists();
2419 if (!tsgls) {
2420 pr_err("alg: aead: failed to allocate test buffers for %s\n",
2421 driver);
2422 err = -ENOMEM;
2423 goto out;
2424 }
2425
2426 err = test_aead(driver, ENCRYPT, suite, req, tsgls);
2427 if (err)
2428 goto out;
2429
2430 err = test_aead(driver, DECRYPT, suite, req, tsgls);
Eric Biggers40153b12019-04-11 21:57:41 -07002431 if (err)
2432 goto out;
2433
Eric Biggers2ea91502019-12-01 13:53:29 -08002434 err = test_aead_extra(driver, desc, req, tsgls);
Eric Biggersed968042019-01-31 23:51:47 -08002435out:
2436 free_cipher_test_sglists(tsgls);
2437 aead_request_free(req);
2438 crypto_free_aead(tfm);
2439 return err;
2440}
2441
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002442static int test_cipher(struct crypto_cipher *tfm, int enc,
Eric Biggersb13b1e02017-02-24 15:46:59 -08002443 const struct cipher_testvec *template,
2444 unsigned int tcount)
Herbert Xuda7f0332008-07-31 17:08:25 +08002445{
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002446 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
2447 unsigned int i, j, k;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002448 char *q;
2449 const char *e;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002450 const char *input, *result;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002451 void *data;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08002452 char *xbuf[XBUFSIZE];
2453 int ret = -ENOMEM;
2454
2455 if (testmgr_alloc_buf(xbuf))
2456 goto out_nobuf;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002457
2458 if (enc == ENCRYPT)
2459 e = "encryption";
2460 else
2461 e = "decryption";
2462
2463 j = 0;
2464 for (i = 0; i < tcount; i++) {
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002465
Stephan Mueller10faa8c2016-08-25 15:15:01 +02002466 if (fips_enabled && template[i].fips_skip)
2467 continue;
2468
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002469 input = enc ? template[i].ptext : template[i].ctext;
2470 result = enc ? template[i].ctext : template[i].ptext;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002471 j++;
2472
Herbert Xufd57f222009-05-29 16:05:42 +10002473 ret = -EINVAL;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002474 if (WARN_ON(template[i].len > PAGE_SIZE))
Herbert Xufd57f222009-05-29 16:05:42 +10002475 goto out;
2476
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002477 data = xbuf[0];
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002478 memcpy(data, input, template[i].len);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002479
2480 crypto_cipher_clear_flags(tfm, ~0);
2481 if (template[i].wk)
Eric Biggers231baec2019-01-18 22:48:00 -08002482 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002483
2484 ret = crypto_cipher_setkey(tfm, template[i].key,
2485 template[i].klen);
Eric Biggers5283a8e2019-04-11 21:57:36 -07002486 if (ret) {
2487 if (ret == template[i].setkey_error)
2488 continue;
2489 pr_err("alg: cipher: %s setkey failed on test vector %u; expected_error=%d, actual_error=%d, flags=%#x\n",
2490 algo, j, template[i].setkey_error, ret,
2491 crypto_cipher_get_flags(tfm));
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002492 goto out;
Eric Biggers5283a8e2019-04-11 21:57:36 -07002493 }
2494 if (template[i].setkey_error) {
2495 pr_err("alg: cipher: %s setkey unexpectedly succeeded on test vector %u; expected_error=%d\n",
2496 algo, j, template[i].setkey_error);
2497 ret = -EINVAL;
2498 goto out;
2499 }
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002500
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002501 for (k = 0; k < template[i].len;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002502 k += crypto_cipher_blocksize(tfm)) {
2503 if (enc)
2504 crypto_cipher_encrypt_one(tfm, data + k,
2505 data + k);
2506 else
2507 crypto_cipher_decrypt_one(tfm, data + k,
2508 data + k);
2509 }
2510
2511 q = data;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002512 if (memcmp(q, result, template[i].len)) {
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002513 printk(KERN_ERR "alg: cipher: Test %d failed "
2514 "on %s for %s\n", j, e, algo);
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002515 hexdump(q, template[i].len);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002516 ret = -EINVAL;
2517 goto out;
2518 }
2519 }
2520
2521 ret = 0;
2522
2523out:
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08002524 testmgr_free_buf(xbuf);
2525out_nobuf:
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002526 return ret;
2527}
2528
Eric Biggers4e7babba2019-01-31 23:51:46 -08002529static int test_skcipher_vec_cfg(const char *driver, int enc,
2530 const struct cipher_testvec *vec,
Eric Biggers951d1332019-04-11 21:57:37 -07002531 const char *vec_name,
Eric Biggers4e7babba2019-01-31 23:51:46 -08002532 const struct testvec_config *cfg,
2533 struct skcipher_request *req,
2534 struct cipher_test_sglists *tsgls)
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002535{
Eric Biggers4e7babba2019-01-31 23:51:46 -08002536 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2537 const unsigned int alignmask = crypto_skcipher_alignmask(tfm);
2538 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2539 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
2540 const char *op = enc ? "encryption" : "decryption";
2541 DECLARE_CRYPTO_WAIT(wait);
2542 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
2543 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
2544 cfg->iv_offset +
2545 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
2546 struct kvec input;
2547 int err;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08002548
Eric Biggers4e7babba2019-01-31 23:51:46 -08002549 /* Set the key */
2550 if (vec->wk)
2551 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03002552 else
Eric Biggers4e7babba2019-01-31 23:51:46 -08002553 crypto_skcipher_clear_flags(tfm,
2554 CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Eric Biggersfd8c37c2019-12-01 13:53:28 -08002555 err = do_setkey(crypto_skcipher_setkey, tfm, vec->key, vec->klen,
2556 cfg, alignmask);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002557 if (err) {
Eric Biggers5283a8e2019-04-11 21:57:36 -07002558 if (err == vec->setkey_error)
Eric Biggers4e7babba2019-01-31 23:51:46 -08002559 return 0;
Eric Biggers951d1332019-04-11 21:57:37 -07002560 pr_err("alg: skcipher: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n",
2561 driver, vec_name, vec->setkey_error, err,
Eric Biggers5283a8e2019-04-11 21:57:36 -07002562 crypto_skcipher_get_flags(tfm));
Eric Biggers4e7babba2019-01-31 23:51:46 -08002563 return err;
2564 }
Eric Biggers5283a8e2019-04-11 21:57:36 -07002565 if (vec->setkey_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07002566 pr_err("alg: skcipher: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n",
2567 driver, vec_name, vec->setkey_error);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002568 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08002569 }
2570
Eric Biggers4e7babba2019-01-31 23:51:46 -08002571 /* The IV must be copied to a buffer, as the algorithm may modify it */
2572 if (ivsize) {
2573 if (WARN_ON(ivsize > MAX_IVLEN))
2574 return -EINVAL;
Eric Biggers8efd9722019-02-14 00:03:51 -08002575 if (vec->generates_iv && !enc)
2576 memcpy(iv, vec->iv_out, ivsize);
2577 else if (vec->iv)
Eric Biggers4e7babba2019-01-31 23:51:46 -08002578 memcpy(iv, vec->iv, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08002579 else
Eric Biggers4e7babba2019-01-31 23:51:46 -08002580 memset(iv, 0, ivsize);
2581 } else {
2582 if (vec->generates_iv) {
Eric Biggers951d1332019-04-11 21:57:37 -07002583 pr_err("alg: skcipher: %s has ivsize=0 but test vector %s generates IV!\n",
2584 driver, vec_name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002585 return -EINVAL;
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03002586 }
Eric Biggers4e7babba2019-01-31 23:51:46 -08002587 iv = NULL;
Herbert Xuda7f0332008-07-31 17:08:25 +08002588 }
2589
Eric Biggers4e7babba2019-01-31 23:51:46 -08002590 /* Build the src/dst scatterlists */
2591 input.iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
2592 input.iov_len = vec->len;
2593 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
2594 vec->len, vec->len, &input, 1);
2595 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07002596 pr_err("alg: skcipher: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n",
2597 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002598 return err;
Herbert Xuda7f0332008-07-31 17:08:25 +08002599 }
2600
Eric Biggers4e7babba2019-01-31 23:51:46 -08002601 /* Do the actual encryption or decryption */
2602 testmgr_poison(req->__ctx, crypto_skcipher_reqsize(tfm));
2603 skcipher_request_set_callback(req, req_flags, crypto_req_done, &wait);
2604 skcipher_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
2605 vec->len, iv);
Eric Biggers65707372019-03-12 22:12:52 -07002606 if (cfg->nosimd)
2607 crypto_disable_simd_for_test();
2608 err = enc ? crypto_skcipher_encrypt(req) : crypto_skcipher_decrypt(req);
2609 if (cfg->nosimd)
2610 crypto_reenable_simd_for_test();
2611 err = crypto_wait_req(err, &wait);
Herbert Xuda7f0332008-07-31 17:08:25 +08002612
Eric Biggersfa353c92019-01-31 23:51:49 -08002613 /* Check that the algorithm didn't overwrite things it shouldn't have */
2614 if (req->cryptlen != vec->len ||
2615 req->iv != iv ||
2616 req->src != tsgls->src.sgl_ptr ||
2617 req->dst != tsgls->dst.sgl_ptr ||
2618 crypto_skcipher_reqtfm(req) != tfm ||
2619 req->base.complete != crypto_req_done ||
2620 req->base.flags != req_flags ||
2621 req->base.data != &wait) {
Eric Biggers951d1332019-04-11 21:57:37 -07002622 pr_err("alg: skcipher: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n",
2623 driver, op, vec_name, cfg->name);
Eric Biggersfa353c92019-01-31 23:51:49 -08002624 if (req->cryptlen != vec->len)
2625 pr_err("alg: skcipher: changed 'req->cryptlen'\n");
2626 if (req->iv != iv)
2627 pr_err("alg: skcipher: changed 'req->iv'\n");
2628 if (req->src != tsgls->src.sgl_ptr)
2629 pr_err("alg: skcipher: changed 'req->src'\n");
2630 if (req->dst != tsgls->dst.sgl_ptr)
2631 pr_err("alg: skcipher: changed 'req->dst'\n");
2632 if (crypto_skcipher_reqtfm(req) != tfm)
2633 pr_err("alg: skcipher: changed 'req->base.tfm'\n");
2634 if (req->base.complete != crypto_req_done)
2635 pr_err("alg: skcipher: changed 'req->base.complete'\n");
2636 if (req->base.flags != req_flags)
2637 pr_err("alg: skcipher: changed 'req->base.flags'\n");
2638 if (req->base.data != &wait)
2639 pr_err("alg: skcipher: changed 'req->base.data'\n");
2640 return -EINVAL;
2641 }
2642 if (is_test_sglist_corrupted(&tsgls->src)) {
Eric Biggers951d1332019-04-11 21:57:37 -07002643 pr_err("alg: skcipher: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n",
2644 driver, op, vec_name, cfg->name);
Eric Biggersfa353c92019-01-31 23:51:49 -08002645 return -EINVAL;
2646 }
2647 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
2648 is_test_sglist_corrupted(&tsgls->dst)) {
Eric Biggers951d1332019-04-11 21:57:37 -07002649 pr_err("alg: skcipher: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n",
2650 driver, op, vec_name, cfg->name);
Eric Biggersfa353c92019-01-31 23:51:49 -08002651 return -EINVAL;
2652 }
2653
Eric Biggers5283a8e2019-04-11 21:57:36 -07002654 /* Check for success or failure */
2655 if (err) {
2656 if (err == vec->crypt_error)
2657 return 0;
Eric Biggers951d1332019-04-11 21:57:37 -07002658 pr_err("alg: skcipher: %s %s failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n",
2659 driver, op, vec_name, vec->crypt_error, err, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07002660 return err;
2661 }
2662 if (vec->crypt_error) {
Eric Biggers951d1332019-04-11 21:57:37 -07002663 pr_err("alg: skcipher: %s %s unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n",
2664 driver, op, vec_name, vec->crypt_error, cfg->name);
Eric Biggers5283a8e2019-04-11 21:57:36 -07002665 return -EINVAL;
2666 }
2667
Eric Biggers4e7babba2019-01-31 23:51:46 -08002668 /* Check for the correct output (ciphertext or plaintext) */
2669 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
2670 vec->len, 0, true);
2671 if (err == -EOVERFLOW) {
Eric Biggers951d1332019-04-11 21:57:37 -07002672 pr_err("alg: skcipher: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n",
2673 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002674 return err;
2675 }
2676 if (err) {
Eric Biggers951d1332019-04-11 21:57:37 -07002677 pr_err("alg: skcipher: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n",
2678 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002679 return err;
2680 }
Herbert Xuda7f0332008-07-31 17:08:25 +08002681
Eric Biggers4e7babba2019-01-31 23:51:46 -08002682 /* If applicable, check that the algorithm generated the correct IV */
Eric Biggers8efd9722019-02-14 00:03:51 -08002683 if (vec->iv_out && memcmp(iv, vec->iv_out, ivsize) != 0) {
Eric Biggers951d1332019-04-11 21:57:37 -07002684 pr_err("alg: skcipher: %s %s test failed (wrong output IV) on test vector %s, cfg=\"%s\"\n",
2685 driver, op, vec_name, cfg->name);
Eric Biggers4e7babba2019-01-31 23:51:46 -08002686 hexdump(iv, ivsize);
2687 return -EINVAL;
Jussi Kivilinna3a338f22013-06-13 17:37:45 +03002688 }
2689
2690 return 0;
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03002691}
2692
Eric Biggers4e7babba2019-01-31 23:51:46 -08002693static int test_skcipher_vec(const char *driver, int enc,
2694 const struct cipher_testvec *vec,
2695 unsigned int vec_num,
2696 struct skcipher_request *req,
2697 struct cipher_test_sglists *tsgls)
2698{
Eric Biggers951d1332019-04-11 21:57:37 -07002699 char vec_name[16];
Eric Biggers4e7babba2019-01-31 23:51:46 -08002700 unsigned int i;
2701 int err;
2702
2703 if (fips_enabled && vec->fips_skip)
2704 return 0;
2705
Eric Biggers951d1332019-04-11 21:57:37 -07002706 sprintf(vec_name, "%u", vec_num);
2707
Eric Biggers4e7babba2019-01-31 23:51:46 -08002708 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
Eric Biggers951d1332019-04-11 21:57:37 -07002709 err = test_skcipher_vec_cfg(driver, enc, vec, vec_name,
Eric Biggers4e7babba2019-01-31 23:51:46 -08002710 &default_cipher_testvec_configs[i],
2711 req, tsgls);
2712 if (err)
2713 return err;
2714 }
2715
2716#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2717 if (!noextratests) {
2718 struct testvec_config cfg;
2719 char cfgname[TESTVEC_CONFIG_NAMELEN];
2720
2721 for (i = 0; i < fuzz_iterations; i++) {
2722 generate_random_testvec_config(&cfg, cfgname,
2723 sizeof(cfgname));
Eric Biggers951d1332019-04-11 21:57:37 -07002724 err = test_skcipher_vec_cfg(driver, enc, vec, vec_name,
Eric Biggers4e7babba2019-01-31 23:51:46 -08002725 &cfg, req, tsgls);
2726 if (err)
2727 return err;
Eric Biggerse63e1b02019-06-02 22:42:33 -07002728 cond_resched();
Eric Biggers4e7babba2019-01-31 23:51:46 -08002729 }
2730 }
2731#endif
2732 return 0;
2733}
2734
Eric Biggersd435e102019-04-11 21:57:40 -07002735#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
2736/*
2737 * Generate a symmetric cipher test vector from the given implementation.
2738 * Assumes the buffers in 'vec' were already allocated.
2739 */
2740static void generate_random_cipher_testvec(struct skcipher_request *req,
2741 struct cipher_testvec *vec,
2742 unsigned int maxdatasize,
2743 char *name, size_t max_namelen)
2744{
2745 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
Eric Biggers9ac0d132019-11-29 10:23:04 -08002746 const unsigned int maxkeysize = crypto_skcipher_max_keysize(tfm);
Eric Biggersd435e102019-04-11 21:57:40 -07002747 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2748 struct scatterlist src, dst;
2749 u8 iv[MAX_IVLEN];
2750 DECLARE_CRYPTO_WAIT(wait);
2751
2752 /* Key: length in [0, maxkeysize], but usually choose maxkeysize */
2753 vec->klen = maxkeysize;
2754 if (prandom_u32() % 4 == 0)
2755 vec->klen = prandom_u32() % (maxkeysize + 1);
2756 generate_random_bytes((u8 *)vec->key, vec->klen);
2757 vec->setkey_error = crypto_skcipher_setkey(tfm, vec->key, vec->klen);
2758
2759 /* IV */
2760 generate_random_bytes((u8 *)vec->iv, ivsize);
2761
2762 /* Plaintext */
2763 vec->len = generate_random_length(maxdatasize);
2764 generate_random_bytes((u8 *)vec->ptext, vec->len);
2765
2766 /* If the key couldn't be set, no need to continue to encrypt. */
2767 if (vec->setkey_error)
2768 goto done;
2769
2770 /* Ciphertext */
2771 sg_init_one(&src, vec->ptext, vec->len);
2772 sg_init_one(&dst, vec->ctext, vec->len);
2773 memcpy(iv, vec->iv, ivsize);
2774 skcipher_request_set_callback(req, 0, crypto_req_done, &wait);
2775 skcipher_request_set_crypt(req, &src, &dst, vec->len, iv);
2776 vec->crypt_error = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
Eric Biggerseb455db2019-12-01 13:53:26 -08002777 if (vec->crypt_error != 0) {
2778 /*
2779 * The only acceptable error here is for an invalid length, so
2780 * skcipher decryption should fail with the same error too.
2781 * We'll test for this. But to keep the API usage well-defined,
2782 * explicitly initialize the ciphertext buffer too.
2783 */
2784 memset((u8 *)vec->ctext, 0, vec->len);
2785 }
Eric Biggersd435e102019-04-11 21:57:40 -07002786done:
2787 snprintf(name, max_namelen, "\"random: len=%u klen=%u\"",
2788 vec->len, vec->klen);
2789}
2790
2791/*
2792 * Test the skcipher algorithm represented by @req against the corresponding
2793 * generic implementation, if one is available.
2794 */
2795static int test_skcipher_vs_generic_impl(const char *driver,
2796 const char *generic_driver,
2797 struct skcipher_request *req,
2798 struct cipher_test_sglists *tsgls)
2799{
2800 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
Eric Biggers9ac0d132019-11-29 10:23:04 -08002801 const unsigned int maxkeysize = crypto_skcipher_max_keysize(tfm);
Eric Biggersd435e102019-04-11 21:57:40 -07002802 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2803 const unsigned int blocksize = crypto_skcipher_blocksize(tfm);
2804 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN;
2805 const char *algname = crypto_skcipher_alg(tfm)->base.cra_name;
2806 char _generic_driver[CRYPTO_MAX_ALG_NAME];
2807 struct crypto_skcipher *generic_tfm = NULL;
2808 struct skcipher_request *generic_req = NULL;
2809 unsigned int i;
2810 struct cipher_testvec vec = { 0 };
2811 char vec_name[64];
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002812 struct testvec_config *cfg;
Eric Biggersd435e102019-04-11 21:57:40 -07002813 char cfgname[TESTVEC_CONFIG_NAMELEN];
2814 int err;
2815
2816 if (noextratests)
2817 return 0;
2818
2819 /* Keywrap isn't supported here yet as it handles its IV differently. */
2820 if (strncmp(algname, "kw(", 3) == 0)
2821 return 0;
2822
2823 if (!generic_driver) { /* Use default naming convention? */
2824 err = build_generic_driver_name(algname, _generic_driver);
2825 if (err)
2826 return err;
2827 generic_driver = _generic_driver;
2828 }
2829
2830 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */
2831 return 0;
2832
2833 generic_tfm = crypto_alloc_skcipher(generic_driver, 0, 0);
2834 if (IS_ERR(generic_tfm)) {
2835 err = PTR_ERR(generic_tfm);
2836 if (err == -ENOENT) {
2837 pr_warn("alg: skcipher: skipping comparison tests for %s because %s is unavailable\n",
2838 driver, generic_driver);
2839 return 0;
2840 }
2841 pr_err("alg: skcipher: error allocating %s (generic impl of %s): %d\n",
2842 generic_driver, algname, err);
2843 return err;
2844 }
2845
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002846 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
2847 if (!cfg) {
2848 err = -ENOMEM;
2849 goto out;
2850 }
2851
Eric Biggersd435e102019-04-11 21:57:40 -07002852 generic_req = skcipher_request_alloc(generic_tfm, GFP_KERNEL);
2853 if (!generic_req) {
2854 err = -ENOMEM;
2855 goto out;
2856 }
2857
2858 /* Check the algorithm properties for consistency. */
2859
Eric Biggersfd60f722019-12-01 13:53:27 -08002860 if (crypto_skcipher_min_keysize(tfm) !=
2861 crypto_skcipher_min_keysize(generic_tfm)) {
2862 pr_err("alg: skcipher: min keysize for %s (%u) doesn't match generic impl (%u)\n",
2863 driver, crypto_skcipher_min_keysize(tfm),
2864 crypto_skcipher_min_keysize(generic_tfm));
2865 err = -EINVAL;
2866 goto out;
2867 }
2868
Eric Biggers9ac0d132019-11-29 10:23:04 -08002869 if (maxkeysize != crypto_skcipher_max_keysize(generic_tfm)) {
Eric Biggersd435e102019-04-11 21:57:40 -07002870 pr_err("alg: skcipher: max keysize for %s (%u) doesn't match generic impl (%u)\n",
Eric Biggers9ac0d132019-11-29 10:23:04 -08002871 driver, maxkeysize,
2872 crypto_skcipher_max_keysize(generic_tfm));
Eric Biggersd435e102019-04-11 21:57:40 -07002873 err = -EINVAL;
2874 goto out;
2875 }
2876
2877 if (ivsize != crypto_skcipher_ivsize(generic_tfm)) {
2878 pr_err("alg: skcipher: ivsize for %s (%u) doesn't match generic impl (%u)\n",
2879 driver, ivsize, crypto_skcipher_ivsize(generic_tfm));
2880 err = -EINVAL;
2881 goto out;
2882 }
2883
2884 if (blocksize != crypto_skcipher_blocksize(generic_tfm)) {
2885 pr_err("alg: skcipher: blocksize for %s (%u) doesn't match generic impl (%u)\n",
2886 driver, blocksize,
2887 crypto_skcipher_blocksize(generic_tfm));
2888 err = -EINVAL;
2889 goto out;
2890 }
2891
2892 /*
2893 * Now generate test vectors using the generic implementation, and test
2894 * the other implementation against them.
2895 */
2896
Eric Biggers9ac0d132019-11-29 10:23:04 -08002897 vec.key = kmalloc(maxkeysize, GFP_KERNEL);
Eric Biggersd435e102019-04-11 21:57:40 -07002898 vec.iv = kmalloc(ivsize, GFP_KERNEL);
2899 vec.ptext = kmalloc(maxdatasize, GFP_KERNEL);
2900 vec.ctext = kmalloc(maxdatasize, GFP_KERNEL);
2901 if (!vec.key || !vec.iv || !vec.ptext || !vec.ctext) {
2902 err = -ENOMEM;
2903 goto out;
2904 }
2905
2906 for (i = 0; i < fuzz_iterations * 8; i++) {
2907 generate_random_cipher_testvec(generic_req, &vec, maxdatasize,
2908 vec_name, sizeof(vec_name));
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002909 generate_random_testvec_config(cfg, cfgname, sizeof(cfgname));
Eric Biggersd435e102019-04-11 21:57:40 -07002910
2911 err = test_skcipher_vec_cfg(driver, ENCRYPT, &vec, vec_name,
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002912 cfg, req, tsgls);
Eric Biggersd435e102019-04-11 21:57:40 -07002913 if (err)
2914 goto out;
2915 err = test_skcipher_vec_cfg(driver, DECRYPT, &vec, vec_name,
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002916 cfg, req, tsgls);
Eric Biggersd435e102019-04-11 21:57:40 -07002917 if (err)
2918 goto out;
2919 cond_resched();
2920 }
2921 err = 0;
2922out:
Arnd Bergmann6b5ca642019-06-18 11:21:52 +02002923 kfree(cfg);
Eric Biggersd435e102019-04-11 21:57:40 -07002924 kfree(vec.key);
2925 kfree(vec.iv);
2926 kfree(vec.ptext);
2927 kfree(vec.ctext);
2928 crypto_free_skcipher(generic_tfm);
2929 skcipher_request_free(generic_req);
2930 return err;
2931}
2932#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
2933static int test_skcipher_vs_generic_impl(const char *driver,
2934 const char *generic_driver,
2935 struct skcipher_request *req,
2936 struct cipher_test_sglists *tsgls)
2937{
2938 return 0;
2939}
2940#endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
2941
Eric Biggers4e7babba2019-01-31 23:51:46 -08002942static int test_skcipher(const char *driver, int enc,
2943 const struct cipher_test_suite *suite,
2944 struct skcipher_request *req,
2945 struct cipher_test_sglists *tsgls)
2946{
2947 unsigned int i;
2948 int err;
2949
2950 for (i = 0; i < suite->count; i++) {
2951 err = test_skcipher_vec(driver, enc, &suite->vecs[i], i, req,
2952 tsgls);
2953 if (err)
2954 return err;
Eric Biggerse63e1b02019-06-02 22:42:33 -07002955 cond_resched();
Eric Biggers4e7babba2019-01-31 23:51:46 -08002956 }
2957 return 0;
2958}
2959
2960static int alg_test_skcipher(const struct alg_test_desc *desc,
2961 const char *driver, u32 type, u32 mask)
2962{
2963 const struct cipher_test_suite *suite = &desc->suite.cipher;
2964 struct crypto_skcipher *tfm;
2965 struct skcipher_request *req = NULL;
2966 struct cipher_test_sglists *tsgls = NULL;
2967 int err;
2968
2969 if (suite->count <= 0) {
2970 pr_err("alg: skcipher: empty test suite for %s\n", driver);
2971 return -EINVAL;
2972 }
2973
2974 tfm = crypto_alloc_skcipher(driver, type, mask);
2975 if (IS_ERR(tfm)) {
2976 pr_err("alg: skcipher: failed to allocate transform for %s: %ld\n",
2977 driver, PTR_ERR(tfm));
2978 return PTR_ERR(tfm);
2979 }
2980
2981 req = skcipher_request_alloc(tfm, GFP_KERNEL);
2982 if (!req) {
2983 pr_err("alg: skcipher: failed to allocate request for %s\n",
2984 driver);
2985 err = -ENOMEM;
2986 goto out;
2987 }
2988
2989 tsgls = alloc_cipher_test_sglists();
2990 if (!tsgls) {
2991 pr_err("alg: skcipher: failed to allocate test buffers for %s\n",
2992 driver);
2993 err = -ENOMEM;
2994 goto out;
2995 }
2996
2997 err = test_skcipher(driver, ENCRYPT, suite, req, tsgls);
2998 if (err)
2999 goto out;
3000
3001 err = test_skcipher(driver, DECRYPT, suite, req, tsgls);
Eric Biggersd435e102019-04-11 21:57:40 -07003002 if (err)
3003 goto out;
3004
3005 err = test_skcipher_vs_generic_impl(driver, desc->generic_driver, req,
3006 tsgls);
Eric Biggers4e7babba2019-01-31 23:51:46 -08003007out:
3008 free_cipher_test_sglists(tsgls);
3009 skcipher_request_free(req);
3010 crypto_free_skcipher(tfm);
3011 return err;
3012}
3013
Eric Biggersb13b1e02017-02-24 15:46:59 -08003014static int test_comp(struct crypto_comp *tfm,
3015 const struct comp_testvec *ctemplate,
3016 const struct comp_testvec *dtemplate,
3017 int ctcount, int dtcount)
Herbert Xuda7f0332008-07-31 17:08:25 +08003018{
3019 const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
Mahipal Challa33607382018-04-11 20:28:32 +02003020 char *output, *decomp_output;
Herbert Xuda7f0332008-07-31 17:08:25 +08003021 unsigned int i;
Herbert Xuda7f0332008-07-31 17:08:25 +08003022 int ret;
3023
Mahipal Challa33607382018-04-11 20:28:32 +02003024 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3025 if (!output)
3026 return -ENOMEM;
3027
3028 decomp_output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3029 if (!decomp_output) {
3030 kfree(output);
3031 return -ENOMEM;
3032 }
3033
Herbert Xuda7f0332008-07-31 17:08:25 +08003034 for (i = 0; i < ctcount; i++) {
Geert Uytterhoevenc79cf912009-03-29 15:44:19 +08003035 int ilen;
3036 unsigned int dlen = COMP_BUF_SIZE;
Herbert Xuda7f0332008-07-31 17:08:25 +08003037
Michael Schupikov22a81182018-10-07 13:58:10 +02003038 memset(output, 0, COMP_BUF_SIZE);
3039 memset(decomp_output, 0, COMP_BUF_SIZE);
Herbert Xuda7f0332008-07-31 17:08:25 +08003040
3041 ilen = ctemplate[i].inlen;
3042 ret = crypto_comp_compress(tfm, ctemplate[i].input,
Mahipal Challa33607382018-04-11 20:28:32 +02003043 ilen, output, &dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08003044 if (ret) {
3045 printk(KERN_ERR "alg: comp: compression failed "
3046 "on test %d for %s: ret=%d\n", i + 1, algo,
3047 -ret);
3048 goto out;
3049 }
3050
Mahipal Challa33607382018-04-11 20:28:32 +02003051 ilen = dlen;
3052 dlen = COMP_BUF_SIZE;
3053 ret = crypto_comp_decompress(tfm, output,
3054 ilen, decomp_output, &dlen);
3055 if (ret) {
3056 pr_err("alg: comp: compression failed: decompress: on test %d for %s failed: ret=%d\n",
3057 i + 1, algo, -ret);
3058 goto out;
3059 }
3060
3061 if (dlen != ctemplate[i].inlen) {
Geert Uytterhoevenb812eb02008-11-28 20:51:28 +08003062 printk(KERN_ERR "alg: comp: Compression test %d "
3063 "failed for %s: output len = %d\n", i + 1, algo,
3064 dlen);
3065 ret = -EINVAL;
3066 goto out;
3067 }
3068
Mahipal Challa33607382018-04-11 20:28:32 +02003069 if (memcmp(decomp_output, ctemplate[i].input,
3070 ctemplate[i].inlen)) {
3071 pr_err("alg: comp: compression failed: output differs: on test %d for %s\n",
3072 i + 1, algo);
3073 hexdump(decomp_output, dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08003074 ret = -EINVAL;
3075 goto out;
3076 }
3077 }
3078
3079 for (i = 0; i < dtcount; i++) {
Geert Uytterhoevenc79cf912009-03-29 15:44:19 +08003080 int ilen;
3081 unsigned int dlen = COMP_BUF_SIZE;
Herbert Xuda7f0332008-07-31 17:08:25 +08003082
Michael Schupikov22a81182018-10-07 13:58:10 +02003083 memset(decomp_output, 0, COMP_BUF_SIZE);
Herbert Xuda7f0332008-07-31 17:08:25 +08003084
3085 ilen = dtemplate[i].inlen;
3086 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
Mahipal Challa33607382018-04-11 20:28:32 +02003087 ilen, decomp_output, &dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08003088 if (ret) {
3089 printk(KERN_ERR "alg: comp: decompression failed "
3090 "on test %d for %s: ret=%d\n", i + 1, algo,
3091 -ret);
3092 goto out;
3093 }
3094
Geert Uytterhoevenb812eb02008-11-28 20:51:28 +08003095 if (dlen != dtemplate[i].outlen) {
3096 printk(KERN_ERR "alg: comp: Decompression test %d "
3097 "failed for %s: output len = %d\n", i + 1, algo,
3098 dlen);
3099 ret = -EINVAL;
3100 goto out;
3101 }
3102
Mahipal Challa33607382018-04-11 20:28:32 +02003103 if (memcmp(decomp_output, dtemplate[i].output, dlen)) {
Herbert Xuda7f0332008-07-31 17:08:25 +08003104 printk(KERN_ERR "alg: comp: Decompression test %d "
3105 "failed for %s\n", i + 1, algo);
Mahipal Challa33607382018-04-11 20:28:32 +02003106 hexdump(decomp_output, dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08003107 ret = -EINVAL;
3108 goto out;
3109 }
3110 }
3111
3112 ret = 0;
3113
3114out:
Mahipal Challa33607382018-04-11 20:28:32 +02003115 kfree(decomp_output);
3116 kfree(output);
Herbert Xuda7f0332008-07-31 17:08:25 +08003117 return ret;
3118}
3119
Eric Biggersb13b1e02017-02-24 15:46:59 -08003120static int test_acomp(struct crypto_acomp *tfm,
Mahipal Challa33607382018-04-11 20:28:32 +02003121 const struct comp_testvec *ctemplate,
Eric Biggersb13b1e02017-02-24 15:46:59 -08003122 const struct comp_testvec *dtemplate,
3123 int ctcount, int dtcount)
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003124{
3125 const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
3126 unsigned int i;
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003127 char *output, *decomp_out;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003128 int ret;
3129 struct scatterlist src, dst;
3130 struct acomp_req *req;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003131 struct crypto_wait wait;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003132
Eric Biggerseb095592016-11-23 10:24:35 -08003133 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3134 if (!output)
3135 return -ENOMEM;
3136
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003137 decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
3138 if (!decomp_out) {
3139 kfree(output);
3140 return -ENOMEM;
3141 }
3142
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003143 for (i = 0; i < ctcount; i++) {
3144 unsigned int dlen = COMP_BUF_SIZE;
3145 int ilen = ctemplate[i].inlen;
Laura Abbott02608e02016-12-21 12:32:54 -08003146 void *input_vec;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003147
Eric Biggersd2110222016-12-30 14:12:00 -06003148 input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL);
Laura Abbott02608e02016-12-21 12:32:54 -08003149 if (!input_vec) {
3150 ret = -ENOMEM;
3151 goto out;
3152 }
3153
Eric Biggerseb095592016-11-23 10:24:35 -08003154 memset(output, 0, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003155 crypto_init_wait(&wait);
Laura Abbott02608e02016-12-21 12:32:54 -08003156 sg_init_one(&src, input_vec, ilen);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003157 sg_init_one(&dst, output, dlen);
3158
3159 req = acomp_request_alloc(tfm);
3160 if (!req) {
3161 pr_err("alg: acomp: request alloc failed for %s\n",
3162 algo);
Laura Abbott02608e02016-12-21 12:32:54 -08003163 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003164 ret = -ENOMEM;
3165 goto out;
3166 }
3167
3168 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3169 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003170 crypto_req_done, &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003171
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003172 ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003173 if (ret) {
3174 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
3175 i + 1, algo, -ret);
Laura Abbott02608e02016-12-21 12:32:54 -08003176 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003177 acomp_request_free(req);
3178 goto out;
3179 }
3180
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003181 ilen = req->dlen;
3182 dlen = COMP_BUF_SIZE;
3183 sg_init_one(&src, output, ilen);
3184 sg_init_one(&dst, decomp_out, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003185 crypto_init_wait(&wait);
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003186 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3187
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003188 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003189 if (ret) {
3190 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
3191 i + 1, algo, -ret);
3192 kfree(input_vec);
3193 acomp_request_free(req);
3194 goto out;
3195 }
3196
3197 if (req->dlen != ctemplate[i].inlen) {
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003198 pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
3199 i + 1, algo, req->dlen);
3200 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08003201 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003202 acomp_request_free(req);
3203 goto out;
3204 }
3205
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003206 if (memcmp(input_vec, decomp_out, req->dlen)) {
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003207 pr_err("alg: acomp: Compression test %d failed for %s\n",
3208 i + 1, algo);
3209 hexdump(output, req->dlen);
3210 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08003211 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003212 acomp_request_free(req);
3213 goto out;
3214 }
3215
Laura Abbott02608e02016-12-21 12:32:54 -08003216 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003217 acomp_request_free(req);
3218 }
3219
3220 for (i = 0; i < dtcount; i++) {
3221 unsigned int dlen = COMP_BUF_SIZE;
3222 int ilen = dtemplate[i].inlen;
Laura Abbott02608e02016-12-21 12:32:54 -08003223 void *input_vec;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003224
Eric Biggersd2110222016-12-30 14:12:00 -06003225 input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL);
Laura Abbott02608e02016-12-21 12:32:54 -08003226 if (!input_vec) {
3227 ret = -ENOMEM;
3228 goto out;
3229 }
3230
Eric Biggerseb095592016-11-23 10:24:35 -08003231 memset(output, 0, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003232 crypto_init_wait(&wait);
Laura Abbott02608e02016-12-21 12:32:54 -08003233 sg_init_one(&src, input_vec, ilen);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003234 sg_init_one(&dst, output, dlen);
3235
3236 req = acomp_request_alloc(tfm);
3237 if (!req) {
3238 pr_err("alg: acomp: request alloc failed for %s\n",
3239 algo);
Laura Abbott02608e02016-12-21 12:32:54 -08003240 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003241 ret = -ENOMEM;
3242 goto out;
3243 }
3244
3245 acomp_request_set_params(req, &src, &dst, ilen, dlen);
3246 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003247 crypto_req_done, &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003248
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003249 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003250 if (ret) {
3251 pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
3252 i + 1, algo, -ret);
Laura Abbott02608e02016-12-21 12:32:54 -08003253 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003254 acomp_request_free(req);
3255 goto out;
3256 }
3257
3258 if (req->dlen != dtemplate[i].outlen) {
3259 pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
3260 i + 1, algo, req->dlen);
3261 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08003262 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003263 acomp_request_free(req);
3264 goto out;
3265 }
3266
3267 if (memcmp(output, dtemplate[i].output, req->dlen)) {
3268 pr_err("alg: acomp: Decompression test %d failed for %s\n",
3269 i + 1, algo);
3270 hexdump(output, req->dlen);
3271 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08003272 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003273 acomp_request_free(req);
3274 goto out;
3275 }
3276
Laura Abbott02608e02016-12-21 12:32:54 -08003277 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003278 acomp_request_free(req);
3279 }
3280
3281 ret = 0;
3282
3283out:
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01003284 kfree(decomp_out);
Eric Biggerseb095592016-11-23 10:24:35 -08003285 kfree(output);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003286 return ret;
3287}
3288
Eric Biggersb13b1e02017-02-24 15:46:59 -08003289static int test_cprng(struct crypto_rng *tfm,
3290 const struct cprng_testvec *template,
Jarod Wilson7647d6c2009-05-04 19:44:50 +08003291 unsigned int tcount)
3292{
3293 const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
Felipe Contrerasfa4ef8a2009-10-27 19:04:42 +08003294 int err = 0, i, j, seedsize;
Jarod Wilson7647d6c2009-05-04 19:44:50 +08003295 u8 *seed;
3296 char result[32];
3297
3298 seedsize = crypto_rng_seedsize(tfm);
3299
3300 seed = kmalloc(seedsize, GFP_KERNEL);
3301 if (!seed) {
3302 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
3303 "for %s\n", algo);
3304 return -ENOMEM;
3305 }
3306
3307 for (i = 0; i < tcount; i++) {
3308 memset(result, 0, 32);
3309
3310 memcpy(seed, template[i].v, template[i].vlen);
3311 memcpy(seed + template[i].vlen, template[i].key,
3312 template[i].klen);
3313 memcpy(seed + template[i].vlen + template[i].klen,
3314 template[i].dt, template[i].dtlen);
3315
3316 err = crypto_rng_reset(tfm, seed, seedsize);
3317 if (err) {
3318 printk(KERN_ERR "alg: cprng: Failed to reset rng "
3319 "for %s\n", algo);
3320 goto out;
3321 }
3322
3323 for (j = 0; j < template[i].loops; j++) {
3324 err = crypto_rng_get_bytes(tfm, result,
3325 template[i].rlen);
Stephan Mueller19e60e12015-03-10 17:00:36 +01003326 if (err < 0) {
Jarod Wilson7647d6c2009-05-04 19:44:50 +08003327 printk(KERN_ERR "alg: cprng: Failed to obtain "
3328 "the correct amount of random data for "
Stephan Mueller19e60e12015-03-10 17:00:36 +01003329 "%s (requested %d)\n", algo,
3330 template[i].rlen);
Jarod Wilson7647d6c2009-05-04 19:44:50 +08003331 goto out;
3332 }
3333 }
3334
3335 err = memcmp(result, template[i].result,
3336 template[i].rlen);
3337 if (err) {
3338 printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
3339 i, algo);
3340 hexdump(result, template[i].rlen);
3341 err = -EINVAL;
3342 goto out;
3343 }
3344 }
3345
3346out:
3347 kfree(seed);
3348 return err;
3349}
3350
Herbert Xuda7f0332008-07-31 17:08:25 +08003351static int alg_test_cipher(const struct alg_test_desc *desc,
3352 const char *driver, u32 type, u32 mask)
3353{
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003354 const struct cipher_test_suite *suite = &desc->suite.cipher;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003355 struct crypto_cipher *tfm;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003356 int err;
Herbert Xuda7f0332008-07-31 17:08:25 +08003357
Herbert Xueed93e02016-11-22 20:08:31 +08003358 tfm = crypto_alloc_cipher(driver, type, mask);
Herbert Xuda7f0332008-07-31 17:08:25 +08003359 if (IS_ERR(tfm)) {
3360 printk(KERN_ERR "alg: cipher: Failed to load transform for "
3361 "%s: %ld\n", driver, PTR_ERR(tfm));
3362 return PTR_ERR(tfm);
3363 }
3364
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003365 err = test_cipher(tfm, ENCRYPT, suite->vecs, suite->count);
3366 if (!err)
3367 err = test_cipher(tfm, DECRYPT, suite->vecs, suite->count);
Herbert Xuda7f0332008-07-31 17:08:25 +08003368
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003369 crypto_free_cipher(tfm);
3370 return err;
3371}
3372
Herbert Xuda7f0332008-07-31 17:08:25 +08003373static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
3374 u32 type, u32 mask)
3375{
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003376 struct crypto_comp *comp;
3377 struct crypto_acomp *acomp;
Herbert Xuda7f0332008-07-31 17:08:25 +08003378 int err;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003379 u32 algo_type = type & CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
Herbert Xuda7f0332008-07-31 17:08:25 +08003380
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01003381 if (algo_type == CRYPTO_ALG_TYPE_ACOMPRESS) {
3382 acomp = crypto_alloc_acomp(driver, type, mask);
3383 if (IS_ERR(acomp)) {
3384 pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
3385 driver, PTR_ERR(acomp));
3386 return PTR_ERR(acomp);
3387 }
3388 err = test_acomp(acomp, desc->suite.comp.comp.vecs,
3389 desc->suite.comp.decomp.vecs,
3390 desc->suite.comp.comp.count,
3391 desc->suite.comp.decomp.count);
3392 crypto_free_acomp(acomp);
3393 } else {
3394 comp = crypto_alloc_comp(driver, type, mask);
3395 if (IS_ERR(comp)) {
3396 pr_err("alg: comp: Failed to load transform for %s: %ld\n",
3397 driver, PTR_ERR(comp));
3398 return PTR_ERR(comp);
3399 }
3400
3401 err = test_comp(comp, desc->suite.comp.comp.vecs,
3402 desc->suite.comp.decomp.vecs,
3403 desc->suite.comp.comp.count,
3404 desc->suite.comp.decomp.count);
3405
3406 crypto_free_comp(comp);
Herbert Xuda7f0332008-07-31 17:08:25 +08003407 }
Herbert Xuda7f0332008-07-31 17:08:25 +08003408 return err;
3409}
3410
Herbert Xu8e3ee852008-11-07 14:58:52 +08003411static int alg_test_crc32c(const struct alg_test_desc *desc,
3412 const char *driver, u32 type, u32 mask)
3413{
3414 struct crypto_shash *tfm;
Eric Biggerscb9dde82019-01-10 12:17:55 -08003415 __le32 val;
Herbert Xu8e3ee852008-11-07 14:58:52 +08003416 int err;
3417
3418 err = alg_test_hash(desc, driver, type, mask);
3419 if (err)
Eric Biggerseb5e6732019-01-23 20:57:35 -08003420 return err;
Herbert Xu8e3ee852008-11-07 14:58:52 +08003421
Herbert Xueed93e02016-11-22 20:08:31 +08003422 tfm = crypto_alloc_shash(driver, type, mask);
Herbert Xu8e3ee852008-11-07 14:58:52 +08003423 if (IS_ERR(tfm)) {
Eric Biggerseb5e6732019-01-23 20:57:35 -08003424 if (PTR_ERR(tfm) == -ENOENT) {
3425 /*
3426 * This crc32c implementation is only available through
3427 * ahash API, not the shash API, so the remaining part
3428 * of the test is not applicable to it.
3429 */
3430 return 0;
3431 }
Herbert Xu8e3ee852008-11-07 14:58:52 +08003432 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
3433 "%ld\n", driver, PTR_ERR(tfm));
Eric Biggerseb5e6732019-01-23 20:57:35 -08003434 return PTR_ERR(tfm);
Herbert Xu8e3ee852008-11-07 14:58:52 +08003435 }
3436
3437 do {
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02003438 SHASH_DESC_ON_STACK(shash, tfm);
3439 u32 *ctx = (u32 *)shash_desc_ctx(shash);
Herbert Xu8e3ee852008-11-07 14:58:52 +08003440
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02003441 shash->tfm = tfm;
Herbert Xu8e3ee852008-11-07 14:58:52 +08003442
Eric Biggerscb9dde82019-01-10 12:17:55 -08003443 *ctx = 420553207;
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02003444 err = crypto_shash_final(shash, (u8 *)&val);
Herbert Xu8e3ee852008-11-07 14:58:52 +08003445 if (err) {
3446 printk(KERN_ERR "alg: crc32c: Operation failed for "
3447 "%s: %d\n", driver, err);
3448 break;
3449 }
3450
Eric Biggerscb9dde82019-01-10 12:17:55 -08003451 if (val != cpu_to_le32(~420553207)) {
3452 pr_err("alg: crc32c: Test failed for %s: %u\n",
3453 driver, le32_to_cpu(val));
Herbert Xu8e3ee852008-11-07 14:58:52 +08003454 err = -EINVAL;
3455 }
3456 } while (0);
3457
3458 crypto_free_shash(tfm);
3459
Herbert Xu8e3ee852008-11-07 14:58:52 +08003460 return err;
3461}
3462
Jarod Wilson7647d6c2009-05-04 19:44:50 +08003463static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
3464 u32 type, u32 mask)
3465{
3466 struct crypto_rng *rng;
3467 int err;
3468
Herbert Xueed93e02016-11-22 20:08:31 +08003469 rng = crypto_alloc_rng(driver, type, mask);
Jarod Wilson7647d6c2009-05-04 19:44:50 +08003470 if (IS_ERR(rng)) {
3471 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
3472 "%ld\n", driver, PTR_ERR(rng));
3473 return PTR_ERR(rng);
3474 }
3475
3476 err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
3477
3478 crypto_free_rng(rng);
3479
3480 return err;
3481}
3482
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003483
Eric Biggersb13b1e02017-02-24 15:46:59 -08003484static int drbg_cavs_test(const struct drbg_testvec *test, int pr,
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003485 const char *driver, u32 type, u32 mask)
3486{
3487 int ret = -EAGAIN;
3488 struct crypto_rng *drng;
3489 struct drbg_test_data test_data;
3490 struct drbg_string addtl, pers, testentropy;
3491 unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
3492
3493 if (!buf)
3494 return -ENOMEM;
3495
Herbert Xueed93e02016-11-22 20:08:31 +08003496 drng = crypto_alloc_rng(driver, type, mask);
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003497 if (IS_ERR(drng)) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04003498 printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003499 "%s\n", driver);
3500 kzfree(buf);
3501 return -ENOMEM;
3502 }
3503
3504 test_data.testentropy = &testentropy;
3505 drbg_string_fill(&testentropy, test->entropy, test->entropylen);
3506 drbg_string_fill(&pers, test->pers, test->perslen);
3507 ret = crypto_drbg_reset_test(drng, &pers, &test_data);
3508 if (ret) {
3509 printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
3510 goto outbuf;
3511 }
3512
3513 drbg_string_fill(&addtl, test->addtla, test->addtllen);
3514 if (pr) {
3515 drbg_string_fill(&testentropy, test->entpra, test->entprlen);
3516 ret = crypto_drbg_get_bytes_addtl_test(drng,
3517 buf, test->expectedlen, &addtl, &test_data);
3518 } else {
3519 ret = crypto_drbg_get_bytes_addtl(drng,
3520 buf, test->expectedlen, &addtl);
3521 }
Stephan Mueller19e60e12015-03-10 17:00:36 +01003522 if (ret < 0) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04003523 printk(KERN_ERR "alg: drbg: could not obtain random data for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003524 "driver %s\n", driver);
3525 goto outbuf;
3526 }
3527
3528 drbg_string_fill(&addtl, test->addtlb, test->addtllen);
3529 if (pr) {
3530 drbg_string_fill(&testentropy, test->entprb, test->entprlen);
3531 ret = crypto_drbg_get_bytes_addtl_test(drng,
3532 buf, test->expectedlen, &addtl, &test_data);
3533 } else {
3534 ret = crypto_drbg_get_bytes_addtl(drng,
3535 buf, test->expectedlen, &addtl);
3536 }
Stephan Mueller19e60e12015-03-10 17:00:36 +01003537 if (ret < 0) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04003538 printk(KERN_ERR "alg: drbg: could not obtain random data for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003539 "driver %s\n", driver);
3540 goto outbuf;
3541 }
3542
3543 ret = memcmp(test->expected, buf, test->expectedlen);
3544
3545outbuf:
3546 crypto_free_rng(drng);
3547 kzfree(buf);
3548 return ret;
3549}
3550
3551
3552static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
3553 u32 type, u32 mask)
3554{
3555 int err = 0;
3556 int pr = 0;
3557 int i = 0;
Eric Biggersb13b1e02017-02-24 15:46:59 -08003558 const struct drbg_testvec *template = desc->suite.drbg.vecs;
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003559 unsigned int tcount = desc->suite.drbg.count;
3560
3561 if (0 == memcmp(driver, "drbg_pr_", 8))
3562 pr = 1;
3563
3564 for (i = 0; i < tcount; i++) {
3565 err = drbg_cavs_test(&template[i], pr, driver, type, mask);
3566 if (err) {
3567 printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
3568 i, driver);
3569 err = -EINVAL;
3570 break;
3571 }
3572 }
3573 return err;
3574
3575}
3576
Eric Biggersb13b1e02017-02-24 15:46:59 -08003577static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003578 const char *alg)
3579{
3580 struct kpp_request *req;
3581 void *input_buf = NULL;
3582 void *output_buf = NULL;
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003583 void *a_public = NULL;
3584 void *a_ss = NULL;
3585 void *shared_secret = NULL;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003586 struct crypto_wait wait;
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003587 unsigned int out_len_max;
3588 int err = -ENOMEM;
3589 struct scatterlist src, dst;
3590
3591 req = kpp_request_alloc(tfm, GFP_KERNEL);
3592 if (!req)
3593 return err;
3594
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003595 crypto_init_wait(&wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003596
3597 err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
3598 if (err < 0)
3599 goto free_req;
3600
3601 out_len_max = crypto_kpp_maxsize(tfm);
3602 output_buf = kzalloc(out_len_max, GFP_KERNEL);
3603 if (!output_buf) {
3604 err = -ENOMEM;
3605 goto free_req;
3606 }
3607
3608 /* Use appropriate parameter as base */
3609 kpp_request_set_input(req, NULL, 0);
3610 sg_init_one(&dst, output_buf, out_len_max);
3611 kpp_request_set_output(req, &dst, out_len_max);
3612 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003613 crypto_req_done, &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003614
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003615 /* Compute party A's public key */
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003616 err = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003617 if (err) {
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003618 pr_err("alg: %s: Party A: generate public key test failed. err %d\n",
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003619 alg, err);
3620 goto free_output;
3621 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003622
3623 if (vec->genkey) {
3624 /* Save party A's public key */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05003625 a_public = kmemdup(sg_virt(req->dst), out_len_max, GFP_KERNEL);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003626 if (!a_public) {
3627 err = -ENOMEM;
3628 goto free_output;
3629 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003630 } else {
3631 /* Verify calculated public key */
3632 if (memcmp(vec->expected_a_public, sg_virt(req->dst),
3633 vec->expected_a_public_size)) {
3634 pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n",
3635 alg);
3636 err = -EINVAL;
3637 goto free_output;
3638 }
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003639 }
3640
3641 /* Calculate shared secret key by using counter part (b) public key. */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05003642 input_buf = kmemdup(vec->b_public, vec->b_public_size, GFP_KERNEL);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003643 if (!input_buf) {
3644 err = -ENOMEM;
3645 goto free_output;
3646 }
3647
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003648 sg_init_one(&src, input_buf, vec->b_public_size);
3649 sg_init_one(&dst, output_buf, out_len_max);
3650 kpp_request_set_input(req, &src, vec->b_public_size);
3651 kpp_request_set_output(req, &dst, out_len_max);
3652 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003653 crypto_req_done, &wait);
3654 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003655 if (err) {
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003656 pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n",
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003657 alg, err);
3658 goto free_all;
3659 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003660
3661 if (vec->genkey) {
3662 /* Save the shared secret obtained by party A */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05003663 a_ss = kmemdup(sg_virt(req->dst), vec->expected_ss_size, GFP_KERNEL);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003664 if (!a_ss) {
3665 err = -ENOMEM;
3666 goto free_all;
3667 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003668
3669 /*
3670 * Calculate party B's shared secret by using party A's
3671 * public key.
3672 */
3673 err = crypto_kpp_set_secret(tfm, vec->b_secret,
3674 vec->b_secret_size);
3675 if (err < 0)
3676 goto free_all;
3677
3678 sg_init_one(&src, a_public, vec->expected_a_public_size);
3679 sg_init_one(&dst, output_buf, out_len_max);
3680 kpp_request_set_input(req, &src, vec->expected_a_public_size);
3681 kpp_request_set_output(req, &dst, out_len_max);
3682 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003683 crypto_req_done, &wait);
3684 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req),
3685 &wait);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003686 if (err) {
3687 pr_err("alg: %s: Party B: compute shared secret failed. err %d\n",
3688 alg, err);
3689 goto free_all;
3690 }
3691
3692 shared_secret = a_ss;
3693 } else {
3694 shared_secret = (void *)vec->expected_ss;
3695 }
3696
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003697 /*
3698 * verify shared secret from which the user will derive
3699 * secret key by executing whatever hash it has chosen
3700 */
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003701 if (memcmp(shared_secret, sg_virt(req->dst),
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003702 vec->expected_ss_size)) {
3703 pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
3704 alg);
3705 err = -EINVAL;
3706 }
3707
3708free_all:
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003709 kfree(a_ss);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003710 kfree(input_buf);
3711free_output:
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03003712 kfree(a_public);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003713 kfree(output_buf);
3714free_req:
3715 kpp_request_free(req);
3716 return err;
3717}
3718
3719static int test_kpp(struct crypto_kpp *tfm, const char *alg,
Eric Biggersb13b1e02017-02-24 15:46:59 -08003720 const struct kpp_testvec *vecs, unsigned int tcount)
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003721{
3722 int ret, i;
3723
3724 for (i = 0; i < tcount; i++) {
3725 ret = do_test_kpp(tfm, vecs++, alg);
3726 if (ret) {
3727 pr_err("alg: %s: test failed on vector %d, err=%d\n",
3728 alg, i + 1, ret);
3729 return ret;
3730 }
3731 }
3732 return 0;
3733}
3734
3735static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
3736 u32 type, u32 mask)
3737{
3738 struct crypto_kpp *tfm;
3739 int err = 0;
3740
Herbert Xueed93e02016-11-22 20:08:31 +08003741 tfm = crypto_alloc_kpp(driver, type, mask);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003742 if (IS_ERR(tfm)) {
3743 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
3744 driver, PTR_ERR(tfm));
3745 return PTR_ERR(tfm);
3746 }
3747 if (desc->suite.kpp.vecs)
3748 err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
3749 desc->suite.kpp.count);
3750
3751 crypto_free_kpp(tfm);
3752 return err;
3753}
3754
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003755static u8 *test_pack_u32(u8 *dst, u32 val)
3756{
3757 memcpy(dst, &val, sizeof(val));
3758 return dst + sizeof(val);
3759}
3760
Herbert Xu50d2b6432016-06-29 19:32:20 +08003761static int test_akcipher_one(struct crypto_akcipher *tfm,
Eric Biggersb13b1e02017-02-24 15:46:59 -08003762 const struct akcipher_testvec *vecs)
Tadeusz Struk946cc462015-06-16 10:31:06 -07003763{
Herbert Xudf27b262016-05-05 16:42:49 +08003764 char *xbuf[XBUFSIZE];
Tadeusz Struk946cc462015-06-16 10:31:06 -07003765 struct akcipher_request *req;
3766 void *outbuf_enc = NULL;
3767 void *outbuf_dec = NULL;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003768 struct crypto_wait wait;
Tadeusz Struk946cc462015-06-16 10:31:06 -07003769 unsigned int out_len_max, out_len = 0;
3770 int err = -ENOMEM;
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03003771 struct scatterlist src, dst, src_tab[3];
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003772 const char *m, *c;
3773 unsigned int m_size, c_size;
3774 const char *op;
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003775 u8 *key, *ptr;
Tadeusz Struk946cc462015-06-16 10:31:06 -07003776
Herbert Xudf27b262016-05-05 16:42:49 +08003777 if (testmgr_alloc_buf(xbuf))
3778 return err;
3779
Tadeusz Struk946cc462015-06-16 10:31:06 -07003780 req = akcipher_request_alloc(tfm, GFP_KERNEL);
3781 if (!req)
Herbert Xudf27b262016-05-05 16:42:49 +08003782 goto free_xbuf;
Tadeusz Struk946cc462015-06-16 10:31:06 -07003783
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003784 crypto_init_wait(&wait);
Tadeusz Struk22287b02015-10-08 09:26:55 -07003785
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003786 key = kmalloc(vecs->key_len + sizeof(u32) * 2 + vecs->param_len,
3787 GFP_KERNEL);
3788 if (!key)
3789 goto free_xbuf;
3790 memcpy(key, vecs->key, vecs->key_len);
3791 ptr = key + vecs->key_len;
3792 ptr = test_pack_u32(ptr, vecs->algo);
3793 ptr = test_pack_u32(ptr, vecs->param_len);
3794 memcpy(ptr, vecs->params, vecs->param_len);
3795
Tadeusz Struk22287b02015-10-08 09:26:55 -07003796 if (vecs->public_key_vec)
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003797 err = crypto_akcipher_set_pub_key(tfm, key, vecs->key_len);
Tadeusz Struk22287b02015-10-08 09:26:55 -07003798 else
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003799 err = crypto_akcipher_set_priv_key(tfm, key, vecs->key_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003800 if (err)
3801 goto free_req;
3802
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003803 /*
3804 * First run test which do not require a private key, such as
3805 * encrypt or verify.
3806 */
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03003807 err = -ENOMEM;
3808 out_len_max = crypto_akcipher_maxsize(tfm);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003809 outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
3810 if (!outbuf_enc)
3811 goto free_req;
3812
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003813 if (!vecs->siggen_sigver_test) {
3814 m = vecs->m;
3815 m_size = vecs->m_size;
3816 c = vecs->c;
3817 c_size = vecs->c_size;
3818 op = "encrypt";
3819 } else {
3820 /* Swap args so we could keep plaintext (digest)
3821 * in vecs->m, and cooked signature in vecs->c.
3822 */
3823 m = vecs->c; /* signature */
3824 m_size = vecs->c_size;
3825 c = vecs->m; /* digest */
3826 c_size = vecs->m_size;
3827 op = "verify";
3828 }
Herbert Xudf27b262016-05-05 16:42:49 +08003829
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003830 if (WARN_ON(m_size > PAGE_SIZE))
3831 goto free_all;
3832 memcpy(xbuf[0], m, m_size);
Herbert Xudf27b262016-05-05 16:42:49 +08003833
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03003834 sg_init_table(src_tab, 3);
Herbert Xudf27b262016-05-05 16:42:49 +08003835 sg_set_buf(&src_tab[0], xbuf[0], 8);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003836 sg_set_buf(&src_tab[1], xbuf[0] + 8, m_size - 8);
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03003837 if (vecs->siggen_sigver_test) {
3838 if (WARN_ON(c_size > PAGE_SIZE))
3839 goto free_all;
3840 memcpy(xbuf[1], c, c_size);
3841 sg_set_buf(&src_tab[2], xbuf[1], c_size);
3842 akcipher_request_set_crypt(req, src_tab, NULL, m_size, c_size);
3843 } else {
3844 sg_init_one(&dst, outbuf_enc, out_len_max);
3845 akcipher_request_set_crypt(req, src_tab, &dst, m_size,
3846 out_len_max);
3847 }
Tadeusz Struk946cc462015-06-16 10:31:06 -07003848 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003849 crypto_req_done, &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003850
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003851 err = crypto_wait_req(vecs->siggen_sigver_test ?
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003852 /* Run asymmetric signature verification */
3853 crypto_akcipher_verify(req) :
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003854 /* Run asymmetric encrypt */
3855 crypto_akcipher_encrypt(req), &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003856 if (err) {
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003857 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003858 goto free_all;
3859 }
Vitaly Chikunovc7381b02019-04-11 18:51:15 +03003860 if (!vecs->siggen_sigver_test) {
3861 if (req->dst_len != c_size) {
3862 pr_err("alg: akcipher: %s test failed. Invalid output len\n",
3863 op);
3864 err = -EINVAL;
3865 goto free_all;
3866 }
3867 /* verify that encrypted message is equal to expected */
3868 if (memcmp(c, outbuf_enc, c_size) != 0) {
3869 pr_err("alg: akcipher: %s test failed. Invalid output\n",
3870 op);
3871 hexdump(outbuf_enc, c_size);
3872 err = -EINVAL;
3873 goto free_all;
3874 }
Tadeusz Struk946cc462015-06-16 10:31:06 -07003875 }
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003876
3877 /*
3878 * Don't invoke (decrypt or sign) test which require a private key
3879 * for vectors with only a public key.
3880 */
Tadeusz Struk946cc462015-06-16 10:31:06 -07003881 if (vecs->public_key_vec) {
3882 err = 0;
3883 goto free_all;
3884 }
3885 outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
3886 if (!outbuf_dec) {
3887 err = -ENOMEM;
3888 goto free_all;
3889 }
Herbert Xudf27b262016-05-05 16:42:49 +08003890
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003891 op = vecs->siggen_sigver_test ? "sign" : "decrypt";
3892 if (WARN_ON(c_size > PAGE_SIZE))
Herbert Xudf27b262016-05-05 16:42:49 +08003893 goto free_all;
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003894 memcpy(xbuf[0], c, c_size);
Herbert Xudf27b262016-05-05 16:42:49 +08003895
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003896 sg_init_one(&src, xbuf[0], c_size);
Tadeusz Struk22287b02015-10-08 09:26:55 -07003897 sg_init_one(&dst, outbuf_dec, out_len_max);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003898 crypto_init_wait(&wait);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003899 akcipher_request_set_crypt(req, &src, &dst, c_size, out_len_max);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003900
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003901 err = crypto_wait_req(vecs->siggen_sigver_test ?
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003902 /* Run asymmetric signature generation */
3903 crypto_akcipher_sign(req) :
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01003904 /* Run asymmetric decrypt */
3905 crypto_akcipher_decrypt(req), &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003906 if (err) {
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003907 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003908 goto free_all;
3909 }
3910 out_len = req->dst_len;
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003911 if (out_len < m_size) {
3912 pr_err("alg: akcipher: %s test failed. Invalid output len %u\n",
3913 op, out_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003914 err = -EINVAL;
3915 goto free_all;
3916 }
3917 /* verify that decrypted message is equal to the original msg */
Vitaly Chikunov0507de92019-01-07 20:54:27 +03003918 if (memchr_inv(outbuf_dec, 0, out_len - m_size) ||
3919 memcmp(m, outbuf_dec + out_len - m_size, m_size)) {
3920 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
Herbert Xu50d2b6432016-06-29 19:32:20 +08003921 hexdump(outbuf_dec, out_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003922 err = -EINVAL;
3923 }
3924free_all:
3925 kfree(outbuf_dec);
3926 kfree(outbuf_enc);
3927free_req:
3928 akcipher_request_free(req);
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +03003929 kfree(key);
Herbert Xudf27b262016-05-05 16:42:49 +08003930free_xbuf:
3931 testmgr_free_buf(xbuf);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003932 return err;
3933}
3934
Herbert Xu50d2b6432016-06-29 19:32:20 +08003935static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
Eric Biggersb13b1e02017-02-24 15:46:59 -08003936 const struct akcipher_testvec *vecs,
3937 unsigned int tcount)
Tadeusz Struk946cc462015-06-16 10:31:06 -07003938{
Herbert Xu15226e42016-07-18 18:20:10 +08003939 const char *algo =
3940 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
Tadeusz Struk946cc462015-06-16 10:31:06 -07003941 int ret, i;
3942
3943 for (i = 0; i < tcount; i++) {
Herbert Xu50d2b6432016-06-29 19:32:20 +08003944 ret = test_akcipher_one(tfm, vecs++);
3945 if (!ret)
3946 continue;
3947
Herbert Xu15226e42016-07-18 18:20:10 +08003948 pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
3949 i + 1, algo, ret);
Herbert Xu50d2b6432016-06-29 19:32:20 +08003950 return ret;
Tadeusz Struk946cc462015-06-16 10:31:06 -07003951 }
3952 return 0;
3953}
3954
Tadeusz Struk946cc462015-06-16 10:31:06 -07003955static int alg_test_akcipher(const struct alg_test_desc *desc,
3956 const char *driver, u32 type, u32 mask)
3957{
3958 struct crypto_akcipher *tfm;
3959 int err = 0;
3960
Herbert Xueed93e02016-11-22 20:08:31 +08003961 tfm = crypto_alloc_akcipher(driver, type, mask);
Tadeusz Struk946cc462015-06-16 10:31:06 -07003962 if (IS_ERR(tfm)) {
3963 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
3964 driver, PTR_ERR(tfm));
3965 return PTR_ERR(tfm);
3966 }
3967 if (desc->suite.akcipher.vecs)
3968 err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
3969 desc->suite.akcipher.count);
3970
3971 crypto_free_akcipher(tfm);
3972 return err;
3973}
3974
Youquan, Song863b5572009-12-23 19:45:20 +08003975static int alg_test_null(const struct alg_test_desc *desc,
3976 const char *driver, u32 type, u32 mask)
3977{
3978 return 0;
3979}
3980
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003981#define __VECS(tv) { .vecs = tv, .count = ARRAY_SIZE(tv) }
3982
Herbert Xuda7f0332008-07-31 17:08:25 +08003983/* Please keep this list sorted by algorithm name. */
3984static const struct alg_test_desc alg_test_descs[] = {
3985 {
Eric Biggers059c2a42018-11-16 17:26:31 -08003986 .alg = "adiantum(xchacha12,aes)",
Eric Biggersd435e102019-04-11 21:57:40 -07003987 .generic_driver = "adiantum(xchacha12-generic,aes-generic,nhpoly1305-generic)",
Eric Biggers059c2a42018-11-16 17:26:31 -08003988 .test = alg_test_skcipher,
3989 .suite = {
3990 .cipher = __VECS(adiantum_xchacha12_aes_tv_template)
3991 },
3992 }, {
3993 .alg = "adiantum(xchacha20,aes)",
Eric Biggersd435e102019-04-11 21:57:40 -07003994 .generic_driver = "adiantum(xchacha20-generic,aes-generic,nhpoly1305-generic)",
Eric Biggers059c2a42018-11-16 17:26:31 -08003995 .test = alg_test_skcipher,
3996 .suite = {
3997 .cipher = __VECS(adiantum_xchacha20_aes_tv_template)
3998 },
3999 }, {
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02004000 .alg = "aegis128",
4001 .test = alg_test_aead,
4002 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004003 .aead = __VECS(aegis128_tv_template)
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02004004 }
4005 }, {
Jarod Wilsone08ca2d2009-05-04 19:46:29 +08004006 .alg = "ansi_cprng",
4007 .test = alg_test_cprng,
4008 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004009 .cprng = __VECS(ansi_cprng_aes_tv_template)
Jarod Wilsone08ca2d2009-05-04 19:46:29 +08004010 }
4011 }, {
Horia Geantabca4feb2014-03-14 17:46:51 +02004012 .alg = "authenc(hmac(md5),ecb(cipher_null))",
4013 .test = alg_test_aead,
Horia Geantabca4feb2014-03-14 17:46:51 +02004014 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004015 .aead = __VECS(hmac_md5_ecb_cipher_null_tv_template)
Horia Geantabca4feb2014-03-14 17:46:51 +02004016 }
4017 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004018 .alg = "authenc(hmac(sha1),cbc(aes))",
Horia Geantae46e9a42012-07-03 19:16:54 +03004019 .test = alg_test_aead,
Herbert Xubcf741c2017-06-28 19:09:07 +08004020 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03004021 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004022 .aead = __VECS(hmac_sha1_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304023 }
4024 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004025 .alg = "authenc(hmac(sha1),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304026 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304027 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004028 .aead = __VECS(hmac_sha1_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304029 }
4030 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004031 .alg = "authenc(hmac(sha1),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304032 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01004033 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304034 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004035 .aead = __VECS(hmac_sha1_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03004036 }
4037 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01004038 .alg = "authenc(hmac(sha1),ctr(aes))",
4039 .test = alg_test_null,
4040 .fips_allowed = 1,
4041 }, {
Horia Geantabca4feb2014-03-14 17:46:51 +02004042 .alg = "authenc(hmac(sha1),ecb(cipher_null))",
4043 .test = alg_test_aead,
Horia Geantabca4feb2014-03-14 17:46:51 +02004044 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004045 .aead = __VECS(hmac_sha1_ecb_cipher_null_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304046 }
4047 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01004048 .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
4049 .test = alg_test_null,
4050 .fips_allowed = 1,
4051 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004052 .alg = "authenc(hmac(sha224),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304053 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304054 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004055 .aead = __VECS(hmac_sha224_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304056 }
4057 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004058 .alg = "authenc(hmac(sha224),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304059 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01004060 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304061 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004062 .aead = __VECS(hmac_sha224_des3_ede_cbc_tv_temp)
Horia Geantabca4feb2014-03-14 17:46:51 +02004063 }
4064 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004065 .alg = "authenc(hmac(sha256),cbc(aes))",
Horia Geantae46e9a42012-07-03 19:16:54 +03004066 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01004067 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03004068 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004069 .aead = __VECS(hmac_sha256_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304070 }
4071 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004072 .alg = "authenc(hmac(sha256),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304073 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304074 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004075 .aead = __VECS(hmac_sha256_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304076 }
4077 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004078 .alg = "authenc(hmac(sha256),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304079 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01004080 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304081 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004082 .aead = __VECS(hmac_sha256_des3_ede_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304083 }
4084 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01004085 .alg = "authenc(hmac(sha256),ctr(aes))",
4086 .test = alg_test_null,
4087 .fips_allowed = 1,
4088 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01004089 .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
4090 .test = alg_test_null,
4091 .fips_allowed = 1,
4092 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004093 .alg = "authenc(hmac(sha384),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_sha384_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304097 }
4098 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004099 .alg = "authenc(hmac(sha384),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_sha384_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(sha384),ctr(aes))",
4107 .test = alg_test_null,
4108 .fips_allowed = 1,
4109 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01004110 .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
4111 .test = alg_test_null,
4112 .fips_allowed = 1,
4113 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004114 .alg = "authenc(hmac(sha512),cbc(aes))",
Marcus Meissnered1afac2016-02-05 14:23:33 +01004115 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03004116 .test = alg_test_aead,
Horia Geantae46e9a42012-07-03 19:16:54 +03004117 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004118 .aead = __VECS(hmac_sha512_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304119 }
4120 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004121 .alg = "authenc(hmac(sha512),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304122 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304123 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004124 .aead = __VECS(hmac_sha512_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05304125 }
4126 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08004127 .alg = "authenc(hmac(sha512),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05304128 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01004129 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05304130 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004131 .aead = __VECS(hmac_sha512_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03004132 }
4133 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01004134 .alg = "authenc(hmac(sha512),ctr(aes))",
4135 .test = alg_test_null,
4136 .fips_allowed = 1,
4137 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01004138 .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
4139 .test = alg_test_null,
4140 .fips_allowed = 1,
4141 }, {
David Sterbaa1afe272019-10-24 18:28:32 +02004142 .alg = "blake2b-160",
4143 .test = alg_test_hash,
4144 .fips_allowed = 0,
4145 .suite = {
4146 .hash = __VECS(blake2b_160_tv_template)
4147 }
4148 }, {
4149 .alg = "blake2b-256",
4150 .test = alg_test_hash,
4151 .fips_allowed = 0,
4152 .suite = {
4153 .hash = __VECS(blake2b_256_tv_template)
4154 }
4155 }, {
4156 .alg = "blake2b-384",
4157 .test = alg_test_hash,
4158 .fips_allowed = 0,
4159 .suite = {
4160 .hash = __VECS(blake2b_384_tv_template)
4161 }
4162 }, {
4163 .alg = "blake2b-512",
4164 .test = alg_test_hash,
4165 .fips_allowed = 0,
4166 .suite = {
4167 .hash = __VECS(blake2b_512_tv_template)
4168 }
4169 }, {
Ard Biesheuvel17e1df62019-11-08 13:22:29 +01004170 .alg = "blake2s-128",
4171 .test = alg_test_hash,
4172 .suite = {
4173 .hash = __VECS(blakes2s_128_tv_template)
4174 }
4175 }, {
4176 .alg = "blake2s-160",
4177 .test = alg_test_hash,
4178 .suite = {
4179 .hash = __VECS(blakes2s_160_tv_template)
4180 }
4181 }, {
4182 .alg = "blake2s-224",
4183 .test = alg_test_hash,
4184 .suite = {
4185 .hash = __VECS(blakes2s_224_tv_template)
4186 }
4187 }, {
4188 .alg = "blake2s-256",
4189 .test = alg_test_hash,
4190 .suite = {
4191 .hash = __VECS(blakes2s_256_tv_template)
4192 }
4193 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004194 .alg = "cbc(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004195 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004196 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004197 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004198 .cipher = __VECS(aes_cbc_tv_template)
4199 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004200 }, {
4201 .alg = "cbc(anubis)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004202 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004203 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004204 .cipher = __VECS(anubis_cbc_tv_template)
4205 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004206 }, {
4207 .alg = "cbc(blowfish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004208 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004209 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004210 .cipher = __VECS(bf_cbc_tv_template)
4211 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004212 }, {
4213 .alg = "cbc(camellia)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004214 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004215 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004216 .cipher = __VECS(camellia_cbc_tv_template)
4217 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004218 }, {
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02004219 .alg = "cbc(cast5)",
4220 .test = alg_test_skcipher,
4221 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004222 .cipher = __VECS(cast5_cbc_tv_template)
4223 },
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02004224 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004225 .alg = "cbc(cast6)",
4226 .test = alg_test_skcipher,
4227 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004228 .cipher = __VECS(cast6_cbc_tv_template)
4229 },
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004230 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004231 .alg = "cbc(des)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004232 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004233 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004234 .cipher = __VECS(des_cbc_tv_template)
4235 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004236 }, {
4237 .alg = "cbc(des3_ede)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004238 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004239 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004240 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004241 .cipher = __VECS(des3_ede_cbc_tv_template)
4242 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004243 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01004244 /* Same as cbc(aes) except the key is stored in
4245 * hardware secure memory which we reference by index
4246 */
4247 .alg = "cbc(paes)",
4248 .test = alg_test_null,
4249 .fips_allowed = 1,
4250 }, {
Gilad Ben-Yosseff0372c02019-04-18 16:38:36 +03004251 /* Same as cbc(sm4) except the key is stored in
4252 * hardware secure memory which we reference by index
4253 */
4254 .alg = "cbc(psm4)",
4255 .test = alg_test_null,
4256 }, {
Jussi Kivilinna9d259172011-10-18 00:02:53 +03004257 .alg = "cbc(serpent)",
4258 .test = alg_test_skcipher,
4259 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004260 .cipher = __VECS(serpent_cbc_tv_template)
4261 },
Jussi Kivilinna9d259172011-10-18 00:02:53 +03004262 }, {
Gilad Ben-Yossef95ba5972018-09-20 14:18:38 +01004263 .alg = "cbc(sm4)",
4264 .test = alg_test_skcipher,
4265 .suite = {
4266 .cipher = __VECS(sm4_cbc_tv_template)
4267 }
4268 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004269 .alg = "cbc(twofish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004270 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004271 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004272 .cipher = __VECS(tf_cbc_tv_template)
4273 },
Herbert Xuda7f0332008-07-31 17:08:25 +08004274 }, {
Ard Biesheuvel092acf02017-02-03 14:49:35 +00004275 .alg = "cbcmac(aes)",
4276 .fips_allowed = 1,
4277 .test = alg_test_hash,
4278 .suite = {
4279 .hash = __VECS(aes_cbcmac_tv_template)
4280 }
4281 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004282 .alg = "ccm(aes)",
Eric Biggers40153b12019-04-11 21:57:41 -07004283 .generic_driver = "ccm_base(ctr(aes-generic),cbcmac(aes-generic))",
Herbert Xuda7f0332008-07-31 17:08:25 +08004284 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004285 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004286 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004287 .aead = __VECS(aes_ccm_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004288 }
4289 }, {
Dmitry Eremin-Solenikov7da66672018-10-20 02:01:53 +03004290 .alg = "cfb(aes)",
4291 .test = alg_test_skcipher,
4292 .fips_allowed = 1,
4293 .suite = {
4294 .cipher = __VECS(aes_cfb_tv_template)
4295 },
4296 }, {
Pascal van Leeuwena06b15b2019-09-13 11:10:39 +02004297 .alg = "cfb(sm4)",
4298 .test = alg_test_skcipher,
4299 .suite = {
4300 .cipher = __VECS(sm4_cfb_tv_template)
4301 }
4302 }, {
Martin Willi3590ebf2015-06-01 13:43:57 +02004303 .alg = "chacha20",
4304 .test = alg_test_skcipher,
4305 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004306 .cipher = __VECS(chacha20_tv_template)
4307 },
Martin Willi3590ebf2015-06-01 13:43:57 +02004308 }, {
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03004309 .alg = "cmac(aes)",
Stephan Mueller8f183752015-08-19 08:42:07 +02004310 .fips_allowed = 1,
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03004311 .test = alg_test_hash,
4312 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004313 .hash = __VECS(aes_cmac128_tv_template)
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03004314 }
4315 }, {
4316 .alg = "cmac(des3_ede)",
Stephan Mueller8f183752015-08-19 08:42:07 +02004317 .fips_allowed = 1,
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03004318 .test = alg_test_hash,
4319 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004320 .hash = __VECS(des3_ede_cmac64_tv_template)
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03004321 }
4322 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03004323 .alg = "compress_null",
4324 .test = alg_test_null,
4325 }, {
Ard Biesheuvelebb34722015-05-04 11:00:17 +02004326 .alg = "crc32",
4327 .test = alg_test_hash,
Milan Broza8a34412019-01-25 10:31:47 +01004328 .fips_allowed = 1,
Ard Biesheuvelebb34722015-05-04 11:00:17 +02004329 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004330 .hash = __VECS(crc32_tv_template)
Ard Biesheuvelebb34722015-05-04 11:00:17 +02004331 }
4332 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004333 .alg = "crc32c",
Herbert Xu8e3ee852008-11-07 14:58:52 +08004334 .test = alg_test_crc32c,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004335 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004336 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004337 .hash = __VECS(crc32c_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004338 }
4339 }, {
Herbert Xu684115212013-09-07 12:56:26 +10004340 .alg = "crct10dif",
4341 .test = alg_test_hash,
4342 .fips_allowed = 1,
4343 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004344 .hash = __VECS(crct10dif_tv_template)
Herbert Xu684115212013-09-07 12:56:26 +10004345 }
4346 }, {
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08004347 .alg = "ctr(aes)",
4348 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004349 .fips_allowed = 1,
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08004350 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004351 .cipher = __VECS(aes_ctr_tv_template)
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08004352 }
4353 }, {
Jussi Kivilinna85b63e32011-10-10 23:03:03 +03004354 .alg = "ctr(blowfish)",
4355 .test = alg_test_skcipher,
4356 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004357 .cipher = __VECS(bf_ctr_tv_template)
Jussi Kivilinna85b63e32011-10-10 23:03:03 +03004358 }
4359 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02004360 .alg = "ctr(camellia)",
4361 .test = alg_test_skcipher,
4362 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004363 .cipher = __VECS(camellia_ctr_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02004364 }
4365 }, {
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02004366 .alg = "ctr(cast5)",
4367 .test = alg_test_skcipher,
4368 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004369 .cipher = __VECS(cast5_ctr_tv_template)
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02004370 }
4371 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004372 .alg = "ctr(cast6)",
4373 .test = alg_test_skcipher,
4374 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004375 .cipher = __VECS(cast6_ctr_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004376 }
4377 }, {
Jussi Kivilinna8163fc32012-10-20 14:53:07 +03004378 .alg = "ctr(des)",
4379 .test = alg_test_skcipher,
4380 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004381 .cipher = __VECS(des_ctr_tv_template)
Jussi Kivilinna8163fc32012-10-20 14:53:07 +03004382 }
4383 }, {
Jussi Kivilinnae080b172012-10-20 14:53:12 +03004384 .alg = "ctr(des3_ede)",
4385 .test = alg_test_skcipher,
Marcelo Cerri0d8da102017-03-20 17:28:05 -03004386 .fips_allowed = 1,
Jussi Kivilinnae080b172012-10-20 14:53:12 +03004387 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004388 .cipher = __VECS(des3_ede_ctr_tv_template)
Jussi Kivilinnae080b172012-10-20 14:53:12 +03004389 }
4390 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01004391 /* Same as ctr(aes) except the key is stored in
4392 * hardware secure memory which we reference by index
4393 */
4394 .alg = "ctr(paes)",
4395 .test = alg_test_null,
4396 .fips_allowed = 1,
4397 }, {
Gilad Ben-Yosseff0372c02019-04-18 16:38:36 +03004398
4399 /* Same as ctr(sm4) except the key is stored in
4400 * hardware secure memory which we reference by index
4401 */
4402 .alg = "ctr(psm4)",
4403 .test = alg_test_null,
4404 }, {
Jussi Kivilinna9d259172011-10-18 00:02:53 +03004405 .alg = "ctr(serpent)",
4406 .test = alg_test_skcipher,
4407 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004408 .cipher = __VECS(serpent_ctr_tv_template)
Jussi Kivilinna9d259172011-10-18 00:02:53 +03004409 }
4410 }, {
Gilad Ben-Yossef95ba5972018-09-20 14:18:38 +01004411 .alg = "ctr(sm4)",
4412 .test = alg_test_skcipher,
4413 .suite = {
4414 .cipher = __VECS(sm4_ctr_tv_template)
4415 }
4416 }, {
Jussi Kivilinna573da622011-10-10 23:03:12 +03004417 .alg = "ctr(twofish)",
4418 .test = alg_test_skcipher,
4419 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004420 .cipher = __VECS(tf_ctr_tv_template)
Jussi Kivilinna573da622011-10-10 23:03:12 +03004421 }
4422 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004423 .alg = "cts(cbc(aes))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004424 .test = alg_test_skcipher,
Gilad Ben-Yossef196ad602018-11-04 10:05:24 +00004425 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004426 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004427 .cipher = __VECS(cts_mode_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004428 }
4429 }, {
Gilad Ben-Yosseff0372c02019-04-18 16:38:36 +03004430 /* Same as cts(cbc((aes)) except the key is stored in
4431 * hardware secure memory which we reference by index
4432 */
4433 .alg = "cts(cbc(paes))",
4434 .test = alg_test_null,
4435 .fips_allowed = 1,
4436 }, {
Ard Biesheuvelf6134572019-11-08 13:22:33 +01004437 .alg = "curve25519",
4438 .test = alg_test_kpp,
4439 .suite = {
4440 .kpp = __VECS(curve25519_tv_template)
4441 }
4442 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004443 .alg = "deflate",
4444 .test = alg_test_comp,
Milan Broz08189042012-12-06 17:16:28 +08004445 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004446 .suite = {
4447 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004448 .comp = __VECS(deflate_comp_tv_template),
4449 .decomp = __VECS(deflate_decomp_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004450 }
4451 }
4452 }, {
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01004453 .alg = "dh",
4454 .test = alg_test_kpp,
4455 .fips_allowed = 1,
4456 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004457 .kpp = __VECS(dh_tv_template)
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01004458 }
4459 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03004460 .alg = "digest_null",
4461 .test = alg_test_null,
4462 }, {
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004463 .alg = "drbg_nopr_ctr_aes128",
4464 .test = alg_test_drbg,
4465 .fips_allowed = 1,
4466 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004467 .drbg = __VECS(drbg_nopr_ctr_aes128_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004468 }
4469 }, {
4470 .alg = "drbg_nopr_ctr_aes192",
4471 .test = alg_test_drbg,
4472 .fips_allowed = 1,
4473 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004474 .drbg = __VECS(drbg_nopr_ctr_aes192_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004475 }
4476 }, {
4477 .alg = "drbg_nopr_ctr_aes256",
4478 .test = alg_test_drbg,
4479 .fips_allowed = 1,
4480 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004481 .drbg = __VECS(drbg_nopr_ctr_aes256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004482 }
4483 }, {
4484 /*
4485 * There is no need to specifically test the DRBG with every
4486 * backend cipher -- covered by drbg_nopr_hmac_sha256 test
4487 */
4488 .alg = "drbg_nopr_hmac_sha1",
4489 .fips_allowed = 1,
4490 .test = alg_test_null,
4491 }, {
4492 .alg = "drbg_nopr_hmac_sha256",
4493 .test = alg_test_drbg,
4494 .fips_allowed = 1,
4495 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004496 .drbg = __VECS(drbg_nopr_hmac_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004497 }
4498 }, {
4499 /* covered by drbg_nopr_hmac_sha256 test */
4500 .alg = "drbg_nopr_hmac_sha384",
4501 .fips_allowed = 1,
4502 .test = alg_test_null,
4503 }, {
4504 .alg = "drbg_nopr_hmac_sha512",
4505 .test = alg_test_null,
4506 .fips_allowed = 1,
4507 }, {
4508 .alg = "drbg_nopr_sha1",
4509 .fips_allowed = 1,
4510 .test = alg_test_null,
4511 }, {
4512 .alg = "drbg_nopr_sha256",
4513 .test = alg_test_drbg,
4514 .fips_allowed = 1,
4515 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004516 .drbg = __VECS(drbg_nopr_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004517 }
4518 }, {
4519 /* covered by drbg_nopr_sha256 test */
4520 .alg = "drbg_nopr_sha384",
4521 .fips_allowed = 1,
4522 .test = alg_test_null,
4523 }, {
4524 .alg = "drbg_nopr_sha512",
4525 .fips_allowed = 1,
4526 .test = alg_test_null,
4527 }, {
4528 .alg = "drbg_pr_ctr_aes128",
4529 .test = alg_test_drbg,
4530 .fips_allowed = 1,
4531 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004532 .drbg = __VECS(drbg_pr_ctr_aes128_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004533 }
4534 }, {
4535 /* covered by drbg_pr_ctr_aes128 test */
4536 .alg = "drbg_pr_ctr_aes192",
4537 .fips_allowed = 1,
4538 .test = alg_test_null,
4539 }, {
4540 .alg = "drbg_pr_ctr_aes256",
4541 .fips_allowed = 1,
4542 .test = alg_test_null,
4543 }, {
4544 .alg = "drbg_pr_hmac_sha1",
4545 .fips_allowed = 1,
4546 .test = alg_test_null,
4547 }, {
4548 .alg = "drbg_pr_hmac_sha256",
4549 .test = alg_test_drbg,
4550 .fips_allowed = 1,
4551 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004552 .drbg = __VECS(drbg_pr_hmac_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004553 }
4554 }, {
4555 /* covered by drbg_pr_hmac_sha256 test */
4556 .alg = "drbg_pr_hmac_sha384",
4557 .fips_allowed = 1,
4558 .test = alg_test_null,
4559 }, {
4560 .alg = "drbg_pr_hmac_sha512",
4561 .test = alg_test_null,
4562 .fips_allowed = 1,
4563 }, {
4564 .alg = "drbg_pr_sha1",
4565 .fips_allowed = 1,
4566 .test = alg_test_null,
4567 }, {
4568 .alg = "drbg_pr_sha256",
4569 .test = alg_test_drbg,
4570 .fips_allowed = 1,
4571 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004572 .drbg = __VECS(drbg_pr_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02004573 }
4574 }, {
4575 /* covered by drbg_pr_sha256 test */
4576 .alg = "drbg_pr_sha384",
4577 .fips_allowed = 1,
4578 .test = alg_test_null,
4579 }, {
4580 .alg = "drbg_pr_sha512",
4581 .fips_allowed = 1,
4582 .test = alg_test_null,
4583 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004584 .alg = "ecb(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004585 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004586 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004587 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004588 .cipher = __VECS(aes_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004589 }
4590 }, {
4591 .alg = "ecb(anubis)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004592 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004593 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004594 .cipher = __VECS(anubis_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004595 }
4596 }, {
4597 .alg = "ecb(arc4)",
Ard Biesheuvel611a23c2019-06-12 18:19:57 +02004598 .generic_driver = "ecb(arc4)-generic",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004599 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004600 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004601 .cipher = __VECS(arc4_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004602 }
4603 }, {
4604 .alg = "ecb(blowfish)",
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(bf_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004608 }
4609 }, {
4610 .alg = "ecb(camellia)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004611 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004612 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004613 .cipher = __VECS(camellia_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004614 }
4615 }, {
4616 .alg = "ecb(cast5)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004617 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004618 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004619 .cipher = __VECS(cast5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004620 }
4621 }, {
4622 .alg = "ecb(cast6)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004623 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004624 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004625 .cipher = __VECS(cast6_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004626 }
4627 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03004628 .alg = "ecb(cipher_null)",
4629 .test = alg_test_null,
Milan Broz6175ca22017-04-21 13:03:06 +02004630 .fips_allowed = 1,
Jussi Kivilinnae4483702013-04-07 16:43:56 +03004631 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004632 .alg = "ecb(des)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004633 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004634 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004635 .cipher = __VECS(des_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004636 }
4637 }, {
4638 .alg = "ecb(des3_ede)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004639 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004640 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004641 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004642 .cipher = __VECS(des3_ede_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004643 }
4644 }, {
Jussi Kivilinna66e5bd02013-01-19 13:31:36 +02004645 .alg = "ecb(fcrypt)",
4646 .test = alg_test_skcipher,
4647 .suite = {
4648 .cipher = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004649 .vecs = fcrypt_pcbc_tv_template,
4650 .count = 1
Jussi Kivilinna66e5bd02013-01-19 13:31:36 +02004651 }
4652 }
4653 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004654 .alg = "ecb(khazad)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004655 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004656 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004657 .cipher = __VECS(khazad_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004658 }
4659 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01004660 /* Same as ecb(aes) except the key is stored in
4661 * hardware secure memory which we reference by index
4662 */
4663 .alg = "ecb(paes)",
4664 .test = alg_test_null,
4665 .fips_allowed = 1,
4666 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004667 .alg = "ecb(seed)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004668 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004669 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004670 .cipher = __VECS(seed_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004671 }
4672 }, {
4673 .alg = "ecb(serpent)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004674 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004675 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004676 .cipher = __VECS(serpent_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004677 }
4678 }, {
Gilad Ben-Yossefcd83a8a2018-03-06 09:44:43 +00004679 .alg = "ecb(sm4)",
4680 .test = alg_test_skcipher,
4681 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004682 .cipher = __VECS(sm4_tv_template)
Gilad Ben-Yossefcd83a8a2018-03-06 09:44:43 +00004683 }
4684 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004685 .alg = "ecb(tea)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004686 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004687 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004688 .cipher = __VECS(tea_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004689 }
4690 }, {
4691 .alg = "ecb(tnepres)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004692 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004693 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004694 .cipher = __VECS(tnepres_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004695 }
4696 }, {
4697 .alg = "ecb(twofish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004698 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004699 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004700 .cipher = __VECS(tf_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004701 }
4702 }, {
4703 .alg = "ecb(xeta)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004704 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004705 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004706 .cipher = __VECS(xeta_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004707 }
4708 }, {
4709 .alg = "ecb(xtea)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004710 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004711 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004712 .cipher = __VECS(xtea_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004713 }
4714 }, {
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +01004715 .alg = "ecdh",
4716 .test = alg_test_kpp,
4717 .fips_allowed = 1,
4718 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004719 .kpp = __VECS(ecdh_tv_template)
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +01004720 }
4721 }, {
Vitaly Chikunov32fbdbd2019-04-11 18:51:21 +03004722 .alg = "ecrdsa",
4723 .test = alg_test_akcipher,
4724 .suite = {
4725 .akcipher = __VECS(ecrdsa_tv_template)
4726 }
4727 }, {
Ard Biesheuvelf975abb2019-08-19 17:17:34 +03004728 .alg = "essiv(authenc(hmac(sha256),cbc(aes)),sha256)",
4729 .test = alg_test_aead,
4730 .fips_allowed = 1,
4731 .suite = {
4732 .aead = __VECS(essiv_hmac_sha256_aes_cbc_tv_temp)
4733 }
4734 }, {
4735 .alg = "essiv(cbc(aes),sha256)",
4736 .test = alg_test_skcipher,
4737 .fips_allowed = 1,
4738 .suite = {
4739 .cipher = __VECS(essiv_aes_cbc_tv_template)
4740 }
4741 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004742 .alg = "gcm(aes)",
Eric Biggers40153b12019-04-11 21:57:41 -07004743 .generic_driver = "gcm_base(ctr(aes-generic),ghash-generic)",
Herbert Xuda7f0332008-07-31 17:08:25 +08004744 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004745 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004746 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08004747 .aead = __VECS(aes_gcm_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004748 }
4749 }, {
Youquan, Song507069c2009-11-23 20:23:04 +08004750 .alg = "ghash",
4751 .test = alg_test_hash,
Jarod Wilson18c0ebd2011-01-29 15:14:35 +11004752 .fips_allowed = 1,
Youquan, Song507069c2009-11-23 20:23:04 +08004753 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004754 .hash = __VECS(ghash_tv_template)
Youquan, Song507069c2009-11-23 20:23:04 +08004755 }
4756 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004757 .alg = "hmac(md5)",
4758 .test = alg_test_hash,
4759 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004760 .hash = __VECS(hmac_md5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004761 }
4762 }, {
4763 .alg = "hmac(rmd128)",
4764 .test = alg_test_hash,
4765 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004766 .hash = __VECS(hmac_rmd128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004767 }
4768 }, {
4769 .alg = "hmac(rmd160)",
4770 .test = alg_test_hash,
4771 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004772 .hash = __VECS(hmac_rmd160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004773 }
4774 }, {
4775 .alg = "hmac(sha1)",
4776 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004777 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004778 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004779 .hash = __VECS(hmac_sha1_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004780 }
4781 }, {
4782 .alg = "hmac(sha224)",
4783 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004784 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004785 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004786 .hash = __VECS(hmac_sha224_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004787 }
4788 }, {
4789 .alg = "hmac(sha256)",
4790 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004791 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004792 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004793 .hash = __VECS(hmac_sha256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004794 }
4795 }, {
raveendra padasalagi98eca722016-07-01 11:16:54 +05304796 .alg = "hmac(sha3-224)",
4797 .test = alg_test_hash,
4798 .fips_allowed = 1,
4799 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004800 .hash = __VECS(hmac_sha3_224_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05304801 }
4802 }, {
4803 .alg = "hmac(sha3-256)",
4804 .test = alg_test_hash,
4805 .fips_allowed = 1,
4806 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004807 .hash = __VECS(hmac_sha3_256_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05304808 }
4809 }, {
4810 .alg = "hmac(sha3-384)",
4811 .test = alg_test_hash,
4812 .fips_allowed = 1,
4813 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004814 .hash = __VECS(hmac_sha3_384_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05304815 }
4816 }, {
4817 .alg = "hmac(sha3-512)",
4818 .test = alg_test_hash,
4819 .fips_allowed = 1,
4820 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004821 .hash = __VECS(hmac_sha3_512_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05304822 }
4823 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004824 .alg = "hmac(sha384)",
4825 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004826 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004827 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004828 .hash = __VECS(hmac_sha384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004829 }
4830 }, {
4831 .alg = "hmac(sha512)",
4832 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10004833 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004834 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004835 .hash = __VECS(hmac_sha512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004836 }
4837 }, {
Pascal van Leeuwen8194fd12019-09-13 17:20:38 +02004838 .alg = "hmac(sm3)",
4839 .test = alg_test_hash,
4840 .suite = {
4841 .hash = __VECS(hmac_sm3_tv_template)
4842 }
4843 }, {
Vitaly Chikunov25a0b9d2018-11-07 00:00:03 +03004844 .alg = "hmac(streebog256)",
4845 .test = alg_test_hash,
4846 .suite = {
4847 .hash = __VECS(hmac_streebog256_tv_template)
4848 }
4849 }, {
4850 .alg = "hmac(streebog512)",
4851 .test = alg_test_hash,
4852 .suite = {
4853 .hash = __VECS(hmac_streebog512_tv_template)
4854 }
4855 }, {
Stephan Muellerbb5530e2015-05-25 15:10:20 +02004856 .alg = "jitterentropy_rng",
4857 .fips_allowed = 1,
4858 .test = alg_test_null,
4859 }, {
Stephan Mueller35351982015-09-21 20:59:56 +02004860 .alg = "kw(aes)",
4861 .test = alg_test_skcipher,
4862 .fips_allowed = 1,
4863 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004864 .cipher = __VECS(aes_kw_tv_template)
Stephan Mueller35351982015-09-21 20:59:56 +02004865 }
4866 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004867 .alg = "lrw(aes)",
Eric Biggersd435e102019-04-11 21:57:40 -07004868 .generic_driver = "lrw(ecb(aes-generic))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004869 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004870 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004871 .cipher = __VECS(aes_lrw_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004872 }
4873 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02004874 .alg = "lrw(camellia)",
Eric Biggersd435e102019-04-11 21:57:40 -07004875 .generic_driver = "lrw(ecb(camellia-generic))",
Jussi Kivilinna08406052012-03-05 20:26:21 +02004876 .test = alg_test_skcipher,
4877 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004878 .cipher = __VECS(camellia_lrw_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02004879 }
4880 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004881 .alg = "lrw(cast6)",
Eric Biggersd435e102019-04-11 21:57:40 -07004882 .generic_driver = "lrw(ecb(cast6-generic))",
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004883 .test = alg_test_skcipher,
4884 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004885 .cipher = __VECS(cast6_lrw_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004886 }
4887 }, {
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03004888 .alg = "lrw(serpent)",
Eric Biggersd435e102019-04-11 21:57:40 -07004889 .generic_driver = "lrw(ecb(serpent-generic))",
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03004890 .test = alg_test_skcipher,
4891 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004892 .cipher = __VECS(serpent_lrw_tv_template)
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03004893 }
4894 }, {
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03004895 .alg = "lrw(twofish)",
Eric Biggersd435e102019-04-11 21:57:40 -07004896 .generic_driver = "lrw(ecb(twofish-generic))",
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03004897 .test = alg_test_skcipher,
4898 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004899 .cipher = __VECS(tf_lrw_tv_template)
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03004900 }
4901 }, {
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02004902 .alg = "lz4",
4903 .test = alg_test_comp,
4904 .fips_allowed = 1,
4905 .suite = {
4906 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004907 .comp = __VECS(lz4_comp_tv_template),
4908 .decomp = __VECS(lz4_decomp_tv_template)
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02004909 }
4910 }
4911 }, {
4912 .alg = "lz4hc",
4913 .test = alg_test_comp,
4914 .fips_allowed = 1,
4915 .suite = {
4916 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004917 .comp = __VECS(lz4hc_comp_tv_template),
4918 .decomp = __VECS(lz4hc_decomp_tv_template)
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02004919 }
4920 }
4921 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004922 .alg = "lzo",
4923 .test = alg_test_comp,
Milan Broz08189042012-12-06 17:16:28 +08004924 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004925 .suite = {
4926 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004927 .comp = __VECS(lzo_comp_tv_template),
4928 .decomp = __VECS(lzo_decomp_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004929 }
4930 }
4931 }, {
Hannah Panf248caf2019-07-02 15:16:02 -07004932 .alg = "lzo-rle",
4933 .test = alg_test_comp,
4934 .fips_allowed = 1,
4935 .suite = {
4936 .comp = {
4937 .comp = __VECS(lzorle_comp_tv_template),
4938 .decomp = __VECS(lzorle_decomp_tv_template)
4939 }
4940 }
4941 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004942 .alg = "md4",
4943 .test = alg_test_hash,
4944 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004945 .hash = __VECS(md4_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004946 }
4947 }, {
4948 .alg = "md5",
4949 .test = alg_test_hash,
4950 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004951 .hash = __VECS(md5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004952 }
4953 }, {
4954 .alg = "michael_mic",
4955 .test = alg_test_hash,
4956 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004957 .hash = __VECS(michael_mic_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004958 }
4959 }, {
Eric Biggers26609a22018-11-16 17:26:29 -08004960 .alg = "nhpoly1305",
4961 .test = alg_test_hash,
4962 .suite = {
4963 .hash = __VECS(nhpoly1305_tv_template)
4964 }
4965 }, {
Puneet Saxenaba0e14a2011-05-04 15:04:10 +10004966 .alg = "ofb(aes)",
4967 .test = alg_test_skcipher,
4968 .fips_allowed = 1,
4969 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004970 .cipher = __VECS(aes_ofb_tv_template)
Puneet Saxenaba0e14a2011-05-04 15:04:10 +10004971 }
4972 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01004973 /* Same as ofb(aes) except the key is stored in
4974 * hardware secure memory which we reference by index
4975 */
4976 .alg = "ofb(paes)",
4977 .test = alg_test_null,
4978 .fips_allowed = 1,
4979 }, {
Pascal van Leeuwena06b15b2019-09-13 11:10:39 +02004980 .alg = "ofb(sm4)",
4981 .test = alg_test_skcipher,
4982 .suite = {
4983 .cipher = __VECS(sm4_ofb_tv_template)
4984 }
4985 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004986 .alg = "pcbc(fcrypt)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004987 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08004988 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004989 .cipher = __VECS(fcrypt_pcbc_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004990 }
4991 }, {
Stephan Mueller12071072017-06-12 23:27:51 +02004992 .alg = "pkcs1pad(rsa,sha224)",
4993 .test = alg_test_null,
4994 .fips_allowed = 1,
4995 }, {
4996 .alg = "pkcs1pad(rsa,sha256)",
4997 .test = alg_test_akcipher,
4998 .fips_allowed = 1,
4999 .suite = {
5000 .akcipher = __VECS(pkcs1pad_rsa_tv_template)
5001 }
5002 }, {
5003 .alg = "pkcs1pad(rsa,sha384)",
5004 .test = alg_test_null,
5005 .fips_allowed = 1,
5006 }, {
5007 .alg = "pkcs1pad(rsa,sha512)",
5008 .test = alg_test_null,
5009 .fips_allowed = 1,
5010 }, {
Martin Willieee9dc62015-06-01 13:43:59 +02005011 .alg = "poly1305",
5012 .test = alg_test_hash,
5013 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005014 .hash = __VECS(poly1305_tv_template)
Martin Willieee9dc62015-06-01 13:43:59 +02005015 }
5016 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005017 .alg = "rfc3686(ctr(aes))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005018 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10005019 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08005020 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005021 .cipher = __VECS(aes_ctr_rfc3686_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005022 }
5023 }, {
Pascal van Leeuwene4886212019-09-13 11:10:42 +02005024 .alg = "rfc3686(ctr(sm4))",
5025 .test = alg_test_skcipher,
5026 .suite = {
5027 .cipher = __VECS(sm4_ctr_rfc3686_tv_template)
5028 }
5029 }, {
Herbert Xu3f31a742015-07-09 07:17:34 +08005030 .alg = "rfc4106(gcm(aes))",
Eric Biggers40153b12019-04-11 21:57:41 -07005031 .generic_driver = "rfc4106(gcm_base(ctr(aes-generic),ghash-generic))",
Adrian Hoban69435b92010-11-04 15:02:04 -04005032 .test = alg_test_aead,
Jarod Wilsondb71f29a2015-01-23 12:42:15 -05005033 .fips_allowed = 1,
Adrian Hoban69435b92010-11-04 15:02:04 -04005034 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08005035 .aead = __VECS(aes_gcm_rfc4106_tv_template)
Adrian Hoban69435b92010-11-04 15:02:04 -04005036 }
5037 }, {
Herbert Xu544c4362015-07-14 16:53:22 +08005038 .alg = "rfc4309(ccm(aes))",
Eric Biggers40153b12019-04-11 21:57:41 -07005039 .generic_driver = "rfc4309(ccm_base(ctr(aes-generic),cbcmac(aes-generic)))",
Jarod Wilson5d667322009-05-04 19:23:40 +08005040 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10005041 .fips_allowed = 1,
Jarod Wilson5d667322009-05-04 19:23:40 +08005042 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08005043 .aead = __VECS(aes_ccm_rfc4309_tv_template)
Jarod Wilson5d667322009-05-04 19:23:40 +08005044 }
5045 }, {
Herbert Xubb687452015-06-16 13:54:24 +08005046 .alg = "rfc4543(gcm(aes))",
Eric Biggers40153b12019-04-11 21:57:41 -07005047 .generic_driver = "rfc4543(gcm_base(ctr(aes-generic),ghash-generic))",
Jussi Kivilinnae9b74412013-04-07 16:43:51 +03005048 .test = alg_test_aead,
5049 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08005050 .aead = __VECS(aes_gcm_rfc4543_tv_template)
Jussi Kivilinnae9b74412013-04-07 16:43:51 +03005051 }
5052 }, {
Martin Williaf2b76b2015-06-01 13:44:01 +02005053 .alg = "rfc7539(chacha20,poly1305)",
5054 .test = alg_test_aead,
5055 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08005056 .aead = __VECS(rfc7539_tv_template)
Martin Williaf2b76b2015-06-01 13:44:01 +02005057 }
5058 }, {
Martin Willi59007582015-06-01 13:44:03 +02005059 .alg = "rfc7539esp(chacha20,poly1305)",
5060 .test = alg_test_aead,
5061 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08005062 .aead = __VECS(rfc7539esp_tv_template)
Martin Willi59007582015-06-01 13:44:03 +02005063 }
5064 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005065 .alg = "rmd128",
5066 .test = alg_test_hash,
5067 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005068 .hash = __VECS(rmd128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005069 }
5070 }, {
5071 .alg = "rmd160",
5072 .test = alg_test_hash,
5073 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005074 .hash = __VECS(rmd160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005075 }
5076 }, {
5077 .alg = "rmd256",
5078 .test = alg_test_hash,
5079 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005080 .hash = __VECS(rmd256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005081 }
5082 }, {
5083 .alg = "rmd320",
5084 .test = alg_test_hash,
5085 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005086 .hash = __VECS(rmd320_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005087 }
5088 }, {
Tadeusz Struk946cc462015-06-16 10:31:06 -07005089 .alg = "rsa",
5090 .test = alg_test_akcipher,
5091 .fips_allowed = 1,
5092 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005093 .akcipher = __VECS(rsa_tv_template)
Tadeusz Struk946cc462015-06-16 10:31:06 -07005094 }
5095 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005096 .alg = "salsa20",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005097 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08005098 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005099 .cipher = __VECS(salsa20_stream_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005100 }
5101 }, {
5102 .alg = "sha1",
5103 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10005104 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08005105 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005106 .hash = __VECS(sha1_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005107 }
5108 }, {
5109 .alg = "sha224",
5110 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10005111 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08005112 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005113 .hash = __VECS(sha224_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005114 }
5115 }, {
5116 .alg = "sha256",
5117 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10005118 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08005119 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005120 .hash = __VECS(sha256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005121 }
5122 }, {
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05305123 .alg = "sha3-224",
5124 .test = alg_test_hash,
5125 .fips_allowed = 1,
5126 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005127 .hash = __VECS(sha3_224_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05305128 }
5129 }, {
5130 .alg = "sha3-256",
5131 .test = alg_test_hash,
5132 .fips_allowed = 1,
5133 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005134 .hash = __VECS(sha3_256_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05305135 }
5136 }, {
5137 .alg = "sha3-384",
5138 .test = alg_test_hash,
5139 .fips_allowed = 1,
5140 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005141 .hash = __VECS(sha3_384_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05305142 }
5143 }, {
5144 .alg = "sha3-512",
5145 .test = alg_test_hash,
5146 .fips_allowed = 1,
5147 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005148 .hash = __VECS(sha3_512_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05305149 }
5150 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005151 .alg = "sha384",
5152 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10005153 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08005154 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005155 .hash = __VECS(sha384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005156 }
5157 }, {
5158 .alg = "sha512",
5159 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10005160 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08005161 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005162 .hash = __VECS(sha512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005163 }
5164 }, {
Gilad Ben-Yossefb7e27532017-08-21 13:51:29 +03005165 .alg = "sm3",
5166 .test = alg_test_hash,
5167 .suite = {
5168 .hash = __VECS(sm3_tv_template)
5169 }
5170 }, {
Vitaly Chikunov25a0b9d2018-11-07 00:00:03 +03005171 .alg = "streebog256",
5172 .test = alg_test_hash,
5173 .suite = {
5174 .hash = __VECS(streebog256_tv_template)
5175 }
5176 }, {
5177 .alg = "streebog512",
5178 .test = alg_test_hash,
5179 .suite = {
5180 .hash = __VECS(streebog512_tv_template)
5181 }
5182 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005183 .alg = "tgr128",
5184 .test = alg_test_hash,
5185 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005186 .hash = __VECS(tgr128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005187 }
5188 }, {
5189 .alg = "tgr160",
5190 .test = alg_test_hash,
5191 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005192 .hash = __VECS(tgr160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005193 }
5194 }, {
5195 .alg = "tgr192",
5196 .test = alg_test_hash,
5197 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005198 .hash = __VECS(tgr192_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005199 }
5200 }, {
Eric Biggersed331ad2018-06-18 10:22:39 -07005201 .alg = "vmac64(aes)",
5202 .test = alg_test_hash,
5203 .suite = {
5204 .hash = __VECS(vmac64_aes_tv_template)
5205 }
5206 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005207 .alg = "wp256",
5208 .test = alg_test_hash,
5209 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005210 .hash = __VECS(wp256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005211 }
5212 }, {
5213 .alg = "wp384",
5214 .test = alg_test_hash,
5215 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005216 .hash = __VECS(wp384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005217 }
5218 }, {
5219 .alg = "wp512",
5220 .test = alg_test_hash,
5221 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005222 .hash = __VECS(wp512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005223 }
5224 }, {
5225 .alg = "xcbc(aes)",
5226 .test = alg_test_hash,
5227 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00005228 .hash = __VECS(aes_xcbc128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005229 }
5230 }, {
Eric Biggersaa762402018-11-16 17:26:22 -08005231 .alg = "xchacha12",
5232 .test = alg_test_skcipher,
5233 .suite = {
5234 .cipher = __VECS(xchacha12_tv_template)
5235 },
5236 }, {
Eric Biggersde61d7a2018-11-16 17:26:20 -08005237 .alg = "xchacha20",
5238 .test = alg_test_skcipher,
5239 .suite = {
5240 .cipher = __VECS(xchacha20_tv_template)
5241 },
5242 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08005243 .alg = "xts(aes)",
Eric Biggersd435e102019-04-11 21:57:40 -07005244 .generic_driver = "xts(ecb(aes-generic))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005245 .test = alg_test_skcipher,
Jarod Wilson2918aa82011-01-29 15:14:01 +11005246 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08005247 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005248 .cipher = __VECS(aes_xts_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08005249 }
Geert Uytterhoeven0c01aed2009-03-04 15:42:15 +08005250 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02005251 .alg = "xts(camellia)",
Eric Biggersd435e102019-04-11 21:57:40 -07005252 .generic_driver = "xts(ecb(camellia-generic))",
Jussi Kivilinna08406052012-03-05 20:26:21 +02005253 .test = alg_test_skcipher,
5254 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005255 .cipher = __VECS(camellia_xts_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02005256 }
5257 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02005258 .alg = "xts(cast6)",
Eric Biggersd435e102019-04-11 21:57:40 -07005259 .generic_driver = "xts(ecb(cast6-generic))",
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02005260 .test = alg_test_skcipher,
5261 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005262 .cipher = __VECS(cast6_xts_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02005263 }
5264 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01005265 /* Same as xts(aes) except the key is stored in
5266 * hardware secure memory which we reference by index
5267 */
5268 .alg = "xts(paes)",
5269 .test = alg_test_null,
5270 .fips_allowed = 1,
5271 }, {
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03005272 .alg = "xts(serpent)",
Eric Biggersd435e102019-04-11 21:57:40 -07005273 .generic_driver = "xts(ecb(serpent-generic))",
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03005274 .test = alg_test_skcipher,
5275 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005276 .cipher = __VECS(serpent_xts_tv_template)
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03005277 }
5278 }, {
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03005279 .alg = "xts(twofish)",
Eric Biggersd435e102019-04-11 21:57:40 -07005280 .generic_driver = "xts(ecb(twofish-generic))",
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03005281 .test = alg_test_skcipher,
5282 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07005283 .cipher = __VECS(tf_xts_tv_template)
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03005284 }
Giovanni Cabiddua368f432017-04-21 21:54:30 +01005285 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01005286 .alg = "xts4096(paes)",
5287 .test = alg_test_null,
5288 .fips_allowed = 1,
5289 }, {
5290 .alg = "xts512(paes)",
5291 .test = alg_test_null,
5292 .fips_allowed = 1,
5293 }, {
Nikolay Borisov67882e72019-05-30 09:52:57 +03005294 .alg = "xxhash64",
5295 .test = alg_test_hash,
5296 .fips_allowed = 1,
5297 .suite = {
5298 .hash = __VECS(xxhash64_tv_template)
5299 }
5300 }, {
Giovanni Cabiddua368f432017-04-21 21:54:30 +01005301 .alg = "zlib-deflate",
5302 .test = alg_test_comp,
5303 .fips_allowed = 1,
5304 .suite = {
5305 .comp = {
5306 .comp = __VECS(zlib_deflate_comp_tv_template),
5307 .decomp = __VECS(zlib_deflate_decomp_tv_template)
5308 }
5309 }
Nick Terrelld28fc3d2018-03-30 12:14:53 -07005310 }, {
5311 .alg = "zstd",
5312 .test = alg_test_comp,
5313 .fips_allowed = 1,
5314 .suite = {
5315 .comp = {
5316 .comp = __VECS(zstd_comp_tv_template),
5317 .decomp = __VECS(zstd_decomp_tv_template)
5318 }
5319 }
Herbert Xuda7f0332008-07-31 17:08:25 +08005320 }
5321};
5322
Eric Biggers3f47a032019-01-31 23:51:43 -08005323static void alg_check_test_descs_order(void)
Jussi Kivilinna57147582013-06-13 17:37:40 +03005324{
5325 int i;
5326
Jussi Kivilinna57147582013-06-13 17:37:40 +03005327 for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
5328 int diff = strcmp(alg_test_descs[i - 1].alg,
5329 alg_test_descs[i].alg);
5330
5331 if (WARN_ON(diff > 0)) {
5332 pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
5333 alg_test_descs[i - 1].alg,
5334 alg_test_descs[i].alg);
5335 }
5336
5337 if (WARN_ON(diff == 0)) {
5338 pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
5339 alg_test_descs[i].alg);
5340 }
5341 }
5342}
5343
Eric Biggers3f47a032019-01-31 23:51:43 -08005344static void alg_check_testvec_configs(void)
5345{
Eric Biggers4e7babba2019-01-31 23:51:46 -08005346 int i;
5347
5348 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++)
5349 WARN_ON(!valid_testvec_config(
5350 &default_cipher_testvec_configs[i]));
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08005351
5352 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++)
5353 WARN_ON(!valid_testvec_config(
5354 &default_hash_testvec_configs[i]));
Eric Biggers3f47a032019-01-31 23:51:43 -08005355}
5356
5357static void testmgr_onetime_init(void)
5358{
5359 alg_check_test_descs_order();
5360 alg_check_testvec_configs();
Eric Biggers5b2706a2019-01-31 23:51:44 -08005361
5362#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
5363 pr_warn("alg: extra crypto tests enabled. This is intended for developer use only.\n");
5364#endif
Eric Biggers3f47a032019-01-31 23:51:43 -08005365}
5366
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005367static int alg_find_test(const char *alg)
Herbert Xuda7f0332008-07-31 17:08:25 +08005368{
5369 int start = 0;
5370 int end = ARRAY_SIZE(alg_test_descs);
5371
5372 while (start < end) {
5373 int i = (start + end) / 2;
5374 int diff = strcmp(alg_test_descs[i].alg, alg);
5375
5376 if (diff > 0) {
5377 end = i;
5378 continue;
5379 }
5380
5381 if (diff < 0) {
5382 start = i + 1;
5383 continue;
5384 }
5385
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005386 return i;
Herbert Xuda7f0332008-07-31 17:08:25 +08005387 }
5388
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005389 return -1;
5390}
5391
5392int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
5393{
5394 int i;
Herbert Xua68f6612009-07-02 16:32:12 +08005395 int j;
Neil Hormand12d6b62008-10-12 20:36:51 +08005396 int rc;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005397
Richard W.M. Jones9e5c9fe2016-05-03 10:00:17 +01005398 if (!fips_enabled && notests) {
5399 printk_once(KERN_INFO "alg: self-tests disabled\n");
5400 return 0;
5401 }
5402
Eric Biggers3f47a032019-01-31 23:51:43 -08005403 DO_ONCE(testmgr_onetime_init);
Jussi Kivilinna57147582013-06-13 17:37:40 +03005404
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005405 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
5406 char nalg[CRYPTO_MAX_ALG_NAME];
5407
5408 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
5409 sizeof(nalg))
5410 return -ENAMETOOLONG;
5411
5412 i = alg_find_test(nalg);
5413 if (i < 0)
5414 goto notest;
5415
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10005416 if (fips_enabled && !alg_test_descs[i].fips_allowed)
5417 goto non_fips_alg;
5418
Jarod Wilson941fb322009-05-04 19:49:23 +08005419 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
5420 goto test_done;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005421 }
5422
5423 i = alg_find_test(alg);
Herbert Xua68f6612009-07-02 16:32:12 +08005424 j = alg_find_test(driver);
5425 if (i < 0 && j < 0)
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005426 goto notest;
5427
Herbert Xua68f6612009-07-02 16:32:12 +08005428 if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
5429 (j >= 0 && !alg_test_descs[j].fips_allowed)))
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10005430 goto non_fips_alg;
5431
Herbert Xua68f6612009-07-02 16:32:12 +08005432 rc = 0;
5433 if (i >= 0)
5434 rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
5435 type, mask);
Cristian Stoica032c8ca2013-07-18 18:57:07 +03005436 if (j >= 0 && j != i)
Herbert Xua68f6612009-07-02 16:32:12 +08005437 rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
5438 type, mask);
5439
Jarod Wilson941fb322009-05-04 19:49:23 +08005440test_done:
Gilad Ben-Yossef95523892019-07-02 14:39:20 +03005441 if (rc && (fips_enabled || panic_on_fail)) {
5442 fips_fail_notify();
Eric Biggerseda69b02019-03-31 13:09:14 -07005443 panic("alg: self-tests for %s (%s) failed in %s mode!\n",
5444 driver, alg, fips_enabled ? "fips" : "panic_on_fail");
Gilad Ben-Yossef95523892019-07-02 14:39:20 +03005445 }
Neil Hormand12d6b62008-10-12 20:36:51 +08005446
Jarod Wilson29ecd4a2009-05-04 19:51:17 +08005447 if (fips_enabled && !rc)
Masanari Iida3e8cffd2014-10-07 00:37:54 +09005448 pr_info("alg: self-tests for %s (%s) passed\n", driver, alg);
Jarod Wilson29ecd4a2009-05-04 19:51:17 +08005449
Neil Hormand12d6b62008-10-12 20:36:51 +08005450 return rc;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10005451
5452notest:
Herbert Xuda7f0332008-07-31 17:08:25 +08005453 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
5454 return 0;
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10005455non_fips_alg:
5456 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08005457}
Alexander Shishkin0b767f92010-06-03 20:53:43 +10005458
Herbert Xu326a6342010-08-06 09:40:28 +08005459#endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
Alexander Shishkin0b767f92010-06-03 20:53:43 +10005460
Herbert Xuda7f0332008-07-31 17:08:25 +08005461EXPORT_SYMBOL_GPL(alg_test);