LCOV - code coverage report
Current view: top level - drivers/common/cnxk - roc_se.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 303 0.0 %
Date: 2025-02-01 18:54:23 Functions: 0 9 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 187 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(C) 2021 Marvell.
       3                 :            :  */
       4                 :            : 
       5                 :            : #include "roc_api.h"
       6                 :            : 
       7                 :            : static uint8_t zuc_key128[32] = {
       8                 :            :         0x44, 0xD7, 0x26, 0xBC, 0x62, 0x6B, 0x13, 0x5E, 0x57, 0x89, 0x35,
       9                 :            :         0xE2, 0x71, 0x35, 0x09, 0xAF, 0x4D, 0x78, 0x2F, 0x13, 0x6B, 0xC4,
      10                 :            :         0x1A, 0xF1, 0x5E, 0x26, 0x3C, 0x4D, 0x78, 0x9A, 0x47, 0xAC};
      11                 :            : 
      12                 :            : static uint8_t zuc_key256[16] = {0x22, 0x2f, 0x24, 0x2a, 0x6d, 0x40,
      13                 :            :                                  0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
      14                 :            :                                  0x40, 0x52, 0x10, 0x30};
      15                 :            : 
      16                 :            : static uint8_t zuc_key256_mac4[16] = {0x22, 0x2f, 0x25, 0x2a, 0x6d, 0x40,
      17                 :            :                                       0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
      18                 :            :                                       0x40, 0x52, 0x10, 0x30};
      19                 :            : 
      20                 :            : static uint8_t zuc_key256_mac8[16] = {0x23, 0x2f, 0x24, 0x2a, 0x6d, 0x40,
      21                 :            :                                       0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
      22                 :            :                                       0x40, 0x52, 0x10, 0x30};
      23                 :            : 
      24                 :            : static uint8_t zuc_key256_mac16[16] = {0x23, 0x2f, 0x25, 0x2a, 0x6d, 0x40,
      25                 :            :                                        0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
      26                 :            :                                        0x40, 0x52, 0x10, 0x30};
      27                 :            : 
      28                 :            : static inline void
      29                 :          0 : cpt_snow3g_key_gen(const uint8_t *ck, uint32_t *keyx)
      30                 :            : {
      31                 :            :         int i, base;
      32                 :            : 
      33         [ #  # ]:          0 :         for (i = 0; i < 4; i++) {
      34                 :          0 :                 base = 4 * i;
      35                 :          0 :                 keyx[3 - i] = (ck[base] << 24) | (ck[base + 1] << 16) |
      36                 :          0 :                               (ck[base + 2] << 8) | (ck[base + 3]);
      37         [ #  # ]:          0 :                 keyx[3 - i] = plt_cpu_to_be_32(keyx[3 - i]);
      38                 :            :         }
      39                 :          0 : }
      40                 :            : 
      41                 :            : static inline int
      42                 :            : cpt_ciph_aes_key_validate(uint16_t key_len)
      43                 :            : {
      44         [ #  # ]:          0 :         switch (key_len) {
      45                 :            :         case 16:
      46                 :            :         case 24:
      47                 :            :         case 32:
      48                 :            :                 return 0;
      49                 :          0 :         default:
      50                 :          0 :                 return -1;
      51                 :            :         }
      52                 :            : }
      53                 :            : 
      54                 :            : static inline int
      55                 :          0 : cpt_ciph_type_set(roc_se_cipher_type type, struct roc_se_ctx *ctx, uint16_t key_len)
      56                 :            : {
      57                 :          0 :         bool chained_op = ctx->ciph_then_auth || ctx->auth_then_ciph;
      58                 :            :         int fc_type = 0;
      59                 :            : 
      60   [ #  #  #  #  :          0 :         switch (type) {
             #  #  #  # ]
      61                 :            :         case ROC_SE_DES3_CBC:
      62                 :            :         case ROC_SE_DES3_ECB:
      63                 :            :         case ROC_SE_DES_DOCSISBPI:
      64                 :            :                 fc_type = ROC_SE_FC_GEN;
      65                 :            :                 break;
      66         [ #  # ]:          0 :         case ROC_SE_AES_CBC:
      67                 :            :         case ROC_SE_AES_ECB:
      68                 :            :         case ROC_SE_AES_CFB:
      69                 :            :         case ROC_SE_AES_CTR:
      70                 :            :         case ROC_SE_AES_GCM:
      71                 :            :         case ROC_SE_AES_CCM:
      72                 :            :         case ROC_SE_AES_DOCSISBPI:
      73         [ #  # ]:          0 :                 if (unlikely(cpt_ciph_aes_key_validate(key_len) != 0))
      74                 :            :                         return -1;
      75                 :            :                 fc_type = ROC_SE_FC_GEN;
      76                 :            :                 break;
      77                 :            :         case ROC_SE_CHACHA20:
      78                 :            :                 fc_type = ROC_SE_FC_GEN;
      79                 :            :                 break;
      80                 :          0 :         case ROC_SE_AES_XTS:
      81                 :          0 :                 key_len = key_len / 2;
      82         [ #  # ]:          0 :                 if (unlikely(key_len == 24)) {
      83                 :          0 :                         plt_err("Invalid AES key len for XTS");
      84                 :          0 :                         return -1;
      85                 :            :                 }
      86         [ #  # ]:          0 :                 if (unlikely(cpt_ciph_aes_key_validate(key_len) != 0))
      87                 :            :                         return -1;
      88                 :            :                 fc_type = ROC_SE_FC_GEN;
      89                 :            :                 break;
      90                 :          0 :         case ROC_SE_ZUC_EEA3:
      91         [ #  # ]:          0 :                 if (unlikely(key_len != 16)) {
      92                 :            :                         /*
      93                 :            :                          * ZUC 256 is not supported with older microcode
      94                 :            :                          * where pdcp_iv_offset is 16
      95                 :            :                          */
      96   [ #  #  #  # ]:          0 :                         if (chained_op || (ctx->pdcp_iv_offset == 16)) {
      97                 :          0 :                                 plt_err("ZUC 256 is not supported with chained operations");
      98                 :          0 :                                 return -1;
      99                 :            :                         }
     100                 :            :                 }
     101         [ #  # ]:          0 :                 if (chained_op)
     102                 :            :                         fc_type = ROC_SE_PDCP_CHAIN;
     103                 :            :                 else
     104                 :            :                         fc_type = ROC_SE_PDCP;
     105                 :            :                 break;
     106                 :          0 :         case ROC_SE_SNOW3G_UEA2:
     107         [ #  # ]:          0 :                 if (unlikely(key_len != 16))
     108                 :            :                         return -1;
     109         [ #  # ]:          0 :                 if (chained_op)
     110                 :            :                         fc_type = ROC_SE_PDCP_CHAIN;
     111                 :            :                 else
     112                 :            :                         fc_type = ROC_SE_PDCP;
     113                 :            :                 break;
     114                 :          0 :         case ROC_SE_AES_CTR_EEA2:
     115         [ #  # ]:          0 :                 if (chained_op)
     116                 :            :                         fc_type = ROC_SE_PDCP_CHAIN;
     117                 :            :                 else
     118                 :            :                         fc_type = ROC_SE_PDCP;
     119                 :            :                 break;
     120                 :          0 :         case ROC_SE_KASUMI_F8_CBC:
     121                 :            :         case ROC_SE_KASUMI_F8_ECB:
     122         [ #  # ]:          0 :                 if (unlikely(key_len != 16))
     123                 :            :                         return -1;
     124                 :            :                 /* No support for AEAD yet */
     125         [ #  # ]:          0 :                 if (unlikely(ctx->hash_type))
     126                 :            :                         return -1;
     127                 :            :                 fc_type = ROC_SE_KASUMI;
     128                 :            :                 break;
     129                 :            :         default:
     130                 :            :                 return -1;
     131                 :            :         }
     132                 :            : 
     133                 :          0 :         ctx->fc_type = fc_type;
     134                 :          0 :         return 0;
     135                 :            : }
     136                 :            : 
     137                 :            : static inline void
     138                 :          0 : cpt_ciph_aes_key_type_set(struct roc_se_context *fctx, uint16_t key_len)
     139                 :            : {
     140                 :            :         roc_se_aes_type aes_key_type = 0;
     141                 :            : 
     142   [ #  #  #  # ]:          0 :         switch (key_len) {
     143                 :            :         case 16:
     144                 :            :                 aes_key_type = ROC_SE_AES_128_BIT;
     145                 :            :                 break;
     146                 :          0 :         case 24:
     147                 :            :                 aes_key_type = ROC_SE_AES_192_BIT;
     148                 :          0 :                 break;
     149                 :          0 :         case 32:
     150                 :            :                 aes_key_type = ROC_SE_AES_256_BIT;
     151                 :          0 :                 break;
     152                 :          0 :         default:
     153                 :            :                 /* This should not happen */
     154                 :          0 :                 plt_err("Invalid AES key len");
     155                 :          0 :                 return;
     156                 :            :         }
     157                 :          0 :         fctx->enc.aes_key = aes_key_type;
     158                 :            : }
     159                 :            : 
     160                 :            : void
     161                 :          0 : roc_se_hmac_opad_ipad_gen(roc_se_auth_type auth_type, const uint8_t *key, uint16_t length,
     162                 :            :                           uint8_t *opad_ipad, roc_se_op_type op_type)
     163                 :            : {
     164                 :          0 :         uint8_t opad[128] = {[0 ... 127] = 0x5c};
     165                 :          0 :         uint8_t ipad[128] = {[0 ... 127] = 0x36};
     166                 :            :         uint8_t ipad_offset, opad_offset;
     167                 :            :         uint32_t i;
     168                 :            : 
     169         [ #  # ]:          0 :         if (op_type == ROC_SE_IPSEC) {
     170         [ #  # ]:          0 :                 if ((auth_type == ROC_SE_MD5_TYPE) || (auth_type == ROC_SE_SHA1_TYPE))
     171                 :            :                         ipad_offset = 24;
     172                 :            :                 else
     173                 :            :                         ipad_offset = 64;
     174                 :            :                 opad_offset = 0;
     175         [ #  # ]:          0 :         } else if (op_type == ROC_SE_TLS) {
     176                 :            :                 ipad_offset = 64;
     177                 :            :                 opad_offset = 0;
     178                 :            :         } else {
     179                 :            :                 ipad_offset = 0;
     180                 :            :                 opad_offset = 64;
     181                 :            :         }
     182                 :            : 
     183                 :            :         /* HMAC OPAD and IPAD */
     184   [ #  #  #  # ]:          0 :         for (i = 0; i < 128 && i < length; i++) {
     185                 :          0 :                 opad[i] = opad[i] ^ key[i];
     186                 :          0 :                 ipad[i] = ipad[i] ^ key[i];
     187                 :            :         }
     188                 :            : 
     189                 :            :         /* Precompute hash of HMAC OPAD and IPAD to avoid
     190                 :            :          * per packet computation
     191                 :            :          */
     192   [ #  #  #  #  :          0 :         switch (auth_type) {
                #  #  # ]
     193                 :          0 :         case ROC_SE_MD5_TYPE:
     194                 :          0 :                 roc_hash_md5_gen(opad, (uint32_t *)&opad_ipad[opad_offset]);
     195                 :          0 :                 roc_hash_md5_gen(ipad, (uint32_t *)&opad_ipad[ipad_offset]);
     196                 :          0 :                 break;
     197                 :          0 :         case ROC_SE_SHA1_TYPE:
     198                 :          0 :                 roc_hash_sha1_gen(opad, (uint32_t *)&opad_ipad[opad_offset]);
     199                 :          0 :                 roc_hash_sha1_gen(ipad, (uint32_t *)&opad_ipad[ipad_offset]);
     200                 :          0 :                 break;
     201                 :          0 :         case ROC_SE_SHA2_SHA224:
     202                 :          0 :                 roc_hash_sha256_gen(opad, (uint32_t *)&opad_ipad[opad_offset], 224);
     203                 :          0 :                 roc_hash_sha256_gen(ipad, (uint32_t *)&opad_ipad[ipad_offset], 224);
     204                 :          0 :                 break;
     205                 :          0 :         case ROC_SE_SHA2_SHA256:
     206                 :          0 :                 roc_hash_sha256_gen(opad, (uint32_t *)&opad_ipad[opad_offset], 256);
     207                 :          0 :                 roc_hash_sha256_gen(ipad, (uint32_t *)&opad_ipad[ipad_offset], 256);
     208                 :          0 :                 break;
     209                 :          0 :         case ROC_SE_SHA2_SHA384:
     210                 :          0 :                 roc_hash_sha512_gen(opad, (uint64_t *)&opad_ipad[opad_offset], 384);
     211                 :          0 :                 roc_hash_sha512_gen(ipad, (uint64_t *)&opad_ipad[ipad_offset], 384);
     212                 :          0 :                 break;
     213                 :          0 :         case ROC_SE_SHA2_SHA512:
     214                 :          0 :                 roc_hash_sha512_gen(opad, (uint64_t *)&opad_ipad[opad_offset], 512);
     215                 :          0 :                 roc_hash_sha512_gen(ipad, (uint64_t *)&opad_ipad[ipad_offset], 512);
     216                 :          0 :                 break;
     217                 :            :         default:
     218                 :            :                 break;
     219                 :            :         }
     220                 :          0 : }
     221                 :            : 
     222                 :            : static int
     223                 :          0 : cpt_pdcp_chain_key_type_get(uint16_t key_len)
     224                 :            : {
     225                 :            :         roc_se_aes_type key_type;
     226                 :            : 
     227   [ #  #  #  # ]:          0 :         switch (key_len) {
     228                 :            :         case 16:
     229                 :            :                 key_type = ROC_SE_AES_128_BIT;
     230                 :            :                 break;
     231                 :          0 :         case 24:
     232                 :            :                 key_type = ROC_SE_AES_192_BIT;
     233                 :          0 :                 break;
     234                 :          0 :         case 32:
     235                 :            :                 key_type = ROC_SE_AES_256_BIT;
     236                 :          0 :                 break;
     237                 :          0 :         default:
     238                 :          0 :                 plt_err("Invalid key len");
     239                 :          0 :                 return -ENOTSUP;
     240                 :            :         }
     241                 :            : 
     242                 :          0 :         return key_type;
     243                 :            : }
     244                 :            : 
     245                 :            : static void
     246                 :          0 : cpt_zuc_const_update(uint8_t *zuc_const, int key_len, int mac_len)
     247                 :            : {
     248         [ #  # ]:          0 :         if (key_len == 16) {
     249                 :            :                 memcpy(zuc_const, zuc_key128, 32);
     250         [ #  # ]:          0 :         } else if (key_len == 32) {
     251   [ #  #  #  # ]:          0 :                 switch (mac_len) {
     252                 :            :                 case 4:
     253                 :            :                         memcpy(zuc_const, zuc_key256_mac4, 16);
     254                 :            :                         break;
     255                 :            :                 case 8:
     256                 :            :                         memcpy(zuc_const, zuc_key256_mac8, 16);
     257                 :            :                         break;
     258                 :            :                 case 16:
     259                 :            :                         memcpy(zuc_const, zuc_key256_mac16, 16);
     260                 :            :                         break;
     261                 :          0 :                 default:
     262                 :          0 :                         plt_err("Unsupported mac len");
     263                 :            :                 }
     264                 :            :         }
     265                 :          0 : }
     266                 :            : 
     267                 :            : int
     268                 :          0 : roc_se_auth_key_set(struct roc_se_ctx *se_ctx, roc_se_auth_type type, const uint8_t *key,
     269                 :            :                     uint16_t key_len, uint16_t mac_len)
     270                 :            : {
     271                 :            :         struct roc_se_kasumi_ctx *k_ctx;
     272                 :            :         struct roc_se_pdcp_ctx *pctx;
     273                 :            :         struct roc_se_context *fctx;
     274                 :            :         uint8_t opcode_minor;
     275                 :            :         bool chained_op;
     276                 :            : 
     277         [ #  # ]:          0 :         if (se_ctx == NULL)
     278                 :            :                 return -1;
     279                 :            : 
     280                 :            :         pctx = &se_ctx->se_ctx.pctx;
     281                 :            :         k_ctx = &se_ctx->se_ctx.k_ctx;
     282                 :            :         fctx = &se_ctx->se_ctx.fctx;
     283                 :            : 
     284                 :          0 :         chained_op = se_ctx->ciph_then_auth || se_ctx->auth_then_ciph;
     285                 :            : 
     286         [ #  # ]:          0 :         if ((type >= ROC_SE_ZUC_EIA3) && (type <= ROC_SE_KASUMI_F9_ECB)) {
     287                 :            :                 uint32_t keyx[4];
     288                 :            :                 int key_type;
     289                 :            : 
     290         [ #  # ]:          0 :                 if (!key_len)
     291                 :            :                         return -1;
     292                 :            : 
     293         [ #  # ]:          0 :                 if (se_ctx->fc_type == ROC_SE_FC_GEN) {
     294                 :          0 :                         plt_err("Cipher and Auth algorithm combination is not supported");
     295                 :          0 :                         return -1;
     296                 :            :                 }
     297                 :            : 
     298                 :            :                 /* For ZUC/SNOW3G/Kasumi */
     299   [ #  #  #  #  :          0 :                 switch (type) {
                      # ]
     300                 :          0 :                 case ROC_SE_SNOW3G_UIA2:
     301                 :          0 :                         pctx->w0.s.state_conf = ROC_SE_PDCP_CHAIN_CTX_KEY_IV;
     302                 :          0 :                         pctx->w0.s.auth_type = ROC_SE_PDCP_CHAIN_ALG_TYPE_SNOW3G;
     303                 :          0 :                         pctx->w0.s.mac_len = mac_len;
     304                 :          0 :                         pctx->w0.s.auth_key_len = key_len;
     305                 :          0 :                         se_ctx->fc_type = ROC_SE_PDCP_CHAIN;
     306                 :          0 :                         cpt_snow3g_key_gen(key, keyx);
     307         [ #  # ]:          0 :                         memcpy(pctx->st.auth_key, keyx, key_len);
     308                 :            : 
     309         [ #  # ]:          0 :                         if (!chained_op)
     310                 :          0 :                                 se_ctx->fc_type = ROC_SE_PDCP;
     311                 :          0 :                         se_ctx->pdcp_auth_alg = ROC_SE_PDCP_ALG_TYPE_SNOW3G;
     312                 :          0 :                         se_ctx->zsk_flags = 0x1;
     313                 :          0 :                         break;
     314                 :          0 :                 case ROC_SE_ZUC_EIA3:
     315         [ #  # ]:          0 :                         if (unlikely(key_len != 16)) {
     316                 :            :                                 /*
     317                 :            :                                  * ZUC 256 is not supported with older microcode
     318                 :            :                                  * where pdcp_iv_offset is 16
     319                 :            :                                  */
     320   [ #  #  #  # ]:          0 :                                 if (chained_op || (se_ctx->pdcp_iv_offset == 16)) {
     321                 :          0 :                                         plt_err("ZUC 256 is not supported with chained operations");
     322                 :          0 :                                         return -1;
     323                 :            :                                 }
     324                 :            :                         }
     325                 :          0 :                         key_type = cpt_pdcp_chain_key_type_get(key_len);
     326         [ #  # ]:          0 :                         if (key_type < 0)
     327                 :            :                                 return key_type;
     328                 :          0 :                         pctx->w0.s.auth_key_len = key_type;
     329                 :          0 :                         pctx->w0.s.state_conf = ROC_SE_PDCP_CHAIN_CTX_KEY_IV;
     330                 :          0 :                         pctx->w0.s.auth_type = ROC_SE_PDCP_CHAIN_ALG_TYPE_ZUC;
     331                 :          0 :                         pctx->w0.s.mac_len = mac_len;
     332         [ #  # ]:          0 :                         memcpy(pctx->st.auth_key, key, key_len);
     333         [ #  # ]:          0 :                         if (key_len == 32)
     334                 :            :                                 roc_se_zuc_bytes_swap(pctx->st.auth_key, key_len);
     335                 :          0 :                         cpt_zuc_const_update(pctx->st.auth_zuc_const, key_len, mac_len);
     336                 :          0 :                         se_ctx->fc_type = ROC_SE_PDCP_CHAIN;
     337                 :            : 
     338         [ #  # ]:          0 :                         if (!chained_op)
     339                 :          0 :                                 se_ctx->fc_type = ROC_SE_PDCP;
     340                 :          0 :                         se_ctx->pdcp_auth_alg = ROC_SE_PDCP_ALG_TYPE_ZUC;
     341                 :          0 :                         se_ctx->zsk_flags = 0x1;
     342                 :          0 :                         break;
     343                 :          0 :                 case ROC_SE_AES_CMAC_EIA2:
     344                 :          0 :                         key_type = cpt_pdcp_chain_key_type_get(key_len);
     345         [ #  # ]:          0 :                         if (key_type < 0)
     346                 :            :                                 return key_type;
     347                 :          0 :                         pctx->w0.s.auth_key_len = key_type;
     348                 :          0 :                         pctx->w0.s.state_conf = ROC_SE_PDCP_CHAIN_CTX_KEY_IV;
     349                 :          0 :                         pctx->w0.s.auth_type = ROC_SE_PDCP_ALG_TYPE_AES_CTR;
     350                 :          0 :                         pctx->w0.s.mac_len = mac_len;
     351         [ #  # ]:          0 :                         memcpy(pctx->st.auth_key, key, key_len);
     352                 :          0 :                         se_ctx->fc_type = ROC_SE_PDCP_CHAIN;
     353                 :            : 
     354         [ #  # ]:          0 :                         if (!chained_op)
     355                 :          0 :                                 se_ctx->fc_type = ROC_SE_PDCP;
     356                 :          0 :                         se_ctx->pdcp_auth_alg = ROC_SE_PDCP_ALG_TYPE_AES_CMAC;
     357                 :          0 :                         se_ctx->eia2 = 1;
     358                 :          0 :                         se_ctx->zsk_flags = 0x1;
     359                 :          0 :                         break;
     360                 :          0 :                 case ROC_SE_KASUMI_F9_ECB:
     361                 :            :                         /* Kasumi ECB mode */
     362                 :          0 :                         se_ctx->k_ecb = 1;
     363                 :          0 :                         memcpy(k_ctx->ci_key, key, key_len);
     364                 :          0 :                         se_ctx->fc_type = ROC_SE_KASUMI;
     365                 :          0 :                         se_ctx->zsk_flags = 0x1;
     366                 :          0 :                         break;
     367                 :          0 :                 case ROC_SE_KASUMI_F9_CBC:
     368                 :          0 :                         memcpy(k_ctx->ci_key, key, key_len);
     369                 :          0 :                         se_ctx->fc_type = ROC_SE_KASUMI;
     370                 :          0 :                         se_ctx->zsk_flags = 0x1;
     371                 :          0 :                         break;
     372                 :            :                 default:
     373                 :            :                         return -1;
     374                 :            :                 }
     375                 :            : 
     376   [ #  #  #  # ]:          0 :                 if ((se_ctx->fc_type == ROC_SE_PDCP_CHAIN) && (mac_len != 4)) {
     377                 :          0 :                         plt_err("Only digest length of 4 is supported with PDCP chain");
     378                 :          0 :                         return -1;
     379                 :            :                 }
     380                 :            : 
     381                 :          0 :                 se_ctx->mac_len = mac_len;
     382                 :          0 :                 se_ctx->hash_type = type;
     383         [ #  # ]:          0 :                 if (chained_op)
     384         [ #  # ]:          0 :                         opcode_minor = se_ctx->ciph_then_auth ? 2 : 3;
     385                 :            :                 else
     386                 :            :                         opcode_minor = ((1 << 4) | 1);
     387                 :            : 
     388                 :          0 :                 se_ctx->template_w4.s.opcode_minor = opcode_minor;
     389                 :          0 :                 return 0;
     390                 :            :         }
     391                 :            : 
     392   [ #  #  #  #  :          0 :         if (!se_ctx->fc_type || (type && type != ROC_SE_GMAC_TYPE && !se_ctx->enc_cipher))
                   #  # ]
     393                 :          0 :                 se_ctx->fc_type = ROC_SE_HASH_HMAC;
     394                 :            : 
     395   [ #  #  #  # ]:          0 :         if (se_ctx->fc_type == ROC_SE_FC_GEN && key_len > 64) {
     396                 :          0 :                 plt_err("Maximum auth key length supported is 64");
     397                 :          0 :                 return -1;
     398                 :            :         }
     399                 :            : 
     400                 :            :         /* For GMAC auth, cipher must be NULL */
     401         [ #  # ]:          0 :         if (type == ROC_SE_GMAC_TYPE) {
     402                 :          0 :                 fctx->enc.enc_cipher = 0;
     403                 :          0 :                 se_ctx->template_w4.s.opcode_minor = BIT(5);
     404                 :            :         }
     405                 :            : 
     406                 :          0 :         fctx->enc.hash_type = type;
     407                 :          0 :         se_ctx->hash_type = type;
     408                 :          0 :         fctx->enc.mac_len = mac_len;
     409                 :          0 :         se_ctx->mac_len = mac_len;
     410                 :            : 
     411         [ #  # ]:          0 :         if (key_len) {
     412                 :            :                 /*
     413                 :            :                  * Chained operation (FC opcode) requires precomputed ipad and opad hashes, but for
     414                 :            :                  * auth only (HMAC opcode) this is not required
     415                 :            :                  */
     416         [ #  # ]:          0 :                 if (chained_op) {
     417                 :          0 :                         memset(fctx->hmac.ipad, 0, sizeof(fctx->hmac.ipad));
     418                 :          0 :                         memset(fctx->hmac.opad, 0, sizeof(fctx->hmac.opad));
     419                 :          0 :                         roc_se_hmac_opad_ipad_gen(type, key, key_len, &fctx->hmac.ipad[0],
     420                 :            :                                                   ROC_SE_FC);
     421                 :          0 :                         fctx->enc.auth_input_type = 0;
     422                 :            :                 } else {
     423                 :          0 :                         se_ctx->hmac = 1;
     424                 :            : 
     425                 :          0 :                         se_ctx->auth_key = plt_zmalloc(key_len, 8);
     426         [ #  # ]:          0 :                         if (se_ctx->auth_key == NULL)
     427                 :            :                                 return -1;
     428                 :            : 
     429                 :            :                         memcpy(se_ctx->auth_key, key, key_len);
     430                 :          0 :                         se_ctx->auth_key_len = key_len;
     431                 :            :                 }
     432                 :            :         }
     433                 :            :         return 0;
     434                 :            : }
     435                 :            : 
     436                 :            : int
     437                 :          0 : roc_se_ciph_key_set(struct roc_se_ctx *se_ctx, roc_se_cipher_type type, const uint8_t *key,
     438                 :            :                     uint16_t key_len)
     439                 :            : {
     440                 :          0 :         struct roc_se_context *fctx = &se_ctx->se_ctx.fctx;
     441                 :            :         struct roc_se_pdcp_ctx *pctx;
     442                 :            :         uint8_t opcode_minor = 0;
     443                 :            :         uint32_t keyx[4];
     444                 :            :         int key_type;
     445                 :            :         int i, ret;
     446                 :            : 
     447                 :            :         /* For NULL cipher, no processing required. */
     448         [ #  # ]:          0 :         if (type == ROC_SE_PASSTHROUGH)
     449                 :            :                 return 0;
     450                 :            : 
     451                 :            :         pctx = &se_ctx->se_ctx.pctx;
     452                 :            : 
     453         [ #  # ]:          0 :         if ((type == ROC_SE_AES_GCM) || (type == ROC_SE_AES_CCM))
     454                 :          0 :                 se_ctx->template_w4.s.opcode_minor = BIT(5);
     455                 :            : 
     456                 :          0 :         ret = cpt_ciph_type_set(type, se_ctx, key_len);
     457         [ #  # ]:          0 :         if (unlikely(ret))
     458                 :            :                 return -1;
     459                 :            : 
     460         [ #  # ]:          0 :         if (se_ctx->fc_type == ROC_SE_FC_GEN) {
     461                 :            :                 /*
     462                 :            :                  * We need to always say IV is from DPTR as user can
     463                 :            :                  * sometimes override IV per operation.
     464                 :            :                  */
     465                 :          0 :                 fctx->enc.iv_source = ROC_SE_FROM_DPTR;
     466                 :            : 
     467         [ #  # ]:          0 :                 if (se_ctx->auth_key_len > 64)
     468                 :            :                         return -1;
     469                 :            :         }
     470                 :            : 
     471   [ #  #  #  #  :          0 :         switch (type) {
          #  #  #  #  #  
             #  #  #  # ]
     472                 :          0 :         case ROC_SE_DES3_CBC:
     473                 :            :                 /* CPT performs DES using 3DES with the 8B DES-key
     474                 :            :                  * replicated 2 more times to match the 24B 3DES-key.
     475                 :            :                  * Eg. If org. key is "0x0a 0x0b", then new key is
     476                 :            :                  * "0x0a 0x0b 0x0a 0x0b 0x0a 0x0b"
     477                 :            :                  */
     478         [ #  # ]:          0 :                 if (key_len == 8) {
     479                 :            :                         /* Skipping the first 8B as it will be copied
     480                 :            :                          * in the regular code flow
     481                 :            :                          */
     482                 :          0 :                         memcpy(fctx->enc.encr_key + key_len, key, key_len);
     483                 :          0 :                         memcpy(fctx->enc.encr_key + 2 * key_len, key, key_len);
     484                 :            :                 }
     485                 :            :                 break;
     486                 :          0 :         case ROC_SE_DES3_ECB:
     487                 :            :                 /* For DES3_ECB IV need to be from CTX. */
     488                 :          0 :                 fctx->enc.iv_source = ROC_SE_FROM_CTX;
     489                 :          0 :                 break;
     490                 :          0 :         case ROC_SE_AES_CBC:
     491                 :            :         case ROC_SE_AES_ECB:
     492                 :            :         case ROC_SE_AES_CFB:
     493                 :            :         case ROC_SE_AES_CTR:
     494                 :            :         case ROC_SE_CHACHA20:
     495                 :          0 :                 cpt_ciph_aes_key_type_set(fctx, key_len);
     496                 :          0 :                 break;
     497                 :          0 :         case ROC_SE_AES_GCM:
     498                 :            :         case ROC_SE_AES_CCM:
     499                 :          0 :                 cpt_ciph_aes_key_type_set(fctx, key_len);
     500                 :          0 :                 break;
     501                 :          0 :         case ROC_SE_AES_XTS:
     502                 :          0 :                 key_len = key_len / 2;
     503                 :          0 :                 cpt_ciph_aes_key_type_set(fctx, key_len);
     504                 :            : 
     505                 :            :                 /* Copy key2 for XTS into ipad */
     506                 :          0 :                 memset(fctx->hmac.ipad, 0, sizeof(fctx->hmac.ipad));
     507                 :          0 :                 memcpy(fctx->hmac.ipad, &key[key_len], key_len);
     508                 :            :                 break;
     509                 :          0 :         case ROC_SE_AES_DOCSISBPI:
     510                 :            :                 /*
     511                 :            :                  * DOCSIS uses the combination of AES-CBC and residual termination blocks that are
     512                 :            :                  * less than 128. Pass it as regular AES-CBC cipher to CPT, but keep type in
     513                 :            :                  * se_ctx as AES_DOCSISBPI to skip block size checks in instruction preparation.
     514                 :            :                  */
     515                 :          0 :                 cpt_ciph_aes_key_type_set(fctx, key_len);
     516                 :          0 :                 fctx->enc.enc_cipher = ROC_SE_AES_CBC;
     517                 :          0 :                 memcpy(fctx->enc.encr_key, key, key_len);
     518                 :          0 :                 goto success;
     519                 :            :         case ROC_SE_DES_DOCSISBPI:
     520                 :            :                 /* See case ROC_SE_DES3_CBC: for explanation */
     521         [ #  # ]:          0 :                 for (i = 0; i < 3; i++)
     522                 :          0 :                         memcpy(fctx->enc.encr_key + key_len * i, key, key_len);
     523                 :            :                 /*
     524                 :            :                  * DOCSIS uses DES-CBC mode with special handling of residual termination blocks
     525                 :            :                  * that are less than 64 bits. Pass it as regular DES-CBC, but keep type in
     526                 :            :                  * se_ctx as DES_DOCSISBPI to skip block size checks in instruction preparation.
     527                 :            :                  */
     528                 :          0 :                 fctx->enc.enc_cipher = ROC_SE_DES3_CBC;
     529                 :          0 :                 goto success;
     530                 :          0 :         case ROC_SE_SNOW3G_UEA2:
     531                 :          0 :                 pctx->w0.s.state_conf = ROC_SE_PDCP_CHAIN_CTX_KEY_IV;
     532                 :          0 :                 pctx->w0.s.cipher_type = ROC_SE_PDCP_CHAIN_ALG_TYPE_SNOW3G;
     533                 :          0 :                 pctx->w0.s.ci_key_len = key_len;
     534                 :          0 :                 cpt_snow3g_key_gen(key, keyx);
     535                 :          0 :                 memcpy(pctx->st.ci_key, keyx, key_len);
     536                 :          0 :                 se_ctx->pdcp_ci_alg = ROC_SE_PDCP_ALG_TYPE_SNOW3G;
     537                 :          0 :                 se_ctx->zsk_flags = 0;
     538                 :          0 :                 goto success;
     539                 :          0 :         case ROC_SE_ZUC_EEA3:
     540                 :          0 :                 key_type = cpt_pdcp_chain_key_type_get(key_len);
     541         [ #  # ]:          0 :                 if (key_type < 0)
     542                 :            :                         return key_type;
     543                 :          0 :                 pctx->w0.s.ci_key_len = key_type;
     544                 :          0 :                 pctx->w0.s.state_conf = ROC_SE_PDCP_CHAIN_CTX_KEY_IV;
     545                 :          0 :                 pctx->w0.s.cipher_type = ROC_SE_PDCP_CHAIN_ALG_TYPE_ZUC;
     546         [ #  # ]:          0 :                 memcpy(pctx->st.ci_key, key, key_len);
     547         [ #  # ]:          0 :                 if (key_len == 32) {
     548                 :            :                         roc_se_zuc_bytes_swap(pctx->st.ci_key, key_len);
     549                 :          0 :                         memcpy(pctx->st.ci_zuc_const, zuc_key256, 16);
     550                 :            :                 } else
     551                 :          0 :                         memcpy(pctx->st.ci_zuc_const, zuc_key128, 32);
     552                 :          0 :                 se_ctx->pdcp_ci_alg = ROC_SE_PDCP_ALG_TYPE_ZUC;
     553                 :          0 :                 se_ctx->zsk_flags = 0;
     554                 :          0 :                 goto success;
     555                 :          0 :         case ROC_SE_AES_CTR_EEA2:
     556                 :          0 :                 key_type = cpt_pdcp_chain_key_type_get(key_len);
     557         [ #  # ]:          0 :                 if (key_type < 0)
     558                 :            :                         return key_type;
     559                 :          0 :                 pctx->w0.s.ci_key_len = key_type;
     560                 :          0 :                 pctx->w0.s.state_conf = ROC_SE_PDCP_CHAIN_CTX_KEY_IV;
     561                 :          0 :                 pctx->w0.s.cipher_type = ROC_SE_PDCP_ALG_TYPE_AES_CTR;
     562                 :          0 :                 memcpy(pctx->st.ci_key, key, key_len);
     563                 :          0 :                 se_ctx->pdcp_ci_alg = ROC_SE_PDCP_ALG_TYPE_AES_CTR;
     564                 :          0 :                 se_ctx->zsk_flags = 0;
     565                 :          0 :                 goto success;
     566                 :          0 :         case ROC_SE_KASUMI_F8_ECB:
     567                 :          0 :                 se_ctx->k_ecb = 1;
     568                 :          0 :                 memcpy(se_ctx->se_ctx.k_ctx.ci_key, key, key_len);
     569                 :          0 :                 se_ctx->zsk_flags = 0;
     570                 :          0 :                 goto success;
     571                 :          0 :         case ROC_SE_KASUMI_F8_CBC:
     572                 :          0 :                 memcpy(se_ctx->se_ctx.k_ctx.ci_key, key, key_len);
     573                 :          0 :                 se_ctx->zsk_flags = 0;
     574                 :          0 :                 goto success;
     575                 :            :         default:
     576                 :            :                 return -1;
     577                 :            :         }
     578                 :            : 
     579                 :            :         /* Only for ROC_SE_FC_GEN case */
     580                 :            : 
     581                 :            :         /* For GMAC auth, cipher must be NULL */
     582         [ #  # ]:          0 :         if (se_ctx->hash_type != ROC_SE_GMAC_TYPE)
     583                 :          0 :                 fctx->enc.enc_cipher = type;
     584                 :            : 
     585                 :          0 :         memcpy(fctx->enc.encr_key, key, key_len);
     586                 :            : 
     587                 :          0 : success:
     588                 :          0 :         se_ctx->enc_cipher = type;
     589         [ #  # ]:          0 :         if (se_ctx->fc_type == ROC_SE_PDCP_CHAIN) {
     590         [ #  # ]:          0 :                 se_ctx->template_w4.s.opcode_minor = se_ctx->ciph_then_auth ? 2 : 3;
     591         [ #  # ]:          0 :         } else if (se_ctx->fc_type == ROC_SE_PDCP) {
     592         [ #  # ]:          0 :                 if (roc_model_is_cn9k())
     593                 :          0 :                         opcode_minor =
     594                 :          0 :                                 ((1 << 7) | (se_ctx->pdcp_ci_alg << 5) | (se_ctx->zsk_flags & 0x7));
     595                 :            :                 else
     596                 :            :                         opcode_minor = ((1 << 4));
     597                 :          0 :                 se_ctx->template_w4.s.opcode_minor = opcode_minor;
     598                 :            :         }
     599                 :            :         return 0;
     600                 :            : }
     601                 :            : 
     602                 :            : void
     603                 :          0 : roc_se_ctx_init(struct roc_se_ctx *roc_se_ctx)
     604                 :            : {
     605                 :          0 :         struct se_ctx_s *ctx = &roc_se_ctx->se_ctx;
     606                 :            :         uint64_t ctx_len, *uc_ctx;
     607                 :            :         uint8_t i;
     608                 :            : 
     609         [ #  # ]:          0 :         switch (roc_se_ctx->fc_type) {
     610                 :            :         case ROC_SE_FC_GEN:
     611                 :            :                 ctx_len = sizeof(struct roc_se_context);
     612                 :            :                 break;
     613                 :            :         case ROC_SE_PDCP_CHAIN:
     614                 :            :         case ROC_SE_PDCP:
     615                 :            :                 ctx_len = sizeof(struct roc_se_pdcp_ctx);
     616                 :            :                 break;
     617                 :            :         case ROC_SE_KASUMI:
     618                 :            :                 ctx_len = sizeof(struct roc_se_kasumi_ctx);
     619                 :            :                 break;
     620                 :            :         case ROC_SE_SM:
     621                 :            :                 ctx_len = sizeof(struct roc_se_sm_context);
     622                 :            :                 break;
     623                 :            :         default:
     624                 :            :                 ctx_len = 0;
     625                 :            :         }
     626                 :            : 
     627                 :          0 :         ctx_len = PLT_ALIGN_CEIL(ctx_len, 8);
     628                 :            : 
     629                 :            :         /* Skip w0 for swap */
     630                 :          0 :         uc_ctx = PLT_PTR_ADD(ctx, sizeof(ctx->w0));
     631         [ #  # ]:          0 :         for (i = 0; i < (ctx_len / 8); i++)
     632         [ #  # ]:          0 :                 uc_ctx[i] = plt_cpu_to_be_64(((uint64_t *)uc_ctx)[i]);
     633                 :            : 
     634                 :            :         /* Include w0 */
     635                 :            :         ctx_len += sizeof(ctx->w0);
     636                 :          0 :         ctx_len = PLT_ALIGN_CEIL(ctx_len, 8);
     637                 :            : 
     638                 :          0 :         ctx->w0.s.aop_valid = 1;
     639                 :          0 :         ctx->w0.s.ctx_hdr_size = 0;
     640                 :            : 
     641                 :          0 :         ctx->w0.s.ctx_size = PLT_ALIGN_FLOOR(ctx_len, 128);
     642                 :            :         if (ctx->w0.s.ctx_size == 0)
     643                 :          0 :                 ctx->w0.s.ctx_size = 1;
     644                 :            : 
     645                 :          0 :         ctx->w0.s.ctx_push_size = ctx_len / 8;
     646                 :            :         if (ctx->w0.s.ctx_push_size > 32)
     647                 :            :                 ctx->w0.s.ctx_push_size = 32;
     648                 :          0 : }

Generated by: LCOV version 1.14