LCOV - code coverage report
Current view: top level - drivers/crypto/openssl - rte_openssl_pmd.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 1083 1492 72.6 %
Date: 2026-07-01 18:02:41 Functions: 52 54 96.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 535 910 58.8 %

           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                 :        334 : 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         [ +  + ]:        334 :         if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
     925         [ +  + ]:        324 :                 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         [ +  - ]:         40 :                         if (likely(op->asym->session != NULL))
     932                 :         40 :                                 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                 :         40 :                         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                 :            : /* process rsa operations */
    2296                 :            : static int
    2297                 :         14 : process_openssl_rsa_op_evp(struct rte_crypto_op *cop,
    2298                 :            :                 struct openssl_asym_session *sess)
    2299                 :            : {
    2300                 :            :         struct rte_crypto_asym_op *op = cop->asym;
    2301                 :         14 :         uint32_t pad = sess->u.r.pad;
    2302                 :            :         uint8_t *tmp;
    2303                 :         14 :         size_t outlen = 0;
    2304                 :            :         int ret = -1;
    2305                 :            : 
    2306                 :         14 :         cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    2307                 :         14 :         EVP_PKEY_CTX *rsa_ctx = sess->u.r.ctx;
    2308         [ +  - ]:         14 :         if (!rsa_ctx)
    2309                 :            :                 return ret;
    2310                 :            : 
    2311      [ +  -  + ]:         14 :         switch (pad) {
    2312                 :            :         case RTE_CRYPTO_RSA_PADDING_PKCS1_5:
    2313                 :            :                 pad = RSA_PKCS1_PADDING;
    2314                 :            :                 break;
    2315                 :          4 :         case RTE_CRYPTO_RSA_PADDING_NONE:
    2316                 :            :                 pad = RSA_NO_PADDING;
    2317                 :          4 :                 break;
    2318                 :          0 :         default:
    2319                 :          0 :                 cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
    2320                 :          0 :                 OPENSSL_LOG(ERR,
    2321                 :            :                                 "rsa pad type not supported %d", pad);
    2322                 :          0 :                 return ret;
    2323                 :            :         }
    2324                 :            : 
    2325   [ +  +  +  +  :         14 :         switch (op->rsa.op_type) {
                      - ]
    2326                 :          4 :         case RTE_CRYPTO_ASYM_OP_ENCRYPT:
    2327         [ -  + ]:          4 :                 if (EVP_PKEY_encrypt_init(rsa_ctx) != 1)
    2328                 :          0 :                         goto err_rsa;
    2329                 :            : 
    2330         [ -  + ]:          4 :                 if (EVP_PKEY_CTX_set_rsa_padding(rsa_ctx, pad) <= 0)
    2331                 :          0 :                         goto err_rsa;
    2332                 :            : 
    2333         [ -  + ]:          4 :                 if (EVP_PKEY_encrypt(rsa_ctx, NULL, &outlen,
    2334                 :          4 :                                 op->rsa.message.data,
    2335                 :            :                                 op->rsa.message.length) <= 0)
    2336                 :          0 :                         goto err_rsa;
    2337                 :            : 
    2338         [ -  + ]:          4 :                 if (outlen <= 0)
    2339                 :          0 :                         goto err_rsa;
    2340                 :            : 
    2341         [ -  + ]:          4 :                 if (EVP_PKEY_encrypt(rsa_ctx, op->rsa.cipher.data, &outlen,
    2342                 :          4 :                                 op->rsa.message.data,
    2343                 :            :                                 op->rsa.message.length) <= 0)
    2344                 :          0 :                         goto err_rsa;
    2345                 :          4 :                 op->rsa.cipher.length = outlen;
    2346                 :            : 
    2347                 :          4 :                 OPENSSL_LOG(DEBUG,
    2348                 :            :                                 "length of encrypted text %zu", outlen);
    2349                 :          4 :                 break;
    2350                 :            : 
    2351                 :          4 :         case RTE_CRYPTO_ASYM_OP_DECRYPT:
    2352         [ -  + ]:          4 :                 if (EVP_PKEY_decrypt_init(rsa_ctx) != 1)
    2353                 :          0 :                         goto err_rsa;
    2354                 :            : 
    2355         [ -  + ]:          4 :                 if (EVP_PKEY_CTX_set_rsa_padding(rsa_ctx, pad) <= 0)
    2356                 :          0 :                         goto err_rsa;
    2357                 :            : 
    2358         [ -  + ]:          4 :                 if (EVP_PKEY_decrypt(rsa_ctx, NULL, &outlen,
    2359                 :          4 :                                 op->rsa.cipher.data,
    2360                 :            :                                 op->rsa.cipher.length) <= 0)
    2361                 :          0 :                         goto err_rsa;
    2362                 :            : 
    2363         [ -  + ]:          4 :                 if (outlen <= 0)
    2364                 :          0 :                         goto err_rsa;
    2365                 :            : 
    2366         [ -  + ]:          4 :                 if (EVP_PKEY_decrypt(rsa_ctx, op->rsa.message.data, &outlen,
    2367                 :          4 :                                 op->rsa.cipher.data,
    2368                 :            :                                 op->rsa.cipher.length) <= 0)
    2369                 :          0 :                         goto err_rsa;
    2370                 :          4 :                 op->rsa.message.length = outlen;
    2371                 :            : 
    2372                 :          4 :                 OPENSSL_LOG(DEBUG, "length of decrypted text %zu", outlen);
    2373                 :          4 :                 break;
    2374                 :            : 
    2375                 :          2 :         case RTE_CRYPTO_ASYM_OP_SIGN:
    2376         [ -  + ]:          2 :                 if (EVP_PKEY_sign_init(rsa_ctx) <= 0)
    2377                 :          0 :                         goto err_rsa;
    2378                 :            : 
    2379         [ -  + ]:          2 :                 if (EVP_PKEY_CTX_set_rsa_padding(rsa_ctx, pad) <= 0)
    2380                 :          0 :                         goto err_rsa;
    2381                 :            : 
    2382         [ -  + ]:          2 :                 if (EVP_PKEY_sign(rsa_ctx, NULL, &outlen,
    2383                 :          2 :                                 op->rsa.message.data,
    2384                 :            :                                 op->rsa.message.length) <= 0)
    2385                 :          0 :                         goto err_rsa;
    2386                 :            : 
    2387         [ -  + ]:          2 :                 if (outlen <= 0)
    2388                 :          0 :                         goto err_rsa;
    2389                 :            : 
    2390         [ -  + ]:          2 :                 if (EVP_PKEY_sign(rsa_ctx, op->rsa.sign.data, &outlen,
    2391                 :          2 :                                 op->rsa.message.data,
    2392                 :            :                                 op->rsa.message.length) <= 0)
    2393                 :          0 :                         goto err_rsa;
    2394                 :          2 :                 op->rsa.sign.length = outlen;
    2395                 :          2 :                 break;
    2396                 :            : 
    2397                 :          4 :         case RTE_CRYPTO_ASYM_OP_VERIFY:
    2398         [ -  + ]:          4 :                 if (EVP_PKEY_verify_recover_init(rsa_ctx) <= 0)
    2399                 :          0 :                         goto err_rsa;
    2400                 :            : 
    2401         [ -  + ]:          4 :                 if (EVP_PKEY_CTX_set_rsa_padding(rsa_ctx, pad) <= 0)
    2402                 :          0 :                         goto err_rsa;
    2403                 :            : 
    2404         [ -  + ]:          4 :                 if (EVP_PKEY_verify_recover(rsa_ctx, NULL, &outlen,
    2405                 :          4 :                                 op->rsa.sign.data,
    2406                 :            :                                 op->rsa.sign.length) <= 0)
    2407                 :          0 :                         goto err_rsa;
    2408                 :            : 
    2409   [ +  -  -  + ]:          4 :                 if ((outlen <= 0) || (outlen != op->rsa.sign.length))
    2410                 :          0 :                         goto err_rsa;
    2411                 :            : 
    2412                 :          4 :                 tmp = OPENSSL_malloc(outlen);
    2413         [ -  + ]:          4 :                 if (tmp == NULL) {
    2414                 :          0 :                         OPENSSL_LOG(ERR, "Memory allocation failed");
    2415                 :          0 :                         goto err_rsa;
    2416                 :            :                 }
    2417                 :            : 
    2418                 :          4 :                 ret = EVP_PKEY_verify_recover(rsa_ctx, tmp, &outlen,
    2419                 :          4 :                                 op->rsa.sign.data,
    2420                 :            :                                 op->rsa.sign.length);
    2421         [ +  + ]:          4 :                 if (ret <= 0) {
    2422                 :            :                         /* OpenSSL RSA verification returns one on
    2423                 :            :                          * successful verification, otherwise 0. Hence,
    2424                 :            :                          * this enqueue operation should succeed even if
    2425                 :            :                          * invalid signature has been requested in verify.
    2426                 :            :                          */
    2427                 :          2 :                         OPENSSL_free(tmp);
    2428                 :          2 :                         goto err_rsa;
    2429                 :            :                 }
    2430                 :            : 
    2431                 :          2 :                 OPENSSL_LOG(DEBUG,
    2432                 :            :                                 "Length of public_decrypt %zu "
    2433                 :            :                                 "length of message %zd",
    2434                 :            :                                 outlen, op->rsa.message.length);
    2435         [ -  + ]:          2 :                 if (CRYPTO_memcmp(tmp, op->rsa.message.data,
    2436                 :            :                                 op->rsa.message.length)) {
    2437                 :          0 :                         OPENSSL_LOG(ERR, "RSA sign Verification failed");
    2438                 :            :                 }
    2439                 :          2 :                 OPENSSL_free(tmp);
    2440                 :          2 :                 break;
    2441                 :            : 
    2442                 :          0 :         default:
    2443                 :            :                 /* allow ops with invalid args to be pushed to
    2444                 :            :                  * completion queue
    2445                 :            :                  */
    2446                 :          0 :                 cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
    2447                 :          0 :                 goto err_rsa;
    2448                 :            :         }
    2449                 :            : 
    2450                 :            :         ret = 0;
    2451                 :         12 :         cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
    2452                 :            : err_rsa:
    2453                 :            :         return ret;
    2454                 :            : 
    2455                 :            : }
    2456                 :            : 
    2457                 :            : static int
    2458                 :          0 : process_openssl_ecfpm_op_evp(struct rte_crypto_op *cop,
    2459                 :            :                 struct openssl_asym_session *sess)
    2460                 :            : {
    2461                 :          0 :         const EC_GROUP *ecgrp = sess->u.ec.group;
    2462                 :            :         EC_POINT *ecpt = NULL;
    2463                 :            :         BN_CTX *ctx = NULL;
    2464                 :            :         BIGNUM *n = NULL;
    2465                 :            :         int ret = -1;
    2466                 :            : 
    2467                 :          0 :         n = BN_bin2bn((const unsigned char *)
    2468                 :          0 :                         cop->asym->ecpm.scalar.data,
    2469                 :          0 :                         cop->asym->ecpm.scalar.length,
    2470                 :            :                         BN_new());
    2471                 :            : 
    2472                 :          0 :         ctx = BN_CTX_new();
    2473         [ #  # ]:          0 :         if (!ctx)
    2474                 :          0 :                 goto err_ecfpm;
    2475                 :            : 
    2476         [ #  # ]:          0 :         if (!EC_POINT_mul(ecgrp, ecpt, n, NULL, NULL, ctx))
    2477                 :          0 :                 goto err_ecfpm;
    2478                 :            : 
    2479         [ #  # ]:          0 :         if (cop->asym->flags & RTE_CRYPTO_ASYM_FLAG_PUB_KEY_COMPRESSED) {
    2480                 :          0 :                 unsigned char *buf = cop->asym->ecpm.r.x.data;
    2481                 :            :                 size_t sz;
    2482                 :            : 
    2483                 :          0 :                 sz = EC_POINT_point2oct(ecgrp, ecpt, POINT_CONVERSION_COMPRESSED, buf, 0, ctx);
    2484         [ #  # ]:          0 :                 if (!sz)
    2485                 :          0 :                         goto err_ecfpm;
    2486                 :            : 
    2487                 :          0 :                 cop->asym->ecpm.r.x.length = sz;
    2488                 :            :         }
    2489                 :            : 
    2490                 :          0 : err_ecfpm:
    2491                 :          0 :         BN_CTX_free(ctx);
    2492                 :          0 :         BN_free(n);
    2493                 :          0 :         return ret;
    2494                 :            : }
    2495                 :            : 
    2496                 :            : static int
    2497                 :          6 : process_openssl_sm2_op_evp(struct rte_crypto_op *cop,
    2498                 :            :                 struct openssl_asym_session *sess)
    2499                 :            : {
    2500                 :            :         EVP_PKEY_CTX *kctx = NULL, *sctx = NULL, *cctx = NULL;
    2501                 :            :         struct rte_crypto_asym_op *op = cop->asym;
    2502                 :          6 :         OSSL_PARAM *params = sess->u.sm2.params;
    2503                 :            :         EVP_MD_CTX *md_ctx = NULL;
    2504                 :            :         ECDSA_SIG *ec_sign = NULL;
    2505                 :            :         EVP_MD *check_md = NULL;
    2506                 :          6 :         EVP_PKEY *pkey = NULL;
    2507                 :            :         int ret = -1;
    2508                 :            : 
    2509                 :          6 :         cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    2510                 :            : 
    2511         [ -  + ]:          6 :         if (cop->asym->sm2.k.data != NULL)
    2512                 :          0 :                 goto err_sm2;
    2513                 :            : 
    2514   [ +  +  +  +  :          6 :         switch (op->sm2.op_type) {
                      - ]
    2515                 :          1 :         case RTE_CRYPTO_ASYM_OP_ENCRYPT:
    2516                 :            :                 {
    2517                 :            :                         OSSL_PARAM *eparams = sess->u.sm2.params;
    2518                 :          1 :                         size_t output_len = 0;
    2519                 :            : 
    2520                 :          1 :                         kctx = EVP_PKEY_CTX_new_id(EVP_PKEY_SM2, NULL);
    2521   [ +  -  +  -  :          2 :                         if (kctx == NULL || EVP_PKEY_fromdata_init(kctx) <= 0 ||
                   -  + ]
    2522                 :          1 :                                 EVP_PKEY_fromdata(kctx, &pkey, EVP_PKEY_KEYPAIR, params) <= 0)
    2523                 :          0 :                                 goto err_sm2;
    2524                 :            : 
    2525                 :          1 :                         cctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
    2526         [ -  + ]:          1 :                         if (!cctx)
    2527                 :          0 :                                 goto err_sm2;
    2528                 :            : 
    2529         [ -  + ]:          1 :                         if (!EVP_PKEY_encrypt_init(cctx))
    2530                 :          0 :                                 goto err_sm2;
    2531                 :            : 
    2532         [ -  + ]:          1 :                         if (!EVP_PKEY_CTX_set_params(cctx, eparams))
    2533                 :          0 :                                 goto err_sm2;
    2534                 :            : 
    2535         [ -  + ]:          1 :                         if (!EVP_PKEY_encrypt(cctx, op->sm2.cipher.data, &output_len,
    2536                 :          1 :                                                                  op->sm2.message.data,
    2537                 :            :                                                                  op->sm2.message.length))
    2538                 :          0 :                                 goto err_sm2;
    2539                 :          1 :                         op->sm2.cipher.length = output_len;
    2540                 :            :                 }
    2541                 :          1 :                 break;
    2542                 :          2 :         case RTE_CRYPTO_ASYM_OP_DECRYPT:
    2543                 :            :                 {
    2544                 :            :                         OSSL_PARAM *eparams = sess->u.sm2.params;
    2545                 :            : 
    2546                 :          2 :                         kctx = EVP_PKEY_CTX_new_id(EVP_PKEY_SM2, NULL);
    2547         [ +  - ]:          2 :                         if (kctx == NULL
    2548         [ +  - ]:          2 :                                 || EVP_PKEY_fromdata_init(kctx) <= 0
    2549         [ -  + ]:          2 :                                 || EVP_PKEY_fromdata(kctx, &pkey, EVP_PKEY_KEYPAIR, params) <= 0)
    2550                 :          0 :                                 goto err_sm2;
    2551                 :            : 
    2552                 :          2 :                         cctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
    2553         [ -  + ]:          2 :                         if (!cctx)
    2554                 :          0 :                                 goto err_sm2;
    2555                 :            : 
    2556         [ -  + ]:          2 :                         if (!EVP_PKEY_decrypt_init(cctx))
    2557                 :          0 :                                 goto err_sm2;
    2558                 :            : 
    2559         [ -  + ]:          2 :                         if (!EVP_PKEY_CTX_set_params(cctx, eparams))
    2560                 :          0 :                                 goto err_sm2;
    2561                 :            : 
    2562         [ -  + ]:          2 :                         if (!EVP_PKEY_decrypt(cctx, op->sm2.message.data, &op->sm2.message.length,
    2563                 :          2 :                                         op->sm2.cipher.data, op->sm2.cipher.length))
    2564                 :          0 :                                 goto err_sm2;
    2565                 :            :                 }
    2566                 :            :                 break;
    2567                 :          1 :         case RTE_CRYPTO_ASYM_OP_SIGN:
    2568                 :            :                 {
    2569                 :          1 :                         unsigned char signbuf[128] = {0};
    2570                 :            :                         const unsigned char *signptr;
    2571                 :            :                         const BIGNUM *r, *s;
    2572                 :            :                         size_t signlen;
    2573                 :            : 
    2574                 :          1 :                         kctx = EVP_PKEY_CTX_new_from_name(NULL, "SM2", NULL);
    2575   [ +  -  +  -  :          2 :                         if (kctx == NULL || EVP_PKEY_fromdata_init(kctx) <= 0 ||
                   -  + ]
    2576                 :          1 :                                 EVP_PKEY_fromdata(kctx, &pkey, EVP_PKEY_KEYPAIR, params) <= 0)
    2577                 :          0 :                                 goto err_sm2;
    2578                 :            : 
    2579                 :          1 :                         md_ctx = EVP_MD_CTX_new();
    2580         [ -  + ]:          1 :                         if (!md_ctx)
    2581                 :          0 :                                 goto err_sm2;
    2582                 :            : 
    2583                 :          1 :                         sctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
    2584         [ -  + ]:          1 :                         if (!sctx)
    2585                 :          0 :                                 goto err_sm2;
    2586                 :            : 
    2587                 :          1 :                         EVP_MD_CTX_set_pkey_ctx(md_ctx, sctx);
    2588                 :            : 
    2589                 :          1 :                         check_md = EVP_MD_fetch(NULL, "sm3", NULL);
    2590         [ -  + ]:          1 :                         if (!check_md)
    2591                 :          0 :                                 goto err_sm2;
    2592                 :            : 
    2593         [ -  + ]:          1 :                         if (!EVP_DigestSignInit(md_ctx, NULL, check_md, NULL, pkey))
    2594                 :          0 :                                 goto err_sm2;
    2595                 :            : 
    2596         [ -  + ]:          1 :                         if (EVP_PKEY_CTX_set1_id(sctx, op->sm2.id.data, op->sm2.id.length) <= 0)
    2597                 :          0 :                                 goto err_sm2;
    2598                 :            : 
    2599         [ -  + ]:          1 :                         if (!EVP_DigestSignUpdate(md_ctx, op->sm2.message.data,
    2600                 :            :                                         op->sm2.message.length))
    2601                 :          0 :                                 goto err_sm2;
    2602                 :            : 
    2603         [ -  + ]:          1 :                         if (!EVP_DigestSignFinal(md_ctx, NULL, &signlen))
    2604                 :          0 :                                 goto err_sm2;
    2605                 :            : 
    2606         [ -  + ]:          1 :                         if (!EVP_DigestSignFinal(md_ctx, signbuf, &signlen))
    2607                 :          0 :                                 goto err_sm2;
    2608                 :            : 
    2609                 :          1 :                         signptr = signbuf;
    2610                 :          1 :                         ec_sign = d2i_ECDSA_SIG(NULL, &signptr, signlen);
    2611         [ -  + ]:          1 :                         if (!ec_sign)
    2612                 :          0 :                                 goto err_sm2;
    2613                 :            : 
    2614                 :          1 :                         r = ECDSA_SIG_get0_r(ec_sign);
    2615                 :          1 :                         s = ECDSA_SIG_get0_s(ec_sign);
    2616         [ -  + ]:          1 :                         if (!r || !s)
    2617                 :          0 :                                 goto err_sm2;
    2618                 :            : 
    2619                 :          1 :                         op->sm2.r.length = BN_num_bytes(r);
    2620                 :          1 :                         op->sm2.s.length = BN_num_bytes(s);
    2621                 :          1 :                         BN_bn2bin(r, op->sm2.r.data);
    2622                 :          1 :                         BN_bn2bin(s, op->sm2.s.data);
    2623                 :            : 
    2624                 :          1 :                         ECDSA_SIG_free(ec_sign);
    2625                 :            :                 }
    2626                 :          1 :                 break;
    2627                 :          2 :         case RTE_CRYPTO_ASYM_OP_VERIFY:
    2628                 :            :                 {
    2629                 :          2 :                         unsigned char signbuf[128] = {0}, *signbuf_new = NULL;
    2630                 :            :                         BIGNUM *r = NULL, *s = NULL;
    2631                 :            :                         size_t signlen;
    2632                 :            : 
    2633                 :          2 :                         kctx = EVP_PKEY_CTX_new_from_name(NULL, "SM2", NULL);
    2634   [ +  -  +  -  :          4 :                         if (kctx == NULL || EVP_PKEY_fromdata_init(kctx) <= 0 ||
                   -  + ]
    2635                 :          2 :                                 EVP_PKEY_fromdata(kctx, &pkey, EVP_PKEY_PUBLIC_KEY, params) <= 0)
    2636                 :          0 :                                 goto err_sm2;
    2637                 :            : 
    2638         [ -  + ]:          2 :                         if (!EVP_PKEY_is_a(pkey, "SM2"))
    2639                 :          0 :                                 goto err_sm2;
    2640                 :            : 
    2641                 :          2 :                         md_ctx = EVP_MD_CTX_new();
    2642         [ -  + ]:          2 :                         if (!md_ctx)
    2643                 :          0 :                                 goto err_sm2;
    2644                 :            : 
    2645                 :          2 :                         sctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
    2646         [ -  + ]:          2 :                         if (!sctx)
    2647                 :          0 :                                 goto err_sm2;
    2648                 :            : 
    2649                 :          2 :                         EVP_MD_CTX_set_pkey_ctx(md_ctx, sctx);
    2650                 :            : 
    2651                 :          2 :                         check_md = EVP_MD_fetch(NULL, "sm3", NULL);
    2652         [ -  + ]:          2 :                         if (!check_md)
    2653                 :          0 :                                 goto err_sm2;
    2654                 :            : 
    2655         [ -  + ]:          2 :                         if (!EVP_DigestVerifyInit(md_ctx, NULL, check_md, NULL, pkey))
    2656                 :          0 :                                 goto err_sm2;
    2657                 :            : 
    2658         [ -  + ]:          2 :                         if (EVP_PKEY_CTX_set1_id(sctx, op->sm2.id.data, op->sm2.id.length) <= 0)
    2659                 :          0 :                                 goto err_sm2;
    2660                 :            : 
    2661         [ -  + ]:          2 :                         if (!EVP_DigestVerifyUpdate(md_ctx, op->sm2.message.data,
    2662                 :            :                                         op->sm2.message.length))
    2663                 :          0 :                                 goto err_sm2;
    2664                 :            : 
    2665                 :          2 :                         ec_sign = ECDSA_SIG_new();
    2666         [ -  + ]:          2 :                         if (!ec_sign)
    2667                 :          0 :                                 goto err_sm2;
    2668                 :            : 
    2669                 :          2 :                         r = BN_bin2bn(op->sm2.r.data, op->sm2.r.length, r);
    2670                 :          2 :                         s = BN_bin2bn(op->sm2.s.data, op->sm2.s.length, s);
    2671         [ -  + ]:          2 :                         if (!r || !s)
    2672                 :          0 :                                 goto err_sm2;
    2673                 :            : 
    2674         [ -  + ]:          2 :                         if (!ECDSA_SIG_set0(ec_sign, r, s)) {
    2675                 :          0 :                                 BN_free(r);
    2676                 :          0 :                                 BN_free(s);
    2677                 :          0 :                                 goto err_sm2;
    2678                 :            :                         }
    2679                 :            : 
    2680                 :            :                         r = NULL;
    2681                 :            :                         s = NULL;
    2682                 :            : 
    2683                 :          2 :                         signbuf_new = signbuf;
    2684                 :          2 :                         signlen = i2d_ECDSA_SIG(ec_sign, (unsigned char **)&signbuf_new);
    2685         [ -  + ]:          2 :                         if (signlen <= 0)
    2686                 :          0 :                                 goto err_sm2;
    2687                 :            : 
    2688         [ -  + ]:          2 :                         if (!EVP_DigestVerifyFinal(md_ctx, signbuf_new, signlen))
    2689                 :          0 :                                 goto err_sm2;
    2690                 :            : 
    2691                 :          2 :                         BN_free(r);
    2692                 :          2 :                         BN_free(s);
    2693                 :          2 :                         ECDSA_SIG_free(ec_sign);
    2694                 :            :         }
    2695                 :          2 :                 break;
    2696                 :          0 :         default:
    2697                 :            :                 /* allow ops with invalid args to be pushed to
    2698                 :            :                  * completion queue
    2699                 :            :                  */
    2700                 :          0 :                 cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
    2701                 :          0 :                 goto err_sm2;
    2702                 :            :         }
    2703                 :            : 
    2704                 :            :         ret = 0;
    2705                 :          6 :         cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
    2706                 :          6 : err_sm2:
    2707                 :          6 :         EVP_MD_free(check_md);
    2708                 :          6 :         EVP_MD_CTX_free(md_ctx);
    2709                 :            : 
    2710                 :          6 :         EVP_PKEY_CTX_free(kctx);
    2711                 :            : 
    2712                 :          6 :         EVP_PKEY_CTX_free(sctx);
    2713                 :            : 
    2714                 :          6 :         EVP_PKEY_CTX_free(cctx);
    2715                 :            : 
    2716                 :          6 :         EVP_PKEY_free(pkey);
    2717                 :            : 
    2718                 :          6 :         return ret;
    2719                 :            : }
    2720                 :            : 
    2721                 :            : static int
    2722                 :          0 : process_openssl_eddsa_op_evp(struct rte_crypto_op *cop,
    2723                 :            :                 struct openssl_asym_session *sess)
    2724                 :            : {
    2725                 :            :         static const char * const instance[] = {"Ed25519", "Ed25519ctx", "Ed25519ph",
    2726                 :            :                                                 "Ed448", "Ed448ph"};
    2727                 :            :         EVP_PKEY_CTX *kctx = NULL, *sctx = NULL, *cctx = NULL;
    2728                 :          0 :         const uint8_t curve_id = sess->u.eddsa.curve_id;
    2729                 :            :         struct rte_crypto_asym_op *op = cop->asym;
    2730                 :          0 :         OSSL_PARAM *params = sess->u.eddsa.params;
    2731                 :            :         OSSL_PARAM_BLD *iparam_bld = NULL;
    2732                 :            :         OSSL_PARAM *iparams = NULL;
    2733                 :          0 :         uint8_t signbuf[128] = {0};
    2734                 :            :         EVP_MD_CTX *md_ctx = NULL;
    2735                 :          0 :         EVP_PKEY *pkey = NULL;
    2736                 :            :         size_t signlen;
    2737                 :            :         int ret = -1;
    2738                 :            : 
    2739                 :          0 :         cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    2740                 :            : 
    2741                 :          0 :         iparam_bld = OSSL_PARAM_BLD_new();
    2742         [ #  # ]:          0 :         if (!iparam_bld)
    2743                 :          0 :                 goto err_eddsa;
    2744                 :            : 
    2745         [ #  # ]:          0 :         if (op->eddsa.instance == RTE_CRYPTO_EDCURVE_25519CTX) {
    2746                 :          0 :                 OSSL_PARAM_BLD_push_octet_string(iparam_bld, "context-string",
    2747                 :          0 :                         op->eddsa.context.data, op->eddsa.context.length);
    2748                 :            : 
    2749                 :            :         }
    2750                 :            : 
    2751                 :          0 :         OSSL_PARAM_BLD_push_utf8_string(iparam_bld, "instance",
    2752                 :          0 :                 instance[op->eddsa.instance], strlen(instance[op->eddsa.instance]));
    2753                 :            : 
    2754                 :          0 :         iparams = OSSL_PARAM_BLD_to_param(iparam_bld);
    2755         [ #  # ]:          0 :         if (!iparams)
    2756                 :          0 :                 goto err_eddsa;
    2757                 :            : 
    2758      [ #  #  # ]:          0 :         switch (op->eddsa.op_type) {
    2759                 :          0 :         case RTE_CRYPTO_ASYM_OP_SIGN:
    2760                 :            :                 {
    2761         [ #  # ]:          0 :                         if (curve_id == RTE_CRYPTO_EC_GROUP_ED25519)
    2762                 :          0 :                                 kctx = EVP_PKEY_CTX_new_from_name(NULL, "ED25519", NULL);
    2763                 :            :                         else
    2764                 :          0 :                                 kctx = EVP_PKEY_CTX_new_from_name(NULL, "ED448", NULL);
    2765                 :            : 
    2766   [ #  #  #  #  :          0 :                         if (kctx == NULL || EVP_PKEY_fromdata_init(kctx) <= 0 ||
                   #  # ]
    2767                 :          0 :                                 EVP_PKEY_fromdata(kctx, &pkey, EVP_PKEY_KEYPAIR, params) <= 0)
    2768                 :          0 :                                 goto err_eddsa;
    2769                 :            : 
    2770                 :          0 :                         md_ctx = EVP_MD_CTX_new();
    2771         [ #  # ]:          0 :                         if (!md_ctx)
    2772                 :          0 :                                 goto err_eddsa;
    2773                 :            : 
    2774                 :          0 :                         sctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
    2775         [ #  # ]:          0 :                         if (!sctx)
    2776                 :          0 :                                 goto err_eddsa;
    2777                 :            : 
    2778                 :          0 :                         EVP_MD_CTX_set_pkey_ctx(md_ctx, sctx);
    2779                 :            : 
    2780                 :            : #if (OPENSSL_VERSION_NUMBER >= 0x30300000L)
    2781                 :            :                         if (!EVP_DigestSignInit_ex(md_ctx, NULL, NULL, NULL, NULL, pkey, iparams))
    2782                 :            :                                 goto err_eddsa;
    2783                 :            : #else
    2784         [ #  # ]:          0 :                         if (op->eddsa.instance == RTE_CRYPTO_EDCURVE_25519 ||
    2785                 :            :                                 op->eddsa.instance == RTE_CRYPTO_EDCURVE_448) {
    2786         [ #  # ]:          0 :                                 if (!EVP_DigestSignInit(md_ctx, NULL, NULL, NULL, pkey))
    2787                 :          0 :                                         goto err_eddsa;
    2788                 :            :                         } else
    2789                 :          0 :                                 goto err_eddsa;
    2790                 :            : #endif
    2791                 :            : 
    2792         [ #  # ]:          0 :                         if (!EVP_DigestSign(md_ctx, NULL, &signlen, op->eddsa.message.data,
    2793                 :            :                                         op->eddsa.message.length))
    2794                 :          0 :                                 goto err_eddsa;
    2795                 :            : 
    2796         [ #  # ]:          0 :                         if (signlen > RTE_DIM(signbuf))
    2797                 :          0 :                                 goto err_eddsa;
    2798                 :            : 
    2799         [ #  # ]:          0 :                         if (!EVP_DigestSign(md_ctx, signbuf, &signlen, op->eddsa.message.data,
    2800                 :            :                                         op->eddsa.message.length))
    2801                 :          0 :                                 goto err_eddsa;
    2802                 :            : 
    2803                 :          0 :                         memcpy(op->eddsa.sign.data, &signbuf[0], signlen);
    2804                 :          0 :                         op->eddsa.sign.length = signlen;
    2805                 :            :                 }
    2806                 :          0 :                 break;
    2807                 :          0 :         case RTE_CRYPTO_ASYM_OP_VERIFY:
    2808                 :            :                 {
    2809         [ #  # ]:          0 :                         if (curve_id == RTE_CRYPTO_EC_GROUP_ED25519)
    2810                 :          0 :                                 kctx = EVP_PKEY_CTX_new_from_name(NULL, "ED25519", NULL);
    2811                 :            :                         else
    2812                 :          0 :                                 kctx = EVP_PKEY_CTX_new_from_name(NULL, "ED448", NULL);
    2813                 :            : 
    2814   [ #  #  #  #  :          0 :                         if (kctx == NULL || EVP_PKEY_fromdata_init(kctx) <= 0 ||
                   #  # ]
    2815                 :          0 :                                 EVP_PKEY_fromdata(kctx, &pkey, EVP_PKEY_PUBLIC_KEY, params) <= 0)
    2816                 :          0 :                                 goto err_eddsa;
    2817                 :            : 
    2818                 :          0 :                         md_ctx = EVP_MD_CTX_new();
    2819         [ #  # ]:          0 :                         if (!md_ctx)
    2820                 :          0 :                                 goto err_eddsa;
    2821                 :            : 
    2822                 :          0 :                         sctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
    2823         [ #  # ]:          0 :                         if (!sctx)
    2824                 :          0 :                                 goto err_eddsa;
    2825                 :            : 
    2826                 :          0 :                         EVP_MD_CTX_set_pkey_ctx(md_ctx, sctx);
    2827                 :            : 
    2828                 :            : #if (OPENSSL_VERSION_NUMBER >= 0x30300000L)
    2829                 :            :                         if (!EVP_DigestVerifyInit_ex(md_ctx, NULL, NULL, NULL, NULL, pkey, iparams))
    2830                 :            :                                 goto err_eddsa;
    2831                 :            : #else
    2832         [ #  # ]:          0 :                         if (op->eddsa.instance == RTE_CRYPTO_EDCURVE_25519 ||
    2833                 :            :                                 op->eddsa.instance == RTE_CRYPTO_EDCURVE_448) {
    2834         [ #  # ]:          0 :                                 if (!EVP_DigestVerifyInit(md_ctx, NULL, NULL, NULL, pkey))
    2835                 :          0 :                                         goto err_eddsa;
    2836                 :            :                         } else
    2837                 :          0 :                                 goto err_eddsa;
    2838                 :            : #endif
    2839                 :            : 
    2840                 :          0 :                         signlen = op->eddsa.sign.length;
    2841                 :          0 :                         memcpy(&signbuf[0], op->eddsa.sign.data, op->eddsa.sign.length);
    2842                 :            : 
    2843                 :          0 :                         ret = EVP_DigestVerify(md_ctx, signbuf, signlen, op->eddsa.message.data,
    2844                 :            :                                         op->eddsa.message.length);
    2845         [ #  # ]:          0 :                         if (ret == 0)
    2846                 :          0 :                                 goto err_eddsa;
    2847                 :            :                 }
    2848                 :            :                 break;
    2849                 :          0 :         default:
    2850                 :            :                 /* allow ops with invalid args to be pushed to
    2851                 :            :                  * completion queue
    2852                 :            :                  */
    2853                 :          0 :                 cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
    2854                 :          0 :                 goto err_eddsa;
    2855                 :            :         }
    2856                 :            : 
    2857                 :            :         ret = 0;
    2858                 :          0 :         cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
    2859                 :          0 : err_eddsa:
    2860                 :          0 :         OSSL_PARAM_BLD_free(iparam_bld);
    2861                 :            : 
    2862                 :          0 :         EVP_PKEY_CTX_free(sctx);
    2863                 :            : 
    2864                 :          0 :         EVP_PKEY_CTX_free(cctx);
    2865                 :            : 
    2866                 :          0 :         EVP_PKEY_free(pkey);
    2867                 :            : 
    2868                 :          0 :         return ret;
    2869                 :            : }
    2870                 :            : 
    2871                 :            : 
    2872                 :            : 
    2873                 :            : #if (OPENSSL_VERSION_NUMBER >= 0x30500000L)
    2874                 :            : static int
    2875                 :            : mlkem_keygen_op_evp(struct rte_crypto_op *cop,
    2876                 :            :                 struct openssl_asym_session *sess)
    2877                 :            : {
    2878                 :            :         struct rte_crypto_ml_kem_op *op = &cop->asym->mlkem;
    2879                 :            :         EVP_PKEY_CTX *pctx = sess->u.ml_kem.pctx;
    2880                 :            :         OSSL_PARAM_BLD *param_bld;
    2881                 :            :         uint8_t seed[64] = {0};
    2882                 :            :         EVP_PKEY *pkey = NULL;
    2883                 :            :         OSSL_PARAM *params;
    2884                 :            :         const char *param;
    2885                 :            :         size_t keylen = 0;
    2886                 :            :         void *key = NULL;
    2887                 :            :         OSSL_PARAM *p;
    2888                 :            : 
    2889                 :            :         param = ml_kem_type_names[sess->u.ml_kem.type];
    2890                 :            :         if (param == NULL) {
    2891                 :            :                 OPENSSL_LOG(ERR, "invalid ml_kem type");
    2892                 :            :                 return -EINVAL;
    2893                 :            :         }
    2894                 :            : 
    2895                 :            :         memcpy(seed, op->keygen.d.data, op->keygen.d.length);
    2896                 :            :         memcpy(seed + op->keygen.d.length,
    2897                 :            :                 op->keygen.z.data, op->keygen.z.length);
    2898                 :            : 
    2899                 :            :         param_bld = OSSL_PARAM_BLD_new();
    2900                 :            :         if (param_bld == NULL) {
    2901                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    2902                 :            :                 return -1;
    2903                 :            :         }
    2904                 :            : 
    2905                 :            :         if (!OSSL_PARAM_BLD_push_utf8_string(param_bld,
    2906                 :            :                         OSSL_PKEY_PARAM_GROUP_NAME,
    2907                 :            :                         param, strlen(param)) ||
    2908                 :            :                 !OSSL_PARAM_BLD_push_octet_string(param_bld,
    2909                 :            :                         OSSL_PKEY_PARAM_ML_KEM_SEED,
    2910                 :            :                         seed, sizeof(seed))) {
    2911                 :            :                 OSSL_PARAM_BLD_free(param_bld);
    2912                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    2913                 :            :                 return -1;
    2914                 :            :         }
    2915                 :            : 
    2916                 :            :         params = OSSL_PARAM_BLD_to_param(param_bld);
    2917                 :            :         OSSL_PARAM_BLD_free(param_bld);
    2918                 :            :         if (params == NULL) {
    2919                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    2920                 :            :                 return -1;
    2921                 :            :         }
    2922                 :            :         if (EVP_PKEY_fromdata_init(pctx) != 1) {
    2923                 :            :                 OSSL_PARAM_free(params);
    2924                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    2925                 :            :                 return -1;
    2926                 :            :         }
    2927                 :            :         if (EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_KEYPAIR, params) != 1) {
    2928                 :            :                 OSSL_PARAM_free(params);
    2929                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    2930                 :            :                 return -1;
    2931                 :            :         }
    2932                 :            :         OSSL_PARAM_free(params);
    2933                 :            : 
    2934                 :            :         if (pkey == NULL) {
    2935                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    2936                 :            :                 return -1;
    2937                 :            :         }
    2938                 :            : 
    2939                 :            :         /* extract public and private keys */
    2940                 :            :         if (EVP_PKEY_todata(pkey, EVP_PKEY_KEYPAIR, &params) != 1) {
    2941                 :            :                 OPENSSL_LOG(ERR, "Failed to convert to key pairs");
    2942                 :            :                 EVP_PKEY_free(pkey);
    2943                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    2944                 :            :                 return -1;
    2945                 :            :         }
    2946                 :            : 
    2947                 :            :         p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_PRIV_KEY);
    2948                 :            :         if (p == NULL) {
    2949                 :            :                 OPENSSL_LOG(ERR, "Failed to locate private key");
    2950                 :            :                 OSSL_PARAM_free(params);
    2951                 :            :                 EVP_PKEY_free(pkey);
    2952                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    2953                 :            :                 return -1;
    2954                 :            :         }
    2955                 :            : 
    2956                 :            :         OSSL_PARAM_get_octet_string(p, &key,
    2957                 :            :                 rte_crypto_ml_kem_pubkey_size[sess->u.ml_kem.type], &keylen);
    2958                 :            :         memcpy(op->keygen.dk.data, key, keylen);
    2959                 :            :         op->keygen.dk.length = keylen;
    2960                 :            : 
    2961                 :            :         p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_PUB_KEY);
    2962                 :            :         if (p == NULL) {
    2963                 :            :                 OPENSSL_LOG(ERR, "Failed to locate public key");
    2964                 :            :                 OSSL_PARAM_free(params);
    2965                 :            :                 EVP_PKEY_free(pkey);
    2966                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    2967                 :            :                 return -1;
    2968                 :            :         }
    2969                 :            : 
    2970                 :            :         OSSL_PARAM_get_octet_string(p, &key,
    2971                 :            :                 rte_crypto_ml_kem_privkey_size[sess->u.ml_kem.type], &keylen);
    2972                 :            :         memcpy(op->keygen.ek.data, key, keylen);
    2973                 :            :         op->keygen.ek.length = keylen;
    2974                 :            : 
    2975                 :            :         OSSL_PARAM_free(params);
    2976                 :            :         EVP_PKEY_free(pkey);
    2977                 :            :         cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
    2978                 :            :         return 0;
    2979                 :            : }
    2980                 :            : 
    2981                 :            : static int
    2982                 :            : mlkem_encap_op_evp(struct rte_crypto_op *cop,
    2983                 :            :                 struct openssl_asym_session *sess)
    2984                 :            : {
    2985                 :            :         struct rte_crypto_ml_kem_op *op = &cop->asym->mlkem;
    2986                 :            :         EVP_PKEY_CTX *pctx = sess->u.ml_kem.pctx;
    2987                 :            :         OSSL_PARAM_BLD *param_bld;
    2988                 :            :         EVP_PKEY_CTX *cctx = NULL;
    2989                 :            :         EVP_PKEY *pkey = NULL;
    2990                 :            :         OSSL_PARAM *params;
    2991                 :            :         const char *param;
    2992                 :            :         size_t keylen = 0;
    2993                 :            :         size_t outlen = 0;
    2994                 :            :         int ret = -1;
    2995                 :            : 
    2996                 :            :         param = ml_kem_type_names[sess->u.ml_kem.type];
    2997                 :            :         if (param == NULL) {
    2998                 :            :                 OPENSSL_LOG(ERR, "invalid ml_kem type");
    2999                 :            :                 return -EINVAL;
    3000                 :            :         }
    3001                 :            : 
    3002                 :            :         param_bld = OSSL_PARAM_BLD_new();
    3003                 :            :         if (param_bld == NULL) {
    3004                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3005                 :            :                 return -1;
    3006                 :            :         }
    3007                 :            : 
    3008                 :            :         if (!OSSL_PARAM_BLD_push_utf8_string(param_bld,
    3009                 :            :                         OSSL_PKEY_PARAM_GROUP_NAME,
    3010                 :            :                         param, strlen(param)) ||
    3011                 :            :                 !OSSL_PARAM_BLD_push_octet_string(param_bld,
    3012                 :            :                         OSSL_PKEY_PARAM_PUB_KEY,
    3013                 :            :                         op->encap.ek.data, op->encap.ek.length)) {
    3014                 :            :                 OSSL_PARAM_BLD_free(param_bld);
    3015                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3016                 :            :                 return -1;
    3017                 :            :         }
    3018                 :            : 
    3019                 :            :         params = OSSL_PARAM_BLD_to_param(param_bld);
    3020                 :            :         OSSL_PARAM_BLD_free(param_bld);
    3021                 :            :         if (params == NULL) {
    3022                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3023                 :            :                 return -1;
    3024                 :            :         }
    3025                 :            : 
    3026                 :            :         cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3027                 :            : 
    3028                 :            :         if (EVP_PKEY_fromdata_init(pctx) != 1)
    3029                 :            :                 goto err_params;
    3030                 :            : 
    3031                 :            :         if (EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_PUBLIC_KEY, params) != 1)
    3032                 :            :                 goto err_params;
    3033                 :            : 
    3034                 :            :         if (pkey == NULL)
    3035                 :            :                 goto err_params;
    3036                 :            : 
    3037                 :            :         cctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
    3038                 :            :         if (cctx == NULL)
    3039                 :            :                 goto err_pkey;
    3040                 :            : 
    3041                 :            : 
    3042                 :            :         if (EVP_PKEY_encapsulate_init(cctx, NULL) != 1)
    3043                 :            :                 goto err_encap;
    3044                 :            : 
    3045                 :            :         if (op->encap.message.length) {
    3046                 :            :                 const OSSL_PARAM kem_params[] = {
    3047                 :            :                         OSSL_PARAM_octet_string(OSSL_KEM_PARAM_IKME,
    3048                 :            :                                 (void *)op->encap.message.data, op->encap.message.length),
    3049                 :            :                         OSSL_PARAM_END
    3050                 :            :                 };
    3051                 :            : 
    3052                 :            :                 if (EVP_PKEY_encapsulate_init(cctx, kem_params) != 1)
    3053                 :            :                         goto err_encap;
    3054                 :            :         }
    3055                 :            : 
    3056                 :            :         if (EVP_PKEY_encapsulate(cctx, NULL, &outlen, NULL, &keylen) != 1) {
    3057                 :            :                 OPENSSL_LOG(ERR, "Failed to determine output length");
    3058                 :            :                 goto err_encap;
    3059                 :            :         }
    3060                 :            : 
    3061                 :            :         if (outlen > op->encap.cipher.length) {
    3062                 :            :                 OPENSSL_LOG(ERR, "Insufficient buffer for cipher text");
    3063                 :            :                 goto err_encap;
    3064                 :            :         }
    3065                 :            : 
    3066                 :            :         if (keylen > op->encap.sk.length) {
    3067                 :            :                 OPENSSL_LOG(ERR, "Insufficient buffer for shared key");
    3068                 :            :                 goto err_encap;
    3069                 :            :         }
    3070                 :            : 
    3071                 :            :         if (EVP_PKEY_encapsulate(cctx, op->encap.cipher.data, &outlen,
    3072                 :            :                         op->encap.sk.data, &keylen) != 1) {
    3073                 :            :                 OPENSSL_LOG(ERR, "Failed to encapculate");
    3074                 :            :                 goto err_encap;
    3075                 :            :         }
    3076                 :            : 
    3077                 :            :         op->encap.cipher.length = outlen;
    3078                 :            :         op->encap.sk.length = keylen;
    3079                 :            :         ret = 0;
    3080                 :            :         cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
    3081                 :            : 
    3082                 :            : err_encap:
    3083                 :            :         EVP_PKEY_CTX_free(cctx);
    3084                 :            : err_pkey:
    3085                 :            :         EVP_PKEY_free(pkey);
    3086                 :            : err_params:
    3087                 :            :         OSSL_PARAM_free(params);
    3088                 :            :         return ret;
    3089                 :            : }
    3090                 :            : 
    3091                 :            : static int
    3092                 :            : mlkem_decap_op_evp(struct rte_crypto_op *cop,
    3093                 :            :                 struct openssl_asym_session *sess)
    3094                 :            : {
    3095                 :            :         struct rte_crypto_ml_kem_op *op = &cop->asym->mlkem;
    3096                 :            :         EVP_PKEY_CTX *pctx = sess->u.ml_kem.pctx;
    3097                 :            :         OSSL_PARAM_BLD *param_bld;
    3098                 :            :         EVP_PKEY_CTX *cctx = NULL;
    3099                 :            :         EVP_PKEY *pkey = NULL;
    3100                 :            :         OSSL_PARAM *params;
    3101                 :            :         const char *param;
    3102                 :            :         size_t keylen = 0;
    3103                 :            :         int ret = -1;
    3104                 :            : 
    3105                 :            :         param = ml_kem_type_names[sess->u.ml_kem.type];
    3106                 :            :         if (param == NULL) {
    3107                 :            :                 OPENSSL_LOG(ERR, "invalid ml_kem type");
    3108                 :            :                 return -EINVAL;
    3109                 :            :         }
    3110                 :            : 
    3111                 :            :         param_bld = OSSL_PARAM_BLD_new();
    3112                 :            :         if (param_bld == NULL) {
    3113                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3114                 :            :                 return -1;
    3115                 :            :         }
    3116                 :            : 
    3117                 :            :         if (!OSSL_PARAM_BLD_push_utf8_string(param_bld,
    3118                 :            :                         OSSL_PKEY_PARAM_GROUP_NAME,
    3119                 :            :                         param, strlen(param)) ||
    3120                 :            :                 !OSSL_PARAM_BLD_push_octet_string(param_bld,
    3121                 :            :                         OSSL_PKEY_PARAM_PRIV_KEY,
    3122                 :            :                         op->decap.dk.data, op->decap.dk.length)) {
    3123                 :            :                 OSSL_PARAM_BLD_free(param_bld);
    3124                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3125                 :            :                 return -1;
    3126                 :            :         }
    3127                 :            : 
    3128                 :            :         params = OSSL_PARAM_BLD_to_param(param_bld);
    3129                 :            :         OSSL_PARAM_BLD_free(param_bld);
    3130                 :            :         if (params == NULL) {
    3131                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3132                 :            :                 return -1;
    3133                 :            :         }
    3134                 :            : 
    3135                 :            :         cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3136                 :            : 
    3137                 :            :         if (EVP_PKEY_fromdata_init(pctx) != 1)
    3138                 :            :                 goto err_params;
    3139                 :            : 
    3140                 :            :         if (EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_PRIVATE_KEY, params) != 1)
    3141                 :            :                 goto err_params;
    3142                 :            : 
    3143                 :            :         if (pkey == NULL)
    3144                 :            :                 goto err_params;
    3145                 :            : 
    3146                 :            :         cctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
    3147                 :            :         if (cctx == NULL)
    3148                 :            :                 goto err_pkey;
    3149                 :            : 
    3150                 :            :         if (EVP_PKEY_decapsulate_init(cctx, NULL) != 1)
    3151                 :            :                 goto err_decap;
    3152                 :            : 
    3153                 :            :         if (EVP_PKEY_decapsulate(cctx, NULL, &keylen,
    3154                 :            :                 op->decap.cipher.data, op->decap.cipher.length) != 1) {
    3155                 :            :                 OPENSSL_LOG(ERR, "Failed to determine output length");
    3156                 :            :                 goto err_decap;
    3157                 :            :         }
    3158                 :            : 
    3159                 :            :         if (keylen > op->decap.sk.length) {
    3160                 :            :                 OPENSSL_LOG(ERR, "Insufficient buffer for shared key");
    3161                 :            :                 goto err_decap;
    3162                 :            :         }
    3163                 :            : 
    3164                 :            :         if (EVP_PKEY_decapsulate(cctx, op->decap.sk.data, &keylen,
    3165                 :            :                         op->decap.cipher.data, op->decap.cipher.length) != 1) {
    3166                 :            :                 OPENSSL_LOG(ERR, "Failed to decapsulate");
    3167                 :            :                 goto err_decap;
    3168                 :            :         }
    3169                 :            : 
    3170                 :            :         op->decap.sk.length = keylen;
    3171                 :            :         ret = 0;
    3172                 :            :         cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
    3173                 :            : 
    3174                 :            : err_decap:
    3175                 :            :         EVP_PKEY_CTX_free(cctx);
    3176                 :            : err_pkey:
    3177                 :            :         EVP_PKEY_free(pkey);
    3178                 :            : err_params:
    3179                 :            :         OSSL_PARAM_free(params);
    3180                 :            :         return ret;
    3181                 :            : }
    3182                 :            : 
    3183                 :            : static int
    3184                 :            : process_openssl_mlkem_op_evp(struct rte_crypto_op *cop,
    3185                 :            :                 struct openssl_asym_session *sess)
    3186                 :            : {
    3187                 :            :         struct rte_crypto_ml_kem_op *op = &cop->asym->mlkem;
    3188                 :            :         int ret = -1;
    3189                 :            : 
    3190                 :            :         switch (op->op) {
    3191                 :            :         case RTE_CRYPTO_ML_KEM_OP_KEYGEN:
    3192                 :            :                 ret = mlkem_keygen_op_evp(cop, sess);
    3193                 :            :                 break;
    3194                 :            :         case RTE_CRYPTO_ML_KEM_OP_KEYVER:
    3195                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
    3196                 :            :                 break;
    3197                 :            :         case RTE_CRYPTO_ML_KEM_OP_ENCAP:
    3198                 :            :                 ret = mlkem_encap_op_evp(cop, sess);
    3199                 :            :                 break;
    3200                 :            :         case RTE_CRYPTO_ML_KEM_OP_DECAP:
    3201                 :            :                 ret = mlkem_decap_op_evp(cop, sess);
    3202                 :            :                 break;
    3203                 :            :         default:
    3204                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
    3205                 :            :                 break;
    3206                 :            :         }
    3207                 :            : 
    3208                 :            :         return ret;
    3209                 :            : }
    3210                 :            : 
    3211                 :            : static int
    3212                 :            : mldsa_keygen_op_evp(struct rte_crypto_op *cop,
    3213                 :            :                 struct openssl_asym_session *sess)
    3214                 :            : {
    3215                 :            :         struct rte_crypto_ml_dsa_op *op = &cop->asym->mldsa;
    3216                 :            :         EVP_PKEY_CTX *pctx = sess->u.ml_dsa.pctx;
    3217                 :            :         OSSL_PARAM_BLD *param_bld;
    3218                 :            :         EVP_PKEY *pkey = NULL;
    3219                 :            :         OSSL_PARAM *params;
    3220                 :            :         const char *param;
    3221                 :            :         size_t keylen = 0;
    3222                 :            :         void *key = NULL;
    3223                 :            :         OSSL_PARAM *p;
    3224                 :            : 
    3225                 :            :         param = ml_dsa_type_names[sess->u.ml_dsa.type];
    3226                 :            :         if (param == NULL) {
    3227                 :            :                 OPENSSL_LOG(ERR, "invalid ml_dsa type");
    3228                 :            :                 return -EINVAL;
    3229                 :            :         }
    3230                 :            : 
    3231                 :            :         param_bld = OSSL_PARAM_BLD_new();
    3232                 :            :         if (param_bld == NULL) {
    3233                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3234                 :            :                 return -1;
    3235                 :            :         }
    3236                 :            : 
    3237                 :            :         if (!OSSL_PARAM_BLD_push_utf8_string(param_bld,
    3238                 :            :                         OSSL_PKEY_PARAM_GROUP_NAME,
    3239                 :            :                         param, strlen(param)) ||
    3240                 :            :                 !OSSL_PARAM_BLD_push_octet_string(param_bld,
    3241                 :            :                         OSSL_PKEY_PARAM_ML_DSA_SEED,
    3242                 :            :                         op->keygen.seed.data, op->keygen.seed.length)) {
    3243                 :            :                 OSSL_PARAM_BLD_free(param_bld);
    3244                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3245                 :            :                 return -1;
    3246                 :            :         }
    3247                 :            : 
    3248                 :            :         params = OSSL_PARAM_BLD_to_param(param_bld);
    3249                 :            :         OSSL_PARAM_BLD_free(param_bld);
    3250                 :            :         if (params == NULL) {
    3251                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3252                 :            :                 return -1;
    3253                 :            :         }
    3254                 :            :         if (EVP_PKEY_fromdata_init(pctx) != 1) {
    3255                 :            :                 OSSL_PARAM_free(params);
    3256                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3257                 :            :                 return -1;
    3258                 :            :         }
    3259                 :            :         if (EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_KEYPAIR, params) != 1) {
    3260                 :            :                 OSSL_PARAM_free(params);
    3261                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3262                 :            :                 return -1;
    3263                 :            :         }
    3264                 :            :         OSSL_PARAM_free(params);
    3265                 :            : 
    3266                 :            :         if (pkey == NULL) {
    3267                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3268                 :            :                 return -1;
    3269                 :            :         }
    3270                 :            : 
    3271                 :            :         /* extract public and private keys */
    3272                 :            :         if (EVP_PKEY_todata(pkey, EVP_PKEY_KEYPAIR, &params) != 1) {
    3273                 :            :                 OPENSSL_LOG(ERR, "Failed to convert to key pairs");
    3274                 :            :                 EVP_PKEY_free(pkey);
    3275                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3276                 :            :                 return -1;
    3277                 :            :         }
    3278                 :            : 
    3279                 :            :         p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_PRIV_KEY);
    3280                 :            :         if (p == NULL) {
    3281                 :            :                 OPENSSL_LOG(ERR, "Failed to locate private key");
    3282                 :            :                 OSSL_PARAM_free(params);
    3283                 :            :                 EVP_PKEY_free(pkey);
    3284                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3285                 :            :                 return -1;
    3286                 :            :         }
    3287                 :            : 
    3288                 :            :         OSSL_PARAM_get_octet_string(p, &key,
    3289                 :            :                 rte_crypto_ml_dsa_privkey_size[sess->u.ml_dsa.type], &keylen);
    3290                 :            :         memcpy(op->keygen.privkey.data, key, keylen);
    3291                 :            :         op->keygen.privkey.length = keylen;
    3292                 :            : 
    3293                 :            :         p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_PUB_KEY);
    3294                 :            :         if (p == NULL) {
    3295                 :            :                 OPENSSL_LOG(ERR, "Failed to locate public key");
    3296                 :            :                 OSSL_PARAM_free(params);
    3297                 :            :                 EVP_PKEY_free(pkey);
    3298                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3299                 :            :                 return -1;
    3300                 :            :         }
    3301                 :            : 
    3302                 :            :         OSSL_PARAM_get_octet_string(p, &key,
    3303                 :            :                 rte_crypto_ml_dsa_pubkey_size[sess->u.ml_dsa.type], &keylen);
    3304                 :            :         memcpy(op->keygen.pubkey.data, key, keylen);
    3305                 :            :         op->keygen.pubkey.length = keylen;
    3306                 :            : 
    3307                 :            :         OSSL_PARAM_free(params);
    3308                 :            :         EVP_PKEY_free(pkey);
    3309                 :            :         cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
    3310                 :            :         return 0;
    3311                 :            : }
    3312                 :            : 
    3313                 :            : static int
    3314                 :            : mldsa_sign_op_evp(struct rte_crypto_op *cop,
    3315                 :            :                 struct openssl_asym_session *sess)
    3316                 :            : {
    3317                 :            :         struct rte_crypto_ml_dsa_op *op = &cop->asym->mldsa;
    3318                 :            :         EVP_PKEY_CTX *pctx = sess->u.ml_dsa.pctx;
    3319                 :            :         const EVP_MD *check_md = NULL;
    3320                 :            :         OSSL_PARAM_BLD *param_bld;
    3321                 :            :         EVP_PKEY_CTX *cctx = NULL;
    3322                 :            :         EVP_PKEY *pkey = NULL;
    3323                 :            :         EVP_SIGNATURE *sigalg;
    3324                 :            :         OSSL_PARAM *params;
    3325                 :            :         const char *param;
    3326                 :            :         unsigned char *md;
    3327                 :            :         size_t siglen = 0;
    3328                 :            :         size_t mdlen = 0;
    3329                 :            :         int ret = -1;
    3330                 :            : 
    3331                 :            :         param = ml_dsa_type_names[sess->u.ml_dsa.type];
    3332                 :            :         if (param == NULL) {
    3333                 :            :                 OPENSSL_LOG(ERR, "invalid ml_dsa type");
    3334                 :            :                 return -EINVAL;
    3335                 :            :         }
    3336                 :            : 
    3337                 :            :         param_bld = OSSL_PARAM_BLD_new();
    3338                 :            :         if (param_bld == NULL) {
    3339                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3340                 :            :                 return -1;
    3341                 :            :         }
    3342                 :            : 
    3343                 :            :         if (!OSSL_PARAM_BLD_push_utf8_string(param_bld,
    3344                 :            :                         OSSL_PKEY_PARAM_GROUP_NAME,
    3345                 :            :                         param, strlen(param)) ||
    3346                 :            :                 (op->siggen.seed.length &&
    3347                 :            :                         !OSSL_PARAM_BLD_push_octet_string(param_bld,
    3348                 :            :                         OSSL_PKEY_PARAM_ML_DSA_SEED,
    3349                 :            :                         op->siggen.seed.data, op->siggen.seed.length)) ||
    3350                 :            :                 !OSSL_PARAM_BLD_push_octet_string(param_bld,
    3351                 :            :                         OSSL_PKEY_PARAM_PRIV_KEY,
    3352                 :            :                         op->siggen.privkey.data, op->siggen.privkey.length)) {
    3353                 :            :                 OSSL_PARAM_BLD_free(param_bld);
    3354                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3355                 :            :                 return -1;
    3356                 :            :         }
    3357                 :            : 
    3358                 :            :         params = OSSL_PARAM_BLD_to_param(param_bld);
    3359                 :            :         OSSL_PARAM_BLD_free(param_bld);
    3360                 :            :         if (params == NULL) {
    3361                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3362                 :            :                 return -1;
    3363                 :            :         }
    3364                 :            : 
    3365                 :            :         if (EVP_PKEY_fromdata_init(pctx) != 1) {
    3366                 :            :                 OSSL_PARAM_free(params);
    3367                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3368                 :            :                 return -1;
    3369                 :            :         }
    3370                 :            : 
    3371                 :            :         if (EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_PRIVATE_KEY, params) != 1) {
    3372                 :            :                 OSSL_PARAM_free(params);
    3373                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3374                 :            :                 return -1;
    3375                 :            :         }
    3376                 :            : 
    3377                 :            :         if (pkey == NULL) {
    3378                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3379                 :            :                 return -1;
    3380                 :            :         }
    3381                 :            : 
    3382                 :            :         cctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
    3383                 :            :         if (cctx == NULL) {
    3384                 :            :                 EVP_PKEY_free(pkey);
    3385                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3386                 :            :                 return -1;
    3387                 :            :         }
    3388                 :            : 
    3389                 :            :         sigalg = EVP_SIGNATURE_fetch(NULL, param, NULL);
    3390                 :            :         if (sigalg == NULL) {
    3391                 :            :                 OPENSSL_LOG(ERR, "Failed to fetch signature algorithm");
    3392                 :            :                 EVP_PKEY_free(pkey);
    3393                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3394                 :            :                 return -1;
    3395                 :            :         }
    3396                 :            : 
    3397                 :            :         if (EVP_PKEY_sign_message_init(cctx, sigalg, NULL) != 1) {
    3398                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3399                 :            :                 return -1;
    3400                 :            :         }
    3401                 :            : 
    3402                 :            :         if (sess->u.ml_dsa.sign_deterministic) {
    3403                 :            :                 int deterministic = 1;
    3404                 :            :                 const OSSL_PARAM sign_params[] = {
    3405                 :            :                         OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_DETERMINISTIC, &deterministic),
    3406                 :            :                         OSSL_PARAM_END
    3407                 :            :                 };
    3408                 :            : 
    3409                 :            :                 if (EVP_PKEY_sign_message_init(cctx, sigalg, sign_params) != 1) {
    3410                 :            :                         EVP_SIGNATURE_free(sigalg);
    3411                 :            :                         cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3412                 :            :                         return -1;
    3413                 :            :                 }
    3414                 :            :         }
    3415                 :            : 
    3416                 :            :         md = op->siggen.message.data;
    3417                 :            :         mdlen = op->siggen.message.length;
    3418                 :            : 
    3419                 :            :         if (op->siggen.mu.length) {
    3420                 :            :                 int has_mu = 1;
    3421                 :            :                 const OSSL_PARAM sign_params[] = {
    3422                 :            :                         OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_MU, &has_mu),
    3423                 :            :                         OSSL_PARAM_END
    3424                 :            :                 };
    3425                 :            : 
    3426                 :            :                 if (EVP_PKEY_sign_message_init(cctx, sigalg, sign_params) != 1) {
    3427                 :            :                         EVP_SIGNATURE_free(sigalg);
    3428                 :            :                         cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3429                 :            :                         return -1;
    3430                 :            :                 }
    3431                 :            : 
    3432                 :            :                 md = op->siggen.mu.data;
    3433                 :            :                 mdlen = op->siggen.mu.length;
    3434                 :            :         } else if (op->siggen.ctx.length) {
    3435                 :            :                 int has_ctx = 1;
    3436                 :            :                 const OSSL_PARAM sign_params[] = {
    3437                 :            :                         OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_MESSAGE_ENCODING, &has_ctx),
    3438                 :            :                         OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_CONTEXT_STRING,
    3439                 :            :                                 (void *)op->siggen.ctx.data, op->siggen.ctx.length),
    3440                 :            :                         OSSL_PARAM_END
    3441                 :            :                 };
    3442                 :            : 
    3443                 :            :                 if (EVP_PKEY_sign_message_init(cctx, sigalg, sign_params) != 1) {
    3444                 :            :                         EVP_SIGNATURE_free(sigalg);
    3445                 :            :                         cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3446                 :            :                         return -1;
    3447                 :            :                 }
    3448                 :            :         }
    3449                 :            : 
    3450                 :            :         EVP_SIGNATURE_free(sigalg);
    3451                 :            : 
    3452                 :            :         switch (op->siggen.hash) {
    3453                 :            :         case RTE_CRYPTO_AUTH_SHA3_224:
    3454                 :            :                 check_md = EVP_sha3_224();
    3455                 :            :                 break;
    3456                 :            :         case RTE_CRYPTO_AUTH_SHA3_256:
    3457                 :            :                 check_md = EVP_sha3_256();
    3458                 :            :                 break;
    3459                 :            :         case RTE_CRYPTO_AUTH_SHA3_384:
    3460                 :            :                 check_md = EVP_sha3_384();
    3461                 :            :                 break;
    3462                 :            :         case RTE_CRYPTO_AUTH_SHA3_512:
    3463                 :            :                 check_md = EVP_sha3_512();
    3464                 :            :                 break;
    3465                 :            :         case RTE_CRYPTO_AUTH_SHAKE_128:
    3466                 :            :                 check_md = EVP_shake128();
    3467                 :            :                 break;
    3468                 :            :         case RTE_CRYPTO_AUTH_SHAKE_256:
    3469                 :            :                 check_md = EVP_shake256();
    3470                 :            :                 break;
    3471                 :            :         default:
    3472                 :            :                 break;
    3473                 :            :         }
    3474                 :            : 
    3475                 :            :         if (op->siggen.hash != 0 && check_md == NULL) {
    3476                 :            :                 OPENSSL_LOG(ERR, "invalid hash type");
    3477                 :            :                 EVP_PKEY_free(pkey);
    3478                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3479                 :            :                 return -1;
    3480                 :            :         }
    3481                 :            : 
    3482                 :            :         if (check_md != NULL) {
    3483                 :            :                 if (EVP_PKEY_CTX_set_signature_md(cctx, check_md) != 1) {
    3484                 :            :                         OPENSSL_LOG(ERR, "Failed to set signature md");
    3485                 :            :                         EVP_PKEY_free(pkey);
    3486                 :            :                         cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3487                 :            :                         return -1;
    3488                 :            :                 }
    3489                 :            :         }
    3490                 :            : 
    3491                 :            :         if (EVP_PKEY_sign(cctx, NULL, &siglen, md, mdlen) != 1) {
    3492                 :            :                 OPENSSL_LOG(ERR, "Failed to determine output length");
    3493                 :            :                 EVP_PKEY_free(pkey);
    3494                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3495                 :            :                 return -1;
    3496                 :            :         }
    3497                 :            : 
    3498                 :            :         if (siglen > op->siggen.sign.length) {
    3499                 :            :                 OPENSSL_LOG(ERR, "Insufficient buffer for signature");
    3500                 :            :                 EVP_PKEY_free(pkey);
    3501                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3502                 :            :                 return -1;
    3503                 :            :         }
    3504                 :            : 
    3505                 :            :         if (EVP_PKEY_sign(cctx, op->siggen.sign.data, &siglen, md, mdlen) != 1) {
    3506                 :            :                 OPENSSL_LOG(ERR, "Failed to sign");
    3507                 :            :                 EVP_PKEY_free(pkey);
    3508                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3509                 :            :                 return -1;
    3510                 :            :         }
    3511                 :            : 
    3512                 :            :         op->siggen.sign.length = siglen;
    3513                 :            : 
    3514                 :            :         OSSL_PARAM_free(params);
    3515                 :            :         EVP_PKEY_CTX_free(cctx);
    3516                 :            :         EVP_PKEY_free(pkey);
    3517                 :            :         ret = 0;
    3518                 :            :         cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
    3519                 :            :         return ret;
    3520                 :            : }
    3521                 :            : 
    3522                 :            : static int
    3523                 :            : mldsa_verify_op_evp(struct rte_crypto_op *cop,
    3524                 :            :                 struct openssl_asym_session *sess)
    3525                 :            : {
    3526                 :            :         struct rte_crypto_ml_dsa_op *op = &cop->asym->mldsa;
    3527                 :            :         EVP_PKEY_CTX *pctx = sess->u.ml_dsa.pctx;
    3528                 :            :         OSSL_PARAM_BLD *param_bld;
    3529                 :            :         EVP_PKEY_CTX *cctx = NULL;
    3530                 :            :         EVP_PKEY *pkey = NULL;
    3531                 :            :         EVP_SIGNATURE *sigalg;
    3532                 :            :         OSSL_PARAM *params;
    3533                 :            :         const char *param;
    3534                 :            :         unsigned char *md;
    3535                 :            :         size_t mdlen = 0;
    3536                 :            :         int ret = -1;
    3537                 :            : 
    3538                 :            :         param = ml_dsa_type_names[sess->u.ml_dsa.type];
    3539                 :            :         if (param == NULL) {
    3540                 :            :                 OPENSSL_LOG(ERR, "invalid ml_dsa type");
    3541                 :            :                 return -EINVAL;
    3542                 :            :         }
    3543                 :            : 
    3544                 :            :         param_bld = OSSL_PARAM_BLD_new();
    3545                 :            :         if (param_bld == NULL) {
    3546                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3547                 :            :                 return -1;
    3548                 :            :         }
    3549                 :            : 
    3550                 :            :         if (!OSSL_PARAM_BLD_push_utf8_string(param_bld,
    3551                 :            :                         OSSL_PKEY_PARAM_GROUP_NAME,
    3552                 :            :                         param, strlen(param)) ||
    3553                 :            :                 !OSSL_PARAM_BLD_push_octet_string(param_bld,
    3554                 :            :                         OSSL_PKEY_PARAM_PUB_KEY,
    3555                 :            :                         op->sigver.pubkey.data, op->sigver.pubkey.length)) {
    3556                 :            :                 OSSL_PARAM_BLD_free(param_bld);
    3557                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3558                 :            :                 return -1;
    3559                 :            :         }
    3560                 :            : 
    3561                 :            :         params = OSSL_PARAM_BLD_to_param(param_bld);
    3562                 :            :         OSSL_PARAM_BLD_free(param_bld);
    3563                 :            :         if (params == NULL) {
    3564                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3565                 :            :                 return -1;
    3566                 :            :         }
    3567                 :            : 
    3568                 :            :         if (EVP_PKEY_fromdata_init(pctx) != 1) {
    3569                 :            :                 OSSL_PARAM_free(params);
    3570                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3571                 :            :                 return -1;
    3572                 :            :         }
    3573                 :            : 
    3574                 :            :         if (EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_PUBLIC_KEY, params) != 1) {
    3575                 :            :                 OSSL_PARAM_free(params);
    3576                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3577                 :            :                 return -1;
    3578                 :            :         }
    3579                 :            : 
    3580                 :            :         if (pkey == NULL) {
    3581                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3582                 :            :                 return -1;
    3583                 :            :         }
    3584                 :            : 
    3585                 :            :         cctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
    3586                 :            :         if (cctx == NULL) {
    3587                 :            :                 EVP_PKEY_free(pkey);
    3588                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3589                 :            :                 return -1;
    3590                 :            :         }
    3591                 :            : 
    3592                 :            :         sigalg = EVP_SIGNATURE_fetch(NULL, param, NULL);
    3593                 :            :         if (sigalg == NULL) {
    3594                 :            :                 OPENSSL_LOG(ERR, "Failed to fetch signature algorithm");
    3595                 :            :                 EVP_PKEY_free(pkey);
    3596                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3597                 :            :                 return -1;
    3598                 :            :         }
    3599                 :            : 
    3600                 :            :         if (EVP_PKEY_verify_message_init(cctx, sigalg, NULL) != 1) {
    3601                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3602                 :            :                 return -1;
    3603                 :            :         }
    3604                 :            : 
    3605                 :            :         md = op->sigver.message.data;
    3606                 :            :         mdlen = op->sigver.message.length;
    3607                 :            : 
    3608                 :            :         if (op->sigver.mu.length) {
    3609                 :            :                 int has_mu = 1;
    3610                 :            :                 const OSSL_PARAM sign_params[] = {
    3611                 :            :                         OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_MU, &has_mu),
    3612                 :            :                         OSSL_PARAM_END
    3613                 :            :                 };
    3614                 :            : 
    3615                 :            :                 if (EVP_PKEY_verify_message_init(cctx, sigalg, sign_params) != 1) {
    3616                 :            :                         EVP_SIGNATURE_free(sigalg);
    3617                 :            :                         cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3618                 :            :                         return -1;
    3619                 :            :                 }
    3620                 :            : 
    3621                 :            :                 md = op->sigver.mu.data;
    3622                 :            :                 mdlen = op->sigver.mu.length;
    3623                 :            :         } else if (op->sigver.ctx.length) {
    3624                 :            :                 const OSSL_PARAM sign_params[] = {
    3625                 :            :                         OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_CONTEXT_STRING,
    3626                 :            :                                 (void *)op->sigver.ctx.data, op->sigver.ctx.length),
    3627                 :            :                         OSSL_PARAM_END
    3628                 :            :                 };
    3629                 :            : 
    3630                 :            :                 if (EVP_PKEY_verify_message_init(cctx, sigalg, sign_params) != 1) {
    3631                 :            :                         EVP_SIGNATURE_free(sigalg);
    3632                 :            :                         cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3633                 :            :                         return -1;
    3634                 :            :                 }
    3635                 :            :         }
    3636                 :            : 
    3637                 :            :         if (EVP_PKEY_verify(cctx, op->sigver.sign.data, op->sigver.sign.length,
    3638                 :            :                 md, mdlen) != 1) {
    3639                 :            :                 EVP_PKEY_free(pkey);
    3640                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3641                 :            :                 return 0;
    3642                 :            :         }
    3643                 :            : 
    3644                 :            :         OSSL_PARAM_free(params);
    3645                 :            :         EVP_PKEY_CTX_free(cctx);
    3646                 :            :         EVP_PKEY_free(pkey);
    3647                 :            :         ret = 0;
    3648                 :            :         cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
    3649                 :            :         return ret;
    3650                 :            : }
    3651                 :            : 
    3652                 :            : static int
    3653                 :            : process_openssl_mldsa_op_evp(struct rte_crypto_op *cop,
    3654                 :            :                 struct openssl_asym_session *sess)
    3655                 :            : {
    3656                 :            :         struct rte_crypto_ml_dsa_op *op = &cop->asym->mldsa;
    3657                 :            :         int ret = -1;
    3658                 :            : 
    3659                 :            :         switch (op->op) {
    3660                 :            :         case RTE_CRYPTO_ML_DSA_OP_KEYGEN:
    3661                 :            :                 ret = mldsa_keygen_op_evp(cop, sess);
    3662                 :            :                 break;
    3663                 :            :         case RTE_CRYPTO_ML_DSA_OP_SIGN:
    3664                 :            :                 ret = mldsa_sign_op_evp(cop, sess);
    3665                 :            :                 break;
    3666                 :            :         case RTE_CRYPTO_ML_DSA_OP_VERIFY:
    3667                 :            :                 ret = mldsa_verify_op_evp(cop, sess);
    3668                 :            :                 break;
    3669                 :            :         default:
    3670                 :            :                 cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
    3671                 :            :                 break;
    3672                 :            :         }
    3673                 :            : 
    3674                 :            :         return ret;
    3675                 :            : }
    3676                 :            : #else
    3677                 :            : static int
    3678                 :            : process_openssl_mlkem_op(struct rte_crypto_op *cop,
    3679                 :            :                 struct openssl_asym_session *sess)
    3680                 :            : {
    3681                 :            :         RTE_SET_USED(cop);
    3682                 :            :         RTE_SET_USED(sess);
    3683                 :            :         return -ENOTSUP;
    3684                 :            : }
    3685                 :            : 
    3686                 :            : static int
    3687                 :            : process_openssl_mldsa_op(struct rte_crypto_op *cop,
    3688                 :            :                 struct openssl_asym_session *sess)
    3689                 :            : {
    3690                 :            :         RTE_SET_USED(cop);
    3691                 :            :         RTE_SET_USED(sess);
    3692                 :            :         return -ENOTSUP;
    3693                 :            : }
    3694                 :            : #endif
    3695                 :            : 
    3696                 :            : static int
    3697                 :         40 : process_asym_op(struct openssl_qp *qp, struct rte_crypto_op *op,
    3698                 :            :                 struct openssl_asym_session *sess)
    3699                 :            : {
    3700                 :            :         int retval = 0;
    3701                 :            : 
    3702                 :         40 :         op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
    3703                 :            : 
    3704   [ +  +  +  +  :         40 :         switch (sess->xfrm_type) {
          +  -  +  -  -  
                      - ]
    3705                 :         14 :         case RTE_CRYPTO_ASYM_XFORM_RSA:
    3706                 :         14 :                 retval = process_openssl_rsa_op_evp(op, sess);
    3707                 :         14 :                 break;
    3708                 :         12 :         case RTE_CRYPTO_ASYM_XFORM_MODEX:
    3709                 :         12 :                 retval = process_openssl_modexp_op(op, sess);
    3710                 :         12 :                 break;
    3711                 :          2 :         case RTE_CRYPTO_ASYM_XFORM_MODINV:
    3712                 :          2 :                 retval = process_openssl_modinv_op(op, sess);
    3713                 :          2 :                 break;
    3714                 :          4 :         case RTE_CRYPTO_ASYM_XFORM_DH:
    3715                 :          4 :                 retval = process_openssl_dh_op_evp(op, sess);
    3716                 :          4 :                 break;
    3717                 :          2 :         case RTE_CRYPTO_ASYM_XFORM_DSA:
    3718         [ +  + ]:          2 :                 if (op->asym->dsa.op_type == RTE_CRYPTO_ASYM_OP_SIGN)
    3719                 :          1 :                         retval = process_openssl_dsa_sign_op_evp(op, sess);
    3720         [ +  - ]:          1 :                 else if (op->asym->dsa.op_type ==
    3721                 :            :                                 RTE_CRYPTO_ASYM_OP_VERIFY)
    3722                 :            :                         retval =
    3723                 :          1 :                                 process_openssl_dsa_verify_op_evp(op, sess);
    3724                 :            :                 else
    3725                 :          0 :                         op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
    3726                 :            :                 break;
    3727                 :          0 :         case RTE_CRYPTO_ASYM_XFORM_ECFPM:
    3728                 :          0 :                 retval = process_openssl_ecfpm_op_evp(op, sess);
    3729                 :          0 :                 break;
    3730                 :          6 :         case RTE_CRYPTO_ASYM_XFORM_SM2:
    3731                 :          6 :                 retval = process_openssl_sm2_op_evp(op, sess);
    3732                 :          6 :                 break;
    3733                 :          0 :         case RTE_CRYPTO_ASYM_XFORM_EDDSA:
    3734                 :          0 :                 retval = process_openssl_eddsa_op_evp(op, sess);
    3735                 :          0 :                 break;
    3736                 :            :         case RTE_CRYPTO_ASYM_XFORM_ML_KEM:
    3737                 :            : #if (OPENSSL_VERSION_NUMBER >= 0x30500000L)
    3738                 :            :                 retval = process_openssl_mlkem_op_evp(op, sess);
    3739                 :            : #else
    3740                 :            :                 retval = process_openssl_mlkem_op(op, sess);
    3741                 :            : #endif
    3742                 :            :                 break;
    3743                 :            :         case RTE_CRYPTO_ASYM_XFORM_ML_DSA:
    3744                 :            : #if (OPENSSL_VERSION_NUMBER >= 0x30500000L)
    3745                 :            :                 retval = process_openssl_mldsa_op_evp(op, sess);
    3746                 :            : #else
    3747                 :            :                 retval = process_openssl_mldsa_op(op, sess);
    3748                 :            : #endif
    3749                 :            :                 break;
    3750                 :          0 :         default:
    3751                 :          0 :                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
    3752                 :            :                 break;
    3753                 :            :         }
    3754         [ +  - ]:         40 :         if (!retval) {
    3755                 :            :                 /* op processed so push to completion queue as processed */
    3756   [ -  +  -  -  :         80 :                 retval = rte_ring_enqueue(qp->processed_ops, (void *)op);
                      - ]
    3757                 :            :                 if (retval)
    3758                 :            :                         /* return error if failed to put in completion queue */
    3759                 :            :                         retval = -1;
    3760                 :            :         }
    3761                 :            : 
    3762                 :         40 :         return retval;
    3763                 :            : }
    3764                 :            : 
    3765                 :            : static void
    3766                 :         10 : copy_plaintext(struct rte_mbuf *m_src, struct rte_mbuf *m_dst,
    3767                 :            :                 struct rte_crypto_op *op)
    3768                 :            : {
    3769                 :            :         uint8_t *p_src, *p_dst;
    3770                 :            : 
    3771                 :         10 :         p_src = rte_pktmbuf_mtod(m_src, uint8_t *);
    3772                 :         10 :         p_dst = rte_pktmbuf_mtod(m_dst, uint8_t *);
    3773                 :            : 
    3774                 :            :         /**
    3775                 :            :          * Copy the content between cipher offset and auth offset
    3776                 :            :          * for generating correct digest.
    3777                 :            :          */
    3778         [ +  + ]:         10 :         if (op->sym->cipher.data.offset > op->sym->auth.data.offset)
    3779                 :          2 :                 memcpy(p_dst + op->sym->auth.data.offset,
    3780                 :          2 :                                 p_src + op->sym->auth.data.offset,
    3781                 :          2 :                                 op->sym->cipher.data.offset -
    3782                 :            :                                 op->sym->auth.data.offset);
    3783                 :         10 : }
    3784                 :            : 
    3785                 :            : /** Process crypto operation for mbuf */
    3786                 :            : static int
    3787                 :        294 : process_op(struct openssl_qp *qp, struct rte_crypto_op *op,
    3788                 :            :                 struct openssl_session *sess)
    3789                 :            : {
    3790                 :            :         struct rte_mbuf *msrc, *mdst;
    3791                 :            :         int retval;
    3792                 :            : 
    3793                 :        294 :         msrc = op->sym->m_src;
    3794         [ +  + ]:        294 :         mdst = op->sym->m_dst ? op->sym->m_dst : op->sym->m_src;
    3795                 :            : 
    3796                 :        294 :         op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
    3797                 :            : 
    3798   [ +  +  +  +  :        294 :         switch (sess->chain_order) {
                +  +  - ]
    3799                 :         45 :         case OPENSSL_CHAIN_ONLY_CIPHER:
    3800                 :         45 :                 process_openssl_cipher_op(qp, op, sess, msrc, mdst);
    3801                 :         45 :                 break;
    3802                 :         56 :         case OPENSSL_CHAIN_ONLY_AUTH:
    3803                 :         56 :                 process_openssl_auth_op(qp, op, sess, msrc, mdst);
    3804                 :         56 :                 break;
    3805                 :         36 :         case OPENSSL_CHAIN_CIPHER_AUTH:
    3806                 :         36 :                 process_openssl_cipher_op(qp, op, sess, msrc, mdst);
    3807                 :            :                 /* OOP */
    3808         [ +  + ]:         36 :                 if (msrc != mdst)
    3809                 :         10 :                         copy_plaintext(msrc, mdst, op);
    3810                 :         36 :                 process_openssl_auth_op(qp, op, sess, mdst, mdst);
    3811                 :         36 :                 break;
    3812                 :         40 :         case OPENSSL_CHAIN_AUTH_CIPHER:
    3813                 :         40 :                 process_openssl_auth_op(qp, op, sess, msrc, mdst);
    3814                 :         40 :                 process_openssl_cipher_op(qp, op, sess, msrc, mdst);
    3815                 :         40 :                 break;
    3816                 :         99 :         case OPENSSL_CHAIN_COMBINED:
    3817                 :         99 :                 process_openssl_combined_op(qp, op, sess, msrc, mdst);
    3818                 :         99 :                 break;
    3819                 :         18 :         case OPENSSL_CHAIN_CIPHER_BPI:
    3820                 :         18 :                 process_openssl_docsis_bpi_op(op, sess, msrc, mdst);
    3821                 :         18 :                 break;
    3822                 :          0 :         default:
    3823                 :          0 :                 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
    3824                 :          0 :                 break;
    3825                 :            :         }
    3826                 :            : 
    3827                 :            :         /* Free session if a session-less crypto op */
    3828         [ +  + ]:        294 :         if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
    3829                 :         10 :                 openssl_reset_session(sess);
    3830                 :            :                 memset(sess, 0, sizeof(struct openssl_session));
    3831         [ -  + ]:         10 :                 rte_mempool_put(qp->sess_mp, op->sym->session);
    3832                 :         10 :                 op->sym->session = NULL;
    3833                 :            :         }
    3834                 :            : 
    3835         [ +  + ]:        294 :         if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
    3836                 :        283 :                 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
    3837                 :            : 
    3838         [ +  - ]:        294 :         if (op->status != RTE_CRYPTO_OP_STATUS_ERROR)
    3839   [ -  +  -  -  :        588 :                 retval = rte_ring_enqueue(qp->processed_ops, (void *)op);
                      - ]
    3840                 :            :         else
    3841                 :            :                 retval = -1;
    3842                 :            : 
    3843                 :        294 :         return retval;
    3844                 :            : }
    3845                 :            : 
    3846                 :            : /*
    3847                 :            :  *------------------------------------------------------------------------------
    3848                 :            :  * PMD Framework
    3849                 :            :  *------------------------------------------------------------------------------
    3850                 :            :  */
    3851                 :            : 
    3852                 :            : /** Enqueue burst */
    3853                 :            : static uint16_t
    3854                 :        334 : openssl_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
    3855                 :            :                 uint16_t nb_ops)
    3856                 :            : {
    3857                 :            :         void *sess;
    3858                 :            :         struct openssl_qp *qp = queue_pair;
    3859                 :            :         int i, retval;
    3860                 :            : 
    3861         [ +  + ]:        668 :         for (i = 0; i < nb_ops; i++) {
    3862                 :        334 :                 sess = get_session(qp, ops[i]);
    3863         [ -  + ]:        334 :                 if (unlikely(sess == NULL))
    3864                 :          0 :                         goto enqueue_err;
    3865                 :            : 
    3866         [ +  + ]:        334 :                 if (ops[i]->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC)
    3867                 :        294 :                         retval = process_op(qp, ops[i],
    3868                 :            :                                         (struct openssl_session *) sess);
    3869                 :            :                 else
    3870                 :         40 :                         retval = process_asym_op(qp, ops[i],
    3871                 :            :                                         (struct openssl_asym_session *) sess);
    3872         [ -  + ]:        334 :                 if (unlikely(retval < 0))
    3873                 :          0 :                         goto enqueue_err;
    3874                 :            :         }
    3875                 :            : 
    3876                 :        334 :         qp->stats.enqueued_count += i;
    3877                 :        334 :         return i;
    3878                 :            : 
    3879                 :          0 : enqueue_err:
    3880                 :          0 :         qp->stats.enqueue_err_count++;
    3881                 :          0 :         return i;
    3882                 :            : }
    3883                 :            : 
    3884                 :            : /** Dequeue burst */
    3885                 :            : static uint16_t
    3886                 :        334 : openssl_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
    3887                 :            :                 uint16_t nb_ops)
    3888                 :            : {
    3889                 :            :         struct openssl_qp *qp = queue_pair;
    3890                 :            : 
    3891                 :            :         unsigned int nb_dequeued = 0;
    3892                 :            : 
    3893   [ -  +  -  -  :        334 :         nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
                      - ]
    3894                 :            :                         (void **)ops, nb_ops, NULL);
    3895                 :        334 :         qp->stats.dequeued_count += nb_dequeued;
    3896                 :            : 
    3897                 :        334 :         return nb_dequeued;
    3898                 :            : }
    3899                 :            : 
    3900                 :            : /** Create OPENSSL crypto device */
    3901                 :            : static int
    3902                 :          2 : cryptodev_openssl_create(const char *name,
    3903                 :            :                         struct rte_vdev_device *vdev,
    3904                 :            :                         struct rte_cryptodev_pmd_init_params *init_params)
    3905                 :            : {
    3906                 :            :         struct rte_cryptodev *dev;
    3907                 :            :         struct openssl_private *internals;
    3908                 :            : 
    3909                 :          2 :         dev = rte_cryptodev_pmd_create(name, &vdev->device, init_params);
    3910         [ -  + ]:          2 :         if (dev == NULL) {
    3911                 :          0 :                 OPENSSL_LOG(ERR, "failed to create cryptodev vdev");
    3912                 :          0 :                 goto init_error;
    3913                 :            :         }
    3914                 :            : 
    3915                 :          2 :         dev->driver_id = cryptodev_driver_id;
    3916                 :          2 :         dev->dev_ops = rte_openssl_pmd_ops;
    3917                 :            : 
    3918                 :            :         /* register rx/tx burst functions for data path */
    3919                 :          2 :         dev->dequeue_burst = openssl_pmd_dequeue_burst;
    3920                 :          2 :         dev->enqueue_burst = openssl_pmd_enqueue_burst;
    3921                 :            : 
    3922                 :          2 :         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
    3923                 :            :                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
    3924                 :            :                         RTE_CRYPTODEV_FF_CPU_AESNI |
    3925                 :            :                         RTE_CRYPTODEV_FF_IN_PLACE_SGL |
    3926                 :            :                         RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
    3927                 :            :                         RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT |
    3928                 :            :                         RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO |
    3929                 :            :                         RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_EXP |
    3930                 :            :                         RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT |
    3931                 :            :                         RTE_CRYPTODEV_FF_SYM_SESSIONLESS;
    3932                 :            : 
    3933                 :          2 :         internals = dev->data->dev_private;
    3934                 :            : 
    3935                 :          2 :         internals->max_nb_qpairs = init_params->max_nb_queue_pairs;
    3936                 :            : 
    3937                 :          2 :         rte_cryptodev_pmd_probing_finish(dev);
    3938                 :            : 
    3939                 :            :         /* Load legacy provider
    3940                 :            :          * Some algorithms are no longer available in earlier version of openssl,
    3941                 :            :          * unless the legacy provider explicitly loaded. e.g. DES
    3942                 :            :          */
    3943                 :          2 :         ossl_legacy_provider_load();
    3944                 :            : 
    3945                 :          2 :         return 0;
    3946                 :            : 
    3947                 :            : init_error:
    3948                 :          0 :         OPENSSL_LOG(ERR, "driver %s: create failed",
    3949                 :            :                         init_params->name);
    3950                 :            : 
    3951                 :          0 :         cryptodev_openssl_remove(vdev);
    3952                 :          0 :         return -EFAULT;
    3953                 :            : }
    3954                 :            : 
    3955                 :            : /** Initialise OPENSSL crypto device */
    3956                 :            : static int
    3957                 :          2 : cryptodev_openssl_probe(struct rte_vdev_device *vdev)
    3958                 :            : {
    3959                 :          4 :         struct rte_cryptodev_pmd_init_params init_params = {
    3960                 :            :                 "",
    3961                 :            :                 sizeof(struct openssl_private),
    3962         [ +  - ]:          2 :                 rte_socket_id(),
    3963                 :            :                 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS
    3964                 :            :         };
    3965                 :            :         const char *name;
    3966                 :            :         const char *input_args;
    3967                 :            : 
    3968                 :            :         name = rte_vdev_device_name(vdev);
    3969                 :            :         if (name == NULL)
    3970                 :            :                 return -EINVAL;
    3971                 :            :         input_args = rte_vdev_device_args(vdev);
    3972                 :            : 
    3973                 :          2 :         rte_cryptodev_pmd_parse_input_args(&init_params, input_args);
    3974                 :            : 
    3975                 :          2 :         return cryptodev_openssl_create(name, vdev, &init_params);
    3976                 :            : }
    3977                 :            : 
    3978                 :            : /** Uninitialise OPENSSL crypto device */
    3979                 :            : static int
    3980         [ +  - ]:          2 : cryptodev_openssl_remove(struct rte_vdev_device *vdev)
    3981                 :            : {
    3982                 :            :         struct rte_cryptodev *cryptodev;
    3983                 :            :         const char *name;
    3984                 :            : 
    3985                 :            :         name = rte_vdev_device_name(vdev);
    3986                 :            :         if (name == NULL)
    3987                 :            :                 return -EINVAL;
    3988                 :            : 
    3989                 :          2 :         cryptodev = rte_cryptodev_pmd_get_named_dev(name);
    3990         [ +  - ]:          2 :         if (cryptodev == NULL)
    3991                 :            :                 return -ENODEV;
    3992                 :            : 
    3993                 :          2 :         ossl_legacy_provider_unload();
    3994                 :            : 
    3995                 :          2 :         return rte_cryptodev_pmd_destroy(cryptodev);
    3996                 :            : }
    3997                 :            : 
    3998                 :            : static struct rte_vdev_driver cryptodev_openssl_pmd_drv = {
    3999                 :            :         .probe = cryptodev_openssl_probe,
    4000                 :            :         .remove = cryptodev_openssl_remove
    4001                 :            : };
    4002                 :            : 
    4003                 :            : static struct cryptodev_driver openssl_crypto_drv;
    4004                 :            : 
    4005                 :        301 : RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_OPENSSL_PMD,
    4006                 :            :         cryptodev_openssl_pmd_drv);
    4007                 :            : RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_OPENSSL_PMD,
    4008                 :            :         "max_nb_queue_pairs=<int> "
    4009                 :            :         "socket_id=<int>");
    4010                 :        301 : RTE_PMD_REGISTER_CRYPTO_DRIVER(openssl_crypto_drv,
    4011                 :            :                 cryptodev_openssl_pmd_drv.driver, cryptodev_driver_id);
    4012         [ -  + ]:        301 : RTE_LOG_REGISTER_DEFAULT(openssl_logtype_driver, INFO);

Generated by: LCOV version 1.14