LCOV - code coverage report
Current view: top level - drivers/crypto/openssl - rte_openssl_pmd.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 1041 1451 71.7 %
Date: 2025-02-01 18:54:23 Functions: 52 54 96.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 514 887 57.9 %

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

Generated by: LCOV version 1.14