Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2026
3 : : */
4 : :
5 : : #include <dirent.h>
6 : : #include <errno.h>
7 : : #include <getopt.h>
8 : : #include <inttypes.h>
9 : : #include <limits.h>
10 : : #include <stdbool.h>
11 : : #include <stdint.h>
12 : : #include <stdio.h>
13 : : #include <stdlib.h>
14 : : #include <string.h>
15 : :
16 : : #include <jansson.h>
17 : :
18 : : #include <rte_cryptodev.h>
19 : : #include <rte_cycles.h>
20 : : #include <rte_eal.h>
21 : : #include <rte_errno.h>
22 : : #include <rte_mbuf.h>
23 : : #include <rte_mempool.h>
24 : : #include <rte_malloc.h>
25 : : #include <rte_memory.h>
26 : :
27 : : #define DEFAULT_MBUF_DATA_ROOM 16384
28 : : #define NUM_QP_DESCRIPTORS 128
29 : : #define MAX_IV_LEN 512
30 : : #define MAX_EC_BYTESIZE 66
31 : : /* CCM AAD is prefixed with an 18-byte reserved area (B0 block + length). */
32 : : #define AES_CCM_AAD_OFFSET 18
33 : : #define IV_OFFSET (sizeof(struct rte_crypto_op) + sizeof(struct rte_crypto_sym_op))
34 : :
35 : : struct wycheproof_data {
36 : : uint8_t *key;
37 : : uint8_t *iv;
38 : : uint8_t *aad;
39 : : uint8_t *msg;
40 : : uint8_t *ct;
41 : : uint8_t *tag;
42 : : uint32_t key_len;
43 : : uint32_t iv_len;
44 : : uint32_t aad_len;
45 : : uint32_t msg_len;
46 : : uint32_t ct_len;
47 : : uint32_t tag_len;
48 : : json_int_t tc_id;
49 : : const char *result;
50 : : };
51 : :
52 : : struct app_env {
53 : : uint8_t dev_id;
54 : : uint8_t hash_dev_id;
55 : : bool hash_dev_own;
56 : : uint32_t mbuf_data_room;
57 : : const char *vectors_path;
58 : : bool debug;
59 : : struct rte_mempool *mbuf_pool;
60 : : struct rte_mempool *session_pool;
61 : : struct rte_mempool *op_pool;
62 : : struct rte_mempool *asym_session_pool;
63 : : struct rte_mempool *asym_op_pool;
64 : : };
65 : :
66 : : struct app_stats {
67 : : uint64_t passed;
68 : : uint64_t failed;
69 : : uint64_t skipped_capability;
70 : : uint64_t skipped_acceptable;
71 : : uint64_t skipped_unsupported;
72 : : };
73 : :
74 : : static struct app_env env = {
75 : : .mbuf_data_room = DEFAULT_MBUF_DATA_ROOM,
76 : : };
77 : :
78 : : static void
79 : : usage(const char *program)
80 : : {
81 : : printf("%s [EAL options] -- "
82 : : "--vectors PATH [--cryptodev NAME | --cryptodev-id ID]\n"
83 : : "\t[--mbuf-dataroom BYTES] [--debug]\n",
84 : : program);
85 : : }
86 : :
87 : : static void
88 : : debug_vector(const char *type, const char *algorithm,
89 : : const struct wycheproof_data *vector, const char *reason)
90 : : {
91 : 0 : if (env.debug)
92 : : printf("%s %s tcId=%" JSON_INTEGER_FORMAT " result=%s: %s\n", type,
93 : 0 : algorithm, vector->tc_id, vector->result, reason);
94 : : }
95 : :
96 : : static void
97 : : debug_file(const char *type, const char *path, const char *reason)
98 : : {
99 : 0 : if (env.debug)
100 : : printf("%s %s: %s\n", type, path, reason);
101 : : }
102 : :
103 : : static int
104 : 0 : parse_uint32(const char *text, uint32_t *value)
105 : : {
106 : : char *end;
107 : : unsigned long parsed;
108 : :
109 : 0 : errno = 0;
110 : 0 : parsed = strtoul(text, &end, 10);
111 : 0 : if (errno != 0 || *text == '\0' || *end != '\0' || parsed > UINT32_MAX)
112 : : return -EINVAL;
113 : :
114 : 0 : *value = parsed;
115 : 0 : return 0;
116 : : }
117 : :
118 : : static int
119 : 0 : parse_args(int argc, char **argv)
120 : : {
121 : : static const struct option options[] = {
122 : : { "vectors", required_argument, NULL, 'v' },
123 : : { "cryptodev", required_argument, NULL, 'c' },
124 : : { "cryptodev-id", required_argument, NULL, 'd' },
125 : : { "mbuf-dataroom", required_argument, NULL, 'm' },
126 : : { "debug", no_argument, NULL, 'D' },
127 : : { NULL, 0, NULL, 0 },
128 : : };
129 : : bool have_device = false;
130 : : int option;
131 : :
132 : 0 : if (rte_cryptodev_count() == 0)
133 : : return -ENODEV;
134 : 0 : env.dev_id = 0;
135 : :
136 : 0 : while ((option = getopt_long(argc, argv, "v:c:d:m:D", options, NULL)) != -1) {
137 : : uint32_t value;
138 : : int dev_id;
139 : :
140 : 0 : switch (option) {
141 : 0 : case 'v':
142 : 0 : env.vectors_path = optarg;
143 : 0 : break;
144 : 0 : case 'c':
145 : 0 : dev_id = rte_cryptodev_get_dev_id(optarg);
146 : 0 : if (dev_id < 0)
147 : 0 : return dev_id;
148 : 0 : env.dev_id = dev_id;
149 : : have_device = true;
150 : 0 : break;
151 : 0 : case 'd':
152 : 0 : if (parse_uint32(optarg, &value) != 0 || value > UINT8_MAX ||
153 : 0 : !rte_cryptodev_is_valid_dev(value))
154 : 0 : return -EINVAL;
155 : 0 : env.dev_id = value;
156 : : have_device = true;
157 : 0 : break;
158 : 0 : case 'm':
159 : 0 : if (parse_uint32(optarg, &env.mbuf_data_room) != 0 ||
160 : 0 : env.mbuf_data_room < RTE_PKTMBUF_HEADROOM ||
161 : : env.mbuf_data_room > UINT16_MAX)
162 : : return -EINVAL;
163 : : break;
164 : 0 : case 'D':
165 : 0 : env.debug = true;
166 : 0 : break;
167 : : default:
168 : : return -EINVAL;
169 : : }
170 : : }
171 : :
172 : 0 : if (env.vectors_path == NULL)
173 : : return -EINVAL;
174 : 0 : if (!have_device)
175 : 0 : printf("Using cryptodev %u (%s)\n", env.dev_id,
176 : 0 : rte_cryptodev_name_get(env.dev_id));
177 : :
178 : : return 0;
179 : : }
180 : :
181 : : static int
182 : 0 : app_init(void)
183 : : {
184 : 0 : struct rte_cryptodev_config config = {
185 : 0 : .socket_id = rte_socket_id(),
186 : : .nb_queue_pairs = 1,
187 : : .ff_disable = 0,
188 : : };
189 : 0 : struct rte_cryptodev_qp_conf queue_pair = {
190 : : .nb_descriptors = NUM_QP_DESCRIPTORS,
191 : : .mp_session = NULL,
192 : : };
193 : : struct rte_cryptodev_info dev_info;
194 : : uint32_t session_size;
195 : : int ret;
196 : :
197 : 0 : rte_cryptodev_info_get(env.dev_id, &dev_info);
198 : 0 : if (dev_info.max_nb_queue_pairs < config.nb_queue_pairs)
199 : : return -ENOTSUP;
200 : :
201 : 0 : ret = rte_cryptodev_configure(env.dev_id, &config);
202 : 0 : if (ret < 0)
203 : : return ret;
204 : :
205 : 0 : env.mbuf_pool = rte_pktmbuf_pool_create("WYCHEPROOF_MBUF_POOL", 64, 0, 0,
206 : 0 : env.mbuf_data_room, rte_socket_id());
207 : 0 : if (env.mbuf_pool == NULL) {
208 : 0 : ret = -rte_errno;
209 : 0 : goto error;
210 : : }
211 : :
212 : 0 : session_size = rte_cryptodev_sym_get_private_session_size(env.dev_id);
213 : : {
214 : : /* The hashing device may differ from the target device, so size the
215 : : * session pool for the largest sym session across all devices.
216 : : */
217 : : uint8_t d;
218 : :
219 : 0 : for (d = 0; d < rte_cryptodev_count(); d++) {
220 : 0 : uint32_t s = rte_cryptodev_sym_get_private_session_size(d);
221 : :
222 : : if (s > session_size)
223 : : session_size = s;
224 : : }
225 : : }
226 : 0 : env.session_pool = rte_cryptodev_sym_session_pool_create("WYCHEPROOF_SESSION_POOL",
227 : 0 : 64, session_size, 0, 0, rte_socket_id());
228 : 0 : if (env.session_pool == NULL) {
229 : 0 : ret = -rte_errno;
230 : 0 : goto error;
231 : : }
232 : :
233 : : /* priv_size is measured from the end of the sym op; the IV lives there. */
234 : 0 : env.op_pool = rte_crypto_op_pool_create("WYCHEPROOF_OP_POOL",
235 : 0 : RTE_CRYPTO_OP_TYPE_SYMMETRIC, 64, 0, MAX_IV_LEN, rte_socket_id());
236 : 0 : if (env.op_pool == NULL) {
237 : 0 : ret = -rte_errno;
238 : 0 : goto error;
239 : : }
240 : :
241 : : /* Asymmetric pools are best-effort; DSA/ECDSA paths gate on the
242 : : * device asymmetric feature flag at runtime.
243 : : */
244 : 0 : env.asym_session_pool = rte_cryptodev_asym_session_pool_create(
245 : 0 : "WYCHEPROOF_ASYM_SESS", 16, 0, 0, rte_socket_id());
246 : 0 : env.asym_op_pool = rte_crypto_op_pool_create("WYCHEPROOF_ASYM_OP",
247 : 0 : RTE_CRYPTO_OP_TYPE_ASYMMETRIC, 64, 0, 0, rte_socket_id());
248 : :
249 : 0 : queue_pair.mp_session = env.session_pool;
250 : 0 : ret = rte_cryptodev_queue_pair_setup(env.dev_id, 0, &queue_pair,
251 : 0 : rte_socket_id());
252 : 0 : if (ret < 0)
253 : 0 : goto error;
254 : :
255 : 0 : ret = rte_cryptodev_start(env.dev_id);
256 : 0 : if (ret < 0)
257 : 0 : goto error;
258 : :
259 : : /* Digest computation for DSA/ECDSA needs a symmetric-auth device. The
260 : : * target device may be asym-only (for example QAT), so pick a separate
261 : : * sym-capable device for hashing when needed.
262 : : */
263 : 0 : env.hash_dev_id = env.dev_id;
264 : 0 : env.hash_dev_own = false;
265 : : {
266 : 0 : struct rte_cryptodev_sym_capability_idx hash_idx = {
267 : : .type = RTE_CRYPTO_SYM_XFORM_AUTH,
268 : : .algo.auth = RTE_CRYPTO_AUTH_SHA256,
269 : : };
270 : 0 : uint8_t count = rte_cryptodev_count();
271 : : uint8_t d;
272 : :
273 : 0 : if (rte_cryptodev_sym_capability_get(env.dev_id, &hash_idx) == NULL) {
274 : 0 : for (d = 0; d < count; d++) {
275 : 0 : struct rte_cryptodev_qp_conf hash_qp = {
276 : : .nb_descriptors = NUM_QP_DESCRIPTORS,
277 : : .mp_session = NULL,
278 : : };
279 : :
280 : 0 : if (d == env.dev_id)
281 : 0 : continue;
282 : 0 : if (rte_cryptodev_sym_capability_get(d, &hash_idx) == NULL)
283 : 0 : continue;
284 : 0 : if (rte_cryptodev_configure(d, &config) < 0)
285 : 0 : continue;
286 : 0 : hash_qp.mp_session = env.session_pool;
287 : 0 : if (rte_cryptodev_queue_pair_setup(d, 0, &hash_qp,
288 : 0 : rte_socket_id()) < 0 ||
289 : 0 : rte_cryptodev_start(d) < 0) {
290 : 0 : rte_cryptodev_close(d);
291 : 0 : continue;
292 : : }
293 : 0 : env.hash_dev_id = d;
294 : 0 : env.hash_dev_own = true;
295 : 0 : break;
296 : : }
297 : : }
298 : : }
299 : :
300 : 0 : return 0;
301 : :
302 : 0 : error:
303 : 0 : rte_mempool_free(env.asym_op_pool);
304 : 0 : rte_mempool_free(env.asym_session_pool);
305 : 0 : rte_mempool_free(env.op_pool);
306 : 0 : rte_mempool_free(env.session_pool);
307 : 0 : rte_mempool_free(env.mbuf_pool);
308 : 0 : env.asym_op_pool = NULL;
309 : 0 : env.asym_session_pool = NULL;
310 : 0 : env.op_pool = NULL;
311 : 0 : env.session_pool = NULL;
312 : 0 : env.mbuf_pool = NULL;
313 : 0 : rte_cryptodev_close(env.dev_id);
314 : 0 : return ret;
315 : : }
316 : :
317 : : static void
318 : 0 : app_uninit(void)
319 : : {
320 : 0 : if (env.hash_dev_own) {
321 : 0 : rte_cryptodev_stop(env.hash_dev_id);
322 : 0 : rte_cryptodev_close(env.hash_dev_id);
323 : : }
324 : 0 : rte_cryptodev_stop(env.dev_id);
325 : 0 : rte_cryptodev_close(env.dev_id);
326 : 0 : rte_mempool_free(env.asym_op_pool);
327 : 0 : rte_mempool_free(env.asym_session_pool);
328 : 0 : rte_mempool_free(env.op_pool);
329 : 0 : rte_mempool_free(env.session_pool);
330 : 0 : rte_mempool_free(env.mbuf_pool);
331 : 0 : }
332 : :
333 : : static void
334 : 0 : free_vector(struct wycheproof_data *vector)
335 : : {
336 : 0 : rte_free_sensitive(vector->key);
337 : 0 : rte_free(vector->iv);
338 : 0 : rte_free(vector->aad);
339 : 0 : rte_free(vector->msg);
340 : 0 : rte_free(vector->ct);
341 : 0 : rte_free(vector->tag);
342 : : memset(vector, 0, sizeof(*vector));
343 : 0 : }
344 : :
345 : : static int
346 : : hex_value(char character)
347 : : {
348 : 0 : if (character >= '0' && character <= '9')
349 : 0 : return character - '0';
350 : 0 : if (character >= 'a' && character <= 'f')
351 : 0 : return character - 'a' + 10;
352 : 0 : if (character >= 'A' && character <= 'F')
353 : 0 : return character - 'A' + 10;
354 : : return -1;
355 : : }
356 : :
357 : : static int
358 : 0 : decode_hex(json_t *value, uint8_t **buffer, uint32_t *length)
359 : : {
360 : : const char *hex;
361 : : size_t hex_length;
362 : : uint8_t *decoded;
363 : : size_t index;
364 : :
365 : 0 : if (!json_is_string(value))
366 : : return -EINVAL;
367 : :
368 : 0 : hex = json_string_value(value);
369 : 0 : hex_length = strlen(hex);
370 : 0 : if ((hex_length & 1) != 0 || hex_length / 2 > UINT32_MAX)
371 : : return -EINVAL;
372 : :
373 : 0 : *length = hex_length / 2;
374 : 0 : if (*length == 0)
375 : : return 0;
376 : :
377 : 0 : decoded = rte_malloc(NULL, *length, 0);
378 : 0 : if (decoded == NULL)
379 : : return -ENOMEM;
380 : :
381 : 0 : for (index = 0; index < *length; index++) {
382 : 0 : int high = hex_value(hex[index * 2]);
383 : 0 : int low = hex_value(hex[index * 2 + 1]);
384 : :
385 : 0 : if (high < 0 || low < 0) {
386 : 0 : rte_free(decoded);
387 : 0 : return -EINVAL;
388 : : }
389 : 0 : decoded[index] = (high << 4) | low;
390 : : }
391 : :
392 : 0 : *buffer = decoded;
393 : 0 : return 0;
394 : : }
395 : :
396 : : static int
397 : 0 : parse_vector(json_t *test, struct wycheproof_data *vector)
398 : : {
399 : : json_t *tc_id;
400 : : int ret;
401 : :
402 : : memset(vector, 0, sizeof(*vector));
403 : 0 : tc_id = json_object_get(test, "tcId");
404 : 0 : vector->result = json_string_value(json_object_get(test, "result"));
405 : 0 : if (!json_is_integer(tc_id) || vector->result == NULL)
406 : : return -EINVAL;
407 : 0 : vector->tc_id = json_integer_value(tc_id);
408 : :
409 : 0 : ret = decode_hex(json_object_get(test, "key"), &vector->key, &vector->key_len);
410 : 0 : if (ret != 0)
411 : 0 : goto error;
412 : 0 : ret = decode_hex(json_object_get(test, "iv"), &vector->iv, &vector->iv_len);
413 : 0 : if (ret != 0)
414 : 0 : goto error;
415 : 0 : ret = decode_hex(json_object_get(test, "aad"), &vector->aad, &vector->aad_len);
416 : 0 : if (ret != 0)
417 : 0 : goto error;
418 : 0 : ret = decode_hex(json_object_get(test, "msg"), &vector->msg, &vector->msg_len);
419 : 0 : if (ret != 0)
420 : 0 : goto error;
421 : 0 : ret = decode_hex(json_object_get(test, "ct"), &vector->ct, &vector->ct_len);
422 : 0 : if (ret != 0)
423 : 0 : goto error;
424 : 0 : ret = decode_hex(json_object_get(test, "tag"), &vector->tag, &vector->tag_len);
425 : 0 : if (ret != 0)
426 : 0 : goto error;
427 : :
428 : : return 0;
429 : :
430 : 0 : error:
431 : 0 : free_vector(vector);
432 : 0 : return ret;
433 : : }
434 : :
435 : : static int
436 : 0 : parse_mac_vector(json_t *test, struct wycheproof_data *vector)
437 : : {
438 : : json_t *tc_id;
439 : : int ret;
440 : :
441 : : memset(vector, 0, sizeof(*vector));
442 : 0 : tc_id = json_object_get(test, "tcId");
443 : 0 : vector->result = json_string_value(json_object_get(test, "result"));
444 : 0 : if (!json_is_integer(tc_id) || vector->result == NULL)
445 : : return -EINVAL;
446 : 0 : vector->tc_id = json_integer_value(tc_id);
447 : :
448 : 0 : ret = decode_hex(json_object_get(test, "key"), &vector->key, &vector->key_len);
449 : 0 : if (ret != 0)
450 : 0 : goto error;
451 : 0 : ret = decode_hex(json_object_get(test, "msg"), &vector->msg, &vector->msg_len);
452 : 0 : if (ret != 0)
453 : 0 : goto error;
454 : 0 : ret = decode_hex(json_object_get(test, "tag"), &vector->tag, &vector->tag_len);
455 : 0 : if (ret != 0)
456 : 0 : goto error;
457 : :
458 : : return 0;
459 : :
460 : 0 : error:
461 : 0 : free_vector(vector);
462 : 0 : return ret;
463 : : }
464 : :
465 : : static int
466 : 0 : parse_mac_with_iv_vector(json_t *test, struct wycheproof_data *vector)
467 : : {
468 : : int ret;
469 : :
470 : 0 : ret = parse_mac_vector(test, vector);
471 : 0 : if (ret != 0)
472 : : return ret;
473 : 0 : ret = decode_hex(json_object_get(test, "iv"), &vector->iv, &vector->iv_len);
474 : 0 : if (ret != 0)
475 : 0 : free_vector(vector);
476 : : return ret;
477 : : }
478 : :
479 : : /* Bounded wait for one completed op; NULL on timeout so a stuck PMD fails the
480 : : * vector instead of hanging the tool.
481 : : */
482 : : static struct rte_crypto_op *
483 : 0 : dequeue_one(uint8_t dev_id)
484 : : {
485 : 0 : struct rte_crypto_op *completed = NULL;
486 : 0 : uint64_t deadline = rte_get_timer_cycles() + rte_get_timer_hz();
487 : :
488 : 0 : while (rte_cryptodev_dequeue_burst(dev_id, 0, &completed, 1) == 0) {
489 : 0 : if (rte_get_timer_cycles() > deadline)
490 : : return NULL;
491 : : rte_pause();
492 : : }
493 : 0 : return completed;
494 : : }
495 : :
496 : : static int
497 : 0 : check_aead_capability(const struct wycheproof_data *vector,
498 : : enum rte_crypto_aead_algorithm algorithm)
499 : : {
500 : 0 : struct rte_cryptodev_sym_capability_idx index = {
501 : : .type = RTE_CRYPTO_SYM_XFORM_AEAD,
502 : : .algo.aead = algorithm,
503 : : };
504 : : const struct rte_cryptodev_symmetric_capability *capability;
505 : :
506 : 0 : capability = rte_cryptodev_sym_capability_get(env.dev_id, &index);
507 : 0 : if (capability == NULL)
508 : : return -ENOTSUP;
509 : :
510 : 0 : return rte_cryptodev_sym_capability_check_aead(capability, vector->key_len,
511 : 0 : vector->tag_len, vector->aad_len, vector->iv_len);
512 : : }
513 : :
514 : : static int
515 : 0 : run_aead(const struct wycheproof_data *vector, enum rte_crypto_aead_algorithm algorithm,
516 : : enum rte_crypto_aead_operation operation, uint8_t **output, uint8_t **digest,
517 : : enum rte_crypto_op_status *status)
518 : : {
519 : 0 : struct rte_crypto_sym_xform xform = { 0 };
520 : : struct rte_cryptodev_sym_session *session = NULL;
521 : 0 : struct rte_crypto_op *operation_op = NULL;
522 : : struct rte_mbuf *mbuf = NULL;
523 : : struct rte_crypto_op *completed = NULL;
524 : : struct rte_crypto_sym_op *sym_op;
525 : : uint8_t *aad = NULL;
526 : : uint8_t *input;
527 : : uint32_t input_len;
528 : : int ret = -ENOMEM;
529 : :
530 : 0 : *output = NULL;
531 : 0 : *digest = NULL;
532 : 0 : xform.type = RTE_CRYPTO_SYM_XFORM_AEAD;
533 : 0 : xform.aead.algo = algorithm;
534 : 0 : xform.aead.op = operation;
535 : 0 : xform.aead.key.data = vector->key;
536 : 0 : xform.aead.key.length = vector->key_len;
537 : 0 : xform.aead.iv.offset = IV_OFFSET;
538 : 0 : xform.aead.iv.length = vector->iv_len;
539 : 0 : xform.aead.aad_length = vector->aad_len;
540 : 0 : xform.aead.digest_length = vector->tag_len;
541 : :
542 : 0 : session = rte_cryptodev_sym_session_create(env.dev_id, &xform, env.session_pool);
543 : 0 : if (session == NULL)
544 : 0 : goto out;
545 : :
546 : 0 : operation_op = rte_crypto_op_alloc(env.op_pool, RTE_CRYPTO_OP_TYPE_SYMMETRIC);
547 : 0 : if (operation_op == NULL)
548 : 0 : goto out;
549 : :
550 : 0 : mbuf = rte_pktmbuf_alloc(env.mbuf_pool);
551 : 0 : if (mbuf == NULL)
552 : 0 : goto out;
553 : 0 : input = operation == RTE_CRYPTO_AEAD_OP_ENCRYPT ? vector->msg : vector->ct;
554 : 0 : input_len = operation == RTE_CRYPTO_AEAD_OP_ENCRYPT ? vector->msg_len : vector->ct_len;
555 : 0 : if (input_len > rte_pktmbuf_tailroom(mbuf)) {
556 : : ret = -EMSGSIZE;
557 : 0 : goto out;
558 : : }
559 : 0 : if (input_len != 0)
560 : 0 : memcpy(rte_pktmbuf_append(mbuf, input_len), input, input_len);
561 : :
562 : 0 : *digest = rte_malloc(NULL, vector->tag_len, RTE_CACHE_LINE_SIZE);
563 : 0 : if (*digest == NULL && vector->tag_len != 0)
564 : 0 : goto out;
565 : 0 : if (operation == RTE_CRYPTO_AEAD_OP_DECRYPT && vector->tag_len != 0)
566 : 0 : memcpy(*digest, vector->tag, vector->tag_len);
567 : :
568 : 0 : sym_op = operation_op->sym;
569 : 0 : sym_op->m_src = mbuf;
570 : 0 : sym_op->aead.data.offset = 0;
571 : 0 : sym_op->aead.data.length = input_len;
572 : 0 : if (algorithm == RTE_CRYPTO_AEAD_AES_CCM) {
573 : 0 : aad = rte_zmalloc(NULL,
574 : 0 : RTE_ALIGN_CEIL(vector->aad_len + AES_CCM_AAD_OFFSET, 16), 0);
575 : 0 : if (aad == NULL)
576 : 0 : goto out;
577 : 0 : if (vector->aad_len != 0)
578 : 0 : memcpy(aad + AES_CCM_AAD_OFFSET, vector->aad, vector->aad_len);
579 : 0 : sym_op->aead.aad.data = aad;
580 : 0 : sym_op->aead.aad.phys_addr = rte_malloc_virt2iova(aad);
581 : 0 : memcpy(rte_crypto_op_ctod_offset(operation_op, uint8_t *, IV_OFFSET) + 1,
582 : 0 : vector->iv, vector->iv_len);
583 : : } else {
584 : 0 : sym_op->aead.aad.data = vector->aad;
585 : 0 : sym_op->aead.aad.phys_addr = rte_malloc_virt2iova(vector->aad);
586 : 0 : memcpy(rte_crypto_op_ctod_offset(operation_op, uint8_t *, IV_OFFSET), vector->iv,
587 : 0 : vector->iv_len);
588 : : }
589 : 0 : sym_op->aead.digest.data = *digest;
590 : 0 : sym_op->aead.digest.phys_addr = rte_malloc_virt2iova(*digest);
591 : 0 : rte_crypto_op_attach_sym_session(operation_op, session);
592 : :
593 : 0 : if (rte_cryptodev_enqueue_burst(env.dev_id, 0, &operation_op, 1) != 1) {
594 : : ret = -EIO;
595 : 0 : goto out;
596 : : }
597 : 0 : completed = dequeue_one(env.dev_id);
598 : 0 : if (completed == NULL)
599 : : /* Device wedged: op still owned by the PMD. Abort without freeing
600 : : * in-flight state; teardown stops the device.
601 : : */
602 : : return -ETIMEDOUT;
603 : :
604 : 0 : *status = completed->status;
605 : 0 : if (*status == RTE_CRYPTO_OP_STATUS_SUCCESS && input_len != 0) {
606 : 0 : *output = rte_malloc(NULL, input_len, 0);
607 : 0 : if (*output == NULL)
608 : 0 : goto out;
609 : 0 : memcpy(*output, rte_pktmbuf_mtod(mbuf, uint8_t *), input_len);
610 : : }
611 : : ret = 0;
612 : :
613 : 0 : out:
614 : 0 : rte_free(aad);
615 : 0 : rte_pktmbuf_free(mbuf);
616 : 0 : rte_crypto_op_free(operation_op);
617 : 0 : rte_cryptodev_sym_session_free(env.dev_id, session);
618 : 0 : if (ret != 0) {
619 : 0 : rte_free(*output);
620 : 0 : rte_free(*digest);
621 : 0 : *output = NULL;
622 : 0 : *digest = NULL;
623 : : }
624 : : return ret;
625 : : }
626 : :
627 : : static int
628 : 0 : run_hmac(const struct wycheproof_data *vector, enum rte_crypto_auth_algorithm algorithm,
629 : : enum rte_crypto_auth_operation operation, uint8_t **digest,
630 : : enum rte_crypto_op_status *status)
631 : : {
632 : 0 : struct rte_crypto_sym_xform xform = { 0 };
633 : : struct rte_cryptodev_sym_session *session = NULL;
634 : 0 : struct rte_crypto_op *op = NULL;
635 : : struct rte_crypto_op *completed = NULL;
636 : : struct rte_mbuf *mbuf = NULL;
637 : : const struct rte_cryptodev_symmetric_capability *capability;
638 : 0 : struct rte_cryptodev_sym_capability_idx index = {
639 : : .type = RTE_CRYPTO_SYM_XFORM_AUTH,
640 : : .algo.auth = algorithm,
641 : : };
642 : : int ret = -ENOMEM;
643 : :
644 : 0 : *digest = NULL;
645 : 0 : capability = rte_cryptodev_sym_capability_get(env.dev_id, &index);
646 : 0 : if (capability == NULL || rte_cryptodev_sym_capability_check_auth(capability,
647 : 0 : vector->key_len, vector->tag_len, 0) != 0)
648 : 0 : return -ENOTSUP;
649 : :
650 : 0 : xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
651 : 0 : xform.auth.algo = algorithm;
652 : 0 : xform.auth.op = operation;
653 : 0 : xform.auth.key.data = vector->key;
654 : 0 : xform.auth.key.length = vector->key_len;
655 : 0 : xform.auth.digest_length = vector->tag_len;
656 : 0 : session = rte_cryptodev_sym_session_create(env.dev_id, &xform, env.session_pool);
657 : 0 : if (session == NULL)
658 : 0 : goto out;
659 : :
660 : 0 : op = rte_crypto_op_alloc(env.op_pool, RTE_CRYPTO_OP_TYPE_SYMMETRIC);
661 : 0 : if (op == NULL)
662 : 0 : goto out;
663 : 0 : mbuf = rte_pktmbuf_alloc(env.mbuf_pool);
664 : 0 : if (mbuf == NULL)
665 : 0 : goto out;
666 : 0 : if (vector->msg_len > rte_pktmbuf_tailroom(mbuf)) {
667 : : ret = -EMSGSIZE;
668 : 0 : goto out;
669 : : }
670 : 0 : if (vector->msg_len != 0)
671 : 0 : memcpy(rte_pktmbuf_append(mbuf, vector->msg_len), vector->msg, vector->msg_len);
672 : :
673 : 0 : *digest = rte_malloc(NULL, vector->tag_len, RTE_CACHE_LINE_SIZE);
674 : 0 : if (*digest == NULL && vector->tag_len != 0)
675 : 0 : goto out;
676 : 0 : if (operation == RTE_CRYPTO_AUTH_OP_VERIFY && vector->tag_len != 0)
677 : 0 : memcpy(*digest, vector->tag, vector->tag_len);
678 : :
679 : 0 : op->sym->m_src = mbuf;
680 : 0 : op->sym->auth.data.offset = 0;
681 : 0 : op->sym->auth.data.length = vector->msg_len;
682 : 0 : op->sym->auth.digest.data = *digest;
683 : 0 : op->sym->auth.digest.phys_addr = rte_malloc_virt2iova(*digest);
684 : 0 : rte_crypto_op_attach_sym_session(op, session);
685 : 0 : if (rte_cryptodev_enqueue_burst(env.dev_id, 0, &op, 1) != 1) {
686 : : ret = -EIO;
687 : 0 : goto out;
688 : : }
689 : 0 : completed = dequeue_one(env.dev_id);
690 : 0 : if (completed == NULL)
691 : : /* Device wedged: op still owned by the PMD. Abort without freeing
692 : : * in-flight state; teardown stops the device.
693 : : */
694 : : return -ETIMEDOUT;
695 : 0 : *status = completed->status;
696 : : ret = 0;
697 : :
698 : 0 : out:
699 : 0 : rte_pktmbuf_free(mbuf);
700 : 0 : rte_crypto_op_free(op);
701 : 0 : rte_cryptodev_sym_session_free(env.dev_id, session);
702 : 0 : if (ret != 0) {
703 : 0 : rte_free(*digest);
704 : 0 : *digest = NULL;
705 : : }
706 : : return ret;
707 : : }
708 : :
709 : : static int
710 : 0 : run_gmac(const struct wycheproof_data *vector, enum rte_crypto_auth_operation operation,
711 : : uint8_t **digest, enum rte_crypto_op_status *status)
712 : : {
713 : 0 : struct rte_crypto_sym_xform xform = { 0 };
714 : : struct rte_cryptodev_sym_session *session = NULL;
715 : 0 : struct rte_crypto_op *op = NULL;
716 : : struct rte_crypto_op *completed = NULL;
717 : : struct rte_mbuf *mbuf = NULL;
718 : : const struct rte_cryptodev_symmetric_capability *capability;
719 : 0 : struct rte_cryptodev_sym_capability_idx index = {
720 : : .type = RTE_CRYPTO_SYM_XFORM_AUTH,
721 : : .algo.auth = RTE_CRYPTO_AUTH_AES_GMAC,
722 : : };
723 : : int ret = -ENOMEM;
724 : :
725 : 0 : *digest = NULL;
726 : 0 : capability = rte_cryptodev_sym_capability_get(env.dev_id, &index);
727 : 0 : if (capability == NULL || rte_cryptodev_sym_capability_check_auth(capability,
728 : 0 : vector->key_len, vector->tag_len, vector->iv_len) != 0)
729 : 0 : return -ENOTSUP;
730 : :
731 : 0 : xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
732 : 0 : xform.auth.algo = RTE_CRYPTO_AUTH_AES_GMAC;
733 : 0 : xform.auth.op = operation;
734 : 0 : xform.auth.key.data = vector->key;
735 : 0 : xform.auth.key.length = vector->key_len;
736 : 0 : xform.auth.iv.offset = IV_OFFSET;
737 : 0 : xform.auth.iv.length = vector->iv_len;
738 : 0 : xform.auth.digest_length = vector->tag_len;
739 : 0 : session = rte_cryptodev_sym_session_create(env.dev_id, &xform, env.session_pool);
740 : 0 : if (session == NULL)
741 : 0 : goto out;
742 : :
743 : 0 : op = rte_crypto_op_alloc(env.op_pool, RTE_CRYPTO_OP_TYPE_SYMMETRIC);
744 : 0 : if (op == NULL)
745 : 0 : goto out;
746 : 0 : mbuf = rte_pktmbuf_alloc(env.mbuf_pool);
747 : 0 : if (mbuf == NULL)
748 : 0 : goto out;
749 : 0 : if (vector->msg_len > rte_pktmbuf_tailroom(mbuf)) {
750 : : ret = -EMSGSIZE;
751 : 0 : goto out;
752 : : }
753 : 0 : if (vector->msg_len != 0)
754 : 0 : memcpy(rte_pktmbuf_append(mbuf, vector->msg_len), vector->msg, vector->msg_len);
755 : :
756 : 0 : *digest = rte_malloc(NULL, vector->tag_len, RTE_CACHE_LINE_SIZE);
757 : 0 : if (*digest == NULL && vector->tag_len != 0)
758 : 0 : goto out;
759 : 0 : if (operation == RTE_CRYPTO_AUTH_OP_VERIFY && vector->tag_len != 0)
760 : 0 : memcpy(*digest, vector->tag, vector->tag_len);
761 : :
762 : 0 : op->sym->m_src = mbuf;
763 : 0 : op->sym->auth.data.offset = 0;
764 : 0 : op->sym->auth.data.length = vector->msg_len;
765 : 0 : op->sym->auth.digest.data = *digest;
766 : 0 : op->sym->auth.digest.phys_addr = rte_malloc_virt2iova(*digest);
767 : 0 : memcpy(rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET), vector->iv, vector->iv_len);
768 : : rte_crypto_op_attach_sym_session(op, session);
769 : 0 : if (rte_cryptodev_enqueue_burst(env.dev_id, 0, &op, 1) != 1) {
770 : : ret = -EIO;
771 : 0 : goto out;
772 : : }
773 : 0 : completed = dequeue_one(env.dev_id);
774 : 0 : if (completed == NULL)
775 : : /* Device wedged: op still owned by the PMD. Abort without freeing
776 : : * in-flight state; teardown stops the device.
777 : : */
778 : : return -ETIMEDOUT;
779 : 0 : *status = completed->status;
780 : : ret = 0;
781 : :
782 : 0 : out:
783 : 0 : rte_pktmbuf_free(mbuf);
784 : 0 : rte_crypto_op_free(op);
785 : 0 : rte_cryptodev_sym_session_free(env.dev_id, session);
786 : 0 : if (ret != 0) {
787 : 0 : rte_free(*digest);
788 : 0 : *digest = NULL;
789 : : }
790 : : return ret;
791 : : }
792 : :
793 : : static int
794 : 0 : validate_aead_vector(const struct wycheproof_data *vector,
795 : : enum rte_crypto_aead_algorithm algorithm, const char *name,
796 : : struct app_stats *stats)
797 : : {
798 : 0 : uint8_t *output = NULL;
799 : 0 : uint8_t *digest = NULL;
800 : 0 : enum rte_crypto_op_status status = RTE_CRYPTO_OP_STATUS_ERROR;
801 : : int ret;
802 : :
803 : 0 : if (strcmp(vector->result, "acceptable") == 0) {
804 : 0 : stats->skipped_acceptable++;
805 : : debug_vector("SKIP", name, vector, "acceptable result policy");
806 : 0 : return 0;
807 : : }
808 : 0 : if (strcmp(vector->result, "valid") != 0 && strcmp(vector->result, "invalid") != 0) {
809 : 0 : stats->skipped_unsupported++;
810 : : debug_vector("SKIP", name, vector, "unknown result");
811 : 0 : return 0;
812 : : }
813 : : /* CCM writes the IV one byte into the reserved area (after the flags byte). */
814 : 0 : if (vector->iv_len + (algorithm == RTE_CRYPTO_AEAD_AES_CCM ? 1u : 0u) > MAX_IV_LEN ||
815 : 0 : vector->msg_len != vector->ct_len ||
816 : 0 : vector->msg_len > env.mbuf_data_room - RTE_PKTMBUF_HEADROOM) {
817 : 0 : stats->skipped_unsupported++;
818 : : debug_vector("SKIP", name, vector, "unsupported message length");
819 : 0 : return 0;
820 : : }
821 : 0 : if (check_aead_capability(vector, algorithm) != 0) {
822 : 0 : stats->skipped_capability++;
823 : : debug_vector("SKIP", name, vector, "PMD capability");
824 : 0 : return 0;
825 : : }
826 : :
827 : 0 : if (strcmp(vector->result, "valid") == 0) {
828 : 0 : ret = run_aead(vector, algorithm, RTE_CRYPTO_AEAD_OP_ENCRYPT, &output, &digest,
829 : : &status);
830 : 0 : if (ret == -ETIMEDOUT)
831 : : return -ETIMEDOUT;
832 : 0 : if (ret == -EMSGSIZE)
833 : 0 : goto unsupported;
834 : 0 : if (ret != 0 || status != RTE_CRYPTO_OP_STATUS_SUCCESS ||
835 : 0 : (vector->ct_len != 0 &&
836 : 0 : memcmp(output, vector->ct, vector->ct_len) != 0) ||
837 : 0 : (vector->tag_len != 0 &&
838 : 0 : memcmp(digest, vector->tag, vector->tag_len) != 0))
839 : 0 : goto failed;
840 : 0 : rte_free(output);
841 : 0 : rte_free(digest);
842 : 0 : output = NULL;
843 : 0 : digest = NULL;
844 : 0 : ret = run_aead(vector, algorithm, RTE_CRYPTO_AEAD_OP_DECRYPT, &output, &digest,
845 : : &status);
846 : 0 : if (ret == -ETIMEDOUT)
847 : : return -ETIMEDOUT;
848 : 0 : if (ret == -EMSGSIZE)
849 : 0 : goto unsupported;
850 : 0 : if (ret != 0 || status != RTE_CRYPTO_OP_STATUS_SUCCESS ||
851 : 0 : (vector->msg_len != 0 &&
852 : 0 : memcmp(output, vector->msg, vector->msg_len) != 0))
853 : 0 : goto failed;
854 : : } else {
855 : 0 : ret = run_aead(vector, algorithm, RTE_CRYPTO_AEAD_OP_DECRYPT, &output, &digest,
856 : : &status);
857 : 0 : if (ret == -ETIMEDOUT)
858 : : return -ETIMEDOUT;
859 : 0 : if (ret == -EMSGSIZE)
860 : 0 : goto unsupported;
861 : 0 : if (ret != 0 || status != RTE_CRYPTO_OP_STATUS_AUTH_FAILED)
862 : 0 : goto failed;
863 : : }
864 : :
865 : 0 : stats->passed++;
866 : 0 : rte_free(output);
867 : 0 : rte_free(digest);
868 : 0 : return 0;
869 : :
870 : 0 : unsupported:
871 : 0 : stats->skipped_unsupported++;
872 : : debug_vector("SKIP", name, vector, "message length exceeds mbuf");
873 : 0 : rte_free(output);
874 : 0 : rte_free(digest);
875 : 0 : return 0;
876 : :
877 : 0 : failed:
878 : 0 : if (env.debug)
879 : 0 : printf("FAIL %s tcId=%" JSON_INTEGER_FORMAT " result=%s status=%d ret=%d\n",
880 : 0 : name, vector->tc_id, vector->result, status, ret);
881 : 0 : stats->failed++;
882 : 0 : rte_free(output);
883 : 0 : rte_free(digest);
884 : 0 : return 0;
885 : : }
886 : :
887 : : static int
888 : 0 : process_aead(json_t *root, enum rte_crypto_aead_algorithm algorithm,
889 : : const char *name, struct app_stats *stats)
890 : : {
891 : 0 : json_t *groups = json_object_get(root, "testGroups");
892 : : json_t *group;
893 : : size_t group_index;
894 : :
895 : 0 : if (!json_is_array(groups))
896 : : return -EINVAL;
897 : :
898 : 0 : json_array_foreach(groups, group_index, group) {
899 : 0 : json_t *tests = json_object_get(group, "tests");
900 : : json_t *test;
901 : : size_t test_index;
902 : :
903 : 0 : if (!json_is_array(tests))
904 : : return -EINVAL;
905 : 0 : json_array_foreach(tests, test_index, test) {
906 : : struct wycheproof_data vector;
907 : 0 : int ret = parse_vector(test, &vector);
908 : :
909 : 0 : if (ret != 0) {
910 : 0 : if (env.debug)
911 : : printf("SKIP %s group=%zu test=%zu: malformed vector\n",
912 : : name, group_index, test_index);
913 : 0 : stats->skipped_unsupported++;
914 : 0 : continue;
915 : : }
916 : 0 : ret = validate_aead_vector(&vector, algorithm, name, stats);
917 : 0 : if (ret == -ETIMEDOUT)
918 : 0 : return -ETIMEDOUT;
919 : 0 : free_vector(&vector);
920 : : }
921 : : }
922 : :
923 : : return 0;
924 : : }
925 : :
926 : : static int
927 : 0 : process_hmac(json_t *root, enum rte_crypto_auth_algorithm algorithm,
928 : : const char *name, struct app_stats *stats)
929 : : {
930 : 0 : json_t *groups = json_object_get(root, "testGroups");
931 : : json_t *group;
932 : : size_t group_index;
933 : :
934 : 0 : if (!json_is_array(groups))
935 : : return -EINVAL;
936 : 0 : json_array_foreach(groups, group_index, group) {
937 : 0 : json_t *tests = json_object_get(group, "tests");
938 : : json_t *test;
939 : : size_t test_index;
940 : :
941 : 0 : if (!json_is_array(tests))
942 : : return -EINVAL;
943 : 0 : json_array_foreach(tests, test_index, test) {
944 : : struct wycheproof_data vector;
945 : 0 : uint8_t *digest = NULL;
946 : 0 : enum rte_crypto_op_status status = RTE_CRYPTO_OP_STATUS_ERROR;
947 : : int ret;
948 : :
949 : 0 : ret = parse_mac_vector(test, &vector);
950 : 0 : if (ret != 0) {
951 : 0 : if (env.debug)
952 : : printf("SKIP %s group=%zu test=%zu: malformed vector\n",
953 : : name, group_index, test_index);
954 : 0 : stats->skipped_unsupported++;
955 : 0 : continue;
956 : : }
957 : 0 : if (strcmp(vector.result, "acceptable") == 0) {
958 : 0 : stats->skipped_acceptable++;
959 : : debug_vector("SKIP", name, &vector, "acceptable result policy");
960 : 0 : } else if (strcmp(vector.result, "valid") == 0) {
961 : 0 : ret = run_hmac(&vector, algorithm, RTE_CRYPTO_AUTH_OP_GENERATE,
962 : : &digest, &status);
963 : 0 : if (ret == -ENOTSUP) {
964 : 0 : stats->skipped_capability++;
965 : : debug_vector("SKIP", name, &vector, "PMD capability");
966 : 0 : } else if (ret == -ETIMEDOUT) {
967 : 0 : return -ETIMEDOUT;
968 : 0 : } else if (ret == -EMSGSIZE) {
969 : 0 : stats->skipped_unsupported++;
970 : : debug_vector("SKIP", name, &vector,
971 : : "message length exceeds mbuf");
972 : 0 : } else if (ret != 0 || status != RTE_CRYPTO_OP_STATUS_SUCCESS ||
973 : 0 : (vector.tag_len != 0 &&
974 : 0 : memcmp(digest, vector.tag, vector.tag_len) != 0)) {
975 : : debug_vector("FAIL", name, &vector,
976 : : "generated tag mismatch");
977 : 0 : stats->failed++;
978 : : } else
979 : 0 : stats->passed++;
980 : 0 : } else if (strcmp(vector.result, "invalid") == 0) {
981 : 0 : ret = run_hmac(&vector, algorithm, RTE_CRYPTO_AUTH_OP_VERIFY,
982 : : &digest, &status);
983 : 0 : if (ret == -ENOTSUP) {
984 : 0 : stats->skipped_capability++;
985 : : debug_vector("SKIP", name, &vector, "PMD capability");
986 : 0 : } else if (ret == -ETIMEDOUT) {
987 : : return -ETIMEDOUT;
988 : 0 : } else if (ret == -EMSGSIZE) {
989 : 0 : stats->skipped_unsupported++;
990 : : debug_vector("SKIP", name, &vector,
991 : : "message length exceeds mbuf");
992 : 0 : } else if (ret != 0 || status != RTE_CRYPTO_OP_STATUS_AUTH_FAILED) {
993 : : debug_vector("FAIL", name, &vector,
994 : : "authentication unexpectedly succeeded");
995 : 0 : stats->failed++;
996 : : } else
997 : 0 : stats->passed++;
998 : : } else {
999 : 0 : stats->skipped_unsupported++;
1000 : : debug_vector("SKIP", name, &vector, "unknown result");
1001 : : }
1002 : 0 : rte_free(digest);
1003 : 0 : free_vector(&vector);
1004 : : }
1005 : : }
1006 : : return 0;
1007 : : }
1008 : :
1009 : : static int
1010 : 0 : process_gmac(json_t *root, struct app_stats *stats)
1011 : : {
1012 : 0 : json_t *groups = json_object_get(root, "testGroups");
1013 : : json_t *group;
1014 : : size_t group_index;
1015 : :
1016 : 0 : if (!json_is_array(groups))
1017 : : return -EINVAL;
1018 : 0 : json_array_foreach(groups, group_index, group) {
1019 : 0 : json_t *tests = json_object_get(group, "tests");
1020 : : json_t *test;
1021 : : size_t test_index;
1022 : :
1023 : 0 : if (!json_is_array(tests))
1024 : : return -EINVAL;
1025 : 0 : json_array_foreach(tests, test_index, test) {
1026 : : struct wycheproof_data vector;
1027 : 0 : uint8_t *digest = NULL;
1028 : 0 : enum rte_crypto_op_status status = RTE_CRYPTO_OP_STATUS_ERROR;
1029 : : int ret;
1030 : :
1031 : 0 : ret = parse_mac_with_iv_vector(test, &vector);
1032 : 0 : if (ret != 0) {
1033 : 0 : if (env.debug)
1034 : : printf("SKIP AES-GMAC group=%zu test=%zu: "
1035 : : "malformed vector\n",
1036 : : group_index, test_index);
1037 : 0 : stats->skipped_unsupported++;
1038 : 0 : continue;
1039 : : }
1040 : 0 : if (vector.iv_len > MAX_IV_LEN) {
1041 : 0 : stats->skipped_unsupported++;
1042 : : debug_vector("SKIP", "AES-GMAC", &vector, "IV too large");
1043 : 0 : rte_free(digest);
1044 : 0 : free_vector(&vector);
1045 : 0 : continue;
1046 : : }
1047 : 0 : if (strcmp(vector.result, "valid") == 0)
1048 : 0 : ret = run_gmac(&vector, RTE_CRYPTO_AUTH_OP_GENERATE,
1049 : : &digest, &status);
1050 : 0 : else if (strcmp(vector.result, "invalid") == 0)
1051 : 0 : ret = run_gmac(&vector, RTE_CRYPTO_AUTH_OP_VERIFY,
1052 : : &digest, &status);
1053 : 0 : else if (strcmp(vector.result, "acceptable") == 0) {
1054 : 0 : stats->skipped_acceptable++;
1055 : : debug_vector("SKIP", "AES-GMAC", &vector,
1056 : : "acceptable result policy");
1057 : : ret = 0;
1058 : : } else {
1059 : 0 : stats->skipped_unsupported++;
1060 : : debug_vector("SKIP", "AES-GMAC", &vector, "unknown result");
1061 : : ret = 0;
1062 : : }
1063 : 0 : if (ret == -ENOTSUP) {
1064 : 0 : stats->skipped_capability++;
1065 : : debug_vector("SKIP", "AES-GMAC", &vector, "PMD capability");
1066 : 0 : } else if (ret == -ETIMEDOUT) {
1067 : 0 : return -ETIMEDOUT;
1068 : 0 : } else if (ret == -EMSGSIZE) {
1069 : 0 : stats->skipped_unsupported++;
1070 : : debug_vector("SKIP", "AES-GMAC", &vector,
1071 : : "message length exceeds mbuf");
1072 : 0 : } else if ((strcmp(vector.result, "valid") == 0 &&
1073 : 0 : (ret != 0 || status != RTE_CRYPTO_OP_STATUS_SUCCESS ||
1074 : 0 : (vector.tag_len != 0 &&
1075 : 0 : memcmp(digest, vector.tag, vector.tag_len) != 0))) ||
1076 : 0 : (strcmp(vector.result, "invalid") == 0 &&
1077 : 0 : (ret != 0 || status != RTE_CRYPTO_OP_STATUS_AUTH_FAILED))) {
1078 : : debug_vector("FAIL", "AES-GMAC", &vector,
1079 : : "unexpected operation result");
1080 : 0 : stats->failed++;
1081 : 0 : } else if (strcmp(vector.result, "valid") == 0 ||
1082 : : strcmp(vector.result, "invalid") == 0)
1083 : 0 : stats->passed++;
1084 : 0 : rte_free(digest);
1085 : 0 : free_vector(&vector);
1086 : : }
1087 : : }
1088 : : return 0;
1089 : : }
1090 : :
1091 : : struct dsa_group {
1092 : : uint8_t *p;
1093 : : uint8_t *q;
1094 : : uint8_t *g;
1095 : : uint8_t *y;
1096 : : uint32_t p_len;
1097 : : uint32_t q_len;
1098 : : uint32_t g_len;
1099 : : uint32_t y_len;
1100 : : enum rte_crypto_auth_algorithm hash;
1101 : : uint32_t digest_len;
1102 : : };
1103 : :
1104 : : static int
1105 : 0 : map_sha(const char *name, enum rte_crypto_auth_algorithm *algo, uint32_t *digest_len)
1106 : : {
1107 : 0 : if (strcmp(name, "SHA-1") == 0) {
1108 : 0 : *algo = RTE_CRYPTO_AUTH_SHA1;
1109 : 0 : *digest_len = 20;
1110 : 0 : } else if (strcmp(name, "SHA-224") == 0) {
1111 : 0 : *algo = RTE_CRYPTO_AUTH_SHA224;
1112 : 0 : *digest_len = 28;
1113 : 0 : } else if (strcmp(name, "SHA-256") == 0) {
1114 : 0 : *algo = RTE_CRYPTO_AUTH_SHA256;
1115 : 0 : *digest_len = 32;
1116 : 0 : } else if (strcmp(name, "SHA-384") == 0) {
1117 : 0 : *algo = RTE_CRYPTO_AUTH_SHA384;
1118 : 0 : *digest_len = 48;
1119 : 0 : } else if (strcmp(name, "SHA-512") == 0) {
1120 : 0 : *algo = RTE_CRYPTO_AUTH_SHA512;
1121 : 0 : *digest_len = 64;
1122 : : } else {
1123 : : return -ENOTSUP;
1124 : : }
1125 : : return 0;
1126 : : }
1127 : :
1128 : : /* Compute a plain (keyless) message digest through the cryptodev auth path. */
1129 : : static int
1130 : 0 : compute_hash(const uint8_t *msg, uint32_t msg_len, enum rte_crypto_auth_algorithm algo,
1131 : : uint8_t *digest, uint32_t digest_len)
1132 : : {
1133 : 0 : struct rte_crypto_sym_xform xform = { 0 };
1134 : : struct rte_cryptodev_sym_session *session = NULL;
1135 : 0 : struct rte_crypto_op *op = NULL;
1136 : : struct rte_crypto_op *completed = NULL;
1137 : : struct rte_mbuf *mbuf = NULL;
1138 : : const struct rte_cryptodev_symmetric_capability *capability;
1139 : 0 : struct rte_cryptodev_sym_capability_idx index = {
1140 : : .type = RTE_CRYPTO_SYM_XFORM_AUTH,
1141 : : .algo.auth = algo,
1142 : : };
1143 : : uint8_t *op_digest = NULL;
1144 : : int ret = -ENOMEM;
1145 : :
1146 : 0 : capability = rte_cryptodev_sym_capability_get(env.hash_dev_id, &index);
1147 : 0 : if (capability == NULL || rte_cryptodev_sym_capability_check_auth(capability,
1148 : : 0, digest_len, 0) != 0)
1149 : 0 : return -ENOTSUP;
1150 : :
1151 : 0 : xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1152 : 0 : xform.auth.algo = algo;
1153 : 0 : xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
1154 : 0 : xform.auth.digest_length = digest_len;
1155 : 0 : session = rte_cryptodev_sym_session_create(env.hash_dev_id, &xform, env.session_pool);
1156 : 0 : if (session == NULL)
1157 : 0 : goto out;
1158 : :
1159 : 0 : op = rte_crypto_op_alloc(env.op_pool, RTE_CRYPTO_OP_TYPE_SYMMETRIC);
1160 : 0 : if (op == NULL)
1161 : 0 : goto out;
1162 : 0 : mbuf = rte_pktmbuf_alloc(env.mbuf_pool);
1163 : 0 : if (mbuf == NULL)
1164 : 0 : goto out;
1165 : 0 : if (msg_len > rte_pktmbuf_tailroom(mbuf)) {
1166 : : ret = -EMSGSIZE;
1167 : 0 : goto out;
1168 : : }
1169 : 0 : if (msg_len != 0)
1170 : 0 : memcpy(rte_pktmbuf_append(mbuf, msg_len), msg, msg_len);
1171 : :
1172 : 0 : op_digest = rte_malloc(NULL, digest_len, RTE_CACHE_LINE_SIZE);
1173 : 0 : if (op_digest == NULL)
1174 : 0 : goto out;
1175 : 0 : op->sym->m_src = mbuf;
1176 : 0 : op->sym->auth.data.offset = 0;
1177 : 0 : op->sym->auth.data.length = msg_len;
1178 : 0 : op->sym->auth.digest.data = op_digest;
1179 : 0 : op->sym->auth.digest.phys_addr = rte_malloc_virt2iova(op_digest);
1180 : 0 : rte_crypto_op_attach_sym_session(op, session);
1181 : 0 : if (rte_cryptodev_enqueue_burst(env.hash_dev_id, 0, &op, 1) != 1) {
1182 : : ret = -EIO;
1183 : 0 : goto out;
1184 : : }
1185 : 0 : completed = dequeue_one(env.hash_dev_id);
1186 : 0 : if (completed == NULL)
1187 : : /* Device wedged: op still owned by the PMD. Abort without freeing
1188 : : * in-flight state; teardown stops the device.
1189 : : */
1190 : : return -ETIMEDOUT;
1191 : 0 : if (completed->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
1192 : : ret = -EIO;
1193 : 0 : goto out;
1194 : : }
1195 : : memcpy(digest, op_digest, digest_len);
1196 : : ret = 0;
1197 : :
1198 : 0 : out:
1199 : 0 : rte_free(op_digest);
1200 : 0 : rte_pktmbuf_free(mbuf);
1201 : 0 : rte_crypto_op_free(op);
1202 : 0 : rte_cryptodev_sym_session_free(env.hash_dev_id, session);
1203 : 0 : return ret;
1204 : : }
1205 : :
1206 : : static int
1207 : 0 : run_dsa_verify(const struct dsa_group *group, const uint8_t *msg, uint32_t msg_len,
1208 : : const uint8_t *r, uint32_t r_len, const uint8_t *s, uint32_t s_len,
1209 : : enum rte_crypto_op_status *status)
1210 : : {
1211 : 0 : struct rte_crypto_asym_xform xform = { 0 };
1212 : 0 : void *session = NULL;
1213 : 0 : struct rte_crypto_op *op = NULL;
1214 : : struct rte_crypto_op *completed = NULL;
1215 : : struct rte_cryptodev_info dev_info;
1216 : 0 : struct rte_cryptodev_asym_capability_idx index = {
1217 : : .type = RTE_CRYPTO_ASYM_XFORM_DSA,
1218 : : };
1219 : : const struct rte_cryptodev_asymmetric_xform_capability *capability;
1220 : : uint8_t digest[64];
1221 : : int ret;
1222 : :
1223 : 0 : if (env.asym_session_pool == NULL || env.asym_op_pool == NULL)
1224 : : return -ENOTSUP;
1225 : 0 : rte_cryptodev_info_get(env.dev_id, &dev_info);
1226 : 0 : if ((dev_info.feature_flags & RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO) == 0)
1227 : : return -ENOTSUP;
1228 : 0 : capability = rte_cryptodev_asym_capability_get(env.dev_id, &index);
1229 : 0 : if (capability == NULL || rte_cryptodev_asym_xform_capability_check_optype(capability,
1230 : : RTE_CRYPTO_ASYM_OP_VERIFY) == 0)
1231 : 0 : return -ENOTSUP;
1232 : :
1233 : 0 : ret = compute_hash(msg, msg_len, group->hash, digest, group->digest_len);
1234 : 0 : if (ret != 0)
1235 : : return ret;
1236 : :
1237 : 0 : xform.xform_type = RTE_CRYPTO_ASYM_XFORM_DSA;
1238 : 0 : xform.next = NULL;
1239 : 0 : xform.dsa.p.data = group->p;
1240 : 0 : xform.dsa.p.length = group->p_len;
1241 : 0 : xform.dsa.q.data = group->q;
1242 : 0 : xform.dsa.q.length = group->q_len;
1243 : 0 : xform.dsa.g.data = group->g;
1244 : 0 : xform.dsa.g.length = group->g_len;
1245 : :
1246 : : /* Coarse capability check cannot confirm every parameter; treat a
1247 : : * session-create failure as unsupported rather than a hard failure.
1248 : : */
1249 : 0 : if (rte_cryptodev_asym_session_create(env.dev_id, &xform, env.asym_session_pool,
1250 : 0 : &session) < 0 || session == NULL) {
1251 : : ret = -ENOTSUP;
1252 : 0 : goto out;
1253 : : }
1254 : : ret = -ENOMEM;
1255 : 0 : op = rte_crypto_op_alloc(env.asym_op_pool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
1256 : 0 : if (op == NULL)
1257 : 0 : goto out;
1258 : 0 : op->asym->dsa.op_type = RTE_CRYPTO_ASYM_OP_VERIFY;
1259 : : /* DSA verify operates on the message digest. */
1260 : 0 : op->asym->dsa.message.data = digest;
1261 : 0 : op->asym->dsa.message.length = group->digest_len;
1262 : 0 : op->asym->dsa.r.data = (uint8_t *)(uintptr_t)r;
1263 : 0 : op->asym->dsa.r.length = r_len;
1264 : 0 : op->asym->dsa.s.data = (uint8_t *)(uintptr_t)s;
1265 : 0 : op->asym->dsa.s.length = s_len;
1266 : 0 : op->asym->dsa.y.data = group->y;
1267 : 0 : op->asym->dsa.y.length = group->y_len;
1268 : 0 : rte_crypto_op_attach_asym_session(op, session);
1269 : 0 : if (rte_cryptodev_enqueue_burst(env.dev_id, 0, &op, 1) != 1) {
1270 : : ret = -EIO;
1271 : 0 : goto out;
1272 : : }
1273 : 0 : completed = dequeue_one(env.dev_id);
1274 : 0 : if (completed == NULL)
1275 : : /* Device wedged: op still owned by the PMD. Abort without freeing
1276 : : * in-flight state; teardown stops the device.
1277 : : */
1278 : : return -ETIMEDOUT;
1279 : 0 : *status = completed->status;
1280 : : ret = 0;
1281 : :
1282 : 0 : out:
1283 : 0 : rte_crypto_op_free(op);
1284 : 0 : if (session != NULL)
1285 : 0 : rte_cryptodev_asym_session_free(env.dev_id, session);
1286 : : return ret;
1287 : : }
1288 : :
1289 : : static int
1290 : 0 : process_dsa_p1363(json_t *root, const char *name, struct app_stats *stats)
1291 : : {
1292 : 0 : json_t *groups = json_object_get(root, "testGroups");
1293 : : json_t *group;
1294 : : size_t group_index;
1295 : :
1296 : 0 : if (!json_is_array(groups))
1297 : : return -EINVAL;
1298 : 0 : json_array_foreach(groups, group_index, group) {
1299 : 0 : json_t *pub = json_object_get(group, "publicKey");
1300 : 0 : json_t *tests = json_object_get(group, "tests");
1301 : 0 : struct dsa_group dsa = { 0 };
1302 : : const char *sha;
1303 : : json_t *test;
1304 : : size_t test_index;
1305 : :
1306 : 0 : if (!json_is_object(pub) || !json_is_array(tests))
1307 : 0 : return -EINVAL;
1308 : 0 : sha = json_string_value(json_object_get(group, "sha"));
1309 : 0 : if (sha == NULL || map_sha(sha, &dsa.hash, &dsa.digest_len) != 0) {
1310 : 0 : stats->skipped_unsupported++;
1311 : 0 : continue;
1312 : : }
1313 : 0 : if (decode_hex(json_object_get(pub, "p"), &dsa.p, &dsa.p_len) != 0 ||
1314 : 0 : decode_hex(json_object_get(pub, "q"), &dsa.q, &dsa.q_len) != 0 ||
1315 : 0 : decode_hex(json_object_get(pub, "g"), &dsa.g, &dsa.g_len) != 0 ||
1316 : 0 : decode_hex(json_object_get(pub, "y"), &dsa.y, &dsa.y_len) != 0) {
1317 : 0 : rte_free(dsa.p);
1318 : 0 : rte_free(dsa.q);
1319 : 0 : rte_free(dsa.g);
1320 : 0 : rte_free(dsa.y);
1321 : 0 : stats->skipped_unsupported++;
1322 : 0 : continue;
1323 : : }
1324 : 0 : json_array_foreach(tests, test_index, test) {
1325 : : struct wycheproof_data vector;
1326 : 0 : uint8_t *sig = NULL;
1327 : 0 : uint32_t sig_len = 0;
1328 : 0 : enum rte_crypto_op_status status = RTE_CRYPTO_OP_STATUS_ERROR;
1329 : : json_t *tc_id;
1330 : : int ret;
1331 : :
1332 : : memset(&vector, 0, sizeof(vector));
1333 : 0 : tc_id = json_object_get(test, "tcId");
1334 : 0 : vector.result = json_string_value(json_object_get(test, "result"));
1335 : 0 : if (!json_is_integer(tc_id) || vector.result == NULL) {
1336 : 0 : stats->skipped_unsupported++;
1337 : 0 : continue;
1338 : : }
1339 : 0 : vector.tc_id = json_integer_value(tc_id);
1340 : 0 : if (decode_hex(json_object_get(test, "msg"), &vector.msg,
1341 : 0 : &vector.msg_len) != 0 ||
1342 : 0 : decode_hex(json_object_get(test, "sig"),
1343 : : &sig, &sig_len) != 0) {
1344 : 0 : rte_free(vector.msg);
1345 : 0 : rte_free(sig);
1346 : 0 : stats->skipped_unsupported++;
1347 : 0 : continue;
1348 : : }
1349 : 0 : if (strcmp(vector.result, "acceptable") == 0) {
1350 : 0 : stats->skipped_acceptable++;
1351 : : debug_vector("SKIP", name, &vector, "acceptable result policy");
1352 : 0 : rte_free(vector.msg);
1353 : 0 : rte_free(sig);
1354 : 0 : continue;
1355 : : }
1356 : 0 : if ((sig_len & 1) != 0 || sig_len == 0) {
1357 : : debug_vector("SKIP", name, &vector, "malformed P1363 signature");
1358 : 0 : stats->skipped_unsupported++;
1359 : 0 : rte_free(vector.msg);
1360 : 0 : rte_free(sig);
1361 : 0 : continue;
1362 : : }
1363 : 0 : ret = run_dsa_verify(&dsa, vector.msg, vector.msg_len, sig, sig_len / 2,
1364 : 0 : sig + sig_len / 2, sig_len / 2, &status);
1365 : 0 : if (ret == -ENOTSUP) {
1366 : 0 : stats->skipped_capability++;
1367 : : debug_vector("SKIP", name, &vector, "PMD capability");
1368 : 0 : } else if (ret == -ETIMEDOUT) {
1369 : 0 : return -ETIMEDOUT;
1370 : 0 : } else if (ret == -EMSGSIZE) {
1371 : 0 : stats->skipped_unsupported++;
1372 : : debug_vector("SKIP", name, &vector, "message length exceeds mbuf");
1373 : 0 : } else if ((strcmp(vector.result, "valid") == 0 &&
1374 : 0 : (ret != 0 || status != RTE_CRYPTO_OP_STATUS_SUCCESS)) ||
1375 : 0 : (strcmp(vector.result, "invalid") == 0 &&
1376 : 0 : (ret != 0 || status == RTE_CRYPTO_OP_STATUS_SUCCESS))) {
1377 : : debug_vector("FAIL", name, &vector,
1378 : : "unexpected verification result");
1379 : 0 : stats->failed++;
1380 : 0 : } else if (strcmp(vector.result, "valid") == 0 ||
1381 : : strcmp(vector.result, "invalid") == 0) {
1382 : 0 : stats->passed++;
1383 : : } else {
1384 : 0 : stats->skipped_unsupported++;
1385 : : debug_vector("SKIP", name, &vector, "unknown result");
1386 : : }
1387 : 0 : rte_free(vector.msg);
1388 : 0 : rte_free(sig);
1389 : : }
1390 : 0 : rte_free(dsa.p);
1391 : 0 : rte_free(dsa.q);
1392 : 0 : rte_free(dsa.g);
1393 : 0 : rte_free(dsa.y);
1394 : : }
1395 : : return 0;
1396 : : }
1397 : :
1398 : : static int
1399 : 0 : map_curve(const char *name, enum rte_crypto_curve_id *curve, uint32_t *bytesize)
1400 : : {
1401 : 0 : if (strcmp(name, "secp224r1") == 0) {
1402 : 0 : *curve = RTE_CRYPTO_EC_GROUP_SECP224R1;
1403 : 0 : *bytesize = 28;
1404 : 0 : } else if (strcmp(name, "secp256r1") == 0) {
1405 : 0 : *curve = RTE_CRYPTO_EC_GROUP_SECP256R1;
1406 : 0 : *bytesize = 32;
1407 : 0 : } else if (strcmp(name, "secp384r1") == 0) {
1408 : 0 : *curve = RTE_CRYPTO_EC_GROUP_SECP384R1;
1409 : 0 : *bytesize = 48;
1410 : 0 : } else if (strcmp(name, "secp521r1") == 0) {
1411 : 0 : *curve = RTE_CRYPTO_EC_GROUP_SECP521R1;
1412 : 0 : *bytesize = 66;
1413 : : } else {
1414 : : return -ENOTSUP;
1415 : : }
1416 : : return 0;
1417 : : }
1418 : :
1419 : : /* Left-align an unsigned big-endian value into a fixed-width field. */
1420 : : static bool
1421 : 0 : value_equals_padded(const uint8_t *a, uint32_t a_len, const uint8_t *b, uint32_t b_len)
1422 : : {
1423 : 0 : while (a_len > 0 && a[0] == 0) {
1424 : 0 : a++;
1425 : 0 : a_len--;
1426 : : }
1427 : 0 : while (b_len > 0 && b[0] == 0) {
1428 : 0 : b++;
1429 : 0 : b_len--;
1430 : : }
1431 : 0 : return a_len == b_len && memcmp(a, b, a_len) == 0;
1432 : : }
1433 : :
1434 : : /* Left-pad a big-endian value into a fixed-width field: strip leading zeros,
1435 : : * reject values wider than the field, zero-pad the remaining prefix.
1436 : : */
1437 : : static int
1438 : 0 : normalize_operand(const uint8_t *value, uint32_t value_len, uint8_t *out, uint32_t bytesize)
1439 : : {
1440 : 0 : while (value_len > 0 && value[0] == 0) {
1441 : 0 : value++;
1442 : 0 : value_len--;
1443 : : }
1444 : 0 : if (value_len > bytesize)
1445 : : return -EINVAL;
1446 : 0 : memset(out, 0, bytesize);
1447 : 0 : memcpy(out + bytesize - value_len, value, value_len);
1448 : 0 : return 0;
1449 : : }
1450 : :
1451 : : static int
1452 : 0 : run_ecdh_ecpoint(enum rte_crypto_curve_id curve, uint32_t bytesize,
1453 : : const uint8_t *priv, uint32_t priv_len, const uint8_t *pub_x,
1454 : : const uint8_t *pub_y, uint8_t **shared_x, uint32_t *shared_len,
1455 : : enum rte_crypto_op_status *status)
1456 : : {
1457 : 0 : struct rte_crypto_asym_xform xform = { 0 };
1458 : 0 : void *session = NULL;
1459 : 0 : struct rte_crypto_op *op = NULL;
1460 : : struct rte_crypto_op *completed = NULL;
1461 : : struct rte_cryptodev_info dev_info;
1462 : 0 : struct rte_cryptodev_asym_capability_idx index = {
1463 : : .type = RTE_CRYPTO_ASYM_XFORM_ECDH,
1464 : : };
1465 : : const struct rte_cryptodev_asymmetric_xform_capability *capability;
1466 : : uint8_t *out_x = NULL;
1467 : : uint8_t *out_y = NULL;
1468 : : uint8_t priv_buf[MAX_EC_BYTESIZE];
1469 : : int ret;
1470 : :
1471 : 0 : *shared_x = NULL;
1472 : 0 : if (env.asym_session_pool == NULL || env.asym_op_pool == NULL)
1473 : : return -ENOTSUP;
1474 : 0 : rte_cryptodev_info_get(env.dev_id, &dev_info);
1475 : 0 : if ((dev_info.feature_flags & RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO) == 0)
1476 : : return -ENOTSUP;
1477 : 0 : capability = rte_cryptodev_asym_capability_get(env.dev_id, &index);
1478 : 0 : if (capability == NULL)
1479 : : return -ENOTSUP;
1480 : 0 : if (rte_cryptodev_asym_xform_capability_check_optype(capability,
1481 : : (enum rte_crypto_asym_op_type)
1482 : : RTE_CRYPTO_ASYM_KE_SHARED_SECRET_COMPUTE) == 0)
1483 : : return -ENOTSUP;
1484 : :
1485 : 0 : if (bytesize > sizeof(priv_buf))
1486 : : return -ENOTSUP;
1487 : 0 : if (normalize_operand(priv, priv_len, priv_buf, bytesize) != 0)
1488 : : return -EINVAL;
1489 : :
1490 : 0 : xform.xform_type = RTE_CRYPTO_ASYM_XFORM_ECDH;
1491 : 0 : xform.next = NULL;
1492 : 0 : xform.ec.curve_id = curve;
1493 : 0 : xform.ec.pkey.data = priv_buf;
1494 : 0 : xform.ec.pkey.length = bytesize;
1495 : 0 : xform.ec.q.x.data = (uint8_t *)(uintptr_t)pub_x;
1496 : 0 : xform.ec.q.x.length = bytesize;
1497 : 0 : xform.ec.q.y.data = (uint8_t *)(uintptr_t)pub_y;
1498 : 0 : xform.ec.q.y.length = bytesize;
1499 : :
1500 : : ret = -ENOMEM;
1501 : 0 : out_x = rte_zmalloc(NULL, bytesize, 0);
1502 : 0 : out_y = rte_zmalloc(NULL, bytesize, 0);
1503 : 0 : if (out_x == NULL || out_y == NULL)
1504 : 0 : goto out;
1505 : : /* Coarse capability check cannot confirm curve support; treat a
1506 : : * session-create failure as unsupported rather than a hard failure.
1507 : : */
1508 : 0 : if (rte_cryptodev_asym_session_create(env.dev_id, &xform, env.asym_session_pool,
1509 : 0 : &session) < 0 || session == NULL) {
1510 : : ret = -ENOTSUP;
1511 : 0 : goto out;
1512 : : }
1513 : 0 : op = rte_crypto_op_alloc(env.asym_op_pool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
1514 : 0 : if (op == NULL)
1515 : 0 : goto out;
1516 : 0 : op->asym->ecdh.ke_type = RTE_CRYPTO_ASYM_KE_SHARED_SECRET_COMPUTE;
1517 : 0 : op->asym->ecdh.priv_key.data = priv_buf;
1518 : 0 : op->asym->ecdh.priv_key.length = bytesize;
1519 : 0 : op->asym->ecdh.pub_key.x.data = (uint8_t *)(uintptr_t)pub_x;
1520 : 0 : op->asym->ecdh.pub_key.x.length = bytesize;
1521 : 0 : op->asym->ecdh.pub_key.y.data = (uint8_t *)(uintptr_t)pub_y;
1522 : 0 : op->asym->ecdh.pub_key.y.length = bytesize;
1523 : 0 : op->asym->ecdh.shared_secret.x.data = out_x;
1524 : 0 : op->asym->ecdh.shared_secret.x.length = bytesize;
1525 : 0 : op->asym->ecdh.shared_secret.y.data = out_y;
1526 : 0 : op->asym->ecdh.shared_secret.y.length = bytesize;
1527 : 0 : rte_crypto_op_attach_asym_session(op, session);
1528 : 0 : if (rte_cryptodev_enqueue_burst(env.dev_id, 0, &op, 1) != 1) {
1529 : : ret = -EIO;
1530 : 0 : goto out;
1531 : : }
1532 : 0 : completed = dequeue_one(env.dev_id);
1533 : 0 : if (completed == NULL)
1534 : : /* Device wedged: op still owned by the PMD. Abort without freeing
1535 : : * in-flight state; teardown stops the device.
1536 : : */
1537 : : return -ETIMEDOUT;
1538 : 0 : *status = completed->status;
1539 : 0 : if (completed->status == RTE_CRYPTO_OP_STATUS_SUCCESS) {
1540 : 0 : *shared_x = out_x;
1541 : 0 : *shared_len = completed->asym->ecdh.shared_secret.x.length;
1542 : : out_x = NULL;
1543 : : }
1544 : : ret = 0;
1545 : :
1546 : 0 : out:
1547 : : /* The private key was copied into the stack buffer; wipe it. */
1548 : 0 : rte_memzero_explicit(priv_buf, sizeof(priv_buf));
1549 : 0 : rte_free(out_x);
1550 : 0 : rte_free(out_y);
1551 : 0 : rte_crypto_op_free(op);
1552 : 0 : if (session != NULL)
1553 : 0 : rte_cryptodev_asym_session_free(env.dev_id, session);
1554 : : return ret;
1555 : : }
1556 : :
1557 : : static int
1558 : 0 : process_ecdh_ecpoint(json_t *root, const char *name, struct app_stats *stats)
1559 : : {
1560 : 0 : json_t *groups = json_object_get(root, "testGroups");
1561 : : json_t *group;
1562 : : size_t group_index;
1563 : :
1564 : 0 : if (!json_is_array(groups))
1565 : : return -EINVAL;
1566 : 0 : json_array_foreach(groups, group_index, group) {
1567 : 0 : json_t *tests = json_object_get(group, "tests");
1568 : : enum rte_crypto_curve_id curve;
1569 : : uint32_t bytesize;
1570 : : const char *curve_name;
1571 : : json_t *test;
1572 : : size_t test_index;
1573 : :
1574 : 0 : if (!json_is_array(tests))
1575 : 0 : return -EINVAL;
1576 : 0 : curve_name = json_string_value(json_object_get(group, "curve"));
1577 : 0 : if (curve_name == NULL || map_curve(curve_name, &curve, &bytesize) != 0) {
1578 : 0 : stats->skipped_unsupported++;
1579 : 0 : continue;
1580 : : }
1581 : 0 : json_array_foreach(tests, test_index, test) {
1582 : : struct wycheproof_data vector;
1583 : 0 : uint8_t *pub = NULL;
1584 : 0 : uint8_t *priv = NULL;
1585 : 0 : uint8_t *shared = NULL;
1586 : 0 : uint8_t *result_x = NULL;
1587 : 0 : uint32_t pub_len = 0, priv_len = 0, shared_len = 0, result_len = 0;
1588 : 0 : enum rte_crypto_op_status status = RTE_CRYPTO_OP_STATUS_ERROR;
1589 : : bool matched;
1590 : : json_t *tc_id;
1591 : : int ret;
1592 : :
1593 : : memset(&vector, 0, sizeof(vector));
1594 : 0 : tc_id = json_object_get(test, "tcId");
1595 : 0 : vector.result = json_string_value(json_object_get(test, "result"));
1596 : 0 : if (!json_is_integer(tc_id) || vector.result == NULL) {
1597 : 0 : stats->skipped_unsupported++;
1598 : 0 : continue;
1599 : : }
1600 : 0 : vector.tc_id = json_integer_value(tc_id);
1601 : 0 : if (decode_hex(json_object_get(test, "public"), &pub, &pub_len) != 0 ||
1602 : 0 : decode_hex(json_object_get(test, "private"), &priv,
1603 : 0 : &priv_len) != 0 ||
1604 : 0 : decode_hex(json_object_get(test, "shared"), &shared,
1605 : : &shared_len) != 0) {
1606 : 0 : rte_free(pub);
1607 : 0 : rte_free_sensitive(priv);
1608 : 0 : rte_free(shared);
1609 : 0 : stats->skipped_unsupported++;
1610 : 0 : continue;
1611 : : }
1612 : 0 : if (strcmp(vector.result, "acceptable") == 0) {
1613 : 0 : stats->skipped_acceptable++;
1614 : : debug_vector("SKIP", name, &vector, "acceptable result policy");
1615 : 0 : goto next;
1616 : : }
1617 : : /* Only uncompressed points (0x04 || x || y) map to the DPDK EC point. */
1618 : 0 : if (pub_len != 1 + 2 * bytesize || pub[0] != 0x04) {
1619 : : debug_vector("SKIP", name, &vector,
1620 : : "non-uncompressed public point");
1621 : 0 : stats->skipped_unsupported++;
1622 : 0 : goto next;
1623 : : }
1624 : : /* Invalid-curve rejection cases carry no expected shared secret;
1625 : : * on-curve validation is a separate ECDH PUB_KEY_VERIFY op, not
1626 : : * part of the raw shared-secret compute primitive.
1627 : : */
1628 : 0 : if (shared_len == 0) {
1629 : : debug_vector("SKIP", name, &vector,
1630 : : "no shared secret (point validation out of scope)");
1631 : 0 : stats->skipped_unsupported++;
1632 : 0 : goto next;
1633 : : }
1634 : 0 : ret = run_ecdh_ecpoint(curve, bytesize, priv, priv_len, pub + 1,
1635 : 0 : pub + 1 + bytesize, &result_x, &result_len, &status);
1636 : 0 : if (ret == -ENOTSUP) {
1637 : 0 : stats->skipped_capability++;
1638 : : debug_vector("SKIP", name, &vector, "PMD capability");
1639 : 0 : goto next;
1640 : : }
1641 : 0 : if (ret == -ETIMEDOUT)
1642 : 0 : return -ETIMEDOUT;
1643 : 0 : if (ret == -EINVAL) {
1644 : 0 : stats->skipped_unsupported++;
1645 : : debug_vector("SKIP", name, &vector, "operand wider than curve");
1646 : 0 : goto next;
1647 : : }
1648 : 0 : if (ret != 0) {
1649 : : debug_vector("FAIL", name, &vector, "shared-secret computation failed");
1650 : 0 : stats->failed++;
1651 : 0 : goto next;
1652 : : }
1653 : 0 : matched = ret == 0 && status == RTE_CRYPTO_OP_STATUS_SUCCESS &&
1654 : 0 : value_equals_padded(result_x, result_len, shared, shared_len);
1655 : 0 : if (strcmp(vector.result, "valid") == 0) {
1656 : 0 : if (ret == 0 && status == RTE_CRYPTO_OP_STATUS_SUCCESS && matched) {
1657 : 0 : stats->passed++;
1658 : 0 : } else if (ret == 0 && status == RTE_CRYPTO_OP_STATUS_SUCCESS) {
1659 : : debug_vector("FAIL", name, &vector, "wrong shared secret");
1660 : 0 : stats->failed++;
1661 : : } else {
1662 : : /* PMD returned an error for a valid vector. */
1663 : : debug_vector("FAIL", name, &vector,
1664 : : "PMD could not compute");
1665 : 0 : stats->failed++;
1666 : : }
1667 : 0 : } else if (strcmp(vector.result, "invalid") == 0) {
1668 : 0 : if (matched) {
1669 : : debug_vector("FAIL", name, &vector,
1670 : : "produced forbidden secret");
1671 : 0 : stats->failed++;
1672 : : } else {
1673 : 0 : stats->passed++;
1674 : : }
1675 : : } else {
1676 : 0 : stats->skipped_unsupported++;
1677 : : debug_vector("SKIP", name, &vector, "unknown result");
1678 : : }
1679 : 0 : next:
1680 : 0 : rte_free(result_x);
1681 : 0 : rte_free(pub);
1682 : 0 : rte_free_sensitive(priv);
1683 : 0 : rte_free(shared);
1684 : : }
1685 : : }
1686 : : return 0;
1687 : : }
1688 : :
1689 : : struct ecdsa_group {
1690 : : uint8_t *wx;
1691 : : uint8_t *wy;
1692 : : uint32_t wx_len;
1693 : : uint32_t wy_len;
1694 : : enum rte_crypto_curve_id curve;
1695 : : uint32_t bytesize;
1696 : : enum rte_crypto_auth_algorithm hash;
1697 : : uint32_t digest_len;
1698 : : };
1699 : :
1700 : : static int
1701 : 0 : run_ecdsa_verify(const struct ecdsa_group *group, const uint8_t *msg, uint32_t msg_len,
1702 : : const uint8_t *r, uint32_t r_len, const uint8_t *s, uint32_t s_len,
1703 : : enum rte_crypto_op_status *status)
1704 : : {
1705 : 0 : struct rte_crypto_asym_xform xform = { 0 };
1706 : 0 : void *session = NULL;
1707 : 0 : struct rte_crypto_op *op = NULL;
1708 : : struct rte_crypto_op *completed = NULL;
1709 : : struct rte_cryptodev_info dev_info;
1710 : 0 : struct rte_cryptodev_asym_capability_idx index = {
1711 : : .type = RTE_CRYPTO_ASYM_XFORM_ECDSA,
1712 : : };
1713 : : const struct rte_cryptodev_asymmetric_xform_capability *capability;
1714 : : uint8_t digest[64];
1715 : : uint8_t wx_buf[MAX_EC_BYTESIZE];
1716 : : uint8_t wy_buf[MAX_EC_BYTESIZE];
1717 : : uint32_t e_len;
1718 : : int ret;
1719 : :
1720 : 0 : if (env.asym_session_pool == NULL || env.asym_op_pool == NULL)
1721 : : return -ENOTSUP;
1722 : 0 : rte_cryptodev_info_get(env.dev_id, &dev_info);
1723 : 0 : if ((dev_info.feature_flags & RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO) == 0)
1724 : : return -ENOTSUP;
1725 : 0 : capability = rte_cryptodev_asym_capability_get(env.dev_id, &index);
1726 : 0 : if (capability == NULL || rte_cryptodev_asym_xform_capability_check_optype(capability,
1727 : : RTE_CRYPTO_ASYM_OP_VERIFY) == 0)
1728 : 0 : return -ENOTSUP;
1729 : :
1730 : 0 : ret = compute_hash(msg, msg_len, group->hash, digest, group->digest_len);
1731 : 0 : if (ret != 0)
1732 : : return ret;
1733 : : /* ECDSA uses e = leftmost Ln bits of the hash (byte-aligned truncation). */
1734 : 0 : e_len = group->digest_len > group->bytesize ? group->bytesize : group->digest_len;
1735 : :
1736 : 0 : if (group->bytesize > sizeof(wx_buf))
1737 : : return -ENOTSUP;
1738 : 0 : if (normalize_operand(group->wx, group->wx_len, wx_buf, group->bytesize) != 0 ||
1739 : 0 : normalize_operand(group->wy, group->wy_len, wy_buf, group->bytesize) != 0)
1740 : 0 : return -EINVAL;
1741 : :
1742 : 0 : xform.xform_type = RTE_CRYPTO_ASYM_XFORM_ECDSA;
1743 : 0 : xform.next = NULL;
1744 : 0 : xform.ec.curve_id = group->curve;
1745 : 0 : xform.ec.q.x.data = wx_buf;
1746 : 0 : xform.ec.q.x.length = group->bytesize;
1747 : 0 : xform.ec.q.y.data = wy_buf;
1748 : 0 : xform.ec.q.y.length = group->bytesize;
1749 : :
1750 : : /* Coarse capability check cannot confirm curve support; treat a
1751 : : * session-create failure as unsupported rather than a hard failure.
1752 : : */
1753 : 0 : if (rte_cryptodev_asym_session_create(env.dev_id, &xform, env.asym_session_pool,
1754 : 0 : &session) < 0 || session == NULL) {
1755 : : ret = -ENOTSUP;
1756 : 0 : goto out;
1757 : : }
1758 : : ret = -ENOMEM;
1759 : 0 : op = rte_crypto_op_alloc(env.asym_op_pool, RTE_CRYPTO_OP_TYPE_ASYMMETRIC);
1760 : 0 : if (op == NULL)
1761 : 0 : goto out;
1762 : 0 : op->asym->ecdsa.op_type = RTE_CRYPTO_ASYM_OP_VERIFY;
1763 : 0 : op->asym->ecdsa.message.data = digest;
1764 : 0 : op->asym->ecdsa.message.length = e_len;
1765 : 0 : op->asym->ecdsa.r.data = (uint8_t *)(uintptr_t)r;
1766 : 0 : op->asym->ecdsa.r.length = r_len;
1767 : 0 : op->asym->ecdsa.s.data = (uint8_t *)(uintptr_t)s;
1768 : 0 : op->asym->ecdsa.s.length = s_len;
1769 : 0 : rte_crypto_op_attach_asym_session(op, session);
1770 : 0 : if (rte_cryptodev_enqueue_burst(env.dev_id, 0, &op, 1) != 1) {
1771 : : ret = -EIO;
1772 : 0 : goto out;
1773 : : }
1774 : 0 : completed = dequeue_one(env.dev_id);
1775 : 0 : if (completed == NULL)
1776 : : /* Device wedged: op still owned by the PMD. Abort without freeing
1777 : : * in-flight state; teardown stops the device.
1778 : : */
1779 : : return -ETIMEDOUT;
1780 : 0 : *status = completed->status;
1781 : : ret = 0;
1782 : :
1783 : 0 : out:
1784 : 0 : rte_crypto_op_free(op);
1785 : 0 : if (session != NULL)
1786 : 0 : rte_cryptodev_asym_session_free(env.dev_id, session);
1787 : : return ret;
1788 : : }
1789 : :
1790 : : static int
1791 : 0 : process_ecdsa_p1363(json_t *root, const char *name, struct app_stats *stats)
1792 : : {
1793 : 0 : json_t *groups = json_object_get(root, "testGroups");
1794 : : json_t *group;
1795 : : size_t group_index;
1796 : :
1797 : 0 : if (!json_is_array(groups))
1798 : : return -EINVAL;
1799 : 0 : json_array_foreach(groups, group_index, group) {
1800 : 0 : json_t *pub = json_object_get(group, "publicKey");
1801 : 0 : json_t *tests = json_object_get(group, "tests");
1802 : 0 : struct ecdsa_group ec = { 0 };
1803 : : const char *sha;
1804 : : const char *curve_name;
1805 : : json_t *test;
1806 : : size_t test_index;
1807 : :
1808 : 0 : if (!json_is_object(pub) || !json_is_array(tests))
1809 : 0 : return -EINVAL;
1810 : 0 : sha = json_string_value(json_object_get(group, "sha"));
1811 : 0 : curve_name = json_string_value(json_object_get(pub, "curve"));
1812 : 0 : if (sha == NULL || curve_name == NULL ||
1813 : 0 : map_sha(sha, &ec.hash, &ec.digest_len) != 0 ||
1814 : 0 : map_curve(curve_name, &ec.curve, &ec.bytesize) != 0) {
1815 : 0 : stats->skipped_unsupported++;
1816 : 0 : continue;
1817 : : }
1818 : 0 : if (decode_hex(json_object_get(pub, "wx"), &ec.wx, &ec.wx_len) != 0 ||
1819 : 0 : decode_hex(json_object_get(pub, "wy"), &ec.wy, &ec.wy_len) != 0) {
1820 : 0 : rte_free(ec.wx);
1821 : 0 : rte_free(ec.wy);
1822 : 0 : stats->skipped_unsupported++;
1823 : 0 : continue;
1824 : : }
1825 : 0 : json_array_foreach(tests, test_index, test) {
1826 : : struct wycheproof_data vector;
1827 : 0 : uint8_t *sig = NULL;
1828 : 0 : uint32_t sig_len = 0;
1829 : 0 : enum rte_crypto_op_status status = RTE_CRYPTO_OP_STATUS_ERROR;
1830 : : json_t *tc_id;
1831 : : int ret;
1832 : :
1833 : : memset(&vector, 0, sizeof(vector));
1834 : 0 : tc_id = json_object_get(test, "tcId");
1835 : 0 : vector.result = json_string_value(json_object_get(test, "result"));
1836 : 0 : if (!json_is_integer(tc_id) || vector.result == NULL) {
1837 : 0 : stats->skipped_unsupported++;
1838 : 0 : continue;
1839 : : }
1840 : 0 : vector.tc_id = json_integer_value(tc_id);
1841 : 0 : if (decode_hex(json_object_get(test, "msg"), &vector.msg,
1842 : 0 : &vector.msg_len) != 0 ||
1843 : 0 : decode_hex(json_object_get(test, "sig"),
1844 : : &sig, &sig_len) != 0) {
1845 : 0 : rte_free(vector.msg);
1846 : 0 : rte_free(sig);
1847 : 0 : stats->skipped_unsupported++;
1848 : 0 : continue;
1849 : : }
1850 : 0 : if (strcmp(vector.result, "acceptable") == 0) {
1851 : 0 : stats->skipped_acceptable++;
1852 : : debug_vector("SKIP", name, &vector, "acceptable result policy");
1853 : 0 : rte_free(vector.msg);
1854 : 0 : rte_free(sig);
1855 : 0 : continue;
1856 : : }
1857 : 0 : if (sig_len != 2 * ec.bytesize) {
1858 : : debug_vector("SKIP", name, &vector,
1859 : : "non-canonical P1363 signature size");
1860 : 0 : stats->skipped_unsupported++;
1861 : 0 : rte_free(vector.msg);
1862 : 0 : rte_free(sig);
1863 : 0 : continue;
1864 : : }
1865 : 0 : ret = run_ecdsa_verify(&ec, vector.msg, vector.msg_len, sig, sig_len / 2,
1866 : 0 : sig + sig_len / 2, sig_len / 2, &status);
1867 : 0 : if (ret == -ENOTSUP) {
1868 : 0 : stats->skipped_capability++;
1869 : : debug_vector("SKIP", name, &vector, "PMD capability");
1870 : 0 : } else if (ret == -ETIMEDOUT) {
1871 : 0 : return -ETIMEDOUT;
1872 : 0 : } else if (ret == -EMSGSIZE) {
1873 : 0 : stats->skipped_unsupported++;
1874 : : debug_vector("SKIP", name, &vector, "message length exceeds mbuf");
1875 : 0 : } else if (ret == -EINVAL) {
1876 : 0 : stats->skipped_unsupported++;
1877 : : debug_vector("SKIP", name, &vector, "operand wider than curve");
1878 : 0 : } else if (ret != 0) {
1879 : : debug_vector("FAIL", name, &vector, "signature verification failed");
1880 : 0 : stats->failed++;
1881 : 0 : } else if (strcmp(vector.result, "valid") == 0) {
1882 : 0 : if (ret == 0 && status == RTE_CRYPTO_OP_STATUS_SUCCESS)
1883 : 0 : stats->passed++;
1884 : : else {
1885 : : debug_vector("FAIL", name, &vector,
1886 : : "valid signature rejected");
1887 : 0 : stats->failed++;
1888 : : }
1889 : 0 : } else if (strcmp(vector.result, "invalid") == 0) {
1890 : 0 : if (ret == 0 && status == RTE_CRYPTO_OP_STATUS_SUCCESS) {
1891 : : debug_vector("FAIL", name, &vector,
1892 : : "invalid signature accepted");
1893 : 0 : stats->failed++;
1894 : : } else {
1895 : 0 : stats->passed++;
1896 : : }
1897 : : } else {
1898 : 0 : stats->skipped_unsupported++;
1899 : : debug_vector("SKIP", name, &vector, "unknown result");
1900 : : }
1901 : 0 : rte_free(vector.msg);
1902 : 0 : rte_free(sig);
1903 : : }
1904 : 0 : rte_free(ec.wx);
1905 : 0 : rte_free(ec.wy);
1906 : : }
1907 : : return 0;
1908 : : }
1909 : :
1910 : : static int
1911 : 0 : process_file(const char *path, struct app_stats *stats)
1912 : : {
1913 : : json_error_t error;
1914 : : json_t *root;
1915 : : const char *algorithm;
1916 : : const char *schema;
1917 : : int ret;
1918 : :
1919 : 0 : root = json_load_file(path, 0, &error);
1920 : 0 : if (root == NULL) {
1921 : 0 : printf("Cannot load %s: JSON error at line %d: %s\n", path, error.line,
1922 : : error.text);
1923 : 0 : return -EINVAL;
1924 : : }
1925 : 0 : algorithm = json_string_value(json_object_get(root, "algorithm"));
1926 : 0 : schema = json_string_value(json_object_get(root, "schema"));
1927 : 0 : if (algorithm == NULL || schema == NULL) {
1928 : : debug_file("SKIP", path, "missing algorithm or schema");
1929 : 0 : stats->skipped_unsupported++;
1930 : 0 : json_decref(root);
1931 : 0 : return 0;
1932 : : }
1933 : 0 : if (strcmp(algorithm, "AES-GCM") == 0 &&
1934 : 0 : strcmp(schema, "aead_test_schema_v1.json") == 0) {
1935 : : printf("Processing AES-GCM vectors: %s\n", path);
1936 : 0 : ret = process_aead(root, RTE_CRYPTO_AEAD_AES_GCM, "AES-GCM", stats);
1937 : 0 : } else if (strcmp(algorithm, "AES-CCM") == 0 &&
1938 : 0 : strcmp(schema, "aead_test_schema_v1.json") == 0) {
1939 : : printf("Processing AES-CCM vectors: %s\n", path);
1940 : 0 : ret = process_aead(root, RTE_CRYPTO_AEAD_AES_CCM, "AES-CCM", stats);
1941 : 0 : } else if (strcmp(algorithm, "CHACHA20-POLY1305") == 0 &&
1942 : 0 : strcmp(schema, "aead_test_schema_v1.json") == 0) {
1943 : : printf("Processing ChaCha20-Poly1305 vectors: %s\n", path);
1944 : 0 : ret = process_aead(root, RTE_CRYPTO_AEAD_CHACHA20_POLY1305,
1945 : : "ChaCha20-Poly1305", stats);
1946 : 0 : } else if (strcmp(algorithm, "SM4-GCM") == 0 &&
1947 : 0 : strcmp(schema, "aead_test_schema_v1.json") == 0) {
1948 : : printf("Processing SM4-GCM vectors: %s\n", path);
1949 : 0 : ret = process_aead(root, RTE_CRYPTO_AEAD_SM4_GCM, "SM4-GCM", stats);
1950 : 0 : } else if (strcmp(algorithm, "HMACSHA256") == 0 &&
1951 : 0 : strcmp(schema, "mac_test_schema_v1.json") == 0) {
1952 : : printf("Processing HMAC-SHA256 vectors: %s\n", path);
1953 : 0 : ret = process_hmac(root, RTE_CRYPTO_AUTH_SHA256_HMAC, "HMAC-SHA256", stats);
1954 : 0 : } else if (strcmp(algorithm, "HMACSHA1") == 0 &&
1955 : 0 : strcmp(schema, "mac_test_schema_v1.json") == 0) {
1956 : : printf("Processing HMAC-SHA1 vectors: %s\n", path);
1957 : 0 : ret = process_hmac(root, RTE_CRYPTO_AUTH_SHA1_HMAC, "HMAC-SHA1", stats);
1958 : 0 : } else if (strcmp(algorithm, "HMACSHA224") == 0 &&
1959 : 0 : strcmp(schema, "mac_test_schema_v1.json") == 0) {
1960 : : printf("Processing HMAC-SHA224 vectors: %s\n", path);
1961 : 0 : ret = process_hmac(root, RTE_CRYPTO_AUTH_SHA224_HMAC, "HMAC-SHA224", stats);
1962 : 0 : } else if (strcmp(algorithm, "HMACSHA384") == 0 &&
1963 : 0 : strcmp(schema, "mac_test_schema_v1.json") == 0) {
1964 : : printf("Processing HMAC-SHA384 vectors: %s\n", path);
1965 : 0 : ret = process_hmac(root, RTE_CRYPTO_AUTH_SHA384_HMAC, "HMAC-SHA384", stats);
1966 : 0 : } else if (strcmp(algorithm, "HMACSHA512") == 0 &&
1967 : 0 : strcmp(schema, "mac_test_schema_v1.json") == 0) {
1968 : : printf("Processing HMAC-SHA512 vectors: %s\n", path);
1969 : 0 : ret = process_hmac(root, RTE_CRYPTO_AUTH_SHA512_HMAC, "HMAC-SHA512", stats);
1970 : 0 : } else if (strcmp(algorithm, "HMACSHA3-224") == 0 &&
1971 : 0 : strcmp(schema, "mac_test_schema_v1.json") == 0) {
1972 : : printf("Processing HMAC-SHA3-224 vectors: %s\n", path);
1973 : 0 : ret = process_hmac(root, RTE_CRYPTO_AUTH_SHA3_224_HMAC, "HMAC-SHA3-224", stats);
1974 : 0 : } else if (strcmp(algorithm, "HMACSHA3-256") == 0 &&
1975 : 0 : strcmp(schema, "mac_test_schema_v1.json") == 0) {
1976 : : printf("Processing HMAC-SHA3-256 vectors: %s\n", path);
1977 : 0 : ret = process_hmac(root, RTE_CRYPTO_AUTH_SHA3_256_HMAC, "HMAC-SHA3-256", stats);
1978 : 0 : } else if (strcmp(algorithm, "HMACSHA3-384") == 0 &&
1979 : 0 : strcmp(schema, "mac_test_schema_v1.json") == 0) {
1980 : : printf("Processing HMAC-SHA3-384 vectors: %s\n", path);
1981 : 0 : ret = process_hmac(root, RTE_CRYPTO_AUTH_SHA3_384_HMAC, "HMAC-SHA3-384", stats);
1982 : 0 : } else if (strcmp(algorithm, "HMACSHA3-512") == 0 &&
1983 : 0 : strcmp(schema, "mac_test_schema_v1.json") == 0) {
1984 : : printf("Processing HMAC-SHA3-512 vectors: %s\n", path);
1985 : 0 : ret = process_hmac(root, RTE_CRYPTO_AUTH_SHA3_512_HMAC, "HMAC-SHA3-512", stats);
1986 : 0 : } else if (strcmp(algorithm, "HMACSM3") == 0 &&
1987 : 0 : strcmp(schema, "mac_test_schema_v1.json") == 0) {
1988 : : printf("Processing HMAC-SM3 vectors: %s\n", path);
1989 : 0 : ret = process_hmac(root, RTE_CRYPTO_AUTH_SM3_HMAC, "HMAC-SM3", stats);
1990 : 0 : } else if (strcmp(algorithm, "AES-CMAC") == 0 &&
1991 : 0 : strcmp(schema, "mac_test_schema_v1.json") == 0) {
1992 : : printf("Processing AES-CMAC vectors: %s\n", path);
1993 : 0 : ret = process_hmac(root, RTE_CRYPTO_AUTH_AES_CMAC, "AES-CMAC", stats);
1994 : 0 : } else if (strcmp(algorithm, "AES-GMAC") == 0 &&
1995 : 0 : strcmp(schema, "mac_with_iv_test_schema_v1.json") == 0) {
1996 : : printf("Processing AES-GMAC vectors: %s\n", path);
1997 : 0 : ret = process_gmac(root, stats);
1998 : 0 : } else if (strcmp(algorithm, "DSA") == 0 &&
1999 : 0 : strcmp(schema, "dsa_p1363_verify_schema_v1.json") == 0) {
2000 : : printf("Processing DSA (P1363) vectors: %s\n", path);
2001 : 0 : ret = process_dsa_p1363(root, "DSA", stats);
2002 : 0 : } else if (strcmp(algorithm, "ECDH") == 0 &&
2003 : 0 : strcmp(schema, "ecdh_ecpoint_test_schema_v1.json") == 0) {
2004 : : printf("Processing ECDH (ecpoint) vectors: %s\n", path);
2005 : 0 : ret = process_ecdh_ecpoint(root, "ECDH", stats);
2006 : 0 : } else if (strcmp(algorithm, "ECDSA") == 0 &&
2007 : 0 : strcmp(schema, "ecdsa_p1363_verify_schema_v1.json") == 0) {
2008 : : printf("Processing ECDSA (P1363) vectors: %s\n", path);
2009 : 0 : ret = process_ecdsa_p1363(root, "ECDSA", stats);
2010 : 0 : } else if (strncmp(algorithm, "AES-", strlen("AES-")) == 0 ||
2011 : 0 : strncmp(algorithm, "SEED-", strlen("SEED-")) == 0 ||
2012 : 0 : strcmp(algorithm, "SM4-CCM") == 0 ||
2013 : 0 : strncmp(algorithm, "HMAC", strlen("HMAC")) == 0 ||
2014 : 0 : strncmp(algorithm, "RSA", strlen("RSA")) == 0 ||
2015 : 0 : strcmp(algorithm, "DSA") == 0 ||
2016 : 0 : strcmp(algorithm, "ECDH") == 0 ||
2017 : : strcmp(algorithm, "ECDSA") == 0) {
2018 : : debug_file("SKIP", path, algorithm);
2019 : 0 : stats->skipped_unsupported++;
2020 : 0 : ret = 0;
2021 : : } else {
2022 : : debug_file("SKIP", path, "unsupported vector file");
2023 : 0 : stats->skipped_unsupported++;
2024 : : ret = 0;
2025 : : }
2026 : 0 : json_decref(root);
2027 : 0 : return ret;
2028 : : }
2029 : :
2030 : : static int
2031 : 0 : process_path(const char *path, struct app_stats *stats)
2032 : : {
2033 : : DIR *directory;
2034 : : struct dirent *entry;
2035 : : int ret = 0;
2036 : :
2037 : 0 : directory = opendir(path);
2038 : 0 : if (directory == NULL) {
2039 : 0 : if (errno == ENOTDIR)
2040 : 0 : return process_file(path, stats);
2041 : 0 : return -errno;
2042 : : }
2043 : 0 : while ((entry = readdir(directory)) != NULL) {
2044 : : char file_path[PATH_MAX];
2045 : 0 : size_t name_length = strlen(entry->d_name);
2046 : :
2047 : 0 : if (name_length < 6 || strcmp(entry->d_name + name_length - 5, ".json") != 0)
2048 : 0 : continue;
2049 : 0 : if (snprintf(file_path, sizeof(file_path), "%s/%s", path, entry->d_name) >=
2050 : : (int)sizeof(file_path)) {
2051 : : ret = -ENAMETOOLONG;
2052 : 0 : break;
2053 : : }
2054 : 0 : ret = process_file(file_path, stats);
2055 : 0 : if (ret != 0)
2056 : : break;
2057 : : }
2058 : 0 : closedir(directory);
2059 : 0 : return ret;
2060 : : }
2061 : :
2062 : : int
2063 : 0 : main(int argc, char **argv)
2064 : : {
2065 : 0 : struct app_stats stats = { 0 };
2066 : 0 : const char *prgname = argv[0];
2067 : : int eal_args;
2068 : : int ret;
2069 : :
2070 : 0 : eal_args = rte_eal_init(argc, argv);
2071 : 0 : if (eal_args < 0)
2072 : 0 : rte_exit(EXIT_FAILURE, "Cannot initialize EAL\n");
2073 : 0 : argc -= eal_args;
2074 : 0 : argv += eal_args;
2075 : :
2076 : 0 : ret = parse_args(argc, argv);
2077 : 0 : if (ret != 0) {
2078 : : usage(prgname);
2079 : : ret = EXIT_FAILURE;
2080 : 0 : goto out_eal;
2081 : : }
2082 : 0 : ret = app_init();
2083 : 0 : if (ret != 0) {
2084 : 0 : printf("Cannot initialize cryptodev %u: %s\n", env.dev_id, rte_strerror(-ret));
2085 : : ret = EXIT_FAILURE;
2086 : 0 : goto out_eal;
2087 : : }
2088 : :
2089 : 0 : ret = process_path(env.vectors_path, &stats);
2090 : 0 : if (ret != 0)
2091 : 0 : printf("Vector processing failed: %s\n", rte_strerror(-ret));
2092 : 0 : printf("Summary: passed=%" PRIu64 " failed=%" PRIu64
2093 : : " skipped_capability=%" PRIu64 " skipped_acceptable=%" PRIu64
2094 : : " skipped_unsupported=%" PRIu64 "\n", stats.passed, stats.failed,
2095 : : stats.skipped_capability, stats.skipped_acceptable, stats.skipped_unsupported);
2096 : 0 : app_uninit();
2097 : 0 : ret = ret == 0 && stats.failed == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
2098 : :
2099 : 0 : out_eal:
2100 : 0 : rte_eal_cleanup();
2101 : : return ret;
2102 : : }
|