Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2016-2017 Intel Corporation
3 : : */
4 : :
5 : : #include <rte_byteorder.h>
6 : : #include <rte_common.h>
7 : : #include <rte_hexdump.h>
8 : : #include <rte_cryptodev.h>
9 : : #include <cryptodev_pmd.h>
10 : : #include <bus_vdev_driver.h>
11 : : #include <rte_malloc.h>
12 : : #include <rte_cpuflags.h>
13 : :
14 : : #include <openssl/cmac.h>
15 : : #include <openssl/hmac.h>
16 : : #include <openssl/evp.h>
17 : : #include <openssl/ec.h>
18 : :
19 : : #include "openssl_pmd_private.h"
20 : : #include "compat.h"
21 : :
22 : : #define DES_BLOCK_SIZE 8
23 : :
24 : : static uint8_t cryptodev_driver_id;
25 : :
26 : : #if (OPENSSL_VERSION_NUMBER < 0x10100000L)
27 : : static HMAC_CTX *HMAC_CTX_new(void)
28 : : {
29 : : HMAC_CTX *ctx = OPENSSL_malloc(sizeof(*ctx));
30 : :
31 : : if (ctx != NULL)
32 : : HMAC_CTX_init(ctx);
33 : : return ctx;
34 : : }
35 : :
36 : : static void HMAC_CTX_free(HMAC_CTX *ctx)
37 : : {
38 : : if (ctx != NULL) {
39 : : HMAC_CTX_cleanup(ctx);
40 : : OPENSSL_free(ctx);
41 : : }
42 : : }
43 : : #endif
44 : :
45 : : #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
46 : :
47 : : #include <openssl/provider.h>
48 : : #include <openssl/core_names.h>
49 : : #include <openssl/param_build.h>
50 : :
51 : : #define MAX_OSSL_ALGO_NAME_SIZE 16
52 : :
53 : : OSSL_PROVIDER *legacy;
54 : : OSSL_PROVIDER *deflt;
55 : :
56 : 2 : static void ossl_legacy_provider_load(void)
57 : : {
58 : : /* Load Multiple providers into the default (NULL) library context */
59 : 2 : legacy = OSSL_PROVIDER_load(NULL, "legacy");
60 [ - + ]: 2 : if (legacy == NULL) {
61 : 0 : OPENSSL_LOG(ERR, "Failed to load Legacy provider");
62 : 0 : return;
63 : : }
64 : :
65 : 2 : deflt = OSSL_PROVIDER_load(NULL, "default");
66 [ - + ]: 2 : if (deflt == NULL) {
67 : 0 : OPENSSL_LOG(ERR, "Failed to load Default provider");
68 : 0 : OSSL_PROVIDER_unload(legacy);
69 : 0 : return;
70 : : }
71 : : }
72 : :
73 : 2 : static void ossl_legacy_provider_unload(void)
74 : : {
75 : 2 : OSSL_PROVIDER_unload(legacy);
76 : 2 : OSSL_PROVIDER_unload(deflt);
77 : 2 : }
78 : :
79 : : static __rte_always_inline const char *
80 : : digest_name_get(enum rte_crypto_auth_algorithm algo)
81 : : {
82 : 87 : switch (algo) {
83 : : case RTE_CRYPTO_AUTH_MD5_HMAC:
84 : : return OSSL_DIGEST_NAME_MD5;
85 : 42 : case RTE_CRYPTO_AUTH_SHA1_HMAC:
86 : 42 : return OSSL_DIGEST_NAME_SHA1;
87 : 4 : case RTE_CRYPTO_AUTH_SHA224_HMAC:
88 : 4 : return OSSL_DIGEST_NAME_SHA2_224;
89 : 6 : case RTE_CRYPTO_AUTH_SHA256_HMAC:
90 : 6 : return OSSL_DIGEST_NAME_SHA2_256;
91 : 4 : case RTE_CRYPTO_AUTH_SHA384_HMAC:
92 : 4 : return OSSL_DIGEST_NAME_SHA2_384;
93 : 17 : case RTE_CRYPTO_AUTH_SHA512_HMAC:
94 : 17 : return OSSL_DIGEST_NAME_SHA2_512;
95 : 2 : case RTE_CRYPTO_AUTH_SHA3_224_HMAC:
96 : 2 : return OSSL_DIGEST_NAME_SHA3_224;
97 : 2 : case RTE_CRYPTO_AUTH_SHA3_256_HMAC:
98 : 2 : return OSSL_DIGEST_NAME_SHA3_256;
99 : 2 : case RTE_CRYPTO_AUTH_SHA3_384_HMAC:
100 : 2 : return OSSL_DIGEST_NAME_SHA3_384;
101 : 2 : case RTE_CRYPTO_AUTH_SHA3_512_HMAC:
102 : 2 : return OSSL_DIGEST_NAME_SHA3_512;
103 : : default:
104 : : return NULL;
105 : : }
106 : : }
107 : : #endif
108 : :
109 : : static int cryptodev_openssl_remove(struct rte_vdev_device *vdev);
110 : :
111 : : /*
112 : : *------------------------------------------------------------------------------
113 : : * Session Prepare
114 : : *------------------------------------------------------------------------------
115 : : */
116 : :
117 : : /** Get xform chain order */
118 : : static enum openssl_chain_order
119 : 276 : openssl_get_chain_order(const struct rte_crypto_sym_xform *xform)
120 : : {
121 : : enum openssl_chain_order res = OPENSSL_CHAIN_NOT_SUPPORTED;
122 : :
123 [ + - ]: 276 : if (xform != NULL) {
124 [ + + ]: 276 : if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
125 [ + + ]: 103 : if (xform->next == NULL)
126 : : res = OPENSSL_CHAIN_ONLY_AUTH;
127 [ + - ]: 37 : else if (xform->next->type ==
128 : : RTE_CRYPTO_SYM_XFORM_CIPHER)
129 : : res = OPENSSL_CHAIN_AUTH_CIPHER;
130 : : }
131 [ + + ]: 276 : if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
132 [ + + ]: 84 : if (xform->next == NULL)
133 : : res = OPENSSL_CHAIN_ONLY_CIPHER;
134 [ + - ]: 32 : else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
135 : : res = OPENSSL_CHAIN_CIPHER_AUTH;
136 : : }
137 [ + + ]: 276 : if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD)
138 : : res = OPENSSL_CHAIN_COMBINED;
139 : : }
140 : :
141 : 276 : return res;
142 : : }
143 : :
144 : : /** Get session cipher key from input cipher key */
145 : : static void
146 : : get_cipher_key(const uint8_t *input_key, int keylen, uint8_t *session_key)
147 : : {
148 : 305 : memcpy(session_key, input_key, keylen);
149 : : }
150 : :
151 : : /** Get key ede 24 bytes standard from input key */
152 : : static int
153 : 12 : get_cipher_key_ede(const uint8_t *key, int keylen, uint8_t *key_ede)
154 : : {
155 : : int res = 0;
156 : :
157 : : /* Initialize keys - 24 bytes: [key1-key2-key3] */
158 [ + + - - ]: 12 : switch (keylen) {
159 : : case 24:
160 : : memcpy(key_ede, key, 24);
161 : : break;
162 : : case 16:
163 : : /* K3 = K1 */
164 : : memcpy(key_ede, key, 16);
165 : 6 : memcpy(key_ede + 16, key, 8);
166 : : break;
167 : : case 8:
168 : : /* K1 = K2 = K3 (DES compatibility) */
169 : : memcpy(key_ede, key, 8);
170 : 0 : memcpy(key_ede + 8, key, 8);
171 : 0 : memcpy(key_ede + 16, key, 8);
172 : : break;
173 : 0 : default:
174 : 0 : OPENSSL_LOG(ERR, "Unsupported key size");
175 : : res = -EINVAL;
176 : : }
177 : :
178 : 12 : return res;
179 : : }
180 : :
181 : : /** Get adequate openssl function for input cipher algorithm */
182 : : static uint8_t
183 : 95 : get_cipher_algo(enum rte_crypto_cipher_algorithm sess_algo, size_t keylen,
184 : : const EVP_CIPHER **algo)
185 : : {
186 : : int res = 0;
187 : :
188 [ + - ]: 95 : if (algo != NULL) {
189 [ + + + + : 95 : switch (sess_algo) {
- - ]
190 [ + + + - ]: 18 : case RTE_CRYPTO_CIPHER_3DES_CBC:
191 : : switch (keylen) {
192 : 2 : case 8:
193 : 2 : *algo = EVP_des_cbc();
194 : 2 : break;
195 : 10 : case 16:
196 : 10 : *algo = EVP_des_ede_cbc();
197 : 10 : break;
198 : 6 : case 24:
199 : 6 : *algo = EVP_des_ede3_cbc();
200 : 6 : break;
201 : : default:
202 : : res = -EINVAL;
203 : : }
204 : : break;
205 : : case RTE_CRYPTO_CIPHER_3DES_CTR:
206 : : break;
207 [ + + + - ]: 57 : case RTE_CRYPTO_CIPHER_AES_CBC:
208 : : switch (keylen) {
209 : 47 : case 16:
210 : 47 : *algo = EVP_aes_128_cbc();
211 : 47 : break;
212 : 6 : case 24:
213 : 6 : *algo = EVP_aes_192_cbc();
214 : 6 : break;
215 : 4 : case 32:
216 : 4 : *algo = EVP_aes_256_cbc();
217 : 4 : break;
218 : : default:
219 : : res = -EINVAL;
220 : : }
221 : : break;
222 [ + + - ]: 8 : case RTE_CRYPTO_CIPHER_AES_XTS:
223 : : switch (keylen) {
224 : 4 : case 32:
225 : 4 : *algo = EVP_aes_128_xts();
226 : 4 : break;
227 : 4 : case 64:
228 : 4 : *algo = EVP_aes_256_xts();
229 : 4 : break;
230 : : default:
231 : : res = -EINVAL;
232 : : }
233 : : break;
234 [ + + + - ]: 12 : case RTE_CRYPTO_CIPHER_AES_CTR:
235 : : switch (keylen) {
236 : 6 : case 16:
237 : 6 : *algo = EVP_aes_128_ctr();
238 : 6 : break;
239 : 2 : case 24:
240 : 2 : *algo = EVP_aes_192_ctr();
241 : 2 : break;
242 : 4 : case 32:
243 : 4 : *algo = EVP_aes_256_ctr();
244 : 4 : break;
245 : : default:
246 : : res = -EINVAL;
247 : : }
248 : : break;
249 : 0 : default:
250 : : res = -EINVAL;
251 : 0 : break;
252 : : }
253 : : } else {
254 : : res = -EINVAL;
255 : : }
256 : :
257 : 95 : return res;
258 : : }
259 : :
260 : : /** Get adequate openssl function for input auth algorithm */
261 : : static uint8_t
262 : 119 : get_auth_algo(enum rte_crypto_auth_algorithm sessalgo,
263 : : const EVP_MD **algo)
264 : : {
265 : : int res = 0;
266 : :
267 [ + - ]: 119 : if (algo != NULL) {
268 [ + + + + : 119 : switch (sessalgo) {
+ + + + +
+ + + - ]
269 : 8 : case RTE_CRYPTO_AUTH_MD5:
270 : : case RTE_CRYPTO_AUTH_MD5_HMAC:
271 : 8 : *algo = EVP_md5();
272 : 8 : break;
273 : 52 : case RTE_CRYPTO_AUTH_SHA1:
274 : : case RTE_CRYPTO_AUTH_SHA1_HMAC:
275 : 52 : *algo = EVP_sha1();
276 : 52 : break;
277 : 6 : case RTE_CRYPTO_AUTH_SHA224:
278 : : case RTE_CRYPTO_AUTH_SHA224_HMAC:
279 : 6 : *algo = EVP_sha224();
280 : 6 : break;
281 : 8 : case RTE_CRYPTO_AUTH_SHA256:
282 : : case RTE_CRYPTO_AUTH_SHA256_HMAC:
283 : 8 : *algo = EVP_sha256();
284 : 8 : break;
285 : 6 : case RTE_CRYPTO_AUTH_SHA384:
286 : : case RTE_CRYPTO_AUTH_SHA384_HMAC:
287 : 6 : *algo = EVP_sha384();
288 : 6 : break;
289 : 19 : case RTE_CRYPTO_AUTH_SHA512:
290 : : case RTE_CRYPTO_AUTH_SHA512_HMAC:
291 : 19 : *algo = EVP_sha512();
292 : 19 : break;
293 : 4 : case RTE_CRYPTO_AUTH_SHA3_224:
294 : : case RTE_CRYPTO_AUTH_SHA3_224_HMAC:
295 : 4 : *algo = EVP_sha3_224();
296 : 4 : break;
297 : 4 : case RTE_CRYPTO_AUTH_SHA3_256:
298 : : case RTE_CRYPTO_AUTH_SHA3_256_HMAC:
299 : 4 : *algo = EVP_sha3_256();
300 : 4 : break;
301 : 4 : case RTE_CRYPTO_AUTH_SHA3_384:
302 : : case RTE_CRYPTO_AUTH_SHA3_384_HMAC:
303 : 4 : *algo = EVP_sha3_384();
304 : 4 : break;
305 : 4 : case RTE_CRYPTO_AUTH_SHA3_512:
306 : : case RTE_CRYPTO_AUTH_SHA3_512_HMAC:
307 : 4 : *algo = EVP_sha3_512();
308 : 4 : break;
309 : : #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
310 : 2 : case RTE_CRYPTO_AUTH_SHAKE_128:
311 : 2 : *algo = EVP_shake128();
312 : 2 : break;
313 : 2 : case RTE_CRYPTO_AUTH_SHAKE_256:
314 : 2 : *algo = EVP_shake256();
315 : 2 : break;
316 : : #endif
317 : : default:
318 : : res = -EINVAL;
319 : : break;
320 : : }
321 : : } else {
322 : : res = -EINVAL;
323 : : }
324 : :
325 : 119 : return res;
326 : : }
327 : :
328 : : /** Get adequate openssl function for input cipher algorithm */
329 : : static uint8_t
330 : 196 : get_aead_algo(enum rte_crypto_aead_algorithm sess_algo, size_t keylen,
331 : : const EVP_CIPHER **algo)
332 : : {
333 : : int res = 0;
334 : :
335 [ + - ]: 196 : if (algo != NULL) {
336 [ + + - ]: 196 : switch (sess_algo) {
337 [ + + + - ]: 156 : case RTE_CRYPTO_AEAD_AES_GCM:
338 : : switch (keylen) {
339 : 84 : case 16:
340 : 84 : *algo = EVP_aes_128_gcm();
341 : 84 : break;
342 : 32 : case 24:
343 : 32 : *algo = EVP_aes_192_gcm();
344 : 32 : break;
345 : 40 : case 32:
346 : 40 : *algo = EVP_aes_256_gcm();
347 : 40 : break;
348 : : default:
349 : : res = -EINVAL;
350 : : }
351 : : break;
352 [ + + + - ]: 40 : case RTE_CRYPTO_AEAD_AES_CCM:
353 : : switch (keylen) {
354 : 16 : case 16:
355 : 16 : *algo = EVP_aes_128_ccm();
356 : 16 : break;
357 : 12 : case 24:
358 : 12 : *algo = EVP_aes_192_ccm();
359 : 12 : break;
360 : 12 : case 32:
361 : 12 : *algo = EVP_aes_256_ccm();
362 : 12 : break;
363 : : default:
364 : : res = -EINVAL;
365 : : }
366 : : break;
367 : : default:
368 : : res = -EINVAL;
369 : : break;
370 : : }
371 : : } else {
372 : : res = -EINVAL;
373 : : }
374 : :
375 : 196 : return res;
376 : : }
377 : :
378 : : /* Set session AEAD encryption parameters */
379 : : static int
380 : 97 : openssl_set_sess_aead_enc_param(struct openssl_session *sess,
381 : : enum rte_crypto_aead_algorithm algo,
382 : : uint8_t tag_len, const uint8_t *key,
383 : : EVP_CIPHER_CTX **ctx)
384 : : {
385 : : int iv_type = 0;
386 : : unsigned int do_ccm;
387 : :
388 : 97 : sess->cipher.direction = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
389 : 97 : sess->auth.operation = RTE_CRYPTO_AUTH_OP_GENERATE;
390 : :
391 : : /* Select AEAD algo */
392 [ + + - ]: 97 : switch (algo) {
393 : 77 : case RTE_CRYPTO_AEAD_AES_GCM:
394 : : iv_type = EVP_CTRL_GCM_SET_IVLEN;
395 [ + - ]: 77 : if (tag_len != 16)
396 : : return -EINVAL;
397 : : do_ccm = 0;
398 : : break;
399 : 20 : case RTE_CRYPTO_AEAD_AES_CCM:
400 : : iv_type = EVP_CTRL_CCM_SET_IVLEN;
401 : : /* Digest size can be 4, 6, 8, 10, 12, 14 or 16 bytes */
402 [ + - + - ]: 20 : if (tag_len < 4 || tag_len > 16 || (tag_len & 1) == 1)
403 : : return -EINVAL;
404 : : do_ccm = 1;
405 : : break;
406 : : default:
407 : : return -ENOTSUP;
408 : : }
409 : :
410 : 97 : sess->cipher.mode = OPENSSL_CIPHER_LIB;
411 : 97 : *ctx = EVP_CIPHER_CTX_new();
412 : :
413 [ + - ]: 97 : if (get_aead_algo(algo, sess->cipher.key.length,
414 : : &sess->cipher.evp_algo) != 0)
415 : : return -EINVAL;
416 : :
417 : 97 : get_cipher_key(key, sess->cipher.key.length, sess->cipher.key.data);
418 : :
419 : 97 : sess->chain_order = OPENSSL_CHAIN_COMBINED;
420 : :
421 [ + - ]: 97 : if (EVP_EncryptInit_ex(*ctx, sess->cipher.evp_algo,
422 : : NULL, NULL, NULL) <= 0)
423 : : return -EINVAL;
424 : :
425 [ + - ]: 97 : if (EVP_CIPHER_CTX_ctrl(*ctx, iv_type, sess->iv.length,
426 : : NULL) <= 0)
427 : : return -EINVAL;
428 : :
429 [ + + ]: 97 : if (do_ccm)
430 : 20 : EVP_CIPHER_CTX_ctrl(*ctx, EVP_CTRL_CCM_SET_TAG,
431 : : tag_len, NULL);
432 : :
433 [ - + ]: 97 : if (EVP_EncryptInit_ex(*ctx, NULL, NULL, key, NULL) <= 0)
434 : 0 : return -EINVAL;
435 : :
436 : : return 0;
437 : : }
438 : :
439 : : /* Set session AEAD decryption parameters */
440 : : static int
441 : 99 : openssl_set_sess_aead_dec_param(struct openssl_session *sess,
442 : : enum rte_crypto_aead_algorithm algo,
443 : : uint8_t tag_len, const uint8_t *key,
444 : : EVP_CIPHER_CTX **ctx)
445 : : {
446 : : int iv_type = 0;
447 : : unsigned int do_ccm = 0;
448 : :
449 : 99 : sess->cipher.direction = RTE_CRYPTO_CIPHER_OP_DECRYPT;
450 : 99 : sess->auth.operation = RTE_CRYPTO_AUTH_OP_VERIFY;
451 : :
452 : : /* Select AEAD algo */
453 [ + + - ]: 99 : switch (algo) {
454 : 79 : case RTE_CRYPTO_AEAD_AES_GCM:
455 : : iv_type = EVP_CTRL_GCM_SET_IVLEN;
456 [ + - ]: 79 : if (tag_len != 16)
457 : : return -EINVAL;
458 : : break;
459 : 20 : case RTE_CRYPTO_AEAD_AES_CCM:
460 : : iv_type = EVP_CTRL_CCM_SET_IVLEN;
461 : : /* Digest size can be 4, 6, 8, 10, 12, 14 or 16 bytes */
462 [ + - + - ]: 20 : if (tag_len < 4 || tag_len > 16 || (tag_len & 1) == 1)
463 : : return -EINVAL;
464 : : do_ccm = 1;
465 : : break;
466 : : default:
467 : : return -ENOTSUP;
468 : : }
469 : :
470 : 99 : sess->cipher.mode = OPENSSL_CIPHER_LIB;
471 : 99 : *ctx = EVP_CIPHER_CTX_new();
472 : :
473 [ + - ]: 99 : if (get_aead_algo(algo, sess->cipher.key.length,
474 : : &sess->cipher.evp_algo) != 0)
475 : : return -EINVAL;
476 : :
477 : 99 : get_cipher_key(key, sess->cipher.key.length, sess->cipher.key.data);
478 : :
479 : 99 : sess->chain_order = OPENSSL_CHAIN_COMBINED;
480 : :
481 [ + - ]: 99 : if (EVP_DecryptInit_ex(*ctx, sess->cipher.evp_algo,
482 : : NULL, NULL, NULL) <= 0)
483 : : return -EINVAL;
484 : :
485 [ + - ]: 99 : if (EVP_CIPHER_CTX_ctrl(*ctx, iv_type,
486 : 99 : sess->iv.length, NULL) <= 0)
487 : : return -EINVAL;
488 : :
489 [ + + ]: 99 : if (do_ccm)
490 : 20 : EVP_CIPHER_CTX_ctrl(*ctx, EVP_CTRL_CCM_SET_TAG,
491 : : tag_len, NULL);
492 : :
493 [ - + ]: 99 : if (EVP_DecryptInit_ex(*ctx, NULL, NULL, key, NULL) <= 0)
494 : 0 : return -EINVAL;
495 : :
496 : : return 0;
497 : : }
498 : :
499 : : #if (OPENSSL_VERSION_NUMBER >= 0x30000000L && OPENSSL_VERSION_NUMBER < 0x30200000L)
500 : 97 : static int openssl_aesni_ctx_clone(EVP_CIPHER_CTX **dest,
501 : : struct openssl_session *sess)
502 : : {
503 : : /* OpenSSL versions 3.0.0 <= V < 3.2.0 have no dupctx() implementation
504 : : * for AES-GCM and AES-CCM. In this case, we have to create new empty
505 : : * contexts and initialise, as we did the original context.
506 : : */
507 [ + + ]: 97 : if (sess->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC)
508 : 10 : sess->aead_algo = RTE_CRYPTO_AEAD_AES_GCM;
509 : :
510 [ + + ]: 97 : if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
511 : 48 : return openssl_set_sess_aead_enc_param(sess, sess->aead_algo,
512 : 48 : sess->auth.digest_length, sess->cipher.key.data,
513 : : dest);
514 : : else
515 : 49 : return openssl_set_sess_aead_dec_param(sess, sess->aead_algo,
516 : 49 : sess->auth.digest_length, sess->cipher.key.data,
517 : : dest);
518 : : }
519 : : #endif
520 : :
521 : : /** Set session cipher parameters */
522 : : static int
523 : 121 : openssl_set_session_cipher_parameters(struct openssl_session *sess,
524 : : const struct rte_crypto_sym_xform *xform)
525 : : {
526 : : /* Select cipher direction */
527 : 121 : sess->cipher.direction = xform->cipher.op;
528 : : /* Select cipher key */
529 : 121 : sess->cipher.key.length = xform->cipher.key.length;
530 : :
531 : : /* Set IV parameters */
532 : 121 : sess->iv.offset = xform->cipher.iv.offset;
533 : 121 : sess->iv.length = xform->cipher.iv.length;
534 : :
535 : : /* Select cipher algo */
536 [ + + + + : 121 : switch (xform->cipher.algo) {
- ]
537 : 95 : case RTE_CRYPTO_CIPHER_3DES_CBC:
538 : : case RTE_CRYPTO_CIPHER_AES_CBC:
539 : : case RTE_CRYPTO_CIPHER_AES_CTR:
540 : : case RTE_CRYPTO_CIPHER_AES_XTS:
541 : 95 : sess->cipher.mode = OPENSSL_CIPHER_LIB;
542 : 95 : sess->cipher.algo = xform->cipher.algo;
543 : 95 : sess->cipher.ctx = EVP_CIPHER_CTX_new();
544 : :
545 [ + - ]: 95 : if (get_cipher_algo(sess->cipher.algo, sess->cipher.key.length,
546 : : &sess->cipher.evp_algo) != 0)
547 : : return -EINVAL;
548 : :
549 : 95 : get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
550 [ + + ]: 95 : sess->cipher.key.data);
551 [ + + ]: 95 : if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
552 [ + - ]: 46 : if (EVP_EncryptInit_ex(sess->cipher.ctx,
553 : : sess->cipher.evp_algo,
554 : 46 : NULL, xform->cipher.key.data,
555 : : NULL) != 1) {
556 : : return -EINVAL;
557 : : }
558 [ + - ]: 49 : } else if (sess->cipher.direction ==
559 : : RTE_CRYPTO_CIPHER_OP_DECRYPT) {
560 [ + - ]: 49 : if (EVP_DecryptInit_ex(sess->cipher.ctx,
561 : : sess->cipher.evp_algo,
562 : 49 : NULL, xform->cipher.key.data,
563 : : NULL) != 1) {
564 : : return -EINVAL;
565 : : }
566 : : }
567 : :
568 : : break;
569 : :
570 : 12 : case RTE_CRYPTO_CIPHER_3DES_CTR:
571 : 12 : sess->cipher.mode = OPENSSL_CIPHER_DES3CTR;
572 : 12 : sess->cipher.ctx = EVP_CIPHER_CTX_new();
573 : :
574 [ + - ]: 12 : if (get_cipher_key_ede(xform->cipher.key.data,
575 : 12 : sess->cipher.key.length,
576 : 12 : sess->cipher.key.data) != 0)
577 : : return -EINVAL;
578 : :
579 : :
580 : : /* We use 3DES encryption also for decryption.
581 : : * IV is not important for 3DES ECB.
582 : : */
583 [ + - ]: 12 : if (EVP_EncryptInit_ex(sess->cipher.ctx, EVP_des_ede3_ecb(),
584 : : NULL, sess->cipher.key.data, NULL) != 1)
585 : : return -EINVAL;
586 : :
587 : : break;
588 : :
589 : 2 : case RTE_CRYPTO_CIPHER_DES_CBC:
590 : 2 : sess->cipher.algo = xform->cipher.algo;
591 : 2 : sess->cipher.ctx = EVP_CIPHER_CTX_new();
592 : 2 : sess->cipher.evp_algo = EVP_des_cbc();
593 : :
594 : 2 : get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
595 [ + + ]: 2 : sess->cipher.key.data);
596 [ + + ]: 2 : if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
597 [ + - ]: 1 : if (EVP_EncryptInit_ex(sess->cipher.ctx,
598 : : sess->cipher.evp_algo,
599 : 1 : NULL, xform->cipher.key.data,
600 : : NULL) != 1) {
601 : : return -EINVAL;
602 : : }
603 [ + - ]: 1 : } else if (sess->cipher.direction ==
604 : : RTE_CRYPTO_CIPHER_OP_DECRYPT) {
605 [ + - ]: 1 : if (EVP_DecryptInit_ex(sess->cipher.ctx,
606 : : sess->cipher.evp_algo,
607 : 1 : NULL, xform->cipher.key.data,
608 : : NULL) != 1) {
609 : : return -EINVAL;
610 : : }
611 : : }
612 : :
613 : : break;
614 : :
615 : 12 : case RTE_CRYPTO_CIPHER_DES_DOCSISBPI:
616 : 12 : sess->cipher.algo = xform->cipher.algo;
617 : 12 : sess->chain_order = OPENSSL_CHAIN_CIPHER_BPI;
618 : 12 : sess->cipher.ctx = EVP_CIPHER_CTX_new();
619 : 12 : sess->cipher.evp_algo = EVP_des_cbc();
620 : :
621 : 12 : sess->cipher.bpi_ctx = EVP_CIPHER_CTX_new();
622 : : /* IV will be ECB encrypted whether direction is encrypt or decrypt */
623 [ + - ]: 12 : if (EVP_EncryptInit_ex(sess->cipher.bpi_ctx, EVP_des_ecb(),
624 : 12 : NULL, xform->cipher.key.data, 0) != 1)
625 : : return -EINVAL;
626 : :
627 : 12 : get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
628 [ + + ]: 12 : sess->cipher.key.data);
629 [ + + ]: 12 : if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
630 [ + - ]: 6 : if (EVP_EncryptInit_ex(sess->cipher.ctx,
631 : : sess->cipher.evp_algo,
632 : 6 : NULL, xform->cipher.key.data,
633 : : NULL) != 1) {
634 : : return -EINVAL;
635 : : }
636 [ + - ]: 6 : } else if (sess->cipher.direction ==
637 : : RTE_CRYPTO_CIPHER_OP_DECRYPT) {
638 [ + - ]: 6 : if (EVP_DecryptInit_ex(sess->cipher.ctx,
639 : : sess->cipher.evp_algo,
640 : 6 : NULL, xform->cipher.key.data,
641 : : NULL) != 1) {
642 : : return -EINVAL;
643 : : }
644 : : }
645 : :
646 : : break;
647 : 0 : default:
648 : 0 : sess->cipher.algo = RTE_CRYPTO_CIPHER_NULL;
649 : 0 : return -ENOTSUP;
650 : : }
651 : :
652 : 121 : EVP_CIPHER_CTX_set_padding(sess->cipher.ctx, 0);
653 : :
654 : 121 : return 0;
655 : : }
656 : :
657 : : /* Set session auth parameters */
658 : : static int
659 : 135 : openssl_set_session_auth_parameters(struct openssl_session *sess,
660 : : const struct rte_crypto_sym_xform *xform)
661 : : {
662 : : # if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
663 : : char algo_name[MAX_OSSL_ALGO_NAME_SIZE];
664 : : OSSL_PARAM params[2];
665 : : const char *algo;
666 : : EVP_MAC *mac;
667 : : # endif
668 : : /* Select auth generate/verify */
669 : 135 : sess->auth.operation = xform->auth.op;
670 : 135 : sess->auth.algo = xform->auth.algo;
671 : :
672 : 135 : sess->auth.digest_length = xform->auth.digest_length;
673 : :
674 : : /* Select auth algo */
675 [ + + + + : 135 : switch (xform->auth.algo) {
- ]
676 : 10 : case RTE_CRYPTO_AUTH_AES_GMAC:
677 : : /*
678 : : * OpenSSL requires GMAC to be a GCM operation
679 : : * with no cipher data length
680 : : */
681 : 10 : sess->cipher.key.length = xform->auth.key.length;
682 : :
683 : : /* Set IV parameters */
684 : 10 : sess->iv.offset = xform->auth.iv.offset;
685 : 10 : sess->iv.length = xform->auth.iv.length;
686 : :
687 [ + + ]: 10 : if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_GENERATE)
688 : 4 : return openssl_set_sess_aead_enc_param(sess,
689 : : RTE_CRYPTO_AEAD_AES_GCM,
690 : : xform->auth.digest_length,
691 : 4 : xform->auth.key.data,
692 : : &sess->cipher.ctx);
693 : : else
694 : 6 : return openssl_set_sess_aead_dec_param(sess,
695 : : RTE_CRYPTO_AEAD_AES_GCM,
696 : : xform->auth.digest_length,
697 : 6 : xform->auth.key.data,
698 : : &sess->cipher.ctx);
699 : : break;
700 : :
701 : 32 : case RTE_CRYPTO_AUTH_MD5:
702 : : case RTE_CRYPTO_AUTH_SHA1:
703 : : case RTE_CRYPTO_AUTH_SHA224:
704 : : case RTE_CRYPTO_AUTH_SHA256:
705 : : case RTE_CRYPTO_AUTH_SHA384:
706 : : case RTE_CRYPTO_AUTH_SHA512:
707 : : case RTE_CRYPTO_AUTH_SHA3_224:
708 : : case RTE_CRYPTO_AUTH_SHA3_256:
709 : : case RTE_CRYPTO_AUTH_SHA3_384:
710 : : case RTE_CRYPTO_AUTH_SHA3_512:
711 : : #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
712 : : case RTE_CRYPTO_AUTH_SHAKE_128:
713 : : case RTE_CRYPTO_AUTH_SHAKE_256:
714 : : #endif
715 : 32 : sess->auth.mode = OPENSSL_AUTH_AS_AUTH;
716 [ + - ]: 32 : if (get_auth_algo(xform->auth.algo,
717 : : &sess->auth.auth.evp_algo) != 0)
718 : : return -EINVAL;
719 : 32 : sess->auth.auth.ctx = EVP_MD_CTX_create();
720 : 32 : break;
721 : :
722 : 6 : case RTE_CRYPTO_AUTH_AES_CMAC:
723 : : # if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
724 [ - + ]: 6 : if (xform->auth.key.length == 16)
725 : : algo = SN_aes_128_cbc;
726 [ # # ]: 0 : else if (xform->auth.key.length == 24)
727 : : algo = SN_aes_192_cbc;
728 [ # # ]: 0 : else if (xform->auth.key.length == 32)
729 : : algo = SN_aes_256_cbc;
730 : : else
731 : : return -EINVAL;
732 : :
733 : : strlcpy(algo_name, algo, sizeof(algo_name));
734 : 6 : params[0] = OSSL_PARAM_construct_utf8_string(
735 : : OSSL_MAC_PARAM_CIPHER, algo_name, 0);
736 : 6 : params[1] = OSSL_PARAM_construct_end();
737 : :
738 : 6 : sess->auth.mode = OPENSSL_AUTH_AS_CMAC;
739 : 6 : mac = EVP_MAC_fetch(NULL, OSSL_MAC_NAME_CMAC, NULL);
740 : 6 : sess->auth.cmac.ctx = EVP_MAC_CTX_new(mac);
741 : 6 : EVP_MAC_free(mac);
742 : :
743 [ - + ]: 6 : if (EVP_MAC_init(sess->auth.cmac.ctx,
744 : 6 : xform->auth.key.data,
745 : 6 : xform->auth.key.length,
746 : : params) != 1)
747 : 0 : return -EINVAL;
748 : : # else
749 : : sess->auth.mode = OPENSSL_AUTH_AS_CMAC;
750 : : sess->auth.cmac.ctx = CMAC_CTX_new();
751 : : if (get_cipher_algo(RTE_CRYPTO_CIPHER_AES_CBC,
752 : : xform->auth.key.length,
753 : : &sess->auth.cmac.evp_algo) != 0)
754 : : return -EINVAL;
755 : : if (CMAC_Init(sess->auth.cmac.ctx,
756 : : xform->auth.key.data,
757 : : xform->auth.key.length,
758 : : sess->auth.cmac.evp_algo, NULL) != 1)
759 : : return -EINVAL;
760 : : # endif
761 : : break;
762 : :
763 : : # if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
764 : 87 : case RTE_CRYPTO_AUTH_MD5_HMAC:
765 : : case RTE_CRYPTO_AUTH_SHA1_HMAC:
766 : : case RTE_CRYPTO_AUTH_SHA224_HMAC:
767 : : case RTE_CRYPTO_AUTH_SHA256_HMAC:
768 : : case RTE_CRYPTO_AUTH_SHA384_HMAC:
769 : : case RTE_CRYPTO_AUTH_SHA512_HMAC:
770 : : case RTE_CRYPTO_AUTH_SHA3_224_HMAC:
771 : : case RTE_CRYPTO_AUTH_SHA3_256_HMAC:
772 : : case RTE_CRYPTO_AUTH_SHA3_384_HMAC:
773 : : case RTE_CRYPTO_AUTH_SHA3_512_HMAC:
774 [ + + + + : 87 : sess->auth.mode = OPENSSL_AUTH_AS_HMAC;
+ + + + +
+ - ]
775 : :
776 : : algo = digest_name_get(xform->auth.algo);
777 : : if (!algo)
778 : : return -EINVAL;
779 : : strlcpy(algo_name, algo, sizeof(algo_name));
780 : :
781 : 87 : mac = EVP_MAC_fetch(NULL, "HMAC", NULL);
782 : 87 : sess->auth.hmac.ctx = EVP_MAC_CTX_new(mac);
783 : 87 : EVP_MAC_free(mac);
784 [ + - ]: 87 : if (get_auth_algo(xform->auth.algo,
785 : : &sess->auth.hmac.evp_algo) != 0)
786 : : return -EINVAL;
787 : :
788 : 87 : params[0] = OSSL_PARAM_construct_utf8_string("digest",
789 : : algo_name, 0);
790 : 87 : params[1] = OSSL_PARAM_construct_end();
791 [ - + ]: 87 : if (EVP_MAC_init(sess->auth.hmac.ctx,
792 : 87 : xform->auth.key.data,
793 : 87 : xform->auth.key.length,
794 : : params) != 1)
795 : 0 : return -EINVAL;
796 : : break;
797 : : # else
798 : : case RTE_CRYPTO_AUTH_MD5_HMAC:
799 : : case RTE_CRYPTO_AUTH_SHA1_HMAC:
800 : : case RTE_CRYPTO_AUTH_SHA224_HMAC:
801 : : case RTE_CRYPTO_AUTH_SHA256_HMAC:
802 : : case RTE_CRYPTO_AUTH_SHA384_HMAC:
803 : : case RTE_CRYPTO_AUTH_SHA512_HMAC:
804 : : case RTE_CRYPTO_AUTH_SHA3_224_HMAC:
805 : : case RTE_CRYPTO_AUTH_SHA3_256_HMAC:
806 : : case RTE_CRYPTO_AUTH_SHA3_384_HMAC:
807 : : case RTE_CRYPTO_AUTH_SHA3_512_HMAC:
808 : : sess->auth.mode = OPENSSL_AUTH_AS_HMAC;
809 : : sess->auth.hmac.ctx = HMAC_CTX_new();
810 : : if (get_auth_algo(xform->auth.algo,
811 : : &sess->auth.hmac.evp_algo) != 0)
812 : : return -EINVAL;
813 : :
814 : : if (HMAC_Init_ex(sess->auth.hmac.ctx,
815 : : xform->auth.key.data,
816 : : xform->auth.key.length,
817 : : sess->auth.hmac.evp_algo, NULL) != 1)
818 : : return -EINVAL;
819 : : break;
820 : : # endif
821 : : default:
822 : : return -ENOTSUP;
823 : : }
824 : :
825 : : return 0;
826 : : }
827 : :
828 : : /* Set session AEAD parameters */
829 : : static int
830 : 89 : openssl_set_session_aead_parameters(struct openssl_session *sess,
831 : : const struct rte_crypto_sym_xform *xform)
832 : : {
833 : : /* Select cipher key */
834 : 89 : sess->cipher.key.length = xform->aead.key.length;
835 : :
836 : : /* Set IV parameters */
837 [ + + ]: 89 : if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_CCM)
838 : : /*
839 : : * For AES-CCM, the actual IV is placed
840 : : * one byte after the start of the IV field,
841 : : * according to the API.
842 : : */
843 : 20 : sess->iv.offset = xform->aead.iv.offset + 1;
844 : : else
845 : 69 : sess->iv.offset = xform->aead.iv.offset;
846 : :
847 : 89 : sess->iv.length = xform->aead.iv.length;
848 : :
849 : 89 : sess->auth.aad_length = xform->aead.aad_length;
850 : 89 : sess->auth.digest_length = xform->aead.digest_length;
851 : :
852 : 89 : sess->aead_algo = xform->aead.algo;
853 : : /* Select cipher direction */
854 [ + + ]: 89 : if (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
855 : 45 : return openssl_set_sess_aead_enc_param(sess, xform->aead.algo,
856 : 45 : xform->aead.digest_length, xform->aead.key.data,
857 : : &sess->cipher.ctx);
858 : : else
859 : 44 : return openssl_set_sess_aead_dec_param(sess, xform->aead.algo,
860 : 44 : xform->aead.digest_length, xform->aead.key.data,
861 : : &sess->cipher.ctx);
862 : : }
863 : :
864 : : /** Parse crypto xform chain and set private session parameters */
865 : : int
866 : 276 : openssl_set_session_parameters(struct openssl_session *sess,
867 : : const struct rte_crypto_sym_xform *xform,
868 : : uint16_t nb_queue_pairs)
869 : : {
870 : : const struct rte_crypto_sym_xform *cipher_xform = NULL;
871 : : const struct rte_crypto_sym_xform *auth_xform = NULL;
872 : : const struct rte_crypto_sym_xform *aead_xform = NULL;
873 : : int ret;
874 : :
875 : 276 : sess->chain_order = openssl_get_chain_order(xform);
876 [ + + + + : 276 : switch (sess->chain_order) {
+ - ]
877 : : case OPENSSL_CHAIN_ONLY_CIPHER:
878 : : cipher_xform = xform;
879 : : break;
880 : 66 : case OPENSSL_CHAIN_ONLY_AUTH:
881 : : auth_xform = xform;
882 : 66 : break;
883 : 32 : case OPENSSL_CHAIN_CIPHER_AUTH:
884 : : cipher_xform = xform;
885 : 32 : auth_xform = xform->next;
886 : 32 : break;
887 : 37 : case OPENSSL_CHAIN_AUTH_CIPHER:
888 : : auth_xform = xform;
889 : 37 : cipher_xform = xform->next;
890 : 37 : break;
891 : 89 : case OPENSSL_CHAIN_COMBINED:
892 : : aead_xform = xform;
893 : 89 : break;
894 : : default:
895 : : return -EINVAL;
896 : : }
897 : :
898 : : /* Default IV length = 0 */
899 : 276 : sess->iv.length = 0;
900 : :
901 : : /* cipher_xform must be check before auth_xform */
902 [ + + ]: 276 : if (cipher_xform) {
903 : 121 : ret = openssl_set_session_cipher_parameters(
904 : : sess, cipher_xform);
905 [ - + ]: 121 : if (ret != 0) {
906 : 0 : OPENSSL_LOG(ERR,
907 : : "Invalid/unsupported cipher parameters");
908 : 0 : return ret;
909 : : }
910 : : }
911 : :
912 [ + + ]: 276 : if (auth_xform) {
913 : 135 : ret = openssl_set_session_auth_parameters(sess, auth_xform);
914 [ - + ]: 135 : if (ret != 0) {
915 : 0 : OPENSSL_LOG(ERR,
916 : : "Invalid/unsupported auth parameters");
917 : 0 : return ret;
918 : : }
919 : : }
920 : :
921 [ + + ]: 276 : if (aead_xform) {
922 : 89 : ret = openssl_set_session_aead_parameters(sess, aead_xform);
923 [ - + ]: 89 : if (ret != 0) {
924 : 0 : OPENSSL_LOG(ERR,
925 : : "Invalid/unsupported AEAD parameters");
926 : 0 : return ret;
927 : : }
928 : : }
929 : :
930 : : /*
931 : : * With only one queue pair, the array of copies is not needed.
932 : : * Otherwise, one entry per queue pair is required.
933 : : */
934 [ + + ]: 276 : sess->ctx_copies_len = nb_queue_pairs > 1 ? nb_queue_pairs : 0;
935 : :
936 : 276 : return 0;
937 : : }
938 : :
939 : : /** Reset private session parameters */
940 : : void
941 : 276 : openssl_reset_session(struct openssl_session *sess)
942 : : {
943 : : /* Free all the qp_ctx entries. */
944 [ + + ]: 2404 : for (uint16_t i = 0; i < sess->ctx_copies_len; i++) {
945 [ + + ]: 2128 : if (sess->qp_ctx[i].cipher != NULL) {
946 : 196 : EVP_CIPHER_CTX_free(sess->qp_ctx[i].cipher);
947 : 196 : sess->qp_ctx[i].cipher = NULL;
948 : : }
949 : :
950 [ + + + - ]: 2128 : switch (sess->auth.mode) {
951 : 1448 : case OPENSSL_AUTH_AS_AUTH:
952 : 1448 : EVP_MD_CTX_destroy(sess->qp_ctx[i].auth);
953 : 1448 : sess->qp_ctx[i].auth = NULL;
954 : 1448 : break;
955 : 632 : case OPENSSL_AUTH_AS_HMAC:
956 : 632 : free_hmac_ctx(sess->qp_ctx[i].hmac);
957 : 632 : sess->qp_ctx[i].hmac = NULL;
958 : 632 : break;
959 : 48 : case OPENSSL_AUTH_AS_CMAC:
960 : 48 : free_cmac_ctx(sess->qp_ctx[i].cmac);
961 : 48 : sess->qp_ctx[i].cmac = NULL;
962 : 48 : break;
963 : : }
964 : : }
965 : :
966 : 276 : EVP_CIPHER_CTX_free(sess->cipher.ctx);
967 : :
968 [ + + + - ]: 276 : switch (sess->auth.mode) {
969 : 183 : case OPENSSL_AUTH_AS_AUTH:
970 : 183 : EVP_MD_CTX_destroy(sess->auth.auth.ctx);
971 : 183 : break;
972 : 87 : case OPENSSL_AUTH_AS_HMAC:
973 : 87 : free_hmac_ctx(sess->auth.hmac.ctx);
974 : : break;
975 : 6 : case OPENSSL_AUTH_AS_CMAC:
976 : 6 : free_cmac_ctx(sess->auth.cmac.ctx);
977 : : break;
978 : : }
979 : :
980 [ + + ]: 276 : if (sess->chain_order == OPENSSL_CHAIN_CIPHER_BPI)
981 : 12 : EVP_CIPHER_CTX_free(sess->cipher.bpi_ctx);
982 : 276 : }
983 : :
984 : : /** Provide session for operation */
985 : : static void *
986 : 334 : get_session(struct openssl_qp *qp, struct rte_crypto_op *op)
987 : : {
988 : : struct openssl_session *sess = NULL;
989 : : struct openssl_asym_session *asym_sess = NULL;
990 : :
991 [ + + ]: 334 : if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
992 [ + + ]: 324 : if (op->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
993 : : /* get existing session */
994 [ + - ]: 284 : if (likely(op->sym->session != NULL))
995 : 284 : sess = CRYPTODEV_GET_SYM_SESS_PRIV(
996 : : op->sym->session);
997 : : } else {
998 [ + - ]: 40 : if (likely(op->asym->session != NULL))
999 : 40 : asym_sess = (struct openssl_asym_session *)
1000 : : op->asym->session->sess_private_data;
1001 : : if (asym_sess == NULL)
1002 : 0 : op->status =
1003 : : RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
1004 : 40 : return asym_sess;
1005 : : }
1006 : : } else {
1007 : : struct rte_cryptodev_sym_session *_sess;
1008 : : /* sessionless asymmetric not supported */
1009 [ + - ]: 10 : if (op->type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC)
1010 : 0 : return NULL;
1011 : :
1012 : : /* provide internal session */
1013 [ - + ]: 10 : rte_mempool_get(qp->sess_mp, (void **)&_sess);
1014 : :
1015 [ + - ]: 10 : if (_sess == NULL)
1016 : : return NULL;
1017 : :
1018 : 10 : sess = (struct openssl_session *)_sess->driver_priv_data;
1019 : :
1020 [ - + ]: 10 : if (unlikely(openssl_set_session_parameters(sess,
1021 : : op->sym->xform, 1) != 0)) {
1022 [ # # ]: 0 : rte_mempool_put(qp->sess_mp, _sess);
1023 : : sess = NULL;
1024 : : }
1025 : 10 : op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
1026 : :
1027 : : }
1028 : :
1029 [ - + ]: 294 : if (sess == NULL)
1030 : 0 : op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
1031 : :
1032 : : return sess;
1033 : : }
1034 : :
1035 : : /*
1036 : : *------------------------------------------------------------------------------
1037 : : * Process Operations
1038 : : *------------------------------------------------------------------------------
1039 : : */
1040 : : static inline int
1041 : 102 : process_openssl_encryption_update(struct rte_mbuf *mbuf_src, int offset,
1042 : : uint8_t **dst, int srclen, EVP_CIPHER_CTX *ctx, uint8_t inplace)
1043 : 102 : {
1044 : : struct rte_mbuf *m;
1045 : : int dstlen;
1046 : : int l, n = srclen;
1047 : 102 : uint8_t *src, temp[EVP_CIPHER_CTX_block_size(ctx)];
1048 : :
1049 [ + - - + ]: 102 : for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
1050 : 0 : m = m->next)
1051 : 0 : offset -= rte_pktmbuf_data_len(m);
1052 : :
1053 [ + - ]: 102 : if (m == 0)
1054 : : return -1;
1055 : :
1056 : 102 : src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
1057 [ + + ]: 102 : if (inplace)
1058 : 38 : *dst = src;
1059 : :
1060 : 102 : l = rte_pktmbuf_data_len(m) - offset;
1061 [ + + ]: 102 : if (srclen <= l) {
1062 [ + - ]: 92 : if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, srclen) <= 0)
1063 : : return -1;
1064 : 92 : *dst += l;
1065 : 92 : return 0;
1066 : : }
1067 : :
1068 [ + - ]: 10 : if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
1069 : : return -1;
1070 : :
1071 : 10 : *dst += dstlen;
1072 : 10 : n -= l;
1073 : :
1074 [ + + ]: 54 : for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
1075 : 44 : uint8_t diff = l - dstlen, rem;
1076 : :
1077 : 44 : src = rte_pktmbuf_mtod(m, uint8_t *);
1078 : 44 : l = RTE_MIN(rte_pktmbuf_data_len(m), n);
1079 [ + + ]: 44 : if (diff && inplace) {
1080 : 12 : rem = RTE_MIN(l,
1081 : : (EVP_CIPHER_CTX_block_size(ctx) - diff));
1082 [ + - ]: 12 : if (EVP_EncryptUpdate(ctx, temp,
1083 : : &dstlen, src, rem) <= 0)
1084 : : return -1;
1085 : 12 : n -= rem;
1086 [ - + ]: 12 : rte_memcpy(*dst, temp, diff);
1087 [ - + ]: 12 : rte_memcpy(src, temp + diff, rem);
1088 : 12 : src += rem;
1089 : 12 : l -= rem;
1090 : : }
1091 [ + + ]: 44 : if (inplace)
1092 : 27 : *dst = src;
1093 [ + - ]: 44 : if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
1094 : : return -1;
1095 : 44 : *dst += dstlen;
1096 : 44 : n -= l;
1097 : : }
1098 : :
1099 : : return 0;
1100 : : }
1101 : :
1102 : : static inline int
1103 : 102 : process_openssl_decryption_update(struct rte_mbuf *mbuf_src, int offset,
1104 : : uint8_t **dst, int srclen, EVP_CIPHER_CTX *ctx, uint8_t inplace)
1105 : 102 : {
1106 : : struct rte_mbuf *m;
1107 : : int dstlen;
1108 : : int l, n = srclen;
1109 : 102 : uint8_t *src, temp[EVP_CIPHER_CTX_block_size(ctx)];
1110 : :
1111 [ + - - + ]: 102 : for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
1112 : 0 : m = m->next)
1113 : 0 : offset -= rte_pktmbuf_data_len(m);
1114 : :
1115 [ + - ]: 102 : if (m == 0)
1116 : : return -1;
1117 : :
1118 : 102 : src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
1119 [ + + ]: 102 : if (inplace)
1120 : 41 : *dst = src;
1121 : :
1122 : 102 : l = rte_pktmbuf_data_len(m) - offset;
1123 [ + + ]: 102 : if (srclen <= l) {
1124 [ + - ]: 95 : if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, srclen) <= 0)
1125 : : return -1;
1126 : 95 : *dst += l;
1127 : 95 : return 0;
1128 : : }
1129 : :
1130 [ + - ]: 7 : if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
1131 : : return -1;
1132 : :
1133 : 7 : *dst += dstlen;
1134 : 7 : n -= l;
1135 : :
1136 [ + + ]: 38 : for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
1137 : 31 : uint8_t diff = l - dstlen, rem;
1138 : :
1139 : 31 : src = rte_pktmbuf_mtod(m, uint8_t *);
1140 : 31 : l = RTE_MIN(rte_pktmbuf_data_len(m), n);
1141 [ + + ]: 31 : if (diff && inplace) {
1142 : 6 : rem = RTE_MIN(l,
1143 : : (EVP_CIPHER_CTX_block_size(ctx) - diff));
1144 [ + - ]: 6 : if (EVP_DecryptUpdate(ctx, temp,
1145 : : &dstlen, src, rem) <= 0)
1146 : : return -1;
1147 : 6 : n -= rem;
1148 [ - + ]: 6 : rte_memcpy(*dst, temp, diff);
1149 [ - + ]: 6 : rte_memcpy(src, temp + diff, rem);
1150 : 6 : src += rem;
1151 : 6 : l -= rem;
1152 : : }
1153 [ + + ]: 31 : if (inplace)
1154 : 22 : *dst = src;
1155 [ + - ]: 31 : if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
1156 : : return -1;
1157 : 31 : *dst += dstlen;
1158 : 31 : n -= l;
1159 : : }
1160 : :
1161 : : return 0;
1162 : : }
1163 : :
1164 : : /** Process standard openssl cipher encryption */
1165 : : static int
1166 : 60 : process_openssl_cipher_encrypt(struct rte_mbuf *mbuf_src, uint8_t *dst,
1167 : : int offset, uint8_t *iv, int srclen, EVP_CIPHER_CTX *ctx,
1168 : : uint8_t inplace)
1169 : : {
1170 : : int totlen;
1171 : :
1172 [ - + ]: 60 : if (EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0)
1173 : 0 : goto process_cipher_encrypt_err;
1174 : :
1175 [ - + ]: 60 : if (process_openssl_encryption_update(mbuf_src, offset, &dst,
1176 : : srclen, ctx, inplace))
1177 : 0 : goto process_cipher_encrypt_err;
1178 : :
1179 [ - + ]: 60 : if (EVP_EncryptFinal_ex(ctx, dst, &totlen) <= 0)
1180 : 0 : goto process_cipher_encrypt_err;
1181 : :
1182 : : return 0;
1183 : :
1184 : 0 : process_cipher_encrypt_err:
1185 : 0 : OPENSSL_LOG(ERR, "Process openssl cipher encrypt failed");
1186 : 0 : return -EINVAL;
1187 : : }
1188 : :
1189 : : /** Process standard openssl cipher encryption */
1190 : : static int
1191 : 12 : process_openssl_cipher_bpi_encrypt(uint8_t *src, uint8_t *dst,
1192 : : uint8_t *iv, int srclen,
1193 : : EVP_CIPHER_CTX *ctx)
1194 : : {
1195 : : uint8_t i;
1196 : : uint8_t encrypted_iv[DES_BLOCK_SIZE];
1197 : : int encrypted_ivlen;
1198 : :
1199 [ + - ]: 12 : if (EVP_EncryptUpdate(ctx, encrypted_iv, &encrypted_ivlen,
1200 : : iv, DES_BLOCK_SIZE) <= 0)
1201 : 0 : goto process_cipher_encrypt_err;
1202 : :
1203 [ + + ]: 72 : for (i = 0; i < srclen; i++)
1204 : 60 : *(dst + i) = *(src + i) ^ (encrypted_iv[i]);
1205 : :
1206 : : return 0;
1207 : :
1208 : : process_cipher_encrypt_err:
1209 : 0 : OPENSSL_LOG(ERR, "Process openssl cipher bpi encrypt failed");
1210 : 0 : return -EINVAL;
1211 : : }
1212 : : /** Process standard openssl cipher decryption */
1213 : : static int
1214 : 61 : process_openssl_cipher_decrypt(struct rte_mbuf *mbuf_src, uint8_t *dst,
1215 : : int offset, uint8_t *iv, int srclen, EVP_CIPHER_CTX *ctx,
1216 : : uint8_t inplace)
1217 : : {
1218 : : int totlen;
1219 : :
1220 [ - + ]: 61 : if (EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0)
1221 : 0 : goto process_cipher_decrypt_err;
1222 : :
1223 [ - + ]: 61 : if (process_openssl_decryption_update(mbuf_src, offset, &dst,
1224 : : srclen, ctx, inplace))
1225 : 0 : goto process_cipher_decrypt_err;
1226 : :
1227 [ - + ]: 61 : if (EVP_DecryptFinal_ex(ctx, dst, &totlen) <= 0)
1228 : 0 : goto process_cipher_decrypt_err;
1229 : : return 0;
1230 : :
1231 : 0 : process_cipher_decrypt_err:
1232 : 0 : OPENSSL_LOG(ERR, "Process openssl cipher decrypt failed");
1233 : 0 : return -EINVAL;
1234 : : }
1235 : :
1236 : : /** Process cipher des 3 ctr encryption, decryption algorithm */
1237 : : static int
1238 : 12 : process_openssl_cipher_des3ctr(struct rte_mbuf *mbuf_src, uint8_t *dst,
1239 : : int offset, uint8_t *iv, int srclen, EVP_CIPHER_CTX *ctx)
1240 : : {
1241 : : uint8_t ebuf[8];
1242 : : uint64_t ctr;
1243 : : int unused, n;
1244 : : struct rte_mbuf *m;
1245 : : uint8_t *src;
1246 : : int l;
1247 : :
1248 [ + - - + ]: 12 : for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
1249 : 0 : m = m->next)
1250 : 0 : offset -= rte_pktmbuf_data_len(m);
1251 : :
1252 [ - + ]: 12 : if (m == 0)
1253 : 0 : goto process_cipher_des3ctr_err;
1254 : :
1255 : 12 : src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
1256 : 12 : l = rte_pktmbuf_data_len(m) - offset;
1257 : :
1258 : : memcpy(&ctr, iv, 8);
1259 : :
1260 [ + + ]: 6156 : for (n = 0; n < srclen; n++) {
1261 [ + + ]: 6144 : if (n % 8 == 0) {
1262 : : uint64_t cpu_ctr;
1263 : :
1264 [ - + ]: 768 : if (EVP_EncryptUpdate(ctx,
1265 : : (unsigned char *)&ebuf, &unused,
1266 : : (const unsigned char *)&ctr, 8) <= 0)
1267 : 0 : goto process_cipher_des3ctr_err;
1268 [ - + ]: 768 : cpu_ctr = rte_be_to_cpu_64(ctr);
1269 : 768 : cpu_ctr++;
1270 [ - + ]: 1536 : ctr = rte_cpu_to_be_64(cpu_ctr);
1271 : : }
1272 : 6144 : dst[n] = *(src++) ^ ebuf[n % 8];
1273 : :
1274 : 6144 : l--;
1275 [ + + ]: 6144 : if (!l) {
1276 : 4 : m = m->next;
1277 [ - + ]: 4 : if (m) {
1278 : 0 : src = rte_pktmbuf_mtod(m, uint8_t *);
1279 : 0 : l = rte_pktmbuf_data_len(m);
1280 : : }
1281 : : }
1282 : : }
1283 : :
1284 : : return 0;
1285 : :
1286 : 0 : process_cipher_des3ctr_err:
1287 : 0 : OPENSSL_LOG(ERR, "Process openssl cipher des 3 ede ctr failed");
1288 : 0 : return -EINVAL;
1289 : : }
1290 : :
1291 : : /** Process AES-GCM encrypt algorithm */
1292 : : static int
1293 : 39 : process_openssl_auth_encryption_gcm(struct rte_mbuf *mbuf_src, int offset,
1294 : : int srclen, uint8_t *aad, int aadlen, uint8_t *iv,
1295 : : uint8_t *dst, uint8_t *tag, EVP_CIPHER_CTX *ctx)
1296 : : {
1297 : 39 : int len = 0;
1298 : : #if OPENSSL_VERSION_NUMBER < 0x10100000L
1299 : : int unused = 0;
1300 : : uint8_t empty[] = {};
1301 : : #endif
1302 : :
1303 [ - + ]: 39 : if (EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0)
1304 : 0 : goto process_auth_encryption_gcm_err;
1305 : :
1306 [ + + ]: 39 : if (aadlen > 0)
1307 [ - + ]: 29 : if (EVP_EncryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
1308 : 0 : goto process_auth_encryption_gcm_err;
1309 : :
1310 [ + + ]: 39 : if (srclen > 0)
1311 [ - + ]: 32 : if (process_openssl_encryption_update(mbuf_src, offset, &dst,
1312 : : srclen, ctx, 0))
1313 : 0 : goto process_auth_encryption_gcm_err;
1314 : :
1315 : : #if OPENSSL_VERSION_NUMBER < 0x10100000L
1316 : : /* Workaround open ssl bug in version less then 1.0.1f */
1317 : : if (EVP_EncryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
1318 : : goto process_auth_encryption_gcm_err;
1319 : : #endif
1320 : :
1321 [ - + ]: 39 : if (EVP_EncryptFinal_ex(ctx, dst, &len) <= 0)
1322 : 0 : goto process_auth_encryption_gcm_err;
1323 : :
1324 [ - + ]: 39 : if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag) <= 0)
1325 : 0 : goto process_auth_encryption_gcm_err;
1326 : :
1327 : : return 0;
1328 : :
1329 : 0 : process_auth_encryption_gcm_err:
1330 : 0 : OPENSSL_LOG(ERR, "Process openssl auth encryption gcm failed");
1331 : 0 : return -EINVAL;
1332 : : }
1333 : :
1334 : : /** Process AES-CCM encrypt algorithm */
1335 : : static int
1336 : 10 : process_openssl_auth_encryption_ccm(struct rte_mbuf *mbuf_src, int offset,
1337 : : int srclen, uint8_t *aad, int aadlen, uint8_t *iv,
1338 : : uint8_t *dst, uint8_t *tag, uint8_t taglen, EVP_CIPHER_CTX *ctx)
1339 : : {
1340 : 10 : int len = 0;
1341 : :
1342 [ - + ]: 10 : if (EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0)
1343 : 0 : goto process_auth_encryption_ccm_err;
1344 : :
1345 [ - + ]: 10 : if (EVP_EncryptUpdate(ctx, NULL, &len, NULL, srclen) <= 0)
1346 : 0 : goto process_auth_encryption_ccm_err;
1347 : :
1348 [ + + ]: 10 : if (aadlen > 0)
1349 : : /*
1350 : : * For AES-CCM, the actual AAD is placed
1351 : : * 18 bytes after the start of the AAD field,
1352 : : * according to the API.
1353 : : */
1354 [ - + ]: 7 : if (EVP_EncryptUpdate(ctx, NULL, &len, aad + 18, aadlen) <= 0)
1355 : 0 : goto process_auth_encryption_ccm_err;
1356 : :
1357 [ + - ]: 10 : if (srclen >= 0)
1358 [ - + ]: 10 : if (process_openssl_encryption_update(mbuf_src, offset, &dst,
1359 : : srclen, ctx, 0))
1360 : 0 : goto process_auth_encryption_ccm_err;
1361 : :
1362 [ - + ]: 10 : if (EVP_EncryptFinal_ex(ctx, dst, &len) <= 0)
1363 : 0 : goto process_auth_encryption_ccm_err;
1364 : :
1365 [ - + ]: 10 : if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_GET_TAG, taglen, tag) <= 0)
1366 : 0 : goto process_auth_encryption_ccm_err;
1367 : :
1368 : : return 0;
1369 : :
1370 : 0 : process_auth_encryption_ccm_err:
1371 : 0 : OPENSSL_LOG(ERR, "Process openssl auth encryption ccm failed");
1372 : 0 : return -EINVAL;
1373 : : }
1374 : :
1375 : : /** Process AES-GCM decrypt algorithm */
1376 : : static int
1377 : 40 : process_openssl_auth_decryption_gcm(struct rte_mbuf *mbuf_src, int offset,
1378 : : int srclen, uint8_t *aad, int aadlen, uint8_t *iv,
1379 : : uint8_t *dst, uint8_t *tag, EVP_CIPHER_CTX *ctx)
1380 : : {
1381 : 40 : int len = 0;
1382 : : #if OPENSSL_VERSION_NUMBER < 0x10100000L
1383 : : int unused = 0;
1384 : : uint8_t empty[] = {};
1385 : : #endif
1386 : :
1387 [ - + ]: 40 : if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag) <= 0)
1388 : 0 : goto process_auth_decryption_gcm_err;
1389 : :
1390 [ - + ]: 40 : if (EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0)
1391 : 0 : goto process_auth_decryption_gcm_err;
1392 : :
1393 [ + + ]: 40 : if (aadlen > 0)
1394 [ - + ]: 30 : if (EVP_DecryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
1395 : 0 : goto process_auth_decryption_gcm_err;
1396 : :
1397 [ + + ]: 40 : if (srclen > 0)
1398 [ - + ]: 31 : if (process_openssl_decryption_update(mbuf_src, offset, &dst,
1399 : : srclen, ctx, 0))
1400 : 0 : goto process_auth_decryption_gcm_err;
1401 : :
1402 : : #if OPENSSL_VERSION_NUMBER < 0x10100000L
1403 : : /* Workaround open ssl bug in version less then 1.0.1f */
1404 : : if (EVP_DecryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
1405 : : goto process_auth_decryption_gcm_err;
1406 : : #endif
1407 : :
1408 [ + + ]: 40 : if (EVP_DecryptFinal_ex(ctx, dst, &len) <= 0)
1409 : 7 : return -EFAULT;
1410 : :
1411 : : return 0;
1412 : :
1413 : 0 : process_auth_decryption_gcm_err:
1414 : 0 : OPENSSL_LOG(ERR, "Process openssl auth decryption gcm failed");
1415 : 0 : return -EINVAL;
1416 : : }
1417 : :
1418 : : /** Process AES-CCM decrypt algorithm */
1419 : : static int
1420 : 10 : process_openssl_auth_decryption_ccm(struct rte_mbuf *mbuf_src, int offset,
1421 : : int srclen, uint8_t *aad, int aadlen, uint8_t *iv,
1422 : : uint8_t *dst, uint8_t *tag, uint8_t tag_len,
1423 : : EVP_CIPHER_CTX *ctx)
1424 : : {
1425 : 10 : int len = 0;
1426 : :
1427 [ - + ]: 10 : if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, tag_len, tag) <= 0)
1428 : 0 : goto process_auth_decryption_ccm_err;
1429 : :
1430 [ - + ]: 10 : if (EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0)
1431 : 0 : goto process_auth_decryption_ccm_err;
1432 : :
1433 [ - + ]: 10 : if (EVP_DecryptUpdate(ctx, NULL, &len, NULL, srclen) <= 0)
1434 : 0 : goto process_auth_decryption_ccm_err;
1435 : :
1436 [ + + ]: 10 : if (aadlen > 0)
1437 : : /*
1438 : : * For AES-CCM, the actual AAD is placed
1439 : : * 18 bytes after the start of the AAD field,
1440 : : * according to the API.
1441 : : */
1442 [ - + ]: 7 : if (EVP_DecryptUpdate(ctx, NULL, &len, aad + 18, aadlen) <= 0)
1443 : 0 : goto process_auth_decryption_ccm_err;
1444 : :
1445 [ + - ]: 10 : if (srclen >= 0)
1446 [ - + ]: 10 : if (process_openssl_decryption_update(mbuf_src, offset, &dst,
1447 : : srclen, ctx, 0))
1448 : 0 : return -EFAULT;
1449 : :
1450 : : return 0;
1451 : :
1452 : 0 : process_auth_decryption_ccm_err:
1453 : 0 : OPENSSL_LOG(ERR, "Process openssl auth decryption ccm failed");
1454 : 0 : return -EINVAL;
1455 : : }
1456 : :
1457 : : /** Process standard openssl auth algorithms */
1458 : : static int
1459 : 32 : process_openssl_auth(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
1460 : : __rte_unused uint8_t *iv, __rte_unused EVP_PKEY * pkey,
1461 : : int srclen, EVP_MD_CTX *ctx, const EVP_MD *algo, int digest_length)
1462 : : {
1463 : : size_t dstlen;
1464 : : struct rte_mbuf *m;
1465 : : int l, n = srclen;
1466 : : uint8_t *src;
1467 : :
1468 [ + - - + ]: 32 : for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
1469 : 0 : m = m->next)
1470 : 0 : offset -= rte_pktmbuf_data_len(m);
1471 : :
1472 [ - + ]: 32 : if (m == 0)
1473 : 0 : goto process_auth_err;
1474 : :
1475 [ - + ]: 32 : if (EVP_DigestInit_ex(ctx, algo, NULL) <= 0)
1476 : 0 : goto process_auth_err;
1477 : :
1478 : 32 : src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
1479 : :
1480 : 32 : l = rte_pktmbuf_data_len(m) - offset;
1481 [ + - ]: 32 : if (srclen <= l) {
1482 [ - + ]: 32 : if (EVP_DigestUpdate(ctx, (char *)src, srclen) <= 0)
1483 : 0 : goto process_auth_err;
1484 : 32 : goto process_auth_final;
1485 : : }
1486 : :
1487 [ # # ]: 0 : if (EVP_DigestUpdate(ctx, (char *)src, l) <= 0)
1488 : 0 : goto process_auth_err;
1489 : :
1490 : 0 : n -= l;
1491 : :
1492 [ # # ]: 0 : for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
1493 : 0 : src = rte_pktmbuf_mtod(m, uint8_t *);
1494 : 0 : l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
1495 [ # # ]: 0 : if (EVP_DigestUpdate(ctx, (char *)src, l) <= 0)
1496 : 0 : goto process_auth_err;
1497 : 0 : n -= l;
1498 : : }
1499 : :
1500 : 0 : process_auth_final:
1501 : : /* SHAKE algorithms are XOFs and require EVP_DigestFinalXOF */
1502 [ + + + + ]: 32 : if (algo == EVP_shake128() || algo == EVP_shake256()) {
1503 : : #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
1504 : : /* Set XOF output length before calling EVP_DigestFinalXOF */
1505 [ - + ]: 4 : if (EVP_MD_CTX_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, digest_length, NULL) <= 0)
1506 : 0 : goto process_auth_err;
1507 [ - + ]: 4 : if (EVP_DigestFinalXOF(ctx, dst, digest_length) <= 0)
1508 : 0 : goto process_auth_err;
1509 : : #else
1510 : : RTE_SET_USED(digest_length);
1511 : : OPENSSL_LOG(ERR, "SHAKE algorithms require OpenSSL 3.0+");
1512 : : goto process_auth_err;
1513 : : #endif
1514 : : } else {
1515 [ - + ]: 28 : if (EVP_DigestFinal_ex(ctx, dst, (unsigned int *)&dstlen) <= 0)
1516 : 0 : goto process_auth_err;
1517 : : }
1518 : :
1519 : : return 0;
1520 : :
1521 : 0 : process_auth_err:
1522 : 0 : OPENSSL_LOG(ERR, "Process openssl auth failed");
1523 : 0 : return -EINVAL;
1524 : : }
1525 : :
1526 : : # if OPENSSL_VERSION_NUMBER >= 0x30000000L
1527 : : /** Process standard openssl auth algorithms with hmac/cmac */
1528 : : static int
1529 : 100 : process_openssl_auth_mac(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
1530 : : int srclen, EVP_MAC_CTX *ctx)
1531 : : {
1532 : : size_t dstlen;
1533 : : struct rte_mbuf *m;
1534 : : int l, n = srclen;
1535 : : uint8_t *src;
1536 : :
1537 [ + - - + ]: 100 : for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
1538 : 0 : m = m->next)
1539 : 0 : offset -= rte_pktmbuf_data_len(m);
1540 : :
1541 [ - + ]: 100 : if (m == 0)
1542 : 0 : goto process_auth_err;
1543 : :
1544 [ - + ]: 100 : if (EVP_MAC_init(ctx, NULL, 0, NULL) <= 0)
1545 : 0 : goto process_auth_err;
1546 : :
1547 : 100 : src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
1548 : :
1549 : 100 : l = rte_pktmbuf_data_len(m) - offset;
1550 [ + + ]: 100 : if (srclen <= l) {
1551 [ - + ]: 90 : if (EVP_MAC_update(ctx, (unsigned char *)src, srclen) != 1)
1552 : 0 : goto process_auth_err;
1553 : 90 : goto process_auth_final;
1554 : : }
1555 : :
1556 [ - + ]: 10 : if (EVP_MAC_update(ctx, (unsigned char *)src, l) != 1)
1557 : 0 : goto process_auth_err;
1558 : :
1559 : 10 : n -= l;
1560 : :
1561 [ + + ]: 62 : for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
1562 : 52 : src = rte_pktmbuf_mtod(m, uint8_t *);
1563 : 52 : l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
1564 [ - + ]: 52 : if (EVP_MAC_update(ctx, (unsigned char *)src, l) != 1)
1565 : 0 : goto process_auth_err;
1566 : 52 : n -= l;
1567 : : }
1568 : :
1569 : 10 : process_auth_final:
1570 [ - + ]: 100 : if (EVP_MAC_final(ctx, dst, &dstlen, DIGEST_LENGTH_MAX) != 1)
1571 : 0 : goto process_auth_err;
1572 : :
1573 : : return 0;
1574 : :
1575 : 0 : process_auth_err:
1576 : 0 : OPENSSL_LOG(ERR, "Process openssl auth failed");
1577 : 0 : return -EINVAL;
1578 : : }
1579 : : # else
1580 : : /** Process standard openssl auth algorithms with hmac */
1581 : : static int
1582 : : process_openssl_auth_hmac(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
1583 : : int srclen, HMAC_CTX *ctx)
1584 : : {
1585 : : unsigned int dstlen;
1586 : : struct rte_mbuf *m;
1587 : : int l, n = srclen;
1588 : : uint8_t *src;
1589 : :
1590 : : for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
1591 : : m = m->next)
1592 : : offset -= rte_pktmbuf_data_len(m);
1593 : :
1594 : : if (m == 0)
1595 : : goto process_auth_err;
1596 : :
1597 : : src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
1598 : :
1599 : : l = rte_pktmbuf_data_len(m) - offset;
1600 : : if (srclen <= l) {
1601 : : if (HMAC_Update(ctx, (unsigned char *)src, srclen) != 1)
1602 : : goto process_auth_err;
1603 : : goto process_auth_final;
1604 : : }
1605 : :
1606 : : if (HMAC_Update(ctx, (unsigned char *)src, l) != 1)
1607 : : goto process_auth_err;
1608 : :
1609 : : n -= l;
1610 : :
1611 : : for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
1612 : : src = rte_pktmbuf_mtod(m, uint8_t *);
1613 : : l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
1614 : : if (HMAC_Update(ctx, (unsigned char *)src, l) != 1)
1615 : : goto process_auth_err;
1616 : : n -= l;
1617 : : }
1618 : :
1619 : : process_auth_final:
1620 : : if (HMAC_Final(ctx, dst, &dstlen) != 1)
1621 : : goto process_auth_err;
1622 : :
1623 : : if (unlikely(HMAC_Init_ex(ctx, NULL, 0, NULL, NULL) != 1))
1624 : : goto process_auth_err;
1625 : :
1626 : : return 0;
1627 : :
1628 : : process_auth_err:
1629 : : OPENSSL_LOG(ERR, "Process openssl auth failed");
1630 : : return -EINVAL;
1631 : : }
1632 : :
1633 : : /** Process standard openssl auth algorithms with cmac */
1634 : : static int
1635 : : process_openssl_auth_cmac(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
1636 : : int srclen, CMAC_CTX *ctx)
1637 : : {
1638 : : unsigned int dstlen;
1639 : : struct rte_mbuf *m;
1640 : : int l, n = srclen;
1641 : : uint8_t *src;
1642 : :
1643 : : for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
1644 : : m = m->next)
1645 : : offset -= rte_pktmbuf_data_len(m);
1646 : :
1647 : : if (m == 0)
1648 : : goto process_auth_err;
1649 : :
1650 : : src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
1651 : :
1652 : : l = rte_pktmbuf_data_len(m) - offset;
1653 : : if (srclen <= l) {
1654 : : if (CMAC_Update(ctx, (unsigned char *)src, srclen) != 1)
1655 : : goto process_auth_err;
1656 : : goto process_auth_final;
1657 : : }
1658 : :
1659 : : if (CMAC_Update(ctx, (unsigned char *)src, l) != 1)
1660 : : goto process_auth_err;
1661 : :
1662 : : n -= l;
1663 : :
1664 : : for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
1665 : : src = rte_pktmbuf_mtod(m, uint8_t *);
1666 : : l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
1667 : : if (CMAC_Update(ctx, (unsigned char *)src, l) != 1)
1668 : : goto process_auth_err;
1669 : : n -= l;
1670 : : }
1671 : :
1672 : : process_auth_final:
1673 : : if (CMAC_Final(ctx, dst, (size_t *)&dstlen) != 1)
1674 : : goto process_auth_err;
1675 : : return 0;
1676 : :
1677 : : process_auth_err:
1678 : : OPENSSL_LOG(ERR, "Process openssl cmac auth failed");
1679 : : return -EINVAL;
1680 : : }
1681 : : # endif
1682 : : /*----------------------------------------------------------------------------*/
1683 : :
1684 : : static inline EVP_CIPHER_CTX *
1685 : 220 : get_local_cipher_ctx(struct openssl_session *sess, struct openssl_qp *qp)
1686 : : {
1687 : : /* If the array is not being used, just return the main context. */
1688 [ + + ]: 220 : if (sess->ctx_copies_len == 0)
1689 : 10 : return sess->cipher.ctx;
1690 : :
1691 : 210 : EVP_CIPHER_CTX **lctx = &sess->qp_ctx[qp->id].cipher;
1692 : :
1693 [ + + ]: 210 : if (unlikely(*lctx == NULL)) {
1694 : : #if OPENSSL_VERSION_NUMBER >= 0x30200000L
1695 : : /* EVP_CIPHER_CTX_dup() added in OSSL 3.2 */
1696 : : *lctx = EVP_CIPHER_CTX_dup(sess->cipher.ctx);
1697 : : return *lctx;
1698 : : #elif OPENSSL_VERSION_NUMBER >= 0x30000000L
1699 [ + + ]: 196 : if (sess->chain_order == OPENSSL_CHAIN_COMBINED) {
1700 : : /* AESNI special-cased to use openssl_aesni_ctx_clone()
1701 : : * to allow for working around lack of
1702 : : * EVP_CIPHER_CTX_copy support for 3.0.0 <= OSSL Version
1703 : : * < 3.2.0.
1704 : : */
1705 [ - + ]: 97 : if (openssl_aesni_ctx_clone(lctx, sess) != 0)
1706 : 0 : *lctx = NULL;
1707 : 97 : return *lctx;
1708 : : }
1709 : : #endif
1710 : :
1711 : 99 : *lctx = EVP_CIPHER_CTX_new();
1712 : 99 : EVP_CIPHER_CTX_copy(*lctx, sess->cipher.ctx);
1713 : : }
1714 : :
1715 : 113 : return *lctx;
1716 : : }
1717 : :
1718 : : static inline EVP_MD_CTX *
1719 : 32 : get_local_auth_ctx(struct openssl_session *sess, struct openssl_qp *qp)
1720 : : {
1721 : : /* If the array is not being used, just return the main context. */
1722 [ - + ]: 32 : if (sess->ctx_copies_len == 0)
1723 : 0 : return sess->auth.auth.ctx;
1724 : :
1725 : 32 : EVP_MD_CTX **lctx = &sess->qp_ctx[qp->id].auth;
1726 : :
1727 [ + - ]: 32 : if (unlikely(*lctx == NULL)) {
1728 : : #if OPENSSL_VERSION_NUMBER >= 0x30100000L
1729 : : /* EVP_MD_CTX_dup() added in OSSL 3.1 */
1730 : : *lctx = EVP_MD_CTX_dup(sess->auth.auth.ctx);
1731 : : #else
1732 : 32 : *lctx = EVP_MD_CTX_new();
1733 : 32 : EVP_MD_CTX_copy(*lctx, sess->auth.auth.ctx);
1734 : : #endif
1735 : : }
1736 : :
1737 : 32 : return *lctx;
1738 : : }
1739 : :
1740 : : #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1741 : : static inline EVP_MAC_CTX *
1742 : : #else
1743 : : static inline HMAC_CTX *
1744 : : #endif
1745 : : get_local_hmac_ctx(struct openssl_session *sess, struct openssl_qp *qp)
1746 : : {
1747 : : #if (OPENSSL_VERSION_NUMBER >= 0x30000000L && OPENSSL_VERSION_NUMBER < 0x30003000L)
1748 : : /* For OpenSSL versions 3.0.0 <= v < 3.0.3, re-initing of
1749 : : * EVP_MAC_CTXs is broken, and doesn't actually reset their
1750 : : * state. This was fixed in OSSL commit c9ddc5af5199 ("Avoid
1751 : : * undefined behavior of provided macs on EVP_MAC
1752 : : * reinitialization"). In cases where the fix is not present,
1753 : : * fall back to duplicating the context every buffer as a
1754 : : * workaround, at the cost of performance.
1755 : : */
1756 : : RTE_SET_USED(qp);
1757 : 94 : return EVP_MAC_CTX_dup(sess->auth.hmac.ctx);
1758 : : #else
1759 : : if (sess->ctx_copies_len == 0)
1760 : : return sess->auth.hmac.ctx;
1761 : :
1762 : : #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1763 : : EVP_MAC_CTX **lctx =
1764 : : #else
1765 : : HMAC_CTX **lctx =
1766 : : #endif
1767 : : &sess->qp_ctx[qp->id].hmac;
1768 : :
1769 : : if (unlikely(*lctx == NULL)) {
1770 : : #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1771 : : *lctx = EVP_MAC_CTX_dup(sess->auth.hmac.ctx);
1772 : : #else
1773 : : *lctx = HMAC_CTX_new();
1774 : : HMAC_CTX_copy(*lctx, sess->auth.hmac.ctx);
1775 : : #endif
1776 : : }
1777 : :
1778 : : return *lctx;
1779 : : #endif
1780 : : }
1781 : :
1782 : : #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1783 : : static inline EVP_MAC_CTX *
1784 : : #else
1785 : : static inline CMAC_CTX *
1786 : : #endif
1787 : : get_local_cmac_ctx(struct openssl_session *sess, struct openssl_qp *qp)
1788 : : {
1789 : : #if (OPENSSL_VERSION_NUMBER >= 0x30000000L && OPENSSL_VERSION_NUMBER < 0x30003000L)
1790 : : /* For OpenSSL versions 3.0.0 <= v < 3.0.3, re-initing of
1791 : : * EVP_MAC_CTXs is broken, and doesn't actually reset their
1792 : : * state. This was fixed in OSSL commit c9ddc5af5199 ("Avoid
1793 : : * undefined behavior of provided macs on EVP_MAC
1794 : : * reinitialization"). In cases where the fix is not present,
1795 : : * fall back to duplicating the context every buffer as a
1796 : : * workaround, at the cost of performance.
1797 : : */
1798 : : RTE_SET_USED(qp);
1799 : 6 : return EVP_MAC_CTX_dup(sess->auth.cmac.ctx);
1800 : : #else
1801 : : if (sess->ctx_copies_len == 0)
1802 : : return sess->auth.cmac.ctx;
1803 : :
1804 : : #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1805 : : EVP_MAC_CTX **lctx =
1806 : : #else
1807 : : CMAC_CTX **lctx =
1808 : : #endif
1809 : : &sess->qp_ctx[qp->id].cmac;
1810 : :
1811 : : if (unlikely(*lctx == NULL)) {
1812 : : #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1813 : : *lctx = EVP_MAC_CTX_dup(sess->auth.cmac.ctx);
1814 : : #else
1815 : : *lctx = CMAC_CTX_new();
1816 : : CMAC_CTX_copy(*lctx, sess->auth.cmac.ctx);
1817 : : #endif
1818 : : }
1819 : :
1820 : : return *lctx;
1821 : : #endif
1822 : : }
1823 : :
1824 : : /** Process auth/cipher combined operation */
1825 : : static void
1826 [ - + ]: 99 : process_openssl_combined_op(struct openssl_qp *qp, struct rte_crypto_op *op,
1827 : : struct openssl_session *sess, struct rte_mbuf *mbuf_src,
1828 : : struct rte_mbuf *mbuf_dst)
1829 : : {
1830 : : /* cipher */
1831 : : uint8_t *dst = NULL, *iv, *tag, *aad;
1832 : : int srclen, aadlen, status = -1;
1833 : : uint32_t offset;
1834 : : uint8_t taglen;
1835 : :
1836 : : /*
1837 : : * Segmented destination buffer is not supported for
1838 : : * encryption/decryption
1839 : : */
1840 [ - + ]: 99 : if (!rte_pktmbuf_is_contiguous(mbuf_dst)) {
1841 : 0 : op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1842 : 0 : return;
1843 : : }
1844 : :
1845 : 99 : EVP_CIPHER_CTX *ctx = get_local_cipher_ctx(sess, qp);
1846 : :
1847 : 99 : iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1848 : : sess->iv.offset);
1849 [ + + ]: 99 : if (sess->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC) {
1850 : : srclen = 0;
1851 : 10 : offset = op->sym->auth.data.offset;
1852 : 10 : aadlen = op->sym->auth.data.length;
1853 : 10 : aad = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
1854 : : op->sym->auth.data.offset);
1855 : 10 : tag = op->sym->auth.digest.data;
1856 [ - + ]: 10 : if (tag == NULL)
1857 : 0 : tag = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1858 : : offset + aadlen);
1859 : : } else {
1860 : 89 : srclen = op->sym->aead.data.length;
1861 : 89 : dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1862 : : op->sym->aead.data.offset);
1863 : : offset = op->sym->aead.data.offset;
1864 : 89 : aad = op->sym->aead.aad.data;
1865 : 89 : aadlen = sess->auth.aad_length;
1866 : 89 : tag = op->sym->aead.digest.data;
1867 [ - + ]: 89 : if (tag == NULL)
1868 : 0 : tag = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1869 : : offset + srclen);
1870 : : }
1871 : :
1872 : 99 : taglen = sess->auth.digest_length;
1873 : :
1874 [ + + ]: 99 : if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
1875 [ + + ]: 49 : if (sess->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC ||
1876 [ + + ]: 45 : sess->aead_algo == RTE_CRYPTO_AEAD_AES_GCM)
1877 : 39 : status = process_openssl_auth_encryption_gcm(
1878 : : mbuf_src, offset, srclen,
1879 : : aad, aadlen, iv,
1880 : : dst, tag, ctx);
1881 : : else
1882 : 10 : status = process_openssl_auth_encryption_ccm(
1883 : : mbuf_src, offset, srclen,
1884 : : aad, aadlen, iv,
1885 : : dst, tag, taglen, ctx);
1886 : :
1887 : : } else {
1888 [ + + ]: 50 : if (sess->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC ||
1889 [ + + ]: 44 : sess->aead_algo == RTE_CRYPTO_AEAD_AES_GCM)
1890 : 40 : status = process_openssl_auth_decryption_gcm(
1891 : : mbuf_src, offset, srclen,
1892 : : aad, aadlen, iv,
1893 : : dst, tag, ctx);
1894 : : else
1895 : 10 : status = process_openssl_auth_decryption_ccm(
1896 : : mbuf_src, offset, srclen,
1897 : : aad, aadlen, iv,
1898 : : dst, tag, taglen, ctx);
1899 : : }
1900 : :
1901 [ + + ]: 99 : if (status != 0) {
1902 [ + - ]: 7 : if (status == (-EFAULT) &&
1903 [ + - ]: 7 : sess->auth.operation ==
1904 : : RTE_CRYPTO_AUTH_OP_VERIFY)
1905 : 7 : op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1906 : : else
1907 : 0 : op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1908 : : }
1909 : : }
1910 : :
1911 : : /** Process cipher operation */
1912 : : static void
1913 : 121 : process_openssl_cipher_op(struct openssl_qp *qp, struct rte_crypto_op *op,
1914 : : struct openssl_session *sess, struct rte_mbuf *mbuf_src,
1915 : : struct rte_mbuf *mbuf_dst)
1916 : : {
1917 : : uint8_t *dst, *iv;
1918 : : int srclen, status;
1919 [ + + ]: 121 : uint8_t inplace = (mbuf_src == mbuf_dst) ? 1 : 0;
1920 : :
1921 : : /*
1922 : : * Segmented OOP destination buffer is not supported for encryption/
1923 : : * decryption. In case of des3ctr, even inplace segmented buffers are
1924 : : * not supported.
1925 : : */
1926 [ + + + - ]: 121 : if (!rte_pktmbuf_is_contiguous(mbuf_dst) &&
1927 [ - + ]: 9 : (!inplace || sess->cipher.mode != OPENSSL_CIPHER_LIB)) {
1928 : 0 : op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1929 : 0 : return;
1930 : : }
1931 : :
1932 : 121 : srclen = op->sym->cipher.data.length;
1933 : 121 : dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1934 : : op->sym->cipher.data.offset);
1935 : :
1936 : 121 : iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1937 : : sess->iv.offset);
1938 : :
1939 : 121 : EVP_CIPHER_CTX *ctx = get_local_cipher_ctx(sess, qp);
1940 : :
1941 [ + + ]: 121 : if (sess->cipher.mode == OPENSSL_CIPHER_LIB)
1942 [ + + ]: 109 : if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1943 : 54 : status = process_openssl_cipher_encrypt(mbuf_src, dst,
1944 : 54 : op->sym->cipher.data.offset, iv,
1945 : : srclen, ctx, inplace);
1946 : : else
1947 : 55 : status = process_openssl_cipher_decrypt(mbuf_src, dst,
1948 : 55 : op->sym->cipher.data.offset, iv,
1949 : : srclen, ctx, inplace);
1950 : : else
1951 : 12 : status = process_openssl_cipher_des3ctr(mbuf_src, dst,
1952 : 12 : op->sym->cipher.data.offset, iv, srclen, ctx);
1953 : :
1954 [ - + ]: 121 : if (status != 0)
1955 : 0 : op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1956 : : }
1957 : :
1958 : : /** Process cipher operation */
1959 : : static void
1960 : 18 : process_openssl_docsis_bpi_op(struct rte_crypto_op *op,
1961 : : struct openssl_session *sess, struct rte_mbuf *mbuf_src,
1962 : : struct rte_mbuf *mbuf_dst)
1963 : : {
1964 : : uint8_t *src, *dst, *iv;
1965 : : uint8_t block_size, last_block_len;
1966 : : int srclen, status = 0;
1967 : :
1968 : 18 : srclen = op->sym->cipher.data.length;
1969 : 18 : src = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
1970 : : op->sym->cipher.data.offset);
1971 : 18 : dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1972 : : op->sym->cipher.data.offset);
1973 : :
1974 : 18 : iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1975 : : sess->iv.offset);
1976 : :
1977 : : block_size = DES_BLOCK_SIZE;
1978 : :
1979 : 18 : last_block_len = srclen % block_size;
1980 [ + + ]: 18 : if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
1981 : : /* Encrypt only with ECB mode XOR IV */
1982 [ + + ]: 9 : if (srclen < block_size) {
1983 : 3 : status = process_openssl_cipher_bpi_encrypt(src, dst,
1984 : : iv, srclen,
1985 : : sess->cipher.bpi_ctx);
1986 : : } else {
1987 : 6 : srclen -= last_block_len;
1988 : : /* Encrypt with the block aligned stream with CBC mode */
1989 : 6 : status = process_openssl_cipher_encrypt(mbuf_src, dst,
1990 : : op->sym->cipher.data.offset, iv,
1991 : : srclen, sess->cipher.ctx, 0);
1992 [ + + ]: 6 : if (last_block_len) {
1993 : : /* Point at last block */
1994 : 3 : dst += srclen;
1995 : : /*
1996 : : * IV is the last encrypted block from
1997 : : * the previous operation
1998 : : */
1999 : 3 : iv = dst - block_size;
2000 : 3 : src += srclen;
2001 : : srclen = last_block_len;
2002 : : /* Encrypt the last frame with ECB mode */
2003 : 3 : status |= process_openssl_cipher_bpi_encrypt(src,
2004 : : dst, iv,
2005 : : srclen, sess->cipher.bpi_ctx);
2006 : : }
2007 : : }
2008 : : } else {
2009 : : /* Decrypt only with ECB mode (encrypt, as it is same operation) */
2010 [ + + ]: 9 : if (srclen < block_size) {
2011 : 3 : status = process_openssl_cipher_bpi_encrypt(src, dst,
2012 : : iv,
2013 : : srclen,
2014 : : sess->cipher.bpi_ctx);
2015 : : } else {
2016 [ + + ]: 6 : if (last_block_len) {
2017 : : /* Point at last block */
2018 : 3 : dst += srclen - last_block_len;
2019 : 3 : src += srclen - last_block_len;
2020 : : /*
2021 : : * IV is the last full block
2022 : : */
2023 : 3 : iv = src - block_size;
2024 : : /*
2025 : : * Decrypt the last frame with ECB mode
2026 : : * (encrypt, as it is the same operation)
2027 : : */
2028 : 3 : status = process_openssl_cipher_bpi_encrypt(src,
2029 : : dst, iv,
2030 : : last_block_len, sess->cipher.bpi_ctx);
2031 : : /* Prepare parameters for CBC mode op */
2032 : 3 : iv = rte_crypto_op_ctod_offset(op, uint8_t *,
2033 : : sess->iv.offset);
2034 : 3 : dst += last_block_len - srclen;
2035 : : srclen -= last_block_len;
2036 : : }
2037 : :
2038 : : /* Decrypt with CBC mode */
2039 : 6 : status |= process_openssl_cipher_decrypt(mbuf_src, dst,
2040 : 6 : op->sym->cipher.data.offset, iv,
2041 : : srclen, sess->cipher.ctx, 0);
2042 : : }
2043 : : }
2044 : :
2045 [ - + ]: 18 : if (status != 0)
2046 : 0 : op->status = RTE_CRYPTO_OP_STATUS_ERROR;
2047 : 18 : }
2048 : :
2049 : : /** Process auth operation */
2050 : : static void
2051 : 132 : process_openssl_auth_op(struct openssl_qp *qp, struct rte_crypto_op *op,
2052 : : struct openssl_session *sess, struct rte_mbuf *mbuf_src,
2053 : : struct rte_mbuf *mbuf_dst)
2054 : : {
2055 : : uint8_t *dst;
2056 : : int srclen, status;
2057 : : EVP_MD_CTX *ctx_a;
2058 : : # if OPENSSL_VERSION_NUMBER >= 0x30000000L
2059 : : EVP_MAC_CTX *ctx_h;
2060 : : EVP_MAC_CTX *ctx_c;
2061 : : # else
2062 : : HMAC_CTX *ctx_h;
2063 : : CMAC_CTX *ctx_c;
2064 : : # endif
2065 : :
2066 : 132 : srclen = op->sym->auth.data.length;
2067 : :
2068 : 132 : dst = qp->temp_digest;
2069 : :
2070 [ + + + - ]: 132 : switch (sess->auth.mode) {
2071 : 32 : case OPENSSL_AUTH_AS_AUTH:
2072 : 32 : ctx_a = get_local_auth_ctx(sess, qp);
2073 : 32 : status = process_openssl_auth(mbuf_src, dst,
2074 : 32 : op->sym->auth.data.offset, NULL, NULL, srclen,
2075 : 32 : ctx_a, sess->auth.auth.evp_algo, sess->auth.digest_length);
2076 : 32 : break;
2077 : : case OPENSSL_AUTH_AS_HMAC:
2078 : : ctx_h = get_local_hmac_ctx(sess, qp);
2079 : : # if OPENSSL_VERSION_NUMBER >= 0x30000000L
2080 : 94 : status = process_openssl_auth_mac(mbuf_src, dst,
2081 : 94 : op->sym->auth.data.offset, srclen,
2082 : : ctx_h);
2083 : : # else
2084 : : status = process_openssl_auth_hmac(mbuf_src, dst,
2085 : : op->sym->auth.data.offset, srclen,
2086 : : ctx_h);
2087 : : # endif
2088 : : #if (OPENSSL_VERSION_NUMBER >= 0x30000000L && OPENSSL_VERSION_NUMBER < 0x30003000L)
2089 : 94 : EVP_MAC_CTX_free(ctx_h);
2090 : : #endif
2091 : 94 : break;
2092 : : case OPENSSL_AUTH_AS_CMAC:
2093 : : ctx_c = get_local_cmac_ctx(sess, qp);
2094 : : # if OPENSSL_VERSION_NUMBER >= 0x30000000L
2095 : 6 : status = process_openssl_auth_mac(mbuf_src, dst,
2096 : 6 : op->sym->auth.data.offset, srclen,
2097 : : ctx_c);
2098 : : # else
2099 : : status = process_openssl_auth_cmac(mbuf_src, dst,
2100 : : op->sym->auth.data.offset, srclen,
2101 : : ctx_c);
2102 : : # endif
2103 : : #if (OPENSSL_VERSION_NUMBER >= 0x30000000L && OPENSSL_VERSION_NUMBER < 0x30003000L)
2104 : 6 : EVP_MAC_CTX_free(ctx_c);
2105 : : #endif
2106 : 6 : break;
2107 : : default:
2108 : : status = -1;
2109 : : break;
2110 : : }
2111 : :
2112 [ + + ]: 132 : if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
2113 [ + + ]: 69 : if (CRYPTO_memcmp(dst, op->sym->auth.digest.data,
2114 : 69 : sess->auth.digest_length) != 0) {
2115 : 4 : op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
2116 : : }
2117 : : } else {
2118 : : uint8_t *auth_dst;
2119 : :
2120 : 63 : auth_dst = op->sym->auth.digest.data;
2121 [ - + ]: 63 : if (auth_dst == NULL)
2122 : 0 : auth_dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
2123 : : op->sym->auth.data.offset +
2124 : : op->sym->auth.data.length);
2125 : 63 : memcpy(auth_dst, dst, sess->auth.digest_length);
2126 : : }
2127 : :
2128 [ - + ]: 132 : if (status != 0)
2129 : 0 : op->status = RTE_CRYPTO_OP_STATUS_ERROR;
2130 : 132 : }
2131 : :
2132 : : /* process dsa sign operation */
2133 : : #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
2134 : : static int
2135 : 1 : process_openssl_dsa_sign_op_evp(struct rte_crypto_op *cop,
2136 : : struct openssl_asym_session *sess)
2137 : : {
2138 : : struct rte_crypto_dsa_op_param *op = &cop->asym->dsa;
2139 : : EVP_PKEY_CTX *dsa_ctx = NULL;
2140 : 1 : EVP_PKEY_CTX *key_ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL);
2141 : 1 : EVP_PKEY *pkey = NULL;
2142 : 1 : OSSL_PARAM_BLD *param_bld = sess->u.s.param_bld;
2143 : : OSSL_PARAM *params = NULL;
2144 : :
2145 : : size_t outlen;
2146 : : unsigned char *dsa_sign_data;
2147 : : const unsigned char *dsa_sign_data_p;
2148 : : int ret = -1;
2149 : :
2150 : 1 : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
2151 : 1 : params = OSSL_PARAM_BLD_to_param(param_bld);
2152 [ - + ]: 1 : if (!params) {
2153 : 0 : OSSL_PARAM_BLD_free(param_bld);
2154 : 0 : return -1;
2155 : : }
2156 : :
2157 [ + - ]: 1 : if (key_ctx == NULL
2158 [ + - ]: 1 : || EVP_PKEY_fromdata_init(key_ctx) <= 0
2159 [ - + ]: 1 : || EVP_PKEY_fromdata(key_ctx, &pkey,
2160 : : EVP_PKEY_KEYPAIR, params) <= 0)
2161 : 0 : goto err_dsa_sign;
2162 : :
2163 : 1 : dsa_ctx = EVP_PKEY_CTX_new(pkey, NULL);
2164 [ - + ]: 1 : if (!dsa_ctx)
2165 : 0 : goto err_dsa_sign;
2166 : :
2167 [ - + ]: 1 : if (EVP_PKEY_sign_init(dsa_ctx) <= 0)
2168 : 0 : goto err_dsa_sign;
2169 : :
2170 [ - + ]: 1 : if (EVP_PKEY_sign(dsa_ctx, NULL, &outlen, op->message.data,
2171 : : op->message.length) <= 0)
2172 : 0 : goto err_dsa_sign;
2173 : :
2174 [ - + ]: 1 : if (outlen <= 0)
2175 : 0 : goto err_dsa_sign;
2176 : :
2177 : 1 : dsa_sign_data = OPENSSL_malloc(outlen);
2178 [ - + ]: 1 : if (!dsa_sign_data)
2179 : 0 : goto err_dsa_sign;
2180 : :
2181 [ - + ]: 1 : if (EVP_PKEY_sign(dsa_ctx, dsa_sign_data, &outlen, op->message.data,
2182 : : op->message.length) <= 0) {
2183 : 0 : OPENSSL_free(dsa_sign_data);
2184 : 0 : goto err_dsa_sign;
2185 : : }
2186 : :
2187 : 1 : dsa_sign_data_p = (const unsigned char *)dsa_sign_data;
2188 : 1 : DSA_SIG *sign = d2i_DSA_SIG(NULL, &dsa_sign_data_p, outlen);
2189 [ - + ]: 1 : if (!sign) {
2190 : 0 : OPENSSL_LOG(ERR, "%s:%d", __func__, __LINE__);
2191 : 0 : OPENSSL_free(dsa_sign_data);
2192 : 0 : goto err_dsa_sign;
2193 : : } else {
2194 : 1 : const BIGNUM *r = NULL, *s = NULL;
2195 : : get_dsa_sign(sign, &r, &s);
2196 : :
2197 : 1 : op->r.length = BN_bn2bin(r, op->r.data);
2198 : 1 : op->s.length = BN_bn2bin(s, op->s.data);
2199 : 1 : cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
2200 : : }
2201 : :
2202 : : ret = 0;
2203 : 1 : DSA_SIG_free(sign);
2204 : 1 : OPENSSL_free(dsa_sign_data);
2205 : :
2206 : 1 : err_dsa_sign:
2207 : : if (params)
2208 : 1 : OSSL_PARAM_free(params);
2209 : 1 : EVP_PKEY_CTX_free(key_ctx);
2210 : 1 : EVP_PKEY_CTX_free(dsa_ctx);
2211 : 1 : EVP_PKEY_free(pkey);
2212 : 1 : return ret;
2213 : : }
2214 : :
2215 : : /* process dsa verify operation */
2216 : : static int
2217 : 1 : process_openssl_dsa_verify_op_evp(struct rte_crypto_op *cop,
2218 : : struct openssl_asym_session *sess)
2219 : : {
2220 : : struct rte_crypto_dsa_op_param *op = &cop->asym->dsa;
2221 : 1 : DSA_SIG *sign = DSA_SIG_new();
2222 : : BIGNUM *r = NULL, *s = NULL;
2223 : : BIGNUM *pub_key = NULL;
2224 : 1 : OSSL_PARAM_BLD *param_bld = sess->u.s.param_bld;
2225 : : OSSL_PARAM *params = NULL;
2226 : 1 : EVP_PKEY *pkey = NULL;
2227 : : EVP_PKEY_CTX *dsa_ctx = NULL;
2228 : 1 : EVP_PKEY_CTX *key_ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL);
2229 : 1 : unsigned char *dsa_sig = NULL;
2230 : : size_t sig_len;
2231 : : int ret = -1;
2232 : :
2233 : 1 : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
2234 [ - + ]: 1 : if (!param_bld) {
2235 : 0 : OPENSSL_LOG(ERR, " %s:%d", __func__, __LINE__);
2236 : 0 : return -1;
2237 : : }
2238 : :
2239 : 1 : r = BN_bin2bn(op->r.data, op->r.length, r);
2240 : 1 : s = BN_bin2bn(op->s.data, op->s.length, s);
2241 : 1 : pub_key = BN_bin2bn(op->y.data, op->y.length, pub_key);
2242 [ + - - + ]: 1 : if (!r || !s || !pub_key) {
2243 : 0 : BN_free(r);
2244 : 0 : BN_free(s);
2245 : 0 : BN_free(pub_key);
2246 : 0 : OSSL_PARAM_BLD_free(param_bld);
2247 : 0 : goto err_dsa_verify;
2248 : : }
2249 : :
2250 : : set_dsa_sign(sign, r, s);
2251 [ - + ]: 1 : if (!OSSL_PARAM_BLD_push_BN(param_bld, OSSL_PKEY_PARAM_PUB_KEY, pub_key)) {
2252 : 0 : OSSL_PARAM_BLD_free(param_bld);
2253 : 0 : goto err_dsa_verify;
2254 : : }
2255 : :
2256 : 1 : params = OSSL_PARAM_BLD_to_param(param_bld);
2257 [ - + ]: 1 : if (!params) {
2258 : 0 : OSSL_PARAM_BLD_free(param_bld);
2259 : 0 : goto err_dsa_verify;
2260 : : }
2261 : :
2262 [ + - ]: 1 : if (key_ctx == NULL
2263 [ + - ]: 1 : || EVP_PKEY_fromdata_init(key_ctx) <= 0
2264 [ - + ]: 1 : || EVP_PKEY_fromdata(key_ctx, &pkey, EVP_PKEY_KEYPAIR, params) <= 0)
2265 : 0 : goto err_dsa_verify;
2266 : :
2267 : 1 : dsa_ctx = EVP_PKEY_CTX_new(pkey, NULL);
2268 [ - + ]: 1 : if (!dsa_ctx)
2269 : 0 : goto err_dsa_verify;
2270 : :
2271 [ - + ]: 1 : if (!sign)
2272 : 0 : goto err_dsa_verify;
2273 : :
2274 : 1 : sig_len = i2d_DSA_SIG(sign, &dsa_sig);
2275 [ - + ]: 1 : if (EVP_PKEY_verify_init(dsa_ctx) <= 0)
2276 : 0 : goto err_dsa_verify;
2277 : :
2278 : 1 : ret = EVP_PKEY_verify(dsa_ctx, dsa_sig, sig_len,
2279 : 1 : op->message.data, op->message.length);
2280 [ + - ]: 1 : if (ret == 1) {
2281 : 1 : cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
2282 : : ret = 0;
2283 : : }
2284 : :
2285 : 1 : OPENSSL_free(dsa_sig);
2286 : 1 : err_dsa_verify:
2287 [ + - ]: 1 : if (sign)
2288 : 1 : DSA_SIG_free(sign);
2289 [ + - ]: 1 : if (params)
2290 : 1 : OSSL_PARAM_free(params);
2291 : 1 : EVP_PKEY_CTX_free(key_ctx);
2292 : 1 : EVP_PKEY_CTX_free(dsa_ctx);
2293 : :
2294 : 1 : BN_free(pub_key);
2295 : 1 : EVP_PKEY_free(pkey);
2296 : :
2297 : 1 : return ret;
2298 : : }
2299 : : #else
2300 : : static int
2301 : : process_openssl_dsa_sign_op(struct rte_crypto_op *cop,
2302 : : struct openssl_asym_session *sess)
2303 : : {
2304 : : struct rte_crypto_dsa_op_param *op = &cop->asym->dsa;
2305 : : DSA *dsa = sess->u.s.dsa;
2306 : : DSA_SIG *sign = NULL;
2307 : :
2308 : : sign = DSA_do_sign(op->message.data,
2309 : : op->message.length,
2310 : : dsa);
2311 : :
2312 : : if (sign == NULL) {
2313 : : OPENSSL_LOG(ERR, "%s:%d", __func__, __LINE__);
2314 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
2315 : : } else {
2316 : : const BIGNUM *r = NULL, *s = NULL;
2317 : : get_dsa_sign(sign, &r, &s);
2318 : :
2319 : : op->r.length = BN_bn2bin(r, op->r.data);
2320 : : op->s.length = BN_bn2bin(s, op->s.data);
2321 : : cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
2322 : : }
2323 : :
2324 : : DSA_SIG_free(sign);
2325 : :
2326 : : return 0;
2327 : : }
2328 : :
2329 : : /* process dsa verify operation */
2330 : : static int
2331 : : process_openssl_dsa_verify_op(struct rte_crypto_op *cop,
2332 : : struct openssl_asym_session *sess)
2333 : : {
2334 : : struct rte_crypto_dsa_op_param *op = &cop->asym->dsa;
2335 : : DSA *dsa = sess->u.s.dsa;
2336 : : int ret;
2337 : : DSA_SIG *sign = DSA_SIG_new();
2338 : : BIGNUM *r = NULL, *s = NULL;
2339 : : BIGNUM *pub_key = NULL;
2340 : :
2341 : : if (sign == NULL) {
2342 : : OPENSSL_LOG(ERR, " %s:%d", __func__, __LINE__);
2343 : : cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
2344 : : return -1;
2345 : : }
2346 : :
2347 : : r = BN_bin2bn(op->r.data,
2348 : : op->r.length,
2349 : : r);
2350 : : s = BN_bin2bn(op->s.data,
2351 : : op->s.length,
2352 : : s);
2353 : : pub_key = BN_bin2bn(op->y.data,
2354 : : op->y.length,
2355 : : pub_key);
2356 : : if (!r || !s || !pub_key) {
2357 : : BN_free(r);
2358 : : BN_free(s);
2359 : : BN_free(pub_key);
2360 : :
2361 : : cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
2362 : : return -1;
2363 : : }
2364 : : set_dsa_sign(sign, r, s);
2365 : : set_dsa_pub_key(dsa, pub_key);
2366 : :
2367 : : ret = DSA_do_verify(op->message.data,
2368 : : op->message.length,
2369 : : sign,
2370 : : dsa);
2371 : :
2372 : : if (ret != 1)
2373 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
2374 : : else
2375 : : cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
2376 : :
2377 : : DSA_SIG_free(sign);
2378 : :
2379 : : return 0;
2380 : : }
2381 : : #endif
2382 : :
2383 : : /* process dh operation */
2384 : : #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
2385 : : static int
2386 : 4 : process_openssl_dh_op_evp(struct rte_crypto_op *cop,
2387 : : struct openssl_asym_session *sess)
2388 : : {
2389 : : struct rte_crypto_dh_op_param *op = &cop->asym->dh;
2390 : 4 : OSSL_PARAM_BLD *param_bld = sess->u.dh.param_bld;
2391 : 4 : OSSL_PARAM_BLD *param_bld_peer = sess->u.dh.param_bld_peer;
2392 : : OSSL_PARAM *params = NULL;
2393 : 4 : EVP_PKEY *dhpkey = NULL;
2394 : 4 : EVP_PKEY *peerkey = NULL;
2395 : 4 : BIGNUM *priv_key = NULL;
2396 : 4 : BIGNUM *pub_key = NULL;
2397 : : int ret = -1;
2398 : :
2399 : 4 : cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
2400 : 4 : EVP_PKEY_CTX *dh_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DH, NULL);
2401 [ + - ]: 4 : if (dh_ctx == NULL || param_bld == NULL)
2402 : : return ret;
2403 : :
2404 [ + + ]: 4 : if (op->ke_type == RTE_CRYPTO_ASYM_KE_SHARED_SECRET_COMPUTE) {
2405 : : OSSL_PARAM *params_peer = NULL;
2406 : :
2407 [ + - ]: 1 : if (!param_bld_peer)
2408 : : return ret;
2409 : :
2410 : 1 : pub_key = BN_bin2bn(op->pub_key.data, op->pub_key.length,
2411 : : pub_key);
2412 [ - + ]: 1 : if (pub_key == NULL) {
2413 : 0 : OSSL_PARAM_BLD_free(param_bld_peer);
2414 : 0 : return ret;
2415 : : }
2416 : :
2417 [ - + ]: 1 : if (!OSSL_PARAM_BLD_push_BN(param_bld_peer, OSSL_PKEY_PARAM_PUB_KEY,
2418 : : pub_key)) {
2419 : 0 : OPENSSL_LOG(ERR, "Failed to set public key");
2420 : 0 : OSSL_PARAM_BLD_free(param_bld_peer);
2421 : 0 : BN_free(pub_key);
2422 : 0 : return ret;
2423 : : }
2424 : :
2425 : 1 : params_peer = OSSL_PARAM_BLD_to_param(param_bld_peer);
2426 [ - + ]: 1 : if (!params_peer) {
2427 : 0 : OSSL_PARAM_BLD_free(param_bld_peer);
2428 : 0 : BN_free(pub_key);
2429 : 0 : return ret;
2430 : : }
2431 : :
2432 : 1 : EVP_PKEY_CTX *peer_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DH, NULL);
2433 [ - + ]: 1 : if (EVP_PKEY_keygen_init(peer_ctx) != 1) {
2434 : 0 : OSSL_PARAM_free(params_peer);
2435 : 0 : BN_free(pub_key);
2436 : 0 : return ret;
2437 : : }
2438 : :
2439 [ - + ]: 1 : if (EVP_PKEY_CTX_set_params(peer_ctx, params_peer) != 1) {
2440 : 0 : EVP_PKEY_CTX_free(peer_ctx);
2441 : 0 : OSSL_PARAM_free(params_peer);
2442 : 0 : BN_free(pub_key);
2443 : 0 : return ret;
2444 : : }
2445 : :
2446 [ - + ]: 1 : if (EVP_PKEY_keygen(peer_ctx, &peerkey) != 1) {
2447 : 0 : EVP_PKEY_CTX_free(peer_ctx);
2448 : 0 : OSSL_PARAM_free(params_peer);
2449 : 0 : BN_free(pub_key);
2450 : 0 : return ret;
2451 : : }
2452 : :
2453 : 1 : priv_key = BN_bin2bn(op->priv_key.data, op->priv_key.length,
2454 : : priv_key);
2455 [ - + ]: 1 : if (priv_key == NULL) {
2456 : 0 : EVP_PKEY_CTX_free(peer_ctx);
2457 : 0 : OSSL_PARAM_free(params_peer);
2458 : 0 : BN_free(pub_key);
2459 : 0 : return ret;
2460 : : }
2461 : :
2462 [ - + ]: 1 : if (!OSSL_PARAM_BLD_push_BN(param_bld, OSSL_PKEY_PARAM_PRIV_KEY,
2463 : : priv_key)) {
2464 : 0 : OPENSSL_LOG(ERR, "Failed to set private key");
2465 : 0 : EVP_PKEY_CTX_free(peer_ctx);
2466 : 0 : OSSL_PARAM_free(params_peer);
2467 : 0 : BN_free(pub_key);
2468 : 0 : BN_free(priv_key);
2469 : 0 : return ret;
2470 : : }
2471 : :
2472 : 1 : OSSL_PARAM_free(params_peer);
2473 : 1 : EVP_PKEY_CTX_free(peer_ctx);
2474 : : }
2475 : :
2476 : 4 : params = OSSL_PARAM_BLD_to_param(param_bld);
2477 [ - + ]: 4 : if (!params)
2478 : 0 : goto err_dh;
2479 : :
2480 [ - + ]: 4 : if (EVP_PKEY_keygen_init(dh_ctx) != 1)
2481 : 0 : goto err_dh;
2482 : :
2483 [ - + ]: 4 : if (EVP_PKEY_CTX_set_params(dh_ctx, params) != 1)
2484 : 0 : goto err_dh;
2485 : :
2486 [ - + ]: 4 : if (EVP_PKEY_keygen(dh_ctx, &dhpkey) != 1)
2487 : 0 : goto err_dh;
2488 : :
2489 [ + + ]: 4 : if (op->ke_type == RTE_CRYPTO_ASYM_KE_PUB_KEY_GENERATE) {
2490 : 2 : OPENSSL_LOG(DEBUG, "%s:%d updated pub key", __func__, __LINE__);
2491 [ - + ]: 2 : if (!EVP_PKEY_get_bn_param(dhpkey, OSSL_PKEY_PARAM_PUB_KEY, &pub_key))
2492 : 0 : goto err_dh;
2493 : : /* output public key */
2494 : 2 : op->pub_key.length = BN_bn2bin(pub_key, op->pub_key.data);
2495 : : }
2496 : :
2497 [ + + ]: 4 : if (op->ke_type == RTE_CRYPTO_ASYM_KE_PRIV_KEY_GENERATE) {
2498 : :
2499 : 1 : OPENSSL_LOG(DEBUG, "%s:%d updated priv key", __func__, __LINE__);
2500 [ - + ]: 1 : if (!EVP_PKEY_get_bn_param(dhpkey, OSSL_PKEY_PARAM_PRIV_KEY, &priv_key))
2501 : 0 : goto err_dh;
2502 : :
2503 : : /* provide generated private key back to user */
2504 : 1 : op->priv_key.length = BN_bn2bin(priv_key, op->priv_key.data);
2505 : : }
2506 : :
2507 [ + + ]: 4 : if (op->ke_type == RTE_CRYPTO_ASYM_KE_SHARED_SECRET_COMPUTE) {
2508 : : size_t skey_len;
2509 : 1 : EVP_PKEY_CTX *sc_ctx = EVP_PKEY_CTX_new(dhpkey, NULL);
2510 [ - + ]: 1 : if (!sc_ctx)
2511 : 0 : goto err_dh;
2512 : :
2513 [ - + ]: 1 : if (EVP_PKEY_derive_init(sc_ctx) <= 0) {
2514 : 0 : EVP_PKEY_CTX_free(sc_ctx);
2515 : 0 : goto err_dh;
2516 : : }
2517 : :
2518 [ - + ]: 1 : if (!peerkey) {
2519 : 0 : EVP_PKEY_CTX_free(sc_ctx);
2520 : 0 : goto err_dh;
2521 : : }
2522 : :
2523 [ - + ]: 1 : if (EVP_PKEY_derive_set_peer(sc_ctx, peerkey) <= 0) {
2524 : 0 : EVP_PKEY_CTX_free(sc_ctx);
2525 : 0 : goto err_dh;
2526 : : }
2527 : :
2528 : : /* Determine buffer length */
2529 [ - + ]: 1 : if (EVP_PKEY_derive(sc_ctx, NULL, &skey_len) <= 0) {
2530 : 0 : EVP_PKEY_CTX_free(sc_ctx);
2531 : 0 : goto err_dh;
2532 : : }
2533 : :
2534 [ - + ]: 1 : if (EVP_PKEY_derive(sc_ctx, op->shared_secret.data, &skey_len) <= 0) {
2535 : 0 : EVP_PKEY_CTX_free(sc_ctx);
2536 : 0 : goto err_dh;
2537 : : }
2538 : :
2539 : 1 : op->shared_secret.length = skey_len;
2540 : 1 : EVP_PKEY_CTX_free(sc_ctx);
2541 : : }
2542 : :
2543 : 4 : cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
2544 : : ret = 0;
2545 : :
2546 : 4 : err_dh:
2547 : 4 : BN_free(pub_key);
2548 : 4 : BN_free(priv_key);
2549 [ + - ]: 4 : if (params)
2550 : 4 : OSSL_PARAM_free(params);
2551 : 4 : EVP_PKEY_free(dhpkey);
2552 : 4 : EVP_PKEY_free(peerkey);
2553 : :
2554 : 4 : EVP_PKEY_CTX_free(dh_ctx);
2555 : :
2556 : 4 : return ret;
2557 : : }
2558 : : #else
2559 : : static int
2560 : : process_openssl_dh_op(struct rte_crypto_op *cop,
2561 : : struct openssl_asym_session *sess)
2562 : : {
2563 : : struct rte_crypto_dh_op_param *op = &cop->asym->dh;
2564 : : struct rte_crypto_asym_op *asym_op = cop->asym;
2565 : : DH *dh_key = sess->u.dh.dh_key;
2566 : : BIGNUM *priv_key = NULL;
2567 : : int ret = 0;
2568 : :
2569 : : if (asym_op->dh.ke_type == RTE_CRYPTO_ASYM_KE_SHARED_SECRET_COMPUTE) {
2570 : : /* compute shared secret using peer public key
2571 : : * and current private key
2572 : : * shared secret = peer_key ^ priv_key mod p
2573 : : */
2574 : : BIGNUM *peer_key = NULL;
2575 : :
2576 : : /* copy private key and peer key and compute shared secret */
2577 : : peer_key = BN_bin2bn(op->pub_key.data,
2578 : : op->pub_key.length,
2579 : : peer_key);
2580 : : if (peer_key == NULL) {
2581 : : cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
2582 : : return -1;
2583 : : }
2584 : : priv_key = BN_bin2bn(op->priv_key.data,
2585 : : op->priv_key.length,
2586 : : priv_key);
2587 : : if (priv_key == NULL) {
2588 : : BN_free(peer_key);
2589 : : cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
2590 : : return -1;
2591 : : }
2592 : : ret = set_dh_priv_key(dh_key, priv_key);
2593 : : if (ret) {
2594 : : OPENSSL_LOG(ERR, "Failed to set private key");
2595 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
2596 : : BN_free(peer_key);
2597 : : BN_free(priv_key);
2598 : : return 0;
2599 : : }
2600 : :
2601 : : ret = DH_compute_key(
2602 : : op->shared_secret.data,
2603 : : peer_key, dh_key);
2604 : : if (ret < 0) {
2605 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
2606 : : BN_free(peer_key);
2607 : : /* priv key is already loaded into dh,
2608 : : * let's not free that directly here.
2609 : : * DH_free() will auto free it later.
2610 : : */
2611 : : return 0;
2612 : : }
2613 : : cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
2614 : : op->shared_secret.length = ret;
2615 : : BN_free(peer_key);
2616 : : return 0;
2617 : : }
2618 : :
2619 : : /*
2620 : : * other options are public and private key generations.
2621 : : *
2622 : : * if user provides private key,
2623 : : * then first set DH with user provided private key
2624 : : */
2625 : : if (asym_op->dh.ke_type == RTE_CRYPTO_ASYM_KE_PUB_KEY_GENERATE &&
2626 : : op->priv_key.length) {
2627 : : /* generate public key using user-provided private key
2628 : : * pub_key = g ^ priv_key mod p
2629 : : */
2630 : :
2631 : : /* load private key into DH */
2632 : : priv_key = BN_bin2bn(op->priv_key.data,
2633 : : op->priv_key.length,
2634 : : priv_key);
2635 : : if (priv_key == NULL) {
2636 : : cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
2637 : : return -1;
2638 : : }
2639 : : ret = set_dh_priv_key(dh_key, priv_key);
2640 : : if (ret) {
2641 : : OPENSSL_LOG(ERR, "Failed to set private key");
2642 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
2643 : : BN_free(priv_key);
2644 : : return 0;
2645 : : }
2646 : : }
2647 : :
2648 : : /* generate public and private key pair.
2649 : : *
2650 : : * if private key already set, generates only public key.
2651 : : *
2652 : : * if private key is not already set, then set it to random value
2653 : : * and update internal private key.
2654 : : */
2655 : : if (!DH_generate_key(dh_key)) {
2656 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
2657 : : return 0;
2658 : : }
2659 : :
2660 : : if (asym_op->dh.ke_type == RTE_CRYPTO_ASYM_KE_PUB_KEY_GENERATE) {
2661 : : const BIGNUM *pub_key = NULL;
2662 : :
2663 : : OPENSSL_LOG(DEBUG, "%s:%d update public key",
2664 : : __func__, __LINE__);
2665 : :
2666 : : /* get the generated keys */
2667 : : get_dh_pub_key(dh_key, &pub_key);
2668 : :
2669 : : /* output public key */
2670 : : op->pub_key.length = BN_bn2bin(pub_key,
2671 : : op->pub_key.data);
2672 : : }
2673 : :
2674 : : if (asym_op->dh.ke_type == RTE_CRYPTO_ASYM_KE_PRIV_KEY_GENERATE) {
2675 : : const BIGNUM *priv_key = NULL;
2676 : :
2677 : : OPENSSL_LOG(DEBUG, "%s:%d updated priv key",
2678 : : __func__, __LINE__);
2679 : :
2680 : : /* get the generated keys */
2681 : : get_dh_priv_key(dh_key, &priv_key);
2682 : :
2683 : : /* provide generated private key back to user */
2684 : : op->priv_key.length = BN_bn2bin(priv_key,
2685 : : op->priv_key.data);
2686 : : }
2687 : :
2688 : : cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
2689 : :
2690 : : return 0;
2691 : : }
2692 : : #endif
2693 : :
2694 : : /* process modinv operation */
2695 : : static int
2696 : 2 : process_openssl_modinv_op(struct rte_crypto_op *cop,
2697 : : struct openssl_asym_session *sess)
2698 : : {
2699 : : struct rte_crypto_asym_op *op = cop->asym;
2700 : 2 : BIGNUM *base = BN_CTX_get(sess->u.m.ctx);
2701 : 2 : BIGNUM *res = BN_CTX_get(sess->u.m.ctx);
2702 : :
2703 [ - + ]: 2 : if (unlikely(base == NULL || res == NULL)) {
2704 : 0 : BN_free(base);
2705 : 0 : BN_free(res);
2706 : 0 : cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
2707 : 0 : return -1;
2708 : : }
2709 : :
2710 : 2 : base = BN_bin2bn((const unsigned char *)op->modinv.base.data,
2711 : 2 : op->modinv.base.length, base);
2712 : :
2713 [ + - ]: 2 : if (BN_mod_inverse(res, base, sess->u.m.modulus, sess->u.m.ctx)) {
2714 : 2 : cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
2715 : 2 : op->modinv.result.length = BN_bn2bin(res, op->modinv.result.data);
2716 : : } else {
2717 : 0 : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
2718 : : }
2719 : :
2720 : 2 : BN_clear(res);
2721 : 2 : BN_clear(base);
2722 : :
2723 : 2 : return 0;
2724 : : }
2725 : :
2726 : : /* process modexp operation */
2727 : : static int
2728 : 12 : process_openssl_modexp_op(struct rte_crypto_op *cop,
2729 : : struct openssl_asym_session *sess)
2730 : : {
2731 : : struct rte_crypto_asym_op *op = cop->asym;
2732 : 12 : BIGNUM *base = BN_CTX_get(sess->u.e.ctx);
2733 : 12 : BIGNUM *res = BN_CTX_get(sess->u.e.ctx);
2734 : :
2735 [ - + ]: 12 : if (unlikely(base == NULL || res == NULL)) {
2736 : 0 : BN_free(base);
2737 : 0 : BN_free(res);
2738 : 0 : cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
2739 : 0 : return -1;
2740 : : }
2741 : :
2742 : 12 : base = BN_bin2bn((const unsigned char *)op->modex.base.data,
2743 : 12 : op->modex.base.length, base);
2744 : :
2745 [ + - ]: 12 : if (BN_mod_exp(res, base, sess->u.e.exp,
2746 : 12 : sess->u.e.mod, sess->u.e.ctx)) {
2747 : 12 : op->modex.result.length = BN_bn2bin(res, op->modex.result.data);
2748 : 12 : cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
2749 : : } else {
2750 : 0 : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
2751 : : }
2752 : :
2753 : 12 : BN_clear(res);
2754 : 12 : BN_clear(base);
2755 : :
2756 : 12 : return 0;
2757 : : }
2758 : :
2759 : : /* process rsa operations */
2760 : : #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
2761 : : static int
2762 : 14 : process_openssl_rsa_op_evp(struct rte_crypto_op *cop,
2763 : : struct openssl_asym_session *sess)
2764 : : {
2765 : : struct rte_crypto_asym_op *op = cop->asym;
2766 : 14 : uint32_t pad = sess->u.r.pad;
2767 : : uint8_t *tmp;
2768 : 14 : size_t outlen = 0;
2769 : : int ret = -1;
2770 : :
2771 : 14 : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
2772 : 14 : EVP_PKEY_CTX *rsa_ctx = sess->u.r.ctx;
2773 [ + - ]: 14 : if (!rsa_ctx)
2774 : : return ret;
2775 : :
2776 [ + - + ]: 14 : switch (pad) {
2777 : : case RTE_CRYPTO_RSA_PADDING_PKCS1_5:
2778 : : pad = RSA_PKCS1_PADDING;
2779 : : break;
2780 : 4 : case RTE_CRYPTO_RSA_PADDING_NONE:
2781 : : pad = RSA_NO_PADDING;
2782 : 4 : break;
2783 : 0 : default:
2784 : 0 : cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
2785 : 0 : OPENSSL_LOG(ERR,
2786 : : "rsa pad type not supported %d", pad);
2787 : 0 : return ret;
2788 : : }
2789 : :
2790 [ + + + + : 14 : switch (op->rsa.op_type) {
- ]
2791 : 4 : case RTE_CRYPTO_ASYM_OP_ENCRYPT:
2792 [ - + ]: 4 : if (EVP_PKEY_encrypt_init(rsa_ctx) != 1)
2793 : 0 : goto err_rsa;
2794 : :
2795 [ - + ]: 4 : if (EVP_PKEY_CTX_set_rsa_padding(rsa_ctx, pad) <= 0)
2796 : 0 : goto err_rsa;
2797 : :
2798 [ - + ]: 4 : if (EVP_PKEY_encrypt(rsa_ctx, NULL, &outlen,
2799 : 4 : op->rsa.message.data,
2800 : : op->rsa.message.length) <= 0)
2801 : 0 : goto err_rsa;
2802 : :
2803 [ - + ]: 4 : if (outlen <= 0)
2804 : 0 : goto err_rsa;
2805 : :
2806 [ - + ]: 4 : if (EVP_PKEY_encrypt(rsa_ctx, op->rsa.cipher.data, &outlen,
2807 : 4 : op->rsa.message.data,
2808 : : op->rsa.message.length) <= 0)
2809 : 0 : goto err_rsa;
2810 : 4 : op->rsa.cipher.length = outlen;
2811 : :
2812 : 4 : OPENSSL_LOG(DEBUG,
2813 : : "length of encrypted text %zu", outlen);
2814 : 4 : break;
2815 : :
2816 : 4 : case RTE_CRYPTO_ASYM_OP_DECRYPT:
2817 [ - + ]: 4 : if (EVP_PKEY_decrypt_init(rsa_ctx) != 1)
2818 : 0 : goto err_rsa;
2819 : :
2820 [ - + ]: 4 : if (EVP_PKEY_CTX_set_rsa_padding(rsa_ctx, pad) <= 0)
2821 : 0 : goto err_rsa;
2822 : :
2823 [ - + ]: 4 : if (EVP_PKEY_decrypt(rsa_ctx, NULL, &outlen,
2824 : 4 : op->rsa.cipher.data,
2825 : : op->rsa.cipher.length) <= 0)
2826 : 0 : goto err_rsa;
2827 : :
2828 [ - + ]: 4 : if (outlen <= 0)
2829 : 0 : goto err_rsa;
2830 : :
2831 [ - + ]: 4 : if (EVP_PKEY_decrypt(rsa_ctx, op->rsa.message.data, &outlen,
2832 : 4 : op->rsa.cipher.data,
2833 : : op->rsa.cipher.length) <= 0)
2834 : 0 : goto err_rsa;
2835 : 4 : op->rsa.message.length = outlen;
2836 : :
2837 : 4 : OPENSSL_LOG(DEBUG, "length of decrypted text %zu", outlen);
2838 : 4 : break;
2839 : :
2840 : 2 : case RTE_CRYPTO_ASYM_OP_SIGN:
2841 [ - + ]: 2 : if (EVP_PKEY_sign_init(rsa_ctx) <= 0)
2842 : 0 : goto err_rsa;
2843 : :
2844 [ - + ]: 2 : if (EVP_PKEY_CTX_set_rsa_padding(rsa_ctx, pad) <= 0)
2845 : 0 : goto err_rsa;
2846 : :
2847 [ - + ]: 2 : if (EVP_PKEY_sign(rsa_ctx, NULL, &outlen,
2848 : 2 : op->rsa.message.data,
2849 : : op->rsa.message.length) <= 0)
2850 : 0 : goto err_rsa;
2851 : :
2852 [ - + ]: 2 : if (outlen <= 0)
2853 : 0 : goto err_rsa;
2854 : :
2855 [ - + ]: 2 : if (EVP_PKEY_sign(rsa_ctx, op->rsa.sign.data, &outlen,
2856 : 2 : op->rsa.message.data,
2857 : : op->rsa.message.length) <= 0)
2858 : 0 : goto err_rsa;
2859 : 2 : op->rsa.sign.length = outlen;
2860 : 2 : break;
2861 : :
2862 : 4 : case RTE_CRYPTO_ASYM_OP_VERIFY:
2863 [ - + ]: 4 : if (EVP_PKEY_verify_recover_init(rsa_ctx) <= 0)
2864 : 0 : goto err_rsa;
2865 : :
2866 [ - + ]: 4 : if (EVP_PKEY_CTX_set_rsa_padding(rsa_ctx, pad) <= 0)
2867 : 0 : goto err_rsa;
2868 : :
2869 [ - + ]: 4 : if (EVP_PKEY_verify_recover(rsa_ctx, NULL, &outlen,
2870 : 4 : op->rsa.sign.data,
2871 : : op->rsa.sign.length) <= 0)
2872 : 0 : goto err_rsa;
2873 : :
2874 [ + - - + ]: 4 : if ((outlen <= 0) || (outlen != op->rsa.sign.length))
2875 : 0 : goto err_rsa;
2876 : :
2877 : 4 : tmp = OPENSSL_malloc(outlen);
2878 [ - + ]: 4 : if (tmp == NULL) {
2879 : 0 : OPENSSL_LOG(ERR, "Memory allocation failed");
2880 : 0 : goto err_rsa;
2881 : : }
2882 : :
2883 : 4 : ret = EVP_PKEY_verify_recover(rsa_ctx, tmp, &outlen,
2884 : 4 : op->rsa.sign.data,
2885 : : op->rsa.sign.length);
2886 [ + + ]: 4 : if (ret <= 0) {
2887 : : /* OpenSSL RSA verification returns one on
2888 : : * successful verification, otherwise 0. Hence,
2889 : : * this enqueue operation should succeed even if
2890 : : * invalid signature has been requested in verify.
2891 : : */
2892 : 2 : OPENSSL_free(tmp);
2893 : 2 : goto err_rsa;
2894 : : }
2895 : :
2896 : 2 : OPENSSL_LOG(DEBUG,
2897 : : "Length of public_decrypt %zu "
2898 : : "length of message %zd",
2899 : : outlen, op->rsa.message.length);
2900 [ - + ]: 2 : if (CRYPTO_memcmp(tmp, op->rsa.message.data,
2901 : : op->rsa.message.length)) {
2902 : 0 : OPENSSL_LOG(ERR, "RSA sign Verification failed");
2903 : : }
2904 : 2 : OPENSSL_free(tmp);
2905 : 2 : break;
2906 : :
2907 : 0 : default:
2908 : : /* allow ops with invalid args to be pushed to
2909 : : * completion queue
2910 : : */
2911 : 0 : cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
2912 : 0 : goto err_rsa;
2913 : : }
2914 : :
2915 : : ret = 0;
2916 : 12 : cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
2917 : : err_rsa:
2918 : : return ret;
2919 : :
2920 : : }
2921 : :
2922 : : static int
2923 : 0 : process_openssl_ecfpm_op_evp(struct rte_crypto_op *cop,
2924 : : struct openssl_asym_session *sess)
2925 : : {
2926 : 0 : const EC_GROUP *ecgrp = sess->u.ec.group;
2927 : : EC_POINT *ecpt = NULL;
2928 : : BN_CTX *ctx = NULL;
2929 : : BIGNUM *n = NULL;
2930 : : int ret = -1;
2931 : :
2932 : 0 : n = BN_bin2bn((const unsigned char *)
2933 : 0 : cop->asym->ecpm.scalar.data,
2934 : 0 : cop->asym->ecpm.scalar.length,
2935 : : BN_new());
2936 : :
2937 : 0 : ctx = BN_CTX_new();
2938 [ # # ]: 0 : if (!ctx)
2939 : 0 : goto err_ecfpm;
2940 : :
2941 [ # # ]: 0 : if (!EC_POINT_mul(ecgrp, ecpt, n, NULL, NULL, ctx))
2942 : 0 : goto err_ecfpm;
2943 : :
2944 [ # # ]: 0 : if (cop->asym->flags & RTE_CRYPTO_ASYM_FLAG_PUB_KEY_COMPRESSED) {
2945 : 0 : unsigned char *buf = cop->asym->ecpm.r.x.data;
2946 : : size_t sz;
2947 : :
2948 : 0 : sz = EC_POINT_point2oct(ecgrp, ecpt, POINT_CONVERSION_COMPRESSED, buf, 0, ctx);
2949 [ # # ]: 0 : if (!sz)
2950 : 0 : goto err_ecfpm;
2951 : :
2952 : 0 : cop->asym->ecpm.r.x.length = sz;
2953 : : }
2954 : :
2955 : 0 : err_ecfpm:
2956 : 0 : BN_CTX_free(ctx);
2957 : 0 : BN_free(n);
2958 : 0 : return ret;
2959 : : }
2960 : :
2961 : : static int
2962 : 6 : process_openssl_sm2_op_evp(struct rte_crypto_op *cop,
2963 : : struct openssl_asym_session *sess)
2964 : : {
2965 : : EVP_PKEY_CTX *kctx = NULL, *sctx = NULL, *cctx = NULL;
2966 : : struct rte_crypto_asym_op *op = cop->asym;
2967 : 6 : OSSL_PARAM *params = sess->u.sm2.params;
2968 : : EVP_MD_CTX *md_ctx = NULL;
2969 : : ECDSA_SIG *ec_sign = NULL;
2970 : : EVP_MD *check_md = NULL;
2971 : 6 : EVP_PKEY *pkey = NULL;
2972 : : int ret = -1;
2973 : :
2974 : 6 : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
2975 : :
2976 [ - + ]: 6 : if (cop->asym->sm2.k.data != NULL)
2977 : 0 : goto err_sm2;
2978 : :
2979 [ + + + + : 6 : switch (op->sm2.op_type) {
- ]
2980 : 1 : case RTE_CRYPTO_ASYM_OP_ENCRYPT:
2981 : : {
2982 : : OSSL_PARAM *eparams = sess->u.sm2.params;
2983 : 1 : size_t output_len = 0;
2984 : :
2985 : 1 : kctx = EVP_PKEY_CTX_new_id(EVP_PKEY_SM2, NULL);
2986 [ + - + - : 2 : if (kctx == NULL || EVP_PKEY_fromdata_init(kctx) <= 0 ||
- + ]
2987 : 1 : EVP_PKEY_fromdata(kctx, &pkey, EVP_PKEY_KEYPAIR, params) <= 0)
2988 : 0 : goto err_sm2;
2989 : :
2990 : 1 : cctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
2991 [ - + ]: 1 : if (!cctx)
2992 : 0 : goto err_sm2;
2993 : :
2994 [ - + ]: 1 : if (!EVP_PKEY_encrypt_init(cctx))
2995 : 0 : goto err_sm2;
2996 : :
2997 [ - + ]: 1 : if (!EVP_PKEY_CTX_set_params(cctx, eparams))
2998 : 0 : goto err_sm2;
2999 : :
3000 [ - + ]: 1 : if (!EVP_PKEY_encrypt(cctx, op->sm2.cipher.data, &output_len,
3001 : 1 : op->sm2.message.data,
3002 : : op->sm2.message.length))
3003 : 0 : goto err_sm2;
3004 : 1 : op->sm2.cipher.length = output_len;
3005 : : }
3006 : 1 : break;
3007 : 2 : case RTE_CRYPTO_ASYM_OP_DECRYPT:
3008 : : {
3009 : : OSSL_PARAM *eparams = sess->u.sm2.params;
3010 : :
3011 : 2 : kctx = EVP_PKEY_CTX_new_id(EVP_PKEY_SM2, NULL);
3012 [ + - ]: 2 : if (kctx == NULL
3013 [ + - ]: 2 : || EVP_PKEY_fromdata_init(kctx) <= 0
3014 [ - + ]: 2 : || EVP_PKEY_fromdata(kctx, &pkey, EVP_PKEY_KEYPAIR, params) <= 0)
3015 : 0 : goto err_sm2;
3016 : :
3017 : 2 : cctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
3018 [ - + ]: 2 : if (!cctx)
3019 : 0 : goto err_sm2;
3020 : :
3021 [ - + ]: 2 : if (!EVP_PKEY_decrypt_init(cctx))
3022 : 0 : goto err_sm2;
3023 : :
3024 [ - + ]: 2 : if (!EVP_PKEY_CTX_set_params(cctx, eparams))
3025 : 0 : goto err_sm2;
3026 : :
3027 [ - + ]: 2 : if (!EVP_PKEY_decrypt(cctx, op->sm2.message.data, &op->sm2.message.length,
3028 : 2 : op->sm2.cipher.data, op->sm2.cipher.length))
3029 : 0 : goto err_sm2;
3030 : : }
3031 : : break;
3032 : 1 : case RTE_CRYPTO_ASYM_OP_SIGN:
3033 : : {
3034 : 1 : unsigned char signbuf[128] = {0};
3035 : : const unsigned char *signptr;
3036 : : const BIGNUM *r, *s;
3037 : : size_t signlen;
3038 : :
3039 : 1 : kctx = EVP_PKEY_CTX_new_from_name(NULL, "SM2", NULL);
3040 [ + - + - : 2 : if (kctx == NULL || EVP_PKEY_fromdata_init(kctx) <= 0 ||
- + ]
3041 : 1 : EVP_PKEY_fromdata(kctx, &pkey, EVP_PKEY_KEYPAIR, params) <= 0)
3042 : 0 : goto err_sm2;
3043 : :
3044 : 1 : md_ctx = EVP_MD_CTX_new();
3045 [ - + ]: 1 : if (!md_ctx)
3046 : 0 : goto err_sm2;
3047 : :
3048 : 1 : sctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
3049 [ - + ]: 1 : if (!sctx)
3050 : 0 : goto err_sm2;
3051 : :
3052 : 1 : EVP_MD_CTX_set_pkey_ctx(md_ctx, sctx);
3053 : :
3054 : 1 : check_md = EVP_MD_fetch(NULL, "sm3", NULL);
3055 [ - + ]: 1 : if (!check_md)
3056 : 0 : goto err_sm2;
3057 : :
3058 [ - + ]: 1 : if (!EVP_DigestSignInit(md_ctx, NULL, check_md, NULL, pkey))
3059 : 0 : goto err_sm2;
3060 : :
3061 [ - + ]: 1 : if (EVP_PKEY_CTX_set1_id(sctx, op->sm2.id.data, op->sm2.id.length) <= 0)
3062 : 0 : goto err_sm2;
3063 : :
3064 [ - + ]: 1 : if (!EVP_DigestSignUpdate(md_ctx, op->sm2.message.data,
3065 : : op->sm2.message.length))
3066 : 0 : goto err_sm2;
3067 : :
3068 [ - + ]: 1 : if (!EVP_DigestSignFinal(md_ctx, NULL, &signlen))
3069 : 0 : goto err_sm2;
3070 : :
3071 [ - + ]: 1 : if (!EVP_DigestSignFinal(md_ctx, signbuf, &signlen))
3072 : 0 : goto err_sm2;
3073 : :
3074 : 1 : signptr = signbuf;
3075 : 1 : ec_sign = d2i_ECDSA_SIG(NULL, &signptr, signlen);
3076 [ - + ]: 1 : if (!ec_sign)
3077 : 0 : goto err_sm2;
3078 : :
3079 : 1 : r = ECDSA_SIG_get0_r(ec_sign);
3080 : 1 : s = ECDSA_SIG_get0_s(ec_sign);
3081 [ - + ]: 1 : if (!r || !s)
3082 : 0 : goto err_sm2;
3083 : :
3084 : 1 : op->sm2.r.length = BN_num_bytes(r);
3085 : 1 : op->sm2.s.length = BN_num_bytes(s);
3086 : 1 : BN_bn2bin(r, op->sm2.r.data);
3087 : 1 : BN_bn2bin(s, op->sm2.s.data);
3088 : :
3089 : 1 : ECDSA_SIG_free(ec_sign);
3090 : : }
3091 : 1 : break;
3092 : 2 : case RTE_CRYPTO_ASYM_OP_VERIFY:
3093 : : {
3094 : 2 : unsigned char signbuf[128] = {0}, *signbuf_new = NULL;
3095 : : BIGNUM *r = NULL, *s = NULL;
3096 : : size_t signlen;
3097 : :
3098 : 2 : kctx = EVP_PKEY_CTX_new_from_name(NULL, "SM2", NULL);
3099 [ + - + - : 4 : if (kctx == NULL || EVP_PKEY_fromdata_init(kctx) <= 0 ||
- + ]
3100 : 2 : EVP_PKEY_fromdata(kctx, &pkey, EVP_PKEY_PUBLIC_KEY, params) <= 0)
3101 : 0 : goto err_sm2;
3102 : :
3103 [ - + ]: 2 : if (!EVP_PKEY_is_a(pkey, "SM2"))
3104 : 0 : goto err_sm2;
3105 : :
3106 : 2 : md_ctx = EVP_MD_CTX_new();
3107 [ - + ]: 2 : if (!md_ctx)
3108 : 0 : goto err_sm2;
3109 : :
3110 : 2 : sctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
3111 [ - + ]: 2 : if (!sctx)
3112 : 0 : goto err_sm2;
3113 : :
3114 : 2 : EVP_MD_CTX_set_pkey_ctx(md_ctx, sctx);
3115 : :
3116 : 2 : check_md = EVP_MD_fetch(NULL, "sm3", NULL);
3117 [ - + ]: 2 : if (!check_md)
3118 : 0 : goto err_sm2;
3119 : :
3120 [ - + ]: 2 : if (!EVP_DigestVerifyInit(md_ctx, NULL, check_md, NULL, pkey))
3121 : 0 : goto err_sm2;
3122 : :
3123 [ - + ]: 2 : if (EVP_PKEY_CTX_set1_id(sctx, op->sm2.id.data, op->sm2.id.length) <= 0)
3124 : 0 : goto err_sm2;
3125 : :
3126 [ - + ]: 2 : if (!EVP_DigestVerifyUpdate(md_ctx, op->sm2.message.data,
3127 : : op->sm2.message.length))
3128 : 0 : goto err_sm2;
3129 : :
3130 : 2 : ec_sign = ECDSA_SIG_new();
3131 [ - + ]: 2 : if (!ec_sign)
3132 : 0 : goto err_sm2;
3133 : :
3134 : 2 : r = BN_bin2bn(op->sm2.r.data, op->sm2.r.length, r);
3135 : 2 : s = BN_bin2bn(op->sm2.s.data, op->sm2.s.length, s);
3136 [ - + ]: 2 : if (!r || !s)
3137 : 0 : goto err_sm2;
3138 : :
3139 [ - + ]: 2 : if (!ECDSA_SIG_set0(ec_sign, r, s)) {
3140 : 0 : BN_free(r);
3141 : 0 : BN_free(s);
3142 : 0 : goto err_sm2;
3143 : : }
3144 : :
3145 : : r = NULL;
3146 : : s = NULL;
3147 : :
3148 : 2 : signbuf_new = signbuf;
3149 : 2 : signlen = i2d_ECDSA_SIG(ec_sign, (unsigned char **)&signbuf_new);
3150 [ - + ]: 2 : if (signlen <= 0)
3151 : 0 : goto err_sm2;
3152 : :
3153 [ - + ]: 2 : if (!EVP_DigestVerifyFinal(md_ctx, signbuf_new, signlen))
3154 : 0 : goto err_sm2;
3155 : :
3156 : 2 : BN_free(r);
3157 : 2 : BN_free(s);
3158 : 2 : ECDSA_SIG_free(ec_sign);
3159 : : }
3160 : 2 : break;
3161 : 0 : default:
3162 : : /* allow ops with invalid args to be pushed to
3163 : : * completion queue
3164 : : */
3165 : 0 : cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
3166 : 0 : goto err_sm2;
3167 : : }
3168 : :
3169 : : ret = 0;
3170 : 6 : cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
3171 : 6 : err_sm2:
3172 : 6 : EVP_MD_free(check_md);
3173 : 6 : EVP_MD_CTX_free(md_ctx);
3174 : :
3175 : 6 : EVP_PKEY_CTX_free(kctx);
3176 : :
3177 : 6 : EVP_PKEY_CTX_free(sctx);
3178 : :
3179 : 6 : EVP_PKEY_CTX_free(cctx);
3180 : :
3181 : 6 : EVP_PKEY_free(pkey);
3182 : :
3183 : 6 : return ret;
3184 : : }
3185 : :
3186 : : static int
3187 : 0 : process_openssl_eddsa_op_evp(struct rte_crypto_op *cop,
3188 : : struct openssl_asym_session *sess)
3189 : : {
3190 : : static const char * const instance[] = {"Ed25519", "Ed25519ctx", "Ed25519ph",
3191 : : "Ed448", "Ed448ph"};
3192 : : EVP_PKEY_CTX *kctx = NULL, *sctx = NULL, *cctx = NULL;
3193 : 0 : const uint8_t curve_id = sess->u.eddsa.curve_id;
3194 : : struct rte_crypto_asym_op *op = cop->asym;
3195 : 0 : OSSL_PARAM *params = sess->u.eddsa.params;
3196 : : OSSL_PARAM_BLD *iparam_bld = NULL;
3197 : : OSSL_PARAM *iparams = NULL;
3198 : 0 : uint8_t signbuf[128] = {0};
3199 : : EVP_MD_CTX *md_ctx = NULL;
3200 : 0 : EVP_PKEY *pkey = NULL;
3201 : : size_t signlen;
3202 : : int ret = -1;
3203 : :
3204 : 0 : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3205 : :
3206 : 0 : iparam_bld = OSSL_PARAM_BLD_new();
3207 [ # # ]: 0 : if (!iparam_bld)
3208 : 0 : goto err_eddsa;
3209 : :
3210 [ # # ]: 0 : if (op->eddsa.instance == RTE_CRYPTO_EDCURVE_25519CTX) {
3211 : 0 : OSSL_PARAM_BLD_push_octet_string(iparam_bld, "context-string",
3212 : 0 : op->eddsa.context.data, op->eddsa.context.length);
3213 : :
3214 : : }
3215 : :
3216 : 0 : OSSL_PARAM_BLD_push_utf8_string(iparam_bld, "instance",
3217 : 0 : instance[op->eddsa.instance], strlen(instance[op->eddsa.instance]));
3218 : :
3219 : 0 : iparams = OSSL_PARAM_BLD_to_param(iparam_bld);
3220 [ # # ]: 0 : if (!iparams)
3221 : 0 : goto err_eddsa;
3222 : :
3223 [ # # # ]: 0 : switch (op->eddsa.op_type) {
3224 : 0 : case RTE_CRYPTO_ASYM_OP_SIGN:
3225 : : {
3226 [ # # ]: 0 : if (curve_id == RTE_CRYPTO_EC_GROUP_ED25519)
3227 : 0 : kctx = EVP_PKEY_CTX_new_from_name(NULL, "ED25519", NULL);
3228 : : else
3229 : 0 : kctx = EVP_PKEY_CTX_new_from_name(NULL, "ED448", NULL);
3230 : :
3231 [ # # # # : 0 : if (kctx == NULL || EVP_PKEY_fromdata_init(kctx) <= 0 ||
# # ]
3232 : 0 : EVP_PKEY_fromdata(kctx, &pkey, EVP_PKEY_KEYPAIR, params) <= 0)
3233 : 0 : goto err_eddsa;
3234 : :
3235 : 0 : md_ctx = EVP_MD_CTX_new();
3236 [ # # ]: 0 : if (!md_ctx)
3237 : 0 : goto err_eddsa;
3238 : :
3239 : 0 : sctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
3240 [ # # ]: 0 : if (!sctx)
3241 : 0 : goto err_eddsa;
3242 : :
3243 : 0 : EVP_MD_CTX_set_pkey_ctx(md_ctx, sctx);
3244 : :
3245 : : #if (OPENSSL_VERSION_NUMBER >= 0x30300000L)
3246 : : if (!EVP_DigestSignInit_ex(md_ctx, NULL, NULL, NULL, NULL, pkey, iparams))
3247 : : goto err_eddsa;
3248 : : #else
3249 [ # # ]: 0 : if (op->eddsa.instance == RTE_CRYPTO_EDCURVE_25519 ||
3250 : : op->eddsa.instance == RTE_CRYPTO_EDCURVE_448) {
3251 [ # # ]: 0 : if (!EVP_DigestSignInit(md_ctx, NULL, NULL, NULL, pkey))
3252 : 0 : goto err_eddsa;
3253 : : } else
3254 : 0 : goto err_eddsa;
3255 : : #endif
3256 : :
3257 [ # # ]: 0 : if (!EVP_DigestSign(md_ctx, NULL, &signlen, op->eddsa.message.data,
3258 : : op->eddsa.message.length))
3259 : 0 : goto err_eddsa;
3260 : :
3261 [ # # ]: 0 : if (signlen > RTE_DIM(signbuf))
3262 : 0 : goto err_eddsa;
3263 : :
3264 [ # # ]: 0 : if (!EVP_DigestSign(md_ctx, signbuf, &signlen, op->eddsa.message.data,
3265 : : op->eddsa.message.length))
3266 : 0 : goto err_eddsa;
3267 : :
3268 : 0 : memcpy(op->eddsa.sign.data, &signbuf[0], signlen);
3269 : 0 : op->eddsa.sign.length = signlen;
3270 : : }
3271 : 0 : break;
3272 : 0 : case RTE_CRYPTO_ASYM_OP_VERIFY:
3273 : : {
3274 [ # # ]: 0 : if (curve_id == RTE_CRYPTO_EC_GROUP_ED25519)
3275 : 0 : kctx = EVP_PKEY_CTX_new_from_name(NULL, "ED25519", NULL);
3276 : : else
3277 : 0 : kctx = EVP_PKEY_CTX_new_from_name(NULL, "ED448", NULL);
3278 : :
3279 [ # # # # : 0 : if (kctx == NULL || EVP_PKEY_fromdata_init(kctx) <= 0 ||
# # ]
3280 : 0 : EVP_PKEY_fromdata(kctx, &pkey, EVP_PKEY_PUBLIC_KEY, params) <= 0)
3281 : 0 : goto err_eddsa;
3282 : :
3283 : 0 : md_ctx = EVP_MD_CTX_new();
3284 [ # # ]: 0 : if (!md_ctx)
3285 : 0 : goto err_eddsa;
3286 : :
3287 : 0 : sctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
3288 [ # # ]: 0 : if (!sctx)
3289 : 0 : goto err_eddsa;
3290 : :
3291 : 0 : EVP_MD_CTX_set_pkey_ctx(md_ctx, sctx);
3292 : :
3293 : : #if (OPENSSL_VERSION_NUMBER >= 0x30300000L)
3294 : : if (!EVP_DigestVerifyInit_ex(md_ctx, NULL, NULL, NULL, NULL, pkey, iparams))
3295 : : goto err_eddsa;
3296 : : #else
3297 [ # # ]: 0 : if (op->eddsa.instance == RTE_CRYPTO_EDCURVE_25519 ||
3298 : : op->eddsa.instance == RTE_CRYPTO_EDCURVE_448) {
3299 [ # # ]: 0 : if (!EVP_DigestVerifyInit(md_ctx, NULL, NULL, NULL, pkey))
3300 : 0 : goto err_eddsa;
3301 : : } else
3302 : 0 : goto err_eddsa;
3303 : : #endif
3304 : :
3305 : 0 : signlen = op->eddsa.sign.length;
3306 : 0 : memcpy(&signbuf[0], op->eddsa.sign.data, op->eddsa.sign.length);
3307 : :
3308 : 0 : ret = EVP_DigestVerify(md_ctx, signbuf, signlen, op->eddsa.message.data,
3309 : : op->eddsa.message.length);
3310 [ # # ]: 0 : if (ret == 0)
3311 : 0 : goto err_eddsa;
3312 : : }
3313 : : break;
3314 : 0 : default:
3315 : : /* allow ops with invalid args to be pushed to
3316 : : * completion queue
3317 : : */
3318 : 0 : cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
3319 : 0 : goto err_eddsa;
3320 : : }
3321 : :
3322 : : ret = 0;
3323 : 0 : cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
3324 : 0 : err_eddsa:
3325 : 0 : OSSL_PARAM_BLD_free(iparam_bld);
3326 : :
3327 : 0 : EVP_PKEY_CTX_free(sctx);
3328 : :
3329 : 0 : EVP_PKEY_CTX_free(cctx);
3330 : :
3331 : 0 : EVP_PKEY_free(pkey);
3332 : :
3333 : 0 : return ret;
3334 : : }
3335 : :
3336 : : #else
3337 : : static int
3338 : : process_openssl_rsa_op(struct rte_crypto_op *cop,
3339 : : struct openssl_asym_session *sess)
3340 : : {
3341 : : int ret = 0;
3342 : : struct rte_crypto_asym_op *op = cop->asym;
3343 : : RSA *rsa = sess->u.r.rsa;
3344 : : uint32_t pad = sess->u.r.pad;
3345 : : uint8_t *tmp;
3346 : :
3347 : : cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
3348 : :
3349 : : switch (pad) {
3350 : : case RTE_CRYPTO_RSA_PADDING_PKCS1_5:
3351 : : pad = RSA_PKCS1_PADDING;
3352 : : break;
3353 : : case RTE_CRYPTO_RSA_PADDING_NONE:
3354 : : pad = RSA_NO_PADDING;
3355 : : break;
3356 : : default:
3357 : : cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
3358 : : OPENSSL_LOG(ERR,
3359 : : "rsa pad type not supported %d", pad);
3360 : : return 0;
3361 : : }
3362 : :
3363 : : switch (op->rsa.op_type) {
3364 : : case RTE_CRYPTO_ASYM_OP_ENCRYPT:
3365 : : ret = RSA_public_encrypt(op->rsa.message.length,
3366 : : op->rsa.message.data,
3367 : : op->rsa.cipher.data,
3368 : : rsa,
3369 : : pad);
3370 : :
3371 : : if (ret > 0)
3372 : : op->rsa.cipher.length = ret;
3373 : : OPENSSL_LOG(DEBUG,
3374 : : "length of encrypted text %d", ret);
3375 : : break;
3376 : :
3377 : : case RTE_CRYPTO_ASYM_OP_DECRYPT:
3378 : : ret = RSA_private_decrypt(op->rsa.cipher.length,
3379 : : op->rsa.cipher.data,
3380 : : op->rsa.message.data,
3381 : : rsa,
3382 : : pad);
3383 : : if (ret > 0)
3384 : : op->rsa.message.length = ret;
3385 : : break;
3386 : :
3387 : : case RTE_CRYPTO_ASYM_OP_SIGN:
3388 : : ret = RSA_private_encrypt(op->rsa.message.length,
3389 : : op->rsa.message.data,
3390 : : op->rsa.sign.data,
3391 : : rsa,
3392 : : pad);
3393 : : if (ret > 0)
3394 : : op->rsa.sign.length = ret;
3395 : : break;
3396 : :
3397 : : case RTE_CRYPTO_ASYM_OP_VERIFY:
3398 : : tmp = rte_malloc(NULL, op->rsa.sign.length, 0);
3399 : : if (tmp == NULL) {
3400 : : OPENSSL_LOG(ERR, "Memory allocation failed");
3401 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3402 : : break;
3403 : : }
3404 : : ret = RSA_public_decrypt(op->rsa.sign.length,
3405 : : op->rsa.sign.data,
3406 : : tmp,
3407 : : rsa,
3408 : : pad);
3409 : :
3410 : : OPENSSL_LOG(DEBUG,
3411 : : "Length of public_decrypt %d "
3412 : : "length of message %zd",
3413 : : ret, op->rsa.message.length);
3414 : : if ((ret <= 0) || (CRYPTO_memcmp(tmp, op->rsa.message.data,
3415 : : op->rsa.message.length))) {
3416 : : OPENSSL_LOG(ERR, "RSA sign Verification failed");
3417 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3418 : : }
3419 : : rte_free(tmp);
3420 : : break;
3421 : :
3422 : : default:
3423 : : /* allow ops with invalid args to be pushed to
3424 : : * completion queue
3425 : : */
3426 : : cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
3427 : : break;
3428 : : }
3429 : :
3430 : : if (ret < 0)
3431 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3432 : :
3433 : : return 0;
3434 : : }
3435 : :
3436 : : static int
3437 : : process_openssl_ecfpm_op(struct rte_crypto_op *cop,
3438 : : struct openssl_asym_session *sess)
3439 : : {
3440 : : RTE_SET_USED(cop);
3441 : : RTE_SET_USED(sess);
3442 : : return -ENOTSUP;
3443 : : }
3444 : :
3445 : : static int
3446 : : process_openssl_sm2_op(struct rte_crypto_op *cop,
3447 : : struct openssl_asym_session *sess)
3448 : : {
3449 : : RTE_SET_USED(cop);
3450 : : RTE_SET_USED(sess);
3451 : : return -ENOTSUP;
3452 : : }
3453 : :
3454 : : static int
3455 : : process_openssl_eddsa_op(struct rte_crypto_op *cop,
3456 : : struct openssl_asym_session *sess)
3457 : : {
3458 : : RTE_SET_USED(cop);
3459 : : RTE_SET_USED(sess);
3460 : : return -ENOTSUP;
3461 : : }
3462 : : #endif
3463 : :
3464 : : #if (OPENSSL_VERSION_NUMBER >= 0x30500000L)
3465 : : static int
3466 : : mlkem_keygen_op_evp(struct rte_crypto_op *cop,
3467 : : struct openssl_asym_session *sess)
3468 : : {
3469 : : struct rte_crypto_ml_kem_op *op = &cop->asym->mlkem;
3470 : : EVP_PKEY_CTX *pctx = sess->u.ml_kem.pctx;
3471 : : OSSL_PARAM_BLD *param_bld;
3472 : : uint8_t seed[64] = {0};
3473 : : EVP_PKEY *pkey = NULL;
3474 : : OSSL_PARAM *params;
3475 : : const char *param;
3476 : : size_t keylen = 0;
3477 : : void *key = NULL;
3478 : : OSSL_PARAM *p;
3479 : :
3480 : : param = ml_kem_type_names[sess->u.ml_kem.type];
3481 : : if (param == NULL) {
3482 : : OPENSSL_LOG(ERR, "invalid ml_kem type");
3483 : : return -EINVAL;
3484 : : }
3485 : :
3486 : : memcpy(seed, op->keygen.d.data, op->keygen.d.length);
3487 : : memcpy(seed + op->keygen.d.length,
3488 : : op->keygen.z.data, op->keygen.z.length);
3489 : :
3490 : : param_bld = OSSL_PARAM_BLD_new();
3491 : : if (param_bld == NULL) {
3492 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3493 : : return -1;
3494 : : }
3495 : :
3496 : : if (!OSSL_PARAM_BLD_push_utf8_string(param_bld,
3497 : : OSSL_PKEY_PARAM_GROUP_NAME,
3498 : : param, strlen(param)) ||
3499 : : !OSSL_PARAM_BLD_push_octet_string(param_bld,
3500 : : OSSL_PKEY_PARAM_ML_KEM_SEED,
3501 : : seed, sizeof(seed))) {
3502 : : OSSL_PARAM_BLD_free(param_bld);
3503 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3504 : : return -1;
3505 : : }
3506 : :
3507 : : params = OSSL_PARAM_BLD_to_param(param_bld);
3508 : : OSSL_PARAM_BLD_free(param_bld);
3509 : : if (params == NULL) {
3510 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3511 : : return -1;
3512 : : }
3513 : : if (EVP_PKEY_fromdata_init(pctx) != 1) {
3514 : : OSSL_PARAM_free(params);
3515 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3516 : : return -1;
3517 : : }
3518 : : if (EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_KEYPAIR, params) != 1) {
3519 : : OSSL_PARAM_free(params);
3520 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3521 : : return -1;
3522 : : }
3523 : : OSSL_PARAM_free(params);
3524 : :
3525 : : if (pkey == NULL) {
3526 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3527 : : return -1;
3528 : : }
3529 : :
3530 : : /* extract public and private keys */
3531 : : if (EVP_PKEY_todata(pkey, EVP_PKEY_KEYPAIR, ¶ms) != 1) {
3532 : : OPENSSL_LOG(ERR, "Failed to convert to key pairs");
3533 : : EVP_PKEY_free(pkey);
3534 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3535 : : return -1;
3536 : : }
3537 : :
3538 : : p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_PRIV_KEY);
3539 : : if (p == NULL) {
3540 : : OPENSSL_LOG(ERR, "Failed to locate private key");
3541 : : OSSL_PARAM_free(params);
3542 : : EVP_PKEY_free(pkey);
3543 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3544 : : return -1;
3545 : : }
3546 : :
3547 : : OSSL_PARAM_get_octet_string(p, &key,
3548 : : rte_crypto_ml_kem_pubkey_size[sess->u.ml_kem.type], &keylen);
3549 : : memcpy(op->keygen.dk.data, key, keylen);
3550 : : op->keygen.dk.length = keylen;
3551 : :
3552 : : p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_PUB_KEY);
3553 : : if (p == NULL) {
3554 : : OPENSSL_LOG(ERR, "Failed to locate public key");
3555 : : OSSL_PARAM_free(params);
3556 : : EVP_PKEY_free(pkey);
3557 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3558 : : return -1;
3559 : : }
3560 : :
3561 : : OSSL_PARAM_get_octet_string(p, &key,
3562 : : rte_crypto_ml_kem_privkey_size[sess->u.ml_kem.type], &keylen);
3563 : : memcpy(op->keygen.ek.data, key, keylen);
3564 : : op->keygen.ek.length = keylen;
3565 : :
3566 : : OSSL_PARAM_free(params);
3567 : : EVP_PKEY_free(pkey);
3568 : : cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
3569 : : return 0;
3570 : : }
3571 : :
3572 : : static int
3573 : : mlkem_encap_op_evp(struct rte_crypto_op *cop,
3574 : : struct openssl_asym_session *sess)
3575 : : {
3576 : : struct rte_crypto_ml_kem_op *op = &cop->asym->mlkem;
3577 : : EVP_PKEY_CTX *pctx = sess->u.ml_kem.pctx;
3578 : : OSSL_PARAM_BLD *param_bld;
3579 : : EVP_PKEY_CTX *cctx = NULL;
3580 : : EVP_PKEY *pkey = NULL;
3581 : : OSSL_PARAM *params;
3582 : : const char *param;
3583 : : size_t keylen = 0;
3584 : : size_t outlen = 0;
3585 : : int ret = -1;
3586 : :
3587 : : param = ml_kem_type_names[sess->u.ml_kem.type];
3588 : : if (param == NULL) {
3589 : : OPENSSL_LOG(ERR, "invalid ml_kem type");
3590 : : return -EINVAL;
3591 : : }
3592 : :
3593 : : param_bld = OSSL_PARAM_BLD_new();
3594 : : if (param_bld == NULL) {
3595 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3596 : : return -1;
3597 : : }
3598 : :
3599 : : if (!OSSL_PARAM_BLD_push_utf8_string(param_bld,
3600 : : OSSL_PKEY_PARAM_GROUP_NAME,
3601 : : param, strlen(param)) ||
3602 : : !OSSL_PARAM_BLD_push_octet_string(param_bld,
3603 : : OSSL_PKEY_PARAM_PUB_KEY,
3604 : : op->encap.ek.data, op->encap.ek.length)) {
3605 : : OSSL_PARAM_BLD_free(param_bld);
3606 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3607 : : return -1;
3608 : : }
3609 : :
3610 : : params = OSSL_PARAM_BLD_to_param(param_bld);
3611 : : OSSL_PARAM_BLD_free(param_bld);
3612 : : if (params == NULL) {
3613 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3614 : : return -1;
3615 : : }
3616 : :
3617 : : if (EVP_PKEY_fromdata_init(pctx) != 1) {
3618 : : OSSL_PARAM_free(params);
3619 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3620 : : return -1;
3621 : : }
3622 : :
3623 : : if (EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_PUBLIC_KEY, params) != 1) {
3624 : : OSSL_PARAM_free(params);
3625 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3626 : : return -1;
3627 : : }
3628 : : OSSL_PARAM_free(params);
3629 : :
3630 : : if (pkey == NULL) {
3631 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3632 : : return -1;
3633 : : }
3634 : :
3635 : : cctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
3636 : : if (cctx == NULL) {
3637 : : EVP_PKEY_free(pkey);
3638 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3639 : : return -1;
3640 : : }
3641 : :
3642 : : if (EVP_PKEY_encapsulate_init(cctx, NULL) != 1) {
3643 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3644 : : return -1;
3645 : : }
3646 : :
3647 : : if (op->encap.message.length) {
3648 : : const OSSL_PARAM kem_params[] = {
3649 : : OSSL_PARAM_octet_string(OSSL_KEM_PARAM_IKME,
3650 : : (void *)op->encap.message.data, op->encap.message.length),
3651 : : OSSL_PARAM_END
3652 : : };
3653 : :
3654 : : if (EVP_PKEY_encapsulate_init(cctx, kem_params) != 1) {
3655 : : EVP_PKEY_free(pkey);
3656 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3657 : : return -1;
3658 : : }
3659 : : }
3660 : :
3661 : : if (EVP_PKEY_encapsulate(cctx, NULL, &outlen, NULL, &keylen) != 1) {
3662 : : OPENSSL_LOG(ERR, "Failed to determine output length");
3663 : : EVP_PKEY_free(pkey);
3664 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3665 : : return -1;
3666 : : }
3667 : :
3668 : : if (outlen > op->encap.cipher.length) {
3669 : : OPENSSL_LOG(ERR, "Insufficient buffer for cipher text");
3670 : : EVP_PKEY_free(pkey);
3671 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3672 : : return -1;
3673 : : }
3674 : :
3675 : : if (keylen > op->encap.sk.length) {
3676 : : OPENSSL_LOG(ERR, "Insufficient buffer for shared key");
3677 : : EVP_PKEY_free(pkey);
3678 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3679 : : return -1;
3680 : : }
3681 : :
3682 : : if (EVP_PKEY_encapsulate(cctx, op->encap.cipher.data, &outlen,
3683 : : op->encap.sk.data, &keylen) != 1) {
3684 : : OPENSSL_LOG(ERR, "Failed to encapculate");
3685 : : EVP_PKEY_free(pkey);
3686 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3687 : : return -1;
3688 : : }
3689 : :
3690 : : op->encap.cipher.length = outlen;
3691 : : op->encap.sk.length = keylen;
3692 : :
3693 : : EVP_PKEY_CTX_free(cctx);
3694 : : EVP_PKEY_free(pkey);
3695 : : ret = 0;
3696 : : cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
3697 : : return ret;
3698 : : }
3699 : :
3700 : : static int
3701 : : mlkem_decap_op_evp(struct rte_crypto_op *cop,
3702 : : struct openssl_asym_session *sess)
3703 : : {
3704 : : struct rte_crypto_ml_kem_op *op = &cop->asym->mlkem;
3705 : : EVP_PKEY_CTX *pctx = sess->u.ml_kem.pctx;
3706 : : OSSL_PARAM_BLD *param_bld;
3707 : : EVP_PKEY_CTX *cctx = NULL;
3708 : : EVP_PKEY *pkey = NULL;
3709 : : OSSL_PARAM *params;
3710 : : const char *param;
3711 : : size_t keylen = 0;
3712 : : int ret = -1;
3713 : :
3714 : : param = ml_kem_type_names[sess->u.ml_kem.type];
3715 : : if (param == NULL) {
3716 : : OPENSSL_LOG(ERR, "invalid ml_kem type");
3717 : : return -EINVAL;
3718 : : }
3719 : :
3720 : : param_bld = OSSL_PARAM_BLD_new();
3721 : : if (param_bld == NULL) {
3722 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3723 : : return -1;
3724 : : }
3725 : :
3726 : : if (!OSSL_PARAM_BLD_push_utf8_string(param_bld,
3727 : : OSSL_PKEY_PARAM_GROUP_NAME,
3728 : : param, strlen(param)) ||
3729 : : !OSSL_PARAM_BLD_push_octet_string(param_bld,
3730 : : OSSL_PKEY_PARAM_PRIV_KEY,
3731 : : op->decap.dk.data, op->decap.dk.length)) {
3732 : : OSSL_PARAM_BLD_free(param_bld);
3733 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3734 : : return -1;
3735 : : }
3736 : :
3737 : : params = OSSL_PARAM_BLD_to_param(param_bld);
3738 : : OSSL_PARAM_BLD_free(param_bld);
3739 : : if (params == NULL) {
3740 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3741 : : return -1;
3742 : : }
3743 : :
3744 : : if (EVP_PKEY_fromdata_init(pctx) != 1) {
3745 : : OSSL_PARAM_free(params);
3746 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3747 : : return -1;
3748 : : }
3749 : :
3750 : : if (EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_PRIVATE_KEY, params) != 1) {
3751 : : OSSL_PARAM_free(params);
3752 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3753 : : return -1;
3754 : : }
3755 : : OSSL_PARAM_free(params);
3756 : :
3757 : : if (pkey == NULL) {
3758 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3759 : : return -1;
3760 : : }
3761 : :
3762 : : cctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
3763 : : if (cctx == NULL) {
3764 : : EVP_PKEY_free(pkey);
3765 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3766 : : return -1;
3767 : : }
3768 : :
3769 : : if (EVP_PKEY_decapsulate_init(cctx, params) != 1) {
3770 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3771 : : return -1;
3772 : : }
3773 : :
3774 : : if (EVP_PKEY_decapsulate(cctx, NULL, &keylen,
3775 : : op->decap.cipher.data, op->decap.cipher.length) != 1) {
3776 : : OPENSSL_LOG(ERR, "Failed to determine output length");
3777 : : EVP_PKEY_free(pkey);
3778 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3779 : : return -1;
3780 : : }
3781 : :
3782 : : if (keylen > op->decap.sk.length) {
3783 : : OPENSSL_LOG(ERR, "Insufficient buffer for shared key");
3784 : : EVP_PKEY_free(pkey);
3785 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3786 : : return -1;
3787 : : }
3788 : :
3789 : : if (EVP_PKEY_decapsulate(cctx, op->decap.sk.data, &keylen,
3790 : : op->decap.cipher.data, op->decap.cipher.length) != 1) {
3791 : : OPENSSL_LOG(ERR, "Failed to decapsulate");
3792 : : EVP_PKEY_free(pkey);
3793 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3794 : : return -1;
3795 : : }
3796 : :
3797 : : op->decap.sk.length = keylen;
3798 : :
3799 : : EVP_PKEY_CTX_free(cctx);
3800 : : EVP_PKEY_free(pkey);
3801 : : ret = 0;
3802 : : cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
3803 : : return ret;
3804 : : }
3805 : :
3806 : : static int
3807 : : process_openssl_mlkem_op_evp(struct rte_crypto_op *cop,
3808 : : struct openssl_asym_session *sess)
3809 : : {
3810 : : struct rte_crypto_ml_kem_op *op = &cop->asym->mlkem;
3811 : : int ret = -1;
3812 : :
3813 : : switch (op->op) {
3814 : : case RTE_CRYPTO_ML_KEM_OP_KEYGEN:
3815 : : ret = mlkem_keygen_op_evp(cop, sess);
3816 : : break;
3817 : : case RTE_CRYPTO_ML_KEM_OP_KEYVER:
3818 : : cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
3819 : : break;
3820 : : case RTE_CRYPTO_ML_KEM_OP_ENCAP:
3821 : : ret = mlkem_encap_op_evp(cop, sess);
3822 : : break;
3823 : : case RTE_CRYPTO_ML_KEM_OP_DECAP:
3824 : : ret = mlkem_decap_op_evp(cop, sess);
3825 : : break;
3826 : : default:
3827 : : cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
3828 : : break;
3829 : : }
3830 : :
3831 : : return ret;
3832 : : }
3833 : :
3834 : : static int
3835 : : mldsa_keygen_op_evp(struct rte_crypto_op *cop,
3836 : : struct openssl_asym_session *sess)
3837 : : {
3838 : : struct rte_crypto_ml_dsa_op *op = &cop->asym->mldsa;
3839 : : EVP_PKEY_CTX *pctx = sess->u.ml_dsa.pctx;
3840 : : OSSL_PARAM_BLD *param_bld;
3841 : : EVP_PKEY *pkey = NULL;
3842 : : OSSL_PARAM *params;
3843 : : const char *param;
3844 : : size_t keylen = 0;
3845 : : void *key = NULL;
3846 : : OSSL_PARAM *p;
3847 : :
3848 : : param = ml_dsa_type_names[sess->u.ml_dsa.type];
3849 : : if (param == NULL) {
3850 : : OPENSSL_LOG(ERR, "invalid ml_dsa type");
3851 : : return -EINVAL;
3852 : : }
3853 : :
3854 : : param_bld = OSSL_PARAM_BLD_new();
3855 : : if (param_bld == NULL) {
3856 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3857 : : return -1;
3858 : : }
3859 : :
3860 : : if (!OSSL_PARAM_BLD_push_utf8_string(param_bld,
3861 : : OSSL_PKEY_PARAM_GROUP_NAME,
3862 : : param, strlen(param)) ||
3863 : : !OSSL_PARAM_BLD_push_octet_string(param_bld,
3864 : : OSSL_PKEY_PARAM_ML_DSA_SEED,
3865 : : op->keygen.seed.data, op->keygen.seed.length)) {
3866 : : OSSL_PARAM_BLD_free(param_bld);
3867 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3868 : : return -1;
3869 : : }
3870 : :
3871 : : params = OSSL_PARAM_BLD_to_param(param_bld);
3872 : : OSSL_PARAM_BLD_free(param_bld);
3873 : : if (params == NULL) {
3874 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3875 : : return -1;
3876 : : }
3877 : : if (EVP_PKEY_fromdata_init(pctx) != 1) {
3878 : : OSSL_PARAM_free(params);
3879 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3880 : : return -1;
3881 : : }
3882 : : if (EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_KEYPAIR, params) != 1) {
3883 : : OSSL_PARAM_free(params);
3884 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3885 : : return -1;
3886 : : }
3887 : : OSSL_PARAM_free(params);
3888 : :
3889 : : if (pkey == NULL) {
3890 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3891 : : return -1;
3892 : : }
3893 : :
3894 : : /* extract public and private keys */
3895 : : if (EVP_PKEY_todata(pkey, EVP_PKEY_KEYPAIR, ¶ms) != 1) {
3896 : : OPENSSL_LOG(ERR, "Failed to convert to key pairs");
3897 : : EVP_PKEY_free(pkey);
3898 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3899 : : return -1;
3900 : : }
3901 : :
3902 : : p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_PRIV_KEY);
3903 : : if (p == NULL) {
3904 : : OPENSSL_LOG(ERR, "Failed to locate private key");
3905 : : OSSL_PARAM_free(params);
3906 : : EVP_PKEY_free(pkey);
3907 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3908 : : return -1;
3909 : : }
3910 : :
3911 : : OSSL_PARAM_get_octet_string(p, &key,
3912 : : rte_crypto_ml_dsa_privkey_size[sess->u.ml_dsa.type], &keylen);
3913 : : memcpy(op->keygen.privkey.data, key, keylen);
3914 : : op->keygen.privkey.length = keylen;
3915 : :
3916 : : p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_PUB_KEY);
3917 : : if (p == NULL) {
3918 : : OPENSSL_LOG(ERR, "Failed to locate public key");
3919 : : OSSL_PARAM_free(params);
3920 : : EVP_PKEY_free(pkey);
3921 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3922 : : return -1;
3923 : : }
3924 : :
3925 : : OSSL_PARAM_get_octet_string(p, &key,
3926 : : rte_crypto_ml_dsa_pubkey_size[sess->u.ml_dsa.type], &keylen);
3927 : : memcpy(op->keygen.pubkey.data, key, keylen);
3928 : : op->keygen.pubkey.length = keylen;
3929 : :
3930 : : OSSL_PARAM_free(params);
3931 : : EVP_PKEY_free(pkey);
3932 : : cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
3933 : : return 0;
3934 : : }
3935 : :
3936 : : static int
3937 : : mldsa_sign_op_evp(struct rte_crypto_op *cop,
3938 : : struct openssl_asym_session *sess)
3939 : : {
3940 : : struct rte_crypto_ml_dsa_op *op = &cop->asym->mldsa;
3941 : : EVP_PKEY_CTX *pctx = sess->u.ml_dsa.pctx;
3942 : : const EVP_MD *check_md = NULL;
3943 : : OSSL_PARAM_BLD *param_bld;
3944 : : EVP_PKEY_CTX *cctx = NULL;
3945 : : EVP_PKEY *pkey = NULL;
3946 : : EVP_SIGNATURE *sigalg;
3947 : : OSSL_PARAM *params;
3948 : : const char *param;
3949 : : unsigned char *md;
3950 : : size_t siglen = 0;
3951 : : size_t mdlen = 0;
3952 : : int ret = -1;
3953 : :
3954 : : param = ml_dsa_type_names[sess->u.ml_dsa.type];
3955 : : if (param == NULL) {
3956 : : OPENSSL_LOG(ERR, "invalid ml_dsa type");
3957 : : return -EINVAL;
3958 : : }
3959 : :
3960 : : param_bld = OSSL_PARAM_BLD_new();
3961 : : if (param_bld == NULL) {
3962 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3963 : : return -1;
3964 : : }
3965 : :
3966 : : if (!OSSL_PARAM_BLD_push_utf8_string(param_bld,
3967 : : OSSL_PKEY_PARAM_GROUP_NAME,
3968 : : param, strlen(param)) ||
3969 : : (op->siggen.seed.length &&
3970 : : !OSSL_PARAM_BLD_push_octet_string(param_bld,
3971 : : OSSL_PKEY_PARAM_ML_DSA_SEED,
3972 : : op->siggen.seed.data, op->siggen.seed.length)) ||
3973 : : !OSSL_PARAM_BLD_push_octet_string(param_bld,
3974 : : OSSL_PKEY_PARAM_PRIV_KEY,
3975 : : op->siggen.privkey.data, op->siggen.privkey.length)) {
3976 : : OSSL_PARAM_BLD_free(param_bld);
3977 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3978 : : return -1;
3979 : : }
3980 : :
3981 : : params = OSSL_PARAM_BLD_to_param(param_bld);
3982 : : OSSL_PARAM_BLD_free(param_bld);
3983 : : if (params == NULL) {
3984 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3985 : : return -1;
3986 : : }
3987 : :
3988 : : if (EVP_PKEY_fromdata_init(pctx) != 1) {
3989 : : OSSL_PARAM_free(params);
3990 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3991 : : return -1;
3992 : : }
3993 : :
3994 : : if (EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_PRIVATE_KEY, params) != 1) {
3995 : : OSSL_PARAM_free(params);
3996 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
3997 : : return -1;
3998 : : }
3999 : :
4000 : : if (pkey == NULL) {
4001 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
4002 : : return -1;
4003 : : }
4004 : :
4005 : : cctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
4006 : : if (cctx == NULL) {
4007 : : EVP_PKEY_free(pkey);
4008 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
4009 : : return -1;
4010 : : }
4011 : :
4012 : : sigalg = EVP_SIGNATURE_fetch(NULL, param, NULL);
4013 : : if (sigalg == NULL) {
4014 : : OPENSSL_LOG(ERR, "Failed to fetch signature algorithm");
4015 : : EVP_PKEY_free(pkey);
4016 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
4017 : : return -1;
4018 : : }
4019 : :
4020 : : if (EVP_PKEY_sign_message_init(cctx, sigalg, NULL) != 1) {
4021 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
4022 : : return -1;
4023 : : }
4024 : :
4025 : : if (sess->u.ml_dsa.sign_deterministic) {
4026 : : int deterministic = 1;
4027 : : const OSSL_PARAM sign_params[] = {
4028 : : OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_DETERMINISTIC, &deterministic),
4029 : : OSSL_PARAM_END
4030 : : };
4031 : :
4032 : : if (EVP_PKEY_sign_message_init(cctx, sigalg, sign_params) != 1) {
4033 : : EVP_SIGNATURE_free(sigalg);
4034 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
4035 : : return -1;
4036 : : }
4037 : : }
4038 : :
4039 : : md = op->siggen.message.data;
4040 : : mdlen = op->siggen.message.length;
4041 : :
4042 : : if (op->siggen.mu.length) {
4043 : : int has_mu = 1;
4044 : : const OSSL_PARAM sign_params[] = {
4045 : : OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_MU, &has_mu),
4046 : : OSSL_PARAM_END
4047 : : };
4048 : :
4049 : : if (EVP_PKEY_sign_message_init(cctx, sigalg, sign_params) != 1) {
4050 : : EVP_SIGNATURE_free(sigalg);
4051 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
4052 : : return -1;
4053 : : }
4054 : :
4055 : : md = op->siggen.mu.data;
4056 : : mdlen = op->siggen.mu.length;
4057 : : } else if (op->siggen.ctx.length) {
4058 : : int has_ctx = 1;
4059 : : const OSSL_PARAM sign_params[] = {
4060 : : OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_MESSAGE_ENCODING, &has_ctx),
4061 : : OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_CONTEXT_STRING,
4062 : : (void *)op->siggen.ctx.data, op->siggen.ctx.length),
4063 : : OSSL_PARAM_END
4064 : : };
4065 : :
4066 : : if (EVP_PKEY_sign_message_init(cctx, sigalg, sign_params) != 1) {
4067 : : EVP_SIGNATURE_free(sigalg);
4068 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
4069 : : return -1;
4070 : : }
4071 : : }
4072 : :
4073 : : EVP_SIGNATURE_free(sigalg);
4074 : :
4075 : : switch (op->siggen.hash) {
4076 : : case RTE_CRYPTO_AUTH_SHA3_224:
4077 : : check_md = EVP_sha3_224();
4078 : : break;
4079 : : case RTE_CRYPTO_AUTH_SHA3_256:
4080 : : check_md = EVP_sha3_256();
4081 : : break;
4082 : : case RTE_CRYPTO_AUTH_SHA3_384:
4083 : : check_md = EVP_sha3_384();
4084 : : break;
4085 : : case RTE_CRYPTO_AUTH_SHA3_512:
4086 : : check_md = EVP_sha3_512();
4087 : : break;
4088 : : #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
4089 : : case RTE_CRYPTO_AUTH_SHAKE_128:
4090 : : check_md = EVP_shake128();
4091 : : break;
4092 : : case RTE_CRYPTO_AUTH_SHAKE_256:
4093 : : check_md = EVP_shake256();
4094 : : break;
4095 : : #endif
4096 : : default:
4097 : : break;
4098 : : }
4099 : :
4100 : : if (op->siggen.hash != 0 && check_md == NULL) {
4101 : : OPENSSL_LOG(ERR, "invalid hash type");
4102 : : EVP_PKEY_free(pkey);
4103 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
4104 : : return -1;
4105 : : }
4106 : :
4107 : : if (check_md != NULL) {
4108 : : if (EVP_PKEY_CTX_set_signature_md(cctx, check_md) != 1) {
4109 : : OPENSSL_LOG(ERR, "Failed to set signature md");
4110 : : EVP_PKEY_free(pkey);
4111 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
4112 : : return -1;
4113 : : }
4114 : : }
4115 : :
4116 : : if (EVP_PKEY_sign(cctx, NULL, &siglen, md, mdlen) != 1) {
4117 : : OPENSSL_LOG(ERR, "Failed to determine output length");
4118 : : EVP_PKEY_free(pkey);
4119 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
4120 : : return -1;
4121 : : }
4122 : :
4123 : : if (siglen > op->siggen.sign.length) {
4124 : : OPENSSL_LOG(ERR, "Insufficient buffer for signature");
4125 : : EVP_PKEY_free(pkey);
4126 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
4127 : : return -1;
4128 : : }
4129 : :
4130 : : if (EVP_PKEY_sign(cctx, op->siggen.sign.data, &siglen, md, mdlen) != 1) {
4131 : : OPENSSL_LOG(ERR, "Failed to sign");
4132 : : EVP_PKEY_free(pkey);
4133 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
4134 : : return -1;
4135 : : }
4136 : :
4137 : : op->siggen.sign.length = siglen;
4138 : :
4139 : : OSSL_PARAM_free(params);
4140 : : EVP_PKEY_CTX_free(cctx);
4141 : : EVP_PKEY_free(pkey);
4142 : : ret = 0;
4143 : : cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
4144 : : return ret;
4145 : : }
4146 : :
4147 : : static int
4148 : : mldsa_verify_op_evp(struct rte_crypto_op *cop,
4149 : : struct openssl_asym_session *sess)
4150 : : {
4151 : : struct rte_crypto_ml_dsa_op *op = &cop->asym->mldsa;
4152 : : EVP_PKEY_CTX *pctx = sess->u.ml_dsa.pctx;
4153 : : OSSL_PARAM_BLD *param_bld;
4154 : : EVP_PKEY_CTX *cctx = NULL;
4155 : : EVP_PKEY *pkey = NULL;
4156 : : EVP_SIGNATURE *sigalg;
4157 : : OSSL_PARAM *params;
4158 : : const char *param;
4159 : : unsigned char *md;
4160 : : size_t mdlen = 0;
4161 : : int ret = -1;
4162 : :
4163 : : param = ml_dsa_type_names[sess->u.ml_dsa.type];
4164 : : if (param == NULL) {
4165 : : OPENSSL_LOG(ERR, "invalid ml_dsa type");
4166 : : return -EINVAL;
4167 : : }
4168 : :
4169 : : param_bld = OSSL_PARAM_BLD_new();
4170 : : if (param_bld == NULL) {
4171 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
4172 : : return -1;
4173 : : }
4174 : :
4175 : : if (!OSSL_PARAM_BLD_push_utf8_string(param_bld,
4176 : : OSSL_PKEY_PARAM_GROUP_NAME,
4177 : : param, strlen(param)) ||
4178 : : !OSSL_PARAM_BLD_push_octet_string(param_bld,
4179 : : OSSL_PKEY_PARAM_PUB_KEY,
4180 : : op->sigver.pubkey.data, op->sigver.pubkey.length)) {
4181 : : OSSL_PARAM_BLD_free(param_bld);
4182 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
4183 : : return -1;
4184 : : }
4185 : :
4186 : : params = OSSL_PARAM_BLD_to_param(param_bld);
4187 : : OSSL_PARAM_BLD_free(param_bld);
4188 : : if (params == NULL) {
4189 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
4190 : : return -1;
4191 : : }
4192 : :
4193 : : if (EVP_PKEY_fromdata_init(pctx) != 1) {
4194 : : OSSL_PARAM_free(params);
4195 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
4196 : : return -1;
4197 : : }
4198 : :
4199 : : if (EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_PUBLIC_KEY, params) != 1) {
4200 : : OSSL_PARAM_free(params);
4201 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
4202 : : return -1;
4203 : : }
4204 : :
4205 : : if (pkey == NULL) {
4206 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
4207 : : return -1;
4208 : : }
4209 : :
4210 : : cctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
4211 : : if (cctx == NULL) {
4212 : : EVP_PKEY_free(pkey);
4213 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
4214 : : return -1;
4215 : : }
4216 : :
4217 : : sigalg = EVP_SIGNATURE_fetch(NULL, param, NULL);
4218 : : if (sigalg == NULL) {
4219 : : OPENSSL_LOG(ERR, "Failed to fetch signature algorithm");
4220 : : EVP_PKEY_free(pkey);
4221 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
4222 : : return -1;
4223 : : }
4224 : :
4225 : : if (EVP_PKEY_verify_message_init(cctx, sigalg, NULL) != 1) {
4226 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
4227 : : return -1;
4228 : : }
4229 : :
4230 : : md = op->sigver.message.data;
4231 : : mdlen = op->sigver.message.length;
4232 : :
4233 : : if (op->sigver.mu.length) {
4234 : : int has_mu = 1;
4235 : : const OSSL_PARAM sign_params[] = {
4236 : : OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_MU, &has_mu),
4237 : : OSSL_PARAM_END
4238 : : };
4239 : :
4240 : : if (EVP_PKEY_verify_message_init(cctx, sigalg, sign_params) != 1) {
4241 : : EVP_SIGNATURE_free(sigalg);
4242 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
4243 : : return -1;
4244 : : }
4245 : :
4246 : : md = op->sigver.mu.data;
4247 : : mdlen = op->sigver.mu.length;
4248 : : } else if (op->sigver.ctx.length) {
4249 : : const OSSL_PARAM sign_params[] = {
4250 : : OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_CONTEXT_STRING,
4251 : : (void *)op->sigver.ctx.data, op->sigver.ctx.length),
4252 : : OSSL_PARAM_END
4253 : : };
4254 : :
4255 : : if (EVP_PKEY_verify_message_init(cctx, sigalg, sign_params) != 1) {
4256 : : EVP_SIGNATURE_free(sigalg);
4257 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
4258 : : return -1;
4259 : : }
4260 : : }
4261 : :
4262 : : if (EVP_PKEY_verify(cctx, op->sigver.sign.data, op->sigver.sign.length,
4263 : : md, mdlen) != 1) {
4264 : : EVP_PKEY_free(pkey);
4265 : : cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
4266 : : return 0;
4267 : : }
4268 : :
4269 : : OSSL_PARAM_free(params);
4270 : : EVP_PKEY_CTX_free(cctx);
4271 : : EVP_PKEY_free(pkey);
4272 : : ret = 0;
4273 : : cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
4274 : : return ret;
4275 : : }
4276 : :
4277 : : static int
4278 : : process_openssl_mldsa_op_evp(struct rte_crypto_op *cop,
4279 : : struct openssl_asym_session *sess)
4280 : : {
4281 : : struct rte_crypto_ml_dsa_op *op = &cop->asym->mldsa;
4282 : : int ret = -1;
4283 : :
4284 : : switch (op->op) {
4285 : : case RTE_CRYPTO_ML_DSA_OP_KEYGEN:
4286 : : ret = mldsa_keygen_op_evp(cop, sess);
4287 : : break;
4288 : : case RTE_CRYPTO_ML_DSA_OP_SIGN:
4289 : : ret = mldsa_sign_op_evp(cop, sess);
4290 : : break;
4291 : : case RTE_CRYPTO_ML_DSA_OP_VERIFY:
4292 : : ret = mldsa_verify_op_evp(cop, sess);
4293 : : break;
4294 : : default:
4295 : : cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
4296 : : break;
4297 : : }
4298 : :
4299 : : return ret;
4300 : : }
4301 : : #else
4302 : : static int
4303 : : process_openssl_mlkem_op(struct rte_crypto_op *cop,
4304 : : struct openssl_asym_session *sess)
4305 : : {
4306 : : RTE_SET_USED(cop);
4307 : : RTE_SET_USED(sess);
4308 : : return -ENOTSUP;
4309 : : }
4310 : :
4311 : : static int
4312 : : process_openssl_mldsa_op(struct rte_crypto_op *cop,
4313 : : struct openssl_asym_session *sess)
4314 : : {
4315 : : RTE_SET_USED(cop);
4316 : : RTE_SET_USED(sess);
4317 : : return -ENOTSUP;
4318 : : }
4319 : : #endif
4320 : :
4321 : : static int
4322 : 40 : process_asym_op(struct openssl_qp *qp, struct rte_crypto_op *op,
4323 : : struct openssl_asym_session *sess)
4324 : : {
4325 : : int retval = 0;
4326 : :
4327 : 40 : op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
4328 : :
4329 [ + + + + : 40 : switch (sess->xfrm_type) {
+ - + - -
- ]
4330 : 14 : case RTE_CRYPTO_ASYM_XFORM_RSA:
4331 : : #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
4332 : 14 : retval = process_openssl_rsa_op_evp(op, sess);
4333 : : # else
4334 : : retval = process_openssl_rsa_op(op, sess);
4335 : : #endif
4336 : 14 : break;
4337 : 12 : case RTE_CRYPTO_ASYM_XFORM_MODEX:
4338 : 12 : retval = process_openssl_modexp_op(op, sess);
4339 : 12 : break;
4340 : 2 : case RTE_CRYPTO_ASYM_XFORM_MODINV:
4341 : 2 : retval = process_openssl_modinv_op(op, sess);
4342 : 2 : break;
4343 : 4 : case RTE_CRYPTO_ASYM_XFORM_DH:
4344 : : #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
4345 : 4 : retval = process_openssl_dh_op_evp(op, sess);
4346 : : # else
4347 : : retval = process_openssl_dh_op(op, sess);
4348 : : #endif
4349 : 4 : break;
4350 : 2 : case RTE_CRYPTO_ASYM_XFORM_DSA:
4351 : : #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
4352 [ + + ]: 2 : if (op->asym->dsa.op_type == RTE_CRYPTO_ASYM_OP_SIGN)
4353 : 1 : retval = process_openssl_dsa_sign_op_evp(op, sess);
4354 [ + - ]: 1 : else if (op->asym->dsa.op_type ==
4355 : : RTE_CRYPTO_ASYM_OP_VERIFY)
4356 : : retval =
4357 : 1 : process_openssl_dsa_verify_op_evp(op, sess);
4358 : : #else
4359 : : if (op->asym->dsa.op_type == RTE_CRYPTO_ASYM_OP_SIGN)
4360 : : retval = process_openssl_dsa_sign_op(op, sess);
4361 : : else if (op->asym->dsa.op_type ==
4362 : : RTE_CRYPTO_ASYM_OP_VERIFY)
4363 : : retval =
4364 : : process_openssl_dsa_verify_op(op, sess);
4365 : : else
4366 : : op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
4367 : : #endif
4368 : : break;
4369 : 0 : case RTE_CRYPTO_ASYM_XFORM_ECFPM:
4370 : : #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
4371 : 0 : retval = process_openssl_ecfpm_op_evp(op, sess);
4372 : : #else
4373 : : retval = process_openssl_ecfpm_op(op, sess);
4374 : : #endif
4375 : 0 : break;
4376 : 6 : case RTE_CRYPTO_ASYM_XFORM_SM2:
4377 : : #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
4378 : 6 : retval = process_openssl_sm2_op_evp(op, sess);
4379 : : #else
4380 : : retval = process_openssl_sm2_op(op, sess);
4381 : : #endif
4382 : 6 : break;
4383 : 0 : case RTE_CRYPTO_ASYM_XFORM_EDDSA:
4384 : : #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
4385 : 0 : retval = process_openssl_eddsa_op_evp(op, sess);
4386 : : #else
4387 : : retval = process_openssl_eddsa_op(op, sess);
4388 : : #endif
4389 : 0 : break;
4390 : : case RTE_CRYPTO_ASYM_XFORM_ML_KEM:
4391 : : #if (OPENSSL_VERSION_NUMBER >= 0x30500000L)
4392 : : retval = process_openssl_mlkem_op_evp(op, sess);
4393 : : #else
4394 : : retval = process_openssl_mlkem_op(op, sess);
4395 : : #endif
4396 : : break;
4397 : : case RTE_CRYPTO_ASYM_XFORM_ML_DSA:
4398 : : #if (OPENSSL_VERSION_NUMBER >= 0x30500000L)
4399 : : retval = process_openssl_mldsa_op_evp(op, sess);
4400 : : #else
4401 : : retval = process_openssl_mldsa_op(op, sess);
4402 : : #endif
4403 : : break;
4404 : 0 : default:
4405 : 0 : op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
4406 : : break;
4407 : : }
4408 [ + - ]: 40 : if (!retval) {
4409 : : /* op processed so push to completion queue as processed */
4410 [ - + - - : 80 : retval = rte_ring_enqueue(qp->processed_ops, (void *)op);
- ]
4411 : : if (retval)
4412 : : /* return error if failed to put in completion queue */
4413 : : retval = -1;
4414 : : }
4415 : :
4416 : 40 : return retval;
4417 : : }
4418 : :
4419 : : static void
4420 : 10 : copy_plaintext(struct rte_mbuf *m_src, struct rte_mbuf *m_dst,
4421 : : struct rte_crypto_op *op)
4422 : : {
4423 : : uint8_t *p_src, *p_dst;
4424 : :
4425 : 10 : p_src = rte_pktmbuf_mtod(m_src, uint8_t *);
4426 : 10 : p_dst = rte_pktmbuf_mtod(m_dst, uint8_t *);
4427 : :
4428 : : /**
4429 : : * Copy the content between cipher offset and auth offset
4430 : : * for generating correct digest.
4431 : : */
4432 [ + + ]: 10 : if (op->sym->cipher.data.offset > op->sym->auth.data.offset)
4433 : 2 : memcpy(p_dst + op->sym->auth.data.offset,
4434 : 2 : p_src + op->sym->auth.data.offset,
4435 : 2 : op->sym->cipher.data.offset -
4436 : : op->sym->auth.data.offset);
4437 : 10 : }
4438 : :
4439 : : /** Process crypto operation for mbuf */
4440 : : static int
4441 : 294 : process_op(struct openssl_qp *qp, struct rte_crypto_op *op,
4442 : : struct openssl_session *sess)
4443 : : {
4444 : : struct rte_mbuf *msrc, *mdst;
4445 : : int retval;
4446 : :
4447 : 294 : msrc = op->sym->m_src;
4448 [ + + ]: 294 : mdst = op->sym->m_dst ? op->sym->m_dst : op->sym->m_src;
4449 : :
4450 : 294 : op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
4451 : :
4452 [ + + + + : 294 : switch (sess->chain_order) {
+ + - ]
4453 : 45 : case OPENSSL_CHAIN_ONLY_CIPHER:
4454 : 45 : process_openssl_cipher_op(qp, op, sess, msrc, mdst);
4455 : 45 : break;
4456 : 56 : case OPENSSL_CHAIN_ONLY_AUTH:
4457 : 56 : process_openssl_auth_op(qp, op, sess, msrc, mdst);
4458 : 56 : break;
4459 : 36 : case OPENSSL_CHAIN_CIPHER_AUTH:
4460 : 36 : process_openssl_cipher_op(qp, op, sess, msrc, mdst);
4461 : : /* OOP */
4462 [ + + ]: 36 : if (msrc != mdst)
4463 : 10 : copy_plaintext(msrc, mdst, op);
4464 : 36 : process_openssl_auth_op(qp, op, sess, mdst, mdst);
4465 : 36 : break;
4466 : 40 : case OPENSSL_CHAIN_AUTH_CIPHER:
4467 : 40 : process_openssl_auth_op(qp, op, sess, msrc, mdst);
4468 : 40 : process_openssl_cipher_op(qp, op, sess, msrc, mdst);
4469 : 40 : break;
4470 : 99 : case OPENSSL_CHAIN_COMBINED:
4471 : 99 : process_openssl_combined_op(qp, op, sess, msrc, mdst);
4472 : 99 : break;
4473 : 18 : case OPENSSL_CHAIN_CIPHER_BPI:
4474 : 18 : process_openssl_docsis_bpi_op(op, sess, msrc, mdst);
4475 : 18 : break;
4476 : 0 : default:
4477 : 0 : op->status = RTE_CRYPTO_OP_STATUS_ERROR;
4478 : 0 : break;
4479 : : }
4480 : :
4481 : : /* Free session if a session-less crypto op */
4482 [ + + ]: 294 : if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
4483 : 10 : openssl_reset_session(sess);
4484 : : memset(sess, 0, sizeof(struct openssl_session));
4485 [ - + ]: 10 : rte_mempool_put(qp->sess_mp, op->sym->session);
4486 : 10 : op->sym->session = NULL;
4487 : : }
4488 : :
4489 [ + + ]: 294 : if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
4490 : 283 : op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
4491 : :
4492 [ + - ]: 294 : if (op->status != RTE_CRYPTO_OP_STATUS_ERROR)
4493 [ - + - - : 588 : retval = rte_ring_enqueue(qp->processed_ops, (void *)op);
- ]
4494 : : else
4495 : : retval = -1;
4496 : :
4497 : 294 : return retval;
4498 : : }
4499 : :
4500 : : /*
4501 : : *------------------------------------------------------------------------------
4502 : : * PMD Framework
4503 : : *------------------------------------------------------------------------------
4504 : : */
4505 : :
4506 : : /** Enqueue burst */
4507 : : static uint16_t
4508 : 334 : openssl_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
4509 : : uint16_t nb_ops)
4510 : : {
4511 : : void *sess;
4512 : : struct openssl_qp *qp = queue_pair;
4513 : : int i, retval;
4514 : :
4515 [ + + ]: 668 : for (i = 0; i < nb_ops; i++) {
4516 : 334 : sess = get_session(qp, ops[i]);
4517 [ - + ]: 334 : if (unlikely(sess == NULL))
4518 : 0 : goto enqueue_err;
4519 : :
4520 [ + + ]: 334 : if (ops[i]->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC)
4521 : 294 : retval = process_op(qp, ops[i],
4522 : : (struct openssl_session *) sess);
4523 : : else
4524 : 40 : retval = process_asym_op(qp, ops[i],
4525 : : (struct openssl_asym_session *) sess);
4526 [ - + ]: 334 : if (unlikely(retval < 0))
4527 : 0 : goto enqueue_err;
4528 : : }
4529 : :
4530 : 334 : qp->stats.enqueued_count += i;
4531 : 334 : return i;
4532 : :
4533 : 0 : enqueue_err:
4534 : 0 : qp->stats.enqueue_err_count++;
4535 : 0 : return i;
4536 : : }
4537 : :
4538 : : /** Dequeue burst */
4539 : : static uint16_t
4540 : 334 : openssl_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
4541 : : uint16_t nb_ops)
4542 : : {
4543 : : struct openssl_qp *qp = queue_pair;
4544 : :
4545 : : unsigned int nb_dequeued = 0;
4546 : :
4547 [ - + - - : 334 : nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
- ]
4548 : : (void **)ops, nb_ops, NULL);
4549 : 334 : qp->stats.dequeued_count += nb_dequeued;
4550 : :
4551 : 334 : return nb_dequeued;
4552 : : }
4553 : :
4554 : : /** Create OPENSSL crypto device */
4555 : : static int
4556 : 2 : cryptodev_openssl_create(const char *name,
4557 : : struct rte_vdev_device *vdev,
4558 : : struct rte_cryptodev_pmd_init_params *init_params)
4559 : : {
4560 : : struct rte_cryptodev *dev;
4561 : : struct openssl_private *internals;
4562 : :
4563 : 2 : dev = rte_cryptodev_pmd_create(name, &vdev->device, init_params);
4564 [ - + ]: 2 : if (dev == NULL) {
4565 : 0 : OPENSSL_LOG(ERR, "failed to create cryptodev vdev");
4566 : 0 : goto init_error;
4567 : : }
4568 : :
4569 : 2 : dev->driver_id = cryptodev_driver_id;
4570 : 2 : dev->dev_ops = rte_openssl_pmd_ops;
4571 : :
4572 : : /* register rx/tx burst functions for data path */
4573 : 2 : dev->dequeue_burst = openssl_pmd_dequeue_burst;
4574 : 2 : dev->enqueue_burst = openssl_pmd_enqueue_burst;
4575 : :
4576 : 2 : dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
4577 : : RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
4578 : : RTE_CRYPTODEV_FF_CPU_AESNI |
4579 : : RTE_CRYPTODEV_FF_IN_PLACE_SGL |
4580 : : RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
4581 : : RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT |
4582 : : RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO |
4583 : : RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_EXP |
4584 : : RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT |
4585 : : RTE_CRYPTODEV_FF_SYM_SESSIONLESS;
4586 : :
4587 : 2 : internals = dev->data->dev_private;
4588 : :
4589 : 2 : internals->max_nb_qpairs = init_params->max_nb_queue_pairs;
4590 : :
4591 : 2 : rte_cryptodev_pmd_probing_finish(dev);
4592 : :
4593 : : # if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
4594 : : /* Load legacy provider
4595 : : * Some algorithms are no longer available in earlier version of openssl,
4596 : : * unless the legacy provider explicitly loaded. e.g. DES
4597 : : */
4598 : 2 : ossl_legacy_provider_load();
4599 : : # endif
4600 : 2 : return 0;
4601 : :
4602 : : init_error:
4603 : 0 : OPENSSL_LOG(ERR, "driver %s: create failed",
4604 : : init_params->name);
4605 : :
4606 : 0 : cryptodev_openssl_remove(vdev);
4607 : 0 : return -EFAULT;
4608 : : }
4609 : :
4610 : : /** Initialise OPENSSL crypto device */
4611 : : static int
4612 : 2 : cryptodev_openssl_probe(struct rte_vdev_device *vdev)
4613 : : {
4614 : 4 : struct rte_cryptodev_pmd_init_params init_params = {
4615 : : "",
4616 : : sizeof(struct openssl_private),
4617 [ + - ]: 2 : rte_socket_id(),
4618 : : RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS
4619 : : };
4620 : : const char *name;
4621 : : const char *input_args;
4622 : :
4623 : : name = rte_vdev_device_name(vdev);
4624 : : if (name == NULL)
4625 : : return -EINVAL;
4626 : : input_args = rte_vdev_device_args(vdev);
4627 : :
4628 : 2 : rte_cryptodev_pmd_parse_input_args(&init_params, input_args);
4629 : :
4630 : 2 : return cryptodev_openssl_create(name, vdev, &init_params);
4631 : : }
4632 : :
4633 : : /** Uninitialise OPENSSL crypto device */
4634 : : static int
4635 [ + - ]: 2 : cryptodev_openssl_remove(struct rte_vdev_device *vdev)
4636 : : {
4637 : : struct rte_cryptodev *cryptodev;
4638 : : const char *name;
4639 : :
4640 : : name = rte_vdev_device_name(vdev);
4641 : : if (name == NULL)
4642 : : return -EINVAL;
4643 : :
4644 : 2 : cryptodev = rte_cryptodev_pmd_get_named_dev(name);
4645 [ + - ]: 2 : if (cryptodev == NULL)
4646 : : return -ENODEV;
4647 : :
4648 : : # if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
4649 : 2 : ossl_legacy_provider_unload();
4650 : : # endif
4651 : 2 : return rte_cryptodev_pmd_destroy(cryptodev);
4652 : : }
4653 : :
4654 : : static struct rte_vdev_driver cryptodev_openssl_pmd_drv = {
4655 : : .probe = cryptodev_openssl_probe,
4656 : : .remove = cryptodev_openssl_remove
4657 : : };
4658 : :
4659 : : static struct cryptodev_driver openssl_crypto_drv;
4660 : :
4661 : 276 : RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_OPENSSL_PMD,
4662 : : cryptodev_openssl_pmd_drv);
4663 : : RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_OPENSSL_PMD,
4664 : : "max_nb_queue_pairs=<int> "
4665 : : "socket_id=<int>");
4666 : 276 : RTE_PMD_REGISTER_CRYPTO_DRIVER(openssl_crypto_drv,
4667 : : cryptodev_openssl_pmd_drv.driver, cryptodev_driver_id);
4668 [ - + ]: 276 : RTE_LOG_REGISTER_DEFAULT(openssl_logtype_driver, INFO);
|