blob: 09f2f0f582bf72a8bb72927ed2cd57b74f0e8bd5 [file] [log] [blame]
Herbert Xuda7f0332008-07-31 17:08:25 +08001/*
2 * Algorithm testing framework and tests.
3 *
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
6 * Copyright (c) 2007 Nokia Siemens Networks
7 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
8 *
Adrian Hoban69435b92010-11-04 15:02:04 -04009 * 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 *
Herbert Xuda7f0332008-07-31 17:08:25 +080016 * 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
Herbert Xu1ce33112015-04-22 15:06:31 +080023#include <crypto/aead.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080024#include <crypto/hash.h>
Herbert Xu12773d92015-08-20 15:21:46 +080025#include <crypto/skcipher.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080026#include <linux/err.h>
Herbert Xu1c41b882015-04-22 13:25:58 +080027#include <linux/fips.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080028#include <linux/module.h>
29#include <linux/scatterlist.h>
30#include <linux/slab.h>
31#include <linux/string.h>
Jarod Wilson7647d6c2009-05-04 19:44:50 +080032#include <crypto/rng.h>
Stephan Mueller64d1cdf2014-05-31 17:25:36 +020033#include <crypto/drbg.h>
Tadeusz Struk946cc462015-06-16 10:31:06 -070034#include <crypto/akcipher.h>
Salvatore Benedetto802c7f12016-06-22 17:49:14 +010035#include <crypto/kpp.h>
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +010036#include <crypto/acompress.h>
Herbert Xuda7f0332008-07-31 17:08:25 +080037
38#include "internal.h"
Alexander Shishkin0b767f92010-06-03 20:53:43 +100039
Richard W.M. Jones9e5c9fe2016-05-03 10:00:17 +010040static bool notests;
41module_param(notests, bool, 0644);
42MODULE_PARM_DESC(notests, "disable crypto self-tests");
43
Herbert Xu326a6342010-08-06 09:40:28 +080044#ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
Alexander Shishkin0b767f92010-06-03 20:53:43 +100045
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
Herbert Xuda7f0332008-07-31 17:08:25 +080054#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
Ard Biesheuvel04b46fb2016-12-08 08:23:52 +000066#define IDX3 1511
Herbert Xuda7f0332008-07-31 17:08:25 +080067#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
Herbert Xuda7f0332008-07-31 17:08:25 +080079struct aead_test_suite {
80 struct {
Eric Biggersb13b1e02017-02-24 15:46:59 -080081 const struct aead_testvec *vecs;
Herbert Xuda7f0332008-07-31 17:08:25 +080082 unsigned int count;
83 } enc, dec;
84};
85
86struct cipher_test_suite {
Eric Biggers92a4c9f2018-05-20 22:50:29 -070087 const struct cipher_testvec *vecs;
88 unsigned int count;
Herbert Xuda7f0332008-07-31 17:08:25 +080089};
90
91struct comp_test_suite {
92 struct {
Eric Biggersb13b1e02017-02-24 15:46:59 -080093 const struct comp_testvec *vecs;
Herbert Xuda7f0332008-07-31 17:08:25 +080094 unsigned int count;
95 } comp, decomp;
96};
97
98struct hash_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -080099 const struct hash_testvec *vecs;
Herbert Xuda7f0332008-07-31 17:08:25 +0800100 unsigned int count;
101};
102
Jarod Wilson7647d6c2009-05-04 19:44:50 +0800103struct cprng_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800104 const struct cprng_testvec *vecs;
Jarod Wilson7647d6c2009-05-04 19:44:50 +0800105 unsigned int count;
106};
107
Stephan Mueller64d1cdf2014-05-31 17:25:36 +0200108struct drbg_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800109 const struct drbg_testvec *vecs;
Stephan Mueller64d1cdf2014-05-31 17:25:36 +0200110 unsigned int count;
111};
112
Tadeusz Struk946cc462015-06-16 10:31:06 -0700113struct akcipher_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800114 const struct akcipher_testvec *vecs;
Tadeusz Struk946cc462015-06-16 10:31:06 -0700115 unsigned int count;
116};
117
Salvatore Benedetto802c7f12016-06-22 17:49:14 +0100118struct kpp_test_suite {
Eric Biggersb13b1e02017-02-24 15:46:59 -0800119 const struct kpp_testvec *vecs;
Salvatore Benedetto802c7f12016-06-22 17:49:14 +0100120 unsigned int count;
121};
122
Herbert Xuda7f0332008-07-31 17:08:25 +0800123struct alg_test_desc {
124 const char *alg;
125 int (*test)(const struct alg_test_desc *desc, const char *driver,
126 u32 type, u32 mask);
Jarod Wilsona1915d52009-05-15 15:16:03 +1000127 int fips_allowed; /* set if alg is allowed in fips mode */
Herbert Xuda7f0332008-07-31 17:08:25 +0800128
129 union {
130 struct aead_test_suite aead;
131 struct cipher_test_suite cipher;
132 struct comp_test_suite comp;
133 struct hash_test_suite hash;
Jarod Wilson7647d6c2009-05-04 19:44:50 +0800134 struct cprng_test_suite cprng;
Stephan Mueller64d1cdf2014-05-31 17:25:36 +0200135 struct drbg_test_suite drbg;
Tadeusz Struk946cc462015-06-16 10:31:06 -0700136 struct akcipher_test_suite akcipher;
Salvatore Benedetto802c7f12016-06-22 17:49:14 +0100137 struct kpp_test_suite kpp;
Herbert Xuda7f0332008-07-31 17:08:25 +0800138 } suite;
139};
140
Eric Biggersb13b1e02017-02-24 15:46:59 -0800141static const unsigned int IDX[8] = {
142 IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
Herbert Xuda7f0332008-07-31 17:08:25 +0800143
Herbert Xuda7f0332008-07-31 17:08:25 +0800144static void hexdump(unsigned char *buf, unsigned int len)
145{
146 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
147 16, 1,
148 buf, len, false);
149}
150
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800151static int testmgr_alloc_buf(char *buf[XBUFSIZE])
152{
153 int i;
154
155 for (i = 0; i < XBUFSIZE; i++) {
156 buf[i] = (void *)__get_free_page(GFP_KERNEL);
157 if (!buf[i])
158 goto err_free_buf;
159 }
160
161 return 0;
162
163err_free_buf:
164 while (i-- > 0)
165 free_page((unsigned long)buf[i]);
166
167 return -ENOMEM;
168}
169
170static void testmgr_free_buf(char *buf[XBUFSIZE])
171{
172 int i;
173
174 for (i = 0; i < XBUFSIZE; i++)
175 free_page((unsigned long)buf[i]);
176}
177
Kamil Konieczny466d7b92018-01-16 15:26:13 +0100178static int ahash_guard_result(char *result, char c, int size)
179{
180 int i;
181
182 for (i = 0; i < size; i++) {
183 if (result[i] != c)
184 return -EINVAL;
185 }
186
187 return 0;
188}
189
Wang, Rui Y018ba952016-02-03 18:26:57 +0800190static int ahash_partial_update(struct ahash_request **preq,
Eric Biggersb13b1e02017-02-24 15:46:59 -0800191 struct crypto_ahash *tfm, const struct hash_testvec *template,
Wang, Rui Y018ba952016-02-03 18:26:57 +0800192 void *hash_buff, int k, int temp, struct scatterlist *sg,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100193 const char *algo, char *result, struct crypto_wait *wait)
Wang, Rui Y018ba952016-02-03 18:26:57 +0800194{
195 char *state;
196 struct ahash_request *req;
197 int statesize, ret = -EINVAL;
Joey Pabalinasda1729c2018-01-01 10:40:14 -1000198 static const unsigned char guard[] = { 0x00, 0xba, 0xad, 0x00 };
Kamil Konieczny466d7b92018-01-16 15:26:13 +0100199 int digestsize = crypto_ahash_digestsize(tfm);
Wang, Rui Y018ba952016-02-03 18:26:57 +0800200
201 req = *preq;
202 statesize = crypto_ahash_statesize(
203 crypto_ahash_reqtfm(req));
Jan Stancek7bcb87b2016-09-28 16:38:37 +0200204 state = kmalloc(statesize + sizeof(guard), GFP_KERNEL);
Wang, Rui Y018ba952016-02-03 18:26:57 +0800205 if (!state) {
Gilad Ben-Yossefcf3f9602017-06-05 08:33:43 +0300206 pr_err("alg: hash: Failed to alloc state for %s\n", algo);
Wang, Rui Y018ba952016-02-03 18:26:57 +0800207 goto out_nostate;
208 }
Jan Stancek7bcb87b2016-09-28 16:38:37 +0200209 memcpy(state + statesize, guard, sizeof(guard));
Kamil Konieczny466d7b92018-01-16 15:26:13 +0100210 memset(result, 1, digestsize);
Wang, Rui Y018ba952016-02-03 18:26:57 +0800211 ret = crypto_ahash_export(req, state);
Jan Stancek7bcb87b2016-09-28 16:38:37 +0200212 WARN_ON(memcmp(state + statesize, guard, sizeof(guard)));
Wang, Rui Y018ba952016-02-03 18:26:57 +0800213 if (ret) {
Gilad Ben-Yossefcf3f9602017-06-05 08:33:43 +0300214 pr_err("alg: hash: Failed to export() for %s\n", algo);
Wang, Rui Y018ba952016-02-03 18:26:57 +0800215 goto out;
216 }
Kamil Konieczny466d7b92018-01-16 15:26:13 +0100217 ret = ahash_guard_result(result, 1, digestsize);
218 if (ret) {
219 pr_err("alg: hash: Failed, export used req->result for %s\n",
220 algo);
221 goto out;
222 }
Wang, Rui Y018ba952016-02-03 18:26:57 +0800223 ahash_request_free(req);
224 req = ahash_request_alloc(tfm, GFP_KERNEL);
225 if (!req) {
226 pr_err("alg: hash: Failed to alloc request for %s\n", algo);
227 goto out_noreq;
228 }
229 ahash_request_set_callback(req,
230 CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100231 crypto_req_done, wait);
Wang, Rui Y018ba952016-02-03 18:26:57 +0800232
233 memcpy(hash_buff, template->plaintext + temp,
234 template->tap[k]);
235 sg_init_one(&sg[0], hash_buff, template->tap[k]);
236 ahash_request_set_crypt(req, sg, result, template->tap[k]);
237 ret = crypto_ahash_import(req, state);
238 if (ret) {
239 pr_err("alg: hash: Failed to import() for %s\n", algo);
240 goto out;
241 }
Kamil Konieczny466d7b92018-01-16 15:26:13 +0100242 ret = ahash_guard_result(result, 1, digestsize);
243 if (ret) {
244 pr_err("alg: hash: Failed, import used req->result for %s\n",
245 algo);
246 goto out;
247 }
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100248 ret = crypto_wait_req(crypto_ahash_update(req), wait);
Wang, Rui Y018ba952016-02-03 18:26:57 +0800249 if (ret)
250 goto out;
251 *preq = req;
252 ret = 0;
253 goto out_noreq;
254out:
255 ahash_request_free(req);
256out_noreq:
257 kfree(state);
258out_nostate:
259 return ret;
260}
261
Gilad Ben-Yossef76715092018-07-01 08:02:35 +0100262enum hash_test {
263 HASH_TEST_DIGEST,
264 HASH_TEST_FINAL,
265 HASH_TEST_FINUP
266};
267
Eric Biggersb13b1e02017-02-24 15:46:59 -0800268static int __test_hash(struct crypto_ahash *tfm,
269 const struct hash_testvec *template, unsigned int tcount,
Gilad Ben-Yossef76715092018-07-01 08:02:35 +0100270 enum hash_test test_type, const int align_offset)
Herbert Xuda7f0332008-07-31 17:08:25 +0800271{
272 const char *algo = crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
Andrew Lutomirskie93acd62017-01-10 15:24:46 -0800273 size_t digest_size = crypto_ahash_digestsize(tfm);
Herbert Xuda7f0332008-07-31 17:08:25 +0800274 unsigned int i, j, k, temp;
275 struct scatterlist sg[8];
Horia Geanta29b77e52014-07-23 11:59:38 +0300276 char *result;
277 char *key;
Herbert Xuda7f0332008-07-31 17:08:25 +0800278 struct ahash_request *req;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100279 struct crypto_wait wait;
Herbert Xuda7f0332008-07-31 17:08:25 +0800280 void *hash_buff;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800281 char *xbuf[XBUFSIZE];
282 int ret = -ENOMEM;
283
Andrew Lutomirskie93acd62017-01-10 15:24:46 -0800284 result = kmalloc(digest_size, GFP_KERNEL);
Horia Geanta29b77e52014-07-23 11:59:38 +0300285 if (!result)
286 return ret;
287 key = kmalloc(MAX_KEYLEN, GFP_KERNEL);
288 if (!key)
289 goto out_nobuf;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800290 if (testmgr_alloc_buf(xbuf))
291 goto out_nobuf;
Herbert Xuda7f0332008-07-31 17:08:25 +0800292
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100293 crypto_init_wait(&wait);
Herbert Xuda7f0332008-07-31 17:08:25 +0800294
295 req = ahash_request_alloc(tfm, GFP_KERNEL);
296 if (!req) {
297 printk(KERN_ERR "alg: hash: Failed to allocate request for "
298 "%s\n", algo);
Herbert Xuda7f0332008-07-31 17:08:25 +0800299 goto out_noreq;
300 }
301 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100302 crypto_req_done, &wait);
Herbert Xuda7f0332008-07-31 17:08:25 +0800303
Herbert Xua0cfae52009-05-29 16:23:12 +1000304 j = 0;
Herbert Xuda7f0332008-07-31 17:08:25 +0800305 for (i = 0; i < tcount; i++) {
Herbert Xua0cfae52009-05-29 16:23:12 +1000306 if (template[i].np)
307 continue;
308
Jussi Kivilinnada5ffe12013-06-13 17:37:55 +0300309 ret = -EINVAL;
310 if (WARN_ON(align_offset + template[i].psize > PAGE_SIZE))
311 goto out;
312
Herbert Xua0cfae52009-05-29 16:23:12 +1000313 j++;
Andrew Lutomirskie93acd62017-01-10 15:24:46 -0800314 memset(result, 0, digest_size);
Herbert Xuda7f0332008-07-31 17:08:25 +0800315
316 hash_buff = xbuf[0];
Jussi Kivilinnada5ffe12013-06-13 17:37:55 +0300317 hash_buff += align_offset;
Herbert Xuda7f0332008-07-31 17:08:25 +0800318
319 memcpy(hash_buff, template[i].plaintext, template[i].psize);
320 sg_init_one(&sg[0], hash_buff, template[i].psize);
321
322 if (template[i].ksize) {
323 crypto_ahash_clear_flags(tfm, ~0);
Horia Geanta29b77e52014-07-23 11:59:38 +0300324 if (template[i].ksize > MAX_KEYLEN) {
325 pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
326 j, algo, template[i].ksize, MAX_KEYLEN);
327 ret = -EINVAL;
328 goto out;
329 }
330 memcpy(key, template[i].key, template[i].ksize);
331 ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
Herbert Xuda7f0332008-07-31 17:08:25 +0800332 if (ret) {
333 printk(KERN_ERR "alg: hash: setkey failed on "
Herbert Xua0cfae52009-05-29 16:23:12 +1000334 "test %d for %s: ret=%d\n", j, algo,
Herbert Xuda7f0332008-07-31 17:08:25 +0800335 -ret);
336 goto out;
337 }
338 }
339
340 ahash_request_set_crypt(req, sg, result, template[i].psize);
Gilad Ben-Yossef76715092018-07-01 08:02:35 +0100341 switch (test_type) {
342 case HASH_TEST_DIGEST:
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100343 ret = crypto_wait_req(crypto_ahash_digest(req), &wait);
David S. Millera8f1a052010-05-19 14:12:03 +1000344 if (ret) {
345 pr_err("alg: hash: digest failed on test %d "
346 "for %s: ret=%d\n", j, algo, -ret);
347 goto out;
Herbert Xuda7f0332008-07-31 17:08:25 +0800348 }
Gilad Ben-Yossef76715092018-07-01 08:02:35 +0100349 break;
350
351 case HASH_TEST_FINAL:
Kamil Konieczny466d7b92018-01-16 15:26:13 +0100352 memset(result, 1, digest_size);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100353 ret = crypto_wait_req(crypto_ahash_init(req), &wait);
David S. Millera8f1a052010-05-19 14:12:03 +1000354 if (ret) {
Gilad Ben-Yossefcf3f9602017-06-05 08:33:43 +0300355 pr_err("alg: hash: init failed on test %d "
David S. Millera8f1a052010-05-19 14:12:03 +1000356 "for %s: ret=%d\n", j, algo, -ret);
357 goto out;
358 }
Kamil Konieczny466d7b92018-01-16 15:26:13 +0100359 ret = ahash_guard_result(result, 1, digest_size);
360 if (ret) {
361 pr_err("alg: hash: init failed on test %d "
362 "for %s: used req->result\n", j, algo);
363 goto out;
364 }
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100365 ret = crypto_wait_req(crypto_ahash_update(req), &wait);
David S. Millera8f1a052010-05-19 14:12:03 +1000366 if (ret) {
Gilad Ben-Yossefcf3f9602017-06-05 08:33:43 +0300367 pr_err("alg: hash: update failed on test %d "
David S. Millera8f1a052010-05-19 14:12:03 +1000368 "for %s: ret=%d\n", j, algo, -ret);
369 goto out;
370 }
Kamil Konieczny466d7b92018-01-16 15:26:13 +0100371 ret = ahash_guard_result(result, 1, digest_size);
372 if (ret) {
373 pr_err("alg: hash: update failed on test %d "
374 "for %s: used req->result\n", j, algo);
375 goto out;
376 }
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100377 ret = crypto_wait_req(crypto_ahash_final(req), &wait);
David S. Millera8f1a052010-05-19 14:12:03 +1000378 if (ret) {
Gilad Ben-Yossefcf3f9602017-06-05 08:33:43 +0300379 pr_err("alg: hash: final failed on test %d "
David S. Millera8f1a052010-05-19 14:12:03 +1000380 "for %s: ret=%d\n", j, algo, -ret);
381 goto out;
382 }
Gilad Ben-Yossef76715092018-07-01 08:02:35 +0100383 break;
384
385 case HASH_TEST_FINUP:
386 memset(result, 1, digest_size);
387 ret = crypto_wait_req(crypto_ahash_init(req), &wait);
388 if (ret) {
389 pr_err("alg: hash: init failed on test %d "
390 "for %s: ret=%d\n", j, algo, -ret);
391 goto out;
392 }
393 ret = ahash_guard_result(result, 1, digest_size);
394 if (ret) {
395 pr_err("alg: hash: init failed on test %d "
396 "for %s: used req->result\n", j, algo);
397 goto out;
398 }
399 ret = crypto_wait_req(crypto_ahash_finup(req), &wait);
400 if (ret) {
401 pr_err("alg: hash: final failed on test %d "
402 "for %s: ret=%d\n", j, algo, -ret);
403 goto out;
404 }
405 break;
Herbert Xuda7f0332008-07-31 17:08:25 +0800406 }
407
408 if (memcmp(result, template[i].digest,
409 crypto_ahash_digestsize(tfm))) {
410 printk(KERN_ERR "alg: hash: Test %d failed for %s\n",
Herbert Xua0cfae52009-05-29 16:23:12 +1000411 j, algo);
Herbert Xuda7f0332008-07-31 17:08:25 +0800412 hexdump(result, crypto_ahash_digestsize(tfm));
413 ret = -EINVAL;
414 goto out;
415 }
416 }
417
Gilad Ben-Yossef76715092018-07-01 08:02:35 +0100418 if (test_type)
419 goto out;
420
Herbert Xuda7f0332008-07-31 17:08:25 +0800421 j = 0;
422 for (i = 0; i < tcount; i++) {
Jussi Kivilinnada5ffe12013-06-13 17:37:55 +0300423 /* alignment tests are only done with continuous buffers */
424 if (align_offset != 0)
425 break;
426
Cristian Stoica5f2b4242014-08-08 14:27:50 +0300427 if (!template[i].np)
428 continue;
Herbert Xuda7f0332008-07-31 17:08:25 +0800429
Cristian Stoica5f2b4242014-08-08 14:27:50 +0300430 j++;
Andrew Lutomirskie93acd62017-01-10 15:24:46 -0800431 memset(result, 0, digest_size);
Herbert Xuda7f0332008-07-31 17:08:25 +0800432
Cristian Stoica5f2b4242014-08-08 14:27:50 +0300433 temp = 0;
434 sg_init_table(sg, template[i].np);
435 ret = -EINVAL;
436 for (k = 0; k < template[i].np; k++) {
437 if (WARN_ON(offset_in_page(IDX[k]) +
438 template[i].tap[k] > PAGE_SIZE))
Herbert Xuda7f0332008-07-31 17:08:25 +0800439 goto out;
Cristian Stoica5f2b4242014-08-08 14:27:50 +0300440 sg_set_buf(&sg[k],
441 memcpy(xbuf[IDX[k] >> PAGE_SHIFT] +
442 offset_in_page(IDX[k]),
443 template[i].plaintext + temp,
444 template[i].tap[k]),
445 template[i].tap[k]);
446 temp += template[i].tap[k];
447 }
Herbert Xuda7f0332008-07-31 17:08:25 +0800448
Cristian Stoica5f2b4242014-08-08 14:27:50 +0300449 if (template[i].ksize) {
450 if (template[i].ksize > MAX_KEYLEN) {
451 pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
452 j, algo, template[i].ksize, MAX_KEYLEN);
Herbert Xuda7f0332008-07-31 17:08:25 +0800453 ret = -EINVAL;
454 goto out;
455 }
Cristian Stoica5f2b4242014-08-08 14:27:50 +0300456 crypto_ahash_clear_flags(tfm, ~0);
457 memcpy(key, template[i].key, template[i].ksize);
458 ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
459
460 if (ret) {
461 printk(KERN_ERR "alg: hash: setkey "
462 "failed on chunking test %d "
463 "for %s: ret=%d\n", j, algo, -ret);
464 goto out;
465 }
466 }
467
468 ahash_request_set_crypt(req, sg, result, template[i].psize);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100469 ret = crypto_wait_req(crypto_ahash_digest(req), &wait);
470 if (ret) {
471 pr_err("alg: hash: digest failed on chunking test %d for %s: ret=%d\n",
472 j, algo, -ret);
Cristian Stoica5f2b4242014-08-08 14:27:50 +0300473 goto out;
474 }
475
476 if (memcmp(result, template[i].digest,
477 crypto_ahash_digestsize(tfm))) {
478 printk(KERN_ERR "alg: hash: Chunking test %d "
479 "failed for %s\n", j, algo);
480 hexdump(result, crypto_ahash_digestsize(tfm));
481 ret = -EINVAL;
482 goto out;
Herbert Xuda7f0332008-07-31 17:08:25 +0800483 }
484 }
485
Wang, Rui Y018ba952016-02-03 18:26:57 +0800486 /* partial update exercise */
487 j = 0;
488 for (i = 0; i < tcount; i++) {
489 /* alignment tests are only done with continuous buffers */
490 if (align_offset != 0)
491 break;
492
493 if (template[i].np < 2)
494 continue;
495
496 j++;
Andrew Lutomirskie93acd62017-01-10 15:24:46 -0800497 memset(result, 0, digest_size);
Wang, Rui Y018ba952016-02-03 18:26:57 +0800498
499 ret = -EINVAL;
500 hash_buff = xbuf[0];
501 memcpy(hash_buff, template[i].plaintext,
502 template[i].tap[0]);
503 sg_init_one(&sg[0], hash_buff, template[i].tap[0]);
504
505 if (template[i].ksize) {
506 crypto_ahash_clear_flags(tfm, ~0);
507 if (template[i].ksize > MAX_KEYLEN) {
508 pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
509 j, algo, template[i].ksize, MAX_KEYLEN);
510 ret = -EINVAL;
511 goto out;
512 }
513 memcpy(key, template[i].key, template[i].ksize);
514 ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
515 if (ret) {
516 pr_err("alg: hash: setkey failed on test %d for %s: ret=%d\n",
517 j, algo, -ret);
518 goto out;
519 }
520 }
521
522 ahash_request_set_crypt(req, sg, result, template[i].tap[0]);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100523 ret = crypto_wait_req(crypto_ahash_init(req), &wait);
Wang, Rui Y018ba952016-02-03 18:26:57 +0800524 if (ret) {
Gilad Ben-Yossefcf3f9602017-06-05 08:33:43 +0300525 pr_err("alg: hash: init failed on test %d for %s: ret=%d\n",
Wang, Rui Y018ba952016-02-03 18:26:57 +0800526 j, algo, -ret);
527 goto out;
528 }
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100529 ret = crypto_wait_req(crypto_ahash_update(req), &wait);
Wang, Rui Y018ba952016-02-03 18:26:57 +0800530 if (ret) {
Gilad Ben-Yossefcf3f9602017-06-05 08:33:43 +0300531 pr_err("alg: hash: update failed on test %d for %s: ret=%d\n",
Wang, Rui Y018ba952016-02-03 18:26:57 +0800532 j, algo, -ret);
533 goto out;
534 }
535
536 temp = template[i].tap[0];
537 for (k = 1; k < template[i].np; k++) {
538 ret = ahash_partial_update(&req, tfm, &template[i],
539 hash_buff, k, temp, &sg[0], algo, result,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100540 &wait);
Wang, Rui Y018ba952016-02-03 18:26:57 +0800541 if (ret) {
Gilad Ben-Yossefcf3f9602017-06-05 08:33:43 +0300542 pr_err("alg: hash: partial update failed on test %d for %s: ret=%d\n",
Wang, Rui Y018ba952016-02-03 18:26:57 +0800543 j, algo, -ret);
544 goto out_noreq;
545 }
546 temp += template[i].tap[k];
547 }
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100548 ret = crypto_wait_req(crypto_ahash_final(req), &wait);
Wang, Rui Y018ba952016-02-03 18:26:57 +0800549 if (ret) {
Gilad Ben-Yossefcf3f9602017-06-05 08:33:43 +0300550 pr_err("alg: hash: final failed on test %d for %s: ret=%d\n",
Wang, Rui Y018ba952016-02-03 18:26:57 +0800551 j, algo, -ret);
552 goto out;
553 }
554 if (memcmp(result, template[i].digest,
555 crypto_ahash_digestsize(tfm))) {
556 pr_err("alg: hash: Partial Test %d failed for %s\n",
557 j, algo);
558 hexdump(result, crypto_ahash_digestsize(tfm));
559 ret = -EINVAL;
560 goto out;
561 }
562 }
563
Herbert Xuda7f0332008-07-31 17:08:25 +0800564 ret = 0;
565
566out:
567 ahash_request_free(req);
568out_noreq:
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800569 testmgr_free_buf(xbuf);
570out_nobuf:
Horia Geanta29b77e52014-07-23 11:59:38 +0300571 kfree(key);
572 kfree(result);
Herbert Xuda7f0332008-07-31 17:08:25 +0800573 return ret;
574}
575
Eric Biggersb13b1e02017-02-24 15:46:59 -0800576static int test_hash(struct crypto_ahash *tfm,
577 const struct hash_testvec *template,
Gilad Ben-Yossef76715092018-07-01 08:02:35 +0100578 unsigned int tcount, enum hash_test test_type)
Jussi Kivilinnada5ffe12013-06-13 17:37:55 +0300579{
580 unsigned int alignmask;
581 int ret;
582
Gilad Ben-Yossef76715092018-07-01 08:02:35 +0100583 ret = __test_hash(tfm, template, tcount, test_type, 0);
Jussi Kivilinnada5ffe12013-06-13 17:37:55 +0300584 if (ret)
585 return ret;
586
587 /* test unaligned buffers, check with one byte offset */
Gilad Ben-Yossef76715092018-07-01 08:02:35 +0100588 ret = __test_hash(tfm, template, tcount, test_type, 1);
Jussi Kivilinnada5ffe12013-06-13 17:37:55 +0300589 if (ret)
590 return ret;
591
592 alignmask = crypto_tfm_alg_alignmask(&tfm->base);
593 if (alignmask) {
594 /* Check if alignment mask for tfm is correctly set. */
Gilad Ben-Yossef76715092018-07-01 08:02:35 +0100595 ret = __test_hash(tfm, template, tcount, test_type,
Jussi Kivilinnada5ffe12013-06-13 17:37:55 +0300596 alignmask + 1);
597 if (ret)
598 return ret;
599 }
600
601 return 0;
602}
603
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +0300604static int __test_aead(struct crypto_aead *tfm, int enc,
Eric Biggersb13b1e02017-02-24 15:46:59 -0800605 const struct aead_testvec *template, unsigned int tcount,
Jussi Kivilinna58dcf542013-06-13 17:37:50 +0300606 const bool diff_dst, const int align_offset)
Herbert Xuda7f0332008-07-31 17:08:25 +0800607{
608 const char *algo = crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm));
609 unsigned int i, j, k, n, temp;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800610 int ret = -ENOMEM;
Herbert Xuda7f0332008-07-31 17:08:25 +0800611 char *q;
612 char *key;
613 struct aead_request *req;
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +0300614 struct scatterlist *sg;
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +0300615 struct scatterlist *sgout;
616 const char *e, *d;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100617 struct crypto_wait wait;
Cristian Stoica424a5da2015-01-28 11:03:05 +0200618 unsigned int authsize, iv_len;
Herbert Xuda7f0332008-07-31 17:08:25 +0800619 void *input;
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +0300620 void *output;
Herbert Xuda7f0332008-07-31 17:08:25 +0800621 void *assoc;
Tadeusz Struk9bac0192014-05-19 09:51:33 -0700622 char *iv;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800623 char *xbuf[XBUFSIZE];
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +0300624 char *xoutbuf[XBUFSIZE];
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800625 char *axbuf[XBUFSIZE];
626
Tadeusz Struk9bac0192014-05-19 09:51:33 -0700627 iv = kzalloc(MAX_IVLEN, GFP_KERNEL);
628 if (!iv)
629 return ret;
Horia Geanta29b77e52014-07-23 11:59:38 +0300630 key = kmalloc(MAX_KEYLEN, GFP_KERNEL);
631 if (!key)
632 goto out_noxbuf;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800633 if (testmgr_alloc_buf(xbuf))
634 goto out_noxbuf;
635 if (testmgr_alloc_buf(axbuf))
636 goto out_noaxbuf;
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +0300637 if (diff_dst && testmgr_alloc_buf(xoutbuf))
638 goto out_nooutbuf;
639
640 /* avoid "the frame size is larger than 1024 bytes" compiler warning */
Kees Cook6da2ec52018-06-12 13:55:00 -0700641 sg = kmalloc(array3_size(sizeof(*sg), 8, (diff_dst ? 4 : 2)),
642 GFP_KERNEL);
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +0300643 if (!sg)
644 goto out_nosg;
Herbert Xu8a525fcd2015-05-27 16:03:43 +0800645 sgout = &sg[16];
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +0300646
647 if (diff_dst)
648 d = "-ddst";
649 else
650 d = "";
651
Herbert Xuda7f0332008-07-31 17:08:25 +0800652 if (enc == ENCRYPT)
653 e = "encryption";
654 else
655 e = "decryption";
656
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100657 crypto_init_wait(&wait);
Herbert Xuda7f0332008-07-31 17:08:25 +0800658
659 req = aead_request_alloc(tfm, GFP_KERNEL);
660 if (!req) {
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +0300661 pr_err("alg: aead%s: Failed to allocate request for %s\n",
662 d, algo);
Herbert Xuda7f0332008-07-31 17:08:25 +0800663 goto out;
664 }
665
666 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100667 crypto_req_done, &wait);
Herbert Xuda7f0332008-07-31 17:08:25 +0800668
Jerome Marchandabfa7f42016-02-03 13:58:12 +0100669 iv_len = crypto_aead_ivsize(tfm);
670
Herbert Xuda7f0332008-07-31 17:08:25 +0800671 for (i = 0, j = 0; i < tcount; i++) {
Cristian Stoica05b1d332014-07-28 13:11:23 +0300672 if (template[i].np)
673 continue;
Eric Biggers5bc3de52019-01-13 15:32:24 -0800674 if (enc && template[i].novrfy)
675 continue;
Herbert Xuda7f0332008-07-31 17:08:25 +0800676
Cristian Stoica05b1d332014-07-28 13:11:23 +0300677 j++;
Herbert Xuda7f0332008-07-31 17:08:25 +0800678
Cristian Stoica05b1d332014-07-28 13:11:23 +0300679 /* some templates have no input data but they will
680 * touch input
681 */
682 input = xbuf[0];
683 input += align_offset;
684 assoc = axbuf[0];
685
686 ret = -EINVAL;
687 if (WARN_ON(align_offset + template[i].ilen >
688 PAGE_SIZE || template[i].alen > PAGE_SIZE))
689 goto out;
690
691 memcpy(input, template[i].input, template[i].ilen);
692 memcpy(assoc, template[i].assoc, template[i].alen);
693 if (template[i].iv)
Cristian Stoica424a5da2015-01-28 11:03:05 +0200694 memcpy(iv, template[i].iv, iv_len);
Cristian Stoica05b1d332014-07-28 13:11:23 +0300695 else
Cristian Stoica424a5da2015-01-28 11:03:05 +0200696 memset(iv, 0, iv_len);
Cristian Stoica05b1d332014-07-28 13:11:23 +0300697
698 crypto_aead_clear_flags(tfm, ~0);
699 if (template[i].wk)
700 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
701
702 if (template[i].klen > MAX_KEYLEN) {
703 pr_err("alg: aead%s: setkey failed on test %d for %s: key size %d > %d\n",
704 d, j, algo, template[i].klen,
705 MAX_KEYLEN);
Herbert Xufd57f222009-05-29 16:05:42 +1000706 ret = -EINVAL;
Cristian Stoica05b1d332014-07-28 13:11:23 +0300707 goto out;
708 }
709 memcpy(key, template[i].key, template[i].klen);
Herbert Xufd57f222009-05-29 16:05:42 +1000710
Cristian Stoica05b1d332014-07-28 13:11:23 +0300711 ret = crypto_aead_setkey(tfm, key, template[i].klen);
Yanjiang Jin0fae0c12016-07-29 16:32:09 +0800712 if (template[i].fail == !ret) {
Cristian Stoica05b1d332014-07-28 13:11:23 +0300713 pr_err("alg: aead%s: setkey failed on test %d for %s: flags=%x\n",
714 d, j, algo, crypto_aead_get_flags(tfm));
715 goto out;
716 } else if (ret)
717 continue;
Herbert Xuda7f0332008-07-31 17:08:25 +0800718
Cristian Stoica05b1d332014-07-28 13:11:23 +0300719 authsize = abs(template[i].rlen - template[i].ilen);
720 ret = crypto_aead_setauthsize(tfm, authsize);
721 if (ret) {
722 pr_err("alg: aead%s: Failed to set authsize to %u on test %d for %s\n",
723 d, authsize, j, algo);
724 goto out;
725 }
Herbert Xuda7f0332008-07-31 17:08:25 +0800726
Herbert Xu8a525fcd2015-05-27 16:03:43 +0800727 k = !!template[i].alen;
728 sg_init_table(sg, k + 1);
729 sg_set_buf(&sg[0], assoc, template[i].alen);
730 sg_set_buf(&sg[k], input,
731 template[i].ilen + (enc ? authsize : 0));
732 output = input;
733
Cristian Stoica05b1d332014-07-28 13:11:23 +0300734 if (diff_dst) {
Herbert Xu8a525fcd2015-05-27 16:03:43 +0800735 sg_init_table(sgout, k + 1);
736 sg_set_buf(&sgout[0], assoc, template[i].alen);
737
Cristian Stoica05b1d332014-07-28 13:11:23 +0300738 output = xoutbuf[0];
739 output += align_offset;
Herbert Xu8a525fcd2015-05-27 16:03:43 +0800740 sg_set_buf(&sgout[k], output,
741 template[i].rlen + (enc ? 0 : authsize));
Cristian Stoica05b1d332014-07-28 13:11:23 +0300742 }
743
Cristian Stoica05b1d332014-07-28 13:11:23 +0300744 aead_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
745 template[i].ilen, iv);
746
Herbert Xu8a525fcd2015-05-27 16:03:43 +0800747 aead_request_set_ad(req, template[i].alen);
Cristian Stoica05b1d332014-07-28 13:11:23 +0300748
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100749 ret = crypto_wait_req(enc ? crypto_aead_encrypt(req)
750 : crypto_aead_decrypt(req), &wait);
Cristian Stoica05b1d332014-07-28 13:11:23 +0300751
752 switch (ret) {
753 case 0:
754 if (template[i].novrfy) {
755 /* verification was supposed to fail */
756 pr_err("alg: aead%s: %s failed on test %d for %s: ret was 0, expected -EBADMSG\n",
757 d, e, j, algo);
758 /* so really, we got a bad message */
759 ret = -EBADMSG;
Horia Geanta29b77e52014-07-23 11:59:38 +0300760 goto out;
761 }
Cristian Stoica05b1d332014-07-28 13:11:23 +0300762 break;
Cristian Stoica05b1d332014-07-28 13:11:23 +0300763 case -EBADMSG:
764 if (template[i].novrfy)
765 /* verification failure was expected */
766 continue;
767 /* fall through */
768 default:
769 pr_err("alg: aead%s: %s failed on test %d for %s: ret=%d\n",
770 d, e, j, algo, -ret);
771 goto out;
772 }
Herbert Xuda7f0332008-07-31 17:08:25 +0800773
Cristian Stoica05b1d332014-07-28 13:11:23 +0300774 q = output;
775 if (memcmp(q, template[i].result, template[i].rlen)) {
776 pr_err("alg: aead%s: Test %d failed on %s for %s\n",
777 d, j, e, algo);
778 hexdump(q, template[i].rlen);
779 ret = -EINVAL;
780 goto out;
Herbert Xuda7f0332008-07-31 17:08:25 +0800781 }
782 }
783
784 for (i = 0, j = 0; i < tcount; i++) {
Jussi Kivilinna58dcf542013-06-13 17:37:50 +0300785 /* alignment tests are only done with continuous buffers */
786 if (align_offset != 0)
787 break;
788
Cristian Stoica05b1d332014-07-28 13:11:23 +0300789 if (!template[i].np)
790 continue;
Herbert Xuda7f0332008-07-31 17:08:25 +0800791
Eric Biggers5bc3de52019-01-13 15:32:24 -0800792 if (enc && template[i].novrfy)
793 continue;
794
Cristian Stoica05b1d332014-07-28 13:11:23 +0300795 j++;
Herbert Xuda7f0332008-07-31 17:08:25 +0800796
Cristian Stoica05b1d332014-07-28 13:11:23 +0300797 if (template[i].iv)
Jerome Marchandabfa7f42016-02-03 13:58:12 +0100798 memcpy(iv, template[i].iv, iv_len);
Cristian Stoica05b1d332014-07-28 13:11:23 +0300799 else
800 memset(iv, 0, MAX_IVLEN);
801
802 crypto_aead_clear_flags(tfm, ~0);
803 if (template[i].wk)
804 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
805 if (template[i].klen > MAX_KEYLEN) {
806 pr_err("alg: aead%s: setkey failed on test %d for %s: key size %d > %d\n",
807 d, j, algo, template[i].klen, MAX_KEYLEN);
808 ret = -EINVAL;
809 goto out;
810 }
811 memcpy(key, template[i].key, template[i].klen);
812
813 ret = crypto_aead_setkey(tfm, key, template[i].klen);
Yanjiang Jin0fae0c12016-07-29 16:32:09 +0800814 if (template[i].fail == !ret) {
Cristian Stoica05b1d332014-07-28 13:11:23 +0300815 pr_err("alg: aead%s: setkey failed on chunk test %d for %s: flags=%x\n",
816 d, j, algo, crypto_aead_get_flags(tfm));
817 goto out;
818 } else if (ret)
819 continue;
820
821 authsize = abs(template[i].rlen - template[i].ilen);
822
823 ret = -EINVAL;
Herbert Xu8a525fcd2015-05-27 16:03:43 +0800824 sg_init_table(sg, template[i].anp + template[i].np);
Cristian Stoica05b1d332014-07-28 13:11:23 +0300825 if (diff_dst)
Herbert Xu8a525fcd2015-05-27 16:03:43 +0800826 sg_init_table(sgout, template[i].anp + template[i].np);
827
828 ret = -EINVAL;
829 for (k = 0, temp = 0; k < template[i].anp; k++) {
830 if (WARN_ON(offset_in_page(IDX[k]) +
831 template[i].atap[k] > PAGE_SIZE))
832 goto out;
833 sg_set_buf(&sg[k],
834 memcpy(axbuf[IDX[k] >> PAGE_SHIFT] +
835 offset_in_page(IDX[k]),
836 template[i].assoc + temp,
837 template[i].atap[k]),
838 template[i].atap[k]);
839 if (diff_dst)
840 sg_set_buf(&sgout[k],
841 axbuf[IDX[k] >> PAGE_SHIFT] +
842 offset_in_page(IDX[k]),
843 template[i].atap[k]);
844 temp += template[i].atap[k];
845 }
846
Cristian Stoica05b1d332014-07-28 13:11:23 +0300847 for (k = 0, temp = 0; k < template[i].np; k++) {
848 if (WARN_ON(offset_in_page(IDX[k]) +
849 template[i].tap[k] > PAGE_SIZE))
850 goto out;
851
852 q = xbuf[IDX[k] >> PAGE_SHIFT] + offset_in_page(IDX[k]);
853 memcpy(q, template[i].input + temp, template[i].tap[k]);
Herbert Xu8a525fcd2015-05-27 16:03:43 +0800854 sg_set_buf(&sg[template[i].anp + k],
855 q, template[i].tap[k]);
Cristian Stoica05b1d332014-07-28 13:11:23 +0300856
857 if (diff_dst) {
858 q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
859 offset_in_page(IDX[k]);
860
861 memset(q, 0, template[i].tap[k]);
862
Herbert Xu8a525fcd2015-05-27 16:03:43 +0800863 sg_set_buf(&sgout[template[i].anp + k],
864 q, template[i].tap[k]);
Cristian Stoica05b1d332014-07-28 13:11:23 +0300865 }
866
867 n = template[i].tap[k];
868 if (k == template[i].np - 1 && enc)
869 n += authsize;
870 if (offset_in_page(q) + n < PAGE_SIZE)
871 q[n] = 0;
872
873 temp += template[i].tap[k];
874 }
875
876 ret = crypto_aead_setauthsize(tfm, authsize);
877 if (ret) {
878 pr_err("alg: aead%s: Failed to set authsize to %u on chunk test %d for %s\n",
879 d, authsize, j, algo);
880 goto out;
881 }
882
883 if (enc) {
Herbert Xu8a525fcd2015-05-27 16:03:43 +0800884 if (WARN_ON(sg[template[i].anp + k - 1].offset +
885 sg[template[i].anp + k - 1].length +
886 authsize > PAGE_SIZE)) {
Horia Geanta29b77e52014-07-23 11:59:38 +0300887 ret = -EINVAL;
888 goto out;
889 }
Herbert Xuda7f0332008-07-31 17:08:25 +0800890
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +0300891 if (diff_dst)
Herbert Xu8a525fcd2015-05-27 16:03:43 +0800892 sgout[template[i].anp + k - 1].length +=
893 authsize;
894 sg[template[i].anp + k - 1].length += authsize;
Cristian Stoica05b1d332014-07-28 13:11:23 +0300895 }
896
897 aead_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
898 template[i].ilen,
899 iv);
900
Herbert Xu8a525fcd2015-05-27 16:03:43 +0800901 aead_request_set_ad(req, template[i].alen);
Cristian Stoica05b1d332014-07-28 13:11:23 +0300902
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +0100903 ret = crypto_wait_req(enc ? crypto_aead_encrypt(req)
904 : crypto_aead_decrypt(req), &wait);
Cristian Stoica05b1d332014-07-28 13:11:23 +0300905
906 switch (ret) {
907 case 0:
908 if (template[i].novrfy) {
909 /* verification was supposed to fail */
910 pr_err("alg: aead%s: %s failed on chunk test %d for %s: ret was 0, expected -EBADMSG\n",
911 d, e, j, algo);
912 /* so really, we got a bad message */
913 ret = -EBADMSG;
914 goto out;
915 }
916 break;
Cristian Stoica05b1d332014-07-28 13:11:23 +0300917 case -EBADMSG:
918 if (template[i].novrfy)
919 /* verification failure was expected */
920 continue;
921 /* fall through */
922 default:
923 pr_err("alg: aead%s: %s failed on chunk test %d for %s: ret=%d\n",
924 d, e, j, algo, -ret);
925 goto out;
926 }
927
928 ret = -EINVAL;
929 for (k = 0, temp = 0; k < template[i].np; k++) {
930 if (diff_dst)
931 q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
932 offset_in_page(IDX[k]);
933 else
Herbert Xuda7f0332008-07-31 17:08:25 +0800934 q = xbuf[IDX[k] >> PAGE_SHIFT] +
935 offset_in_page(IDX[k]);
936
Cristian Stoica05b1d332014-07-28 13:11:23 +0300937 n = template[i].tap[k];
938 if (k == template[i].np - 1)
939 n += enc ? authsize : -authsize;
Herbert Xuda7f0332008-07-31 17:08:25 +0800940
Cristian Stoica05b1d332014-07-28 13:11:23 +0300941 if (memcmp(q, template[i].result + temp, n)) {
942 pr_err("alg: aead%s: Chunk test %d failed on %s at page %u for %s\n",
943 d, j, e, k, algo);
944 hexdump(q, n);
Herbert Xuda7f0332008-07-31 17:08:25 +0800945 goto out;
946 }
947
Cristian Stoica05b1d332014-07-28 13:11:23 +0300948 q += n;
949 if (k == template[i].np - 1 && !enc) {
950 if (!diff_dst &&
951 memcmp(q, template[i].input +
952 temp + n, authsize))
953 n = authsize;
Horia Geanta8ec25c52013-11-28 15:11:18 +0200954 else
Cristian Stoica05b1d332014-07-28 13:11:23 +0300955 n = 0;
956 } else {
957 for (n = 0; offset_in_page(q + n) && q[n]; n++)
958 ;
Herbert Xuda7f0332008-07-31 17:08:25 +0800959 }
Cristian Stoica05b1d332014-07-28 13:11:23 +0300960 if (n) {
961 pr_err("alg: aead%s: Result buffer corruption in chunk test %d on %s at page %u for %s: %u bytes:\n",
962 d, j, e, k, algo, n);
963 hexdump(q, n);
Herbert Xuda7f0332008-07-31 17:08:25 +0800964 goto out;
965 }
966
Cristian Stoica05b1d332014-07-28 13:11:23 +0300967 temp += template[i].tap[k];
Herbert Xuda7f0332008-07-31 17:08:25 +0800968 }
969 }
970
971 ret = 0;
972
973out:
974 aead_request_free(req);
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +0300975 kfree(sg);
976out_nosg:
977 if (diff_dst)
978 testmgr_free_buf(xoutbuf);
979out_nooutbuf:
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800980 testmgr_free_buf(axbuf);
981out_noaxbuf:
982 testmgr_free_buf(xbuf);
983out_noxbuf:
Horia Geanta29b77e52014-07-23 11:59:38 +0300984 kfree(key);
Tadeusz Struk9bac0192014-05-19 09:51:33 -0700985 kfree(iv);
Herbert Xuda7f0332008-07-31 17:08:25 +0800986 return ret;
987}
988
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +0300989static int test_aead(struct crypto_aead *tfm, int enc,
Eric Biggersb13b1e02017-02-24 15:46:59 -0800990 const struct aead_testvec *template, unsigned int tcount)
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +0300991{
Jussi Kivilinna58dcf542013-06-13 17:37:50 +0300992 unsigned int alignmask;
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +0300993 int ret;
994
995 /* test 'dst == src' case */
Jussi Kivilinna58dcf542013-06-13 17:37:50 +0300996 ret = __test_aead(tfm, enc, template, tcount, false, 0);
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +0300997 if (ret)
998 return ret;
999
1000 /* test 'dst != src' case */
Jussi Kivilinna58dcf542013-06-13 17:37:50 +03001001 ret = __test_aead(tfm, enc, template, tcount, true, 0);
1002 if (ret)
1003 return ret;
1004
1005 /* test unaligned buffers, check with one byte offset */
1006 ret = __test_aead(tfm, enc, template, tcount, true, 1);
1007 if (ret)
1008 return ret;
1009
1010 alignmask = crypto_tfm_alg_alignmask(&tfm->base);
1011 if (alignmask) {
1012 /* Check if alignment mask for tfm is correctly set. */
1013 ret = __test_aead(tfm, enc, template, tcount, true,
1014 alignmask + 1);
1015 if (ret)
1016 return ret;
1017 }
1018
1019 return 0;
Jussi Kivilinnad8a32ac2012-09-21 10:26:52 +03001020}
1021
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001022static int test_cipher(struct crypto_cipher *tfm, int enc,
Eric Biggersb13b1e02017-02-24 15:46:59 -08001023 const struct cipher_testvec *template,
1024 unsigned int tcount)
Herbert Xuda7f0332008-07-31 17:08:25 +08001025{
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001026 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
1027 unsigned int i, j, k;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001028 char *q;
1029 const char *e;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001030 const char *input, *result;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001031 void *data;
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001032 char *xbuf[XBUFSIZE];
1033 int ret = -ENOMEM;
1034
1035 if (testmgr_alloc_buf(xbuf))
1036 goto out_nobuf;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001037
1038 if (enc == ENCRYPT)
1039 e = "encryption";
1040 else
1041 e = "decryption";
1042
1043 j = 0;
1044 for (i = 0; i < tcount; i++) {
1045 if (template[i].np)
1046 continue;
1047
Stephan Mueller10faa8c2016-08-25 15:15:01 +02001048 if (fips_enabled && template[i].fips_skip)
1049 continue;
1050
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001051 input = enc ? template[i].ptext : template[i].ctext;
1052 result = enc ? template[i].ctext : template[i].ptext;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001053 j++;
1054
Herbert Xufd57f222009-05-29 16:05:42 +10001055 ret = -EINVAL;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001056 if (WARN_ON(template[i].len > PAGE_SIZE))
Herbert Xufd57f222009-05-29 16:05:42 +10001057 goto out;
1058
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001059 data = xbuf[0];
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001060 memcpy(data, input, template[i].len);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001061
1062 crypto_cipher_clear_flags(tfm, ~0);
1063 if (template[i].wk)
1064 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
1065
1066 ret = crypto_cipher_setkey(tfm, template[i].key,
1067 template[i].klen);
Yanjiang Jin0fae0c12016-07-29 16:32:09 +08001068 if (template[i].fail == !ret) {
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001069 printk(KERN_ERR "alg: cipher: setkey failed "
1070 "on test %d for %s: flags=%x\n", j,
1071 algo, crypto_cipher_get_flags(tfm));
1072 goto out;
1073 } else if (ret)
1074 continue;
1075
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001076 for (k = 0; k < template[i].len;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001077 k += crypto_cipher_blocksize(tfm)) {
1078 if (enc)
1079 crypto_cipher_encrypt_one(tfm, data + k,
1080 data + k);
1081 else
1082 crypto_cipher_decrypt_one(tfm, data + k,
1083 data + k);
1084 }
1085
1086 q = data;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001087 if (memcmp(q, result, template[i].len)) {
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001088 printk(KERN_ERR "alg: cipher: Test %d failed "
1089 "on %s for %s\n", j, e, algo);
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001090 hexdump(q, template[i].len);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001091 ret = -EINVAL;
1092 goto out;
1093 }
1094 }
1095
1096 ret = 0;
1097
1098out:
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001099 testmgr_free_buf(xbuf);
1100out_nobuf:
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001101 return ret;
1102}
1103
Herbert Xu12773d92015-08-20 15:21:46 +08001104static int __test_skcipher(struct crypto_skcipher *tfm, int enc,
Eric Biggersb13b1e02017-02-24 15:46:59 -08001105 const struct cipher_testvec *template,
1106 unsigned int tcount,
Jussi Kivilinna3a338f22013-06-13 17:37:45 +03001107 const bool diff_dst, const int align_offset)
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001108{
Herbert Xuda7f0332008-07-31 17:08:25 +08001109 const char *algo =
Herbert Xu12773d92015-08-20 15:21:46 +08001110 crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm));
Herbert Xuda7f0332008-07-31 17:08:25 +08001111 unsigned int i, j, k, n, temp;
Herbert Xuda7f0332008-07-31 17:08:25 +08001112 char *q;
Herbert Xu12773d92015-08-20 15:21:46 +08001113 struct skcipher_request *req;
Herbert Xuda7f0332008-07-31 17:08:25 +08001114 struct scatterlist sg[8];
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001115 struct scatterlist sgout[8];
1116 const char *e, *d;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001117 struct crypto_wait wait;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001118 const char *input, *result;
Herbert Xuda7f0332008-07-31 17:08:25 +08001119 void *data;
1120 char iv[MAX_IVLEN];
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001121 char *xbuf[XBUFSIZE];
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001122 char *xoutbuf[XBUFSIZE];
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001123 int ret = -ENOMEM;
Andrey Ryabinin84cba172015-09-10 13:11:55 +03001124 unsigned int ivsize = crypto_skcipher_ivsize(tfm);
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001125
1126 if (testmgr_alloc_buf(xbuf))
1127 goto out_nobuf;
Herbert Xuda7f0332008-07-31 17:08:25 +08001128
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001129 if (diff_dst && testmgr_alloc_buf(xoutbuf))
1130 goto out_nooutbuf;
1131
1132 if (diff_dst)
1133 d = "-ddst";
1134 else
1135 d = "";
1136
Herbert Xuda7f0332008-07-31 17:08:25 +08001137 if (enc == ENCRYPT)
1138 e = "encryption";
1139 else
1140 e = "decryption";
1141
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001142 crypto_init_wait(&wait);
Herbert Xuda7f0332008-07-31 17:08:25 +08001143
Herbert Xu12773d92015-08-20 15:21:46 +08001144 req = skcipher_request_alloc(tfm, GFP_KERNEL);
Herbert Xuda7f0332008-07-31 17:08:25 +08001145 if (!req) {
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001146 pr_err("alg: skcipher%s: Failed to allocate request for %s\n",
1147 d, algo);
Herbert Xuda7f0332008-07-31 17:08:25 +08001148 goto out;
1149 }
1150
Herbert Xu12773d92015-08-20 15:21:46 +08001151 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001152 crypto_req_done, &wait);
Herbert Xuda7f0332008-07-31 17:08:25 +08001153
1154 j = 0;
1155 for (i = 0; i < tcount; i++) {
Cristian Stoicabbb9a7d2014-08-08 14:27:52 +03001156 if (template[i].np && !template[i].also_non_np)
1157 continue;
1158
Stephan Mueller10faa8c2016-08-25 15:15:01 +02001159 if (fips_enabled && template[i].fips_skip)
1160 continue;
1161
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001162 if (template[i].iv && !(template[i].generates_iv && enc))
Andrey Ryabinin84cba172015-09-10 13:11:55 +03001163 memcpy(iv, template[i].iv, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001164 else
1165 memset(iv, 0, MAX_IVLEN);
1166
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001167 input = enc ? template[i].ptext : template[i].ctext;
1168 result = enc ? template[i].ctext : template[i].ptext;
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001169 j++;
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001170 ret = -EINVAL;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001171 if (WARN_ON(align_offset + template[i].len > PAGE_SIZE))
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001172 goto out;
1173
1174 data = xbuf[0];
1175 data += align_offset;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001176 memcpy(data, input, template[i].len);
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001177
Herbert Xu12773d92015-08-20 15:21:46 +08001178 crypto_skcipher_clear_flags(tfm, ~0);
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001179 if (template[i].wk)
Herbert Xu12773d92015-08-20 15:21:46 +08001180 crypto_skcipher_set_flags(tfm,
1181 CRYPTO_TFM_REQ_WEAK_KEY);
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001182
Herbert Xu12773d92015-08-20 15:21:46 +08001183 ret = crypto_skcipher_setkey(tfm, template[i].key,
1184 template[i].klen);
Yanjiang Jin0fae0c12016-07-29 16:32:09 +08001185 if (template[i].fail == !ret) {
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001186 pr_err("alg: skcipher%s: setkey failed on test %d for %s: flags=%x\n",
Herbert Xu12773d92015-08-20 15:21:46 +08001187 d, j, algo, crypto_skcipher_get_flags(tfm));
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001188 goto out;
1189 } else if (ret)
1190 continue;
1191
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001192 sg_init_one(&sg[0], data, template[i].len);
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001193 if (diff_dst) {
1194 data = xoutbuf[0];
Jussi Kivilinna3a338f22013-06-13 17:37:45 +03001195 data += align_offset;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001196 sg_init_one(&sgout[0], data, template[i].len);
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001197 }
Herbert Xuda7f0332008-07-31 17:08:25 +08001198
Herbert Xu12773d92015-08-20 15:21:46 +08001199 skcipher_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001200 template[i].len, iv);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001201 ret = crypto_wait_req(enc ? crypto_skcipher_encrypt(req) :
1202 crypto_skcipher_decrypt(req), &wait);
Herbert Xuda7f0332008-07-31 17:08:25 +08001203
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001204 if (ret) {
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001205 pr_err("alg: skcipher%s: %s failed on test %d for %s: ret=%d\n",
1206 d, e, j, algo, -ret);
1207 goto out;
1208 }
Herbert Xuda7f0332008-07-31 17:08:25 +08001209
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001210 q = data;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001211 if (memcmp(q, result, template[i].len)) {
Boris BREZILLON8a826a32015-06-16 11:46:46 +02001212 pr_err("alg: skcipher%s: Test %d failed (invalid result) on %s for %s\n",
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001213 d, j, e, algo);
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001214 hexdump(q, template[i].len);
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001215 ret = -EINVAL;
1216 goto out;
Herbert Xuda7f0332008-07-31 17:08:25 +08001217 }
Boris BREZILLON8a826a32015-06-16 11:46:46 +02001218
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001219 if (template[i].generates_iv && enc &&
1220 memcmp(iv, template[i].iv, crypto_skcipher_ivsize(tfm))) {
Boris BREZILLON8a826a32015-06-16 11:46:46 +02001221 pr_err("alg: skcipher%s: Test %d failed (invalid output IV) on %s for %s\n",
1222 d, j, e, algo);
1223 hexdump(iv, crypto_skcipher_ivsize(tfm));
1224 ret = -EINVAL;
1225 goto out;
1226 }
Herbert Xuda7f0332008-07-31 17:08:25 +08001227 }
1228
1229 j = 0;
1230 for (i = 0; i < tcount; i++) {
Jussi Kivilinna3a338f22013-06-13 17:37:45 +03001231 /* alignment tests are only done with continuous buffers */
1232 if (align_offset != 0)
1233 break;
Herbert Xuda7f0332008-07-31 17:08:25 +08001234
Cristian Stoicabbb9a7d2014-08-08 14:27:52 +03001235 if (!template[i].np)
1236 continue;
1237
Stephan Mueller10faa8c2016-08-25 15:15:01 +02001238 if (fips_enabled && template[i].fips_skip)
1239 continue;
1240
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001241 if (template[i].iv && !(template[i].generates_iv && enc))
Andrey Ryabinin84cba172015-09-10 13:11:55 +03001242 memcpy(iv, template[i].iv, ivsize);
Herbert Xuda7f0332008-07-31 17:08:25 +08001243 else
1244 memset(iv, 0, MAX_IVLEN);
1245
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001246 input = enc ? template[i].ptext : template[i].ctext;
1247 result = enc ? template[i].ctext : template[i].ptext;
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001248 j++;
Herbert Xu12773d92015-08-20 15:21:46 +08001249 crypto_skcipher_clear_flags(tfm, ~0);
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001250 if (template[i].wk)
Herbert Xu12773d92015-08-20 15:21:46 +08001251 crypto_skcipher_set_flags(tfm,
1252 CRYPTO_TFM_REQ_WEAK_KEY);
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001253
Herbert Xu12773d92015-08-20 15:21:46 +08001254 ret = crypto_skcipher_setkey(tfm, template[i].key,
1255 template[i].klen);
Yanjiang Jin0fae0c12016-07-29 16:32:09 +08001256 if (template[i].fail == !ret) {
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001257 pr_err("alg: skcipher%s: setkey failed on chunk test %d for %s: flags=%x\n",
Herbert Xu12773d92015-08-20 15:21:46 +08001258 d, j, algo, crypto_skcipher_get_flags(tfm));
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001259 goto out;
1260 } else if (ret)
1261 continue;
1262
1263 temp = 0;
1264 ret = -EINVAL;
1265 sg_init_table(sg, template[i].np);
1266 if (diff_dst)
1267 sg_init_table(sgout, template[i].np);
1268 for (k = 0; k < template[i].np; k++) {
1269 if (WARN_ON(offset_in_page(IDX[k]) +
1270 template[i].tap[k] > PAGE_SIZE))
Herbert Xuda7f0332008-07-31 17:08:25 +08001271 goto out;
Herbert Xuda7f0332008-07-31 17:08:25 +08001272
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001273 q = xbuf[IDX[k] >> PAGE_SHIFT] + offset_in_page(IDX[k]);
1274
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001275 memcpy(q, input + temp, template[i].tap[k]);
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001276
1277 if (offset_in_page(q) + template[i].tap[k] < PAGE_SIZE)
1278 q[template[i].tap[k]] = 0;
1279
1280 sg_set_buf(&sg[k], q, template[i].tap[k]);
1281 if (diff_dst) {
1282 q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
1283 offset_in_page(IDX[k]);
1284
1285 sg_set_buf(&sgout[k], q, template[i].tap[k]);
1286
1287 memset(q, 0, template[i].tap[k]);
1288 if (offset_in_page(q) +
1289 template[i].tap[k] < PAGE_SIZE)
1290 q[template[i].tap[k]] = 0;
1291 }
1292
1293 temp += template[i].tap[k];
1294 }
1295
Herbert Xu12773d92015-08-20 15:21:46 +08001296 skcipher_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001297 template[i].len, iv);
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001298
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001299 ret = crypto_wait_req(enc ? crypto_skcipher_encrypt(req) :
1300 crypto_skcipher_decrypt(req), &wait);
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001301
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001302 if (ret) {
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001303 pr_err("alg: skcipher%s: %s failed on chunk test %d for %s: ret=%d\n",
1304 d, e, j, algo, -ret);
1305 goto out;
1306 }
1307
1308 temp = 0;
1309 ret = -EINVAL;
1310 for (k = 0; k < template[i].np; k++) {
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001311 if (diff_dst)
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001312 q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
1313 offset_in_page(IDX[k]);
1314 else
Herbert Xuda7f0332008-07-31 17:08:25 +08001315 q = xbuf[IDX[k] >> PAGE_SHIFT] +
1316 offset_in_page(IDX[k]);
1317
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001318 if (memcmp(q, result + temp, template[i].tap[k])) {
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001319 pr_err("alg: skcipher%s: Chunk test %d failed on %s at page %u for %s\n",
1320 d, j, e, k, algo);
1321 hexdump(q, template[i].tap[k]);
Herbert Xuda7f0332008-07-31 17:08:25 +08001322 goto out;
1323 }
1324
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001325 q += template[i].tap[k];
1326 for (n = 0; offset_in_page(q + n) && q[n]; n++)
1327 ;
1328 if (n) {
1329 pr_err("alg: skcipher%s: Result buffer corruption in chunk test %d on %s at page %u for %s: %u bytes:\n",
1330 d, j, e, k, algo, n);
1331 hexdump(q, n);
1332 goto out;
Herbert Xuda7f0332008-07-31 17:08:25 +08001333 }
Cristian Stoicaa1aa44a2014-08-08 14:27:51 +03001334 temp += template[i].tap[k];
Herbert Xuda7f0332008-07-31 17:08:25 +08001335 }
1336 }
1337
1338 ret = 0;
1339
1340out:
Herbert Xu12773d92015-08-20 15:21:46 +08001341 skcipher_request_free(req);
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001342 if (diff_dst)
1343 testmgr_free_buf(xoutbuf);
1344out_nooutbuf:
Herbert Xuf8b0d4d2009-05-06 14:15:47 +08001345 testmgr_free_buf(xbuf);
1346out_nobuf:
Herbert Xuda7f0332008-07-31 17:08:25 +08001347 return ret;
1348}
1349
Herbert Xu12773d92015-08-20 15:21:46 +08001350static int test_skcipher(struct crypto_skcipher *tfm, int enc,
Eric Biggersb13b1e02017-02-24 15:46:59 -08001351 const struct cipher_testvec *template,
1352 unsigned int tcount)
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001353{
Jussi Kivilinna3a338f22013-06-13 17:37:45 +03001354 unsigned int alignmask;
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001355 int ret;
1356
1357 /* test 'dst == src' case */
Jussi Kivilinna3a338f22013-06-13 17:37:45 +03001358 ret = __test_skcipher(tfm, enc, template, tcount, false, 0);
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001359 if (ret)
1360 return ret;
1361
1362 /* test 'dst != src' case */
Jussi Kivilinna3a338f22013-06-13 17:37:45 +03001363 ret = __test_skcipher(tfm, enc, template, tcount, true, 0);
1364 if (ret)
1365 return ret;
1366
1367 /* test unaligned buffers, check with one byte offset */
1368 ret = __test_skcipher(tfm, enc, template, tcount, true, 1);
1369 if (ret)
1370 return ret;
1371
1372 alignmask = crypto_tfm_alg_alignmask(&tfm->base);
1373 if (alignmask) {
1374 /* Check if alignment mask for tfm is correctly set. */
1375 ret = __test_skcipher(tfm, enc, template, tcount, true,
1376 alignmask + 1);
1377 if (ret)
1378 return ret;
1379 }
1380
1381 return 0;
Jussi Kivilinna08d6af82012-09-21 10:26:47 +03001382}
1383
Eric Biggersb13b1e02017-02-24 15:46:59 -08001384static int test_comp(struct crypto_comp *tfm,
1385 const struct comp_testvec *ctemplate,
1386 const struct comp_testvec *dtemplate,
1387 int ctcount, int dtcount)
Herbert Xuda7f0332008-07-31 17:08:25 +08001388{
1389 const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
Mahipal Challa33607382018-04-11 20:28:32 +02001390 char *output, *decomp_output;
Herbert Xuda7f0332008-07-31 17:08:25 +08001391 unsigned int i;
Herbert Xuda7f0332008-07-31 17:08:25 +08001392 int ret;
1393
Mahipal Challa33607382018-04-11 20:28:32 +02001394 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1395 if (!output)
1396 return -ENOMEM;
1397
1398 decomp_output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1399 if (!decomp_output) {
1400 kfree(output);
1401 return -ENOMEM;
1402 }
1403
Herbert Xuda7f0332008-07-31 17:08:25 +08001404 for (i = 0; i < ctcount; i++) {
Geert Uytterhoevenc79cf912009-03-29 15:44:19 +08001405 int ilen;
1406 unsigned int dlen = COMP_BUF_SIZE;
Herbert Xuda7f0332008-07-31 17:08:25 +08001407
Michael Schupikov22a81182018-10-07 13:58:10 +02001408 memset(output, 0, COMP_BUF_SIZE);
1409 memset(decomp_output, 0, COMP_BUF_SIZE);
Herbert Xuda7f0332008-07-31 17:08:25 +08001410
1411 ilen = ctemplate[i].inlen;
1412 ret = crypto_comp_compress(tfm, ctemplate[i].input,
Mahipal Challa33607382018-04-11 20:28:32 +02001413 ilen, output, &dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08001414 if (ret) {
1415 printk(KERN_ERR "alg: comp: compression failed "
1416 "on test %d for %s: ret=%d\n", i + 1, algo,
1417 -ret);
1418 goto out;
1419 }
1420
Mahipal Challa33607382018-04-11 20:28:32 +02001421 ilen = dlen;
1422 dlen = COMP_BUF_SIZE;
1423 ret = crypto_comp_decompress(tfm, output,
1424 ilen, decomp_output, &dlen);
1425 if (ret) {
1426 pr_err("alg: comp: compression failed: decompress: on test %d for %s failed: ret=%d\n",
1427 i + 1, algo, -ret);
1428 goto out;
1429 }
1430
1431 if (dlen != ctemplate[i].inlen) {
Geert Uytterhoevenb812eb02008-11-28 20:51:28 +08001432 printk(KERN_ERR "alg: comp: Compression test %d "
1433 "failed for %s: output len = %d\n", i + 1, algo,
1434 dlen);
1435 ret = -EINVAL;
1436 goto out;
1437 }
1438
Mahipal Challa33607382018-04-11 20:28:32 +02001439 if (memcmp(decomp_output, ctemplate[i].input,
1440 ctemplate[i].inlen)) {
1441 pr_err("alg: comp: compression failed: output differs: on test %d for %s\n",
1442 i + 1, algo);
1443 hexdump(decomp_output, dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08001444 ret = -EINVAL;
1445 goto out;
1446 }
1447 }
1448
1449 for (i = 0; i < dtcount; i++) {
Geert Uytterhoevenc79cf912009-03-29 15:44:19 +08001450 int ilen;
1451 unsigned int dlen = COMP_BUF_SIZE;
Herbert Xuda7f0332008-07-31 17:08:25 +08001452
Michael Schupikov22a81182018-10-07 13:58:10 +02001453 memset(decomp_output, 0, COMP_BUF_SIZE);
Herbert Xuda7f0332008-07-31 17:08:25 +08001454
1455 ilen = dtemplate[i].inlen;
1456 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
Mahipal Challa33607382018-04-11 20:28:32 +02001457 ilen, decomp_output, &dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08001458 if (ret) {
1459 printk(KERN_ERR "alg: comp: decompression failed "
1460 "on test %d for %s: ret=%d\n", i + 1, algo,
1461 -ret);
1462 goto out;
1463 }
1464
Geert Uytterhoevenb812eb02008-11-28 20:51:28 +08001465 if (dlen != dtemplate[i].outlen) {
1466 printk(KERN_ERR "alg: comp: Decompression test %d "
1467 "failed for %s: output len = %d\n", i + 1, algo,
1468 dlen);
1469 ret = -EINVAL;
1470 goto out;
1471 }
1472
Mahipal Challa33607382018-04-11 20:28:32 +02001473 if (memcmp(decomp_output, dtemplate[i].output, dlen)) {
Herbert Xuda7f0332008-07-31 17:08:25 +08001474 printk(KERN_ERR "alg: comp: Decompression test %d "
1475 "failed for %s\n", i + 1, algo);
Mahipal Challa33607382018-04-11 20:28:32 +02001476 hexdump(decomp_output, dlen);
Herbert Xuda7f0332008-07-31 17:08:25 +08001477 ret = -EINVAL;
1478 goto out;
1479 }
1480 }
1481
1482 ret = 0;
1483
1484out:
Mahipal Challa33607382018-04-11 20:28:32 +02001485 kfree(decomp_output);
1486 kfree(output);
Herbert Xuda7f0332008-07-31 17:08:25 +08001487 return ret;
1488}
1489
Eric Biggersb13b1e02017-02-24 15:46:59 -08001490static int test_acomp(struct crypto_acomp *tfm,
Mahipal Challa33607382018-04-11 20:28:32 +02001491 const struct comp_testvec *ctemplate,
Eric Biggersb13b1e02017-02-24 15:46:59 -08001492 const struct comp_testvec *dtemplate,
1493 int ctcount, int dtcount)
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001494{
1495 const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
1496 unsigned int i;
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01001497 char *output, *decomp_out;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001498 int ret;
1499 struct scatterlist src, dst;
1500 struct acomp_req *req;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001501 struct crypto_wait wait;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001502
Eric Biggerseb095592016-11-23 10:24:35 -08001503 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1504 if (!output)
1505 return -ENOMEM;
1506
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01001507 decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL);
1508 if (!decomp_out) {
1509 kfree(output);
1510 return -ENOMEM;
1511 }
1512
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001513 for (i = 0; i < ctcount; i++) {
1514 unsigned int dlen = COMP_BUF_SIZE;
1515 int ilen = ctemplate[i].inlen;
Laura Abbott02608e02016-12-21 12:32:54 -08001516 void *input_vec;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001517
Eric Biggersd2110222016-12-30 14:12:00 -06001518 input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL);
Laura Abbott02608e02016-12-21 12:32:54 -08001519 if (!input_vec) {
1520 ret = -ENOMEM;
1521 goto out;
1522 }
1523
Eric Biggerseb095592016-11-23 10:24:35 -08001524 memset(output, 0, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001525 crypto_init_wait(&wait);
Laura Abbott02608e02016-12-21 12:32:54 -08001526 sg_init_one(&src, input_vec, ilen);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001527 sg_init_one(&dst, output, dlen);
1528
1529 req = acomp_request_alloc(tfm);
1530 if (!req) {
1531 pr_err("alg: acomp: request alloc failed for %s\n",
1532 algo);
Laura Abbott02608e02016-12-21 12:32:54 -08001533 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001534 ret = -ENOMEM;
1535 goto out;
1536 }
1537
1538 acomp_request_set_params(req, &src, &dst, ilen, dlen);
1539 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001540 crypto_req_done, &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001541
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001542 ret = crypto_wait_req(crypto_acomp_compress(req), &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001543 if (ret) {
1544 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
1545 i + 1, algo, -ret);
Laura Abbott02608e02016-12-21 12:32:54 -08001546 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001547 acomp_request_free(req);
1548 goto out;
1549 }
1550
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01001551 ilen = req->dlen;
1552 dlen = COMP_BUF_SIZE;
1553 sg_init_one(&src, output, ilen);
1554 sg_init_one(&dst, decomp_out, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001555 crypto_init_wait(&wait);
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01001556 acomp_request_set_params(req, &src, &dst, ilen, dlen);
1557
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001558 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01001559 if (ret) {
1560 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n",
1561 i + 1, algo, -ret);
1562 kfree(input_vec);
1563 acomp_request_free(req);
1564 goto out;
1565 }
1566
1567 if (req->dlen != ctemplate[i].inlen) {
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001568 pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n",
1569 i + 1, algo, req->dlen);
1570 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08001571 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001572 acomp_request_free(req);
1573 goto out;
1574 }
1575
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01001576 if (memcmp(input_vec, decomp_out, req->dlen)) {
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001577 pr_err("alg: acomp: Compression test %d failed for %s\n",
1578 i + 1, algo);
1579 hexdump(output, req->dlen);
1580 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08001581 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001582 acomp_request_free(req);
1583 goto out;
1584 }
1585
Laura Abbott02608e02016-12-21 12:32:54 -08001586 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001587 acomp_request_free(req);
1588 }
1589
1590 for (i = 0; i < dtcount; i++) {
1591 unsigned int dlen = COMP_BUF_SIZE;
1592 int ilen = dtemplate[i].inlen;
Laura Abbott02608e02016-12-21 12:32:54 -08001593 void *input_vec;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001594
Eric Biggersd2110222016-12-30 14:12:00 -06001595 input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL);
Laura Abbott02608e02016-12-21 12:32:54 -08001596 if (!input_vec) {
1597 ret = -ENOMEM;
1598 goto out;
1599 }
1600
Eric Biggerseb095592016-11-23 10:24:35 -08001601 memset(output, 0, dlen);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001602 crypto_init_wait(&wait);
Laura Abbott02608e02016-12-21 12:32:54 -08001603 sg_init_one(&src, input_vec, ilen);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001604 sg_init_one(&dst, output, dlen);
1605
1606 req = acomp_request_alloc(tfm);
1607 if (!req) {
1608 pr_err("alg: acomp: request alloc failed for %s\n",
1609 algo);
Laura Abbott02608e02016-12-21 12:32:54 -08001610 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001611 ret = -ENOMEM;
1612 goto out;
1613 }
1614
1615 acomp_request_set_params(req, &src, &dst, ilen, dlen);
1616 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001617 crypto_req_done, &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001618
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01001619 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001620 if (ret) {
1621 pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n",
1622 i + 1, algo, -ret);
Laura Abbott02608e02016-12-21 12:32:54 -08001623 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001624 acomp_request_free(req);
1625 goto out;
1626 }
1627
1628 if (req->dlen != dtemplate[i].outlen) {
1629 pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n",
1630 i + 1, algo, req->dlen);
1631 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08001632 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001633 acomp_request_free(req);
1634 goto out;
1635 }
1636
1637 if (memcmp(output, dtemplate[i].output, req->dlen)) {
1638 pr_err("alg: acomp: Decompression test %d failed for %s\n",
1639 i + 1, algo);
1640 hexdump(output, req->dlen);
1641 ret = -EINVAL;
Laura Abbott02608e02016-12-21 12:32:54 -08001642 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001643 acomp_request_free(req);
1644 goto out;
1645 }
1646
Laura Abbott02608e02016-12-21 12:32:54 -08001647 kfree(input_vec);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001648 acomp_request_free(req);
1649 }
1650
1651 ret = 0;
1652
1653out:
Giovanni Cabiddua9943a02017-04-19 14:27:18 +01001654 kfree(decomp_out);
Eric Biggerseb095592016-11-23 10:24:35 -08001655 kfree(output);
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001656 return ret;
1657}
1658
Eric Biggersb13b1e02017-02-24 15:46:59 -08001659static int test_cprng(struct crypto_rng *tfm,
1660 const struct cprng_testvec *template,
Jarod Wilson7647d6c2009-05-04 19:44:50 +08001661 unsigned int tcount)
1662{
1663 const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
Felipe Contrerasfa4ef8a2009-10-27 19:04:42 +08001664 int err = 0, i, j, seedsize;
Jarod Wilson7647d6c2009-05-04 19:44:50 +08001665 u8 *seed;
1666 char result[32];
1667
1668 seedsize = crypto_rng_seedsize(tfm);
1669
1670 seed = kmalloc(seedsize, GFP_KERNEL);
1671 if (!seed) {
1672 printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
1673 "for %s\n", algo);
1674 return -ENOMEM;
1675 }
1676
1677 for (i = 0; i < tcount; i++) {
1678 memset(result, 0, 32);
1679
1680 memcpy(seed, template[i].v, template[i].vlen);
1681 memcpy(seed + template[i].vlen, template[i].key,
1682 template[i].klen);
1683 memcpy(seed + template[i].vlen + template[i].klen,
1684 template[i].dt, template[i].dtlen);
1685
1686 err = crypto_rng_reset(tfm, seed, seedsize);
1687 if (err) {
1688 printk(KERN_ERR "alg: cprng: Failed to reset rng "
1689 "for %s\n", algo);
1690 goto out;
1691 }
1692
1693 for (j = 0; j < template[i].loops; j++) {
1694 err = crypto_rng_get_bytes(tfm, result,
1695 template[i].rlen);
Stephan Mueller19e60e12015-03-10 17:00:36 +01001696 if (err < 0) {
Jarod Wilson7647d6c2009-05-04 19:44:50 +08001697 printk(KERN_ERR "alg: cprng: Failed to obtain "
1698 "the correct amount of random data for "
Stephan Mueller19e60e12015-03-10 17:00:36 +01001699 "%s (requested %d)\n", algo,
1700 template[i].rlen);
Jarod Wilson7647d6c2009-05-04 19:44:50 +08001701 goto out;
1702 }
1703 }
1704
1705 err = memcmp(result, template[i].result,
1706 template[i].rlen);
1707 if (err) {
1708 printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
1709 i, algo);
1710 hexdump(result, template[i].rlen);
1711 err = -EINVAL;
1712 goto out;
1713 }
1714 }
1715
1716out:
1717 kfree(seed);
1718 return err;
1719}
1720
Herbert Xuda7f0332008-07-31 17:08:25 +08001721static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
1722 u32 type, u32 mask)
1723{
1724 struct crypto_aead *tfm;
1725 int err = 0;
1726
Herbert Xueed93e02016-11-22 20:08:31 +08001727 tfm = crypto_alloc_aead(driver, type, mask);
Herbert Xuda7f0332008-07-31 17:08:25 +08001728 if (IS_ERR(tfm)) {
1729 printk(KERN_ERR "alg: aead: Failed to load transform for %s: "
1730 "%ld\n", driver, PTR_ERR(tfm));
1731 return PTR_ERR(tfm);
1732 }
1733
1734 if (desc->suite.aead.enc.vecs) {
1735 err = test_aead(tfm, ENCRYPT, desc->suite.aead.enc.vecs,
1736 desc->suite.aead.enc.count);
1737 if (err)
1738 goto out;
1739 }
1740
1741 if (!err && desc->suite.aead.dec.vecs)
1742 err = test_aead(tfm, DECRYPT, desc->suite.aead.dec.vecs,
1743 desc->suite.aead.dec.count);
1744
1745out:
1746 crypto_free_aead(tfm);
1747 return err;
1748}
1749
1750static int alg_test_cipher(const struct alg_test_desc *desc,
1751 const char *driver, u32 type, u32 mask)
1752{
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001753 const struct cipher_test_suite *suite = &desc->suite.cipher;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001754 struct crypto_cipher *tfm;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001755 int err;
Herbert Xuda7f0332008-07-31 17:08:25 +08001756
Herbert Xueed93e02016-11-22 20:08:31 +08001757 tfm = crypto_alloc_cipher(driver, type, mask);
Herbert Xuda7f0332008-07-31 17:08:25 +08001758 if (IS_ERR(tfm)) {
1759 printk(KERN_ERR "alg: cipher: Failed to load transform for "
1760 "%s: %ld\n", driver, PTR_ERR(tfm));
1761 return PTR_ERR(tfm);
1762 }
1763
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001764 err = test_cipher(tfm, ENCRYPT, suite->vecs, suite->count);
1765 if (!err)
1766 err = test_cipher(tfm, DECRYPT, suite->vecs, suite->count);
Herbert Xuda7f0332008-07-31 17:08:25 +08001767
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001768 crypto_free_cipher(tfm);
1769 return err;
1770}
1771
1772static int alg_test_skcipher(const struct alg_test_desc *desc,
1773 const char *driver, u32 type, u32 mask)
1774{
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001775 const struct cipher_test_suite *suite = &desc->suite.cipher;
Herbert Xu12773d92015-08-20 15:21:46 +08001776 struct crypto_skcipher *tfm;
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001777 int err;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001778
Herbert Xueed93e02016-11-22 20:08:31 +08001779 tfm = crypto_alloc_skcipher(driver, type, mask);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001780 if (IS_ERR(tfm)) {
1781 printk(KERN_ERR "alg: skcipher: Failed to load transform for "
1782 "%s: %ld\n", driver, PTR_ERR(tfm));
1783 return PTR_ERR(tfm);
1784 }
1785
Eric Biggers92a4c9f2018-05-20 22:50:29 -07001786 err = test_skcipher(tfm, ENCRYPT, suite->vecs, suite->count);
1787 if (!err)
1788 err = test_skcipher(tfm, DECRYPT, suite->vecs, suite->count);
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10001789
Herbert Xu12773d92015-08-20 15:21:46 +08001790 crypto_free_skcipher(tfm);
Herbert Xuda7f0332008-07-31 17:08:25 +08001791 return err;
1792}
1793
1794static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
1795 u32 type, u32 mask)
1796{
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001797 struct crypto_comp *comp;
1798 struct crypto_acomp *acomp;
Herbert Xuda7f0332008-07-31 17:08:25 +08001799 int err;
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001800 u32 algo_type = type & CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
Herbert Xuda7f0332008-07-31 17:08:25 +08001801
Giovanni Cabiddud7db7a82016-10-21 13:19:54 +01001802 if (algo_type == CRYPTO_ALG_TYPE_ACOMPRESS) {
1803 acomp = crypto_alloc_acomp(driver, type, mask);
1804 if (IS_ERR(acomp)) {
1805 pr_err("alg: acomp: Failed to load transform for %s: %ld\n",
1806 driver, PTR_ERR(acomp));
1807 return PTR_ERR(acomp);
1808 }
1809 err = test_acomp(acomp, desc->suite.comp.comp.vecs,
1810 desc->suite.comp.decomp.vecs,
1811 desc->suite.comp.comp.count,
1812 desc->suite.comp.decomp.count);
1813 crypto_free_acomp(acomp);
1814 } else {
1815 comp = crypto_alloc_comp(driver, type, mask);
1816 if (IS_ERR(comp)) {
1817 pr_err("alg: comp: Failed to load transform for %s: %ld\n",
1818 driver, PTR_ERR(comp));
1819 return PTR_ERR(comp);
1820 }
1821
1822 err = test_comp(comp, desc->suite.comp.comp.vecs,
1823 desc->suite.comp.decomp.vecs,
1824 desc->suite.comp.comp.count,
1825 desc->suite.comp.decomp.count);
1826
1827 crypto_free_comp(comp);
Herbert Xuda7f0332008-07-31 17:08:25 +08001828 }
Herbert Xuda7f0332008-07-31 17:08:25 +08001829 return err;
1830}
1831
Eric Biggers9b3abc0162018-05-19 22:07:41 -07001832static int __alg_test_hash(const struct hash_testvec *template,
1833 unsigned int tcount, const char *driver,
1834 u32 type, u32 mask)
Herbert Xuda7f0332008-07-31 17:08:25 +08001835{
1836 struct crypto_ahash *tfm;
1837 int err;
1838
Herbert Xueed93e02016-11-22 20:08:31 +08001839 tfm = crypto_alloc_ahash(driver, type, mask);
Herbert Xuda7f0332008-07-31 17:08:25 +08001840 if (IS_ERR(tfm)) {
1841 printk(KERN_ERR "alg: hash: Failed to load transform for %s: "
1842 "%ld\n", driver, PTR_ERR(tfm));
1843 return PTR_ERR(tfm);
1844 }
1845
Gilad Ben-Yossef76715092018-07-01 08:02:35 +01001846 err = test_hash(tfm, template, tcount, HASH_TEST_DIGEST);
David S. Millera8f1a052010-05-19 14:12:03 +10001847 if (!err)
Gilad Ben-Yossef76715092018-07-01 08:02:35 +01001848 err = test_hash(tfm, template, tcount, HASH_TEST_FINAL);
1849 if (!err)
1850 err = test_hash(tfm, template, tcount, HASH_TEST_FINUP);
Herbert Xuda7f0332008-07-31 17:08:25 +08001851 crypto_free_ahash(tfm);
1852 return err;
1853}
1854
Eric Biggers9b3abc0162018-05-19 22:07:41 -07001855static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1856 u32 type, u32 mask)
1857{
1858 const struct hash_testvec *template = desc->suite.hash.vecs;
1859 unsigned int tcount = desc->suite.hash.count;
1860 unsigned int nr_unkeyed, nr_keyed;
1861 int err;
1862
1863 /*
1864 * For OPTIONAL_KEY algorithms, we have to do all the unkeyed tests
1865 * first, before setting a key on the tfm. To make this easier, we
1866 * require that the unkeyed test vectors (if any) are listed first.
1867 */
1868
1869 for (nr_unkeyed = 0; nr_unkeyed < tcount; nr_unkeyed++) {
1870 if (template[nr_unkeyed].ksize)
1871 break;
1872 }
1873 for (nr_keyed = 0; nr_unkeyed + nr_keyed < tcount; nr_keyed++) {
1874 if (!template[nr_unkeyed + nr_keyed].ksize) {
1875 pr_err("alg: hash: test vectors for %s out of order, "
1876 "unkeyed ones must come first\n", desc->alg);
1877 return -EINVAL;
1878 }
1879 }
1880
1881 err = 0;
1882 if (nr_unkeyed) {
1883 err = __alg_test_hash(template, nr_unkeyed, driver, type, mask);
1884 template += nr_unkeyed;
1885 }
1886
1887 if (!err && nr_keyed)
1888 err = __alg_test_hash(template, nr_keyed, driver, type, mask);
1889
1890 return err;
1891}
1892
Herbert Xu8e3ee852008-11-07 14:58:52 +08001893static int alg_test_crc32c(const struct alg_test_desc *desc,
1894 const char *driver, u32 type, u32 mask)
1895{
1896 struct crypto_shash *tfm;
Eric Biggerscb9dde82019-01-10 12:17:55 -08001897 __le32 val;
Herbert Xu8e3ee852008-11-07 14:58:52 +08001898 int err;
1899
1900 err = alg_test_hash(desc, driver, type, mask);
1901 if (err)
1902 goto out;
1903
Herbert Xueed93e02016-11-22 20:08:31 +08001904 tfm = crypto_alloc_shash(driver, type, mask);
Herbert Xu8e3ee852008-11-07 14:58:52 +08001905 if (IS_ERR(tfm)) {
1906 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
1907 "%ld\n", driver, PTR_ERR(tfm));
1908 err = PTR_ERR(tfm);
1909 goto out;
1910 }
1911
1912 do {
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02001913 SHASH_DESC_ON_STACK(shash, tfm);
1914 u32 *ctx = (u32 *)shash_desc_ctx(shash);
Herbert Xu8e3ee852008-11-07 14:58:52 +08001915
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02001916 shash->tfm = tfm;
1917 shash->flags = 0;
Herbert Xu8e3ee852008-11-07 14:58:52 +08001918
Eric Biggerscb9dde82019-01-10 12:17:55 -08001919 *ctx = 420553207;
Jan-Simon Möller4c5c3022012-07-02 13:48:30 +02001920 err = crypto_shash_final(shash, (u8 *)&val);
Herbert Xu8e3ee852008-11-07 14:58:52 +08001921 if (err) {
1922 printk(KERN_ERR "alg: crc32c: Operation failed for "
1923 "%s: %d\n", driver, err);
1924 break;
1925 }
1926
Eric Biggerscb9dde82019-01-10 12:17:55 -08001927 if (val != cpu_to_le32(~420553207)) {
1928 pr_err("alg: crc32c: Test failed for %s: %u\n",
1929 driver, le32_to_cpu(val));
Herbert Xu8e3ee852008-11-07 14:58:52 +08001930 err = -EINVAL;
1931 }
1932 } while (0);
1933
1934 crypto_free_shash(tfm);
1935
1936out:
1937 return err;
1938}
1939
Jarod Wilson7647d6c2009-05-04 19:44:50 +08001940static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
1941 u32 type, u32 mask)
1942{
1943 struct crypto_rng *rng;
1944 int err;
1945
Herbert Xueed93e02016-11-22 20:08:31 +08001946 rng = crypto_alloc_rng(driver, type, mask);
Jarod Wilson7647d6c2009-05-04 19:44:50 +08001947 if (IS_ERR(rng)) {
1948 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
1949 "%ld\n", driver, PTR_ERR(rng));
1950 return PTR_ERR(rng);
1951 }
1952
1953 err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
1954
1955 crypto_free_rng(rng);
1956
1957 return err;
1958}
1959
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02001960
Eric Biggersb13b1e02017-02-24 15:46:59 -08001961static int drbg_cavs_test(const struct drbg_testvec *test, int pr,
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02001962 const char *driver, u32 type, u32 mask)
1963{
1964 int ret = -EAGAIN;
1965 struct crypto_rng *drng;
1966 struct drbg_test_data test_data;
1967 struct drbg_string addtl, pers, testentropy;
1968 unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
1969
1970 if (!buf)
1971 return -ENOMEM;
1972
Herbert Xueed93e02016-11-22 20:08:31 +08001973 drng = crypto_alloc_rng(driver, type, mask);
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02001974 if (IS_ERR(drng)) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04001975 printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02001976 "%s\n", driver);
1977 kzfree(buf);
1978 return -ENOMEM;
1979 }
1980
1981 test_data.testentropy = &testentropy;
1982 drbg_string_fill(&testentropy, test->entropy, test->entropylen);
1983 drbg_string_fill(&pers, test->pers, test->perslen);
1984 ret = crypto_drbg_reset_test(drng, &pers, &test_data);
1985 if (ret) {
1986 printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
1987 goto outbuf;
1988 }
1989
1990 drbg_string_fill(&addtl, test->addtla, test->addtllen);
1991 if (pr) {
1992 drbg_string_fill(&testentropy, test->entpra, test->entprlen);
1993 ret = crypto_drbg_get_bytes_addtl_test(drng,
1994 buf, test->expectedlen, &addtl, &test_data);
1995 } else {
1996 ret = crypto_drbg_get_bytes_addtl(drng,
1997 buf, test->expectedlen, &addtl);
1998 }
Stephan Mueller19e60e12015-03-10 17:00:36 +01001999 if (ret < 0) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04002000 printk(KERN_ERR "alg: drbg: could not obtain random data for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002001 "driver %s\n", driver);
2002 goto outbuf;
2003 }
2004
2005 drbg_string_fill(&addtl, test->addtlb, test->addtllen);
2006 if (pr) {
2007 drbg_string_fill(&testentropy, test->entprb, test->entprlen);
2008 ret = crypto_drbg_get_bytes_addtl_test(drng,
2009 buf, test->expectedlen, &addtl, &test_data);
2010 } else {
2011 ret = crypto_drbg_get_bytes_addtl(drng,
2012 buf, test->expectedlen, &addtl);
2013 }
Stephan Mueller19e60e12015-03-10 17:00:36 +01002014 if (ret < 0) {
Jarod Wilson2fc0d252014-07-29 15:47:56 -04002015 printk(KERN_ERR "alg: drbg: could not obtain random data for "
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002016 "driver %s\n", driver);
2017 goto outbuf;
2018 }
2019
2020 ret = memcmp(test->expected, buf, test->expectedlen);
2021
2022outbuf:
2023 crypto_free_rng(drng);
2024 kzfree(buf);
2025 return ret;
2026}
2027
2028
2029static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
2030 u32 type, u32 mask)
2031{
2032 int err = 0;
2033 int pr = 0;
2034 int i = 0;
Eric Biggersb13b1e02017-02-24 15:46:59 -08002035 const struct drbg_testvec *template = desc->suite.drbg.vecs;
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002036 unsigned int tcount = desc->suite.drbg.count;
2037
2038 if (0 == memcmp(driver, "drbg_pr_", 8))
2039 pr = 1;
2040
2041 for (i = 0; i < tcount; i++) {
2042 err = drbg_cavs_test(&template[i], pr, driver, type, mask);
2043 if (err) {
2044 printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
2045 i, driver);
2046 err = -EINVAL;
2047 break;
2048 }
2049 }
2050 return err;
2051
2052}
2053
Eric Biggersb13b1e02017-02-24 15:46:59 -08002054static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002055 const char *alg)
2056{
2057 struct kpp_request *req;
2058 void *input_buf = NULL;
2059 void *output_buf = NULL;
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002060 void *a_public = NULL;
2061 void *a_ss = NULL;
2062 void *shared_secret = NULL;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002063 struct crypto_wait wait;
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002064 unsigned int out_len_max;
2065 int err = -ENOMEM;
2066 struct scatterlist src, dst;
2067
2068 req = kpp_request_alloc(tfm, GFP_KERNEL);
2069 if (!req)
2070 return err;
2071
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002072 crypto_init_wait(&wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002073
2074 err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size);
2075 if (err < 0)
2076 goto free_req;
2077
2078 out_len_max = crypto_kpp_maxsize(tfm);
2079 output_buf = kzalloc(out_len_max, GFP_KERNEL);
2080 if (!output_buf) {
2081 err = -ENOMEM;
2082 goto free_req;
2083 }
2084
2085 /* Use appropriate parameter as base */
2086 kpp_request_set_input(req, NULL, 0);
2087 sg_init_one(&dst, output_buf, out_len_max);
2088 kpp_request_set_output(req, &dst, out_len_max);
2089 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002090 crypto_req_done, &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002091
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002092 /* Compute party A's public key */
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002093 err = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002094 if (err) {
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002095 pr_err("alg: %s: Party A: generate public key test failed. err %d\n",
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002096 alg, err);
2097 goto free_output;
2098 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002099
2100 if (vec->genkey) {
2101 /* Save party A's public key */
2102 a_public = kzalloc(out_len_max, GFP_KERNEL);
2103 if (!a_public) {
2104 err = -ENOMEM;
2105 goto free_output;
2106 }
2107 memcpy(a_public, sg_virt(req->dst), out_len_max);
2108 } else {
2109 /* Verify calculated public key */
2110 if (memcmp(vec->expected_a_public, sg_virt(req->dst),
2111 vec->expected_a_public_size)) {
2112 pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n",
2113 alg);
2114 err = -EINVAL;
2115 goto free_output;
2116 }
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002117 }
2118
2119 /* Calculate shared secret key by using counter part (b) public key. */
2120 input_buf = kzalloc(vec->b_public_size, GFP_KERNEL);
2121 if (!input_buf) {
2122 err = -ENOMEM;
2123 goto free_output;
2124 }
2125
2126 memcpy(input_buf, vec->b_public, vec->b_public_size);
2127 sg_init_one(&src, input_buf, vec->b_public_size);
2128 sg_init_one(&dst, output_buf, out_len_max);
2129 kpp_request_set_input(req, &src, vec->b_public_size);
2130 kpp_request_set_output(req, &dst, out_len_max);
2131 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002132 crypto_req_done, &wait);
2133 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002134 if (err) {
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002135 pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n",
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002136 alg, err);
2137 goto free_all;
2138 }
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002139
2140 if (vec->genkey) {
2141 /* Save the shared secret obtained by party A */
2142 a_ss = kzalloc(vec->expected_ss_size, GFP_KERNEL);
2143 if (!a_ss) {
2144 err = -ENOMEM;
2145 goto free_all;
2146 }
2147 memcpy(a_ss, sg_virt(req->dst), vec->expected_ss_size);
2148
2149 /*
2150 * Calculate party B's shared secret by using party A's
2151 * public key.
2152 */
2153 err = crypto_kpp_set_secret(tfm, vec->b_secret,
2154 vec->b_secret_size);
2155 if (err < 0)
2156 goto free_all;
2157
2158 sg_init_one(&src, a_public, vec->expected_a_public_size);
2159 sg_init_one(&dst, output_buf, out_len_max);
2160 kpp_request_set_input(req, &src, vec->expected_a_public_size);
2161 kpp_request_set_output(req, &dst, out_len_max);
2162 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002163 crypto_req_done, &wait);
2164 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req),
2165 &wait);
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002166 if (err) {
2167 pr_err("alg: %s: Party B: compute shared secret failed. err %d\n",
2168 alg, err);
2169 goto free_all;
2170 }
2171
2172 shared_secret = a_ss;
2173 } else {
2174 shared_secret = (void *)vec->expected_ss;
2175 }
2176
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002177 /*
2178 * verify shared secret from which the user will derive
2179 * secret key by executing whatever hash it has chosen
2180 */
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002181 if (memcmp(shared_secret, sg_virt(req->dst),
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002182 vec->expected_ss_size)) {
2183 pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
2184 alg);
2185 err = -EINVAL;
2186 }
2187
2188free_all:
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002189 kfree(a_ss);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002190 kfree(input_buf);
2191free_output:
Tudor-Dan Ambarus47d3fd32017-05-30 17:52:49 +03002192 kfree(a_public);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002193 kfree(output_buf);
2194free_req:
2195 kpp_request_free(req);
2196 return err;
2197}
2198
2199static int test_kpp(struct crypto_kpp *tfm, const char *alg,
Eric Biggersb13b1e02017-02-24 15:46:59 -08002200 const struct kpp_testvec *vecs, unsigned int tcount)
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002201{
2202 int ret, i;
2203
2204 for (i = 0; i < tcount; i++) {
2205 ret = do_test_kpp(tfm, vecs++, alg);
2206 if (ret) {
2207 pr_err("alg: %s: test failed on vector %d, err=%d\n",
2208 alg, i + 1, ret);
2209 return ret;
2210 }
2211 }
2212 return 0;
2213}
2214
2215static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
2216 u32 type, u32 mask)
2217{
2218 struct crypto_kpp *tfm;
2219 int err = 0;
2220
Herbert Xueed93e02016-11-22 20:08:31 +08002221 tfm = crypto_alloc_kpp(driver, type, mask);
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002222 if (IS_ERR(tfm)) {
2223 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n",
2224 driver, PTR_ERR(tfm));
2225 return PTR_ERR(tfm);
2226 }
2227 if (desc->suite.kpp.vecs)
2228 err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs,
2229 desc->suite.kpp.count);
2230
2231 crypto_free_kpp(tfm);
2232 return err;
2233}
2234
Herbert Xu50d2b6432016-06-29 19:32:20 +08002235static int test_akcipher_one(struct crypto_akcipher *tfm,
Eric Biggersb13b1e02017-02-24 15:46:59 -08002236 const struct akcipher_testvec *vecs)
Tadeusz Struk946cc462015-06-16 10:31:06 -07002237{
Herbert Xudf27b262016-05-05 16:42:49 +08002238 char *xbuf[XBUFSIZE];
Tadeusz Struk946cc462015-06-16 10:31:06 -07002239 struct akcipher_request *req;
2240 void *outbuf_enc = NULL;
2241 void *outbuf_dec = NULL;
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002242 struct crypto_wait wait;
Tadeusz Struk946cc462015-06-16 10:31:06 -07002243 unsigned int out_len_max, out_len = 0;
2244 int err = -ENOMEM;
Tadeusz Struk22287b02015-10-08 09:26:55 -07002245 struct scatterlist src, dst, src_tab[2];
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002246 const char *m, *c;
2247 unsigned int m_size, c_size;
2248 const char *op;
Tadeusz Struk946cc462015-06-16 10:31:06 -07002249
Herbert Xudf27b262016-05-05 16:42:49 +08002250 if (testmgr_alloc_buf(xbuf))
2251 return err;
2252
Tadeusz Struk946cc462015-06-16 10:31:06 -07002253 req = akcipher_request_alloc(tfm, GFP_KERNEL);
2254 if (!req)
Herbert Xudf27b262016-05-05 16:42:49 +08002255 goto free_xbuf;
Tadeusz Struk946cc462015-06-16 10:31:06 -07002256
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002257 crypto_init_wait(&wait);
Tadeusz Struk22287b02015-10-08 09:26:55 -07002258
2259 if (vecs->public_key_vec)
2260 err = crypto_akcipher_set_pub_key(tfm, vecs->key,
2261 vecs->key_len);
2262 else
2263 err = crypto_akcipher_set_priv_key(tfm, vecs->key,
2264 vecs->key_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002265 if (err)
2266 goto free_req;
2267
Salvatore Benedetto57763f52016-07-04 10:52:34 +01002268 err = -ENOMEM;
Tadeusz Struk22287b02015-10-08 09:26:55 -07002269 out_len_max = crypto_akcipher_maxsize(tfm);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002270
2271 /*
2272 * First run test which do not require a private key, such as
2273 * encrypt or verify.
2274 */
Tadeusz Struk946cc462015-06-16 10:31:06 -07002275 outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
2276 if (!outbuf_enc)
2277 goto free_req;
2278
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002279 if (!vecs->siggen_sigver_test) {
2280 m = vecs->m;
2281 m_size = vecs->m_size;
2282 c = vecs->c;
2283 c_size = vecs->c_size;
2284 op = "encrypt";
2285 } else {
2286 /* Swap args so we could keep plaintext (digest)
2287 * in vecs->m, and cooked signature in vecs->c.
2288 */
2289 m = vecs->c; /* signature */
2290 m_size = vecs->c_size;
2291 c = vecs->m; /* digest */
2292 c_size = vecs->m_size;
2293 op = "verify";
2294 }
Herbert Xudf27b262016-05-05 16:42:49 +08002295
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002296 if (WARN_ON(m_size > PAGE_SIZE))
2297 goto free_all;
2298 memcpy(xbuf[0], m, m_size);
Herbert Xudf27b262016-05-05 16:42:49 +08002299
Tadeusz Struk22287b02015-10-08 09:26:55 -07002300 sg_init_table(src_tab, 2);
Herbert Xudf27b262016-05-05 16:42:49 +08002301 sg_set_buf(&src_tab[0], xbuf[0], 8);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002302 sg_set_buf(&src_tab[1], xbuf[0] + 8, m_size - 8);
Tadeusz Struk22287b02015-10-08 09:26:55 -07002303 sg_init_one(&dst, outbuf_enc, out_len_max);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002304 akcipher_request_set_crypt(req, src_tab, &dst, m_size,
Tadeusz Struk22287b02015-10-08 09:26:55 -07002305 out_len_max);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002306 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002307 crypto_req_done, &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002308
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002309 err = crypto_wait_req(vecs->siggen_sigver_test ?
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002310 /* Run asymmetric signature verification */
2311 crypto_akcipher_verify(req) :
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002312 /* Run asymmetric encrypt */
2313 crypto_akcipher_encrypt(req), &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002314 if (err) {
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002315 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002316 goto free_all;
2317 }
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002318 if (req->dst_len != c_size) {
2319 pr_err("alg: akcipher: %s test failed. Invalid output len\n",
2320 op);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002321 err = -EINVAL;
2322 goto free_all;
2323 }
2324 /* verify that encrypted message is equal to expected */
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002325 if (memcmp(c, outbuf_enc, c_size)) {
2326 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
2327 hexdump(outbuf_enc, c_size);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002328 err = -EINVAL;
2329 goto free_all;
2330 }
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002331
2332 /*
2333 * Don't invoke (decrypt or sign) test which require a private key
2334 * for vectors with only a public key.
2335 */
Tadeusz Struk946cc462015-06-16 10:31:06 -07002336 if (vecs->public_key_vec) {
2337 err = 0;
2338 goto free_all;
2339 }
2340 outbuf_dec = kzalloc(out_len_max, GFP_KERNEL);
2341 if (!outbuf_dec) {
2342 err = -ENOMEM;
2343 goto free_all;
2344 }
Herbert Xudf27b262016-05-05 16:42:49 +08002345
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002346 op = vecs->siggen_sigver_test ? "sign" : "decrypt";
2347 if (WARN_ON(c_size > PAGE_SIZE))
Herbert Xudf27b262016-05-05 16:42:49 +08002348 goto free_all;
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002349 memcpy(xbuf[0], c, c_size);
Herbert Xudf27b262016-05-05 16:42:49 +08002350
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002351 sg_init_one(&src, xbuf[0], c_size);
Tadeusz Struk22287b02015-10-08 09:26:55 -07002352 sg_init_one(&dst, outbuf_dec, out_len_max);
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002353 crypto_init_wait(&wait);
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002354 akcipher_request_set_crypt(req, &src, &dst, c_size, out_len_max);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002355
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002356 err = crypto_wait_req(vecs->siggen_sigver_test ?
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002357 /* Run asymmetric signature generation */
2358 crypto_akcipher_sign(req) :
Gilad Ben-Yossef7f397132017-10-18 08:00:43 +01002359 /* Run asymmetric decrypt */
2360 crypto_akcipher_decrypt(req), &wait);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002361 if (err) {
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002362 pr_err("alg: akcipher: %s test failed. err %d\n", op, err);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002363 goto free_all;
2364 }
2365 out_len = req->dst_len;
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002366 if (out_len < m_size) {
2367 pr_err("alg: akcipher: %s test failed. Invalid output len %u\n",
2368 op, out_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002369 err = -EINVAL;
2370 goto free_all;
2371 }
2372 /* verify that decrypted message is equal to the original msg */
Vitaly Chikunov0507de92019-01-07 20:54:27 +03002373 if (memchr_inv(outbuf_dec, 0, out_len - m_size) ||
2374 memcmp(m, outbuf_dec + out_len - m_size, m_size)) {
2375 pr_err("alg: akcipher: %s test failed. Invalid output\n", op);
Herbert Xu50d2b6432016-06-29 19:32:20 +08002376 hexdump(outbuf_dec, out_len);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002377 err = -EINVAL;
2378 }
2379free_all:
2380 kfree(outbuf_dec);
2381 kfree(outbuf_enc);
2382free_req:
2383 akcipher_request_free(req);
Herbert Xudf27b262016-05-05 16:42:49 +08002384free_xbuf:
2385 testmgr_free_buf(xbuf);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002386 return err;
2387}
2388
Herbert Xu50d2b6432016-06-29 19:32:20 +08002389static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
Eric Biggersb13b1e02017-02-24 15:46:59 -08002390 const struct akcipher_testvec *vecs,
2391 unsigned int tcount)
Tadeusz Struk946cc462015-06-16 10:31:06 -07002392{
Herbert Xu15226e42016-07-18 18:20:10 +08002393 const char *algo =
2394 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm));
Tadeusz Struk946cc462015-06-16 10:31:06 -07002395 int ret, i;
2396
2397 for (i = 0; i < tcount; i++) {
Herbert Xu50d2b6432016-06-29 19:32:20 +08002398 ret = test_akcipher_one(tfm, vecs++);
2399 if (!ret)
2400 continue;
2401
Herbert Xu15226e42016-07-18 18:20:10 +08002402 pr_err("alg: akcipher: test %d failed for %s, err=%d\n",
2403 i + 1, algo, ret);
Herbert Xu50d2b6432016-06-29 19:32:20 +08002404 return ret;
Tadeusz Struk946cc462015-06-16 10:31:06 -07002405 }
2406 return 0;
2407}
2408
Tadeusz Struk946cc462015-06-16 10:31:06 -07002409static int alg_test_akcipher(const struct alg_test_desc *desc,
2410 const char *driver, u32 type, u32 mask)
2411{
2412 struct crypto_akcipher *tfm;
2413 int err = 0;
2414
Herbert Xueed93e02016-11-22 20:08:31 +08002415 tfm = crypto_alloc_akcipher(driver, type, mask);
Tadeusz Struk946cc462015-06-16 10:31:06 -07002416 if (IS_ERR(tfm)) {
2417 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n",
2418 driver, PTR_ERR(tfm));
2419 return PTR_ERR(tfm);
2420 }
2421 if (desc->suite.akcipher.vecs)
2422 err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs,
2423 desc->suite.akcipher.count);
2424
2425 crypto_free_akcipher(tfm);
2426 return err;
2427}
2428
Youquan, Song863b5572009-12-23 19:45:20 +08002429static int alg_test_null(const struct alg_test_desc *desc,
2430 const char *driver, u32 type, u32 mask)
2431{
2432 return 0;
2433}
2434
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002435#define __VECS(tv) { .vecs = tv, .count = ARRAY_SIZE(tv) }
2436
Herbert Xuda7f0332008-07-31 17:08:25 +08002437/* Please keep this list sorted by algorithm name. */
2438static const struct alg_test_desc alg_test_descs[] = {
2439 {
Eric Biggers059c2a42018-11-16 17:26:31 -08002440 .alg = "adiantum(xchacha12,aes)",
2441 .test = alg_test_skcipher,
2442 .suite = {
2443 .cipher = __VECS(adiantum_xchacha12_aes_tv_template)
2444 },
2445 }, {
2446 .alg = "adiantum(xchacha20,aes)",
2447 .test = alg_test_skcipher,
2448 .suite = {
2449 .cipher = __VECS(adiantum_xchacha20_aes_tv_template)
2450 },
2451 }, {
Ondrej Mosnacekb87dc202018-05-11 14:12:50 +02002452 .alg = "aegis128",
2453 .test = alg_test_aead,
2454 .suite = {
2455 .aead = {
2456 .enc = __VECS(aegis128_enc_tv_template),
2457 .dec = __VECS(aegis128_dec_tv_template),
2458 }
2459 }
2460 }, {
2461 .alg = "aegis128l",
2462 .test = alg_test_aead,
2463 .suite = {
2464 .aead = {
2465 .enc = __VECS(aegis128l_enc_tv_template),
2466 .dec = __VECS(aegis128l_dec_tv_template),
2467 }
2468 }
2469 }, {
2470 .alg = "aegis256",
2471 .test = alg_test_aead,
2472 .suite = {
2473 .aead = {
2474 .enc = __VECS(aegis256_enc_tv_template),
2475 .dec = __VECS(aegis256_dec_tv_template),
2476 }
2477 }
2478 }, {
Jarod Wilsone08ca2d2009-05-04 19:46:29 +08002479 .alg = "ansi_cprng",
2480 .test = alg_test_cprng,
2481 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002482 .cprng = __VECS(ansi_cprng_aes_tv_template)
Jarod Wilsone08ca2d2009-05-04 19:46:29 +08002483 }
2484 }, {
Horia Geantabca4feb2014-03-14 17:46:51 +02002485 .alg = "authenc(hmac(md5),ecb(cipher_null))",
2486 .test = alg_test_aead,
Horia Geantabca4feb2014-03-14 17:46:51 +02002487 .suite = {
2488 .aead = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002489 .enc = __VECS(hmac_md5_ecb_cipher_null_enc_tv_template),
2490 .dec = __VECS(hmac_md5_ecb_cipher_null_dec_tv_template)
Horia Geantabca4feb2014-03-14 17:46:51 +02002491 }
2492 }
2493 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002494 .alg = "authenc(hmac(sha1),cbc(aes))",
Horia Geantae46e9a42012-07-03 19:16:54 +03002495 .test = alg_test_aead,
Herbert Xubcf741c2017-06-28 19:09:07 +08002496 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03002497 .suite = {
2498 .aead = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002499 .enc = __VECS(hmac_sha1_aes_cbc_enc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302500 }
2501 }
2502 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002503 .alg = "authenc(hmac(sha1),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302504 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302505 .suite = {
2506 .aead = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002507 .enc = __VECS(hmac_sha1_des_cbc_enc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302508 }
2509 }
2510 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002511 .alg = "authenc(hmac(sha1),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302512 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01002513 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302514 .suite = {
2515 .aead = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002516 .enc = __VECS(hmac_sha1_des3_ede_cbc_enc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03002517 }
2518 }
2519 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01002520 .alg = "authenc(hmac(sha1),ctr(aes))",
2521 .test = alg_test_null,
2522 .fips_allowed = 1,
2523 }, {
Horia Geantabca4feb2014-03-14 17:46:51 +02002524 .alg = "authenc(hmac(sha1),ecb(cipher_null))",
2525 .test = alg_test_aead,
Horia Geantabca4feb2014-03-14 17:46:51 +02002526 .suite = {
2527 .aead = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002528 .enc = __VECS(hmac_sha1_ecb_cipher_null_enc_tv_temp),
2529 .dec = __VECS(hmac_sha1_ecb_cipher_null_dec_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302530 }
2531 }
2532 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01002533 .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
2534 .test = alg_test_null,
2535 .fips_allowed = 1,
2536 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002537 .alg = "authenc(hmac(sha224),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302538 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302539 .suite = {
2540 .aead = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002541 .enc = __VECS(hmac_sha224_des_cbc_enc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302542 }
2543 }
2544 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002545 .alg = "authenc(hmac(sha224),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302546 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01002547 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302548 .suite = {
2549 .aead = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002550 .enc = __VECS(hmac_sha224_des3_ede_cbc_enc_tv_temp)
Horia Geantabca4feb2014-03-14 17:46:51 +02002551 }
2552 }
2553 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002554 .alg = "authenc(hmac(sha256),cbc(aes))",
Horia Geantae46e9a42012-07-03 19:16:54 +03002555 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01002556 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03002557 .suite = {
2558 .aead = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002559 .enc = __VECS(hmac_sha256_aes_cbc_enc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302560 }
2561 }
2562 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002563 .alg = "authenc(hmac(sha256),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302564 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302565 .suite = {
2566 .aead = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002567 .enc = __VECS(hmac_sha256_des_cbc_enc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302568 }
2569 }
2570 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002571 .alg = "authenc(hmac(sha256),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302572 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01002573 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302574 .suite = {
2575 .aead = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002576 .enc = __VECS(hmac_sha256_des3_ede_cbc_enc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302577 }
2578 }
2579 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01002580 .alg = "authenc(hmac(sha256),ctr(aes))",
2581 .test = alg_test_null,
2582 .fips_allowed = 1,
2583 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01002584 .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
2585 .test = alg_test_null,
2586 .fips_allowed = 1,
2587 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002588 .alg = "authenc(hmac(sha384),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302589 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302590 .suite = {
2591 .aead = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002592 .enc = __VECS(hmac_sha384_des_cbc_enc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302593 }
2594 }
2595 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002596 .alg = "authenc(hmac(sha384),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302597 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01002598 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302599 .suite = {
2600 .aead = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002601 .enc = __VECS(hmac_sha384_des3_ede_cbc_enc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03002602 }
2603 }
2604 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01002605 .alg = "authenc(hmac(sha384),ctr(aes))",
2606 .test = alg_test_null,
2607 .fips_allowed = 1,
2608 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01002609 .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
2610 .test = alg_test_null,
2611 .fips_allowed = 1,
2612 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002613 .alg = "authenc(hmac(sha512),cbc(aes))",
Marcus Meissnered1afac2016-02-05 14:23:33 +01002614 .fips_allowed = 1,
Horia Geantae46e9a42012-07-03 19:16:54 +03002615 .test = alg_test_aead,
Horia Geantae46e9a42012-07-03 19:16:54 +03002616 .suite = {
2617 .aead = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002618 .enc = __VECS(hmac_sha512_aes_cbc_enc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302619 }
2620 }
2621 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002622 .alg = "authenc(hmac(sha512),cbc(des))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302623 .test = alg_test_aead,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302624 .suite = {
2625 .aead = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002626 .enc = __VECS(hmac_sha512_des_cbc_enc_tv_temp)
Nitesh Lal5208ed22014-05-21 17:09:08 +05302627 }
2628 }
2629 }, {
Herbert Xua4198fd2015-07-30 17:53:23 +08002630 .alg = "authenc(hmac(sha512),cbc(des3_ede))",
Nitesh Lal5208ed22014-05-21 17:09:08 +05302631 .test = alg_test_aead,
Marcus Meissnered1afac2016-02-05 14:23:33 +01002632 .fips_allowed = 1,
Nitesh Lal5208ed22014-05-21 17:09:08 +05302633 .suite = {
2634 .aead = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002635 .enc = __VECS(hmac_sha512_des3_ede_cbc_enc_tv_temp)
Horia Geantae46e9a42012-07-03 19:16:54 +03002636 }
2637 }
2638 }, {
Marcus Meissnerfb16abc2016-02-06 11:53:07 +01002639 .alg = "authenc(hmac(sha512),ctr(aes))",
2640 .test = alg_test_null,
2641 .fips_allowed = 1,
2642 }, {
Marcus Meissner88886902016-02-19 13:34:28 +01002643 .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
2644 .test = alg_test_null,
2645 .fips_allowed = 1,
2646 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08002647 .alg = "cbc(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002648 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10002649 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08002650 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002651 .cipher = __VECS(aes_cbc_tv_template)
2652 },
Herbert Xuda7f0332008-07-31 17:08:25 +08002653 }, {
2654 .alg = "cbc(anubis)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002655 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08002656 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002657 .cipher = __VECS(anubis_cbc_tv_template)
2658 },
Herbert Xuda7f0332008-07-31 17:08:25 +08002659 }, {
2660 .alg = "cbc(blowfish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002661 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08002662 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002663 .cipher = __VECS(bf_cbc_tv_template)
2664 },
Herbert Xuda7f0332008-07-31 17:08:25 +08002665 }, {
2666 .alg = "cbc(camellia)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002667 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08002668 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002669 .cipher = __VECS(camellia_cbc_tv_template)
2670 },
Herbert Xuda7f0332008-07-31 17:08:25 +08002671 }, {
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02002672 .alg = "cbc(cast5)",
2673 .test = alg_test_skcipher,
2674 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002675 .cipher = __VECS(cast5_cbc_tv_template)
2676 },
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02002677 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02002678 .alg = "cbc(cast6)",
2679 .test = alg_test_skcipher,
2680 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002681 .cipher = __VECS(cast6_cbc_tv_template)
2682 },
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02002683 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08002684 .alg = "cbc(des)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002685 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08002686 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002687 .cipher = __VECS(des_cbc_tv_template)
2688 },
Herbert Xuda7f0332008-07-31 17:08:25 +08002689 }, {
2690 .alg = "cbc(des3_ede)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002691 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10002692 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08002693 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002694 .cipher = __VECS(des3_ede_cbc_tv_template)
2695 },
Herbert Xuda7f0332008-07-31 17:08:25 +08002696 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01002697 /* Same as cbc(aes) except the key is stored in
2698 * hardware secure memory which we reference by index
2699 */
2700 .alg = "cbc(paes)",
2701 .test = alg_test_null,
2702 .fips_allowed = 1,
2703 }, {
Jussi Kivilinna9d259172011-10-18 00:02:53 +03002704 .alg = "cbc(serpent)",
2705 .test = alg_test_skcipher,
2706 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002707 .cipher = __VECS(serpent_cbc_tv_template)
2708 },
Jussi Kivilinna9d259172011-10-18 00:02:53 +03002709 }, {
Gilad Ben-Yossef95ba5972018-09-20 14:18:38 +01002710 .alg = "cbc(sm4)",
2711 .test = alg_test_skcipher,
2712 .suite = {
2713 .cipher = __VECS(sm4_cbc_tv_template)
2714 }
2715 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08002716 .alg = "cbc(twofish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002717 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08002718 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002719 .cipher = __VECS(tf_cbc_tv_template)
2720 },
Herbert Xuda7f0332008-07-31 17:08:25 +08002721 }, {
Ard Biesheuvel092acf02017-02-03 14:49:35 +00002722 .alg = "cbcmac(aes)",
2723 .fips_allowed = 1,
2724 .test = alg_test_hash,
2725 .suite = {
2726 .hash = __VECS(aes_cbcmac_tv_template)
2727 }
2728 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08002729 .alg = "ccm(aes)",
2730 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10002731 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08002732 .suite = {
2733 .aead = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002734 .enc = __VECS(aes_ccm_enc_tv_template),
2735 .dec = __VECS(aes_ccm_dec_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08002736 }
2737 }
2738 }, {
Dmitry Eremin-Solenikov7da66672018-10-20 02:01:53 +03002739 .alg = "cfb(aes)",
2740 .test = alg_test_skcipher,
2741 .fips_allowed = 1,
2742 .suite = {
2743 .cipher = __VECS(aes_cfb_tv_template)
2744 },
2745 }, {
Martin Willi3590ebf2015-06-01 13:43:57 +02002746 .alg = "chacha20",
2747 .test = alg_test_skcipher,
2748 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002749 .cipher = __VECS(chacha20_tv_template)
2750 },
Martin Willi3590ebf2015-06-01 13:43:57 +02002751 }, {
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03002752 .alg = "cmac(aes)",
Stephan Mueller8f183752015-08-19 08:42:07 +02002753 .fips_allowed = 1,
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03002754 .test = alg_test_hash,
2755 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002756 .hash = __VECS(aes_cmac128_tv_template)
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03002757 }
2758 }, {
2759 .alg = "cmac(des3_ede)",
Stephan Mueller8f183752015-08-19 08:42:07 +02002760 .fips_allowed = 1,
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03002761 .test = alg_test_hash,
2762 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002763 .hash = __VECS(des3_ede_cmac64_tv_template)
Jussi Kivilinna93b5e862013-04-08 10:48:44 +03002764 }
2765 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03002766 .alg = "compress_null",
2767 .test = alg_test_null,
2768 }, {
Ard Biesheuvelebb34722015-05-04 11:00:17 +02002769 .alg = "crc32",
2770 .test = alg_test_hash,
2771 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002772 .hash = __VECS(crc32_tv_template)
Ard Biesheuvelebb34722015-05-04 11:00:17 +02002773 }
2774 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08002775 .alg = "crc32c",
Herbert Xu8e3ee852008-11-07 14:58:52 +08002776 .test = alg_test_crc32c,
Jarod Wilsona1915d52009-05-15 15:16:03 +10002777 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08002778 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002779 .hash = __VECS(crc32c_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08002780 }
2781 }, {
Herbert Xu684115212013-09-07 12:56:26 +10002782 .alg = "crct10dif",
2783 .test = alg_test_hash,
2784 .fips_allowed = 1,
2785 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002786 .hash = __VECS(crct10dif_tv_template)
Herbert Xu684115212013-09-07 12:56:26 +10002787 }
2788 }, {
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08002789 .alg = "ctr(aes)",
2790 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10002791 .fips_allowed = 1,
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08002792 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002793 .cipher = __VECS(aes_ctr_tv_template)
Jarod Wilsonf7cb80f2009-05-06 17:29:17 +08002794 }
2795 }, {
Jussi Kivilinna85b63e32011-10-10 23:03:03 +03002796 .alg = "ctr(blowfish)",
2797 .test = alg_test_skcipher,
2798 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002799 .cipher = __VECS(bf_ctr_tv_template)
Jussi Kivilinna85b63e32011-10-10 23:03:03 +03002800 }
2801 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02002802 .alg = "ctr(camellia)",
2803 .test = alg_test_skcipher,
2804 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002805 .cipher = __VECS(camellia_ctr_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02002806 }
2807 }, {
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02002808 .alg = "ctr(cast5)",
2809 .test = alg_test_skcipher,
2810 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002811 .cipher = __VECS(cast5_ctr_tv_template)
Johannes Goetzfrieda2c58262012-07-11 19:37:21 +02002812 }
2813 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02002814 .alg = "ctr(cast6)",
2815 .test = alg_test_skcipher,
2816 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002817 .cipher = __VECS(cast6_ctr_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02002818 }
2819 }, {
Jussi Kivilinna8163fc32012-10-20 14:53:07 +03002820 .alg = "ctr(des)",
2821 .test = alg_test_skcipher,
2822 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002823 .cipher = __VECS(des_ctr_tv_template)
Jussi Kivilinna8163fc32012-10-20 14:53:07 +03002824 }
2825 }, {
Jussi Kivilinnae080b172012-10-20 14:53:12 +03002826 .alg = "ctr(des3_ede)",
2827 .test = alg_test_skcipher,
Marcelo Cerri0d8da102017-03-20 17:28:05 -03002828 .fips_allowed = 1,
Jussi Kivilinnae080b172012-10-20 14:53:12 +03002829 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002830 .cipher = __VECS(des3_ede_ctr_tv_template)
Jussi Kivilinnae080b172012-10-20 14:53:12 +03002831 }
2832 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01002833 /* Same as ctr(aes) except the key is stored in
2834 * hardware secure memory which we reference by index
2835 */
2836 .alg = "ctr(paes)",
2837 .test = alg_test_null,
2838 .fips_allowed = 1,
2839 }, {
Jussi Kivilinna9d259172011-10-18 00:02:53 +03002840 .alg = "ctr(serpent)",
2841 .test = alg_test_skcipher,
2842 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002843 .cipher = __VECS(serpent_ctr_tv_template)
Jussi Kivilinna9d259172011-10-18 00:02:53 +03002844 }
2845 }, {
Gilad Ben-Yossef95ba5972018-09-20 14:18:38 +01002846 .alg = "ctr(sm4)",
2847 .test = alg_test_skcipher,
2848 .suite = {
2849 .cipher = __VECS(sm4_ctr_tv_template)
2850 }
2851 }, {
Jussi Kivilinna573da622011-10-10 23:03:12 +03002852 .alg = "ctr(twofish)",
2853 .test = alg_test_skcipher,
2854 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002855 .cipher = __VECS(tf_ctr_tv_template)
Jussi Kivilinna573da622011-10-10 23:03:12 +03002856 }
2857 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08002858 .alg = "cts(cbc(aes))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10002859 .test = alg_test_skcipher,
Gilad Ben-Yossef196ad602018-11-04 10:05:24 +00002860 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08002861 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07002862 .cipher = __VECS(cts_mode_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08002863 }
2864 }, {
2865 .alg = "deflate",
2866 .test = alg_test_comp,
Milan Broz08189042012-12-06 17:16:28 +08002867 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08002868 .suite = {
2869 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002870 .comp = __VECS(deflate_comp_tv_template),
2871 .decomp = __VECS(deflate_decomp_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08002872 }
2873 }
2874 }, {
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002875 .alg = "dh",
2876 .test = alg_test_kpp,
2877 .fips_allowed = 1,
2878 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002879 .kpp = __VECS(dh_tv_template)
Salvatore Benedetto802c7f12016-06-22 17:49:14 +01002880 }
2881 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03002882 .alg = "digest_null",
2883 .test = alg_test_null,
2884 }, {
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002885 .alg = "drbg_nopr_ctr_aes128",
2886 .test = alg_test_drbg,
2887 .fips_allowed = 1,
2888 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002889 .drbg = __VECS(drbg_nopr_ctr_aes128_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002890 }
2891 }, {
2892 .alg = "drbg_nopr_ctr_aes192",
2893 .test = alg_test_drbg,
2894 .fips_allowed = 1,
2895 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002896 .drbg = __VECS(drbg_nopr_ctr_aes192_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002897 }
2898 }, {
2899 .alg = "drbg_nopr_ctr_aes256",
2900 .test = alg_test_drbg,
2901 .fips_allowed = 1,
2902 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002903 .drbg = __VECS(drbg_nopr_ctr_aes256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002904 }
2905 }, {
2906 /*
2907 * There is no need to specifically test the DRBG with every
2908 * backend cipher -- covered by drbg_nopr_hmac_sha256 test
2909 */
2910 .alg = "drbg_nopr_hmac_sha1",
2911 .fips_allowed = 1,
2912 .test = alg_test_null,
2913 }, {
2914 .alg = "drbg_nopr_hmac_sha256",
2915 .test = alg_test_drbg,
2916 .fips_allowed = 1,
2917 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002918 .drbg = __VECS(drbg_nopr_hmac_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002919 }
2920 }, {
2921 /* covered by drbg_nopr_hmac_sha256 test */
2922 .alg = "drbg_nopr_hmac_sha384",
2923 .fips_allowed = 1,
2924 .test = alg_test_null,
2925 }, {
2926 .alg = "drbg_nopr_hmac_sha512",
2927 .test = alg_test_null,
2928 .fips_allowed = 1,
2929 }, {
2930 .alg = "drbg_nopr_sha1",
2931 .fips_allowed = 1,
2932 .test = alg_test_null,
2933 }, {
2934 .alg = "drbg_nopr_sha256",
2935 .test = alg_test_drbg,
2936 .fips_allowed = 1,
2937 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002938 .drbg = __VECS(drbg_nopr_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002939 }
2940 }, {
2941 /* covered by drbg_nopr_sha256 test */
2942 .alg = "drbg_nopr_sha384",
2943 .fips_allowed = 1,
2944 .test = alg_test_null,
2945 }, {
2946 .alg = "drbg_nopr_sha512",
2947 .fips_allowed = 1,
2948 .test = alg_test_null,
2949 }, {
2950 .alg = "drbg_pr_ctr_aes128",
2951 .test = alg_test_drbg,
2952 .fips_allowed = 1,
2953 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002954 .drbg = __VECS(drbg_pr_ctr_aes128_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002955 }
2956 }, {
2957 /* covered by drbg_pr_ctr_aes128 test */
2958 .alg = "drbg_pr_ctr_aes192",
2959 .fips_allowed = 1,
2960 .test = alg_test_null,
2961 }, {
2962 .alg = "drbg_pr_ctr_aes256",
2963 .fips_allowed = 1,
2964 .test = alg_test_null,
2965 }, {
2966 .alg = "drbg_pr_hmac_sha1",
2967 .fips_allowed = 1,
2968 .test = alg_test_null,
2969 }, {
2970 .alg = "drbg_pr_hmac_sha256",
2971 .test = alg_test_drbg,
2972 .fips_allowed = 1,
2973 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002974 .drbg = __VECS(drbg_pr_hmac_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002975 }
2976 }, {
2977 /* covered by drbg_pr_hmac_sha256 test */
2978 .alg = "drbg_pr_hmac_sha384",
2979 .fips_allowed = 1,
2980 .test = alg_test_null,
2981 }, {
2982 .alg = "drbg_pr_hmac_sha512",
2983 .test = alg_test_null,
2984 .fips_allowed = 1,
2985 }, {
2986 .alg = "drbg_pr_sha1",
2987 .fips_allowed = 1,
2988 .test = alg_test_null,
2989 }, {
2990 .alg = "drbg_pr_sha256",
2991 .test = alg_test_drbg,
2992 .fips_allowed = 1,
2993 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00002994 .drbg = __VECS(drbg_pr_sha256_tv_template)
Stephan Mueller64d1cdf2014-05-31 17:25:36 +02002995 }
2996 }, {
2997 /* covered by drbg_pr_sha256 test */
2998 .alg = "drbg_pr_sha384",
2999 .fips_allowed = 1,
3000 .test = alg_test_null,
3001 }, {
3002 .alg = "drbg_pr_sha512",
3003 .fips_allowed = 1,
3004 .test = alg_test_null,
3005 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003006 .alg = "ecb(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003007 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003008 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003009 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003010 .cipher = __VECS(aes_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003011 }
3012 }, {
3013 .alg = "ecb(anubis)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003014 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003015 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003016 .cipher = __VECS(anubis_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003017 }
3018 }, {
3019 .alg = "ecb(arc4)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003020 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003021 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003022 .cipher = __VECS(arc4_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003023 }
3024 }, {
3025 .alg = "ecb(blowfish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003026 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003027 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003028 .cipher = __VECS(bf_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003029 }
3030 }, {
3031 .alg = "ecb(camellia)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003032 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003033 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003034 .cipher = __VECS(camellia_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003035 }
3036 }, {
3037 .alg = "ecb(cast5)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003038 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003039 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003040 .cipher = __VECS(cast5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003041 }
3042 }, {
3043 .alg = "ecb(cast6)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003044 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003045 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003046 .cipher = __VECS(cast6_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003047 }
3048 }, {
Jussi Kivilinnae4483702013-04-07 16:43:56 +03003049 .alg = "ecb(cipher_null)",
3050 .test = alg_test_null,
Milan Broz6175ca22017-04-21 13:03:06 +02003051 .fips_allowed = 1,
Jussi Kivilinnae4483702013-04-07 16:43:56 +03003052 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003053 .alg = "ecb(des)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003054 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003055 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003056 .cipher = __VECS(des_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003057 }
3058 }, {
3059 .alg = "ecb(des3_ede)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003060 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003061 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003062 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003063 .cipher = __VECS(des3_ede_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003064 }
3065 }, {
Jussi Kivilinna66e5bd02013-01-19 13:31:36 +02003066 .alg = "ecb(fcrypt)",
3067 .test = alg_test_skcipher,
3068 .suite = {
3069 .cipher = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003070 .vecs = fcrypt_pcbc_tv_template,
3071 .count = 1
Jussi Kivilinna66e5bd02013-01-19 13:31:36 +02003072 }
3073 }
3074 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003075 .alg = "ecb(khazad)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003076 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003077 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003078 .cipher = __VECS(khazad_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003079 }
3080 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01003081 /* Same as ecb(aes) except the key is stored in
3082 * hardware secure memory which we reference by index
3083 */
3084 .alg = "ecb(paes)",
3085 .test = alg_test_null,
3086 .fips_allowed = 1,
3087 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003088 .alg = "ecb(seed)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003089 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003090 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003091 .cipher = __VECS(seed_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003092 }
3093 }, {
3094 .alg = "ecb(serpent)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003095 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003096 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003097 .cipher = __VECS(serpent_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003098 }
3099 }, {
Gilad Ben-Yossefcd83a8a2018-03-06 09:44:43 +00003100 .alg = "ecb(sm4)",
3101 .test = alg_test_skcipher,
3102 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003103 .cipher = __VECS(sm4_tv_template)
Gilad Ben-Yossefcd83a8a2018-03-06 09:44:43 +00003104 }
3105 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003106 .alg = "ecb(tea)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003107 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003108 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003109 .cipher = __VECS(tea_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003110 }
3111 }, {
3112 .alg = "ecb(tnepres)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003113 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003114 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003115 .cipher = __VECS(tnepres_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003116 }
3117 }, {
3118 .alg = "ecb(twofish)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003119 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003120 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003121 .cipher = __VECS(tf_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003122 }
3123 }, {
3124 .alg = "ecb(xeta)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003125 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003126 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003127 .cipher = __VECS(xeta_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003128 }
3129 }, {
3130 .alg = "ecb(xtea)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003131 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003132 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003133 .cipher = __VECS(xtea_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003134 }
3135 }, {
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +01003136 .alg = "ecdh",
3137 .test = alg_test_kpp,
3138 .fips_allowed = 1,
3139 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003140 .kpp = __VECS(ecdh_tv_template)
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +01003141 }
3142 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003143 .alg = "gcm(aes)",
3144 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003145 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003146 .suite = {
3147 .aead = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003148 .enc = __VECS(aes_gcm_enc_tv_template),
3149 .dec = __VECS(aes_gcm_dec_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003150 }
3151 }
3152 }, {
Youquan, Song507069c2009-11-23 20:23:04 +08003153 .alg = "ghash",
3154 .test = alg_test_hash,
Jarod Wilson18c0ebd2011-01-29 15:14:35 +11003155 .fips_allowed = 1,
Youquan, Song507069c2009-11-23 20:23:04 +08003156 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003157 .hash = __VECS(ghash_tv_template)
Youquan, Song507069c2009-11-23 20:23:04 +08003158 }
3159 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003160 .alg = "hmac(md5)",
3161 .test = alg_test_hash,
3162 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003163 .hash = __VECS(hmac_md5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003164 }
3165 }, {
3166 .alg = "hmac(rmd128)",
3167 .test = alg_test_hash,
3168 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003169 .hash = __VECS(hmac_rmd128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003170 }
3171 }, {
3172 .alg = "hmac(rmd160)",
3173 .test = alg_test_hash,
3174 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003175 .hash = __VECS(hmac_rmd160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003176 }
3177 }, {
3178 .alg = "hmac(sha1)",
3179 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003180 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003181 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003182 .hash = __VECS(hmac_sha1_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003183 }
3184 }, {
3185 .alg = "hmac(sha224)",
3186 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003187 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003188 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003189 .hash = __VECS(hmac_sha224_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003190 }
3191 }, {
3192 .alg = "hmac(sha256)",
3193 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003194 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003195 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003196 .hash = __VECS(hmac_sha256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003197 }
3198 }, {
raveendra padasalagi98eca722016-07-01 11:16:54 +05303199 .alg = "hmac(sha3-224)",
3200 .test = alg_test_hash,
3201 .fips_allowed = 1,
3202 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003203 .hash = __VECS(hmac_sha3_224_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05303204 }
3205 }, {
3206 .alg = "hmac(sha3-256)",
3207 .test = alg_test_hash,
3208 .fips_allowed = 1,
3209 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003210 .hash = __VECS(hmac_sha3_256_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05303211 }
3212 }, {
3213 .alg = "hmac(sha3-384)",
3214 .test = alg_test_hash,
3215 .fips_allowed = 1,
3216 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003217 .hash = __VECS(hmac_sha3_384_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05303218 }
3219 }, {
3220 .alg = "hmac(sha3-512)",
3221 .test = alg_test_hash,
3222 .fips_allowed = 1,
3223 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003224 .hash = __VECS(hmac_sha3_512_tv_template)
raveendra padasalagi98eca722016-07-01 11:16:54 +05303225 }
3226 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003227 .alg = "hmac(sha384)",
3228 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003229 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003230 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003231 .hash = __VECS(hmac_sha384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003232 }
3233 }, {
3234 .alg = "hmac(sha512)",
3235 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003236 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003237 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003238 .hash = __VECS(hmac_sha512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003239 }
3240 }, {
Vitaly Chikunov25a0b9d2018-11-07 00:00:03 +03003241 .alg = "hmac(streebog256)",
3242 .test = alg_test_hash,
3243 .suite = {
3244 .hash = __VECS(hmac_streebog256_tv_template)
3245 }
3246 }, {
3247 .alg = "hmac(streebog512)",
3248 .test = alg_test_hash,
3249 .suite = {
3250 .hash = __VECS(hmac_streebog512_tv_template)
3251 }
3252 }, {
Stephan Muellerbb5530e2015-05-25 15:10:20 +02003253 .alg = "jitterentropy_rng",
3254 .fips_allowed = 1,
3255 .test = alg_test_null,
3256 }, {
Stephan Mueller35351982015-09-21 20:59:56 +02003257 .alg = "kw(aes)",
3258 .test = alg_test_skcipher,
3259 .fips_allowed = 1,
3260 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003261 .cipher = __VECS(aes_kw_tv_template)
Stephan Mueller35351982015-09-21 20:59:56 +02003262 }
3263 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003264 .alg = "lrw(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003265 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003266 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003267 .cipher = __VECS(aes_lrw_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003268 }
3269 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02003270 .alg = "lrw(camellia)",
3271 .test = alg_test_skcipher,
3272 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003273 .cipher = __VECS(camellia_lrw_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02003274 }
3275 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003276 .alg = "lrw(cast6)",
3277 .test = alg_test_skcipher,
3278 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003279 .cipher = __VECS(cast6_lrw_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003280 }
3281 }, {
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03003282 .alg = "lrw(serpent)",
3283 .test = alg_test_skcipher,
3284 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003285 .cipher = __VECS(serpent_lrw_tv_template)
Jussi Kivilinnad7bfc0f2011-10-18 13:32:34 +03003286 }
3287 }, {
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03003288 .alg = "lrw(twofish)",
3289 .test = alg_test_skcipher,
3290 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003291 .cipher = __VECS(tf_lrw_tv_template)
Jussi Kivilinna0b2a1552011-10-18 13:32:50 +03003292 }
3293 }, {
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02003294 .alg = "lz4",
3295 .test = alg_test_comp,
3296 .fips_allowed = 1,
3297 .suite = {
3298 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003299 .comp = __VECS(lz4_comp_tv_template),
3300 .decomp = __VECS(lz4_decomp_tv_template)
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02003301 }
3302 }
3303 }, {
3304 .alg = "lz4hc",
3305 .test = alg_test_comp,
3306 .fips_allowed = 1,
3307 .suite = {
3308 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003309 .comp = __VECS(lz4hc_comp_tv_template),
3310 .decomp = __VECS(lz4hc_decomp_tv_template)
KOVACS Krisztian1443cc92014-08-22 10:44:36 +02003311 }
3312 }
3313 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003314 .alg = "lzo",
3315 .test = alg_test_comp,
Milan Broz08189042012-12-06 17:16:28 +08003316 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003317 .suite = {
3318 .comp = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003319 .comp = __VECS(lzo_comp_tv_template),
3320 .decomp = __VECS(lzo_decomp_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003321 }
3322 }
3323 }, {
3324 .alg = "md4",
3325 .test = alg_test_hash,
3326 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003327 .hash = __VECS(md4_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003328 }
3329 }, {
3330 .alg = "md5",
3331 .test = alg_test_hash,
3332 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003333 .hash = __VECS(md5_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003334 }
3335 }, {
3336 .alg = "michael_mic",
3337 .test = alg_test_hash,
3338 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003339 .hash = __VECS(michael_mic_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003340 }
3341 }, {
Ondrej Mosnacek4feb4c52018-05-11 14:19:10 +02003342 .alg = "morus1280",
3343 .test = alg_test_aead,
3344 .suite = {
3345 .aead = {
3346 .enc = __VECS(morus1280_enc_tv_template),
3347 .dec = __VECS(morus1280_dec_tv_template),
3348 }
3349 }
3350 }, {
3351 .alg = "morus640",
3352 .test = alg_test_aead,
3353 .suite = {
3354 .aead = {
3355 .enc = __VECS(morus640_enc_tv_template),
3356 .dec = __VECS(morus640_dec_tv_template),
3357 }
3358 }
3359 }, {
Eric Biggers26609a22018-11-16 17:26:29 -08003360 .alg = "nhpoly1305",
3361 .test = alg_test_hash,
3362 .suite = {
3363 .hash = __VECS(nhpoly1305_tv_template)
3364 }
3365 }, {
Puneet Saxenaba0e14a2011-05-04 15:04:10 +10003366 .alg = "ofb(aes)",
3367 .test = alg_test_skcipher,
3368 .fips_allowed = 1,
3369 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003370 .cipher = __VECS(aes_ofb_tv_template)
Puneet Saxenaba0e14a2011-05-04 15:04:10 +10003371 }
3372 }, {
Gilad Ben-Yossefa794d8d2018-04-23 08:25:14 +01003373 /* Same as ofb(aes) except the key is stored in
3374 * hardware secure memory which we reference by index
3375 */
3376 .alg = "ofb(paes)",
3377 .test = alg_test_null,
3378 .fips_allowed = 1,
3379 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003380 .alg = "pcbc(fcrypt)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003381 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003382 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003383 .cipher = __VECS(fcrypt_pcbc_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003384 }
3385 }, {
Stephan Mueller12071072017-06-12 23:27:51 +02003386 .alg = "pkcs1pad(rsa,sha224)",
3387 .test = alg_test_null,
3388 .fips_allowed = 1,
3389 }, {
3390 .alg = "pkcs1pad(rsa,sha256)",
3391 .test = alg_test_akcipher,
3392 .fips_allowed = 1,
3393 .suite = {
3394 .akcipher = __VECS(pkcs1pad_rsa_tv_template)
3395 }
3396 }, {
3397 .alg = "pkcs1pad(rsa,sha384)",
3398 .test = alg_test_null,
3399 .fips_allowed = 1,
3400 }, {
3401 .alg = "pkcs1pad(rsa,sha512)",
3402 .test = alg_test_null,
3403 .fips_allowed = 1,
3404 }, {
Martin Willieee9dc62015-06-01 13:43:59 +02003405 .alg = "poly1305",
3406 .test = alg_test_hash,
3407 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003408 .hash = __VECS(poly1305_tv_template)
Martin Willieee9dc62015-06-01 13:43:59 +02003409 }
3410 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003411 .alg = "rfc3686(ctr(aes))",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003412 .test = alg_test_skcipher,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003413 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003414 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003415 .cipher = __VECS(aes_ctr_rfc3686_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003416 }
3417 }, {
Herbert Xu3f31a742015-07-09 07:17:34 +08003418 .alg = "rfc4106(gcm(aes))",
Adrian Hoban69435b92010-11-04 15:02:04 -04003419 .test = alg_test_aead,
Jarod Wilsondb71f29a2015-01-23 12:42:15 -05003420 .fips_allowed = 1,
Adrian Hoban69435b92010-11-04 15:02:04 -04003421 .suite = {
3422 .aead = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003423 .enc = __VECS(aes_gcm_rfc4106_enc_tv_template),
3424 .dec = __VECS(aes_gcm_rfc4106_dec_tv_template)
Adrian Hoban69435b92010-11-04 15:02:04 -04003425 }
3426 }
3427 }, {
Herbert Xu544c4362015-07-14 16:53:22 +08003428 .alg = "rfc4309(ccm(aes))",
Jarod Wilson5d667322009-05-04 19:23:40 +08003429 .test = alg_test_aead,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003430 .fips_allowed = 1,
Jarod Wilson5d667322009-05-04 19:23:40 +08003431 .suite = {
3432 .aead = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003433 .enc = __VECS(aes_ccm_rfc4309_enc_tv_template),
3434 .dec = __VECS(aes_ccm_rfc4309_dec_tv_template)
Jarod Wilson5d667322009-05-04 19:23:40 +08003435 }
3436 }
3437 }, {
Herbert Xubb687452015-06-16 13:54:24 +08003438 .alg = "rfc4543(gcm(aes))",
Jussi Kivilinnae9b74412013-04-07 16:43:51 +03003439 .test = alg_test_aead,
3440 .suite = {
3441 .aead = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003442 .enc = __VECS(aes_gcm_rfc4543_enc_tv_template),
3443 .dec = __VECS(aes_gcm_rfc4543_dec_tv_template),
Jussi Kivilinnae9b74412013-04-07 16:43:51 +03003444 }
3445 }
3446 }, {
Martin Williaf2b76b2015-06-01 13:44:01 +02003447 .alg = "rfc7539(chacha20,poly1305)",
3448 .test = alg_test_aead,
3449 .suite = {
3450 .aead = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003451 .enc = __VECS(rfc7539_enc_tv_template),
3452 .dec = __VECS(rfc7539_dec_tv_template),
Martin Williaf2b76b2015-06-01 13:44:01 +02003453 }
3454 }
3455 }, {
Martin Willi59007582015-06-01 13:44:03 +02003456 .alg = "rfc7539esp(chacha20,poly1305)",
3457 .test = alg_test_aead,
3458 .suite = {
3459 .aead = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003460 .enc = __VECS(rfc7539esp_enc_tv_template),
3461 .dec = __VECS(rfc7539esp_dec_tv_template),
Martin Willi59007582015-06-01 13:44:03 +02003462 }
3463 }
3464 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003465 .alg = "rmd128",
3466 .test = alg_test_hash,
3467 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003468 .hash = __VECS(rmd128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003469 }
3470 }, {
3471 .alg = "rmd160",
3472 .test = alg_test_hash,
3473 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003474 .hash = __VECS(rmd160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003475 }
3476 }, {
3477 .alg = "rmd256",
3478 .test = alg_test_hash,
3479 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003480 .hash = __VECS(rmd256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003481 }
3482 }, {
3483 .alg = "rmd320",
3484 .test = alg_test_hash,
3485 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003486 .hash = __VECS(rmd320_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003487 }
3488 }, {
Tadeusz Struk946cc462015-06-16 10:31:06 -07003489 .alg = "rsa",
3490 .test = alg_test_akcipher,
3491 .fips_allowed = 1,
3492 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003493 .akcipher = __VECS(rsa_tv_template)
Tadeusz Struk946cc462015-06-16 10:31:06 -07003494 }
3495 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003496 .alg = "salsa20",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003497 .test = alg_test_skcipher,
Herbert Xuda7f0332008-07-31 17:08:25 +08003498 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003499 .cipher = __VECS(salsa20_stream_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003500 }
3501 }, {
3502 .alg = "sha1",
3503 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003504 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003505 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003506 .hash = __VECS(sha1_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003507 }
3508 }, {
3509 .alg = "sha224",
3510 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003511 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003512 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003513 .hash = __VECS(sha224_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003514 }
3515 }, {
3516 .alg = "sha256",
3517 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003518 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003519 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003520 .hash = __VECS(sha256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003521 }
3522 }, {
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05303523 .alg = "sha3-224",
3524 .test = alg_test_hash,
3525 .fips_allowed = 1,
3526 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003527 .hash = __VECS(sha3_224_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05303528 }
3529 }, {
3530 .alg = "sha3-256",
3531 .test = alg_test_hash,
3532 .fips_allowed = 1,
3533 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003534 .hash = __VECS(sha3_256_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05303535 }
3536 }, {
3537 .alg = "sha3-384",
3538 .test = alg_test_hash,
3539 .fips_allowed = 1,
3540 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003541 .hash = __VECS(sha3_384_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05303542 }
3543 }, {
3544 .alg = "sha3-512",
3545 .test = alg_test_hash,
3546 .fips_allowed = 1,
3547 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003548 .hash = __VECS(sha3_512_tv_template)
raveendra padasalagi79cc6ab2016-06-17 10:30:36 +05303549 }
3550 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003551 .alg = "sha384",
3552 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003553 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003554 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003555 .hash = __VECS(sha384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003556 }
3557 }, {
3558 .alg = "sha512",
3559 .test = alg_test_hash,
Jarod Wilsona1915d52009-05-15 15:16:03 +10003560 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003561 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003562 .hash = __VECS(sha512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003563 }
3564 }, {
Gilad Ben-Yossefb7e27532017-08-21 13:51:29 +03003565 .alg = "sm3",
3566 .test = alg_test_hash,
3567 .suite = {
3568 .hash = __VECS(sm3_tv_template)
3569 }
3570 }, {
Vitaly Chikunov25a0b9d2018-11-07 00:00:03 +03003571 .alg = "streebog256",
3572 .test = alg_test_hash,
3573 .suite = {
3574 .hash = __VECS(streebog256_tv_template)
3575 }
3576 }, {
3577 .alg = "streebog512",
3578 .test = alg_test_hash,
3579 .suite = {
3580 .hash = __VECS(streebog512_tv_template)
3581 }
3582 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003583 .alg = "tgr128",
3584 .test = alg_test_hash,
3585 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003586 .hash = __VECS(tgr128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003587 }
3588 }, {
3589 .alg = "tgr160",
3590 .test = alg_test_hash,
3591 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003592 .hash = __VECS(tgr160_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003593 }
3594 }, {
3595 .alg = "tgr192",
3596 .test = alg_test_hash,
3597 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003598 .hash = __VECS(tgr192_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003599 }
3600 }, {
Eric Biggersed331ad2018-06-18 10:22:39 -07003601 .alg = "vmac64(aes)",
3602 .test = alg_test_hash,
3603 .suite = {
3604 .hash = __VECS(vmac64_aes_tv_template)
3605 }
3606 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003607 .alg = "wp256",
3608 .test = alg_test_hash,
3609 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003610 .hash = __VECS(wp256_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003611 }
3612 }, {
3613 .alg = "wp384",
3614 .test = alg_test_hash,
3615 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003616 .hash = __VECS(wp384_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003617 }
3618 }, {
3619 .alg = "wp512",
3620 .test = alg_test_hash,
3621 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003622 .hash = __VECS(wp512_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003623 }
3624 }, {
3625 .alg = "xcbc(aes)",
3626 .test = alg_test_hash,
3627 .suite = {
Ard Biesheuvel21c8e722017-01-12 13:40:39 +00003628 .hash = __VECS(aes_xcbc128_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003629 }
3630 }, {
Eric Biggersaa762402018-11-16 17:26:22 -08003631 .alg = "xchacha12",
3632 .test = alg_test_skcipher,
3633 .suite = {
3634 .cipher = __VECS(xchacha12_tv_template)
3635 },
3636 }, {
Eric Biggersde61d7a2018-11-16 17:26:20 -08003637 .alg = "xchacha20",
3638 .test = alg_test_skcipher,
3639 .suite = {
3640 .cipher = __VECS(xchacha20_tv_template)
3641 },
3642 }, {
Herbert Xuda7f0332008-07-31 17:08:25 +08003643 .alg = "xts(aes)",
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003644 .test = alg_test_skcipher,
Jarod Wilson2918aa82011-01-29 15:14:01 +11003645 .fips_allowed = 1,
Herbert Xuda7f0332008-07-31 17:08:25 +08003646 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003647 .cipher = __VECS(aes_xts_tv_template)
Herbert Xuda7f0332008-07-31 17:08:25 +08003648 }
Geert Uytterhoeven0c01aed2009-03-04 15:42:15 +08003649 }, {
Jussi Kivilinna08406052012-03-05 20:26:21 +02003650 .alg = "xts(camellia)",
3651 .test = alg_test_skcipher,
3652 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003653 .cipher = __VECS(camellia_xts_tv_template)
Jussi Kivilinna08406052012-03-05 20:26:21 +02003654 }
3655 }, {
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003656 .alg = "xts(cast6)",
3657 .test = alg_test_skcipher,
3658 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003659 .cipher = __VECS(cast6_xts_tv_template)
Johannes Goetzfried9b8b0402012-07-11 19:38:29 +02003660 }
3661 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01003662 /* Same as xts(aes) except the key is stored in
3663 * hardware secure memory which we reference by index
3664 */
3665 .alg = "xts(paes)",
3666 .test = alg_test_null,
3667 .fips_allowed = 1,
3668 }, {
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03003669 .alg = "xts(serpent)",
3670 .test = alg_test_skcipher,
3671 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003672 .cipher = __VECS(serpent_xts_tv_template)
Jussi Kivilinna18be20b92011-10-18 13:33:17 +03003673 }
3674 }, {
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03003675 .alg = "xts(twofish)",
3676 .test = alg_test_skcipher,
3677 .suite = {
Eric Biggers92a4c9f2018-05-20 22:50:29 -07003678 .cipher = __VECS(tf_xts_tv_template)
Jussi Kivilinnaaed265b2011-10-18 13:33:33 +03003679 }
Giovanni Cabiddua368f432017-04-21 21:54:30 +01003680 }, {
Gilad Ben-Yossef15f47ce2018-05-11 09:04:06 +01003681 .alg = "xts4096(paes)",
3682 .test = alg_test_null,
3683 .fips_allowed = 1,
3684 }, {
3685 .alg = "xts512(paes)",
3686 .test = alg_test_null,
3687 .fips_allowed = 1,
3688 }, {
Giovanni Cabiddua368f432017-04-21 21:54:30 +01003689 .alg = "zlib-deflate",
3690 .test = alg_test_comp,
3691 .fips_allowed = 1,
3692 .suite = {
3693 .comp = {
3694 .comp = __VECS(zlib_deflate_comp_tv_template),
3695 .decomp = __VECS(zlib_deflate_decomp_tv_template)
3696 }
3697 }
Nick Terrelld28fc3d2018-03-30 12:14:53 -07003698 }, {
3699 .alg = "zstd",
3700 .test = alg_test_comp,
3701 .fips_allowed = 1,
3702 .suite = {
3703 .comp = {
3704 .comp = __VECS(zstd_comp_tv_template),
3705 .decomp = __VECS(zstd_decomp_tv_template)
3706 }
3707 }
Herbert Xuda7f0332008-07-31 17:08:25 +08003708 }
3709};
3710
Jussi Kivilinna57147582013-06-13 17:37:40 +03003711static bool alg_test_descs_checked;
3712
3713static void alg_test_descs_check_order(void)
3714{
3715 int i;
3716
3717 /* only check once */
3718 if (alg_test_descs_checked)
3719 return;
3720
3721 alg_test_descs_checked = true;
3722
3723 for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) {
3724 int diff = strcmp(alg_test_descs[i - 1].alg,
3725 alg_test_descs[i].alg);
3726
3727 if (WARN_ON(diff > 0)) {
3728 pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n",
3729 alg_test_descs[i - 1].alg,
3730 alg_test_descs[i].alg);
3731 }
3732
3733 if (WARN_ON(diff == 0)) {
3734 pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n",
3735 alg_test_descs[i].alg);
3736 }
3737 }
3738}
3739
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003740static int alg_find_test(const char *alg)
Herbert Xuda7f0332008-07-31 17:08:25 +08003741{
3742 int start = 0;
3743 int end = ARRAY_SIZE(alg_test_descs);
3744
3745 while (start < end) {
3746 int i = (start + end) / 2;
3747 int diff = strcmp(alg_test_descs[i].alg, alg);
3748
3749 if (diff > 0) {
3750 end = i;
3751 continue;
3752 }
3753
3754 if (diff < 0) {
3755 start = i + 1;
3756 continue;
3757 }
3758
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003759 return i;
Herbert Xuda7f0332008-07-31 17:08:25 +08003760 }
3761
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003762 return -1;
3763}
3764
3765int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
3766{
3767 int i;
Herbert Xua68f6612009-07-02 16:32:12 +08003768 int j;
Neil Hormand12d6b62008-10-12 20:36:51 +08003769 int rc;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003770
Richard W.M. Jones9e5c9fe2016-05-03 10:00:17 +01003771 if (!fips_enabled && notests) {
3772 printk_once(KERN_INFO "alg: self-tests disabled\n");
3773 return 0;
3774 }
3775
Jussi Kivilinna57147582013-06-13 17:37:40 +03003776 alg_test_descs_check_order();
3777
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003778 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
3779 char nalg[CRYPTO_MAX_ALG_NAME];
3780
3781 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
3782 sizeof(nalg))
3783 return -ENAMETOOLONG;
3784
3785 i = alg_find_test(nalg);
3786 if (i < 0)
3787 goto notest;
3788
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10003789 if (fips_enabled && !alg_test_descs[i].fips_allowed)
3790 goto non_fips_alg;
3791
Jarod Wilson941fb322009-05-04 19:49:23 +08003792 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
3793 goto test_done;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003794 }
3795
3796 i = alg_find_test(alg);
Herbert Xua68f6612009-07-02 16:32:12 +08003797 j = alg_find_test(driver);
3798 if (i < 0 && j < 0)
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003799 goto notest;
3800
Herbert Xua68f6612009-07-02 16:32:12 +08003801 if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) ||
3802 (j >= 0 && !alg_test_descs[j].fips_allowed)))
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10003803 goto non_fips_alg;
3804
Herbert Xua68f6612009-07-02 16:32:12 +08003805 rc = 0;
3806 if (i >= 0)
3807 rc |= alg_test_descs[i].test(alg_test_descs + i, driver,
3808 type, mask);
Cristian Stoica032c8ca2013-07-18 18:57:07 +03003809 if (j >= 0 && j != i)
Herbert Xua68f6612009-07-02 16:32:12 +08003810 rc |= alg_test_descs[j].test(alg_test_descs + j, driver,
3811 type, mask);
3812
Jarod Wilson941fb322009-05-04 19:49:23 +08003813test_done:
Neil Hormand12d6b62008-10-12 20:36:51 +08003814 if (fips_enabled && rc)
3815 panic("%s: %s alg self test failed in fips mode!\n", driver, alg);
3816
Jarod Wilson29ecd4a2009-05-04 19:51:17 +08003817 if (fips_enabled && !rc)
Masanari Iida3e8cffd2014-10-07 00:37:54 +09003818 pr_info("alg: self-tests for %s (%s) passed\n", driver, alg);
Jarod Wilson29ecd4a2009-05-04 19:51:17 +08003819
Neil Hormand12d6b62008-10-12 20:36:51 +08003820 return rc;
Herbert Xu1aa4ecd2008-08-17 17:01:56 +10003821
3822notest:
Herbert Xuda7f0332008-07-31 17:08:25 +08003823 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
3824 return 0;
Jarod Wilsona3bef3a2009-05-15 15:17:05 +10003825non_fips_alg:
3826 return -EINVAL;
Herbert Xuda7f0332008-07-31 17:08:25 +08003827}
Alexander Shishkin0b767f92010-06-03 20:53:43 +10003828
Herbert Xu326a6342010-08-06 09:40:28 +08003829#endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
Alexander Shishkin0b767f92010-06-03 20:53:43 +10003830
Herbert Xuda7f0332008-07-31 17:08:25 +08003831EXPORT_SYMBOL_GPL(alg_test);