]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - crypto/testmgr.c
crypto: bcm - remove unused function do_decrypt()
[mirror_ubuntu-focal-kernel.git] / crypto / testmgr.c
CommitLineData
da7f033d
HX
1/*
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>
8 *
69435b94
AH
9 * Updated RFC4106 AES-GCM testing.
10 * Authors: Aidan O'Mahony (aidan.o.mahony@intel.com)
11 * Adrian Hoban <adrian.hoban@intel.com>
12 * Gabriele Paoloni <gabriele.paoloni@intel.com>
13 * Tadeusz Struk (tadeusz.struk@intel.com)
14 * Copyright (c) 2010, Intel Corporation.
15 *
da7f033d
HX
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the Free
18 * Software Foundation; either version 2 of the License, or (at your option)
19 * any later version.
20 *
21 */
22
1ce33115 23#include <crypto/aead.h>
da7f033d 24#include <crypto/hash.h>
12773d93 25#include <crypto/skcipher.h>
da7f033d 26#include <linux/err.h>
1c41b882 27#include <linux/fips.h>
da7f033d
HX
28#include <linux/module.h>
29#include <linux/scatterlist.h>
30#include <linux/slab.h>
31#include <linux/string.h>
7647d6ce 32#include <crypto/rng.h>
64d1cdfb 33#include <crypto/drbg.h>
946cc463 34#include <crypto/akcipher.h>
802c7f1c 35#include <crypto/kpp.h>
d7db7a88 36#include <crypto/acompress.h>
da7f033d
HX
37
38#include "internal.h"
0b767f96 39
9e5c9fe4
RJ
40static bool notests;
41module_param(notests, bool, 0644);
42MODULE_PARM_DESC(notests, "disable crypto self-tests");
43
326a6346 44#ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
0b767f96
AS
45
46/* a perfect nop */
47int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
48{
49 return 0;
50}
51
52#else
53
da7f033d
HX
54#include "testmgr.h"
55
56/*
57 * Need slab memory for testing (size in number of pages).
58 */
59#define XBUFSIZE 8
60
61/*
62 * Indexes into the xbuf to simulate cross-page access.
63 */
64#define IDX1 32
65#define IDX2 32400
04b46fbd 66#define IDX3 1511
da7f033d
HX
67#define IDX4 8193
68#define IDX5 22222
69#define IDX6 17101
70#define IDX7 27333
71#define IDX8 3000
72
73/*
74* Used by test_cipher()
75*/
76#define ENCRYPT 1
77#define DECRYPT 0
78
da7f033d 79struct aead_test_suite {
a0d608ee
EB
80 const struct aead_testvec *vecs;
81 unsigned int count;
da7f033d
HX
82};
83
84struct cipher_test_suite {
92a4c9fe
EB
85 const struct cipher_testvec *vecs;
86 unsigned int count;
da7f033d
HX
87};
88
89struct comp_test_suite {
90 struct {
b13b1e0c 91 const struct comp_testvec *vecs;
da7f033d
HX
92 unsigned int count;
93 } comp, decomp;
94};
95
96struct hash_test_suite {
b13b1e0c 97 const struct hash_testvec *vecs;
da7f033d
HX
98 unsigned int count;
99};
100
7647d6ce 101struct cprng_test_suite {
b13b1e0c 102 const struct cprng_testvec *vecs;
7647d6ce
JW
103 unsigned int count;
104};
105
64d1cdfb 106struct drbg_test_suite {
b13b1e0c 107 const struct drbg_testvec *vecs;
64d1cdfb
SM
108 unsigned int count;
109};
110
946cc463 111struct akcipher_test_suite {
b13b1e0c 112 const struct akcipher_testvec *vecs;
946cc463
TS
113 unsigned int count;
114};
115
802c7f1c 116struct kpp_test_suite {
b13b1e0c 117 const struct kpp_testvec *vecs;
802c7f1c
SB
118 unsigned int count;
119};
120
da7f033d
HX
121struct alg_test_desc {
122 const char *alg;
123 int (*test)(const struct alg_test_desc *desc, const char *driver,
124 u32 type, u32 mask);
a1915d51 125 int fips_allowed; /* set if alg is allowed in fips mode */
da7f033d
HX
126
127 union {
128 struct aead_test_suite aead;
129 struct cipher_test_suite cipher;
130 struct comp_test_suite comp;
131 struct hash_test_suite hash;
7647d6ce 132 struct cprng_test_suite cprng;
64d1cdfb 133 struct drbg_test_suite drbg;
946cc463 134 struct akcipher_test_suite akcipher;
802c7f1c 135 struct kpp_test_suite kpp;
da7f033d
HX
136 } suite;
137};
138
b13b1e0c
EB
139static const unsigned int IDX[8] = {
140 IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
da7f033d 141
da7f033d
HX
142static void hexdump(unsigned char *buf, unsigned int len)
143{
144 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
145 16, 1,
146 buf, len, false);
147}
148
f8b0d4d0
HX
149static int testmgr_alloc_buf(char *buf[XBUFSIZE])
150{
151 int i;
152
153 for (i = 0; i < XBUFSIZE; i++) {
154 buf[i] = (void *)__get_free_page(GFP_KERNEL);
155 if (!buf[i])
156 goto err_free_buf;
157 }
158
159 return 0;
160
161err_free_buf:
162 while (i-- > 0)
163 free_page((unsigned long)buf[i]);
164
165 return -ENOMEM;
166}
167
168static void testmgr_free_buf(char *buf[XBUFSIZE])
169{
170 int i;
171
172 for (i = 0; i < XBUFSIZE; i++)
173 free_page((unsigned long)buf[i]);
174}
175
466d7b9f
KK
176static int ahash_guard_result(char *result, char c, int size)
177{
178 int i;
179
180 for (i = 0; i < size; i++) {
181 if (result[i] != c)
182 return -EINVAL;
183 }
184
185 return 0;
186}
187
018ba95c 188static int ahash_partial_update(struct ahash_request **preq,
b13b1e0c 189 struct crypto_ahash *tfm, const struct hash_testvec *template,
018ba95c 190 void *hash_buff, int k, int temp, struct scatterlist *sg,
7f397136 191 const char *algo, char *result, struct crypto_wait *wait)
018ba95c
WR
192{
193 char *state;
194 struct ahash_request *req;
195 int statesize, ret = -EINVAL;
da1729ce 196 static const unsigned char guard[] = { 0x00, 0xba, 0xad, 0x00 };
466d7b9f 197 int digestsize = crypto_ahash_digestsize(tfm);
018ba95c
WR
198
199 req = *preq;
200 statesize = crypto_ahash_statesize(
201 crypto_ahash_reqtfm(req));
7bcb87bc 202 state = kmalloc(statesize + sizeof(guard), GFP_KERNEL);
018ba95c 203 if (!state) {
cf3f9609 204 pr_err("alg: hash: Failed to alloc state for %s\n", algo);
018ba95c
WR
205 goto out_nostate;
206 }
7bcb87bc 207 memcpy(state + statesize, guard, sizeof(guard));
466d7b9f 208 memset(result, 1, digestsize);
018ba95c 209 ret = crypto_ahash_export(req, state);
7bcb87bc 210 WARN_ON(memcmp(state + statesize, guard, sizeof(guard)));
018ba95c 211 if (ret) {
cf3f9609 212 pr_err("alg: hash: Failed to export() for %s\n", algo);
018ba95c
WR
213 goto out;
214 }
466d7b9f
KK
215 ret = ahash_guard_result(result, 1, digestsize);
216 if (ret) {
217 pr_err("alg: hash: Failed, export used req->result for %s\n",
218 algo);
219 goto out;
220 }
018ba95c
WR
221 ahash_request_free(req);
222 req = ahash_request_alloc(tfm, GFP_KERNEL);
223 if (!req) {
224 pr_err("alg: hash: Failed to alloc request for %s\n", algo);
225 goto out_noreq;
226 }
227 ahash_request_set_callback(req,
228 CRYPTO_TFM_REQ_MAY_BACKLOG,
7f397136 229 crypto_req_done, wait);
018ba95c
WR
230
231 memcpy(hash_buff, template->plaintext + temp,
232 template->tap[k]);
233 sg_init_one(&sg[0], hash_buff, template->tap[k]);
234 ahash_request_set_crypt(req, sg, result, template->tap[k]);
235 ret = crypto_ahash_import(req, state);
236 if (ret) {
237 pr_err("alg: hash: Failed to import() for %s\n", algo);
238 goto out;
239 }
466d7b9f
KK
240 ret = ahash_guard_result(result, 1, digestsize);
241 if (ret) {
242 pr_err("alg: hash: Failed, import used req->result for %s\n",
243 algo);
244 goto out;
245 }
7f397136 246 ret = crypto_wait_req(crypto_ahash_update(req), wait);
018ba95c
WR
247 if (ret)
248 goto out;
249 *preq = req;
250 ret = 0;
251 goto out_noreq;
252out:
253 ahash_request_free(req);
254out_noreq:
255 kfree(state);
256out_nostate:
257 return ret;
258}
259
76715095
GBY
260enum hash_test {
261 HASH_TEST_DIGEST,
262 HASH_TEST_FINAL,
263 HASH_TEST_FINUP
264};
265
b13b1e0c
EB
266static int __test_hash(struct crypto_ahash *tfm,
267 const struct hash_testvec *template, unsigned int tcount,
76715095 268 enum hash_test test_type, const int align_offset)
da7f033d
HX
269{
270 const char *algo = crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
e93acd6f 271 size_t digest_size = crypto_ahash_digestsize(tfm);
da7f033d
HX
272 unsigned int i, j, k, temp;
273 struct scatterlist sg[8];
29b77e5d
HG
274 char *result;
275 char *key;
da7f033d 276 struct ahash_request *req;
7f397136 277 struct crypto_wait wait;
da7f033d 278 void *hash_buff;
f8b0d4d0
HX
279 char *xbuf[XBUFSIZE];
280 int ret = -ENOMEM;
281
e93acd6f 282 result = kmalloc(digest_size, GFP_KERNEL);
29b77e5d
HG
283 if (!result)
284 return ret;
285 key = kmalloc(MAX_KEYLEN, GFP_KERNEL);
286 if (!key)
287 goto out_nobuf;
f8b0d4d0
HX
288 if (testmgr_alloc_buf(xbuf))
289 goto out_nobuf;
da7f033d 290
7f397136 291 crypto_init_wait(&wait);
da7f033d
HX
292
293 req = ahash_request_alloc(tfm, GFP_KERNEL);
294 if (!req) {
295 printk(KERN_ERR "alg: hash: Failed to allocate request for "
296 "%s\n", algo);
da7f033d
HX
297 goto out_noreq;
298 }
299 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
7f397136 300 crypto_req_done, &wait);
da7f033d 301
a0cfae59 302 j = 0;
da7f033d 303 for (i = 0; i < tcount; i++) {
a0cfae59
HX
304 if (template[i].np)
305 continue;
306
da5ffe11
JK
307 ret = -EINVAL;
308 if (WARN_ON(align_offset + template[i].psize > PAGE_SIZE))
309 goto out;
310
a0cfae59 311 j++;
e93acd6f 312 memset(result, 0, digest_size);
da7f033d
HX
313
314 hash_buff = xbuf[0];
da5ffe11 315 hash_buff += align_offset;
da7f033d
HX
316
317 memcpy(hash_buff, template[i].plaintext, template[i].psize);
318 sg_init_one(&sg[0], hash_buff, template[i].psize);
319
320 if (template[i].ksize) {
321 crypto_ahash_clear_flags(tfm, ~0);
29b77e5d
HG
322 if (template[i].ksize > MAX_KEYLEN) {
323 pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
324 j, algo, template[i].ksize, MAX_KEYLEN);
325 ret = -EINVAL;
326 goto out;
327 }
328 memcpy(key, template[i].key, template[i].ksize);
329 ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
da7f033d
HX
330 if (ret) {
331 printk(KERN_ERR "alg: hash: setkey failed on "
a0cfae59 332 "test %d for %s: ret=%d\n", j, algo,
da7f033d
HX
333 -ret);
334 goto out;
335 }
336 }
337
338 ahash_request_set_crypt(req, sg, result, template[i].psize);
76715095
GBY
339 switch (test_type) {
340 case HASH_TEST_DIGEST:
7f397136 341 ret = crypto_wait_req(crypto_ahash_digest(req), &wait);
a8f1a052
DM
342 if (ret) {
343 pr_err("alg: hash: digest failed on test %d "
344 "for %s: ret=%d\n", j, algo, -ret);
345 goto out;
346 }
76715095
GBY
347 break;
348
349 case HASH_TEST_FINAL:
466d7b9f 350 memset(result, 1, digest_size);
7f397136 351 ret = crypto_wait_req(crypto_ahash_init(req), &wait);
a8f1a052 352 if (ret) {
cf3f9609 353 pr_err("alg: hash: init failed on test %d "
a8f1a052
DM
354 "for %s: ret=%d\n", j, algo, -ret);
355 goto out;
356 }
466d7b9f
KK
357 ret = ahash_guard_result(result, 1, digest_size);
358 if (ret) {
359 pr_err("alg: hash: init failed on test %d "
360 "for %s: used req->result\n", j, algo);
361 goto out;
362 }
7f397136 363 ret = crypto_wait_req(crypto_ahash_update(req), &wait);
a8f1a052 364 if (ret) {
cf3f9609 365 pr_err("alg: hash: update failed on test %d "
a8f1a052
DM
366 "for %s: ret=%d\n", j, algo, -ret);
367 goto out;
368 }
466d7b9f
KK
369 ret = ahash_guard_result(result, 1, digest_size);
370 if (ret) {
371 pr_err("alg: hash: update failed on test %d "
372 "for %s: used req->result\n", j, algo);
373 goto out;
374 }
7f397136 375 ret = crypto_wait_req(crypto_ahash_final(req), &wait);
a8f1a052 376 if (ret) {
cf3f9609 377 pr_err("alg: hash: final failed on test %d "
a8f1a052
DM
378 "for %s: ret=%d\n", j, algo, -ret);
379 goto out;
da7f033d 380 }
76715095
GBY
381 break;
382
383 case HASH_TEST_FINUP:
384 memset(result, 1, digest_size);
385 ret = crypto_wait_req(crypto_ahash_init(req), &wait);
386 if (ret) {
387 pr_err("alg: hash: init failed on test %d "
388 "for %s: ret=%d\n", j, algo, -ret);
389 goto out;
390 }
391 ret = ahash_guard_result(result, 1, digest_size);
392 if (ret) {
393 pr_err("alg: hash: init failed on test %d "
394 "for %s: used req->result\n", j, algo);
395 goto out;
396 }
397 ret = crypto_wait_req(crypto_ahash_finup(req), &wait);
398 if (ret) {
399 pr_err("alg: hash: final failed on test %d "
400 "for %s: ret=%d\n", j, algo, -ret);
401 goto out;
402 }
403 break;
da7f033d
HX
404 }
405
406 if (memcmp(result, template[i].digest,
407 crypto_ahash_digestsize(tfm))) {
408 printk(KERN_ERR "alg: hash: Test %d failed for %s\n",
a0cfae59 409 j, algo);
da7f033d
HX
410 hexdump(result, crypto_ahash_digestsize(tfm));
411 ret = -EINVAL;
412 goto out;
413 }
414 }
415
76715095
GBY
416 if (test_type)
417 goto out;
418
da7f033d
HX
419 j = 0;
420 for (i = 0; i < tcount; i++) {
da5ffe11
JK
421 /* alignment tests are only done with continuous buffers */
422 if (align_offset != 0)
423 break;
424
5f2b424e
CS
425 if (!template[i].np)
426 continue;
da7f033d 427
5f2b424e 428 j++;
e93acd6f 429 memset(result, 0, digest_size);
da7f033d 430
5f2b424e
CS
431 temp = 0;
432 sg_init_table(sg, template[i].np);
433 ret = -EINVAL;
434 for (k = 0; k < template[i].np; k++) {
435 if (WARN_ON(offset_in_page(IDX[k]) +
436 template[i].tap[k] > PAGE_SIZE))
437 goto out;
438 sg_set_buf(&sg[k],
439 memcpy(xbuf[IDX[k] >> PAGE_SHIFT] +
440 offset_in_page(IDX[k]),
441 template[i].plaintext + temp,
442 template[i].tap[k]),
443 template[i].tap[k]);
444 temp += template[i].tap[k];
445 }
da7f033d 446
5f2b424e
CS
447 if (template[i].ksize) {
448 if (template[i].ksize > MAX_KEYLEN) {
449 pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
450 j, algo, template[i].ksize, MAX_KEYLEN);
451 ret = -EINVAL;
da7f033d
HX
452 goto out;
453 }
5f2b424e
CS
454 crypto_ahash_clear_flags(tfm, ~0);
455 memcpy(key, template[i].key, template[i].ksize);
456 ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
da7f033d 457
5f2b424e
CS
458 if (ret) {
459 printk(KERN_ERR "alg: hash: setkey "
460 "failed on chunking test %d "
461 "for %s: ret=%d\n", j, algo, -ret);
da7f033d
HX
462 goto out;
463 }
464 }
5f2b424e
CS
465
466 ahash_request_set_crypt(req, sg, result, template[i].psize);
7f397136
GBY
467 ret = crypto_wait_req(crypto_ahash_digest(req), &wait);
468 if (ret) {
469 pr_err("alg: hash: digest failed on chunking test %d for %s: ret=%d\n",
470 j, algo, -ret);
5f2b424e
CS
471 goto out;
472 }
473
474 if (memcmp(result, template[i].digest,
475 crypto_ahash_digestsize(tfm))) {
476 printk(KERN_ERR "alg: hash: Chunking test %d "
477 "failed for %s\n", j, algo);
478 hexdump(result, crypto_ahash_digestsize(tfm));
479 ret = -EINVAL;
018ba95c
WR
480 goto out;
481 }
482 }
483
484 /* partial update exercise */
485 j = 0;
486 for (i = 0; i < tcount; i++) {
487 /* alignment tests are only done with continuous buffers */
488 if (align_offset != 0)
489 break;
490
491 if (template[i].np < 2)
492 continue;
493
494 j++;
e93acd6f 495 memset(result, 0, digest_size);
018ba95c
WR
496
497 ret = -EINVAL;
498 hash_buff = xbuf[0];
499 memcpy(hash_buff, template[i].plaintext,
500 template[i].tap[0]);
501 sg_init_one(&sg[0], hash_buff, template[i].tap[0]);
502
503 if (template[i].ksize) {
504 crypto_ahash_clear_flags(tfm, ~0);
505 if (template[i].ksize > MAX_KEYLEN) {
506 pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
507 j, algo, template[i].ksize, MAX_KEYLEN);
508 ret = -EINVAL;
509 goto out;
510 }
511 memcpy(key, template[i].key, template[i].ksize);
512 ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
513 if (ret) {
514 pr_err("alg: hash: setkey failed on test %d for %s: ret=%d\n",
515 j, algo, -ret);
516 goto out;
517 }
518 }
519
520 ahash_request_set_crypt(req, sg, result, template[i].tap[0]);
7f397136 521 ret = crypto_wait_req(crypto_ahash_init(req), &wait);
018ba95c 522 if (ret) {
cf3f9609 523 pr_err("alg: hash: init failed on test %d for %s: ret=%d\n",
018ba95c
WR
524 j, algo, -ret);
525 goto out;
526 }
7f397136 527 ret = crypto_wait_req(crypto_ahash_update(req), &wait);
018ba95c 528 if (ret) {
cf3f9609 529 pr_err("alg: hash: update failed on test %d for %s: ret=%d\n",
018ba95c
WR
530 j, algo, -ret);
531 goto out;
532 }
533
534 temp = template[i].tap[0];
535 for (k = 1; k < template[i].np; k++) {
536 ret = ahash_partial_update(&req, tfm, &template[i],
537 hash_buff, k, temp, &sg[0], algo, result,
7f397136 538 &wait);
018ba95c 539 if (ret) {
cf3f9609 540 pr_err("alg: hash: partial update failed on test %d for %s: ret=%d\n",
018ba95c
WR
541 j, algo, -ret);
542 goto out_noreq;
543 }
544 temp += template[i].tap[k];
545 }
7f397136 546 ret = crypto_wait_req(crypto_ahash_final(req), &wait);
018ba95c 547 if (ret) {
cf3f9609 548 pr_err("alg: hash: final failed on test %d for %s: ret=%d\n",
018ba95c
WR
549 j, algo, -ret);
550 goto out;
551 }
552 if (memcmp(result, template[i].digest,
553 crypto_ahash_digestsize(tfm))) {
554 pr_err("alg: hash: Partial Test %d failed for %s\n",
555 j, algo);
556 hexdump(result, crypto_ahash_digestsize(tfm));
557 ret = -EINVAL;
5f2b424e
CS
558 goto out;
559 }
da7f033d
HX
560 }
561
562 ret = 0;
563
564out:
565 ahash_request_free(req);
566out_noreq:
f8b0d4d0
HX
567 testmgr_free_buf(xbuf);
568out_nobuf:
29b77e5d
HG
569 kfree(key);
570 kfree(result);
da7f033d
HX
571 return ret;
572}
573
b13b1e0c
EB
574static int test_hash(struct crypto_ahash *tfm,
575 const struct hash_testvec *template,
76715095 576 unsigned int tcount, enum hash_test test_type)
da5ffe11
JK
577{
578 unsigned int alignmask;
579 int ret;
580
76715095 581 ret = __test_hash(tfm, template, tcount, test_type, 0);
da5ffe11
JK
582 if (ret)
583 return ret;
584
585 /* test unaligned buffers, check with one byte offset */
76715095 586 ret = __test_hash(tfm, template, tcount, test_type, 1);
da5ffe11
JK
587 if (ret)
588 return ret;
589
590 alignmask = crypto_tfm_alg_alignmask(&tfm->base);
591 if (alignmask) {
592 /* Check if alignment mask for tfm is correctly set. */
76715095 593 ret = __test_hash(tfm, template, tcount, test_type,
da5ffe11
JK
594 alignmask + 1);
595 if (ret)
596 return ret;
597 }
598
599 return 0;
600}
601
d8a32ac2 602static int __test_aead(struct crypto_aead *tfm, int enc,
b13b1e0c 603 const struct aead_testvec *template, unsigned int tcount,
58dcf548 604 const bool diff_dst, const int align_offset)
da7f033d
HX
605{
606 const char *algo = crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm));
607 unsigned int i, j, k, n, temp;
f8b0d4d0 608 int ret = -ENOMEM;
da7f033d
HX
609 char *q;
610 char *key;
611 struct aead_request *req;
d8a32ac2 612 struct scatterlist *sg;
d8a32ac2
JK
613 struct scatterlist *sgout;
614 const char *e, *d;
7f397136 615 struct crypto_wait wait;
424a5da6 616 unsigned int authsize, iv_len;
9bac019d 617 char *iv;
f8b0d4d0 618 char *xbuf[XBUFSIZE];
d8a32ac2 619 char *xoutbuf[XBUFSIZE];
f8b0d4d0
HX
620 char *axbuf[XBUFSIZE];
621
9bac019d
TS
622 iv = kzalloc(MAX_IVLEN, GFP_KERNEL);
623 if (!iv)
624 return ret;
29b77e5d
HG
625 key = kmalloc(MAX_KEYLEN, GFP_KERNEL);
626 if (!key)
627 goto out_noxbuf;
f8b0d4d0
HX
628 if (testmgr_alloc_buf(xbuf))
629 goto out_noxbuf;
630 if (testmgr_alloc_buf(axbuf))
631 goto out_noaxbuf;
d8a32ac2
JK
632 if (diff_dst && testmgr_alloc_buf(xoutbuf))
633 goto out_nooutbuf;
634
635 /* avoid "the frame size is larger than 1024 bytes" compiler warning */
6da2ec56
KC
636 sg = kmalloc(array3_size(sizeof(*sg), 8, (diff_dst ? 4 : 2)),
637 GFP_KERNEL);
d8a32ac2
JK
638 if (!sg)
639 goto out_nosg;
8a525fcd 640 sgout = &sg[16];
d8a32ac2
JK
641
642 if (diff_dst)
643 d = "-ddst";
644 else
645 d = "";
646
da7f033d
HX
647 if (enc == ENCRYPT)
648 e = "encryption";
649 else
650 e = "decryption";
651
7f397136 652 crypto_init_wait(&wait);
da7f033d
HX
653
654 req = aead_request_alloc(tfm, GFP_KERNEL);
655 if (!req) {
d8a32ac2
JK
656 pr_err("alg: aead%s: Failed to allocate request for %s\n",
657 d, algo);
da7f033d
HX
658 goto out;
659 }
660
661 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
7f397136 662 crypto_req_done, &wait);
da7f033d 663
abfa7f43
JM
664 iv_len = crypto_aead_ivsize(tfm);
665
da7f033d 666 for (i = 0, j = 0; i < tcount; i++) {
a0d608ee
EB
667 const char *input, *expected_output;
668 unsigned int inlen, outlen;
669 char *inbuf, *outbuf, *assocbuf;
670
05b1d338
CS
671 if (template[i].np)
672 continue;
a0d608ee
EB
673 if (enc) {
674 if (template[i].novrfy)
675 continue;
676 input = template[i].ptext;
677 inlen = template[i].plen;
678 expected_output = template[i].ctext;
679 outlen = template[i].clen;
680 } else {
681 input = template[i].ctext;
682 inlen = template[i].clen;
683 expected_output = template[i].ptext;
684 outlen = template[i].plen;
685 }
da7f033d 686
05b1d338 687 j++;
fd57f22a 688
05b1d338
CS
689 /* some templates have no input data but they will
690 * touch input
691 */
a0d608ee
EB
692 inbuf = xbuf[0] + align_offset;
693 assocbuf = axbuf[0];
da7f033d 694
05b1d338 695 ret = -EINVAL;
a0d608ee
EB
696 if (WARN_ON(align_offset + template[i].clen > PAGE_SIZE ||
697 template[i].alen > PAGE_SIZE))
05b1d338 698 goto out;
da7f033d 699
a0d608ee
EB
700 memcpy(inbuf, input, inlen);
701 memcpy(assocbuf, template[i].assoc, template[i].alen);
05b1d338 702 if (template[i].iv)
424a5da6 703 memcpy(iv, template[i].iv, iv_len);
05b1d338 704 else
424a5da6 705 memset(iv, 0, iv_len);
05b1d338
CS
706
707 crypto_aead_clear_flags(tfm, ~0);
708 if (template[i].wk)
709 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
710
711 if (template[i].klen > MAX_KEYLEN) {
712 pr_err("alg: aead%s: setkey failed on test %d for %s: key size %d > %d\n",
713 d, j, algo, template[i].klen,
714 MAX_KEYLEN);
715 ret = -EINVAL;
716 goto out;
717 }
718 memcpy(key, template[i].key, template[i].klen);
da7f033d 719
05b1d338 720 ret = crypto_aead_setkey(tfm, key, template[i].klen);
0fae0c1e 721 if (template[i].fail == !ret) {
05b1d338
CS
722 pr_err("alg: aead%s: setkey failed on test %d for %s: flags=%x\n",
723 d, j, algo, crypto_aead_get_flags(tfm));
724 goto out;
725 } else if (ret)
726 continue;
da7f033d 727
a0d608ee 728 authsize = template[i].clen - template[i].plen;
05b1d338
CS
729 ret = crypto_aead_setauthsize(tfm, authsize);
730 if (ret) {
731 pr_err("alg: aead%s: Failed to set authsize to %u on test %d for %s\n",
732 d, authsize, j, algo);
733 goto out;
734 }
da7f033d 735
8a525fcd
HX
736 k = !!template[i].alen;
737 sg_init_table(sg, k + 1);
a0d608ee
EB
738 sg_set_buf(&sg[0], assocbuf, template[i].alen);
739 sg_set_buf(&sg[k], inbuf, template[i].clen);
740 outbuf = inbuf;
8a525fcd 741
05b1d338 742 if (diff_dst) {
8a525fcd 743 sg_init_table(sgout, k + 1);
a0d608ee 744 sg_set_buf(&sgout[0], assocbuf, template[i].alen);
8a525fcd 745
a0d608ee
EB
746 outbuf = xoutbuf[0] + align_offset;
747 sg_set_buf(&sgout[k], outbuf, template[i].clen);
05b1d338 748 }
d8a32ac2 749
a0d608ee
EB
750 aead_request_set_crypt(req, sg, (diff_dst) ? sgout : sg, inlen,
751 iv);
da7f033d 752
8a525fcd 753 aead_request_set_ad(req, template[i].alen);
da7f033d 754
7f397136
GBY
755 ret = crypto_wait_req(enc ? crypto_aead_encrypt(req)
756 : crypto_aead_decrypt(req), &wait);
da7f033d 757
05b1d338
CS
758 switch (ret) {
759 case 0:
760 if (template[i].novrfy) {
761 /* verification was supposed to fail */
762 pr_err("alg: aead%s: %s failed on test %d for %s: ret was 0, expected -EBADMSG\n",
763 d, e, j, algo);
764 /* so really, we got a bad message */
765 ret = -EBADMSG;
da7f033d
HX
766 goto out;
767 }
05b1d338 768 break;
05b1d338
CS
769 case -EBADMSG:
770 if (template[i].novrfy)
771 /* verification failure was expected */
772 continue;
773 /* fall through */
774 default:
775 pr_err("alg: aead%s: %s failed on test %d for %s: ret=%d\n",
776 d, e, j, algo, -ret);
777 goto out;
778 }
779
a0d608ee 780 if (memcmp(outbuf, expected_output, outlen)) {
05b1d338
CS
781 pr_err("alg: aead%s: Test %d failed on %s for %s\n",
782 d, j, e, algo);
a0d608ee 783 hexdump(outbuf, outlen);
05b1d338
CS
784 ret = -EINVAL;
785 goto out;
da7f033d
HX
786 }
787 }
788
789 for (i = 0, j = 0; i < tcount; i++) {
a0d608ee
EB
790 const char *input, *expected_output;
791 unsigned int inlen, outlen;
792
58dcf548
JK
793 /* alignment tests are only done with continuous buffers */
794 if (align_offset != 0)
795 break;
796
05b1d338
CS
797 if (!template[i].np)
798 continue;
da7f033d 799
a0d608ee
EB
800 if (enc) {
801 if (template[i].novrfy)
802 continue;
803 input = template[i].ptext;
804 inlen = template[i].plen;
805 expected_output = template[i].ctext;
806 outlen = template[i].clen;
807 } else {
808 input = template[i].ctext;
809 inlen = template[i].clen;
810 expected_output = template[i].ptext;
811 outlen = template[i].plen;
812 }
5bc3de58 813
05b1d338 814 j++;
da7f033d 815
05b1d338 816 if (template[i].iv)
abfa7f43 817 memcpy(iv, template[i].iv, iv_len);
05b1d338
CS
818 else
819 memset(iv, 0, MAX_IVLEN);
820
821 crypto_aead_clear_flags(tfm, ~0);
822 if (template[i].wk)
823 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
824 if (template[i].klen > MAX_KEYLEN) {
825 pr_err("alg: aead%s: setkey failed on test %d for %s: key size %d > %d\n",
826 d, j, algo, template[i].klen, MAX_KEYLEN);
827 ret = -EINVAL;
828 goto out;
829 }
830 memcpy(key, template[i].key, template[i].klen);
da7f033d 831
05b1d338 832 ret = crypto_aead_setkey(tfm, key, template[i].klen);
0fae0c1e 833 if (template[i].fail == !ret) {
05b1d338
CS
834 pr_err("alg: aead%s: setkey failed on chunk test %d for %s: flags=%x\n",
835 d, j, algo, crypto_aead_get_flags(tfm));
836 goto out;
837 } else if (ret)
838 continue;
da7f033d 839
a0d608ee 840 authsize = template[i].clen - template[i].plen;
da7f033d 841
05b1d338 842 ret = -EINVAL;
8a525fcd 843 sg_init_table(sg, template[i].anp + template[i].np);
05b1d338 844 if (diff_dst)
8a525fcd
HX
845 sg_init_table(sgout, template[i].anp + template[i].np);
846
847 ret = -EINVAL;
848 for (k = 0, temp = 0; k < template[i].anp; k++) {
849 if (WARN_ON(offset_in_page(IDX[k]) +
850 template[i].atap[k] > PAGE_SIZE))
851 goto out;
852 sg_set_buf(&sg[k],
853 memcpy(axbuf[IDX[k] >> PAGE_SHIFT] +
854 offset_in_page(IDX[k]),
855 template[i].assoc + temp,
856 template[i].atap[k]),
857 template[i].atap[k]);
858 if (diff_dst)
859 sg_set_buf(&sgout[k],
860 axbuf[IDX[k] >> PAGE_SHIFT] +
861 offset_in_page(IDX[k]),
862 template[i].atap[k]);
863 temp += template[i].atap[k];
864 }
865
05b1d338 866 for (k = 0, temp = 0; k < template[i].np; k++) {
a0d608ee
EB
867 n = template[i].tap[k];
868 if (k == template[i].np - 1 && !enc)
869 n += authsize;
870
871 if (WARN_ON(offset_in_page(IDX[k]) + n > PAGE_SIZE))
05b1d338 872 goto out;
da7f033d 873
05b1d338 874 q = xbuf[IDX[k] >> PAGE_SHIFT] + offset_in_page(IDX[k]);
a0d608ee
EB
875 memcpy(q, input + temp, n);
876 sg_set_buf(&sg[template[i].anp + k], q, n);
da7f033d 877
05b1d338
CS
878 if (diff_dst) {
879 q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
880 offset_in_page(IDX[k]);
d8a32ac2 881
a0d608ee 882 memset(q, 0, n);
d8a32ac2 883
a0d608ee 884 sg_set_buf(&sgout[template[i].anp + k], q, n);
05b1d338 885 }
d8a32ac2 886
05b1d338
CS
887 if (k == template[i].np - 1 && enc)
888 n += authsize;
889 if (offset_in_page(q) + n < PAGE_SIZE)
890 q[n] = 0;
d8a32ac2 891
a0d608ee 892 temp += n;
05b1d338 893 }
8ec25c51 894
05b1d338
CS
895 ret = crypto_aead_setauthsize(tfm, authsize);
896 if (ret) {
897 pr_err("alg: aead%s: Failed to set authsize to %u on chunk test %d for %s\n",
898 d, authsize, j, algo);
899 goto out;
900 }
da7f033d 901
05b1d338 902 if (enc) {
8a525fcd
HX
903 if (WARN_ON(sg[template[i].anp + k - 1].offset +
904 sg[template[i].anp + k - 1].length +
905 authsize > PAGE_SIZE)) {
05b1d338 906 ret = -EINVAL;
da7f033d
HX
907 goto out;
908 }
909
05b1d338 910 if (diff_dst)
8a525fcd
HX
911 sgout[template[i].anp + k - 1].length +=
912 authsize;
913 sg[template[i].anp + k - 1].length += authsize;
05b1d338 914 }
da7f033d 915
05b1d338 916 aead_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
a0d608ee 917 inlen, iv);
da7f033d 918
8a525fcd 919 aead_request_set_ad(req, template[i].alen);
da7f033d 920
7f397136
GBY
921 ret = crypto_wait_req(enc ? crypto_aead_encrypt(req)
922 : crypto_aead_decrypt(req), &wait);
da7f033d 923
05b1d338
CS
924 switch (ret) {
925 case 0:
926 if (template[i].novrfy) {
927 /* verification was supposed to fail */
928 pr_err("alg: aead%s: %s failed on chunk test %d for %s: ret was 0, expected -EBADMSG\n",
929 d, e, j, algo);
930 /* so really, we got a bad message */
931 ret = -EBADMSG;
da7f033d
HX
932 goto out;
933 }
05b1d338 934 break;
05b1d338
CS
935 case -EBADMSG:
936 if (template[i].novrfy)
937 /* verification failure was expected */
938 continue;
939 /* fall through */
940 default:
941 pr_err("alg: aead%s: %s failed on chunk test %d for %s: ret=%d\n",
942 d, e, j, algo, -ret);
943 goto out;
944 }
da7f033d 945
05b1d338
CS
946 ret = -EINVAL;
947 for (k = 0, temp = 0; k < template[i].np; k++) {
948 if (diff_dst)
949 q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
950 offset_in_page(IDX[k]);
951 else
952 q = xbuf[IDX[k] >> PAGE_SHIFT] +
953 offset_in_page(IDX[k]);
da7f033d 954
05b1d338 955 n = template[i].tap[k];
a0d608ee
EB
956 if (k == template[i].np - 1 && enc)
957 n += authsize;
da7f033d 958
a0d608ee 959 if (memcmp(q, expected_output + temp, n)) {
05b1d338
CS
960 pr_err("alg: aead%s: Chunk test %d failed on %s at page %u for %s\n",
961 d, j, e, k, algo);
962 hexdump(q, n);
963 goto out;
964 }
da7f033d 965
05b1d338
CS
966 q += n;
967 if (k == template[i].np - 1 && !enc) {
a0d608ee
EB
968 if (!diff_dst && memcmp(q, input + temp + n,
969 authsize))
05b1d338
CS
970 n = authsize;
971 else
972 n = 0;
973 } else {
974 for (n = 0; offset_in_page(q + n) && q[n]; n++)
975 ;
976 }
977 if (n) {
978 pr_err("alg: aead%s: Result buffer corruption in chunk test %d on %s at page %u for %s: %u bytes:\n",
979 d, j, e, k, algo, n);
980 hexdump(q, n);
981 goto out;
da7f033d 982 }
05b1d338
CS
983
984 temp += template[i].tap[k];
da7f033d
HX
985 }
986 }
987
988 ret = 0;
989
990out:
991 aead_request_free(req);
d8a32ac2
JK
992 kfree(sg);
993out_nosg:
994 if (diff_dst)
995 testmgr_free_buf(xoutbuf);
996out_nooutbuf:
f8b0d4d0
HX
997 testmgr_free_buf(axbuf);
998out_noaxbuf:
999 testmgr_free_buf(xbuf);
1000out_noxbuf:
29b77e5d 1001 kfree(key);
9bac019d 1002 kfree(iv);
da7f033d
HX
1003 return ret;
1004}
1005
d8a32ac2 1006static int test_aead(struct crypto_aead *tfm, int enc,
b13b1e0c 1007 const struct aead_testvec *template, unsigned int tcount)
d8a32ac2 1008{
58dcf548 1009 unsigned int alignmask;
d8a32ac2
JK
1010 int ret;
1011
1012 /* test 'dst == src' case */
58dcf548 1013 ret = __test_aead(tfm, enc, template, tcount, false, 0);
d8a32ac2
JK
1014 if (ret)
1015 return ret;
1016
1017 /* test 'dst != src' case */
58dcf548
JK
1018 ret = __test_aead(tfm, enc, template, tcount, true, 0);
1019 if (ret)
1020 return ret;
1021
1022 /* test unaligned buffers, check with one byte offset */
1023 ret = __test_aead(tfm, enc, template, tcount, true, 1);
1024 if (ret)
1025 return ret;
1026
1027 alignmask = crypto_tfm_alg_alignmask(&tfm->base);
1028 if (alignmask) {
1029 /* Check if alignment mask for tfm is correctly set. */
1030 ret = __test_aead(tfm, enc, template, tcount, true,
1031 alignmask + 1);
1032 if (ret)
1033 return ret;
1034 }
1035
1036 return 0;
d8a32ac2
JK
1037}
1038
1aa4ecd9 1039static int test_cipher(struct crypto_cipher *tfm, int enc,
b13b1e0c
EB
1040 const struct cipher_testvec *template,
1041 unsigned int tcount)
1aa4ecd9
HX
1042{
1043 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
1044 unsigned int i, j, k;
1aa4ecd9
HX
1045 char *q;
1046 const char *e;
92a4c9fe 1047 const char *input, *result;
1aa4ecd9 1048 void *data;
f8b0d4d0
HX
1049 char *xbuf[XBUFSIZE];
1050 int ret = -ENOMEM;
1051
1052 if (testmgr_alloc_buf(xbuf))
1053 goto out_nobuf;
1aa4ecd9
HX
1054
1055 if (enc == ENCRYPT)
1056 e = "encryption";
1057 else
1058 e = "decryption";
1059
1060 j = 0;
1061 for (i = 0; i < tcount; i++) {
1062 if (template[i].np)
1063 continue;
1064
10faa8c0
SM
1065 if (fips_enabled && template[i].fips_skip)
1066 continue;
1067
92a4c9fe
EB
1068 input = enc ? template[i].ptext : template[i].ctext;
1069 result = enc ? template[i].ctext : template[i].ptext;
1aa4ecd9
HX
1070 j++;
1071
fd57f22a 1072 ret = -EINVAL;
92a4c9fe 1073 if (WARN_ON(template[i].len > PAGE_SIZE))
fd57f22a
HX
1074 goto out;
1075
1aa4ecd9 1076 data = xbuf[0];
92a4c9fe 1077 memcpy(data, input, template[i].len);
1aa4ecd9
HX
1078
1079 crypto_cipher_clear_flags(tfm, ~0);
1080 if (template[i].wk)
1081 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
1082
1083 ret = crypto_cipher_setkey(tfm, template[i].key,
1084 template[i].klen);
0fae0c1e 1085 if (template[i].fail == !ret) {
1aa4ecd9
HX
1086 printk(KERN_ERR "alg: cipher: setkey failed "
1087 "on test %d for %s: flags=%x\n", j,
1088 algo, crypto_cipher_get_flags(tfm));
1089 goto out;
1090 } else if (ret)
1091 continue;
1092
92a4c9fe 1093 for (k = 0; k < template[i].len;
1aa4ecd9
HX
1094 k += crypto_cipher_blocksize(tfm)) {
1095 if (enc)
1096 crypto_cipher_encrypt_one(tfm, data + k,
1097 data + k);
1098 else
1099 crypto_cipher_decrypt_one(tfm, data + k,
1100 data + k);
1101 }
1102
1103 q = data;
92a4c9fe 1104 if (memcmp(q, result, template[i].len)) {
1aa4ecd9
HX
1105 printk(KERN_ERR "alg: cipher: Test %d failed "
1106 "on %s for %s\n", j, e, algo);
92a4c9fe 1107 hexdump(q, template[i].len);
1aa4ecd9
HX
1108 ret = -EINVAL;
1109 goto out;
1110 }
1111 }
1112
1113 ret = 0;
1114
1115out:
f8b0d4d0
HX
1116 testmgr_free_buf(xbuf);
1117out_nobuf:
1aa4ecd9
HX
1118 return ret;
1119}
1120
12773d93 1121static int __test_skcipher(struct crypto_skcipher *tfm, int enc,
b13b1e0c
EB
1122 const struct cipher_testvec *template,
1123 unsigned int tcount,
3a338f20 1124 const bool diff_dst, const int align_offset)
da7f033d
HX
1125{
1126 const char *algo =
12773d93 1127 crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm));
da7f033d 1128 unsigned int i, j, k, n, temp;
da7f033d 1129 char *q;
12773d93 1130 struct skcipher_request *req;
da7f033d 1131 struct scatterlist sg[8];
08d6af8c
JK
1132 struct scatterlist sgout[8];
1133 const char *e, *d;
7f397136 1134 struct crypto_wait wait;
92a4c9fe 1135 const char *input, *result;
da7f033d
HX
1136 void *data;
1137 char iv[MAX_IVLEN];
f8b0d4d0 1138 char *xbuf[XBUFSIZE];
08d6af8c 1139 char *xoutbuf[XBUFSIZE];
f8b0d4d0 1140 int ret = -ENOMEM;
84cba178 1141 unsigned int ivsize = crypto_skcipher_ivsize(tfm);
f8b0d4d0
HX
1142
1143 if (testmgr_alloc_buf(xbuf))
1144 goto out_nobuf;
da7f033d 1145
08d6af8c
JK
1146 if (diff_dst && testmgr_alloc_buf(xoutbuf))
1147 goto out_nooutbuf;
1148
1149 if (diff_dst)
1150 d = "-ddst";
1151 else
1152 d = "";
1153
da7f033d
HX
1154 if (enc == ENCRYPT)
1155 e = "encryption";
1156 else
1157 e = "decryption";
1158
7f397136 1159 crypto_init_wait(&wait);
da7f033d 1160
12773d93 1161 req = skcipher_request_alloc(tfm, GFP_KERNEL);
da7f033d 1162 if (!req) {
08d6af8c
JK
1163 pr_err("alg: skcipher%s: Failed to allocate request for %s\n",
1164 d, algo);
da7f033d
HX
1165 goto out;
1166 }
1167
12773d93 1168 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
7f397136 1169 crypto_req_done, &wait);
da7f033d
HX
1170
1171 j = 0;
1172 for (i = 0; i < tcount; i++) {
bbb9a7dd
CS
1173 if (template[i].np && !template[i].also_non_np)
1174 continue;
1175
10faa8c0
SM
1176 if (fips_enabled && template[i].fips_skip)
1177 continue;
1178
92a4c9fe 1179 if (template[i].iv && !(template[i].generates_iv && enc))
84cba178 1180 memcpy(iv, template[i].iv, ivsize);
da7f033d
HX
1181 else
1182 memset(iv, 0, MAX_IVLEN);
1183
92a4c9fe
EB
1184 input = enc ? template[i].ptext : template[i].ctext;
1185 result = enc ? template[i].ctext : template[i].ptext;
a1aa44a2 1186 j++;
a1aa44a2 1187 ret = -EINVAL;
92a4c9fe 1188 if (WARN_ON(align_offset + template[i].len > PAGE_SIZE))
a1aa44a2 1189 goto out;
da7f033d 1190
a1aa44a2
CS
1191 data = xbuf[0];
1192 data += align_offset;
92a4c9fe 1193 memcpy(data, input, template[i].len);
a1aa44a2 1194
12773d93 1195 crypto_skcipher_clear_flags(tfm, ~0);
a1aa44a2 1196 if (template[i].wk)
12773d93
HX
1197 crypto_skcipher_set_flags(tfm,
1198 CRYPTO_TFM_REQ_WEAK_KEY);
da7f033d 1199
12773d93
HX
1200 ret = crypto_skcipher_setkey(tfm, template[i].key,
1201 template[i].klen);
0fae0c1e 1202 if (template[i].fail == !ret) {
a1aa44a2 1203 pr_err("alg: skcipher%s: setkey failed on test %d for %s: flags=%x\n",
12773d93 1204 d, j, algo, crypto_skcipher_get_flags(tfm));
a1aa44a2
CS
1205 goto out;
1206 } else if (ret)
1207 continue;
1208
92a4c9fe 1209 sg_init_one(&sg[0], data, template[i].len);
a1aa44a2
CS
1210 if (diff_dst) {
1211 data = xoutbuf[0];
1212 data += align_offset;
92a4c9fe 1213 sg_init_one(&sgout[0], data, template[i].len);
a1aa44a2 1214 }
da7f033d 1215
12773d93 1216 skcipher_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
92a4c9fe 1217 template[i].len, iv);
7f397136
GBY
1218 ret = crypto_wait_req(enc ? crypto_skcipher_encrypt(req) :
1219 crypto_skcipher_decrypt(req), &wait);
a1aa44a2 1220
7f397136 1221 if (ret) {
a1aa44a2
CS
1222 pr_err("alg: skcipher%s: %s failed on test %d for %s: ret=%d\n",
1223 d, e, j, algo, -ret);
1224 goto out;
1225 }
da7f033d 1226
a1aa44a2 1227 q = data;
92a4c9fe 1228 if (memcmp(q, result, template[i].len)) {
8a826a34 1229 pr_err("alg: skcipher%s: Test %d failed (invalid result) on %s for %s\n",
a1aa44a2 1230 d, j, e, algo);
92a4c9fe 1231 hexdump(q, template[i].len);
a1aa44a2
CS
1232 ret = -EINVAL;
1233 goto out;
da7f033d 1234 }
8a826a34 1235
92a4c9fe
EB
1236 if (template[i].generates_iv && enc &&
1237 memcmp(iv, template[i].iv, crypto_skcipher_ivsize(tfm))) {
8a826a34
BB
1238 pr_err("alg: skcipher%s: Test %d failed (invalid output IV) on %s for %s\n",
1239 d, j, e, algo);
1240 hexdump(iv, crypto_skcipher_ivsize(tfm));
1241 ret = -EINVAL;
1242 goto out;
1243 }
da7f033d
HX
1244 }
1245
1246 j = 0;
1247 for (i = 0; i < tcount; i++) {
3a338f20
JK
1248 /* alignment tests are only done with continuous buffers */
1249 if (align_offset != 0)
1250 break;
da7f033d 1251
bbb9a7dd
CS
1252 if (!template[i].np)
1253 continue;
1254
10faa8c0
SM
1255 if (fips_enabled && template[i].fips_skip)
1256 continue;
1257
92a4c9fe 1258 if (template[i].iv && !(template[i].generates_iv && enc))
84cba178 1259 memcpy(iv, template[i].iv, ivsize);
da7f033d
HX
1260 else
1261 memset(iv, 0, MAX_IVLEN);
1262
92a4c9fe
EB
1263 input = enc ? template[i].ptext : template[i].ctext;
1264 result = enc ? template[i].ctext : template[i].ptext;
a1aa44a2 1265 j++;
12773d93 1266 crypto_skcipher_clear_flags(tfm, ~0);
a1aa44a2 1267 if (template[i].wk)
12773d93
HX
1268 crypto_skcipher_set_flags(tfm,
1269 CRYPTO_TFM_REQ_WEAK_KEY);
da7f033d 1270
12773d93
HX
1271 ret = crypto_skcipher_setkey(tfm, template[i].key,
1272 template[i].klen);
0fae0c1e 1273 if (template[i].fail == !ret) {
a1aa44a2 1274 pr_err("alg: skcipher%s: setkey failed on chunk test %d for %s: flags=%x\n",
12773d93 1275 d, j, algo, crypto_skcipher_get_flags(tfm));
a1aa44a2
CS
1276 goto out;
1277 } else if (ret)
1278 continue;
da7f033d 1279
a1aa44a2
CS
1280 temp = 0;
1281 ret = -EINVAL;
1282 sg_init_table(sg, template[i].np);
1283 if (diff_dst)
1284 sg_init_table(sgout, template[i].np);
1285 for (k = 0; k < template[i].np; k++) {
1286 if (WARN_ON(offset_in_page(IDX[k]) +
1287 template[i].tap[k] > PAGE_SIZE))
da7f033d 1288 goto out;
da7f033d 1289
a1aa44a2 1290 q = xbuf[IDX[k] >> PAGE_SHIFT] + offset_in_page(IDX[k]);
da7f033d 1291
92a4c9fe 1292 memcpy(q, input + temp, template[i].tap[k]);
a1aa44a2
CS
1293
1294 if (offset_in_page(q) + template[i].tap[k] < PAGE_SIZE)
1295 q[template[i].tap[k]] = 0;
1296
1297 sg_set_buf(&sg[k], q, template[i].tap[k]);
1298 if (diff_dst) {
1299 q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
da7f033d
HX
1300 offset_in_page(IDX[k]);
1301
a1aa44a2 1302 sg_set_buf(&sgout[k], q, template[i].tap[k]);
da7f033d 1303
a1aa44a2
CS
1304 memset(q, 0, template[i].tap[k]);
1305 if (offset_in_page(q) +
1306 template[i].tap[k] < PAGE_SIZE)
da7f033d 1307 q[template[i].tap[k]] = 0;
a1aa44a2 1308 }
da7f033d 1309
a1aa44a2
CS
1310 temp += template[i].tap[k];
1311 }
08d6af8c 1312
12773d93 1313 skcipher_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
92a4c9fe 1314 template[i].len, iv);
08d6af8c 1315
7f397136
GBY
1316 ret = crypto_wait_req(enc ? crypto_skcipher_encrypt(req) :
1317 crypto_skcipher_decrypt(req), &wait);
da7f033d 1318
7f397136 1319 if (ret) {
a1aa44a2
CS
1320 pr_err("alg: skcipher%s: %s failed on chunk test %d for %s: ret=%d\n",
1321 d, e, j, algo, -ret);
1322 goto out;
1323 }
da7f033d 1324
a1aa44a2
CS
1325 temp = 0;
1326 ret = -EINVAL;
1327 for (k = 0; k < template[i].np; k++) {
1328 if (diff_dst)
1329 q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
1330 offset_in_page(IDX[k]);
1331 else
1332 q = xbuf[IDX[k] >> PAGE_SHIFT] +
1333 offset_in_page(IDX[k]);
da7f033d 1334
92a4c9fe 1335 if (memcmp(q, result + temp, template[i].tap[k])) {
a1aa44a2
CS
1336 pr_err("alg: skcipher%s: Chunk test %d failed on %s at page %u for %s\n",
1337 d, j, e, k, algo);
1338 hexdump(q, template[i].tap[k]);
da7f033d
HX
1339 goto out;
1340 }
1341
a1aa44a2
CS
1342 q += template[i].tap[k];
1343 for (n = 0; offset_in_page(q + n) && q[n]; n++)
1344 ;
1345 if (n) {
1346 pr_err("alg: skcipher%s: Result buffer corruption in chunk test %d on %s at page %u for %s: %u bytes:\n",
1347 d, j, e, k, algo, n);
1348 hexdump(q, n);
1349 goto out;
da7f033d 1350 }
a1aa44a2 1351 temp += template[i].tap[k];
da7f033d
HX
1352 }
1353 }
1354
1355 ret = 0;
1356
1357out:
12773d93 1358 skcipher_request_free(req);
08d6af8c
JK
1359 if (diff_dst)
1360 testmgr_free_buf(xoutbuf);
1361out_nooutbuf:
f8b0d4d0
HX
1362 testmgr_free_buf(xbuf);
1363out_nobuf:
da7f033d
HX
1364 return ret;
1365}
1366
12773d93 1367static int test_skcipher(struct crypto_skcipher *tfm, int enc,
b13b1e0c
EB
1368 const struct cipher_testvec *template,
1369 unsigned int tcount)
08d6af8c 1370{
3a338f20 1371 unsigned int alignmask;
08d6af8c
JK
1372 int ret;
1373
1374 /* test 'dst == src' case */
3a338f20 1375 ret = __test_skcipher(tfm, enc, template, tcount, false, 0);
08d6af8c
JK
1376 if (ret)
1377 return ret;
1378
1379 /* test 'dst != src' case */
3a338f20
JK
1380 ret = __test_skcipher(tfm, enc, template, tcount, true, 0);
1381 if (ret)
1382 return ret;
1383
1384 /* test unaligned buffers, check with one byte offset */
1385 ret = __test_skcipher(tfm, enc, template, tcount, true, 1);
1386 if (ret)
1387 return ret;
1388
1389 alignmask = crypto_tfm_alg_alignmask(&tfm->base);
1390 if (alignmask) {
1391 /* Check if alignment mask for tfm is correctly set. */
1392 ret = __test_skcipher(tfm, enc, template, tcount, true,
1393 alignmask + 1);
1394 if (ret)
1395 return ret;
1396 }
1397
1398 return 0;
08d6af8c
JK
1399}
1400
b13b1e0c
EB
1401static int test_comp(struct crypto_comp *tfm,
1402 const struct comp_testvec *ctemplate,
1403 const struct comp_testvec *dtemplate,
1404 int ctcount, int dtcount)
da7f033d
HX
1405{
1406 const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
33607384 1407 char *output, *decomp_output;
da7f033d 1408 unsigned int i;
da7f033d
HX
1409 int ret;
1410
33607384
MC
1411 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1412 if (!output)
1413 return -ENOMEM;
1414
1415 decomp_output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1416 if (!decomp_output) {
1417 kfree(output);
1418 return -ENOMEM;
1419 }
1420
da7f033d 1421 for (i = 0; i < ctcount; i++) {
c79cf910
GU
1422 int ilen;
1423 unsigned int dlen = COMP_BUF_SIZE;
da7f033d 1424
22a8118d
MS
1425 memset(output, 0, COMP_BUF_SIZE);
1426 memset(decomp_output, 0, COMP_BUF_SIZE);
da7f033d
HX
1427
1428 ilen = ctemplate[i].inlen;
1429 ret = crypto_comp_compress(tfm, ctemplate[i].input,
33607384 1430 ilen, output, &dlen);
da7f033d
HX
1431 if (ret) {
1432 printk(KERN_ERR "alg: comp: compression failed "
1433 "on test %d for %s: ret=%d\n", i + 1, algo,
1434 -ret);
1435 goto out;
1436 }
1437
33607384
MC
1438 ilen = dlen;
1439 dlen = COMP_BUF_SIZE;
1440 ret = crypto_comp_decompress(tfm, output,
1441 ilen, decomp_output, &dlen);
1442 if (ret) {
1443 pr_err("alg: comp: compression failed: decompress: on test %d for %s failed: ret=%d\n",
1444 i + 1, algo, -ret);
1445 goto out;
1446 }
1447
1448 if (dlen != ctemplate[i].inlen) {
b812eb00
GU
1449 printk(KERN_ERR "alg: comp: Compression test %d "
1450 "failed for %s: output len = %d\n", i + 1, algo,
1451 dlen);
1452 ret = -EINVAL;
1453 goto out;
1454 }
1455
33607384
MC
1456 if (memcmp(decomp_output, ctemplate[i].input,
1457 ctemplate[i].inlen)) {
1458 pr_err("alg: comp: compression failed: output differs: on test %d for %s\n",
1459 i + 1, algo);
1460 hexdump(decomp_output, dlen);
da7f033d
HX
1461 ret = -EINVAL;
1462 goto out;
1463 }
1464 }
1465
1466 for (i = 0; i < dtcount; i++) {
c79cf910
GU
1467 int ilen;
1468 unsigned int dlen = COMP_BUF_SIZE;
da7f033d 1469
22a8118d 1470 memset(decomp_output, 0, COMP_BUF_SIZE);
da7f033d
HX
1471
1472 ilen = dtemplate[i].inlen;
1473 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
33607384 1474 ilen, decomp_output, &dlen);
da7f033d
HX
1475 if (ret) {
1476 printk(KERN_ERR "alg: comp: decompression failed "
1477 "on test %d for %s: ret=%d\n", i + 1, algo,
1478 -ret);
1479 goto out;
1480 }
1481
b812eb00
GU
1482 if (dlen != dtemplate[i].outlen) {
1483 printk(KERN_ERR "alg: comp: Decompression test %d "
1484 "failed for %s: output len = %d\n", i + 1, algo,
1485 dlen);
1486 ret = -EINVAL;
1487 goto out;
1488 }
1489
33607384 1490 if (memcmp(decomp_output, dtemplate[i].output, dlen)) {
da7f033d
HX
1491 printk(KERN_ERR "alg: comp: Decompression test %d "
1492 "failed for %s\n", i + 1, algo);
33607384 1493 hexdump(decomp_output, dlen);
da7f033d
HX
1494 ret = -EINVAL;
1495 goto out;
1496 }
1497 }
1498
1499 ret = 0;
1500
1501out:
33607384
MC
1502 kfree(decomp_output);
1503 kfree(output);
da7f033d
HX
1504 return ret;
1505}
1506
b13b1e0c 1507static int test_acomp(struct crypto_acomp *tfm,
33607384 1508 const struct comp_testvec *ctemplate,
b13b1e0c
EB
1509 const struct comp_testvec *dtemplate,
1510 int ctcount, int dtcount)
d7db7a88
GC
1511{
1512 const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
1513 unsigned int i;
a9943a0a 1514 char *output, *decomp_out;
d7db7a88
GC
1515 int ret;
1516 struct scatterlist src, dst;
1517 struct acomp_req *req;
7f397136 1518 struct crypto_wait wait;
d7db7a88 1519
eb095593
EB
1520 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1521 if (!output)
1522 return -ENOMEM;
1523
a9943a0a
GC
1524 decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1525 if (!decomp_out) {
1526 kfree(output);
1527 return -ENOMEM;
1528 }
1529
d7db7a88
GC
1530 for (i = 0; i < ctcount; i++) {
1531 unsigned int dlen = COMP_BUF_SIZE;
1532 int ilen = ctemplate[i].inlen;
02608e02 1533 void *input_vec;
d7db7a88 1534
d2110224 1535 input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL);
02608e02
LA
1536 if (!input_vec) {
1537 ret = -ENOMEM;
1538 goto out;
1539 }
1540
eb095593 1541 memset(output, 0, dlen);
7f397136 1542 crypto_init_wait(&wait);
02608e02 1543 sg_init_one(&src, input_vec, ilen);
d7db7a88
GC
1544 sg_init_one(&dst, output, dlen);
1545
1546 req = acomp_request_alloc(tfm);
1547 if (!req) {
1548 pr_err("alg: acomp: request alloc failed for %s\n",
1549 algo);
02608e02 1550 kfree(input_vec);
d7db7a88
GC
1551 ret = -ENOMEM;
1552 goto out;
1553 }
1554
1555 acomp_request_set_params(req, &src, &dst, ilen, dlen);
1556 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
7f397136 1557 crypto_req_done, &wait);
d7db7a88 1558
7f397136 1559 ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
d7db7a88
GC
1560 if (ret) {
1561 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
1562 i + 1, algo, -ret);
02608e02 1563 kfree(input_vec);
d7db7a88
GC
1564 acomp_request_free(req);
1565 goto out;
1566 }
1567
a9943a0a
GC
1568 ilen = req->dlen;
1569 dlen = COMP_BUF_SIZE;
1570 sg_init_one(&src, output, ilen);
1571 sg_init_one(&dst, decomp_out, dlen);
7f397136 1572 crypto_init_wait(&wait);
a9943a0a
GC
1573 acomp_request_set_params(req, &src, &dst, ilen, dlen);
1574
7f397136 1575 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
a9943a0a
GC
1576 if (ret) {
1577 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
1578 i + 1, algo, -ret);
1579 kfree(input_vec);
1580 acomp_request_free(req);
1581 goto out;
1582 }
1583
1584 if (req->dlen != ctemplate[i].inlen) {
d7db7a88
GC
1585 pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
1586 i + 1, algo, req->dlen);
1587 ret = -EINVAL;
02608e02 1588 kfree(input_vec);
d7db7a88
GC
1589 acomp_request_free(req);
1590 goto out;
1591 }
1592
a9943a0a 1593 if (memcmp(input_vec, decomp_out, req->dlen)) {
d7db7a88
GC
1594 pr_err("alg: acomp: Compression test %d failed for %s\n",
1595 i + 1, algo);
1596 hexdump(output, req->dlen);
1597 ret = -EINVAL;
02608e02 1598 kfree(input_vec);
d7db7a88
GC
1599 acomp_request_free(req);
1600 goto out;
1601 }
1602
02608e02 1603 kfree(input_vec);
d7db7a88
GC
1604 acomp_request_free(req);
1605 }
1606
1607 for (i = 0; i < dtcount; i++) {
1608 unsigned int dlen = COMP_BUF_SIZE;
1609 int ilen = dtemplate[i].inlen;
02608e02
LA
1610 void *input_vec;
1611
d2110224 1612 input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL);
02608e02
LA
1613 if (!input_vec) {
1614 ret = -ENOMEM;
1615 goto out;
1616 }
d7db7a88 1617
eb095593 1618 memset(output, 0, dlen);
7f397136 1619 crypto_init_wait(&wait);
02608e02 1620 sg_init_one(&src, input_vec, ilen);
d7db7a88
GC
1621 sg_init_one(&dst, output, dlen);
1622
1623 req = acomp_request_alloc(tfm);
1624 if (!req) {
1625 pr_err("alg: acomp: request alloc failed for %s\n",
1626 algo);
02608e02 1627 kfree(input_vec);
d7db7a88
GC
1628 ret = -ENOMEM;
1629 goto out;
1630 }
1631
1632 acomp_request_set_params(req, &src, &dst, ilen, dlen);
1633 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
7f397136 1634 crypto_req_done, &wait);
d7db7a88 1635
7f397136 1636 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
d7db7a88
GC
1637 if (ret) {
1638 pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
1639 i + 1, algo, -ret);
02608e02 1640 kfree(input_vec);
d7db7a88
GC
1641 acomp_request_free(req);
1642 goto out;
1643 }
1644
1645 if (req->dlen != dtemplate[i].outlen) {
1646 pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
1647 i + 1, algo, req->dlen);
1648 ret = -EINVAL;
02608e02 1649 kfree(input_vec);
d7db7a88
GC
1650 acomp_request_free(req);
1651 goto out;
1652 }
1653
1654 if (memcmp(output, dtemplate[i].output, req->dlen)) {
1655 pr_err("alg: acomp: Decompression test %d failed for %s\n",
1656 i + 1, algo);
1657 hexdump(output, req->dlen);
1658 ret = -EINVAL;
02608e02 1659 kfree(input_vec);
d7db7a88
GC
1660 acomp_request_free(req);
1661 goto out;
1662 }
1663
02608e02 1664 kfree(input_vec);
d7db7a88
GC
1665 acomp_request_free(req);
1666 }
1667
1668 ret = 0;
1669
1670out:
a9943a0a 1671 kfree(decomp_out);
eb095593 1672 kfree(output);
d7db7a88
GC
1673 return ret;
1674}
1675
b13b1e0c
EB
1676static int test_cprng(struct crypto_rng *tfm,
1677 const struct cprng_testvec *template,
7647d6ce
JW
1678 unsigned int tcount)
1679{
1680 const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
fa4ef8a6 1681 int err = 0, i, j, seedsize;
7647d6ce
JW
1682 u8 *seed;
1683 char result[32];
1684
1685 seedsize = crypto_rng_seedsize(tfm);
1686
1687 seed = kmalloc(seedsize, GFP_KERNEL);
1688 if (!seed) {
1689 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
1690 "for %s\n", algo);
1691 return -ENOMEM;
1692 }
1693
1694 for (i = 0; i < tcount; i++) {
1695 memset(result, 0, 32);
1696
1697 memcpy(seed, template[i].v, template[i].vlen);
1698 memcpy(seed + template[i].vlen, template[i].key,
1699 template[i].klen);
1700 memcpy(seed + template[i].vlen + template[i].klen,
1701 template[i].dt, template[i].dtlen);
1702
1703 err = crypto_rng_reset(tfm, seed, seedsize);
1704 if (err) {
1705 printk(KERN_ERR "alg: cprng: Failed to reset rng "
1706 "for %s\n", algo);
1707 goto out;
1708 }
1709
1710 for (j = 0; j < template[i].loops; j++) {
1711 err = crypto_rng_get_bytes(tfm, result,
1712 template[i].rlen);
19e60e13 1713 if (err < 0) {
7647d6ce
JW
1714 printk(KERN_ERR "alg: cprng: Failed to obtain "
1715 "the correct amount of random data for "
19e60e13
SM
1716 "%s (requested %d)\n", algo,
1717 template[i].rlen);
7647d6ce
JW
1718 goto out;
1719 }
1720 }
1721
1722 err = memcmp(result, template[i].result,
1723 template[i].rlen);
1724 if (err) {
1725 printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
1726 i, algo);
1727 hexdump(result, template[i].rlen);
1728 err = -EINVAL;
1729 goto out;
1730 }
1731 }
1732
1733out:
1734 kfree(seed);
1735 return err;
1736}
1737
da7f033d
HX
1738static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
1739 u32 type, u32 mask)
1740{
a0d608ee 1741 const struct aead_test_suite *suite = &desc->suite.aead;
da7f033d 1742 struct crypto_aead *tfm;
a0d608ee 1743 int err;
da7f033d 1744
eed93e0c 1745 tfm = crypto_alloc_aead(driver, type, mask);
da7f033d
HX
1746 if (IS_ERR(tfm)) {
1747 printk(KERN_ERR "alg: aead: Failed to load transform for %s: "
1748 "%ld\n", driver, PTR_ERR(tfm));
1749 return PTR_ERR(tfm);
1750 }
1751
a0d608ee
EB
1752 err = test_aead(tfm, ENCRYPT, suite->vecs, suite->count);
1753 if (!err)
1754 err = test_aead(tfm, DECRYPT, suite->vecs, suite->count);
da7f033d 1755
da7f033d
HX
1756 crypto_free_aead(tfm);
1757 return err;
1758}
1759
1760static int alg_test_cipher(const struct alg_test_desc *desc,
1761 const char *driver, u32 type, u32 mask)
1762{
92a4c9fe 1763 const struct cipher_test_suite *suite = &desc->suite.cipher;
1aa4ecd9 1764 struct crypto_cipher *tfm;
92a4c9fe 1765 int err;
da7f033d 1766
eed93e0c 1767 tfm = crypto_alloc_cipher(driver, type, mask);
da7f033d
HX
1768 if (IS_ERR(tfm)) {
1769 printk(KERN_ERR "alg: cipher: Failed to load transform for "
1770 "%s: %ld\n", driver, PTR_ERR(tfm));
1771 return PTR_ERR(tfm);
1772 }
1773
92a4c9fe
EB
1774 err = test_cipher(tfm, ENCRYPT, suite->vecs, suite->count);
1775 if (!err)
1776 err = test_cipher(tfm, DECRYPT, suite->vecs, suite->count);
da7f033d 1777
1aa4ecd9
HX
1778 crypto_free_cipher(tfm);
1779 return err;
1780}
1781
1782static int alg_test_skcipher(const struct alg_test_desc *desc,
1783 const char *driver, u32 type, u32 mask)
1784{
92a4c9fe 1785 const struct cipher_test_suite *suite = &desc->suite.cipher;
12773d93 1786 struct crypto_skcipher *tfm;
92a4c9fe 1787 int err;
1aa4ecd9 1788
eed93e0c 1789 tfm = crypto_alloc_skcipher(driver, type, mask);
1aa4ecd9
HX
1790 if (IS_ERR(tfm)) {
1791 printk(KERN_ERR "alg: skcipher: Failed to load transform for "
1792 "%s: %ld\n", driver, PTR_ERR(tfm));
1793 return PTR_ERR(tfm);
1794 }
1795
92a4c9fe
EB
1796 err = test_skcipher(tfm, ENCRYPT, suite->vecs, suite->count);
1797 if (!err)
1798 err = test_skcipher(tfm, DECRYPT, suite->vecs, suite->count);
1aa4ecd9 1799
12773d93 1800 crypto_free_skcipher(tfm);
da7f033d
HX
1801 return err;
1802}
1803
1804static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
1805 u32 type, u32 mask)
1806{
d7db7a88
GC
1807 struct crypto_comp *comp;
1808 struct crypto_acomp *acomp;
da7f033d 1809 int err;
d7db7a88
GC
1810 u32 algo_type = type & CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
1811
1812 if (algo_type == CRYPTO_ALG_TYPE_ACOMPRESS) {
1813 acomp = crypto_alloc_acomp(driver, type, mask);
1814 if (IS_ERR(acomp)) {
1815 pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
1816 driver, PTR_ERR(acomp));
1817 return PTR_ERR(acomp);
1818 }
1819 err = test_acomp(acomp, desc->suite.comp.comp.vecs,
1820 desc->suite.comp.decomp.vecs,
1821 desc->suite.comp.comp.count,
1822 desc->suite.comp.decomp.count);
1823 crypto_free_acomp(acomp);
1824 } else {
1825 comp = crypto_alloc_comp(driver, type, mask);
1826 if (IS_ERR(comp)) {
1827 pr_err("alg: comp: Failed to load transform for %s: %ld\n",
1828 driver, PTR_ERR(comp));
1829 return PTR_ERR(comp);
1830 }
da7f033d 1831
d7db7a88
GC
1832 err = test_comp(comp, desc->suite.comp.comp.vecs,
1833 desc->suite.comp.decomp.vecs,
1834 desc->suite.comp.comp.count,
1835 desc->suite.comp.decomp.count);
da7f033d 1836
d7db7a88
GC
1837 crypto_free_comp(comp);
1838 }
da7f033d
HX
1839 return err;
1840}
1841
9b3abc01
EB
1842static int __alg_test_hash(const struct hash_testvec *template,
1843 unsigned int tcount, const char *driver,
1844 u32 type, u32 mask)
da7f033d
HX
1845{
1846 struct crypto_ahash *tfm;
1847 int err;
1848
eed93e0c 1849 tfm = crypto_alloc_ahash(driver, type, mask);
da7f033d
HX
1850 if (IS_ERR(tfm)) {
1851 printk(KERN_ERR "alg: hash: Failed to load transform for %s: "
1852 "%ld\n", driver, PTR_ERR(tfm));
1853 return PTR_ERR(tfm);
1854 }
1855
76715095
GBY
1856 err = test_hash(tfm, template, tcount, HASH_TEST_DIGEST);
1857 if (!err)
1858 err = test_hash(tfm, template, tcount, HASH_TEST_FINAL);
a8f1a052 1859 if (!err)
76715095 1860 err = test_hash(tfm, template, tcount, HASH_TEST_FINUP);
da7f033d
HX
1861 crypto_free_ahash(tfm);
1862 return err;
1863}
1864
9b3abc01
EB
1865static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1866 u32 type, u32 mask)
1867{
1868 const struct hash_testvec *template = desc->suite.hash.vecs;
1869 unsigned int tcount = desc->suite.hash.count;
1870 unsigned int nr_unkeyed, nr_keyed;
1871 int err;
1872
1873 /*
1874 * For OPTIONAL_KEY algorithms, we have to do all the unkeyed tests
1875 * first, before setting a key on the tfm. To make this easier, we
1876 * require that the unkeyed test vectors (if any) are listed first.
1877 */
1878
1879 for (nr_unkeyed = 0; nr_unkeyed < tcount; nr_unkeyed++) {
1880 if (template[nr_unkeyed].ksize)
1881 break;
1882 }
1883 for (nr_keyed = 0; nr_unkeyed + nr_keyed < tcount; nr_keyed++) {
1884 if (!template[nr_unkeyed + nr_keyed].ksize) {
1885 pr_err("alg: hash: test vectors for %s out of order, "
1886 "unkeyed ones must come first\n", desc->alg);
1887 return -EINVAL;
1888 }
1889 }
1890
1891 err = 0;
1892 if (nr_unkeyed) {
1893 err = __alg_test_hash(template, nr_unkeyed, driver, type, mask);
1894 template += nr_unkeyed;
1895 }
1896
1897 if (!err && nr_keyed)
1898 err = __alg_test_hash(template, nr_keyed, driver, type, mask);
1899
1900 return err;
1901}
1902
8e3ee85e
HX
1903static int alg_test_crc32c(const struct alg_test_desc *desc,
1904 const char *driver, u32 type, u32 mask)
1905{
1906 struct crypto_shash *tfm;
cb9dde88 1907 __le32 val;
8e3ee85e
HX
1908 int err;
1909
1910 err = alg_test_hash(desc, driver, type, mask);
1911 if (err)
1912 goto out;
1913
eed93e0c 1914 tfm = crypto_alloc_shash(driver, type, mask);
8e3ee85e
HX
1915 if (IS_ERR(tfm)) {
1916 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
1917 "%ld\n", driver, PTR_ERR(tfm));
1918 err = PTR_ERR(tfm);
1919 goto out;
1920 }
1921
1922 do {
4c5c3024
JSM
1923 SHASH_DESC_ON_STACK(shash, tfm);
1924 u32 *ctx = (u32 *)shash_desc_ctx(shash);
8e3ee85e 1925
4c5c3024
JSM
1926 shash->tfm = tfm;
1927 shash->flags = 0;
8e3ee85e 1928
cb9dde88 1929 *ctx = 420553207;
4c5c3024 1930 err = crypto_shash_final(shash, (u8 *)&val);
8e3ee85e
HX
1931 if (err) {
1932 printk(KERN_ERR "alg: crc32c: Operation failed for "
1933 "%s: %d\n", driver, err);
1934 break;
1935 }
1936
cb9dde88
EB
1937 if (val != cpu_to_le32(~420553207)) {
1938 pr_err("alg: crc32c: Test failed for %s: %u\n",
1939 driver, le32_to_cpu(val));
8e3ee85e
HX
1940 err = -EINVAL;
1941 }
1942 } while (0);
1943
1944 crypto_free_shash(tfm);
1945
1946out:
1947 return err;
1948}
1949
7647d6ce
JW
1950static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
1951 u32 type, u32 mask)
1952{
1953 struct crypto_rng *rng;
1954 int err;
1955
eed93e0c 1956 rng = crypto_alloc_rng(driver, type, mask);
7647d6ce
JW
1957 if (IS_ERR(rng)) {
1958 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
1959 "%ld\n", driver, PTR_ERR(rng));
1960 return PTR_ERR(rng);
1961 }
1962
1963 err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
1964
1965 crypto_free_rng(rng);
1966
1967 return err;
1968}
1969
64d1cdfb 1970
b13b1e0c 1971static int drbg_cavs_test(const struct drbg_testvec *test, int pr,
64d1cdfb
SM
1972 const char *driver, u32 type, u32 mask)
1973{
1974 int ret = -EAGAIN;
1975 struct crypto_rng *drng;
1976 struct drbg_test_data test_data;
1977 struct drbg_string addtl, pers, testentropy;
1978 unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
1979
1980 if (!buf)
1981 return -ENOMEM;
1982
eed93e0c 1983 drng = crypto_alloc_rng(driver, type, mask);
64d1cdfb 1984 if (IS_ERR(drng)) {
2fc0d258 1985 printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
64d1cdfb
SM
1986 "%s\n", driver);
1987 kzfree(buf);
1988 return -ENOMEM;
1989 }
1990
1991 test_data.testentropy = &testentropy;
1992 drbg_string_fill(&testentropy, test->entropy, test->entropylen);
1993 drbg_string_fill(&pers, test->pers, test->perslen);
1994 ret = crypto_drbg_reset_test(drng, &pers, &test_data);
1995 if (ret) {
1996 printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
1997 goto outbuf;
1998 }
1999
2000 drbg_string_fill(&addtl, test->addtla, test->addtllen);
2001 if (pr) {
2002 drbg_string_fill(&testentropy, test->entpra, test->entprlen);
2003 ret = crypto_drbg_get_bytes_addtl_test(drng,
2004 buf, test->expectedlen, &addtl, &test_data);
2005 } else {
2006 ret = crypto_drbg_get_bytes_addtl(drng,
2007 buf, test->expectedlen, &addtl);
2008 }
19e60e13 2009 if (ret < 0) {
2fc0d258 2010 printk(KERN_ERR "alg: drbg: could not obtain random data for "
64d1cdfb
SM
2011 "driver %s\n", driver);
2012 goto outbuf;
2013 }
2014
2015 drbg_string_fill(&addtl, test->addtlb, test->addtllen);
2016 if (pr) {
2017 drbg_string_fill(&testentropy, test->entprb, test->entprlen);
2018 ret = crypto_drbg_get_bytes_addtl_test(drng,
2019 buf, test->expectedlen, &addtl, &test_data);
2020 } else {
2021 ret = crypto_drbg_get_bytes_addtl(drng,
2022 buf, test->expectedlen, &addtl);
2023 }
19e60e13 2024 if (ret < 0) {
2fc0d258 2025 printk(KERN_ERR "alg: drbg: could not obtain random data for "
64d1cdfb
SM
2026 "driver %s\n", driver);
2027 goto outbuf;
2028 }
2029
2030 ret = memcmp(test->expected, buf, test->expectedlen);
2031
2032outbuf:
2033 crypto_free_rng(drng);
2034 kzfree(buf);
2035 return ret;
2036}
2037
2038
2039static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
2040 u32 type, u32 mask)
2041{
2042 int err = 0;
2043 int pr = 0;
2044 int i = 0;
b13b1e0c 2045 const struct drbg_testvec *template = desc->suite.drbg.vecs;
64d1cdfb
SM
2046 unsigned int tcount = desc->suite.drbg.count;
2047
2048 if (0 == memcmp(driver, "drbg_pr_", 8))
2049 pr = 1;
2050
2051 for (i = 0; i < tcount; i++) {
2052 err = drbg_cavs_test(&template[i], pr, driver, type, mask);
2053 if (err) {
2054 printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
2055 i, driver);
2056 err = -EINVAL;
2057 break;
2058 }
2059 }
2060 return err;
2061
2062}
2063
b13b1e0c 2064static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
802c7f1c
SB
2065 const char *alg)
2066{
2067 struct kpp_request *req;
2068 void *input_buf = NULL;
2069 void *output_buf = NULL;
47d3fd39
TDA
2070 void *a_public = NULL;
2071 void *a_ss = NULL;
2072 void *shared_secret = NULL;
7f397136 2073 struct crypto_wait wait;
802c7f1c
SB
2074 unsigned int out_len_max;
2075 int err = -ENOMEM;
2076 struct scatterlist src, dst;
2077
2078 req = kpp_request_alloc(tfm, GFP_KERNEL);
2079 if (!req)
2080 return err;
2081
7f397136 2082 crypto_init_wait(&wait);
802c7f1c
SB
2083
2084 err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
2085 if (err < 0)
2086 goto free_req;
2087
2088 out_len_max = crypto_kpp_maxsize(tfm);
2089 output_buf = kzalloc(out_len_max, GFP_KERNEL);
2090 if (!output_buf) {
2091 err = -ENOMEM;
2092 goto free_req;
2093 }
2094
2095 /* Use appropriate parameter as base */
2096 kpp_request_set_input(req, NULL, 0);
2097 sg_init_one(&dst, output_buf, out_len_max);
2098 kpp_request_set_output(req, &dst, out_len_max);
2099 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
7f397136 2100 crypto_req_done, &wait);
802c7f1c 2101
47d3fd39 2102 /* Compute party A's public key */
7f397136 2103 err = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
802c7f1c 2104 if (err) {
47d3fd39 2105 pr_err("alg: %s: Party A: generate public key test failed. err %d\n",
802c7f1c
SB
2106 alg, err);
2107 goto free_output;
2108 }
47d3fd39
TDA
2109
2110 if (vec->genkey) {
2111 /* Save party A's public key */
2112 a_public = kzalloc(out_len_max, GFP_KERNEL);
2113 if (!a_public) {
2114 err = -ENOMEM;
2115 goto free_output;
2116 }
2117 memcpy(a_public, sg_virt(req->dst), out_len_max);
2118 } else {
2119 /* Verify calculated public key */
2120 if (memcmp(vec->expected_a_public, sg_virt(req->dst),
2121 vec->expected_a_public_size)) {
2122 pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n",
2123 alg);
2124 err = -EINVAL;
2125 goto free_output;
2126 }
802c7f1c
SB
2127 }
2128
2129 /* Calculate shared secret key by using counter part (b) public key. */
2130 input_buf = kzalloc(vec->b_public_size, GFP_KERNEL);
2131 if (!input_buf) {
2132 err = -ENOMEM;
2133 goto free_output;
2134 }
2135
2136 memcpy(input_buf, vec->b_public, vec->b_public_size);
2137 sg_init_one(&src, input_buf, vec->b_public_size);
2138 sg_init_one(&dst, output_buf, out_len_max);
2139 kpp_request_set_input(req, &src, vec->b_public_size);
2140 kpp_request_set_output(req, &dst, out_len_max);
2141 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
7f397136
GBY
2142 crypto_req_done, &wait);
2143 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait);
802c7f1c 2144 if (err) {
47d3fd39 2145 pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n",
802c7f1c
SB
2146 alg, err);
2147 goto free_all;
2148 }
47d3fd39
TDA
2149
2150 if (vec->genkey) {
2151 /* Save the shared secret obtained by party A */
2152 a_ss = kzalloc(vec->expected_ss_size, GFP_KERNEL);
2153 if (!a_ss) {
2154 err = -ENOMEM;
2155 goto free_all;
2156 }
2157 memcpy(a_ss, sg_virt(req->dst), vec->expected_ss_size);
2158
2159 /*
2160 * Calculate party B's shared secret by using party A's
2161 * public key.
2162 */
2163 err = crypto_kpp_set_secret(tfm, vec->b_secret,
2164 vec->b_secret_size);
2165 if (err < 0)
2166 goto free_all;
2167
2168 sg_init_one(&src, a_public, vec->expected_a_public_size);
2169 sg_init_one(&dst, output_buf, out_len_max);
2170 kpp_request_set_input(req, &src, vec->expected_a_public_size);
2171 kpp_request_set_output(req, &dst, out_len_max);
2172 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
7f397136
GBY
2173 crypto_req_done, &wait);
2174 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req),
2175 &wait);
47d3fd39
TDA
2176 if (err) {
2177 pr_err("alg: %s: Party B: compute shared secret failed. err %d\n",
2178 alg, err);
2179 goto free_all;
2180 }
2181
2182 shared_secret = a_ss;
2183 } else {
2184 shared_secret = (void *)vec->expected_ss;
2185 }
2186
802c7f1c
SB
2187 /*
2188 * verify shared secret from which the user will derive
2189 * secret key by executing whatever hash it has chosen
2190 */
47d3fd39 2191 if (memcmp(shared_secret, sg_virt(req->dst),
802c7f1c
SB
2192 vec->expected_ss_size)) {
2193 pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
2194 alg);
2195 err = -EINVAL;
2196 }
2197
2198free_all:
47d3fd39 2199 kfree(a_ss);
802c7f1c
SB
2200 kfree(input_buf);
2201free_output:
47d3fd39 2202 kfree(a_public);
802c7f1c
SB
2203 kfree(output_buf);
2204free_req:
2205 kpp_request_free(req);
2206 return err;
2207}
2208
2209static int test_kpp(struct crypto_kpp *tfm, const char *alg,
b13b1e0c 2210 const struct kpp_testvec *vecs, unsigned int tcount)
802c7f1c
SB
2211{
2212 int ret, i;
2213
2214 for (i = 0; i < tcount; i++) {
2215 ret = do_test_kpp(tfm, vecs++, alg);
2216 if (ret) {
2217 pr_err("alg: %s: test failed on vector %d, err=%d\n",
2218 alg, i + 1, ret);
2219 return ret;
2220 }
2221 }
2222 return 0;
2223}
2224
2225static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
2226 u32 type, u32 mask)
2227{
2228 struct crypto_kpp *tfm;
2229 int err = 0;
2230
eed93e0c 2231 tfm = crypto_alloc_kpp(driver, type, mask);
802c7f1c
SB
2232 if (IS_ERR(tfm)) {
2233 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
2234 driver, PTR_ERR(tfm));
2235 return PTR_ERR(tfm);
2236 }
2237 if (desc->suite.kpp.vecs)
2238 err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
2239 desc->suite.kpp.count);
2240
2241 crypto_free_kpp(tfm);
2242 return err;
2243}
2244
50d2b643 2245static int test_akcipher_one(struct crypto_akcipher *tfm,
b13b1e0c 2246 const struct akcipher_testvec *vecs)
946cc463 2247{
df27b26f 2248 char *xbuf[XBUFSIZE];
946cc463
TS
2249 struct akcipher_request *req;
2250 void *outbuf_enc = NULL;
2251 void *outbuf_dec = NULL;
7f397136 2252 struct crypto_wait wait;
946cc463
TS
2253 unsigned int out_len_max, out_len = 0;
2254 int err = -ENOMEM;
22287b0b 2255 struct scatterlist src, dst, src_tab[2];
0507de94
VC
2256 const char *m, *c;
2257 unsigned int m_size, c_size;
2258 const char *op;
946cc463 2259
df27b26f
HX
2260 if (testmgr_alloc_buf(xbuf))
2261 return err;
2262
946cc463
TS
2263 req = akcipher_request_alloc(tfm, GFP_KERNEL);
2264 if (!req)
df27b26f 2265 goto free_xbuf;
946cc463 2266
7f397136 2267 crypto_init_wait(&wait);
946cc463 2268
22287b0b
TS
2269 if (vecs->public_key_vec)
2270 err = crypto_akcipher_set_pub_key(tfm, vecs->key,
2271 vecs->key_len);
2272 else
2273 err = crypto_akcipher_set_priv_key(tfm, vecs->key,
2274 vecs->key_len);
2275 if (err)
946cc463 2276 goto free_req;
946cc463 2277
57763f5e 2278 err = -ENOMEM;
22287b0b 2279 out_len_max = crypto_akcipher_maxsize(tfm);
0507de94
VC
2280
2281 /*
2282 * First run test which do not require a private key, such as
2283 * encrypt or verify.
2284 */
946cc463
TS
2285 outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
2286 if (!outbuf_enc)
2287 goto free_req;
2288
0507de94
VC
2289 if (!vecs->siggen_sigver_test) {
2290 m = vecs->m;
2291 m_size = vecs->m_size;
2292 c = vecs->c;
2293 c_size = vecs->c_size;
2294 op = "encrypt";
2295 } else {
2296 /* Swap args so we could keep plaintext (digest)
2297 * in vecs->m, and cooked signature in vecs->c.
2298 */
2299 m = vecs->c; /* signature */
2300 m_size = vecs->c_size;
2301 c = vecs->m; /* digest */
2302 c_size = vecs->m_size;
2303 op = "verify";
2304 }
df27b26f 2305
0507de94
VC
2306 if (WARN_ON(m_size > PAGE_SIZE))
2307 goto free_all;
2308 memcpy(xbuf[0], m, m_size);
df27b26f 2309
22287b0b 2310 sg_init_table(src_tab, 2);
df27b26f 2311 sg_set_buf(&src_tab[0], xbuf[0], 8);
0507de94 2312 sg_set_buf(&src_tab[1], xbuf[0] + 8, m_size - 8);
22287b0b 2313 sg_init_one(&dst, outbuf_enc, out_len_max);
0507de94 2314 akcipher_request_set_crypt(req, src_tab, &dst, m_size,
22287b0b 2315 out_len_max);
946cc463 2316 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
7f397136 2317 crypto_req_done, &wait);
946cc463 2318
7f397136 2319 err = crypto_wait_req(vecs->siggen_sigver_test ?
0507de94
VC
2320 /* Run asymmetric signature verification */
2321 crypto_akcipher_verify(req) :
7f397136
GBY
2322 /* Run asymmetric encrypt */
2323 crypto_akcipher_encrypt(req), &wait);
946cc463 2324 if (err) {
0507de94 2325 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
946cc463
TS
2326 goto free_all;
2327 }
0507de94
VC
2328 if (req->dst_len != c_size) {
2329 pr_err("alg: akcipher: %s test failed. Invalid output len\n",
2330 op);
946cc463
TS
2331 err = -EINVAL;
2332 goto free_all;
2333 }
2334 /* verify that encrypted message is equal to expected */
0507de94
VC
2335 if (memcmp(c, outbuf_enc, c_size)) {
2336 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
2337 hexdump(outbuf_enc, c_size);
946cc463
TS
2338 err = -EINVAL;
2339 goto free_all;
2340 }
0507de94
VC
2341
2342 /*
2343 * Don't invoke (decrypt or sign) test which require a private key
2344 * for vectors with only a public key.
2345 */
946cc463
TS
2346 if (vecs->public_key_vec) {
2347 err = 0;
2348 goto free_all;
2349 }
2350 outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
2351 if (!outbuf_dec) {
2352 err = -ENOMEM;
2353 goto free_all;
2354 }
df27b26f 2355
0507de94
VC
2356 op = vecs->siggen_sigver_test ? "sign" : "decrypt";
2357 if (WARN_ON(c_size > PAGE_SIZE))
df27b26f 2358 goto free_all;
0507de94 2359 memcpy(xbuf[0], c, c_size);
df27b26f 2360
0507de94 2361 sg_init_one(&src, xbuf[0], c_size);
22287b0b 2362 sg_init_one(&dst, outbuf_dec, out_len_max);
7f397136 2363 crypto_init_wait(&wait);
0507de94 2364 akcipher_request_set_crypt(req, &src, &dst, c_size, out_len_max);
946cc463 2365
7f397136 2366 err = crypto_wait_req(vecs->siggen_sigver_test ?
0507de94
VC
2367 /* Run asymmetric signature generation */
2368 crypto_akcipher_sign(req) :
7f397136
GBY
2369 /* Run asymmetric decrypt */
2370 crypto_akcipher_decrypt(req), &wait);
946cc463 2371 if (err) {
0507de94 2372 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
946cc463
TS
2373 goto free_all;
2374 }
2375 out_len = req->dst_len;
0507de94
VC
2376 if (out_len < m_size) {
2377 pr_err("alg: akcipher: %s test failed. Invalid output len %u\n",
2378 op, out_len);
946cc463
TS
2379 err = -EINVAL;
2380 goto free_all;
2381 }
2382 /* verify that decrypted message is equal to the original msg */
0507de94
VC
2383 if (memchr_inv(outbuf_dec, 0, out_len - m_size) ||
2384 memcmp(m, outbuf_dec + out_len - m_size, m_size)) {
2385 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
50d2b643 2386 hexdump(outbuf_dec, out_len);
946cc463
TS
2387 err = -EINVAL;
2388 }
2389free_all:
2390 kfree(outbuf_dec);
2391 kfree(outbuf_enc);
2392free_req:
2393 akcipher_request_free(req);
df27b26f
HX
2394free_xbuf:
2395 testmgr_free_buf(xbuf);
946cc463
TS
2396 return err;
2397}
2398
50d2b643 2399static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
b13b1e0c
EB
2400 const struct akcipher_testvec *vecs,
2401 unsigned int tcount)
946cc463 2402{
15226e48
HX
2403 const char *algo =
2404 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
946cc463
TS
2405 int ret, i;
2406
2407 for (i = 0; i < tcount; i++) {
50d2b643
HX
2408 ret = test_akcipher_one(tfm, vecs++);
2409 if (!ret)
2410 continue;
946cc463 2411
15226e48
HX
2412 pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
2413 i + 1, algo, ret);
50d2b643
HX
2414 return ret;
2415 }
946cc463
TS
2416 return 0;
2417}
2418
2419static int alg_test_akcipher(const struct alg_test_desc *desc,
2420 const char *driver, u32 type, u32 mask)
2421{
2422 struct crypto_akcipher *tfm;
2423 int err = 0;
2424
eed93e0c 2425 tfm = crypto_alloc_akcipher(driver, type, mask);
946cc463
TS
2426 if (IS_ERR(tfm)) {
2427 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
2428 driver, PTR_ERR(tfm));
2429 return PTR_ERR(tfm);
2430 }
2431 if (desc->suite.akcipher.vecs)
2432 err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
2433 desc->suite.akcipher.count);
2434
2435 crypto_free_akcipher(tfm);
2436 return err;
2437}
2438
863b557a
YS
2439static int alg_test_null(const struct alg_test_desc *desc,
2440 const char *driver, u32 type, u32 mask)
2441{
2442 return 0;
2443}
2444
21c8e720
AB
2445#define __VECS(tv) { .vecs = tv, .count = ARRAY_SIZE(tv) }
2446
da7f033d
HX
2447/* Please keep this list sorted by algorithm name. */
2448static const struct alg_test_desc alg_test_descs[] = {
2449 {
059c2a4d
EB
2450 .alg = "adiantum(xchacha12,aes)",
2451 .test = alg_test_skcipher,
2452 .suite = {
2453 .cipher = __VECS(adiantum_xchacha12_aes_tv_template)
2454 },
2455 }, {
2456 .alg = "adiantum(xchacha20,aes)",
2457 .test = alg_test_skcipher,
2458 .suite = {
2459 .cipher = __VECS(adiantum_xchacha20_aes_tv_template)
2460 },
2461 }, {
b87dc203
OM
2462 .alg = "aegis128",
2463 .test = alg_test_aead,
2464 .suite = {
a0d608ee 2465 .aead = __VECS(aegis128_tv_template)
b87dc203
OM
2466 }
2467 }, {
2468 .alg = "aegis128l",
2469 .test = alg_test_aead,
2470 .suite = {
a0d608ee 2471 .aead = __VECS(aegis128l_tv_template)
b87dc203
OM
2472 }
2473 }, {
2474 .alg = "aegis256",
2475 .test = alg_test_aead,
2476 .suite = {
a0d608ee 2477 .aead = __VECS(aegis256_tv_template)
b87dc203
OM
2478 }
2479 }, {
e08ca2da
JW
2480 .alg = "ansi_cprng",
2481 .test = alg_test_cprng,
2482 .suite = {
21c8e720 2483 .cprng = __VECS(ansi_cprng_aes_tv_template)
e08ca2da 2484 }
bca4feb0
HG
2485 }, {
2486 .alg = "authenc(hmac(md5),ecb(cipher_null))",
2487 .test = alg_test_aead,
bca4feb0 2488 .suite = {
a0d608ee 2489 .aead = __VECS(hmac_md5_ecb_cipher_null_tv_template)
bca4feb0 2490 }
e46e9a46 2491 }, {
a4198fd4 2492 .alg = "authenc(hmac(sha1),cbc(aes))",
e46e9a46 2493 .test = alg_test_aead,
bcf741cb 2494 .fips_allowed = 1,
e46e9a46 2495 .suite = {
a0d608ee 2496 .aead = __VECS(hmac_sha1_aes_cbc_tv_temp)
5208ed2c
NL
2497 }
2498 }, {
a4198fd4 2499 .alg = "authenc(hmac(sha1),cbc(des))",
5208ed2c 2500 .test = alg_test_aead,
5208ed2c 2501 .suite = {
a0d608ee 2502 .aead = __VECS(hmac_sha1_des_cbc_tv_temp)
5208ed2c
NL
2503 }
2504 }, {
a4198fd4 2505 .alg = "authenc(hmac(sha1),cbc(des3_ede))",
5208ed2c 2506 .test = alg_test_aead,
ed1afac9 2507 .fips_allowed = 1,
5208ed2c 2508 .suite = {
a0d608ee 2509 .aead = __VECS(hmac_sha1_des3_ede_cbc_tv_temp)
e46e9a46 2510 }
fb16abc2
MM
2511 }, {
2512 .alg = "authenc(hmac(sha1),ctr(aes))",
2513 .test = alg_test_null,
2514 .fips_allowed = 1,
bca4feb0
HG
2515 }, {
2516 .alg = "authenc(hmac(sha1),ecb(cipher_null))",
2517 .test = alg_test_aead,
bca4feb0 2518 .suite = {
a0d608ee 2519 .aead = __VECS(hmac_sha1_ecb_cipher_null_tv_temp)
5208ed2c 2520 }
8888690e
MM
2521 }, {
2522 .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
2523 .test = alg_test_null,
2524 .fips_allowed = 1,
5208ed2c 2525 }, {
a4198fd4 2526 .alg = "authenc(hmac(sha224),cbc(des))",
5208ed2c 2527 .test = alg_test_aead,
5208ed2c 2528 .suite = {
a0d608ee 2529 .aead = __VECS(hmac_sha224_des_cbc_tv_temp)
5208ed2c
NL
2530 }
2531 }, {
a4198fd4 2532 .alg = "authenc(hmac(sha224),cbc(des3_ede))",
5208ed2c 2533 .test = alg_test_aead,
ed1afac9 2534 .fips_allowed = 1,
5208ed2c 2535 .suite = {
a0d608ee 2536 .aead = __VECS(hmac_sha224_des3_ede_cbc_tv_temp)
bca4feb0 2537 }
e46e9a46 2538 }, {
a4198fd4 2539 .alg = "authenc(hmac(sha256),cbc(aes))",
e46e9a46 2540 .test = alg_test_aead,
ed1afac9 2541 .fips_allowed = 1,
e46e9a46 2542 .suite = {
a0d608ee 2543 .aead = __VECS(hmac_sha256_aes_cbc_tv_temp)
5208ed2c
NL
2544 }
2545 }, {
a4198fd4 2546 .alg = "authenc(hmac(sha256),cbc(des))",
5208ed2c 2547 .test = alg_test_aead,
5208ed2c 2548 .suite = {
a0d608ee 2549 .aead = __VECS(hmac_sha256_des_cbc_tv_temp)
5208ed2c
NL
2550 }
2551 }, {
a4198fd4 2552 .alg = "authenc(hmac(sha256),cbc(des3_ede))",
5208ed2c 2553 .test = alg_test_aead,
ed1afac9 2554 .fips_allowed = 1,
5208ed2c 2555 .suite = {
a0d608ee 2556 .aead = __VECS(hmac_sha256_des3_ede_cbc_tv_temp)
5208ed2c 2557 }
fb16abc2
MM
2558 }, {
2559 .alg = "authenc(hmac(sha256),ctr(aes))",
2560 .test = alg_test_null,
2561 .fips_allowed = 1,
8888690e
MM
2562 }, {
2563 .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
2564 .test = alg_test_null,
2565 .fips_allowed = 1,
5208ed2c 2566 }, {
a4198fd4 2567 .alg = "authenc(hmac(sha384),cbc(des))",
5208ed2c 2568 .test = alg_test_aead,
5208ed2c 2569 .suite = {
a0d608ee 2570 .aead = __VECS(hmac_sha384_des_cbc_tv_temp)
5208ed2c
NL
2571 }
2572 }, {
a4198fd4 2573 .alg = "authenc(hmac(sha384),cbc(des3_ede))",
5208ed2c 2574 .test = alg_test_aead,
ed1afac9 2575 .fips_allowed = 1,
5208ed2c 2576 .suite = {
a0d608ee 2577 .aead = __VECS(hmac_sha384_des3_ede_cbc_tv_temp)
e46e9a46 2578 }
fb16abc2
MM
2579 }, {
2580 .alg = "authenc(hmac(sha384),ctr(aes))",
2581 .test = alg_test_null,
2582 .fips_allowed = 1,
8888690e
MM
2583 }, {
2584 .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
2585 .test = alg_test_null,
2586 .fips_allowed = 1,
e46e9a46 2587 }, {
a4198fd4 2588 .alg = "authenc(hmac(sha512),cbc(aes))",
ed1afac9 2589 .fips_allowed = 1,
e46e9a46 2590 .test = alg_test_aead,
e46e9a46 2591 .suite = {
a0d608ee 2592 .aead = __VECS(hmac_sha512_aes_cbc_tv_temp)
5208ed2c
NL
2593 }
2594 }, {
a4198fd4 2595 .alg = "authenc(hmac(sha512),cbc(des))",
5208ed2c 2596 .test = alg_test_aead,
5208ed2c 2597 .suite = {
a0d608ee 2598 .aead = __VECS(hmac_sha512_des_cbc_tv_temp)
5208ed2c
NL
2599 }
2600 }, {
a4198fd4 2601 .alg = "authenc(hmac(sha512),cbc(des3_ede))",
5208ed2c 2602 .test = alg_test_aead,
ed1afac9 2603 .fips_allowed = 1,
5208ed2c 2604 .suite = {
a0d608ee 2605 .aead = __VECS(hmac_sha512_des3_ede_cbc_tv_temp)
e46e9a46 2606 }
fb16abc2
MM
2607 }, {
2608 .alg = "authenc(hmac(sha512),ctr(aes))",
2609 .test = alg_test_null,
2610 .fips_allowed = 1,
8888690e
MM
2611 }, {
2612 .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
2613 .test = alg_test_null,
2614 .fips_allowed = 1,
e08ca2da 2615 }, {
da7f033d 2616 .alg = "cbc(aes)",
1aa4ecd9 2617 .test = alg_test_skcipher,
a1915d51 2618 .fips_allowed = 1,
da7f033d 2619 .suite = {
92a4c9fe
EB
2620 .cipher = __VECS(aes_cbc_tv_template)
2621 },
da7f033d
HX
2622 }, {
2623 .alg = "cbc(anubis)",
1aa4ecd9 2624 .test = alg_test_skcipher,
da7f033d 2625 .suite = {
92a4c9fe
EB
2626 .cipher = __VECS(anubis_cbc_tv_template)
2627 },
da7f033d
HX
2628 }, {
2629 .alg = "cbc(blowfish)",
1aa4ecd9 2630 .test = alg_test_skcipher,
da7f033d 2631 .suite = {
92a4c9fe
EB
2632 .cipher = __VECS(bf_cbc_tv_template)
2633 },
da7f033d
HX
2634 }, {
2635 .alg = "cbc(camellia)",
1aa4ecd9 2636 .test = alg_test_skcipher,
da7f033d 2637 .suite = {
92a4c9fe
EB
2638 .cipher = __VECS(camellia_cbc_tv_template)
2639 },
a2c58260
JG
2640 }, {
2641 .alg = "cbc(cast5)",
2642 .test = alg_test_skcipher,
2643 .suite = {
92a4c9fe
EB
2644 .cipher = __VECS(cast5_cbc_tv_template)
2645 },
9b8b0405
JG
2646 }, {
2647 .alg = "cbc(cast6)",
2648 .test = alg_test_skcipher,
2649 .suite = {
92a4c9fe
EB
2650 .cipher = __VECS(cast6_cbc_tv_template)
2651 },
da7f033d
HX
2652 }, {
2653 .alg = "cbc(des)",
1aa4ecd9 2654 .test = alg_test_skcipher,
da7f033d 2655 .suite = {
92a4c9fe
EB
2656 .cipher = __VECS(des_cbc_tv_template)
2657 },
da7f033d
HX
2658 }, {
2659 .alg = "cbc(des3_ede)",
1aa4ecd9 2660 .test = alg_test_skcipher,
a1915d51 2661 .fips_allowed = 1,
da7f033d 2662 .suite = {
92a4c9fe
EB
2663 .cipher = __VECS(des3_ede_cbc_tv_template)
2664 },
a794d8d8
GBY
2665 }, {
2666 /* Same as cbc(aes) except the key is stored in
2667 * hardware secure memory which we reference by index
2668 */
2669 .alg = "cbc(paes)",
2670 .test = alg_test_null,
2671 .fips_allowed = 1,
9d25917d
JK
2672 }, {
2673 .alg = "cbc(serpent)",
2674 .test = alg_test_skcipher,
2675 .suite = {
92a4c9fe
EB
2676 .cipher = __VECS(serpent_cbc_tv_template)
2677 },
95ba5973
GBY
2678 }, {
2679 .alg = "cbc(sm4)",
2680 .test = alg_test_skcipher,
2681 .suite = {
2682 .cipher = __VECS(sm4_cbc_tv_template)
2683 }
da7f033d
HX
2684 }, {
2685 .alg = "cbc(twofish)",
1aa4ecd9 2686 .test = alg_test_skcipher,
da7f033d 2687 .suite = {
92a4c9fe
EB
2688 .cipher = __VECS(tf_cbc_tv_template)
2689 },
092acf06
AB
2690 }, {
2691 .alg = "cbcmac(aes)",
2692 .fips_allowed = 1,
2693 .test = alg_test_hash,
2694 .suite = {
2695 .hash = __VECS(aes_cbcmac_tv_template)
2696 }
da7f033d
HX
2697 }, {
2698 .alg = "ccm(aes)",
2699 .test = alg_test_aead,
a1915d51 2700 .fips_allowed = 1,
da7f033d 2701 .suite = {
a0d608ee 2702 .aead = __VECS(aes_ccm_tv_template)
da7f033d 2703 }
7da66670
DES
2704 }, {
2705 .alg = "cfb(aes)",
2706 .test = alg_test_skcipher,
2707 .fips_allowed = 1,
2708 .suite = {
2709 .cipher = __VECS(aes_cfb_tv_template)
2710 },
3590ebf2
MW
2711 }, {
2712 .alg = "chacha20",
2713 .test = alg_test_skcipher,
2714 .suite = {
92a4c9fe
EB
2715 .cipher = __VECS(chacha20_tv_template)
2716 },
93b5e86a
JK
2717 }, {
2718 .alg = "cmac(aes)",
8f183751 2719 .fips_allowed = 1,
93b5e86a
JK
2720 .test = alg_test_hash,
2721 .suite = {
21c8e720 2722 .hash = __VECS(aes_cmac128_tv_template)
93b5e86a
JK
2723 }
2724 }, {
2725 .alg = "cmac(des3_ede)",
8f183751 2726 .fips_allowed = 1,
93b5e86a
JK
2727 .test = alg_test_hash,
2728 .suite = {
21c8e720 2729 .hash = __VECS(des3_ede_cmac64_tv_template)
93b5e86a 2730 }
e448370d
JK
2731 }, {
2732 .alg = "compress_null",
2733 .test = alg_test_null,
ebb3472f
AB
2734 }, {
2735 .alg = "crc32",
2736 .test = alg_test_hash,
2737 .suite = {
21c8e720 2738 .hash = __VECS(crc32_tv_template)
ebb3472f 2739 }
da7f033d
HX
2740 }, {
2741 .alg = "crc32c",
8e3ee85e 2742 .test = alg_test_crc32c,
a1915d51 2743 .fips_allowed = 1,
da7f033d 2744 .suite = {
21c8e720 2745 .hash = __VECS(crc32c_tv_template)
da7f033d 2746 }
68411521
HX
2747 }, {
2748 .alg = "crct10dif",
2749 .test = alg_test_hash,
2750 .fips_allowed = 1,
2751 .suite = {
21c8e720 2752 .hash = __VECS(crct10dif_tv_template)
68411521 2753 }
f7cb80f2
JW
2754 }, {
2755 .alg = "ctr(aes)",
2756 .test = alg_test_skcipher,
a1915d51 2757 .fips_allowed = 1,
f7cb80f2 2758 .suite = {
92a4c9fe 2759 .cipher = __VECS(aes_ctr_tv_template)
f7cb80f2 2760 }
85b63e34
JK
2761 }, {
2762 .alg = "ctr(blowfish)",
2763 .test = alg_test_skcipher,
2764 .suite = {
92a4c9fe 2765 .cipher = __VECS(bf_ctr_tv_template)
85b63e34 2766 }
0840605e
JK
2767 }, {
2768 .alg = "ctr(camellia)",
2769 .test = alg_test_skcipher,
2770 .suite = {
92a4c9fe 2771 .cipher = __VECS(camellia_ctr_tv_template)
0840605e 2772 }
a2c58260
JG
2773 }, {
2774 .alg = "ctr(cast5)",
2775 .test = alg_test_skcipher,
2776 .suite = {
92a4c9fe 2777 .cipher = __VECS(cast5_ctr_tv_template)
a2c58260 2778 }
9b8b0405
JG
2779 }, {
2780 .alg = "ctr(cast6)",
2781 .test = alg_test_skcipher,
2782 .suite = {
92a4c9fe 2783 .cipher = __VECS(cast6_ctr_tv_template)
9b8b0405 2784 }
8163fc30
JK
2785 }, {
2786 .alg = "ctr(des)",
2787 .test = alg_test_skcipher,
2788 .suite = {
92a4c9fe 2789 .cipher = __VECS(des_ctr_tv_template)
8163fc30 2790 }
e080b17a
JK
2791 }, {
2792 .alg = "ctr(des3_ede)",
2793 .test = alg_test_skcipher,
0d8da104 2794 .fips_allowed = 1,
e080b17a 2795 .suite = {
92a4c9fe 2796 .cipher = __VECS(des3_ede_ctr_tv_template)
e080b17a 2797 }
a794d8d8
GBY
2798 }, {
2799 /* Same as ctr(aes) except the key is stored in
2800 * hardware secure memory which we reference by index
2801 */
2802 .alg = "ctr(paes)",
2803 .test = alg_test_null,
2804 .fips_allowed = 1,
9d25917d
JK
2805 }, {
2806 .alg = "ctr(serpent)",
2807 .test = alg_test_skcipher,
2808 .suite = {
92a4c9fe 2809 .cipher = __VECS(serpent_ctr_tv_template)
9d25917d 2810 }
95ba5973
GBY
2811 }, {
2812 .alg = "ctr(sm4)",
2813 .test = alg_test_skcipher,
2814 .suite = {
2815 .cipher = __VECS(sm4_ctr_tv_template)
2816 }
573da620
JK
2817 }, {
2818 .alg = "ctr(twofish)",
2819 .test = alg_test_skcipher,
2820 .suite = {
92a4c9fe 2821 .cipher = __VECS(tf_ctr_tv_template)
573da620 2822 }
da7f033d
HX
2823 }, {
2824 .alg = "cts(cbc(aes))",
1aa4ecd9 2825 .test = alg_test_skcipher,
196ad604 2826 .fips_allowed = 1,
da7f033d 2827 .suite = {
92a4c9fe 2828 .cipher = __VECS(cts_mode_tv_template)
da7f033d
HX
2829 }
2830 }, {
2831 .alg = "deflate",
2832 .test = alg_test_comp,
0818904d 2833 .fips_allowed = 1,
da7f033d
HX
2834 .suite = {
2835 .comp = {
21c8e720
AB
2836 .comp = __VECS(deflate_comp_tv_template),
2837 .decomp = __VECS(deflate_decomp_tv_template)
da7f033d
HX
2838 }
2839 }
802c7f1c
SB
2840 }, {
2841 .alg = "dh",
2842 .test = alg_test_kpp,
2843 .fips_allowed = 1,
2844 .suite = {
21c8e720 2845 .kpp = __VECS(dh_tv_template)
802c7f1c 2846 }
e448370d
JK
2847 }, {
2848 .alg = "digest_null",
2849 .test = alg_test_null,
64d1cdfb
SM
2850 }, {
2851 .alg = "drbg_nopr_ctr_aes128",
2852 .test = alg_test_drbg,
2853 .fips_allowed = 1,
2854 .suite = {
21c8e720 2855 .drbg = __VECS(drbg_nopr_ctr_aes128_tv_template)
64d1cdfb
SM
2856 }
2857 }, {
2858 .alg = "drbg_nopr_ctr_aes192",
2859 .test = alg_test_drbg,
2860 .fips_allowed = 1,
2861 .suite = {
21c8e720 2862 .drbg = __VECS(drbg_nopr_ctr_aes192_tv_template)
64d1cdfb
SM
2863 }
2864 }, {
2865 .alg = "drbg_nopr_ctr_aes256",
2866 .test = alg_test_drbg,
2867 .fips_allowed = 1,
2868 .suite = {
21c8e720 2869 .drbg = __VECS(drbg_nopr_ctr_aes256_tv_template)
64d1cdfb
SM
2870 }
2871 }, {
2872 /*
2873 * There is no need to specifically test the DRBG with every
2874 * backend cipher -- covered by drbg_nopr_hmac_sha256 test
2875 */
2876 .alg = "drbg_nopr_hmac_sha1",
2877 .fips_allowed = 1,
2878 .test = alg_test_null,
2879 }, {
2880 .alg = "drbg_nopr_hmac_sha256",
2881 .test = alg_test_drbg,
2882 .fips_allowed = 1,
2883 .suite = {
21c8e720 2884 .drbg = __VECS(drbg_nopr_hmac_sha256_tv_template)
64d1cdfb
SM
2885 }
2886 }, {
2887 /* covered by drbg_nopr_hmac_sha256 test */
2888 .alg = "drbg_nopr_hmac_sha384",
2889 .fips_allowed = 1,
2890 .test = alg_test_null,
2891 }, {
2892 .alg = "drbg_nopr_hmac_sha512",
2893 .test = alg_test_null,
2894 .fips_allowed = 1,
2895 }, {
2896 .alg = "drbg_nopr_sha1",
2897 .fips_allowed = 1,
2898 .test = alg_test_null,
2899 }, {
2900 .alg = "drbg_nopr_sha256",
2901 .test = alg_test_drbg,
2902 .fips_allowed = 1,
2903 .suite = {
21c8e720 2904 .drbg = __VECS(drbg_nopr_sha256_tv_template)
64d1cdfb
SM
2905 }
2906 }, {
2907 /* covered by drbg_nopr_sha256 test */
2908 .alg = "drbg_nopr_sha384",
2909 .fips_allowed = 1,
2910 .test = alg_test_null,
2911 }, {
2912 .alg = "drbg_nopr_sha512",
2913 .fips_allowed = 1,
2914 .test = alg_test_null,
2915 }, {
2916 .alg = "drbg_pr_ctr_aes128",
2917 .test = alg_test_drbg,
2918 .fips_allowed = 1,
2919 .suite = {
21c8e720 2920 .drbg = __VECS(drbg_pr_ctr_aes128_tv_template)
64d1cdfb
SM
2921 }
2922 }, {
2923 /* covered by drbg_pr_ctr_aes128 test */
2924 .alg = "drbg_pr_ctr_aes192",
2925 .fips_allowed = 1,
2926 .test = alg_test_null,
2927 }, {
2928 .alg = "drbg_pr_ctr_aes256",
2929 .fips_allowed = 1,
2930 .test = alg_test_null,
2931 }, {
2932 .alg = "drbg_pr_hmac_sha1",
2933 .fips_allowed = 1,
2934 .test = alg_test_null,
2935 }, {
2936 .alg = "drbg_pr_hmac_sha256",
2937 .test = alg_test_drbg,
2938 .fips_allowed = 1,
2939 .suite = {
21c8e720 2940 .drbg = __VECS(drbg_pr_hmac_sha256_tv_template)
64d1cdfb
SM
2941 }
2942 }, {
2943 /* covered by drbg_pr_hmac_sha256 test */
2944 .alg = "drbg_pr_hmac_sha384",
2945 .fips_allowed = 1,
2946 .test = alg_test_null,
2947 }, {
2948 .alg = "drbg_pr_hmac_sha512",
2949 .test = alg_test_null,
2950 .fips_allowed = 1,
2951 }, {
2952 .alg = "drbg_pr_sha1",
2953 .fips_allowed = 1,
2954 .test = alg_test_null,
2955 }, {
2956 .alg = "drbg_pr_sha256",
2957 .test = alg_test_drbg,
2958 .fips_allowed = 1,
2959 .suite = {
21c8e720 2960 .drbg = __VECS(drbg_pr_sha256_tv_template)
64d1cdfb
SM
2961 }
2962 }, {
2963 /* covered by drbg_pr_sha256 test */
2964 .alg = "drbg_pr_sha384",
2965 .fips_allowed = 1,
2966 .test = alg_test_null,
2967 }, {
2968 .alg = "drbg_pr_sha512",
2969 .fips_allowed = 1,
2970 .test = alg_test_null,
da7f033d
HX
2971 }, {
2972 .alg = "ecb(aes)",
1aa4ecd9 2973 .test = alg_test_skcipher,
a1915d51 2974 .fips_allowed = 1,
da7f033d 2975 .suite = {
92a4c9fe 2976 .cipher = __VECS(aes_tv_template)
da7f033d
HX
2977 }
2978 }, {
2979 .alg = "ecb(anubis)",
1aa4ecd9 2980 .test = alg_test_skcipher,
da7f033d 2981 .suite = {
92a4c9fe 2982 .cipher = __VECS(anubis_tv_template)
da7f033d
HX
2983 }
2984 }, {
2985 .alg = "ecb(arc4)",
1aa4ecd9 2986 .test = alg_test_skcipher,
da7f033d 2987 .suite = {
92a4c9fe 2988 .cipher = __VECS(arc4_tv_template)
da7f033d
HX
2989 }
2990 }, {
2991 .alg = "ecb(blowfish)",
1aa4ecd9 2992 .test = alg_test_skcipher,
da7f033d 2993 .suite = {
92a4c9fe 2994 .cipher = __VECS(bf_tv_template)
da7f033d
HX
2995 }
2996 }, {
2997 .alg = "ecb(camellia)",
1aa4ecd9 2998 .test = alg_test_skcipher,
da7f033d 2999 .suite = {
92a4c9fe 3000 .cipher = __VECS(camellia_tv_template)
da7f033d
HX
3001 }
3002 }, {
3003 .alg = "ecb(cast5)",
1aa4ecd9 3004 .test = alg_test_skcipher,
da7f033d 3005 .suite = {
92a4c9fe 3006 .cipher = __VECS(cast5_tv_template)
da7f033d
HX
3007 }
3008 }, {
3009 .alg = "ecb(cast6)",
1aa4ecd9 3010 .test = alg_test_skcipher,
da7f033d 3011 .suite = {
92a4c9fe 3012 .cipher = __VECS(cast6_tv_template)
da7f033d 3013 }
e448370d
JK
3014 }, {
3015 .alg = "ecb(cipher_null)",
3016 .test = alg_test_null,
6175ca2b 3017 .fips_allowed = 1,
da7f033d
HX
3018 }, {
3019 .alg = "ecb(des)",
1aa4ecd9 3020 .test = alg_test_skcipher,
da7f033d 3021 .suite = {
92a4c9fe 3022 .cipher = __VECS(des_tv_template)
da7f033d
HX
3023 }
3024 }, {
3025 .alg = "ecb(des3_ede)",
1aa4ecd9 3026 .test = alg_test_skcipher,
a1915d51 3027 .fips_allowed = 1,
da7f033d 3028 .suite = {
92a4c9fe 3029 .cipher = __VECS(des3_ede_tv_template)
da7f033d 3030 }
66e5bd00
JK
3031 }, {
3032 .alg = "ecb(fcrypt)",
3033 .test = alg_test_skcipher,
3034 .suite = {
3035 .cipher = {
92a4c9fe
EB
3036 .vecs = fcrypt_pcbc_tv_template,
3037 .count = 1
66e5bd00
JK
3038 }
3039 }
da7f033d
HX
3040 }, {
3041 .alg = "ecb(khazad)",
1aa4ecd9 3042 .test = alg_test_skcipher,
da7f033d 3043 .suite = {
92a4c9fe 3044 .cipher = __VECS(khazad_tv_template)
da7f033d 3045 }
15f47ce5
GBY
3046 }, {
3047 /* Same as ecb(aes) except the key is stored in
3048 * hardware secure memory which we reference by index
3049 */
3050 .alg = "ecb(paes)",
3051 .test = alg_test_null,
3052 .fips_allowed = 1,
da7f033d
HX
3053 }, {
3054 .alg = "ecb(seed)",
1aa4ecd9 3055 .test = alg_test_skcipher,
da7f033d 3056 .suite = {
92a4c9fe 3057 .cipher = __VECS(seed_tv_template)
da7f033d
HX
3058 }
3059 }, {
3060 .alg = "ecb(serpent)",
1aa4ecd9 3061 .test = alg_test_skcipher,
da7f033d 3062 .suite = {
92a4c9fe 3063 .cipher = __VECS(serpent_tv_template)
da7f033d 3064 }
cd83a8a7
GBY
3065 }, {
3066 .alg = "ecb(sm4)",
3067 .test = alg_test_skcipher,
3068 .suite = {
92a4c9fe 3069 .cipher = __VECS(sm4_tv_template)
cd83a8a7 3070 }
da7f033d
HX
3071 }, {
3072 .alg = "ecb(tea)",
1aa4ecd9 3073 .test = alg_test_skcipher,
da7f033d 3074 .suite = {
92a4c9fe 3075 .cipher = __VECS(tea_tv_template)
da7f033d
HX
3076 }
3077 }, {
3078 .alg = "ecb(tnepres)",
1aa4ecd9 3079 .test = alg_test_skcipher,
da7f033d 3080 .suite = {
92a4c9fe 3081 .cipher = __VECS(tnepres_tv_template)
da7f033d
HX
3082 }
3083 }, {
3084 .alg = "ecb(twofish)",
1aa4ecd9 3085 .test = alg_test_skcipher,
da7f033d 3086 .suite = {
92a4c9fe 3087 .cipher = __VECS(tf_tv_template)
da7f033d
HX
3088 }
3089 }, {
3090 .alg = "ecb(xeta)",
1aa4ecd9 3091 .test = alg_test_skcipher,
da7f033d 3092 .suite = {
92a4c9fe 3093 .cipher = __VECS(xeta_tv_template)
da7f033d
HX
3094 }
3095 }, {
3096 .alg = "ecb(xtea)",
1aa4ecd9 3097 .test = alg_test_skcipher,
da7f033d 3098 .suite = {
92a4c9fe 3099 .cipher = __VECS(xtea_tv_template)
da7f033d 3100 }
3c4b2390
SB
3101 }, {
3102 .alg = "ecdh",
3103 .test = alg_test_kpp,
3104 .fips_allowed = 1,
3105 .suite = {
21c8e720 3106 .kpp = __VECS(ecdh_tv_template)
3c4b2390 3107 }
da7f033d
HX
3108 }, {
3109 .alg = "gcm(aes)",
3110 .test = alg_test_aead,
a1915d51 3111 .fips_allowed = 1,
da7f033d 3112 .suite = {
a0d608ee 3113 .aead = __VECS(aes_gcm_tv_template)
da7f033d 3114 }
507069c9
YS
3115 }, {
3116 .alg = "ghash",
3117 .test = alg_test_hash,
18c0ebd2 3118 .fips_allowed = 1,
507069c9 3119 .suite = {
21c8e720 3120 .hash = __VECS(ghash_tv_template)
507069c9 3121 }
da7f033d
HX
3122 }, {
3123 .alg = "hmac(md5)",
3124 .test = alg_test_hash,
3125 .suite = {
21c8e720 3126 .hash = __VECS(hmac_md5_tv_template)
da7f033d
HX
3127 }
3128 }, {
3129 .alg = "hmac(rmd128)",
3130 .test = alg_test_hash,
3131 .suite = {
21c8e720 3132 .hash = __VECS(hmac_rmd128_tv_template)
da7f033d
HX
3133 }
3134 }, {
3135 .alg = "hmac(rmd160)",
3136 .test = alg_test_hash,
3137 .suite = {
21c8e720 3138 .hash = __VECS(hmac_rmd160_tv_template)
da7f033d
HX
3139 }
3140 }, {
3141 .alg = "hmac(sha1)",
3142 .test = alg_test_hash,
a1915d51 3143 .fips_allowed = 1,
da7f033d 3144 .suite = {
21c8e720 3145 .hash = __VECS(hmac_sha1_tv_template)
da7f033d
HX
3146 }
3147 }, {
3148 .alg = "hmac(sha224)",
3149 .test = alg_test_hash,
a1915d51 3150 .fips_allowed = 1,
da7f033d 3151 .suite = {
21c8e720 3152 .hash = __VECS(hmac_sha224_tv_template)
da7f033d
HX
3153 }
3154 }, {
3155 .alg = "hmac(sha256)",
3156 .test = alg_test_hash,
a1915d51 3157 .fips_allowed = 1,
da7f033d 3158 .suite = {
21c8e720 3159 .hash = __VECS(hmac_sha256_tv_template)
da7f033d 3160 }
98eca72f 3161 }, {
3162 .alg = "hmac(sha3-224)",
3163 .test = alg_test_hash,
3164 .fips_allowed = 1,
3165 .suite = {
21c8e720 3166 .hash = __VECS(hmac_sha3_224_tv_template)
98eca72f 3167 }
3168 }, {
3169 .alg = "hmac(sha3-256)",
3170 .test = alg_test_hash,
3171 .fips_allowed = 1,
3172 .suite = {
21c8e720 3173 .hash = __VECS(hmac_sha3_256_tv_template)
98eca72f 3174 }
3175 }, {
3176 .alg = "hmac(sha3-384)",
3177 .test = alg_test_hash,
3178 .fips_allowed = 1,
3179 .suite = {
21c8e720 3180 .hash = __VECS(hmac_sha3_384_tv_template)
98eca72f 3181 }
3182 }, {
3183 .alg = "hmac(sha3-512)",
3184 .test = alg_test_hash,
3185 .fips_allowed = 1,
3186 .suite = {
21c8e720 3187 .hash = __VECS(hmac_sha3_512_tv_template)
98eca72f 3188 }
da7f033d
HX
3189 }, {
3190 .alg = "hmac(sha384)",
3191 .test = alg_test_hash,
a1915d51 3192 .fips_allowed = 1,
da7f033d 3193 .suite = {
21c8e720 3194 .hash = __VECS(hmac_sha384_tv_template)
da7f033d
HX
3195 }
3196 }, {
3197 .alg = "hmac(sha512)",
3198 .test = alg_test_hash,
a1915d51 3199 .fips_allowed = 1,
da7f033d 3200 .suite = {
21c8e720 3201 .hash = __VECS(hmac_sha512_tv_template)
da7f033d 3202 }
25a0b9d4
VC
3203 }, {
3204 .alg = "hmac(streebog256)",
3205 .test = alg_test_hash,
3206 .suite = {
3207 .hash = __VECS(hmac_streebog256_tv_template)
3208 }
3209 }, {
3210 .alg = "hmac(streebog512)",
3211 .test = alg_test_hash,
3212 .suite = {
3213 .hash = __VECS(hmac_streebog512_tv_template)
3214 }
bb5530e4
SM
3215 }, {
3216 .alg = "jitterentropy_rng",
3217 .fips_allowed = 1,
3218 .test = alg_test_null,
35351988
SM
3219 }, {
3220 .alg = "kw(aes)",
3221 .test = alg_test_skcipher,
3222 .fips_allowed = 1,
3223 .suite = {
92a4c9fe 3224 .cipher = __VECS(aes_kw_tv_template)
35351988 3225 }
da7f033d
HX
3226 }, {
3227 .alg = "lrw(aes)",
1aa4ecd9 3228 .test = alg_test_skcipher,
da7f033d 3229 .suite = {
92a4c9fe 3230 .cipher = __VECS(aes_lrw_tv_template)
da7f033d 3231 }
0840605e
JK
3232 }, {
3233 .alg = "lrw(camellia)",
3234 .test = alg_test_skcipher,
3235 .suite = {
92a4c9fe 3236 .cipher = __VECS(camellia_lrw_tv_template)
0840605e 3237 }
9b8b0405
JG
3238 }, {
3239 .alg = "lrw(cast6)",
3240 .test = alg_test_skcipher,
3241 .suite = {
92a4c9fe 3242 .cipher = __VECS(cast6_lrw_tv_template)
9b8b0405 3243 }
d7bfc0fa
JK
3244 }, {
3245 .alg = "lrw(serpent)",
3246 .test = alg_test_skcipher,
3247 .suite = {
92a4c9fe 3248 .cipher = __VECS(serpent_lrw_tv_template)
d7bfc0fa 3249 }
0b2a1551
JK
3250 }, {
3251 .alg = "lrw(twofish)",
3252 .test = alg_test_skcipher,
3253 .suite = {
92a4c9fe 3254 .cipher = __VECS(tf_lrw_tv_template)
0b2a1551 3255 }
1443cc9b
KK
3256 }, {
3257 .alg = "lz4",
3258 .test = alg_test_comp,
3259 .fips_allowed = 1,
3260 .suite = {
3261 .comp = {
21c8e720
AB
3262 .comp = __VECS(lz4_comp_tv_template),
3263 .decomp = __VECS(lz4_decomp_tv_template)
1443cc9b
KK
3264 }
3265 }
3266 }, {
3267 .alg = "lz4hc",
3268 .test = alg_test_comp,
3269 .fips_allowed = 1,
3270 .suite = {
3271 .comp = {
21c8e720
AB
3272 .comp = __VECS(lz4hc_comp_tv_template),
3273 .decomp = __VECS(lz4hc_decomp_tv_template)
1443cc9b
KK
3274 }
3275 }
da7f033d
HX
3276 }, {
3277 .alg = "lzo",
3278 .test = alg_test_comp,
0818904d 3279 .fips_allowed = 1,
da7f033d
HX
3280 .suite = {
3281 .comp = {
21c8e720
AB
3282 .comp = __VECS(lzo_comp_tv_template),
3283 .decomp = __VECS(lzo_decomp_tv_template)
da7f033d
HX
3284 }
3285 }
3286 }, {
3287 .alg = "md4",
3288 .test = alg_test_hash,
3289 .suite = {
21c8e720 3290 .hash = __VECS(md4_tv_template)
da7f033d
HX
3291 }
3292 }, {
3293 .alg = "md5",
3294 .test = alg_test_hash,
3295 .suite = {
21c8e720 3296 .hash = __VECS(md5_tv_template)
da7f033d
HX
3297 }
3298 }, {
3299 .alg = "michael_mic",
3300 .test = alg_test_hash,
3301 .suite = {
21c8e720 3302 .hash = __VECS(michael_mic_tv_template)
da7f033d 3303 }
4feb4c59
OM
3304 }, {
3305 .alg = "morus1280",
3306 .test = alg_test_aead,
3307 .suite = {
a0d608ee 3308 .aead = __VECS(morus1280_tv_template)
4feb4c59
OM
3309 }
3310 }, {
3311 .alg = "morus640",
3312 .test = alg_test_aead,
3313 .suite = {
a0d608ee 3314 .aead = __VECS(morus640_tv_template)
4feb4c59 3315 }
26609a21
EB
3316 }, {
3317 .alg = "nhpoly1305",
3318 .test = alg_test_hash,
3319 .suite = {
3320 .hash = __VECS(nhpoly1305_tv_template)
3321 }
ba0e14ac
PS
3322 }, {
3323 .alg = "ofb(aes)",
3324 .test = alg_test_skcipher,
3325 .fips_allowed = 1,
3326 .suite = {
92a4c9fe 3327 .cipher = __VECS(aes_ofb_tv_template)
ba0e14ac 3328 }
a794d8d8
GBY
3329 }, {
3330 /* Same as ofb(aes) except the key is stored in
3331 * hardware secure memory which we reference by index
3332 */
3333 .alg = "ofb(paes)",
3334 .test = alg_test_null,
3335 .fips_allowed = 1,
da7f033d
HX
3336 }, {
3337 .alg = "pcbc(fcrypt)",
1aa4ecd9 3338 .test = alg_test_skcipher,
da7f033d 3339 .suite = {
92a4c9fe 3340 .cipher = __VECS(fcrypt_pcbc_tv_template)
da7f033d 3341 }
1207107c
SM
3342 }, {
3343 .alg = "pkcs1pad(rsa,sha224)",
3344 .test = alg_test_null,
3345 .fips_allowed = 1,
3346 }, {
3347 .alg = "pkcs1pad(rsa,sha256)",
3348 .test = alg_test_akcipher,
3349 .fips_allowed = 1,
3350 .suite = {
3351 .akcipher = __VECS(pkcs1pad_rsa_tv_template)
3352 }
3353 }, {
3354 .alg = "pkcs1pad(rsa,sha384)",
3355 .test = alg_test_null,
3356 .fips_allowed = 1,
3357 }, {
3358 .alg = "pkcs1pad(rsa,sha512)",
3359 .test = alg_test_null,
3360 .fips_allowed = 1,
eee9dc61
MW
3361 }, {
3362 .alg = "poly1305",
3363 .test = alg_test_hash,
3364 .suite = {
21c8e720 3365 .hash = __VECS(poly1305_tv_template)
eee9dc61 3366 }
da7f033d
HX
3367 }, {
3368 .alg = "rfc3686(ctr(aes))",
1aa4ecd9 3369 .test = alg_test_skcipher,
a1915d51 3370 .fips_allowed = 1,
da7f033d 3371 .suite = {
92a4c9fe 3372 .cipher = __VECS(aes_ctr_rfc3686_tv_template)
da7f033d 3373 }
5d667322 3374 }, {
3f31a740 3375 .alg = "rfc4106(gcm(aes))",
69435b94 3376 .test = alg_test_aead,
db71f29a 3377 .fips_allowed = 1,
69435b94 3378 .suite = {
a0d608ee 3379 .aead = __VECS(aes_gcm_rfc4106_tv_template)
69435b94
AH
3380 }
3381 }, {
544c436a 3382 .alg = "rfc4309(ccm(aes))",
5d667322 3383 .test = alg_test_aead,
a1915d51 3384 .fips_allowed = 1,
5d667322 3385 .suite = {
a0d608ee 3386 .aead = __VECS(aes_ccm_rfc4309_tv_template)
5d667322 3387 }
e9b7441a 3388 }, {
bb68745e 3389 .alg = "rfc4543(gcm(aes))",
e9b7441a
JK
3390 .test = alg_test_aead,
3391 .suite = {
a0d608ee 3392 .aead = __VECS(aes_gcm_rfc4543_tv_template)
e9b7441a 3393 }
af2b76b5
MW
3394 }, {
3395 .alg = "rfc7539(chacha20,poly1305)",
3396 .test = alg_test_aead,
3397 .suite = {
a0d608ee 3398 .aead = __VECS(rfc7539_tv_template)
af2b76b5 3399 }
5900758d
MW
3400 }, {
3401 .alg = "rfc7539esp(chacha20,poly1305)",
3402 .test = alg_test_aead,
3403 .suite = {
a0d608ee 3404 .aead = __VECS(rfc7539esp_tv_template)
5900758d 3405 }
da7f033d
HX
3406 }, {
3407 .alg = "rmd128",
3408 .test = alg_test_hash,
3409 .suite = {
21c8e720 3410 .hash = __VECS(rmd128_tv_template)
da7f033d
HX
3411 }
3412 }, {
3413 .alg = "rmd160",
3414 .test = alg_test_hash,
3415 .suite = {
21c8e720 3416 .hash = __VECS(rmd160_tv_template)
da7f033d
HX
3417 }
3418 }, {
3419 .alg = "rmd256",
3420 .test = alg_test_hash,
3421 .suite = {
21c8e720 3422 .hash = __VECS(rmd256_tv_template)
da7f033d
HX
3423 }
3424 }, {
3425 .alg = "rmd320",
3426 .test = alg_test_hash,
3427 .suite = {
21c8e720 3428 .hash = __VECS(rmd320_tv_template)
da7f033d 3429 }
946cc463
TS
3430 }, {
3431 .alg = "rsa",
3432 .test = alg_test_akcipher,
3433 .fips_allowed = 1,
3434 .suite = {
21c8e720 3435 .akcipher = __VECS(rsa_tv_template)
946cc463 3436 }
da7f033d
HX
3437 }, {
3438 .alg = "salsa20",
1aa4ecd9 3439 .test = alg_test_skcipher,
da7f033d 3440 .suite = {
92a4c9fe 3441 .cipher = __VECS(salsa20_stream_tv_template)
da7f033d
HX
3442 }
3443 }, {
3444 .alg = "sha1",
3445 .test = alg_test_hash,
a1915d51 3446 .fips_allowed = 1,
da7f033d 3447 .suite = {
21c8e720 3448 .hash = __VECS(sha1_tv_template)
da7f033d
HX
3449 }
3450 }, {
3451 .alg = "sha224",
3452 .test = alg_test_hash,
a1915d51 3453 .fips_allowed = 1,
da7f033d 3454 .suite = {
21c8e720 3455 .hash = __VECS(sha224_tv_template)
da7f033d
HX
3456 }
3457 }, {
3458 .alg = "sha256",
3459 .test = alg_test_hash,
a1915d51 3460 .fips_allowed = 1,
da7f033d 3461 .suite = {
21c8e720 3462 .hash = __VECS(sha256_tv_template)
da7f033d 3463 }
79cc6ab8 3464 }, {
3465 .alg = "sha3-224",
3466 .test = alg_test_hash,
3467 .fips_allowed = 1,
3468 .suite = {
21c8e720 3469 .hash = __VECS(sha3_224_tv_template)
79cc6ab8 3470 }
3471 }, {
3472 .alg = "sha3-256",
3473 .test = alg_test_hash,
3474 .fips_allowed = 1,
3475 .suite = {
21c8e720 3476 .hash = __VECS(sha3_256_tv_template)
79cc6ab8 3477 }
3478 }, {
3479 .alg = "sha3-384",
3480 .test = alg_test_hash,
3481 .fips_allowed = 1,
3482 .suite = {
21c8e720 3483 .hash = __VECS(sha3_384_tv_template)
79cc6ab8 3484 }
3485 }, {
3486 .alg = "sha3-512",
3487 .test = alg_test_hash,
3488 .fips_allowed = 1,
3489 .suite = {
21c8e720 3490 .hash = __VECS(sha3_512_tv_template)
79cc6ab8 3491 }
da7f033d
HX
3492 }, {
3493 .alg = "sha384",
3494 .test = alg_test_hash,
a1915d51 3495 .fips_allowed = 1,
da7f033d 3496 .suite = {
21c8e720 3497 .hash = __VECS(sha384_tv_template)
da7f033d
HX
3498 }
3499 }, {
3500 .alg = "sha512",
3501 .test = alg_test_hash,
a1915d51 3502 .fips_allowed = 1,
da7f033d 3503 .suite = {
21c8e720 3504 .hash = __VECS(sha512_tv_template)
da7f033d 3505 }
b7e27530
GBY
3506 }, {
3507 .alg = "sm3",
3508 .test = alg_test_hash,
3509 .suite = {
3510 .hash = __VECS(sm3_tv_template)
3511 }
25a0b9d4
VC
3512 }, {
3513 .alg = "streebog256",
3514 .test = alg_test_hash,
3515 .suite = {
3516 .hash = __VECS(streebog256_tv_template)
3517 }
3518 }, {
3519 .alg = "streebog512",
3520 .test = alg_test_hash,
3521 .suite = {
3522 .hash = __VECS(streebog512_tv_template)
3523 }
da7f033d
HX
3524 }, {
3525 .alg = "tgr128",
3526 .test = alg_test_hash,
3527 .suite = {
21c8e720 3528 .hash = __VECS(tgr128_tv_template)
da7f033d
HX
3529 }
3530 }, {
3531 .alg = "tgr160",
3532 .test = alg_test_hash,
3533 .suite = {
21c8e720 3534 .hash = __VECS(tgr160_tv_template)
da7f033d
HX
3535 }
3536 }, {
3537 .alg = "tgr192",
3538 .test = alg_test_hash,
3539 .suite = {
21c8e720 3540 .hash = __VECS(tgr192_tv_template)
da7f033d 3541 }
ed331ada
EB
3542 }, {
3543 .alg = "vmac64(aes)",
3544 .test = alg_test_hash,
3545 .suite = {
3546 .hash = __VECS(vmac64_aes_tv_template)
3547 }
da7f033d
HX
3548 }, {
3549 .alg = "wp256",
3550 .test = alg_test_hash,
3551 .suite = {
21c8e720 3552 .hash = __VECS(wp256_tv_template)
da7f033d
HX
3553 }
3554 }, {
3555 .alg = "wp384",
3556 .test = alg_test_hash,
3557 .suite = {
21c8e720 3558 .hash = __VECS(wp384_tv_template)
da7f033d
HX
3559 }
3560 }, {
3561 .alg = "wp512",
3562 .test = alg_test_hash,
3563 .suite = {
21c8e720 3564 .hash = __VECS(wp512_tv_template)
da7f033d
HX
3565 }
3566 }, {
3567 .alg = "xcbc(aes)",
3568 .test = alg_test_hash,
3569 .suite = {
21c8e720 3570 .hash = __VECS(aes_xcbc128_tv_template)
da7f033d 3571 }
aa762409
EB
3572 }, {
3573 .alg = "xchacha12",
3574 .test = alg_test_skcipher,
3575 .suite = {
3576 .cipher = __VECS(xchacha12_tv_template)
3577 },
de61d7ae
EB
3578 }, {
3579 .alg = "xchacha20",
3580 .test = alg_test_skcipher,
3581 .suite = {
3582 .cipher = __VECS(xchacha20_tv_template)
3583 },
da7f033d
HX
3584 }, {
3585 .alg = "xts(aes)",
1aa4ecd9 3586 .test = alg_test_skcipher,
2918aa8d 3587 .fips_allowed = 1,
da7f033d 3588 .suite = {
92a4c9fe 3589 .cipher = __VECS(aes_xts_tv_template)
da7f033d 3590 }
0840605e
JK
3591 }, {
3592 .alg = "xts(camellia)",
3593 .test = alg_test_skcipher,
3594 .suite = {
92a4c9fe 3595 .cipher = __VECS(camellia_xts_tv_template)
0840605e 3596 }
9b8b0405
JG
3597 }, {
3598 .alg = "xts(cast6)",
3599 .test = alg_test_skcipher,
3600 .suite = {
92a4c9fe 3601 .cipher = __VECS(cast6_xts_tv_template)
9b8b0405 3602 }
15f47ce5
GBY
3603 }, {
3604 /* Same as xts(aes) except the key is stored in
3605 * hardware secure memory which we reference by index
3606 */
3607 .alg = "xts(paes)",
3608 .test = alg_test_null,
3609 .fips_allowed = 1,
18be20b9
JK
3610 }, {
3611 .alg = "xts(serpent)",
3612 .test = alg_test_skcipher,
3613 .suite = {
92a4c9fe 3614 .cipher = __VECS(serpent_xts_tv_template)
18be20b9 3615 }
aed265b9
JK
3616 }, {
3617 .alg = "xts(twofish)",
3618 .test = alg_test_skcipher,
3619 .suite = {
92a4c9fe 3620 .cipher = __VECS(tf_xts_tv_template)
aed265b9 3621 }
15f47ce5
GBY
3622 }, {
3623 .alg = "xts4096(paes)",
3624 .test = alg_test_null,
3625 .fips_allowed = 1,
3626 }, {
3627 .alg = "xts512(paes)",
3628 .test = alg_test_null,
3629 .fips_allowed = 1,
a368f43d
GC
3630 }, {
3631 .alg = "zlib-deflate",
3632 .test = alg_test_comp,
3633 .fips_allowed = 1,
3634 .suite = {
3635 .comp = {
3636 .comp = __VECS(zlib_deflate_comp_tv_template),
3637 .decomp = __VECS(zlib_deflate_decomp_tv_template)
3638 }
3639 }
d28fc3db
NT
3640 }, {
3641 .alg = "zstd",
3642 .test = alg_test_comp,
3643 .fips_allowed = 1,
3644 .suite = {
3645 .comp = {
3646 .comp = __VECS(zstd_comp_tv_template),
3647 .decomp = __VECS(zstd_decomp_tv_template)
3648 }
3649 }
da7f033d
HX
3650 }
3651};
3652
5714758b
JK
3653static bool alg_test_descs_checked;
3654
3655static void alg_test_descs_check_order(void)
3656{
3657 int i;
3658
3659 /* only check once */
3660 if (alg_test_descs_checked)
3661 return;
3662
3663 alg_test_descs_checked = true;
3664
3665 for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
3666 int diff = strcmp(alg_test_descs[i - 1].alg,
3667 alg_test_descs[i].alg);
3668
3669 if (WARN_ON(diff > 0)) {
3670 pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
3671 alg_test_descs[i - 1].alg,
3672 alg_test_descs[i].alg);
3673 }
3674
3675 if (WARN_ON(diff == 0)) {
3676 pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
3677 alg_test_descs[i].alg);
3678 }
3679 }
3680}
3681
1aa4ecd9 3682static int alg_find_test(const char *alg)
da7f033d
HX
3683{
3684 int start = 0;
3685 int end = ARRAY_SIZE(alg_test_descs);
3686
3687 while (start < end) {
3688 int i = (start + end) / 2;
3689 int diff = strcmp(alg_test_descs[i].alg, alg);
3690
3691 if (diff > 0) {
3692 end = i;
3693 continue;
3694 }
3695
3696 if (diff < 0) {
3697 start = i + 1;
3698 continue;
3699 }
3700
1aa4ecd9
HX
3701 return i;
3702 }
3703
3704 return -1;
3705}
3706
3707int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
3708{
3709 int i;
a68f6610 3710 int j;
d12d6b6d 3711 int rc;
1aa4ecd9 3712
9e5c9fe4
RJ
3713 if (!fips_enabled && notests) {
3714 printk_once(KERN_INFO "alg: self-tests disabled\n");
3715 return 0;
3716 }
3717
5714758b
JK
3718 alg_test_descs_check_order();
3719
1aa4ecd9
HX
3720 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
3721 char nalg[CRYPTO_MAX_ALG_NAME];
3722
3723 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
3724 sizeof(nalg))
3725 return -ENAMETOOLONG;
3726
3727 i = alg_find_test(nalg);
3728 if (i < 0)
3729 goto notest;
3730
a3bef3a3
JW
3731 if (fips_enabled && !alg_test_descs[i].fips_allowed)
3732 goto non_fips_alg;
3733
941fb328
JW
3734 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
3735 goto test_done;
da7f033d
HX
3736 }
3737
1aa4ecd9 3738 i = alg_find_test(alg);
a68f6610
HX
3739 j = alg_find_test(driver);
3740 if (i < 0 && j < 0)
1aa4ecd9
HX
3741 goto notest;
3742
a68f6610
HX
3743 if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
3744 (j >= 0 && !alg_test_descs[j].fips_allowed)))
a3bef3a3
JW
3745 goto non_fips_alg;
3746
a68f6610
HX
3747 rc = 0;
3748 if (i >= 0)
3749 rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
3750 type, mask);
032c8cac 3751 if (j >= 0 && j != i)
a68f6610
HX
3752 rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
3753 type, mask);
3754
941fb328 3755test_done:
d12d6b6d
NH
3756 if (fips_enabled && rc)
3757 panic("%s: %s alg self test failed in fips mode!\n", driver, alg);
3758
29ecd4ab 3759 if (fips_enabled && !rc)
3e8cffd4 3760 pr_info("alg: self-tests for %s (%s) passed\n", driver, alg);
29ecd4ab 3761
d12d6b6d 3762 return rc;
1aa4ecd9
HX
3763
3764notest:
da7f033d
HX
3765 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
3766 return 0;
a3bef3a3
JW
3767non_fips_alg:
3768 return -EINVAL;
da7f033d 3769}
0b767f96 3770
326a6346 3771#endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
0b767f96 3772
da7f033d 3773EXPORT_SYMBOL_GPL(alg_test);