LCOV - code coverage report
Current view: top level - drivers/crypto/cnxk - cn20k_tls.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 393 0.0 %
Date: 2025-08-01 17:49:26 Functions: 0 13 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 298 0.0 %

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

Generated by: LCOV version 1.14