LCOV - code coverage report
Current view: top level - drivers/crypto/cnxk - cn10k_tls.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 390 0.0 %
Date: 2025-03-01 20:23:48 Functions: 0 13 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 292 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                 :            : {
     332         [ #  # ]:          0 :         enum rte_security_tls_version tls_ver = tls_xfrm->ver;
     333                 :            :         struct rte_crypto_sym_xform *auth_xfrm, *cipher_xfrm;
     334                 :            :         const uint8_t *key = NULL;
     335                 :            :         uint64_t *tmp, *tmp_key;
     336                 :            :         uint32_t replay_win_sz;
     337                 :            :         uint8_t *cipher_key;
     338                 :            :         int i, length = 0;
     339                 :            :         size_t offset;
     340                 :            : 
     341                 :            :         /* Initialize the SA */
     342                 :            :         memset(read_sa, 0, sizeof(struct roc_ie_ot_tls_read_sa));
     343                 :            : 
     344         [ #  # ]:          0 :         if (tls_ver == RTE_SECURITY_VERSION_TLS_1_2) {
     345                 :          0 :                 read_sa->w2.s.version_select = ROC_IE_OT_TLS_VERSION_TLS_12;
     346                 :          0 :                 read_sa->tls_12.ctx.ar_valid_mask = tls_xfrm->tls_1_2.seq_no - 1;
     347         [ #  # ]:          0 :         } else if (tls_ver == RTE_SECURITY_VERSION_DTLS_1_2) {
     348                 :          0 :                 read_sa->w2.s.version_select = ROC_IE_OT_TLS_VERSION_DTLS_12;
     349         [ #  # ]:          0 :         } else if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3) {
     350                 :          0 :                 read_sa->w2.s.version_select = ROC_IE_OT_TLS_VERSION_TLS_13;
     351                 :          0 :                 read_sa->tls_13.ctx.ar_valid_mask = tls_xfrm->tls_1_3.seq_no - 1;
     352                 :            :         }
     353                 :            : 
     354                 :          0 :         cipher_key = read_sa->cipher_key;
     355                 :            : 
     356                 :            :         /* Set encryption algorithm */
     357         [ #  # ]:          0 :         if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
     358                 :          0 :                 length = crypto_xfrm->aead.key.length;
     359         [ #  # ]:          0 :                 if (crypto_xfrm->aead.algo == RTE_CRYPTO_AEAD_AES_GCM) {
     360                 :          0 :                         read_sa->w2.s.cipher_select = ROC_IE_OT_TLS_CIPHER_AES_GCM;
     361         [ #  # ]:          0 :                         if (length == 16)
     362                 :          0 :                                 read_sa->w2.s.aes_key_len = ROC_IE_OT_TLS_AES_KEY_LEN_128;
     363                 :            :                         else
     364                 :          0 :                                 read_sa->w2.s.aes_key_len = ROC_IE_OT_TLS_AES_KEY_LEN_256;
     365                 :            :                 }
     366                 :            : 
     367         [ #  # ]:          0 :                 if (crypto_xfrm->aead.algo == RTE_CRYPTO_AEAD_CHACHA20_POLY1305) {
     368                 :          0 :                         read_sa->w2.s.cipher_select = ROC_IE_OT_TLS_CIPHER_CHACHA_POLY;
     369                 :          0 :                         read_sa->w2.s.aes_key_len = ROC_IE_OT_TLS_AES_KEY_LEN_256;
     370                 :            :                 }
     371                 :            : 
     372                 :          0 :                 key = crypto_xfrm->aead.key.data;
     373         [ #  # ]:          0 :                 memcpy(cipher_key, key, length);
     374                 :            : 
     375         [ #  # ]:          0 :                 if (tls_ver == RTE_SECURITY_VERSION_TLS_1_2)
     376                 :          0 :                         memcpy(((uint8_t *)cipher_key + 32), &tls_xfrm->tls_1_2.imp_nonce, 4);
     377         [ #  # ]:          0 :                 else if (tls_ver == RTE_SECURITY_VERSION_DTLS_1_2)
     378                 :          0 :                         memcpy(((uint8_t *)cipher_key + 32), &tls_xfrm->dtls_1_2.imp_nonce, 4);
     379         [ #  # ]:          0 :                 else if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3)
     380                 :          0 :                         memcpy(((uint8_t *)cipher_key + 32), &tls_xfrm->tls_1_3.imp_nonce, 12);
     381                 :            : 
     382                 :          0 :                 goto key_swap;
     383                 :            :         }
     384                 :            : 
     385         [ #  # ]:          0 :         if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
     386                 :            :                 auth_xfrm = crypto_xfrm;
     387                 :          0 :                 cipher_xfrm = crypto_xfrm->next;
     388                 :            :         } else {
     389                 :            :                 cipher_xfrm = crypto_xfrm;
     390                 :          0 :                 auth_xfrm = crypto_xfrm->next;
     391                 :            :         }
     392                 :            : 
     393         [ #  # ]:          0 :         if (cipher_xfrm != NULL) {
     394         [ #  # ]:          0 :                 if (cipher_xfrm->cipher.algo == RTE_CRYPTO_CIPHER_3DES_CBC) {
     395                 :          0 :                         read_sa->w2.s.cipher_select = ROC_IE_OT_TLS_CIPHER_3DES;
     396                 :          0 :                         length = cipher_xfrm->cipher.key.length;
     397         [ #  # ]:          0 :                 } else if (cipher_xfrm->cipher.algo == RTE_CRYPTO_CIPHER_AES_CBC) {
     398                 :          0 :                         read_sa->w2.s.cipher_select = ROC_IE_OT_TLS_CIPHER_AES_CBC;
     399                 :          0 :                         length = cipher_xfrm->cipher.key.length;
     400         [ #  # ]:          0 :                         if (length == 16)
     401                 :          0 :                                 read_sa->w2.s.aes_key_len = ROC_IE_OT_TLS_AES_KEY_LEN_128;
     402         [ #  # ]:          0 :                         else if (length == 32)
     403                 :          0 :                                 read_sa->w2.s.aes_key_len = ROC_IE_OT_TLS_AES_KEY_LEN_256;
     404                 :            :                         else
     405                 :            :                                 return -EINVAL;
     406                 :            :                 } else {
     407                 :            :                         return -EINVAL;
     408                 :            :                 }
     409                 :            : 
     410                 :          0 :                 key = cipher_xfrm->cipher.key.data;
     411                 :          0 :                 memcpy(cipher_key, key, length);
     412                 :            :         }
     413                 :            : 
     414   [ #  #  #  #  :          0 :         switch (auth_xfrm->auth.algo) {
                      # ]
     415                 :          0 :         case RTE_CRYPTO_AUTH_MD5_HMAC:
     416                 :          0 :                 read_sa->w2.s.mac_select = ROC_IE_OT_TLS_MAC_MD5;
     417                 :          0 :                 tls_opt->mac_len = 0;
     418                 :          0 :                 break;
     419                 :          0 :         case RTE_CRYPTO_AUTH_SHA1_HMAC:
     420                 :          0 :                 read_sa->w2.s.mac_select = ROC_IE_OT_TLS_MAC_SHA1;
     421                 :          0 :                 tls_opt->mac_len = 20;
     422                 :          0 :                 break;
     423                 :          0 :         case RTE_CRYPTO_AUTH_SHA256_HMAC:
     424                 :          0 :                 read_sa->w2.s.mac_select = ROC_IE_OT_TLS_MAC_SHA2_256;
     425                 :          0 :                 tls_opt->mac_len = 32;
     426                 :          0 :                 break;
     427                 :          0 :         case RTE_CRYPTO_AUTH_SHA384_HMAC:
     428                 :          0 :                 read_sa->w2.s.mac_select = ROC_IE_OT_TLS_MAC_SHA2_384;
     429                 :          0 :                 tls_opt->mac_len = 48;
     430                 :          0 :                 break;
     431                 :            :         default:
     432                 :            :                 return -EINVAL;
     433                 :            :         }
     434                 :            : 
     435                 :          0 :         roc_se_hmac_opad_ipad_gen(read_sa->w2.s.mac_select, auth_xfrm->auth.key.data,
     436                 :          0 :                                   auth_xfrm->auth.key.length, read_sa->tls_12.opad_ipad,
     437                 :            :                                   ROC_SE_TLS);
     438                 :            : 
     439                 :            :         tmp = (uint64_t *)read_sa->tls_12.opad_ipad;
     440         [ #  # ]:          0 :         for (i = 0; i < (int)(ROC_CTX_MAX_OPAD_IPAD_LEN / sizeof(uint64_t)); i++)
     441         [ #  # ]:          0 :                 tmp[i] = rte_be_to_cpu_64(tmp[i]);
     442                 :            : 
     443                 :          0 : key_swap:
     444                 :            :         tmp_key = (uint64_t *)cipher_key;
     445         [ #  # ]:          0 :         for (i = 0; i < (int)(ROC_IE_OT_TLS_CTX_MAX_KEY_IV_LEN / sizeof(uint64_t)); i++)
     446         [ #  # ]:          0 :                 tmp_key[i] = rte_be_to_cpu_64(tmp_key[i]);
     447                 :            : 
     448         [ #  # ]:          0 :         if (tls_xfrm->ver == RTE_SECURITY_VERSION_DTLS_1_2) {
     449                 :            :                 /* Only support power-of-two window sizes supported */
     450                 :          0 :                 replay_win_sz = tls_xfrm->dtls_1_2.ar_win_sz;
     451         [ #  # ]:          0 :                 if (replay_win_sz) {
     452         [ #  # ]:          0 :                         if (!rte_is_power_of_2(replay_win_sz) ||
     453                 :            :                             replay_win_sz > ROC_IE_OT_TLS_AR_WIN_SIZE_MAX)
     454                 :            :                                 return -ENOTSUP;
     455                 :            : 
     456                 :          0 :                         read_sa->w0.s.ar_win = rte_log2_u32(replay_win_sz) - 5;
     457                 :            :                 }
     458                 :            :         }
     459                 :            : 
     460                 :          0 :         read_sa->w0.s.ctx_hdr_size = ROC_IE_OT_TLS_CTX_HDR_SIZE;
     461                 :          0 :         read_sa->w0.s.aop_valid = 1;
     462                 :            : 
     463                 :            :         offset = offsetof(struct roc_ie_ot_tls_read_sa, tls_12.ctx);
     464         [ #  # ]:          0 :         if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3)
     465                 :            :                 offset = offsetof(struct roc_ie_ot_tls_read_sa, tls_13.ctx);
     466                 :            : 
     467                 :            :         /* Entire context size in 128B units */
     468                 :          0 :         read_sa->w0.s.ctx_size =
     469                 :          0 :                 (PLT_ALIGN_CEIL(tls_read_ctx_size(read_sa, tls_ver), ROC_CTX_UNIT_128B) /
     470                 :          0 :                  ROC_CTX_UNIT_128B) -
     471                 :            :                 1;
     472                 :            : 
     473                 :            :         /* Word offset for HW managed CTX field */
     474                 :          0 :         read_sa->w0.s.hw_ctx_off = offset / 8;
     475                 :          0 :         read_sa->w0.s.ctx_push_size = read_sa->w0.s.hw_ctx_off;
     476                 :            : 
     477                 :            :         rte_wmb();
     478                 :            : 
     479                 :          0 :         return 0;
     480                 :            : }
     481                 :            : 
     482                 :            : static int
     483                 :          0 : tls_write_sa_fill(struct roc_ie_ot_tls_write_sa *write_sa,
     484                 :            :                   struct rte_security_tls_record_xform *tls_xfrm,
     485                 :            :                   struct rte_crypto_sym_xform *crypto_xfrm)
     486                 :            : {
     487                 :          0 :         enum rte_security_tls_version tls_ver = tls_xfrm->ver;
     488                 :            :         struct rte_crypto_sym_xform *auth_xfrm, *cipher_xfrm;
     489                 :            :         const uint8_t *key = NULL;
     490                 :            :         uint8_t *cipher_key;
     491                 :            :         uint64_t *tmp_key;
     492                 :            :         int i, length = 0;
     493                 :            :         size_t offset;
     494                 :            : 
     495         [ #  # ]:          0 :         if (tls_ver == RTE_SECURITY_VERSION_TLS_1_2) {
     496                 :          0 :                 write_sa->w2.s.version_select = ROC_IE_OT_TLS_VERSION_TLS_12;
     497                 :          0 :                 write_sa->tls_12.seq_num = tls_xfrm->tls_1_2.seq_no - 1;
     498         [ #  # ]:          0 :         } else if (tls_ver == RTE_SECURITY_VERSION_DTLS_1_2) {
     499                 :          0 :                 write_sa->w2.s.version_select = ROC_IE_OT_TLS_VERSION_DTLS_12;
     500                 :          0 :                 write_sa->tls_12.seq_num = ((uint64_t)tls_xfrm->dtls_1_2.epoch << 48) |
     501                 :          0 :                                            (tls_xfrm->dtls_1_2.seq_no & 0x0000ffffffffffff);
     502                 :          0 :                 write_sa->tls_12.seq_num -= 1;
     503         [ #  # ]:          0 :         } else if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3) {
     504                 :          0 :                 write_sa->w2.s.version_select = ROC_IE_OT_TLS_VERSION_TLS_13;
     505                 :          0 :                 write_sa->tls_13.seq_num = tls_xfrm->tls_1_3.seq_no - 1;
     506                 :            :         }
     507                 :            : 
     508                 :          0 :         cipher_key = write_sa->cipher_key;
     509                 :            : 
     510                 :            :         /* Set encryption algorithm */
     511         [ #  # ]:          0 :         if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
     512                 :          0 :                 length = crypto_xfrm->aead.key.length;
     513         [ #  # ]:          0 :                 if (crypto_xfrm->aead.algo == RTE_CRYPTO_AEAD_AES_GCM) {
     514                 :          0 :                         write_sa->w2.s.cipher_select = ROC_IE_OT_TLS_CIPHER_AES_GCM;
     515         [ #  # ]:          0 :                         if (length == 16)
     516                 :          0 :                                 write_sa->w2.s.aes_key_len = ROC_IE_OT_TLS_AES_KEY_LEN_128;
     517                 :            :                         else
     518                 :          0 :                                 write_sa->w2.s.aes_key_len = ROC_IE_OT_TLS_AES_KEY_LEN_256;
     519                 :            :                 }
     520         [ #  # ]:          0 :                 if (crypto_xfrm->aead.algo == RTE_CRYPTO_AEAD_CHACHA20_POLY1305) {
     521                 :          0 :                         write_sa->w2.s.cipher_select = ROC_IE_OT_TLS_CIPHER_CHACHA_POLY;
     522                 :          0 :                         write_sa->w2.s.aes_key_len = ROC_IE_OT_TLS_AES_KEY_LEN_256;
     523                 :            :                 }
     524                 :            : 
     525                 :          0 :                 key = crypto_xfrm->aead.key.data;
     526         [ #  # ]:          0 :                 memcpy(cipher_key, key, length);
     527                 :            : 
     528         [ #  # ]:          0 :                 if (tls_ver == RTE_SECURITY_VERSION_TLS_1_2)
     529                 :          0 :                         memcpy(((uint8_t *)cipher_key + 32), &tls_xfrm->tls_1_2.imp_nonce, 4);
     530         [ #  # ]:          0 :                 else if (tls_ver == RTE_SECURITY_VERSION_DTLS_1_2)
     531                 :          0 :                         memcpy(((uint8_t *)cipher_key + 32), &tls_xfrm->dtls_1_2.imp_nonce, 4);
     532         [ #  # ]:          0 :                 else if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3)
     533                 :          0 :                         memcpy(((uint8_t *)cipher_key + 32), &tls_xfrm->tls_1_3.imp_nonce, 12);
     534                 :            : 
     535                 :          0 :                 goto key_swap;
     536                 :            :         }
     537                 :            : 
     538         [ #  # ]:          0 :         if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
     539                 :            :                 auth_xfrm = crypto_xfrm;
     540                 :          0 :                 cipher_xfrm = crypto_xfrm->next;
     541                 :            :         } else {
     542                 :            :                 cipher_xfrm = crypto_xfrm;
     543                 :          0 :                 auth_xfrm = crypto_xfrm->next;
     544                 :            :         }
     545                 :            : 
     546         [ #  # ]:          0 :         if (cipher_xfrm != NULL) {
     547         [ #  # ]:          0 :                 if (cipher_xfrm->cipher.algo == RTE_CRYPTO_CIPHER_3DES_CBC) {
     548                 :          0 :                         write_sa->w2.s.cipher_select = ROC_IE_OT_TLS_CIPHER_3DES;
     549                 :          0 :                         length = cipher_xfrm->cipher.key.length;
     550         [ #  # ]:          0 :                 } else if (cipher_xfrm->cipher.algo == RTE_CRYPTO_CIPHER_AES_CBC) {
     551                 :          0 :                         write_sa->w2.s.cipher_select = ROC_IE_OT_TLS_CIPHER_AES_CBC;
     552                 :          0 :                         length = cipher_xfrm->cipher.key.length;
     553         [ #  # ]:          0 :                         if (length == 16)
     554                 :          0 :                                 write_sa->w2.s.aes_key_len = ROC_IE_OT_TLS_AES_KEY_LEN_128;
     555         [ #  # ]:          0 :                         else if (length == 32)
     556                 :          0 :                                 write_sa->w2.s.aes_key_len = ROC_IE_OT_TLS_AES_KEY_LEN_256;
     557                 :            :                         else
     558                 :            :                                 return -EINVAL;
     559                 :            :                 } else {
     560                 :            :                         return -EINVAL;
     561                 :            :                 }
     562                 :            : 
     563                 :          0 :                 key = cipher_xfrm->cipher.key.data;
     564         [ #  # ]:          0 :                 if (key != NULL && length != 0) {
     565                 :            :                         /* Copy encryption key */
     566                 :          0 :                         memcpy(cipher_key, key, length);
     567                 :            :                 }
     568                 :            :         }
     569                 :            : 
     570         [ #  # ]:          0 :         if (auth_xfrm != NULL) {
     571         [ #  # ]:          0 :                 if (auth_xfrm->auth.algo == RTE_CRYPTO_AUTH_MD5_HMAC)
     572                 :          0 :                         write_sa->w2.s.mac_select = ROC_IE_OT_TLS_MAC_MD5;
     573         [ #  # ]:          0 :                 else if (auth_xfrm->auth.algo == RTE_CRYPTO_AUTH_SHA1_HMAC)
     574                 :          0 :                         write_sa->w2.s.mac_select = ROC_IE_OT_TLS_MAC_SHA1;
     575         [ #  # ]:          0 :                 else if (auth_xfrm->auth.algo == RTE_CRYPTO_AUTH_SHA256_HMAC)
     576                 :          0 :                         write_sa->w2.s.mac_select = ROC_IE_OT_TLS_MAC_SHA2_256;
     577         [ #  # ]:          0 :                 else if (auth_xfrm->auth.algo == RTE_CRYPTO_AUTH_SHA384_HMAC)
     578                 :          0 :                         write_sa->w2.s.mac_select = ROC_IE_OT_TLS_MAC_SHA2_384;
     579                 :            :                 else
     580                 :            :                         return -EINVAL;
     581                 :            : 
     582                 :          0 :                 roc_se_hmac_opad_ipad_gen(write_sa->w2.s.mac_select, auth_xfrm->auth.key.data,
     583                 :          0 :                                           auth_xfrm->auth.key.length, write_sa->tls_12.opad_ipad,
     584                 :            :                                           ROC_SE_TLS);
     585                 :            :         }
     586                 :            : 
     587                 :          0 :         tmp_key = (uint64_t *)write_sa->tls_12.opad_ipad;
     588         [ #  # ]:          0 :         for (i = 0; i < (int)(ROC_CTX_MAX_OPAD_IPAD_LEN / sizeof(uint64_t)); i++)
     589         [ #  # ]:          0 :                 tmp_key[i] = rte_be_to_cpu_64(tmp_key[i]);
     590                 :            : 
     591                 :          0 : key_swap:
     592                 :            :         tmp_key = (uint64_t *)cipher_key;
     593         [ #  # ]:          0 :         for (i = 0; i < (int)(ROC_IE_OT_TLS_CTX_MAX_KEY_IV_LEN / sizeof(uint64_t)); i++)
     594         [ #  # ]:          0 :                 tmp_key[i] = rte_be_to_cpu_64(tmp_key[i]);
     595                 :            : 
     596                 :          0 :         write_sa->w0.s.ctx_hdr_size = ROC_IE_OT_TLS_CTX_HDR_SIZE;
     597                 :            :         /* Entire context size in 128B units */
     598                 :          0 :         write_sa->w0.s.ctx_size =
     599                 :            :                 (PLT_ALIGN_CEIL(sizeof(struct roc_ie_ot_tls_write_sa), ROC_CTX_UNIT_128B) /
     600                 :            :                  ROC_CTX_UNIT_128B) -
     601                 :            :                 1;
     602                 :            :         offset = offsetof(struct roc_ie_ot_tls_write_sa, tls_12.w26_rsvd7);
     603                 :            : 
     604         [ #  # ]:          0 :         if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3) {
     605                 :            :                 offset = offsetof(struct roc_ie_ot_tls_write_sa, tls_13.w10_rsvd7);
     606                 :          0 :                 write_sa->w0.s.ctx_size -= 1;
     607                 :            :         }
     608                 :            : 
     609                 :            :         /* Word offset for HW managed CTX field */
     610                 :          0 :         write_sa->w0.s.hw_ctx_off = offset / 8;
     611                 :          0 :         write_sa->w0.s.ctx_push_size = write_sa->w0.s.hw_ctx_off;
     612                 :            : 
     613                 :          0 :         write_sa->w0.s.aop_valid = 1;
     614                 :            : 
     615                 :          0 :         write_sa->w2.s.iv_at_cptr = ROC_IE_OT_TLS_IV_SRC_DEFAULT;
     616                 :            : 
     617         [ #  # ]:          0 :         if (write_sa->w2.s.version_select != ROC_IE_OT_TLS_VERSION_TLS_13) {
     618                 :            : #ifdef LA_IPSEC_DEBUG
     619                 :            :                 if (tls_xfrm->options.iv_gen_disable == 1)
     620                 :            :                         write_sa->w2.s.iv_at_cptr = ROC_IE_OT_TLS_IV_SRC_FROM_SA;
     621                 :            : #else
     622         [ #  # ]:          0 :                 if (tls_xfrm->options.iv_gen_disable) {
     623                 :          0 :                         plt_err("Application provided IV is not supported");
     624                 :          0 :                         return -ENOTSUP;
     625                 :            :                 }
     626                 :            : #endif
     627                 :            :         }
     628                 :            : 
     629                 :            :         rte_wmb();
     630                 :            : 
     631                 :          0 :         return 0;
     632                 :            : }
     633                 :            : 
     634                 :            : static int
     635                 :          0 : cn10k_tls_read_sa_create(struct roc_cpt *roc_cpt, struct roc_cpt_lf *lf,
     636                 :            :                          struct rte_security_tls_record_xform *tls_xfrm,
     637                 :            :                          struct rte_crypto_sym_xform *crypto_xfrm,
     638                 :            :                          struct cn10k_sec_session *sec_sess)
     639                 :            : {
     640                 :            :         struct roc_ie_ot_tls_read_sa *sa_dptr;
     641                 :          0 :         uint8_t tls_ver = tls_xfrm->ver;
     642                 :            :         struct cn10k_tls_record *tls;
     643                 :            :         union cpt_inst_w4 inst_w4;
     644                 :            :         void *read_sa;
     645                 :            :         int ret = 0;
     646                 :            : 
     647                 :            :         tls = &sec_sess->tls_rec;
     648                 :          0 :         read_sa = &tls->read_sa;
     649                 :            : 
     650                 :            :         /* Allocate memory to be used as dptr for CPT ucode WRITE_SA op */
     651                 :          0 :         sa_dptr = plt_zmalloc(sizeof(struct roc_ie_ot_tls_read_sa), 8);
     652         [ #  # ]:          0 :         if (sa_dptr == NULL) {
     653                 :          0 :                 plt_err("Could not allocate memory for SA DPTR");
     654                 :          0 :                 return -ENOMEM;
     655                 :            :         }
     656                 :            : 
     657                 :            :         /* Translate security parameters to SA */
     658                 :          0 :         ret = tls_read_sa_fill(sa_dptr, tls_xfrm, crypto_xfrm, &sec_sess->tls_opt);
     659         [ #  # ]:          0 :         if (ret) {
     660                 :          0 :                 plt_err("Could not fill read session parameters");
     661                 :          0 :                 goto sa_dptr_free;
     662                 :            :         }
     663         [ #  # ]:          0 :         if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
     664                 :          0 :                 sec_sess->iv_offset = crypto_xfrm->aead.iv.offset;
     665                 :          0 :                 sec_sess->iv_length = crypto_xfrm->aead.iv.length;
     666         [ #  # ]:          0 :         } else if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
     667                 :          0 :                 sec_sess->iv_offset = crypto_xfrm->cipher.iv.offset;
     668                 :          0 :                 sec_sess->iv_length = crypto_xfrm->cipher.iv.length;
     669                 :            :         } else {
     670                 :          0 :                 sec_sess->iv_offset = crypto_xfrm->auth.iv.offset;
     671                 :          0 :                 sec_sess->iv_length = crypto_xfrm->auth.iv.length;
     672                 :            :         }
     673                 :            : 
     674                 :          0 :         sec_sess->proto = RTE_SECURITY_PROTOCOL_TLS_RECORD;
     675                 :            : 
     676                 :            :         /* pre-populate CPT INST word 4 */
     677                 :          0 :         inst_w4.u64 = 0;
     678                 :          0 :         if ((tls_ver == RTE_SECURITY_VERSION_TLS_1_2) ||
     679         [ #  # ]:          0 :             (tls_ver == RTE_SECURITY_VERSION_DTLS_1_2)) {
     680                 :          0 :                 inst_w4.s.opcode_major = ROC_IE_OT_TLS_MAJOR_OP_RECORD_DEC | ROC_IE_OT_INPLACE_BIT;
     681                 :          0 :                 sec_sess->tls_opt.tail_fetch_len = 0;
     682         [ #  # ]:          0 :                 if (sa_dptr->w2.s.cipher_select == ROC_IE_OT_TLS_CIPHER_3DES)
     683                 :          0 :                         sec_sess->tls_opt.tail_fetch_len = 1;
     684         [ #  # ]:          0 :                 else if (sa_dptr->w2.s.cipher_select == ROC_IE_OT_TLS_CIPHER_AES_CBC)
     685                 :          0 :                         sec_sess->tls_opt.tail_fetch_len = 2;
     686         [ #  # ]:          0 :         } else if (tls_xfrm->ver == RTE_SECURITY_VERSION_TLS_1_3) {
     687                 :          0 :                 inst_w4.s.opcode_major =
     688                 :            :                         ROC_IE_OT_TLS13_MAJOR_OP_RECORD_DEC | ROC_IE_OT_INPLACE_BIT;
     689                 :            :         }
     690                 :            : 
     691                 :          0 :         sec_sess->tls_opt.tls_ver = tls_ver;
     692                 :          0 :         sec_sess->inst.w4 = inst_w4.u64;
     693                 :          0 :         sec_sess->inst.w7 = cpt_inst_w7_get(roc_cpt, read_sa);
     694                 :            : 
     695                 :            :         memset(read_sa, 0, sizeof(struct roc_ie_ot_tls_read_sa));
     696                 :            : 
     697                 :            :         /* Copy word0 from sa_dptr to populate ctx_push_sz ctx_size fields */
     698                 :            :         memcpy(read_sa, sa_dptr, 8);
     699                 :            : 
     700                 :            :         rte_atomic_thread_fence(rte_memory_order_seq_cst);
     701                 :            : 
     702                 :            :         /* Write session using microcode opcode */
     703                 :          0 :         ret = roc_cpt_ctx_write(lf, sa_dptr, read_sa, sizeof(struct roc_ie_ot_tls_read_sa));
     704         [ #  # ]:          0 :         if (ret) {
     705                 :          0 :                 plt_err("Could not write read session to hardware");
     706                 :          0 :                 goto sa_dptr_free;
     707                 :            :         }
     708                 :            : 
     709                 :            :         /* Trigger CTX flush so that data is written back to DRAM */
     710                 :          0 :         ret = roc_cpt_lf_ctx_flush(lf, read_sa, true);
     711         [ #  # ]:          0 :         if (ret == -EFAULT) {
     712                 :          0 :                 plt_err("Could not flush TLS read session to hardware");
     713                 :          0 :                 goto sa_dptr_free;
     714                 :            :         }
     715                 :            : 
     716                 :            :         rte_atomic_thread_fence(rte_memory_order_seq_cst);
     717                 :            : 
     718                 :          0 : sa_dptr_free:
     719                 :          0 :         plt_free(sa_dptr);
     720                 :            : 
     721                 :          0 :         return ret;
     722                 :            : }
     723                 :            : 
     724                 :            : static int
     725                 :          0 : cn10k_tls_write_sa_create(struct roc_cpt *roc_cpt, struct roc_cpt_lf *lf,
     726                 :            :                           struct rte_security_tls_record_xform *tls_xfrm,
     727                 :            :                           struct rte_crypto_sym_xform *crypto_xfrm,
     728                 :            :                           struct cn10k_sec_session *sec_sess)
     729                 :            : {
     730                 :            :         struct roc_ie_ot_tls_write_sa *sa_dptr;
     731                 :          0 :         uint8_t tls_ver = tls_xfrm->ver;
     732                 :            :         struct cn10k_tls_record *tls;
     733                 :            :         union cpt_inst_w4 inst_w4;
     734                 :            :         void *write_sa;
     735                 :            :         int ret = 0;
     736                 :            : 
     737                 :            :         tls = &sec_sess->tls_rec;
     738                 :          0 :         write_sa = &tls->write_sa;
     739                 :            : 
     740                 :            :         /* Allocate memory to be used as dptr for CPT ucode WRITE_SA op */
     741                 :          0 :         sa_dptr = plt_zmalloc(sizeof(struct roc_ie_ot_tls_write_sa), 8);
     742         [ #  # ]:          0 :         if (sa_dptr == NULL) {
     743                 :          0 :                 plt_err("Could not allocate memory for SA DPTR");
     744                 :          0 :                 return -ENOMEM;
     745                 :            :         }
     746                 :            : 
     747                 :            :         /* Translate security parameters to SA */
     748                 :          0 :         ret = tls_write_sa_fill(sa_dptr, tls_xfrm, crypto_xfrm);
     749         [ #  # ]:          0 :         if (ret) {
     750                 :          0 :                 plt_err("Could not fill write session parameters");
     751                 :          0 :                 goto sa_dptr_free;
     752                 :            :         }
     753                 :            : 
     754         [ #  # ]:          0 :         if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
     755                 :          0 :                 sec_sess->iv_offset = crypto_xfrm->aead.iv.offset;
     756                 :          0 :                 sec_sess->iv_length = crypto_xfrm->aead.iv.length;
     757         [ #  # ]:          0 :         } else if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
     758                 :          0 :                 sec_sess->iv_offset = crypto_xfrm->cipher.iv.offset;
     759                 :          0 :                 sec_sess->iv_length = crypto_xfrm->cipher.iv.length;
     760                 :            :         } else {
     761                 :          0 :                 sec_sess->iv_offset = crypto_xfrm->next->cipher.iv.offset;
     762                 :          0 :                 sec_sess->iv_length = crypto_xfrm->next->cipher.iv.length;
     763                 :            :         }
     764                 :            : 
     765                 :          0 :         sec_sess->tls_opt.is_write = 1;
     766                 :          0 :         sec_sess->tls_opt.pad_shift = 0;
     767                 :          0 :         sec_sess->tls_opt.tls_ver = tls_ver;
     768                 :          0 :         sec_sess->tls_opt.enable_padding = tls_xfrm->options.extra_padding_enable;
     769                 :          0 :         sec_sess->max_extended_len = tls_write_rlens_get(tls_xfrm, crypto_xfrm);
     770                 :          0 :         sec_sess->proto = RTE_SECURITY_PROTOCOL_TLS_RECORD;
     771                 :            : 
     772                 :            :         /* pre-populate CPT INST word 4 */
     773                 :          0 :         inst_w4.u64 = 0;
     774                 :          0 :         if ((tls_ver == RTE_SECURITY_VERSION_TLS_1_2) ||
     775         [ #  # ]:          0 :             (tls_ver == RTE_SECURITY_VERSION_DTLS_1_2)) {
     776                 :          0 :                 inst_w4.s.opcode_major = ROC_IE_OT_TLS_MAJOR_OP_RECORD_ENC | ROC_IE_OT_INPLACE_BIT;
     777         [ #  # ]:          0 :                 if (sa_dptr->w2.s.cipher_select == ROC_IE_OT_TLS_CIPHER_3DES)
     778                 :          0 :                         sec_sess->tls_opt.pad_shift = 3;
     779                 :            :                 else
     780                 :          0 :                         sec_sess->tls_opt.pad_shift = 4;
     781         [ #  # ]:          0 :         } else if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3) {
     782                 :          0 :                 inst_w4.s.opcode_major =
     783                 :            :                         ROC_IE_OT_TLS13_MAJOR_OP_RECORD_ENC | ROC_IE_OT_INPLACE_BIT;
     784                 :            :         }
     785                 :          0 :         sec_sess->inst.w4 = inst_w4.u64;
     786                 :          0 :         sec_sess->inst.w7 = cpt_inst_w7_get(roc_cpt, write_sa);
     787                 :            : 
     788                 :            :         memset(write_sa, 0, sizeof(struct roc_ie_ot_tls_write_sa));
     789                 :            : 
     790                 :            :         /* Copy word0 from sa_dptr to populate ctx_push_sz ctx_size fields */
     791                 :            :         memcpy(write_sa, sa_dptr, 8);
     792                 :            : 
     793                 :            :         rte_atomic_thread_fence(rte_memory_order_seq_cst);
     794                 :            : 
     795                 :            :         /* Write session using microcode opcode */
     796                 :          0 :         ret = roc_cpt_ctx_write(lf, sa_dptr, write_sa, sizeof(struct roc_ie_ot_tls_write_sa));
     797         [ #  # ]:          0 :         if (ret) {
     798                 :          0 :                 plt_err("Could not write tls write session to hardware");
     799                 :          0 :                 goto sa_dptr_free;
     800                 :            :         }
     801                 :            : 
     802                 :            :         /* Trigger CTX flush so that data is written back to DRAM */
     803                 :          0 :         ret = roc_cpt_lf_ctx_flush(lf, write_sa, false);
     804         [ #  # ]:          0 :         if (ret == -EFAULT) {
     805                 :          0 :                 plt_err("Could not flush TLS write session to hardware");
     806                 :          0 :                 goto sa_dptr_free;
     807                 :            :         }
     808                 :            : 
     809                 :            :         rte_atomic_thread_fence(rte_memory_order_seq_cst);
     810                 :            : 
     811                 :          0 : sa_dptr_free:
     812                 :          0 :         plt_free(sa_dptr);
     813                 :            : 
     814                 :          0 :         return ret;
     815                 :            : }
     816                 :            : 
     817                 :            : int
     818                 :          0 : cn10k_tls_record_session_update(struct cnxk_cpt_vf *vf, struct cnxk_cpt_qp *qp,
     819                 :            :                                 struct cn10k_sec_session *sess,
     820                 :            :                                 struct rte_security_session_conf *conf)
     821                 :            : {
     822                 :            :         struct roc_cpt *roc_cpt;
     823                 :            :         int ret;
     824                 :            : 
     825         [ #  # ]:          0 :         if (conf->tls_record.type == RTE_SECURITY_TLS_SESS_TYPE_READ)
     826                 :            :                 return -ENOTSUP;
     827                 :            : 
     828                 :          0 :         roc_cpt = &vf->cpt;
     829                 :          0 :         ret = cn10k_tls_write_sa_create(roc_cpt, &qp->lf, &conf->tls_record, conf->crypto_xform,
     830                 :            :                                         (struct cn10k_sec_session *)sess);
     831                 :          0 :         return ret;
     832                 :            : }
     833                 :            : 
     834                 :            : int
     835                 :          0 : cn10k_tls_record_session_create(struct cnxk_cpt_vf *vf, struct cnxk_cpt_qp *qp,
     836                 :            :                                 struct rte_security_tls_record_xform *tls_xfrm,
     837                 :            :                                 struct rte_crypto_sym_xform *crypto_xfrm,
     838                 :            :                                 struct rte_security_session *sess)
     839                 :            : {
     840                 :            :         struct roc_cpt *roc_cpt;
     841                 :            :         int ret;
     842                 :            : 
     843                 :          0 :         ret = cnxk_tls_xform_verify(tls_xfrm, crypto_xfrm);
     844         [ #  # ]:          0 :         if (ret)
     845                 :            :                 return ret;
     846                 :            : 
     847                 :          0 :         roc_cpt = &vf->cpt;
     848                 :            : 
     849         [ #  # ]:          0 :         if (tls_xfrm->type == RTE_SECURITY_TLS_SESS_TYPE_READ)
     850                 :          0 :                 return cn10k_tls_read_sa_create(roc_cpt, &qp->lf, tls_xfrm, crypto_xfrm,
     851                 :            :                                                 (struct cn10k_sec_session *)sess);
     852                 :            :         else
     853                 :          0 :                 return cn10k_tls_write_sa_create(roc_cpt, &qp->lf, tls_xfrm, crypto_xfrm,
     854                 :            :                                                  (struct cn10k_sec_session *)sess);
     855                 :            : }
     856                 :            : 
     857                 :            : int
     858                 :          0 : cn10k_sec_tls_session_destroy(struct cnxk_cpt_qp *qp, struct cn10k_sec_session *sess)
     859                 :            : {
     860                 :            :         struct cn10k_tls_record *tls;
     861                 :            :         struct roc_cpt_lf *lf;
     862                 :            :         void *sa_dptr = NULL;
     863                 :            :         int ret;
     864                 :            : 
     865                 :          0 :         lf = &qp->lf;
     866                 :            : 
     867                 :            :         tls = &sess->tls_rec;
     868                 :            : 
     869                 :            :         /* Trigger CTX flush to write dirty data back to DRAM */
     870                 :          0 :         roc_cpt_lf_ctx_flush(lf, &tls->read_sa, false);
     871                 :            : 
     872                 :            :         ret = -1;
     873                 :            : 
     874         [ #  # ]:          0 :         if (sess->tls_opt.is_write) {
     875                 :          0 :                 sa_dptr = plt_zmalloc(sizeof(struct roc_ie_ot_tls_write_sa), 8);
     876         [ #  # ]:          0 :                 if (sa_dptr != NULL) {
     877                 :            :                         tls_write_sa_init(sa_dptr);
     878                 :            : 
     879                 :          0 :                         ret = roc_cpt_ctx_write(lf, sa_dptr, &tls->write_sa,
     880                 :            :                                                 sizeof(struct roc_ie_ot_tls_write_sa));
     881                 :            :                 }
     882                 :            :         } else {
     883                 :          0 :                 sa_dptr = plt_zmalloc(sizeof(struct roc_ie_ot_tls_read_sa), 8);
     884         [ #  # ]:          0 :                 if (sa_dptr != NULL) {
     885                 :            :                         tls_read_sa_init(sa_dptr);
     886                 :            : 
     887                 :          0 :                         ret = roc_cpt_ctx_write(lf, sa_dptr, &tls->read_sa,
     888                 :            :                                                 sizeof(struct roc_ie_ot_tls_read_sa));
     889                 :            :                 }
     890                 :            :         }
     891                 :            : 
     892                 :          0 :         plt_free(sa_dptr);
     893                 :            : 
     894         [ #  # ]:          0 :         if (ret) {
     895                 :            :                 /* MC write_ctx failed. Attempt reload of CTX */
     896                 :            : 
     897                 :            :                 /* Wait for 1 ms so that flush is complete */
     898                 :            :                 rte_delay_ms(1);
     899                 :            : 
     900                 :            :                 rte_atomic_thread_fence(rte_memory_order_seq_cst);
     901                 :            : 
     902                 :            :                 /* Trigger CTX reload to fetch new data from DRAM */
     903                 :          0 :                 roc_cpt_lf_ctx_reload(lf, &tls->read_sa);
     904                 :            :         }
     905                 :            : 
     906                 :          0 :         return 0;
     907                 :            : }

Generated by: LCOV version 1.14