blob: 52417dde811f19a8a2ee175220c27278d283e618 [file] [log] [blame]
Herbert Xuda7f0332008-07-31 17:08:25 +08001/*
2 * Algorithm testing framework and tests.
3 *
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
6 * Copyright (c) 2007 Nokia Siemens Networks
7 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
Eric Biggers3f47a032019-01-31 23:51:43 -08008 * Copyright (c) 2019 Google LLC
Herbert Xuda7f0332008-07-31 17:08:25 +08009 *
Adrian Hoban69435b92010-11-04 15:02:04 -040010 * Updated RFC4106 AES-GCM testing.
11 * Authors: Aidan O'Mahony (aidan.o.mahony@intel.com)
12 * Adrian Hoban <adrian.hoban@intel.com>
13 * Gabriele Paoloni <gabriele.paoloni@intel.com>
14 * Tadeusz Struk (tadeusz.struk@intel.com)
15 * Copyright (c) 2010, Intel Corporation.
16 *
Herbert Xuda7f0332008-07-31 17:08:25 +080017 * This program is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the Free
19 * Software Foundation; either version 2 of the License, or (at your option)
20 * any later version.
21 *
22 */
23
Herbert Xu1ce33112015-04-22 15:06:31 +080024#include <crypto/aead.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080025#include <crypto/hash.h>
Herbert Xu12773d92015-08-20 15:21:46 +080026#include <crypto/skcipher.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080027#include <linux/err.h>
Herbert Xu1c41b882015-04-22 13:25:58 +080028#include <linux/fips.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080029#include <linux/module.h>
Eric Biggers3f47a032019-01-31 23:51:43 -080030#include <linux/once.h>
Eric Biggers25f9ddd2019-01-31 23:51:45 -080031#include <linux/random.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080032#include <linux/scatterlist.h>
33#include <linux/slab.h>
34#include <linux/string.h>
Jarod Wilson7647d6c2009-05-04 19:44:50 +080035#include <crypto/rng.h>
Stephan Mueller64d1cdf2014-05-31 17:25:36 +020036#include <crypto/drbg.h>
Tadeusz Struk946cc462015-06-16 10:31:06 -070037#include <crypto/akcipher.h>
Salvatore Benedetto802c7f12016-06-22 17:49:14 +010038#include <crypto/kpp.h>
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +010039#include <crypto/acompress.h>
Eric Biggersb55e1a32019-03-12 22:12:47 -070040#include <crypto/internal/simd.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080041
42#include "internal.h"
Alexander Shishkin0b767f92010-06-03 20:53:43 +100043
Richard W.M. Jones9e5c9fe2016-05-03 10:00:17 +010044static bool notests;
45module_param(notests, bool, 0644);
46MODULE_PARM_DESC(notests, "disable crypto self-tests");
47
Eric Biggers5b2706a2019-01-31 23:51:44 -080048#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
49static bool noextratests;
50module_param(noextratests, bool, 0644);
51MODULE_PARM_DESC(noextratests, "disable expensive crypto self-tests");
52
53static unsigned int fuzz_iterations = 100;
54module_param(fuzz_iterations, uint, 0644);
55MODULE_PARM_DESC(fuzz_iterations, "number of fuzz test iterations");
Eric Biggersb55e1a32019-03-12 22:12:47 -070056
57DEFINE_PER_CPU(bool, crypto_simd_disabled_for_test);
58EXPORT_PER_CPU_SYMBOL_GPL(crypto_simd_disabled_for_test);
Eric Biggers5b2706a2019-01-31 23:51:44 -080059#endif
60
Herbert Xu326a6342010-08-06 09:40:28 +080061#ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
Alexander Shishkin0b767f92010-06-03 20:53:43 +100062
63/* a perfect nop */
64int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
65{
66 return 0;
67}
68
69#else
70
Herbert Xuda7f0332008-07-31 17:08:25 +080071#include "testmgr.h"
72
73/*
74 * Need slab memory for testing (size in number of pages).
75 */
76#define XBUFSIZE 8
77
78/*
Herbert Xuda7f0332008-07-31 17:08:25 +080079* Used by test_cipher()
80*/
81#define ENCRYPT 1
82#define DECRYPT 0
83
Herbert Xuda7f0332008-07-31 17:08:25 +080084struct aead_test_suite {
Eric Biggersa0d608ee2019-01-13 15:32:28 -080085 const struct aead_testvec *vecs;
86 unsigned int count;
Herbert Xuda7f0332008-07-31 17:08:25 +080087};
88
89struct cipher_test_suite {
Eric Biggers92a4c9f2018-05-20 22:50:29 -070090 const struct cipher_testvec *vecs;
91 unsigned int count;
Herbert Xuda7f0332008-07-31 17:08:25 +080092};
93
94struct comp_test_suite {
95 struct {
Eric Biggersb13b1e02017-02-24 15:46:59 -080096 const struct comp_testvec *vecs;
Herbert Xuda7f0332008-07-31 17:08:25 +080097 unsigned int count;
98 } comp, decomp;
99};
100
101struct hash_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800102 const struct hash_testvec *vecs;
Herbert Xuda7f0332008-07-31 17:08:25 +0800103 unsigned int count;
104};
105
Jarod Wilson7647d6c2009-05-04 19:44:50 +0800106struct cprng_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800107 const struct cprng_testvec *vecs;
Jarod Wilson7647d6c2009-05-04 19:44:50 +0800108 unsigned int count;
109};
110
Stephan Mueller64d1cdf2014-05-31 17:25:36 +0200111struct drbg_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800112 const struct drbg_testvec *vecs;
Stephan Mueller64d1cdf2014-05-31 17:25:36 +0200113 unsigned int count;
114};
115
Tadeusz Struk946cc462015-06-16 10:31:06 -0700116struct akcipher_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800117 const struct akcipher_testvec *vecs;
Tadeusz Struk946cc462015-06-16 10:31:06 -0700118 unsigned int count;
119};
120
Salvatore Benedetto802c7f12016-06-22 17:49:14 +0100121struct kpp_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800122 const struct kpp_testvec *vecs;
Salvatore Benedetto802c7f12016-06-22 17:49:14 +0100123 unsigned int count;
124};
125
Herbert Xuda7f0332008-07-31 17:08:25 +0800126struct alg_test_desc {
127 const char *alg;
128 int (*test)(const struct alg_test_desc *desc, const char *driver,
129 u32 type, u32 mask);
Jarod Wilsona1915d52009-05-15 15:16:03 +1000130 int fips_allowed; /* set if alg is allowed in fips mode */
Herbert Xuda7f0332008-07-31 17:08:25 +0800131
132 union {
133 struct aead_test_suite aead;
134 struct cipher_test_suite cipher;
135 struct comp_test_suite comp;
136 struct hash_test_suite hash;
Jarod Wilson7647d6c2009-05-04 19:44:50 +0800137 struct cprng_test_suite cprng;
Stephan Mueller64d1cdf2014-05-31 17:25:36 +0200138 struct drbg_test_suite drbg;
Tadeusz Struk946cc462015-06-16 10:31:06 -0700139 struct akcipher_test_suite akcipher;
Salvatore Benedetto802c7f12016-06-22 17:49:14 +0100140 struct kpp_test_suite kpp;
Herbert Xuda7f0332008-07-31 17:08:25 +0800141 } suite;
142};
143
Herbert Xuda7f0332008-07-31 17:08:25 +0800144static void hexdump(unsigned char *buf, unsigned int len)
145{
146 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
147 16, 1,
148 buf, len, false);
149}
150
Eric Biggers3f47a032019-01-31 23:51:43 -0800151static int __testmgr_alloc_buf(char *buf[XBUFSIZE], int order)
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800152{
153 int i;
154
155 for (i = 0; i < XBUFSIZE; i++) {
Eric Biggers3f47a032019-01-31 23:51:43 -0800156 buf[i] = (char *)__get_free_pages(GFP_KERNEL, order);
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800157 if (!buf[i])
158 goto err_free_buf;
159 }
160
161 return 0;
162
163err_free_buf:
164 while (i-- > 0)
Eric Biggers3f47a032019-01-31 23:51:43 -0800165 free_pages((unsigned long)buf[i], order);
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800166
167 return -ENOMEM;
168}
169
Eric Biggers3f47a032019-01-31 23:51:43 -0800170static int testmgr_alloc_buf(char *buf[XBUFSIZE])
171{
172 return __testmgr_alloc_buf(buf, 0);
173}
174
175static void __testmgr_free_buf(char *buf[XBUFSIZE], int order)
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800176{
177 int i;
178
179 for (i = 0; i < XBUFSIZE; i++)
Eric Biggers3f47a032019-01-31 23:51:43 -0800180 free_pages((unsigned long)buf[i], order);
181}
182
183static void testmgr_free_buf(char *buf[XBUFSIZE])
184{
185 __testmgr_free_buf(buf, 0);
186}
187
188#define TESTMGR_POISON_BYTE 0xfe
189#define TESTMGR_POISON_LEN 16
190
191static inline void testmgr_poison(void *addr, size_t len)
192{
193 memset(addr, TESTMGR_POISON_BYTE, len);
194}
195
196/* Is the memory region still fully poisoned? */
197static inline bool testmgr_is_poison(const void *addr, size_t len)
198{
199 return memchr_inv(addr, TESTMGR_POISON_BYTE, len) == NULL;
200}
201
202/* flush type for hash algorithms */
203enum flush_type {
204 /* merge with update of previous buffer(s) */
205 FLUSH_TYPE_NONE = 0,
206
207 /* update with previous buffer(s) before doing this one */
208 FLUSH_TYPE_FLUSH,
209
210 /* likewise, but also export and re-import the intermediate state */
211 FLUSH_TYPE_REIMPORT,
212};
213
214/* finalization function for hash algorithms */
215enum finalization_type {
216 FINALIZATION_TYPE_FINAL, /* use final() */
217 FINALIZATION_TYPE_FINUP, /* use finup() */
218 FINALIZATION_TYPE_DIGEST, /* use digest() */
219};
220
221#define TEST_SG_TOTAL 10000
222
223/**
224 * struct test_sg_division - description of a scatterlist entry
225 *
226 * This struct describes one entry of a scatterlist being constructed to check a
227 * crypto test vector.
228 *
229 * @proportion_of_total: length of this chunk relative to the total length,
230 * given as a proportion out of TEST_SG_TOTAL so that it
231 * scales to fit any test vector
232 * @offset: byte offset into a 2-page buffer at which this chunk will start
233 * @offset_relative_to_alignmask: if true, add the algorithm's alignmask to the
234 * @offset
235 * @flush_type: for hashes, whether an update() should be done now vs.
236 * continuing to accumulate data
237 */
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;
243};
244
245/**
246 * struct testvec_config - configuration for testing a crypto test vector
247 *
248 * This struct describes the data layout and other parameters with which each
249 * crypto test vector can be tested.
250 *
251 * @name: name of this config, logged for debugging purposes if a test fails
252 * @inplace: operate on the data in-place, if applicable for the algorithm type?
253 * @req_flags: extra request_flags, e.g. CRYPTO_TFM_REQ_MAY_SLEEP
254 * @src_divs: description of how to arrange the source scatterlist
255 * @dst_divs: description of how to arrange the dst scatterlist, if applicable
256 * for the algorithm type. Defaults to @src_divs if unset.
257 * @iv_offset: misalignment of the IV in the range [0..MAX_ALGAPI_ALIGNMASK+1],
258 * where 0 is aligned to a 2*(MAX_ALGAPI_ALIGNMASK+1) byte boundary
259 * @iv_offset_relative_to_alignmask: if true, add the algorithm's alignmask to
260 * the @iv_offset
261 * @finalization_type: what finalization function to use for hashes
262 */
263struct testvec_config {
264 const char *name;
265 bool inplace;
266 u32 req_flags;
267 struct test_sg_division src_divs[XBUFSIZE];
268 struct test_sg_division dst_divs[XBUFSIZE];
269 unsigned int iv_offset;
270 bool iv_offset_relative_to_alignmask;
271 enum finalization_type finalization_type;
272};
273
274#define TESTVEC_CONFIG_NAMELEN 192
275
Eric Biggers4e7babba2019-01-31 23:51:46 -0800276/*
277 * The following are the lists of testvec_configs to test for each algorithm
278 * type when the basic crypto self-tests are enabled, i.e. when
279 * CONFIG_CRYPTO_MANAGER_DISABLE_TESTS is unset. They aim to provide good test
280 * coverage, while keeping the test time much shorter than the full fuzz tests
281 * so that the basic tests can be enabled in a wider range of circumstances.
282 */
283
284/* Configs for skciphers and aeads */
285static const struct testvec_config default_cipher_testvec_configs[] = {
286 {
287 .name = "in-place",
288 .inplace = true,
289 .src_divs = { { .proportion_of_total = 10000 } },
290 }, {
291 .name = "out-of-place",
292 .src_divs = { { .proportion_of_total = 10000 } },
293 }, {
294 .name = "unaligned buffer, offset=1",
295 .src_divs = { { .proportion_of_total = 10000, .offset = 1 } },
296 .iv_offset = 1,
297 }, {
298 .name = "buffer aligned only to alignmask",
299 .src_divs = {
300 {
301 .proportion_of_total = 10000,
302 .offset = 1,
303 .offset_relative_to_alignmask = true,
304 },
305 },
306 .iv_offset = 1,
307 .iv_offset_relative_to_alignmask = true,
308 }, {
309 .name = "two even aligned splits",
310 .src_divs = {
311 { .proportion_of_total = 5000 },
312 { .proportion_of_total = 5000 },
313 },
314 }, {
315 .name = "uneven misaligned splits, may sleep",
316 .req_flags = CRYPTO_TFM_REQ_MAY_SLEEP,
317 .src_divs = {
318 { .proportion_of_total = 1900, .offset = 33 },
319 { .proportion_of_total = 3300, .offset = 7 },
320 { .proportion_of_total = 4800, .offset = 18 },
321 },
322 .iv_offset = 3,
323 }, {
324 .name = "misaligned splits crossing pages, inplace",
325 .inplace = true,
326 .src_divs = {
327 {
328 .proportion_of_total = 7500,
329 .offset = PAGE_SIZE - 32
330 }, {
331 .proportion_of_total = 2500,
332 .offset = PAGE_SIZE - 7
333 },
334 },
335 }
336};
337
Eric Biggers4cc2dcf2019-01-31 23:51:48 -0800338static const struct testvec_config default_hash_testvec_configs[] = {
339 {
340 .name = "init+update+final aligned buffer",
341 .src_divs = { { .proportion_of_total = 10000 } },
342 .finalization_type = FINALIZATION_TYPE_FINAL,
343 }, {
344 .name = "init+finup aligned buffer",
345 .src_divs = { { .proportion_of_total = 10000 } },
346 .finalization_type = FINALIZATION_TYPE_FINUP,
347 }, {
348 .name = "digest aligned buffer",
349 .src_divs = { { .proportion_of_total = 10000 } },
350 .finalization_type = FINALIZATION_TYPE_DIGEST,
351 }, {
352 .name = "init+update+final misaligned buffer",
353 .src_divs = { { .proportion_of_total = 10000, .offset = 1 } },
354 .finalization_type = FINALIZATION_TYPE_FINAL,
355 }, {
356 .name = "digest buffer aligned only to alignmask",
357 .src_divs = {
358 {
359 .proportion_of_total = 10000,
360 .offset = 1,
361 .offset_relative_to_alignmask = true,
362 },
363 },
364 .finalization_type = FINALIZATION_TYPE_DIGEST,
365 }, {
366 .name = "init+update+update+final two even splits",
367 .src_divs = {
368 { .proportion_of_total = 5000 },
369 {
370 .proportion_of_total = 5000,
371 .flush_type = FLUSH_TYPE_FLUSH,
372 },
373 },
374 .finalization_type = FINALIZATION_TYPE_FINAL,
375 }, {
376 .name = "digest uneven misaligned splits, may sleep",
377 .req_flags = CRYPTO_TFM_REQ_MAY_SLEEP,
378 .src_divs = {
379 { .proportion_of_total = 1900, .offset = 33 },
380 { .proportion_of_total = 3300, .offset = 7 },
381 { .proportion_of_total = 4800, .offset = 18 },
382 },
383 .finalization_type = FINALIZATION_TYPE_DIGEST,
384 }, {
385 .name = "digest misaligned splits crossing pages",
386 .src_divs = {
387 {
388 .proportion_of_total = 7500,
389 .offset = PAGE_SIZE - 32,
390 }, {
391 .proportion_of_total = 2500,
392 .offset = PAGE_SIZE - 7,
393 },
394 },
395 .finalization_type = FINALIZATION_TYPE_DIGEST,
396 }, {
397 .name = "import/export",
398 .src_divs = {
399 {
400 .proportion_of_total = 6500,
401 .flush_type = FLUSH_TYPE_REIMPORT,
402 }, {
403 .proportion_of_total = 3500,
404 .flush_type = FLUSH_TYPE_REIMPORT,
405 },
406 },
407 .finalization_type = FINALIZATION_TYPE_FINAL,
408 }
409};
410
Eric Biggers3f47a032019-01-31 23:51:43 -0800411static unsigned int count_test_sg_divisions(const struct test_sg_division *divs)
412{
413 unsigned int remaining = TEST_SG_TOTAL;
414 unsigned int ndivs = 0;
415
416 do {
417 remaining -= divs[ndivs++].proportion_of_total;
418 } while (remaining);
419
420 return ndivs;
421}
422
423static bool valid_sg_divisions(const struct test_sg_division *divs,
424 unsigned int count, bool *any_flushes_ret)
425{
426 unsigned int total = 0;
427 unsigned int i;
428
429 for (i = 0; i < count && total != TEST_SG_TOTAL; i++) {
430 if (divs[i].proportion_of_total <= 0 ||
431 divs[i].proportion_of_total > TEST_SG_TOTAL - total)
432 return false;
433 total += divs[i].proportion_of_total;
434 if (divs[i].flush_type != FLUSH_TYPE_NONE)
435 *any_flushes_ret = true;
436 }
437 return total == TEST_SG_TOTAL &&
438 memchr_inv(&divs[i], 0, (count - i) * sizeof(divs[0])) == NULL;
439}
440
441/*
442 * Check whether the given testvec_config is valid. This isn't strictly needed
443 * since every testvec_config should be valid, but check anyway so that people
444 * don't unknowingly add broken configs that don't do what they wanted.
445 */
446static bool valid_testvec_config(const struct testvec_config *cfg)
447{
448 bool any_flushes = false;
449
450 if (cfg->name == NULL)
451 return false;
452
453 if (!valid_sg_divisions(cfg->src_divs, ARRAY_SIZE(cfg->src_divs),
454 &any_flushes))
455 return false;
456
457 if (cfg->dst_divs[0].proportion_of_total) {
458 if (!valid_sg_divisions(cfg->dst_divs,
459 ARRAY_SIZE(cfg->dst_divs),
460 &any_flushes))
461 return false;
462 } else {
463 if (memchr_inv(cfg->dst_divs, 0, sizeof(cfg->dst_divs)))
464 return false;
465 /* defaults to dst_divs=src_divs */
466 }
467
468 if (cfg->iv_offset +
469 (cfg->iv_offset_relative_to_alignmask ? MAX_ALGAPI_ALIGNMASK : 0) >
470 MAX_ALGAPI_ALIGNMASK + 1)
471 return false;
472
473 if (any_flushes && cfg->finalization_type == FINALIZATION_TYPE_DIGEST)
474 return false;
475
476 return true;
477}
478
479struct test_sglist {
480 char *bufs[XBUFSIZE];
481 struct scatterlist sgl[XBUFSIZE];
482 struct scatterlist sgl_saved[XBUFSIZE];
483 struct scatterlist *sgl_ptr;
484 unsigned int nents;
485};
486
487static int init_test_sglist(struct test_sglist *tsgl)
488{
489 return __testmgr_alloc_buf(tsgl->bufs, 1 /* two pages per buffer */);
490}
491
492static void destroy_test_sglist(struct test_sglist *tsgl)
493{
494 return __testmgr_free_buf(tsgl->bufs, 1 /* two pages per buffer */);
495}
496
497/**
498 * build_test_sglist() - build a scatterlist for a crypto test
499 *
500 * @tsgl: the scatterlist to build. @tsgl->bufs[] contains an array of 2-page
501 * buffers which the scatterlist @tsgl->sgl[] will be made to point into.
502 * @divs: the layout specification on which the scatterlist will be based
503 * @alignmask: the algorithm's alignmask
504 * @total_len: the total length of the scatterlist to build in bytes
505 * @data: if non-NULL, the buffers will be filled with this data until it ends.
506 * Otherwise the buffers will be poisoned. In both cases, some bytes
507 * past the end of each buffer will be poisoned to help detect overruns.
508 * @out_divs: if non-NULL, the test_sg_division to which each scatterlist entry
509 * corresponds will be returned here. This will match @divs except
510 * that divisions resolving to a length of 0 are omitted as they are
511 * not included in the scatterlist.
512 *
513 * Return: 0 or a -errno value
514 */
515static int build_test_sglist(struct test_sglist *tsgl,
516 const struct test_sg_division *divs,
517 const unsigned int alignmask,
518 const unsigned int total_len,
519 struct iov_iter *data,
520 const struct test_sg_division *out_divs[XBUFSIZE])
521{
522 struct {
523 const struct test_sg_division *div;
524 size_t length;
525 } partitions[XBUFSIZE];
526 const unsigned int ndivs = count_test_sg_divisions(divs);
527 unsigned int len_remaining = total_len;
528 unsigned int i;
529
530 BUILD_BUG_ON(ARRAY_SIZE(partitions) != ARRAY_SIZE(tsgl->sgl));
531 if (WARN_ON(ndivs > ARRAY_SIZE(partitions)))
532 return -EINVAL;
533
534 /* Calculate the (div, length) pairs */
535 tsgl->nents = 0;
536 for (i = 0; i < ndivs; i++) {
537 unsigned int len_this_sg =
538 min(len_remaining,
539 (total_len * divs[i].proportion_of_total +
540 TEST_SG_TOTAL / 2) / TEST_SG_TOTAL);
541
542 if (len_this_sg != 0) {
543 partitions[tsgl->nents].div = &divs[i];
544 partitions[tsgl->nents].length = len_this_sg;
545 tsgl->nents++;
546 len_remaining -= len_this_sg;
547 }
548 }
549 if (tsgl->nents == 0) {
550 partitions[tsgl->nents].div = &divs[0];
551 partitions[tsgl->nents].length = 0;
552 tsgl->nents++;
553 }
554 partitions[tsgl->nents - 1].length += len_remaining;
555
556 /* Set up the sgl entries and fill the data or poison */
557 sg_init_table(tsgl->sgl, tsgl->nents);
558 for (i = 0; i < tsgl->nents; i++) {
559 unsigned int offset = partitions[i].div->offset;
560 void *addr;
561
562 if (partitions[i].div->offset_relative_to_alignmask)
563 offset += alignmask;
564
565 while (offset + partitions[i].length + TESTMGR_POISON_LEN >
566 2 * PAGE_SIZE) {
567 if (WARN_ON(offset <= 0))
568 return -EINVAL;
569 offset /= 2;
570 }
571
572 addr = &tsgl->bufs[i][offset];
573 sg_set_buf(&tsgl->sgl[i], addr, partitions[i].length);
574
575 if (out_divs)
576 out_divs[i] = partitions[i].div;
577
578 if (data) {
579 size_t copy_len, copied;
580
581 copy_len = min(partitions[i].length, data->count);
582 copied = copy_from_iter(addr, copy_len, data);
583 if (WARN_ON(copied != copy_len))
584 return -EINVAL;
585 testmgr_poison(addr + copy_len, partitions[i].length +
586 TESTMGR_POISON_LEN - copy_len);
587 } else {
588 testmgr_poison(addr, partitions[i].length +
589 TESTMGR_POISON_LEN);
590 }
591 }
592
593 sg_mark_end(&tsgl->sgl[tsgl->nents - 1]);
594 tsgl->sgl_ptr = tsgl->sgl;
595 memcpy(tsgl->sgl_saved, tsgl->sgl, tsgl->nents * sizeof(tsgl->sgl[0]));
596 return 0;
597}
598
599/*
600 * Verify that a scatterlist crypto operation produced the correct output.
601 *
602 * @tsgl: scatterlist containing the actual output
603 * @expected_output: buffer containing the expected output
604 * @len_to_check: length of @expected_output in bytes
605 * @unchecked_prefix_len: number of ignored bytes in @tsgl prior to real result
606 * @check_poison: verify that the poison bytes after each chunk are intact?
607 *
608 * Return: 0 if correct, -EINVAL if incorrect, -EOVERFLOW if buffer overrun.
609 */
610static int verify_correct_output(const struct test_sglist *tsgl,
611 const char *expected_output,
612 unsigned int len_to_check,
613 unsigned int unchecked_prefix_len,
614 bool check_poison)
615{
616 unsigned int i;
617
618 for (i = 0; i < tsgl->nents; i++) {
619 struct scatterlist *sg = &tsgl->sgl_ptr[i];
620 unsigned int len = sg->length;
621 unsigned int offset = sg->offset;
622 const char *actual_output;
623
624 if (unchecked_prefix_len) {
625 if (unchecked_prefix_len >= len) {
626 unchecked_prefix_len -= len;
627 continue;
628 }
629 offset += unchecked_prefix_len;
630 len -= unchecked_prefix_len;
631 unchecked_prefix_len = 0;
632 }
633 len = min(len, len_to_check);
634 actual_output = page_address(sg_page(sg)) + offset;
635 if (memcmp(expected_output, actual_output, len) != 0)
636 return -EINVAL;
637 if (check_poison &&
638 !testmgr_is_poison(actual_output + len, TESTMGR_POISON_LEN))
639 return -EOVERFLOW;
640 len_to_check -= len;
641 expected_output += len;
642 }
643 if (WARN_ON(len_to_check != 0))
644 return -EINVAL;
645 return 0;
646}
647
648static bool is_test_sglist_corrupted(const struct test_sglist *tsgl)
649{
650 unsigned int i;
651
652 for (i = 0; i < tsgl->nents; i++) {
653 if (tsgl->sgl[i].page_link != tsgl->sgl_saved[i].page_link)
654 return true;
655 if (tsgl->sgl[i].offset != tsgl->sgl_saved[i].offset)
656 return true;
657 if (tsgl->sgl[i].length != tsgl->sgl_saved[i].length)
658 return true;
659 }
660 return false;
661}
662
663struct cipher_test_sglists {
664 struct test_sglist src;
665 struct test_sglist dst;
666};
667
668static struct cipher_test_sglists *alloc_cipher_test_sglists(void)
669{
670 struct cipher_test_sglists *tsgls;
671
672 tsgls = kmalloc(sizeof(*tsgls), GFP_KERNEL);
673 if (!tsgls)
674 return NULL;
675
676 if (init_test_sglist(&tsgls->src) != 0)
677 goto fail_kfree;
678 if (init_test_sglist(&tsgls->dst) != 0)
679 goto fail_destroy_src;
680
681 return tsgls;
682
683fail_destroy_src:
684 destroy_test_sglist(&tsgls->src);
685fail_kfree:
686 kfree(tsgls);
687 return NULL;
688}
689
690static void free_cipher_test_sglists(struct cipher_test_sglists *tsgls)
691{
692 if (tsgls) {
693 destroy_test_sglist(&tsgls->src);
694 destroy_test_sglist(&tsgls->dst);
695 kfree(tsgls);
696 }
697}
698
699/* Build the src and dst scatterlists for an skcipher or AEAD test */
700static int build_cipher_test_sglists(struct cipher_test_sglists *tsgls,
701 const struct testvec_config *cfg,
702 unsigned int alignmask,
703 unsigned int src_total_len,
704 unsigned int dst_total_len,
705 const struct kvec *inputs,
706 unsigned int nr_inputs)
707{
708 struct iov_iter input;
709 int err;
710
711 iov_iter_kvec(&input, WRITE, inputs, nr_inputs, src_total_len);
712 err = build_test_sglist(&tsgls->src, cfg->src_divs, alignmask,
713 cfg->inplace ?
714 max(dst_total_len, src_total_len) :
715 src_total_len,
716 &input, NULL);
717 if (err)
718 return err;
719
720 if (cfg->inplace) {
721 tsgls->dst.sgl_ptr = tsgls->src.sgl;
722 tsgls->dst.nents = tsgls->src.nents;
723 return 0;
724 }
725 return build_test_sglist(&tsgls->dst,
726 cfg->dst_divs[0].proportion_of_total ?
727 cfg->dst_divs : cfg->src_divs,
728 alignmask, dst_total_len, NULL, NULL);
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800729}
730
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800731#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
732static char *generate_random_sgl_divisions(struct test_sg_division *divs,
733 size_t max_divs, char *p, char *end,
734 bool gen_flushes)
735{
736 struct test_sg_division *div = divs;
737 unsigned int remaining = TEST_SG_TOTAL;
738
739 do {
740 unsigned int this_len;
741
742 if (div == &divs[max_divs - 1] || prandom_u32() % 2 == 0)
743 this_len = remaining;
744 else
745 this_len = 1 + (prandom_u32() % remaining);
746 div->proportion_of_total = this_len;
747
748 if (prandom_u32() % 4 == 0)
749 div->offset = (PAGE_SIZE - 128) + (prandom_u32() % 128);
750 else if (prandom_u32() % 2 == 0)
751 div->offset = prandom_u32() % 32;
752 else
753 div->offset = prandom_u32() % PAGE_SIZE;
754 if (prandom_u32() % 8 == 0)
755 div->offset_relative_to_alignmask = true;
756
757 div->flush_type = FLUSH_TYPE_NONE;
758 if (gen_flushes) {
759 switch (prandom_u32() % 4) {
760 case 0:
761 div->flush_type = FLUSH_TYPE_REIMPORT;
762 break;
763 case 1:
764 div->flush_type = FLUSH_TYPE_FLUSH;
765 break;
766 }
767 }
768
769 BUILD_BUG_ON(TEST_SG_TOTAL != 10000); /* for "%u.%u%%" */
770 p += scnprintf(p, end - p, "%s%u.%u%%@%s+%u%s",
771 div->flush_type == FLUSH_TYPE_NONE ? "" :
772 div->flush_type == FLUSH_TYPE_FLUSH ?
773 "<flush> " : "<reimport> ",
774 this_len / 100, this_len % 100,
775 div->offset_relative_to_alignmask ?
776 "alignmask" : "",
777 div->offset, this_len == remaining ? "" : ", ");
778 remaining -= this_len;
779 div++;
780 } while (remaining);
781
782 return p;
783}
784
785/* Generate a random testvec_config for fuzz testing */
786static void generate_random_testvec_config(struct testvec_config *cfg,
787 char *name, size_t max_namelen)
788{
789 char *p = name;
790 char * const end = name + max_namelen;
791
792 memset(cfg, 0, sizeof(*cfg));
793
794 cfg->name = name;
795
796 p += scnprintf(p, end - p, "random:");
797
798 if (prandom_u32() % 2 == 0) {
799 cfg->inplace = true;
800 p += scnprintf(p, end - p, " inplace");
801 }
802
803 if (prandom_u32() % 2 == 0) {
804 cfg->req_flags |= CRYPTO_TFM_REQ_MAY_SLEEP;
805 p += scnprintf(p, end - p, " may_sleep");
806 }
807
808 switch (prandom_u32() % 4) {
809 case 0:
810 cfg->finalization_type = FINALIZATION_TYPE_FINAL;
811 p += scnprintf(p, end - p, " use_final");
812 break;
813 case 1:
814 cfg->finalization_type = FINALIZATION_TYPE_FINUP;
815 p += scnprintf(p, end - p, " use_finup");
816 break;
817 default:
818 cfg->finalization_type = FINALIZATION_TYPE_DIGEST;
819 p += scnprintf(p, end - p, " use_digest");
820 break;
821 }
822
823 p += scnprintf(p, end - p, " src_divs=[");
824 p = generate_random_sgl_divisions(cfg->src_divs,
825 ARRAY_SIZE(cfg->src_divs), p, end,
826 (cfg->finalization_type !=
827 FINALIZATION_TYPE_DIGEST));
828 p += scnprintf(p, end - p, "]");
829
830 if (!cfg->inplace && prandom_u32() % 2 == 0) {
831 p += scnprintf(p, end - p, " dst_divs=[");
832 p = generate_random_sgl_divisions(cfg->dst_divs,
833 ARRAY_SIZE(cfg->dst_divs),
834 p, end, false);
835 p += scnprintf(p, end - p, "]");
836 }
837
838 if (prandom_u32() % 2 == 0) {
839 cfg->iv_offset = 1 + (prandom_u32() % MAX_ALGAPI_ALIGNMASK);
840 p += scnprintf(p, end - p, " iv_offset=%u", cfg->iv_offset);
841 }
842
843 WARN_ON_ONCE(!valid_testvec_config(cfg));
844}
Eric Biggersb55e1a32019-03-12 22:12:47 -0700845
846static void crypto_disable_simd_for_test(void)
847{
848 preempt_disable();
849 __this_cpu_write(crypto_simd_disabled_for_test, true);
850}
851
852static void crypto_reenable_simd_for_test(void)
853{
854 __this_cpu_write(crypto_simd_disabled_for_test, false);
855 preempt_enable();
856}
857#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
858static void crypto_disable_simd_for_test(void)
859{
860}
861
862static void crypto_reenable_simd_for_test(void)
863{
864}
865#endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
Eric Biggers25f9ddd2019-01-31 23:51:45 -0800866
Eric Biggers4cc2dcf2019-01-31 23:51:48 -0800867static int check_nonfinal_hash_op(const char *op, int err,
868 u8 *result, unsigned int digestsize,
869 const char *driver, unsigned int vec_num,
870 const struct testvec_config *cfg)
Kamil Konieczny466d7b92018-01-16 15:26:13 +0100871{
Eric Biggers4cc2dcf2019-01-31 23:51:48 -0800872 if (err) {
873 pr_err("alg: hash: %s %s() failed with err %d on test vector %u, cfg=\"%s\"\n",
874 driver, op, err, vec_num, cfg->name);
875 return err;
876 }
877 if (!testmgr_is_poison(result, digestsize)) {
878 pr_err("alg: hash: %s %s() used result buffer on test vector %u, cfg=\"%s\"\n",
879 driver, op, vec_num, cfg->name);
880 return -EINVAL;
881 }
882 return 0;
883}
Kamil Konieczny466d7b92018-01-16 15:26:13 +0100884
Eric Biggers4cc2dcf2019-01-31 23:51:48 -0800885static int test_hash_vec_cfg(const char *driver,
886 const struct hash_testvec *vec,
887 unsigned int vec_num,
888 const struct testvec_config *cfg,
889 struct ahash_request *req,
890 struct test_sglist *tsgl,
891 u8 *hashstate)
892{
893 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
894 const unsigned int alignmask = crypto_ahash_alignmask(tfm);
895 const unsigned int digestsize = crypto_ahash_digestsize(tfm);
896 const unsigned int statesize = crypto_ahash_statesize(tfm);
897 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
898 const struct test_sg_division *divs[XBUFSIZE];
899 DECLARE_CRYPTO_WAIT(wait);
900 struct kvec _input;
901 struct iov_iter input;
902 unsigned int i;
903 struct scatterlist *pending_sgl;
904 unsigned int pending_len;
905 u8 result[HASH_MAX_DIGESTSIZE + TESTMGR_POISON_LEN];
906 int err;
907
908 /* Set the key, if specified */
909 if (vec->ksize) {
910 err = crypto_ahash_setkey(tfm, vec->key, vec->ksize);
911 if (err) {
912 pr_err("alg: hash: %s setkey failed with err %d on test vector %u; flags=%#x\n",
913 driver, err, vec_num,
914 crypto_ahash_get_flags(tfm));
915 return err;
916 }
917 }
918
919 /* Build the scatterlist for the source data */
920 _input.iov_base = (void *)vec->plaintext;
921 _input.iov_len = vec->psize;
922 iov_iter_kvec(&input, WRITE, &_input, 1, vec->psize);
923 err = build_test_sglist(tsgl, cfg->src_divs, alignmask, vec->psize,
924 &input, divs);
925 if (err) {
926 pr_err("alg: hash: %s: error preparing scatterlist for test vector %u, cfg=\"%s\"\n",
927 driver, vec_num, cfg->name);
928 return err;
929 }
930
931 /* Do the actual hashing */
932
933 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
934 testmgr_poison(result, digestsize + TESTMGR_POISON_LEN);
935
936 if (cfg->finalization_type == FINALIZATION_TYPE_DIGEST) {
937 /* Just using digest() */
938 ahash_request_set_callback(req, req_flags, crypto_req_done,
939 &wait);
940 ahash_request_set_crypt(req, tsgl->sgl, result, vec->psize);
941 err = crypto_wait_req(crypto_ahash_digest(req), &wait);
942 if (err) {
943 pr_err("alg: hash: %s digest() failed with err %d on test vector %u, cfg=\"%s\"\n",
944 driver, err, vec_num, cfg->name);
945 return err;
946 }
947 goto result_ready;
948 }
949
950 /* Using init(), zero or more update(), then final() or finup() */
951
952 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
953 ahash_request_set_crypt(req, NULL, result, 0);
954 err = crypto_wait_req(crypto_ahash_init(req), &wait);
955 err = check_nonfinal_hash_op("init", err, result, digestsize,
956 driver, vec_num, cfg);
957 if (err)
958 return err;
959
960 pending_sgl = NULL;
961 pending_len = 0;
962 for (i = 0; i < tsgl->nents; i++) {
963 if (divs[i]->flush_type != FLUSH_TYPE_NONE &&
964 pending_sgl != NULL) {
965 /* update() with the pending data */
966 ahash_request_set_callback(req, req_flags,
967 crypto_req_done, &wait);
968 ahash_request_set_crypt(req, pending_sgl, result,
969 pending_len);
970 err = crypto_wait_req(crypto_ahash_update(req), &wait);
971 err = check_nonfinal_hash_op("update", err,
972 result, digestsize,
973 driver, vec_num, cfg);
974 if (err)
975 return err;
976 pending_sgl = NULL;
977 pending_len = 0;
978 }
979 if (divs[i]->flush_type == FLUSH_TYPE_REIMPORT) {
980 /* Test ->export() and ->import() */
981 testmgr_poison(hashstate + statesize,
982 TESTMGR_POISON_LEN);
983 err = crypto_ahash_export(req, hashstate);
984 err = check_nonfinal_hash_op("export", err,
985 result, digestsize,
986 driver, vec_num, cfg);
987 if (err)
988 return err;
989 if (!testmgr_is_poison(hashstate + statesize,
990 TESTMGR_POISON_LEN)) {
991 pr_err("alg: hash: %s export() overran state buffer on test vector %u, cfg=\"%s\"\n",
992 driver, vec_num, cfg->name);
993 return -EOVERFLOW;
994 }
995
996 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm));
997 err = crypto_ahash_import(req, hashstate);
998 err = check_nonfinal_hash_op("import", err,
999 result, digestsize,
1000 driver, vec_num, cfg);
1001 if (err)
1002 return err;
1003 }
1004 if (pending_sgl == NULL)
1005 pending_sgl = &tsgl->sgl[i];
1006 pending_len += tsgl->sgl[i].length;
1007 }
1008
1009 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait);
1010 ahash_request_set_crypt(req, pending_sgl, result, pending_len);
1011 if (cfg->finalization_type == FINALIZATION_TYPE_FINAL) {
1012 /* finish with update() and final() */
1013 err = crypto_wait_req(crypto_ahash_update(req), &wait);
1014 err = check_nonfinal_hash_op("update", err, result, digestsize,
1015 driver, vec_num, cfg);
1016 if (err)
1017 return err;
1018 err = crypto_wait_req(crypto_ahash_final(req), &wait);
1019 if (err) {
1020 pr_err("alg: hash: %s final() failed with err %d on test vector %u, cfg=\"%s\"\n",
1021 driver, err, vec_num, cfg->name);
1022 return err;
1023 }
1024 } else {
1025 /* finish with finup() */
1026 err = crypto_wait_req(crypto_ahash_finup(req), &wait);
1027 if (err) {
1028 pr_err("alg: hash: %s finup() failed with err %d on test vector %u, cfg=\"%s\"\n",
1029 driver, err, vec_num, cfg->name);
1030 return err;
1031 }
1032 }
1033
1034result_ready:
1035 /* Check that the algorithm produced the correct digest */
1036 if (memcmp(result, vec->digest, digestsize) != 0) {
1037 pr_err("alg: hash: %s test failed (wrong result) on test vector %u, cfg=\"%s\"\n",
1038 driver, vec_num, cfg->name);
1039 return -EINVAL;
1040 }
1041 if (!testmgr_is_poison(&result[digestsize], TESTMGR_POISON_LEN)) {
1042 pr_err("alg: hash: %s overran result buffer on test vector %u, cfg=\"%s\"\n",
1043 driver, vec_num, cfg->name);
1044 return -EOVERFLOW;
1045 }
1046
1047 return 0;
1048}
1049
1050static int test_hash_vec(const char *driver, const struct hash_testvec *vec,
1051 unsigned int vec_num, struct ahash_request *req,
1052 struct test_sglist *tsgl, u8 *hashstate)
1053{
1054 unsigned int i;
1055 int err;
1056
1057 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++) {
1058 err = test_hash_vec_cfg(driver, vec, vec_num,
1059 &default_hash_testvec_configs[i],
1060 req, tsgl, hashstate);
1061 if (err)
1062 return err;
1063 }
1064
1065#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1066 if (!noextratests) {
1067 struct testvec_config cfg;
1068 char cfgname[TESTVEC_CONFIG_NAMELEN];
1069
1070 for (i = 0; i < fuzz_iterations; i++) {
1071 generate_random_testvec_config(&cfg, cfgname,
1072 sizeof(cfgname));
1073 err = test_hash_vec_cfg(driver, vec, vec_num, &cfg,
1074 req, tsgl, hashstate);
1075 if (err)
1076 return err;
1077 }
1078 }
1079#endif
1080 return 0;
1081}
1082
1083static int __alg_test_hash(const struct hash_testvec *vecs,
1084 unsigned int num_vecs, const char *driver,
1085 u32 type, u32 mask)
1086{
1087 struct crypto_ahash *tfm;
1088 struct ahash_request *req = NULL;
1089 struct test_sglist *tsgl = NULL;
1090 u8 *hashstate = NULL;
1091 unsigned int i;
1092 int err;
1093
1094 tfm = crypto_alloc_ahash(driver, type, mask);
1095 if (IS_ERR(tfm)) {
1096 pr_err("alg: hash: failed to allocate transform for %s: %ld\n",
1097 driver, PTR_ERR(tfm));
1098 return PTR_ERR(tfm);
1099 }
1100
1101 req = ahash_request_alloc(tfm, GFP_KERNEL);
1102 if (!req) {
1103 pr_err("alg: hash: failed to allocate request for %s\n",
1104 driver);
1105 err = -ENOMEM;
1106 goto out;
1107 }
1108
1109 tsgl = kmalloc(sizeof(*tsgl), GFP_KERNEL);
1110 if (!tsgl || init_test_sglist(tsgl) != 0) {
1111 pr_err("alg: hash: failed to allocate test buffers for %s\n",
1112 driver);
1113 kfree(tsgl);
1114 tsgl = NULL;
1115 err = -ENOMEM;
1116 goto out;
1117 }
1118
1119 hashstate = kmalloc(crypto_ahash_statesize(tfm) + TESTMGR_POISON_LEN,
1120 GFP_KERNEL);
1121 if (!hashstate) {
1122 pr_err("alg: hash: failed to allocate hash state buffer for %s\n",
1123 driver);
1124 err = -ENOMEM;
1125 goto out;
1126 }
1127
1128 for (i = 0; i < num_vecs; i++) {
1129 err = test_hash_vec(driver, &vecs[i], i, req, tsgl, hashstate);
1130 if (err)
1131 goto out;
1132 }
1133 err = 0;
1134out:
1135 kfree(hashstate);
1136 if (tsgl) {
1137 destroy_test_sglist(tsgl);
1138 kfree(tsgl);
1139 }
1140 ahash_request_free(req);
1141 crypto_free_ahash(tfm);
1142 return err;
1143}
1144
1145static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1146 u32 type, u32 mask)
1147{
1148 const struct hash_testvec *template = desc->suite.hash.vecs;
1149 unsigned int tcount = desc->suite.hash.count;
1150 unsigned int nr_unkeyed, nr_keyed;
1151 int err;
1152
1153 /*
1154 * For OPTIONAL_KEY algorithms, we have to do all the unkeyed tests
1155 * first, before setting a key on the tfm. To make this easier, we
1156 * require that the unkeyed test vectors (if any) are listed first.
1157 */
1158
1159 for (nr_unkeyed = 0; nr_unkeyed < tcount; nr_unkeyed++) {
1160 if (template[nr_unkeyed].ksize)
1161 break;
1162 }
1163 for (nr_keyed = 0; nr_unkeyed + nr_keyed < tcount; nr_keyed++) {
1164 if (!template[nr_unkeyed + nr_keyed].ksize) {
1165 pr_err("alg: hash: test vectors for %s out of order, "
1166 "unkeyed ones must come first\n", desc->alg);
Kamil Konieczny466d7b92018-01-16 15:26:13 +01001167 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08001168 }
1169 }
1170
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001171 err = 0;
1172 if (nr_unkeyed) {
1173 err = __alg_test_hash(template, nr_unkeyed, driver, type, mask);
1174 template += nr_unkeyed;
Herbert Xuda7f0332008-07-31 17:08:25 +08001175 }
1176
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001177 if (!err && nr_keyed)
1178 err = __alg_test_hash(template, nr_keyed, driver, type, mask);
Wang, Rui Y018ba952016-02-03 18:26:57 +08001179
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08001180 return err;
Jussi Kivilinnada5ffe12013-06-13 17:37:55 +03001181}
1182
Eric Biggersed968042019-01-31 23:51:47 -08001183static int test_aead_vec_cfg(const char *driver, int enc,
1184 const struct aead_testvec *vec,
1185 unsigned int vec_num,
1186 const struct testvec_config *cfg,
1187 struct aead_request *req,
1188 struct cipher_test_sglists *tsgls)
Herbert Xuda7f0332008-07-31 17:08:25 +08001189{
Eric Biggersed968042019-01-31 23:51:47 -08001190 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1191 const unsigned int alignmask = crypto_aead_alignmask(tfm);
1192 const unsigned int ivsize = crypto_aead_ivsize(tfm);
1193 const unsigned int authsize = vec->clen - vec->plen;
1194 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1195 const char *op = enc ? "encryption" : "decryption";
1196 DECLARE_CRYPTO_WAIT(wait);
1197 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
1198 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
1199 cfg->iv_offset +
1200 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
1201 struct kvec input[2];
1202 int err;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001203
Eric Biggersed968042019-01-31 23:51:47 -08001204 /* Set the key */
1205 if (vec->wk)
1206 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001207 else
Eric Biggersed968042019-01-31 23:51:47 -08001208 crypto_aead_clear_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1209 err = crypto_aead_setkey(tfm, vec->key, vec->klen);
1210 if (err) {
1211 if (vec->fail) /* expectedly failed to set key? */
1212 return 0;
1213 pr_err("alg: aead: %s setkey failed with err %d on test vector %u; flags=%#x\n",
1214 driver, err, vec_num, crypto_aead_get_flags(tfm));
1215 return err;
1216 }
1217 if (vec->fail) {
1218 pr_err("alg: aead: %s setkey unexpectedly succeeded on test vector %u\n",
1219 driver, vec_num);
1220 return -EINVAL;
1221 }
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001222
Eric Biggersed968042019-01-31 23:51:47 -08001223 /* Set the authentication tag size */
1224 err = crypto_aead_setauthsize(tfm, authsize);
1225 if (err) {
1226 pr_err("alg: aead: %s setauthsize failed with err %d on test vector %u\n",
1227 driver, err, vec_num);
1228 return err;
1229 }
1230
1231 /* The IV must be copied to a buffer, as the algorithm may modify it */
1232 if (WARN_ON(ivsize > MAX_IVLEN))
1233 return -EINVAL;
1234 if (vec->iv)
1235 memcpy(iv, vec->iv, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001236 else
Eric Biggersed968042019-01-31 23:51:47 -08001237 memset(iv, 0, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001238
Eric Biggersed968042019-01-31 23:51:47 -08001239 /* Build the src/dst scatterlists */
1240 input[0].iov_base = (void *)vec->assoc;
1241 input[0].iov_len = vec->alen;
1242 input[1].iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
1243 input[1].iov_len = enc ? vec->plen : vec->clen;
1244 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
1245 vec->alen + (enc ? vec->plen :
1246 vec->clen),
1247 vec->alen + (enc ? vec->clen :
1248 vec->plen),
1249 input, 2);
1250 if (err) {
1251 pr_err("alg: aead: %s %s: error preparing scatterlists for test vector %u, cfg=\"%s\"\n",
1252 driver, op, vec_num, cfg->name);
1253 return err;
Herbert Xuda7f0332008-07-31 17:08:25 +08001254 }
1255
Eric Biggersed968042019-01-31 23:51:47 -08001256 /* Do the actual encryption or decryption */
1257 testmgr_poison(req->__ctx, crypto_aead_reqsize(tfm));
1258 aead_request_set_callback(req, req_flags, crypto_req_done, &wait);
1259 aead_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
1260 enc ? vec->plen : vec->clen, iv);
1261 aead_request_set_ad(req, vec->alen);
1262 err = crypto_wait_req(enc ? crypto_aead_encrypt(req) :
1263 crypto_aead_decrypt(req), &wait);
Eric Biggersed968042019-01-31 23:51:47 -08001264 if (err) {
1265 if (err == -EBADMSG && vec->novrfy)
1266 return 0;
1267 pr_err("alg: aead: %s %s failed with err %d on test vector %u, cfg=\"%s\"\n",
1268 driver, op, err, vec_num, cfg->name);
1269 return err;
1270 }
1271 if (vec->novrfy) {
1272 pr_err("alg: aead: %s %s unexpectedly succeeded on test vector %u, cfg=\"%s\"\n",
1273 driver, op, vec_num, cfg->name);
1274 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08001275 }
1276
Eric Biggersa6e5ef92019-01-31 23:51:50 -08001277 /* Check that the algorithm didn't overwrite things it shouldn't have */
1278 if (req->cryptlen != (enc ? vec->plen : vec->clen) ||
1279 req->assoclen != vec->alen ||
1280 req->iv != iv ||
1281 req->src != tsgls->src.sgl_ptr ||
1282 req->dst != tsgls->dst.sgl_ptr ||
1283 crypto_aead_reqtfm(req) != tfm ||
1284 req->base.complete != crypto_req_done ||
1285 req->base.flags != req_flags ||
1286 req->base.data != &wait) {
1287 pr_err("alg: aead: %s %s corrupted request struct on test vector %u, cfg=\"%s\"\n",
1288 driver, op, vec_num, cfg->name);
1289 if (req->cryptlen != (enc ? vec->plen : vec->clen))
1290 pr_err("alg: aead: changed 'req->cryptlen'\n");
1291 if (req->assoclen != vec->alen)
1292 pr_err("alg: aead: changed 'req->assoclen'\n");
1293 if (req->iv != iv)
1294 pr_err("alg: aead: changed 'req->iv'\n");
1295 if (req->src != tsgls->src.sgl_ptr)
1296 pr_err("alg: aead: changed 'req->src'\n");
1297 if (req->dst != tsgls->dst.sgl_ptr)
1298 pr_err("alg: aead: changed 'req->dst'\n");
1299 if (crypto_aead_reqtfm(req) != tfm)
1300 pr_err("alg: aead: changed 'req->base.tfm'\n");
1301 if (req->base.complete != crypto_req_done)
1302 pr_err("alg: aead: changed 'req->base.complete'\n");
1303 if (req->base.flags != req_flags)
1304 pr_err("alg: aead: changed 'req->base.flags'\n");
1305 if (req->base.data != &wait)
1306 pr_err("alg: aead: changed 'req->base.data'\n");
1307 return -EINVAL;
1308 }
1309 if (is_test_sglist_corrupted(&tsgls->src)) {
1310 pr_err("alg: aead: %s %s corrupted src sgl on test vector %u, cfg=\"%s\"\n",
1311 driver, op, vec_num, cfg->name);
1312 return -EINVAL;
1313 }
1314 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
1315 is_test_sglist_corrupted(&tsgls->dst)) {
1316 pr_err("alg: aead: %s %s corrupted dst sgl on test vector %u, cfg=\"%s\"\n",
1317 driver, op, vec_num, cfg->name);
1318 return -EINVAL;
1319 }
1320
Eric Biggersed968042019-01-31 23:51:47 -08001321 /* Check for the correct output (ciphertext or plaintext) */
1322 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
1323 enc ? vec->clen : vec->plen,
1324 vec->alen, enc || !cfg->inplace);
1325 if (err == -EOVERFLOW) {
1326 pr_err("alg: aead: %s %s overran dst buffer on test vector %u, cfg=\"%s\"\n",
1327 driver, op, vec_num, cfg->name);
1328 return err;
Herbert Xuda7f0332008-07-31 17:08:25 +08001329 }
Eric Biggersed968042019-01-31 23:51:47 -08001330 if (err) {
1331 pr_err("alg: aead: %s %s test failed (wrong result) on test vector %u, cfg=\"%s\"\n",
1332 driver, op, vec_num, cfg->name);
1333 return err;
Jussi Kivilinna58dcf542013-06-13 17:37:50 +03001334 }
1335
1336 return 0;
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001337}
1338
Eric Biggersed968042019-01-31 23:51:47 -08001339static int test_aead_vec(const char *driver, int enc,
1340 const struct aead_testvec *vec, unsigned int vec_num,
1341 struct aead_request *req,
1342 struct cipher_test_sglists *tsgls)
1343{
1344 unsigned int i;
1345 int err;
1346
1347 if (enc && vec->novrfy)
1348 return 0;
1349
1350 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
1351 err = test_aead_vec_cfg(driver, enc, vec, vec_num,
1352 &default_cipher_testvec_configs[i],
1353 req, tsgls);
1354 if (err)
1355 return err;
1356 }
1357
1358#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1359 if (!noextratests) {
1360 struct testvec_config cfg;
1361 char cfgname[TESTVEC_CONFIG_NAMELEN];
1362
1363 for (i = 0; i < fuzz_iterations; i++) {
1364 generate_random_testvec_config(&cfg, cfgname,
1365 sizeof(cfgname));
1366 err = test_aead_vec_cfg(driver, enc, vec, vec_num,
1367 &cfg, req, tsgls);
1368 if (err)
1369 return err;
1370 }
1371 }
1372#endif
1373 return 0;
1374}
1375
1376static int test_aead(const char *driver, int enc,
1377 const struct aead_test_suite *suite,
1378 struct aead_request *req,
1379 struct cipher_test_sglists *tsgls)
1380{
1381 unsigned int i;
1382 int err;
1383
1384 for (i = 0; i < suite->count; i++) {
1385 err = test_aead_vec(driver, enc, &suite->vecs[i], i, req,
1386 tsgls);
1387 if (err)
1388 return err;
1389 }
1390 return 0;
1391}
1392
1393static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
1394 u32 type, u32 mask)
1395{
1396 const struct aead_test_suite *suite = &desc->suite.aead;
1397 struct crypto_aead *tfm;
1398 struct aead_request *req = NULL;
1399 struct cipher_test_sglists *tsgls = NULL;
1400 int err;
1401
1402 if (suite->count <= 0) {
1403 pr_err("alg: aead: empty test suite for %s\n", driver);
1404 return -EINVAL;
1405 }
1406
1407 tfm = crypto_alloc_aead(driver, type, mask);
1408 if (IS_ERR(tfm)) {
1409 pr_err("alg: aead: failed to allocate transform for %s: %ld\n",
1410 driver, PTR_ERR(tfm));
1411 return PTR_ERR(tfm);
1412 }
1413
1414 req = aead_request_alloc(tfm, GFP_KERNEL);
1415 if (!req) {
1416 pr_err("alg: aead: failed to allocate request for %s\n",
1417 driver);
1418 err = -ENOMEM;
1419 goto out;
1420 }
1421
1422 tsgls = alloc_cipher_test_sglists();
1423 if (!tsgls) {
1424 pr_err("alg: aead: failed to allocate test buffers for %s\n",
1425 driver);
1426 err = -ENOMEM;
1427 goto out;
1428 }
1429
1430 err = test_aead(driver, ENCRYPT, suite, req, tsgls);
1431 if (err)
1432 goto out;
1433
1434 err = test_aead(driver, DECRYPT, suite, req, tsgls);
1435out:
1436 free_cipher_test_sglists(tsgls);
1437 aead_request_free(req);
1438 crypto_free_aead(tfm);
1439 return err;
1440}
1441
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001442static int test_cipher(struct crypto_cipher *tfm, int enc,
Eric Biggersb13b1e02017-02-24 15:46:59 -08001443 const struct cipher_testvec *template,
1444 unsigned int tcount)
Herbert Xuda7f0332008-07-31 17:08:25 +08001445{
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001446 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
1447 unsigned int i, j, k;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001448 char *q;
1449 const char *e;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001450 const char *input, *result;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001451 void *data;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001452 char *xbuf[XBUFSIZE];
1453 int ret = -ENOMEM;
1454
1455 if (testmgr_alloc_buf(xbuf))
1456 goto out_nobuf;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001457
1458 if (enc == ENCRYPT)
1459 e = "encryption";
1460 else
1461 e = "decryption";
1462
1463 j = 0;
1464 for (i = 0; i < tcount; i++) {
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001465
Stephan Mueller10faa8c2016-08-25 15:15:01 +02001466 if (fips_enabled && template[i].fips_skip)
1467 continue;
1468
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001469 input = enc ? template[i].ptext : template[i].ctext;
1470 result = enc ? template[i].ctext : template[i].ptext;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001471 j++;
1472
Herbert Xufd57f222009-05-29 16:05:42 +10001473 ret = -EINVAL;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001474 if (WARN_ON(template[i].len > PAGE_SIZE))
Herbert Xufd57f222009-05-29 16:05:42 +10001475 goto out;
1476
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001477 data = xbuf[0];
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001478 memcpy(data, input, template[i].len);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001479
1480 crypto_cipher_clear_flags(tfm, ~0);
1481 if (template[i].wk)
Eric Biggers231baec2019-01-18 22:48:00 -08001482 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001483
1484 ret = crypto_cipher_setkey(tfm, template[i].key,
1485 template[i].klen);
Yanjiang Jin0fae0c12016-07-29 16:32:09 +08001486 if (template[i].fail == !ret) {
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001487 printk(KERN_ERR "alg: cipher: setkey failed "
1488 "on test %d for %s: flags=%x\n", j,
1489 algo, crypto_cipher_get_flags(tfm));
1490 goto out;
1491 } else if (ret)
1492 continue;
1493
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001494 for (k = 0; k < template[i].len;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001495 k += crypto_cipher_blocksize(tfm)) {
1496 if (enc)
1497 crypto_cipher_encrypt_one(tfm, data + k,
1498 data + k);
1499 else
1500 crypto_cipher_decrypt_one(tfm, data + k,
1501 data + k);
1502 }
1503
1504 q = data;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001505 if (memcmp(q, result, template[i].len)) {
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001506 printk(KERN_ERR "alg: cipher: Test %d failed "
1507 "on %s for %s\n", j, e, algo);
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001508 hexdump(q, template[i].len);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001509 ret = -EINVAL;
1510 goto out;
1511 }
1512 }
1513
1514 ret = 0;
1515
1516out:
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001517 testmgr_free_buf(xbuf);
1518out_nobuf:
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001519 return ret;
1520}
1521
Eric Biggers4e7babba2019-01-31 23:51:46 -08001522static int test_skcipher_vec_cfg(const char *driver, int enc,
1523 const struct cipher_testvec *vec,
1524 unsigned int vec_num,
1525 const struct testvec_config *cfg,
1526 struct skcipher_request *req,
1527 struct cipher_test_sglists *tsgls)
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001528{
Eric Biggers4e7babba2019-01-31 23:51:46 -08001529 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1530 const unsigned int alignmask = crypto_skcipher_alignmask(tfm);
1531 const unsigned int ivsize = crypto_skcipher_ivsize(tfm);
1532 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags;
1533 const char *op = enc ? "encryption" : "decryption";
1534 DECLARE_CRYPTO_WAIT(wait);
1535 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN];
1536 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) +
1537 cfg->iv_offset +
1538 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0);
1539 struct kvec input;
1540 int err;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001541
Eric Biggers4e7babba2019-01-31 23:51:46 -08001542 /* Set the key */
1543 if (vec->wk)
1544 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001545 else
Eric Biggers4e7babba2019-01-31 23:51:46 -08001546 crypto_skcipher_clear_flags(tfm,
1547 CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
1548 err = crypto_skcipher_setkey(tfm, vec->key, vec->klen);
1549 if (err) {
1550 if (vec->fail) /* expectedly failed to set key? */
1551 return 0;
1552 pr_err("alg: skcipher: %s setkey failed with err %d on test vector %u; flags=%#x\n",
1553 driver, err, vec_num, crypto_skcipher_get_flags(tfm));
1554 return err;
1555 }
1556 if (vec->fail) {
1557 pr_err("alg: skcipher: %s setkey unexpectedly succeeded on test vector %u\n",
1558 driver, vec_num);
1559 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08001560 }
1561
Eric Biggers4e7babba2019-01-31 23:51:46 -08001562 /* The IV must be copied to a buffer, as the algorithm may modify it */
1563 if (ivsize) {
1564 if (WARN_ON(ivsize > MAX_IVLEN))
1565 return -EINVAL;
Eric Biggers8efd9722019-02-14 00:03:51 -08001566 if (vec->generates_iv && !enc)
1567 memcpy(iv, vec->iv_out, ivsize);
1568 else if (vec->iv)
Eric Biggers4e7babba2019-01-31 23:51:46 -08001569 memcpy(iv, vec->iv, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001570 else
Eric Biggers4e7babba2019-01-31 23:51:46 -08001571 memset(iv, 0, ivsize);
1572 } else {
1573 if (vec->generates_iv) {
1574 pr_err("alg: skcipher: %s has ivsize=0 but test vector %u generates IV!\n",
1575 driver, vec_num);
1576 return -EINVAL;
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001577 }
Eric Biggers4e7babba2019-01-31 23:51:46 -08001578 iv = NULL;
Herbert Xuda7f0332008-07-31 17:08:25 +08001579 }
1580
Eric Biggers4e7babba2019-01-31 23:51:46 -08001581 /* Build the src/dst scatterlists */
1582 input.iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext;
1583 input.iov_len = vec->len;
1584 err = build_cipher_test_sglists(tsgls, cfg, alignmask,
1585 vec->len, vec->len, &input, 1);
1586 if (err) {
1587 pr_err("alg: skcipher: %s %s: error preparing scatterlists for test vector %u, cfg=\"%s\"\n",
1588 driver, op, vec_num, cfg->name);
1589 return err;
Herbert Xuda7f0332008-07-31 17:08:25 +08001590 }
1591
Eric Biggers4e7babba2019-01-31 23:51:46 -08001592 /* Do the actual encryption or decryption */
1593 testmgr_poison(req->__ctx, crypto_skcipher_reqsize(tfm));
1594 skcipher_request_set_callback(req, req_flags, crypto_req_done, &wait);
1595 skcipher_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr,
1596 vec->len, iv);
1597 err = crypto_wait_req(enc ? crypto_skcipher_encrypt(req) :
1598 crypto_skcipher_decrypt(req), &wait);
1599 if (err) {
1600 pr_err("alg: skcipher: %s %s failed with err %d on test vector %u, cfg=\"%s\"\n",
1601 driver, op, err, vec_num, cfg->name);
1602 return err;
1603 }
Herbert Xuda7f0332008-07-31 17:08:25 +08001604
Eric Biggersfa353c92019-01-31 23:51:49 -08001605 /* Check that the algorithm didn't overwrite things it shouldn't have */
1606 if (req->cryptlen != vec->len ||
1607 req->iv != iv ||
1608 req->src != tsgls->src.sgl_ptr ||
1609 req->dst != tsgls->dst.sgl_ptr ||
1610 crypto_skcipher_reqtfm(req) != tfm ||
1611 req->base.complete != crypto_req_done ||
1612 req->base.flags != req_flags ||
1613 req->base.data != &wait) {
1614 pr_err("alg: skcipher: %s %s corrupted request struct on test vector %u, cfg=\"%s\"\n",
1615 driver, op, vec_num, cfg->name);
1616 if (req->cryptlen != vec->len)
1617 pr_err("alg: skcipher: changed 'req->cryptlen'\n");
1618 if (req->iv != iv)
1619 pr_err("alg: skcipher: changed 'req->iv'\n");
1620 if (req->src != tsgls->src.sgl_ptr)
1621 pr_err("alg: skcipher: changed 'req->src'\n");
1622 if (req->dst != tsgls->dst.sgl_ptr)
1623 pr_err("alg: skcipher: changed 'req->dst'\n");
1624 if (crypto_skcipher_reqtfm(req) != tfm)
1625 pr_err("alg: skcipher: changed 'req->base.tfm'\n");
1626 if (req->base.complete != crypto_req_done)
1627 pr_err("alg: skcipher: changed 'req->base.complete'\n");
1628 if (req->base.flags != req_flags)
1629 pr_err("alg: skcipher: changed 'req->base.flags'\n");
1630 if (req->base.data != &wait)
1631 pr_err("alg: skcipher: changed 'req->base.data'\n");
1632 return -EINVAL;
1633 }
1634 if (is_test_sglist_corrupted(&tsgls->src)) {
1635 pr_err("alg: skcipher: %s %s corrupted src sgl on test vector %u, cfg=\"%s\"\n",
1636 driver, op, vec_num, cfg->name);
1637 return -EINVAL;
1638 }
1639 if (tsgls->dst.sgl_ptr != tsgls->src.sgl &&
1640 is_test_sglist_corrupted(&tsgls->dst)) {
1641 pr_err("alg: skcipher: %s %s corrupted dst sgl on test vector %u, cfg=\"%s\"\n",
1642 driver, op, vec_num, cfg->name);
1643 return -EINVAL;
1644 }
1645
Eric Biggers4e7babba2019-01-31 23:51:46 -08001646 /* Check for the correct output (ciphertext or plaintext) */
1647 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext,
1648 vec->len, 0, true);
1649 if (err == -EOVERFLOW) {
1650 pr_err("alg: skcipher: %s %s overran dst buffer on test vector %u, cfg=\"%s\"\n",
1651 driver, op, vec_num, cfg->name);
1652 return err;
1653 }
1654 if (err) {
1655 pr_err("alg: skcipher: %s %s test failed (wrong result) on test vector %u, cfg=\"%s\"\n",
1656 driver, op, vec_num, cfg->name);
1657 return err;
1658 }
Herbert Xuda7f0332008-07-31 17:08:25 +08001659
Eric Biggers4e7babba2019-01-31 23:51:46 -08001660 /* If applicable, check that the algorithm generated the correct IV */
Eric Biggers8efd9722019-02-14 00:03:51 -08001661 if (vec->iv_out && memcmp(iv, vec->iv_out, ivsize) != 0) {
Eric Biggers4e7babba2019-01-31 23:51:46 -08001662 pr_err("alg: skcipher: %s %s test failed (wrong output IV) on test vector %u, cfg=\"%s\"\n",
1663 driver, op, vec_num, cfg->name);
1664 hexdump(iv, ivsize);
1665 return -EINVAL;
Jussi Kivilinna3a338f22013-06-13 17:37:45 +03001666 }
1667
1668 return 0;
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001669}
1670
Eric Biggers4e7babba2019-01-31 23:51:46 -08001671static int test_skcipher_vec(const char *driver, int enc,
1672 const struct cipher_testvec *vec,
1673 unsigned int vec_num,
1674 struct skcipher_request *req,
1675 struct cipher_test_sglists *tsgls)
1676{
1677 unsigned int i;
1678 int err;
1679
1680 if (fips_enabled && vec->fips_skip)
1681 return 0;
1682
1683 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) {
1684 err = test_skcipher_vec_cfg(driver, enc, vec, vec_num,
1685 &default_cipher_testvec_configs[i],
1686 req, tsgls);
1687 if (err)
1688 return err;
1689 }
1690
1691#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
1692 if (!noextratests) {
1693 struct testvec_config cfg;
1694 char cfgname[TESTVEC_CONFIG_NAMELEN];
1695
1696 for (i = 0; i < fuzz_iterations; i++) {
1697 generate_random_testvec_config(&cfg, cfgname,
1698 sizeof(cfgname));
1699 err = test_skcipher_vec_cfg(driver, enc, vec, vec_num,
1700 &cfg, req, tsgls);
1701 if (err)
1702 return err;
1703 }
1704 }
1705#endif
1706 return 0;
1707}
1708
1709static int test_skcipher(const char *driver, int enc,
1710 const struct cipher_test_suite *suite,
1711 struct skcipher_request *req,
1712 struct cipher_test_sglists *tsgls)
1713{
1714 unsigned int i;
1715 int err;
1716
1717 for (i = 0; i < suite->count; i++) {
1718 err = test_skcipher_vec(driver, enc, &suite->vecs[i], i, req,
1719 tsgls);
1720 if (err)
1721 return err;
1722 }
1723 return 0;
1724}
1725
1726static int alg_test_skcipher(const struct alg_test_desc *desc,
1727 const char *driver, u32 type, u32 mask)
1728{
1729 const struct cipher_test_suite *suite = &desc->suite.cipher;
1730 struct crypto_skcipher *tfm;
1731 struct skcipher_request *req = NULL;
1732 struct cipher_test_sglists *tsgls = NULL;
1733 int err;
1734
1735 if (suite->count <= 0) {
1736 pr_err("alg: skcipher: empty test suite for %s\n", driver);
1737 return -EINVAL;
1738 }
1739
1740 tfm = crypto_alloc_skcipher(driver, type, mask);
1741 if (IS_ERR(tfm)) {
1742 pr_err("alg: skcipher: failed to allocate transform for %s: %ld\n",
1743 driver, PTR_ERR(tfm));
1744 return PTR_ERR(tfm);
1745 }
1746
1747 req = skcipher_request_alloc(tfm, GFP_KERNEL);
1748 if (!req) {
1749 pr_err("alg: skcipher: failed to allocate request for %s\n",
1750 driver);
1751 err = -ENOMEM;
1752 goto out;
1753 }
1754
1755 tsgls = alloc_cipher_test_sglists();
1756 if (!tsgls) {
1757 pr_err("alg: skcipher: failed to allocate test buffers for %s\n",
1758 driver);
1759 err = -ENOMEM;
1760 goto out;
1761 }
1762
1763 err = test_skcipher(driver, ENCRYPT, suite, req, tsgls);
1764 if (err)
1765 goto out;
1766
1767 err = test_skcipher(driver, DECRYPT, suite, req, tsgls);
1768out:
1769 free_cipher_test_sglists(tsgls);
1770 skcipher_request_free(req);
1771 crypto_free_skcipher(tfm);
1772 return err;
1773}
1774
Eric Biggersb13b1e02017-02-24 15:46:59 -08001775static int test_comp(struct crypto_comp *tfm,
1776 const struct comp_testvec *ctemplate,
1777 const struct comp_testvec *dtemplate,
1778 int ctcount, int dtcount)
Herbert Xuda7f0332008-07-31 17:08:25 +08001779{
1780 const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
Mahipal Challa33607382018-04-11 20:28:32 +02001781 char *output, *decomp_output;
Herbert Xuda7f0332008-07-31 17:08:25 +08001782 unsigned int i;
Herbert Xuda7f0332008-07-31 17:08:25 +08001783 int ret;
1784
Mahipal Challa33607382018-04-11 20:28:32 +02001785 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1786 if (!output)
1787 return -ENOMEM;
1788
1789 decomp_output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1790 if (!decomp_output) {
1791 kfree(output);
1792 return -ENOMEM;
1793 }
1794
Herbert Xuda7f0332008-07-31 17:08:25 +08001795 for (i = 0; i < ctcount; i++) {
Geert Uytterhoevenc79cf912009-03-29 15:44:19 +08001796 int ilen;
1797 unsigned int dlen = COMP_BUF_SIZE;
Herbert Xuda7f0332008-07-31 17:08:25 +08001798
Michael Schupikov22a81182018-10-07 13:58:10 +02001799 memset(output, 0, COMP_BUF_SIZE);
1800 memset(decomp_output, 0, COMP_BUF_SIZE);
Herbert Xuda7f0332008-07-31 17:08:25 +08001801
1802 ilen = ctemplate[i].inlen;
1803 ret = crypto_comp_compress(tfm, ctemplate[i].input,
Mahipal Challa33607382018-04-11 20:28:32 +02001804 ilen, output, &dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08001805 if (ret) {
1806 printk(KERN_ERR "alg: comp: compression failed "
1807 "on test %d for %s: ret=%d\n", i + 1, algo,
1808 -ret);
1809 goto out;
1810 }
1811
Mahipal Challa33607382018-04-11 20:28:32 +02001812 ilen = dlen;
1813 dlen = COMP_BUF_SIZE;
1814 ret = crypto_comp_decompress(tfm, output,
1815 ilen, decomp_output, &dlen);
1816 if (ret) {
1817 pr_err("alg: comp: compression failed: decompress: on test %d for %s failed: ret=%d\n",
1818 i + 1, algo, -ret);
1819 goto out;
1820 }
1821
1822 if (dlen != ctemplate[i].inlen) {
Geert Uytterhoevenb812eb02008-11-28 20:51:28 +08001823 printk(KERN_ERR "alg: comp: Compression test %d "
1824 "failed for %s: output len = %d\n", i + 1, algo,
1825 dlen);
1826 ret = -EINVAL;
1827 goto out;
1828 }
1829
Mahipal Challa33607382018-04-11 20:28:32 +02001830 if (memcmp(decomp_output, ctemplate[i].input,
1831 ctemplate[i].inlen)) {
1832 pr_err("alg: comp: compression failed: output differs: on test %d for %s\n",
1833 i + 1, algo);
1834 hexdump(decomp_output, dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08001835 ret = -EINVAL;
1836 goto out;
1837 }
1838 }
1839
1840 for (i = 0; i < dtcount; i++) {
Geert Uytterhoevenc79cf912009-03-29 15:44:19 +08001841 int ilen;
1842 unsigned int dlen = COMP_BUF_SIZE;
Herbert Xuda7f0332008-07-31 17:08:25 +08001843
Michael Schupikov22a81182018-10-07 13:58:10 +02001844 memset(decomp_output, 0, COMP_BUF_SIZE);
Herbert Xuda7f0332008-07-31 17:08:25 +08001845
1846 ilen = dtemplate[i].inlen;
1847 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
Mahipal Challa33607382018-04-11 20:28:32 +02001848 ilen, decomp_output, &dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08001849 if (ret) {
1850 printk(KERN_ERR "alg: comp: decompression failed "
1851 "on test %d for %s: ret=%d\n", i + 1, algo,
1852 -ret);
1853 goto out;
1854 }
1855
Geert Uytterhoevenb812eb02008-11-28 20:51:28 +08001856 if (dlen != dtemplate[i].outlen) {
1857 printk(KERN_ERR "alg: comp: Decompression test %d "
1858 "failed for %s: output len = %d\n", i + 1, algo,
1859 dlen);
1860 ret = -EINVAL;
1861 goto out;
1862 }
1863
Mahipal Challa33607382018-04-11 20:28:32 +02001864 if (memcmp(decomp_output, dtemplate[i].output, dlen)) {
Herbert Xuda7f0332008-07-31 17:08:25 +08001865 printk(KERN_ERR "alg: comp: Decompression test %d "
1866 "failed for %s\n", i + 1, algo);
Mahipal Challa33607382018-04-11 20:28:32 +02001867 hexdump(decomp_output, dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08001868 ret = -EINVAL;
1869 goto out;
1870 }
1871 }
1872
1873 ret = 0;
1874
1875out:
Mahipal Challa33607382018-04-11 20:28:32 +02001876 kfree(decomp_output);
1877 kfree(output);
Herbert Xuda7f0332008-07-31 17:08:25 +08001878 return ret;
1879}
1880
Eric Biggersb13b1e02017-02-24 15:46:59 -08001881static int test_acomp(struct crypto_acomp *tfm,
Mahipal Challa33607382018-04-11 20:28:32 +02001882 const struct comp_testvec *ctemplate,
Eric Biggersb13b1e02017-02-24 15:46:59 -08001883 const struct comp_testvec *dtemplate,
1884 int ctcount, int dtcount)
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001885{
1886 const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
1887 unsigned int i;
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01001888 char *output, *decomp_out;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001889 int ret;
1890 struct scatterlist src, dst;
1891 struct acomp_req *req;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001892 struct crypto_wait wait;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001893
Eric Biggerseb095592016-11-23 10:24:35 -08001894 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1895 if (!output)
1896 return -ENOMEM;
1897
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01001898 decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1899 if (!decomp_out) {
1900 kfree(output);
1901 return -ENOMEM;
1902 }
1903
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001904 for (i = 0; i < ctcount; i++) {
1905 unsigned int dlen = COMP_BUF_SIZE;
1906 int ilen = ctemplate[i].inlen;
Laura Abbott02608e02016-12-21 12:32:54 -08001907 void *input_vec;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001908
Eric Biggersd2110222016-12-30 14:12:00 -06001909 input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL);
Laura Abbott02608e02016-12-21 12:32:54 -08001910 if (!input_vec) {
1911 ret = -ENOMEM;
1912 goto out;
1913 }
1914
Eric Biggerseb095592016-11-23 10:24:35 -08001915 memset(output, 0, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001916 crypto_init_wait(&wait);
Laura Abbott02608e02016-12-21 12:32:54 -08001917 sg_init_one(&src, input_vec, ilen);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001918 sg_init_one(&dst, output, dlen);
1919
1920 req = acomp_request_alloc(tfm);
1921 if (!req) {
1922 pr_err("alg: acomp: request alloc failed for %s\n",
1923 algo);
Laura Abbott02608e02016-12-21 12:32:54 -08001924 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001925 ret = -ENOMEM;
1926 goto out;
1927 }
1928
1929 acomp_request_set_params(req, &src, &dst, ilen, dlen);
1930 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001931 crypto_req_done, &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001932
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001933 ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001934 if (ret) {
1935 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
1936 i + 1, algo, -ret);
Laura Abbott02608e02016-12-21 12:32:54 -08001937 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001938 acomp_request_free(req);
1939 goto out;
1940 }
1941
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01001942 ilen = req->dlen;
1943 dlen = COMP_BUF_SIZE;
1944 sg_init_one(&src, output, ilen);
1945 sg_init_one(&dst, decomp_out, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001946 crypto_init_wait(&wait);
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01001947 acomp_request_set_params(req, &src, &dst, ilen, dlen);
1948
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001949 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01001950 if (ret) {
1951 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
1952 i + 1, algo, -ret);
1953 kfree(input_vec);
1954 acomp_request_free(req);
1955 goto out;
1956 }
1957
1958 if (req->dlen != ctemplate[i].inlen) {
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001959 pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
1960 i + 1, algo, req->dlen);
1961 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08001962 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001963 acomp_request_free(req);
1964 goto out;
1965 }
1966
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01001967 if (memcmp(input_vec, decomp_out, req->dlen)) {
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001968 pr_err("alg: acomp: Compression test %d failed for %s\n",
1969 i + 1, algo);
1970 hexdump(output, req->dlen);
1971 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08001972 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001973 acomp_request_free(req);
1974 goto out;
1975 }
1976
Laura Abbott02608e02016-12-21 12:32:54 -08001977 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001978 acomp_request_free(req);
1979 }
1980
1981 for (i = 0; i < dtcount; i++) {
1982 unsigned int dlen = COMP_BUF_SIZE;
1983 int ilen = dtemplate[i].inlen;
Laura Abbott02608e02016-12-21 12:32:54 -08001984 void *input_vec;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001985
Eric Biggersd2110222016-12-30 14:12:00 -06001986 input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL);
Laura Abbott02608e02016-12-21 12:32:54 -08001987 if (!input_vec) {
1988 ret = -ENOMEM;
1989 goto out;
1990 }
1991
Eric Biggerseb095592016-11-23 10:24:35 -08001992 memset(output, 0, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001993 crypto_init_wait(&wait);
Laura Abbott02608e02016-12-21 12:32:54 -08001994 sg_init_one(&src, input_vec, ilen);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001995 sg_init_one(&dst, output, dlen);
1996
1997 req = acomp_request_alloc(tfm);
1998 if (!req) {
1999 pr_err("alg: acomp: request alloc failed for %s\n",
2000 algo);
Laura Abbott02608e02016-12-21 12:32:54 -08002001 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002002 ret = -ENOMEM;
2003 goto out;
2004 }
2005
2006 acomp_request_set_params(req, &src, &dst, ilen, dlen);
2007 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002008 crypto_req_done, &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002009
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002010 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002011 if (ret) {
2012 pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
2013 i + 1, algo, -ret);
Laura Abbott02608e02016-12-21 12:32:54 -08002014 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002015 acomp_request_free(req);
2016 goto out;
2017 }
2018
2019 if (req->dlen != dtemplate[i].outlen) {
2020 pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
2021 i + 1, algo, req->dlen);
2022 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08002023 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002024 acomp_request_free(req);
2025 goto out;
2026 }
2027
2028 if (memcmp(output, dtemplate[i].output, req->dlen)) {
2029 pr_err("alg: acomp: Decompression test %d failed for %s\n",
2030 i + 1, algo);
2031 hexdump(output, req->dlen);
2032 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08002033 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002034 acomp_request_free(req);
2035 goto out;
2036 }
2037
Laura Abbott02608e02016-12-21 12:32:54 -08002038 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002039 acomp_request_free(req);
2040 }
2041
2042 ret = 0;
2043
2044out:
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002045 kfree(decomp_out);
Eric Biggerseb095592016-11-23 10:24:35 -08002046 kfree(output);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002047 return ret;
2048}
2049
Eric Biggersb13b1e02017-02-24 15:46:59 -08002050static int test_cprng(struct crypto_rng *tfm,
2051 const struct cprng_testvec *template,
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002052 unsigned int tcount)
2053{
2054 const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
Felipe Contrerasfa4ef8a2009-10-27 19:04:42 +08002055 int err = 0, i, j, seedsize;
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002056 u8 *seed;
2057 char result[32];
2058
2059 seedsize = crypto_rng_seedsize(tfm);
2060
2061 seed = kmalloc(seedsize, GFP_KERNEL);
2062 if (!seed) {
2063 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
2064 "for %s\n", algo);
2065 return -ENOMEM;
2066 }
2067
2068 for (i = 0; i < tcount; i++) {
2069 memset(result, 0, 32);
2070
2071 memcpy(seed, template[i].v, template[i].vlen);
2072 memcpy(seed + template[i].vlen, template[i].key,
2073 template[i].klen);
2074 memcpy(seed + template[i].vlen + template[i].klen,
2075 template[i].dt, template[i].dtlen);
2076
2077 err = crypto_rng_reset(tfm, seed, seedsize);
2078 if (err) {
2079 printk(KERN_ERR "alg: cprng: Failed to reset rng "
2080 "for %s\n", algo);
2081 goto out;
2082 }
2083
2084 for (j = 0; j < template[i].loops; j++) {
2085 err = crypto_rng_get_bytes(tfm, result,
2086 template[i].rlen);
Stephan Mueller19e60e12015-03-10 17:00:36 +01002087 if (err < 0) {
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002088 printk(KERN_ERR "alg: cprng: Failed to obtain "
2089 "the correct amount of random data for "
Stephan Mueller19e60e12015-03-10 17:00:36 +01002090 "%s (requested %d)\n", algo,
2091 template[i].rlen);
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002092 goto out;
2093 }
2094 }
2095
2096 err = memcmp(result, template[i].result,
2097 template[i].rlen);
2098 if (err) {
2099 printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
2100 i, algo);
2101 hexdump(result, template[i].rlen);
2102 err = -EINVAL;
2103 goto out;
2104 }
2105 }
2106
2107out:
2108 kfree(seed);
2109 return err;
2110}
2111
Herbert Xuda7f0332008-07-31 17:08:25 +08002112static int alg_test_cipher(const struct alg_test_desc *desc,
2113 const char *driver, u32 type, u32 mask)
2114{
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002115 const struct cipher_test_suite *suite = &desc->suite.cipher;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002116 struct crypto_cipher *tfm;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002117 int err;
Herbert Xuda7f0332008-07-31 17:08:25 +08002118
Herbert Xueed93e02016-11-22 20:08:31 +08002119 tfm = crypto_alloc_cipher(driver, type, mask);
Herbert Xuda7f0332008-07-31 17:08:25 +08002120 if (IS_ERR(tfm)) {
2121 printk(KERN_ERR "alg: cipher: Failed to load transform for "
2122 "%s: %ld\n", driver, PTR_ERR(tfm));
2123 return PTR_ERR(tfm);
2124 }
2125
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002126 err = test_cipher(tfm, ENCRYPT, suite->vecs, suite->count);
2127 if (!err)
2128 err = test_cipher(tfm, DECRYPT, suite->vecs, suite->count);
Herbert Xuda7f0332008-07-31 17:08:25 +08002129
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002130 crypto_free_cipher(tfm);
2131 return err;
2132}
2133
Herbert Xuda7f0332008-07-31 17:08:25 +08002134static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
2135 u32 type, u32 mask)
2136{
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002137 struct crypto_comp *comp;
2138 struct crypto_acomp *acomp;
Herbert Xuda7f0332008-07-31 17:08:25 +08002139 int err;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002140 u32 algo_type = type & CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
Herbert Xuda7f0332008-07-31 17:08:25 +08002141
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002142 if (algo_type == CRYPTO_ALG_TYPE_ACOMPRESS) {
2143 acomp = crypto_alloc_acomp(driver, type, mask);
2144 if (IS_ERR(acomp)) {
2145 pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
2146 driver, PTR_ERR(acomp));
2147 return PTR_ERR(acomp);
2148 }
2149 err = test_acomp(acomp, desc->suite.comp.comp.vecs,
2150 desc->suite.comp.decomp.vecs,
2151 desc->suite.comp.comp.count,
2152 desc->suite.comp.decomp.count);
2153 crypto_free_acomp(acomp);
2154 } else {
2155 comp = crypto_alloc_comp(driver, type, mask);
2156 if (IS_ERR(comp)) {
2157 pr_err("alg: comp: Failed to load transform for %s: %ld\n",
2158 driver, PTR_ERR(comp));
2159 return PTR_ERR(comp);
2160 }
2161
2162 err = test_comp(comp, desc->suite.comp.comp.vecs,
2163 desc->suite.comp.decomp.vecs,
2164 desc->suite.comp.comp.count,
2165 desc->suite.comp.decomp.count);
2166
2167 crypto_free_comp(comp);
Herbert Xuda7f0332008-07-31 17:08:25 +08002168 }
Herbert Xuda7f0332008-07-31 17:08:25 +08002169 return err;
2170}
2171
Herbert Xu8e3ee852008-11-07 14:58:52 +08002172static int alg_test_crc32c(const struct alg_test_desc *desc,
2173 const char *driver, u32 type, u32 mask)
2174{
2175 struct crypto_shash *tfm;
Eric Biggerscb9dde82019-01-10 12:17:55 -08002176 __le32 val;
Herbert Xu8e3ee852008-11-07 14:58:52 +08002177 int err;
2178
2179 err = alg_test_hash(desc, driver, type, mask);
2180 if (err)
Eric Biggerseb5e6732019-01-23 20:57:35 -08002181 return err;
Herbert Xu8e3ee852008-11-07 14:58:52 +08002182
Herbert Xueed93e02016-11-22 20:08:31 +08002183 tfm = crypto_alloc_shash(driver, type, mask);
Herbert Xu8e3ee852008-11-07 14:58:52 +08002184 if (IS_ERR(tfm)) {
Eric Biggerseb5e6732019-01-23 20:57:35 -08002185 if (PTR_ERR(tfm) == -ENOENT) {
2186 /*
2187 * This crc32c implementation is only available through
2188 * ahash API, not the shash API, so the remaining part
2189 * of the test is not applicable to it.
2190 */
2191 return 0;
2192 }
Herbert Xu8e3ee852008-11-07 14:58:52 +08002193 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
2194 "%ld\n", driver, PTR_ERR(tfm));
Eric Biggerseb5e6732019-01-23 20:57:35 -08002195 return PTR_ERR(tfm);
Herbert Xu8e3ee852008-11-07 14:58:52 +08002196 }
2197
2198 do {
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02002199 SHASH_DESC_ON_STACK(shash, tfm);
2200 u32 *ctx = (u32 *)shash_desc_ctx(shash);
Herbert Xu8e3ee852008-11-07 14:58:52 +08002201
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02002202 shash->tfm = tfm;
2203 shash->flags = 0;
Herbert Xu8e3ee852008-11-07 14:58:52 +08002204
Eric Biggerscb9dde82019-01-10 12:17:55 -08002205 *ctx = 420553207;
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02002206 err = crypto_shash_final(shash, (u8 *)&val);
Herbert Xu8e3ee852008-11-07 14:58:52 +08002207 if (err) {
2208 printk(KERN_ERR "alg: crc32c: Operation failed for "
2209 "%s: %d\n", driver, err);
2210 break;
2211 }
2212
Eric Biggerscb9dde82019-01-10 12:17:55 -08002213 if (val != cpu_to_le32(~420553207)) {
2214 pr_err("alg: crc32c: Test failed for %s: %u\n",
2215 driver, le32_to_cpu(val));
Herbert Xu8e3ee852008-11-07 14:58:52 +08002216 err = -EINVAL;
2217 }
2218 } while (0);
2219
2220 crypto_free_shash(tfm);
2221
Herbert Xu8e3ee852008-11-07 14:58:52 +08002222 return err;
2223}
2224
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002225static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
2226 u32 type, u32 mask)
2227{
2228 struct crypto_rng *rng;
2229 int err;
2230
Herbert Xueed93e02016-11-22 20:08:31 +08002231 rng = crypto_alloc_rng(driver, type, mask);
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002232 if (IS_ERR(rng)) {
2233 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
2234 "%ld\n", driver, PTR_ERR(rng));
2235 return PTR_ERR(rng);
2236 }
2237
2238 err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
2239
2240 crypto_free_rng(rng);
2241
2242 return err;
2243}
2244
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002245
Eric Biggersb13b1e02017-02-24 15:46:59 -08002246static int drbg_cavs_test(const struct drbg_testvec *test, int pr,
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002247 const char *driver, u32 type, u32 mask)
2248{
2249 int ret = -EAGAIN;
2250 struct crypto_rng *drng;
2251 struct drbg_test_data test_data;
2252 struct drbg_string addtl, pers, testentropy;
2253 unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
2254
2255 if (!buf)
2256 return -ENOMEM;
2257
Herbert Xueed93e02016-11-22 20:08:31 +08002258 drng = crypto_alloc_rng(driver, type, mask);
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002259 if (IS_ERR(drng)) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04002260 printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002261 "%s\n", driver);
2262 kzfree(buf);
2263 return -ENOMEM;
2264 }
2265
2266 test_data.testentropy = &testentropy;
2267 drbg_string_fill(&testentropy, test->entropy, test->entropylen);
2268 drbg_string_fill(&pers, test->pers, test->perslen);
2269 ret = crypto_drbg_reset_test(drng, &pers, &test_data);
2270 if (ret) {
2271 printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
2272 goto outbuf;
2273 }
2274
2275 drbg_string_fill(&addtl, test->addtla, test->addtllen);
2276 if (pr) {
2277 drbg_string_fill(&testentropy, test->entpra, test->entprlen);
2278 ret = crypto_drbg_get_bytes_addtl_test(drng,
2279 buf, test->expectedlen, &addtl, &test_data);
2280 } else {
2281 ret = crypto_drbg_get_bytes_addtl(drng,
2282 buf, test->expectedlen, &addtl);
2283 }
Stephan Mueller19e60e12015-03-10 17:00:36 +01002284 if (ret < 0) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04002285 printk(KERN_ERR "alg: drbg: could not obtain random data for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002286 "driver %s\n", driver);
2287 goto outbuf;
2288 }
2289
2290 drbg_string_fill(&addtl, test->addtlb, test->addtllen);
2291 if (pr) {
2292 drbg_string_fill(&testentropy, test->entprb, test->entprlen);
2293 ret = crypto_drbg_get_bytes_addtl_test(drng,
2294 buf, test->expectedlen, &addtl, &test_data);
2295 } else {
2296 ret = crypto_drbg_get_bytes_addtl(drng,
2297 buf, test->expectedlen, &addtl);
2298 }
Stephan Mueller19e60e12015-03-10 17:00:36 +01002299 if (ret < 0) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04002300 printk(KERN_ERR "alg: drbg: could not obtain random data for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002301 "driver %s\n", driver);
2302 goto outbuf;
2303 }
2304
2305 ret = memcmp(test->expected, buf, test->expectedlen);
2306
2307outbuf:
2308 crypto_free_rng(drng);
2309 kzfree(buf);
2310 return ret;
2311}
2312
2313
2314static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
2315 u32 type, u32 mask)
2316{
2317 int err = 0;
2318 int pr = 0;
2319 int i = 0;
Eric Biggersb13b1e02017-02-24 15:46:59 -08002320 const struct drbg_testvec *template = desc->suite.drbg.vecs;
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002321 unsigned int tcount = desc->suite.drbg.count;
2322
2323 if (0 == memcmp(driver, "drbg_pr_", 8))
2324 pr = 1;
2325
2326 for (i = 0; i < tcount; i++) {
2327 err = drbg_cavs_test(&template[i], pr, driver, type, mask);
2328 if (err) {
2329 printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
2330 i, driver);
2331 err = -EINVAL;
2332 break;
2333 }
2334 }
2335 return err;
2336
2337}
2338
Eric Biggersb13b1e02017-02-24 15:46:59 -08002339static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002340 const char *alg)
2341{
2342 struct kpp_request *req;
2343 void *input_buf = NULL;
2344 void *output_buf = NULL;
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002345 void *a_public = NULL;
2346 void *a_ss = NULL;
2347 void *shared_secret = NULL;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002348 struct crypto_wait wait;
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002349 unsigned int out_len_max;
2350 int err = -ENOMEM;
2351 struct scatterlist src, dst;
2352
2353 req = kpp_request_alloc(tfm, GFP_KERNEL);
2354 if (!req)
2355 return err;
2356
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002357 crypto_init_wait(&wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002358
2359 err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
2360 if (err < 0)
2361 goto free_req;
2362
2363 out_len_max = crypto_kpp_maxsize(tfm);
2364 output_buf = kzalloc(out_len_max, GFP_KERNEL);
2365 if (!output_buf) {
2366 err = -ENOMEM;
2367 goto free_req;
2368 }
2369
2370 /* Use appropriate parameter as base */
2371 kpp_request_set_input(req, NULL, 0);
2372 sg_init_one(&dst, output_buf, out_len_max);
2373 kpp_request_set_output(req, &dst, out_len_max);
2374 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002375 crypto_req_done, &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002376
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002377 /* Compute party A's public key */
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002378 err = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002379 if (err) {
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002380 pr_err("alg: %s: Party A: generate public key test failed. err %d\n",
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002381 alg, err);
2382 goto free_output;
2383 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002384
2385 if (vec->genkey) {
2386 /* Save party A's public key */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05002387 a_public = kmemdup(sg_virt(req->dst), out_len_max, GFP_KERNEL);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002388 if (!a_public) {
2389 err = -ENOMEM;
2390 goto free_output;
2391 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002392 } else {
2393 /* Verify calculated public key */
2394 if (memcmp(vec->expected_a_public, sg_virt(req->dst),
2395 vec->expected_a_public_size)) {
2396 pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n",
2397 alg);
2398 err = -EINVAL;
2399 goto free_output;
2400 }
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002401 }
2402
2403 /* Calculate shared secret key by using counter part (b) public key. */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05002404 input_buf = kmemdup(vec->b_public, vec->b_public_size, GFP_KERNEL);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002405 if (!input_buf) {
2406 err = -ENOMEM;
2407 goto free_output;
2408 }
2409
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002410 sg_init_one(&src, input_buf, vec->b_public_size);
2411 sg_init_one(&dst, output_buf, out_len_max);
2412 kpp_request_set_input(req, &src, vec->b_public_size);
2413 kpp_request_set_output(req, &dst, out_len_max);
2414 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002415 crypto_req_done, &wait);
2416 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002417 if (err) {
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002418 pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n",
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002419 alg, err);
2420 goto free_all;
2421 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002422
2423 if (vec->genkey) {
2424 /* Save the shared secret obtained by party A */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05002425 a_ss = kmemdup(sg_virt(req->dst), vec->expected_ss_size, GFP_KERNEL);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002426 if (!a_ss) {
2427 err = -ENOMEM;
2428 goto free_all;
2429 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002430
2431 /*
2432 * Calculate party B's shared secret by using party A's
2433 * public key.
2434 */
2435 err = crypto_kpp_set_secret(tfm, vec->b_secret,
2436 vec->b_secret_size);
2437 if (err < 0)
2438 goto free_all;
2439
2440 sg_init_one(&src, a_public, vec->expected_a_public_size);
2441 sg_init_one(&dst, output_buf, out_len_max);
2442 kpp_request_set_input(req, &src, vec->expected_a_public_size);
2443 kpp_request_set_output(req, &dst, out_len_max);
2444 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002445 crypto_req_done, &wait);
2446 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req),
2447 &wait);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002448 if (err) {
2449 pr_err("alg: %s: Party B: compute shared secret failed. err %d\n",
2450 alg, err);
2451 goto free_all;
2452 }
2453
2454 shared_secret = a_ss;
2455 } else {
2456 shared_secret = (void *)vec->expected_ss;
2457 }
2458
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002459 /*
2460 * verify shared secret from which the user will derive
2461 * secret key by executing whatever hash it has chosen
2462 */
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002463 if (memcmp(shared_secret, sg_virt(req->dst),
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002464 vec->expected_ss_size)) {
2465 pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
2466 alg);
2467 err = -EINVAL;
2468 }
2469
2470free_all:
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002471 kfree(a_ss);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002472 kfree(input_buf);
2473free_output:
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002474 kfree(a_public);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002475 kfree(output_buf);
2476free_req:
2477 kpp_request_free(req);
2478 return err;
2479}
2480
2481static int test_kpp(struct crypto_kpp *tfm, const char *alg,
Eric Biggersb13b1e02017-02-24 15:46:59 -08002482 const struct kpp_testvec *vecs, unsigned int tcount)
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002483{
2484 int ret, i;
2485
2486 for (i = 0; i < tcount; i++) {
2487 ret = do_test_kpp(tfm, vecs++, alg);
2488 if (ret) {
2489 pr_err("alg: %s: test failed on vector %d, err=%d\n",
2490 alg, i + 1, ret);
2491 return ret;
2492 }
2493 }
2494 return 0;
2495}
2496
2497static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
2498 u32 type, u32 mask)
2499{
2500 struct crypto_kpp *tfm;
2501 int err = 0;
2502
Herbert Xueed93e02016-11-22 20:08:31 +08002503 tfm = crypto_alloc_kpp(driver, type, mask);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002504 if (IS_ERR(tfm)) {
2505 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
2506 driver, PTR_ERR(tfm));
2507 return PTR_ERR(tfm);
2508 }
2509 if (desc->suite.kpp.vecs)
2510 err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
2511 desc->suite.kpp.count);
2512
2513 crypto_free_kpp(tfm);
2514 return err;
2515}
2516
Herbert Xu50d2b6432016-06-29 19:32:20 +08002517static int test_akcipher_one(struct crypto_akcipher *tfm,
Eric Biggersb13b1e02017-02-24 15:46:59 -08002518 const struct akcipher_testvec *vecs)
Tadeusz Struk946cc462015-06-16 10:31:06 -07002519{
Herbert Xudf27b262016-05-05 16:42:49 +08002520 char *xbuf[XBUFSIZE];
Tadeusz Struk946cc462015-06-16 10:31:06 -07002521 struct akcipher_request *req;
2522 void *outbuf_enc = NULL;
2523 void *outbuf_dec = NULL;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002524 struct crypto_wait wait;
Tadeusz Struk946cc462015-06-16 10:31:06 -07002525 unsigned int out_len_max, out_len = 0;
2526 int err = -ENOMEM;
Tadeusz Struk22287b02015-10-08 09:26:55 -07002527 struct scatterlist src, dst, src_tab[2];
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002528 const char *m, *c;
2529 unsigned int m_size, c_size;
2530 const char *op;
Tadeusz Struk946cc462015-06-16 10:31:06 -07002531
Herbert Xudf27b262016-05-05 16:42:49 +08002532 if (testmgr_alloc_buf(xbuf))
2533 return err;
2534
Tadeusz Struk946cc462015-06-16 10:31:06 -07002535 req = akcipher_request_alloc(tfm, GFP_KERNEL);
2536 if (!req)
Herbert Xudf27b262016-05-05 16:42:49 +08002537 goto free_xbuf;
Tadeusz Struk946cc462015-06-16 10:31:06 -07002538
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002539 crypto_init_wait(&wait);
Tadeusz Struk22287b02015-10-08 09:26:55 -07002540
2541 if (vecs->public_key_vec)
2542 err = crypto_akcipher_set_pub_key(tfm, vecs->key,
2543 vecs->key_len);
2544 else
2545 err = crypto_akcipher_set_priv_key(tfm, vecs->key,
2546 vecs->key_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002547 if (err)
2548 goto free_req;
2549
Salvatore Benedetto57763f52016-07-04 10:52:34 +01002550 err = -ENOMEM;
Tadeusz Struk22287b02015-10-08 09:26:55 -07002551 out_len_max = crypto_akcipher_maxsize(tfm);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002552
2553 /*
2554 * First run test which do not require a private key, such as
2555 * encrypt or verify.
2556 */
Tadeusz Struk946cc462015-06-16 10:31:06 -07002557 outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
2558 if (!outbuf_enc)
2559 goto free_req;
2560
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002561 if (!vecs->siggen_sigver_test) {
2562 m = vecs->m;
2563 m_size = vecs->m_size;
2564 c = vecs->c;
2565 c_size = vecs->c_size;
2566 op = "encrypt";
2567 } else {
2568 /* Swap args so we could keep plaintext (digest)
2569 * in vecs->m, and cooked signature in vecs->c.
2570 */
2571 m = vecs->c; /* signature */
2572 m_size = vecs->c_size;
2573 c = vecs->m; /* digest */
2574 c_size = vecs->m_size;
2575 op = "verify";
2576 }
Herbert Xudf27b262016-05-05 16:42:49 +08002577
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002578 if (WARN_ON(m_size > PAGE_SIZE))
2579 goto free_all;
2580 memcpy(xbuf[0], m, m_size);
Herbert Xudf27b262016-05-05 16:42:49 +08002581
Tadeusz Struk22287b02015-10-08 09:26:55 -07002582 sg_init_table(src_tab, 2);
Herbert Xudf27b262016-05-05 16:42:49 +08002583 sg_set_buf(&src_tab[0], xbuf[0], 8);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002584 sg_set_buf(&src_tab[1], xbuf[0] + 8, m_size - 8);
Tadeusz Struk22287b02015-10-08 09:26:55 -07002585 sg_init_one(&dst, outbuf_enc, out_len_max);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002586 akcipher_request_set_crypt(req, src_tab, &dst, m_size,
Tadeusz Struk22287b02015-10-08 09:26:55 -07002587 out_len_max);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002588 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002589 crypto_req_done, &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002590
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002591 err = crypto_wait_req(vecs->siggen_sigver_test ?
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002592 /* Run asymmetric signature verification */
2593 crypto_akcipher_verify(req) :
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002594 /* Run asymmetric encrypt */
2595 crypto_akcipher_encrypt(req), &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002596 if (err) {
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002597 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002598 goto free_all;
2599 }
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002600 if (req->dst_len != c_size) {
2601 pr_err("alg: akcipher: %s test failed. Invalid output len\n",
2602 op);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002603 err = -EINVAL;
2604 goto free_all;
2605 }
2606 /* verify that encrypted message is equal to expected */
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002607 if (memcmp(c, outbuf_enc, c_size)) {
2608 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
2609 hexdump(outbuf_enc, c_size);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002610 err = -EINVAL;
2611 goto free_all;
2612 }
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002613
2614 /*
2615 * Don't invoke (decrypt or sign) test which require a private key
2616 * for vectors with only a public key.
2617 */
Tadeusz Struk946cc462015-06-16 10:31:06 -07002618 if (vecs->public_key_vec) {
2619 err = 0;
2620 goto free_all;
2621 }
2622 outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
2623 if (!outbuf_dec) {
2624 err = -ENOMEM;
2625 goto free_all;
2626 }
Herbert Xudf27b262016-05-05 16:42:49 +08002627
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002628 op = vecs->siggen_sigver_test ? "sign" : "decrypt";
2629 if (WARN_ON(c_size > PAGE_SIZE))
Herbert Xudf27b262016-05-05 16:42:49 +08002630 goto free_all;
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002631 memcpy(xbuf[0], c, c_size);
Herbert Xudf27b262016-05-05 16:42:49 +08002632
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002633 sg_init_one(&src, xbuf[0], c_size);
Tadeusz Struk22287b02015-10-08 09:26:55 -07002634 sg_init_one(&dst, outbuf_dec, out_len_max);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002635 crypto_init_wait(&wait);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002636 akcipher_request_set_crypt(req, &src, &dst, c_size, out_len_max);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002637
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002638 err = crypto_wait_req(vecs->siggen_sigver_test ?
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002639 /* Run asymmetric signature generation */
2640 crypto_akcipher_sign(req) :
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002641 /* Run asymmetric decrypt */
2642 crypto_akcipher_decrypt(req), &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002643 if (err) {
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002644 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002645 goto free_all;
2646 }
2647 out_len = req->dst_len;
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002648 if (out_len < m_size) {
2649 pr_err("alg: akcipher: %s test failed. Invalid output len %u\n",
2650 op, out_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002651 err = -EINVAL;
2652 goto free_all;
2653 }
2654 /* verify that decrypted message is equal to the original msg */
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002655 if (memchr_inv(outbuf_dec, 0, out_len - m_size) ||
2656 memcmp(m, outbuf_dec + out_len - m_size, m_size)) {
2657 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
Herbert Xu50d2b6432016-06-29 19:32:20 +08002658 hexdump(outbuf_dec, out_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002659 err = -EINVAL;
2660 }
2661free_all:
2662 kfree(outbuf_dec);
2663 kfree(outbuf_enc);
2664free_req:
2665 akcipher_request_free(req);
Herbert Xudf27b262016-05-05 16:42:49 +08002666free_xbuf:
2667 testmgr_free_buf(xbuf);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002668 return err;
2669}
2670
Herbert Xu50d2b6432016-06-29 19:32:20 +08002671static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
Eric Biggersb13b1e02017-02-24 15:46:59 -08002672 const struct akcipher_testvec *vecs,
2673 unsigned int tcount)
Tadeusz Struk946cc462015-06-16 10:31:06 -07002674{
Herbert Xu15226e42016-07-18 18:20:10 +08002675 const char *algo =
2676 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
Tadeusz Struk946cc462015-06-16 10:31:06 -07002677 int ret, i;
2678
2679 for (i = 0; i < tcount; i++) {
Herbert Xu50d2b6432016-06-29 19:32:20 +08002680 ret = test_akcipher_one(tfm, vecs++);
2681 if (!ret)
2682 continue;
2683
Herbert Xu15226e42016-07-18 18:20:10 +08002684 pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
2685 i + 1, algo, ret);
Herbert Xu50d2b6432016-06-29 19:32:20 +08002686 return ret;
Tadeusz Struk946cc462015-06-16 10:31:06 -07002687 }
2688 return 0;
2689}
2690
Tadeusz Struk946cc462015-06-16 10:31:06 -07002691static int alg_test_akcipher(const struct alg_test_desc *desc,
2692 const char *driver, u32 type, u32 mask)
2693{
2694 struct crypto_akcipher *tfm;
2695 int err = 0;
2696
Herbert Xueed93e02016-11-22 20:08:31 +08002697 tfm = crypto_alloc_akcipher(driver, type, mask);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002698 if (IS_ERR(tfm)) {
2699 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
2700 driver, PTR_ERR(tfm));
2701 return PTR_ERR(tfm);
2702 }
2703 if (desc->suite.akcipher.vecs)
2704 err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
2705 desc->suite.akcipher.count);
2706
2707 crypto_free_akcipher(tfm);
2708 return err;
2709}
2710
Youquan, Song863b5572009-12-23 19:45:20 +08002711static int alg_test_null(const struct alg_test_desc *desc,
2712 const char *driver, u32 type, u32 mask)
2713{
2714 return 0;
2715}
2716
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002717#define __VECS(tv) { .vecs = tv, .count = ARRAY_SIZE(tv) }
2718
Herbert Xuda7f0332008-07-31 17:08:25 +08002719/* Please keep this list sorted by algorithm name. */
2720static const struct alg_test_desc alg_test_descs[] = {
2721 {
Eric Biggers059c2a42018-11-16 17:26:31 -08002722 .alg = "adiantum(xchacha12,aes)",
2723 .test = alg_test_skcipher,
2724 .suite = {
2725 .cipher = __VECS(adiantum_xchacha12_aes_tv_template)
2726 },
2727 }, {
2728 .alg = "adiantum(xchacha20,aes)",
2729 .test = alg_test_skcipher,
2730 .suite = {
2731 .cipher = __VECS(adiantum_xchacha20_aes_tv_template)
2732 },
2733 }, {
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02002734 .alg = "aegis128",
2735 .test = alg_test_aead,
2736 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002737 .aead = __VECS(aegis128_tv_template)
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02002738 }
2739 }, {
2740 .alg = "aegis128l",
2741 .test = alg_test_aead,
2742 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002743 .aead = __VECS(aegis128l_tv_template)
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02002744 }
2745 }, {
2746 .alg = "aegis256",
2747 .test = alg_test_aead,
2748 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002749 .aead = __VECS(aegis256_tv_template)
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02002750 }
2751 }, {
Jarod Wilsone08ca2d2009-05-04 19:46:29 +08002752 .alg = "ansi_cprng",
2753 .test = alg_test_cprng,
2754 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002755 .cprng = __VECS(ansi_cprng_aes_tv_template)
Jarod Wilsone08ca2d2009-05-04 19:46:29 +08002756 }
2757 }, {
Horia Geantabca4feb2014-03-14 17:46:51 +02002758 .alg = "authenc(hmac(md5),ecb(cipher_null))",
2759 .test = alg_test_aead,
Horia Geantabca4feb2014-03-14 17:46:51 +02002760 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002761 .aead = __VECS(hmac_md5_ecb_cipher_null_tv_template)
Horia Geantabca4feb2014-03-14 17:46:51 +02002762 }
2763 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002764 .alg = "authenc(hmac(sha1),cbc(aes))",
Horia Geantae46e9a42012-07-03 19:16:54 +03002765 .test = alg_test_aead,
Herbert Xubcf741c2017-06-28 19:09:07 +08002766 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03002767 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002768 .aead = __VECS(hmac_sha1_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302769 }
2770 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002771 .alg = "authenc(hmac(sha1),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302772 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302773 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002774 .aead = __VECS(hmac_sha1_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302775 }
2776 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002777 .alg = "authenc(hmac(sha1),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302778 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01002779 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302780 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002781 .aead = __VECS(hmac_sha1_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03002782 }
2783 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01002784 .alg = "authenc(hmac(sha1),ctr(aes))",
2785 .test = alg_test_null,
2786 .fips_allowed = 1,
2787 }, {
Horia Geantabca4feb2014-03-14 17:46:51 +02002788 .alg = "authenc(hmac(sha1),ecb(cipher_null))",
2789 .test = alg_test_aead,
Horia Geantabca4feb2014-03-14 17:46:51 +02002790 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002791 .aead = __VECS(hmac_sha1_ecb_cipher_null_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302792 }
2793 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01002794 .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
2795 .test = alg_test_null,
2796 .fips_allowed = 1,
2797 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002798 .alg = "authenc(hmac(sha224),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302799 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302800 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002801 .aead = __VECS(hmac_sha224_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302802 }
2803 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002804 .alg = "authenc(hmac(sha224),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302805 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01002806 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302807 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002808 .aead = __VECS(hmac_sha224_des3_ede_cbc_tv_temp)
Horia Geantabca4feb2014-03-14 17:46:51 +02002809 }
2810 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002811 .alg = "authenc(hmac(sha256),cbc(aes))",
Horia Geantae46e9a42012-07-03 19:16:54 +03002812 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01002813 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03002814 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002815 .aead = __VECS(hmac_sha256_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302816 }
2817 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002818 .alg = "authenc(hmac(sha256),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302819 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302820 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002821 .aead = __VECS(hmac_sha256_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302822 }
2823 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002824 .alg = "authenc(hmac(sha256),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302825 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01002826 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302827 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002828 .aead = __VECS(hmac_sha256_des3_ede_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302829 }
2830 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01002831 .alg = "authenc(hmac(sha256),ctr(aes))",
2832 .test = alg_test_null,
2833 .fips_allowed = 1,
2834 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01002835 .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
2836 .test = alg_test_null,
2837 .fips_allowed = 1,
2838 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002839 .alg = "authenc(hmac(sha384),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302840 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302841 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002842 .aead = __VECS(hmac_sha384_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302843 }
2844 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002845 .alg = "authenc(hmac(sha384),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302846 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01002847 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302848 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002849 .aead = __VECS(hmac_sha384_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03002850 }
2851 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01002852 .alg = "authenc(hmac(sha384),ctr(aes))",
2853 .test = alg_test_null,
2854 .fips_allowed = 1,
2855 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01002856 .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
2857 .test = alg_test_null,
2858 .fips_allowed = 1,
2859 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002860 .alg = "authenc(hmac(sha512),cbc(aes))",
Marcus Meissnered1afac2016-02-05 14:23:33 +01002861 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03002862 .test = alg_test_aead,
Horia Geantae46e9a42012-07-03 19:16:54 +03002863 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002864 .aead = __VECS(hmac_sha512_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302865 }
2866 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002867 .alg = "authenc(hmac(sha512),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302868 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302869 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002870 .aead = __VECS(hmac_sha512_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302871 }
2872 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002873 .alg = "authenc(hmac(sha512),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302874 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01002875 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302876 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002877 .aead = __VECS(hmac_sha512_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03002878 }
2879 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01002880 .alg = "authenc(hmac(sha512),ctr(aes))",
2881 .test = alg_test_null,
2882 .fips_allowed = 1,
2883 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01002884 .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
2885 .test = alg_test_null,
2886 .fips_allowed = 1,
2887 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08002888 .alg = "cbc(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002889 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10002890 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08002891 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002892 .cipher = __VECS(aes_cbc_tv_template)
2893 },
Herbert Xuda7f0332008-07-31 17:08:25 +08002894 }, {
2895 .alg = "cbc(anubis)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002896 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08002897 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002898 .cipher = __VECS(anubis_cbc_tv_template)
2899 },
Herbert Xuda7f0332008-07-31 17:08:25 +08002900 }, {
2901 .alg = "cbc(blowfish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002902 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08002903 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002904 .cipher = __VECS(bf_cbc_tv_template)
2905 },
Herbert Xuda7f0332008-07-31 17:08:25 +08002906 }, {
2907 .alg = "cbc(camellia)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002908 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08002909 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002910 .cipher = __VECS(camellia_cbc_tv_template)
2911 },
Herbert Xuda7f0332008-07-31 17:08:25 +08002912 }, {
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02002913 .alg = "cbc(cast5)",
2914 .test = alg_test_skcipher,
2915 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002916 .cipher = __VECS(cast5_cbc_tv_template)
2917 },
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02002918 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02002919 .alg = "cbc(cast6)",
2920 .test = alg_test_skcipher,
2921 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002922 .cipher = __VECS(cast6_cbc_tv_template)
2923 },
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02002924 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08002925 .alg = "cbc(des)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002926 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08002927 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002928 .cipher = __VECS(des_cbc_tv_template)
2929 },
Herbert Xuda7f0332008-07-31 17:08:25 +08002930 }, {
2931 .alg = "cbc(des3_ede)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002932 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10002933 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08002934 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002935 .cipher = __VECS(des3_ede_cbc_tv_template)
2936 },
Herbert Xuda7f0332008-07-31 17:08:25 +08002937 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01002938 /* Same as cbc(aes) except the key is stored in
2939 * hardware secure memory which we reference by index
2940 */
2941 .alg = "cbc(paes)",
2942 .test = alg_test_null,
2943 .fips_allowed = 1,
2944 }, {
Jussi Kivilinna9d259172011-10-18 00:02:53 +03002945 .alg = "cbc(serpent)",
2946 .test = alg_test_skcipher,
2947 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002948 .cipher = __VECS(serpent_cbc_tv_template)
2949 },
Jussi Kivilinna9d259172011-10-18 00:02:53 +03002950 }, {
Gilad Ben-Yossef95ba5972018-09-20 14:18:38 +01002951 .alg = "cbc(sm4)",
2952 .test = alg_test_skcipher,
2953 .suite = {
2954 .cipher = __VECS(sm4_cbc_tv_template)
2955 }
2956 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08002957 .alg = "cbc(twofish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002958 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08002959 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002960 .cipher = __VECS(tf_cbc_tv_template)
2961 },
Herbert Xuda7f0332008-07-31 17:08:25 +08002962 }, {
Ard Biesheuvel092acf02017-02-03 14:49:35 +00002963 .alg = "cbcmac(aes)",
2964 .fips_allowed = 1,
2965 .test = alg_test_hash,
2966 .suite = {
2967 .hash = __VECS(aes_cbcmac_tv_template)
2968 }
2969 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08002970 .alg = "ccm(aes)",
2971 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10002972 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08002973 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002974 .aead = __VECS(aes_ccm_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08002975 }
2976 }, {
Dmitry Eremin-Solenikov7da66672018-10-20 02:01:53 +03002977 .alg = "cfb(aes)",
2978 .test = alg_test_skcipher,
2979 .fips_allowed = 1,
2980 .suite = {
2981 .cipher = __VECS(aes_cfb_tv_template)
2982 },
2983 }, {
Martin Willi3590ebf2015-06-01 13:43:57 +02002984 .alg = "chacha20",
2985 .test = alg_test_skcipher,
2986 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002987 .cipher = __VECS(chacha20_tv_template)
2988 },
Martin Willi3590ebf2015-06-01 13:43:57 +02002989 }, {
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03002990 .alg = "cmac(aes)",
Stephan Mueller8f183752015-08-19 08:42:07 +02002991 .fips_allowed = 1,
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03002992 .test = alg_test_hash,
2993 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002994 .hash = __VECS(aes_cmac128_tv_template)
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03002995 }
2996 }, {
2997 .alg = "cmac(des3_ede)",
Stephan Mueller8f183752015-08-19 08:42:07 +02002998 .fips_allowed = 1,
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03002999 .test = alg_test_hash,
3000 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003001 .hash = __VECS(des3_ede_cmac64_tv_template)
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03003002 }
3003 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03003004 .alg = "compress_null",
3005 .test = alg_test_null,
3006 }, {
Ard Biesheuvelebb34722015-05-04 11:00:17 +02003007 .alg = "crc32",
3008 .test = alg_test_hash,
Milan Broza8a34412019-01-25 10:31:47 +01003009 .fips_allowed = 1,
Ard Biesheuvelebb34722015-05-04 11:00:17 +02003010 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003011 .hash = __VECS(crc32_tv_template)
Ard Biesheuvelebb34722015-05-04 11:00:17 +02003012 }
3013 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003014 .alg = "crc32c",
Herbert Xu8e3ee852008-11-07 14:58:52 +08003015 .test = alg_test_crc32c,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003016 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003017 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003018 .hash = __VECS(crc32c_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003019 }
3020 }, {
Herbert Xu684115212013-09-07 12:56:26 +10003021 .alg = "crct10dif",
3022 .test = alg_test_hash,
3023 .fips_allowed = 1,
3024 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003025 .hash = __VECS(crct10dif_tv_template)
Herbert Xu684115212013-09-07 12:56:26 +10003026 }
3027 }, {
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08003028 .alg = "ctr(aes)",
3029 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003030 .fips_allowed = 1,
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08003031 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003032 .cipher = __VECS(aes_ctr_tv_template)
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08003033 }
3034 }, {
Jussi Kivilinna85b63e32011-10-10 23:03:03 +03003035 .alg = "ctr(blowfish)",
3036 .test = alg_test_skcipher,
3037 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003038 .cipher = __VECS(bf_ctr_tv_template)
Jussi Kivilinna85b63e32011-10-10 23:03:03 +03003039 }
3040 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02003041 .alg = "ctr(camellia)",
3042 .test = alg_test_skcipher,
3043 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003044 .cipher = __VECS(camellia_ctr_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02003045 }
3046 }, {
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02003047 .alg = "ctr(cast5)",
3048 .test = alg_test_skcipher,
3049 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003050 .cipher = __VECS(cast5_ctr_tv_template)
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02003051 }
3052 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003053 .alg = "ctr(cast6)",
3054 .test = alg_test_skcipher,
3055 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003056 .cipher = __VECS(cast6_ctr_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003057 }
3058 }, {
Jussi Kivilinna8163fc32012-10-20 14:53:07 +03003059 .alg = "ctr(des)",
3060 .test = alg_test_skcipher,
3061 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003062 .cipher = __VECS(des_ctr_tv_template)
Jussi Kivilinna8163fc32012-10-20 14:53:07 +03003063 }
3064 }, {
Jussi Kivilinnae080b172012-10-20 14:53:12 +03003065 .alg = "ctr(des3_ede)",
3066 .test = alg_test_skcipher,
Marcelo Cerri0d8da102017-03-20 17:28:05 -03003067 .fips_allowed = 1,
Jussi Kivilinnae080b172012-10-20 14:53:12 +03003068 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003069 .cipher = __VECS(des3_ede_ctr_tv_template)
Jussi Kivilinnae080b172012-10-20 14:53:12 +03003070 }
3071 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01003072 /* Same as ctr(aes) except the key is stored in
3073 * hardware secure memory which we reference by index
3074 */
3075 .alg = "ctr(paes)",
3076 .test = alg_test_null,
3077 .fips_allowed = 1,
3078 }, {
Jussi Kivilinna9d259172011-10-18 00:02:53 +03003079 .alg = "ctr(serpent)",
3080 .test = alg_test_skcipher,
3081 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003082 .cipher = __VECS(serpent_ctr_tv_template)
Jussi Kivilinna9d259172011-10-18 00:02:53 +03003083 }
3084 }, {
Gilad Ben-Yossef95ba5972018-09-20 14:18:38 +01003085 .alg = "ctr(sm4)",
3086 .test = alg_test_skcipher,
3087 .suite = {
3088 .cipher = __VECS(sm4_ctr_tv_template)
3089 }
3090 }, {
Jussi Kivilinna573da622011-10-10 23:03:12 +03003091 .alg = "ctr(twofish)",
3092 .test = alg_test_skcipher,
3093 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003094 .cipher = __VECS(tf_ctr_tv_template)
Jussi Kivilinna573da622011-10-10 23:03:12 +03003095 }
3096 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003097 .alg = "cts(cbc(aes))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003098 .test = alg_test_skcipher,
Gilad Ben-Yossef196ad602018-11-04 10:05:24 +00003099 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003100 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003101 .cipher = __VECS(cts_mode_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003102 }
3103 }, {
3104 .alg = "deflate",
3105 .test = alg_test_comp,
Milan Broz08189042012-12-06 17:16:28 +08003106 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003107 .suite = {
3108 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003109 .comp = __VECS(deflate_comp_tv_template),
3110 .decomp = __VECS(deflate_decomp_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003111 }
3112 }
3113 }, {
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003114 .alg = "dh",
3115 .test = alg_test_kpp,
3116 .fips_allowed = 1,
3117 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003118 .kpp = __VECS(dh_tv_template)
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003119 }
3120 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03003121 .alg = "digest_null",
3122 .test = alg_test_null,
3123 }, {
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003124 .alg = "drbg_nopr_ctr_aes128",
3125 .test = alg_test_drbg,
3126 .fips_allowed = 1,
3127 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003128 .drbg = __VECS(drbg_nopr_ctr_aes128_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003129 }
3130 }, {
3131 .alg = "drbg_nopr_ctr_aes192",
3132 .test = alg_test_drbg,
3133 .fips_allowed = 1,
3134 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003135 .drbg = __VECS(drbg_nopr_ctr_aes192_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003136 }
3137 }, {
3138 .alg = "drbg_nopr_ctr_aes256",
3139 .test = alg_test_drbg,
3140 .fips_allowed = 1,
3141 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003142 .drbg = __VECS(drbg_nopr_ctr_aes256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003143 }
3144 }, {
3145 /*
3146 * There is no need to specifically test the DRBG with every
3147 * backend cipher -- covered by drbg_nopr_hmac_sha256 test
3148 */
3149 .alg = "drbg_nopr_hmac_sha1",
3150 .fips_allowed = 1,
3151 .test = alg_test_null,
3152 }, {
3153 .alg = "drbg_nopr_hmac_sha256",
3154 .test = alg_test_drbg,
3155 .fips_allowed = 1,
3156 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003157 .drbg = __VECS(drbg_nopr_hmac_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003158 }
3159 }, {
3160 /* covered by drbg_nopr_hmac_sha256 test */
3161 .alg = "drbg_nopr_hmac_sha384",
3162 .fips_allowed = 1,
3163 .test = alg_test_null,
3164 }, {
3165 .alg = "drbg_nopr_hmac_sha512",
3166 .test = alg_test_null,
3167 .fips_allowed = 1,
3168 }, {
3169 .alg = "drbg_nopr_sha1",
3170 .fips_allowed = 1,
3171 .test = alg_test_null,
3172 }, {
3173 .alg = "drbg_nopr_sha256",
3174 .test = alg_test_drbg,
3175 .fips_allowed = 1,
3176 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003177 .drbg = __VECS(drbg_nopr_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003178 }
3179 }, {
3180 /* covered by drbg_nopr_sha256 test */
3181 .alg = "drbg_nopr_sha384",
3182 .fips_allowed = 1,
3183 .test = alg_test_null,
3184 }, {
3185 .alg = "drbg_nopr_sha512",
3186 .fips_allowed = 1,
3187 .test = alg_test_null,
3188 }, {
3189 .alg = "drbg_pr_ctr_aes128",
3190 .test = alg_test_drbg,
3191 .fips_allowed = 1,
3192 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003193 .drbg = __VECS(drbg_pr_ctr_aes128_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003194 }
3195 }, {
3196 /* covered by drbg_pr_ctr_aes128 test */
3197 .alg = "drbg_pr_ctr_aes192",
3198 .fips_allowed = 1,
3199 .test = alg_test_null,
3200 }, {
3201 .alg = "drbg_pr_ctr_aes256",
3202 .fips_allowed = 1,
3203 .test = alg_test_null,
3204 }, {
3205 .alg = "drbg_pr_hmac_sha1",
3206 .fips_allowed = 1,
3207 .test = alg_test_null,
3208 }, {
3209 .alg = "drbg_pr_hmac_sha256",
3210 .test = alg_test_drbg,
3211 .fips_allowed = 1,
3212 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003213 .drbg = __VECS(drbg_pr_hmac_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003214 }
3215 }, {
3216 /* covered by drbg_pr_hmac_sha256 test */
3217 .alg = "drbg_pr_hmac_sha384",
3218 .fips_allowed = 1,
3219 .test = alg_test_null,
3220 }, {
3221 .alg = "drbg_pr_hmac_sha512",
3222 .test = alg_test_null,
3223 .fips_allowed = 1,
3224 }, {
3225 .alg = "drbg_pr_sha1",
3226 .fips_allowed = 1,
3227 .test = alg_test_null,
3228 }, {
3229 .alg = "drbg_pr_sha256",
3230 .test = alg_test_drbg,
3231 .fips_allowed = 1,
3232 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003233 .drbg = __VECS(drbg_pr_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003234 }
3235 }, {
3236 /* covered by drbg_pr_sha256 test */
3237 .alg = "drbg_pr_sha384",
3238 .fips_allowed = 1,
3239 .test = alg_test_null,
3240 }, {
3241 .alg = "drbg_pr_sha512",
3242 .fips_allowed = 1,
3243 .test = alg_test_null,
3244 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003245 .alg = "ecb(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003246 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003247 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003248 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003249 .cipher = __VECS(aes_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003250 }
3251 }, {
3252 .alg = "ecb(anubis)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003253 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003254 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003255 .cipher = __VECS(anubis_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003256 }
3257 }, {
3258 .alg = "ecb(arc4)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003259 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003260 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003261 .cipher = __VECS(arc4_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003262 }
3263 }, {
3264 .alg = "ecb(blowfish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003265 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003266 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003267 .cipher = __VECS(bf_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003268 }
3269 }, {
3270 .alg = "ecb(camellia)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003271 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003272 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003273 .cipher = __VECS(camellia_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003274 }
3275 }, {
3276 .alg = "ecb(cast5)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003277 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003278 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003279 .cipher = __VECS(cast5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003280 }
3281 }, {
3282 .alg = "ecb(cast6)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003283 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003284 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003285 .cipher = __VECS(cast6_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003286 }
3287 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03003288 .alg = "ecb(cipher_null)",
3289 .test = alg_test_null,
Milan Broz6175ca22017-04-21 13:03:06 +02003290 .fips_allowed = 1,
Jussi Kivilinnae4483702013-04-07 16:43:56 +03003291 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003292 .alg = "ecb(des)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003293 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003294 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003295 .cipher = __VECS(des_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003296 }
3297 }, {
3298 .alg = "ecb(des3_ede)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003299 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003300 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003301 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003302 .cipher = __VECS(des3_ede_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003303 }
3304 }, {
Jussi Kivilinna66e5bd02013-01-19 13:31:36 +02003305 .alg = "ecb(fcrypt)",
3306 .test = alg_test_skcipher,
3307 .suite = {
3308 .cipher = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003309 .vecs = fcrypt_pcbc_tv_template,
3310 .count = 1
Jussi Kivilinna66e5bd02013-01-19 13:31:36 +02003311 }
3312 }
3313 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003314 .alg = "ecb(khazad)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003315 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003316 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003317 .cipher = __VECS(khazad_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003318 }
3319 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01003320 /* Same as ecb(aes) except the key is stored in
3321 * hardware secure memory which we reference by index
3322 */
3323 .alg = "ecb(paes)",
3324 .test = alg_test_null,
3325 .fips_allowed = 1,
3326 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003327 .alg = "ecb(seed)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003328 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003329 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003330 .cipher = __VECS(seed_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003331 }
3332 }, {
3333 .alg = "ecb(serpent)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003334 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003335 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003336 .cipher = __VECS(serpent_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003337 }
3338 }, {
Gilad Ben-Yossefcd83a8a2018-03-06 09:44:43 +00003339 .alg = "ecb(sm4)",
3340 .test = alg_test_skcipher,
3341 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003342 .cipher = __VECS(sm4_tv_template)
Gilad Ben-Yossefcd83a8a2018-03-06 09:44:43 +00003343 }
3344 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003345 .alg = "ecb(tea)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003346 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003347 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003348 .cipher = __VECS(tea_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003349 }
3350 }, {
3351 .alg = "ecb(tnepres)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003352 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003353 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003354 .cipher = __VECS(tnepres_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003355 }
3356 }, {
3357 .alg = "ecb(twofish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003358 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003359 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003360 .cipher = __VECS(tf_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003361 }
3362 }, {
3363 .alg = "ecb(xeta)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003364 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003365 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003366 .cipher = __VECS(xeta_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003367 }
3368 }, {
3369 .alg = "ecb(xtea)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003370 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003371 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003372 .cipher = __VECS(xtea_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003373 }
3374 }, {
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +01003375 .alg = "ecdh",
3376 .test = alg_test_kpp,
3377 .fips_allowed = 1,
3378 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003379 .kpp = __VECS(ecdh_tv_template)
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +01003380 }
3381 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003382 .alg = "gcm(aes)",
3383 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003384 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003385 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003386 .aead = __VECS(aes_gcm_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003387 }
3388 }, {
Youquan, Song507069c2009-11-23 20:23:04 +08003389 .alg = "ghash",
3390 .test = alg_test_hash,
Jarod Wilson18c0ebd2011-01-29 15:14:35 +11003391 .fips_allowed = 1,
Youquan, Song507069c2009-11-23 20:23:04 +08003392 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003393 .hash = __VECS(ghash_tv_template)
Youquan, Song507069c2009-11-23 20:23:04 +08003394 }
3395 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003396 .alg = "hmac(md5)",
3397 .test = alg_test_hash,
3398 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003399 .hash = __VECS(hmac_md5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003400 }
3401 }, {
3402 .alg = "hmac(rmd128)",
3403 .test = alg_test_hash,
3404 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003405 .hash = __VECS(hmac_rmd128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003406 }
3407 }, {
3408 .alg = "hmac(rmd160)",
3409 .test = alg_test_hash,
3410 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003411 .hash = __VECS(hmac_rmd160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003412 }
3413 }, {
3414 .alg = "hmac(sha1)",
3415 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003416 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003417 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003418 .hash = __VECS(hmac_sha1_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003419 }
3420 }, {
3421 .alg = "hmac(sha224)",
3422 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003423 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003424 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003425 .hash = __VECS(hmac_sha224_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003426 }
3427 }, {
3428 .alg = "hmac(sha256)",
3429 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003430 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003431 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003432 .hash = __VECS(hmac_sha256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003433 }
3434 }, {
raveendra padasalagi98eca722016-07-01 11:16:54 +05303435 .alg = "hmac(sha3-224)",
3436 .test = alg_test_hash,
3437 .fips_allowed = 1,
3438 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003439 .hash = __VECS(hmac_sha3_224_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05303440 }
3441 }, {
3442 .alg = "hmac(sha3-256)",
3443 .test = alg_test_hash,
3444 .fips_allowed = 1,
3445 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003446 .hash = __VECS(hmac_sha3_256_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05303447 }
3448 }, {
3449 .alg = "hmac(sha3-384)",
3450 .test = alg_test_hash,
3451 .fips_allowed = 1,
3452 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003453 .hash = __VECS(hmac_sha3_384_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05303454 }
3455 }, {
3456 .alg = "hmac(sha3-512)",
3457 .test = alg_test_hash,
3458 .fips_allowed = 1,
3459 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003460 .hash = __VECS(hmac_sha3_512_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05303461 }
3462 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003463 .alg = "hmac(sha384)",
3464 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003465 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003466 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003467 .hash = __VECS(hmac_sha384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003468 }
3469 }, {
3470 .alg = "hmac(sha512)",
3471 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003472 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003473 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003474 .hash = __VECS(hmac_sha512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003475 }
3476 }, {
Vitaly Chikunov25a0b9d2018-11-07 00:00:03 +03003477 .alg = "hmac(streebog256)",
3478 .test = alg_test_hash,
3479 .suite = {
3480 .hash = __VECS(hmac_streebog256_tv_template)
3481 }
3482 }, {
3483 .alg = "hmac(streebog512)",
3484 .test = alg_test_hash,
3485 .suite = {
3486 .hash = __VECS(hmac_streebog512_tv_template)
3487 }
3488 }, {
Stephan Muellerbb5530e2015-05-25 15:10:20 +02003489 .alg = "jitterentropy_rng",
3490 .fips_allowed = 1,
3491 .test = alg_test_null,
3492 }, {
Stephan Mueller35351982015-09-21 20:59:56 +02003493 .alg = "kw(aes)",
3494 .test = alg_test_skcipher,
3495 .fips_allowed = 1,
3496 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003497 .cipher = __VECS(aes_kw_tv_template)
Stephan Mueller35351982015-09-21 20:59:56 +02003498 }
3499 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003500 .alg = "lrw(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003501 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003502 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003503 .cipher = __VECS(aes_lrw_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003504 }
3505 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02003506 .alg = "lrw(camellia)",
3507 .test = alg_test_skcipher,
3508 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003509 .cipher = __VECS(camellia_lrw_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02003510 }
3511 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003512 .alg = "lrw(cast6)",
3513 .test = alg_test_skcipher,
3514 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003515 .cipher = __VECS(cast6_lrw_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003516 }
3517 }, {
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03003518 .alg = "lrw(serpent)",
3519 .test = alg_test_skcipher,
3520 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003521 .cipher = __VECS(serpent_lrw_tv_template)
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03003522 }
3523 }, {
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03003524 .alg = "lrw(twofish)",
3525 .test = alg_test_skcipher,
3526 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003527 .cipher = __VECS(tf_lrw_tv_template)
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03003528 }
3529 }, {
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02003530 .alg = "lz4",
3531 .test = alg_test_comp,
3532 .fips_allowed = 1,
3533 .suite = {
3534 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003535 .comp = __VECS(lz4_comp_tv_template),
3536 .decomp = __VECS(lz4_decomp_tv_template)
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02003537 }
3538 }
3539 }, {
3540 .alg = "lz4hc",
3541 .test = alg_test_comp,
3542 .fips_allowed = 1,
3543 .suite = {
3544 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003545 .comp = __VECS(lz4hc_comp_tv_template),
3546 .decomp = __VECS(lz4hc_decomp_tv_template)
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02003547 }
3548 }
3549 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003550 .alg = "lzo",
3551 .test = alg_test_comp,
Milan Broz08189042012-12-06 17:16:28 +08003552 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003553 .suite = {
3554 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003555 .comp = __VECS(lzo_comp_tv_template),
3556 .decomp = __VECS(lzo_decomp_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003557 }
3558 }
3559 }, {
3560 .alg = "md4",
3561 .test = alg_test_hash,
3562 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003563 .hash = __VECS(md4_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003564 }
3565 }, {
3566 .alg = "md5",
3567 .test = alg_test_hash,
3568 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003569 .hash = __VECS(md5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003570 }
3571 }, {
3572 .alg = "michael_mic",
3573 .test = alg_test_hash,
3574 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003575 .hash = __VECS(michael_mic_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003576 }
3577 }, {
Ondrej Mosnacek4feb4c52018-05-11 14:19:10 +02003578 .alg = "morus1280",
3579 .test = alg_test_aead,
3580 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003581 .aead = __VECS(morus1280_tv_template)
Ondrej Mosnacek4feb4c52018-05-11 14:19:10 +02003582 }
3583 }, {
3584 .alg = "morus640",
3585 .test = alg_test_aead,
3586 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003587 .aead = __VECS(morus640_tv_template)
Ondrej Mosnacek4feb4c52018-05-11 14:19:10 +02003588 }
3589 }, {
Eric Biggers26609a22018-11-16 17:26:29 -08003590 .alg = "nhpoly1305",
3591 .test = alg_test_hash,
3592 .suite = {
3593 .hash = __VECS(nhpoly1305_tv_template)
3594 }
3595 }, {
Puneet Saxenaba0e14a2011-05-04 15:04:10 +10003596 .alg = "ofb(aes)",
3597 .test = alg_test_skcipher,
3598 .fips_allowed = 1,
3599 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003600 .cipher = __VECS(aes_ofb_tv_template)
Puneet Saxenaba0e14a2011-05-04 15:04:10 +10003601 }
3602 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01003603 /* Same as ofb(aes) except the key is stored in
3604 * hardware secure memory which we reference by index
3605 */
3606 .alg = "ofb(paes)",
3607 .test = alg_test_null,
3608 .fips_allowed = 1,
3609 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003610 .alg = "pcbc(fcrypt)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003611 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003612 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003613 .cipher = __VECS(fcrypt_pcbc_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003614 }
3615 }, {
Stephan Mueller12071072017-06-12 23:27:51 +02003616 .alg = "pkcs1pad(rsa,sha224)",
3617 .test = alg_test_null,
3618 .fips_allowed = 1,
3619 }, {
3620 .alg = "pkcs1pad(rsa,sha256)",
3621 .test = alg_test_akcipher,
3622 .fips_allowed = 1,
3623 .suite = {
3624 .akcipher = __VECS(pkcs1pad_rsa_tv_template)
3625 }
3626 }, {
3627 .alg = "pkcs1pad(rsa,sha384)",
3628 .test = alg_test_null,
3629 .fips_allowed = 1,
3630 }, {
3631 .alg = "pkcs1pad(rsa,sha512)",
3632 .test = alg_test_null,
3633 .fips_allowed = 1,
3634 }, {
Martin Willieee9dc62015-06-01 13:43:59 +02003635 .alg = "poly1305",
3636 .test = alg_test_hash,
3637 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003638 .hash = __VECS(poly1305_tv_template)
Martin Willieee9dc62015-06-01 13:43:59 +02003639 }
3640 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003641 .alg = "rfc3686(ctr(aes))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003642 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003643 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003644 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003645 .cipher = __VECS(aes_ctr_rfc3686_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003646 }
3647 }, {
Herbert Xu3f31a742015-07-09 07:17:34 +08003648 .alg = "rfc4106(gcm(aes))",
Adrian Hoban69435b92010-11-04 15:02:04 -04003649 .test = alg_test_aead,
Jarod Wilsondb71f29a2015-01-23 12:42:15 -05003650 .fips_allowed = 1,
Adrian Hoban69435b92010-11-04 15:02:04 -04003651 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003652 .aead = __VECS(aes_gcm_rfc4106_tv_template)
Adrian Hoban69435b92010-11-04 15:02:04 -04003653 }
3654 }, {
Herbert Xu544c4362015-07-14 16:53:22 +08003655 .alg = "rfc4309(ccm(aes))",
Jarod Wilson5d667322009-05-04 19:23:40 +08003656 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003657 .fips_allowed = 1,
Jarod Wilson5d667322009-05-04 19:23:40 +08003658 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003659 .aead = __VECS(aes_ccm_rfc4309_tv_template)
Jarod Wilson5d667322009-05-04 19:23:40 +08003660 }
3661 }, {
Herbert Xubb687452015-06-16 13:54:24 +08003662 .alg = "rfc4543(gcm(aes))",
Jussi Kivilinnae9b74412013-04-07 16:43:51 +03003663 .test = alg_test_aead,
3664 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003665 .aead = __VECS(aes_gcm_rfc4543_tv_template)
Jussi Kivilinnae9b74412013-04-07 16:43:51 +03003666 }
3667 }, {
Martin Williaf2b76b2015-06-01 13:44:01 +02003668 .alg = "rfc7539(chacha20,poly1305)",
3669 .test = alg_test_aead,
3670 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003671 .aead = __VECS(rfc7539_tv_template)
Martin Williaf2b76b2015-06-01 13:44:01 +02003672 }
3673 }, {
Martin Willi59007582015-06-01 13:44:03 +02003674 .alg = "rfc7539esp(chacha20,poly1305)",
3675 .test = alg_test_aead,
3676 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003677 .aead = __VECS(rfc7539esp_tv_template)
Martin Willi59007582015-06-01 13:44:03 +02003678 }
3679 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003680 .alg = "rmd128",
3681 .test = alg_test_hash,
3682 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003683 .hash = __VECS(rmd128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003684 }
3685 }, {
3686 .alg = "rmd160",
3687 .test = alg_test_hash,
3688 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003689 .hash = __VECS(rmd160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003690 }
3691 }, {
3692 .alg = "rmd256",
3693 .test = alg_test_hash,
3694 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003695 .hash = __VECS(rmd256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003696 }
3697 }, {
3698 .alg = "rmd320",
3699 .test = alg_test_hash,
3700 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003701 .hash = __VECS(rmd320_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003702 }
3703 }, {
Tadeusz Struk946cc462015-06-16 10:31:06 -07003704 .alg = "rsa",
3705 .test = alg_test_akcipher,
3706 .fips_allowed = 1,
3707 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003708 .akcipher = __VECS(rsa_tv_template)
Tadeusz Struk946cc462015-06-16 10:31:06 -07003709 }
3710 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003711 .alg = "salsa20",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003712 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003713 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003714 .cipher = __VECS(salsa20_stream_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003715 }
3716 }, {
3717 .alg = "sha1",
3718 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003719 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003720 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003721 .hash = __VECS(sha1_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003722 }
3723 }, {
3724 .alg = "sha224",
3725 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003726 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003727 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003728 .hash = __VECS(sha224_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003729 }
3730 }, {
3731 .alg = "sha256",
3732 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003733 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003734 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003735 .hash = __VECS(sha256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003736 }
3737 }, {
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05303738 .alg = "sha3-224",
3739 .test = alg_test_hash,
3740 .fips_allowed = 1,
3741 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003742 .hash = __VECS(sha3_224_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05303743 }
3744 }, {
3745 .alg = "sha3-256",
3746 .test = alg_test_hash,
3747 .fips_allowed = 1,
3748 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003749 .hash = __VECS(sha3_256_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05303750 }
3751 }, {
3752 .alg = "sha3-384",
3753 .test = alg_test_hash,
3754 .fips_allowed = 1,
3755 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003756 .hash = __VECS(sha3_384_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05303757 }
3758 }, {
3759 .alg = "sha3-512",
3760 .test = alg_test_hash,
3761 .fips_allowed = 1,
3762 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003763 .hash = __VECS(sha3_512_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05303764 }
3765 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003766 .alg = "sha384",
3767 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003768 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003769 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003770 .hash = __VECS(sha384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003771 }
3772 }, {
3773 .alg = "sha512",
3774 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003775 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003776 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003777 .hash = __VECS(sha512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003778 }
3779 }, {
Gilad Ben-Yossefb7e27532017-08-21 13:51:29 +03003780 .alg = "sm3",
3781 .test = alg_test_hash,
3782 .suite = {
3783 .hash = __VECS(sm3_tv_template)
3784 }
3785 }, {
Vitaly Chikunov25a0b9d2018-11-07 00:00:03 +03003786 .alg = "streebog256",
3787 .test = alg_test_hash,
3788 .suite = {
3789 .hash = __VECS(streebog256_tv_template)
3790 }
3791 }, {
3792 .alg = "streebog512",
3793 .test = alg_test_hash,
3794 .suite = {
3795 .hash = __VECS(streebog512_tv_template)
3796 }
3797 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003798 .alg = "tgr128",
3799 .test = alg_test_hash,
3800 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003801 .hash = __VECS(tgr128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003802 }
3803 }, {
3804 .alg = "tgr160",
3805 .test = alg_test_hash,
3806 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003807 .hash = __VECS(tgr160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003808 }
3809 }, {
3810 .alg = "tgr192",
3811 .test = alg_test_hash,
3812 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003813 .hash = __VECS(tgr192_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003814 }
3815 }, {
Eric Biggersed331ad2018-06-18 10:22:39 -07003816 .alg = "vmac64(aes)",
3817 .test = alg_test_hash,
3818 .suite = {
3819 .hash = __VECS(vmac64_aes_tv_template)
3820 }
3821 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003822 .alg = "wp256",
3823 .test = alg_test_hash,
3824 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003825 .hash = __VECS(wp256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003826 }
3827 }, {
3828 .alg = "wp384",
3829 .test = alg_test_hash,
3830 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003831 .hash = __VECS(wp384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003832 }
3833 }, {
3834 .alg = "wp512",
3835 .test = alg_test_hash,
3836 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003837 .hash = __VECS(wp512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003838 }
3839 }, {
3840 .alg = "xcbc(aes)",
3841 .test = alg_test_hash,
3842 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003843 .hash = __VECS(aes_xcbc128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003844 }
3845 }, {
Eric Biggersaa762402018-11-16 17:26:22 -08003846 .alg = "xchacha12",
3847 .test = alg_test_skcipher,
3848 .suite = {
3849 .cipher = __VECS(xchacha12_tv_template)
3850 },
3851 }, {
Eric Biggersde61d7a2018-11-16 17:26:20 -08003852 .alg = "xchacha20",
3853 .test = alg_test_skcipher,
3854 .suite = {
3855 .cipher = __VECS(xchacha20_tv_template)
3856 },
3857 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003858 .alg = "xts(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003859 .test = alg_test_skcipher,
Jarod Wilson2918aa82011-01-29 15:14:01 +11003860 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003861 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003862 .cipher = __VECS(aes_xts_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003863 }
Geert Uytterhoeven0c01aed2009-03-04 15:42:15 +08003864 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02003865 .alg = "xts(camellia)",
3866 .test = alg_test_skcipher,
3867 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003868 .cipher = __VECS(camellia_xts_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02003869 }
3870 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003871 .alg = "xts(cast6)",
3872 .test = alg_test_skcipher,
3873 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003874 .cipher = __VECS(cast6_xts_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003875 }
3876 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01003877 /* Same as xts(aes) except the key is stored in
3878 * hardware secure memory which we reference by index
3879 */
3880 .alg = "xts(paes)",
3881 .test = alg_test_null,
3882 .fips_allowed = 1,
3883 }, {
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03003884 .alg = "xts(serpent)",
3885 .test = alg_test_skcipher,
3886 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003887 .cipher = __VECS(serpent_xts_tv_template)
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03003888 }
3889 }, {
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03003890 .alg = "xts(twofish)",
3891 .test = alg_test_skcipher,
3892 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003893 .cipher = __VECS(tf_xts_tv_template)
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03003894 }
Giovanni Cabiddua368f432017-04-21 21:54:30 +01003895 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01003896 .alg = "xts4096(paes)",
3897 .test = alg_test_null,
3898 .fips_allowed = 1,
3899 }, {
3900 .alg = "xts512(paes)",
3901 .test = alg_test_null,
3902 .fips_allowed = 1,
3903 }, {
Giovanni Cabiddua368f432017-04-21 21:54:30 +01003904 .alg = "zlib-deflate",
3905 .test = alg_test_comp,
3906 .fips_allowed = 1,
3907 .suite = {
3908 .comp = {
3909 .comp = __VECS(zlib_deflate_comp_tv_template),
3910 .decomp = __VECS(zlib_deflate_decomp_tv_template)
3911 }
3912 }
Nick Terrelld28fc3d2018-03-30 12:14:53 -07003913 }, {
3914 .alg = "zstd",
3915 .test = alg_test_comp,
3916 .fips_allowed = 1,
3917 .suite = {
3918 .comp = {
3919 .comp = __VECS(zstd_comp_tv_template),
3920 .decomp = __VECS(zstd_decomp_tv_template)
3921 }
3922 }
Herbert Xuda7f0332008-07-31 17:08:25 +08003923 }
3924};
3925
Eric Biggers3f47a032019-01-31 23:51:43 -08003926static void alg_check_test_descs_order(void)
Jussi Kivilinna57147582013-06-13 17:37:40 +03003927{
3928 int i;
3929
Jussi Kivilinna57147582013-06-13 17:37:40 +03003930 for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
3931 int diff = strcmp(alg_test_descs[i - 1].alg,
3932 alg_test_descs[i].alg);
3933
3934 if (WARN_ON(diff > 0)) {
3935 pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
3936 alg_test_descs[i - 1].alg,
3937 alg_test_descs[i].alg);
3938 }
3939
3940 if (WARN_ON(diff == 0)) {
3941 pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
3942 alg_test_descs[i].alg);
3943 }
3944 }
3945}
3946
Eric Biggers3f47a032019-01-31 23:51:43 -08003947static void alg_check_testvec_configs(void)
3948{
Eric Biggers4e7babba2019-01-31 23:51:46 -08003949 int i;
3950
3951 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++)
3952 WARN_ON(!valid_testvec_config(
3953 &default_cipher_testvec_configs[i]));
Eric Biggers4cc2dcf2019-01-31 23:51:48 -08003954
3955 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++)
3956 WARN_ON(!valid_testvec_config(
3957 &default_hash_testvec_configs[i]));
Eric Biggers3f47a032019-01-31 23:51:43 -08003958}
3959
3960static void testmgr_onetime_init(void)
3961{
3962 alg_check_test_descs_order();
3963 alg_check_testvec_configs();
Eric Biggers5b2706a2019-01-31 23:51:44 -08003964
3965#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
3966 pr_warn("alg: extra crypto tests enabled. This is intended for developer use only.\n");
3967#endif
Eric Biggers3f47a032019-01-31 23:51:43 -08003968}
3969
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003970static int alg_find_test(const char *alg)
Herbert Xuda7f0332008-07-31 17:08:25 +08003971{
3972 int start = 0;
3973 int end = ARRAY_SIZE(alg_test_descs);
3974
3975 while (start < end) {
3976 int i = (start + end) / 2;
3977 int diff = strcmp(alg_test_descs[i].alg, alg);
3978
3979 if (diff > 0) {
3980 end = i;
3981 continue;
3982 }
3983
3984 if (diff < 0) {
3985 start = i + 1;
3986 continue;
3987 }
3988
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003989 return i;
Herbert Xuda7f0332008-07-31 17:08:25 +08003990 }
3991
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003992 return -1;
3993}
3994
3995int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
3996{
3997 int i;
Herbert Xua68f6612009-07-02 16:32:12 +08003998 int j;
Neil Hormand12d6b62008-10-12 20:36:51 +08003999 int rc;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004000
Richard W.M. Jones9e5c9fe2016-05-03 10:00:17 +01004001 if (!fips_enabled && notests) {
4002 printk_once(KERN_INFO "alg: self-tests disabled\n");
4003 return 0;
4004 }
4005
Eric Biggers3f47a032019-01-31 23:51:43 -08004006 DO_ONCE(testmgr_onetime_init);
Jussi Kivilinna57147582013-06-13 17:37:40 +03004007
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004008 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
4009 char nalg[CRYPTO_MAX_ALG_NAME];
4010
4011 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
4012 sizeof(nalg))
4013 return -ENAMETOOLONG;
4014
4015 i = alg_find_test(nalg);
4016 if (i < 0)
4017 goto notest;
4018
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10004019 if (fips_enabled && !alg_test_descs[i].fips_allowed)
4020 goto non_fips_alg;
4021
Jarod Wilson941fb322009-05-04 19:49:23 +08004022 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
4023 goto test_done;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004024 }
4025
4026 i = alg_find_test(alg);
Herbert Xua68f6612009-07-02 16:32:12 +08004027 j = alg_find_test(driver);
4028 if (i < 0 && j < 0)
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004029 goto notest;
4030
Herbert Xua68f6612009-07-02 16:32:12 +08004031 if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
4032 (j >= 0 && !alg_test_descs[j].fips_allowed)))
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10004033 goto non_fips_alg;
4034
Herbert Xua68f6612009-07-02 16:32:12 +08004035 rc = 0;
4036 if (i >= 0)
4037 rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
4038 type, mask);
Cristian Stoica032c8ca2013-07-18 18:57:07 +03004039 if (j >= 0 && j != i)
Herbert Xua68f6612009-07-02 16:32:12 +08004040 rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
4041 type, mask);
4042
Jarod Wilson941fb322009-05-04 19:49:23 +08004043test_done:
Neil Hormand12d6b62008-10-12 20:36:51 +08004044 if (fips_enabled && rc)
4045 panic("%s: %s alg self test failed in fips mode!\n", driver, alg);
4046
Jarod Wilson29ecd4a2009-05-04 19:51:17 +08004047 if (fips_enabled && !rc)
Masanari Iida3e8cffd2014-10-07 00:37:54 +09004048 pr_info("alg: self-tests for %s (%s) passed\n", driver, alg);
Jarod Wilson29ecd4a2009-05-04 19:51:17 +08004049
Neil Hormand12d6b62008-10-12 20:36:51 +08004050 return rc;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004051
4052notest:
Herbert Xuda7f0332008-07-31 17:08:25 +08004053 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
4054 return 0;
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10004055non_fips_alg:
4056 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08004057}
Alexander Shishkin0b767f92010-06-03 20:53:43 +10004058
Herbert Xu326a6342010-08-06 09:40:28 +08004059#endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
Alexander Shishkin0b767f92010-06-03 20:53:43 +10004060
Herbert Xuda7f0332008-07-31 17:08:25 +08004061EXPORT_SYMBOL_GPL(alg_test);