1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2017 Intel Corporation
5 #define OPENSSL_API_COMPAT 0x10100000L
7 #include <rte_common.h>
8 #include <rte_hexdump.h>
9 #include <rte_cryptodev.h>
10 #include <cryptodev_pmd.h>
11 #include <rte_bus_vdev.h>
12 #include <rte_malloc.h>
13 #include <rte_cpuflags.h>
15 #include <openssl/hmac.h>
16 #include <openssl/evp.h>
18 #include "openssl_pmd_private.h"
21 #define DES_BLOCK_SIZE 8
23 static uint8_t cryptodev_driver_id;
25 #if (OPENSSL_VERSION_NUMBER < 0x10100000L)
26 static HMAC_CTX *HMAC_CTX_new(void)
28 HMAC_CTX *ctx = OPENSSL_malloc(sizeof(*ctx));
35 static void HMAC_CTX_free(HMAC_CTX *ctx)
38 HMAC_CTX_cleanup(ctx);
44 #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
46 #include <openssl/provider.h>
47 #include <openssl/core_names.h>
48 #include <openssl/param_build.h>
50 #define MAX_OSSL_ALGO_NAME_SIZE 16
52 OSSL_PROVIDER *legacy;
55 static void ossl_legacy_provider_load(void)
57 /* Load Multiple providers into the default (NULL) library context */
58 legacy = OSSL_PROVIDER_load(NULL, "legacy");
60 OPENSSL_LOG(ERR, "Failed to load Legacy provider\n");
64 deflt = OSSL_PROVIDER_load(NULL, "default");
66 OPENSSL_LOG(ERR, "Failed to load Default provider\n");
67 OSSL_PROVIDER_unload(legacy);
72 static void ossl_legacy_provider_unload(void)
74 OSSL_PROVIDER_unload(legacy);
75 OSSL_PROVIDER_unload(deflt);
78 static __rte_always_inline const char *
79 digest_name_get(enum rte_crypto_auth_algorithm algo)
82 case RTE_CRYPTO_AUTH_MD5_HMAC:
83 return OSSL_DIGEST_NAME_MD5;
84 case RTE_CRYPTO_AUTH_SHA1_HMAC:
85 return OSSL_DIGEST_NAME_SHA1;
86 case RTE_CRYPTO_AUTH_SHA224_HMAC:
87 return OSSL_DIGEST_NAME_SHA2_224;
88 case RTE_CRYPTO_AUTH_SHA256_HMAC:
89 return OSSL_DIGEST_NAME_SHA2_256;
90 case RTE_CRYPTO_AUTH_SHA384_HMAC:
91 return OSSL_DIGEST_NAME_SHA2_384;
92 case RTE_CRYPTO_AUTH_SHA512_HMAC:
93 return OSSL_DIGEST_NAME_SHA2_512;
100 static int cryptodev_openssl_remove(struct rte_vdev_device *vdev);
102 /*----------------------------------------------------------------------------*/
105 * Increment counter by 1
106 * Counter is 64 bit array, big-endian
109 ctr_inc(uint8_t *ctr)
111 uint64_t *ctr64 = (uint64_t *)ctr;
113 *ctr64 = __builtin_bswap64(*ctr64);
115 *ctr64 = __builtin_bswap64(*ctr64);
119 *------------------------------------------------------------------------------
121 *------------------------------------------------------------------------------
124 /** Get xform chain order */
125 static enum openssl_chain_order
126 openssl_get_chain_order(const struct rte_crypto_sym_xform *xform)
128 enum openssl_chain_order res = OPENSSL_CHAIN_NOT_SUPPORTED;
131 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
132 if (xform->next == NULL)
133 res = OPENSSL_CHAIN_ONLY_AUTH;
134 else if (xform->next->type ==
135 RTE_CRYPTO_SYM_XFORM_CIPHER)
136 res = OPENSSL_CHAIN_AUTH_CIPHER;
138 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
139 if (xform->next == NULL)
140 res = OPENSSL_CHAIN_ONLY_CIPHER;
141 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
142 res = OPENSSL_CHAIN_CIPHER_AUTH;
144 if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD)
145 res = OPENSSL_CHAIN_COMBINED;
151 /** Get session cipher key from input cipher key */
153 get_cipher_key(const uint8_t *input_key, int keylen, uint8_t *session_key)
155 memcpy(session_key, input_key, keylen);
158 /** Get key ede 24 bytes standard from input key */
160 get_cipher_key_ede(const uint8_t *key, int keylen, uint8_t *key_ede)
164 /* Initialize keys - 24 bytes: [key1-key2-key3] */
167 memcpy(key_ede, key, 24);
171 memcpy(key_ede, key, 16);
172 memcpy(key_ede + 16, key, 8);
175 /* K1 = K2 = K3 (DES compatibility) */
176 memcpy(key_ede, key, 8);
177 memcpy(key_ede + 8, key, 8);
178 memcpy(key_ede + 16, key, 8);
181 OPENSSL_LOG(ERR, "Unsupported key size");
188 /** Get adequate openssl function for input cipher algorithm */
190 get_cipher_algo(enum rte_crypto_cipher_algorithm sess_algo, size_t keylen,
191 const EVP_CIPHER **algo)
197 case RTE_CRYPTO_CIPHER_3DES_CBC:
200 *algo = EVP_des_cbc();
203 *algo = EVP_des_ede_cbc();
206 *algo = EVP_des_ede3_cbc();
212 case RTE_CRYPTO_CIPHER_3DES_CTR:
214 case RTE_CRYPTO_CIPHER_AES_CBC:
217 *algo = EVP_aes_128_cbc();
220 *algo = EVP_aes_192_cbc();
223 *algo = EVP_aes_256_cbc();
229 case RTE_CRYPTO_CIPHER_AES_CTR:
232 *algo = EVP_aes_128_ctr();
235 *algo = EVP_aes_192_ctr();
238 *algo = EVP_aes_256_ctr();
255 /** Get adequate openssl function for input auth algorithm */
257 get_auth_algo(enum rte_crypto_auth_algorithm sessalgo,
264 case RTE_CRYPTO_AUTH_MD5:
265 case RTE_CRYPTO_AUTH_MD5_HMAC:
268 case RTE_CRYPTO_AUTH_SHA1:
269 case RTE_CRYPTO_AUTH_SHA1_HMAC:
272 case RTE_CRYPTO_AUTH_SHA224:
273 case RTE_CRYPTO_AUTH_SHA224_HMAC:
274 *algo = EVP_sha224();
276 case RTE_CRYPTO_AUTH_SHA256:
277 case RTE_CRYPTO_AUTH_SHA256_HMAC:
278 *algo = EVP_sha256();
280 case RTE_CRYPTO_AUTH_SHA384:
281 case RTE_CRYPTO_AUTH_SHA384_HMAC:
282 *algo = EVP_sha384();
284 case RTE_CRYPTO_AUTH_SHA512:
285 case RTE_CRYPTO_AUTH_SHA512_HMAC:
286 *algo = EVP_sha512();
299 /** Get adequate openssl function for input cipher algorithm */
301 get_aead_algo(enum rte_crypto_aead_algorithm sess_algo, size_t keylen,
302 const EVP_CIPHER **algo)
308 case RTE_CRYPTO_AEAD_AES_GCM:
311 *algo = EVP_aes_128_gcm();
314 *algo = EVP_aes_192_gcm();
317 *algo = EVP_aes_256_gcm();
323 case RTE_CRYPTO_AEAD_AES_CCM:
326 *algo = EVP_aes_128_ccm();
329 *algo = EVP_aes_192_ccm();
332 *algo = EVP_aes_256_ccm();
349 /* Set session AEAD encryption parameters */
351 openssl_set_sess_aead_enc_param(struct openssl_session *sess,
352 enum rte_crypto_aead_algorithm algo,
353 uint8_t tag_len, const uint8_t *key)
358 sess->cipher.direction = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
359 sess->auth.operation = RTE_CRYPTO_AUTH_OP_GENERATE;
361 /* Select AEAD algo */
363 case RTE_CRYPTO_AEAD_AES_GCM:
364 iv_type = EVP_CTRL_GCM_SET_IVLEN;
369 case RTE_CRYPTO_AEAD_AES_CCM:
370 iv_type = EVP_CTRL_CCM_SET_IVLEN;
371 /* Digest size can be 4, 6, 8, 10, 12, 14 or 16 bytes */
372 if (tag_len < 4 || tag_len > 16 || (tag_len & 1) == 1)
380 sess->cipher.mode = OPENSSL_CIPHER_LIB;
381 sess->cipher.ctx = EVP_CIPHER_CTX_new();
383 if (get_aead_algo(algo, sess->cipher.key.length,
384 &sess->cipher.evp_algo) != 0)
387 get_cipher_key(key, sess->cipher.key.length, sess->cipher.key.data);
389 sess->chain_order = OPENSSL_CHAIN_COMBINED;
391 if (EVP_EncryptInit_ex(sess->cipher.ctx, sess->cipher.evp_algo,
392 NULL, NULL, NULL) <= 0)
395 if (EVP_CIPHER_CTX_ctrl(sess->cipher.ctx, iv_type, sess->iv.length,
400 EVP_CIPHER_CTX_ctrl(sess->cipher.ctx, EVP_CTRL_CCM_SET_TAG,
403 if (EVP_EncryptInit_ex(sess->cipher.ctx, NULL, NULL, key, NULL) <= 0)
409 /* Set session AEAD decryption parameters */
411 openssl_set_sess_aead_dec_param(struct openssl_session *sess,
412 enum rte_crypto_aead_algorithm algo,
413 uint8_t tag_len, const uint8_t *key)
416 unsigned int do_ccm = 0;
418 sess->cipher.direction = RTE_CRYPTO_CIPHER_OP_DECRYPT;
419 sess->auth.operation = RTE_CRYPTO_AUTH_OP_VERIFY;
421 /* Select AEAD algo */
423 case RTE_CRYPTO_AEAD_AES_GCM:
424 iv_type = EVP_CTRL_GCM_SET_IVLEN;
428 case RTE_CRYPTO_AEAD_AES_CCM:
429 iv_type = EVP_CTRL_CCM_SET_IVLEN;
430 /* Digest size can be 4, 6, 8, 10, 12, 14 or 16 bytes */
431 if (tag_len < 4 || tag_len > 16 || (tag_len & 1) == 1)
439 sess->cipher.mode = OPENSSL_CIPHER_LIB;
440 sess->cipher.ctx = EVP_CIPHER_CTX_new();
442 if (get_aead_algo(algo, sess->cipher.key.length,
443 &sess->cipher.evp_algo) != 0)
446 get_cipher_key(key, sess->cipher.key.length, sess->cipher.key.data);
448 sess->chain_order = OPENSSL_CHAIN_COMBINED;
450 if (EVP_DecryptInit_ex(sess->cipher.ctx, sess->cipher.evp_algo,
451 NULL, NULL, NULL) <= 0)
454 if (EVP_CIPHER_CTX_ctrl(sess->cipher.ctx, iv_type,
455 sess->iv.length, NULL) <= 0)
459 EVP_CIPHER_CTX_ctrl(sess->cipher.ctx, EVP_CTRL_CCM_SET_TAG,
462 if (EVP_DecryptInit_ex(sess->cipher.ctx, NULL, NULL, key, NULL) <= 0)
468 /** Set session cipher parameters */
470 openssl_set_session_cipher_parameters(struct openssl_session *sess,
471 const struct rte_crypto_sym_xform *xform)
473 /* Select cipher direction */
474 sess->cipher.direction = xform->cipher.op;
475 /* Select cipher key */
476 sess->cipher.key.length = xform->cipher.key.length;
478 /* Set IV parameters */
479 sess->iv.offset = xform->cipher.iv.offset;
480 sess->iv.length = xform->cipher.iv.length;
482 /* Select cipher algo */
483 switch (xform->cipher.algo) {
484 case RTE_CRYPTO_CIPHER_3DES_CBC:
485 case RTE_CRYPTO_CIPHER_AES_CBC:
486 case RTE_CRYPTO_CIPHER_AES_CTR:
487 sess->cipher.mode = OPENSSL_CIPHER_LIB;
488 sess->cipher.algo = xform->cipher.algo;
489 sess->cipher.ctx = EVP_CIPHER_CTX_new();
491 if (get_cipher_algo(sess->cipher.algo, sess->cipher.key.length,
492 &sess->cipher.evp_algo) != 0)
495 get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
496 sess->cipher.key.data);
497 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
498 if (EVP_EncryptInit_ex(sess->cipher.ctx,
499 sess->cipher.evp_algo,
500 NULL, xform->cipher.key.data,
504 } else if (sess->cipher.direction ==
505 RTE_CRYPTO_CIPHER_OP_DECRYPT) {
506 if (EVP_DecryptInit_ex(sess->cipher.ctx,
507 sess->cipher.evp_algo,
508 NULL, xform->cipher.key.data,
516 case RTE_CRYPTO_CIPHER_3DES_CTR:
517 sess->cipher.mode = OPENSSL_CIPHER_DES3CTR;
518 sess->cipher.ctx = EVP_CIPHER_CTX_new();
520 if (get_cipher_key_ede(xform->cipher.key.data,
521 sess->cipher.key.length,
522 sess->cipher.key.data) != 0)
526 case RTE_CRYPTO_CIPHER_DES_CBC:
527 sess->cipher.algo = xform->cipher.algo;
528 sess->cipher.ctx = EVP_CIPHER_CTX_new();
529 sess->cipher.evp_algo = EVP_des_cbc();
531 get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
532 sess->cipher.key.data);
533 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
534 if (EVP_EncryptInit_ex(sess->cipher.ctx,
535 sess->cipher.evp_algo,
536 NULL, xform->cipher.key.data,
540 } else if (sess->cipher.direction ==
541 RTE_CRYPTO_CIPHER_OP_DECRYPT) {
542 if (EVP_DecryptInit_ex(sess->cipher.ctx,
543 sess->cipher.evp_algo,
544 NULL, xform->cipher.key.data,
552 case RTE_CRYPTO_CIPHER_DES_DOCSISBPI:
553 sess->cipher.algo = xform->cipher.algo;
554 sess->chain_order = OPENSSL_CHAIN_CIPHER_BPI;
555 sess->cipher.ctx = EVP_CIPHER_CTX_new();
556 sess->cipher.evp_algo = EVP_des_cbc();
558 sess->cipher.bpi_ctx = EVP_CIPHER_CTX_new();
559 /* IV will be ECB encrypted whether direction is encrypt or decrypt */
560 if (EVP_EncryptInit_ex(sess->cipher.bpi_ctx, EVP_des_ecb(),
561 NULL, xform->cipher.key.data, 0) != 1)
564 get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
565 sess->cipher.key.data);
566 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
567 if (EVP_EncryptInit_ex(sess->cipher.ctx,
568 sess->cipher.evp_algo,
569 NULL, xform->cipher.key.data,
573 } else if (sess->cipher.direction ==
574 RTE_CRYPTO_CIPHER_OP_DECRYPT) {
575 if (EVP_DecryptInit_ex(sess->cipher.ctx,
576 sess->cipher.evp_algo,
577 NULL, xform->cipher.key.data,
585 sess->cipher.algo = RTE_CRYPTO_CIPHER_NULL;
592 /* Set session auth parameters */
594 openssl_set_session_auth_parameters(struct openssl_session *sess,
595 const struct rte_crypto_sym_xform *xform)
597 /* Select auth generate/verify */
598 sess->auth.operation = xform->auth.op;
599 sess->auth.algo = xform->auth.algo;
601 sess->auth.digest_length = xform->auth.digest_length;
603 /* Select auth algo */
604 switch (xform->auth.algo) {
605 case RTE_CRYPTO_AUTH_AES_GMAC:
607 * OpenSSL requires GMAC to be a GCM operation
608 * with no cipher data length
610 sess->cipher.key.length = xform->auth.key.length;
612 /* Set IV parameters */
613 sess->iv.offset = xform->auth.iv.offset;
614 sess->iv.length = xform->auth.iv.length;
616 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_GENERATE)
617 return openssl_set_sess_aead_enc_param(sess,
618 RTE_CRYPTO_AEAD_AES_GCM,
619 xform->auth.digest_length,
620 xform->auth.key.data);
622 return openssl_set_sess_aead_dec_param(sess,
623 RTE_CRYPTO_AEAD_AES_GCM,
624 xform->auth.digest_length,
625 xform->auth.key.data);
628 case RTE_CRYPTO_AUTH_MD5:
629 case RTE_CRYPTO_AUTH_SHA1:
630 case RTE_CRYPTO_AUTH_SHA224:
631 case RTE_CRYPTO_AUTH_SHA256:
632 case RTE_CRYPTO_AUTH_SHA384:
633 case RTE_CRYPTO_AUTH_SHA512:
634 sess->auth.mode = OPENSSL_AUTH_AS_AUTH;
635 if (get_auth_algo(xform->auth.algo,
636 &sess->auth.auth.evp_algo) != 0)
638 sess->auth.auth.ctx = EVP_MD_CTX_create();
641 # if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
642 case RTE_CRYPTO_AUTH_MD5_HMAC:
643 case RTE_CRYPTO_AUTH_SHA1_HMAC:
644 case RTE_CRYPTO_AUTH_SHA224_HMAC:
645 case RTE_CRYPTO_AUTH_SHA256_HMAC:
646 case RTE_CRYPTO_AUTH_SHA384_HMAC:
647 case RTE_CRYPTO_AUTH_SHA512_HMAC:
648 sess->auth.mode = OPENSSL_AUTH_AS_HMAC;
650 OSSL_PARAM params[2];
652 algo = digest_name_get(xform->auth.algo);
655 char algo_name[MAX_OSSL_ALGO_NAME_SIZE];
656 rte_memcpy(algo_name, algo, (sizeof(algo)+1));
658 EVP_MAC *mac = EVP_MAC_fetch(NULL, "HMAC", NULL);
659 sess->auth.hmac.ctx = EVP_MAC_CTX_new(mac);
661 if (get_auth_algo(xform->auth.algo,
662 &sess->auth.hmac.evp_algo) != 0)
665 params[0] = OSSL_PARAM_construct_utf8_string("digest",
667 params[1] = OSSL_PARAM_construct_end();
668 if (EVP_MAC_init(sess->auth.hmac.ctx,
669 xform->auth.key.data,
670 xform->auth.key.length,
675 case RTE_CRYPTO_AUTH_MD5_HMAC:
676 case RTE_CRYPTO_AUTH_SHA1_HMAC:
677 case RTE_CRYPTO_AUTH_SHA224_HMAC:
678 case RTE_CRYPTO_AUTH_SHA256_HMAC:
679 case RTE_CRYPTO_AUTH_SHA384_HMAC:
680 case RTE_CRYPTO_AUTH_SHA512_HMAC:
681 sess->auth.mode = OPENSSL_AUTH_AS_HMAC;
682 sess->auth.hmac.ctx = HMAC_CTX_new();
683 if (get_auth_algo(xform->auth.algo,
684 &sess->auth.hmac.evp_algo) != 0)
687 if (HMAC_Init_ex(sess->auth.hmac.ctx,
688 xform->auth.key.data,
689 xform->auth.key.length,
690 sess->auth.hmac.evp_algo, NULL) != 1)
701 /* Set session AEAD parameters */
703 openssl_set_session_aead_parameters(struct openssl_session *sess,
704 const struct rte_crypto_sym_xform *xform)
706 /* Select cipher key */
707 sess->cipher.key.length = xform->aead.key.length;
709 /* Set IV parameters */
710 if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_CCM)
712 * For AES-CCM, the actual IV is placed
713 * one byte after the start of the IV field,
714 * according to the API.
716 sess->iv.offset = xform->aead.iv.offset + 1;
718 sess->iv.offset = xform->aead.iv.offset;
720 sess->iv.length = xform->aead.iv.length;
722 sess->auth.aad_length = xform->aead.aad_length;
723 sess->auth.digest_length = xform->aead.digest_length;
725 sess->aead_algo = xform->aead.algo;
726 /* Select cipher direction */
727 if (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
728 return openssl_set_sess_aead_enc_param(sess, xform->aead.algo,
729 xform->aead.digest_length, xform->aead.key.data);
731 return openssl_set_sess_aead_dec_param(sess, xform->aead.algo,
732 xform->aead.digest_length, xform->aead.key.data);
735 /** Parse crypto xform chain and set private session parameters */
737 openssl_set_session_parameters(struct openssl_session *sess,
738 const struct rte_crypto_sym_xform *xform)
740 const struct rte_crypto_sym_xform *cipher_xform = NULL;
741 const struct rte_crypto_sym_xform *auth_xform = NULL;
742 const struct rte_crypto_sym_xform *aead_xform = NULL;
745 sess->chain_order = openssl_get_chain_order(xform);
746 switch (sess->chain_order) {
747 case OPENSSL_CHAIN_ONLY_CIPHER:
748 cipher_xform = xform;
750 case OPENSSL_CHAIN_ONLY_AUTH:
753 case OPENSSL_CHAIN_CIPHER_AUTH:
754 cipher_xform = xform;
755 auth_xform = xform->next;
757 case OPENSSL_CHAIN_AUTH_CIPHER:
759 cipher_xform = xform->next;
761 case OPENSSL_CHAIN_COMBINED:
768 /* Default IV length = 0 */
771 /* cipher_xform must be check before auth_xform */
773 ret = openssl_set_session_cipher_parameters(
777 "Invalid/unsupported cipher parameters");
783 ret = openssl_set_session_auth_parameters(sess, auth_xform);
786 "Invalid/unsupported auth parameters");
792 ret = openssl_set_session_aead_parameters(sess, aead_xform);
795 "Invalid/unsupported AEAD parameters");
803 /** Reset private session parameters */
805 openssl_reset_session(struct openssl_session *sess)
807 EVP_CIPHER_CTX_free(sess->cipher.ctx);
809 if (sess->chain_order == OPENSSL_CHAIN_CIPHER_BPI)
810 EVP_CIPHER_CTX_free(sess->cipher.bpi_ctx);
812 switch (sess->auth.mode) {
813 case OPENSSL_AUTH_AS_AUTH:
814 EVP_MD_CTX_destroy(sess->auth.auth.ctx);
816 case OPENSSL_AUTH_AS_HMAC:
817 EVP_PKEY_free(sess->auth.hmac.pkey);
818 # if OPENSSL_VERSION_NUMBER >= 0x30000000L
819 EVP_MAC_CTX_free(sess->auth.hmac.ctx);
821 HMAC_CTX_free(sess->auth.hmac.ctx);
829 /** Provide session for operation */
831 get_session(struct openssl_qp *qp, struct rte_crypto_op *op)
833 struct openssl_session *sess = NULL;
834 struct openssl_asym_session *asym_sess = NULL;
836 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
837 if (op->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
838 /* get existing session */
839 if (likely(op->sym->session != NULL))
840 sess = (struct openssl_session *)
841 get_sym_session_private_data(
843 cryptodev_driver_id);
845 if (likely(op->asym->session != NULL))
846 asym_sess = (struct openssl_asym_session *)
847 op->asym->session->sess_private_data;
848 if (asym_sess == NULL)
850 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
854 /* sessionless asymmetric not supported */
855 if (op->type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC)
858 /* provide internal session */
859 void *_sess = rte_cryptodev_sym_session_create(qp->sess_mp);
860 void *_sess_private_data = NULL;
865 if (rte_mempool_get(qp->sess_mp_priv,
866 (void **)&_sess_private_data))
869 sess = (struct openssl_session *)_sess_private_data;
871 if (unlikely(openssl_set_session_parameters(sess,
872 op->sym->xform) != 0)) {
873 rte_mempool_put(qp->sess_mp, _sess);
874 rte_mempool_put(qp->sess_mp_priv, _sess_private_data);
877 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
878 set_sym_session_private_data(op->sym->session,
879 cryptodev_driver_id, _sess_private_data);
883 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
889 *------------------------------------------------------------------------------
891 *------------------------------------------------------------------------------
894 process_openssl_encryption_update(struct rte_mbuf *mbuf_src, int offset,
895 uint8_t **dst, int srclen, EVP_CIPHER_CTX *ctx, uint8_t inplace)
900 uint8_t *src, temp[EVP_CIPHER_CTX_block_size(ctx)];
902 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
904 offset -= rte_pktmbuf_data_len(m);
909 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
913 l = rte_pktmbuf_data_len(m) - offset;
915 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, srclen) <= 0)
921 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
927 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
928 uint8_t diff = l - dstlen, rem;
930 src = rte_pktmbuf_mtod(m, uint8_t *);
931 l = RTE_MIN(rte_pktmbuf_data_len(m), n);
932 if (diff && inplace) {
934 (EVP_CIPHER_CTX_block_size(ctx) - diff));
935 if (EVP_EncryptUpdate(ctx, temp,
936 &dstlen, src, rem) <= 0)
939 rte_memcpy(*dst, temp, diff);
940 rte_memcpy(src, temp + diff, rem);
946 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
956 process_openssl_decryption_update(struct rte_mbuf *mbuf_src, int offset,
957 uint8_t **dst, int srclen, EVP_CIPHER_CTX *ctx, uint8_t inplace)
962 uint8_t *src, temp[EVP_CIPHER_CTX_block_size(ctx)];
964 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
966 offset -= rte_pktmbuf_data_len(m);
971 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
975 l = rte_pktmbuf_data_len(m) - offset;
977 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, srclen) <= 0)
983 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
989 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
990 uint8_t diff = l - dstlen, rem;
992 src = rte_pktmbuf_mtod(m, uint8_t *);
993 l = RTE_MIN(rte_pktmbuf_data_len(m), n);
994 if (diff && inplace) {
996 (EVP_CIPHER_CTX_block_size(ctx) - diff));
997 if (EVP_DecryptUpdate(ctx, temp,
998 &dstlen, src, rem) <= 0)
1001 rte_memcpy(*dst, temp, diff);
1002 rte_memcpy(src, temp + diff, rem);
1008 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
1017 /** Process standard openssl cipher encryption */
1019 process_openssl_cipher_encrypt(struct rte_mbuf *mbuf_src, uint8_t *dst,
1020 int offset, uint8_t *iv, int srclen, EVP_CIPHER_CTX *ctx,
1025 if (EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0)
1026 goto process_cipher_encrypt_err;
1028 EVP_CIPHER_CTX_set_padding(ctx, 0);
1030 if (process_openssl_encryption_update(mbuf_src, offset, &dst,
1031 srclen, ctx, inplace))
1032 goto process_cipher_encrypt_err;
1034 if (EVP_EncryptFinal_ex(ctx, dst, &totlen) <= 0)
1035 goto process_cipher_encrypt_err;
1039 process_cipher_encrypt_err:
1040 OPENSSL_LOG(ERR, "Process openssl cipher encrypt failed");
1044 /** Process standard openssl cipher encryption */
1046 process_openssl_cipher_bpi_encrypt(uint8_t *src, uint8_t *dst,
1047 uint8_t *iv, int srclen,
1048 EVP_CIPHER_CTX *ctx)
1051 uint8_t encrypted_iv[DES_BLOCK_SIZE];
1052 int encrypted_ivlen;
1054 if (EVP_EncryptUpdate(ctx, encrypted_iv, &encrypted_ivlen,
1055 iv, DES_BLOCK_SIZE) <= 0)
1056 goto process_cipher_encrypt_err;
1058 for (i = 0; i < srclen; i++)
1059 *(dst + i) = *(src + i) ^ (encrypted_iv[i]);
1063 process_cipher_encrypt_err:
1064 OPENSSL_LOG(ERR, "Process openssl cipher bpi encrypt failed");
1067 /** Process standard openssl cipher decryption */
1069 process_openssl_cipher_decrypt(struct rte_mbuf *mbuf_src, uint8_t *dst,
1070 int offset, uint8_t *iv, int srclen, EVP_CIPHER_CTX *ctx,
1075 if (EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0)
1076 goto process_cipher_decrypt_err;
1078 EVP_CIPHER_CTX_set_padding(ctx, 0);
1080 if (process_openssl_decryption_update(mbuf_src, offset, &dst,
1081 srclen, ctx, inplace))
1082 goto process_cipher_decrypt_err;
1084 if (EVP_DecryptFinal_ex(ctx, dst, &totlen) <= 0)
1085 goto process_cipher_decrypt_err;
1088 process_cipher_decrypt_err:
1089 OPENSSL_LOG(ERR, "Process openssl cipher decrypt failed");
1093 /** Process cipher des 3 ctr encryption, decryption algorithm */
1095 process_openssl_cipher_des3ctr(struct rte_mbuf *mbuf_src, uint8_t *dst,
1096 int offset, uint8_t *iv, uint8_t *key, int srclen,
1097 EVP_CIPHER_CTX *ctx)
1099 uint8_t ebuf[8], ctr[8];
1105 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
1107 offset -= rte_pktmbuf_data_len(m);
1110 goto process_cipher_des3ctr_err;
1112 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
1113 l = rte_pktmbuf_data_len(m) - offset;
1115 /* We use 3DES encryption also for decryption.
1116 * IV is not important for 3DES ecb
1118 if (EVP_EncryptInit_ex(ctx, EVP_des_ede3_ecb(), NULL, key, NULL) <= 0)
1119 goto process_cipher_des3ctr_err;
1123 for (n = 0; n < srclen; n++) {
1125 if (EVP_EncryptUpdate(ctx,
1126 (unsigned char *)&ebuf, &unused,
1127 (const unsigned char *)&ctr, 8) <= 0)
1128 goto process_cipher_des3ctr_err;
1131 dst[n] = *(src++) ^ ebuf[n % 8];
1137 src = rte_pktmbuf_mtod(m, uint8_t *);
1138 l = rte_pktmbuf_data_len(m);
1145 process_cipher_des3ctr_err:
1146 OPENSSL_LOG(ERR, "Process openssl cipher des 3 ede ctr failed");
1150 /** Process AES-GCM encrypt algorithm */
1152 process_openssl_auth_encryption_gcm(struct rte_mbuf *mbuf_src, int offset,
1153 int srclen, uint8_t *aad, int aadlen, uint8_t *iv,
1154 uint8_t *dst, uint8_t *tag, EVP_CIPHER_CTX *ctx)
1156 int len = 0, unused = 0;
1157 uint8_t empty[] = {};
1159 if (EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0)
1160 goto process_auth_encryption_gcm_err;
1163 if (EVP_EncryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
1164 goto process_auth_encryption_gcm_err;
1167 if (process_openssl_encryption_update(mbuf_src, offset, &dst,
1169 goto process_auth_encryption_gcm_err;
1171 /* Workaround open ssl bug in version less then 1.0.1f */
1172 if (EVP_EncryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
1173 goto process_auth_encryption_gcm_err;
1175 if (EVP_EncryptFinal_ex(ctx, dst, &len) <= 0)
1176 goto process_auth_encryption_gcm_err;
1178 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag) <= 0)
1179 goto process_auth_encryption_gcm_err;
1183 process_auth_encryption_gcm_err:
1184 OPENSSL_LOG(ERR, "Process openssl auth encryption gcm failed");
1188 /** Process AES-CCM encrypt algorithm */
1190 process_openssl_auth_encryption_ccm(struct rte_mbuf *mbuf_src, int offset,
1191 int srclen, uint8_t *aad, int aadlen, uint8_t *iv,
1192 uint8_t *dst, uint8_t *tag, uint8_t taglen, EVP_CIPHER_CTX *ctx)
1196 if (EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0)
1197 goto process_auth_encryption_ccm_err;
1199 if (EVP_EncryptUpdate(ctx, NULL, &len, NULL, srclen) <= 0)
1200 goto process_auth_encryption_ccm_err;
1204 * For AES-CCM, the actual AAD is placed
1205 * 18 bytes after the start of the AAD field,
1206 * according to the API.
1208 if (EVP_EncryptUpdate(ctx, NULL, &len, aad + 18, aadlen) <= 0)
1209 goto process_auth_encryption_ccm_err;
1212 if (process_openssl_encryption_update(mbuf_src, offset, &dst,
1214 goto process_auth_encryption_ccm_err;
1216 if (EVP_EncryptFinal_ex(ctx, dst, &len) <= 0)
1217 goto process_auth_encryption_ccm_err;
1219 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_GET_TAG, taglen, tag) <= 0)
1220 goto process_auth_encryption_ccm_err;
1224 process_auth_encryption_ccm_err:
1225 OPENSSL_LOG(ERR, "Process openssl auth encryption ccm failed");
1229 /** Process AES-GCM decrypt algorithm */
1231 process_openssl_auth_decryption_gcm(struct rte_mbuf *mbuf_src, int offset,
1232 int srclen, uint8_t *aad, int aadlen, uint8_t *iv,
1233 uint8_t *dst, uint8_t *tag, EVP_CIPHER_CTX *ctx)
1235 int len = 0, unused = 0;
1236 uint8_t empty[] = {};
1238 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag) <= 0)
1239 goto process_auth_decryption_gcm_err;
1241 if (EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0)
1242 goto process_auth_decryption_gcm_err;
1245 if (EVP_DecryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
1246 goto process_auth_decryption_gcm_err;
1249 if (process_openssl_decryption_update(mbuf_src, offset, &dst,
1251 goto process_auth_decryption_gcm_err;
1253 /* Workaround open ssl bug in version less then 1.0.1f */
1254 if (EVP_DecryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
1255 goto process_auth_decryption_gcm_err;
1257 if (EVP_DecryptFinal_ex(ctx, dst, &len) <= 0)
1262 process_auth_decryption_gcm_err:
1263 OPENSSL_LOG(ERR, "Process openssl auth decryption gcm failed");
1267 /** Process AES-CCM decrypt algorithm */
1269 process_openssl_auth_decryption_ccm(struct rte_mbuf *mbuf_src, int offset,
1270 int srclen, uint8_t *aad, int aadlen, uint8_t *iv,
1271 uint8_t *dst, uint8_t *tag, uint8_t tag_len,
1272 EVP_CIPHER_CTX *ctx)
1276 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, tag_len, tag) <= 0)
1277 goto process_auth_decryption_ccm_err;
1279 if (EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0)
1280 goto process_auth_decryption_ccm_err;
1282 if (EVP_DecryptUpdate(ctx, NULL, &len, NULL, srclen) <= 0)
1283 goto process_auth_decryption_ccm_err;
1287 * For AES-CCM, the actual AAD is placed
1288 * 18 bytes after the start of the AAD field,
1289 * according to the API.
1291 if (EVP_DecryptUpdate(ctx, NULL, &len, aad + 18, aadlen) <= 0)
1292 goto process_auth_decryption_ccm_err;
1295 if (process_openssl_decryption_update(mbuf_src, offset, &dst,
1301 process_auth_decryption_ccm_err:
1302 OPENSSL_LOG(ERR, "Process openssl auth decryption ccm failed");
1306 /** Process standard openssl auth algorithms */
1308 process_openssl_auth(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
1309 __rte_unused uint8_t *iv, __rte_unused EVP_PKEY * pkey,
1310 int srclen, EVP_MD_CTX *ctx, const EVP_MD *algo)
1317 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
1319 offset -= rte_pktmbuf_data_len(m);
1322 goto process_auth_err;
1324 if (EVP_DigestInit_ex(ctx, algo, NULL) <= 0)
1325 goto process_auth_err;
1327 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
1329 l = rte_pktmbuf_data_len(m) - offset;
1331 if (EVP_DigestUpdate(ctx, (char *)src, srclen) <= 0)
1332 goto process_auth_err;
1333 goto process_auth_final;
1336 if (EVP_DigestUpdate(ctx, (char *)src, l) <= 0)
1337 goto process_auth_err;
1341 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
1342 src = rte_pktmbuf_mtod(m, uint8_t *);
1343 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
1344 if (EVP_DigestUpdate(ctx, (char *)src, l) <= 0)
1345 goto process_auth_err;
1350 if (EVP_DigestFinal_ex(ctx, dst, (unsigned int *)&dstlen) <= 0)
1351 goto process_auth_err;
1355 OPENSSL_LOG(ERR, "Process openssl auth failed");
1359 # if OPENSSL_VERSION_NUMBER >= 0x30000000L
1360 /** Process standard openssl auth algorithms with hmac */
1362 process_openssl_auth_hmac(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
1363 int srclen, EVP_MAC_CTX *ctx)
1370 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
1372 offset -= rte_pktmbuf_data_len(m);
1375 goto process_auth_err;
1377 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
1379 l = rte_pktmbuf_data_len(m) - offset;
1381 if (EVP_MAC_update(ctx, (unsigned char *)src, srclen) != 1)
1382 goto process_auth_err;
1383 goto process_auth_final;
1386 if (EVP_MAC_update(ctx, (unsigned char *)src, l) != 1)
1387 goto process_auth_err;
1391 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
1392 src = rte_pktmbuf_mtod(m, uint8_t *);
1393 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
1394 if (EVP_MAC_update(ctx, (unsigned char *)src, l) != 1)
1395 goto process_auth_err;
1400 if (EVP_MAC_final(ctx, dst, &dstlen, sizeof(dst)) != 1)
1401 goto process_auth_err;
1403 EVP_MAC_CTX_free(ctx);
1407 EVP_MAC_CTX_free(ctx);
1408 OPENSSL_LOG(ERR, "Process openssl auth failed");
1412 /** Process standard openssl auth algorithms with hmac */
1414 process_openssl_auth_hmac(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
1415 int srclen, HMAC_CTX *ctx)
1417 unsigned int dstlen;
1422 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
1424 offset -= rte_pktmbuf_data_len(m);
1427 goto process_auth_err;
1429 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
1431 l = rte_pktmbuf_data_len(m) - offset;
1433 if (HMAC_Update(ctx, (unsigned char *)src, srclen) != 1)
1434 goto process_auth_err;
1435 goto process_auth_final;
1438 if (HMAC_Update(ctx, (unsigned char *)src, l) != 1)
1439 goto process_auth_err;
1443 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
1444 src = rte_pktmbuf_mtod(m, uint8_t *);
1445 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
1446 if (HMAC_Update(ctx, (unsigned char *)src, l) != 1)
1447 goto process_auth_err;
1452 if (HMAC_Final(ctx, dst, &dstlen) != 1)
1453 goto process_auth_err;
1455 if (unlikely(HMAC_Init_ex(ctx, NULL, 0, NULL, NULL) != 1))
1456 goto process_auth_err;
1461 OPENSSL_LOG(ERR, "Process openssl auth failed");
1465 /*----------------------------------------------------------------------------*/
1467 /** Process auth/cipher combined operation */
1469 process_openssl_combined_op
1470 (struct rte_crypto_op *op, struct openssl_session *sess,
1471 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
1474 uint8_t *dst = NULL, *iv, *tag, *aad;
1475 int srclen, aadlen, status = -1;
1480 * Segmented destination buffer is not supported for
1481 * encryption/decryption
1483 if (!rte_pktmbuf_is_contiguous(mbuf_dst)) {
1484 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1488 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1490 if (sess->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC) {
1492 offset = op->sym->auth.data.offset;
1493 aadlen = op->sym->auth.data.length;
1494 aad = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
1495 op->sym->auth.data.offset);
1496 tag = op->sym->auth.digest.data;
1498 tag = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1501 srclen = op->sym->aead.data.length;
1502 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1503 op->sym->aead.data.offset);
1504 offset = op->sym->aead.data.offset;
1505 aad = op->sym->aead.aad.data;
1506 aadlen = sess->auth.aad_length;
1507 tag = op->sym->aead.digest.data;
1509 tag = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1513 taglen = sess->auth.digest_length;
1515 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
1516 if (sess->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC ||
1517 sess->aead_algo == RTE_CRYPTO_AEAD_AES_GCM)
1518 status = process_openssl_auth_encryption_gcm(
1519 mbuf_src, offset, srclen,
1521 dst, tag, sess->cipher.ctx);
1523 status = process_openssl_auth_encryption_ccm(
1524 mbuf_src, offset, srclen,
1526 dst, tag, taglen, sess->cipher.ctx);
1529 if (sess->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC ||
1530 sess->aead_algo == RTE_CRYPTO_AEAD_AES_GCM)
1531 status = process_openssl_auth_decryption_gcm(
1532 mbuf_src, offset, srclen,
1534 dst, tag, sess->cipher.ctx);
1536 status = process_openssl_auth_decryption_ccm(
1537 mbuf_src, offset, srclen,
1539 dst, tag, taglen, sess->cipher.ctx);
1543 if (status == (-EFAULT) &&
1544 sess->auth.operation ==
1545 RTE_CRYPTO_AUTH_OP_VERIFY)
1546 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1548 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1552 /** Process cipher operation */
1554 process_openssl_cipher_op
1555 (struct rte_crypto_op *op, struct openssl_session *sess,
1556 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
1560 uint8_t inplace = (mbuf_src == mbuf_dst) ? 1 : 0;
1561 EVP_CIPHER_CTX *ctx_copy;
1564 * Segmented OOP destination buffer is not supported for encryption/
1565 * decryption. In case of des3ctr, even inplace segmented buffers are
1568 if (!rte_pktmbuf_is_contiguous(mbuf_dst) &&
1569 (!inplace || sess->cipher.mode != OPENSSL_CIPHER_LIB)) {
1570 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1574 srclen = op->sym->cipher.data.length;
1575 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1576 op->sym->cipher.data.offset);
1578 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1580 ctx_copy = EVP_CIPHER_CTX_new();
1581 EVP_CIPHER_CTX_copy(ctx_copy, sess->cipher.ctx);
1583 if (sess->cipher.mode == OPENSSL_CIPHER_LIB)
1584 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1585 status = process_openssl_cipher_encrypt(mbuf_src, dst,
1586 op->sym->cipher.data.offset, iv,
1587 srclen, ctx_copy, inplace);
1589 status = process_openssl_cipher_decrypt(mbuf_src, dst,
1590 op->sym->cipher.data.offset, iv,
1591 srclen, ctx_copy, inplace);
1593 status = process_openssl_cipher_des3ctr(mbuf_src, dst,
1594 op->sym->cipher.data.offset, iv,
1595 sess->cipher.key.data, srclen,
1598 EVP_CIPHER_CTX_free(ctx_copy);
1600 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1603 /** Process cipher operation */
1605 process_openssl_docsis_bpi_op(struct rte_crypto_op *op,
1606 struct openssl_session *sess, struct rte_mbuf *mbuf_src,
1607 struct rte_mbuf *mbuf_dst)
1609 uint8_t *src, *dst, *iv;
1610 uint8_t block_size, last_block_len;
1611 int srclen, status = 0;
1613 srclen = op->sym->cipher.data.length;
1614 src = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
1615 op->sym->cipher.data.offset);
1616 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1617 op->sym->cipher.data.offset);
1619 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1622 block_size = DES_BLOCK_SIZE;
1624 last_block_len = srclen % block_size;
1625 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
1626 /* Encrypt only with ECB mode XOR IV */
1627 if (srclen < block_size) {
1628 status = process_openssl_cipher_bpi_encrypt(src, dst,
1630 sess->cipher.bpi_ctx);
1632 srclen -= last_block_len;
1633 /* Encrypt with the block aligned stream with CBC mode */
1634 status = process_openssl_cipher_encrypt(mbuf_src, dst,
1635 op->sym->cipher.data.offset, iv,
1636 srclen, sess->cipher.ctx, 0);
1637 if (last_block_len) {
1638 /* Point at last block */
1641 * IV is the last encrypted block from
1642 * the previous operation
1644 iv = dst - block_size;
1646 srclen = last_block_len;
1647 /* Encrypt the last frame with ECB mode */
1648 status |= process_openssl_cipher_bpi_encrypt(src,
1650 srclen, sess->cipher.bpi_ctx);
1654 /* Decrypt only with ECB mode (encrypt, as it is same operation) */
1655 if (srclen < block_size) {
1656 status = process_openssl_cipher_bpi_encrypt(src, dst,
1659 sess->cipher.bpi_ctx);
1661 if (last_block_len) {
1662 /* Point at last block */
1663 dst += srclen - last_block_len;
1664 src += srclen - last_block_len;
1666 * IV is the last full block
1668 iv = src - block_size;
1670 * Decrypt the last frame with ECB mode
1671 * (encrypt, as it is the same operation)
1673 status = process_openssl_cipher_bpi_encrypt(src,
1675 last_block_len, sess->cipher.bpi_ctx);
1676 /* Prepare parameters for CBC mode op */
1677 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1679 dst += last_block_len - srclen;
1680 srclen -= last_block_len;
1683 /* Decrypt with CBC mode */
1684 status |= process_openssl_cipher_decrypt(mbuf_src, dst,
1685 op->sym->cipher.data.offset, iv,
1686 srclen, sess->cipher.ctx, 0);
1691 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1694 /** Process auth operation */
1696 process_openssl_auth_op(struct openssl_qp *qp, struct rte_crypto_op *op,
1697 struct openssl_session *sess, struct rte_mbuf *mbuf_src,
1698 struct rte_mbuf *mbuf_dst)
1703 # if OPENSSL_VERSION_NUMBER >= 0x30000000L
1710 srclen = op->sym->auth.data.length;
1712 dst = qp->temp_digest;
1714 switch (sess->auth.mode) {
1715 case OPENSSL_AUTH_AS_AUTH:
1716 ctx_a = EVP_MD_CTX_create();
1717 EVP_MD_CTX_copy_ex(ctx_a, sess->auth.auth.ctx);
1718 status = process_openssl_auth(mbuf_src, dst,
1719 op->sym->auth.data.offset, NULL, NULL, srclen,
1720 ctx_a, sess->auth.auth.evp_algo);
1721 EVP_MD_CTX_destroy(ctx_a);
1723 case OPENSSL_AUTH_AS_HMAC:
1724 # if OPENSSL_VERSION_NUMBER >= 0x30000000L
1725 mac = EVP_MAC_fetch(NULL, "HMAC", NULL);
1726 ctx_h = EVP_MAC_CTX_new(mac);
1727 ctx_h = EVP_MAC_CTX_dup(sess->auth.hmac.ctx);
1729 status = process_openssl_auth_hmac(mbuf_src, dst,
1730 op->sym->auth.data.offset, srclen,
1733 ctx_h = HMAC_CTX_new();
1734 HMAC_CTX_copy(ctx_h, sess->auth.hmac.ctx);
1735 status = process_openssl_auth_hmac(mbuf_src, dst,
1736 op->sym->auth.data.offset, srclen,
1738 HMAC_CTX_free(ctx_h);
1746 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
1747 if (CRYPTO_memcmp(dst, op->sym->auth.digest.data,
1748 sess->auth.digest_length) != 0) {
1749 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1754 auth_dst = op->sym->auth.digest.data;
1755 if (auth_dst == NULL)
1756 auth_dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1757 op->sym->auth.data.offset +
1758 op->sym->auth.data.length);
1759 memcpy(auth_dst, dst, sess->auth.digest_length);
1763 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1766 /* process dsa sign operation */
1768 process_openssl_dsa_sign_op(struct rte_crypto_op *cop,
1769 struct openssl_asym_session *sess)
1771 struct rte_crypto_dsa_op_param *op = &cop->asym->dsa;
1772 DSA *dsa = sess->u.s.dsa;
1773 DSA_SIG *sign = NULL;
1775 sign = DSA_do_sign(op->message.data,
1780 OPENSSL_LOG(ERR, "%s:%d\n", __func__, __LINE__);
1781 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
1783 const BIGNUM *r = NULL, *s = NULL;
1784 get_dsa_sign(sign, &r, &s);
1786 op->r.length = BN_bn2bin(r, op->r.data);
1787 op->s.length = BN_bn2bin(s, op->s.data);
1788 cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1796 /* process dsa verify operation */
1798 process_openssl_dsa_verify_op(struct rte_crypto_op *cop,
1799 struct openssl_asym_session *sess)
1801 struct rte_crypto_dsa_op_param *op = &cop->asym->dsa;
1802 DSA *dsa = sess->u.s.dsa;
1804 DSA_SIG *sign = DSA_SIG_new();
1805 BIGNUM *r = NULL, *s = NULL;
1806 BIGNUM *pub_key = NULL;
1809 OPENSSL_LOG(ERR, " %s:%d\n", __func__, __LINE__);
1810 cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
1814 r = BN_bin2bn(op->r.data,
1817 s = BN_bin2bn(op->s.data,
1820 pub_key = BN_bin2bn(op->y.data,
1823 if (!r || !s || !pub_key) {
1828 cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
1831 set_dsa_sign(sign, r, s);
1832 set_dsa_pub_key(dsa, pub_key);
1834 ret = DSA_do_verify(op->message.data,
1840 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
1842 cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1849 /* process dh operation */
1850 #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
1852 process_openssl_dh_op_evp(struct rte_crypto_op *cop,
1853 struct openssl_asym_session *sess)
1855 struct rte_crypto_dh_op_param *op = &cop->asym->dh;
1856 OSSL_PARAM_BLD *param_bld = sess->u.dh.param_bld;
1857 OSSL_PARAM_BLD *param_bld_peer = sess->u.dh.param_bld_peer;
1858 OSSL_PARAM *params = NULL;
1859 EVP_PKEY *dhpkey = NULL;
1860 EVP_PKEY *peerkey = NULL;
1861 BIGNUM *priv_key = NULL;
1862 BIGNUM *pub_key = NULL;
1865 cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
1866 EVP_PKEY_CTX *dh_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DH, NULL);
1867 if (dh_ctx == NULL || param_bld == NULL)
1870 if (op->ke_type == RTE_CRYPTO_ASYM_KE_SHARED_SECRET_COMPUTE) {
1871 OSSL_PARAM *params_peer = NULL;
1873 if (!param_bld_peer)
1876 pub_key = BN_bin2bn(op->pub_key.data, op->pub_key.length,
1878 if (pub_key == NULL) {
1879 OSSL_PARAM_BLD_free(param_bld_peer);
1883 if (!OSSL_PARAM_BLD_push_BN(param_bld_peer, OSSL_PKEY_PARAM_PUB_KEY,
1885 OPENSSL_LOG(ERR, "Failed to set public key\n");
1886 OSSL_PARAM_BLD_free(param_bld_peer);
1891 params_peer = OSSL_PARAM_BLD_to_param(param_bld_peer);
1893 OSSL_PARAM_BLD_free(param_bld_peer);
1898 EVP_PKEY_CTX *peer_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DH, NULL);
1899 if (EVP_PKEY_keygen_init(peer_ctx) != 1) {
1900 OSSL_PARAM_free(params_peer);
1905 if (EVP_PKEY_CTX_set_params(peer_ctx, params_peer) != 1) {
1906 EVP_PKEY_CTX_free(peer_ctx);
1907 OSSL_PARAM_free(params_peer);
1912 if (EVP_PKEY_keygen(peer_ctx, &peerkey) != 1) {
1913 EVP_PKEY_CTX_free(peer_ctx);
1914 OSSL_PARAM_free(params_peer);
1919 priv_key = BN_bin2bn(op->priv_key.data, op->priv_key.length,
1921 if (priv_key == NULL) {
1922 EVP_PKEY_CTX_free(peer_ctx);
1923 OSSL_PARAM_free(params_peer);
1928 if (!OSSL_PARAM_BLD_push_BN(param_bld, OSSL_PKEY_PARAM_PRIV_KEY,
1930 OPENSSL_LOG(ERR, "Failed to set private key\n");
1931 EVP_PKEY_CTX_free(peer_ctx);
1932 OSSL_PARAM_free(params_peer);
1938 OSSL_PARAM_free(params_peer);
1939 EVP_PKEY_CTX_free(peer_ctx);
1942 params = OSSL_PARAM_BLD_to_param(param_bld);
1946 if (EVP_PKEY_keygen_init(dh_ctx) != 1)
1949 if (EVP_PKEY_CTX_set_params(dh_ctx, params) != 1)
1952 if (EVP_PKEY_keygen(dh_ctx, &dhpkey) != 1)
1955 if (op->ke_type == RTE_CRYPTO_ASYM_KE_PUB_KEY_GENERATE) {
1956 OPENSSL_LOG(DEBUG, "%s:%d updated pub key\n", __func__, __LINE__);
1957 if (!EVP_PKEY_get_bn_param(dhpkey, OSSL_PKEY_PARAM_PUB_KEY, &pub_key))
1959 /* output public key */
1960 op->pub_key.length = BN_bn2bin(pub_key, op->pub_key.data);
1963 if (op->ke_type == RTE_CRYPTO_ASYM_KE_PRIV_KEY_GENERATE) {
1965 OPENSSL_LOG(DEBUG, "%s:%d updated priv key\n", __func__, __LINE__);
1966 if (!EVP_PKEY_get_bn_param(dhpkey, OSSL_PKEY_PARAM_PRIV_KEY, &priv_key))
1969 /* provide generated private key back to user */
1970 op->priv_key.length = BN_bn2bin(priv_key, op->priv_key.data);
1973 if (op->ke_type == RTE_CRYPTO_ASYM_KE_SHARED_SECRET_COMPUTE) {
1975 EVP_PKEY_CTX *sc_ctx = EVP_PKEY_CTX_new(dhpkey, NULL);
1979 if (EVP_PKEY_derive_init(sc_ctx) <= 0) {
1980 EVP_PKEY_CTX_free(sc_ctx);
1985 EVP_PKEY_CTX_free(sc_ctx);
1989 if (EVP_PKEY_derive_set_peer(sc_ctx, peerkey) <= 0) {
1990 EVP_PKEY_CTX_free(sc_ctx);
1994 /* Determine buffer length */
1995 if (EVP_PKEY_derive(sc_ctx, NULL, &skey_len) <= 0) {
1996 EVP_PKEY_CTX_free(sc_ctx);
2000 if (EVP_PKEY_derive(sc_ctx, op->shared_secret.data, &skey_len) <= 0) {
2001 EVP_PKEY_CTX_free(sc_ctx);
2005 op->shared_secret.length = skey_len;
2006 EVP_PKEY_CTX_free(sc_ctx);
2009 cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
2018 OSSL_PARAM_free(params);
2020 EVP_PKEY_free(dhpkey);
2022 EVP_PKEY_free(peerkey);
2024 EVP_PKEY_CTX_free(dh_ctx);
2030 process_openssl_dh_op(struct rte_crypto_op *cop,
2031 struct openssl_asym_session *sess)
2033 struct rte_crypto_dh_op_param *op = &cop->asym->dh;
2034 struct rte_crypto_asym_op *asym_op = cop->asym;
2035 DH *dh_key = sess->u.dh.dh_key;
2036 BIGNUM *priv_key = NULL;
2039 if (asym_op->dh.ke_type == RTE_CRYPTO_ASYM_KE_SHARED_SECRET_COMPUTE) {
2040 /* compute shared secret using peer public key
2041 * and current private key
2042 * shared secret = peer_key ^ priv_key mod p
2044 BIGNUM *peer_key = NULL;
2046 /* copy private key and peer key and compute shared secret */
2047 peer_key = BN_bin2bn(op->pub_key.data,
2050 if (peer_key == NULL) {
2051 cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
2054 priv_key = BN_bin2bn(op->priv_key.data,
2055 op->priv_key.length,
2057 if (priv_key == NULL) {
2059 cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
2062 ret = set_dh_priv_key(dh_key, priv_key);
2064 OPENSSL_LOG(ERR, "Failed to set private key\n");
2065 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
2071 ret = DH_compute_key(
2072 op->shared_secret.data,
2075 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
2077 /* priv key is already loaded into dh,
2078 * let's not free that directly here.
2079 * DH_free() will auto free it later.
2083 cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
2084 op->shared_secret.length = ret;
2090 * other options are public and private key generations.
2092 * if user provides private key,
2093 * then first set DH with user provided private key
2095 if (asym_op->dh.ke_type == RTE_CRYPTO_ASYM_KE_PUB_KEY_GENERATE &&
2096 op->priv_key.length) {
2097 /* generate public key using user-provided private key
2098 * pub_key = g ^ priv_key mod p
2101 /* load private key into DH */
2102 priv_key = BN_bin2bn(op->priv_key.data,
2103 op->priv_key.length,
2105 if (priv_key == NULL) {
2106 cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
2109 ret = set_dh_priv_key(dh_key, priv_key);
2111 OPENSSL_LOG(ERR, "Failed to set private key\n");
2112 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
2118 /* generate public and private key pair.
2120 * if private key already set, generates only public key.
2122 * if private key is not already set, then set it to random value
2123 * and update internal private key.
2125 if (!DH_generate_key(dh_key)) {
2126 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
2130 if (asym_op->dh.ke_type == RTE_CRYPTO_ASYM_KE_PUB_KEY_GENERATE) {
2131 const BIGNUM *pub_key = NULL;
2133 OPENSSL_LOG(DEBUG, "%s:%d update public key\n",
2134 __func__, __LINE__);
2136 /* get the generated keys */
2137 get_dh_pub_key(dh_key, &pub_key);
2139 /* output public key */
2140 op->pub_key.length = BN_bn2bin(pub_key,
2144 if (asym_op->dh.ke_type == RTE_CRYPTO_ASYM_KE_PRIV_KEY_GENERATE) {
2145 const BIGNUM *priv_key = NULL;
2147 OPENSSL_LOG(DEBUG, "%s:%d updated priv key\n",
2148 __func__, __LINE__);
2150 /* get the generated keys */
2151 get_dh_priv_key(dh_key, &priv_key);
2153 /* provide generated private key back to user */
2154 op->priv_key.length = BN_bn2bin(priv_key,
2158 cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
2164 /* process modinv operation */
2166 process_openssl_modinv_op(struct rte_crypto_op *cop,
2167 struct openssl_asym_session *sess)
2169 struct rte_crypto_asym_op *op = cop->asym;
2170 BIGNUM *base = BN_CTX_get(sess->u.m.ctx);
2171 BIGNUM *res = BN_CTX_get(sess->u.m.ctx);
2173 if (unlikely(base == NULL || res == NULL)) {
2176 cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
2180 base = BN_bin2bn((const unsigned char *)op->modinv.base.data,
2181 op->modinv.base.length, base);
2183 if (BN_mod_inverse(res, base, sess->u.m.modulus, sess->u.m.ctx)) {
2184 cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
2185 op->modinv.result.length = BN_bn2bin(res, op->modinv.result.data);
2187 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
2196 /* process modexp operation */
2198 process_openssl_modexp_op(struct rte_crypto_op *cop,
2199 struct openssl_asym_session *sess)
2201 struct rte_crypto_asym_op *op = cop->asym;
2202 BIGNUM *base = BN_CTX_get(sess->u.e.ctx);
2203 BIGNUM *res = BN_CTX_get(sess->u.e.ctx);
2205 if (unlikely(base == NULL || res == NULL)) {
2208 cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
2212 base = BN_bin2bn((const unsigned char *)op->modex.base.data,
2213 op->modex.base.length, base);
2215 if (BN_mod_exp(res, base, sess->u.e.exp,
2216 sess->u.e.mod, sess->u.e.ctx)) {
2217 op->modex.result.length = BN_bn2bin(res, op->modex.result.data);
2218 cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
2220 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
2229 /* process rsa operations */
2230 #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
2232 process_openssl_rsa_op_evp(struct rte_crypto_op *cop,
2233 struct openssl_asym_session *sess)
2235 struct rte_crypto_asym_op *op = cop->asym;
2236 uint32_t pad = (op->rsa.padding.type);
2241 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
2242 EVP_PKEY_CTX *rsa_ctx = sess->u.r.ctx;
2247 case RTE_CRYPTO_RSA_PADDING_PKCS1_5:
2248 pad = RSA_PKCS1_PADDING;
2250 case RTE_CRYPTO_RSA_PADDING_NONE:
2251 pad = RSA_NO_PADDING;
2254 cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
2256 "rsa pad type not supported %d\n", pad);
2260 switch (op->rsa.op_type) {
2261 case RTE_CRYPTO_ASYM_OP_ENCRYPT:
2262 if (EVP_PKEY_encrypt_init(rsa_ctx) != 1)
2265 if (EVP_PKEY_CTX_set_rsa_padding(rsa_ctx, pad) <= 0)
2268 if (EVP_PKEY_encrypt(rsa_ctx, NULL, &outlen,
2269 op->rsa.message.data,
2270 op->rsa.message.length) <= 0)
2276 if (EVP_PKEY_encrypt(rsa_ctx, op->rsa.cipher.data, &outlen,
2277 op->rsa.message.data,
2278 op->rsa.message.length) <= 0)
2280 op->rsa.cipher.length = outlen;
2283 "length of encrypted text %zu\n", outlen);
2286 case RTE_CRYPTO_ASYM_OP_DECRYPT:
2287 if (EVP_PKEY_decrypt_init(rsa_ctx) != 1)
2290 if (EVP_PKEY_CTX_set_rsa_padding(rsa_ctx, pad) <= 0)
2293 if (EVP_PKEY_decrypt(rsa_ctx, NULL, &outlen,
2294 op->rsa.cipher.data,
2295 op->rsa.cipher.length) <= 0)
2301 if (EVP_PKEY_decrypt(rsa_ctx, op->rsa.message.data, &outlen,
2302 op->rsa.cipher.data,
2303 op->rsa.cipher.length) <= 0)
2305 op->rsa.message.length = outlen;
2307 OPENSSL_LOG(DEBUG, "length of decrypted text %zu\n", outlen);
2310 case RTE_CRYPTO_ASYM_OP_SIGN:
2311 if (EVP_PKEY_sign_init(rsa_ctx) <= 0)
2314 if (EVP_PKEY_CTX_set_rsa_padding(rsa_ctx, pad) <= 0)
2317 if (EVP_PKEY_sign(rsa_ctx, op->rsa.sign.data, &outlen,
2318 op->rsa.message.data,
2319 op->rsa.message.length) <= 0)
2321 op->rsa.sign.length = outlen;
2324 case RTE_CRYPTO_ASYM_OP_VERIFY:
2325 tmp = rte_malloc(NULL, op->rsa.sign.length, 0);
2327 OPENSSL_LOG(ERR, "Memory allocation failed");
2331 if (EVP_PKEY_verify_recover_init(rsa_ctx) <= 0) {
2336 if (EVP_PKEY_CTX_set_rsa_padding(rsa_ctx, pad) <= 0) {
2341 if (EVP_PKEY_verify_recover(rsa_ctx, tmp, &outlen,
2343 op->rsa.sign.length) <= 0) {
2349 "Length of public_decrypt %zu "
2350 "length of message %zd\n",
2351 outlen, op->rsa.message.length);
2352 if (CRYPTO_memcmp(tmp, op->rsa.message.data,
2353 op->rsa.message.length)) {
2354 OPENSSL_LOG(ERR, "RSA sign Verification failed");
2360 /* allow ops with invalid args to be pushed to
2363 cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
2368 cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
2375 process_openssl_rsa_op(struct rte_crypto_op *cop,
2376 struct openssl_asym_session *sess)
2379 struct rte_crypto_asym_op *op = cop->asym;
2380 RSA *rsa = sess->u.r.rsa;
2381 uint32_t pad = (op->rsa.padding.type);
2384 cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
2387 case RTE_CRYPTO_RSA_PADDING_PKCS1_5:
2388 pad = RSA_PKCS1_PADDING;
2390 case RTE_CRYPTO_RSA_PADDING_NONE:
2391 pad = RSA_NO_PADDING;
2394 cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
2396 "rsa pad type not supported %d\n", pad);
2400 switch (op->rsa.op_type) {
2401 case RTE_CRYPTO_ASYM_OP_ENCRYPT:
2402 ret = RSA_public_encrypt(op->rsa.message.length,
2403 op->rsa.message.data,
2404 op->rsa.cipher.data,
2409 op->rsa.cipher.length = ret;
2411 "length of encrypted text %d\n", ret);
2414 case RTE_CRYPTO_ASYM_OP_DECRYPT:
2415 ret = RSA_private_decrypt(op->rsa.cipher.length,
2416 op->rsa.cipher.data,
2417 op->rsa.message.data,
2421 op->rsa.message.length = ret;
2424 case RTE_CRYPTO_ASYM_OP_SIGN:
2425 ret = RSA_private_encrypt(op->rsa.message.length,
2426 op->rsa.message.data,
2431 op->rsa.sign.length = ret;
2434 case RTE_CRYPTO_ASYM_OP_VERIFY:
2435 tmp = rte_malloc(NULL, op->rsa.sign.length, 0);
2437 OPENSSL_LOG(ERR, "Memory allocation failed");
2438 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
2441 ret = RSA_public_decrypt(op->rsa.sign.length,
2448 "Length of public_decrypt %d "
2449 "length of message %zd\n",
2450 ret, op->rsa.message.length);
2451 if ((ret <= 0) || (CRYPTO_memcmp(tmp, op->rsa.message.data,
2452 op->rsa.message.length))) {
2453 OPENSSL_LOG(ERR, "RSA sign Verification failed");
2454 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
2460 /* allow ops with invalid args to be pushed to
2463 cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
2468 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
2475 process_asym_op(struct openssl_qp *qp, struct rte_crypto_op *op,
2476 struct openssl_asym_session *sess)
2480 op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
2482 switch (sess->xfrm_type) {
2483 case RTE_CRYPTO_ASYM_XFORM_RSA:
2484 #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
2485 retval = process_openssl_rsa_op_evp(op, sess);
2487 retval = process_openssl_rsa_op(op, sess);
2490 case RTE_CRYPTO_ASYM_XFORM_MODEX:
2491 retval = process_openssl_modexp_op(op, sess);
2493 case RTE_CRYPTO_ASYM_XFORM_MODINV:
2494 retval = process_openssl_modinv_op(op, sess);
2496 case RTE_CRYPTO_ASYM_XFORM_DH:
2497 #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
2498 retval = process_openssl_dh_op_evp(op, sess);
2500 retval = process_openssl_dh_op(op, sess);
2503 case RTE_CRYPTO_ASYM_XFORM_DSA:
2504 if (op->asym->dsa.op_type == RTE_CRYPTO_ASYM_OP_SIGN)
2505 retval = process_openssl_dsa_sign_op(op, sess);
2506 else if (op->asym->dsa.op_type ==
2507 RTE_CRYPTO_ASYM_OP_VERIFY)
2509 process_openssl_dsa_verify_op(op, sess);
2511 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
2514 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
2518 /* op processed so push to completion queue as processed */
2519 retval = rte_ring_enqueue(qp->processed_ops, (void *)op);
2521 /* return error if failed to put in completion queue */
2529 copy_plaintext(struct rte_mbuf *m_src, struct rte_mbuf *m_dst,
2530 struct rte_crypto_op *op)
2532 uint8_t *p_src, *p_dst;
2534 p_src = rte_pktmbuf_mtod(m_src, uint8_t *);
2535 p_dst = rte_pktmbuf_mtod(m_dst, uint8_t *);
2538 * Copy the content between cipher offset and auth offset
2539 * for generating correct digest.
2541 if (op->sym->cipher.data.offset > op->sym->auth.data.offset)
2542 memcpy(p_dst + op->sym->auth.data.offset,
2543 p_src + op->sym->auth.data.offset,
2544 op->sym->cipher.data.offset -
2545 op->sym->auth.data.offset);
2548 /** Process crypto operation for mbuf */
2550 process_op(struct openssl_qp *qp, struct rte_crypto_op *op,
2551 struct openssl_session *sess)
2553 struct rte_mbuf *msrc, *mdst;
2556 msrc = op->sym->m_src;
2557 mdst = op->sym->m_dst ? op->sym->m_dst : op->sym->m_src;
2559 op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
2561 switch (sess->chain_order) {
2562 case OPENSSL_CHAIN_ONLY_CIPHER:
2563 process_openssl_cipher_op(op, sess, msrc, mdst);
2565 case OPENSSL_CHAIN_ONLY_AUTH:
2566 process_openssl_auth_op(qp, op, sess, msrc, mdst);
2568 case OPENSSL_CHAIN_CIPHER_AUTH:
2569 process_openssl_cipher_op(op, sess, msrc, mdst);
2572 copy_plaintext(msrc, mdst, op);
2573 process_openssl_auth_op(qp, op, sess, mdst, mdst);
2575 case OPENSSL_CHAIN_AUTH_CIPHER:
2576 process_openssl_auth_op(qp, op, sess, msrc, mdst);
2577 process_openssl_cipher_op(op, sess, msrc, mdst);
2579 case OPENSSL_CHAIN_COMBINED:
2580 process_openssl_combined_op(op, sess, msrc, mdst);
2582 case OPENSSL_CHAIN_CIPHER_BPI:
2583 process_openssl_docsis_bpi_op(op, sess, msrc, mdst);
2586 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
2590 /* Free session if a session-less crypto op */
2591 if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
2592 openssl_reset_session(sess);
2593 memset(sess, 0, sizeof(struct openssl_session));
2594 memset(op->sym->session, 0,
2595 rte_cryptodev_sym_get_existing_header_session_size(
2597 rte_mempool_put(qp->sess_mp_priv, sess);
2598 rte_mempool_put(qp->sess_mp, op->sym->session);
2599 op->sym->session = NULL;
2602 if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
2603 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
2605 if (op->status != RTE_CRYPTO_OP_STATUS_ERROR)
2606 retval = rte_ring_enqueue(qp->processed_ops, (void *)op);
2614 *------------------------------------------------------------------------------
2616 *------------------------------------------------------------------------------
2619 /** Enqueue burst */
2621 openssl_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
2625 struct openssl_qp *qp = queue_pair;
2628 for (i = 0; i < nb_ops; i++) {
2629 sess = get_session(qp, ops[i]);
2630 if (unlikely(sess == NULL))
2633 if (ops[i]->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC)
2634 retval = process_op(qp, ops[i],
2635 (struct openssl_session *) sess);
2637 retval = process_asym_op(qp, ops[i],
2638 (struct openssl_asym_session *) sess);
2639 if (unlikely(retval < 0))
2643 qp->stats.enqueued_count += i;
2647 qp->stats.enqueue_err_count++;
2651 /** Dequeue burst */
2653 openssl_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
2656 struct openssl_qp *qp = queue_pair;
2658 unsigned int nb_dequeued = 0;
2660 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
2661 (void **)ops, nb_ops, NULL);
2662 qp->stats.dequeued_count += nb_dequeued;
2667 /** Create OPENSSL crypto device */
2669 cryptodev_openssl_create(const char *name,
2670 struct rte_vdev_device *vdev,
2671 struct rte_cryptodev_pmd_init_params *init_params)
2673 struct rte_cryptodev *dev;
2674 struct openssl_private *internals;
2676 dev = rte_cryptodev_pmd_create(name, &vdev->device, init_params);
2678 OPENSSL_LOG(ERR, "failed to create cryptodev vdev");
2682 dev->driver_id = cryptodev_driver_id;
2683 dev->dev_ops = rte_openssl_pmd_ops;
2685 /* register rx/tx burst functions for data path */
2686 dev->dequeue_burst = openssl_pmd_dequeue_burst;
2687 dev->enqueue_burst = openssl_pmd_enqueue_burst;
2689 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
2690 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
2691 RTE_CRYPTODEV_FF_CPU_AESNI |
2692 RTE_CRYPTODEV_FF_IN_PLACE_SGL |
2693 RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
2694 RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT |
2695 RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO |
2696 RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_EXP |
2697 RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT |
2698 RTE_CRYPTODEV_FF_SYM_SESSIONLESS;
2700 internals = dev->data->dev_private;
2702 internals->max_nb_qpairs = init_params->max_nb_queue_pairs;
2704 rte_cryptodev_pmd_probing_finish(dev);
2706 # if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
2707 /* Load legacy provider
2708 * Some algorithms are no longer available in earlier version of openssl,
2709 * unless the legacy provider explicitly loaded. e.g. DES
2711 ossl_legacy_provider_load();
2716 OPENSSL_LOG(ERR, "driver %s: create failed",
2719 cryptodev_openssl_remove(vdev);
2723 /** Initialise OPENSSL crypto device */
2725 cryptodev_openssl_probe(struct rte_vdev_device *vdev)
2727 struct rte_cryptodev_pmd_init_params init_params = {
2729 sizeof(struct openssl_private),
2731 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS
2734 const char *input_args;
2736 name = rte_vdev_device_name(vdev);
2739 input_args = rte_vdev_device_args(vdev);
2741 rte_cryptodev_pmd_parse_input_args(&init_params, input_args);
2743 return cryptodev_openssl_create(name, vdev, &init_params);
2746 /** Uninitialise OPENSSL crypto device */
2748 cryptodev_openssl_remove(struct rte_vdev_device *vdev)
2750 struct rte_cryptodev *cryptodev;
2753 name = rte_vdev_device_name(vdev);
2757 cryptodev = rte_cryptodev_pmd_get_named_dev(name);
2758 if (cryptodev == NULL)
2761 # if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
2762 ossl_legacy_provider_unload();
2764 return rte_cryptodev_pmd_destroy(cryptodev);
2767 static struct rte_vdev_driver cryptodev_openssl_pmd_drv = {
2768 .probe = cryptodev_openssl_probe,
2769 .remove = cryptodev_openssl_remove
2772 static struct cryptodev_driver openssl_crypto_drv;
2774 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_OPENSSL_PMD,
2775 cryptodev_openssl_pmd_drv);
2776 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_OPENSSL_PMD,
2777 "max_nb_queue_pairs=<int> "
2779 RTE_PMD_REGISTER_CRYPTO_DRIVER(openssl_crypto_drv,
2780 cryptodev_openssl_pmd_drv.driver, cryptodev_driver_id);
2781 RTE_LOG_REGISTER_DEFAULT(openssl_logtype_driver, INFO);