LCOV - code coverage report
Current view: top level - drivers/crypto/openssl - rte_openssl_pmd.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 1136 1565 72.6 %
Date: 2026-11-01 19:11:22 Functions: 57 59 96.6 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 578 979 59.0 %

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

Generated by: LCOV version 1.14