LCOV - code coverage report
Current view: top level - drivers/crypto/cnxk - cnxk_ae.h (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 773 0.0 %
Date: 2024-12-01 18:57:19 Functions: 0 1 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 316 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(C) 2021 Marvell.
       3                 :            :  */
       4                 :            : 
       5                 :            : #ifndef _CNXK_AE_H_
       6                 :            : #define _CNXK_AE_H_
       7                 :            : 
       8                 :            : #include <rte_common.h>
       9                 :            : #include <rte_crypto_asym.h>
      10                 :            : #include <rte_malloc.h>
      11                 :            : 
      12                 :            : #include "roc_ae.h"
      13                 :            : 
      14                 :            : #include "cnxk_cryptodev_ops.h"
      15                 :            : 
      16                 :            : #define ASYM_SESS_SIZE sizeof(struct rte_cryptodev_asym_session)
      17                 :            : 
      18                 :            : #define CNXK_AE_EDDSA_MAX_PARAM_LEN 1024
      19                 :            : 
      20                 :            : struct cnxk_ae_sess {
      21                 :            :         uint8_t rte_sess[ASYM_SESS_SIZE];
      22                 :            :         enum rte_crypto_asym_xform_type xfrm_type;
      23                 :            :         union {
      24                 :            :                 struct rte_crypto_rsa_xform rsa_ctx;
      25                 :            :                 struct rte_crypto_modex_xform mod_ctx;
      26                 :            :                 struct roc_ae_ec_ctx ec_ctx;
      27                 :            :         };
      28                 :            :         uint64_t *cnxk_fpm_iova;
      29                 :            :         struct roc_ae_ec_group **ec_grp;
      30                 :            :         uint64_t cpt_inst_w4;
      31                 :            :         uint64_t cpt_inst_w7;
      32                 :            :         uint64_t cpt_inst_w2;
      33                 :            :         struct cnxk_cpt_qp *qp;
      34                 :            :         struct roc_cpt_lf *lf;
      35                 :            :         uint64_t msg_max_sz;
      36                 :            :         struct hw_ctx_s {
      37                 :            :                 union {
      38                 :            :                         struct {
      39                 :            :                                 uint64_t rsvd : 48;
      40                 :            : 
      41                 :            :                                 uint64_t ctx_push_size : 7;
      42                 :            :                                 uint64_t rsvd1 : 1;
      43                 :            : 
      44                 :            :                                 uint64_t ctx_hdr_size : 2;
      45                 :            :                                 uint64_t aop_valid : 1;
      46                 :            :                                 uint64_t rsvd2 : 1;
      47                 :            :                                 uint64_t ctx_size : 4;
      48                 :            :                         } s;
      49                 :            :                         uint64_t u64;
      50                 :            :                 } w0;
      51                 :            :                 uint8_t rsvd[256];
      52                 :            :         } hw_ctx __plt_aligned(ROC_ALIGN);
      53                 :            : };
      54                 :            : 
      55                 :            : static __rte_always_inline void
      56                 :            : cnxk_ae_modex_param_normalize(uint8_t **data, size_t *len, size_t max)
      57                 :            : {
      58                 :          0 :         uint8_t msw_len = *len % 8;
      59                 :          0 :         uint64_t msw_val = 0;
      60                 :            :         size_t i;
      61                 :            : 
      62         [ #  # ]:          0 :         if (*len <= 8)
      63                 :          0 :                 return;
      64                 :            : 
      65   [ #  #  #  # ]:          0 :         memcpy(&msw_val, *data, msw_len);
      66   [ #  #  #  # ]:          0 :         if (msw_val != 0)
      67                 :            :                 return;
      68                 :            : 
      69   [ #  #  #  #  :          0 :         for (i = msw_len; i < *len && (*len - i) < max; i += 8) {
             #  #  #  # ]
      70                 :          0 :                 memcpy(&msw_val, &(*data)[i], 8);
      71   [ #  #  #  # ]:          0 :                 if (msw_val != 0)
      72                 :            :                         break;
      73                 :            :         }
      74                 :          0 :         *data += i;
      75                 :          0 :         *len -= i;
      76                 :            : }
      77                 :            : 
      78                 :            : static __rte_always_inline int
      79                 :            : cnxk_ae_fill_modex_params(struct cnxk_ae_sess *sess,
      80                 :            :                           struct rte_crypto_asym_xform *xform)
      81                 :            : {
      82                 :            :         struct rte_crypto_modex_xform *ctx = &sess->mod_ctx;
      83                 :          0 :         size_t exp_len = xform->modex.exponent.length;
      84                 :          0 :         size_t mod_len = xform->modex.modulus.length;
      85                 :          0 :         uint8_t *exp = xform->modex.exponent.data;
      86         [ #  # ]:          0 :         uint8_t *mod = xform->modex.modulus.data;
      87                 :            : 
      88                 :            :         cnxk_ae_modex_param_normalize(&mod, &mod_len, SIZE_MAX);
      89                 :            :         cnxk_ae_modex_param_normalize(&exp, &exp_len, mod_len);
      90                 :            : 
      91   [ #  #  #  # ]:          0 :         if (unlikely(exp_len == 0 || mod_len == 0))
      92                 :            :                 return -EINVAL;
      93                 :            : 
      94         [ #  # ]:          0 :         if (unlikely(exp_len > mod_len))
      95                 :            :                 return -ENOTSUP;
      96                 :            : 
      97                 :            :         /* Allocate buffer to hold modexp params */
      98                 :          0 :         ctx->modulus.data = rte_malloc(NULL, mod_len + exp_len, 0);
      99         [ #  # ]:          0 :         if (ctx->modulus.data == NULL)
     100                 :            :                 return -ENOMEM;
     101                 :            : 
     102                 :            :         /* Set up modexp prime modulus and private exponent */
     103                 :            :         memcpy(ctx->modulus.data, mod, mod_len);
     104                 :          0 :         ctx->exponent.data = ctx->modulus.data + mod_len;
     105                 :            :         memcpy(ctx->exponent.data, exp, exp_len);
     106                 :            : 
     107                 :          0 :         ctx->modulus.length = mod_len;
     108                 :          0 :         ctx->exponent.length = exp_len;
     109                 :            : 
     110                 :            :         return 0;
     111                 :            : }
     112                 :            : 
     113                 :            : static __rte_always_inline int
     114                 :            : cnxk_ae_fill_rsa_params(struct cnxk_ae_sess *sess,
     115                 :            :                         struct rte_crypto_asym_xform *xform)
     116                 :            : {
     117                 :          0 :         struct rte_crypto_rsa_priv_key_qt qt = xform->rsa.qt;
     118                 :            :         struct rte_crypto_rsa_xform *xfrm_rsa = &xform->rsa;
     119                 :            :         struct rte_crypto_rsa_xform *rsa = &sess->rsa_ctx;
     120                 :          0 :         struct rte_crypto_param_t d = xform->rsa.d;
     121                 :          0 :         size_t mod_len = xfrm_rsa->n.length;
     122                 :          0 :         size_t exp_len = xfrm_rsa->e.length;
     123                 :            :         uint64_t total_size;
     124                 :            :         size_t len = 0;
     125                 :            : 
     126                 :            :         /* Set private key type */
     127                 :          0 :         rsa->key_type = xfrm_rsa->key_type;
     128                 :            : 
     129         [ #  # ]:          0 :         if (rsa->key_type == RTE_RSA_KEY_TYPE_QT) {
     130   [ #  #  #  # ]:          0 :                 if (qt.p.length != 0 && qt.p.data == NULL)
     131                 :            :                         return -EINVAL;
     132                 :            : 
     133                 :            :                 /* Make sure key length used is not more than mod_len/2 */
     134         [ #  # ]:          0 :                 if (qt.p.data != NULL)
     135         [ #  # ]:          0 :                         len = (((mod_len / 2) < qt.p.length) ? 0 : qt.p.length * 5);
     136         [ #  # ]:          0 :         } else if (rsa->key_type == RTE_RSA_KEY_TYPE_EXP) {
     137   [ #  #  #  # ]:          0 :                 if (d.length != 0 && d.data == NULL)
     138                 :            :                         return -EINVAL;
     139                 :            : 
     140                 :            :                 len = d.length;
     141                 :            :         }
     142                 :            : 
     143                 :            :         /* Total size required for RSA key params(n,e,(q,dQ,p,dP,qInv)) */
     144                 :          0 :         total_size = mod_len + exp_len + len;
     145                 :            : 
     146                 :            :         /* Allocate buffer to hold all RSA keys */
     147                 :          0 :         rsa->n.data = rte_malloc(NULL, total_size, 0);
     148         [ #  # ]:          0 :         if (rsa->n.data == NULL)
     149                 :            :                 return -ENOMEM;
     150                 :            : 
     151                 :            :         /* Set up RSA prime modulus and public key exponent */
     152         [ #  # ]:          0 :         memcpy(rsa->n.data, xfrm_rsa->n.data, mod_len);
     153                 :          0 :         rsa->e.data = rsa->n.data + mod_len;
     154                 :          0 :         memcpy(rsa->e.data, xfrm_rsa->e.data, exp_len);
     155                 :            : 
     156         [ #  # ]:          0 :         if (rsa->key_type == RTE_RSA_KEY_TYPE_QT) {
     157                 :            :                 /* Private key in quintuple format */
     158         [ #  # ]:          0 :                 rsa->qt.q.data = rsa->e.data + exp_len;
     159                 :            :                 memcpy(rsa->qt.q.data, qt.q.data, qt.q.length);
     160                 :          0 :                 rsa->qt.dQ.data = rsa->qt.q.data + qt.q.length;
     161                 :            :                 memcpy(rsa->qt.dQ.data, qt.dQ.data, qt.dQ.length);
     162                 :          0 :                 rsa->qt.p.data = rsa->qt.dQ.data + qt.dQ.length;
     163         [ #  # ]:          0 :                 if (qt.p.data != NULL)
     164                 :            :                         memcpy(rsa->qt.p.data, qt.p.data, qt.p.length);
     165                 :          0 :                 rsa->qt.dP.data = rsa->qt.p.data + qt.p.length;
     166                 :            :                 memcpy(rsa->qt.dP.data, qt.dP.data, qt.dP.length);
     167                 :          0 :                 rsa->qt.qInv.data = rsa->qt.dP.data + qt.dP.length;
     168                 :            :                 memcpy(rsa->qt.qInv.data, qt.qInv.data, qt.qInv.length);
     169                 :            : 
     170                 :          0 :                 rsa->qt.q.length = qt.q.length;
     171                 :          0 :                 rsa->qt.dQ.length = qt.dQ.length;
     172                 :          0 :                 rsa->qt.p.length = qt.p.length;
     173                 :          0 :                 rsa->qt.dP.length = qt.dP.length;
     174                 :          0 :                 rsa->qt.qInv.length = qt.qInv.length;
     175         [ #  # ]:          0 :         } else if (rsa->key_type == RTE_RSA_KEY_TYPE_EXP) {
     176                 :            :                 /* Private key in exponent format */
     177                 :          0 :                 rsa->d.data = rsa->e.data + exp_len;
     178                 :            :                 memcpy(rsa->d.data, d.data, d.length);
     179                 :          0 :                 rsa->d.length = d.length;
     180                 :            :         }
     181                 :          0 :         rsa->n.length = mod_len;
     182                 :          0 :         rsa->e.length = exp_len;
     183                 :            : 
     184                 :            :         /* Set padding info */
     185                 :          0 :         rsa->padding.type = xform->rsa.padding.type;
     186                 :            : 
     187                 :            :         return 0;
     188                 :            : }
     189                 :            : 
     190                 :            : static __rte_always_inline int
     191                 :            : cnxk_ae_fill_ec_params(struct cnxk_ae_sess *sess, struct rte_crypto_asym_xform *xform)
     192                 :            : {
     193                 :            :         struct roc_ae_ec_ctx *ec = &sess->ec_ctx;
     194                 :            :         union cpt_inst_w4 w4;
     195                 :            : 
     196   [ #  #  #  #  :          0 :         switch (xform->ec.curve_id) {
             #  #  #  #  
                      # ]
     197                 :          0 :         case RTE_CRYPTO_EC_GROUP_SECP192R1:
     198                 :          0 :                 ec->curveid = ROC_AE_EC_ID_P192;
     199                 :          0 :                 break;
     200                 :          0 :         case RTE_CRYPTO_EC_GROUP_SECP224R1:
     201                 :          0 :                 ec->curveid = ROC_AE_EC_ID_P224;
     202                 :          0 :                 break;
     203                 :          0 :         case RTE_CRYPTO_EC_GROUP_SECP256R1:
     204                 :          0 :                 ec->curveid = ROC_AE_EC_ID_P256;
     205                 :          0 :                 break;
     206                 :          0 :         case RTE_CRYPTO_EC_GROUP_SECP384R1:
     207                 :          0 :                 ec->curveid = ROC_AE_EC_ID_P384;
     208                 :          0 :                 break;
     209                 :          0 :         case RTE_CRYPTO_EC_GROUP_SECP521R1:
     210                 :          0 :                 ec->curveid = ROC_AE_EC_ID_P521;
     211                 :          0 :                 break;
     212                 :          0 :         case RTE_CRYPTO_EC_GROUP_SM2:
     213                 :          0 :                 ec->curveid = ROC_AE_EC_ID_SM2;
     214                 :          0 :                 break;
     215                 :          0 :         case RTE_CRYPTO_EC_GROUP_ED25519:
     216                 :          0 :                 ec->curveid = ROC_AE_EC_ID_ED25519;
     217                 :          0 :                 w4.s.param1 = ROC_AE_ED_PARAM1_25519;
     218                 :          0 :                 break;
     219                 :          0 :         case RTE_CRYPTO_EC_GROUP_ED448:
     220                 :          0 :                 ec->curveid = ROC_AE_EC_ID_ED448;
     221                 :          0 :                 w4.s.param1 = ROC_AE_ED_PARAM1_448;
     222                 :          0 :                 break;
     223                 :            :         default:
     224                 :            :                 /* Only NIST curves (FIPS 186-4) and SM2 are supported */
     225                 :            :                 return -EINVAL;
     226                 :            :         }
     227                 :            : 
     228         [ #  # ]:          0 :         if (xform->xform_type == RTE_CRYPTO_ASYM_XFORM_ECPM)
     229                 :            :                 return 0;
     230                 :            : 
     231                 :          0 :         ec->pkey.length = xform->ec.pkey.length;
     232         [ #  # ]:          0 :         if (ec->pkey.length > ROC_AE_EC_DATA_MAX)
     233                 :          0 :                 ec->pkey.length = ROC_AE_EC_DATA_MAX;
     234         [ #  # ]:          0 :         if (ec->pkey.length)
     235         [ #  # ]:          0 :                 rte_memcpy(ec->pkey.data, xform->ec.pkey.data, ec->pkey.length);
     236                 :            : 
     237                 :          0 :         ec->q.x.length = xform->ec.q.x.length;
     238         [ #  # ]:          0 :         if (ec->q.x.length > ROC_AE_EC_DATA_MAX)
     239                 :          0 :                 ec->q.x.length = ROC_AE_EC_DATA_MAX;
     240         [ #  # ]:          0 :         if (ec->q.x.length)
     241         [ #  # ]:          0 :                 rte_memcpy(ec->q.x.data, xform->ec.q.x.data, ec->q.x.length);
     242                 :            : 
     243         [ #  # ]:          0 :         if (xform->xform_type == RTE_CRYPTO_ASYM_XFORM_EDDSA) {
     244                 :          0 :                 w4.s.opcode_major = ROC_AE_MAJOR_OP_EDDSA;
     245                 :            : 
     246                 :            :                 /* Use q.x to store compressed public key. q.y is set to 0 */
     247                 :          0 :                 ec->q.y.length = 0;
     248                 :          0 :                 goto _exit;
     249                 :            :         }
     250                 :            : 
     251                 :          0 :         ec->q.y.length = xform->ec.q.y.length;
     252         [ #  # ]:          0 :         if (ec->q.y.length > ROC_AE_EC_DATA_MAX)
     253                 :          0 :                 ec->q.y.length = ROC_AE_EC_DATA_MAX;
     254         [ #  # ]:          0 :         if (xform->ec.q.y.length)
     255         [ #  # ]:          0 :                 rte_memcpy(ec->q.y.data, xform->ec.q.y.data, ec->q.y.length);
     256                 :            : 
     257                 :          0 : _exit:
     258                 :          0 :         sess->cpt_inst_w4 = w4.u64;
     259                 :            :         return 0;
     260                 :            : }
     261                 :            : 
     262                 :            : static __rte_always_inline int
     263                 :            : cnxk_ae_fill_session_parameters(struct cnxk_ae_sess *sess,
     264                 :            :                                 struct rte_crypto_asym_xform *xform)
     265                 :            : {
     266                 :            :         int ret;
     267                 :            : 
     268                 :          0 :         sess->xfrm_type = xform->xform_type;
     269                 :          0 :         sess->msg_max_sz = cnxk_cpt_asym_get_mlen();
     270                 :            : 
     271   [ #  #  #  # ]:          0 :         switch (xform->xform_type) {
     272                 :            :         case RTE_CRYPTO_ASYM_XFORM_RSA:
     273                 :            :                 ret = cnxk_ae_fill_rsa_params(sess, xform);
     274                 :            :                 break;
     275                 :            :         case RTE_CRYPTO_ASYM_XFORM_MODEX:
     276                 :            :                 ret = cnxk_ae_fill_modex_params(sess, xform);
     277                 :            :                 break;
     278                 :            :         case RTE_CRYPTO_ASYM_XFORM_ECDSA:
     279                 :            :                 /* Fall through */
     280                 :            :         case RTE_CRYPTO_ASYM_XFORM_ECDH:
     281                 :            :         case RTE_CRYPTO_ASYM_XFORM_ECPM:
     282                 :            :         case RTE_CRYPTO_ASYM_XFORM_ECFPM:
     283                 :            :         case RTE_CRYPTO_ASYM_XFORM_SM2:
     284                 :            :         case RTE_CRYPTO_ASYM_XFORM_EDDSA:
     285                 :            :                 ret = cnxk_ae_fill_ec_params(sess, xform);
     286                 :            :                 break;
     287                 :            :         default:
     288                 :            :                 return -ENOTSUP;
     289                 :            :         }
     290                 :            :         return ret;
     291                 :            : }
     292                 :            : 
     293                 :            : static inline void
     294                 :          0 : cnxk_ae_free_session_parameters(struct cnxk_ae_sess *sess)
     295                 :            : {
     296                 :            :         struct rte_crypto_modex_xform *mod;
     297                 :            :         struct rte_crypto_rsa_xform *rsa;
     298                 :            : 
     299      [ #  #  # ]:          0 :         switch (sess->xfrm_type) {
     300                 :          0 :         case RTE_CRYPTO_ASYM_XFORM_RSA:
     301                 :            :                 rsa = &sess->rsa_ctx;
     302                 :          0 :                 rte_free(rsa->n.data);
     303                 :          0 :                 break;
     304                 :          0 :         case RTE_CRYPTO_ASYM_XFORM_MODEX:
     305                 :            :                 mod = &sess->mod_ctx;
     306                 :          0 :                 rte_free(mod->modulus.data);
     307                 :          0 :                 break;
     308                 :            :         case RTE_CRYPTO_ASYM_XFORM_ECDSA:
     309                 :            :                 /* Fall through */
     310                 :            :         case RTE_CRYPTO_ASYM_XFORM_ECPM:
     311                 :            :         case RTE_CRYPTO_ASYM_XFORM_ECFPM:
     312                 :            :                 break;
     313                 :            :         default:
     314                 :            :                 break;
     315                 :            :         }
     316                 :          0 : }
     317                 :            : 
     318                 :            : static __rte_always_inline int
     319                 :            : cnxk_ae_modex_prep(struct rte_crypto_op *op, struct roc_ae_buf_ptr *meta_buf,
     320                 :            :                    struct rte_crypto_modex_xform *mod, struct cpt_inst_s *inst)
     321                 :            : {
     322                 :          0 :         uint32_t exp_len = mod->exponent.length;
     323                 :          0 :         uint32_t mod_len = mod->modulus.length;
     324                 :            :         struct rte_crypto_mod_op_param mod_op;
     325                 :            :         uint64_t total_key_len;
     326                 :            :         union cpt_inst_w4 w4;
     327                 :            :         size_t base_len;
     328                 :            :         uint32_t dlen;
     329                 :            :         uint8_t *dptr;
     330                 :            : 
     331                 :          0 :         mod_op = op->asym->modex;
     332                 :            : 
     333                 :            :         base_len = mod_op.base.length;
     334                 :          0 :         if (unlikely(base_len > mod_len)) {
     335                 :            :                 cnxk_ae_modex_param_normalize(&mod_op.base.data, &base_len, mod_len);
     336         [ #  # ]:          0 :                 if (base_len > mod_len) {
     337                 :          0 :                         op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
     338                 :          0 :                         return -ENOTSUP;
     339                 :            :                 }
     340                 :            :         }
     341                 :            : 
     342                 :          0 :         total_key_len = mod_len + exp_len;
     343                 :            : 
     344                 :            :         /* Input buffer */
     345                 :            :         dptr = meta_buf->vaddr;
     346                 :          0 :         inst->dptr = (uintptr_t)dptr;
     347                 :          0 :         memcpy(dptr, mod->modulus.data, total_key_len);
     348                 :          0 :         dptr += total_key_len;
     349                 :            :         memcpy(dptr, mod_op.base.data, base_len);
     350                 :          0 :         dptr += base_len;
     351                 :          0 :         dlen = total_key_len + base_len;
     352                 :            : 
     353                 :            :         /* Setup opcodes */
     354                 :          0 :         w4.s.opcode_major = ROC_AE_MAJOR_OP_MODEX;
     355                 :          0 :         w4.s.opcode_minor = ROC_AE_MINOR_OP_MODEX;
     356                 :            : 
     357                 :          0 :         w4.s.param1 = mod_len;
     358                 :          0 :         w4.s.param2 = exp_len;
     359                 :          0 :         w4.s.dlen = dlen;
     360                 :            : 
     361                 :          0 :         inst->w4.u64 = w4.u64;
     362                 :          0 :         inst->rptr = (uintptr_t)dptr;
     363                 :            : 
     364                 :          0 :         return 0;
     365                 :            : }
     366                 :            : 
     367                 :            : static __rte_always_inline void
     368                 :            : cnxk_ae_rsa_prep(struct rte_crypto_op *op, struct roc_ae_buf_ptr *meta_buf,
     369                 :            :                  struct rte_crypto_rsa_xform *rsa,
     370                 :            :                  rte_crypto_param *crypto_param, struct cpt_inst_s *inst)
     371                 :            : {
     372                 :            :         struct rte_crypto_rsa_op_param rsa_op;
     373                 :          0 :         uint32_t mod_len = rsa->n.length;
     374                 :          0 :         uint32_t exp_len = rsa->e.length;
     375                 :            :         uint64_t total_key_len;
     376                 :            :         union cpt_inst_w4 w4;
     377                 :            :         uint32_t in_size;
     378                 :            :         uint32_t dlen;
     379                 :            :         uint8_t *dptr;
     380                 :            : 
     381                 :            :         rsa_op = op->asym->rsa;
     382                 :          0 :         total_key_len = mod_len + exp_len;
     383                 :            : 
     384                 :            :         /* Input buffer */
     385                 :            :         dptr = meta_buf->vaddr;
     386                 :          0 :         inst->dptr = (uintptr_t)dptr;
     387                 :          0 :         memcpy(dptr, rsa->n.data, total_key_len);
     388                 :          0 :         dptr += total_key_len;
     389                 :            : 
     390                 :          0 :         in_size = crypto_param->length;
     391                 :          0 :         memcpy(dptr, crypto_param->data, in_size);
     392                 :            : 
     393                 :          0 :         dptr += in_size;
     394                 :          0 :         dlen = total_key_len + in_size;
     395                 :            : 
     396   [ #  #  #  # ]:          0 :         if (rsa->padding.type == RTE_CRYPTO_RSA_PADDING_NONE) {
     397                 :            :                 /* Use mod_exp operation for no_padding type */
     398                 :          0 :                 w4.s.opcode_minor = ROC_AE_MINOR_OP_MODEX;
     399                 :          0 :                 w4.s.param2 = exp_len;
     400                 :            :         } else {
     401                 :            :                 if (rsa_op.op_type == RTE_CRYPTO_ASYM_OP_ENCRYPT) {
     402                 :          0 :                         w4.s.opcode_minor = ROC_AE_MINOR_OP_PKCS_ENC;
     403                 :            :                         /* Public key encrypt, use BT2*/
     404                 :          0 :                         w4.s.param2 = ROC_AE_CPT_BLOCK_TYPE2 |
     405                 :          0 :                                       ((uint16_t)(exp_len) << 1);
     406                 :            :                 } else if (rsa_op.op_type == RTE_CRYPTO_ASYM_OP_VERIFY) {
     407                 :          0 :                         w4.s.opcode_minor = ROC_AE_MINOR_OP_PKCS_DEC;
     408                 :            :                         /* Public key decrypt, use BT1 */
     409                 :          0 :                         w4.s.param2 = ROC_AE_CPT_BLOCK_TYPE1;
     410                 :            :                 }
     411                 :            :         }
     412                 :            : 
     413                 :          0 :         w4.s.opcode_major = ROC_AE_MAJOR_OP_MODEX;
     414                 :            : 
     415                 :          0 :         w4.s.param1 = mod_len;
     416                 :          0 :         w4.s.dlen = dlen;
     417                 :            : 
     418                 :          0 :         inst->w4.u64 = w4.u64;
     419                 :          0 :         inst->rptr = (uintptr_t)dptr;
     420                 :          0 : }
     421                 :            : 
     422                 :            : static __rte_always_inline void
     423                 :            : cnxk_ae_rsa_exp_prep(struct rte_crypto_op *op, struct roc_ae_buf_ptr *meta_buf,
     424                 :            :                      struct rte_crypto_rsa_xform *rsa, rte_crypto_param *crypto_param,
     425                 :            :                      struct cpt_inst_s *inst)
     426                 :            : {
     427                 :            :         struct rte_crypto_rsa_op_param rsa_op;
     428                 :          0 :         uint32_t privkey_len = rsa->d.length;
     429                 :          0 :         uint32_t mod_len = rsa->n.length;
     430                 :            :         union cpt_inst_w4 w4;
     431                 :            :         uint32_t in_size;
     432                 :            :         uint32_t dlen;
     433                 :            :         uint8_t *dptr;
     434                 :            : 
     435                 :            :         rsa_op = op->asym->rsa;
     436                 :            : 
     437                 :            :         /* Input buffer */
     438                 :            :         dptr = meta_buf->vaddr;
     439                 :          0 :         inst->dptr = (uintptr_t)dptr;
     440   [ #  #  #  # ]:          0 :         memcpy(dptr, rsa->n.data, mod_len);
     441                 :          0 :         dptr += mod_len;
     442                 :          0 :         memcpy(dptr, rsa->d.data, privkey_len);
     443                 :          0 :         dptr += privkey_len;
     444                 :            : 
     445                 :          0 :         in_size = crypto_param->length;
     446                 :          0 :         memcpy(dptr, crypto_param->data, in_size);
     447                 :            : 
     448                 :          0 :         dptr += in_size;
     449                 :          0 :         dlen = mod_len + privkey_len + in_size;
     450                 :            : 
     451   [ #  #  #  # ]:          0 :         if (rsa->padding.type == RTE_CRYPTO_RSA_PADDING_NONE) {
     452                 :            :                 /* Use mod_exp operation for no_padding type */
     453                 :          0 :                 w4.s.opcode_minor = ROC_AE_MINOR_OP_MODEX;
     454                 :          0 :                 w4.s.param2 = privkey_len;
     455                 :            :         } else {
     456                 :            :                 if (rsa_op.op_type == RTE_CRYPTO_ASYM_OP_SIGN) {
     457                 :          0 :                         w4.s.opcode_minor = ROC_AE_MINOR_OP_PKCS_ENC;
     458                 :            :                         /* Private key encrypt (exponent), use BT1*/
     459                 :          0 :                         w4.s.param2 = ROC_AE_CPT_BLOCK_TYPE1 | ((uint16_t)(privkey_len) << 1);
     460                 :            :                 } else if (rsa_op.op_type == RTE_CRYPTO_ASYM_OP_DECRYPT) {
     461                 :          0 :                         w4.s.opcode_minor = ROC_AE_MINOR_OP_PKCS_DEC;
     462                 :            :                         /* Private key decrypt (exponent), use BT2 */
     463                 :          0 :                         w4.s.param2 = ROC_AE_CPT_BLOCK_TYPE2;
     464                 :            :                 }
     465                 :            :         }
     466                 :            : 
     467                 :          0 :         w4.s.opcode_major = ROC_AE_MAJOR_OP_MODEX;
     468                 :            : 
     469                 :          0 :         w4.s.param1 = mod_len;
     470                 :          0 :         w4.s.dlen = dlen;
     471                 :            : 
     472                 :          0 :         inst->w4.u64 = w4.u64;
     473                 :          0 :         inst->rptr = (uintptr_t)dptr;
     474                 :          0 : }
     475                 :            : 
     476                 :            : static __rte_always_inline void
     477                 :            : cnxk_ae_rsa_crt_prep(struct rte_crypto_op *op, struct roc_ae_buf_ptr *meta_buf,
     478                 :            :                      struct rte_crypto_rsa_xform *rsa, rte_crypto_param *crypto_param,
     479                 :            :                      struct cpt_inst_s *inst)
     480                 :            : {
     481                 :          0 :         uint32_t qInv_len = rsa->qt.qInv.length;
     482                 :            :         struct rte_crypto_rsa_op_param rsa_op;
     483                 :          0 :         uint32_t dP_len = rsa->qt.dP.length;
     484                 :          0 :         uint32_t dQ_len = rsa->qt.dQ.length;
     485                 :          0 :         uint32_t p_len = rsa->qt.p.length;
     486                 :          0 :         uint32_t q_len = rsa->qt.q.length;
     487                 :          0 :         uint32_t mod_len = rsa->n.length;
     488                 :            :         uint64_t total_key_len;
     489                 :            :         union cpt_inst_w4 w4;
     490                 :            :         uint32_t in_size;
     491                 :            :         uint32_t dlen;
     492                 :            :         uint8_t *dptr;
     493                 :            : 
     494                 :            :         rsa_op = op->asym->rsa;
     495                 :          0 :         total_key_len = p_len + q_len + dP_len + dQ_len + qInv_len;
     496                 :            : 
     497                 :            :         /* Input buffer */
     498                 :            :         dptr = meta_buf->vaddr;
     499                 :          0 :         inst->dptr = (uintptr_t)dptr;
     500   [ #  #  #  # ]:          0 :         memcpy(dptr, rsa->qt.q.data, total_key_len);
     501                 :          0 :         dptr += total_key_len;
     502                 :            : 
     503                 :          0 :         in_size = crypto_param->length;
     504                 :          0 :         memcpy(dptr, crypto_param->data, in_size);
     505                 :            : 
     506                 :          0 :         dptr += in_size;
     507                 :          0 :         dlen = total_key_len + in_size;
     508                 :            : 
     509   [ #  #  #  # ]:          0 :         if (rsa->padding.type == RTE_CRYPTO_RSA_PADDING_NONE) {
     510                 :            :                 /*Use mod_exp operation for no_padding type */
     511                 :          0 :                 w4.s.opcode_minor = ROC_AE_MINOR_OP_MODEX_CRT;
     512                 :            :         } else {
     513                 :            :                 if (rsa_op.op_type == RTE_CRYPTO_ASYM_OP_SIGN) {
     514                 :          0 :                         w4.s.opcode_minor = ROC_AE_MINOR_OP_PKCS_ENC_CRT;
     515                 :            :                         /* Private encrypt, use BT1 */
     516                 :          0 :                         w4.s.param2 = ROC_AE_CPT_BLOCK_TYPE1;
     517                 :            :                 } else if (rsa_op.op_type == RTE_CRYPTO_ASYM_OP_DECRYPT) {
     518                 :          0 :                         w4.s.opcode_minor = ROC_AE_MINOR_OP_PKCS_DEC_CRT;
     519                 :            :                         /* Private decrypt, use BT2 */
     520                 :          0 :                         w4.s.param2 = ROC_AE_CPT_BLOCK_TYPE2;
     521                 :            :                 }
     522                 :            :         }
     523                 :            : 
     524                 :          0 :         w4.s.opcode_major = ROC_AE_MAJOR_OP_MODEX;
     525                 :            : 
     526                 :          0 :         w4.s.param1 = mod_len;
     527                 :          0 :         w4.s.dlen = dlen;
     528                 :            : 
     529                 :          0 :         inst->w4.u64 = w4.u64;
     530                 :          0 :         inst->rptr = (uintptr_t)dptr;
     531                 :          0 : }
     532                 :            : 
     533                 :            : static __rte_always_inline int __rte_hot
     534                 :            : cnxk_ae_enqueue_rsa_op(struct rte_crypto_op *op, struct roc_ae_buf_ptr *meta_buf,
     535                 :            :                        struct cnxk_ae_sess *sess, struct cpt_inst_s *inst)
     536                 :            : {
     537                 :            :         struct rte_crypto_rsa_op_param *rsa = &op->asym->rsa;
     538                 :            :         struct rte_crypto_rsa_xform *ctx = &sess->rsa_ctx;
     539                 :            : 
     540   [ #  #  #  #  :          0 :         switch (rsa->op_type) {
                      # ]
     541         [ #  # ]:          0 :         case RTE_CRYPTO_ASYM_OP_VERIFY:
     542                 :            :                 cnxk_ae_rsa_prep(op, meta_buf, ctx, &rsa->sign, inst);
     543                 :            :                 break;
     544         [ #  # ]:          0 :         case RTE_CRYPTO_ASYM_OP_ENCRYPT:
     545                 :            :                 cnxk_ae_rsa_prep(op, meta_buf, ctx, &rsa->message, inst);
     546                 :            :                 break;
     547                 :          0 :         case RTE_CRYPTO_ASYM_OP_SIGN:
     548         [ #  # ]:          0 :                 if (ctx->key_type == RTE_RSA_KEY_TYPE_QT)
     549                 :            :                         cnxk_ae_rsa_crt_prep(op, meta_buf, ctx, &rsa->message, inst);
     550                 :            :                 else
     551                 :            :                         cnxk_ae_rsa_exp_prep(op, meta_buf, ctx, &rsa->message, inst);
     552                 :            :                 break;
     553                 :          0 :         case RTE_CRYPTO_ASYM_OP_DECRYPT:
     554         [ #  # ]:          0 :                 if (ctx->key_type == RTE_RSA_KEY_TYPE_QT)
     555                 :            :                         cnxk_ae_rsa_crt_prep(op, meta_buf, ctx, &rsa->cipher, inst);
     556                 :            :                 else
     557                 :            :                         cnxk_ae_rsa_exp_prep(op, meta_buf, ctx, &rsa->cipher, inst);
     558                 :            :                 break;
     559                 :          0 :         default:
     560                 :          0 :                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
     561                 :          0 :                 return -EINVAL;
     562                 :            :         }
     563                 :            :         return 0;
     564                 :            : }
     565                 :            : 
     566                 :            : static __rte_always_inline void
     567                 :            : cnxk_ae_ecdsa_sign_prep(struct rte_crypto_ecdsa_op_param *ecdsa,
     568                 :            :                         struct roc_ae_buf_ptr *meta_buf,
     569                 :            :                         uint64_t fpm_table_iova, struct roc_ae_ec_group *ec_grp,
     570                 :            :                         struct cnxk_ae_sess *sess, struct cpt_inst_s *inst)
     571                 :            : {
     572                 :          0 :         uint16_t message_len = ecdsa->message.length;
     573                 :          0 :         uint16_t pkey_len = sess->ec_ctx.pkey.length;
     574                 :            :         uint8_t curveid = sess->ec_ctx.curveid;
     575                 :            :         uint16_t p_align, k_align, m_align;
     576                 :          0 :         uint16_t k_len = ecdsa->k.length;
     577                 :            :         uint16_t order_len, prime_len;
     578                 :            :         uint16_t o_offset, pk_offset;
     579                 :            :         union cpt_inst_w4 w4;
     580                 :            :         uint16_t dlen;
     581                 :            :         uint8_t *dptr;
     582                 :            : 
     583                 :          0 :         prime_len = ec_grp->prime.length;
     584                 :          0 :         order_len = ec_grp->order.length;
     585                 :            : 
     586                 :            :         /* Truncate input length to curve prime length */
     587                 :            :         if (message_len > prime_len)
     588                 :            :                 message_len = prime_len;
     589                 :          0 :         m_align = RTE_ALIGN_CEIL(message_len, 8);
     590                 :            : 
     591                 :          0 :         p_align = RTE_ALIGN_CEIL(prime_len, 8);
     592                 :          0 :         k_align = RTE_ALIGN_CEIL(k_len, 8);
     593                 :            : 
     594                 :            :         /* Set write offset for order and private key */
     595                 :          0 :         o_offset = prime_len - order_len;
     596                 :          0 :         pk_offset = p_align - pkey_len;
     597                 :            : 
     598                 :            :         /* Input buffer */
     599                 :            :         dptr = meta_buf->vaddr;
     600                 :          0 :         inst->dptr = (uintptr_t)dptr;
     601                 :            : 
     602                 :            :         /*
     603                 :            :          * Set dlen = sum(sizeof(fpm address), ROUNDUP8(scalar len, input len),
     604                 :            :          * ROUNDUP8(priv key len, prime len, order len)).
     605                 :            :          * Please note, private key, order cannot exceed prime
     606                 :            :          * length i.e 3 * p_align.
     607                 :            :          */
     608                 :          0 :         dlen = sizeof(fpm_table_iova) + k_align + m_align + p_align * 5;
     609                 :            : 
     610                 :          0 :         memset(dptr, 0, dlen);
     611                 :            : 
     612                 :          0 :         *(uint64_t *)dptr = fpm_table_iova;
     613                 :          0 :         dptr += sizeof(fpm_table_iova);
     614                 :            : 
     615                 :          0 :         memcpy(dptr, ecdsa->k.data, k_len);
     616                 :          0 :         dptr += k_align;
     617                 :            : 
     618                 :          0 :         memcpy(dptr, ec_grp->prime.data, prime_len);
     619                 :          0 :         dptr += p_align;
     620                 :            : 
     621                 :          0 :         memcpy(dptr + o_offset, ec_grp->order.data, order_len);
     622                 :          0 :         dptr += p_align;
     623                 :            : 
     624                 :          0 :         memcpy(dptr + pk_offset, sess->ec_ctx.pkey.data, pkey_len);
     625                 :          0 :         dptr += p_align;
     626                 :            : 
     627                 :          0 :         memcpy(dptr, ecdsa->message.data, message_len);
     628                 :          0 :         dptr += m_align;
     629                 :            : 
     630                 :          0 :         memcpy(dptr, ec_grp->consta.data, prime_len);
     631                 :          0 :         dptr += p_align;
     632                 :            : 
     633                 :          0 :         memcpy(dptr, ec_grp->constb.data, prime_len);
     634                 :          0 :         dptr += p_align;
     635                 :            : 
     636                 :            :         /* Setup opcodes */
     637                 :          0 :         w4.s.opcode_major = ROC_AE_MAJOR_OP_EC;
     638                 :          0 :         w4.s.opcode_minor = ROC_AE_MINOR_OP_EC_SIGN;
     639                 :            : 
     640                 :          0 :         w4.s.param1 = curveid | (message_len << 8);
     641                 :          0 :         w4.s.param2 = (p_align << 8) | k_len;
     642                 :          0 :         w4.s.dlen = dlen;
     643                 :            : 
     644                 :          0 :         inst->w4.u64 = w4.u64;
     645                 :          0 :         inst->rptr = (uintptr_t)dptr;
     646                 :          0 : }
     647                 :            : 
     648                 :            : static __rte_always_inline void
     649                 :            : cnxk_ae_ecdsa_verify_prep(struct rte_crypto_ecdsa_op_param *ecdsa,
     650                 :            :                           struct roc_ae_buf_ptr *meta_buf,
     651                 :            :                           uint64_t fpm_table_iova,
     652                 :            :                           struct roc_ae_ec_group *ec_grp, struct cnxk_ae_sess *sess,
     653                 :            :                           struct cpt_inst_s *inst)
     654                 :            : {
     655                 :          0 :         uint32_t message_len = ecdsa->message.length;
     656                 :          0 :         uint16_t qx_len = sess->ec_ctx.q.x.length;
     657                 :          0 :         uint16_t qy_len = sess->ec_ctx.q.y.length;
     658                 :            :         uint8_t curveid = sess->ec_ctx.curveid;
     659                 :            :         uint16_t o_offset, r_offset, s_offset;
     660                 :          0 :         uint16_t r_len = ecdsa->r.length;
     661                 :          0 :         uint16_t s_len = ecdsa->s.length;
     662                 :            :         uint16_t order_len, prime_len;
     663                 :            :         uint16_t qx_offset, qy_offset;
     664                 :            :         uint16_t p_align, m_align;
     665                 :            :         union cpt_inst_w4 w4;
     666                 :            :         uint16_t dlen;
     667                 :            :         uint8_t *dptr;
     668                 :            : 
     669                 :          0 :         prime_len = ec_grp->prime.length;
     670                 :          0 :         order_len = ec_grp->order.length;
     671                 :            : 
     672                 :            :         /* Truncate input length to curve prime length */
     673                 :            :         if (message_len > prime_len)
     674                 :            :                 message_len = prime_len;
     675                 :            : 
     676                 :          0 :         m_align = RTE_ALIGN_CEIL(message_len, 8);
     677                 :          0 :         p_align = RTE_ALIGN_CEIL(prime_len, 8);
     678                 :            : 
     679                 :            :         /* Set write offset for sign, order and public key coordinates */
     680                 :          0 :         o_offset = prime_len - order_len;
     681                 :          0 :         qx_offset = prime_len - qx_len;
     682                 :          0 :         qy_offset = prime_len - qy_len;
     683                 :          0 :         r_offset = prime_len - r_len;
     684                 :          0 :         s_offset = prime_len - s_len;
     685                 :            : 
     686                 :            :         /* Input buffer */
     687                 :            :         dptr = meta_buf->vaddr;
     688                 :          0 :         inst->dptr = (uintptr_t)dptr;
     689                 :            : 
     690                 :            :         /*
     691                 :            :          * Set dlen = sum(sizeof(fpm address), ROUNDUP8(message len),
     692                 :            :          * ROUNDUP8(sign len(r and s), public key len(x and y coordinates),
     693                 :            :          * prime len, order len)).
     694                 :            :          * Please note sign, public key and order can not exceed prime length
     695                 :            :          * i.e. 6 * p_align
     696                 :            :          */
     697                 :          0 :         dlen = sizeof(fpm_table_iova) + m_align + (8 * p_align);
     698                 :            : 
     699                 :          0 :         memset(dptr, 0, dlen);
     700                 :            : 
     701                 :          0 :         *(uint64_t *)dptr = fpm_table_iova;
     702                 :          0 :         dptr += sizeof(fpm_table_iova);
     703                 :            : 
     704                 :          0 :         memcpy(dptr + r_offset, ecdsa->r.data, r_len);
     705                 :          0 :         dptr += p_align;
     706                 :            : 
     707                 :          0 :         memcpy(dptr + s_offset, ecdsa->s.data, s_len);
     708                 :          0 :         dptr += p_align;
     709                 :            : 
     710                 :          0 :         memcpy(dptr, ecdsa->message.data, message_len);
     711                 :          0 :         dptr += m_align;
     712                 :            : 
     713                 :          0 :         memcpy(dptr + o_offset, ec_grp->order.data, order_len);
     714                 :          0 :         dptr += p_align;
     715                 :            : 
     716                 :          0 :         memcpy(dptr, ec_grp->prime.data, prime_len);
     717                 :          0 :         dptr += p_align;
     718                 :            : 
     719                 :          0 :         memcpy(dptr + qx_offset, sess->ec_ctx.q.x.data, qx_len);
     720                 :          0 :         dptr += p_align;
     721                 :            : 
     722                 :          0 :         memcpy(dptr + qy_offset, sess->ec_ctx.q.y.data, qy_len);
     723                 :          0 :         dptr += p_align;
     724                 :            : 
     725                 :          0 :         memcpy(dptr, ec_grp->consta.data, prime_len);
     726                 :          0 :         dptr += p_align;
     727                 :            : 
     728                 :          0 :         memcpy(dptr, ec_grp->constb.data, prime_len);
     729                 :          0 :         dptr += p_align;
     730                 :            : 
     731                 :            :         /* Setup opcodes */
     732                 :          0 :         w4.s.opcode_major = ROC_AE_MAJOR_OP_EC;
     733                 :          0 :         w4.s.opcode_minor = ROC_AE_MINOR_OP_EC_VERIFY;
     734                 :            : 
     735                 :          0 :         w4.s.param1 = curveid | (message_len << 8);
     736                 :          0 :         w4.s.param2 = 0;
     737                 :          0 :         w4.s.dlen = dlen;
     738                 :            : 
     739                 :          0 :         inst->w4.u64 = w4.u64;
     740                 :          0 :         inst->rptr = (uintptr_t)dptr;
     741                 :          0 : }
     742                 :            : 
     743                 :            : static __rte_always_inline int __rte_hot
     744                 :            : cnxk_ae_enqueue_ecdsa_op(struct rte_crypto_op *op,
     745                 :            :                          struct roc_ae_buf_ptr *meta_buf,
     746                 :            :                          struct cnxk_ae_sess *sess, uint64_t *fpm_iova,
     747                 :            :                          struct roc_ae_ec_group **ec_grp,
     748                 :            :                          struct cpt_inst_s *inst)
     749                 :            : {
     750                 :            :         struct rte_crypto_ecdsa_op_param *ecdsa = &op->asym->ecdsa;
     751                 :          0 :         uint8_t curveid = sess->ec_ctx.curveid;
     752                 :            : 
     753                 :          0 :         if (ecdsa->op_type == RTE_CRYPTO_ASYM_OP_SIGN)
     754                 :          0 :                 cnxk_ae_ecdsa_sign_prep(ecdsa, meta_buf, fpm_iova[curveid],
     755                 :          0 :                                         ec_grp[curveid], sess, inst);
     756         [ #  # ]:          0 :         else if (ecdsa->op_type == RTE_CRYPTO_ASYM_OP_VERIFY)
     757                 :          0 :                 cnxk_ae_ecdsa_verify_prep(ecdsa, meta_buf, fpm_iova[curveid],
     758                 :          0 :                                           ec_grp[curveid], sess, inst);
     759                 :            :         else {
     760                 :          0 :                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
     761                 :          0 :                 return -EINVAL;
     762                 :            :         }
     763                 :            :         return 0;
     764                 :            : }
     765                 :            : 
     766                 :            : static __rte_always_inline void
     767                 :            : cnxk_ae_eddsa_sign_prep(struct rte_crypto_eddsa_op_param *eddsa, struct roc_ae_buf_ptr *meta_buf,
     768                 :            :                         uint64_t fpm_table_iova, struct roc_ae_ec_group *ec_grp,
     769                 :            :                         struct cnxk_ae_sess *sess, struct cpt_inst_s *inst)
     770                 :            : {
     771                 :          0 :         const uint8_t iv_sha512[] = {
     772                 :            :                 0x6a, 0x09, 0xe6, 0x67, 0xf3, 0xbc, 0xc9, 0x08,
     773                 :            :                 0xbb, 0x67, 0xae, 0x85, 0x84, 0xca, 0xa7, 0x3b,
     774                 :            :                 0x3c, 0x6e, 0xf3, 0x72, 0xfe, 0x94, 0xf8, 0x2b,
     775                 :            :                 0xa5, 0x4f, 0xf5, 0x3a, 0x5f, 0x1d, 0x36, 0xf1,
     776                 :            :                 0x51, 0x0e, 0x52, 0x7f, 0xad, 0xe6, 0x82, 0xd1,
     777                 :            :                 0x9b, 0x05, 0x68, 0x8c, 0x2b, 0x3e, 0x6c, 0x1f,
     778                 :            :                 0x1f, 0x83, 0xd9, 0xab, 0xfb, 0x41, 0xbd, 0x6b,
     779                 :            :                 0x5b, 0xe0, 0xcd, 0x19, 0x13, 0x7e, 0x21, 0x79};
     780                 :          0 :         const uint8_t domx_ed25519[] = {
     781                 :            :                 0x53, 0x69, 0x67, 0x45, 0x64, 0x32, 0x35, 0x35,
     782                 :            :                 0x31, 0x39, 0x20, 0x6E, 0x6F, 0x20, 0x45, 0x64,
     783                 :            :                 0x32, 0x35, 0x35, 0x31, 0x39, 0x20, 0x63, 0x6F,
     784                 :            :                 0x6C, 0x6C, 0x69, 0x73, 0x69, 0x6F, 0x6E, 0x73,
     785                 :            :                 0x00, 0x00};
     786                 :          0 :         const uint8_t domx_ed448[] = {
     787                 :            :                 0x53, 0x69, 0x67, 0x45, 0x64, 0x34, 0x34, 0x38,
     788                 :            :                 0x00, 0x00};
     789                 :            : 
     790                 :          0 :         uint16_t pubkey_len = sess->ec_ctx.q.x.length;
     791                 :          0 :         uint16_t message_len = eddsa->message.length;
     792                 :          0 :         uint16_t pkey_len = sess->ec_ctx.pkey.length;
     793                 :            :         uint8_t curveid = sess->ec_ctx.curveid;
     794                 :            :         const uint8_t *domx_ptr = NULL;
     795                 :            :         uint16_t order_len, prime_len;
     796                 :            :         uint16_t ctx_align, k_align;
     797                 :            :         uint8_t pub = 0, ph = 0;
     798                 :            :         uint64_t message_handle;
     799                 :            :         union cpt_inst_w4 w4;
     800                 :            :         uint8_t domx_len = 0;
     801                 :            :         uint8_t ctx_len = 0;
     802                 :            :         uint16_t iv_len = 0;
     803                 :            :         uint64_t ctrl = 0;
     804                 :            :         uint16_t dlen;
     805                 :            :         uint8_t *dptr;
     806                 :            : 
     807                 :          0 :         if (eddsa->instance == RTE_CRYPTO_EDCURVE_25519PH ||
     808                 :            :             eddsa->instance == RTE_CRYPTO_EDCURVE_448PH)
     809                 :            :                 ph = 1;
     810                 :            : 
     811         [ #  # ]:          0 :         if (curveid == ROC_AE_EC_ID_ED25519)
     812                 :            :                 iv_len = sizeof(iv_sha512);
     813                 :            : 
     814                 :          0 :         prime_len = ec_grp->prime.length;
     815                 :          0 :         order_len = ec_grp->order.length;
     816                 :          0 :         ctx_len = eddsa->context.length;
     817                 :            : 
     818         [ #  # ]:          0 :         if (curveid == ROC_AE_EC_ID_ED25519) {
     819         [ #  # ]:          0 :                 if (ph || ctx_len) {
     820                 :            :                         domx_ptr = domx_ed25519;
     821                 :            :                         domx_len = sizeof(domx_ed25519);
     822                 :            :                 }
     823                 :            :         } else {
     824                 :            :                 domx_ptr = domx_ed448;
     825                 :            :                 domx_len = sizeof(domx_ed448);
     826                 :            :         }
     827                 :            : 
     828         [ #  # ]:          0 :         if (pubkey_len)
     829                 :            :                 pub = 1;
     830                 :            : 
     831                 :          0 :         ctx_align = RTE_ALIGN_CEIL(ctx_len + domx_len, 8);
     832                 :          0 :         k_align = RTE_ALIGN_CEIL(pkey_len, 8);
     833                 :            : 
     834                 :            :         /* Set control word */
     835                 :            :         ctrl = message_len;
     836                 :          0 :         ctrl |= (ctx_len + domx_len) << 16;
     837                 :            : 
     838                 :            :         /* Copy message and set message handle in metabuf */
     839                 :            :         dptr = meta_buf->vaddr;
     840         [ #  # ]:          0 :         memcpy(dptr, eddsa->message.data, message_len);
     841                 :            :         message_handle = (uint64_t)dptr;
     842                 :          0 :         dptr += RTE_ALIGN_CEIL(message_len, 8);
     843                 :            : 
     844                 :            :         /* Input buffer */
     845                 :          0 :         inst->dptr = (uintptr_t)dptr;
     846                 :            : 
     847                 :            :         /*
     848                 :            :          * Set dlen = sum(sizeof(fpm address), input handle, ctrl,
     849                 :            :          * ROUNDUP8(prime len, order len, constant), ROUNDUP8(priv and
     850                 :            :          * pubkey len), ROUNDUP8(context len) and iv len (if ED25519)).
     851                 :            :          */
     852                 :          0 :         dlen = sizeof(fpm_table_iova) + sizeof(message_handle) + sizeof(ctrl) + prime_len * 3 +
     853                 :          0 :                 k_align * 2 + ctx_align + iv_len;
     854                 :            : 
     855                 :          0 :         *(uint64_t *)dptr = fpm_table_iova;
     856                 :            :         dptr += sizeof(fpm_table_iova);
     857                 :            : 
     858         [ #  # ]:          0 :         *(uint64_t *)dptr = rte_cpu_to_be_64(message_handle);
     859                 :            :         dptr += sizeof(message_handle);
     860                 :            : 
     861         [ #  # ]:          0 :         *(uint64_t *)dptr = rte_cpu_to_be_64(ctrl);
     862                 :          0 :         dptr += sizeof(ctrl);
     863                 :            : 
     864         [ #  # ]:          0 :         memcpy(dptr, ec_grp->prime.data, prime_len);
     865                 :          0 :         dptr += prime_len;
     866                 :            : 
     867                 :          0 :         memcpy(dptr, ec_grp->order.data, order_len);
     868                 :          0 :         dptr += prime_len;
     869                 :            : 
     870                 :          0 :         memcpy(dptr, ec_grp->consta.data, prime_len);
     871                 :          0 :         dptr += prime_len;
     872                 :            : 
     873                 :          0 :         memcpy(dptr, sess->ec_ctx.pkey.data, pkey_len);
     874                 :          0 :         dptr += k_align;
     875                 :            : 
     876                 :          0 :         memcpy(dptr, sess->ec_ctx.q.x.data, pubkey_len);
     877                 :          0 :         dptr += k_align;
     878                 :            : 
     879                 :          0 :         memcpy(dptr, domx_ptr, domx_len);
     880         [ #  # ]:          0 :         if (eddsa->instance != RTE_CRYPTO_EDCURVE_25519) {
     881                 :          0 :                 memset(dptr + (domx_len - 1), ctx_len, 1);
     882                 :          0 :                 memset(dptr + (domx_len - 2), ph, 1);
     883                 :            :         }
     884                 :            : 
     885         [ #  # ]:          0 :         memcpy(dptr + domx_len, eddsa->context.data, ctx_len);
     886                 :          0 :         dptr += ctx_align;
     887                 :            : 
     888         [ #  # ]:          0 :         if (curveid == ROC_AE_EC_ID_ED25519) {
     889                 :          0 :                 memcpy(dptr, iv_sha512, iv_len);
     890                 :          0 :                 dptr += iv_len;
     891                 :            :         }
     892                 :            : 
     893                 :            :         /* Setup opcodes */
     894                 :          0 :         w4.u64 = sess->cpt_inst_w4;
     895                 :          0 :         w4.s.opcode_minor = ROC_AE_MINOR_OP_ED_SIGN;
     896                 :          0 :         w4.s.param1 |= ((pub << ROC_AE_ED_PARAM1_KEYGEN_BIT) | (ph << ROC_AE_EC_PARAM1_PH_BIT));
     897                 :          0 :         w4.s.param2 = 0;
     898                 :          0 :         w4.s.dlen = dlen;
     899                 :            : 
     900                 :          0 :         inst->w4.u64 = w4.u64;
     901                 :          0 :         inst->rptr = (uintptr_t)dptr;
     902                 :          0 : }
     903                 :            : 
     904                 :            : static __rte_always_inline void
     905                 :            : cnxk_ae_eddsa_verify_prep(struct rte_crypto_eddsa_op_param *eddsa, struct roc_ae_buf_ptr *meta_buf,
     906                 :            :                           uint64_t fpm_table_iova, struct roc_ae_ec_group *ec_grp,
     907                 :            :                           struct cnxk_ae_sess *sess, struct cpt_inst_s *inst)
     908                 :            : {
     909                 :          0 :         const uint8_t iv_sha512[] = {
     910                 :            :                 0x6a, 0x09, 0xe6, 0x67, 0xf3, 0xbc, 0xc9, 0x08,
     911                 :            :                 0xbb, 0x67, 0xae, 0x85, 0x84, 0xca, 0xa7, 0x3b,
     912                 :            :                 0x3c, 0x6e, 0xf3, 0x72, 0xfe, 0x94, 0xf8, 0x2b,
     913                 :            :                 0xa5, 0x4f, 0xf5, 0x3a, 0x5f, 0x1d, 0x36, 0xf1,
     914                 :            :                 0x51, 0x0e, 0x52, 0x7f, 0xad, 0xe6, 0x82, 0xd1,
     915                 :            :                 0x9b, 0x05, 0x68, 0x8c, 0x2b, 0x3e, 0x6c, 0x1f,
     916                 :            :                 0x1f, 0x83, 0xd9, 0xab, 0xfb, 0x41, 0xbd, 0x6b,
     917                 :            :                 0x5b, 0xe0, 0xcd, 0x19, 0x13, 0x7e, 0x21, 0x79};
     918                 :          0 :         const uint8_t domx_ed25519[] = {
     919                 :            :                 0x53, 0x69, 0x67, 0x45, 0x64, 0x32, 0x35, 0x35,
     920                 :            :                 0x31, 0x39, 0x20, 0x6E, 0x6F, 0x20, 0x45, 0x64,
     921                 :            :                 0x32, 0x35, 0x35, 0x31, 0x39, 0x20, 0x63, 0x6F,
     922                 :            :                 0x6C, 0x6C, 0x69, 0x73, 0x69, 0x6F, 0x6E, 0x73,
     923                 :            :                 0x00, 0x00};
     924                 :          0 :         const uint8_t domx_ed448[] = {
     925                 :            :                 0x53, 0x69, 0x67, 0x45, 0x64, 0x34, 0x34, 0x38,
     926                 :            :                 0x00, 0x00};
     927                 :            : 
     928                 :          0 :         uint16_t pubkey_len = sess->ec_ctx.q.x.length;
     929                 :          0 :         uint16_t message_len = eddsa->message.length;
     930                 :          0 :         uint16_t s_len = eddsa->sign.length / 2;
     931                 :            :         uint8_t curveid = sess->ec_ctx.curveid;
     932                 :            :         uint16_t ctx_align, k_align, s_align;
     933                 :            :         const uint8_t *domx_ptr = NULL;
     934                 :            :         uint16_t order_len, prime_len;
     935                 :            :         uint64_t message_handle;
     936                 :            :         union cpt_inst_w4 w4;
     937                 :            :         uint8_t domx_len = 0;
     938                 :            :         uint16_t iv_len = 0;
     939                 :            :         uint8_t ctx_len = 0;
     940                 :            :         uint64_t ctrl = 0;
     941                 :            :         uint8_t ph = 0;
     942                 :            :         uint16_t dlen;
     943                 :            :         uint8_t *dptr;
     944                 :            : 
     945                 :          0 :         if (eddsa->instance == RTE_CRYPTO_EDCURVE_25519PH ||
     946                 :            :             eddsa->instance == RTE_CRYPTO_EDCURVE_448PH)
     947                 :            :                 ph = 1;
     948                 :            : 
     949         [ #  # ]:          0 :         if (curveid == ROC_AE_EC_ID_ED25519)
     950                 :            :                 iv_len = sizeof(iv_sha512);
     951                 :            : 
     952                 :          0 :         prime_len = ec_grp->prime.length;
     953                 :          0 :         order_len = ec_grp->order.length;
     954                 :          0 :         ctx_len = eddsa->context.length;
     955                 :            : 
     956         [ #  # ]:          0 :         if (curveid == ROC_AE_EC_ID_ED25519) {
     957         [ #  # ]:          0 :                 if (ph || ctx_len) {
     958                 :            :                         domx_ptr = domx_ed25519;
     959                 :            :                         domx_len = sizeof(domx_ed25519);
     960                 :            :                 }
     961                 :            :         } else {
     962                 :            :                 domx_ptr = domx_ed448;
     963                 :            :                 domx_len = sizeof(domx_ed448);
     964                 :            :         }
     965                 :            : 
     966                 :          0 :         ctx_align = RTE_ALIGN_CEIL(ctx_len + domx_len, 8);
     967                 :          0 :         k_align = RTE_ALIGN_CEIL(pubkey_len, 8);
     968                 :          0 :         s_align = RTE_ALIGN_CEIL(s_len, 8);
     969                 :            : 
     970                 :            :         /* Set control word */
     971                 :            :         ctrl = message_len;
     972                 :          0 :         ctrl |= (ctx_len + domx_len) << 16;
     973                 :            : 
     974                 :            :         /* Copy message and set message handle in metabuf */
     975                 :            :         dptr = meta_buf->vaddr;
     976         [ #  # ]:          0 :         memcpy(dptr, eddsa->message.data, message_len);
     977                 :            :         message_handle = (uint64_t)dptr;
     978                 :          0 :         dptr += RTE_ALIGN_CEIL(message_len, 8);
     979                 :            : 
     980                 :            :         /* Input buffer */
     981                 :          0 :         inst->dptr = (uintptr_t)dptr;
     982                 :            : 
     983                 :            :         /*
     984                 :            :          * Set dlen = sum(sizeof(fpm address), input handle, ctrl,
     985                 :            :          * ROUNDUP8(prime len, order len, constant), ROUNDUP8(pub key len),
     986                 :            :          * ROUNDUP8(s and r len), context and iv len (if ED25519)).
     987                 :            :          */
     988                 :          0 :         dlen = sizeof(fpm_table_iova) + sizeof(message_handle) + sizeof(ctrl) + prime_len * 3 +
     989                 :          0 :                 k_align + s_align * 2 + ctx_align + iv_len;
     990                 :            : 
     991                 :          0 :         *(uint64_t *)dptr = fpm_table_iova;
     992                 :            :         dptr += sizeof(fpm_table_iova);
     993                 :            : 
     994         [ #  # ]:          0 :         *(uint64_t *)dptr = rte_cpu_to_be_64(message_handle);
     995                 :            :         dptr += sizeof(message_handle);
     996                 :            : 
     997         [ #  # ]:          0 :         *(uint64_t *)dptr = rte_cpu_to_be_64(ctrl);
     998                 :          0 :         dptr += sizeof(ctrl);
     999                 :            : 
    1000         [ #  # ]:          0 :         memcpy(dptr, ec_grp->prime.data, prime_len);
    1001                 :          0 :         dptr += prime_len;
    1002                 :            : 
    1003                 :          0 :         memcpy(dptr, ec_grp->order.data, order_len);
    1004                 :          0 :         dptr += prime_len;
    1005                 :            : 
    1006                 :          0 :         memcpy(dptr, ec_grp->consta.data, prime_len);
    1007                 :          0 :         dptr += prime_len;
    1008                 :            : 
    1009                 :          0 :         memcpy(dptr, sess->ec_ctx.q.x.data, pubkey_len);
    1010                 :          0 :         dptr += k_align;
    1011                 :            : 
    1012                 :          0 :         memcpy(dptr, eddsa->sign.data, s_len);
    1013                 :          0 :         dptr += s_align;
    1014                 :            : 
    1015                 :          0 :         memcpy(dptr, eddsa->sign.data + s_len, s_len);
    1016                 :          0 :         dptr += s_align;
    1017                 :            : 
    1018                 :          0 :         memcpy(dptr, domx_ptr, domx_len);
    1019         [ #  # ]:          0 :         if (eddsa->instance != RTE_CRYPTO_EDCURVE_25519) {
    1020                 :          0 :                 memset(dptr + (domx_len - 1), ctx_len, 1);
    1021                 :          0 :                 memset(dptr + (domx_len - 2), ph, 1);
    1022                 :            :         }
    1023                 :            : 
    1024         [ #  # ]:          0 :         memcpy(dptr + domx_len, eddsa->context.data, ctx_len);
    1025                 :          0 :         dptr += ctx_align;
    1026                 :            : 
    1027         [ #  # ]:          0 :         if (curveid == ROC_AE_EC_ID_ED25519) {
    1028                 :          0 :                 memcpy(dptr, iv_sha512, iv_len);
    1029                 :          0 :                 dptr += iv_len;
    1030                 :            :         }
    1031                 :            : 
    1032                 :            :         /* Setup opcodes */
    1033                 :          0 :         w4.u64 = sess->cpt_inst_w4;
    1034                 :          0 :         w4.s.opcode_minor = ROC_AE_MINOR_OP_ED_VERIFY;
    1035                 :            : 
    1036                 :          0 :         w4.s.param1 |= (ph << ROC_AE_EC_PARAM1_PH_BIT);
    1037                 :          0 :         w4.s.param2 = 0;
    1038                 :          0 :         w4.s.dlen = dlen;
    1039                 :            : 
    1040                 :          0 :         inst->w4.u64 = w4.u64;
    1041                 :          0 :         inst->rptr = (uintptr_t)dptr;
    1042                 :          0 : }
    1043                 :            : 
    1044                 :            : static __rte_always_inline int __rte_hot
    1045                 :            : cnxk_ae_enqueue_eddsa_op(struct rte_crypto_op *op, struct roc_ae_buf_ptr *meta_buf,
    1046                 :            :                          struct cnxk_ae_sess *sess, uint64_t *fpm_iova,
    1047                 :            :                          struct roc_ae_ec_group **ec_grp, struct cpt_inst_s *inst)
    1048                 :            : {
    1049                 :            :         struct rte_crypto_eddsa_op_param *eddsa = &op->asym->eddsa;
    1050                 :          0 :         uint8_t curveid = sess->ec_ctx.curveid;
    1051                 :            : 
    1052                 :          0 :         if (eddsa->message.length > (sess->msg_max_sz - CNXK_AE_EDDSA_MAX_PARAM_LEN)) {
    1053                 :          0 :                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
    1054                 :          0 :                 return -EINVAL;
    1055                 :            :         }
    1056                 :            : 
    1057         [ #  # ]:          0 :         if (eddsa->op_type == RTE_CRYPTO_ASYM_OP_SIGN)
    1058         [ #  # ]:          0 :                 cnxk_ae_eddsa_sign_prep(eddsa, meta_buf, fpm_iova[curveid], ec_grp[curveid], sess,
    1059                 :            :                                           inst);
    1060         [ #  # ]:          0 :         else if (eddsa->op_type == RTE_CRYPTO_ASYM_OP_VERIFY)
    1061         [ #  # ]:          0 :                 cnxk_ae_eddsa_verify_prep(eddsa, meta_buf, fpm_iova[curveid], ec_grp[curveid], sess,
    1062                 :            :                                           inst);
    1063                 :            :         else {
    1064                 :          0 :                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
    1065                 :          0 :                 return -EINVAL;
    1066                 :            :         }
    1067                 :            :         return 0;
    1068                 :            : }
    1069                 :            : 
    1070                 :            : static __rte_always_inline void
    1071                 :            : cnxk_ae_sm2_sign_prep(struct rte_crypto_sm2_op_param *sm2,
    1072                 :            :                         struct roc_ae_buf_ptr *meta_buf,
    1073                 :            :                         uint64_t fpm_table_iova, struct roc_ae_ec_group *ec_grp,
    1074                 :            :                         struct cnxk_ae_sess *sess, struct cpt_inst_s *inst)
    1075                 :            : {
    1076                 :          0 :         uint16_t message_len = sm2->message.length;
    1077                 :          0 :         uint16_t pkey_len = sess->ec_ctx.pkey.length;
    1078                 :            :         uint16_t p_align, k_align, m_align;
    1079                 :          0 :         uint16_t k_len = sm2->k.length;
    1080                 :            :         uint16_t order_len, prime_len;
    1081                 :            :         uint16_t o_offset, pk_offset;
    1082                 :            :         union cpt_inst_w4 w4;
    1083                 :            :         uint16_t dlen;
    1084                 :            :         uint8_t *dptr;
    1085                 :            : 
    1086                 :          0 :         prime_len = ec_grp->prime.length;
    1087                 :            :         if (prime_len > ROC_AE_EC_DATA_MAX)
    1088                 :            :                 prime_len = ROC_AE_EC_DATA_MAX;
    1089                 :          0 :         order_len = ec_grp->order.length;
    1090                 :            :         if (order_len > ROC_AE_EC_DATA_MAX)
    1091                 :            :                 order_len = ROC_AE_EC_DATA_MAX;
    1092                 :            : 
    1093                 :            :         /* Truncate input length to curve prime length */
    1094                 :            :         if (message_len > prime_len)
    1095                 :            :                 message_len = prime_len;
    1096                 :          0 :         m_align = RTE_ALIGN_CEIL(message_len, 8);
    1097                 :            : 
    1098                 :          0 :         p_align = RTE_ALIGN_CEIL(prime_len, 8);
    1099                 :          0 :         k_align = RTE_ALIGN_CEIL(k_len, 8);
    1100                 :            : 
    1101                 :            :         /* Set write offset for order and private key */
    1102                 :          0 :         o_offset = prime_len - order_len;
    1103                 :          0 :         pk_offset = p_align - pkey_len;
    1104                 :            : 
    1105                 :            :         /* Input buffer */
    1106                 :            :         dptr = meta_buf->vaddr;
    1107                 :          0 :         inst->dptr = (uintptr_t)dptr;
    1108                 :            : 
    1109                 :            :         /*
    1110                 :            :          * Set dlen = sum(sizeof(fpm address), ROUNDUP8(scalar len, input len),
    1111                 :            :          * ROUNDUP8(priv key len, prime len, order len)).
    1112                 :            :          * Please note, private key, order cannot exceed prime
    1113                 :            :          * length i.e 3 * p_align.
    1114                 :            :          */
    1115                 :          0 :         dlen = sizeof(fpm_table_iova) + k_align + m_align + p_align * 5;
    1116                 :            : 
    1117                 :          0 :         memset(dptr, 0, dlen);
    1118                 :            : 
    1119                 :          0 :         *(uint64_t *)dptr = fpm_table_iova;
    1120                 :          0 :         dptr += sizeof(fpm_table_iova);
    1121                 :            : 
    1122         [ #  # ]:          0 :         rte_memcpy(dptr, sm2->k.data, k_len);
    1123                 :          0 :         dptr += k_align;
    1124                 :            : 
    1125         [ #  # ]:          0 :         rte_memcpy(dptr, ec_grp->prime.data, prime_len);
    1126                 :          0 :         dptr += p_align;
    1127                 :            : 
    1128         [ #  # ]:          0 :         rte_memcpy(dptr + o_offset, ec_grp->order.data, order_len);
    1129                 :          0 :         dptr += p_align;
    1130                 :            : 
    1131         [ #  # ]:          0 :         rte_memcpy(dptr + pk_offset, sess->ec_ctx.pkey.data, pkey_len);
    1132                 :          0 :         dptr += p_align;
    1133                 :            : 
    1134         [ #  # ]:          0 :         rte_memcpy(dptr, sm2->message.data, message_len);
    1135                 :          0 :         dptr += m_align;
    1136                 :            : 
    1137         [ #  # ]:          0 :         rte_memcpy(dptr, ec_grp->consta.data, prime_len);
    1138                 :          0 :         dptr += p_align;
    1139                 :            : 
    1140         [ #  # ]:          0 :         rte_memcpy(dptr, ec_grp->constb.data, prime_len);
    1141                 :          0 :         dptr += p_align;
    1142                 :            : 
    1143                 :            :         /* Setup opcodes */
    1144                 :          0 :         w4.s.opcode_major = ROC_AE_MAJOR_OP_EC;
    1145                 :          0 :         w4.s.opcode_minor = ROC_AE_MINOR_OP_EC_SIGN;
    1146                 :            : 
    1147                 :            :         /* prime length of SM2 curve is same as that of P256. */
    1148                 :          0 :         w4.s.param1 = ROC_AE_EC_ID_P256 |
    1149                 :          0 :                 ROC_AE_EC_PARAM1_SM2 | ROC_AE_EC_PARAM1_NONNIST | (message_len << 8);
    1150                 :          0 :         w4.s.param2 = (p_align << 8) | k_len;
    1151                 :          0 :         w4.s.dlen = dlen;
    1152                 :            : 
    1153                 :          0 :         inst->w4.u64 = w4.u64;
    1154                 :          0 :         inst->rptr = (uintptr_t)dptr;
    1155                 :          0 : }
    1156                 :            : 
    1157                 :            : static __rte_always_inline void
    1158                 :            : cnxk_ae_sm2_verify_prep(struct rte_crypto_sm2_op_param *sm2,
    1159                 :            :                           struct roc_ae_buf_ptr *meta_buf,
    1160                 :            :                           uint64_t fpm_table_iova,
    1161                 :            :                           struct roc_ae_ec_group *ec_grp, struct cnxk_ae_sess *sess,
    1162                 :            :                           struct cpt_inst_s *inst)
    1163                 :            : {
    1164                 :          0 :         uint32_t message_len = sm2->message.length;
    1165                 :            :         uint16_t o_offset, r_offset, s_offset;
    1166                 :          0 :         uint16_t qx_len = sess->ec_ctx.q.x.length;
    1167                 :          0 :         uint16_t qy_len = sess->ec_ctx.q.y.length;
    1168                 :          0 :         uint16_t r_len = sm2->r.length;
    1169                 :          0 :         uint16_t s_len = sm2->s.length;
    1170                 :            :         uint16_t order_len, prime_len;
    1171                 :            :         uint16_t qx_offset, qy_offset;
    1172                 :            :         uint16_t p_align, m_align;
    1173                 :            :         union cpt_inst_w4 w4;
    1174                 :            :         uint16_t dlen;
    1175                 :            :         uint8_t *dptr;
    1176                 :            : 
    1177                 :          0 :         prime_len = ec_grp->prime.length;
    1178                 :            :         if (prime_len > ROC_AE_EC_DATA_MAX)
    1179                 :            :                 prime_len = ROC_AE_EC_DATA_MAX;
    1180                 :          0 :         order_len = ec_grp->order.length;
    1181                 :            :         if (order_len > ROC_AE_EC_DATA_MAX)
    1182                 :            :                 order_len = ROC_AE_EC_DATA_MAX;
    1183                 :            : 
    1184                 :            :         /* Truncate input length to curve prime length */
    1185                 :          0 :         if (message_len > prime_len)
    1186                 :            :                 message_len = prime_len;
    1187                 :            : 
    1188                 :          0 :         m_align = RTE_ALIGN_CEIL(message_len, 8);
    1189                 :          0 :         p_align = RTE_ALIGN_CEIL(prime_len, 8);
    1190                 :            : 
    1191                 :            :         /* Set write offset for sign, order and public key coordinates */
    1192                 :          0 :         o_offset = prime_len - order_len;
    1193                 :          0 :         qx_offset = prime_len - qx_len;
    1194                 :          0 :         qy_offset = prime_len - qy_len;
    1195                 :          0 :         r_offset = prime_len - r_len;
    1196                 :          0 :         s_offset = prime_len - s_len;
    1197                 :            : 
    1198                 :            :         /* Input buffer */
    1199                 :            :         dptr = meta_buf->vaddr;
    1200                 :          0 :         inst->dptr = (uintptr_t)dptr;
    1201                 :            : 
    1202                 :            :         /*
    1203                 :            :          * Set dlen = sum(sizeof(fpm address), ROUNDUP8(message len),
    1204                 :            :          * ROUNDUP8(sign len(r and s), public key len(x and y coordinates),
    1205                 :            :          * prime len, order len)).
    1206                 :            :          * Please note sign, public key and order can not exceed prime length
    1207                 :            :          * i.e. 6 * p_align
    1208                 :            :          */
    1209                 :          0 :         dlen = sizeof(fpm_table_iova) + m_align + (8 * p_align);
    1210                 :            : 
    1211                 :          0 :         memset(dptr, 0, dlen);
    1212                 :            : 
    1213                 :          0 :         *(uint64_t *)dptr = fpm_table_iova;
    1214                 :          0 :         dptr += sizeof(fpm_table_iova);
    1215                 :            : 
    1216         [ #  # ]:          0 :         rte_memcpy(dptr + r_offset, sm2->r.data, r_len);
    1217                 :          0 :         dptr += p_align;
    1218                 :            : 
    1219         [ #  # ]:          0 :         rte_memcpy(dptr + s_offset, sm2->s.data, s_len);
    1220                 :          0 :         dptr += p_align;
    1221                 :            : 
    1222         [ #  # ]:          0 :         rte_memcpy(dptr, sm2->message.data, message_len);
    1223                 :          0 :         dptr += m_align;
    1224                 :            : 
    1225         [ #  # ]:          0 :         rte_memcpy(dptr + o_offset, ec_grp->order.data, order_len);
    1226                 :          0 :         dptr += p_align;
    1227                 :            : 
    1228         [ #  # ]:          0 :         rte_memcpy(dptr, ec_grp->prime.data, prime_len);
    1229                 :          0 :         dptr += p_align;
    1230                 :            : 
    1231         [ #  # ]:          0 :         rte_memcpy(dptr + qx_offset, sess->ec_ctx.q.x.data, qx_len);
    1232                 :          0 :         dptr += p_align;
    1233                 :            : 
    1234         [ #  # ]:          0 :         rte_memcpy(dptr + qy_offset, sess->ec_ctx.q.y.data, qy_len);
    1235                 :          0 :         dptr += p_align;
    1236                 :            : 
    1237         [ #  # ]:          0 :         rte_memcpy(dptr, ec_grp->consta.data, prime_len);
    1238                 :          0 :         dptr += p_align;
    1239                 :            : 
    1240         [ #  # ]:          0 :         rte_memcpy(dptr, ec_grp->constb.data, prime_len);
    1241                 :          0 :         dptr += p_align;
    1242                 :            : 
    1243                 :            :         /* Setup opcodes */
    1244                 :          0 :         w4.s.opcode_major = ROC_AE_MAJOR_OP_EC;
    1245                 :          0 :         w4.s.opcode_minor = ROC_AE_MINOR_OP_EC_VERIFY;
    1246                 :            : 
    1247                 :            :         /* prime length of SM2 curve is same as that of P256. */
    1248                 :          0 :         w4.s.param1 = ROC_AE_EC_ID_P256 |
    1249                 :          0 :                 ROC_AE_EC_PARAM1_SM2 | ROC_AE_EC_PARAM1_NONNIST | (message_len << 8);
    1250                 :          0 :         w4.s.param2 = 0;
    1251                 :          0 :         w4.s.dlen = dlen;
    1252                 :            : 
    1253                 :          0 :         inst->w4.u64 = w4.u64;
    1254                 :          0 :         inst->rptr = (uintptr_t)dptr;
    1255                 :          0 : }
    1256                 :            : 
    1257                 :            : static __rte_always_inline int __rte_hot
    1258                 :            : cnxk_ae_enqueue_sm2_op(struct rte_crypto_op *op,
    1259                 :            :                          struct roc_ae_buf_ptr *meta_buf,
    1260                 :            :                          struct cnxk_ae_sess *sess, uint64_t *fpm_iova,
    1261                 :            :                          struct roc_ae_ec_group **ec_grp,
    1262                 :            :                          struct cpt_inst_s *inst)
    1263                 :            : {
    1264                 :            :         struct rte_crypto_sm2_op_param *sm2 = &op->asym->sm2;
    1265                 :          0 :         uint8_t curveid = sess->ec_ctx.curveid;
    1266                 :            : 
    1267                 :          0 :         if (sm2->op_type == RTE_CRYPTO_ASYM_OP_SIGN)
    1268                 :          0 :                 cnxk_ae_sm2_sign_prep(sm2, meta_buf, fpm_iova[curveid],
    1269         [ #  # ]:          0 :                                         ec_grp[curveid], sess, inst);
    1270         [ #  # ]:          0 :         else if (sm2->op_type == RTE_CRYPTO_ASYM_OP_VERIFY)
    1271                 :          0 :                 cnxk_ae_sm2_verify_prep(sm2, meta_buf, fpm_iova[curveid],
    1272         [ #  # ]:          0 :                                           ec_grp[curveid], sess, inst);
    1273                 :            :         else {
    1274                 :          0 :                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
    1275                 :          0 :                 return -EINVAL;
    1276                 :            :         }
    1277                 :            :         return 0;
    1278                 :            : }
    1279                 :            : 
    1280                 :            : static __rte_always_inline int
    1281                 :            : cnxk_ae_ecfpm_prep(rte_crypto_param *scalar,
    1282                 :            :                    struct roc_ae_buf_ptr *meta_buf, uint64_t *fpm_iova,
    1283                 :            :                    struct roc_ae_ec_group *ec_grp, uint8_t curveid,
    1284                 :            :                    struct cpt_inst_s *inst)
    1285                 :            : {
    1286                 :            :         uint16_t scalar_align, p_align;
    1287                 :            :         uint16_t dlen, prime_len;
    1288                 :            :         uint64_t fpm_table_iova;
    1289                 :            :         union cpt_inst_w4 w4;
    1290                 :            :         uint8_t *dptr;
    1291                 :            : 
    1292                 :          0 :         prime_len = ec_grp->prime.length;
    1293                 :          0 :         fpm_table_iova = (uint64_t)fpm_iova[curveid];
    1294                 :            : 
    1295                 :            :         /* Input buffer */
    1296                 :            :         dptr = meta_buf->vaddr;
    1297                 :          0 :         inst->dptr = (uintptr_t)dptr;
    1298                 :            : 
    1299                 :          0 :         p_align = RTE_ALIGN_CEIL(prime_len, 8);
    1300                 :          0 :         scalar_align = RTE_ALIGN_CEIL(scalar->length, 8);
    1301                 :            : 
    1302                 :            :         /*
    1303                 :            :          * Set dlen = sum(ROUNDUP8(input point(x and y coordinates), prime,
    1304                 :            :          * scalar length),
    1305                 :            :          * Please note point length is equivalent to prime of the curve
    1306                 :            :          */
    1307                 :          0 :         dlen = sizeof(fpm_table_iova) + 3 * p_align + scalar_align;
    1308                 :            : 
    1309                 :          0 :         memset(dptr, 0, dlen);
    1310                 :            : 
    1311                 :          0 :         *(uint64_t *)dptr = fpm_table_iova;
    1312                 :          0 :         dptr += sizeof(fpm_table_iova);
    1313                 :            : 
    1314                 :            :         /* Copy scalar, prime */
    1315                 :          0 :         memcpy(dptr, scalar->data, scalar->length);
    1316                 :          0 :         dptr += scalar_align;
    1317                 :          0 :         memcpy(dptr, ec_grp->prime.data, ec_grp->prime.length);
    1318                 :          0 :         dptr += p_align;
    1319                 :          0 :         memcpy(dptr, ec_grp->consta.data, ec_grp->consta.length);
    1320                 :          0 :         dptr += p_align;
    1321                 :          0 :         memcpy(dptr, ec_grp->constb.data, ec_grp->constb.length);
    1322                 :          0 :         dptr += p_align;
    1323                 :            : 
    1324                 :            :         /* Setup opcodes */
    1325                 :          0 :         w4.s.opcode_major = ROC_AE_MAJOR_OP_ECC;
    1326                 :          0 :         w4.s.opcode_minor = ROC_AE_MINOR_OP_ECC_FPM;
    1327                 :            : 
    1328                 :          0 :         w4.s.param1 = curveid | (1 << 8);
    1329                 :          0 :         w4.s.param2 = scalar->length;
    1330                 :          0 :         w4.s.dlen = dlen;
    1331                 :            : 
    1332                 :          0 :         inst->w4.u64 = w4.u64;
    1333                 :          0 :         inst->rptr = (uintptr_t)dptr;
    1334                 :            : 
    1335                 :          0 :         return 0;
    1336                 :            : }
    1337                 :            : 
    1338                 :            : static __rte_always_inline int
    1339                 :            : cnxk_ae_edfpm_prep(rte_crypto_param *scalar, struct roc_ae_buf_ptr *meta_buf, uint64_t *fpm_iova,
    1340                 :            :                    struct roc_ae_ec_group *ec_grp, uint8_t curveid, struct cpt_inst_s *inst)
    1341                 :            : {
    1342                 :          0 :         const uint8_t iv_sha512[] = {
    1343                 :            :                 0x6a, 0x09, 0xe6, 0x67, 0xf3, 0xbc, 0xc9, 0x08,
    1344                 :            :                 0xbb, 0x67, 0xae, 0x85, 0x84, 0xca, 0xa7, 0x3b,
    1345                 :            :                 0x3c, 0x6e, 0xf3, 0x72, 0xfe, 0x94, 0xf8, 0x2b,
    1346                 :            :                 0xa5, 0x4f, 0xf5, 0x3a, 0x5f, 0x1d, 0x36, 0xf1,
    1347                 :            :                 0x51, 0x0e, 0x52, 0x7f, 0xad, 0xe6, 0x82, 0xd1,
    1348                 :            :                 0x9b, 0x05, 0x68, 0x8c, 0x2b, 0x3e, 0x6c, 0x1f,
    1349                 :            :                 0x1f, 0x83, 0xd9, 0xab, 0xfb, 0x41, 0xbd, 0x6b,
    1350                 :            :                 0x5b, 0xe0, 0xcd, 0x19, 0x13, 0x7e, 0x21, 0x79};
    1351                 :            :         uint16_t dlen, prime_len, order_len, iv_len;
    1352                 :            :         uint64_t fpm_table_iova;
    1353                 :            :         uint16_t scalar_align;
    1354                 :            :         union cpt_inst_w4 w4;
    1355                 :            :         uint16_t prime_bit;
    1356                 :            :         uint8_t *dptr;
    1357                 :            : 
    1358                 :          0 :         if (curveid == ROC_AE_EC_ID_ED25519) {
    1359                 :            :                 prime_bit = ROC_AE_ED_PARAM1_25519;
    1360                 :            :                 iv_len = sizeof(iv_sha512);
    1361                 :            :         } else {
    1362                 :            :                 prime_bit = ROC_AE_ED_PARAM1_448;
    1363                 :            :                 iv_len = 0;
    1364                 :            :         }
    1365                 :            : 
    1366                 :          0 :         prime_len = ec_grp->prime.length;
    1367                 :          0 :         order_len = ec_grp->order.length;
    1368                 :          0 :         fpm_table_iova = (uint64_t)fpm_iova[curveid];
    1369                 :            : 
    1370                 :            :         /* Input buffer */
    1371                 :            :         dptr = meta_buf->vaddr;
    1372                 :          0 :         inst->dptr = (uintptr_t)dptr;
    1373                 :            : 
    1374                 :          0 :         scalar_align = RTE_ALIGN_CEIL(scalar->length, 8);
    1375                 :            : 
    1376                 :            :         /*
    1377                 :            :          * Set dlen = sum(sizeof(fpm address), ROUNDUP8(prime len, order len, constant,
    1378                 :            :          * private key len and iv len (if ED25519))).
    1379                 :            :          */
    1380                 :          0 :         dlen = sizeof(fpm_table_iova) + 3 * prime_len + scalar_align + iv_len;
    1381                 :            : 
    1382                 :          0 :         *(uint64_t *)dptr = fpm_table_iova;
    1383                 :          0 :         dptr += sizeof(fpm_table_iova);
    1384                 :            : 
    1385         [ #  # ]:          0 :         memcpy(dptr, ec_grp->prime.data, prime_len);
    1386                 :          0 :         dptr += prime_len;
    1387                 :          0 :         memcpy(dptr, ec_grp->order.data, order_len);
    1388                 :          0 :         dptr += prime_len;
    1389                 :          0 :         memcpy(dptr, ec_grp->consta.data, prime_len);
    1390                 :          0 :         dptr += prime_len;
    1391                 :            : 
    1392                 :            :         memcpy(dptr, scalar->data, scalar->length);
    1393                 :          0 :         dptr += scalar_align;
    1394         [ #  # ]:          0 :         if (curveid == ROC_AE_EC_ID_ED25519) {
    1395                 :            :                 memcpy(dptr, iv_sha512, sizeof(iv_sha512));
    1396                 :          0 :                 dptr += iv_len;
    1397                 :            :         }
    1398                 :            : 
    1399                 :            :         /* Setup opcodes */
    1400                 :          0 :         w4.s.opcode_major = ROC_AE_MAJOR_OP_EDDSA;
    1401                 :          0 :         w4.s.opcode_minor = ROC_AE_MINOR_OP_ED_KEYGEN;
    1402                 :            : 
    1403                 :          0 :         w4.s.param1 = prime_bit;
    1404                 :          0 :         w4.s.param2 = 0;
    1405                 :          0 :         w4.s.dlen = dlen;
    1406                 :            : 
    1407                 :          0 :         inst->w4.u64 = w4.u64;
    1408                 :          0 :         inst->rptr = (uintptr_t)dptr;
    1409                 :            : 
    1410                 :          0 :         return 0;
    1411                 :            : }
    1412                 :            : 
    1413                 :            : static __rte_always_inline int
    1414                 :            : cnxk_ae_ecpm_prep(rte_crypto_param *scalar, struct rte_crypto_ec_point *p,
    1415                 :            :                   struct roc_ae_buf_ptr *meta_buf,
    1416                 :            :                   struct roc_ae_ec_group *ec_grp, uint8_t curveid,
    1417                 :            :                   struct cpt_inst_s *inst)
    1418                 :            : {
    1419                 :          0 :         uint16_t x1_len = p->x.length;
    1420                 :          0 :         uint16_t y1_len = p->y.length;
    1421                 :            :         uint16_t scalar_align, p_align;
    1422                 :            :         uint16_t x1_offset, y1_offset;
    1423                 :            :         uint16_t dlen, prime_len;
    1424                 :            :         union cpt_inst_w4 w4;
    1425                 :            :         uint8_t *dptr;
    1426                 :            : 
    1427                 :          0 :         prime_len = ec_grp->prime.length;
    1428                 :            : 
    1429                 :            :         /* Input buffer */
    1430                 :            :         dptr = meta_buf->vaddr;
    1431                 :          0 :         inst->dptr = (uintptr_t)dptr;
    1432                 :            : 
    1433                 :          0 :         p_align = RTE_ALIGN_CEIL(prime_len, 8);
    1434                 :          0 :         scalar_align = RTE_ALIGN_CEIL(scalar->length, 8);
    1435                 :            : 
    1436                 :            :         /*
    1437                 :            :          * Set dlen = sum(ROUNDUP8(input point(x and y coordinates), prime,
    1438                 :            :          * scalar length),
    1439                 :            :          * Please note point length is equivalent to prime of the curve
    1440                 :            :          */
    1441                 :          0 :         dlen = 5 * p_align + scalar_align;
    1442                 :            : 
    1443                 :          0 :         x1_offset = prime_len - x1_len;
    1444                 :          0 :         y1_offset = prime_len - y1_len;
    1445                 :            : 
    1446                 :          0 :         memset(dptr, 0, dlen);
    1447                 :            : 
    1448                 :            :         /* Copy input point, scalar, prime */
    1449                 :          0 :         memcpy(dptr + x1_offset, p->x.data, x1_len);
    1450                 :          0 :         dptr += p_align;
    1451                 :          0 :         memcpy(dptr + y1_offset, p->y.data, y1_len);
    1452                 :          0 :         dptr += p_align;
    1453                 :          0 :         memcpy(dptr, scalar->data, scalar->length);
    1454                 :          0 :         dptr += scalar_align;
    1455                 :          0 :         memcpy(dptr, ec_grp->prime.data, ec_grp->prime.length);
    1456                 :          0 :         dptr += p_align;
    1457                 :          0 :         memcpy(dptr, ec_grp->consta.data, ec_grp->consta.length);
    1458                 :          0 :         dptr += p_align;
    1459                 :          0 :         memcpy(dptr, ec_grp->constb.data, ec_grp->constb.length);
    1460                 :          0 :         dptr += p_align;
    1461                 :            : 
    1462                 :            :         /* Setup opcodes */
    1463                 :          0 :         w4.s.opcode_major = ROC_AE_MAJOR_OP_ECC;
    1464                 :          0 :         w4.s.opcode_minor = ROC_AE_MINOR_OP_ECC_UMP;
    1465                 :            : 
    1466                 :          0 :         w4.s.param1 = curveid;
    1467                 :          0 :         w4.s.param2 = scalar->length;
    1468                 :          0 :         w4.s.dlen = dlen;
    1469                 :            : 
    1470                 :          0 :         inst->w4.u64 = w4.u64;
    1471                 :          0 :         inst->rptr = (uintptr_t)dptr;
    1472                 :            : 
    1473                 :          0 :         return 0;
    1474                 :            : }
    1475                 :            : 
    1476                 :            : static __rte_always_inline int
    1477                 :            : cnxk_ae_random_prep(uint16_t len, struct roc_ae_buf_ptr *meta_buf,
    1478                 :            :                    struct cpt_inst_s *inst)
    1479                 :            : {
    1480                 :            :         union cpt_inst_w4 w4;
    1481                 :            :         uint8_t *dptr;
    1482                 :            : 
    1483                 :            :         /* Input buffer */
    1484                 :            :         dptr = meta_buf->vaddr;
    1485                 :          0 :         inst->dptr = (uintptr_t)dptr;
    1486                 :            : 
    1487                 :            :         /* Setup opcodes */
    1488                 :          0 :         w4.s.opcode_major = ROC_AE_MAJOR_OP_RANDOM;
    1489                 :          0 :         w4.s.opcode_minor = ROC_AE_MINOR_OP_RANDOM;
    1490                 :            : 
    1491                 :          0 :         w4.s.param1 = len;
    1492                 :          0 :         w4.s.param2 = 0;
    1493                 :          0 :         w4.s.dlen = 0;
    1494                 :            : 
    1495                 :          0 :         inst->w4.u64 = w4.u64;
    1496                 :          0 :         inst->rptr = (uintptr_t)dptr;
    1497                 :            : 
    1498                 :          0 :         return 0;
    1499                 :            : }
    1500                 :            : 
    1501                 :            : static __rte_always_inline int __rte_hot
    1502                 :            : cnxk_ae_enqueue_ecdh_op(struct rte_crypto_op *op,
    1503                 :            :                          struct roc_ae_buf_ptr *meta_buf,
    1504                 :            :                          struct cnxk_ae_sess *sess, uint64_t *fpm_iova,
    1505                 :            :                          struct roc_ae_ec_group **ec_grp,
    1506                 :            :                          struct cpt_inst_s *inst)
    1507                 :            : {
    1508                 :            :         struct rte_crypto_ecdh_op_param *ecdh = &op->asym->ecdh;
    1509                 :          0 :         uint8_t curveid = sess->ec_ctx.curveid;
    1510                 :            :         struct rte_crypto_ec_point point;
    1511                 :            :         rte_crypto_uint scalar;
    1512                 :            :         int ret = 0;
    1513                 :            : 
    1514                 :          0 :         switch (ecdh->ke_type) {
    1515                 :          0 :         case RTE_CRYPTO_ASYM_KE_PRIV_KEY_GENERATE:
    1516                 :          0 :                 cnxk_ae_random_prep(ecdh->priv_key.length, meta_buf, inst);
    1517                 :            :                 break;
    1518                 :          0 :         case RTE_CRYPTO_ASYM_KE_PUB_KEY_GENERATE:
    1519                 :          0 :                 scalar.data = sess->ec_ctx.pkey.data;
    1520                 :          0 :                 scalar.length = sess->ec_ctx.pkey.length;
    1521         [ #  # ]:          0 :                 if (curveid == ROC_AE_EC_ID_ED25519 || curveid == ROC_AE_EC_ID_ED448)
    1522         [ #  # ]:          0 :                         cnxk_ae_edfpm_prep(&scalar, meta_buf, fpm_iova, ec_grp[curveid],
    1523                 :            :                                 curveid, inst);
    1524                 :            :                 else
    1525                 :          0 :                         cnxk_ae_ecfpm_prep(&scalar, meta_buf, fpm_iova, ec_grp[curveid],
    1526                 :            :                                 curveid, inst);
    1527                 :            :                 break;
    1528                 :          0 :         case RTE_CRYPTO_ASYM_KE_PUB_KEY_VERIFY:
    1529                 :          0 :                 scalar.data = ec_grp[curveid]->order.data;
    1530                 :          0 :                 scalar.length = ec_grp[curveid]->order.length;
    1531                 :            :                 cnxk_ae_ecpm_prep(&scalar, &ecdh->pub_key, meta_buf,
    1532                 :            :                         ec_grp[curveid], curveid, inst);
    1533                 :            :                 break;
    1534                 :          0 :         case RTE_CRYPTO_ASYM_KE_SHARED_SECRET_COMPUTE:
    1535                 :          0 :                 scalar.data = sess->ec_ctx.pkey.data;
    1536                 :          0 :                 scalar.length = sess->ec_ctx.pkey.length;
    1537                 :          0 :                 point.x.data = sess->ec_ctx.q.x.data;
    1538                 :          0 :                 point.x.length = sess->ec_ctx.q.x.length;
    1539                 :          0 :                 point.y.data = sess->ec_ctx.q.y.data;
    1540                 :          0 :                 point.y.length = sess->ec_ctx.q.y.length;
    1541                 :          0 :                 cnxk_ae_ecpm_prep(&scalar, &point, meta_buf,
    1542                 :          0 :                         ec_grp[curveid], curveid, inst);
    1543                 :            :                 break;
    1544                 :          0 :         default:
    1545                 :          0 :                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
    1546                 :            :                 ret = -EINVAL;
    1547                 :            :         }
    1548                 :            : 
    1549                 :            :         return ret;
    1550                 :            : }
    1551                 :            : 
    1552                 :            : static __rte_always_inline void
    1553                 :            : cnxk_ae_dequeue_rsa_op(struct rte_crypto_op *cop, uint8_t *rptr,
    1554                 :            :                        struct rte_crypto_rsa_xform *rsa_ctx)
    1555                 :            : {
    1556                 :            :         struct rte_crypto_rsa_op_param *rsa = &cop->asym->rsa;
    1557                 :            : 
    1558                 :          0 :         switch (rsa->op_type) {
    1559                 :          0 :         case RTE_CRYPTO_ASYM_OP_ENCRYPT:
    1560                 :          0 :                 rsa->cipher.length = rsa_ctx->n.length;
    1561                 :          0 :                 memcpy(rsa->cipher.data, rptr, rsa->cipher.length);
    1562                 :            :                 break;
    1563                 :          0 :         case RTE_CRYPTO_ASYM_OP_DECRYPT:
    1564         [ #  # ]:          0 :                 if (rsa_ctx->padding.type == RTE_CRYPTO_RSA_PADDING_NONE) {
    1565                 :          0 :                         rsa->message.length = rsa_ctx->n.length;
    1566                 :          0 :                         memcpy(rsa->message.data, rptr, rsa->message.length);
    1567                 :            :                 } else {
    1568                 :            :                         /* Get length of decrypted output */
    1569                 :          0 :                         rsa->message.length =
    1570         [ #  # ]:          0 :                                 rte_cpu_to_be_16(*((uint16_t *)rptr));
    1571                 :            :                         /*
    1572                 :            :                          * Offset output data pointer by length field
    1573                 :            :                          * (2 bytes) and copy decrypted data.
    1574                 :            :                          */
    1575                 :          0 :                         memcpy(rsa->message.data, rptr + 2,
    1576                 :            :                                rsa->message.length);
    1577                 :            :                 }
    1578                 :            :                 break;
    1579                 :          0 :         case RTE_CRYPTO_ASYM_OP_SIGN:
    1580                 :          0 :                 rsa->sign.length = rsa_ctx->n.length;
    1581                 :          0 :                 memcpy(rsa->sign.data, rptr, rsa->sign.length);
    1582                 :            :                 break;
    1583                 :          0 :         case RTE_CRYPTO_ASYM_OP_VERIFY:
    1584         [ #  # ]:          0 :                 if (rsa_ctx->padding.type == RTE_CRYPTO_RSA_PADDING_NONE) {
    1585                 :          0 :                         rsa->sign.length = rsa_ctx->n.length;
    1586                 :          0 :                         memcpy(rsa->sign.data, rptr, rsa->sign.length);
    1587                 :            :                 } else {
    1588                 :            :                         /* Get length of signed output */
    1589                 :          0 :                         rsa->sign.length =
    1590         [ #  # ]:          0 :                                 rte_cpu_to_be_16(*((uint16_t *)rptr));
    1591                 :            :                         /*
    1592                 :            :                          * Offset output data pointer by length field
    1593                 :            :                          * (2 bytes) and copy signed data.
    1594                 :            :                          */
    1595                 :          0 :                         memcpy(rsa->sign.data, rptr + 2, rsa->sign.length);
    1596                 :            :                 }
    1597         [ #  # ]:          0 :                 if (memcmp(rsa->sign.data, rsa->message.data,
    1598                 :            :                            rsa->message.length)) {
    1599                 :          0 :                         cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
    1600                 :            :                 }
    1601                 :            :                 break;
    1602                 :          0 :         default:
    1603                 :          0 :                 cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
    1604                 :          0 :                 break;
    1605                 :            :         }
    1606                 :            : }
    1607                 :            : 
    1608                 :            : static __rte_always_inline void
    1609                 :            : cnxk_ae_dequeue_ecdsa_op(struct rte_crypto_ecdsa_op_param *ecdsa, uint8_t *rptr,
    1610                 :            :                          struct roc_ae_ec_ctx *ec,
    1611                 :            :                          struct roc_ae_ec_group **ec_grp)
    1612                 :            : {
    1613                 :          0 :         int prime_len = ec_grp[ec->curveid]->prime.length;
    1614                 :            : 
    1615                 :          0 :         if (ecdsa->op_type == RTE_CRYPTO_ASYM_OP_VERIFY)
    1616                 :            :                 return;
    1617                 :            : 
    1618                 :            :         /* Separate out sign r and s components */
    1619                 :          0 :         memcpy(ecdsa->r.data, rptr, prime_len);
    1620                 :          0 :         memcpy(ecdsa->s.data, rptr + RTE_ALIGN_CEIL(prime_len, 8), prime_len);
    1621                 :          0 :         ecdsa->r.length = prime_len;
    1622                 :          0 :         ecdsa->s.length = prime_len;
    1623                 :            : }
    1624                 :            : 
    1625                 :            : static __rte_always_inline void
    1626                 :            : cnxk_ae_dequeue_eddsa_op(struct rte_crypto_eddsa_op_param *eddsa, uint8_t *rptr)
    1627                 :            : {
    1628                 :          0 :         if (eddsa->op_type == RTE_CRYPTO_ASYM_OP_VERIFY)
    1629                 :            :                 return;
    1630                 :            : 
    1631                 :            :         /* Separate out sign r and s components */
    1632         [ #  # ]:          0 :         if (eddsa->instance == RTE_CRYPTO_EDCURVE_25519 ||
    1633                 :            :                 eddsa->instance == RTE_CRYPTO_EDCURVE_25519CTX ||
    1634                 :            :                 eddsa->instance == RTE_CRYPTO_EDCURVE_25519PH) {
    1635                 :          0 :                 eddsa->sign.length = 64;
    1636                 :          0 :                 memcpy(eddsa->sign.data, rptr, eddsa->sign.length);
    1637                 :            :         } else {
    1638                 :          0 :                 eddsa->sign.length = 114;
    1639                 :          0 :                 memcpy(eddsa->sign.data, rptr, 57);
    1640                 :          0 :                 memcpy(eddsa->sign.data + 57, rptr + 64, 57);
    1641                 :            :         }
    1642                 :            : }
    1643                 :            : 
    1644                 :            : static __rte_always_inline void
    1645                 :            : cnxk_ae_dequeue_sm2_op(struct rte_crypto_sm2_op_param *sm2, uint8_t *rptr,
    1646                 :            :                          struct roc_ae_ec_ctx *ec,
    1647                 :            :                          struct roc_ae_ec_group **ec_grp)
    1648                 :            : {
    1649                 :          0 :         int prime_len = ec_grp[ec->curveid]->prime.length;
    1650                 :            : 
    1651                 :          0 :         if (sm2->op_type == RTE_CRYPTO_ASYM_OP_VERIFY)
    1652                 :            :                 return;
    1653                 :            : 
    1654                 :            :         /* Separate out sign r and s components */
    1655         [ #  # ]:          0 :         rte_memcpy(sm2->r.data, rptr, prime_len);
    1656         [ #  # ]:          0 :         rte_memcpy(sm2->s.data, rptr + RTE_ALIGN_CEIL(prime_len, 8), prime_len);
    1657                 :          0 :         sm2->r.length = prime_len;
    1658                 :          0 :         sm2->s.length = prime_len;
    1659                 :            : }
    1660                 :            : 
    1661                 :            : static __rte_always_inline void
    1662                 :            : cnxk_ae_dequeue_ecpm_op(struct rte_crypto_ecpm_op_param *ecpm, uint8_t *rptr,
    1663                 :            :                         struct roc_ae_ec_ctx *ec,
    1664                 :            :                         struct roc_ae_ec_group **ec_grp)
    1665                 :            : {
    1666                 :          0 :         int prime_len = ec_grp[ec->curveid]->prime.length;
    1667                 :            : 
    1668                 :          0 :         memcpy(ecpm->r.x.data, rptr, prime_len);
    1669                 :          0 :         memcpy(ecpm->r.y.data, rptr + RTE_ALIGN_CEIL(prime_len, 8), prime_len);
    1670                 :          0 :         ecpm->r.x.length = prime_len;
    1671                 :          0 :         ecpm->r.y.length = prime_len;
    1672                 :          0 : }
    1673                 :            : 
    1674                 :            : static __rte_always_inline void
    1675                 :            : cnxk_ae_dequeue_ecdh_op(struct rte_crypto_ecdh_op_param *ecdh, uint8_t *rptr,
    1676                 :            :                         struct roc_ae_ec_ctx *ec,
    1677                 :            :                         struct roc_ae_ec_group **ec_grp, uint16_t flags)
    1678                 :            : {
    1679                 :          0 :         int prime_len = ec_grp[ec->curveid]->prime.length;
    1680                 :            : 
    1681                 :          0 :         switch (ecdh->ke_type) {
    1682                 :          0 :         case RTE_CRYPTO_ASYM_KE_PRIV_KEY_GENERATE:
    1683                 :          0 :                 memcpy(ecdh->priv_key.data, rptr, prime_len);
    1684                 :          0 :                 ecdh->priv_key.length = prime_len;
    1685                 :          0 :                 break;
    1686                 :          0 :         case RTE_CRYPTO_ASYM_KE_PUB_KEY_GENERATE:
    1687         [ #  # ]:          0 :                 memcpy(ecdh->pub_key.x.data, rptr, prime_len);
    1688                 :          0 :                 ecdh->pub_key.x.length = prime_len;
    1689         [ #  # ]:          0 :                 if (!(flags & RTE_CRYPTO_ASYM_FLAG_PUB_KEY_COMPRESSED)) {
    1690                 :          0 :                         memcpy(ecdh->pub_key.y.data, rptr + RTE_ALIGN_CEIL(prime_len, 8),
    1691                 :            :                                 prime_len);
    1692                 :          0 :                         ecdh->pub_key.y.length = prime_len;
    1693                 :            :                 }
    1694                 :            :                 break;
    1695                 :            :         case RTE_CRYPTO_ASYM_KE_PUB_KEY_VERIFY:
    1696                 :            :                 break;
    1697                 :          0 :         case RTE_CRYPTO_ASYM_KE_SHARED_SECRET_COMPUTE:
    1698                 :          0 :                 memcpy(ecdh->shared_secret.x.data, rptr, prime_len);
    1699                 :          0 :                 memcpy(ecdh->shared_secret.y.data, rptr + RTE_ALIGN_CEIL(prime_len, 8), prime_len);
    1700                 :          0 :                 ecdh->shared_secret.x.length = prime_len;
    1701                 :          0 :                 ecdh->shared_secret.y.length = prime_len;
    1702                 :          0 :                 break;
    1703                 :            :         default:
    1704                 :            :                 break;
    1705                 :            :         }
    1706                 :            : }
    1707                 :            : 
    1708                 :            : static __rte_always_inline void *
    1709                 :            : cnxk_ae_alloc_meta(struct roc_ae_buf_ptr *buf,
    1710                 :            :                    struct rte_mempool *cpt_meta_pool,
    1711                 :            :                    struct cpt_inflight_req *infl_req)
    1712                 :            : {
    1713                 :            :         uint8_t *mdata;
    1714                 :            : 
    1715         [ #  # ]:          0 :         if (unlikely(rte_mempool_get(cpt_meta_pool, (void **)&mdata) < 0))
    1716                 :            :                 return NULL;
    1717                 :            : 
    1718                 :          0 :         buf->vaddr = mdata;
    1719                 :            : 
    1720                 :          0 :         infl_req->mdata = mdata;
    1721                 :          0 :         infl_req->op_flags |= CPT_OP_FLAGS_METABUF;
    1722                 :            : 
    1723                 :            :         return mdata;
    1724                 :            : }
    1725                 :            : 
    1726                 :            : static __rte_always_inline int32_t __rte_hot
    1727                 :            : cnxk_ae_enqueue(struct cnxk_cpt_qp *qp, struct rte_crypto_op *op,
    1728                 :            :                 struct cpt_inflight_req *infl_req, struct cpt_inst_s *inst,
    1729                 :            :                 struct cnxk_ae_sess *sess)
    1730                 :            : {
    1731                 :            :         struct cpt_qp_meta_info *minfo = &qp->meta_info;
    1732                 :            :         struct rte_crypto_asym_op *asym_op = op->asym;
    1733                 :            :         struct roc_ae_buf_ptr meta_buf;
    1734                 :            :         uint64_t *mop;
    1735                 :            :         void *mdata;
    1736                 :            :         int ret;
    1737                 :            : 
    1738         [ #  # ]:          0 :         mdata = cnxk_ae_alloc_meta(&meta_buf, minfo->pool, infl_req);
    1739         [ #  # ]:          0 :         if (mdata == NULL)
    1740                 :            :                 return -ENOMEM;
    1741                 :            : 
    1742                 :            :         /* Reserve 8B for RPTR */
    1743                 :          0 :         meta_buf.vaddr = PLT_PTR_ADD(mdata, sizeof(uint64_t));
    1744                 :            : 
    1745   [ #  #  #  #  :          0 :         switch (sess->xfrm_type) {
             #  #  #  #  
                      # ]
    1746         [ #  # ]:          0 :         case RTE_CRYPTO_ASYM_XFORM_MODEX:
    1747                 :            :                 ret = cnxk_ae_modex_prep(op, &meta_buf, &sess->mod_ctx, inst);
    1748         [ #  # ]:          0 :                 if (unlikely(ret))
    1749                 :          0 :                         goto req_fail;
    1750                 :            :                 break;
    1751                 :            :         case RTE_CRYPTO_ASYM_XFORM_RSA:
    1752                 :            :                 ret = cnxk_ae_enqueue_rsa_op(op, &meta_buf, sess, inst);
    1753         [ #  # ]:          0 :                 if (unlikely(ret))
    1754                 :          0 :                         goto req_fail;
    1755                 :            :                 break;
    1756                 :          0 :         case RTE_CRYPTO_ASYM_XFORM_ECDSA:
    1757         [ #  # ]:          0 :                 ret = cnxk_ae_enqueue_ecdsa_op(op, &meta_buf, sess,
    1758                 :            :                                                sess->cnxk_fpm_iova,
    1759                 :            :                                                sess->ec_grp, inst);
    1760         [ #  # ]:          0 :                 if (unlikely(ret))
    1761                 :          0 :                         goto req_fail;
    1762                 :            :                 break;
    1763                 :          0 :         case RTE_CRYPTO_ASYM_XFORM_EDDSA:
    1764         [ #  # ]:          0 :                 ret = cnxk_ae_enqueue_eddsa_op(op, &meta_buf, sess,
    1765                 :            :                                                sess->cnxk_fpm_iova,
    1766                 :            :                                                sess->ec_grp, inst);
    1767         [ #  # ]:          0 :                 if (unlikely(ret))
    1768                 :          0 :                         goto req_fail;
    1769                 :            :                 break;
    1770                 :          0 :         case RTE_CRYPTO_ASYM_XFORM_SM2:
    1771         [ #  # ]:          0 :                 ret = cnxk_ae_enqueue_sm2_op(op, &meta_buf, sess,
    1772                 :            :                                                sess->cnxk_fpm_iova,
    1773                 :            :                                                sess->ec_grp, inst);
    1774         [ #  # ]:          0 :                 if (unlikely(ret))
    1775                 :          0 :                         goto req_fail;
    1776                 :            :                 break;
    1777                 :          0 :         case RTE_CRYPTO_ASYM_XFORM_ECPM:
    1778                 :          0 :                 ret = cnxk_ae_ecpm_prep(&asym_op->ecpm.scalar, &asym_op->ecpm.p, &meta_buf,
    1779                 :          0 :                                         sess->ec_grp[sess->ec_ctx.curveid],
    1780                 :          0 :                                         sess->ec_ctx.curveid, inst);
    1781                 :            :                 if (unlikely(ret))
    1782                 :            :                         goto req_fail;
    1783                 :            :                 break;
    1784                 :          0 :         case RTE_CRYPTO_ASYM_XFORM_ECFPM:
    1785                 :          0 :                 ret = cnxk_ae_ecfpm_prep(&asym_op->ecpm.scalar, &meta_buf,
    1786                 :            :                                          sess->cnxk_fpm_iova,
    1787                 :          0 :                                          sess->ec_grp[sess->ec_ctx.curveid],
    1788                 :          0 :                                          sess->ec_ctx.curveid, inst);
    1789                 :            :                 if (unlikely(ret))
    1790                 :            :                         goto req_fail;
    1791                 :            :                 break;
    1792                 :          0 :         case RTE_CRYPTO_ASYM_XFORM_ECDH:
    1793   [ #  #  #  #  :          0 :                 ret = cnxk_ae_enqueue_ecdh_op(op, &meta_buf, sess,
                      # ]
    1794                 :            :                                                sess->cnxk_fpm_iova,
    1795                 :            :                                                sess->ec_grp, inst);
    1796         [ #  # ]:          0 :                 if (unlikely(ret))
    1797                 :          0 :                         goto req_fail;
    1798                 :            :                 break;
    1799                 :          0 :         default:
    1800                 :          0 :                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
    1801                 :            :                 ret = -EINVAL;
    1802                 :          0 :                 goto req_fail;
    1803                 :            :         }
    1804                 :            : 
    1805                 :            :         mop = mdata;
    1806                 :          0 :         mop[0] = inst->rptr;
    1807                 :          0 :         return 0;
    1808                 :            : 
    1809                 :          0 : req_fail:
    1810         [ #  # ]:          0 :         rte_mempool_put(minfo->pool, infl_req->mdata);
    1811                 :          0 :         return ret;
    1812                 :            : }
    1813                 :            : 
    1814                 :            : static __rte_always_inline void
    1815                 :            : cnxk_ae_post_process(struct rte_crypto_op *cop, struct cnxk_ae_sess *sess,
    1816                 :            :                      uint8_t *rptr)
    1817                 :            : {
    1818                 :            :         struct rte_crypto_asym_op *op = cop->asym;
    1819                 :            : 
    1820   [ #  #  #  #  :          0 :         switch (sess->xfrm_type) {
             #  #  #  # ]
    1821   [ #  #  #  #  :          0 :         case RTE_CRYPTO_ASYM_XFORM_RSA:
                      # ]
    1822                 :            :                 cnxk_ae_dequeue_rsa_op(cop, rptr, &sess->rsa_ctx);
    1823                 :            :                 break;
    1824                 :          0 :         case RTE_CRYPTO_ASYM_XFORM_MODEX:
    1825                 :          0 :                 op->modex.result.length = sess->mod_ctx.modulus.length;
    1826                 :          0 :                 memcpy(op->modex.result.data, rptr, op->modex.result.length);
    1827                 :            :                 break;
    1828                 :          0 :         case RTE_CRYPTO_ASYM_XFORM_ECDSA:
    1829         [ #  # ]:          0 :                 cnxk_ae_dequeue_ecdsa_op(&op->ecdsa, rptr, &sess->ec_ctx,
    1830                 :            :                                          sess->ec_grp);
    1831                 :            :                 break;
    1832         [ #  # ]:          0 :         case RTE_CRYPTO_ASYM_XFORM_EDDSA:
    1833                 :            :                 cnxk_ae_dequeue_eddsa_op(&op->eddsa, rptr);
    1834                 :            :                 break;
    1835                 :          0 :         case RTE_CRYPTO_ASYM_XFORM_SM2:
    1836         [ #  # ]:          0 :                 cnxk_ae_dequeue_sm2_op(&op->sm2, rptr, &sess->ec_ctx,
    1837                 :            :                                          sess->ec_grp);
    1838                 :            :                 break;
    1839                 :          0 :         case RTE_CRYPTO_ASYM_XFORM_ECPM:
    1840                 :            :         case RTE_CRYPTO_ASYM_XFORM_ECFPM:
    1841                 :          0 :                 cnxk_ae_dequeue_ecpm_op(&op->ecpm, rptr, &sess->ec_ctx,
    1842                 :            :                                         sess->ec_grp);
    1843                 :            :                 break;
    1844                 :          0 :         case RTE_CRYPTO_ASYM_XFORM_ECDH:
    1845                 :          0 :                 cnxk_ae_dequeue_ecdh_op(&op->ecdh, rptr, &sess->ec_ctx,
    1846   [ #  #  #  # ]:          0 :                                         sess->ec_grp, op->flags);
    1847                 :            :                 break;
    1848                 :          0 :         default:
    1849                 :          0 :                 cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
    1850                 :          0 :                 break;
    1851                 :            :         }
    1852                 :            : }
    1853                 :            : #endif /* _CNXK_AE_H_ */

Generated by: LCOV version 1.14