blob: 99f84160cb1dc7b4247385572218630e7ca1e972 [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>
Herbert Xuda7f0332008-07-31 17:08:25 +080031#include <linux/scatterlist.h>
32#include <linux/slab.h>
33#include <linux/string.h>
Jarod Wilson7647d6c2009-05-04 19:44:50 +080034#include <crypto/rng.h>
Stephan Mueller64d1cdf2014-05-31 17:25:36 +020035#include <crypto/drbg.h>
Tadeusz Struk946cc462015-06-16 10:31:06 -070036#include <crypto/akcipher.h>
Salvatore Benedetto802c7f12016-06-22 17:49:14 +010037#include <crypto/kpp.h>
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +010038#include <crypto/acompress.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080039
40#include "internal.h"
Alexander Shishkin0b767f92010-06-03 20:53:43 +100041
Richard W.M. Jones9e5c9fe2016-05-03 10:00:17 +010042static bool notests;
43module_param(notests, bool, 0644);
44MODULE_PARM_DESC(notests, "disable crypto self-tests");
45
Eric Biggers5b2706a2019-01-31 23:51:44 -080046#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
47static bool noextratests;
48module_param(noextratests, bool, 0644);
49MODULE_PARM_DESC(noextratests, "disable expensive crypto self-tests");
50
51static unsigned int fuzz_iterations = 100;
52module_param(fuzz_iterations, uint, 0644);
53MODULE_PARM_DESC(fuzz_iterations, "number of fuzz test iterations");
54#endif
55
Herbert Xu326a6342010-08-06 09:40:28 +080056#ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
Alexander Shishkin0b767f92010-06-03 20:53:43 +100057
58/* a perfect nop */
59int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
60{
61 return 0;
62}
63
64#else
65
Herbert Xuda7f0332008-07-31 17:08:25 +080066#include "testmgr.h"
67
68/*
69 * Need slab memory for testing (size in number of pages).
70 */
71#define XBUFSIZE 8
72
73/*
74 * Indexes into the xbuf to simulate cross-page access.
75 */
76#define IDX1 32
77#define IDX2 32400
Ard Biesheuvel04b46fb2016-12-08 08:23:52 +000078#define IDX3 1511
Herbert Xuda7f0332008-07-31 17:08:25 +080079#define IDX4 8193
80#define IDX5 22222
81#define IDX6 17101
82#define IDX7 27333
83#define IDX8 3000
84
85/*
86* Used by test_cipher()
87*/
88#define ENCRYPT 1
89#define DECRYPT 0
90
Herbert Xuda7f0332008-07-31 17:08:25 +080091struct aead_test_suite {
Eric Biggersa0d608ee2019-01-13 15:32:28 -080092 const struct aead_testvec *vecs;
93 unsigned int count;
Herbert Xuda7f0332008-07-31 17:08:25 +080094};
95
96struct cipher_test_suite {
Eric Biggers92a4c9f2018-05-20 22:50:29 -070097 const struct cipher_testvec *vecs;
98 unsigned int count;
Herbert Xuda7f0332008-07-31 17:08:25 +080099};
100
101struct comp_test_suite {
102 struct {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800103 const struct comp_testvec *vecs;
Herbert Xuda7f0332008-07-31 17:08:25 +0800104 unsigned int count;
105 } comp, decomp;
106};
107
108struct hash_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800109 const struct hash_testvec *vecs;
Herbert Xuda7f0332008-07-31 17:08:25 +0800110 unsigned int count;
111};
112
Jarod Wilson7647d6c2009-05-04 19:44:50 +0800113struct cprng_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800114 const struct cprng_testvec *vecs;
Jarod Wilson7647d6c2009-05-04 19:44:50 +0800115 unsigned int count;
116};
117
Stephan Mueller64d1cdf2014-05-31 17:25:36 +0200118struct drbg_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800119 const struct drbg_testvec *vecs;
Stephan Mueller64d1cdf2014-05-31 17:25:36 +0200120 unsigned int count;
121};
122
Tadeusz Struk946cc462015-06-16 10:31:06 -0700123struct akcipher_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800124 const struct akcipher_testvec *vecs;
Tadeusz Struk946cc462015-06-16 10:31:06 -0700125 unsigned int count;
126};
127
Salvatore Benedetto802c7f12016-06-22 17:49:14 +0100128struct kpp_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800129 const struct kpp_testvec *vecs;
Salvatore Benedetto802c7f12016-06-22 17:49:14 +0100130 unsigned int count;
131};
132
Herbert Xuda7f0332008-07-31 17:08:25 +0800133struct alg_test_desc {
134 const char *alg;
135 int (*test)(const struct alg_test_desc *desc, const char *driver,
136 u32 type, u32 mask);
Jarod Wilsona1915d52009-05-15 15:16:03 +1000137 int fips_allowed; /* set if alg is allowed in fips mode */
Herbert Xuda7f0332008-07-31 17:08:25 +0800138
139 union {
140 struct aead_test_suite aead;
141 struct cipher_test_suite cipher;
142 struct comp_test_suite comp;
143 struct hash_test_suite hash;
Jarod Wilson7647d6c2009-05-04 19:44:50 +0800144 struct cprng_test_suite cprng;
Stephan Mueller64d1cdf2014-05-31 17:25:36 +0200145 struct drbg_test_suite drbg;
Tadeusz Struk946cc462015-06-16 10:31:06 -0700146 struct akcipher_test_suite akcipher;
Salvatore Benedetto802c7f12016-06-22 17:49:14 +0100147 struct kpp_test_suite kpp;
Herbert Xuda7f0332008-07-31 17:08:25 +0800148 } suite;
149};
150
Eric Biggersb13b1e02017-02-24 15:46:59 -0800151static const unsigned int IDX[8] = {
152 IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
Herbert Xuda7f0332008-07-31 17:08:25 +0800153
Herbert Xuda7f0332008-07-31 17:08:25 +0800154static void hexdump(unsigned char *buf, unsigned int len)
155{
156 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
157 16, 1,
158 buf, len, false);
159}
160
Eric Biggers3f47a032019-01-31 23:51:43 -0800161static int __testmgr_alloc_buf(char *buf[XBUFSIZE], int order)
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800162{
163 int i;
164
165 for (i = 0; i < XBUFSIZE; i++) {
Eric Biggers3f47a032019-01-31 23:51:43 -0800166 buf[i] = (char *)__get_free_pages(GFP_KERNEL, order);
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800167 if (!buf[i])
168 goto err_free_buf;
169 }
170
171 return 0;
172
173err_free_buf:
174 while (i-- > 0)
Eric Biggers3f47a032019-01-31 23:51:43 -0800175 free_pages((unsigned long)buf[i], order);
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800176
177 return -ENOMEM;
178}
179
Eric Biggers3f47a032019-01-31 23:51:43 -0800180static int testmgr_alloc_buf(char *buf[XBUFSIZE])
181{
182 return __testmgr_alloc_buf(buf, 0);
183}
184
185static void __testmgr_free_buf(char *buf[XBUFSIZE], int order)
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800186{
187 int i;
188
189 for (i = 0; i < XBUFSIZE; i++)
Eric Biggers3f47a032019-01-31 23:51:43 -0800190 free_pages((unsigned long)buf[i], order);
191}
192
193static void testmgr_free_buf(char *buf[XBUFSIZE])
194{
195 __testmgr_free_buf(buf, 0);
196}
197
198#define TESTMGR_POISON_BYTE 0xfe
199#define TESTMGR_POISON_LEN 16
200
201static inline void testmgr_poison(void *addr, size_t len)
202{
203 memset(addr, TESTMGR_POISON_BYTE, len);
204}
205
206/* Is the memory region still fully poisoned? */
207static inline bool testmgr_is_poison(const void *addr, size_t len)
208{
209 return memchr_inv(addr, TESTMGR_POISON_BYTE, len) == NULL;
210}
211
212/* flush type for hash algorithms */
213enum flush_type {
214 /* merge with update of previous buffer(s) */
215 FLUSH_TYPE_NONE = 0,
216
217 /* update with previous buffer(s) before doing this one */
218 FLUSH_TYPE_FLUSH,
219
220 /* likewise, but also export and re-import the intermediate state */
221 FLUSH_TYPE_REIMPORT,
222};
223
224/* finalization function for hash algorithms */
225enum finalization_type {
226 FINALIZATION_TYPE_FINAL, /* use final() */
227 FINALIZATION_TYPE_FINUP, /* use finup() */
228 FINALIZATION_TYPE_DIGEST, /* use digest() */
229};
230
231#define TEST_SG_TOTAL 10000
232
233/**
234 * struct test_sg_division - description of a scatterlist entry
235 *
236 * This struct describes one entry of a scatterlist being constructed to check a
237 * crypto test vector.
238 *
239 * @proportion_of_total: length of this chunk relative to the total length,
240 * given as a proportion out of TEST_SG_TOTAL so that it
241 * scales to fit any test vector
242 * @offset: byte offset into a 2-page buffer at which this chunk will start
243 * @offset_relative_to_alignmask: if true, add the algorithm's alignmask to the
244 * @offset
245 * @flush_type: for hashes, whether an update() should be done now vs.
246 * continuing to accumulate data
247 */
248struct test_sg_division {
249 unsigned int proportion_of_total;
250 unsigned int offset;
251 bool offset_relative_to_alignmask;
252 enum flush_type flush_type;
253};
254
255/**
256 * struct testvec_config - configuration for testing a crypto test vector
257 *
258 * This struct describes the data layout and other parameters with which each
259 * crypto test vector can be tested.
260 *
261 * @name: name of this config, logged for debugging purposes if a test fails
262 * @inplace: operate on the data in-place, if applicable for the algorithm type?
263 * @req_flags: extra request_flags, e.g. CRYPTO_TFM_REQ_MAY_SLEEP
264 * @src_divs: description of how to arrange the source scatterlist
265 * @dst_divs: description of how to arrange the dst scatterlist, if applicable
266 * for the algorithm type. Defaults to @src_divs if unset.
267 * @iv_offset: misalignment of the IV in the range [0..MAX_ALGAPI_ALIGNMASK+1],
268 * where 0 is aligned to a 2*(MAX_ALGAPI_ALIGNMASK+1) byte boundary
269 * @iv_offset_relative_to_alignmask: if true, add the algorithm's alignmask to
270 * the @iv_offset
271 * @finalization_type: what finalization function to use for hashes
272 */
273struct testvec_config {
274 const char *name;
275 bool inplace;
276 u32 req_flags;
277 struct test_sg_division src_divs[XBUFSIZE];
278 struct test_sg_division dst_divs[XBUFSIZE];
279 unsigned int iv_offset;
280 bool iv_offset_relative_to_alignmask;
281 enum finalization_type finalization_type;
282};
283
284#define TESTVEC_CONFIG_NAMELEN 192
285
286static unsigned int count_test_sg_divisions(const struct test_sg_division *divs)
287{
288 unsigned int remaining = TEST_SG_TOTAL;
289 unsigned int ndivs = 0;
290
291 do {
292 remaining -= divs[ndivs++].proportion_of_total;
293 } while (remaining);
294
295 return ndivs;
296}
297
298static bool valid_sg_divisions(const struct test_sg_division *divs,
299 unsigned int count, bool *any_flushes_ret)
300{
301 unsigned int total = 0;
302 unsigned int i;
303
304 for (i = 0; i < count && total != TEST_SG_TOTAL; i++) {
305 if (divs[i].proportion_of_total <= 0 ||
306 divs[i].proportion_of_total > TEST_SG_TOTAL - total)
307 return false;
308 total += divs[i].proportion_of_total;
309 if (divs[i].flush_type != FLUSH_TYPE_NONE)
310 *any_flushes_ret = true;
311 }
312 return total == TEST_SG_TOTAL &&
313 memchr_inv(&divs[i], 0, (count - i) * sizeof(divs[0])) == NULL;
314}
315
316/*
317 * Check whether the given testvec_config is valid. This isn't strictly needed
318 * since every testvec_config should be valid, but check anyway so that people
319 * don't unknowingly add broken configs that don't do what they wanted.
320 */
321static bool valid_testvec_config(const struct testvec_config *cfg)
322{
323 bool any_flushes = false;
324
325 if (cfg->name == NULL)
326 return false;
327
328 if (!valid_sg_divisions(cfg->src_divs, ARRAY_SIZE(cfg->src_divs),
329 &any_flushes))
330 return false;
331
332 if (cfg->dst_divs[0].proportion_of_total) {
333 if (!valid_sg_divisions(cfg->dst_divs,
334 ARRAY_SIZE(cfg->dst_divs),
335 &any_flushes))
336 return false;
337 } else {
338 if (memchr_inv(cfg->dst_divs, 0, sizeof(cfg->dst_divs)))
339 return false;
340 /* defaults to dst_divs=src_divs */
341 }
342
343 if (cfg->iv_offset +
344 (cfg->iv_offset_relative_to_alignmask ? MAX_ALGAPI_ALIGNMASK : 0) >
345 MAX_ALGAPI_ALIGNMASK + 1)
346 return false;
347
348 if (any_flushes && cfg->finalization_type == FINALIZATION_TYPE_DIGEST)
349 return false;
350
351 return true;
352}
353
354struct test_sglist {
355 char *bufs[XBUFSIZE];
356 struct scatterlist sgl[XBUFSIZE];
357 struct scatterlist sgl_saved[XBUFSIZE];
358 struct scatterlist *sgl_ptr;
359 unsigned int nents;
360};
361
362static int init_test_sglist(struct test_sglist *tsgl)
363{
364 return __testmgr_alloc_buf(tsgl->bufs, 1 /* two pages per buffer */);
365}
366
367static void destroy_test_sglist(struct test_sglist *tsgl)
368{
369 return __testmgr_free_buf(tsgl->bufs, 1 /* two pages per buffer */);
370}
371
372/**
373 * build_test_sglist() - build a scatterlist for a crypto test
374 *
375 * @tsgl: the scatterlist to build. @tsgl->bufs[] contains an array of 2-page
376 * buffers which the scatterlist @tsgl->sgl[] will be made to point into.
377 * @divs: the layout specification on which the scatterlist will be based
378 * @alignmask: the algorithm's alignmask
379 * @total_len: the total length of the scatterlist to build in bytes
380 * @data: if non-NULL, the buffers will be filled with this data until it ends.
381 * Otherwise the buffers will be poisoned. In both cases, some bytes
382 * past the end of each buffer will be poisoned to help detect overruns.
383 * @out_divs: if non-NULL, the test_sg_division to which each scatterlist entry
384 * corresponds will be returned here. This will match @divs except
385 * that divisions resolving to a length of 0 are omitted as they are
386 * not included in the scatterlist.
387 *
388 * Return: 0 or a -errno value
389 */
390static int build_test_sglist(struct test_sglist *tsgl,
391 const struct test_sg_division *divs,
392 const unsigned int alignmask,
393 const unsigned int total_len,
394 struct iov_iter *data,
395 const struct test_sg_division *out_divs[XBUFSIZE])
396{
397 struct {
398 const struct test_sg_division *div;
399 size_t length;
400 } partitions[XBUFSIZE];
401 const unsigned int ndivs = count_test_sg_divisions(divs);
402 unsigned int len_remaining = total_len;
403 unsigned int i;
404
405 BUILD_BUG_ON(ARRAY_SIZE(partitions) != ARRAY_SIZE(tsgl->sgl));
406 if (WARN_ON(ndivs > ARRAY_SIZE(partitions)))
407 return -EINVAL;
408
409 /* Calculate the (div, length) pairs */
410 tsgl->nents = 0;
411 for (i = 0; i < ndivs; i++) {
412 unsigned int len_this_sg =
413 min(len_remaining,
414 (total_len * divs[i].proportion_of_total +
415 TEST_SG_TOTAL / 2) / TEST_SG_TOTAL);
416
417 if (len_this_sg != 0) {
418 partitions[tsgl->nents].div = &divs[i];
419 partitions[tsgl->nents].length = len_this_sg;
420 tsgl->nents++;
421 len_remaining -= len_this_sg;
422 }
423 }
424 if (tsgl->nents == 0) {
425 partitions[tsgl->nents].div = &divs[0];
426 partitions[tsgl->nents].length = 0;
427 tsgl->nents++;
428 }
429 partitions[tsgl->nents - 1].length += len_remaining;
430
431 /* Set up the sgl entries and fill the data or poison */
432 sg_init_table(tsgl->sgl, tsgl->nents);
433 for (i = 0; i < tsgl->nents; i++) {
434 unsigned int offset = partitions[i].div->offset;
435 void *addr;
436
437 if (partitions[i].div->offset_relative_to_alignmask)
438 offset += alignmask;
439
440 while (offset + partitions[i].length + TESTMGR_POISON_LEN >
441 2 * PAGE_SIZE) {
442 if (WARN_ON(offset <= 0))
443 return -EINVAL;
444 offset /= 2;
445 }
446
447 addr = &tsgl->bufs[i][offset];
448 sg_set_buf(&tsgl->sgl[i], addr, partitions[i].length);
449
450 if (out_divs)
451 out_divs[i] = partitions[i].div;
452
453 if (data) {
454 size_t copy_len, copied;
455
456 copy_len = min(partitions[i].length, data->count);
457 copied = copy_from_iter(addr, copy_len, data);
458 if (WARN_ON(copied != copy_len))
459 return -EINVAL;
460 testmgr_poison(addr + copy_len, partitions[i].length +
461 TESTMGR_POISON_LEN - copy_len);
462 } else {
463 testmgr_poison(addr, partitions[i].length +
464 TESTMGR_POISON_LEN);
465 }
466 }
467
468 sg_mark_end(&tsgl->sgl[tsgl->nents - 1]);
469 tsgl->sgl_ptr = tsgl->sgl;
470 memcpy(tsgl->sgl_saved, tsgl->sgl, tsgl->nents * sizeof(tsgl->sgl[0]));
471 return 0;
472}
473
474/*
475 * Verify that a scatterlist crypto operation produced the correct output.
476 *
477 * @tsgl: scatterlist containing the actual output
478 * @expected_output: buffer containing the expected output
479 * @len_to_check: length of @expected_output in bytes
480 * @unchecked_prefix_len: number of ignored bytes in @tsgl prior to real result
481 * @check_poison: verify that the poison bytes after each chunk are intact?
482 *
483 * Return: 0 if correct, -EINVAL if incorrect, -EOVERFLOW if buffer overrun.
484 */
485static int verify_correct_output(const struct test_sglist *tsgl,
486 const char *expected_output,
487 unsigned int len_to_check,
488 unsigned int unchecked_prefix_len,
489 bool check_poison)
490{
491 unsigned int i;
492
493 for (i = 0; i < tsgl->nents; i++) {
494 struct scatterlist *sg = &tsgl->sgl_ptr[i];
495 unsigned int len = sg->length;
496 unsigned int offset = sg->offset;
497 const char *actual_output;
498
499 if (unchecked_prefix_len) {
500 if (unchecked_prefix_len >= len) {
501 unchecked_prefix_len -= len;
502 continue;
503 }
504 offset += unchecked_prefix_len;
505 len -= unchecked_prefix_len;
506 unchecked_prefix_len = 0;
507 }
508 len = min(len, len_to_check);
509 actual_output = page_address(sg_page(sg)) + offset;
510 if (memcmp(expected_output, actual_output, len) != 0)
511 return -EINVAL;
512 if (check_poison &&
513 !testmgr_is_poison(actual_output + len, TESTMGR_POISON_LEN))
514 return -EOVERFLOW;
515 len_to_check -= len;
516 expected_output += len;
517 }
518 if (WARN_ON(len_to_check != 0))
519 return -EINVAL;
520 return 0;
521}
522
523static bool is_test_sglist_corrupted(const struct test_sglist *tsgl)
524{
525 unsigned int i;
526
527 for (i = 0; i < tsgl->nents; i++) {
528 if (tsgl->sgl[i].page_link != tsgl->sgl_saved[i].page_link)
529 return true;
530 if (tsgl->sgl[i].offset != tsgl->sgl_saved[i].offset)
531 return true;
532 if (tsgl->sgl[i].length != tsgl->sgl_saved[i].length)
533 return true;
534 }
535 return false;
536}
537
538struct cipher_test_sglists {
539 struct test_sglist src;
540 struct test_sglist dst;
541};
542
543static struct cipher_test_sglists *alloc_cipher_test_sglists(void)
544{
545 struct cipher_test_sglists *tsgls;
546
547 tsgls = kmalloc(sizeof(*tsgls), GFP_KERNEL);
548 if (!tsgls)
549 return NULL;
550
551 if (init_test_sglist(&tsgls->src) != 0)
552 goto fail_kfree;
553 if (init_test_sglist(&tsgls->dst) != 0)
554 goto fail_destroy_src;
555
556 return tsgls;
557
558fail_destroy_src:
559 destroy_test_sglist(&tsgls->src);
560fail_kfree:
561 kfree(tsgls);
562 return NULL;
563}
564
565static void free_cipher_test_sglists(struct cipher_test_sglists *tsgls)
566{
567 if (tsgls) {
568 destroy_test_sglist(&tsgls->src);
569 destroy_test_sglist(&tsgls->dst);
570 kfree(tsgls);
571 }
572}
573
574/* Build the src and dst scatterlists for an skcipher or AEAD test */
575static int build_cipher_test_sglists(struct cipher_test_sglists *tsgls,
576 const struct testvec_config *cfg,
577 unsigned int alignmask,
578 unsigned int src_total_len,
579 unsigned int dst_total_len,
580 const struct kvec *inputs,
581 unsigned int nr_inputs)
582{
583 struct iov_iter input;
584 int err;
585
586 iov_iter_kvec(&input, WRITE, inputs, nr_inputs, src_total_len);
587 err = build_test_sglist(&tsgls->src, cfg->src_divs, alignmask,
588 cfg->inplace ?
589 max(dst_total_len, src_total_len) :
590 src_total_len,
591 &input, NULL);
592 if (err)
593 return err;
594
595 if (cfg->inplace) {
596 tsgls->dst.sgl_ptr = tsgls->src.sgl;
597 tsgls->dst.nents = tsgls->src.nents;
598 return 0;
599 }
600 return build_test_sglist(&tsgls->dst,
601 cfg->dst_divs[0].proportion_of_total ?
602 cfg->dst_divs : cfg->src_divs,
603 alignmask, dst_total_len, NULL, NULL);
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800604}
605
Kamil Konieczny466d7b92018-01-16 15:26:13 +0100606static int ahash_guard_result(char *result, char c, int size)
607{
608 int i;
609
610 for (i = 0; i < size; i++) {
611 if (result[i] != c)
612 return -EINVAL;
613 }
614
615 return 0;
616}
617
Wang, Rui Y018ba952016-02-03 18:26:57 +0800618static int ahash_partial_update(struct ahash_request **preq,
Eric Biggersb13b1e02017-02-24 15:46:59 -0800619 struct crypto_ahash *tfm, const struct hash_testvec *template,
Wang, Rui Y018ba952016-02-03 18:26:57 +0800620 void *hash_buff, int k, int temp, struct scatterlist *sg,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100621 const char *algo, char *result, struct crypto_wait *wait)
Wang, Rui Y018ba952016-02-03 18:26:57 +0800622{
623 char *state;
624 struct ahash_request *req;
625 int statesize, ret = -EINVAL;
Joey Pabalinasda1729c2018-01-01 10:40:14 -1000626 static const unsigned char guard[] = { 0x00, 0xba, 0xad, 0x00 };
Kamil Konieczny466d7b92018-01-16 15:26:13 +0100627 int digestsize = crypto_ahash_digestsize(tfm);
Wang, Rui Y018ba952016-02-03 18:26:57 +0800628
629 req = *preq;
630 statesize = crypto_ahash_statesize(
631 crypto_ahash_reqtfm(req));
Jan Stancek7bcb87b2016-09-28 16:38:37 +0200632 state = kmalloc(statesize + sizeof(guard), GFP_KERNEL);
Wang, Rui Y018ba952016-02-03 18:26:57 +0800633 if (!state) {
Gilad Ben-Yossefcf3f9602017-06-05 08:33:43 +0300634 pr_err("alg: hash: Failed to alloc state for %s\n", algo);
Wang, Rui Y018ba952016-02-03 18:26:57 +0800635 goto out_nostate;
636 }
Jan Stancek7bcb87b2016-09-28 16:38:37 +0200637 memcpy(state + statesize, guard, sizeof(guard));
Kamil Konieczny466d7b92018-01-16 15:26:13 +0100638 memset(result, 1, digestsize);
Wang, Rui Y018ba952016-02-03 18:26:57 +0800639 ret = crypto_ahash_export(req, state);
Jan Stancek7bcb87b2016-09-28 16:38:37 +0200640 WARN_ON(memcmp(state + statesize, guard, sizeof(guard)));
Wang, Rui Y018ba952016-02-03 18:26:57 +0800641 if (ret) {
Gilad Ben-Yossefcf3f9602017-06-05 08:33:43 +0300642 pr_err("alg: hash: Failed to export() for %s\n", algo);
Wang, Rui Y018ba952016-02-03 18:26:57 +0800643 goto out;
644 }
Kamil Konieczny466d7b92018-01-16 15:26:13 +0100645 ret = ahash_guard_result(result, 1, digestsize);
646 if (ret) {
647 pr_err("alg: hash: Failed, export used req->result for %s\n",
648 algo);
649 goto out;
650 }
Wang, Rui Y018ba952016-02-03 18:26:57 +0800651 ahash_request_free(req);
652 req = ahash_request_alloc(tfm, GFP_KERNEL);
653 if (!req) {
654 pr_err("alg: hash: Failed to alloc request for %s\n", algo);
655 goto out_noreq;
656 }
657 ahash_request_set_callback(req,
658 CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100659 crypto_req_done, wait);
Wang, Rui Y018ba952016-02-03 18:26:57 +0800660
661 memcpy(hash_buff, template->plaintext + temp,
662 template->tap[k]);
663 sg_init_one(&sg[0], hash_buff, template->tap[k]);
664 ahash_request_set_crypt(req, sg, result, template->tap[k]);
665 ret = crypto_ahash_import(req, state);
666 if (ret) {
667 pr_err("alg: hash: Failed to import() for %s\n", algo);
668 goto out;
669 }
Kamil Konieczny466d7b92018-01-16 15:26:13 +0100670 ret = ahash_guard_result(result, 1, digestsize);
671 if (ret) {
672 pr_err("alg: hash: Failed, import used req->result for %s\n",
673 algo);
674 goto out;
675 }
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100676 ret = crypto_wait_req(crypto_ahash_update(req), wait);
Wang, Rui Y018ba952016-02-03 18:26:57 +0800677 if (ret)
678 goto out;
679 *preq = req;
680 ret = 0;
681 goto out_noreq;
682out:
683 ahash_request_free(req);
684out_noreq:
685 kfree(state);
686out_nostate:
687 return ret;
688}
689
Gilad Ben-Yossef76715092018-07-01 08:02:35 +0100690enum hash_test {
691 HASH_TEST_DIGEST,
692 HASH_TEST_FINAL,
693 HASH_TEST_FINUP
694};
695
Eric Biggersb13b1e02017-02-24 15:46:59 -0800696static int __test_hash(struct crypto_ahash *tfm,
697 const struct hash_testvec *template, unsigned int tcount,
Gilad Ben-Yossef76715092018-07-01 08:02:35 +0100698 enum hash_test test_type, const int align_offset)
Herbert Xuda7f0332008-07-31 17:08:25 +0800699{
700 const char *algo = crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
Andrew Lutomirskie93acd62017-01-10 15:24:46 -0800701 size_t digest_size = crypto_ahash_digestsize(tfm);
Herbert Xuda7f0332008-07-31 17:08:25 +0800702 unsigned int i, j, k, temp;
703 struct scatterlist sg[8];
Horia Geanta29b77e52014-07-23 11:59:38 +0300704 char *result;
705 char *key;
Herbert Xuda7f0332008-07-31 17:08:25 +0800706 struct ahash_request *req;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100707 struct crypto_wait wait;
Herbert Xuda7f0332008-07-31 17:08:25 +0800708 void *hash_buff;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800709 char *xbuf[XBUFSIZE];
710 int ret = -ENOMEM;
711
Andrew Lutomirskie93acd62017-01-10 15:24:46 -0800712 result = kmalloc(digest_size, GFP_KERNEL);
Horia Geanta29b77e52014-07-23 11:59:38 +0300713 if (!result)
714 return ret;
715 key = kmalloc(MAX_KEYLEN, GFP_KERNEL);
716 if (!key)
717 goto out_nobuf;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800718 if (testmgr_alloc_buf(xbuf))
719 goto out_nobuf;
Herbert Xuda7f0332008-07-31 17:08:25 +0800720
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100721 crypto_init_wait(&wait);
Herbert Xuda7f0332008-07-31 17:08:25 +0800722
723 req = ahash_request_alloc(tfm, GFP_KERNEL);
724 if (!req) {
725 printk(KERN_ERR "alg: hash: Failed to allocate request for "
726 "%s\n", algo);
Herbert Xuda7f0332008-07-31 17:08:25 +0800727 goto out_noreq;
728 }
729 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100730 crypto_req_done, &wait);
Herbert Xuda7f0332008-07-31 17:08:25 +0800731
Herbert Xua0cfae52009-05-29 16:23:12 +1000732 j = 0;
Herbert Xuda7f0332008-07-31 17:08:25 +0800733 for (i = 0; i < tcount; i++) {
Herbert Xua0cfae52009-05-29 16:23:12 +1000734 if (template[i].np)
735 continue;
736
Jussi Kivilinnada5ffe12013-06-13 17:37:55 +0300737 ret = -EINVAL;
738 if (WARN_ON(align_offset + template[i].psize > PAGE_SIZE))
739 goto out;
740
Herbert Xua0cfae52009-05-29 16:23:12 +1000741 j++;
Andrew Lutomirskie93acd62017-01-10 15:24:46 -0800742 memset(result, 0, digest_size);
Herbert Xuda7f0332008-07-31 17:08:25 +0800743
744 hash_buff = xbuf[0];
Jussi Kivilinnada5ffe12013-06-13 17:37:55 +0300745 hash_buff += align_offset;
Herbert Xuda7f0332008-07-31 17:08:25 +0800746
747 memcpy(hash_buff, template[i].plaintext, template[i].psize);
748 sg_init_one(&sg[0], hash_buff, template[i].psize);
749
750 if (template[i].ksize) {
751 crypto_ahash_clear_flags(tfm, ~0);
Horia Geanta29b77e52014-07-23 11:59:38 +0300752 if (template[i].ksize > MAX_KEYLEN) {
753 pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
754 j, algo, template[i].ksize, MAX_KEYLEN);
755 ret = -EINVAL;
756 goto out;
757 }
758 memcpy(key, template[i].key, template[i].ksize);
759 ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
Herbert Xuda7f0332008-07-31 17:08:25 +0800760 if (ret) {
761 printk(KERN_ERR "alg: hash: setkey failed on "
Herbert Xua0cfae52009-05-29 16:23:12 +1000762 "test %d for %s: ret=%d\n", j, algo,
Herbert Xuda7f0332008-07-31 17:08:25 +0800763 -ret);
764 goto out;
765 }
766 }
767
768 ahash_request_set_crypt(req, sg, result, template[i].psize);
Gilad Ben-Yossef76715092018-07-01 08:02:35 +0100769 switch (test_type) {
770 case HASH_TEST_DIGEST:
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100771 ret = crypto_wait_req(crypto_ahash_digest(req), &wait);
David S. Millera8f1a052010-05-19 14:12:03 +1000772 if (ret) {
773 pr_err("alg: hash: digest failed on test %d "
774 "for %s: ret=%d\n", j, algo, -ret);
775 goto out;
Herbert Xuda7f0332008-07-31 17:08:25 +0800776 }
Gilad Ben-Yossef76715092018-07-01 08:02:35 +0100777 break;
778
779 case HASH_TEST_FINAL:
Kamil Konieczny466d7b92018-01-16 15:26:13 +0100780 memset(result, 1, digest_size);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100781 ret = crypto_wait_req(crypto_ahash_init(req), &wait);
David S. Millera8f1a052010-05-19 14:12:03 +1000782 if (ret) {
Gilad Ben-Yossefcf3f9602017-06-05 08:33:43 +0300783 pr_err("alg: hash: init failed on test %d "
David S. Millera8f1a052010-05-19 14:12:03 +1000784 "for %s: ret=%d\n", j, algo, -ret);
785 goto out;
786 }
Kamil Konieczny466d7b92018-01-16 15:26:13 +0100787 ret = ahash_guard_result(result, 1, digest_size);
788 if (ret) {
789 pr_err("alg: hash: init failed on test %d "
790 "for %s: used req->result\n", j, algo);
791 goto out;
792 }
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100793 ret = crypto_wait_req(crypto_ahash_update(req), &wait);
David S. Millera8f1a052010-05-19 14:12:03 +1000794 if (ret) {
Gilad Ben-Yossefcf3f9602017-06-05 08:33:43 +0300795 pr_err("alg: hash: update failed on test %d "
David S. Millera8f1a052010-05-19 14:12:03 +1000796 "for %s: ret=%d\n", j, algo, -ret);
797 goto out;
798 }
Kamil Konieczny466d7b92018-01-16 15:26:13 +0100799 ret = ahash_guard_result(result, 1, digest_size);
800 if (ret) {
801 pr_err("alg: hash: update failed on test %d "
802 "for %s: used req->result\n", j, algo);
803 goto out;
804 }
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100805 ret = crypto_wait_req(crypto_ahash_final(req), &wait);
David S. Millera8f1a052010-05-19 14:12:03 +1000806 if (ret) {
Gilad Ben-Yossefcf3f9602017-06-05 08:33:43 +0300807 pr_err("alg: hash: final failed on test %d "
David S. Millera8f1a052010-05-19 14:12:03 +1000808 "for %s: ret=%d\n", j, algo, -ret);
809 goto out;
810 }
Gilad Ben-Yossef76715092018-07-01 08:02:35 +0100811 break;
812
813 case HASH_TEST_FINUP:
814 memset(result, 1, digest_size);
815 ret = crypto_wait_req(crypto_ahash_init(req), &wait);
816 if (ret) {
817 pr_err("alg: hash: init failed on test %d "
818 "for %s: ret=%d\n", j, algo, -ret);
819 goto out;
820 }
821 ret = ahash_guard_result(result, 1, digest_size);
822 if (ret) {
823 pr_err("alg: hash: init failed on test %d "
824 "for %s: used req->result\n", j, algo);
825 goto out;
826 }
827 ret = crypto_wait_req(crypto_ahash_finup(req), &wait);
828 if (ret) {
829 pr_err("alg: hash: final failed on test %d "
830 "for %s: ret=%d\n", j, algo, -ret);
831 goto out;
832 }
833 break;
Herbert Xuda7f0332008-07-31 17:08:25 +0800834 }
835
836 if (memcmp(result, template[i].digest,
837 crypto_ahash_digestsize(tfm))) {
838 printk(KERN_ERR "alg: hash: Test %d failed for %s\n",
Herbert Xua0cfae52009-05-29 16:23:12 +1000839 j, algo);
Herbert Xuda7f0332008-07-31 17:08:25 +0800840 hexdump(result, crypto_ahash_digestsize(tfm));
841 ret = -EINVAL;
842 goto out;
843 }
844 }
845
Gilad Ben-Yossef76715092018-07-01 08:02:35 +0100846 if (test_type)
847 goto out;
848
Herbert Xuda7f0332008-07-31 17:08:25 +0800849 j = 0;
850 for (i = 0; i < tcount; i++) {
Jussi Kivilinnada5ffe12013-06-13 17:37:55 +0300851 /* alignment tests are only done with continuous buffers */
852 if (align_offset != 0)
853 break;
854
Cristian Stoica5f2b4242014-08-08 14:27:50 +0300855 if (!template[i].np)
856 continue;
Herbert Xuda7f0332008-07-31 17:08:25 +0800857
Cristian Stoica5f2b4242014-08-08 14:27:50 +0300858 j++;
Andrew Lutomirskie93acd62017-01-10 15:24:46 -0800859 memset(result, 0, digest_size);
Herbert Xuda7f0332008-07-31 17:08:25 +0800860
Cristian Stoica5f2b4242014-08-08 14:27:50 +0300861 temp = 0;
862 sg_init_table(sg, template[i].np);
863 ret = -EINVAL;
864 for (k = 0; k < template[i].np; k++) {
865 if (WARN_ON(offset_in_page(IDX[k]) +
866 template[i].tap[k] > PAGE_SIZE))
Herbert Xuda7f0332008-07-31 17:08:25 +0800867 goto out;
Cristian Stoica5f2b4242014-08-08 14:27:50 +0300868 sg_set_buf(&sg[k],
869 memcpy(xbuf[IDX[k] >> PAGE_SHIFT] +
870 offset_in_page(IDX[k]),
871 template[i].plaintext + temp,
872 template[i].tap[k]),
873 template[i].tap[k]);
874 temp += template[i].tap[k];
875 }
Herbert Xuda7f0332008-07-31 17:08:25 +0800876
Cristian Stoica5f2b4242014-08-08 14:27:50 +0300877 if (template[i].ksize) {
878 if (template[i].ksize > MAX_KEYLEN) {
879 pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
880 j, algo, template[i].ksize, MAX_KEYLEN);
Herbert Xuda7f0332008-07-31 17:08:25 +0800881 ret = -EINVAL;
882 goto out;
883 }
Cristian Stoica5f2b4242014-08-08 14:27:50 +0300884 crypto_ahash_clear_flags(tfm, ~0);
885 memcpy(key, template[i].key, template[i].ksize);
886 ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
887
888 if (ret) {
889 printk(KERN_ERR "alg: hash: setkey "
890 "failed on chunking test %d "
891 "for %s: ret=%d\n", j, algo, -ret);
892 goto out;
893 }
894 }
895
896 ahash_request_set_crypt(req, sg, result, template[i].psize);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100897 ret = crypto_wait_req(crypto_ahash_digest(req), &wait);
898 if (ret) {
899 pr_err("alg: hash: digest failed on chunking test %d for %s: ret=%d\n",
900 j, algo, -ret);
Cristian Stoica5f2b4242014-08-08 14:27:50 +0300901 goto out;
902 }
903
904 if (memcmp(result, template[i].digest,
905 crypto_ahash_digestsize(tfm))) {
906 printk(KERN_ERR "alg: hash: Chunking test %d "
907 "failed for %s\n", j, algo);
908 hexdump(result, crypto_ahash_digestsize(tfm));
909 ret = -EINVAL;
910 goto out;
Herbert Xuda7f0332008-07-31 17:08:25 +0800911 }
912 }
913
Wang, Rui Y018ba952016-02-03 18:26:57 +0800914 /* partial update exercise */
915 j = 0;
916 for (i = 0; i < tcount; i++) {
917 /* alignment tests are only done with continuous buffers */
918 if (align_offset != 0)
919 break;
920
921 if (template[i].np < 2)
922 continue;
923
924 j++;
Andrew Lutomirskie93acd62017-01-10 15:24:46 -0800925 memset(result, 0, digest_size);
Wang, Rui Y018ba952016-02-03 18:26:57 +0800926
927 ret = -EINVAL;
928 hash_buff = xbuf[0];
929 memcpy(hash_buff, template[i].plaintext,
930 template[i].tap[0]);
931 sg_init_one(&sg[0], hash_buff, template[i].tap[0]);
932
933 if (template[i].ksize) {
934 crypto_ahash_clear_flags(tfm, ~0);
935 if (template[i].ksize > MAX_KEYLEN) {
936 pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
937 j, algo, template[i].ksize, MAX_KEYLEN);
938 ret = -EINVAL;
939 goto out;
940 }
941 memcpy(key, template[i].key, template[i].ksize);
942 ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
943 if (ret) {
944 pr_err("alg: hash: setkey failed on test %d for %s: ret=%d\n",
945 j, algo, -ret);
946 goto out;
947 }
948 }
949
950 ahash_request_set_crypt(req, sg, result, template[i].tap[0]);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100951 ret = crypto_wait_req(crypto_ahash_init(req), &wait);
Wang, Rui Y018ba952016-02-03 18:26:57 +0800952 if (ret) {
Gilad Ben-Yossefcf3f9602017-06-05 08:33:43 +0300953 pr_err("alg: hash: init failed on test %d for %s: ret=%d\n",
Wang, Rui Y018ba952016-02-03 18:26:57 +0800954 j, algo, -ret);
955 goto out;
956 }
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100957 ret = crypto_wait_req(crypto_ahash_update(req), &wait);
Wang, Rui Y018ba952016-02-03 18:26:57 +0800958 if (ret) {
Gilad Ben-Yossefcf3f9602017-06-05 08:33:43 +0300959 pr_err("alg: hash: update failed on test %d for %s: ret=%d\n",
Wang, Rui Y018ba952016-02-03 18:26:57 +0800960 j, algo, -ret);
961 goto out;
962 }
963
964 temp = template[i].tap[0];
965 for (k = 1; k < template[i].np; k++) {
966 ret = ahash_partial_update(&req, tfm, &template[i],
967 hash_buff, k, temp, &sg[0], algo, result,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100968 &wait);
Wang, Rui Y018ba952016-02-03 18:26:57 +0800969 if (ret) {
Gilad Ben-Yossefcf3f9602017-06-05 08:33:43 +0300970 pr_err("alg: hash: partial update failed on test %d for %s: ret=%d\n",
Wang, Rui Y018ba952016-02-03 18:26:57 +0800971 j, algo, -ret);
972 goto out_noreq;
973 }
974 temp += template[i].tap[k];
975 }
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100976 ret = crypto_wait_req(crypto_ahash_final(req), &wait);
Wang, Rui Y018ba952016-02-03 18:26:57 +0800977 if (ret) {
Gilad Ben-Yossefcf3f9602017-06-05 08:33:43 +0300978 pr_err("alg: hash: final failed on test %d for %s: ret=%d\n",
Wang, Rui Y018ba952016-02-03 18:26:57 +0800979 j, algo, -ret);
980 goto out;
981 }
982 if (memcmp(result, template[i].digest,
983 crypto_ahash_digestsize(tfm))) {
984 pr_err("alg: hash: Partial Test %d failed for %s\n",
985 j, algo);
986 hexdump(result, crypto_ahash_digestsize(tfm));
987 ret = -EINVAL;
988 goto out;
989 }
990 }
991
Herbert Xuda7f0332008-07-31 17:08:25 +0800992 ret = 0;
993
994out:
995 ahash_request_free(req);
996out_noreq:
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800997 testmgr_free_buf(xbuf);
998out_nobuf:
Horia Geanta29b77e52014-07-23 11:59:38 +0300999 kfree(key);
1000 kfree(result);
Herbert Xuda7f0332008-07-31 17:08:25 +08001001 return ret;
1002}
1003
Eric Biggersb13b1e02017-02-24 15:46:59 -08001004static int test_hash(struct crypto_ahash *tfm,
1005 const struct hash_testvec *template,
Gilad Ben-Yossef76715092018-07-01 08:02:35 +01001006 unsigned int tcount, enum hash_test test_type)
Jussi Kivilinnada5ffe12013-06-13 17:37:55 +03001007{
1008 unsigned int alignmask;
1009 int ret;
1010
Gilad Ben-Yossef76715092018-07-01 08:02:35 +01001011 ret = __test_hash(tfm, template, tcount, test_type, 0);
Jussi Kivilinnada5ffe12013-06-13 17:37:55 +03001012 if (ret)
1013 return ret;
1014
1015 /* test unaligned buffers, check with one byte offset */
Gilad Ben-Yossef76715092018-07-01 08:02:35 +01001016 ret = __test_hash(tfm, template, tcount, test_type, 1);
Jussi Kivilinnada5ffe12013-06-13 17:37:55 +03001017 if (ret)
1018 return ret;
1019
1020 alignmask = crypto_tfm_alg_alignmask(&tfm->base);
1021 if (alignmask) {
1022 /* Check if alignment mask for tfm is correctly set. */
Gilad Ben-Yossef76715092018-07-01 08:02:35 +01001023 ret = __test_hash(tfm, template, tcount, test_type,
Jussi Kivilinnada5ffe12013-06-13 17:37:55 +03001024 alignmask + 1);
1025 if (ret)
1026 return ret;
1027 }
1028
1029 return 0;
1030}
1031
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001032static int __test_aead(struct crypto_aead *tfm, int enc,
Eric Biggersb13b1e02017-02-24 15:46:59 -08001033 const struct aead_testvec *template, unsigned int tcount,
Jussi Kivilinna58dcf542013-06-13 17:37:50 +03001034 const bool diff_dst, const int align_offset)
Herbert Xuda7f0332008-07-31 17:08:25 +08001035{
1036 const char *algo = crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm));
1037 unsigned int i, j, k, n, temp;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001038 int ret = -ENOMEM;
Herbert Xuda7f0332008-07-31 17:08:25 +08001039 char *q;
1040 char *key;
1041 struct aead_request *req;
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001042 struct scatterlist *sg;
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001043 struct scatterlist *sgout;
1044 const char *e, *d;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001045 struct crypto_wait wait;
Cristian Stoica424a5da2015-01-28 11:03:05 +02001046 unsigned int authsize, iv_len;
Tadeusz Struk9bac0192014-05-19 09:51:33 -07001047 char *iv;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001048 char *xbuf[XBUFSIZE];
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001049 char *xoutbuf[XBUFSIZE];
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001050 char *axbuf[XBUFSIZE];
1051
Tadeusz Struk9bac0192014-05-19 09:51:33 -07001052 iv = kzalloc(MAX_IVLEN, GFP_KERNEL);
1053 if (!iv)
1054 return ret;
Horia Geanta29b77e52014-07-23 11:59:38 +03001055 key = kmalloc(MAX_KEYLEN, GFP_KERNEL);
1056 if (!key)
1057 goto out_noxbuf;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001058 if (testmgr_alloc_buf(xbuf))
1059 goto out_noxbuf;
1060 if (testmgr_alloc_buf(axbuf))
1061 goto out_noaxbuf;
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001062 if (diff_dst && testmgr_alloc_buf(xoutbuf))
1063 goto out_nooutbuf;
1064
1065 /* avoid "the frame size is larger than 1024 bytes" compiler warning */
Kees Cook6da2ec52018-06-12 13:55:00 -07001066 sg = kmalloc(array3_size(sizeof(*sg), 8, (diff_dst ? 4 : 2)),
1067 GFP_KERNEL);
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001068 if (!sg)
1069 goto out_nosg;
Herbert Xu8a525fcd2015-05-27 16:03:43 +08001070 sgout = &sg[16];
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001071
1072 if (diff_dst)
1073 d = "-ddst";
1074 else
1075 d = "";
1076
Herbert Xuda7f0332008-07-31 17:08:25 +08001077 if (enc == ENCRYPT)
1078 e = "encryption";
1079 else
1080 e = "decryption";
1081
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001082 crypto_init_wait(&wait);
Herbert Xuda7f0332008-07-31 17:08:25 +08001083
1084 req = aead_request_alloc(tfm, GFP_KERNEL);
1085 if (!req) {
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001086 pr_err("alg: aead%s: Failed to allocate request for %s\n",
1087 d, algo);
Herbert Xuda7f0332008-07-31 17:08:25 +08001088 goto out;
1089 }
1090
1091 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001092 crypto_req_done, &wait);
Herbert Xuda7f0332008-07-31 17:08:25 +08001093
Jerome Marchandabfa7f42016-02-03 13:58:12 +01001094 iv_len = crypto_aead_ivsize(tfm);
1095
Herbert Xuda7f0332008-07-31 17:08:25 +08001096 for (i = 0, j = 0; i < tcount; i++) {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08001097 const char *input, *expected_output;
1098 unsigned int inlen, outlen;
1099 char *inbuf, *outbuf, *assocbuf;
1100
Cristian Stoica05b1d332014-07-28 13:11:23 +03001101 if (template[i].np)
1102 continue;
Eric Biggersa0d608ee2019-01-13 15:32:28 -08001103 if (enc) {
1104 if (template[i].novrfy)
1105 continue;
1106 input = template[i].ptext;
1107 inlen = template[i].plen;
1108 expected_output = template[i].ctext;
1109 outlen = template[i].clen;
1110 } else {
1111 input = template[i].ctext;
1112 inlen = template[i].clen;
1113 expected_output = template[i].ptext;
1114 outlen = template[i].plen;
1115 }
Herbert Xuda7f0332008-07-31 17:08:25 +08001116
Cristian Stoica05b1d332014-07-28 13:11:23 +03001117 j++;
Herbert Xuda7f0332008-07-31 17:08:25 +08001118
Cristian Stoica05b1d332014-07-28 13:11:23 +03001119 /* some templates have no input data but they will
1120 * touch input
1121 */
Eric Biggersa0d608ee2019-01-13 15:32:28 -08001122 inbuf = xbuf[0] + align_offset;
1123 assocbuf = axbuf[0];
Cristian Stoica05b1d332014-07-28 13:11:23 +03001124
1125 ret = -EINVAL;
Eric Biggersa0d608ee2019-01-13 15:32:28 -08001126 if (WARN_ON(align_offset + template[i].clen > PAGE_SIZE ||
1127 template[i].alen > PAGE_SIZE))
Cristian Stoica05b1d332014-07-28 13:11:23 +03001128 goto out;
1129
Eric Biggersa0d608ee2019-01-13 15:32:28 -08001130 memcpy(inbuf, input, inlen);
1131 memcpy(assocbuf, template[i].assoc, template[i].alen);
Cristian Stoica05b1d332014-07-28 13:11:23 +03001132 if (template[i].iv)
Cristian Stoica424a5da2015-01-28 11:03:05 +02001133 memcpy(iv, template[i].iv, iv_len);
Cristian Stoica05b1d332014-07-28 13:11:23 +03001134 else
Cristian Stoica424a5da2015-01-28 11:03:05 +02001135 memset(iv, 0, iv_len);
Cristian Stoica05b1d332014-07-28 13:11:23 +03001136
1137 crypto_aead_clear_flags(tfm, ~0);
1138 if (template[i].wk)
Eric Biggers231baec2019-01-18 22:48:00 -08001139 crypto_aead_set_flags(tfm,
1140 CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Cristian Stoica05b1d332014-07-28 13:11:23 +03001141
1142 if (template[i].klen > MAX_KEYLEN) {
1143 pr_err("alg: aead%s: setkey failed on test %d for %s: key size %d > %d\n",
1144 d, j, algo, template[i].klen,
1145 MAX_KEYLEN);
Herbert Xufd57f222009-05-29 16:05:42 +10001146 ret = -EINVAL;
Cristian Stoica05b1d332014-07-28 13:11:23 +03001147 goto out;
1148 }
1149 memcpy(key, template[i].key, template[i].klen);
Herbert Xufd57f222009-05-29 16:05:42 +10001150
Cristian Stoica05b1d332014-07-28 13:11:23 +03001151 ret = crypto_aead_setkey(tfm, key, template[i].klen);
Yanjiang Jin0fae0c12016-07-29 16:32:09 +08001152 if (template[i].fail == !ret) {
Cristian Stoica05b1d332014-07-28 13:11:23 +03001153 pr_err("alg: aead%s: setkey failed on test %d for %s: flags=%x\n",
1154 d, j, algo, crypto_aead_get_flags(tfm));
1155 goto out;
1156 } else if (ret)
1157 continue;
Herbert Xuda7f0332008-07-31 17:08:25 +08001158
Eric Biggersa0d608ee2019-01-13 15:32:28 -08001159 authsize = template[i].clen - template[i].plen;
Cristian Stoica05b1d332014-07-28 13:11:23 +03001160 ret = crypto_aead_setauthsize(tfm, authsize);
1161 if (ret) {
1162 pr_err("alg: aead%s: Failed to set authsize to %u on test %d for %s\n",
1163 d, authsize, j, algo);
1164 goto out;
1165 }
Herbert Xuda7f0332008-07-31 17:08:25 +08001166
Herbert Xu8a525fcd2015-05-27 16:03:43 +08001167 k = !!template[i].alen;
1168 sg_init_table(sg, k + 1);
Eric Biggersa0d608ee2019-01-13 15:32:28 -08001169 sg_set_buf(&sg[0], assocbuf, template[i].alen);
1170 sg_set_buf(&sg[k], inbuf, template[i].clen);
1171 outbuf = inbuf;
Herbert Xu8a525fcd2015-05-27 16:03:43 +08001172
Cristian Stoica05b1d332014-07-28 13:11:23 +03001173 if (diff_dst) {
Herbert Xu8a525fcd2015-05-27 16:03:43 +08001174 sg_init_table(sgout, k + 1);
Eric Biggersa0d608ee2019-01-13 15:32:28 -08001175 sg_set_buf(&sgout[0], assocbuf, template[i].alen);
Herbert Xu8a525fcd2015-05-27 16:03:43 +08001176
Eric Biggersa0d608ee2019-01-13 15:32:28 -08001177 outbuf = xoutbuf[0] + align_offset;
1178 sg_set_buf(&sgout[k], outbuf, template[i].clen);
Cristian Stoica05b1d332014-07-28 13:11:23 +03001179 }
1180
Eric Biggersa0d608ee2019-01-13 15:32:28 -08001181 aead_request_set_crypt(req, sg, (diff_dst) ? sgout : sg, inlen,
1182 iv);
Cristian Stoica05b1d332014-07-28 13:11:23 +03001183
Herbert Xu8a525fcd2015-05-27 16:03:43 +08001184 aead_request_set_ad(req, template[i].alen);
Cristian Stoica05b1d332014-07-28 13:11:23 +03001185
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001186 ret = crypto_wait_req(enc ? crypto_aead_encrypt(req)
1187 : crypto_aead_decrypt(req), &wait);
Cristian Stoica05b1d332014-07-28 13:11:23 +03001188
1189 switch (ret) {
1190 case 0:
1191 if (template[i].novrfy) {
1192 /* verification was supposed to fail */
1193 pr_err("alg: aead%s: %s failed on test %d for %s: ret was 0, expected -EBADMSG\n",
1194 d, e, j, algo);
1195 /* so really, we got a bad message */
1196 ret = -EBADMSG;
Horia Geanta29b77e52014-07-23 11:59:38 +03001197 goto out;
1198 }
Cristian Stoica05b1d332014-07-28 13:11:23 +03001199 break;
Cristian Stoica05b1d332014-07-28 13:11:23 +03001200 case -EBADMSG:
1201 if (template[i].novrfy)
1202 /* verification failure was expected */
1203 continue;
1204 /* fall through */
1205 default:
1206 pr_err("alg: aead%s: %s failed on test %d for %s: ret=%d\n",
1207 d, e, j, algo, -ret);
1208 goto out;
1209 }
Herbert Xuda7f0332008-07-31 17:08:25 +08001210
Eric Biggersa0d608ee2019-01-13 15:32:28 -08001211 if (memcmp(outbuf, expected_output, outlen)) {
Cristian Stoica05b1d332014-07-28 13:11:23 +03001212 pr_err("alg: aead%s: Test %d failed on %s for %s\n",
1213 d, j, e, algo);
Eric Biggersa0d608ee2019-01-13 15:32:28 -08001214 hexdump(outbuf, outlen);
Cristian Stoica05b1d332014-07-28 13:11:23 +03001215 ret = -EINVAL;
1216 goto out;
Herbert Xuda7f0332008-07-31 17:08:25 +08001217 }
1218 }
1219
1220 for (i = 0, j = 0; i < tcount; i++) {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08001221 const char *input, *expected_output;
1222 unsigned int inlen, outlen;
1223
Jussi Kivilinna58dcf542013-06-13 17:37:50 +03001224 /* alignment tests are only done with continuous buffers */
1225 if (align_offset != 0)
1226 break;
1227
Cristian Stoica05b1d332014-07-28 13:11:23 +03001228 if (!template[i].np)
1229 continue;
Herbert Xuda7f0332008-07-31 17:08:25 +08001230
Eric Biggersa0d608ee2019-01-13 15:32:28 -08001231 if (enc) {
1232 if (template[i].novrfy)
1233 continue;
1234 input = template[i].ptext;
1235 inlen = template[i].plen;
1236 expected_output = template[i].ctext;
1237 outlen = template[i].clen;
1238 } else {
1239 input = template[i].ctext;
1240 inlen = template[i].clen;
1241 expected_output = template[i].ptext;
1242 outlen = template[i].plen;
1243 }
Eric Biggers5bc3de52019-01-13 15:32:24 -08001244
Cristian Stoica05b1d332014-07-28 13:11:23 +03001245 j++;
Herbert Xuda7f0332008-07-31 17:08:25 +08001246
Cristian Stoica05b1d332014-07-28 13:11:23 +03001247 if (template[i].iv)
Jerome Marchandabfa7f42016-02-03 13:58:12 +01001248 memcpy(iv, template[i].iv, iv_len);
Cristian Stoica05b1d332014-07-28 13:11:23 +03001249 else
1250 memset(iv, 0, MAX_IVLEN);
1251
1252 crypto_aead_clear_flags(tfm, ~0);
1253 if (template[i].wk)
Eric Biggers231baec2019-01-18 22:48:00 -08001254 crypto_aead_set_flags(tfm,
1255 CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Cristian Stoica05b1d332014-07-28 13:11:23 +03001256 if (template[i].klen > MAX_KEYLEN) {
1257 pr_err("alg: aead%s: setkey failed on test %d for %s: key size %d > %d\n",
1258 d, j, algo, template[i].klen, MAX_KEYLEN);
1259 ret = -EINVAL;
1260 goto out;
1261 }
1262 memcpy(key, template[i].key, template[i].klen);
1263
1264 ret = crypto_aead_setkey(tfm, key, template[i].klen);
Yanjiang Jin0fae0c12016-07-29 16:32:09 +08001265 if (template[i].fail == !ret) {
Cristian Stoica05b1d332014-07-28 13:11:23 +03001266 pr_err("alg: aead%s: setkey failed on chunk test %d for %s: flags=%x\n",
1267 d, j, algo, crypto_aead_get_flags(tfm));
1268 goto out;
1269 } else if (ret)
1270 continue;
1271
Eric Biggersa0d608ee2019-01-13 15:32:28 -08001272 authsize = template[i].clen - template[i].plen;
Cristian Stoica05b1d332014-07-28 13:11:23 +03001273
1274 ret = -EINVAL;
Herbert Xu8a525fcd2015-05-27 16:03:43 +08001275 sg_init_table(sg, template[i].anp + template[i].np);
Cristian Stoica05b1d332014-07-28 13:11:23 +03001276 if (diff_dst)
Herbert Xu8a525fcd2015-05-27 16:03:43 +08001277 sg_init_table(sgout, template[i].anp + template[i].np);
1278
1279 ret = -EINVAL;
1280 for (k = 0, temp = 0; k < template[i].anp; k++) {
1281 if (WARN_ON(offset_in_page(IDX[k]) +
1282 template[i].atap[k] > PAGE_SIZE))
1283 goto out;
1284 sg_set_buf(&sg[k],
1285 memcpy(axbuf[IDX[k] >> PAGE_SHIFT] +
1286 offset_in_page(IDX[k]),
1287 template[i].assoc + temp,
1288 template[i].atap[k]),
1289 template[i].atap[k]);
1290 if (diff_dst)
1291 sg_set_buf(&sgout[k],
1292 axbuf[IDX[k] >> PAGE_SHIFT] +
1293 offset_in_page(IDX[k]),
1294 template[i].atap[k]);
1295 temp += template[i].atap[k];
1296 }
1297
Cristian Stoica05b1d332014-07-28 13:11:23 +03001298 for (k = 0, temp = 0; k < template[i].np; k++) {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08001299 n = template[i].tap[k];
1300 if (k == template[i].np - 1 && !enc)
1301 n += authsize;
1302
1303 if (WARN_ON(offset_in_page(IDX[k]) + n > PAGE_SIZE))
Cristian Stoica05b1d332014-07-28 13:11:23 +03001304 goto out;
1305
1306 q = xbuf[IDX[k] >> PAGE_SHIFT] + offset_in_page(IDX[k]);
Eric Biggersa0d608ee2019-01-13 15:32:28 -08001307 memcpy(q, input + temp, n);
1308 sg_set_buf(&sg[template[i].anp + k], q, n);
Cristian Stoica05b1d332014-07-28 13:11:23 +03001309
1310 if (diff_dst) {
1311 q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
1312 offset_in_page(IDX[k]);
1313
Eric Biggersa0d608ee2019-01-13 15:32:28 -08001314 memset(q, 0, n);
Cristian Stoica05b1d332014-07-28 13:11:23 +03001315
Eric Biggersa0d608ee2019-01-13 15:32:28 -08001316 sg_set_buf(&sgout[template[i].anp + k], q, n);
Cristian Stoica05b1d332014-07-28 13:11:23 +03001317 }
1318
Cristian Stoica05b1d332014-07-28 13:11:23 +03001319 if (k == template[i].np - 1 && enc)
1320 n += authsize;
1321 if (offset_in_page(q) + n < PAGE_SIZE)
1322 q[n] = 0;
1323
Eric Biggersa0d608ee2019-01-13 15:32:28 -08001324 temp += n;
Cristian Stoica05b1d332014-07-28 13:11:23 +03001325 }
1326
1327 ret = crypto_aead_setauthsize(tfm, authsize);
1328 if (ret) {
1329 pr_err("alg: aead%s: Failed to set authsize to %u on chunk test %d for %s\n",
1330 d, authsize, j, algo);
1331 goto out;
1332 }
1333
1334 if (enc) {
Herbert Xu8a525fcd2015-05-27 16:03:43 +08001335 if (WARN_ON(sg[template[i].anp + k - 1].offset +
1336 sg[template[i].anp + k - 1].length +
1337 authsize > PAGE_SIZE)) {
Horia Geanta29b77e52014-07-23 11:59:38 +03001338 ret = -EINVAL;
1339 goto out;
1340 }
Herbert Xuda7f0332008-07-31 17:08:25 +08001341
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001342 if (diff_dst)
Herbert Xu8a525fcd2015-05-27 16:03:43 +08001343 sgout[template[i].anp + k - 1].length +=
1344 authsize;
1345 sg[template[i].anp + k - 1].length += authsize;
Cristian Stoica05b1d332014-07-28 13:11:23 +03001346 }
1347
1348 aead_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
Eric Biggersa0d608ee2019-01-13 15:32:28 -08001349 inlen, iv);
Cristian Stoica05b1d332014-07-28 13:11:23 +03001350
Herbert Xu8a525fcd2015-05-27 16:03:43 +08001351 aead_request_set_ad(req, template[i].alen);
Cristian Stoica05b1d332014-07-28 13:11:23 +03001352
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001353 ret = crypto_wait_req(enc ? crypto_aead_encrypt(req)
1354 : crypto_aead_decrypt(req), &wait);
Cristian Stoica05b1d332014-07-28 13:11:23 +03001355
1356 switch (ret) {
1357 case 0:
1358 if (template[i].novrfy) {
1359 /* verification was supposed to fail */
1360 pr_err("alg: aead%s: %s failed on chunk test %d for %s: ret was 0, expected -EBADMSG\n",
1361 d, e, j, algo);
1362 /* so really, we got a bad message */
1363 ret = -EBADMSG;
1364 goto out;
1365 }
1366 break;
Cristian Stoica05b1d332014-07-28 13:11:23 +03001367 case -EBADMSG:
1368 if (template[i].novrfy)
1369 /* verification failure was expected */
1370 continue;
1371 /* fall through */
1372 default:
1373 pr_err("alg: aead%s: %s failed on chunk test %d for %s: ret=%d\n",
1374 d, e, j, algo, -ret);
1375 goto out;
1376 }
1377
1378 ret = -EINVAL;
1379 for (k = 0, temp = 0; k < template[i].np; k++) {
1380 if (diff_dst)
1381 q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
1382 offset_in_page(IDX[k]);
1383 else
Herbert Xuda7f0332008-07-31 17:08:25 +08001384 q = xbuf[IDX[k] >> PAGE_SHIFT] +
1385 offset_in_page(IDX[k]);
1386
Cristian Stoica05b1d332014-07-28 13:11:23 +03001387 n = template[i].tap[k];
Eric Biggersa0d608ee2019-01-13 15:32:28 -08001388 if (k == template[i].np - 1 && enc)
1389 n += authsize;
Herbert Xuda7f0332008-07-31 17:08:25 +08001390
Eric Biggersa0d608ee2019-01-13 15:32:28 -08001391 if (memcmp(q, expected_output + temp, n)) {
Cristian Stoica05b1d332014-07-28 13:11:23 +03001392 pr_err("alg: aead%s: Chunk test %d failed on %s at page %u for %s\n",
1393 d, j, e, k, algo);
1394 hexdump(q, n);
Herbert Xuda7f0332008-07-31 17:08:25 +08001395 goto out;
1396 }
1397
Cristian Stoica05b1d332014-07-28 13:11:23 +03001398 q += n;
1399 if (k == template[i].np - 1 && !enc) {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08001400 if (!diff_dst && memcmp(q, input + temp + n,
1401 authsize))
Cristian Stoica05b1d332014-07-28 13:11:23 +03001402 n = authsize;
Horia Geanta8ec25c52013-11-28 15:11:18 +02001403 else
Cristian Stoica05b1d332014-07-28 13:11:23 +03001404 n = 0;
1405 } else {
1406 for (n = 0; offset_in_page(q + n) && q[n]; n++)
1407 ;
Herbert Xuda7f0332008-07-31 17:08:25 +08001408 }
Cristian Stoica05b1d332014-07-28 13:11:23 +03001409 if (n) {
1410 pr_err("alg: aead%s: Result buffer corruption in chunk test %d on %s at page %u for %s: %u bytes:\n",
1411 d, j, e, k, algo, n);
1412 hexdump(q, n);
Herbert Xuda7f0332008-07-31 17:08:25 +08001413 goto out;
1414 }
1415
Cristian Stoica05b1d332014-07-28 13:11:23 +03001416 temp += template[i].tap[k];
Herbert Xuda7f0332008-07-31 17:08:25 +08001417 }
1418 }
1419
1420 ret = 0;
1421
1422out:
1423 aead_request_free(req);
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001424 kfree(sg);
1425out_nosg:
1426 if (diff_dst)
1427 testmgr_free_buf(xoutbuf);
1428out_nooutbuf:
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001429 testmgr_free_buf(axbuf);
1430out_noaxbuf:
1431 testmgr_free_buf(xbuf);
1432out_noxbuf:
Horia Geanta29b77e52014-07-23 11:59:38 +03001433 kfree(key);
Tadeusz Struk9bac0192014-05-19 09:51:33 -07001434 kfree(iv);
Herbert Xuda7f0332008-07-31 17:08:25 +08001435 return ret;
1436}
1437
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001438static int test_aead(struct crypto_aead *tfm, int enc,
Eric Biggersb13b1e02017-02-24 15:46:59 -08001439 const struct aead_testvec *template, unsigned int tcount)
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001440{
Jussi Kivilinna58dcf542013-06-13 17:37:50 +03001441 unsigned int alignmask;
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001442 int ret;
1443
1444 /* test 'dst == src' case */
Jussi Kivilinna58dcf542013-06-13 17:37:50 +03001445 ret = __test_aead(tfm, enc, template, tcount, false, 0);
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001446 if (ret)
1447 return ret;
1448
1449 /* test 'dst != src' case */
Jussi Kivilinna58dcf542013-06-13 17:37:50 +03001450 ret = __test_aead(tfm, enc, template, tcount, true, 0);
1451 if (ret)
1452 return ret;
1453
1454 /* test unaligned buffers, check with one byte offset */
1455 ret = __test_aead(tfm, enc, template, tcount, true, 1);
1456 if (ret)
1457 return ret;
1458
1459 alignmask = crypto_tfm_alg_alignmask(&tfm->base);
1460 if (alignmask) {
1461 /* Check if alignment mask for tfm is correctly set. */
1462 ret = __test_aead(tfm, enc, template, tcount, true,
1463 alignmask + 1);
1464 if (ret)
1465 return ret;
1466 }
1467
1468 return 0;
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001469}
1470
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001471static int test_cipher(struct crypto_cipher *tfm, int enc,
Eric Biggersb13b1e02017-02-24 15:46:59 -08001472 const struct cipher_testvec *template,
1473 unsigned int tcount)
Herbert Xuda7f0332008-07-31 17:08:25 +08001474{
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001475 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
1476 unsigned int i, j, k;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001477 char *q;
1478 const char *e;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001479 const char *input, *result;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001480 void *data;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001481 char *xbuf[XBUFSIZE];
1482 int ret = -ENOMEM;
1483
1484 if (testmgr_alloc_buf(xbuf))
1485 goto out_nobuf;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001486
1487 if (enc == ENCRYPT)
1488 e = "encryption";
1489 else
1490 e = "decryption";
1491
1492 j = 0;
1493 for (i = 0; i < tcount; i++) {
1494 if (template[i].np)
1495 continue;
1496
Stephan Mueller10faa8c2016-08-25 15:15:01 +02001497 if (fips_enabled && template[i].fips_skip)
1498 continue;
1499
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001500 input = enc ? template[i].ptext : template[i].ctext;
1501 result = enc ? template[i].ctext : template[i].ptext;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001502 j++;
1503
Herbert Xufd57f222009-05-29 16:05:42 +10001504 ret = -EINVAL;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001505 if (WARN_ON(template[i].len > PAGE_SIZE))
Herbert Xufd57f222009-05-29 16:05:42 +10001506 goto out;
1507
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001508 data = xbuf[0];
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001509 memcpy(data, input, template[i].len);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001510
1511 crypto_cipher_clear_flags(tfm, ~0);
1512 if (template[i].wk)
Eric Biggers231baec2019-01-18 22:48:00 -08001513 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001514
1515 ret = crypto_cipher_setkey(tfm, template[i].key,
1516 template[i].klen);
Yanjiang Jin0fae0c12016-07-29 16:32:09 +08001517 if (template[i].fail == !ret) {
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001518 printk(KERN_ERR "alg: cipher: setkey failed "
1519 "on test %d for %s: flags=%x\n", j,
1520 algo, crypto_cipher_get_flags(tfm));
1521 goto out;
1522 } else if (ret)
1523 continue;
1524
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001525 for (k = 0; k < template[i].len;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001526 k += crypto_cipher_blocksize(tfm)) {
1527 if (enc)
1528 crypto_cipher_encrypt_one(tfm, data + k,
1529 data + k);
1530 else
1531 crypto_cipher_decrypt_one(tfm, data + k,
1532 data + k);
1533 }
1534
1535 q = data;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001536 if (memcmp(q, result, template[i].len)) {
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001537 printk(KERN_ERR "alg: cipher: Test %d failed "
1538 "on %s for %s\n", j, e, algo);
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001539 hexdump(q, template[i].len);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001540 ret = -EINVAL;
1541 goto out;
1542 }
1543 }
1544
1545 ret = 0;
1546
1547out:
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001548 testmgr_free_buf(xbuf);
1549out_nobuf:
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001550 return ret;
1551}
1552
Herbert Xu12773d92015-08-20 15:21:46 +08001553static int __test_skcipher(struct crypto_skcipher *tfm, int enc,
Eric Biggersb13b1e02017-02-24 15:46:59 -08001554 const struct cipher_testvec *template,
1555 unsigned int tcount,
Jussi Kivilinna3a338f22013-06-13 17:37:45 +03001556 const bool diff_dst, const int align_offset)
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001557{
Herbert Xuda7f0332008-07-31 17:08:25 +08001558 const char *algo =
Herbert Xu12773d92015-08-20 15:21:46 +08001559 crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm));
Herbert Xuda7f0332008-07-31 17:08:25 +08001560 unsigned int i, j, k, n, temp;
Herbert Xuda7f0332008-07-31 17:08:25 +08001561 char *q;
Herbert Xu12773d92015-08-20 15:21:46 +08001562 struct skcipher_request *req;
Herbert Xuda7f0332008-07-31 17:08:25 +08001563 struct scatterlist sg[8];
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001564 struct scatterlist sgout[8];
1565 const char *e, *d;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001566 struct crypto_wait wait;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001567 const char *input, *result;
Herbert Xuda7f0332008-07-31 17:08:25 +08001568 void *data;
1569 char iv[MAX_IVLEN];
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001570 char *xbuf[XBUFSIZE];
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001571 char *xoutbuf[XBUFSIZE];
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001572 int ret = -ENOMEM;
Andrey Ryabinin84cba172015-09-10 13:11:55 +03001573 unsigned int ivsize = crypto_skcipher_ivsize(tfm);
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001574
1575 if (testmgr_alloc_buf(xbuf))
1576 goto out_nobuf;
Herbert Xuda7f0332008-07-31 17:08:25 +08001577
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001578 if (diff_dst && testmgr_alloc_buf(xoutbuf))
1579 goto out_nooutbuf;
1580
1581 if (diff_dst)
1582 d = "-ddst";
1583 else
1584 d = "";
1585
Herbert Xuda7f0332008-07-31 17:08:25 +08001586 if (enc == ENCRYPT)
1587 e = "encryption";
1588 else
1589 e = "decryption";
1590
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001591 crypto_init_wait(&wait);
Herbert Xuda7f0332008-07-31 17:08:25 +08001592
Herbert Xu12773d92015-08-20 15:21:46 +08001593 req = skcipher_request_alloc(tfm, GFP_KERNEL);
Herbert Xuda7f0332008-07-31 17:08:25 +08001594 if (!req) {
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001595 pr_err("alg: skcipher%s: Failed to allocate request for %s\n",
1596 d, algo);
Herbert Xuda7f0332008-07-31 17:08:25 +08001597 goto out;
1598 }
1599
Herbert Xu12773d92015-08-20 15:21:46 +08001600 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001601 crypto_req_done, &wait);
Herbert Xuda7f0332008-07-31 17:08:25 +08001602
1603 j = 0;
1604 for (i = 0; i < tcount; i++) {
Cristian Stoicabbb9a7d2014-08-08 14:27:52 +03001605 if (template[i].np && !template[i].also_non_np)
1606 continue;
1607
Stephan Mueller10faa8c2016-08-25 15:15:01 +02001608 if (fips_enabled && template[i].fips_skip)
1609 continue;
1610
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001611 if (template[i].iv && !(template[i].generates_iv && enc))
Andrey Ryabinin84cba172015-09-10 13:11:55 +03001612 memcpy(iv, template[i].iv, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001613 else
1614 memset(iv, 0, MAX_IVLEN);
1615
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001616 input = enc ? template[i].ptext : template[i].ctext;
1617 result = enc ? template[i].ctext : template[i].ptext;
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001618 j++;
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001619 ret = -EINVAL;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001620 if (WARN_ON(align_offset + template[i].len > PAGE_SIZE))
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001621 goto out;
1622
1623 data = xbuf[0];
1624 data += align_offset;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001625 memcpy(data, input, template[i].len);
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001626
Herbert Xu12773d92015-08-20 15:21:46 +08001627 crypto_skcipher_clear_flags(tfm, ~0);
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001628 if (template[i].wk)
Eric Biggers231baec2019-01-18 22:48:00 -08001629 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001630
Herbert Xu12773d92015-08-20 15:21:46 +08001631 ret = crypto_skcipher_setkey(tfm, template[i].key,
1632 template[i].klen);
Yanjiang Jin0fae0c12016-07-29 16:32:09 +08001633 if (template[i].fail == !ret) {
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001634 pr_err("alg: skcipher%s: setkey failed on test %d for %s: flags=%x\n",
Herbert Xu12773d92015-08-20 15:21:46 +08001635 d, j, algo, crypto_skcipher_get_flags(tfm));
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001636 goto out;
1637 } else if (ret)
1638 continue;
1639
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001640 sg_init_one(&sg[0], data, template[i].len);
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001641 if (diff_dst) {
1642 data = xoutbuf[0];
Jussi Kivilinna3a338f22013-06-13 17:37:45 +03001643 data += align_offset;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001644 sg_init_one(&sgout[0], data, template[i].len);
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001645 }
Herbert Xuda7f0332008-07-31 17:08:25 +08001646
Herbert Xu12773d92015-08-20 15:21:46 +08001647 skcipher_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001648 template[i].len, iv);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001649 ret = crypto_wait_req(enc ? crypto_skcipher_encrypt(req) :
1650 crypto_skcipher_decrypt(req), &wait);
Herbert Xuda7f0332008-07-31 17:08:25 +08001651
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001652 if (ret) {
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001653 pr_err("alg: skcipher%s: %s failed on test %d for %s: ret=%d\n",
1654 d, e, j, algo, -ret);
1655 goto out;
1656 }
Herbert Xuda7f0332008-07-31 17:08:25 +08001657
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001658 q = data;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001659 if (memcmp(q, result, template[i].len)) {
Boris BREZILLON8a826a32015-06-16 11:46:46 +02001660 pr_err("alg: skcipher%s: Test %d failed (invalid result) on %s for %s\n",
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001661 d, j, e, algo);
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001662 hexdump(q, template[i].len);
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001663 ret = -EINVAL;
1664 goto out;
Herbert Xuda7f0332008-07-31 17:08:25 +08001665 }
Boris BREZILLON8a826a32015-06-16 11:46:46 +02001666
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001667 if (template[i].generates_iv && enc &&
1668 memcmp(iv, template[i].iv, crypto_skcipher_ivsize(tfm))) {
Boris BREZILLON8a826a32015-06-16 11:46:46 +02001669 pr_err("alg: skcipher%s: Test %d failed (invalid output IV) on %s for %s\n",
1670 d, j, e, algo);
1671 hexdump(iv, crypto_skcipher_ivsize(tfm));
1672 ret = -EINVAL;
1673 goto out;
1674 }
Herbert Xuda7f0332008-07-31 17:08:25 +08001675 }
1676
1677 j = 0;
1678 for (i = 0; i < tcount; i++) {
Jussi Kivilinna3a338f22013-06-13 17:37:45 +03001679 /* alignment tests are only done with continuous buffers */
1680 if (align_offset != 0)
1681 break;
Herbert Xuda7f0332008-07-31 17:08:25 +08001682
Cristian Stoicabbb9a7d2014-08-08 14:27:52 +03001683 if (!template[i].np)
1684 continue;
1685
Stephan Mueller10faa8c2016-08-25 15:15:01 +02001686 if (fips_enabled && template[i].fips_skip)
1687 continue;
1688
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001689 if (template[i].iv && !(template[i].generates_iv && enc))
Andrey Ryabinin84cba172015-09-10 13:11:55 +03001690 memcpy(iv, template[i].iv, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001691 else
1692 memset(iv, 0, MAX_IVLEN);
1693
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001694 input = enc ? template[i].ptext : template[i].ctext;
1695 result = enc ? template[i].ctext : template[i].ptext;
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001696 j++;
Herbert Xu12773d92015-08-20 15:21:46 +08001697 crypto_skcipher_clear_flags(tfm, ~0);
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001698 if (template[i].wk)
Eric Biggers231baec2019-01-18 22:48:00 -08001699 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001700
Herbert Xu12773d92015-08-20 15:21:46 +08001701 ret = crypto_skcipher_setkey(tfm, template[i].key,
1702 template[i].klen);
Yanjiang Jin0fae0c12016-07-29 16:32:09 +08001703 if (template[i].fail == !ret) {
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001704 pr_err("alg: skcipher%s: setkey failed on chunk test %d for %s: flags=%x\n",
Herbert Xu12773d92015-08-20 15:21:46 +08001705 d, j, algo, crypto_skcipher_get_flags(tfm));
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001706 goto out;
1707 } else if (ret)
1708 continue;
1709
1710 temp = 0;
1711 ret = -EINVAL;
1712 sg_init_table(sg, template[i].np);
1713 if (diff_dst)
1714 sg_init_table(sgout, template[i].np);
1715 for (k = 0; k < template[i].np; k++) {
1716 if (WARN_ON(offset_in_page(IDX[k]) +
1717 template[i].tap[k] > PAGE_SIZE))
Herbert Xuda7f0332008-07-31 17:08:25 +08001718 goto out;
Herbert Xuda7f0332008-07-31 17:08:25 +08001719
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001720 q = xbuf[IDX[k] >> PAGE_SHIFT] + offset_in_page(IDX[k]);
1721
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001722 memcpy(q, input + temp, template[i].tap[k]);
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001723
1724 if (offset_in_page(q) + template[i].tap[k] < PAGE_SIZE)
1725 q[template[i].tap[k]] = 0;
1726
1727 sg_set_buf(&sg[k], q, template[i].tap[k]);
1728 if (diff_dst) {
1729 q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
1730 offset_in_page(IDX[k]);
1731
1732 sg_set_buf(&sgout[k], q, template[i].tap[k]);
1733
1734 memset(q, 0, template[i].tap[k]);
1735 if (offset_in_page(q) +
1736 template[i].tap[k] < PAGE_SIZE)
1737 q[template[i].tap[k]] = 0;
1738 }
1739
1740 temp += template[i].tap[k];
1741 }
1742
Herbert Xu12773d92015-08-20 15:21:46 +08001743 skcipher_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001744 template[i].len, iv);
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001745
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001746 ret = crypto_wait_req(enc ? crypto_skcipher_encrypt(req) :
1747 crypto_skcipher_decrypt(req), &wait);
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001748
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001749 if (ret) {
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001750 pr_err("alg: skcipher%s: %s failed on chunk test %d for %s: ret=%d\n",
1751 d, e, j, algo, -ret);
1752 goto out;
1753 }
1754
1755 temp = 0;
1756 ret = -EINVAL;
1757 for (k = 0; k < template[i].np; k++) {
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001758 if (diff_dst)
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001759 q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
1760 offset_in_page(IDX[k]);
1761 else
Herbert Xuda7f0332008-07-31 17:08:25 +08001762 q = xbuf[IDX[k] >> PAGE_SHIFT] +
1763 offset_in_page(IDX[k]);
1764
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001765 if (memcmp(q, result + temp, template[i].tap[k])) {
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001766 pr_err("alg: skcipher%s: Chunk test %d failed on %s at page %u for %s\n",
1767 d, j, e, k, algo);
1768 hexdump(q, template[i].tap[k]);
Herbert Xuda7f0332008-07-31 17:08:25 +08001769 goto out;
1770 }
1771
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001772 q += template[i].tap[k];
1773 for (n = 0; offset_in_page(q + n) && q[n]; n++)
1774 ;
1775 if (n) {
1776 pr_err("alg: skcipher%s: Result buffer corruption in chunk test %d on %s at page %u for %s: %u bytes:\n",
1777 d, j, e, k, algo, n);
1778 hexdump(q, n);
1779 goto out;
Herbert Xuda7f0332008-07-31 17:08:25 +08001780 }
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001781 temp += template[i].tap[k];
Herbert Xuda7f0332008-07-31 17:08:25 +08001782 }
1783 }
1784
1785 ret = 0;
1786
1787out:
Herbert Xu12773d92015-08-20 15:21:46 +08001788 skcipher_request_free(req);
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001789 if (diff_dst)
1790 testmgr_free_buf(xoutbuf);
1791out_nooutbuf:
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001792 testmgr_free_buf(xbuf);
1793out_nobuf:
Herbert Xuda7f0332008-07-31 17:08:25 +08001794 return ret;
1795}
1796
Herbert Xu12773d92015-08-20 15:21:46 +08001797static int test_skcipher(struct crypto_skcipher *tfm, int enc,
Eric Biggersb13b1e02017-02-24 15:46:59 -08001798 const struct cipher_testvec *template,
1799 unsigned int tcount)
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001800{
Jussi Kivilinna3a338f22013-06-13 17:37:45 +03001801 unsigned int alignmask;
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001802 int ret;
1803
1804 /* test 'dst == src' case */
Jussi Kivilinna3a338f22013-06-13 17:37:45 +03001805 ret = __test_skcipher(tfm, enc, template, tcount, false, 0);
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001806 if (ret)
1807 return ret;
1808
1809 /* test 'dst != src' case */
Jussi Kivilinna3a338f22013-06-13 17:37:45 +03001810 ret = __test_skcipher(tfm, enc, template, tcount, true, 0);
1811 if (ret)
1812 return ret;
1813
1814 /* test unaligned buffers, check with one byte offset */
1815 ret = __test_skcipher(tfm, enc, template, tcount, true, 1);
1816 if (ret)
1817 return ret;
1818
1819 alignmask = crypto_tfm_alg_alignmask(&tfm->base);
1820 if (alignmask) {
1821 /* Check if alignment mask for tfm is correctly set. */
1822 ret = __test_skcipher(tfm, enc, template, tcount, true,
1823 alignmask + 1);
1824 if (ret)
1825 return ret;
1826 }
1827
1828 return 0;
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001829}
1830
Eric Biggersb13b1e02017-02-24 15:46:59 -08001831static int test_comp(struct crypto_comp *tfm,
1832 const struct comp_testvec *ctemplate,
1833 const struct comp_testvec *dtemplate,
1834 int ctcount, int dtcount)
Herbert Xuda7f0332008-07-31 17:08:25 +08001835{
1836 const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
Mahipal Challa33607382018-04-11 20:28:32 +02001837 char *output, *decomp_output;
Herbert Xuda7f0332008-07-31 17:08:25 +08001838 unsigned int i;
Herbert Xuda7f0332008-07-31 17:08:25 +08001839 int ret;
1840
Mahipal Challa33607382018-04-11 20:28:32 +02001841 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1842 if (!output)
1843 return -ENOMEM;
1844
1845 decomp_output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1846 if (!decomp_output) {
1847 kfree(output);
1848 return -ENOMEM;
1849 }
1850
Herbert Xuda7f0332008-07-31 17:08:25 +08001851 for (i = 0; i < ctcount; i++) {
Geert Uytterhoevenc79cf912009-03-29 15:44:19 +08001852 int ilen;
1853 unsigned int dlen = COMP_BUF_SIZE;
Herbert Xuda7f0332008-07-31 17:08:25 +08001854
Michael Schupikov22a81182018-10-07 13:58:10 +02001855 memset(output, 0, COMP_BUF_SIZE);
1856 memset(decomp_output, 0, COMP_BUF_SIZE);
Herbert Xuda7f0332008-07-31 17:08:25 +08001857
1858 ilen = ctemplate[i].inlen;
1859 ret = crypto_comp_compress(tfm, ctemplate[i].input,
Mahipal Challa33607382018-04-11 20:28:32 +02001860 ilen, output, &dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08001861 if (ret) {
1862 printk(KERN_ERR "alg: comp: compression failed "
1863 "on test %d for %s: ret=%d\n", i + 1, algo,
1864 -ret);
1865 goto out;
1866 }
1867
Mahipal Challa33607382018-04-11 20:28:32 +02001868 ilen = dlen;
1869 dlen = COMP_BUF_SIZE;
1870 ret = crypto_comp_decompress(tfm, output,
1871 ilen, decomp_output, &dlen);
1872 if (ret) {
1873 pr_err("alg: comp: compression failed: decompress: on test %d for %s failed: ret=%d\n",
1874 i + 1, algo, -ret);
1875 goto out;
1876 }
1877
1878 if (dlen != ctemplate[i].inlen) {
Geert Uytterhoevenb812eb02008-11-28 20:51:28 +08001879 printk(KERN_ERR "alg: comp: Compression test %d "
1880 "failed for %s: output len = %d\n", i + 1, algo,
1881 dlen);
1882 ret = -EINVAL;
1883 goto out;
1884 }
1885
Mahipal Challa33607382018-04-11 20:28:32 +02001886 if (memcmp(decomp_output, ctemplate[i].input,
1887 ctemplate[i].inlen)) {
1888 pr_err("alg: comp: compression failed: output differs: on test %d for %s\n",
1889 i + 1, algo);
1890 hexdump(decomp_output, dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08001891 ret = -EINVAL;
1892 goto out;
1893 }
1894 }
1895
1896 for (i = 0; i < dtcount; i++) {
Geert Uytterhoevenc79cf912009-03-29 15:44:19 +08001897 int ilen;
1898 unsigned int dlen = COMP_BUF_SIZE;
Herbert Xuda7f0332008-07-31 17:08:25 +08001899
Michael Schupikov22a81182018-10-07 13:58:10 +02001900 memset(decomp_output, 0, COMP_BUF_SIZE);
Herbert Xuda7f0332008-07-31 17:08:25 +08001901
1902 ilen = dtemplate[i].inlen;
1903 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
Mahipal Challa33607382018-04-11 20:28:32 +02001904 ilen, decomp_output, &dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08001905 if (ret) {
1906 printk(KERN_ERR "alg: comp: decompression failed "
1907 "on test %d for %s: ret=%d\n", i + 1, algo,
1908 -ret);
1909 goto out;
1910 }
1911
Geert Uytterhoevenb812eb02008-11-28 20:51:28 +08001912 if (dlen != dtemplate[i].outlen) {
1913 printk(KERN_ERR "alg: comp: Decompression test %d "
1914 "failed for %s: output len = %d\n", i + 1, algo,
1915 dlen);
1916 ret = -EINVAL;
1917 goto out;
1918 }
1919
Mahipal Challa33607382018-04-11 20:28:32 +02001920 if (memcmp(decomp_output, dtemplate[i].output, dlen)) {
Herbert Xuda7f0332008-07-31 17:08:25 +08001921 printk(KERN_ERR "alg: comp: Decompression test %d "
1922 "failed for %s\n", i + 1, algo);
Mahipal Challa33607382018-04-11 20:28:32 +02001923 hexdump(decomp_output, dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08001924 ret = -EINVAL;
1925 goto out;
1926 }
1927 }
1928
1929 ret = 0;
1930
1931out:
Mahipal Challa33607382018-04-11 20:28:32 +02001932 kfree(decomp_output);
1933 kfree(output);
Herbert Xuda7f0332008-07-31 17:08:25 +08001934 return ret;
1935}
1936
Eric Biggersb13b1e02017-02-24 15:46:59 -08001937static int test_acomp(struct crypto_acomp *tfm,
Mahipal Challa33607382018-04-11 20:28:32 +02001938 const struct comp_testvec *ctemplate,
Eric Biggersb13b1e02017-02-24 15:46:59 -08001939 const struct comp_testvec *dtemplate,
1940 int ctcount, int dtcount)
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001941{
1942 const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
1943 unsigned int i;
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01001944 char *output, *decomp_out;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001945 int ret;
1946 struct scatterlist src, dst;
1947 struct acomp_req *req;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001948 struct crypto_wait wait;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001949
Eric Biggerseb095592016-11-23 10:24:35 -08001950 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1951 if (!output)
1952 return -ENOMEM;
1953
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01001954 decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1955 if (!decomp_out) {
1956 kfree(output);
1957 return -ENOMEM;
1958 }
1959
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001960 for (i = 0; i < ctcount; i++) {
1961 unsigned int dlen = COMP_BUF_SIZE;
1962 int ilen = ctemplate[i].inlen;
Laura Abbott02608e02016-12-21 12:32:54 -08001963 void *input_vec;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001964
Eric Biggersd2110222016-12-30 14:12:00 -06001965 input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL);
Laura Abbott02608e02016-12-21 12:32:54 -08001966 if (!input_vec) {
1967 ret = -ENOMEM;
1968 goto out;
1969 }
1970
Eric Biggerseb095592016-11-23 10:24:35 -08001971 memset(output, 0, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001972 crypto_init_wait(&wait);
Laura Abbott02608e02016-12-21 12:32:54 -08001973 sg_init_one(&src, input_vec, ilen);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001974 sg_init_one(&dst, output, dlen);
1975
1976 req = acomp_request_alloc(tfm);
1977 if (!req) {
1978 pr_err("alg: acomp: request alloc failed for %s\n",
1979 algo);
Laura Abbott02608e02016-12-21 12:32:54 -08001980 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001981 ret = -ENOMEM;
1982 goto out;
1983 }
1984
1985 acomp_request_set_params(req, &src, &dst, ilen, dlen);
1986 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001987 crypto_req_done, &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001988
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001989 ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001990 if (ret) {
1991 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
1992 i + 1, algo, -ret);
Laura Abbott02608e02016-12-21 12:32:54 -08001993 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001994 acomp_request_free(req);
1995 goto out;
1996 }
1997
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01001998 ilen = req->dlen;
1999 dlen = COMP_BUF_SIZE;
2000 sg_init_one(&src, output, ilen);
2001 sg_init_one(&dst, decomp_out, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002002 crypto_init_wait(&wait);
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002003 acomp_request_set_params(req, &src, &dst, ilen, dlen);
2004
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002005 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002006 if (ret) {
2007 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
2008 i + 1, algo, -ret);
2009 kfree(input_vec);
2010 acomp_request_free(req);
2011 goto out;
2012 }
2013
2014 if (req->dlen != ctemplate[i].inlen) {
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002015 pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
2016 i + 1, algo, req->dlen);
2017 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08002018 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002019 acomp_request_free(req);
2020 goto out;
2021 }
2022
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002023 if (memcmp(input_vec, decomp_out, req->dlen)) {
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002024 pr_err("alg: acomp: Compression test %d failed for %s\n",
2025 i + 1, algo);
2026 hexdump(output, req->dlen);
2027 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08002028 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002029 acomp_request_free(req);
2030 goto out;
2031 }
2032
Laura Abbott02608e02016-12-21 12:32:54 -08002033 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002034 acomp_request_free(req);
2035 }
2036
2037 for (i = 0; i < dtcount; i++) {
2038 unsigned int dlen = COMP_BUF_SIZE;
2039 int ilen = dtemplate[i].inlen;
Laura Abbott02608e02016-12-21 12:32:54 -08002040 void *input_vec;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002041
Eric Biggersd2110222016-12-30 14:12:00 -06002042 input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL);
Laura Abbott02608e02016-12-21 12:32:54 -08002043 if (!input_vec) {
2044 ret = -ENOMEM;
2045 goto out;
2046 }
2047
Eric Biggerseb095592016-11-23 10:24:35 -08002048 memset(output, 0, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002049 crypto_init_wait(&wait);
Laura Abbott02608e02016-12-21 12:32:54 -08002050 sg_init_one(&src, input_vec, ilen);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002051 sg_init_one(&dst, output, dlen);
2052
2053 req = acomp_request_alloc(tfm);
2054 if (!req) {
2055 pr_err("alg: acomp: request alloc failed for %s\n",
2056 algo);
Laura Abbott02608e02016-12-21 12:32:54 -08002057 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002058 ret = -ENOMEM;
2059 goto out;
2060 }
2061
2062 acomp_request_set_params(req, &src, &dst, ilen, dlen);
2063 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002064 crypto_req_done, &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002065
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002066 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002067 if (ret) {
2068 pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
2069 i + 1, algo, -ret);
Laura Abbott02608e02016-12-21 12:32:54 -08002070 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002071 acomp_request_free(req);
2072 goto out;
2073 }
2074
2075 if (req->dlen != dtemplate[i].outlen) {
2076 pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
2077 i + 1, algo, req->dlen);
2078 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08002079 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002080 acomp_request_free(req);
2081 goto out;
2082 }
2083
2084 if (memcmp(output, dtemplate[i].output, req->dlen)) {
2085 pr_err("alg: acomp: Decompression test %d failed for %s\n",
2086 i + 1, algo);
2087 hexdump(output, req->dlen);
2088 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08002089 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002090 acomp_request_free(req);
2091 goto out;
2092 }
2093
Laura Abbott02608e02016-12-21 12:32:54 -08002094 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002095 acomp_request_free(req);
2096 }
2097
2098 ret = 0;
2099
2100out:
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01002101 kfree(decomp_out);
Eric Biggerseb095592016-11-23 10:24:35 -08002102 kfree(output);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002103 return ret;
2104}
2105
Eric Biggersb13b1e02017-02-24 15:46:59 -08002106static int test_cprng(struct crypto_rng *tfm,
2107 const struct cprng_testvec *template,
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002108 unsigned int tcount)
2109{
2110 const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
Felipe Contrerasfa4ef8a2009-10-27 19:04:42 +08002111 int err = 0, i, j, seedsize;
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002112 u8 *seed;
2113 char result[32];
2114
2115 seedsize = crypto_rng_seedsize(tfm);
2116
2117 seed = kmalloc(seedsize, GFP_KERNEL);
2118 if (!seed) {
2119 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
2120 "for %s\n", algo);
2121 return -ENOMEM;
2122 }
2123
2124 for (i = 0; i < tcount; i++) {
2125 memset(result, 0, 32);
2126
2127 memcpy(seed, template[i].v, template[i].vlen);
2128 memcpy(seed + template[i].vlen, template[i].key,
2129 template[i].klen);
2130 memcpy(seed + template[i].vlen + template[i].klen,
2131 template[i].dt, template[i].dtlen);
2132
2133 err = crypto_rng_reset(tfm, seed, seedsize);
2134 if (err) {
2135 printk(KERN_ERR "alg: cprng: Failed to reset rng "
2136 "for %s\n", algo);
2137 goto out;
2138 }
2139
2140 for (j = 0; j < template[i].loops; j++) {
2141 err = crypto_rng_get_bytes(tfm, result,
2142 template[i].rlen);
Stephan Mueller19e60e12015-03-10 17:00:36 +01002143 if (err < 0) {
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002144 printk(KERN_ERR "alg: cprng: Failed to obtain "
2145 "the correct amount of random data for "
Stephan Mueller19e60e12015-03-10 17:00:36 +01002146 "%s (requested %d)\n", algo,
2147 template[i].rlen);
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002148 goto out;
2149 }
2150 }
2151
2152 err = memcmp(result, template[i].result,
2153 template[i].rlen);
2154 if (err) {
2155 printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
2156 i, algo);
2157 hexdump(result, template[i].rlen);
2158 err = -EINVAL;
2159 goto out;
2160 }
2161 }
2162
2163out:
2164 kfree(seed);
2165 return err;
2166}
2167
Herbert Xuda7f0332008-07-31 17:08:25 +08002168static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
2169 u32 type, u32 mask)
2170{
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002171 const struct aead_test_suite *suite = &desc->suite.aead;
Herbert Xuda7f0332008-07-31 17:08:25 +08002172 struct crypto_aead *tfm;
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002173 int err;
Herbert Xuda7f0332008-07-31 17:08:25 +08002174
Herbert Xueed93e02016-11-22 20:08:31 +08002175 tfm = crypto_alloc_aead(driver, type, mask);
Herbert Xuda7f0332008-07-31 17:08:25 +08002176 if (IS_ERR(tfm)) {
2177 printk(KERN_ERR "alg: aead: Failed to load transform for %s: "
2178 "%ld\n", driver, PTR_ERR(tfm));
2179 return PTR_ERR(tfm);
2180 }
2181
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002182 err = test_aead(tfm, ENCRYPT, suite->vecs, suite->count);
2183 if (!err)
2184 err = test_aead(tfm, DECRYPT, suite->vecs, suite->count);
Herbert Xuda7f0332008-07-31 17:08:25 +08002185
Herbert Xuda7f0332008-07-31 17:08:25 +08002186 crypto_free_aead(tfm);
2187 return err;
2188}
2189
2190static int alg_test_cipher(const struct alg_test_desc *desc,
2191 const char *driver, u32 type, u32 mask)
2192{
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002193 const struct cipher_test_suite *suite = &desc->suite.cipher;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002194 struct crypto_cipher *tfm;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002195 int err;
Herbert Xuda7f0332008-07-31 17:08:25 +08002196
Herbert Xueed93e02016-11-22 20:08:31 +08002197 tfm = crypto_alloc_cipher(driver, type, mask);
Herbert Xuda7f0332008-07-31 17:08:25 +08002198 if (IS_ERR(tfm)) {
2199 printk(KERN_ERR "alg: cipher: Failed to load transform for "
2200 "%s: %ld\n", driver, PTR_ERR(tfm));
2201 return PTR_ERR(tfm);
2202 }
2203
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002204 err = test_cipher(tfm, ENCRYPT, suite->vecs, suite->count);
2205 if (!err)
2206 err = test_cipher(tfm, DECRYPT, suite->vecs, suite->count);
Herbert Xuda7f0332008-07-31 17:08:25 +08002207
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002208 crypto_free_cipher(tfm);
2209 return err;
2210}
2211
2212static int alg_test_skcipher(const struct alg_test_desc *desc,
2213 const char *driver, u32 type, u32 mask)
2214{
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002215 const struct cipher_test_suite *suite = &desc->suite.cipher;
Herbert Xu12773d92015-08-20 15:21:46 +08002216 struct crypto_skcipher *tfm;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002217 int err;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002218
Herbert Xueed93e02016-11-22 20:08:31 +08002219 tfm = crypto_alloc_skcipher(driver, type, mask);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002220 if (IS_ERR(tfm)) {
2221 printk(KERN_ERR "alg: skcipher: Failed to load transform for "
2222 "%s: %ld\n", driver, PTR_ERR(tfm));
2223 return PTR_ERR(tfm);
2224 }
2225
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002226 err = test_skcipher(tfm, ENCRYPT, suite->vecs, suite->count);
2227 if (!err)
2228 err = test_skcipher(tfm, DECRYPT, suite->vecs, suite->count);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002229
Herbert Xu12773d92015-08-20 15:21:46 +08002230 crypto_free_skcipher(tfm);
Herbert Xuda7f0332008-07-31 17:08:25 +08002231 return err;
2232}
2233
2234static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
2235 u32 type, u32 mask)
2236{
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002237 struct crypto_comp *comp;
2238 struct crypto_acomp *acomp;
Herbert Xuda7f0332008-07-31 17:08:25 +08002239 int err;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002240 u32 algo_type = type & CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
Herbert Xuda7f0332008-07-31 17:08:25 +08002241
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01002242 if (algo_type == CRYPTO_ALG_TYPE_ACOMPRESS) {
2243 acomp = crypto_alloc_acomp(driver, type, mask);
2244 if (IS_ERR(acomp)) {
2245 pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
2246 driver, PTR_ERR(acomp));
2247 return PTR_ERR(acomp);
2248 }
2249 err = test_acomp(acomp, desc->suite.comp.comp.vecs,
2250 desc->suite.comp.decomp.vecs,
2251 desc->suite.comp.comp.count,
2252 desc->suite.comp.decomp.count);
2253 crypto_free_acomp(acomp);
2254 } else {
2255 comp = crypto_alloc_comp(driver, type, mask);
2256 if (IS_ERR(comp)) {
2257 pr_err("alg: comp: Failed to load transform for %s: %ld\n",
2258 driver, PTR_ERR(comp));
2259 return PTR_ERR(comp);
2260 }
2261
2262 err = test_comp(comp, desc->suite.comp.comp.vecs,
2263 desc->suite.comp.decomp.vecs,
2264 desc->suite.comp.comp.count,
2265 desc->suite.comp.decomp.count);
2266
2267 crypto_free_comp(comp);
Herbert Xuda7f0332008-07-31 17:08:25 +08002268 }
Herbert Xuda7f0332008-07-31 17:08:25 +08002269 return err;
2270}
2271
Eric Biggers9b3abc0162018-05-19 22:07:41 -07002272static int __alg_test_hash(const struct hash_testvec *template,
2273 unsigned int tcount, const char *driver,
2274 u32 type, u32 mask)
Herbert Xuda7f0332008-07-31 17:08:25 +08002275{
2276 struct crypto_ahash *tfm;
2277 int err;
2278
Herbert Xueed93e02016-11-22 20:08:31 +08002279 tfm = crypto_alloc_ahash(driver, type, mask);
Herbert Xuda7f0332008-07-31 17:08:25 +08002280 if (IS_ERR(tfm)) {
2281 printk(KERN_ERR "alg: hash: Failed to load transform for %s: "
2282 "%ld\n", driver, PTR_ERR(tfm));
2283 return PTR_ERR(tfm);
2284 }
2285
Gilad Ben-Yossef76715092018-07-01 08:02:35 +01002286 err = test_hash(tfm, template, tcount, HASH_TEST_DIGEST);
David S. Millera8f1a052010-05-19 14:12:03 +10002287 if (!err)
Gilad Ben-Yossef76715092018-07-01 08:02:35 +01002288 err = test_hash(tfm, template, tcount, HASH_TEST_FINAL);
2289 if (!err)
2290 err = test_hash(tfm, template, tcount, HASH_TEST_FINUP);
Herbert Xuda7f0332008-07-31 17:08:25 +08002291 crypto_free_ahash(tfm);
2292 return err;
2293}
2294
Eric Biggers9b3abc0162018-05-19 22:07:41 -07002295static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
2296 u32 type, u32 mask)
2297{
2298 const struct hash_testvec *template = desc->suite.hash.vecs;
2299 unsigned int tcount = desc->suite.hash.count;
2300 unsigned int nr_unkeyed, nr_keyed;
2301 int err;
2302
2303 /*
2304 * For OPTIONAL_KEY algorithms, we have to do all the unkeyed tests
2305 * first, before setting a key on the tfm. To make this easier, we
2306 * require that the unkeyed test vectors (if any) are listed first.
2307 */
2308
2309 for (nr_unkeyed = 0; nr_unkeyed < tcount; nr_unkeyed++) {
2310 if (template[nr_unkeyed].ksize)
2311 break;
2312 }
2313 for (nr_keyed = 0; nr_unkeyed + nr_keyed < tcount; nr_keyed++) {
2314 if (!template[nr_unkeyed + nr_keyed].ksize) {
2315 pr_err("alg: hash: test vectors for %s out of order, "
2316 "unkeyed ones must come first\n", desc->alg);
2317 return -EINVAL;
2318 }
2319 }
2320
2321 err = 0;
2322 if (nr_unkeyed) {
2323 err = __alg_test_hash(template, nr_unkeyed, driver, type, mask);
2324 template += nr_unkeyed;
2325 }
2326
2327 if (!err && nr_keyed)
2328 err = __alg_test_hash(template, nr_keyed, driver, type, mask);
2329
2330 return err;
2331}
2332
Herbert Xu8e3ee852008-11-07 14:58:52 +08002333static int alg_test_crc32c(const struct alg_test_desc *desc,
2334 const char *driver, u32 type, u32 mask)
2335{
2336 struct crypto_shash *tfm;
Eric Biggerscb9dde82019-01-10 12:17:55 -08002337 __le32 val;
Herbert Xu8e3ee852008-11-07 14:58:52 +08002338 int err;
2339
2340 err = alg_test_hash(desc, driver, type, mask);
2341 if (err)
Eric Biggerseb5e6732019-01-23 20:57:35 -08002342 return err;
Herbert Xu8e3ee852008-11-07 14:58:52 +08002343
Herbert Xueed93e02016-11-22 20:08:31 +08002344 tfm = crypto_alloc_shash(driver, type, mask);
Herbert Xu8e3ee852008-11-07 14:58:52 +08002345 if (IS_ERR(tfm)) {
Eric Biggerseb5e6732019-01-23 20:57:35 -08002346 if (PTR_ERR(tfm) == -ENOENT) {
2347 /*
2348 * This crc32c implementation is only available through
2349 * ahash API, not the shash API, so the remaining part
2350 * of the test is not applicable to it.
2351 */
2352 return 0;
2353 }
Herbert Xu8e3ee852008-11-07 14:58:52 +08002354 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
2355 "%ld\n", driver, PTR_ERR(tfm));
Eric Biggerseb5e6732019-01-23 20:57:35 -08002356 return PTR_ERR(tfm);
Herbert Xu8e3ee852008-11-07 14:58:52 +08002357 }
2358
2359 do {
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02002360 SHASH_DESC_ON_STACK(shash, tfm);
2361 u32 *ctx = (u32 *)shash_desc_ctx(shash);
Herbert Xu8e3ee852008-11-07 14:58:52 +08002362
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02002363 shash->tfm = tfm;
2364 shash->flags = 0;
Herbert Xu8e3ee852008-11-07 14:58:52 +08002365
Eric Biggerscb9dde82019-01-10 12:17:55 -08002366 *ctx = 420553207;
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02002367 err = crypto_shash_final(shash, (u8 *)&val);
Herbert Xu8e3ee852008-11-07 14:58:52 +08002368 if (err) {
2369 printk(KERN_ERR "alg: crc32c: Operation failed for "
2370 "%s: %d\n", driver, err);
2371 break;
2372 }
2373
Eric Biggerscb9dde82019-01-10 12:17:55 -08002374 if (val != cpu_to_le32(~420553207)) {
2375 pr_err("alg: crc32c: Test failed for %s: %u\n",
2376 driver, le32_to_cpu(val));
Herbert Xu8e3ee852008-11-07 14:58:52 +08002377 err = -EINVAL;
2378 }
2379 } while (0);
2380
2381 crypto_free_shash(tfm);
2382
Herbert Xu8e3ee852008-11-07 14:58:52 +08002383 return err;
2384}
2385
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002386static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
2387 u32 type, u32 mask)
2388{
2389 struct crypto_rng *rng;
2390 int err;
2391
Herbert Xueed93e02016-11-22 20:08:31 +08002392 rng = crypto_alloc_rng(driver, type, mask);
Jarod Wilson7647d6c2009-05-04 19:44:50 +08002393 if (IS_ERR(rng)) {
2394 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
2395 "%ld\n", driver, PTR_ERR(rng));
2396 return PTR_ERR(rng);
2397 }
2398
2399 err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
2400
2401 crypto_free_rng(rng);
2402
2403 return err;
2404}
2405
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002406
Eric Biggersb13b1e02017-02-24 15:46:59 -08002407static int drbg_cavs_test(const struct drbg_testvec *test, int pr,
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002408 const char *driver, u32 type, u32 mask)
2409{
2410 int ret = -EAGAIN;
2411 struct crypto_rng *drng;
2412 struct drbg_test_data test_data;
2413 struct drbg_string addtl, pers, testentropy;
2414 unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
2415
2416 if (!buf)
2417 return -ENOMEM;
2418
Herbert Xueed93e02016-11-22 20:08:31 +08002419 drng = crypto_alloc_rng(driver, type, mask);
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002420 if (IS_ERR(drng)) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04002421 printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002422 "%s\n", driver);
2423 kzfree(buf);
2424 return -ENOMEM;
2425 }
2426
2427 test_data.testentropy = &testentropy;
2428 drbg_string_fill(&testentropy, test->entropy, test->entropylen);
2429 drbg_string_fill(&pers, test->pers, test->perslen);
2430 ret = crypto_drbg_reset_test(drng, &pers, &test_data);
2431 if (ret) {
2432 printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
2433 goto outbuf;
2434 }
2435
2436 drbg_string_fill(&addtl, test->addtla, test->addtllen);
2437 if (pr) {
2438 drbg_string_fill(&testentropy, test->entpra, test->entprlen);
2439 ret = crypto_drbg_get_bytes_addtl_test(drng,
2440 buf, test->expectedlen, &addtl, &test_data);
2441 } else {
2442 ret = crypto_drbg_get_bytes_addtl(drng,
2443 buf, test->expectedlen, &addtl);
2444 }
Stephan Mueller19e60e12015-03-10 17:00:36 +01002445 if (ret < 0) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04002446 printk(KERN_ERR "alg: drbg: could not obtain random data for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002447 "driver %s\n", driver);
2448 goto outbuf;
2449 }
2450
2451 drbg_string_fill(&addtl, test->addtlb, test->addtllen);
2452 if (pr) {
2453 drbg_string_fill(&testentropy, test->entprb, test->entprlen);
2454 ret = crypto_drbg_get_bytes_addtl_test(drng,
2455 buf, test->expectedlen, &addtl, &test_data);
2456 } else {
2457 ret = crypto_drbg_get_bytes_addtl(drng,
2458 buf, test->expectedlen, &addtl);
2459 }
Stephan Mueller19e60e12015-03-10 17:00:36 +01002460 if (ret < 0) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04002461 printk(KERN_ERR "alg: drbg: could not obtain random data for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002462 "driver %s\n", driver);
2463 goto outbuf;
2464 }
2465
2466 ret = memcmp(test->expected, buf, test->expectedlen);
2467
2468outbuf:
2469 crypto_free_rng(drng);
2470 kzfree(buf);
2471 return ret;
2472}
2473
2474
2475static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
2476 u32 type, u32 mask)
2477{
2478 int err = 0;
2479 int pr = 0;
2480 int i = 0;
Eric Biggersb13b1e02017-02-24 15:46:59 -08002481 const struct drbg_testvec *template = desc->suite.drbg.vecs;
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002482 unsigned int tcount = desc->suite.drbg.count;
2483
2484 if (0 == memcmp(driver, "drbg_pr_", 8))
2485 pr = 1;
2486
2487 for (i = 0; i < tcount; i++) {
2488 err = drbg_cavs_test(&template[i], pr, driver, type, mask);
2489 if (err) {
2490 printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
2491 i, driver);
2492 err = -EINVAL;
2493 break;
2494 }
2495 }
2496 return err;
2497
2498}
2499
Eric Biggersb13b1e02017-02-24 15:46:59 -08002500static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002501 const char *alg)
2502{
2503 struct kpp_request *req;
2504 void *input_buf = NULL;
2505 void *output_buf = NULL;
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002506 void *a_public = NULL;
2507 void *a_ss = NULL;
2508 void *shared_secret = NULL;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002509 struct crypto_wait wait;
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002510 unsigned int out_len_max;
2511 int err = -ENOMEM;
2512 struct scatterlist src, dst;
2513
2514 req = kpp_request_alloc(tfm, GFP_KERNEL);
2515 if (!req)
2516 return err;
2517
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002518 crypto_init_wait(&wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002519
2520 err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
2521 if (err < 0)
2522 goto free_req;
2523
2524 out_len_max = crypto_kpp_maxsize(tfm);
2525 output_buf = kzalloc(out_len_max, GFP_KERNEL);
2526 if (!output_buf) {
2527 err = -ENOMEM;
2528 goto free_req;
2529 }
2530
2531 /* Use appropriate parameter as base */
2532 kpp_request_set_input(req, NULL, 0);
2533 sg_init_one(&dst, output_buf, out_len_max);
2534 kpp_request_set_output(req, &dst, out_len_max);
2535 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002536 crypto_req_done, &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002537
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002538 /* Compute party A's public key */
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002539 err = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002540 if (err) {
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002541 pr_err("alg: %s: Party A: generate public key test failed. err %d\n",
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002542 alg, err);
2543 goto free_output;
2544 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002545
2546 if (vec->genkey) {
2547 /* Save party A's public key */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05002548 a_public = kmemdup(sg_virt(req->dst), out_len_max, GFP_KERNEL);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002549 if (!a_public) {
2550 err = -ENOMEM;
2551 goto free_output;
2552 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002553 } else {
2554 /* Verify calculated public key */
2555 if (memcmp(vec->expected_a_public, sg_virt(req->dst),
2556 vec->expected_a_public_size)) {
2557 pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n",
2558 alg);
2559 err = -EINVAL;
2560 goto free_output;
2561 }
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002562 }
2563
2564 /* Calculate shared secret key by using counter part (b) public key. */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05002565 input_buf = kmemdup(vec->b_public, vec->b_public_size, GFP_KERNEL);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002566 if (!input_buf) {
2567 err = -ENOMEM;
2568 goto free_output;
2569 }
2570
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002571 sg_init_one(&src, input_buf, vec->b_public_size);
2572 sg_init_one(&dst, output_buf, out_len_max);
2573 kpp_request_set_input(req, &src, vec->b_public_size);
2574 kpp_request_set_output(req, &dst, out_len_max);
2575 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002576 crypto_req_done, &wait);
2577 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002578 if (err) {
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002579 pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n",
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002580 alg, err);
2581 goto free_all;
2582 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002583
2584 if (vec->genkey) {
2585 /* Save the shared secret obtained by party A */
Christopher Diaz Riverose3d90e522019-01-28 19:01:18 -05002586 a_ss = kmemdup(sg_virt(req->dst), vec->expected_ss_size, GFP_KERNEL);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002587 if (!a_ss) {
2588 err = -ENOMEM;
2589 goto free_all;
2590 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002591
2592 /*
2593 * Calculate party B's shared secret by using party A's
2594 * public key.
2595 */
2596 err = crypto_kpp_set_secret(tfm, vec->b_secret,
2597 vec->b_secret_size);
2598 if (err < 0)
2599 goto free_all;
2600
2601 sg_init_one(&src, a_public, vec->expected_a_public_size);
2602 sg_init_one(&dst, output_buf, out_len_max);
2603 kpp_request_set_input(req, &src, vec->expected_a_public_size);
2604 kpp_request_set_output(req, &dst, out_len_max);
2605 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002606 crypto_req_done, &wait);
2607 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req),
2608 &wait);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002609 if (err) {
2610 pr_err("alg: %s: Party B: compute shared secret failed. err %d\n",
2611 alg, err);
2612 goto free_all;
2613 }
2614
2615 shared_secret = a_ss;
2616 } else {
2617 shared_secret = (void *)vec->expected_ss;
2618 }
2619
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002620 /*
2621 * verify shared secret from which the user will derive
2622 * secret key by executing whatever hash it has chosen
2623 */
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002624 if (memcmp(shared_secret, sg_virt(req->dst),
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002625 vec->expected_ss_size)) {
2626 pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
2627 alg);
2628 err = -EINVAL;
2629 }
2630
2631free_all:
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002632 kfree(a_ss);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002633 kfree(input_buf);
2634free_output:
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002635 kfree(a_public);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002636 kfree(output_buf);
2637free_req:
2638 kpp_request_free(req);
2639 return err;
2640}
2641
2642static int test_kpp(struct crypto_kpp *tfm, const char *alg,
Eric Biggersb13b1e02017-02-24 15:46:59 -08002643 const struct kpp_testvec *vecs, unsigned int tcount)
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002644{
2645 int ret, i;
2646
2647 for (i = 0; i < tcount; i++) {
2648 ret = do_test_kpp(tfm, vecs++, alg);
2649 if (ret) {
2650 pr_err("alg: %s: test failed on vector %d, err=%d\n",
2651 alg, i + 1, ret);
2652 return ret;
2653 }
2654 }
2655 return 0;
2656}
2657
2658static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
2659 u32 type, u32 mask)
2660{
2661 struct crypto_kpp *tfm;
2662 int err = 0;
2663
Herbert Xueed93e02016-11-22 20:08:31 +08002664 tfm = crypto_alloc_kpp(driver, type, mask);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002665 if (IS_ERR(tfm)) {
2666 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
2667 driver, PTR_ERR(tfm));
2668 return PTR_ERR(tfm);
2669 }
2670 if (desc->suite.kpp.vecs)
2671 err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
2672 desc->suite.kpp.count);
2673
2674 crypto_free_kpp(tfm);
2675 return err;
2676}
2677
Herbert Xu50d2b6432016-06-29 19:32:20 +08002678static int test_akcipher_one(struct crypto_akcipher *tfm,
Eric Biggersb13b1e02017-02-24 15:46:59 -08002679 const struct akcipher_testvec *vecs)
Tadeusz Struk946cc462015-06-16 10:31:06 -07002680{
Herbert Xudf27b262016-05-05 16:42:49 +08002681 char *xbuf[XBUFSIZE];
Tadeusz Struk946cc462015-06-16 10:31:06 -07002682 struct akcipher_request *req;
2683 void *outbuf_enc = NULL;
2684 void *outbuf_dec = NULL;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002685 struct crypto_wait wait;
Tadeusz Struk946cc462015-06-16 10:31:06 -07002686 unsigned int out_len_max, out_len = 0;
2687 int err = -ENOMEM;
Tadeusz Struk22287b02015-10-08 09:26:55 -07002688 struct scatterlist src, dst, src_tab[2];
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002689 const char *m, *c;
2690 unsigned int m_size, c_size;
2691 const char *op;
Tadeusz Struk946cc462015-06-16 10:31:06 -07002692
Herbert Xudf27b262016-05-05 16:42:49 +08002693 if (testmgr_alloc_buf(xbuf))
2694 return err;
2695
Tadeusz Struk946cc462015-06-16 10:31:06 -07002696 req = akcipher_request_alloc(tfm, GFP_KERNEL);
2697 if (!req)
Herbert Xudf27b262016-05-05 16:42:49 +08002698 goto free_xbuf;
Tadeusz Struk946cc462015-06-16 10:31:06 -07002699
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002700 crypto_init_wait(&wait);
Tadeusz Struk22287b02015-10-08 09:26:55 -07002701
2702 if (vecs->public_key_vec)
2703 err = crypto_akcipher_set_pub_key(tfm, vecs->key,
2704 vecs->key_len);
2705 else
2706 err = crypto_akcipher_set_priv_key(tfm, vecs->key,
2707 vecs->key_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002708 if (err)
2709 goto free_req;
2710
Salvatore Benedetto57763f52016-07-04 10:52:34 +01002711 err = -ENOMEM;
Tadeusz Struk22287b02015-10-08 09:26:55 -07002712 out_len_max = crypto_akcipher_maxsize(tfm);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002713
2714 /*
2715 * First run test which do not require a private key, such as
2716 * encrypt or verify.
2717 */
Tadeusz Struk946cc462015-06-16 10:31:06 -07002718 outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
2719 if (!outbuf_enc)
2720 goto free_req;
2721
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002722 if (!vecs->siggen_sigver_test) {
2723 m = vecs->m;
2724 m_size = vecs->m_size;
2725 c = vecs->c;
2726 c_size = vecs->c_size;
2727 op = "encrypt";
2728 } else {
2729 /* Swap args so we could keep plaintext (digest)
2730 * in vecs->m, and cooked signature in vecs->c.
2731 */
2732 m = vecs->c; /* signature */
2733 m_size = vecs->c_size;
2734 c = vecs->m; /* digest */
2735 c_size = vecs->m_size;
2736 op = "verify";
2737 }
Herbert Xudf27b262016-05-05 16:42:49 +08002738
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002739 if (WARN_ON(m_size > PAGE_SIZE))
2740 goto free_all;
2741 memcpy(xbuf[0], m, m_size);
Herbert Xudf27b262016-05-05 16:42:49 +08002742
Tadeusz Struk22287b02015-10-08 09:26:55 -07002743 sg_init_table(src_tab, 2);
Herbert Xudf27b262016-05-05 16:42:49 +08002744 sg_set_buf(&src_tab[0], xbuf[0], 8);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002745 sg_set_buf(&src_tab[1], xbuf[0] + 8, m_size - 8);
Tadeusz Struk22287b02015-10-08 09:26:55 -07002746 sg_init_one(&dst, outbuf_enc, out_len_max);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002747 akcipher_request_set_crypt(req, src_tab, &dst, m_size,
Tadeusz Struk22287b02015-10-08 09:26:55 -07002748 out_len_max);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002749 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002750 crypto_req_done, &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002751
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002752 err = crypto_wait_req(vecs->siggen_sigver_test ?
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002753 /* Run asymmetric signature verification */
2754 crypto_akcipher_verify(req) :
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002755 /* Run asymmetric encrypt */
2756 crypto_akcipher_encrypt(req), &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002757 if (err) {
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002758 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002759 goto free_all;
2760 }
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002761 if (req->dst_len != c_size) {
2762 pr_err("alg: akcipher: %s test failed. Invalid output len\n",
2763 op);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002764 err = -EINVAL;
2765 goto free_all;
2766 }
2767 /* verify that encrypted message is equal to expected */
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002768 if (memcmp(c, outbuf_enc, c_size)) {
2769 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
2770 hexdump(outbuf_enc, c_size);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002771 err = -EINVAL;
2772 goto free_all;
2773 }
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002774
2775 /*
2776 * Don't invoke (decrypt or sign) test which require a private key
2777 * for vectors with only a public key.
2778 */
Tadeusz Struk946cc462015-06-16 10:31:06 -07002779 if (vecs->public_key_vec) {
2780 err = 0;
2781 goto free_all;
2782 }
2783 outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
2784 if (!outbuf_dec) {
2785 err = -ENOMEM;
2786 goto free_all;
2787 }
Herbert Xudf27b262016-05-05 16:42:49 +08002788
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002789 op = vecs->siggen_sigver_test ? "sign" : "decrypt";
2790 if (WARN_ON(c_size > PAGE_SIZE))
Herbert Xudf27b262016-05-05 16:42:49 +08002791 goto free_all;
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002792 memcpy(xbuf[0], c, c_size);
Herbert Xudf27b262016-05-05 16:42:49 +08002793
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002794 sg_init_one(&src, xbuf[0], c_size);
Tadeusz Struk22287b02015-10-08 09:26:55 -07002795 sg_init_one(&dst, outbuf_dec, out_len_max);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002796 crypto_init_wait(&wait);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002797 akcipher_request_set_crypt(req, &src, &dst, c_size, out_len_max);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002798
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002799 err = crypto_wait_req(vecs->siggen_sigver_test ?
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002800 /* Run asymmetric signature generation */
2801 crypto_akcipher_sign(req) :
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002802 /* Run asymmetric decrypt */
2803 crypto_akcipher_decrypt(req), &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002804 if (err) {
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002805 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002806 goto free_all;
2807 }
2808 out_len = req->dst_len;
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002809 if (out_len < m_size) {
2810 pr_err("alg: akcipher: %s test failed. Invalid output len %u\n",
2811 op, out_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002812 err = -EINVAL;
2813 goto free_all;
2814 }
2815 /* verify that decrypted message is equal to the original msg */
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002816 if (memchr_inv(outbuf_dec, 0, out_len - m_size) ||
2817 memcmp(m, outbuf_dec + out_len - m_size, m_size)) {
2818 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
Herbert Xu50d2b6432016-06-29 19:32:20 +08002819 hexdump(outbuf_dec, out_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002820 err = -EINVAL;
2821 }
2822free_all:
2823 kfree(outbuf_dec);
2824 kfree(outbuf_enc);
2825free_req:
2826 akcipher_request_free(req);
Herbert Xudf27b262016-05-05 16:42:49 +08002827free_xbuf:
2828 testmgr_free_buf(xbuf);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002829 return err;
2830}
2831
Herbert Xu50d2b6432016-06-29 19:32:20 +08002832static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
Eric Biggersb13b1e02017-02-24 15:46:59 -08002833 const struct akcipher_testvec *vecs,
2834 unsigned int tcount)
Tadeusz Struk946cc462015-06-16 10:31:06 -07002835{
Herbert Xu15226e42016-07-18 18:20:10 +08002836 const char *algo =
2837 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
Tadeusz Struk946cc462015-06-16 10:31:06 -07002838 int ret, i;
2839
2840 for (i = 0; i < tcount; i++) {
Herbert Xu50d2b6432016-06-29 19:32:20 +08002841 ret = test_akcipher_one(tfm, vecs++);
2842 if (!ret)
2843 continue;
2844
Herbert Xu15226e42016-07-18 18:20:10 +08002845 pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
2846 i + 1, algo, ret);
Herbert Xu50d2b6432016-06-29 19:32:20 +08002847 return ret;
Tadeusz Struk946cc462015-06-16 10:31:06 -07002848 }
2849 return 0;
2850}
2851
Tadeusz Struk946cc462015-06-16 10:31:06 -07002852static int alg_test_akcipher(const struct alg_test_desc *desc,
2853 const char *driver, u32 type, u32 mask)
2854{
2855 struct crypto_akcipher *tfm;
2856 int err = 0;
2857
Herbert Xueed93e02016-11-22 20:08:31 +08002858 tfm = crypto_alloc_akcipher(driver, type, mask);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002859 if (IS_ERR(tfm)) {
2860 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
2861 driver, PTR_ERR(tfm));
2862 return PTR_ERR(tfm);
2863 }
2864 if (desc->suite.akcipher.vecs)
2865 err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
2866 desc->suite.akcipher.count);
2867
2868 crypto_free_akcipher(tfm);
2869 return err;
2870}
2871
Youquan, Song863b5572009-12-23 19:45:20 +08002872static int alg_test_null(const struct alg_test_desc *desc,
2873 const char *driver, u32 type, u32 mask)
2874{
2875 return 0;
2876}
2877
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002878#define __VECS(tv) { .vecs = tv, .count = ARRAY_SIZE(tv) }
2879
Herbert Xuda7f0332008-07-31 17:08:25 +08002880/* Please keep this list sorted by algorithm name. */
2881static const struct alg_test_desc alg_test_descs[] = {
2882 {
Eric Biggers059c2a42018-11-16 17:26:31 -08002883 .alg = "adiantum(xchacha12,aes)",
2884 .test = alg_test_skcipher,
2885 .suite = {
2886 .cipher = __VECS(adiantum_xchacha12_aes_tv_template)
2887 },
2888 }, {
2889 .alg = "adiantum(xchacha20,aes)",
2890 .test = alg_test_skcipher,
2891 .suite = {
2892 .cipher = __VECS(adiantum_xchacha20_aes_tv_template)
2893 },
2894 }, {
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02002895 .alg = "aegis128",
2896 .test = alg_test_aead,
2897 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002898 .aead = __VECS(aegis128_tv_template)
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02002899 }
2900 }, {
2901 .alg = "aegis128l",
2902 .test = alg_test_aead,
2903 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002904 .aead = __VECS(aegis128l_tv_template)
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02002905 }
2906 }, {
2907 .alg = "aegis256",
2908 .test = alg_test_aead,
2909 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002910 .aead = __VECS(aegis256_tv_template)
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02002911 }
2912 }, {
Jarod Wilsone08ca2d2009-05-04 19:46:29 +08002913 .alg = "ansi_cprng",
2914 .test = alg_test_cprng,
2915 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002916 .cprng = __VECS(ansi_cprng_aes_tv_template)
Jarod Wilsone08ca2d2009-05-04 19:46:29 +08002917 }
2918 }, {
Horia Geantabca4feb2014-03-14 17:46:51 +02002919 .alg = "authenc(hmac(md5),ecb(cipher_null))",
2920 .test = alg_test_aead,
Horia Geantabca4feb2014-03-14 17:46:51 +02002921 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002922 .aead = __VECS(hmac_md5_ecb_cipher_null_tv_template)
Horia Geantabca4feb2014-03-14 17:46:51 +02002923 }
2924 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002925 .alg = "authenc(hmac(sha1),cbc(aes))",
Horia Geantae46e9a42012-07-03 19:16:54 +03002926 .test = alg_test_aead,
Herbert Xubcf741c2017-06-28 19:09:07 +08002927 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03002928 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002929 .aead = __VECS(hmac_sha1_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302930 }
2931 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002932 .alg = "authenc(hmac(sha1),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302933 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302934 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002935 .aead = __VECS(hmac_sha1_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302936 }
2937 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002938 .alg = "authenc(hmac(sha1),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302939 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01002940 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302941 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002942 .aead = __VECS(hmac_sha1_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03002943 }
2944 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01002945 .alg = "authenc(hmac(sha1),ctr(aes))",
2946 .test = alg_test_null,
2947 .fips_allowed = 1,
2948 }, {
Horia Geantabca4feb2014-03-14 17:46:51 +02002949 .alg = "authenc(hmac(sha1),ecb(cipher_null))",
2950 .test = alg_test_aead,
Horia Geantabca4feb2014-03-14 17:46:51 +02002951 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002952 .aead = __VECS(hmac_sha1_ecb_cipher_null_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302953 }
2954 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01002955 .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
2956 .test = alg_test_null,
2957 .fips_allowed = 1,
2958 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002959 .alg = "authenc(hmac(sha224),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302960 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302961 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002962 .aead = __VECS(hmac_sha224_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302963 }
2964 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002965 .alg = "authenc(hmac(sha224),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302966 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01002967 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302968 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002969 .aead = __VECS(hmac_sha224_des3_ede_cbc_tv_temp)
Horia Geantabca4feb2014-03-14 17:46:51 +02002970 }
2971 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002972 .alg = "authenc(hmac(sha256),cbc(aes))",
Horia Geantae46e9a42012-07-03 19:16:54 +03002973 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01002974 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03002975 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002976 .aead = __VECS(hmac_sha256_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302977 }
2978 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002979 .alg = "authenc(hmac(sha256),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302980 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302981 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002982 .aead = __VECS(hmac_sha256_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302983 }
2984 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002985 .alg = "authenc(hmac(sha256),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302986 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01002987 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302988 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08002989 .aead = __VECS(hmac_sha256_des3_ede_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302990 }
2991 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01002992 .alg = "authenc(hmac(sha256),ctr(aes))",
2993 .test = alg_test_null,
2994 .fips_allowed = 1,
2995 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01002996 .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
2997 .test = alg_test_null,
2998 .fips_allowed = 1,
2999 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003000 .alg = "authenc(hmac(sha384),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303001 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303002 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003003 .aead = __VECS(hmac_sha384_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303004 }
3005 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003006 .alg = "authenc(hmac(sha384),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303007 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01003008 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303009 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003010 .aead = __VECS(hmac_sha384_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03003011 }
3012 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01003013 .alg = "authenc(hmac(sha384),ctr(aes))",
3014 .test = alg_test_null,
3015 .fips_allowed = 1,
3016 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01003017 .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
3018 .test = alg_test_null,
3019 .fips_allowed = 1,
3020 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003021 .alg = "authenc(hmac(sha512),cbc(aes))",
Marcus Meissnered1afac2016-02-05 14:23:33 +01003022 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03003023 .test = alg_test_aead,
Horia Geantae46e9a42012-07-03 19:16:54 +03003024 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003025 .aead = __VECS(hmac_sha512_aes_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303026 }
3027 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003028 .alg = "authenc(hmac(sha512),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303029 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303030 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003031 .aead = __VECS(hmac_sha512_des_cbc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05303032 }
3033 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08003034 .alg = "authenc(hmac(sha512),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05303035 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01003036 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05303037 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003038 .aead = __VECS(hmac_sha512_des3_ede_cbc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03003039 }
3040 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01003041 .alg = "authenc(hmac(sha512),ctr(aes))",
3042 .test = alg_test_null,
3043 .fips_allowed = 1,
3044 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01003045 .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
3046 .test = alg_test_null,
3047 .fips_allowed = 1,
3048 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003049 .alg = "cbc(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003050 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003051 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003052 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003053 .cipher = __VECS(aes_cbc_tv_template)
3054 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003055 }, {
3056 .alg = "cbc(anubis)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003057 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003058 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003059 .cipher = __VECS(anubis_cbc_tv_template)
3060 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003061 }, {
3062 .alg = "cbc(blowfish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003063 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003064 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003065 .cipher = __VECS(bf_cbc_tv_template)
3066 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003067 }, {
3068 .alg = "cbc(camellia)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003069 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003070 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003071 .cipher = __VECS(camellia_cbc_tv_template)
3072 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003073 }, {
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02003074 .alg = "cbc(cast5)",
3075 .test = alg_test_skcipher,
3076 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003077 .cipher = __VECS(cast5_cbc_tv_template)
3078 },
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02003079 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003080 .alg = "cbc(cast6)",
3081 .test = alg_test_skcipher,
3082 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003083 .cipher = __VECS(cast6_cbc_tv_template)
3084 },
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003085 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003086 .alg = "cbc(des)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003087 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003088 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003089 .cipher = __VECS(des_cbc_tv_template)
3090 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003091 }, {
3092 .alg = "cbc(des3_ede)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003093 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003094 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003095 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003096 .cipher = __VECS(des3_ede_cbc_tv_template)
3097 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003098 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01003099 /* Same as cbc(aes) except the key is stored in
3100 * hardware secure memory which we reference by index
3101 */
3102 .alg = "cbc(paes)",
3103 .test = alg_test_null,
3104 .fips_allowed = 1,
3105 }, {
Jussi Kivilinna9d259172011-10-18 00:02:53 +03003106 .alg = "cbc(serpent)",
3107 .test = alg_test_skcipher,
3108 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003109 .cipher = __VECS(serpent_cbc_tv_template)
3110 },
Jussi Kivilinna9d259172011-10-18 00:02:53 +03003111 }, {
Gilad Ben-Yossef95ba5972018-09-20 14:18:38 +01003112 .alg = "cbc(sm4)",
3113 .test = alg_test_skcipher,
3114 .suite = {
3115 .cipher = __VECS(sm4_cbc_tv_template)
3116 }
3117 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003118 .alg = "cbc(twofish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003119 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003120 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003121 .cipher = __VECS(tf_cbc_tv_template)
3122 },
Herbert Xuda7f0332008-07-31 17:08:25 +08003123 }, {
Ard Biesheuvel092acf02017-02-03 14:49:35 +00003124 .alg = "cbcmac(aes)",
3125 .fips_allowed = 1,
3126 .test = alg_test_hash,
3127 .suite = {
3128 .hash = __VECS(aes_cbcmac_tv_template)
3129 }
3130 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003131 .alg = "ccm(aes)",
3132 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003133 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003134 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003135 .aead = __VECS(aes_ccm_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003136 }
3137 }, {
Dmitry Eremin-Solenikov7da66672018-10-20 02:01:53 +03003138 .alg = "cfb(aes)",
3139 .test = alg_test_skcipher,
3140 .fips_allowed = 1,
3141 .suite = {
3142 .cipher = __VECS(aes_cfb_tv_template)
3143 },
3144 }, {
Martin Willi3590ebf2015-06-01 13:43:57 +02003145 .alg = "chacha20",
3146 .test = alg_test_skcipher,
3147 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003148 .cipher = __VECS(chacha20_tv_template)
3149 },
Martin Willi3590ebf2015-06-01 13:43:57 +02003150 }, {
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03003151 .alg = "cmac(aes)",
Stephan Mueller8f183752015-08-19 08:42:07 +02003152 .fips_allowed = 1,
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03003153 .test = alg_test_hash,
3154 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003155 .hash = __VECS(aes_cmac128_tv_template)
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03003156 }
3157 }, {
3158 .alg = "cmac(des3_ede)",
Stephan Mueller8f183752015-08-19 08:42:07 +02003159 .fips_allowed = 1,
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03003160 .test = alg_test_hash,
3161 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003162 .hash = __VECS(des3_ede_cmac64_tv_template)
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03003163 }
3164 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03003165 .alg = "compress_null",
3166 .test = alg_test_null,
3167 }, {
Ard Biesheuvelebb34722015-05-04 11:00:17 +02003168 .alg = "crc32",
3169 .test = alg_test_hash,
Milan Broza8a34412019-01-25 10:31:47 +01003170 .fips_allowed = 1,
Ard Biesheuvelebb34722015-05-04 11:00:17 +02003171 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003172 .hash = __VECS(crc32_tv_template)
Ard Biesheuvelebb34722015-05-04 11:00:17 +02003173 }
3174 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003175 .alg = "crc32c",
Herbert Xu8e3ee852008-11-07 14:58:52 +08003176 .test = alg_test_crc32c,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003177 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003178 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003179 .hash = __VECS(crc32c_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003180 }
3181 }, {
Herbert Xu684115212013-09-07 12:56:26 +10003182 .alg = "crct10dif",
3183 .test = alg_test_hash,
3184 .fips_allowed = 1,
3185 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003186 .hash = __VECS(crct10dif_tv_template)
Herbert Xu684115212013-09-07 12:56:26 +10003187 }
3188 }, {
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08003189 .alg = "ctr(aes)",
3190 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003191 .fips_allowed = 1,
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08003192 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003193 .cipher = __VECS(aes_ctr_tv_template)
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08003194 }
3195 }, {
Jussi Kivilinna85b63e32011-10-10 23:03:03 +03003196 .alg = "ctr(blowfish)",
3197 .test = alg_test_skcipher,
3198 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003199 .cipher = __VECS(bf_ctr_tv_template)
Jussi Kivilinna85b63e32011-10-10 23:03:03 +03003200 }
3201 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02003202 .alg = "ctr(camellia)",
3203 .test = alg_test_skcipher,
3204 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003205 .cipher = __VECS(camellia_ctr_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02003206 }
3207 }, {
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02003208 .alg = "ctr(cast5)",
3209 .test = alg_test_skcipher,
3210 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003211 .cipher = __VECS(cast5_ctr_tv_template)
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02003212 }
3213 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003214 .alg = "ctr(cast6)",
3215 .test = alg_test_skcipher,
3216 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003217 .cipher = __VECS(cast6_ctr_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003218 }
3219 }, {
Jussi Kivilinna8163fc32012-10-20 14:53:07 +03003220 .alg = "ctr(des)",
3221 .test = alg_test_skcipher,
3222 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003223 .cipher = __VECS(des_ctr_tv_template)
Jussi Kivilinna8163fc32012-10-20 14:53:07 +03003224 }
3225 }, {
Jussi Kivilinnae080b172012-10-20 14:53:12 +03003226 .alg = "ctr(des3_ede)",
3227 .test = alg_test_skcipher,
Marcelo Cerri0d8da102017-03-20 17:28:05 -03003228 .fips_allowed = 1,
Jussi Kivilinnae080b172012-10-20 14:53:12 +03003229 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003230 .cipher = __VECS(des3_ede_ctr_tv_template)
Jussi Kivilinnae080b172012-10-20 14:53:12 +03003231 }
3232 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01003233 /* Same as ctr(aes) except the key is stored in
3234 * hardware secure memory which we reference by index
3235 */
3236 .alg = "ctr(paes)",
3237 .test = alg_test_null,
3238 .fips_allowed = 1,
3239 }, {
Jussi Kivilinna9d259172011-10-18 00:02:53 +03003240 .alg = "ctr(serpent)",
3241 .test = alg_test_skcipher,
3242 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003243 .cipher = __VECS(serpent_ctr_tv_template)
Jussi Kivilinna9d259172011-10-18 00:02:53 +03003244 }
3245 }, {
Gilad Ben-Yossef95ba5972018-09-20 14:18:38 +01003246 .alg = "ctr(sm4)",
3247 .test = alg_test_skcipher,
3248 .suite = {
3249 .cipher = __VECS(sm4_ctr_tv_template)
3250 }
3251 }, {
Jussi Kivilinna573da622011-10-10 23:03:12 +03003252 .alg = "ctr(twofish)",
3253 .test = alg_test_skcipher,
3254 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003255 .cipher = __VECS(tf_ctr_tv_template)
Jussi Kivilinna573da622011-10-10 23:03:12 +03003256 }
3257 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003258 .alg = "cts(cbc(aes))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003259 .test = alg_test_skcipher,
Gilad Ben-Yossef196ad602018-11-04 10:05:24 +00003260 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003261 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003262 .cipher = __VECS(cts_mode_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003263 }
3264 }, {
3265 .alg = "deflate",
3266 .test = alg_test_comp,
Milan Broz08189042012-12-06 17:16:28 +08003267 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003268 .suite = {
3269 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003270 .comp = __VECS(deflate_comp_tv_template),
3271 .decomp = __VECS(deflate_decomp_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003272 }
3273 }
3274 }, {
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003275 .alg = "dh",
3276 .test = alg_test_kpp,
3277 .fips_allowed = 1,
3278 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003279 .kpp = __VECS(dh_tv_template)
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01003280 }
3281 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03003282 .alg = "digest_null",
3283 .test = alg_test_null,
3284 }, {
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003285 .alg = "drbg_nopr_ctr_aes128",
3286 .test = alg_test_drbg,
3287 .fips_allowed = 1,
3288 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003289 .drbg = __VECS(drbg_nopr_ctr_aes128_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003290 }
3291 }, {
3292 .alg = "drbg_nopr_ctr_aes192",
3293 .test = alg_test_drbg,
3294 .fips_allowed = 1,
3295 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003296 .drbg = __VECS(drbg_nopr_ctr_aes192_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003297 }
3298 }, {
3299 .alg = "drbg_nopr_ctr_aes256",
3300 .test = alg_test_drbg,
3301 .fips_allowed = 1,
3302 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003303 .drbg = __VECS(drbg_nopr_ctr_aes256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003304 }
3305 }, {
3306 /*
3307 * There is no need to specifically test the DRBG with every
3308 * backend cipher -- covered by drbg_nopr_hmac_sha256 test
3309 */
3310 .alg = "drbg_nopr_hmac_sha1",
3311 .fips_allowed = 1,
3312 .test = alg_test_null,
3313 }, {
3314 .alg = "drbg_nopr_hmac_sha256",
3315 .test = alg_test_drbg,
3316 .fips_allowed = 1,
3317 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003318 .drbg = __VECS(drbg_nopr_hmac_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003319 }
3320 }, {
3321 /* covered by drbg_nopr_hmac_sha256 test */
3322 .alg = "drbg_nopr_hmac_sha384",
3323 .fips_allowed = 1,
3324 .test = alg_test_null,
3325 }, {
3326 .alg = "drbg_nopr_hmac_sha512",
3327 .test = alg_test_null,
3328 .fips_allowed = 1,
3329 }, {
3330 .alg = "drbg_nopr_sha1",
3331 .fips_allowed = 1,
3332 .test = alg_test_null,
3333 }, {
3334 .alg = "drbg_nopr_sha256",
3335 .test = alg_test_drbg,
3336 .fips_allowed = 1,
3337 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003338 .drbg = __VECS(drbg_nopr_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003339 }
3340 }, {
3341 /* covered by drbg_nopr_sha256 test */
3342 .alg = "drbg_nopr_sha384",
3343 .fips_allowed = 1,
3344 .test = alg_test_null,
3345 }, {
3346 .alg = "drbg_nopr_sha512",
3347 .fips_allowed = 1,
3348 .test = alg_test_null,
3349 }, {
3350 .alg = "drbg_pr_ctr_aes128",
3351 .test = alg_test_drbg,
3352 .fips_allowed = 1,
3353 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003354 .drbg = __VECS(drbg_pr_ctr_aes128_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003355 }
3356 }, {
3357 /* covered by drbg_pr_ctr_aes128 test */
3358 .alg = "drbg_pr_ctr_aes192",
3359 .fips_allowed = 1,
3360 .test = alg_test_null,
3361 }, {
3362 .alg = "drbg_pr_ctr_aes256",
3363 .fips_allowed = 1,
3364 .test = alg_test_null,
3365 }, {
3366 .alg = "drbg_pr_hmac_sha1",
3367 .fips_allowed = 1,
3368 .test = alg_test_null,
3369 }, {
3370 .alg = "drbg_pr_hmac_sha256",
3371 .test = alg_test_drbg,
3372 .fips_allowed = 1,
3373 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003374 .drbg = __VECS(drbg_pr_hmac_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003375 }
3376 }, {
3377 /* covered by drbg_pr_hmac_sha256 test */
3378 .alg = "drbg_pr_hmac_sha384",
3379 .fips_allowed = 1,
3380 .test = alg_test_null,
3381 }, {
3382 .alg = "drbg_pr_hmac_sha512",
3383 .test = alg_test_null,
3384 .fips_allowed = 1,
3385 }, {
3386 .alg = "drbg_pr_sha1",
3387 .fips_allowed = 1,
3388 .test = alg_test_null,
3389 }, {
3390 .alg = "drbg_pr_sha256",
3391 .test = alg_test_drbg,
3392 .fips_allowed = 1,
3393 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003394 .drbg = __VECS(drbg_pr_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02003395 }
3396 }, {
3397 /* covered by drbg_pr_sha256 test */
3398 .alg = "drbg_pr_sha384",
3399 .fips_allowed = 1,
3400 .test = alg_test_null,
3401 }, {
3402 .alg = "drbg_pr_sha512",
3403 .fips_allowed = 1,
3404 .test = alg_test_null,
3405 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003406 .alg = "ecb(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003407 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003408 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003409 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003410 .cipher = __VECS(aes_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003411 }
3412 }, {
3413 .alg = "ecb(anubis)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003414 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003415 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003416 .cipher = __VECS(anubis_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003417 }
3418 }, {
3419 .alg = "ecb(arc4)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003420 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003421 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003422 .cipher = __VECS(arc4_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003423 }
3424 }, {
3425 .alg = "ecb(blowfish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003426 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003427 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003428 .cipher = __VECS(bf_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003429 }
3430 }, {
3431 .alg = "ecb(camellia)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003432 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003433 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003434 .cipher = __VECS(camellia_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003435 }
3436 }, {
3437 .alg = "ecb(cast5)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003438 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003439 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003440 .cipher = __VECS(cast5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003441 }
3442 }, {
3443 .alg = "ecb(cast6)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003444 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003445 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003446 .cipher = __VECS(cast6_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003447 }
3448 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03003449 .alg = "ecb(cipher_null)",
3450 .test = alg_test_null,
Milan Broz6175ca22017-04-21 13:03:06 +02003451 .fips_allowed = 1,
Jussi Kivilinnae4483702013-04-07 16:43:56 +03003452 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003453 .alg = "ecb(des)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003454 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003455 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003456 .cipher = __VECS(des_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003457 }
3458 }, {
3459 .alg = "ecb(des3_ede)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003460 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003461 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003462 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003463 .cipher = __VECS(des3_ede_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003464 }
3465 }, {
Jussi Kivilinna66e5bd02013-01-19 13:31:36 +02003466 .alg = "ecb(fcrypt)",
3467 .test = alg_test_skcipher,
3468 .suite = {
3469 .cipher = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003470 .vecs = fcrypt_pcbc_tv_template,
3471 .count = 1
Jussi Kivilinna66e5bd02013-01-19 13:31:36 +02003472 }
3473 }
3474 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003475 .alg = "ecb(khazad)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003476 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003477 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003478 .cipher = __VECS(khazad_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003479 }
3480 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01003481 /* Same as ecb(aes) except the key is stored in
3482 * hardware secure memory which we reference by index
3483 */
3484 .alg = "ecb(paes)",
3485 .test = alg_test_null,
3486 .fips_allowed = 1,
3487 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003488 .alg = "ecb(seed)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003489 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003490 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003491 .cipher = __VECS(seed_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003492 }
3493 }, {
3494 .alg = "ecb(serpent)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003495 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003496 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003497 .cipher = __VECS(serpent_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003498 }
3499 }, {
Gilad Ben-Yossefcd83a8a2018-03-06 09:44:43 +00003500 .alg = "ecb(sm4)",
3501 .test = alg_test_skcipher,
3502 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003503 .cipher = __VECS(sm4_tv_template)
Gilad Ben-Yossefcd83a8a2018-03-06 09:44:43 +00003504 }
3505 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003506 .alg = "ecb(tea)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003507 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003508 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003509 .cipher = __VECS(tea_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003510 }
3511 }, {
3512 .alg = "ecb(tnepres)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003513 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003514 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003515 .cipher = __VECS(tnepres_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003516 }
3517 }, {
3518 .alg = "ecb(twofish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003519 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003520 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003521 .cipher = __VECS(tf_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003522 }
3523 }, {
3524 .alg = "ecb(xeta)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003525 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003526 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003527 .cipher = __VECS(xeta_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003528 }
3529 }, {
3530 .alg = "ecb(xtea)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003531 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003532 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003533 .cipher = __VECS(xtea_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003534 }
3535 }, {
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +01003536 .alg = "ecdh",
3537 .test = alg_test_kpp,
3538 .fips_allowed = 1,
3539 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003540 .kpp = __VECS(ecdh_tv_template)
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +01003541 }
3542 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003543 .alg = "gcm(aes)",
3544 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003545 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003546 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003547 .aead = __VECS(aes_gcm_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003548 }
3549 }, {
Youquan, Song507069c2009-11-23 20:23:04 +08003550 .alg = "ghash",
3551 .test = alg_test_hash,
Jarod Wilson18c0ebd2011-01-29 15:14:35 +11003552 .fips_allowed = 1,
Youquan, Song507069c2009-11-23 20:23:04 +08003553 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003554 .hash = __VECS(ghash_tv_template)
Youquan, Song507069c2009-11-23 20:23:04 +08003555 }
3556 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003557 .alg = "hmac(md5)",
3558 .test = alg_test_hash,
3559 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003560 .hash = __VECS(hmac_md5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003561 }
3562 }, {
3563 .alg = "hmac(rmd128)",
3564 .test = alg_test_hash,
3565 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003566 .hash = __VECS(hmac_rmd128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003567 }
3568 }, {
3569 .alg = "hmac(rmd160)",
3570 .test = alg_test_hash,
3571 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003572 .hash = __VECS(hmac_rmd160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003573 }
3574 }, {
3575 .alg = "hmac(sha1)",
3576 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003577 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003578 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003579 .hash = __VECS(hmac_sha1_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003580 }
3581 }, {
3582 .alg = "hmac(sha224)",
3583 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003584 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003585 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003586 .hash = __VECS(hmac_sha224_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003587 }
3588 }, {
3589 .alg = "hmac(sha256)",
3590 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003591 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003592 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003593 .hash = __VECS(hmac_sha256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003594 }
3595 }, {
raveendra padasalagi98eca722016-07-01 11:16:54 +05303596 .alg = "hmac(sha3-224)",
3597 .test = alg_test_hash,
3598 .fips_allowed = 1,
3599 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003600 .hash = __VECS(hmac_sha3_224_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05303601 }
3602 }, {
3603 .alg = "hmac(sha3-256)",
3604 .test = alg_test_hash,
3605 .fips_allowed = 1,
3606 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003607 .hash = __VECS(hmac_sha3_256_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05303608 }
3609 }, {
3610 .alg = "hmac(sha3-384)",
3611 .test = alg_test_hash,
3612 .fips_allowed = 1,
3613 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003614 .hash = __VECS(hmac_sha3_384_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05303615 }
3616 }, {
3617 .alg = "hmac(sha3-512)",
3618 .test = alg_test_hash,
3619 .fips_allowed = 1,
3620 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003621 .hash = __VECS(hmac_sha3_512_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05303622 }
3623 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003624 .alg = "hmac(sha384)",
3625 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003626 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003627 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003628 .hash = __VECS(hmac_sha384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003629 }
3630 }, {
3631 .alg = "hmac(sha512)",
3632 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003633 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003634 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003635 .hash = __VECS(hmac_sha512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003636 }
3637 }, {
Vitaly Chikunov25a0b9d2018-11-07 00:00:03 +03003638 .alg = "hmac(streebog256)",
3639 .test = alg_test_hash,
3640 .suite = {
3641 .hash = __VECS(hmac_streebog256_tv_template)
3642 }
3643 }, {
3644 .alg = "hmac(streebog512)",
3645 .test = alg_test_hash,
3646 .suite = {
3647 .hash = __VECS(hmac_streebog512_tv_template)
3648 }
3649 }, {
Stephan Muellerbb5530e2015-05-25 15:10:20 +02003650 .alg = "jitterentropy_rng",
3651 .fips_allowed = 1,
3652 .test = alg_test_null,
3653 }, {
Stephan Mueller35351982015-09-21 20:59:56 +02003654 .alg = "kw(aes)",
3655 .test = alg_test_skcipher,
3656 .fips_allowed = 1,
3657 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003658 .cipher = __VECS(aes_kw_tv_template)
Stephan Mueller35351982015-09-21 20:59:56 +02003659 }
3660 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003661 .alg = "lrw(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003662 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003663 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003664 .cipher = __VECS(aes_lrw_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003665 }
3666 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02003667 .alg = "lrw(camellia)",
3668 .test = alg_test_skcipher,
3669 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003670 .cipher = __VECS(camellia_lrw_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02003671 }
3672 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003673 .alg = "lrw(cast6)",
3674 .test = alg_test_skcipher,
3675 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003676 .cipher = __VECS(cast6_lrw_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003677 }
3678 }, {
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03003679 .alg = "lrw(serpent)",
3680 .test = alg_test_skcipher,
3681 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003682 .cipher = __VECS(serpent_lrw_tv_template)
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03003683 }
3684 }, {
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03003685 .alg = "lrw(twofish)",
3686 .test = alg_test_skcipher,
3687 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003688 .cipher = __VECS(tf_lrw_tv_template)
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03003689 }
3690 }, {
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02003691 .alg = "lz4",
3692 .test = alg_test_comp,
3693 .fips_allowed = 1,
3694 .suite = {
3695 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003696 .comp = __VECS(lz4_comp_tv_template),
3697 .decomp = __VECS(lz4_decomp_tv_template)
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02003698 }
3699 }
3700 }, {
3701 .alg = "lz4hc",
3702 .test = alg_test_comp,
3703 .fips_allowed = 1,
3704 .suite = {
3705 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003706 .comp = __VECS(lz4hc_comp_tv_template),
3707 .decomp = __VECS(lz4hc_decomp_tv_template)
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02003708 }
3709 }
3710 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003711 .alg = "lzo",
3712 .test = alg_test_comp,
Milan Broz08189042012-12-06 17:16:28 +08003713 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003714 .suite = {
3715 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003716 .comp = __VECS(lzo_comp_tv_template),
3717 .decomp = __VECS(lzo_decomp_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003718 }
3719 }
3720 }, {
3721 .alg = "md4",
3722 .test = alg_test_hash,
3723 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003724 .hash = __VECS(md4_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003725 }
3726 }, {
3727 .alg = "md5",
3728 .test = alg_test_hash,
3729 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003730 .hash = __VECS(md5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003731 }
3732 }, {
3733 .alg = "michael_mic",
3734 .test = alg_test_hash,
3735 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003736 .hash = __VECS(michael_mic_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003737 }
3738 }, {
Ondrej Mosnacek4feb4c52018-05-11 14:19:10 +02003739 .alg = "morus1280",
3740 .test = alg_test_aead,
3741 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003742 .aead = __VECS(morus1280_tv_template)
Ondrej Mosnacek4feb4c52018-05-11 14:19:10 +02003743 }
3744 }, {
3745 .alg = "morus640",
3746 .test = alg_test_aead,
3747 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003748 .aead = __VECS(morus640_tv_template)
Ondrej Mosnacek4feb4c52018-05-11 14:19:10 +02003749 }
3750 }, {
Eric Biggers26609a22018-11-16 17:26:29 -08003751 .alg = "nhpoly1305",
3752 .test = alg_test_hash,
3753 .suite = {
3754 .hash = __VECS(nhpoly1305_tv_template)
3755 }
3756 }, {
Puneet Saxenaba0e14a2011-05-04 15:04:10 +10003757 .alg = "ofb(aes)",
3758 .test = alg_test_skcipher,
3759 .fips_allowed = 1,
3760 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003761 .cipher = __VECS(aes_ofb_tv_template)
Puneet Saxenaba0e14a2011-05-04 15:04:10 +10003762 }
3763 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01003764 /* Same as ofb(aes) except the key is stored in
3765 * hardware secure memory which we reference by index
3766 */
3767 .alg = "ofb(paes)",
3768 .test = alg_test_null,
3769 .fips_allowed = 1,
3770 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003771 .alg = "pcbc(fcrypt)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003772 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003773 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003774 .cipher = __VECS(fcrypt_pcbc_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003775 }
3776 }, {
Stephan Mueller12071072017-06-12 23:27:51 +02003777 .alg = "pkcs1pad(rsa,sha224)",
3778 .test = alg_test_null,
3779 .fips_allowed = 1,
3780 }, {
3781 .alg = "pkcs1pad(rsa,sha256)",
3782 .test = alg_test_akcipher,
3783 .fips_allowed = 1,
3784 .suite = {
3785 .akcipher = __VECS(pkcs1pad_rsa_tv_template)
3786 }
3787 }, {
3788 .alg = "pkcs1pad(rsa,sha384)",
3789 .test = alg_test_null,
3790 .fips_allowed = 1,
3791 }, {
3792 .alg = "pkcs1pad(rsa,sha512)",
3793 .test = alg_test_null,
3794 .fips_allowed = 1,
3795 }, {
Martin Willieee9dc62015-06-01 13:43:59 +02003796 .alg = "poly1305",
3797 .test = alg_test_hash,
3798 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003799 .hash = __VECS(poly1305_tv_template)
Martin Willieee9dc62015-06-01 13:43:59 +02003800 }
3801 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003802 .alg = "rfc3686(ctr(aes))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003803 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003804 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003805 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003806 .cipher = __VECS(aes_ctr_rfc3686_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003807 }
3808 }, {
Herbert Xu3f31a742015-07-09 07:17:34 +08003809 .alg = "rfc4106(gcm(aes))",
Adrian Hoban69435b92010-11-04 15:02:04 -04003810 .test = alg_test_aead,
Jarod Wilsondb71f29a2015-01-23 12:42:15 -05003811 .fips_allowed = 1,
Adrian Hoban69435b92010-11-04 15:02:04 -04003812 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003813 .aead = __VECS(aes_gcm_rfc4106_tv_template)
Adrian Hoban69435b92010-11-04 15:02:04 -04003814 }
3815 }, {
Herbert Xu544c4362015-07-14 16:53:22 +08003816 .alg = "rfc4309(ccm(aes))",
Jarod Wilson5d667322009-05-04 19:23:40 +08003817 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003818 .fips_allowed = 1,
Jarod Wilson5d667322009-05-04 19:23:40 +08003819 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003820 .aead = __VECS(aes_ccm_rfc4309_tv_template)
Jarod Wilson5d667322009-05-04 19:23:40 +08003821 }
3822 }, {
Herbert Xubb687452015-06-16 13:54:24 +08003823 .alg = "rfc4543(gcm(aes))",
Jussi Kivilinnae9b74412013-04-07 16:43:51 +03003824 .test = alg_test_aead,
3825 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003826 .aead = __VECS(aes_gcm_rfc4543_tv_template)
Jussi Kivilinnae9b74412013-04-07 16:43:51 +03003827 }
3828 }, {
Martin Williaf2b76b2015-06-01 13:44:01 +02003829 .alg = "rfc7539(chacha20,poly1305)",
3830 .test = alg_test_aead,
3831 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003832 .aead = __VECS(rfc7539_tv_template)
Martin Williaf2b76b2015-06-01 13:44:01 +02003833 }
3834 }, {
Martin Willi59007582015-06-01 13:44:03 +02003835 .alg = "rfc7539esp(chacha20,poly1305)",
3836 .test = alg_test_aead,
3837 .suite = {
Eric Biggersa0d608ee2019-01-13 15:32:28 -08003838 .aead = __VECS(rfc7539esp_tv_template)
Martin Willi59007582015-06-01 13:44:03 +02003839 }
3840 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003841 .alg = "rmd128",
3842 .test = alg_test_hash,
3843 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003844 .hash = __VECS(rmd128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003845 }
3846 }, {
3847 .alg = "rmd160",
3848 .test = alg_test_hash,
3849 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003850 .hash = __VECS(rmd160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003851 }
3852 }, {
3853 .alg = "rmd256",
3854 .test = alg_test_hash,
3855 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003856 .hash = __VECS(rmd256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003857 }
3858 }, {
3859 .alg = "rmd320",
3860 .test = alg_test_hash,
3861 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003862 .hash = __VECS(rmd320_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003863 }
3864 }, {
Tadeusz Struk946cc462015-06-16 10:31:06 -07003865 .alg = "rsa",
3866 .test = alg_test_akcipher,
3867 .fips_allowed = 1,
3868 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003869 .akcipher = __VECS(rsa_tv_template)
Tadeusz Struk946cc462015-06-16 10:31:06 -07003870 }
3871 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003872 .alg = "salsa20",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003873 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003874 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003875 .cipher = __VECS(salsa20_stream_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003876 }
3877 }, {
3878 .alg = "sha1",
3879 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003880 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003881 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003882 .hash = __VECS(sha1_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003883 }
3884 }, {
3885 .alg = "sha224",
3886 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003887 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003888 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003889 .hash = __VECS(sha224_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003890 }
3891 }, {
3892 .alg = "sha256",
3893 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003894 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003895 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003896 .hash = __VECS(sha256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003897 }
3898 }, {
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05303899 .alg = "sha3-224",
3900 .test = alg_test_hash,
3901 .fips_allowed = 1,
3902 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003903 .hash = __VECS(sha3_224_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05303904 }
3905 }, {
3906 .alg = "sha3-256",
3907 .test = alg_test_hash,
3908 .fips_allowed = 1,
3909 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003910 .hash = __VECS(sha3_256_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05303911 }
3912 }, {
3913 .alg = "sha3-384",
3914 .test = alg_test_hash,
3915 .fips_allowed = 1,
3916 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003917 .hash = __VECS(sha3_384_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05303918 }
3919 }, {
3920 .alg = "sha3-512",
3921 .test = alg_test_hash,
3922 .fips_allowed = 1,
3923 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003924 .hash = __VECS(sha3_512_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05303925 }
3926 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003927 .alg = "sha384",
3928 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003929 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003930 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003931 .hash = __VECS(sha384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003932 }
3933 }, {
3934 .alg = "sha512",
3935 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003936 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003937 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003938 .hash = __VECS(sha512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003939 }
3940 }, {
Gilad Ben-Yossefb7e27532017-08-21 13:51:29 +03003941 .alg = "sm3",
3942 .test = alg_test_hash,
3943 .suite = {
3944 .hash = __VECS(sm3_tv_template)
3945 }
3946 }, {
Vitaly Chikunov25a0b9d2018-11-07 00:00:03 +03003947 .alg = "streebog256",
3948 .test = alg_test_hash,
3949 .suite = {
3950 .hash = __VECS(streebog256_tv_template)
3951 }
3952 }, {
3953 .alg = "streebog512",
3954 .test = alg_test_hash,
3955 .suite = {
3956 .hash = __VECS(streebog512_tv_template)
3957 }
3958 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003959 .alg = "tgr128",
3960 .test = alg_test_hash,
3961 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003962 .hash = __VECS(tgr128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003963 }
3964 }, {
3965 .alg = "tgr160",
3966 .test = alg_test_hash,
3967 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003968 .hash = __VECS(tgr160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003969 }
3970 }, {
3971 .alg = "tgr192",
3972 .test = alg_test_hash,
3973 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003974 .hash = __VECS(tgr192_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003975 }
3976 }, {
Eric Biggersed331ad2018-06-18 10:22:39 -07003977 .alg = "vmac64(aes)",
3978 .test = alg_test_hash,
3979 .suite = {
3980 .hash = __VECS(vmac64_aes_tv_template)
3981 }
3982 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003983 .alg = "wp256",
3984 .test = alg_test_hash,
3985 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003986 .hash = __VECS(wp256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003987 }
3988 }, {
3989 .alg = "wp384",
3990 .test = alg_test_hash,
3991 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003992 .hash = __VECS(wp384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003993 }
3994 }, {
3995 .alg = "wp512",
3996 .test = alg_test_hash,
3997 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003998 .hash = __VECS(wp512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003999 }
4000 }, {
4001 .alg = "xcbc(aes)",
4002 .test = alg_test_hash,
4003 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00004004 .hash = __VECS(aes_xcbc128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004005 }
4006 }, {
Eric Biggersaa762402018-11-16 17:26:22 -08004007 .alg = "xchacha12",
4008 .test = alg_test_skcipher,
4009 .suite = {
4010 .cipher = __VECS(xchacha12_tv_template)
4011 },
4012 }, {
Eric Biggersde61d7a2018-11-16 17:26:20 -08004013 .alg = "xchacha20",
4014 .test = alg_test_skcipher,
4015 .suite = {
4016 .cipher = __VECS(xchacha20_tv_template)
4017 },
4018 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08004019 .alg = "xts(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004020 .test = alg_test_skcipher,
Jarod Wilson2918aa82011-01-29 15:14:01 +11004021 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08004022 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004023 .cipher = __VECS(aes_xts_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08004024 }
Geert Uytterhoeven0c01aed2009-03-04 15:42:15 +08004025 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02004026 .alg = "xts(camellia)",
4027 .test = alg_test_skcipher,
4028 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004029 .cipher = __VECS(camellia_xts_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02004030 }
4031 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004032 .alg = "xts(cast6)",
4033 .test = alg_test_skcipher,
4034 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004035 .cipher = __VECS(cast6_xts_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02004036 }
4037 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01004038 /* Same as xts(aes) except the key is stored in
4039 * hardware secure memory which we reference by index
4040 */
4041 .alg = "xts(paes)",
4042 .test = alg_test_null,
4043 .fips_allowed = 1,
4044 }, {
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03004045 .alg = "xts(serpent)",
4046 .test = alg_test_skcipher,
4047 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004048 .cipher = __VECS(serpent_xts_tv_template)
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03004049 }
4050 }, {
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03004051 .alg = "xts(twofish)",
4052 .test = alg_test_skcipher,
4053 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07004054 .cipher = __VECS(tf_xts_tv_template)
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03004055 }
Giovanni Cabiddua368f432017-04-21 21:54:30 +01004056 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01004057 .alg = "xts4096(paes)",
4058 .test = alg_test_null,
4059 .fips_allowed = 1,
4060 }, {
4061 .alg = "xts512(paes)",
4062 .test = alg_test_null,
4063 .fips_allowed = 1,
4064 }, {
Giovanni Cabiddua368f432017-04-21 21:54:30 +01004065 .alg = "zlib-deflate",
4066 .test = alg_test_comp,
4067 .fips_allowed = 1,
4068 .suite = {
4069 .comp = {
4070 .comp = __VECS(zlib_deflate_comp_tv_template),
4071 .decomp = __VECS(zlib_deflate_decomp_tv_template)
4072 }
4073 }
Nick Terrelld28fc3d2018-03-30 12:14:53 -07004074 }, {
4075 .alg = "zstd",
4076 .test = alg_test_comp,
4077 .fips_allowed = 1,
4078 .suite = {
4079 .comp = {
4080 .comp = __VECS(zstd_comp_tv_template),
4081 .decomp = __VECS(zstd_decomp_tv_template)
4082 }
4083 }
Herbert Xuda7f0332008-07-31 17:08:25 +08004084 }
4085};
4086
Eric Biggers3f47a032019-01-31 23:51:43 -08004087static void alg_check_test_descs_order(void)
Jussi Kivilinna57147582013-06-13 17:37:40 +03004088{
4089 int i;
4090
Jussi Kivilinna57147582013-06-13 17:37:40 +03004091 for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
4092 int diff = strcmp(alg_test_descs[i - 1].alg,
4093 alg_test_descs[i].alg);
4094
4095 if (WARN_ON(diff > 0)) {
4096 pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
4097 alg_test_descs[i - 1].alg,
4098 alg_test_descs[i].alg);
4099 }
4100
4101 if (WARN_ON(diff == 0)) {
4102 pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
4103 alg_test_descs[i].alg);
4104 }
4105 }
4106}
4107
Eric Biggers3f47a032019-01-31 23:51:43 -08004108static void alg_check_testvec_configs(void)
4109{
4110}
4111
4112static void testmgr_onetime_init(void)
4113{
4114 alg_check_test_descs_order();
4115 alg_check_testvec_configs();
Eric Biggers5b2706a2019-01-31 23:51:44 -08004116
4117#ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
4118 pr_warn("alg: extra crypto tests enabled. This is intended for developer use only.\n");
4119#endif
Eric Biggers3f47a032019-01-31 23:51:43 -08004120}
4121
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004122static int alg_find_test(const char *alg)
Herbert Xuda7f0332008-07-31 17:08:25 +08004123{
4124 int start = 0;
4125 int end = ARRAY_SIZE(alg_test_descs);
4126
4127 while (start < end) {
4128 int i = (start + end) / 2;
4129 int diff = strcmp(alg_test_descs[i].alg, alg);
4130
4131 if (diff > 0) {
4132 end = i;
4133 continue;
4134 }
4135
4136 if (diff < 0) {
4137 start = i + 1;
4138 continue;
4139 }
4140
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004141 return i;
Herbert Xuda7f0332008-07-31 17:08:25 +08004142 }
4143
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004144 return -1;
4145}
4146
4147int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
4148{
4149 int i;
Herbert Xua68f6612009-07-02 16:32:12 +08004150 int j;
Neil Hormand12d6b62008-10-12 20:36:51 +08004151 int rc;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004152
Richard W.M. Jones9e5c9fe2016-05-03 10:00:17 +01004153 if (!fips_enabled && notests) {
4154 printk_once(KERN_INFO "alg: self-tests disabled\n");
4155 return 0;
4156 }
4157
Eric Biggers3f47a032019-01-31 23:51:43 -08004158 DO_ONCE(testmgr_onetime_init);
Jussi Kivilinna57147582013-06-13 17:37:40 +03004159
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004160 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
4161 char nalg[CRYPTO_MAX_ALG_NAME];
4162
4163 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
4164 sizeof(nalg))
4165 return -ENAMETOOLONG;
4166
4167 i = alg_find_test(nalg);
4168 if (i < 0)
4169 goto notest;
4170
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10004171 if (fips_enabled && !alg_test_descs[i].fips_allowed)
4172 goto non_fips_alg;
4173
Jarod Wilson941fb322009-05-04 19:49:23 +08004174 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
4175 goto test_done;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004176 }
4177
4178 i = alg_find_test(alg);
Herbert Xua68f6612009-07-02 16:32:12 +08004179 j = alg_find_test(driver);
4180 if (i < 0 && j < 0)
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004181 goto notest;
4182
Herbert Xua68f6612009-07-02 16:32:12 +08004183 if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
4184 (j >= 0 && !alg_test_descs[j].fips_allowed)))
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10004185 goto non_fips_alg;
4186
Herbert Xua68f6612009-07-02 16:32:12 +08004187 rc = 0;
4188 if (i >= 0)
4189 rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
4190 type, mask);
Cristian Stoica032c8ca2013-07-18 18:57:07 +03004191 if (j >= 0 && j != i)
Herbert Xua68f6612009-07-02 16:32:12 +08004192 rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
4193 type, mask);
4194
Jarod Wilson941fb322009-05-04 19:49:23 +08004195test_done:
Neil Hormand12d6b62008-10-12 20:36:51 +08004196 if (fips_enabled && rc)
4197 panic("%s: %s alg self test failed in fips mode!\n", driver, alg);
4198
Jarod Wilson29ecd4a2009-05-04 19:51:17 +08004199 if (fips_enabled && !rc)
Masanari Iida3e8cffd2014-10-07 00:37:54 +09004200 pr_info("alg: self-tests for %s (%s) passed\n", driver, alg);
Jarod Wilson29ecd4a2009-05-04 19:51:17 +08004201
Neil Hormand12d6b62008-10-12 20:36:51 +08004202 return rc;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10004203
4204notest:
Herbert Xuda7f0332008-07-31 17:08:25 +08004205 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
4206 return 0;
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10004207non_fips_alg:
4208 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08004209}
Alexander Shishkin0b767f92010-06-03 20:53:43 +10004210
Herbert Xu326a6342010-08-06 09:40:28 +08004211#endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
Alexander Shishkin0b767f92010-06-03 20:53:43 +10004212
Herbert Xuda7f0332008-07-31 17:08:25 +08004213EXPORT_SYMBOL_GPL(alg_test);