LCOV - code coverage report
Current view: top level - drivers/crypto/cnxk - cn10k_tls.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 406 0.0 %
Date: 2026-11-01 19:11:22 Functions: 0 13 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 320 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(C) 2024 Marvell.
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <rte_crypto_sym.h>
       6                 :            : #include <rte_cryptodev.h>
       7                 :            : #include <rte_security.h>
       8                 :            : 
       9                 :            : #include <cryptodev_pmd.h>
      10                 :            : 
      11                 :            : #include "roc_cpt.h"
      12                 :            : #include "roc_se.h"
      13                 :            : 
      14                 :            : #include "cn10k_cryptodev_sec.h"
      15                 :            : #include "cn10k_tls.h"
      16                 :            : #include "cnxk_cryptodev.h"
      17                 :            : #include "cnxk_cryptodev_ops.h"
      18                 :            : #include "cnxk_security.h"
      19                 :            : 
      20                 :            : static int
      21                 :          0 : tls_xform_cipher_auth_verify(struct rte_crypto_sym_xform *cipher_xform,
      22                 :            :                              struct rte_crypto_sym_xform *auth_xform)
      23                 :            : {
      24                 :          0 :         enum rte_crypto_cipher_algorithm c_algo = cipher_xform->cipher.algo;
      25                 :          0 :         enum rte_crypto_auth_algorithm a_algo = auth_xform->auth.algo;
      26                 :            :         int ret = -ENOTSUP;
      27                 :            : 
      28   [ #  #  #  # ]:          0 :         switch (c_algo) {
      29                 :          0 :         case RTE_CRYPTO_CIPHER_NULL:
      30         [ #  # ]:          0 :                 if ((a_algo == RTE_CRYPTO_AUTH_MD5_HMAC) || (a_algo == RTE_CRYPTO_AUTH_SHA1_HMAC) ||
      31                 :          0 :                     (a_algo == RTE_CRYPTO_AUTH_SHA256_HMAC) ||
      32         [ #  # ]:          0 :                     (a_algo == RTE_CRYPTO_AUTH_SHA384_HMAC))
      33                 :            :                         ret = 0;
      34                 :            :                 break;
      35                 :          0 :         case RTE_CRYPTO_CIPHER_3DES_CBC:
      36         [ #  # ]:          0 :                 if (a_algo == RTE_CRYPTO_AUTH_SHA1_HMAC)
      37                 :            :                         ret = 0;
      38                 :            :                 break;
      39                 :          0 :         case RTE_CRYPTO_CIPHER_AES_CBC:
      40                 :          0 :                 if ((a_algo == RTE_CRYPTO_AUTH_SHA1_HMAC) ||
      41   [ #  #  #  # ]:          0 :                     (a_algo == RTE_CRYPTO_AUTH_SHA256_HMAC) ||
      42                 :            :                     (a_algo == RTE_CRYPTO_AUTH_SHA384_HMAC))
      43                 :            :                         ret = 0;
      44                 :            :                 break;
      45                 :            :         default:
      46                 :            :                 break;
      47                 :            :         }
      48                 :            : 
      49                 :          0 :         return ret;
      50                 :            : }
      51                 :            : 
      52                 :            : static int
      53                 :          0 : tls_xform_cipher_verify(struct rte_crypto_sym_xform *crypto_xform)
      54                 :            : {
      55                 :          0 :         enum rte_crypto_cipher_algorithm c_algo = crypto_xform->cipher.algo;
      56                 :          0 :         uint16_t keylen = crypto_xform->cipher.key.length;
      57                 :            : 
      58         [ #  # ]:          0 :         if (((c_algo == RTE_CRYPTO_CIPHER_NULL) && (keylen == 0)) ||
      59   [ #  #  #  # ]:          0 :             ((c_algo == RTE_CRYPTO_CIPHER_3DES_CBC) && (keylen == 24)) ||
      60         [ #  # ]:          0 :             ((c_algo == RTE_CRYPTO_CIPHER_AES_CBC) && ((keylen == 16) || (keylen == 32))))
      61                 :          0 :                 return 0;
      62                 :            : 
      63                 :            :         return -EINVAL;
      64                 :            : }
      65                 :            : 
      66                 :            : static int
      67                 :          0 : tls_xform_auth_verify(struct rte_crypto_sym_xform *crypto_xform)
      68                 :            : {
      69                 :          0 :         enum rte_crypto_auth_algorithm a_algo = crypto_xform->auth.algo;
      70                 :          0 :         uint16_t keylen = crypto_xform->auth.key.length;
      71                 :            : 
      72         [ #  # ]:          0 :         if (((a_algo == RTE_CRYPTO_AUTH_MD5_HMAC) && (keylen == 16)) ||
      73         [ #  # ]:          0 :             ((a_algo == RTE_CRYPTO_AUTH_SHA1_HMAC) && (keylen == 20)) ||
      74         [ #  # ]:          0 :             ((a_algo == RTE_CRYPTO_AUTH_SHA256_HMAC) && (keylen == 32)) ||
      75         [ #  # ]:          0 :             ((a_algo == RTE_CRYPTO_AUTH_SHA384_HMAC) && (keylen == 48)))
      76                 :          0 :                 return 0;
      77                 :            : 
      78                 :            :         return -EINVAL;
      79                 :            : }
      80                 :            : 
      81                 :            : static int
      82                 :          0 : tls_xform_aead_verify(struct rte_security_tls_record_xform *tls_xform,
      83                 :            :                       struct rte_crypto_sym_xform *crypto_xform)
      84                 :            : {
      85                 :          0 :         uint16_t keylen = crypto_xform->aead.key.length;
      86                 :            : 
      87         [ #  # ]:          0 :         if (tls_xform->type == RTE_SECURITY_TLS_SESS_TYPE_WRITE &&
      88         [ #  # ]:          0 :             crypto_xform->aead.op != RTE_CRYPTO_AEAD_OP_ENCRYPT)
      89                 :            :                 return -EINVAL;
      90                 :            : 
      91         [ #  # ]:          0 :         if (tls_xform->type == RTE_SECURITY_TLS_SESS_TYPE_READ &&
      92         [ #  # ]:          0 :             crypto_xform->aead.op != RTE_CRYPTO_AEAD_OP_DECRYPT)
      93                 :            :                 return -EINVAL;
      94                 :            : 
      95         [ #  # ]:          0 :         if (crypto_xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM) {
      96         [ #  # ]:          0 :                 if ((keylen == 16) || (keylen == 32))
      97                 :            :                         return 0;
      98                 :            :         }
      99                 :            : 
     100   [ #  #  #  # ]:          0 :         if ((crypto_xform->aead.algo == RTE_CRYPTO_AEAD_CHACHA20_POLY1305) && (keylen == 32))
     101                 :          0 :                 return 0;
     102                 :            : 
     103                 :            :         return -EINVAL;
     104                 :            : }
     105                 :            : 
     106                 :            : static int
     107                 :          0 : cnxk_tls_xform_verify(struct rte_security_tls_record_xform *tls_xform,
     108                 :            :                       struct rte_crypto_sym_xform *crypto_xform)
     109                 :            : {
     110                 :            :         struct rte_crypto_sym_xform *auth_xform, *cipher_xform = NULL;
     111                 :            :         int ret = 0;
     112                 :            : 
     113         [ #  # ]:          0 :         if ((tls_xform->ver != RTE_SECURITY_VERSION_TLS_1_2) &&
     114         [ #  # ]:          0 :             (tls_xform->ver != RTE_SECURITY_VERSION_DTLS_1_2) &&
     115                 :            :             (tls_xform->ver != RTE_SECURITY_VERSION_TLS_1_3))
     116                 :            :                 return -EINVAL;
     117                 :            : 
     118         [ #  # ]:          0 :         if ((tls_xform->type != RTE_SECURITY_TLS_SESS_TYPE_READ) &&
     119                 :            :             (tls_xform->type != RTE_SECURITY_TLS_SESS_TYPE_WRITE))
     120                 :            :                 return -EINVAL;
     121                 :            : 
     122         [ #  # ]:          0 :         if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
     123                 :            :                 /* optional padding is not allowed in TLS-1.2 for AEAD */
     124   [ #  #  #  # ]:          0 :                 if ((tls_xform->options.extra_padding_enable == 1) &&
     125                 :            :                     (tls_xform->ver != RTE_SECURITY_VERSION_TLS_1_3))
     126                 :            :                         return -EINVAL;
     127                 :            : 
     128                 :          0 :                 return tls_xform_aead_verify(tls_xform, crypto_xform);
     129                 :            :         }
     130                 :            : 
     131                 :            :         /* TLS-1.3 only support AEAD.
     132                 :            :          * Control should not reach here for TLS-1.3
     133                 :            :          */
     134         [ #  # ]:          0 :         if (tls_xform->ver == RTE_SECURITY_VERSION_TLS_1_3)
     135                 :            :                 return -EINVAL;
     136                 :            : 
     137         [ #  # ]:          0 :         if (tls_xform->type == RTE_SECURITY_TLS_SESS_TYPE_WRITE) {
     138                 :            :                 /* Egress */
     139                 :            : 
     140                 :            :                 /* First should be for auth in Egress */
     141         [ #  # ]:          0 :                 if (crypto_xform->type != RTE_CRYPTO_SYM_XFORM_AUTH)
     142                 :            :                         return -EINVAL;
     143                 :            : 
     144                 :            :                 /* Next if present, should be for cipher in Egress */
     145         [ #  # ]:          0 :                 if ((crypto_xform->next != NULL) &&
     146         [ #  # ]:          0 :                     (crypto_xform->next->type != RTE_CRYPTO_SYM_XFORM_CIPHER))
     147                 :            :                         return -EINVAL;
     148                 :            : 
     149                 :            :                 auth_xform = crypto_xform;
     150                 :            :                 cipher_xform = crypto_xform->next;
     151                 :            :         } else {
     152                 :            :                 /* Ingress */
     153                 :            : 
     154                 :            :                 /* First can be for auth only when next is NULL in Ingress. */
     155         [ #  # ]:          0 :                 if ((crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) &&
     156         [ #  # ]:          0 :                     (crypto_xform->next != NULL))
     157                 :            :                         return -EINVAL;
     158         [ #  # ]:          0 :                 else if ((crypto_xform->type != RTE_CRYPTO_SYM_XFORM_CIPHER) ||
     159         [ #  # ]:          0 :                          (crypto_xform->next->type != RTE_CRYPTO_SYM_XFORM_AUTH))
     160                 :            :                         return -EINVAL;
     161                 :            : 
     162                 :            :                 cipher_xform = crypto_xform;
     163                 :            :                 auth_xform = crypto_xform->next;
     164                 :            :         }
     165                 :            : 
     166         [ #  # ]:          0 :         if (cipher_xform) {
     167         [ #  # ]:          0 :                 if ((tls_xform->type == RTE_SECURITY_TLS_SESS_TYPE_WRITE) &&
     168         [ #  # ]:          0 :                     !(cipher_xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT &&
     169         [ #  # ]:          0 :                       auth_xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE))
     170                 :            :                         return -EINVAL;
     171                 :            : 
     172         [ #  # ]:          0 :                 if ((tls_xform->type == RTE_SECURITY_TLS_SESS_TYPE_READ) &&
     173         [ #  # ]:          0 :                     !(cipher_xform->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT &&
     174         [ #  # ]:          0 :                       auth_xform->auth.op == RTE_CRYPTO_AUTH_OP_VERIFY))
     175                 :            :                         return -EINVAL;
     176                 :            :         } else {
     177         [ #  # ]:          0 :                 if ((tls_xform->type == RTE_SECURITY_TLS_SESS_TYPE_WRITE) &&
     178         [ #  # ]:          0 :                     (auth_xform->auth.op != RTE_CRYPTO_AUTH_OP_GENERATE))
     179                 :            :                         return -EINVAL;
     180                 :            : 
     181         [ #  # ]:          0 :                 if ((tls_xform->type == RTE_SECURITY_TLS_SESS_TYPE_READ) &&
     182         [ #  # ]:          0 :                     (auth_xform->auth.op == RTE_CRYPTO_AUTH_OP_VERIFY))
     183                 :            :                         return -EINVAL;
     184                 :            :         }
     185                 :            : 
     186         [ #  # ]:          0 :         if (cipher_xform)
     187                 :          0 :                 ret = tls_xform_cipher_verify(cipher_xform);
     188                 :            : 
     189         [ #  # ]:          0 :         if (!ret)
     190                 :          0 :                 ret = tls_xform_auth_verify(auth_xform);
     191                 :            : 
     192         [ #  # ]:          0 :         if (cipher_xform && !ret)
     193                 :          0 :                 return tls_xform_cipher_auth_verify(cipher_xform, auth_xform);
     194                 :            : 
     195                 :            :         return ret;
     196                 :            : }
     197                 :            : 
     198                 :            : static int
     199                 :          0 : tls_write_rlens_get(struct rte_security_tls_record_xform *tls_xfrm,
     200                 :            :                     struct rte_crypto_sym_xform *crypto_xfrm)
     201                 :            : {
     202                 :            :         enum rte_crypto_cipher_algorithm c_algo = RTE_CRYPTO_CIPHER_NULL;
     203                 :            :         enum rte_crypto_auth_algorithm a_algo = RTE_CRYPTO_AUTH_NULL;
     204                 :            :         uint8_t roundup_byte, tls_hdr_len;
     205                 :            :         uint8_t mac_len, iv_len;
     206                 :            : 
     207      [ #  #  # ]:          0 :         switch (tls_xfrm->ver) {
     208                 :            :         case RTE_SECURITY_VERSION_TLS_1_2:
     209                 :            :         case RTE_SECURITY_VERSION_TLS_1_3:
     210                 :            :                 tls_hdr_len = 5;
     211                 :            :                 break;
     212                 :          0 :         case RTE_SECURITY_VERSION_DTLS_1_2:
     213                 :            :                 tls_hdr_len = 13;
     214                 :          0 :                 break;
     215                 :          0 :         default:
     216                 :            :                 tls_hdr_len = 0;
     217                 :          0 :                 break;
     218                 :            :         }
     219                 :            : 
     220                 :            :         /* Get Cipher and Auth algo */
     221         [ #  # ]:          0 :         if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD)
     222                 :          0 :                 return tls_hdr_len + ROC_CPT_AES_GCM_IV_LEN + ROC_CPT_AES_GCM_MAC_LEN;
     223                 :            : 
     224         [ #  # ]:          0 :         if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
     225                 :          0 :                 c_algo = crypto_xfrm->cipher.algo;
     226         [ #  # ]:          0 :                 if (crypto_xfrm->next)
     227                 :          0 :                         a_algo = crypto_xfrm->next->auth.algo;
     228                 :            :         } else {
     229                 :          0 :                 a_algo = crypto_xfrm->auth.algo;
     230         [ #  # ]:          0 :                 if (crypto_xfrm->next)
     231                 :          0 :                         c_algo = crypto_xfrm->next->cipher.algo;
     232                 :            :         }
     233                 :            : 
     234   [ #  #  #  # ]:          0 :         switch (c_algo) {
     235                 :            :         case RTE_CRYPTO_CIPHER_NULL:
     236                 :            :                 roundup_byte = 4;
     237                 :            :                 iv_len = 0;
     238                 :            :                 break;
     239                 :          0 :         case RTE_CRYPTO_CIPHER_3DES_CBC:
     240                 :            :                 roundup_byte = ROC_CPT_DES_BLOCK_LENGTH;
     241                 :            :                 iv_len = ROC_CPT_DES_IV_LEN;
     242                 :          0 :                 break;
     243                 :          0 :         case RTE_CRYPTO_CIPHER_AES_CBC:
     244                 :            :                 roundup_byte = ROC_CPT_AES_BLOCK_LENGTH;
     245                 :            :                 iv_len = ROC_CPT_AES_CBC_IV_LEN;
     246                 :          0 :                 break;
     247                 :          0 :         default:
     248                 :            :                 roundup_byte = 0;
     249                 :            :                 iv_len = 0;
     250                 :          0 :                 break;
     251                 :            :         }
     252                 :            : 
     253                 :            :         switch (a_algo) {
     254                 :            :         case RTE_CRYPTO_AUTH_NULL:
     255                 :            :                 mac_len = 0;
     256                 :            :                 break;
     257                 :            :         case RTE_CRYPTO_AUTH_MD5_HMAC:
     258                 :            :                 mac_len = 16;
     259                 :            :                 break;
     260                 :            :         case RTE_CRYPTO_AUTH_SHA1_HMAC:
     261                 :            :                 mac_len = 20;
     262                 :            :                 break;
     263                 :            :         case RTE_CRYPTO_AUTH_SHA256_HMAC:
     264                 :            :                 mac_len = 32;
     265                 :            :                 break;
     266                 :            :         case RTE_CRYPTO_AUTH_SHA384_HMAC:
     267                 :            :                 mac_len = 32;
     268                 :            :                 break;
     269                 :            :         default:
     270                 :            :                 mac_len = 0;
     271                 :            :                 break;
     272                 :            :         }
     273                 :            : 
     274                 :          0 :         return tls_hdr_len + iv_len + mac_len + roundup_byte;
     275                 :            : }
     276                 :            : 
     277                 :            : static void
     278                 :            : tls_write_sa_init(struct roc_ie_ot_tls_write_sa *sa)
     279                 :            : {
     280                 :            :         size_t offset;
     281                 :            : 
     282                 :            :         memset(sa, 0, sizeof(struct roc_ie_ot_tls_write_sa));
     283                 :            : 
     284                 :            :         offset = offsetof(struct roc_ie_ot_tls_write_sa, tls_12.w26_rsvd7);
     285                 :          0 :         sa->w0.s.hw_ctx_off = offset / ROC_CTX_UNIT_8B;
     286                 :          0 :         sa->w0.s.ctx_push_size = sa->w0.s.hw_ctx_off;
     287                 :          0 :         sa->w0.s.ctx_size = ROC_IE_OT_TLS_CTX_ILEN;
     288                 :          0 :         sa->w0.s.ctx_hdr_size = ROC_IE_OT_TLS_CTX_HDR_SIZE;
     289                 :          0 :         sa->w0.s.aop_valid = 1;
     290                 :            : }
     291                 :            : 
     292                 :            : static void
     293                 :            : tls_read_sa_init(struct roc_ie_ot_tls_read_sa *sa)
     294                 :            : {
     295                 :            :         size_t offset;
     296                 :            : 
     297                 :            :         memset(sa, 0, sizeof(struct roc_ie_ot_tls_read_sa));
     298                 :            : 
     299                 :            :         offset = offsetof(struct roc_ie_ot_tls_read_sa, tls_12.ctx);
     300                 :          0 :         sa->w0.s.hw_ctx_off = offset / ROC_CTX_UNIT_8B;
     301                 :          0 :         sa->w0.s.ctx_push_size = sa->w0.s.hw_ctx_off;
     302                 :          0 :         sa->w0.s.ctx_size = ROC_IE_OT_TLS_CTX_ILEN;
     303                 :          0 :         sa->w0.s.ctx_hdr_size = ROC_IE_OT_TLS_CTX_HDR_SIZE;
     304                 :          0 :         sa->w0.s.aop_valid = 1;
     305                 :            : }
     306                 :            : 
     307                 :            : static size_t
     308                 :            : tls_read_ctx_size(struct roc_ie_ot_tls_read_sa *sa, enum rte_security_tls_version tls_ver)
     309                 :            : {
     310                 :            :         size_t size;
     311                 :            : 
     312                 :            :         /* Variable based on Anti-replay Window */
     313         [ #  # ]:          0 :         if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3) {
     314                 :            :                 size = offsetof(struct roc_ie_ot_tls_read_sa, tls_13.ctx) +
     315                 :            :                        sizeof(struct roc_ie_ot_tls_1_3_read_ctx_update_reg);
     316                 :            :         } else {
     317                 :            :                 size = offsetof(struct roc_ie_ot_tls_read_sa, tls_12.ctx) +
     318                 :            :                        offsetof(struct roc_ie_ot_tls_read_ctx_update_reg, ar_winbits);
     319                 :            :         }
     320                 :            : 
     321         [ #  # ]:          0 :         if (sa->w0.s.ar_win)
     322                 :          0 :                 size += (1 << (sa->w0.s.ar_win - 1)) * sizeof(uint64_t);
     323                 :            : 
     324                 :            :         return size;
     325                 :            : }
     326                 :            : 
     327                 :            : static int
     328                 :          0 : tls_read_sa_fill(struct roc_ie_ot_tls_read_sa *read_sa,
     329                 :            :                  struct rte_security_tls_record_xform *tls_xfrm,
     330                 :            :                  struct rte_crypto_sym_xform *crypto_xfrm, struct cn10k_tls_opt *tls_opt,
     331                 :            :                  uint8_t ctx_ilen)
     332                 :            : {
     333         [ #  # ]:          0 :         enum rte_security_tls_version tls_ver = tls_xfrm->ver;
     334                 :            :         struct rte_crypto_sym_xform *auth_xfrm, *cipher_xfrm;
     335                 :            :         const uint8_t *key = NULL;
     336                 :            :         uint64_t *tmp, *tmp_key;
     337                 :            :         uint32_t replay_win_sz;
     338                 :            :         uint8_t *cipher_key;
     339                 :            :         int i, length = 0;
     340                 :            :         size_t offset;
     341                 :            : 
     342                 :            :         /* Initialize the SA */
     343                 :            :         memset(read_sa, 0, sizeof(struct roc_ie_ot_tls_read_sa));
     344                 :            : 
     345         [ #  # ]:          0 :         if (tls_ver == RTE_SECURITY_VERSION_TLS_1_2) {
     346                 :          0 :                 read_sa->w2.s.version_select = ROC_IE_OT_TLS_VERSION_TLS_12;
     347                 :          0 :                 read_sa->tls_12.ctx.ar_valid_mask = tls_xfrm->tls_1_2.seq_no - 1;
     348         [ #  # ]:          0 :         } else if (tls_ver == RTE_SECURITY_VERSION_DTLS_1_2) {
     349                 :          0 :                 read_sa->w2.s.version_select = ROC_IE_OT_TLS_VERSION_DTLS_12;
     350         [ #  # ]:          0 :         } else if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3) {
     351                 :          0 :                 read_sa->w2.s.version_select = ROC_IE_OT_TLS_VERSION_TLS_13;
     352                 :          0 :                 read_sa->tls_13.ctx.ar_valid_mask = tls_xfrm->tls_1_3.seq_no - 1;
     353                 :            :         }
     354                 :            : 
     355                 :          0 :         cipher_key = read_sa->cipher_key;
     356                 :            : 
     357                 :            :         /* Set encryption algorithm */
     358         [ #  # ]:          0 :         if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
     359                 :          0 :                 length = crypto_xfrm->aead.key.length;
     360         [ #  # ]:          0 :                 if (crypto_xfrm->aead.algo == RTE_CRYPTO_AEAD_AES_GCM) {
     361         [ #  # ]:          0 :                         if (length != 16 && length != 32)
     362                 :            :                                 return -EINVAL;
     363                 :          0 :                         read_sa->w2.s.cipher_select = ROC_IE_OT_TLS_CIPHER_AES_GCM;
     364         [ #  # ]:          0 :                         if (length == 16)
     365                 :          0 :                                 read_sa->w2.s.aes_key_len = ROC_IE_OT_TLS_AES_KEY_LEN_128;
     366                 :            :                         else
     367                 :          0 :                                 read_sa->w2.s.aes_key_len = ROC_IE_OT_TLS_AES_KEY_LEN_256;
     368         [ #  # ]:          0 :                 } else if (crypto_xfrm->aead.algo == RTE_CRYPTO_AEAD_CHACHA20_POLY1305) {
     369         [ #  # ]:          0 :                         if (length != 32)
     370                 :            :                                 return -EINVAL;
     371                 :          0 :                         read_sa->w2.s.cipher_select = ROC_IE_OT_TLS_CIPHER_CHACHA_POLY;
     372                 :          0 :                         read_sa->w2.s.aes_key_len = ROC_IE_OT_TLS_AES_KEY_LEN_256;
     373                 :            :                 } else {
     374                 :            :                         return -EINVAL;
     375                 :            :                 }
     376                 :            : 
     377                 :          0 :                 key = crypto_xfrm->aead.key.data;
     378         [ #  # ]:          0 :                 if (key == NULL)
     379                 :            :                         return -EINVAL;
     380         [ #  # ]:          0 :                 memcpy(cipher_key, key, length);
     381                 :            : 
     382         [ #  # ]:          0 :                 if (tls_ver == RTE_SECURITY_VERSION_TLS_1_2)
     383                 :          0 :                         memcpy(((uint8_t *)cipher_key + 32), &tls_xfrm->tls_1_2.imp_nonce, 4);
     384         [ #  # ]:          0 :                 else if (tls_ver == RTE_SECURITY_VERSION_DTLS_1_2)
     385                 :          0 :                         memcpy(((uint8_t *)cipher_key + 32), &tls_xfrm->dtls_1_2.imp_nonce, 4);
     386         [ #  # ]:          0 :                 else if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3)
     387                 :          0 :                         memcpy(((uint8_t *)cipher_key + 32), &tls_xfrm->tls_1_3.imp_nonce, 12);
     388                 :            : 
     389                 :          0 :                 goto key_swap;
     390                 :            :         }
     391                 :            : 
     392         [ #  # ]:          0 :         if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
     393                 :            :                 auth_xfrm = crypto_xfrm;
     394                 :          0 :                 cipher_xfrm = crypto_xfrm->next;
     395                 :            :         } else {
     396                 :            :                 cipher_xfrm = crypto_xfrm;
     397                 :          0 :                 auth_xfrm = crypto_xfrm->next;
     398                 :            :         }
     399                 :            : 
     400         [ #  # ]:          0 :         if (cipher_xfrm != NULL) {
     401         [ #  # ]:          0 :                 if (cipher_xfrm->cipher.algo == RTE_CRYPTO_CIPHER_3DES_CBC) {
     402                 :          0 :                         read_sa->w2.s.cipher_select = ROC_IE_OT_TLS_CIPHER_3DES;
     403                 :          0 :                         length = cipher_xfrm->cipher.key.length;
     404         [ #  # ]:          0 :                         if (length != 24)
     405                 :            :                                 return -EINVAL;
     406         [ #  # ]:          0 :                 } else if (cipher_xfrm->cipher.algo == RTE_CRYPTO_CIPHER_AES_CBC) {
     407                 :          0 :                         read_sa->w2.s.cipher_select = ROC_IE_OT_TLS_CIPHER_AES_CBC;
     408                 :          0 :                         length = cipher_xfrm->cipher.key.length;
     409         [ #  # ]:          0 :                         if (length == 16)
     410                 :          0 :                                 read_sa->w2.s.aes_key_len = ROC_IE_OT_TLS_AES_KEY_LEN_128;
     411         [ #  # ]:          0 :                         else if (length == 32)
     412                 :          0 :                                 read_sa->w2.s.aes_key_len = ROC_IE_OT_TLS_AES_KEY_LEN_256;
     413                 :            :                         else
     414                 :            :                                 return -EINVAL;
     415                 :            :                 } else {
     416                 :            :                         return -EINVAL;
     417                 :            :                 }
     418                 :            : 
     419                 :          0 :                 key = cipher_xfrm->cipher.key.data;
     420         [ #  # ]:          0 :                 if (key == NULL || length == 0)
     421                 :            :                         return -EINVAL;
     422                 :          0 :                 memcpy(cipher_key, key, length);
     423                 :            :         }
     424                 :            : 
     425   [ #  #  #  #  :          0 :         switch (auth_xfrm->auth.algo) {
                      # ]
     426                 :          0 :         case RTE_CRYPTO_AUTH_MD5_HMAC:
     427                 :          0 :                 read_sa->w2.s.mac_select = ROC_IE_OT_TLS_MAC_MD5;
     428                 :          0 :                 tls_opt->mac_len = 0;
     429                 :          0 :                 break;
     430                 :          0 :         case RTE_CRYPTO_AUTH_SHA1_HMAC:
     431                 :          0 :                 read_sa->w2.s.mac_select = ROC_IE_OT_TLS_MAC_SHA1;
     432                 :          0 :                 tls_opt->mac_len = 20;
     433                 :          0 :                 break;
     434                 :          0 :         case RTE_CRYPTO_AUTH_SHA256_HMAC:
     435                 :          0 :                 read_sa->w2.s.mac_select = ROC_IE_OT_TLS_MAC_SHA2_256;
     436                 :          0 :                 tls_opt->mac_len = 32;
     437                 :          0 :                 break;
     438                 :          0 :         case RTE_CRYPTO_AUTH_SHA384_HMAC:
     439                 :          0 :                 read_sa->w2.s.mac_select = ROC_IE_OT_TLS_MAC_SHA2_384;
     440                 :          0 :                 tls_opt->mac_len = 48;
     441                 :          0 :                 break;
     442                 :            :         default:
     443                 :            :                 return -EINVAL;
     444                 :            :         }
     445                 :            : 
     446                 :          0 :         roc_se_hmac_opad_ipad_gen(read_sa->w2.s.mac_select, auth_xfrm->auth.key.data,
     447                 :          0 :                                   auth_xfrm->auth.key.length, read_sa->tls_12.opad_ipad,
     448                 :            :                                   ROC_SE_TLS);
     449                 :            : 
     450                 :            :         tmp = (uint64_t *)read_sa->tls_12.opad_ipad;
     451         [ #  # ]:          0 :         for (i = 0; i < (int)(ROC_CTX_MAX_OPAD_IPAD_LEN / sizeof(uint64_t)); i++)
     452         [ #  # ]:          0 :                 tmp[i] = rte_be_to_cpu_64(tmp[i]);
     453                 :            : 
     454                 :          0 : key_swap:
     455                 :            :         tmp_key = (uint64_t *)cipher_key;
     456         [ #  # ]:          0 :         for (i = 0; i < (int)(ROC_IE_OT_TLS_CTX_MAX_KEY_IV_LEN / sizeof(uint64_t)); i++)
     457         [ #  # ]:          0 :                 tmp_key[i] = rte_be_to_cpu_64(tmp_key[i]);
     458                 :            : 
     459         [ #  # ]:          0 :         if (tls_xfrm->ver == RTE_SECURITY_VERSION_DTLS_1_2) {
     460                 :            :                 /* Only support power-of-two window sizes supported */
     461                 :          0 :                 replay_win_sz = tls_xfrm->dtls_1_2.ar_win_sz;
     462         [ #  # ]:          0 :                 if (replay_win_sz) {
     463         [ #  # ]:          0 :                         if (!rte_is_power_of_2(replay_win_sz) ||
     464                 :            :                             replay_win_sz > ROC_IE_OT_TLS_AR_WIN_SIZE_MAX)
     465                 :            :                                 return -ENOTSUP;
     466                 :            : 
     467                 :          0 :                         read_sa->w0.s.ar_win = rte_log2_u32(replay_win_sz) - 5;
     468                 :            :                 }
     469                 :            :         }
     470                 :            : 
     471                 :          0 :         read_sa->w0.s.ctx_hdr_size = ROC_IE_OT_TLS_CTX_HDR_SIZE;
     472                 :          0 :         read_sa->w0.s.aop_valid = 1;
     473                 :            : 
     474                 :            :         offset = offsetof(struct roc_ie_ot_tls_read_sa, tls_12.ctx);
     475         [ #  # ]:          0 :         if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3)
     476                 :            :                 offset = offsetof(struct roc_ie_ot_tls_read_sa, tls_13.ctx);
     477                 :            : 
     478                 :            :         /* Entire context size in 128B units */
     479                 :          0 :         read_sa->w0.s.ctx_size =
     480                 :          0 :                 (PLT_ALIGN_CEIL(tls_read_ctx_size(read_sa, tls_ver), ROC_CTX_UNIT_128B) /
     481                 :          0 :                  ROC_CTX_UNIT_128B) -
     482                 :            :                 1;
     483                 :            : 
     484         [ #  # ]:          0 :         if (read_sa->w0.s.ctx_size < ctx_ilen)
     485                 :          0 :                 read_sa->w0.s.ctx_size = ctx_ilen;
     486                 :            : 
     487                 :            :         /* Word offset for HW managed CTX field */
     488                 :          0 :         read_sa->w0.s.hw_ctx_off = offset / 8;
     489                 :          0 :         read_sa->w0.s.ctx_push_size = read_sa->w0.s.hw_ctx_off;
     490                 :            : 
     491                 :            :         rte_wmb();
     492                 :            : 
     493                 :          0 :         return 0;
     494                 :            : }
     495                 :            : 
     496                 :            : static int
     497                 :          0 : tls_write_sa_fill(struct roc_ie_ot_tls_write_sa *write_sa,
     498                 :            :                   struct rte_security_tls_record_xform *tls_xfrm,
     499                 :            :                   struct rte_crypto_sym_xform *crypto_xfrm, uint8_t ctx_ilen)
     500                 :            : {
     501                 :          0 :         enum rte_security_tls_version tls_ver = tls_xfrm->ver;
     502                 :            :         struct rte_crypto_sym_xform *auth_xfrm, *cipher_xfrm;
     503                 :            :         const uint8_t *key = NULL;
     504                 :            :         uint8_t *cipher_key;
     505                 :            :         uint64_t *tmp_key;
     506                 :            :         int i, length = 0;
     507                 :            :         size_t offset;
     508                 :            : 
     509         [ #  # ]:          0 :         if (tls_ver == RTE_SECURITY_VERSION_TLS_1_2) {
     510                 :          0 :                 write_sa->w2.s.version_select = ROC_IE_OT_TLS_VERSION_TLS_12;
     511                 :          0 :                 write_sa->tls_12.seq_num = tls_xfrm->tls_1_2.seq_no - 1;
     512         [ #  # ]:          0 :         } else if (tls_ver == RTE_SECURITY_VERSION_DTLS_1_2) {
     513                 :          0 :                 write_sa->w2.s.version_select = ROC_IE_OT_TLS_VERSION_DTLS_12;
     514                 :          0 :                 write_sa->tls_12.seq_num = ((uint64_t)tls_xfrm->dtls_1_2.epoch << 48) |
     515                 :          0 :                                            (tls_xfrm->dtls_1_2.seq_no & 0x0000ffffffffffff);
     516                 :          0 :                 write_sa->tls_12.seq_num -= 1;
     517         [ #  # ]:          0 :         } else if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3) {
     518                 :          0 :                 write_sa->w2.s.version_select = ROC_IE_OT_TLS_VERSION_TLS_13;
     519                 :          0 :                 write_sa->tls_13.seq_num = tls_xfrm->tls_1_3.seq_no - 1;
     520                 :            :         }
     521                 :            : 
     522                 :          0 :         cipher_key = write_sa->cipher_key;
     523                 :            : 
     524                 :            :         /* Set encryption algorithm */
     525         [ #  # ]:          0 :         if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
     526                 :          0 :                 length = crypto_xfrm->aead.key.length;
     527         [ #  # ]:          0 :                 if (crypto_xfrm->aead.algo == RTE_CRYPTO_AEAD_AES_GCM) {
     528         [ #  # ]:          0 :                         if (length != 16 && length != 32)
     529                 :            :                                 return -EINVAL;
     530                 :          0 :                         write_sa->w2.s.cipher_select = ROC_IE_OT_TLS_CIPHER_AES_GCM;
     531         [ #  # ]:          0 :                         if (length == 16)
     532                 :          0 :                                 write_sa->w2.s.aes_key_len = ROC_IE_OT_TLS_AES_KEY_LEN_128;
     533                 :            :                         else
     534                 :          0 :                                 write_sa->w2.s.aes_key_len = ROC_IE_OT_TLS_AES_KEY_LEN_256;
     535         [ #  # ]:          0 :                 } else if (crypto_xfrm->aead.algo == RTE_CRYPTO_AEAD_CHACHA20_POLY1305) {
     536         [ #  # ]:          0 :                         if (length != 32)
     537                 :            :                                 return -EINVAL;
     538                 :          0 :                         write_sa->w2.s.cipher_select = ROC_IE_OT_TLS_CIPHER_CHACHA_POLY;
     539                 :          0 :                         write_sa->w2.s.aes_key_len = ROC_IE_OT_TLS_AES_KEY_LEN_256;
     540                 :            :                 } else {
     541                 :            :                         return -EINVAL;
     542                 :            :                 }
     543                 :            : 
     544                 :          0 :                 key = crypto_xfrm->aead.key.data;
     545         [ #  # ]:          0 :                 if (key == NULL)
     546                 :            :                         return -EINVAL;
     547         [ #  # ]:          0 :                 memcpy(cipher_key, key, length);
     548                 :            : 
     549         [ #  # ]:          0 :                 if (tls_ver == RTE_SECURITY_VERSION_TLS_1_2)
     550                 :          0 :                         memcpy(((uint8_t *)cipher_key + 32), &tls_xfrm->tls_1_2.imp_nonce, 4);
     551         [ #  # ]:          0 :                 else if (tls_ver == RTE_SECURITY_VERSION_DTLS_1_2)
     552                 :          0 :                         memcpy(((uint8_t *)cipher_key + 32), &tls_xfrm->dtls_1_2.imp_nonce, 4);
     553         [ #  # ]:          0 :                 else if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3)
     554                 :          0 :                         memcpy(((uint8_t *)cipher_key + 32), &tls_xfrm->tls_1_3.imp_nonce, 12);
     555                 :            : 
     556                 :          0 :                 goto key_swap;
     557                 :            :         }
     558                 :            : 
     559         [ #  # ]:          0 :         if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
     560                 :            :                 auth_xfrm = crypto_xfrm;
     561                 :          0 :                 cipher_xfrm = crypto_xfrm->next;
     562                 :            :         } else {
     563                 :            :                 cipher_xfrm = crypto_xfrm;
     564                 :          0 :                 auth_xfrm = crypto_xfrm->next;
     565                 :            :         }
     566                 :            : 
     567         [ #  # ]:          0 :         if (cipher_xfrm != NULL) {
     568         [ #  # ]:          0 :                 if (cipher_xfrm->cipher.algo == RTE_CRYPTO_CIPHER_3DES_CBC) {
     569                 :          0 :                         write_sa->w2.s.cipher_select = ROC_IE_OT_TLS_CIPHER_3DES;
     570                 :          0 :                         length = cipher_xfrm->cipher.key.length;
     571         [ #  # ]:          0 :                         if (length != 24)
     572                 :            :                                 return -EINVAL;
     573         [ #  # ]:          0 :                 } else if (cipher_xfrm->cipher.algo == RTE_CRYPTO_CIPHER_AES_CBC) {
     574                 :          0 :                         write_sa->w2.s.cipher_select = ROC_IE_OT_TLS_CIPHER_AES_CBC;
     575                 :          0 :                         length = cipher_xfrm->cipher.key.length;
     576         [ #  # ]:          0 :                         if (length == 16)
     577                 :          0 :                                 write_sa->w2.s.aes_key_len = ROC_IE_OT_TLS_AES_KEY_LEN_128;
     578         [ #  # ]:          0 :                         else if (length == 32)
     579                 :          0 :                                 write_sa->w2.s.aes_key_len = ROC_IE_OT_TLS_AES_KEY_LEN_256;
     580                 :            :                         else
     581                 :            :                                 return -EINVAL;
     582                 :            :                 } else {
     583                 :            :                         return -EINVAL;
     584                 :            :                 }
     585                 :            : 
     586                 :          0 :                 key = cipher_xfrm->cipher.key.data;
     587         [ #  # ]:          0 :                 if (key != NULL && length != 0) {
     588                 :            :                         /* Copy encryption key */
     589                 :          0 :                         memcpy(cipher_key, key, length);
     590                 :            :                 }
     591                 :            :         }
     592                 :            : 
     593         [ #  # ]:          0 :         if (auth_xfrm != NULL) {
     594         [ #  # ]:          0 :                 if (auth_xfrm->auth.algo == RTE_CRYPTO_AUTH_MD5_HMAC)
     595                 :          0 :                         write_sa->w2.s.mac_select = ROC_IE_OT_TLS_MAC_MD5;
     596         [ #  # ]:          0 :                 else if (auth_xfrm->auth.algo == RTE_CRYPTO_AUTH_SHA1_HMAC)
     597                 :          0 :                         write_sa->w2.s.mac_select = ROC_IE_OT_TLS_MAC_SHA1;
     598         [ #  # ]:          0 :                 else if (auth_xfrm->auth.algo == RTE_CRYPTO_AUTH_SHA256_HMAC)
     599                 :          0 :                         write_sa->w2.s.mac_select = ROC_IE_OT_TLS_MAC_SHA2_256;
     600         [ #  # ]:          0 :                 else if (auth_xfrm->auth.algo == RTE_CRYPTO_AUTH_SHA384_HMAC)
     601                 :          0 :                         write_sa->w2.s.mac_select = ROC_IE_OT_TLS_MAC_SHA2_384;
     602                 :            :                 else
     603                 :            :                         return -EINVAL;
     604                 :            : 
     605                 :          0 :                 roc_se_hmac_opad_ipad_gen(write_sa->w2.s.mac_select, auth_xfrm->auth.key.data,
     606                 :          0 :                                           auth_xfrm->auth.key.length, write_sa->tls_12.opad_ipad,
     607                 :            :                                           ROC_SE_TLS);
     608                 :            :         }
     609                 :            : 
     610                 :          0 :         tmp_key = (uint64_t *)write_sa->tls_12.opad_ipad;
     611         [ #  # ]:          0 :         for (i = 0; i < (int)(ROC_CTX_MAX_OPAD_IPAD_LEN / sizeof(uint64_t)); i++)
     612         [ #  # ]:          0 :                 tmp_key[i] = rte_be_to_cpu_64(tmp_key[i]);
     613                 :            : 
     614                 :          0 : key_swap:
     615                 :            :         tmp_key = (uint64_t *)cipher_key;
     616         [ #  # ]:          0 :         for (i = 0; i < (int)(ROC_IE_OT_TLS_CTX_MAX_KEY_IV_LEN / sizeof(uint64_t)); i++)
     617         [ #  # ]:          0 :                 tmp_key[i] = rte_be_to_cpu_64(tmp_key[i]);
     618                 :            : 
     619                 :          0 :         write_sa->w0.s.ctx_hdr_size = ROC_IE_OT_TLS_CTX_HDR_SIZE;
     620                 :            :         /* Entire context size in 128B units */
     621                 :          0 :         write_sa->w0.s.ctx_size =
     622                 :            :                 (PLT_ALIGN_CEIL(sizeof(struct roc_ie_ot_tls_write_sa), ROC_CTX_UNIT_128B) /
     623                 :            :                  ROC_CTX_UNIT_128B) -
     624                 :            :                 1;
     625                 :            :         offset = offsetof(struct roc_ie_ot_tls_write_sa, tls_12.w26_rsvd7);
     626                 :            : 
     627         [ #  # ]:          0 :         if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3) {
     628                 :            :                 offset = offsetof(struct roc_ie_ot_tls_write_sa, tls_13.w10_rsvd7);
     629                 :          0 :                 write_sa->w0.s.ctx_size -= 1;
     630                 :            :         }
     631                 :            : 
     632         [ #  # ]:          0 :         if (write_sa->w0.s.ctx_size < ctx_ilen)
     633                 :          0 :                 write_sa->w0.s.ctx_size = ctx_ilen;
     634                 :            : 
     635                 :            :         /* Word offset for HW managed CTX field */
     636                 :          0 :         write_sa->w0.s.hw_ctx_off = offset / 8;
     637                 :          0 :         write_sa->w0.s.ctx_push_size = write_sa->w0.s.hw_ctx_off;
     638                 :            : 
     639                 :          0 :         write_sa->w0.s.aop_valid = 1;
     640                 :            : 
     641                 :          0 :         write_sa->w2.s.iv_at_cptr = ROC_IE_OT_TLS_IV_SRC_DEFAULT;
     642                 :            : 
     643         [ #  # ]:          0 :         if (write_sa->w2.s.version_select != ROC_IE_OT_TLS_VERSION_TLS_13) {
     644                 :            : #ifdef LA_IPSEC_DEBUG
     645                 :            :                 if (tls_xfrm->options.iv_gen_disable == 1)
     646                 :            :                         write_sa->w2.s.iv_at_cptr = ROC_IE_OT_TLS_IV_SRC_FROM_SA;
     647                 :            : #else
     648         [ #  # ]:          0 :                 if (tls_xfrm->options.iv_gen_disable) {
     649                 :          0 :                         plt_err("Application provided IV is not supported");
     650                 :          0 :                         return -ENOTSUP;
     651                 :            :                 }
     652                 :            : #endif
     653                 :            :         }
     654                 :            : 
     655                 :            :         rte_wmb();
     656                 :            : 
     657                 :          0 :         return 0;
     658                 :            : }
     659                 :            : 
     660                 :            : static int
     661                 :          0 : cn10k_tls_read_sa_create(struct roc_cpt *roc_cpt, struct roc_cpt_lf *lf,
     662                 :            :                          struct rte_security_tls_record_xform *tls_xfrm,
     663                 :            :                          struct rte_crypto_sym_xform *crypto_xfrm,
     664                 :            :                          struct cn10k_sec_session *sec_sess)
     665                 :            : {
     666                 :            :         struct roc_ie_ot_tls_read_sa *sa_dptr;
     667                 :          0 :         uint8_t tls_ver = tls_xfrm->ver;
     668                 :            :         struct cn10k_tls_record *tls;
     669                 :            :         union cpt_inst_w4 inst_w4;
     670                 :            :         void *read_sa;
     671                 :            :         int ret = 0;
     672                 :            : 
     673                 :            :         tls = &sec_sess->tls_rec;
     674                 :          0 :         read_sa = &tls->read_sa;
     675                 :            : 
     676                 :            :         /* Allocate memory to be used as dptr for CPT ucode WRITE_SA op */
     677                 :          0 :         sa_dptr = plt_zmalloc(sizeof(struct roc_ie_ot_tls_read_sa), 8);
     678         [ #  # ]:          0 :         if (sa_dptr == NULL) {
     679                 :          0 :                 plt_err("Could not allocate memory for SA DPTR");
     680                 :          0 :                 return -ENOMEM;
     681                 :            :         }
     682                 :            : 
     683                 :            :         /* Translate security parameters to SA */
     684                 :          0 :         ret = tls_read_sa_fill(sa_dptr, tls_xfrm, crypto_xfrm, &sec_sess->tls_opt,
     685                 :          0 :                                roc_cpt->ctx_ilen);
     686         [ #  # ]:          0 :         if (ret) {
     687                 :          0 :                 plt_err("Could not fill read session parameters");
     688                 :          0 :                 goto sa_dptr_free;
     689                 :            :         }
     690         [ #  # ]:          0 :         if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
     691                 :          0 :                 sec_sess->iv_offset = crypto_xfrm->aead.iv.offset;
     692                 :          0 :                 sec_sess->iv_length = crypto_xfrm->aead.iv.length;
     693         [ #  # ]:          0 :         } else if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
     694                 :          0 :                 sec_sess->iv_offset = crypto_xfrm->cipher.iv.offset;
     695                 :          0 :                 sec_sess->iv_length = crypto_xfrm->cipher.iv.length;
     696                 :            :         } else {
     697                 :          0 :                 sec_sess->iv_offset = crypto_xfrm->auth.iv.offset;
     698                 :          0 :                 sec_sess->iv_length = crypto_xfrm->auth.iv.length;
     699                 :            :         }
     700                 :            : 
     701                 :          0 :         sec_sess->proto = RTE_SECURITY_PROTOCOL_TLS_RECORD;
     702                 :            : 
     703                 :            :         /* pre-populate CPT INST word 4 */
     704                 :          0 :         inst_w4.u64 = 0;
     705                 :          0 :         if ((tls_ver == RTE_SECURITY_VERSION_TLS_1_2) ||
     706         [ #  # ]:          0 :             (tls_ver == RTE_SECURITY_VERSION_DTLS_1_2)) {
     707                 :          0 :                 inst_w4.s.opcode_major = ROC_IE_OT_TLS_MAJOR_OP_RECORD_DEC | ROC_IE_OT_INPLACE_BIT;
     708                 :          0 :                 sec_sess->tls_opt.tail_fetch_len = 0;
     709         [ #  # ]:          0 :                 if (sa_dptr->w2.s.cipher_select == ROC_IE_OT_TLS_CIPHER_3DES)
     710                 :          0 :                         sec_sess->tls_opt.tail_fetch_len = 1;
     711         [ #  # ]:          0 :                 else if (sa_dptr->w2.s.cipher_select == ROC_IE_OT_TLS_CIPHER_AES_CBC)
     712                 :          0 :                         sec_sess->tls_opt.tail_fetch_len = 2;
     713         [ #  # ]:          0 :         } else if (tls_xfrm->ver == RTE_SECURITY_VERSION_TLS_1_3) {
     714                 :          0 :                 inst_w4.s.opcode_major =
     715                 :            :                         ROC_IE_OT_TLS13_MAJOR_OP_RECORD_DEC | ROC_IE_OT_INPLACE_BIT;
     716                 :            :         }
     717                 :            : 
     718                 :          0 :         sec_sess->tls_opt.tls_ver = tls_ver;
     719         [ #  # ]:          0 :         sec_sess->inst.w4 = inst_w4.u64;
     720                 :          0 :         sec_sess->inst.w7 = cnxk_cpt_sec_inst_w7_get(roc_cpt, read_sa);
     721                 :            : 
     722                 :            :         memset(read_sa, 0, sizeof(struct roc_ie_ot_tls_read_sa));
     723                 :            : 
     724                 :            :         /* Copy word0 from sa_dptr to populate ctx_push_sz ctx_size fields */
     725                 :            :         memcpy(read_sa, sa_dptr, 8);
     726                 :            : 
     727                 :            :         rte_atomic_thread_fence(rte_memory_order_seq_cst);
     728                 :            : 
     729                 :            :         /* Write session using microcode opcode */
     730                 :          0 :         ret = roc_cpt_ctx_write(lf, sa_dptr, read_sa, sizeof(struct roc_ie_ot_tls_read_sa));
     731         [ #  # ]:          0 :         if (ret) {
     732                 :          0 :                 plt_err("Could not write read session to hardware");
     733                 :          0 :                 goto sa_dptr_free;
     734                 :            :         }
     735                 :            : 
     736                 :            :         /* Trigger CTX flush so that data is written back to DRAM */
     737                 :          0 :         ret = roc_cpt_lf_ctx_flush(lf, read_sa, true);
     738         [ #  # ]:          0 :         if (ret == -EFAULT) {
     739                 :          0 :                 plt_err("Could not flush TLS read session to hardware");
     740                 :          0 :                 goto sa_dptr_free;
     741                 :            :         }
     742                 :            : 
     743                 :            :         rte_atomic_thread_fence(rte_memory_order_seq_cst);
     744                 :            : 
     745                 :          0 : sa_dptr_free:
     746                 :          0 :         plt_free(sa_dptr);
     747                 :            : 
     748                 :          0 :         return ret;
     749                 :            : }
     750                 :            : 
     751                 :            : static int
     752                 :          0 : cn10k_tls_write_sa_create(struct roc_cpt *roc_cpt, struct roc_cpt_lf *lf,
     753                 :            :                           struct rte_security_tls_record_xform *tls_xfrm,
     754                 :            :                           struct rte_crypto_sym_xform *crypto_xfrm,
     755                 :            :                           struct cn10k_sec_session *sec_sess)
     756                 :            : {
     757                 :            :         struct roc_ie_ot_tls_write_sa *sa_dptr;
     758                 :          0 :         uint8_t tls_ver = tls_xfrm->ver;
     759                 :            :         struct cn10k_tls_record *tls;
     760                 :            :         union cpt_inst_w4 inst_w4;
     761                 :            :         void *write_sa;
     762                 :            :         int ret = 0;
     763                 :            : 
     764                 :            :         tls = &sec_sess->tls_rec;
     765                 :          0 :         write_sa = &tls->write_sa;
     766                 :            : 
     767                 :            :         /* Allocate memory to be used as dptr for CPT ucode WRITE_SA op */
     768                 :          0 :         sa_dptr = plt_zmalloc(sizeof(struct roc_ie_ot_tls_write_sa), 8);
     769         [ #  # ]:          0 :         if (sa_dptr == NULL) {
     770                 :          0 :                 plt_err("Could not allocate memory for SA DPTR");
     771                 :          0 :                 return -ENOMEM;
     772                 :            :         }
     773                 :            : 
     774                 :            :         /* Translate security parameters to SA */
     775                 :          0 :         ret = tls_write_sa_fill(sa_dptr, tls_xfrm, crypto_xfrm, roc_cpt->ctx_ilen);
     776         [ #  # ]:          0 :         if (ret) {
     777                 :          0 :                 plt_err("Could not fill write session parameters");
     778                 :          0 :                 goto sa_dptr_free;
     779                 :            :         }
     780                 :            : 
     781         [ #  # ]:          0 :         if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
     782                 :          0 :                 sec_sess->iv_offset = crypto_xfrm->aead.iv.offset;
     783                 :          0 :                 sec_sess->iv_length = crypto_xfrm->aead.iv.length;
     784         [ #  # ]:          0 :         } else if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
     785                 :          0 :                 sec_sess->iv_offset = crypto_xfrm->cipher.iv.offset;
     786                 :          0 :                 sec_sess->iv_length = crypto_xfrm->cipher.iv.length;
     787                 :            :         } else {
     788                 :          0 :                 sec_sess->iv_offset = crypto_xfrm->next->cipher.iv.offset;
     789                 :          0 :                 sec_sess->iv_length = crypto_xfrm->next->cipher.iv.length;
     790                 :            :         }
     791                 :            : 
     792                 :          0 :         sec_sess->tls_opt.is_write = 1;
     793                 :          0 :         sec_sess->tls_opt.pad_shift = 0;
     794                 :          0 :         sec_sess->tls_opt.tls_ver = tls_ver;
     795                 :          0 :         sec_sess->tls_opt.enable_padding = tls_xfrm->options.extra_padding_enable;
     796                 :          0 :         sec_sess->max_extended_len = tls_write_rlens_get(tls_xfrm, crypto_xfrm);
     797                 :          0 :         sec_sess->proto = RTE_SECURITY_PROTOCOL_TLS_RECORD;
     798                 :            : 
     799                 :            :         /* pre-populate CPT INST word 4 */
     800                 :          0 :         inst_w4.u64 = 0;
     801                 :          0 :         if ((tls_ver == RTE_SECURITY_VERSION_TLS_1_2) ||
     802         [ #  # ]:          0 :             (tls_ver == RTE_SECURITY_VERSION_DTLS_1_2)) {
     803                 :          0 :                 inst_w4.s.opcode_major = ROC_IE_OT_TLS_MAJOR_OP_RECORD_ENC | ROC_IE_OT_INPLACE_BIT;
     804         [ #  # ]:          0 :                 if (sa_dptr->w2.s.cipher_select == ROC_IE_OT_TLS_CIPHER_3DES)
     805                 :          0 :                         sec_sess->tls_opt.pad_shift = 3;
     806                 :            :                 else
     807                 :          0 :                         sec_sess->tls_opt.pad_shift = 4;
     808         [ #  # ]:          0 :         } else if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3) {
     809                 :          0 :                 inst_w4.s.opcode_major =
     810                 :            :                         ROC_IE_OT_TLS13_MAJOR_OP_RECORD_ENC | ROC_IE_OT_INPLACE_BIT;
     811                 :            :         }
     812         [ #  # ]:          0 :         sec_sess->inst.w4 = inst_w4.u64;
     813                 :          0 :         sec_sess->inst.w7 = cnxk_cpt_sec_inst_w7_get(roc_cpt, write_sa);
     814                 :            : 
     815                 :            :         memset(write_sa, 0, sizeof(struct roc_ie_ot_tls_write_sa));
     816                 :            : 
     817                 :            :         /* Copy word0 from sa_dptr to populate ctx_push_sz ctx_size fields */
     818                 :            :         memcpy(write_sa, sa_dptr, 8);
     819                 :            : 
     820                 :            :         rte_atomic_thread_fence(rte_memory_order_seq_cst);
     821                 :            : 
     822                 :            :         /* Write session using microcode opcode */
     823                 :          0 :         ret = roc_cpt_ctx_write(lf, sa_dptr, write_sa, sizeof(struct roc_ie_ot_tls_write_sa));
     824         [ #  # ]:          0 :         if (ret) {
     825                 :          0 :                 plt_err("Could not write tls write session to hardware");
     826                 :          0 :                 goto sa_dptr_free;
     827                 :            :         }
     828                 :            : 
     829                 :            :         /* Trigger CTX flush so that data is written back to DRAM */
     830                 :          0 :         ret = roc_cpt_lf_ctx_flush(lf, write_sa, false);
     831         [ #  # ]:          0 :         if (ret == -EFAULT) {
     832                 :          0 :                 plt_err("Could not flush TLS write session to hardware");
     833                 :          0 :                 goto sa_dptr_free;
     834                 :            :         }
     835                 :            : 
     836                 :            :         rte_atomic_thread_fence(rte_memory_order_seq_cst);
     837                 :            : 
     838                 :          0 : sa_dptr_free:
     839                 :          0 :         plt_free(sa_dptr);
     840                 :            : 
     841                 :          0 :         return ret;
     842                 :            : }
     843                 :            : 
     844                 :            : int
     845                 :          0 : cn10k_tls_record_session_update(struct cnxk_cpt_vf *vf, struct cnxk_cpt_qp *qp,
     846                 :            :                                 struct cn10k_sec_session *sess,
     847                 :            :                                 struct rte_security_session_conf *conf)
     848                 :            : {
     849                 :            :         struct roc_cpt *roc_cpt;
     850                 :            :         int ret;
     851                 :            : 
     852         [ #  # ]:          0 :         if (conf->tls_record.type == RTE_SECURITY_TLS_SESS_TYPE_READ)
     853                 :            :                 return -ENOTSUP;
     854                 :            : 
     855                 :          0 :         ret = cnxk_tls_xform_verify(&conf->tls_record, conf->crypto_xform);
     856         [ #  # ]:          0 :         if (ret)
     857                 :            :                 return ret;
     858                 :            : 
     859                 :          0 :         roc_cpt = &vf->cpt;
     860                 :          0 :         ret = cn10k_tls_write_sa_create(roc_cpt, &qp->lf, &conf->tls_record, conf->crypto_xform,
     861                 :            :                                         (struct cn10k_sec_session *)sess);
     862                 :          0 :         return ret;
     863                 :            : }
     864                 :            : 
     865                 :            : int
     866                 :          0 : cn10k_tls_record_session_create(struct cnxk_cpt_vf *vf, struct cnxk_cpt_qp *qp,
     867                 :            :                                 struct rte_security_tls_record_xform *tls_xfrm,
     868                 :            :                                 struct rte_crypto_sym_xform *crypto_xfrm,
     869                 :            :                                 struct rte_security_session *sess)
     870                 :            : {
     871                 :            :         struct roc_cpt *roc_cpt;
     872                 :            :         int ret;
     873                 :            : 
     874                 :          0 :         ret = cnxk_tls_xform_verify(tls_xfrm, crypto_xfrm);
     875         [ #  # ]:          0 :         if (ret)
     876                 :            :                 return ret;
     877                 :            : 
     878                 :          0 :         roc_cpt = &vf->cpt;
     879                 :            : 
     880         [ #  # ]:          0 :         if (tls_xfrm->type == RTE_SECURITY_TLS_SESS_TYPE_READ)
     881                 :          0 :                 return cn10k_tls_read_sa_create(roc_cpt, &qp->lf, tls_xfrm, crypto_xfrm,
     882                 :            :                                                 (struct cn10k_sec_session *)sess);
     883                 :            :         else
     884                 :          0 :                 return cn10k_tls_write_sa_create(roc_cpt, &qp->lf, tls_xfrm, crypto_xfrm,
     885                 :            :                                                  (struct cn10k_sec_session *)sess);
     886                 :            : }
     887                 :            : 
     888                 :            : int
     889                 :          0 : cn10k_sec_tls_session_destroy(struct cnxk_cpt_qp *qp, struct cn10k_sec_session *sess)
     890                 :            : {
     891                 :            :         struct cn10k_tls_record *tls;
     892                 :            :         struct roc_cpt_lf *lf;
     893                 :            :         void *sa_dptr = NULL;
     894                 :            :         int ret;
     895                 :            : 
     896                 :          0 :         lf = &qp->lf;
     897                 :            : 
     898                 :            :         tls = &sess->tls_rec;
     899                 :            : 
     900                 :            :         /* Trigger CTX flush to write dirty data back to DRAM */
     901                 :          0 :         roc_cpt_lf_ctx_flush(lf, &tls->read_sa, false);
     902                 :            : 
     903                 :            :         ret = -1;
     904                 :            : 
     905         [ #  # ]:          0 :         if (sess->tls_opt.is_write) {
     906                 :          0 :                 sa_dptr = plt_zmalloc(sizeof(struct roc_ie_ot_tls_write_sa), 8);
     907         [ #  # ]:          0 :                 if (sa_dptr != NULL) {
     908                 :            :                         tls_write_sa_init(sa_dptr);
     909                 :            : 
     910                 :          0 :                         ret = roc_cpt_ctx_write(lf, sa_dptr, &tls->write_sa,
     911                 :            :                                                 sizeof(struct roc_ie_ot_tls_write_sa));
     912                 :            :                 }
     913                 :            :         } else {
     914                 :          0 :                 sa_dptr = plt_zmalloc(sizeof(struct roc_ie_ot_tls_read_sa), 8);
     915         [ #  # ]:          0 :                 if (sa_dptr != NULL) {
     916                 :            :                         tls_read_sa_init(sa_dptr);
     917                 :            : 
     918                 :          0 :                         ret = roc_cpt_ctx_write(lf, sa_dptr, &tls->read_sa,
     919                 :            :                                                 sizeof(struct roc_ie_ot_tls_read_sa));
     920                 :            :                 }
     921                 :            :         }
     922                 :            : 
     923                 :          0 :         plt_free(sa_dptr);
     924                 :            : 
     925         [ #  # ]:          0 :         if (ret) {
     926                 :            :                 /* MC write_ctx failed. Attempt reload of CTX */
     927                 :            : 
     928                 :            :                 /* Wait for 1 ms so that flush is complete */
     929                 :            :                 rte_delay_ms(1);
     930                 :            : 
     931                 :            :                 rte_atomic_thread_fence(rte_memory_order_seq_cst);
     932                 :            : 
     933                 :            :                 /* Trigger CTX reload to fetch new data from DRAM */
     934                 :          0 :                 roc_cpt_lf_ctx_reload(lf, &tls->read_sa);
     935                 :            :         }
     936                 :            : 
     937                 :          0 :         return 0;
     938                 :            : }

Generated by: LCOV version 1.14